strawberry-audio-player-win.../src/globalshortcuts/globalshortcutsbackend-gsd.cpp

120 lines
4.1 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
* This file was part of Clementine.
* Copyright 2010, David Sansome <me@davidsansome.com>
2021-03-20 21:14:47 +01:00
* Copyright 2019-2021, Jonas Kvinge <jonas@jkvinge.net>
2018-02-27 18:06:05 +01:00
*
* 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"
#include <dbus/gnomesettingsdaemon.h>
2018-07-03 19:48:08 +02:00
#include <QObject>
#include <QCoreApplication>
2018-02-27 18:06:05 +01:00
#include <QDateTime>
#include <QMap>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
2020-02-09 02:29:35 +01:00
#include <QAction>
#include <QtDebug>
2018-02-27 18:06:05 +01:00
#include "core/logging.h"
2021-01-26 19:13:29 +01:00
#include "globalshortcutsmanager.h"
#include "globalshortcutsbackend.h"
#include "globalshortcutsbackend-gsd.h"
2021-01-26 19:13:29 +01:00
const char *GlobalShortcutsBackendGSD::kGsdService = "org.gnome.SettingsDaemon.MediaKeys";
const char *GlobalShortcutsBackendGSD::kGsdService2 = "org.gnome.SettingsDaemon";
const char *GlobalShortcutsBackendGSD::kGsdPath = "/org/gnome/SettingsDaemon/MediaKeys";
2018-02-27 18:06:05 +01:00
2021-01-26 19:13:29 +01:00
GlobalShortcutsBackendGSD::GlobalShortcutsBackendGSD(GlobalShortcutsManager *parent)
: GlobalShortcutsBackend(parent),
2018-02-27 18:06:05 +01:00
interface_(nullptr),
is_connected_(false) {}
2021-01-26 19:13:29 +01:00
bool GlobalShortcutsBackendGSD::DoRegister() {
qLog(Debug) << "Registering";
2019-01-08 23:25:50 +01:00
if (!interface_) {
if (QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService)) {
interface_ = new OrgGnomeSettingsDaemonMediaKeysInterface(kGsdService, kGsdPath, QDBusConnection::sessionBus(), this);
}
else if (QDBusConnection::sessionBus().interface()->isServiceRegistered(kGsdService2)) {
interface_ = new OrgGnomeSettingsDaemonMediaKeysInterface(kGsdService2, kGsdPath, QDBusConnection::sessionBus(), this);
}
2018-02-27 18:06:05 +01:00
}
if (!interface_) {
2019-01-08 23:25:50 +01:00
qLog(Warning) << "Gnome settings daemon not registered";
return false;
2018-02-27 18:06:05 +01:00
}
QDBusPendingReply<> reply = interface_->GrabMediaPlayerKeys(QCoreApplication::applicationName(), QDateTime::currentDateTime().toSecsSinceEpoch());
2018-02-27 18:06:05 +01:00
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
2021-01-26 19:13:29 +01:00
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, &GlobalShortcutsBackendGSD::RegisterFinished);
2018-02-27 18:06:05 +01:00
return true;
2018-02-27 18:06:05 +01:00
}
2021-01-26 19:13:29 +01:00
void GlobalShortcutsBackendGSD::RegisterFinished(QDBusPendingCallWatcher *watcher) {
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;
}
2021-01-26 19:13:29 +01:00
QObject::connect(interface_, &OrgGnomeSettingsDaemonMediaKeysInterface::MediaPlayerKeyPressed, this, &GlobalShortcutsBackendGSD::GnomeMediaKeyPressed);
2018-02-27 18:06:05 +01:00
is_connected_ = true;
qLog(Debug) << "Registered";
2018-02-27 18:06:05 +01:00
}
2021-01-26 19:13:29 +01:00
void GlobalShortcutsBackendGSD::DoUnregister() {
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());
2021-01-26 19:13:29 +01:00
QObject::disconnect(interface_, &OrgGnomeSettingsDaemonMediaKeysInterface::MediaPlayerKeyPressed, this, &GlobalShortcutsBackendGSD::GnomeMediaKeyPressed);
2018-02-27 18:06:05 +01:00
}
2021-03-21 04:47:11 +01:00
void GlobalShortcutsBackendGSD::GnomeMediaKeyPressed(const QString&, const QString &key) {
auto shortcuts = manager_->shortcuts();
if (key == "Play") shortcuts["play_pause"].action->trigger();
if (key == "Stop") shortcuts["stop"].action->trigger();
if (key == "Next") shortcuts["next_track"].action->trigger();
if (key == "Previous") shortcuts["prev_track"].action->trigger();
2018-02-27 18:06:05 +01:00
}