strawberry-audio-player-win.../src/settings/globalshortcutssettingspage...

370 lines
12 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 <QWidget>
2020-02-09 02:29:35 +01:00
#include <QList>
#include <QStringList>
#include <QAction>
#include <QHeaderView>
2018-02-27 18:06:05 +01:00
#include <QMessageBox>
#include <QProcess>
#include <QTreeWidget>
#include <QKeySequence>
2018-02-27 18:06:05 +01:00
#include <QShortcut>
#include <QSettings>
#include <QCheckBox>
#include <QGroupBox>
#include <QPushButton>
#include <QRadioButton>
2020-02-09 02:29:35 +01:00
#include <QLabel>
2018-02-27 18:06:05 +01:00
#include "core/iconloader.h"
2020-02-09 02:29:35 +01:00
#include "core/logging.h"
#include "core/settings.h"
#include "utilities/envutils.h"
#include "globalshortcuts/globalshortcutgrabber.h"
2021-01-26 19:13:29 +01:00
#include "globalshortcuts/globalshortcutsmanager.h"
#include "settingspage.h"
#include "settingsdialog.h"
2021-01-26 19:13:29 +01:00
#include "globalshortcutssettingspage.h"
#include "ui_globalshortcutssettingspage.h"
2018-02-27 18:06:05 +01:00
const char *GlobalShortcutsSettingsPage::kSettingsGroup = "GlobalShortcuts";
2021-06-20 19:04:08 +02:00
GlobalShortcutsSettingsPage::GlobalShortcutsSettingsPage(SettingsDialog *dialog, QWidget *parent)
: SettingsPage(dialog, parent),
2018-02-27 18:06:05 +01:00
ui_(new Ui_GlobalShortcutsSettingsPage),
2020-10-17 17:29:09 +02:00
initialized_(false),
grabber_(new GlobalShortcutGrabber()) {
2018-02-27 18:06:05 +01:00
ui_->setupUi(this);
ui_->shortcut_options->setEnabled(false);
ui_->list->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
2024-04-09 23:20:26 +02:00
setWindowIcon(IconLoader::Load(QStringLiteral("keyboard"), true, 0, 32));
2018-02-27 18:06:05 +01:00
2021-01-26 16:48:04 +01:00
QObject::connect(ui_->list, &QTreeWidget::currentItemChanged, this, &GlobalShortcutsSettingsPage::ItemClicked);
QObject::connect(ui_->radio_none, &QRadioButton::clicked, this, &GlobalShortcutsSettingsPage::NoneClicked);
QObject::connect(ui_->radio_default, &QRadioButton::clicked, this, &GlobalShortcutsSettingsPage::DefaultClicked);
QObject::connect(ui_->radio_custom, &QRadioButton::clicked, this, &GlobalShortcutsSettingsPage::ChangeClicked);
QObject::connect(ui_->button_change, &QPushButton::clicked, this, &GlobalShortcutsSettingsPage::ChangeClicked);
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) && defined(HAVE_DBUS)
2021-05-29 18:36:58 +02:00
QObject::connect(ui_->checkbox_kde, &QCheckBox::toggled, this, &GlobalShortcutsSettingsPage::ShortcutOptionsChanged);
2021-05-29 20:35:55 +02:00
QObject::connect(ui_->checkbox_gnome, &QCheckBox::toggled, this, &GlobalShortcutsSettingsPage::ShortcutOptionsChanged);
QObject::connect(ui_->checkbox_mate, &QCheckBox::toggled, this, &GlobalShortcutsSettingsPage::ShortcutOptionsChanged);
QObject::connect(ui_->button_gnome_open, &QPushButton::clicked, this, &GlobalShortcutsSettingsPage::OpenGnomeKeybindingProperties);
QObject::connect(ui_->button_mate_open, &QPushButton::clicked, this, &GlobalShortcutsSettingsPage::OpenMateKeybindingProperties);
#else
ui_->widget_kde->hide();
2021-05-29 20:35:55 +02:00
ui_->widget_gnome->hide();
ui_->widget_mate->hide();
#endif // defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) && defined(HAVE_DBUS)
#ifdef HAVE_X11_GLOBALSHORTCUTS
QObject::connect(ui_->checkbox_x11, &QCheckBox::toggled, this, &GlobalShortcutsSettingsPage::ShortcutOptionsChanged);
#else
ui_->widget_x11->hide();
#endif // HAVE_X11_GLOBALSHORTCUTS
#ifndef Q_OS_MACOS
ui_->widget_macos_access->hide();
#endif // Q_OS_MACOS
2018-02-27 18:06:05 +01:00
}
GlobalShortcutsSettingsPage::~GlobalShortcutsSettingsPage() { delete ui_; }
void GlobalShortcutsSettingsPage::Load() {
Settings s;
2020-10-01 22:04:38 +02:00
s.beginGroup(kSettingsGroup);
2021-01-26 19:13:29 +01:00
GlobalShortcutsManager *manager = dialog()->global_shortcuts_manager();
2018-02-27 18:06:05 +01:00
2020-10-17 17:29:09 +02:00
if (!initialized_) {
initialized_ = true;
2018-02-27 18:06:05 +01:00
de_ = Utilities::DesktopEnvironment();
ui_->widget_warning->hide();
#ifdef Q_OS_MACOS
QObject::connect(ui_->button_macos_preferences, &QPushButton::clicked, manager, &GlobalShortcutsManager::ShowMacAccessibilityDialog);
#endif
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) && defined(HAVE_DBUS)
2018-02-27 18:06:05 +01:00
if (GlobalShortcutsManager::IsKdeAvailable()) {
2021-05-29 20:35:55 +02:00
qLog(Debug) << "KDE (KGlobalAccel) backend is available.";
ui_->widget_kde->show();
}
else {
qLog(Debug) << "KDE (KGlobalAccel) backend is unavailable.";
ui_->widget_kde->hide();
}
if (GlobalShortcutsManager::IsGnomeAvailable()) {
2020-10-31 02:08:19 +01:00
qLog(Debug) << "Gnome (GSD) backend is available.";
2021-05-29 20:35:55 +02:00
ui_->widget_gnome->show();
2019-01-08 23:25:01 +01:00
}
else {
2020-10-31 02:08:19 +01:00
qLog(Debug) << "Gnome (GSD) backend is unavailable.";
2021-05-29 20:35:55 +02:00
ui_->widget_gnome->hide();
}
if (GlobalShortcutsManager::IsMateAvailable()) {
2021-05-29 20:35:55 +02:00
qLog(Debug) << "MATE backend is available.";
ui_->widget_mate->show();
2020-10-31 02:08:19 +01:00
}
else {
2021-05-29 20:35:55 +02:00
qLog(Debug) << "MATE backend is unavailable.";
ui_->widget_mate->hide();
2020-10-31 02:08:19 +01:00
}
#endif // defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) && defined(HAVE_DBUS)
#ifdef HAVE_X11_GLOBALSHORTCUTS
if (GlobalShortcutsManager::IsX11Available()) {
2019-01-08 23:25:01 +01:00
qLog(Debug) << "X11 backend is available.";
ui_->widget_x11->show();
}
else {
qLog(Debug) << "X11 backend is unavailable.";
ui_->widget_x11->hide();
2018-02-27 18:06:05 +01:00
}
#endif // HAVE_X11_GLOBALSHORTCUTS
2018-02-27 18:06:05 +01:00
2024-04-23 17:15:42 +02:00
const QList<GlobalShortcutsManager::Shortcut> shortcuts = manager->shortcuts().values();
2021-03-21 04:47:11 +01:00
for (const GlobalShortcutsManager::Shortcut &i : shortcuts) {
2018-02-27 18:06:05 +01:00
Shortcut shortcut;
2020-10-01 22:04:38 +02:00
shortcut.s = i;
shortcut.key = i.action->shortcut();
shortcut.item = new QTreeWidgetItem(ui_->list, QStringList() << i.action->text() << i.action->shortcut().toString(QKeySequence::NativeText));
shortcut.item->setData(0, Qt::UserRole, i.id);
shortcuts_[i.id] = shortcut;
2018-02-27 18:06:05 +01:00
}
ui_->list->sortItems(0, Qt::AscendingOrder);
ItemClicked(ui_->list->topLevelItem(0));
}
2024-04-23 17:15:42 +02:00
const QList<Shortcut> shortcuts = shortcuts_.values();
2021-03-21 04:47:11 +01:00
for (const Shortcut &shortcut : shortcuts) {
2020-10-01 22:04:38 +02:00
SetShortcut(shortcut.s.id, shortcut.s.action->shortcut());
2018-02-27 18:06:05 +01:00
}
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) && defined(HAVE_DBUS)
2020-10-31 02:08:19 +01:00
if (ui_->widget_kde->isVisibleTo(this)) {
ui_->checkbox_kde->setChecked(s.value("use_kde", true).toBool());
}
2021-05-29 20:35:55 +02:00
if (ui_->widget_gnome->isVisibleTo(this)) {
ui_->checkbox_gnome->setChecked(s.value("use_gnome", true).toBool());
}
if (ui_->widget_mate->isVisibleTo(this)) {
ui_->checkbox_mate->setChecked(s.value("use_mate", true).toBool());
}
2022-03-22 21:09:05 +01:00
#endif // defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) && defined(HAVE_DBUS)
#ifdef HAVE_X11_GLOBALSHORTCUTS
if (ui_->widget_x11->isVisibleTo(this)) {
2020-10-31 02:08:19 +01:00
ui_->checkbox_x11->setChecked(s.value("use_x11", false).toBool());
2018-02-27 18:06:05 +01:00
}
#endif // HAVE_X11_GLOBALSHORTCUTS
2018-02-27 18:06:05 +01:00
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) && (defined(HAVE_DBUS) || defined(HAVE_X11_GLOBALSHORTCUTS))
2020-10-31 02:08:19 +01:00
ShortcutOptionsChanged();
#endif
2019-01-08 23:25:01 +01:00
2018-07-01 22:26:46 +02:00
#ifdef Q_OS_MACOS
ui_->widget_macos_access->setVisible(!GlobalShortcutsManager::IsMacAccessibilityEnabled());
2018-07-01 22:26:46 +02:00
#endif // Q_OS_MACOS
2018-02-27 18:06:05 +01:00
2020-10-01 22:04:38 +02:00
s.endGroup();
Init(ui_->layout_globalshortcutssettingspage->parentWidget());
if (!Settings().childGroups().contains(QLatin1String(kSettingsGroup))) set_changed();
2020-10-12 17:20:18 +02:00
2018-02-27 18:06:05 +01:00
}
void GlobalShortcutsSettingsPage::Save() {
Settings s;
2020-10-01 22:04:38 +02:00
s.beginGroup(kSettingsGroup);
2024-04-23 17:15:42 +02:00
const QList<Shortcut> shortcuts = shortcuts_.values();
2021-03-21 04:47:11 +01:00
for (const Shortcut &shortcut : shortcuts) {
2020-10-01 22:04:38 +02:00
shortcut.s.action->setShortcut(shortcut.key);
shortcut.s.shortcut->setKey(shortcut.key);
s.setValue(shortcut.s.id, shortcut.key.toString());
2018-02-27 18:06:05 +01:00
}
#if defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) && defined(HAVE_DBUS)
2020-10-31 02:08:19 +01:00
s.setValue("use_kde", ui_->checkbox_kde->isChecked());
2021-05-29 20:35:55 +02:00
s.setValue("use_gnome", ui_->checkbox_gnome->isChecked());
s.setValue("use_mate", ui_->checkbox_mate->isChecked());
#endif // defined(Q_OS_UNIX) && !defined(Q_OS_MACOS) && defined(HAVE_DBUS)
#ifdef HAVE_X11_GLOBALSHORTCUTS
2020-10-01 22:04:38 +02:00
s.setValue("use_x11", ui_->checkbox_x11->isChecked());
#endif // HAVE_X11_GLOBALSHORTCUTS
2020-10-01 22:04:38 +02:00
s.endGroup();
2018-02-27 18:06:05 +01:00
dialog()->global_shortcuts_manager()->ReloadSettings();
}
2020-10-31 02:08:19 +01:00
void GlobalShortcutsSettingsPage::ShortcutOptionsChanged() {
bool configure_shortcuts = (ui_->widget_kde->isVisibleTo(this) && ui_->checkbox_kde->isChecked()) ||
(ui_->widget_x11->isVisibleTo(this) && ui_->checkbox_x11->isChecked());
2020-10-31 02:08:19 +01:00
ui_->list->setEnabled(configure_shortcuts);
ui_->shortcut_options->setEnabled(configure_shortcuts);
2020-10-31 02:08:19 +01:00
if (ui_->widget_x11->isVisibleTo(this) && ui_->checkbox_x11->isChecked()) {
ui_->widget_warning->show();
X11Warning();
}
else {
ui_->widget_warning->hide();
}
}
void GlobalShortcutsSettingsPage::OpenGnomeKeybindingProperties() {
2024-04-09 23:20:26 +02:00
if (!QProcess::startDetached(QStringLiteral("gnome-keybinding-properties"), QStringList())) {
if (!QProcess::startDetached(QStringLiteral("gnome-control-center"), QStringList() << QStringLiteral("keyboard"))) {
QMessageBox::warning(this, QStringLiteral("Error"), tr("The \"%1\" command could not be started.").arg(QStringLiteral("gnome-keybinding-properties")));
}
}
}
2021-05-29 20:35:55 +02:00
void GlobalShortcutsSettingsPage::OpenMateKeybindingProperties() {
2024-04-09 23:20:26 +02:00
if (!QProcess::startDetached(QStringLiteral("mate-keybinding-properties"), QStringList())) {
if (!QProcess::startDetached(QStringLiteral("mate-control-center"), QStringList() << QStringLiteral("keyboard"))) {
QMessageBox::warning(this, QStringLiteral("Error"), tr("The \"%1\" command could not be started.").arg(QStringLiteral("mate-keybinding-properties")));
2021-05-29 20:35:55 +02:00
}
}
}
void GlobalShortcutsSettingsPage::SetShortcut(const QString &id, const QKeySequence &key) {
Shortcut &shortcut = shortcuts_[id];
shortcut.key = key;
shortcut.item->setText(1, key.toString(QKeySequence::NativeText));
}
2018-02-27 18:06:05 +01:00
void GlobalShortcutsSettingsPage::ItemClicked(QTreeWidgetItem *item) {
current_id_ = item->data(0, Qt::UserRole).toString();
Shortcut &shortcut = shortcuts_[current_id_];
// Enable options
ui_->shortcut_options->setEnabled(true);
ui_->shortcut_options->setTitle(tr("Shortcut for %1").arg(shortcut.s.action->text()));
2021-08-23 21:21:08 +02:00
if (shortcut.key == shortcut.s.default_key) {
2018-02-27 18:06:05 +01:00
ui_->radio_default->setChecked(true);
2021-08-23 21:21:08 +02:00
}
else if (shortcut.key.isEmpty()) {
2018-02-27 18:06:05 +01:00
ui_->radio_none->setChecked(true);
2021-08-23 21:21:08 +02:00
}
else {
2018-02-27 18:06:05 +01:00
ui_->radio_custom->setChecked(true);
2021-08-23 21:21:08 +02:00
}
2018-02-27 18:06:05 +01:00
}
void GlobalShortcutsSettingsPage::NoneClicked() {
2018-02-27 18:06:05 +01:00
SetShortcut(current_id_, QKeySequence());
set_changed();
2018-02-27 18:06:05 +01:00
}
void GlobalShortcutsSettingsPage::DefaultClicked() {
2018-02-27 18:06:05 +01:00
SetShortcut(current_id_, shortcuts_[current_id_].s.default_key);
set_changed();
2018-02-27 18:06:05 +01:00
}
void GlobalShortcutsSettingsPage::ChangeClicked() {
2018-10-02 00:38:52 +02:00
2021-01-26 19:13:29 +01:00
GlobalShortcutsManager *manager = dialog()->global_shortcuts_manager();
2018-02-27 18:06:05 +01:00
manager->Unregister();
QKeySequence key = grabber_->GetKey(shortcuts_[current_id_].s.action->text());
manager->Register();
if (key.isEmpty()) return;
// Check if this key sequence is used by any other actions
2024-04-23 17:15:42 +02:00
const QStringList ids = shortcuts_.keys();
2021-03-21 04:47:11 +01:00
for (const QString &id : ids) {
2018-02-27 18:06:05 +01:00
if (shortcuts_[id].key == key) SetShortcut(id, QKeySequence());
}
ui_->radio_custom->setChecked(true);
SetShortcut(current_id_, key);
set_changed();
2018-02-27 18:06:05 +01:00
}
void GlobalShortcutsSettingsPage::X11Warning() {
2021-05-29 20:35:55 +02:00
QString de = de_.toLower();
if (de == QStringLiteral("kde") || de == QStringLiteral("gnome") || de == QStringLiteral("x-cinnamon") || de == QStringLiteral("mate")) {
QString text(tr("Using X11 shortcuts on %1 is not recommended and can cause keyboard to become unresponsive!").arg(de_));
if (de == QStringLiteral("kde")) {
2020-10-31 02:08:19 +01:00
text += tr(" Shortcuts on %1 are usually used through MPRIS and KGlobalAccel.").arg(de_);
2021-05-29 20:35:55 +02:00
}
else if (de == QStringLiteral("gnome")) {
2021-05-29 20:35:55 +02:00
text += tr(" Shortcuts on %1 are usually used through Gnome Settings Daemon and should be configured in gnome-settings-daemon instead.").arg(de_);
}
else if (de == QStringLiteral("x-cinnamon")) {
2021-05-29 20:35:55 +02:00
text += tr(" Shortcuts on %1 are usually used through Gnome Settings Daemon and should be configured in cinnamon-settings-daemon instead.").arg(de_);
}
else if (de == QStringLiteral("mate")) {
2021-05-29 20:35:55 +02:00
text += tr(" Shortcuts on %1 are usually used through MATE Settings Daemon and should be configured there instead.").arg(de_);
}
ui_->label_warn_text->setText(text);
ui_->widget_warning->show();
}
else {
ui_->widget_warning->hide();
}
}