]> git.sur5r.net Git - minitube/blob - src/gnomeglobalshortcutbackend.cpp
Global shortcuts
[minitube] / src / gnomeglobalshortcutbackend.cpp
1 #include "gnomeglobalshortcutbackend.h"
2 #include "globalshortcuts.h"
3
4 #include <QAction>
5 #include <QtDebug>
6
7 #ifdef QT_DBUS_LIB
8 #  include <QtDBus>
9 #endif
10
11 const char* GnomeGlobalShortcutBackend::kGsdService = "org.gnome.SettingsDaemon";
12 const char* GnomeGlobalShortcutBackend::kGsdPath = "/org/gnome/SettingsDaemon/MediaKeys";
13 const char* GnomeGlobalShortcutBackend::kGsdInterface = "org.gnome.SettingsDaemon.MediaKeys";
14
15 GnomeGlobalShortcutBackend::GnomeGlobalShortcutBackend(GlobalShortcuts* parent)
16     : GlobalShortcutBackend(parent),
17     interface_(NULL) { }
18
19 bool GnomeGlobalShortcutBackend::IsGsdAvailable() {
20 #ifdef QT_DBUS_LIB
21     return QDBusConnection::sessionBus().interface()->isServiceRegistered(
22             GnomeGlobalShortcutBackend::kGsdService);
23 #else // QT_DBUS_LIB
24     return false;
25 #endif
26 }
27
28 bool GnomeGlobalShortcutBackend::DoRegister() {
29     // qDebug() << __PRETTY_FUNCTION__;
30 #ifdef QT_DBUS_LIB
31     // Check if the GSD service is available
32     if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService))
33         return false;
34
35     if (!interface_) {
36         interface_ = new QDBusInterface(
37                 kGsdService, kGsdPath, kGsdInterface, QDBusConnection::sessionBus(), this);
38     }
39
40     connect(interface_, SIGNAL(MediaPlayerKeyPressed(QString,QString)),
41             this, SLOT(GnomeMediaKeyPressed(QString,QString)));
42
43     return true;
44 #else // QT_DBUS_LIB
45     return false;
46 #endif
47 }
48
49 void GnomeGlobalShortcutBackend::DoUnregister() {
50
51 #ifdef QT_DBUS_LIB
52     // Check if the GSD service is available
53     if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService))
54         return;
55     if (!interface_)
56         return;
57
58     disconnect(interface_, SIGNAL(MediaPlayerKeyPressed(QString,QString)),
59                this, SLOT(GnomeMediaKeyPressed(QString,QString)));
60 #endif
61 }
62
63 void GnomeGlobalShortcutBackend::GnomeMediaKeyPressed(const QString&, const QString& key) {
64     if (key == "Play")     manager_->shortcuts()["play_pause"].action->trigger();
65     if (key == "Stop")     manager_->shortcuts()["stop"].action->trigger();
66     if (key == "Next")     manager_->shortcuts()["next_track"].action->trigger();
67     if (key == "Previous") manager_->shortcuts()["prev_track"].action->trigger();
68 }