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

0001 /**
0002  * SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
0003  * SPDX-FileCopyrightText: 2019 Piyush Aggarwal <piyushaggarwal002@gmail.com>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006  */
0007 
0008 #include "findthisdeviceplugin.h"
0009 
0010 // KF
0011 #include <KPluginFactory>
0012 
0013 #ifndef Q_OS_WIN
0014 #include <PulseAudioQt/Context>
0015 #include <PulseAudioQt/Sink>
0016 #endif
0017 
0018 // Qt
0019 #include "plugin_findthisdevice_debug.h"
0020 #include <QDBusConnection>
0021 #include <QMediaPlayer>
0022 
0023 #if QT_VERSION_MAJOR == 6
0024 #include <QAudioOutput>
0025 #endif
0026 
0027 K_PLUGIN_CLASS_WITH_JSON(FindThisDevicePlugin, "kdeconnect_findthisdevice.json")
0028 
0029 void FindThisDevicePlugin::receivePacket(const NetworkPacket & /*np*/)
0030 {
0031     const QString soundFile = config()->getString(QStringLiteral("ringtone"), defaultSound());
0032     const QUrl soundURL = QUrl::fromLocalFile(soundFile);
0033 
0034     if (soundURL.isEmpty()) {
0035         qCWarning(KDECONNECT_PLUGIN_FINDTHISDEVICE) << "Not playing sound, no valid ring tone specified.";
0036         return;
0037     }
0038 
0039     QMediaPlayer *player = new QMediaPlayer;
0040 #if QT_VERSION_MAJOR < 6
0041     player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
0042     player->setMedia(soundURL);
0043     player->setVolume(100);
0044 #else
0045     auto audioOutput = new QAudioOutput();
0046     audioOutput->setVolume(100);
0047     player->setSource(soundURL);
0048     player->setAudioOutput(audioOutput);
0049     connect(player, &QMediaPlayer::playingChanged, player, &QObject::deleteLater);
0050 #endif
0051     player->play();
0052 
0053 #ifndef Q_OS_WIN
0054     const auto sinks = PulseAudioQt::Context::instance()->sinks();
0055     QVector<PulseAudioQt::Sink *> mutedSinks;
0056     for (auto sink : sinks) {
0057         if (sink->isMuted()) {
0058             sink->setMuted(false);
0059             mutedSinks.append(sink);
0060         }
0061     }
0062 #if QT_VERSION_MAJOR < 6
0063     connect(player, &QMediaPlayer::stateChanged, this, [mutedSinks] {
0064 #else
0065     connect(player, &QMediaPlayer::playingChanged, this, [mutedSinks] {
0066 #endif
0067         for (auto sink : qAsConst(mutedSinks)) {
0068             sink->setMuted(true);
0069         }
0070     });
0071 #endif
0072 
0073     player->play();
0074 #if QT_VERSION_MAJOR < 6
0075     connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
0076 #else
0077     connect(player, &QMediaPlayer::playingChanged, player, &QObject::deleteLater);
0078 #endif
0079     // TODO: ensure to use built-in loudspeakers
0080 }
0081 
0082 QString FindThisDevicePlugin::dbusPath() const
0083 {
0084     return QLatin1String("/modules/kdeconnect/devices/%1/findthisdevice").arg(device()->id());
0085 }
0086 
0087 #include "findthisdeviceplugin.moc"
0088 #include "moc_findthisdeviceplugin.cpp"