Register and unregister for media key notifications properly from the gnome settings daemon

This commit is contained in:
David Sansome 2011-10-14 22:01:25 +01:00
parent dea15c91ba
commit 2bffe1fa82
2 changed files with 39 additions and 5 deletions

View File

@ -17,6 +17,7 @@
#include "gnomeglobalshortcutbackend.h"
#include "globalshortcuts.h"
#include "core/closure.h"
#include "core/logging.h"
#ifdef QT_DBUS_LIB
@ -24,6 +25,8 @@
#endif
#include <QAction>
#include <QCoreApplication>
#include <QDateTime>
#include <QtDebug>
#ifdef QT_DBUS_LIB
@ -36,10 +39,12 @@ const char* GnomeGlobalShortcutBackend::kGsdInterface = "org.gnome.SettingsDaemo
GnomeGlobalShortcutBackend::GnomeGlobalShortcutBackend(GlobalShortcuts* parent)
: GlobalShortcutBackend(parent),
interface_(NULL)
interface_(NULL),
is_connected_(false)
{
}
bool GnomeGlobalShortcutBackend::DoRegister() {
#ifdef QT_DBUS_LIB
qLog(Debug) << "registering";
@ -54,10 +59,14 @@ bool GnomeGlobalShortcutBackend::DoRegister() {
kGsdService, kGsdPath, QDBusConnection::sessionBus(), this);
}
connect(interface_, SIGNAL(MediaPlayerKeyPressed(QString,QString)),
this, SLOT(GnomeMediaKeyPressed(QString,QString)));
QDBusPendingReply<> reply =
interface_->GrabMediaPlayerKeys(QCoreApplication::applicationName(),
QDateTime::currentDateTime().toTime_t());
qLog(Debug) << "registered";
QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(reply, this);
NewClosure(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
this, SLOT(RegisterFinished(QDBusPendingCallWatcher*)),
watcher);
return true;
#else // QT_DBUS_LIB
@ -66,15 +75,35 @@ bool GnomeGlobalShortcutBackend::DoRegister() {
#endif
}
void GnomeGlobalShortcutBackend::RegisterFinished(QDBusPendingCallWatcher* watcher) {
QDBusMessage reply = watcher->reply();
watcher->deleteLater();
if (reply.type() == QDBusMessage::ErrorMessage) {
qLog(Warning) << "Failed to grab media keys"
<< reply.errorName() <<reply.errorMessage();
return;
}
connect(interface_, SIGNAL(MediaPlayerKeyPressed(QString,QString)),
this, SLOT(GnomeMediaKeyPressed(QString,QString)));
is_connected_ = true;
qLog(Debug) << "registered";
}
void GnomeGlobalShortcutBackend::DoUnregister() {
qLog(Debug) << "unregister";
#ifdef QT_DBUS_LIB
// Check if the GSD service is available
if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService))
return;
if (!interface_)
if (!interface_ || !is_connected_)
return;
is_connected_ = false;
interface_->ReleaseMediaPlayerKeys(QCoreApplication::applicationName());
disconnect(interface_, SIGNAL(MediaPlayerKeyPressed(QString,QString)),
this, SLOT(GnomeMediaKeyPressed(QString,QString)));
#endif

View File

@ -22,6 +22,8 @@
class OrgGnomeSettingsDaemonMediaKeysInterface;
class QDBusPendingCallWatcher;
class GnomeGlobalShortcutBackend : public GlobalShortcutBackend {
Q_OBJECT
@ -38,10 +40,13 @@ protected:
void DoUnregister();
private slots:
void RegisterFinished(QDBusPendingCallWatcher* watcher);
void GnomeMediaKeyPressed(const QString& application, const QString& key);
private:
OrgGnomeSettingsDaemonMediaKeysInterface* interface_;
bool is_connected_;
};
#endif // GNOMEGLOBALSHORTCUTBACKEND_H