2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
2014-11-02 19:36:21 +01:00
|
|
|
Copyright 2010, Paweł Bara <keirangtp@gmail.com>
|
|
|
|
Copyright 2010-2011, David Sansome <davidsansome@gmail.com>
|
|
|
|
Copyright 2010, 2014, John Maguire <john.maguire@gmail.com>
|
|
|
|
Copyright 2013, Alexander Bikadorov <abiku@cs.tu-berlin.de>
|
2014-11-02 19:40:52 +01:00
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2010-03-24 00:11:46 +01:00
|
|
|
|
|
|
|
Clementine 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.
|
|
|
|
|
|
|
|
Clementine 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 Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-03-21 20:02:56 +01:00
|
|
|
#include "globalshortcuts.h"
|
2010-04-13 15:55:54 +02:00
|
|
|
|
2010-05-17 13:06:50 +02:00
|
|
|
#include <QAction>
|
2013-11-11 19:22:52 +01:00
|
|
|
#include <QShortcut>
|
|
|
|
#include <QtDebug>
|
2010-03-25 23:37:00 +01:00
|
|
|
|
2020-09-18 16:15:19 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "gnomeglobalshortcutbackend.h"
|
|
|
|
#include "kglobalaccelglobalshortcutbackend.h"
|
|
|
|
#include "mac_startup.h"
|
|
|
|
#include "macglobalshortcutbackend.h"
|
|
|
|
#include "qxtglobalshortcutbackend.h"
|
|
|
|
|
2018-11-29 22:32:12 +01:00
|
|
|
#ifdef HAVE_DBUS
|
2014-02-07 16:34:20 +01:00
|
|
|
#include <QtDBus>
|
2010-03-22 00:19:56 +01:00
|
|
|
#endif
|
2010-03-21 20:02:56 +01:00
|
|
|
|
2010-05-17 13:06:50 +02:00
|
|
|
const char* GlobalShortcuts::kSettingsGroup = "Shortcuts";
|
2010-03-21 20:02:56 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
GlobalShortcuts::GlobalShortcuts(QWidget* parent)
|
|
|
|
: QWidget(parent),
|
|
|
|
gnome_backend_(nullptr),
|
|
|
|
system_backend_(nullptr),
|
2019-12-06 05:25:28 +01:00
|
|
|
use_gnome_(false),
|
2019-12-07 20:30:10 +01:00
|
|
|
have_kglobalaccel_(
|
2019-12-09 17:25:29 +01:00
|
|
|
KGlobalAccelShortcutBackend::IsKGlobalAccelAvailable()) {
|
2010-05-17 13:06:50 +02:00
|
|
|
settings_.beginGroup(kSettingsGroup);
|
|
|
|
|
|
|
|
// Create actions
|
|
|
|
AddShortcut("play", tr("Play"), SIGNAL(Play()));
|
|
|
|
AddShortcut("pause", tr("Pause"), SIGNAL(Pause()));
|
2014-02-07 16:34:20 +01:00
|
|
|
AddShortcut("play_pause", tr("Play/Pause"), SIGNAL(PlayPause()),
|
|
|
|
QKeySequence(Qt::Key_MediaPlay));
|
|
|
|
AddShortcut("stop", tr("Stop"), SIGNAL(Stop()),
|
|
|
|
QKeySequence(Qt::Key_MediaStop));
|
|
|
|
AddShortcut("stop_after", tr("Stop playing after current track"),
|
|
|
|
SIGNAL(StopAfter()));
|
|
|
|
AddShortcut("next_track", tr("Next track"), SIGNAL(Next()),
|
|
|
|
QKeySequence(Qt::Key_MediaNext));
|
|
|
|
AddShortcut("prev_track", tr("Previous track"), SIGNAL(Previous()),
|
|
|
|
QKeySequence(Qt::Key_MediaPrevious));
|
2010-05-17 13:06:50 +02:00
|
|
|
AddShortcut("inc_volume", tr("Increase volume"), SIGNAL(IncVolume()));
|
|
|
|
AddShortcut("dec_volume", tr("Decrease volume"), SIGNAL(DecVolume()));
|
|
|
|
AddShortcut("mute", tr("Mute"), SIGNAL(Mute()));
|
|
|
|
AddShortcut("seek_forward", tr("Seek forward"), SIGNAL(SeekForward()));
|
|
|
|
AddShortcut("seek_backward", tr("Seek backward"), SIGNAL(SeekBackward()));
|
2010-07-24 14:39:09 +02:00
|
|
|
AddShortcut("show_hide", tr("Show/Hide"), SIGNAL(ShowHide()));
|
|
|
|
AddShortcut("show_osd", tr("Show OSD"), SIGNAL(ShowOSD()));
|
2014-02-07 16:34:20 +01:00
|
|
|
AddShortcut(
|
|
|
|
"toggle_pretty_osd", tr("Toggle Pretty OSD"),
|
|
|
|
SIGNAL(TogglePrettyOSD())); // Toggling possible only for pretty OSD
|
|
|
|
AddShortcut("shuffle_mode", tr("Change shuffle mode"),
|
|
|
|
SIGNAL(CycleShuffleMode()));
|
|
|
|
AddShortcut("repeat_mode", tr("Change repeat mode"),
|
|
|
|
SIGNAL(CycleRepeatMode()));
|
|
|
|
AddShortcut("toggle_last_fm_scrobbling",
|
|
|
|
tr("Enable/disable Last.fm scrobbling"),
|
|
|
|
SIGNAL(ToggleScrobbling()));
|
2015-06-17 14:21:24 +02:00
|
|
|
AddShortcut("love_last_fm_scrobbling", tr("Love (Last.fm scrobbling)"),
|
|
|
|
SIGNAL(Love()));
|
|
|
|
AddShortcut("ban_last_fm_scrobbling", tr("Ban (Last.fm scrobbling)"),
|
|
|
|
SIGNAL(Ban()));
|
2017-05-31 18:59:36 +02:00
|
|
|
AddShortcut("remove_current_song_from_playlist",
|
|
|
|
tr("Remove current song from playlist"),
|
|
|
|
SIGNAL(RemoveCurrentSong()));
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2019-11-10 15:07:12 +01:00
|
|
|
AddRatingShortcut("rate_zero_star", tr("Rate the current song 0 stars"), 0);
|
|
|
|
AddRatingShortcut("rate_one_star", tr("Rate the current song 1 star"), 1);
|
|
|
|
AddRatingShortcut("rate_two_star", tr("Rate the current song 2 stars"), 2);
|
|
|
|
AddRatingShortcut("rate_three_star", tr("Rate the current song 3 stars"), 3);
|
|
|
|
AddRatingShortcut("rate_four_star", tr("Rate the current song 4 stars"), 4);
|
|
|
|
AddRatingShortcut("rate_five_star", tr("Rate the current song 5 stars"), 5);
|
2010-12-17 01:21:20 +01:00
|
|
|
|
2010-05-17 13:06:50 +02:00
|
|
|
// Create backends - these do the actual shortcut registration
|
2011-01-10 00:11:51 +01:00
|
|
|
gnome_backend_ = new GnomeGlobalShortcutBackend(this);
|
2019-12-06 05:25:28 +01:00
|
|
|
kglobalaccel_backend_ = new KGlobalAccelShortcutBackend(this);
|
2010-03-21 20:02:56 +01:00
|
|
|
|
2010-05-17 13:06:50 +02:00
|
|
|
#ifndef Q_OS_DARWIN
|
|
|
|
system_backend_ = new QxtGlobalShortcutBackend(this);
|
2010-05-27 15:21:05 +02:00
|
|
|
#else
|
2010-06-17 22:31:34 +02:00
|
|
|
system_backend_ = new MacGlobalShortcutBackend(this);
|
2010-04-13 15:55:54 +02:00
|
|
|
#endif
|
2010-03-21 20:02:56 +01:00
|
|
|
|
2010-05-17 13:06:50 +02:00
|
|
|
ReloadSettings();
|
2010-05-17 02:31:39 +02:00
|
|
|
}
|
|
|
|
|
2010-12-17 01:21:20 +01:00
|
|
|
void GlobalShortcuts::AddShortcut(const QString& id, const QString& name,
|
2010-05-17 13:06:50 +02:00
|
|
|
const char* signal,
|
2010-12-17 01:21:20 +01:00
|
|
|
const QKeySequence& default_key) {
|
|
|
|
Shortcut shortcut = AddShortcut(id, name, default_key);
|
|
|
|
connect(shortcut.action, SIGNAL(triggered()), this, signal);
|
|
|
|
}
|
|
|
|
|
2019-11-10 15:16:39 +01:00
|
|
|
void GlobalShortcuts::AddRatingShortcut(const QString& id, const QString& name,
|
|
|
|
int rating,
|
2010-12-17 01:21:20 +01:00
|
|
|
const QKeySequence& default_key) {
|
|
|
|
Shortcut shortcut = AddShortcut(id, name, default_key);
|
2019-11-10 15:16:39 +01:00
|
|
|
connect(shortcut.action, &QAction::triggered,
|
|
|
|
[this, rating]() { RateCurrentSong(rating); });
|
2010-12-17 01:21:20 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
GlobalShortcuts::Shortcut GlobalShortcuts::AddShortcut(
|
|
|
|
const QString& id, const QString& name, const QKeySequence& default_key) {
|
2010-05-17 13:06:50 +02:00
|
|
|
Shortcut shortcut;
|
|
|
|
shortcut.action = new QAction(name, this);
|
2013-11-11 19:22:52 +01:00
|
|
|
QKeySequence key_sequence = QKeySequence::fromString(
|
|
|
|
settings_.value(id, default_key.toString()).toString());
|
|
|
|
shortcut.action->setShortcut(key_sequence);
|
2010-05-17 13:06:50 +02:00
|
|
|
shortcut.id = id;
|
|
|
|
shortcut.default_key = default_key;
|
2010-03-21 20:02:56 +01:00
|
|
|
|
2013-11-11 19:22:52 +01:00
|
|
|
// Create application wide QShortcut to hide keyevents mapped to global
|
|
|
|
// shortcuts from widgets.
|
|
|
|
shortcut.shortcut = new QShortcut(key_sequence, this);
|
|
|
|
shortcut.shortcut->setContext(Qt::ApplicationShortcut);
|
|
|
|
|
2010-05-17 13:06:50 +02:00
|
|
|
shortcuts_[id] = shortcut;
|
2010-12-17 01:21:20 +01:00
|
|
|
|
|
|
|
return shortcut;
|
2010-05-17 13:06:50 +02:00
|
|
|
}
|
2010-03-21 20:02:56 +01:00
|
|
|
|
2010-05-17 13:06:50 +02:00
|
|
|
bool GlobalShortcuts::IsGsdAvailable() const {
|
2018-11-29 22:32:12 +01:00
|
|
|
#ifdef HAVE_DBUS
|
2010-05-17 13:06:50 +02:00
|
|
|
return QDBusConnection::sessionBus().interface()->isServiceRegistered(
|
2019-01-09 05:11:28 +01:00
|
|
|
GnomeGlobalShortcutBackend::kGsdService) ||
|
|
|
|
QDBusConnection::sessionBus().interface()->isServiceRegistered(
|
|
|
|
GnomeGlobalShortcutBackend::kGsdService2);
|
2018-11-29 22:32:12 +01:00
|
|
|
#else // HAVE_DBUS
|
2010-03-22 00:19:56 +01:00
|
|
|
return false;
|
|
|
|
#endif
|
2010-03-21 20:02:56 +01:00
|
|
|
}
|
|
|
|
|
2010-05-17 13:06:50 +02:00
|
|
|
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.
|
2010-07-10 21:17:21 +02:00
|
|
|
use_gnome_ = settings_.value("use_gnome", true).toBool();
|
2010-05-17 13:06:50 +02:00
|
|
|
|
2010-07-10 21:17:21 +02:00
|
|
|
Unregister();
|
|
|
|
Register();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalShortcuts::Unregister() {
|
2014-02-07 16:34:20 +01:00
|
|
|
if (gnome_backend_->is_active()) gnome_backend_->Unregister();
|
2019-12-06 05:25:28 +01:00
|
|
|
if (kglobalaccel_backend_->is_active()) kglobalaccel_backend_->Unregister();
|
2014-02-07 16:34:20 +01:00
|
|
|
if (system_backend_->is_active()) system_backend_->Unregister();
|
2010-07-10 21:17:21 +02:00
|
|
|
}
|
2010-05-17 13:06:50 +02:00
|
|
|
|
2010-07-10 21:17:21 +02:00
|
|
|
void GlobalShortcuts::Register() {
|
2019-12-07 20:30:10 +01:00
|
|
|
if (have_kglobalaccel_ && kglobalaccel_backend_->Register()) return;
|
2014-02-07 16:34:20 +01:00
|
|
|
if (use_gnome_ && gnome_backend_->Register()) return;
|
2011-01-22 13:47:19 +01:00
|
|
|
system_backend_->Register();
|
2010-05-17 13:06:50 +02:00
|
|
|
}
|
2010-06-18 00:15:07 +02:00
|
|
|
|
|
|
|
bool GlobalShortcuts::IsMacAccessibilityEnabled() const {
|
|
|
|
#ifdef Q_OS_MAC
|
2014-02-07 16:34:20 +01:00
|
|
|
return static_cast<MacGlobalShortcutBackend*>(system_backend_)
|
|
|
|
->IsAccessibilityEnabled();
|
2010-06-18 00:15:07 +02:00
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalShortcuts::ShowMacAccessibilityDialog() {
|
|
|
|
#ifdef Q_OS_MAC
|
2014-02-07 16:34:20 +01:00
|
|
|
static_cast<MacGlobalShortcutBackend*>(system_backend_)
|
|
|
|
->ShowAccessibilityDialog();
|
2010-06-18 00:15:07 +02:00
|
|
|
#endif
|
|
|
|
}
|