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>
|
|
|
|
* SPDX-FileCopyrightText: 2019 Piyush Aggarwal <piyushaggarwal002@gmail.com>
|
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 "findthisdeviceplugin.h"
|
|
|
|
|
|
|
|
// KF
|
|
|
|
#include <KPluginFactory>
|
2019-05-14 21:16:41 +01:00
|
|
|
|
|
|
|
#ifndef Q_OS_WIN
|
2019-05-13 23:36:29 +01:00
|
|
|
#include <PulseAudioQt/Context>
|
|
|
|
#include <PulseAudioQt/Sink>
|
2019-05-14 21:16:41 +01:00
|
|
|
#endif
|
2019-09-19 23:45:39 +01:00
|
|
|
|
2018-03-29 00:46:12 +01:00
|
|
|
// Qt
|
2022-09-10 22:23:52 +01:00
|
|
|
#include "plugin_findthisdevice_debug.h"
|
2018-03-29 00:46:12 +01:00
|
|
|
#include <QDBusConnection>
|
2019-04-30 17:24:09 +01:00
|
|
|
#include <QMediaPlayer>
|
2018-03-29 00:46:12 +01:00
|
|
|
|
2019-06-12 21:16:54 +01:00
|
|
|
K_PLUGIN_CLASS_WITH_JSON(FindThisDevicePlugin, "kdeconnect_findthisdevice.json")
|
2018-03-29 00:46:12 +01:00
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
FindThisDevicePlugin::FindThisDevicePlugin(QObject *parent, const QVariantList &args)
|
2018-03-29 00:46:12 +01:00
|
|
|
: KdeConnectPlugin(parent, args)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
FindThisDevicePlugin::~FindThisDevicePlugin() = default;
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
bool FindThisDevicePlugin::receivePacket(const NetworkPacket &np)
|
2018-03-29 00:46:12 +01:00
|
|
|
{
|
|
|
|
Q_UNUSED(np);
|
2019-10-27 17:08:51 +00:00
|
|
|
|
|
|
|
const QString soundFile = config()->getString(QStringLiteral("ringtone"), defaultSound());
|
2020-05-14 14:06:49 +01:00
|
|
|
const QUrl soundURL = QUrl::fromLocalFile(soundFile);
|
2019-10-27 17:08:51 +00:00
|
|
|
|
2018-03-29 00:46:12 +01:00
|
|
|
if (soundURL.isEmpty()) {
|
2019-09-19 23:45:39 +01:00
|
|
|
qCWarning(KDECONNECT_PLUGIN_FINDTHISDEVICE) << "Not playing sound, no valid ring tone specified.";
|
2018-03-29 00:46:12 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-09-10 22:23:52 +01:00
|
|
|
QMediaPlayer *player = new QMediaPlayer;
|
2019-04-30 17:24:09 +01:00
|
|
|
player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
|
|
|
|
player->setMedia(soundURL);
|
|
|
|
player->setVolume(100);
|
2019-05-13 23:36:29 +01:00
|
|
|
|
2019-05-14 21:16:41 +01:00
|
|
|
#ifndef Q_OS_WIN
|
2019-05-13 23:36:29 +01:00
|
|
|
const auto sinks = PulseAudioQt::Context::instance()->sinks();
|
2022-09-10 22:23:52 +01:00
|
|
|
QVector<PulseAudioQt::Sink *> mutedSinks;
|
2019-05-13 23:36:29 +01:00
|
|
|
for (auto sink : sinks) {
|
|
|
|
if (sink->isMuted()) {
|
|
|
|
sink->setMuted(false);
|
|
|
|
mutedSinks.append(sink);
|
|
|
|
}
|
|
|
|
}
|
2022-09-10 22:23:52 +01:00
|
|
|
connect(player, &QMediaPlayer::stateChanged, this, [player, mutedSinks] {
|
2019-05-13 23:36:29 +01:00
|
|
|
for (auto sink : qAsConst(mutedSinks)) {
|
|
|
|
sink->setMuted(true);
|
|
|
|
}
|
|
|
|
});
|
2019-05-14 21:16:41 +01:00
|
|
|
#endif
|
2019-05-13 23:36:29 +01:00
|
|
|
|
2019-09-19 23:45:39 +01:00
|
|
|
player->play();
|
|
|
|
connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
|
2018-03-29 00:46:12 +01:00
|
|
|
// TODO: ensure to use built-in loudspeakers
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FindThisDevicePlugin::dbusPath() const
|
|
|
|
{
|
2019-06-10 15:40:28 +01:00
|
|
|
return QStringLiteral("/modules/kdeconnect/devices/") + device()->id() + QStringLiteral("/findthisdevice");
|
2018-03-29 00:46:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "findthisdeviceplugin.moc"
|