2018-03-29 00:46:12 +01:00
|
|
|
/**
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
|
2018-03-29 00:46:12 +01:00
|
|
|
*
|
2020-08-17 10:48:10 +01:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
|
2018-03-29 00:46:12 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "findthisdevice_config.h"
|
2019-03-21 22:26:32 +00:00
|
|
|
#include "findthisdeviceplugin.h"
|
2018-03-29 00:46:12 +01:00
|
|
|
|
|
|
|
// KF
|
|
|
|
#include <KLocalizedString>
|
|
|
|
#include <KPluginFactory>
|
|
|
|
// Qt
|
2019-04-30 17:24:09 +01:00
|
|
|
#include <QMediaPlayer>
|
2022-09-10 22:23:52 +01:00
|
|
|
#include <QStandardPaths>
|
2018-03-29 00:46:12 +01:00
|
|
|
|
2023-07-20 09:14:07 +01:00
|
|
|
#if QT_VERSION_MAJOR == 6
|
|
|
|
#include <QAudioOutput>
|
|
|
|
#endif
|
|
|
|
|
2023-07-21 11:42:38 +01:00
|
|
|
K_PLUGIN_CLASS(FindThisDeviceConfig)
|
2018-03-29 00:46:12 +01:00
|
|
|
|
2023-07-21 11:26:19 +01:00
|
|
|
FindThisDeviceConfig::FindThisDeviceConfig(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
|
|
|
|
: KdeConnectPluginKcm(parent, data, args)
|
2018-03-29 00:46:12 +01:00
|
|
|
{
|
2023-07-26 09:40:17 +01:00
|
|
|
m_ui.setupUi(widget());
|
2018-03-29 00:46:12 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
const QStringList soundDirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("sounds"), QStandardPaths::LocateDirectory);
|
2018-03-29 00:46:12 +01:00
|
|
|
if (!soundDirs.isEmpty()) {
|
2023-07-26 09:40:17 +01:00
|
|
|
m_ui.soundFileRequester->setStartDir(QUrl::fromLocalFile(soundDirs.last()));
|
2018-03-29 00:46:12 +01:00
|
|
|
}
|
|
|
|
|
2023-07-26 09:40:17 +01:00
|
|
|
connect(m_ui.playSoundButton, &QToolButton::clicked, this, [this]() {
|
|
|
|
if (const QUrl soundUrl = m_ui.soundFileRequester->url(); soundUrl.isValid()) {
|
2023-07-20 14:59:08 +01:00
|
|
|
playSound(soundUrl);
|
|
|
|
}
|
|
|
|
});
|
2023-07-26 09:40:17 +01:00
|
|
|
connect(m_ui.soundFileRequester, &KUrlRequester::textChanged, this, &FindThisDeviceConfig::markAsChanged);
|
2018-03-29 00:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindThisDeviceConfig::defaults()
|
|
|
|
{
|
|
|
|
KCModule::defaults();
|
|
|
|
|
2023-07-26 09:40:17 +01:00
|
|
|
m_ui.soundFileRequester->setText(defaultSound());
|
2023-04-17 18:10:08 +01:00
|
|
|
markAsChanged();
|
2018-03-29 00:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindThisDeviceConfig::load()
|
|
|
|
{
|
|
|
|
KCModule::load();
|
|
|
|
|
2019-10-27 17:08:51 +00:00
|
|
|
const QString ringTone = config()->getString(QStringLiteral("ringtone"), defaultSound());
|
2023-07-26 09:40:17 +01:00
|
|
|
m_ui.soundFileRequester->setText(ringTone);
|
2018-03-29 00:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FindThisDeviceConfig::save()
|
|
|
|
{
|
2023-07-26 09:40:17 +01:00
|
|
|
config()->set(QStringLiteral("ringtone"), m_ui.soundFileRequester->text());
|
2018-03-29 00:46:12 +01:00
|
|
|
|
|
|
|
KCModule::save();
|
|
|
|
}
|
|
|
|
|
2023-07-20 14:59:08 +01:00
|
|
|
void FindThisDeviceConfig::playSound(const QUrl &soundUrl)
|
2018-03-29 00:46:12 +01:00
|
|
|
{
|
2023-07-20 14:59:08 +01:00
|
|
|
QMediaPlayer *player = new QMediaPlayer;
|
2023-07-20 09:14:07 +01:00
|
|
|
#if QT_VERSION_MAJOR < 6
|
2023-07-20 14:59:08 +01:00
|
|
|
player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
|
|
|
|
player->setMedia(soundUrl);
|
|
|
|
player->setVolume(100);
|
|
|
|
player->play();
|
|
|
|
connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
|
2023-07-20 09:14:07 +01:00
|
|
|
#else
|
2023-07-20 14:59:08 +01:00
|
|
|
auto audioOutput = new QAudioOutput();
|
|
|
|
audioOutput->setVolume(100);
|
|
|
|
player->setSource(soundUrl);
|
|
|
|
player->setAudioOutput(audioOutput);
|
|
|
|
player->play();
|
|
|
|
connect(player, &QMediaPlayer::playingChanged, player, &QObject::deleteLater);
|
2023-07-20 09:14:07 +01:00
|
|
|
#endif
|
2018-03-29 00:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "findthisdevice_config.moc"
|
2023-07-26 09:15:11 +01:00
|
|
|
#include "moc_findthisdevice_config.cpp"
|