File indexing completed on 2024-04-14 04:51:45

0001 /**
0002  * SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "findthisdevice_config.h"
0008 #include "findthisdeviceplugin.h"
0009 
0010 // KF
0011 #include <KLocalizedString>
0012 #include <KPluginFactory>
0013 // Qt
0014 #include <QMediaPlayer>
0015 #include <QStandardPaths>
0016 
0017 #if QT_VERSION_MAJOR == 6
0018 #include <QAudioOutput>
0019 #endif
0020 
0021 K_PLUGIN_CLASS(FindThisDeviceConfig)
0022 
0023 FindThisDeviceConfig::FindThisDeviceConfig(QObject *parent, const KPluginMetaData &data, const QVariantList &args)
0024     : KdeConnectPluginKcm(parent, data, args)
0025 {
0026     m_ui.setupUi(widget());
0027 
0028     const QStringList soundDirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("sounds"), QStandardPaths::LocateDirectory);
0029     if (!soundDirs.isEmpty()) {
0030         m_ui.soundFileRequester->setStartDir(QUrl::fromLocalFile(soundDirs.last()));
0031     }
0032 
0033     connect(m_ui.playSoundButton, &QToolButton::clicked, this, [this]() {
0034         if (const QUrl soundUrl = m_ui.soundFileRequester->url(); soundUrl.isValid()) {
0035             playSound(soundUrl);
0036         }
0037     });
0038     connect(m_ui.soundFileRequester, &KUrlRequester::textChanged, this, &FindThisDeviceConfig::markAsChanged);
0039 }
0040 
0041 void FindThisDeviceConfig::defaults()
0042 {
0043     KCModule::defaults();
0044 
0045     m_ui.soundFileRequester->setText(defaultSound());
0046     markAsChanged();
0047 }
0048 
0049 void FindThisDeviceConfig::load()
0050 {
0051     KCModule::load();
0052 
0053     const QString ringTone = config()->getString(QStringLiteral("ringtone"), defaultSound());
0054     m_ui.soundFileRequester->setText(ringTone);
0055 }
0056 
0057 void FindThisDeviceConfig::save()
0058 {
0059     config()->set(QStringLiteral("ringtone"), m_ui.soundFileRequester->text());
0060 
0061     KCModule::save();
0062 }
0063 
0064 void FindThisDeviceConfig::playSound(const QUrl &soundUrl)
0065 {
0066     QMediaPlayer *player = new QMediaPlayer;
0067 #if QT_VERSION_MAJOR < 6
0068     player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
0069     player->setMedia(soundUrl);
0070     player->setVolume(100);
0071     player->play();
0072     connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
0073 #else
0074     auto audioOutput = new QAudioOutput();
0075     audioOutput->setVolume(100);
0076     player->setSource(soundUrl);
0077     player->setAudioOutput(audioOutput);
0078     player->play();
0079     connect(player, &QMediaPlayer::playingChanged, player, &QObject::deleteLater);
0080 #endif
0081 }
0082 
0083 #include "findthisdevice_config.moc"
0084 #include "moc_findthisdevice_config.cpp"