2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
*
|
|
|
|
* Strawberry is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Strawberry is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Strawberry. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-09 18:39:44 +02:00
|
|
|
*
|
2018-02-27 18:06:05 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
#include <dbus/gnomesettingsdaemon.h>
|
2018-07-03 19:48:08 +02:00
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
#include <QObject>
|
|
|
|
#include <QCoreApplication>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QDateTime>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QMap>
|
2019-01-01 20:07:29 +01:00
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusMessage>
|
|
|
|
#include <QDBusPendingCallWatcher>
|
|
|
|
#include <QDBusPendingReply>
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "core/closure.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "globalshortcuts.h"
|
2019-01-01 20:07:29 +01:00
|
|
|
#include "globalshortcutbackend.h"
|
|
|
|
#include "globalshortcutbackend-dbus.h"
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
const char *GlobalShortcutBackendDBus::kGsdService = "org.gnome.SettingsDaemon";
|
|
|
|
const char *GlobalShortcutBackendDBus::kGsdPath = "/org/gnome/SettingsDaemon/MediaKeys";
|
|
|
|
const char *GlobalShortcutBackendDBus::kGsdInterface = "org.gnome.SettingsDaemon.MediaKeys";
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
GlobalShortcutBackendDBus::GlobalShortcutBackendDBus(GlobalShortcuts *parent)
|
2018-02-27 18:06:05 +01:00
|
|
|
: GlobalShortcutBackend(parent),
|
|
|
|
interface_(nullptr),
|
|
|
|
is_connected_(false) {}
|
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
GlobalShortcutBackendDBus::~GlobalShortcutBackendDBus(){}
|
|
|
|
|
|
|
|
bool GlobalShortcutBackendDBus::DoRegister() {
|
|
|
|
|
|
|
|
qLog(Debug) << "Registering";
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
// Check if the GSD service is available
|
|
|
|
if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService)) {
|
2019-01-01 20:07:29 +01:00
|
|
|
qLog(Warning) << "Gnome settings daemon not registered";
|
2018-02-27 18:06:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!interface_) {
|
|
|
|
interface_ = new OrgGnomeSettingsDaemonMediaKeysInterface(kGsdService, kGsdPath, QDBusConnection::sessionBus(), this);
|
|
|
|
}
|
|
|
|
|
|
|
|
QDBusPendingReply<> reply = interface_->GrabMediaPlayerKeys(QCoreApplication::applicationName(), QDateTime::currentDateTime().toTime_t());
|
|
|
|
|
|
|
|
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
|
|
|
|
NewClosure(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), this, SLOT(RegisterFinished(QDBusPendingCallWatcher*)), watcher);
|
|
|
|
|
|
|
|
return true;
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
void GlobalShortcutBackendDBus::RegisterFinished(QDBusPendingCallWatcher *watcher) {
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
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;
|
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
qLog(Debug) << "Registered";
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
void GlobalShortcutBackendDBus::DoUnregister() {
|
2018-05-01 00:41:33 +02:00
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
qLog(Debug) << "Unregister";
|
2018-07-03 19:48:08 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
// Check if the GSD service is available
|
|
|
|
if (!QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService))
|
|
|
|
return;
|
|
|
|
if (!interface_ || !is_connected_) return;
|
|
|
|
|
|
|
|
is_connected_ = false;
|
|
|
|
|
|
|
|
interface_->ReleaseMediaPlayerKeys(QCoreApplication::applicationName());
|
2018-05-01 00:41:33 +02:00
|
|
|
disconnect(interface_, SIGNAL(MediaPlayerKeyPressed(QString, QString)), this, SLOT(GnomeMediaKeyPressed(QString, QString)));
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
void GlobalShortcutBackendDBus::GnomeMediaKeyPressed(const QString&, const QString& key) {
|
2018-02-27 18:06:05 +01:00
|
|
|
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();
|
|
|
|
}
|