File: gnomemediakeys.cpp

package info (click to toggle)
cantata 3.4.0.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,428 kB
  • sloc: cpp: 109,584; perl: 1,366; xml: 722; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (176 lines) | stat: -rw-r--r-- 5,167 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "gnomemediakeys.h"
#include "mediakeysinterface.h"
#include "settingsdaemoninterface.h"
#include <QCoreApplication>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
#include <QDBusServiceWatcher>

static const char* constOrigService = "org.gnome.SettingsDaemon";
static const char* constNewService = "org.gnome.SettingsDaemon.MediaKeys";
static const char* constDaemonPath = "/org/gnome/SettingsDaemon";
static const char* constMediaKeysPath = "/org/gnome/SettingsDaemon/MediaKeys";

GnomeMediaKeys::GnomeMediaKeys(QObject* p)
	: MultiMediaKeysInterface(p), daemon(nullptr), mk(nullptr), watcher(nullptr)
{
}

bool GnomeMediaKeys::activate()
{
	if (mk) {
		return true;
	}
	if (daemonIsRunning()) {
		grabKeys();
		return true;
	}
	return false;
}

void GnomeMediaKeys::deactivate()
{
	if (mk) {
		releaseKeys();
		disconnectDaemon();
		if (watcher) {
			watcher->deleteLater();
			watcher = nullptr;
		}
	}
}

bool GnomeMediaKeys::daemonIsRunning()
{
	// Check if the service is available
	if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(constNewService) && !QDBusConnection::sessionBus().interface()->isServiceRegistered(constOrigService)) {
		//...not already started, so attempt to start!
		QDBusConnection::sessionBus().interface()->startService(constOrigService);// ??
		if (!daemon) {
			daemon = new OrgGnomeSettingsDaemonInterface(constOrigService, constDaemonPath, QDBusConnection::sessionBus(), this);
			connect(daemon, SIGNAL(PluginActivated(QString)), this, SLOT(pluginActivated(QString)));
			daemon->Start();
			return false;
		}
	}
	return true;
}

void GnomeMediaKeys::releaseKeys()
{
	if (mk) {
		mk->ReleaseMediaPlayerKeys(QCoreApplication::applicationName());
		disconnect(mk, SIGNAL(MediaPlayerKeyPressed(QString, QString)), this, SLOT(keyPressed(QString, QString)));
		mk->deleteLater();
		mk = nullptr;
	}
}

void GnomeMediaKeys::grabKeys()
{
	QStringList services{constNewService, constOrigService};
	for (const auto& service : services) {
		if (QDBusConnection::sessionBus().interface()->isServiceRegistered(service)) {
			if (!mk) {
				mk = new OrgGnomeSettingsDaemonMediaKeysInterface(service, constMediaKeysPath, QDBusConnection::sessionBus(), this);
			}

			QDBusPendingReply<> reply = mk->GrabMediaPlayerKeys(QCoreApplication::applicationName(), QDateTime::currentDateTime().toSecsSinceEpoch());
			QDBusPendingCallWatcher* callWatcher = new QDBusPendingCallWatcher(reply, this);
			connect(callWatcher, SIGNAL(finished(QDBusPendingCallWatcher*)), this, SLOT(registerFinished(QDBusPendingCallWatcher*)));

			if (!watcher) {
				watcher = new QDBusServiceWatcher(this);
				watcher->setConnection(QDBusConnection::sessionBus());
				watcher->setWatchMode(QDBusServiceWatcher::WatchForOwnerChange);
				connect(watcher, SIGNAL(serviceOwnerChanged(QString, QString, QString)), this, SLOT(serviceOwnerChanged(QString, QString, QString)));
			}
			serviceName = service;
			break;
		}
	}
}

void GnomeMediaKeys::disconnectDaemon()
{
	if (daemon) {
		disconnect(daemon, SIGNAL(PluginActivated(QString)), this, SLOT(pluginActivated(QString)));
		daemon->deleteLater();
		daemon = nullptr;
	}
}

void GnomeMediaKeys::serviceOwnerChanged(const QString& name, const QString&, const QString&)
{
	if (name == serviceName) {
		releaseKeys();
		disconnectDaemon();
		if (daemonIsRunning()) {
			grabKeys();
		}
	}
}

void GnomeMediaKeys::registerFinished(QDBusPendingCallWatcher* watcher)
{
	QDBusMessage reply = watcher->reply();
	watcher->deleteLater();

	if (QDBusMessage::ErrorMessage != reply.type()) {
		connect(mk, SIGNAL(MediaPlayerKeyPressed(QString, QString)), this, SLOT(keyPressed(QString, QString)));
		disconnectDaemon();
	}
}

void GnomeMediaKeys::keyPressed(const QString& app, const QString& key)
{
	if (QCoreApplication::applicationName() != app) {
		return;
	}
	if (QLatin1String("Play") == key) {
		emit playPause();
	}
	else if (QLatin1String("Stop") == key) {
		emit stop();
	}
	else if (QLatin1String("Next") == key) {
		emit next();
	}
	else if (QLatin1String("Previous") == key) {
		emit previous();
	}
}

void GnomeMediaKeys::pluginActivated(const QString& name)
{
	if (QLatin1String("media-keys") == name) {
		grabKeys();
	}
}

#include "moc_gnomemediakeys.cpp"