]> git.sur5r.net Git - minitube/blob - src/gnomeglobalshortcutbackend.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / gnomeglobalshortcutbackend.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Musique.
4 Copyright 2013, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Musique is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 Musique is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Musique.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "gnomeglobalshortcutbackend.h"
22 #include "globalshortcuts.h"
23 #include "constants.h"
24
25 #include <QAction>
26 #include <QtDebug>
27
28 #ifdef QT_DBUS_LIB
29 #  include <QtDBus>
30 #endif
31
32 const char* GnomeGlobalShortcutBackend::kGsdService = "org.gnome.SettingsDaemon";
33 const char* GnomeGlobalShortcutBackend::kGsdPath = "/org/gnome/SettingsDaemon/MediaKeys";
34 const char* GnomeGlobalShortcutBackend::kGsdInterface = "org.gnome.SettingsDaemon.MediaKeys";
35
36 GnomeGlobalShortcutBackend::GnomeGlobalShortcutBackend(GlobalShortcuts* parent)
37     : GlobalShortcutBackend(parent),
38       interface_(NULL) { }
39
40 bool GnomeGlobalShortcutBackend::IsGsdAvailable() {
41 #ifdef QT_DBUS_LIB
42     return QDBusConnection::sessionBus().interface()->isServiceRegistered(
43                 GnomeGlobalShortcutBackend::kGsdService);
44 #else // QT_DBUS_LIB
45     return false;
46 #endif
47 }
48
49 bool GnomeGlobalShortcutBackend::DoRegister() {
50     // qDebug() << __PRETTY_FUNCTION__;
51 #ifdef QT_DBUS_LIB
52     // Check if the GSD service is available
53     if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService))
54         return false;
55
56     if (!interface_) {
57         interface_ = new QDBusInterface(
58                     kGsdService, kGsdPath, kGsdInterface, QDBusConnection::sessionBus(), this);
59     }
60
61     QDBusMessage reply = interface_->call("GrabMediaPlayerKeys", Constants::NAME, (unsigned int) 0);
62     if (reply.type() == QDBusMessage::ErrorMessage) {
63         qWarning() << "Failed to grab media player keys. Error:" << reply.errorMessage();
64     }
65
66     connect(interface_, SIGNAL(MediaPlayerKeyPressed(QString,QString)),
67             this, SLOT(GnomeMediaKeyPressed(QString,QString)));
68
69     return true;
70 #else // QT_DBUS_LIB
71     return false;
72 #endif
73 }
74
75 void GnomeGlobalShortcutBackend::DoUnregister() {
76
77 #ifdef QT_DBUS_LIB
78     // Check if the GSD service is available
79     if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService))
80         return;
81     if (!interface_)
82         return;
83
84     disconnect(interface_, SIGNAL(MediaPlayerKeyPressed(QString,QString)),
85                this, SLOT(GnomeMediaKeyPressed(QString,QString)));
86
87     interface_->call("ReleaseMediaPlayerKeys", Constants::NAME);
88 #endif
89 }
90
91 void GnomeGlobalShortcutBackend::GnomeMediaKeyPressed(const QString&, const QString& key) {
92     if (key == "Play")     manager_->shortcuts()["play_pause"].action->trigger();
93     if (key == "Stop")     manager_->shortcuts()["stop"].action->trigger();
94     if (key == "Next")     manager_->shortcuts()["next_track"].action->trigger();
95     if (key == "Previous") manager_->shortcuts()["prev_track"].action->trigger();
96 }