From ba129a1542b550bf57c91181b1e7e3aa065832fc Mon Sep 17 00:00:00 2001 From: Albert Vaca Date: Wed, 24 Jul 2013 23:51:06 +0200 Subject: [PATCH] PauseMusicPackageInterface now supports MPRIS Other small improvements --- daemon/linkproviders/avahitcplinkprovider.cpp | 1 - daemon/networkpackage.cpp | 10 ++-- .../clipboardpackageinterface.cpp | 6 ++- .../clipboardpackageinterface.h | 2 +- .../pausemusicpackageinterface.cpp | 52 +++++++++++++++---- .../pausemusicpackageinterface.h | 5 +- 6 files changed, 57 insertions(+), 19 deletions(-) diff --git a/daemon/linkproviders/avahitcplinkprovider.cpp b/daemon/linkproviders/avahitcplinkprovider.cpp index 3b3f5f9a1..02033614f 100644 --- a/daemon/linkproviders/avahitcplinkprovider.cpp +++ b/daemon/linkproviders/avahitcplinkprovider.cpp @@ -53,7 +53,6 @@ void AvahiTcpLinkProvider::newConnection() NetworkPackage::createIdentityPackage(&np); int written = socket->write(np.serialize()); - qDebug() << np.serialize(); qDebug() << "AvahiTcpLinkProvider sent package." << written << " bytes written, waiting for reply"; } diff --git a/daemon/networkpackage.cpp b/daemon/networkpackage.cpp index 79c8ee083..255ef363e 100644 --- a/daemon/networkpackage.cpp +++ b/daemon/networkpackage.cpp @@ -54,10 +54,12 @@ QByteArray NetworkPackage::serialize() const bool ok; QJson::Serializer serializer; QByteArray json = serializer.serialize(variant,&ok); - if (!ok) qDebug() << "Serialization error:" << serializer.errorMessage(); - json.append('\n'); - - qDebug() << "utlims chars:" << (int)(json[json.size()-1]) << (int)(json[json.size()-2]) << (int)(json[json.size()-3]); + if (!ok) { + qDebug() << "Serialization error:" << serializer.errorMessage(); + } else { + qDebug() << "Serialized package:" << json; + json.append('\n'); + } return json; } diff --git a/daemon/packageinterfaces/clipboardpackageinterface.cpp b/daemon/packageinterfaces/clipboardpackageinterface.cpp index d63f9260b..201fa9ef6 100644 --- a/daemon/packageinterfaces/clipboardpackageinterface.cpp +++ b/daemon/packageinterfaces/clipboardpackageinterface.cpp @@ -27,11 +27,13 @@ ClipboardPackageInterface::ClipboardPackageInterface() { clipboard = QApplication::clipboard(); ignore_next_clipboard_change = false; - connect(clipboard,SIGNAL(changed(QClipboard::Mode)),this,SLOT(clipboardChanged())); + connect(clipboard,SIGNAL(changed(QClipboard::Mode)),this,SLOT(clipboardChanged(QClipboard::Mode))); } -void ClipboardPackageInterface::clipboardChanged() +void ClipboardPackageInterface::clipboardChanged(QClipboard::Mode mode) { + if (mode != QClipboard::QClipboard::Clipboard) return; + if (ignore_next_clipboard_change) { ignore_next_clipboard_change = false; return; diff --git a/daemon/packageinterfaces/clipboardpackageinterface.h b/daemon/packageinterfaces/clipboardpackageinterface.h index 0b12a2e00..bc896d755 100644 --- a/daemon/packageinterfaces/clipboardpackageinterface.h +++ b/daemon/packageinterfaces/clipboardpackageinterface.h @@ -41,7 +41,7 @@ public Q_SLOTS: virtual bool receivePackage(const Device& device, const NetworkPackage& np); private Q_SLOTS: - void clipboardChanged(); + void clipboardChanged(QClipboard::Mode mode); private: bool ignore_next_clipboard_change; diff --git a/daemon/packageinterfaces/pausemusicpackageinterface.cpp b/daemon/packageinterfaces/pausemusicpackageinterface.cpp index fff3270d9..f3e55bc74 100644 --- a/daemon/packageinterfaces/pausemusicpackageinterface.cpp +++ b/daemon/packageinterfaces/pausemusicpackageinterface.cpp @@ -21,12 +21,16 @@ #include "pausemusicpackageinterface.h" #include +#include +#include +#include +#include +#include PauseMusicPackageInterface::PauseMusicPackageInterface() { //TODO: Be able to change this from settings pauseWhen = PauseWhenRinging; - paused = false; } @@ -36,7 +40,7 @@ bool PauseMusicPackageInterface::receivePackage (const Device& device, const Net bool pauseConditionFulfilled = false; - //TODO: I have manually tested it and it works for both cases, but I should somehow write a test for this logic + //TODO: I have manually tested it and it works for both "pauseWhen" cases, but I should somehow write a test for this logic if (pauseWhen == PauseWhenRinging) { if (np.type() == PACKAGE_TYPE_NOTIFICATION) { if (np.get("notificationType") != "ringing") return false; @@ -53,15 +57,43 @@ bool PauseMusicPackageInterface::receivePackage (const Device& device, const Net qDebug() << "PauseMusicPackageReceiver - PauseCondition:" << pauseConditionFulfilled; - if (pauseConditionFulfilled && !paused) { - //TODO: Use KDE DBUS API - system("qdbus org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Pause"); - } if (!pauseConditionFulfilled && paused) { - //FIXME: Play does not work, using PlayPause - system("qdbus org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.PlayPause"); - } + if (pauseConditionFulfilled) { + //TODO: Make this async + //Search for interfaces currently playing + QStringList interfaces = QDBusConnection::sessionBus().interface()->registeredServiceNames().value(); + Q_FOREACH (const QString& iface, interfaces) { + if (iface.startsWith("org.mpris.MediaPlayer2")) { + QDBusInterface *dbusInterface = new QDBusInterface(iface, "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties", QDBusConnection::sessionBus(), this); + QDBusInterface *mprisInterface = new QDBusInterface(iface, "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player", QDBusConnection::sessionBus(), this); - paused = pauseConditionFulfilled; + QString status = (qvariant_cast(dbusInterface->call(QDBus::Block,"Get","org.mpris.MediaPlayer2.Player","PlaybackStatus").arguments().first()).variant()).toString(); + if (status == "Playing") { + if (!pausedSources.contains(iface)) { + pausedSources.insert(iface); + mprisInterface->call(QDBus::Block,"Pause"); + } + } + + delete dbusInterface; + delete mprisInterface; + } + } + } if (!pauseConditionFulfilled) { + //TODO: Make this async + Q_FOREACH (const QString& iface, pausedSources) { + QDBusInterface *mprisInterface = new QDBusInterface(iface, "/org/mpris/MediaPlayer2", "org.mpris.MediaPlayer2.Player", QDBusConnection::sessionBus(), this); + //FIXME: Calling play does not work in spotify + //mprisInterface->call(QDBus::Block,"Play"); + //Workaround: Using playpause instead (checking first if it is already playing) + QDBusInterface *dbusInterface = new QDBusInterface(iface, "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties", QDBusConnection::sessionBus(), this); + QString status = (qvariant_cast(dbusInterface->call(QDBus::Block,"Get","org.mpris.MediaPlayer2.Player","PlaybackStatus").arguments().first()).variant()).toString(); + if (status == "Paused") mprisInterface->call(QDBus::Block,"PlayPause"); + delete dbusInterface; + //End of workaround + delete mprisInterface; + } + pausedSources.clear(); + } return true; diff --git a/daemon/packageinterfaces/pausemusicpackageinterface.h b/daemon/packageinterfaces/pausemusicpackageinterface.h index 6e7e433bb..17935381e 100644 --- a/daemon/packageinterfaces/pausemusicpackageinterface.h +++ b/daemon/packageinterfaces/pausemusicpackageinterface.h @@ -23,6 +23,9 @@ #include "packageinterface.h" +#include +#include + class PauseMusicPackageInterface : public PackageInterface { @@ -33,7 +36,7 @@ public: private: enum PauseCondtions { PauseWhenTalking, PauseWhenRinging, NeverPause }; PauseCondtions pauseWhen; - bool paused; + QSet pausedSources; };