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

182 lines
5.9 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>
* Copyright 2018, 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 <QWidget>
#include <QVariant>
#include <QString>
2018-02-27 18:06:05 +01:00
#include <QAction>
#include <QShortcut>
#include <QKeySequence>
2018-07-03 19:48:08 +02:00
#ifdef HAVE_DBUS
# include <QDBusConnectionInterface>
2018-02-27 18:06:05 +01:00
#endif
#include "core/logging.h"
#include "globalshortcuts.h"
#include "globalshortcutbackend.h"
#ifdef HAVE_DBUS
# include "globalshortcutbackend-gsd.h"
#endif
#if defined(HAVE_X11) || defined(Q_OS_WIN)
# include "globalshortcutbackend-system.h"
2018-07-01 22:26:46 +02:00
#endif
#ifdef Q_OS_MACOS
# include "globalshortcutbackend-macos.h"
#endif
#include "settings/shortcutssettingspage.h"
GlobalShortcuts::GlobalShortcuts(QWidget *parent)
2018-02-27 18:06:05 +01:00
: QWidget(parent),
dbus_backend_(nullptr),
2018-02-27 18:06:05 +01:00
system_backend_(nullptr),
use_gsd_(true),
use_x11_(false)
{
2018-02-27 18:06:05 +01:00
settings_.beginGroup(GlobalShortcutsSettingsPage::kSettingsGroup);
// Create actions
AddShortcut("play", "Play", SIGNAL(Play()));
AddShortcut("pause", "Pause", SIGNAL(Pause()));
AddShortcut("play_pause", "Play/Pause", SIGNAL(PlayPause()), QKeySequence(Qt::Key_MediaPlay));
AddShortcut("stop", "Stop", SIGNAL(Stop()), QKeySequence(Qt::Key_MediaStop));
AddShortcut("stop_after", "Stop playing after current track", SIGNAL(StopAfter()));
AddShortcut("next_track", "Next track", SIGNAL(Next()), QKeySequence(Qt::Key_MediaNext));
AddShortcut("prev_track", "Previous track", SIGNAL(Previous()), QKeySequence(Qt::Key_MediaPrevious));
AddShortcut("inc_volume", "Increase volume", SIGNAL(IncVolume()));
AddShortcut("dec_volume", "Decrease volume", SIGNAL(DecVolume()));
2018-02-27 18:06:05 +01:00
AddShortcut("mute", tr("Mute"), SIGNAL(Mute()));
AddShortcut("seek_forward", "Seek forward", SIGNAL(SeekForward()));
AddShortcut("seek_backward", "Seek backward", SIGNAL(SeekBackward()));
AddShortcut("show_hide", "Show/Hide", SIGNAL(ShowHide()));
AddShortcut("show_osd", "Show OSD", SIGNAL(ShowOSD()));
AddShortcut("toggle_pretty_osd", "Toggle Pretty OSD", SIGNAL(TogglePrettyOSD())); // Toggling possible only for pretty OSD
AddShortcut("shuffle_mode", "Change shuffle mode", SIGNAL(CycleShuffleMode()));
AddShortcut("repeat_mode", "Change repeat mode", SIGNAL(CycleRepeatMode()));
AddShortcut("toggle_scrobbling", "Enable/disable scrobbling", SIGNAL(ToggleScrobbling()));
2018-02-27 18:06:05 +01:00
// Create backends - these do the actual shortcut registration
#ifdef HAVE_DBUS
dbus_backend_ = new GlobalShortcutBackendGSD(this);
#endif
2018-02-27 18:06:05 +01:00
#if defined(HAVE_X11) || defined(Q_OS_WIN)
system_backend_ = new GlobalShortcutBackendSystem(this);
#endif
#ifdef Q_OS_MACOS
system_backend_ = new GlobalShortcutBackendMacOS(this);
2018-02-27 18:06:05 +01:00
#endif
ReloadSettings();
}
void GlobalShortcuts::ReloadSettings() {
// The actual shortcuts have been set in our actions for us by the config dialog already - we just need to reread the gnome settings.
use_gsd_ = settings_.value("use_gsd", true).toBool();
use_x11_ = settings_.value("use_x11", true).toBool();
Unregister();
Register();
}
void GlobalShortcuts::AddShortcut(const QString &id, const QString &name, const char *signal, const QKeySequence &default_key) {
2018-02-27 18:06:05 +01:00
Shortcut shortcut = AddShortcut(id, name, default_key);
connect(shortcut.action, SIGNAL(triggered()), this, signal);
2018-02-27 18:06:05 +01:00
}
GlobalShortcuts::Shortcut GlobalShortcuts::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;
}
bool GlobalShortcuts::IsGsdAvailable() const {
2018-07-03 19:48:08 +02:00
#ifdef HAVE_DBUS
return QDBusConnection::sessionBus().interface()->isServiceRegistered(GlobalShortcutBackendGSD::kGsdService) || QDBusConnection::sessionBus().interface()->isServiceRegistered(GlobalShortcutBackendGSD::kGsdService2);
2018-07-03 19:48:08 +02:00
#else
2018-02-27 18:06:05 +01:00
return false;
#endif
}
bool GlobalShortcuts::IsX11Available() const {
2018-02-27 18:06:05 +01:00
#ifdef HAVE_X11
return true;
#else
return false;
#endif
2018-02-27 18:06:05 +01:00
}
void GlobalShortcuts::Register() {
if (use_gsd_ && dbus_backend_ && dbus_backend_->Register()) return;
#ifdef HAVE_X11 // If this system has X11, only use the system backend if X11 is enabled in the global shortcut settings
if (use_x11_)
#endif
if (system_backend_) system_backend_->Register();
2018-02-27 18:06:05 +01:00
}
void GlobalShortcuts::Unregister() {
if (dbus_backend_ && dbus_backend_->is_active()) dbus_backend_->Unregister();
if (system_backend_ && system_backend_->is_active()) system_backend_->Unregister();
2018-02-27 18:06:05 +01:00
}
bool GlobalShortcuts::IsMacAccessibilityEnabled() const {
#ifdef Q_OS_MACOS
2019-01-01 22:35:23 +01:00
if (system_backend_) return static_cast<GlobalShortcutBackendMacOS*>(system_backend_)->IsAccessibilityEnabled();
else return false;
2018-02-27 18:06:05 +01:00
#else
return true;
#endif
}
void GlobalShortcuts::ShowMacAccessibilityDialog() {
#ifdef Q_OS_MACOS
2019-01-01 22:35:23 +01:00
if (system_backend_) static_cast<GlobalShortcutBackendMacOS*>(system_backend_)->ShowAccessibilityDialog();
2018-02-27 18:06:05 +01:00
#endif
}