kdeconnect-kde/plugins/findthisdevice/findthisdeviceplugin.cpp
Alexander Lohnau 2e67f95017 Add explicit moc includes to cpp files
The rationale is explained in https://planet.kde.org/friedrich-kossebau-2023-06-28-include-also-moc-files-of-headers/

In case of KDEConnect, it impressively speeds up compilation. Before it
took 390 seconds on a clean build and with this change it took 330 seconds.
This is due to the mocs_compilation having to include the header files
and thus all their headers. Due to the lots of small plugins we have,
this means that the same headers must be compiled plenty of times.
When we include the moc files directly in the C++ file, they are already
available.
2023-07-30 07:27:45 +00:00

97 lines
2.7 KiB
C++

/**
* SPDX-FileCopyrightText: 2018 Friedrich W. H. Kossebau <kossebau@kde.org>
* SPDX-FileCopyrightText: 2019 Piyush Aggarwal <piyushaggarwal002@gmail.com>
*
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
*/
#include "findthisdeviceplugin.h"
// KF
#include <KPluginFactory>
#ifndef Q_OS_WIN
#include <PulseAudioQt/Context>
#include <PulseAudioQt/Sink>
#endif
// Qt
#include "plugin_findthisdevice_debug.h"
#include <QDBusConnection>
#include <QMediaPlayer>
#if QT_VERSION_MAJOR == 6
#include <QAudioOutput>
#endif
K_PLUGIN_CLASS_WITH_JSON(FindThisDevicePlugin, "kdeconnect_findthisdevice.json")
FindThisDevicePlugin::FindThisDevicePlugin(QObject *parent, const QVariantList &args)
: KdeConnectPlugin(parent, args)
{
}
bool FindThisDevicePlugin::receivePacket(const NetworkPacket &np)
{
Q_UNUSED(np);
const QString soundFile = config()->getString(QStringLiteral("ringtone"), defaultSound());
const QUrl soundURL = QUrl::fromLocalFile(soundFile);
if (soundURL.isEmpty()) {
qCWarning(KDECONNECT_PLUGIN_FINDTHISDEVICE) << "Not playing sound, no valid ring tone specified.";
return true;
}
QMediaPlayer *player = new QMediaPlayer;
#if QT_VERSION_MAJOR < 6
player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
player->setMedia(soundURL);
player->setVolume(100);
#else
auto audioOutput = new QAudioOutput();
audioOutput->setVolume(100);
player->setSource(soundURL);
player->setAudioOutput(audioOutput);
connect(player, &QMediaPlayer::playingChanged, player, &QObject::deleteLater);
#endif
player->play();
#ifndef Q_OS_WIN
const auto sinks = PulseAudioQt::Context::instance()->sinks();
QVector<PulseAudioQt::Sink *> mutedSinks;
for (auto sink : sinks) {
if (sink->isMuted()) {
sink->setMuted(false);
mutedSinks.append(sink);
}
}
#if QT_VERSION_MAJOR < 6
connect(player, &QMediaPlayer::stateChanged, this, [mutedSinks] {
#else
connect(player, &QMediaPlayer::playingChanged, this, [mutedSinks] {
#endif
for (auto sink : qAsConst(mutedSinks)) {
sink->setMuted(true);
}
});
#endif
player->play();
#if QT_VERSION_MAJOR < 6
connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
#else
connect(player, &QMediaPlayer::playingChanged, player, &QObject::deleteLater);
#endif
// TODO: ensure to use built-in loudspeakers
return true;
}
QString FindThisDevicePlugin::dbusPath() const
{
return QStringLiteral("/modules/kdeconnect/devices/") + device()->id() + QStringLiteral("/findthisdevice");
}
#include "findthisdeviceplugin.moc"
#include "moc_findthisdeviceplugin.cpp"