Changeset View
Changeset View
Standalone View
Standalone View
plugins/pausemusic/pausemusicplugin.cpp
Show All 20 Lines | |||||
21 | #include "pausemusicplugin.h" | 21 | #include "pausemusicplugin.h" | ||
22 | 22 | | |||
23 | #include <QDBusConnection> | 23 | #include <QDBusConnection> | ||
24 | #include <QDBusInterface> | 24 | #include <QDBusInterface> | ||
25 | #include <QDBusConnectionInterface> | 25 | #include <QDBusConnectionInterface> | ||
26 | #include <QDBusMessage> | 26 | #include <QDBusMessage> | ||
27 | #include <QDBusReply> | 27 | #include <QDBusReply> | ||
28 | #include <QDebug> | 28 | #include <QDebug> | ||
29 | #include <QProcess> | ||||
29 | 30 | | |||
30 | #include <KPluginFactory> | 31 | #include <KPluginFactory> | ||
31 | 32 | | |||
32 | K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_pausemusic.json", registerPlugin< PauseMusicPlugin >(); ) | 33 | K_PLUGIN_FACTORY_WITH_JSON( KdeConnectPluginFactory, "kdeconnect_pausemusic.json", registerPlugin< PauseMusicPlugin >(); ) | ||
33 | 34 | | |||
34 | //TODO: Port this away from KMix to use only Pulseaudio | 35 | Q_LOGGING_CATEGORY(KDECONNECT_PLUGIN_PAUSEMUSIC, "kdeconnect.plugin.pausemusic") | ||
35 | int PauseMusicPlugin::isKMixMuted() { | | |||
36 | QDBusInterface kmixInterface(QStringLiteral("org.kde.kmix"), QStringLiteral("/Mixers"), QStringLiteral("org.kde.KMix.MixSet")); | | |||
37 | QString mixer = kmixInterface.property("currentMasterMixer").toString(); | | |||
38 | QString control = kmixInterface.property("currentMasterControl").toString(); | | |||
39 | | ||||
40 | if (mixer.isEmpty() || control.isEmpty()) | | |||
41 | return -1; | | |||
42 | | ||||
43 | mixer.replace(':','_'); | | |||
44 | mixer.replace('.','_'); | | |||
45 | mixer.replace('-','_'); | | |||
46 | control.replace(':','_'); | | |||
47 | control.replace('.','_'); | | |||
48 | control.replace('-','_'); | | |||
49 | | ||||
50 | QDBusInterface mixerInterface(QStringLiteral("org.kde.kmix"), "/Mixers/"+mixer+"/"+control, QStringLiteral("org.kde.KMix.Control")); | | |||
51 | if (mixerInterface.property("mute").toBool()) return 1; | | |||
52 | return (mixerInterface.property("volume").toInt() == 0); | | |||
53 | } | | |||
54 | 36 | | |||
55 | PauseMusicPlugin::PauseMusicPlugin(QObject* parent, const QVariantList& args) | 37 | PauseMusicPlugin::PauseMusicPlugin(QObject* parent, const QVariantList& args) | ||
56 | : KdeConnectPlugin(parent, args) | 38 | : KdeConnectPlugin(parent, args) | ||
57 | , muted(false) | 39 | , muted(false) | ||
58 | { | 40 | {} | ||
59 | QDBusInterface kmixInterface(QStringLiteral("org.kde.kmix"), QStringLiteral("/kmix/KMixWindow/actions/mute"), QStringLiteral("org.qtproject.Qt.QAction")); | | |||
60 | } | | |||
61 | 41 | | |||
62 | bool PauseMusicPlugin::receivePackage(const NetworkPackage& np) | 42 | bool PauseMusicPlugin::receivePackage(const NetworkPackage& np) | ||
63 | { | 43 | { | ||
64 | bool pauseOnlyWhenTalking = config()->get(QStringLiteral("conditionTalking"), false); | 44 | bool pauseOnlyWhenTalking = config()->get(QStringLiteral("conditionTalking"), false); | ||
65 | 45 | | |||
66 | if (pauseOnlyWhenTalking) { | 46 | if (pauseOnlyWhenTalking) { | ||
67 | if (np.get<QString>(QStringLiteral("event")) != QLatin1String("talking")) { | 47 | if (np.get<QString>(QStringLiteral("event")) != QLatin1String("talking")) { | ||
68 | return true; | 48 | return true; | ||
69 | } | 49 | } | ||
70 | } else { //Pause as soon as it rings | 50 | } else { //Pause as soon as it rings | ||
71 | if (np.get<QString>(QStringLiteral("event")) != QLatin1String("ringing") && np.get<QString>(QStringLiteral("event")) != QLatin1String("talking")) { | 51 | if (np.get<QString>(QStringLiteral("event")) != QLatin1String("ringing") && np.get<QString>(QStringLiteral("event")) != QLatin1String("talking")) { | ||
72 | return true; | 52 | return true; | ||
73 | } | 53 | } | ||
74 | } | 54 | } | ||
75 | 55 | | |||
76 | bool pauseConditionFulfilled = !np.get<bool>(QStringLiteral("isCancel")); | 56 | bool pauseConditionFulfilled = !np.get<bool>(QStringLiteral("isCancel")); | ||
77 | 57 | | |||
78 | bool pause = config()->get(QStringLiteral("actionPause"), true); | 58 | bool pause = config()->get(QStringLiteral("actionPause"), true); | ||
79 | bool mute = config()->get(QStringLiteral("actionMute"), false); | 59 | bool mute = config()->get(QStringLiteral("actionMute"), false); | ||
80 | 60 | | |||
81 | if (pauseConditionFulfilled) { | 61 | if (pauseConditionFulfilled) { | ||
82 | 62 | | |||
83 | if (mute) { | 63 | if (mute) { | ||
84 | QDBusInterface kmixInterface(QStringLiteral("org.kde.kmix"), QStringLiteral("/kmix/KMixWindow/actions/mute"), QStringLiteral("org.qtproject.Qt.QAction")); | 64 | qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Muting system volume"; | ||
85 | if (isKMixMuted() == 0) { | 65 | QProcess::startDetached("pactl set-sink-mute @DEFAULT_SINK@ 1"); | ||
86 | muted = true; | 66 | muted = true; | ||
thomasp: This function is static. | |||||
87 | kmixInterface.call(QStringLiteral("trigger")); | | |||
88 | } | | |||
89 | } | 67 | } | ||
90 | 68 | | |||
91 | if (pause) { | 69 | if (pause) { | ||
92 | //Search for interfaces currently playing | 70 | //Search for interfaces currently playing | ||
93 | const QStringList interfaces = QDBusConnection::sessionBus().interface()->registeredServiceNames().value(); | 71 | const QStringList interfaces = QDBusConnection::sessionBus().interface()->registeredServiceNames().value(); | ||
94 | for (const QString& iface : interfaces) { | 72 | for (const QString& iface : interfaces) { | ||
95 | if (iface.startsWith(QLatin1String("org.mpris.MediaPlayer2"))) { | 73 | if (iface.startsWith(QLatin1String("org.mpris.MediaPlayer2"))) { | ||
96 | QDBusInterface mprisInterface(iface, QStringLiteral("/org/mpris/MediaPlayer2"), QStringLiteral("org.mpris.MediaPlayer2.Player")); | 74 | QDBusInterface mprisInterface(iface, QStringLiteral("/org/mpris/MediaPlayer2"), QStringLiteral("org.mpris.MediaPlayer2.Player")); | ||
Show All 10 Lines | |||||
107 | } | 85 | } | ||
108 | } | 86 | } | ||
109 | } | 87 | } | ||
110 | } | 88 | } | ||
111 | 89 | | |||
112 | } else { | 90 | } else { | ||
113 | 91 | | |||
114 | if (mute && muted) { | 92 | if (mute && muted) { | ||
115 | QDBusInterface kmixInterface(QStringLiteral("org.kde.kmix"), QStringLiteral("/kmix/KMixWindow/actions/mute"), QStringLiteral("org.qtproject.Qt.QAction")); | 93 | | ||
116 | if (isKMixMuted() > 0) { | 94 | qCDebug(KDECONNECT_PLUGIN_PAUSEMUSIC) << "Unmuting system volume"; | ||
117 | kmixInterface.call(QStringLiteral("trigger")); | 95 | QProcess::startDetached("pactl set-sink-mute @DEFAULT_SINK@ 0"); | ||
118 | } | 96 | | ||
119 | muted = false; | 97 | muted = false; | ||
120 | } | 98 | } | ||
121 | 99 | | |||
122 | if (pause && !pausedSources.empty()) { | 100 | if (pause && !pausedSources.empty()) { | ||
123 | for (const QString& iface : qAsConst(pausedSources)) { | 101 | for (const QString& iface : qAsConst(pausedSources)) { | ||
124 | QDBusInterface mprisInterface(iface, QStringLiteral("/org/mpris/MediaPlayer2"), QStringLiteral("org.mpris.MediaPlayer2.Player")); | 102 | QDBusInterface mprisInterface(iface, QStringLiteral("/org/mpris/MediaPlayer2"), QStringLiteral("org.mpris.MediaPlayer2.Player")); | ||
125 | mprisInterface.asyncCall(QStringLiteral("PlayPause")); | 103 | mprisInterface.asyncCall(QStringLiteral("PlayPause")); | ||
126 | } | 104 | } | ||
Show All 10 Lines |
This function is static.