]> git.sur5r.net Git - minitube/blob - src/gnomeglobalshortcutbackend.cpp
New upstream version 2.1.3
[minitube] / src / gnomeglobalshortcutbackend.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
6 Minitube 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 Minitube 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 Minitube.  If not, see <http://www.gnu.org/licenses/>.
18
19 $END_LICENSE */
20
21 #include "gnomeglobalshortcutbackend.h"
22 #include "globalshortcuts.h"
23
24 #include <QAction>
25 #include <QtDebug>
26 #include <QtDBus>
27
28 const char* GnomeGlobalShortcutBackend::kGsdService = "org.gnome.SettingsDaemon";
29 const char* GnomeGlobalShortcutBackend::kGsdPath = "/org/gnome/SettingsDaemon/MediaKeys";
30 const char* GnomeGlobalShortcutBackend::kGsdInterface = "org.gnome.SettingsDaemon.MediaKeys";
31
32 GnomeGlobalShortcutBackend::GnomeGlobalShortcutBackend(GlobalShortcuts* parent)
33     : GlobalShortcutBackend(parent),
34     interface_(NULL) { }
35
36 bool GnomeGlobalShortcutBackend::IsGsdAvailable() {
37     return QDBusConnection::sessionBus().interface()->isServiceRegistered(
38             GnomeGlobalShortcutBackend::kGsdService);
39 }
40
41 bool GnomeGlobalShortcutBackend::DoRegister() {
42     // qDebug() << __PRETTY_FUNCTION__;
43     // Check if the GSD service is available
44     if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService))
45         return false;
46
47     if (!interface_) {
48         interface_ = new QDBusInterface(
49                 kGsdService, kGsdPath, kGsdInterface, QDBusConnection::sessionBus(), this);
50     }
51
52     connect(interface_, SIGNAL(MediaPlayerKeyPressed(QString,QString)),
53             this, SLOT(GnomeMediaKeyPressed(QString,QString)));
54
55     return true;
56 }
57
58 void GnomeGlobalShortcutBackend::DoUnregister() {
59     // Check if the GSD service is available
60     if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService))
61         return;
62     if (!interface_)
63         return;
64
65     disconnect(interface_, SIGNAL(MediaPlayerKeyPressed(QString,QString)),
66                this, SLOT(GnomeMediaKeyPressed(QString,QString)));
67 }
68
69 void GnomeGlobalShortcutBackend::GnomeMediaKeyPressed(const QString&, const QString& key) {
70     if (key == "Play")     manager_->shortcuts()["play_pause"].action->trigger();
71     if (key == "Stop")     manager_->shortcuts()["stop"].action->trigger();
72     if (key == "Next")     manager_->shortcuts()["next_track"].action->trigger();
73     if (key == "Previous") manager_->shortcuts()["prev_track"].action->trigger();
74 }