2018-02-27 18:06:05 +01:00
|
|
|
/*
|
|
|
|
* Strawberry Music Player
|
|
|
|
* This file was part of Clementine.
|
|
|
|
* Copyright 2010, David Sansome <me@davidsansome.com>
|
2019-01-01 20:07:29 +01:00
|
|
|
* 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"
|
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QWidget>
|
2020-02-09 02:29:35 +01:00
|
|
|
#include <QList>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QVariant>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QAction>
|
|
|
|
#include <QHeaderView>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QProcess>
|
2018-05-01 00:41:33 +02:00
|
|
|
#include <QTreeWidget>
|
|
|
|
#include <QKeySequence>
|
2018-02-27 18:06:05 +01:00
|
|
|
#include <QShortcut>
|
2018-05-01 00:41:33 +02:00
|
|
|
#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
|
|
|
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "core/iconloader.h"
|
2020-02-09 02:29:35 +01:00
|
|
|
#include "core/logging.h"
|
2018-07-01 22:26:46 +02:00
|
|
|
#include "core/utilities.h"
|
2019-08-06 20:31:54 +02:00
|
|
|
#ifdef Q_OS_MACOS
|
|
|
|
# include "core/mac_utilities.h"
|
|
|
|
#endif
|
2018-05-01 00:41:33 +02:00
|
|
|
#include "globalshortcuts/globalshortcutgrabber.h"
|
|
|
|
#include "globalshortcuts/globalshortcuts.h"
|
|
|
|
#include "settingspage.h"
|
|
|
|
#include "settingsdialog.h"
|
2018-02-27 18:06:05 +01:00
|
|
|
#include "shortcutssettingspage.h"
|
|
|
|
#include "ui_shortcutssettingspage.h"
|
|
|
|
|
|
|
|
const char *GlobalShortcutsSettingsPage::kSettingsGroup = "GlobalShortcuts";
|
|
|
|
|
|
|
|
GlobalShortcutsSettingsPage::GlobalShortcutsSettingsPage(SettingsDialog *dialog)
|
|
|
|
: SettingsPage(dialog),
|
|
|
|
ui_(new Ui_GlobalShortcutsSettingsPage),
|
|
|
|
initialised_(false),
|
|
|
|
grabber_(new GlobalShortcutGrabber) {
|
2019-01-01 20:07:29 +01:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
ui_->setupUi(this);
|
|
|
|
ui_->shortcut_options->setEnabled(false);
|
|
|
|
ui_->list->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
|
|
|
|
setWindowIcon(IconLoader::Load("keyboard"));
|
|
|
|
|
|
|
|
connect(ui_->list, SIGNAL(currentItemChanged(QTreeWidgetItem*, QTreeWidgetItem*)), SLOT(ItemClicked(QTreeWidgetItem*)));
|
|
|
|
connect(ui_->radio_none, SIGNAL(clicked()), SLOT(NoneClicked()));
|
|
|
|
connect(ui_->radio_default, SIGNAL(clicked()), SLOT(DefaultClicked()));
|
|
|
|
connect(ui_->radio_custom, SIGNAL(clicked()), SLOT(ChangeClicked()));
|
2019-01-01 20:07:29 +01:00
|
|
|
connect(ui_->button_change, SIGNAL(clicked()), SLOT(ChangeClicked()));
|
|
|
|
|
|
|
|
#if !defined(Q_OS_WIN) && !defined(Q_OS_MACOS)
|
|
|
|
#ifdef HAVE_DBUS
|
2019-01-25 21:36:28 +01:00
|
|
|
connect(ui_->checkbox_gsd, SIGNAL(clicked(bool)), SLOT(GSDChanged(bool)));
|
|
|
|
connect(ui_->button_gsd_open, SIGNAL(clicked()), SLOT(OpenGnomeKeybindingProperties()));
|
2019-01-01 20:07:29 +01:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_X11
|
|
|
|
connect(ui_->checkbox_x11, SIGNAL(clicked(bool)), SLOT(X11Changed(bool)));
|
|
|
|
#endif
|
|
|
|
#endif // !defined(Q_OS_WIN) && !defined(Q_OS_MACOS)
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalShortcutsSettingsPage::~GlobalShortcutsSettingsPage() { delete ui_; }
|
|
|
|
|
|
|
|
bool GlobalShortcutsSettingsPage::IsEnabled() const {
|
|
|
|
|
2018-06-28 01:15:32 +02:00
|
|
|
#ifdef Q_OS_MACOS
|
2019-01-01 22:35:23 +01:00
|
|
|
qLog(Debug) << Utilities::GetMacOsVersion();
|
|
|
|
if (Utilities::GetMacOsVersion() < 6) { // Leopard and earlier.
|
2018-02-27 18:06:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalShortcutsSettingsPage::Load() {
|
|
|
|
|
2020-10-01 22:04:38 +02:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
GlobalShortcuts *manager = dialog()->global_shortcuts_manager();
|
|
|
|
|
|
|
|
if (!initialised_) {
|
|
|
|
initialised_ = true;
|
|
|
|
|
2019-01-25 21:36:28 +01:00
|
|
|
de_ = Utilities::DesktopEnvironment();
|
|
|
|
ui_->widget_warning->hide();
|
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
connect(ui_->button_macos_open, SIGNAL(clicked()), manager, SLOT(ShowMacAccessibilityDialog()));
|
2018-02-27 18:06:05 +01:00
|
|
|
|
2019-01-08 23:25:01 +01:00
|
|
|
if (manager->IsGsdAvailable()) {
|
2019-01-25 21:36:28 +01:00
|
|
|
qLog(Debug) << "Gnome (GSD) D-Bus backend is available.";
|
|
|
|
ui_->widget_gsd->show();
|
2019-01-08 23:25:01 +01:00
|
|
|
}
|
|
|
|
else {
|
2019-01-25 21:36:28 +01:00
|
|
|
qLog(Debug) << "Gnome (GSD) D-Bus backend is unavailable.";
|
|
|
|
ui_->widget_gsd->hide();
|
2019-01-01 20:07:29 +01:00
|
|
|
}
|
|
|
|
|
2019-01-08 23:25:01 +01:00
|
|
|
if (manager->IsX11Available()) {
|
|
|
|
qLog(Debug) << "X11 backend is available.";
|
|
|
|
ui_->widget_x11->show();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
qLog(Debug) << "X11 backend is unavailable.";
|
2019-01-01 20:07:29 +01:00
|
|
|
ui_->widget_x11->hide();
|
2020-10-01 22:04:38 +02:00
|
|
|
s.setValue("use_x11", false);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-01 22:04:38 +02:00
|
|
|
for (const GlobalShortcuts::Shortcut &i : manager->shortcuts().values()) {
|
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));
|
|
|
|
}
|
|
|
|
|
2020-10-01 22:04:38 +02:00
|
|
|
for (const Shortcut &shortcut : shortcuts_.values()) {
|
|
|
|
SetShortcut(shortcut.s.id, shortcut.s.action->shortcut());
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-01 22:04:38 +02:00
|
|
|
bool use_gsd = s.value("use_gsd", true).toBool();
|
2019-01-25 21:36:28 +01:00
|
|
|
if (ui_->widget_gsd->isVisibleTo(this)) {
|
|
|
|
ui_->checkbox_gsd->setChecked(use_gsd);
|
2019-01-01 20:07:29 +01:00
|
|
|
}
|
|
|
|
|
2020-10-01 22:04:38 +02:00
|
|
|
bool use_x11 = s.value("use_x11", false).toBool();
|
2019-01-01 20:07:29 +01:00
|
|
|
if (ui_->widget_x11->isVisibleTo(this)) {
|
|
|
|
ui_->checkbox_x11->setChecked(use_x11);
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-08 23:25:01 +01:00
|
|
|
#if !defined(Q_OS_WIN) && !defined(Q_OS_MACOS)
|
|
|
|
#ifdef HAVE_DBUS
|
2019-01-25 21:36:28 +01:00
|
|
|
GSDChanged(true);
|
2019-01-08 23:25:01 +01:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_X11
|
|
|
|
X11Changed(true);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
ui_->widget_macos->setVisible(!manager->IsMacAccessibilityEnabled());
|
2018-07-01 22:26:46 +02:00
|
|
|
#ifdef Q_OS_MACOS
|
2019-01-01 20:07:29 +01:00
|
|
|
qint32 macos_version = Utilities::GetMacOsVersion();
|
|
|
|
ui_->label_macos->setVisible(macos_version < 9);
|
|
|
|
ui_->label_macos_mavericks->setVisible(macos_version >= 9);
|
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();
|
|
|
|
|
2020-05-25 23:56:54 +02:00
|
|
|
Init(ui_->layout_globalshortcutssettingspage->parentWidget());
|
|
|
|
|
2020-10-12 17:20:18 +02:00
|
|
|
if (!QSettings().childGroups().contains(kSettingsGroup)) set_changed();
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalShortcutsSettingsPage::Save() {
|
|
|
|
|
2020-10-01 22:04:38 +02:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
|
|
|
for (const Shortcut &shortcut : shortcuts_.values()) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-10-01 22:04:38 +02:00
|
|
|
s.setValue("use_gsd", ui_->checkbox_gsd->isChecked());
|
|
|
|
s.setValue("use_x11", ui_->checkbox_x11->isChecked());
|
|
|
|
|
|
|
|
s.endGroup();
|
2018-02-27 18:06:05 +01:00
|
|
|
|
|
|
|
dialog()->global_shortcuts_manager()->ReloadSettings();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-01-01 20:07:29 +01:00
|
|
|
void GlobalShortcutsSettingsPage::X11Changed(bool) {
|
|
|
|
|
2019-01-08 23:25:01 +01:00
|
|
|
if (!ui_->widget_x11->isVisibleTo(this)) return;
|
2019-01-01 20:07:29 +01:00
|
|
|
|
|
|
|
if (ui_->checkbox_x11->isChecked()) {
|
|
|
|
ui_->list->setEnabled(true);
|
2019-01-09 19:22:38 +01:00
|
|
|
ui_->shortcut_options->setEnabled(true);
|
2019-01-25 21:36:28 +01:00
|
|
|
X11Warning();
|
2019-01-01 20:07:29 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
ui_->list->setEnabled(false);
|
2019-01-09 19:22:38 +01:00
|
|
|
ui_->shortcut_options->setEnabled(false);
|
2019-01-25 21:36:28 +01:00
|
|
|
ui_->widget_warning->hide();
|
2019-01-01 20:07:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2020-06-17 19:58:21 +02:00
|
|
|
|
2019-01-25 21:36:28 +01:00
|
|
|
void GlobalShortcutsSettingsPage::GSDChanged(bool) {
|
2019-01-01 20:07:29 +01:00
|
|
|
|
2019-01-25 21:36:28 +01:00
|
|
|
if (!ui_->widget_gsd->isVisibleTo(this)) return;
|
2019-01-01 20:07:29 +01:00
|
|
|
|
2019-01-25 21:36:28 +01:00
|
|
|
if (ui_->checkbox_gsd->isChecked()) {
|
2019-01-01 20:07:29 +01:00
|
|
|
if (ui_->checkbox_x11->isEnabled()) {
|
|
|
|
ui_->checkbox_x11->setEnabled(false);
|
|
|
|
}
|
|
|
|
if (ui_->checkbox_x11->isChecked()) {
|
|
|
|
ui_->checkbox_x11->setChecked(false);
|
|
|
|
}
|
2019-01-25 21:36:28 +01:00
|
|
|
ui_->list->setEnabled(false);
|
|
|
|
ui_->shortcut_options->setEnabled(false);
|
|
|
|
ui_->widget_warning->hide();
|
2019-01-01 20:07:29 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (!ui_->checkbox_x11->isEnabled()) {
|
|
|
|
ui_->checkbox_x11->setEnabled(true);
|
2019-01-09 19:22:38 +01:00
|
|
|
if (ui_->checkbox_x11->isChecked()) {
|
|
|
|
ui_->list->setEnabled(true);
|
|
|
|
ui_->shortcut_options->setEnabled(true);
|
2019-01-25 21:36:28 +01:00
|
|
|
X11Warning();
|
2019-01-09 19:22:38 +01:00
|
|
|
}
|
2019-01-01 20:07:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
void GlobalShortcutsSettingsPage::OpenGnomeKeybindingProperties() {
|
|
|
|
|
2020-06-05 22:15:58 +02:00
|
|
|
if (!QProcess::startDetached("gnome-keybinding-properties", QStringList())) {
|
2019-01-01 20:07:29 +01:00
|
|
|
if (!QProcess::startDetached("gnome-control-center", QStringList() << "keyboard")) {
|
2019-02-22 20:24:38 +01:00
|
|
|
QMessageBox::warning(this, "Error", tr("The \"%1\" command could not be started.").arg("gnome-keybinding-properties"));
|
2019-01-01 20:07:29 +01: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()));
|
|
|
|
|
|
|
|
if (shortcut.key == shortcut.s.default_key)
|
|
|
|
ui_->radio_default->setChecked(true);
|
|
|
|
else if (shortcut.key.isEmpty())
|
|
|
|
ui_->radio_none->setChecked(true);
|
|
|
|
else
|
|
|
|
ui_->radio_custom->setChecked(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalShortcutsSettingsPage::NoneClicked() {
|
2020-05-25 23:56:54 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
SetShortcut(current_id_, QKeySequence());
|
2020-05-25 23:56:54 +02:00
|
|
|
set_changed();
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalShortcutsSettingsPage::DefaultClicked() {
|
2020-05-25 23:56:54 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
SetShortcut(current_id_, shortcuts_[current_id_].s.default_key);
|
2020-05-25 23:56:54 +02:00
|
|
|
set_changed();
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalShortcutsSettingsPage::ChangeClicked() {
|
2018-10-02 00:38:52 +02:00
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
GlobalShortcuts *manager = dialog()->global_shortcuts_manager();
|
|
|
|
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
|
|
|
|
for (const QString &id : shortcuts_.keys()) {
|
|
|
|
if (shortcuts_[id].key == key) SetShortcut(id, QKeySequence());
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_->radio_custom->setChecked(true);
|
|
|
|
SetShortcut(current_id_, key);
|
|
|
|
|
2020-05-25 23:56:54 +02:00
|
|
|
set_changed();
|
|
|
|
|
2018-02-27 18:06:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-25 21:36:28 +01:00
|
|
|
void GlobalShortcutsSettingsPage::X11Warning() {
|
|
|
|
|
|
|
|
if (de_.toLower() == "kde" || de_.toLower() == "gnome" || de_.toLower() == "x-cinnamon") {
|
|
|
|
QString text(tr("Using X11 shortcuts on %1 is not recommended and can cause keyboard to become unresponsive!").arg(de_));
|
|
|
|
if (de_.toLower() == "kde")
|
|
|
|
text += tr(" Shortcuts on %1 are usually used through MPRIS D-Bus and should be configured in %1 settings instead.").arg(de_);
|
|
|
|
else if (de_.toLower() == "gnome")
|
|
|
|
text += tr(" Shortcuts on %1 are usually used through GSD D-Bus and should be configured in gnome-settings-daemon instead.").arg(de_);
|
|
|
|
else if (de_.toLower() == "x-cinnamon")
|
|
|
|
text += tr(" Shortcuts on %1 are usually used through GSD D-Bus and should be configured in cinnamon-settings-daemon instead.").arg(de_);
|
|
|
|
else
|
|
|
|
text += tr(" Shortcuts should be configured in %1 settings instead.").arg(de_);
|
|
|
|
ui_->label_warn_text->setText(text);
|
|
|
|
ui_->widget_warning->show();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ui_->widget_warning->hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|