[OpenDocString] kdeconnect-kde (cpp)
findthisdeviceplugin.cpp
FindThisDevicePlugin::FindThisDevicePlugin(QObject *parent, const QVariantList &args)
    : KdeConnectPlugin(parent, args)
{
}
This method builds a KdeConnectPlugin object and assigns it to the given arguments.
FindThisDevicePlugin::~FindThisDevicePlugin() = default;
This method is used to disable the FindThisDevicePlugin singleton instance for this device plugin type.
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;
    player->setAudioRole(QAudio::Role(QAudio::NotificationRole));
    player->setMedia(soundURL);
    player->setVolume(100);

#ifndef Q_OS_WIN
    const auto sinks = PulseAudioQt::Context::instance()->sinks();
    QVector mutedSinks;
    for (auto sink : sinks) {
        if (sink->isMuted()) {
            sink->setMuted(false);
            mutedSinks.append(sink);
        }
    }
    connect(player, &QMediaPlayer::stateChanged, this, [player, mutedSinks] {
        for (auto sink : qAsConst(mutedSinks)) {
            sink->setMuted(true);
        }
    });
#endif

    player->play();
    connect(player, &QMediaPlayer::stateChanged, player, &QObject::deleteLater);
    // TODO: ensure to use built-in loudspeakers

    return true;
}
This method creates a QMediaPlayer object from a NetworkPacket object. It takes the ringtone config from the plugin. It constructs the sound file, sets the volume 100% of the sound and sets the muted state to true. It also connects the stateChanged signal to the stateChanged signal.
QString FindThisDevicePlugin::dbusPath() const
{
    return QStringLiteral("/modules/kdeconnect/devices/") + device()->id() + QStringLiteral("/findthisdevice");
}
Returns the dbus path as a QString.