strawberry-audio-player-win.../src/globalshortcuts/globalshortcutsmanager.cpp

239 lines
7.6 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 2018-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 <functional>
#include <QApplication>
#include <QWidget>
#include <QString>
2018-02-27 18:06:05 +01:00
#include <QAction>
#include <QShortcut>
#include <QKeySequence>
2018-02-27 18:06:05 +01:00
2021-01-26 19:13:29 +01:00
#include "globalshortcutsmanager.h"
#include "globalshortcutsbackend.h"
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) && defined(HAVE_DBUS)
2021-01-26 19:13:29 +01:00
# include "globalshortcutsbackend-kde.h"
2021-05-29 20:35:55 +02:00
# include "globalshortcutsbackend-gnome.h"
# include "globalshortcutsbackend-mate.h"
#endif
#ifdef HAVE_X11_GLOBALSHORTCUTS
# include "globalshortcutsbackend-x11.h"
2018-07-01 22:26:46 +02:00
#endif
#ifdef Q_OS_WIN
# include "globalshortcutsbackend-win.h"
#endif
#ifdef Q_OS_MACOS
2021-01-26 19:13:29 +01:00
# include "globalshortcutsbackend-macos.h"
#endif
2021-01-26 19:13:29 +01:00
#include "settings/globalshortcutssettingspage.h"
GlobalShortcutsManager::GlobalShortcutsManager(QWidget *parent) : QWidget(parent) {
2018-02-27 18:06:05 +01:00
settings_.beginGroup(GlobalShortcutsSettingsPage::kSettingsGroup);
// Create actions
AddShortcut("play", tr("Play"), std::bind(&GlobalShortcutsManager::Play, this));
AddShortcut("pause", tr("Pause"), std::bind(&GlobalShortcutsManager::Pause, this));
AddShortcut("play_pause", tr("Play/Pause"), std::bind(&GlobalShortcutsManager::PlayPause, this), QKeySequence(Qt::Key_MediaPlay));
AddShortcut("stop", tr("Stop"), std::bind(&GlobalShortcutsManager::Stop, this), QKeySequence(Qt::Key_MediaStop));
AddShortcut("stop_after", tr("Stop playing after current track"), std::bind(&GlobalShortcutsManager::StopAfter, this));
AddShortcut("next_track", tr("Next track"), std::bind(&GlobalShortcutsManager::Next, this), QKeySequence(Qt::Key_MediaNext));
AddShortcut("prev_track", tr("Previous track"), std::bind(&GlobalShortcutsManager::Previous, this), QKeySequence(Qt::Key_MediaPrevious));
AddShortcut("inc_volume", tr("Increase volume"), std::bind(&GlobalShortcutsManager::IncVolume, this));
AddShortcut("dec_volume", tr("Decrease volume"), std::bind(&GlobalShortcutsManager::DecVolume, this));
AddShortcut("mute", tr("Mute"), std::bind(&GlobalShortcutsManager::Mute, this));
AddShortcut("seek_forward", tr("Seek forward"), std::bind(&GlobalShortcutsManager::SeekForward, this));
AddShortcut("seek_backward", tr("Seek backward"), std::bind(&GlobalShortcutsManager::SeekBackward, this));
AddShortcut("show_hide", tr("Show/Hide"), std::bind(&GlobalShortcutsManager::ShowHide, this));
AddShortcut("show_osd", tr("Show OSD"), std::bind(&GlobalShortcutsManager::ShowOSD, this));
AddShortcut("toggle_pretty_osd", tr("Toggle Pretty OSD"), std::bind(&GlobalShortcutsManager::TogglePrettyOSD, this)); // Toggling possible only for pretty OSD
AddShortcut("shuffle_mode", tr("Change shuffle mode"), std::bind(&GlobalShortcutsManager::CycleShuffleMode, this));
AddShortcut("repeat_mode", tr("Change repeat mode"), std::bind(&GlobalShortcutsManager::CycleRepeatMode, this));
AddShortcut("toggle_scrobbling", tr("Enable/disable scrobbling"), std::bind(&GlobalShortcutsManager::ToggleScrobbling, this));
AddShortcut("love", tr("Love"), std::bind(&GlobalShortcutsManager::Love, this));
2018-02-27 18:06:05 +01:00
// Create backends - these do the actual shortcut registration
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) && defined(HAVE_DBUS)
backends_ << new GlobalShortcutsBackendKDE(this, this);
backends_ << new GlobalShortcutsBackendGnome(this, this);
backends_ << new GlobalShortcutsBackendMate(this, this);
#endif
2018-02-27 18:06:05 +01:00
#ifdef Q_OS_MACOS
backends_ << new GlobalShortcutsBackendMacOS(this, this);
2019-02-23 01:10:45 +01:00
#endif
2021-05-29 22:20:46 +02:00
#ifdef Q_OS_WIN
backends_ << new GlobalShortcutsBackendWin(this, this);
2019-02-23 01:10:45 +01:00
#endif
2021-05-29 22:20:46 +02:00
#ifdef HAVE_X11_GLOBALSHORTCUTS
backends_ << new GlobalShortcutsBackendX11(this, this);
2018-02-27 18:06:05 +01:00
#endif
ReloadSettings();
}
2021-01-26 19:13:29 +01:00
void GlobalShortcutsManager::ReloadSettings() {
backends_enabled_.clear();
#ifdef Q_OS_MACOS
backends_enabled_ << GlobalShortcutsBackend::Type_MacOS;
#endif
#ifdef Q_OS_WIN
backends_enabled_ << GlobalShortcutsBackend::Type_Win;
#endif
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) && defined(HAVE_DBUS)
if (settings_.value("use_kde", true).toBool()) {
backends_enabled_ << GlobalShortcutsBackend::Type_KDE;
}
if (settings_.value("use_gnome", true).toBool()) {
backends_enabled_ << GlobalShortcutsBackend::Type_Gnome;
}
if (settings_.value("use_mate", true).toBool()) {
backends_enabled_ << GlobalShortcutsBackend::Type_Mate;
}
#endif
#ifdef HAVE_X11_GLOBALSHORTCUTS
if (settings_.value("use_x11", false).toBool()) {
backends_enabled_ << GlobalShortcutsBackend::Type_X11;
}
#endif
Unregister();
Register();
}
2021-06-20 19:04:08 +02:00
void GlobalShortcutsManager::AddShortcut(const QString &id, const QString &name, std::function<void()> signal, const QKeySequence &default_key) { // clazy:exclude=function-args-by-ref
2018-02-27 18:06:05 +01:00
Shortcut shortcut = AddShortcut(id, name, default_key);
QObject::connect(shortcut.action, &QAction::triggered, this, signal);
2018-02-27 18:06:05 +01:00
}
2021-01-26 19:13:29 +01:00
GlobalShortcutsManager::Shortcut GlobalShortcutsManager::AddShortcut(const QString &id, const QString &name, const QKeySequence &default_key) {
2018-02-27 18:06:05 +01:00
Shortcut shortcut;
shortcut.action = new QAction(name, this);
QKeySequence key_sequence = QKeySequence::fromString(settings_.value(id, default_key.toString()).toString());
shortcut.action->setShortcut(key_sequence);
shortcut.id = id;
shortcut.default_key = default_key;
// Create application wide QShortcut to hide keyevents mapped to global shortcuts from widgets.
2018-02-27 18:06:05 +01:00
shortcut.shortcut = new QShortcut(key_sequence, this);
shortcut.shortcut->setContext(Qt::ApplicationShortcut);
shortcuts_[id] = shortcut;
return shortcut;
}
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) && defined(HAVE_DBUS)
2018-02-27 18:06:05 +01:00
bool GlobalShortcutsManager::IsKdeAvailable() {
return GlobalShortcutsBackendKDE::IsKDEAvailable();
2022-03-22 21:19:59 +01:00
2018-02-27 18:06:05 +01:00
}
bool GlobalShortcutsManager::IsGnomeAvailable() {
2021-05-29 20:35:55 +02:00
return GlobalShortcutsBackendGnome::IsGnomeAvailable();
2022-03-22 21:19:59 +01:00
2021-05-29 20:35:55 +02:00
}
bool GlobalShortcutsManager::IsMateAvailable() {
2020-10-31 02:08:19 +01:00
return GlobalShortcutsBackendMate::IsMateAvailable();
2022-03-22 21:19:59 +01:00
2020-10-31 02:08:19 +01:00
}
2022-03-22 21:09:05 +01:00
#endif // defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) && defined(HAVE_DBUS)
#ifdef HAVE_X11_GLOBALSHORTCUTS
2021-06-22 13:41:38 +02:00
bool GlobalShortcutsManager::IsX11Available() {
2018-02-27 18:06:05 +01:00
return GlobalShortcutsBackendX11::IsX11Available();
2022-03-22 21:19:59 +01:00
2018-02-27 18:06:05 +01:00
}
#endif // HAVE_X11_GLOBALSHORTCUTS
2020-10-31 02:08:19 +01:00
bool GlobalShortcutsManager::Register() {
2021-05-29 22:20:46 +02:00
for (GlobalShortcutsBackend *backend : backends_) {
if (backend->IsAvailable() && backends_enabled_.contains(backend->type())) {
return backend->Register();
2021-05-29 22:20:46 +02:00
}
}
qLog(Warning) << "No global shortcuts enabled.";
return false;
2020-10-31 02:08:19 +01:00
2018-02-27 18:06:05 +01:00
}
2021-01-26 19:13:29 +01:00
void GlobalShortcutsManager::Unregister() {
2020-10-31 02:08:19 +01:00
for (GlobalShortcutsBackend *backend : backends_) {
if (backend->is_active()) {
backend->Unregister();
}
}
2020-10-31 02:08:19 +01:00
2018-02-27 18:06:05 +01:00
}
#ifdef Q_OS_MACOS
bool GlobalShortcutsManager::IsMacAccessibilityEnabled() {
return GlobalShortcutsBackendMacOS::IsAccessibilityEnabled();
2018-02-27 18:06:05 +01:00
}
#endif // Q_OS_MACOS
void GlobalShortcutsManager::ShowMacAccessibilityDialog() {
#ifdef Q_OS_MACOS
GlobalShortcutsBackendMacOS::ShowAccessibilityDialog();
2018-02-27 18:06:05 +01:00
#endif
2018-02-27 18:06:05 +01:00
}