1 /*
2  * Cantata
3  *
4  * Copyright (c) 2011-2020 Craig Drummond <craig.p.drummond@gmail.com>
5  *
6  * ----
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; see the file COPYING.  If not, write to
20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  * Boston, MA 02110-1301, USA.
22  */
23 
24 #include "gnomemediakeys.h"
25 #include "settingsdaemoninterface.h"
26 #include "mediakeysinterface.h"
27 #include <QDBusConnection>
28 #include <QDBusConnectionInterface>
29 #include <QDBusPendingReply>
30 #include <QDBusPendingCallWatcher>
31 #include <QDBusServiceWatcher>
32 #include <QCoreApplication>
33 
34 static const char * constOrigService = "org.gnome.SettingsDaemon";
35 static const char * constNewService = "org.gnome.SettingsDaemon.MediaKeys";
36 static const char * constDaemonPath = "/org/gnome/SettingsDaemon";
37 static const char * constMediaKeysPath = "/org/gnome/SettingsDaemon/MediaKeys";
38 
GnomeMediaKeys(QObject * p)39 GnomeMediaKeys::GnomeMediaKeys(QObject *p)
40     : MultiMediaKeysInterface(p)
41     , daemon(nullptr)
42     , mk(nullptr)
43     , watcher(nullptr)
44 {
45 }
46 
activate()47 bool GnomeMediaKeys::activate()
48 {
49     if (mk) {
50         return true;
51     }
52     if (daemonIsRunning()) {
53         grabKeys();
54         return true;
55     }
56     return false;
57 }
58 
deactivate()59 void GnomeMediaKeys::deactivate()
60 {
61     if (mk) {
62         releaseKeys();
63         disconnectDaemon();
64         if (watcher) {
65             watcher->deleteLater();
66             watcher=nullptr;
67         }
68     }
69 }
70 
daemonIsRunning()71 bool GnomeMediaKeys::daemonIsRunning()
72 {
73     // Check if the service is available
74     if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(constNewService) &&
75         !QDBusConnection::sessionBus().interface()->isServiceRegistered(constOrigService)) {
76         //...not already started, so attempt to start!
77         QDBusConnection::sessionBus().interface()->startService(constOrigService); // ??
78         if (!daemon) {
79             daemon = new OrgGnomeSettingsDaemonInterface(constOrigService, constDaemonPath, QDBusConnection::sessionBus(), this);
80             connect(daemon, SIGNAL(PluginActivated(QString)), this, SLOT(pluginActivated(QString)));
81             daemon->Start();
82             return false;
83         }
84     }
85     return true;
86 }
87 
releaseKeys()88 void GnomeMediaKeys::releaseKeys()
89 {
90     if (mk) {
91         mk->ReleaseMediaPlayerKeys(QCoreApplication::applicationName());
92         disconnect(mk, SIGNAL(MediaPlayerKeyPressed(QString,QString)), this, SLOT(keyPressed(QString,QString)));
93         mk->deleteLater();
94         mk=nullptr;
95     }
96 }
97 
grabKeys()98 void GnomeMediaKeys::grabKeys()
99 {
100     QStringList services { constNewService, constOrigService };
101     for (const auto &service: services) {
102         if (QDBusConnection::sessionBus().interface()->isServiceRegistered(service)) {
103             if (!mk) {
104                 mk = new OrgGnomeSettingsDaemonMediaKeysInterface(service, constMediaKeysPath, QDBusConnection::sessionBus(), this);
105             }
106 
107             QDBusPendingReply<> reply = mk->GrabMediaPlayerKeys(QCoreApplication::applicationName(), QDateTime::currentDateTime().toTime_t());
108             QDBusPendingCallWatcher *callWatcher = new QDBusPendingCallWatcher(reply, this);
109             connect(callWatcher, SIGNAL(finished(QDBusPendingCallWatcher *)), this, SLOT(registerFinished(QDBusPendingCallWatcher*)));
110 
111             if (!watcher) {
112                 watcher = new QDBusServiceWatcher(this);
113                 watcher->setConnection(QDBusConnection::sessionBus());
114                 watcher->setWatchMode(QDBusServiceWatcher::WatchForOwnerChange);
115                 connect(watcher, SIGNAL(serviceOwnerChanged(QString, QString, QString)), this, SLOT(serviceOwnerChanged(QString, QString, QString)));
116             }
117             serviceName = service;
118             break;
119         }
120     }
121 }
122 
disconnectDaemon()123 void GnomeMediaKeys::disconnectDaemon()
124 {
125     if (daemon) {
126         disconnect(daemon, SIGNAL(PluginActivated(QString)), this, SLOT(pluginActivated(QString)));
127         daemon->deleteLater();
128         daemon=nullptr;
129     }
130 }
131 
serviceOwnerChanged(const QString & name,const QString &,const QString &)132 void GnomeMediaKeys::serviceOwnerChanged(const QString &name, const QString &, const QString &)
133 {
134     if (name==serviceName) {
135         releaseKeys();
136         disconnectDaemon();
137         if (daemonIsRunning()) {
138             grabKeys();
139         }
140     }
141 }
142 
registerFinished(QDBusPendingCallWatcher * watcher)143 void GnomeMediaKeys::registerFinished(QDBusPendingCallWatcher *watcher)
144 {
145     QDBusMessage reply = watcher->reply();
146     watcher->deleteLater();
147 
148     if (QDBusMessage::ErrorMessage!=reply.type()) {
149         connect(mk, SIGNAL(MediaPlayerKeyPressed(QString, QString)),  this, SLOT(keyPressed(QString,QString)));
150         disconnectDaemon();
151     }
152 }
153 
keyPressed(const QString & app,const QString & key)154 void GnomeMediaKeys::keyPressed(const QString &app, const QString &key)
155 {
156     if (QCoreApplication::applicationName()!=app) {
157         return;
158     }
159     if (QLatin1String("Play")==key) {
160         emit playPause();
161     } else if (QLatin1String("Stop")==key) {
162         emit stop();
163     } else if (QLatin1String("Next")==key) {
164         emit next();
165     } else if (QLatin1String("Previous")==key) {
166         emit previous();
167     }
168 }
169 
pluginActivated(const QString & name)170 void GnomeMediaKeys::pluginActivated(const QString &name)
171 {
172     if (QLatin1String("media-keys")==name) {
173         grabKeys();
174     }
175 }
176 
177 #include "moc_gnomemediakeys.cpp"
178