strawberry-audio-player-win.../src/globalshortcuts/globalshortcutsbackend-mate...

120 lines
4.1 KiB
C++
Raw Normal View History

2018-02-27 18:06:05 +01:00
/*
* Strawberry Music Player
2021-05-29 20:35:55 +02:00
* Copyright 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"
2021-05-29 20:35:55 +02:00
#include <dbus/matesettingsdaemon.h>
2018-07-03 19:48:08 +02:00
#include <QObject>
#include <QCoreApplication>
2018-02-27 18:06:05 +01:00
#include <QDateTime>
#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"
2021-05-29 20:35:55 +02:00
#include "globalshortcutsbackend-mate.h"
2021-05-29 20:35:55 +02:00
const char *GlobalShortcutsBackendMate::kService1 = "org.mate.SettingsDaemon.MediaKeys";
const char *GlobalShortcutsBackendMate::kService2 = "org.mate.SettingsDaemon";
const char *GlobalShortcutsBackendMate::kPath = "/org/mate/SettingsDaemon/MediaKeys";
2018-02-27 18:06:05 +01:00
2021-06-20 19:04:08 +02:00
GlobalShortcutsBackendMate::GlobalShortcutsBackendMate(GlobalShortcutsManager *manager, QObject *parent)
: GlobalShortcutsBackend(manager, parent),
2018-02-27 18:06:05 +01:00
interface_(nullptr),
is_connected_(false) {}
2021-05-29 20:35:55 +02:00
bool GlobalShortcutsBackendMate::IsAvailable() {
return QDBusConnection::sessionBus().interface()->isServiceRegistered(kService1) || QDBusConnection::sessionBus().interface()->isServiceRegistered(kService2);
}
bool GlobalShortcutsBackendMate::DoRegister() {
qLog(Debug) << "Registering";
2019-01-08 23:25:50 +01:00
if (!interface_) {
2021-05-29 20:35:55 +02:00
if (QDBusConnection::sessionBus().interface()->isServiceRegistered(kService1)) {
interface_ = new OrgMateSettingsDaemonMediaKeysInterface(kService1, kPath, QDBusConnection::sessionBus(), this);
2019-01-08 23:25:50 +01:00
}
2021-05-29 20:35:55 +02:00
else if (QDBusConnection::sessionBus().interface()->isServiceRegistered(kService2)) {
interface_ = new OrgMateSettingsDaemonMediaKeysInterface(kService2, kPath, QDBusConnection::sessionBus(), this);
2019-01-08 23:25:50 +01:00
}
2018-02-27 18:06:05 +01:00
}
if (!interface_) {
2021-05-29 20:35:55 +02:00
qLog(Warning) << "Mate settings daemon not registered";
2019-01-08 23:25:50 +01:00
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-05-29 20:35:55 +02:00
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, &GlobalShortcutsBackendMate::RegisterFinished);
2018-02-27 18:06:05 +01:00
return true;
2018-02-27 18:06:05 +01:00
}
2021-05-29 20:35:55 +02:00
void GlobalShortcutsBackendMate::RegisterFinished(QDBusPendingCallWatcher *watcher) {
2018-02-27 18:06:05 +01:00
QDBusMessage reply = watcher->reply();
watcher->deleteLater();
if (reply.type() == QDBusMessage::ErrorMessage) {
2021-07-11 09:49:38 +02:00
qLog(Warning) << "Failed to grab media keys" << reply.errorName() << reply.errorMessage();
2018-02-27 18:06:05 +01:00
return;
}
2021-05-29 20:35:55 +02:00
QObject::connect(interface_, &OrgMateSettingsDaemonMediaKeysInterface::MediaPlayerKeyPressed, this, &GlobalShortcutsBackendMate::MateMediaKeyPressed);
2018-02-27 18:06:05 +01:00
is_connected_ = true;
2021-05-29 20:35:55 +02:00
qLog(Debug) << "Registered.";
2018-02-27 18:06:05 +01:00
}
2021-05-29 20:35:55 +02:00
void GlobalShortcutsBackendMate::DoUnregister() {
qLog(Debug) << "Unregister";
2018-07-03 19:48:08 +02:00
2021-05-29 20:35:55 +02:00
if (!IsAvailable() || !interface_ || !is_connected_) return;
2018-02-27 18:06:05 +01:00
is_connected_ = false;
interface_->ReleaseMediaPlayerKeys(QCoreApplication::applicationName());
2021-05-29 20:35:55 +02:00
QObject::disconnect(interface_, &OrgMateSettingsDaemonMediaKeysInterface::MediaPlayerKeyPressed, this, &GlobalShortcutsBackendMate::MateMediaKeyPressed);
2018-02-27 18:06:05 +01:00
}
2021-05-29 20:35:55 +02:00
void GlobalShortcutsBackendMate::MateMediaKeyPressed(const QString&, const QString &key) {
2021-03-21 04:47:11 +01:00
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
}