]> git.sur5r.net Git - minitube/commitdiff
Global shortcuts
author <> <>
Sun, 1 Aug 2010 13:04:55 +0000 (15:04 +0200)
committerFlavio <flavio@guodil.local>
Sun, 1 Aug 2010 13:04:55 +0000 (15:04 +0200)
minitube.pro
src/MainWindow.cpp
src/MediaView.h
src/globalshortcutbackend.cpp [new file with mode: 0644]
src/globalshortcutbackend.h [new file with mode: 0644]
src/globalshortcuts.cpp [new file with mode: 0644]
src/globalshortcuts.h [new file with mode: 0644]
src/gnomeglobalshortcutbackend.cpp [new file with mode: 0644]
src/gnomeglobalshortcutbackend.h [new file with mode: 0644]
src/main.cpp
src/playlistwidget.h

index 0c00ef22dd5fbe6df8592b85f4215d3b8660ebac..75119a7e56cf5363fb1de0b0b6b890fcab4a5db1 100755 (executable)
@@ -10,6 +10,9 @@ TARGET = minitube
 QT += network \
     xml \
     phonon
+unix:!mac {
+    QT += dbus
+}
 include(src/qtsingleapplication/qtsingleapplication.pri)
 HEADERS += src/MainWindow.h \
     src/SearchView.h \
@@ -41,7 +44,10 @@ HEADERS += src/MainWindow.h \
     src/flickcharm.h \
     src/videodefinition.h \
     src/fontutils.h \
-    src/thlibrary/thblackbar.h
+    src/thlibrary/thblackbar.h \
+    src/gnomeglobalshortcutbackend.h \
+    src/globalshortcuts.h \
+    src/globalshortcutbackend.h
 SOURCES += src/main.cpp \
     src/MainWindow.cpp \
     src/SearchView.cpp \
@@ -71,7 +77,10 @@ SOURCES += src/main.cpp \
     src/videodefinition.cpp \
     src/constants.cpp \
     src/fontutils.cpp \
-    src/thlibrary/thblackbar.cpp
+    src/thlibrary/thblackbar.cpp \
+    src/gnomeglobalshortcutbackend.cpp \
+    src/globalshortcuts.cpp \
+    src/globalshortcutbackend.cpp
 RESOURCES += resources.qrc
 DESTDIR = build/target/
 OBJECTS_DIR = build/obj/
index 19bd15f000a7d4d86bd564619d695525e3f82d26..5ab28aac94ff0b9cb26af358c8766e7d1038398a 100755 (executable)
@@ -5,6 +5,13 @@
 #include "global.h"
 #include "videodefinition.h"
 #include "fontutils.h"
+#include "globalshortcuts.h"
+#ifdef Q_WS_X11
+#include "gnomeglobalshortcutbackend.h"
+#endif
+#ifdef APP_MAC
+#include "local/mac/mac_startup.h"
+#endif
 
 MainWindow::MainWindow() :
         aboutView(0),
@@ -58,6 +65,19 @@ MainWindow::MainWindow() :
     showWidget(searchView);
 
     setCentralWidget(views);
