[OpenDocString] kdeconnect-kde (cpp)
pausemusicplugin.cpp
PauseMusicPlugin::PauseMusicPlugin(QObject *parent, const QVariantList &args)
    : KdeConnectPlugin(parent, args)
    , mutedSinks()
{
}
Constructs a plugin object and sets up muted sinks.
bool PauseMusicPlugin::receivePacket(const NetworkPacket &np)
{
    bool pauseOnlyWhenTalking = config()->getBool(QStringLiteral("conditionTalking"), false);

    if (pauseOnlyWhenTalking) {
        if (np.get(QStringLiteral("event")) != QLatin1String("talking")) {
            return true;
        }
    } else { // Pause as soon as it rings
        if (np.get(QStringLiteral("event")) != QLatin1String("ringing") && np.get(QStringLiteral("event")) != QLatin1String("talking")) {
            return true;
        }
    }

    bool pauseConditionFulfilled = !np.get(QStringLiteral("isCancel"));

    bool pause = config()->getBool(QStringLiteral("actionPause"), true);
    bool mute = config()->getBool(QStringLiteral("actionMute"), false);

    const bool autoResume = config()->getBool(QStringLiteral("actionResume"), true);

    if (pauseConditionFulfilled) {
        if (mute) {
            qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Muting system volume";
            const auto sinks = PulseAudioQt::Context::instance()->sinks();
            for (const auto sink : sinks) {
                if (!sink->isMuted()) {
                    sink->setMuted(true);
                    mutedSinks.insert(sink->name());
                }
            }
        }

        if (pause) {
            // Search for interfaces currently playing
            const QStringList interfaces = QDBusConnection::sessionBus().interface()->registeredServiceNames().value();
            for (const QString &iface : interfaces) {
                if (iface.startsWith(QLatin1String("org.mpris.MediaPlayer2"))) {
                    OrgMprisMediaPlayer2PlayerInterface mprisInterface(iface, QStringLiteral("/org/mpris/MediaPlayer2"), QDBusConnection::sessionBus());
                    QString status = mprisInterface.playbackStatus();
                    if (status == QLatin1String("Playing")) {
                        if (!pausedSources.contains(iface)) {
                            pausedSources.insert(iface);
                            if (mprisInterface.canPause()) {
                                mprisInterface.Pause();
                            } else {
                                mprisInterface.Stop();
                            }
                        }
                    }
                }
            }
        }

    } else {
        if (mute) {
            qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Unmuting system volume";

            if (autoResume) {
                const auto sinks = PulseAudioQt::Context::instance()->sinks();
                for (const auto sink : sinks) {
                    if (mutedSinks.contains(sink->name())) {
                        sink->setMuted(false);
                    }
                }
            }
            mutedSinks.clear();
        }

        if (pause && !pausedSources.empty()) {
            if (autoResume) {
                for (const QString &iface : qAsConst(pausedSources)) {
                    OrgMprisMediaPlayer2PlayerInterface mprisInterface(iface, QStringLiteral("/org/mpris/MediaPlayer2"), QDBusConnection::sessionBus());
                    mprisInterface.Play();
                }
            }
            pausedSources.clear();
        }
    }

    return true;
}
This method receives a NetworkPacket object np and sets the value of the "pauseOnlyWhenTalking" parameter to true if the packet is pause-only and is not ringing, and if the condition fulfilled is true, the function returns true. If the condition fulfills, the function returns true. If the condition is not fulfilled, the function returns true. If the condition is fulfilled, the function returns true. If the condition is fulfilled, the function returns true. If the condition is not fulfilled, the function creates one of the "mute" and "autoResume" parameters are set to true, the function shows the action of the plugin as the resume-pause and mute events, which can be performed on each interface in the list, which will play the system volume.