+
+    // Global shortcuts
+    GlobalShortcuts &shortcuts = GlobalShortcuts::instance();
+#ifdef Q_WS_X11
+    if (GnomeGlobalShortcutBackend::IsGsdAvailable())
+        shortcuts.setBackend(new GnomeGlobalShortcutBackend(&shortcuts));
+#endif
+#ifdef APP_MAC
+    mac::MacSetup();
+#endif
+    connect(&shortcuts, SIGNAL(PlayPause()), pauseAct, SLOT(trigger()));
+    connect(&shortcuts, SIGNAL(Stop()), this, SLOT(stop()));
+    connect(&shortcuts, SIGNAL(Next()), skipAct, SLOT(trigger()));
 }
 
 MainWindow::~MainWindow() {
index f3bd3dd33f9eca687c6db8ffb302a70a61e3b03e..04dd92c55ae40d75de31ba8f3bd620764200b3cd 100644 (file)
@@ -7,7 +7,7 @@
 #include <phonon/videowidget.h>
 #include "View.h"
 #include "ListModel.h"
-#include "thblackbar.h"
+#include "thlibrary/thblackbar.h"
 #include "searchparams.h"
 #include "playlistwidget.h"
 #include "loadingwidget.h"
diff --git a/src/globalshortcutbackend.cpp b/src/globalshortcutbackend.cpp
new file mode 100644 (file)
index 0000000..8646b7a
--- /dev/null
@@ -0,0 +1,24 @@
+#include "globalshortcutbackend.h"
+#include "globalshortcuts.h"
+
+GlobalShortcutBackend::GlobalShortcutBackend(GlobalShortcuts *parent)
+    : QObject(parent),
+    manager_(parent),
+    active_(false) { }
+
+bool GlobalShortcutBackend::Register() {
+    bool ret = DoRegister();
+    if (ret)
+        active_ = true;
+    return ret;
+}
+
+void GlobalShortcutBackend::Unregister() {
+    DoUnregister();
+    active_ = false;
+}
+
+void GlobalShortcutBackend::Reregister() {
+    Unregister();
+    Register();
+}
diff --git a/src/globalshortcutbackend.h b/src/globalshortcutbackend.h
new file mode 100644 (file)
index 0000000..e6844bb
--- /dev/null
@@ -0,0 +1,27 @@
+#ifndef GLOBALSHORTCUTBACKEND_H
+#define GLOBALSHORTCUTBACKEND_H
+
+#include <QObject>
+
+class GlobalShortcuts;
+
+class GlobalShortcutBackend : public QObject {
+public:
+  GlobalShortcutBackend(GlobalShortcuts* parent = 0);
+  virtual ~GlobalShortcutBackend() {}
+
+  bool is_active() const { return active_; }
+
+  bool Register();
+  void Unregister();
+  void Reregister();
+
+protected:
+  virtual bool DoRegister() = 0;
+  virtual void DoUnregister() = 0;
+
+  GlobalShortcuts* manager_;
+  bool active_;
+};
+
+#endif // GLOBALSHORTCUTBACKEND_H
diff --git a/src/globalshortcuts.cpp b/src/globalshortcuts.cpp
new file mode 100644 (file)
index 0000000..39eda6d
--- /dev/null
@@ -0,0 +1,50 @@
+#include "globalshortcuts.h"
+#include "globalshortcutbackend.h"
+
+static GlobalShortcuts *singleton = 0;
+
+GlobalShortcuts& GlobalShortcuts::instance() {
+    if (!singleton) singleton = new GlobalShortcuts();
+    return *singleton;
+}
+
+GlobalShortcuts::GlobalShortcuts(QObject *parent)
+    : QObject(parent),
+    backend(0) {
+
+    // Create actions
+    AddShortcut("play", tr("Play"), SIGNAL(Play()));
+    AddShortcut("pause", tr("Pause"), SIGNAL(Pause()));
+    AddShortcut("play_pause", tr("Play/Pause"), SIGNAL(PlayPause()), QKeySequence(Qt::Key_MediaPlay));
+    AddShortcut("stop", tr("Stop"), SIGNAL(Stop()), QKeySequence(Qt::Key_MediaStop));
+    AddShortcut("stop_after", tr("Stop playing after current track"), SIGNAL(StopAfter()));
+    AddShortcut("next_track", tr("Next track"), SIGNAL(Next()), QKeySequence(Qt::Key_MediaNext));
+    AddShortcut("prev_track", tr("Previous track"), SIGNAL(Previous()), QKeySequence(Qt::Key_MediaPrevious));
+    AddShortcut("inc_volume", tr("Increase volume"), SIGNAL(IncVolume()));
+    AddShortcut("dec_volume", tr("Decrease volume"), SIGNAL(DecVolume()));
+    AddShortcut("mute", tr("Mute"), SIGNAL(Mute()));
+    AddShortcut("seek_forward", tr("Seek forward"), SIGNAL(SeekForward()));
+    AddShortcut("seek_backward", tr("Seek backward"), SIGNAL(SeekBackward()));
+
+}
+
+void GlobalShortcuts::AddShortcut(const QString &id, const QString &name,
+                                  const char* signal,
+                                  const QKeySequence &default_key) {
+    Shortcut shortcut;
+    shortcut.action = new QAction(name, this);
+    shortcut.action->setShortcut(default_key);
+    shortcut.id = id;
+    shortcut.default_key = default_key;
+
+    connect(shortcut.action, SIGNAL(triggered()), this, signal);
+
+    shortcuts_[id] = shortcut;
+}
+
+void GlobalShortcuts::reload() {
+    if (backend) {
+        backend->Unregister();
+        backend->Register();
+    }
+}
diff --git a/src/globalshortcuts.h b/src/globalshortcuts.h
new file mode 100644 (file)
index 0000000..62cd935
--- /dev/null
@@ -0,0 +1,56 @@
+#ifndef GLOBALSHORTCUTS_H
+#define GLOBALSHORTCUTS_H
+
+#include <QtCore>
+#include <QAction>
+
+class GlobalShortcutBackend;
+
+class GlobalShortcuts : public QObject {
+
+    Q_OBJECT
+
+public:
+    static GlobalShortcuts& instance();
+
+    struct Shortcut {
+        QString id;
+        QKeySequence default_key;
+        QAction* action;
+    };
+
+    QMap<QString, Shortcut> shortcuts() const { return shortcuts_; }
+    void setBackend(GlobalShortcutBackend* backend) {
+        this->backend = backend;
+        reload();
+    }
+
+public slots:
+    void reload();
+
+signals:
+    void Play();
+    void Pause();
+    void PlayPause();
+    void Stop();
+    void StopAfter();
+    void Next();
+    void Previous();
+    void IncVolume();
+    void DecVolume();
+    void Mute();
+    void SeekForward();
+    void SeekBackward();
+
+private:
+    GlobalShortcuts(QObject* parent = 0);
+    void AddShortcut(const QString& id, const QString& name, const char* signal,
+                     const QKeySequence& default_key = QKeySequence(0));
+
+private:
+    GlobalShortcutBackend* backend;
+
+    QMap<QString, Shortcut> shortcuts_;
+};
+
+#endif
diff --git a/src/gnomeglobalshortcutbackend.cpp b/src/gnomeglobalshortcutbackend.cpp
new file mode 100644 (file)
index 0000000..f34c20d
--- /dev/null
@@ -0,0 +1,68 @@
+#include "gnomeglobalshortcutbackend.h"
+#include "globalshortcuts.h"
+
+#include <QAction>
+#include <QtDebug>
+
+#ifdef QT_DBUS_LIB
+#  include <QtDBus>
+#endif
+
+const char* GnomeGlobalShortcutBackend::kGsdService = "org.gnome.SettingsDaemon";
+const char* GnomeGlobalShortcutBackend::kGsdPath = "/org/gnome/SettingsDaemon/MediaKeys";
+const char* GnomeGlobalShortcutBackend::kGsdInterface = "org.gnome.SettingsDaemon.MediaKeys";
+
+GnomeGlobalShortcutBackend::GnomeGlobalShortcutBackend(GlobalShortcuts* parent)
+    : GlobalShortcutBackend(parent),
+    interface_(NULL) { }
+
+bool GnomeGlobalShortcutBackend::IsGsdAvailable() {
+#ifdef QT_DBUS_LIB
+    return QDBusConnection::sessionBus().interface()->isServiceRegistered(
+            GnomeGlobalShortcutBackend::kGsdService);
+#else // QT_DBUS_LIB
+    return false;
+#endif
+}
+
+bool GnomeGlobalShortcutBackend::DoRegister() {
+    // qDebug() << __PRETTY_FUNCTION__;
+#ifdef QT_DBUS_LIB
+    // Check if the GSD service is available
+    if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService))
+        return false;
+
+    if (!interface_) {
+        interface_ = new QDBusInterface(
+                kGsdService, kGsdPath, kGsdInterface, QDBusConnection::sessionBus(), this);
+    }
+
+    connect(interface_, SIGNAL(MediaPlayerKeyPressed(QString,QString)),
+            this, SLOT(GnomeMediaKeyPressed(QString,QString)));
+
+    return true;
+#else // QT_DBUS_LIB
+    return false;
+#endif
+}
+
+void GnomeGlobalShortcutBackend::DoUnregister() {
+
+#ifdef QT_DBUS_LIB
+    // Check if the GSD service is available
+    if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService))
+        return;
+    if (!interface_)
+        return;
+
+    disconnect(interface_, SIGNAL(MediaPlayerKeyPressed(QString,QString)),
+               this, SLOT(GnomeMediaKeyPressed(QString,QString)));
+#endif
+}
+
+void GnomeGlobalShortcutBackend::GnomeMediaKeyPressed(const QString&, const QString& key) {
+    if (key == "Play")     manager_->shortcuts()["play_pause"].action->trigger();
+    if (key == "Stop")     manager_->shortcuts()["stop"].action->trigger();
+    if (key == "Next")     manager_->shortcuts()["next_track"].action->trigger();
+    if (key == "Previous") manager_->shortcuts()["prev_track"].action->trigger();
+}
diff --git a/src/gnomeglobalshortcutbackend.h b/src/gnomeglobalshortcutbackend.h
new file mode 100644 (file)
index 0000000..bf6dd89
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef GNOMEGLOBALSHORTCUTBACKEND_H
+#define GNOMEGLOBALSHORTCUTBACKEND_H
+
+#include "globalshortcutbackend.h"
+
+class QDBusInterface;
+
+class GnomeGlobalShortcutBackend : public GlobalShortcutBackend {
+    Q_OBJECT
+
+public:
+    GnomeGlobalShortcutBackend(GlobalShortcuts* parent);
+    static bool IsGsdAvailable();
+    static const char* kGsdService;
+    static const char* kGsdPath;
+    static const char* kGsdInterface;
+
+protected:
+    bool DoRegister();
+    void DoUnregister();
+
+private slots:
+    void GnomeMediaKeyPressed(const QString& application, const QString& key);
+
+private:
+    QDBusInterface* interface_;
+};
+
+#endif // GNOMEGLOBALSHORTCUTBACKEND_H
index 36f5d91b7297f5d004aef43d07768eb3f3ab343e..ec2d77ce5251f36637669bbd0b1b2c782086eeea 100755 (executable)
@@ -2,9 +2,16 @@
 #include <qtsingleapplication.h>
 #include "constants.h"
 #include "MainWindow.h"
+#ifdef APP_MAC
+#include "local/mac/mac_startup.h"
+#endif
 
 int main(int argc, char **argv) {
 
+#ifdef APP_MAC
+    mac::MacMain();
+#endif
+
     QtSingleApplication app(argc, argv);
     if (app.sendMessage("Wake up!"))
         return 0;
index 8d2bb29028719b736b034451adc6a52733a3962e..71cb6874b4748257a5436a8a1195dddb349234ad 100644 (file)
@@ -2,7 +2,7 @@
 #define PLAYLISTWIDGET_H
 
 #include <QtGui>
-#include "thblackbar.h"
+#include "thlibrary/thblackbar.h"
 
 class PlaylistWidget : public QWidget
 {