Fix style
This commit is contained in:
parent
a3531d749a
commit
2307c15227
@ -44,7 +44,7 @@ GlobalShortcuts::GlobalShortcuts(QWidget* parent)
|
|||||||
system_backend_(nullptr),
|
system_backend_(nullptr),
|
||||||
use_gnome_(false),
|
use_gnome_(false),
|
||||||
have_kglobalaccel_(
|
have_kglobalaccel_(
|
||||||
KGlobalAccelShortcutBackend::isKGlobalAccelAvailable()) {
|
KGlobalAccelShortcutBackend::IsKGlobalAccelAvailable()) {
|
||||||
settings_.beginGroup(kSettingsGroup);
|
settings_.beginGroup(kSettingsGroup);
|
||||||
|
|
||||||
// Create actions
|
// Create actions
|
||||||
|
@ -16,18 +16,17 @@
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
#ifdef HAVE_DBUS
|
#ifdef HAVE_DBUS
|
||||||
QString compDisplayName() {
|
QString ComponentDisplayName() {
|
||||||
if (!QGuiApplication::applicationDisplayName().isEmpty()) {
|
return QGuiApplication::applicationDisplayName().isEmpty()
|
||||||
return QGuiApplication::applicationDisplayName();
|
? QCoreApplication::applicationName()
|
||||||
}
|
: QGuiApplication::applicationDisplayName();
|
||||||
return QCoreApplication::applicationName();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString compUniqueName() { return QCoreApplication::applicationName(); }
|
QString ComponentUniqueName() { return QCoreApplication::applicationName(); }
|
||||||
|
|
||||||
const QString& id_ActionUnique(const QStringList& id) { return id.at(1); }
|
const QString& IdActionUniqueName(const QStringList& id) { return id.at(1); }
|
||||||
|
|
||||||
bool isCorrectMediaKeyShortcut(const GlobalShortcuts::Shortcut& shortcut) {
|
bool IsCorrectMediaKeyShortcut(const GlobalShortcuts::Shortcut& shortcut) {
|
||||||
if (shortcut.id == QStringLiteral("play_pause")) {
|
if (shortcut.id == QStringLiteral("play_pause")) {
|
||||||
return shortcut.action->shortcut() == QKeySequence(Qt::Key_MediaPlay);
|
return shortcut.action->shortcut() == QKeySequence(Qt::Key_MediaPlay);
|
||||||
} else if (shortcut.id == QStringLiteral("stop")) {
|
} else if (shortcut.id == QStringLiteral("stop")) {
|
||||||
@ -49,8 +48,7 @@ KGlobalAccelShortcutBackend::KGlobalAccelShortcutBackend(
|
|||||||
GlobalShortcuts* parent)
|
GlobalShortcuts* parent)
|
||||||
: GlobalShortcutBackend(parent),
|
: GlobalShortcutBackend(parent),
|
||||||
iface_(nullptr),
|
iface_(nullptr),
|
||||||
component_(nullptr),
|
component_(nullptr) {}
|
||||||
nameToAction_() {}
|
|
||||||
|
|
||||||
#else // HAVE_DBUS
|
#else // HAVE_DBUS
|
||||||
KGlobalAccelShortcutBackend::KGlobalAccelShortcutBackend(
|
KGlobalAccelShortcutBackend::KGlobalAccelShortcutBackend(
|
||||||
@ -58,10 +56,10 @@ KGlobalAccelShortcutBackend::KGlobalAccelShortcutBackend(
|
|||||||
: GlobalShortcutBackend(parent) {}
|
: GlobalShortcutBackend(parent) {}
|
||||||
#endif // HAVE_DBUS
|
#endif // HAVE_DBUS
|
||||||
|
|
||||||
bool KGlobalAccelShortcutBackend::isKGlobalAccelAvailable() {
|
bool KGlobalAccelShortcutBackend::IsKGlobalAccelAvailable() {
|
||||||
#ifdef HAVE_DBUS
|
#ifdef HAVE_DBUS
|
||||||
return QDBusConnection::sessionBus().interface()->isServiceRegistered(
|
return QDBusConnection::sessionBus().interface()->isServiceRegistered(
|
||||||
Service);
|
kService);
|
||||||
#else // HAVE_DBUS
|
#else // HAVE_DBUS
|
||||||
return false;
|
return false;
|
||||||
#endif // HAVE_DBUS
|
#endif // HAVE_DBUS
|
||||||
@ -71,21 +69,21 @@ bool KGlobalAccelShortcutBackend::DoRegister() {
|
|||||||
#ifdef HAVE_DBUS
|
#ifdef HAVE_DBUS
|
||||||
qLog(Debug) << "Registering shortcuts";
|
qLog(Debug) << "Registering shortcuts";
|
||||||
|
|
||||||
if (!acquireInterface()) return false;
|
if (!AcquireInterface()) return false;
|
||||||
|
|
||||||
bool complete = true;
|
bool complete = true;
|
||||||
for (const GlobalShortcuts::Shortcut& shortcut :
|
for (const GlobalShortcuts::Shortcut& shortcut :
|
||||||
manager_->shortcuts().values()) {
|
manager_->shortcuts().values()) {
|
||||||
if (shortcut.action->shortcut().isEmpty()) continue;
|
if (shortcut.action->shortcut().isEmpty()) continue;
|
||||||
|
|
||||||
if (!registerShortcut(shortcut)) complete = false;
|
if (!RegisterShortcut(shortcut)) complete = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!acquireComponent()) return false;
|
if (!AcquireComponent()) return false;
|
||||||
|
|
||||||
QObject::connect(component_,
|
QObject::connect(component_,
|
||||||
&OrgKdeKglobalaccelComponentInterface::globalShortcutPressed,
|
&OrgKdeKglobalaccelComponentInterface::globalShortcutPressed,
|
||||||
this, &KGlobalAccelShortcutBackend::onShortcutPressed);
|
this, &KGlobalAccelShortcutBackend::OnShortcutPressed);
|
||||||
|
|
||||||
return complete;
|
return complete;
|
||||||
#else // HAVE_DBUS
|
#else // HAVE_DBUS
|
||||||
@ -96,25 +94,24 @@ bool KGlobalAccelShortcutBackend::DoRegister() {
|
|||||||
|
|
||||||
void KGlobalAccelShortcutBackend::DoUnregister() {
|
void KGlobalAccelShortcutBackend::DoUnregister() {
|
||||||
#ifdef HAVE_DBUS
|
#ifdef HAVE_DBUS
|
||||||
if (!acquireInterface()) return;
|
if (!AcquireInterface()) return;
|
||||||
|
|
||||||
if (!acquireComponent()) return;
|
if (!AcquireComponent()) return;
|
||||||
|
|
||||||
for (const GlobalShortcuts::Shortcut& shortcut : manager_->shortcuts())
|
for (const GlobalShortcuts::Shortcut& shortcut : manager_->shortcuts())
|
||||||
unregisterAction(shortcut.id, shortcut.action);
|
UnregisterAction(shortcut.id, shortcut.action);
|
||||||
#endif // HAVE_DBUS
|
#endif // HAVE_DBUS
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef HAVE_DBUS
|
#ifdef HAVE_DBUS
|
||||||
|
|
||||||
const char* KGlobalAccelShortcutBackend::Service = "org.kde.kglobalaccel";
|
const char* KGlobalAccelShortcutBackend::kService = "org.kde.kglobalaccel";
|
||||||
const char* KGlobalAccelShortcutBackend::Path = "/kglobalaccel";
|
const char* KGlobalAccelShortcutBackend::kPath = "/kglobalaccel";
|
||||||
|
|
||||||
bool KGlobalAccelShortcutBackend::acquireComponent() {
|
bool KGlobalAccelShortcutBackend::AcquireComponent() {
|
||||||
Q_ASSERT(iface_ && iface_->isValid());
|
Q_ASSERT(iface_ && iface_->isValid());
|
||||||
|
|
||||||
QString componentName = compUniqueName();
|
QDBusReply<QDBusObjectPath> reply = iface_->getComponent(ComponentUniqueName());
|
||||||
QDBusReply<QDBusObjectPath> reply = iface_->getComponent(compUniqueName());
|
|
||||||
if (!reply.isValid()) {
|
if (!reply.isValid()) {
|
||||||
if (reply.error().name() ==
|
if (reply.error().name() ==
|
||||||
QLatin1String("org.kde.kglobalaccel.NoSuchComponent"))
|
QLatin1String("org.kde.kglobalaccel.NoSuchComponent"))
|
||||||
@ -125,12 +122,12 @@ bool KGlobalAccelShortcutBackend::acquireComponent() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
component_ = new org::kde::kglobalaccel::Component(
|
component_ = new org::kde::kglobalaccel::Component(
|
||||||
Service, reply.value().path(), QDBusConnection::sessionBus(), iface_);
|
kService, reply.value().path(), QDBusConnection::sessionBus(), iface_);
|
||||||
|
|
||||||
if (!component_->isValid()) {
|
if (!component_->isValid()) {
|
||||||
qLog(Warning) << "Failed to get KGlobalAccel component:"
|
qLog(Warning) << "Failed to get KGlobalAccel component:"
|
||||||
<< QDBusConnection::sessionBus().lastError();
|
<< QDBusConnection::sessionBus().lastError();
|
||||||
delete component_;
|
component_->deleteLater();
|
||||||
component_ = nullptr;
|
component_ = nullptr;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -138,12 +135,12 @@ bool KGlobalAccelShortcutBackend::acquireComponent() {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool KGlobalAccelShortcutBackend::acquireInterface() {
|
bool KGlobalAccelShortcutBackend::AcquireInterface() {
|
||||||
if (iface_ && iface_->isValid()) return true;
|
if (iface_ && iface_->isValid()) return true;
|
||||||
|
|
||||||
if (isKGlobalAccelAvailable()) {
|
if (IsKGlobalAccelAvailable()) {
|
||||||
iface_ = new OrgKdeKGlobalAccelInterface(
|
iface_ = new OrgKdeKGlobalAccelInterface(
|
||||||
Service, Path, QDBusConnection::sessionBus(), this);
|
kService, kPath, QDBusConnection::sessionBus(), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (iface_ && iface_->isValid()) return true;
|
if (iface_ && iface_->isValid()) return true;
|
||||||
@ -155,20 +152,20 @@ bool KGlobalAccelShortcutBackend::acquireInterface() {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
QStringList KGlobalAccelShortcutBackend::id(const QString& name,
|
QStringList KGlobalAccelShortcutBackend::GetId(const QString& name,
|
||||||
const QAction* action) {
|
const QAction* action) {
|
||||||
Q_ASSERT(action);
|
Q_ASSERT(action);
|
||||||
|
|
||||||
QStringList ret;
|
QStringList ret;
|
||||||
ret << compUniqueName();
|
ret << ComponentUniqueName();
|
||||||
ret << name;
|
ret << name;
|
||||||
ret << compDisplayName();
|
ret << ComponentDisplayName();
|
||||||
ret << action->text().replace(QLatin1Char('&'), QStringLiteral(""));
|
ret << action->text().replace(QLatin1Char('&'), QStringLiteral(""));
|
||||||
if (ret.back().isEmpty()) ret.back() = name;
|
if (ret.back().isEmpty()) ret.back() = name;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<int> KGlobalAccelShortcutBackend::intList(
|
QList<int> KGlobalAccelShortcutBackend::ToIntList(
|
||||||
const QList<QKeySequence>& seq) {
|
const QList<QKeySequence>& seq) {
|
||||||
QList<int> ret;
|
QList<int> ret;
|
||||||
for (const QKeySequence& sequence : seq) {
|
for (const QKeySequence& sequence : seq) {
|
||||||
@ -180,9 +177,8 @@ QList<int> KGlobalAccelShortcutBackend::intList(
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool KGlobalAccelShortcutBackend::registerAction(const QString& name,
|
bool KGlobalAccelShortcutBackend::RegisterAction(const QString& name,
|
||||||
QAction* action,
|
QAction* action) {
|
||||||
QStringList& actionId) {
|
|
||||||
Q_ASSERT(action);
|
Q_ASSERT(action);
|
||||||
|
|
||||||
if (name.isEmpty() &&
|
if (name.isEmpty() &&
|
||||||
@ -192,34 +188,34 @@ bool KGlobalAccelShortcutBackend::registerAction(const QString& name,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
actionId = id(name, action);
|
QStringList action_id = GetId(name, action);
|
||||||
nameToAction_.insertMulti(id_ActionUnique(actionId), action);
|
name_to_action_.insertMulti(IdActionUniqueName(action_id), action);
|
||||||
iface_->doRegister(actionId);
|
iface_->doRegister(action_id);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool KGlobalAccelShortcutBackend::registerShortcut(
|
bool KGlobalAccelShortcutBackend::RegisterShortcut(
|
||||||
const GlobalShortcuts::Shortcut& shortcut) {
|
const GlobalShortcuts::Shortcut& shortcut) {
|
||||||
QStringList actionId;
|
if (!RegisterAction(shortcut.id, shortcut.action)) return false;
|
||||||
if (!registerAction(shortcut.id, shortcut.action, actionId)) return false;
|
|
||||||
|
|
||||||
QList<QKeySequence> activeShortcut;
|
QList<QKeySequence> active_shortcut;
|
||||||
activeShortcut << shortcut.action->shortcut();
|
active_shortcut << shortcut.action->shortcut();
|
||||||
|
|
||||||
|
QStringList action_id = GetId(shortcut.id, shortcut.action);
|
||||||
const QList<int> result = iface_->setShortcut(
|
const QList<int> result = iface_->setShortcut(
|
||||||
actionId, intList(activeShortcut), SetShortcutFlag::SetPresent);
|
action_id, ToIntList(active_shortcut), SetShortcutFlag::SetPresent);
|
||||||
|
|
||||||
const QList<QKeySequence> resultSequence = shortcutList(result);
|
const QList<QKeySequence> result_sequence = ToKeySequenceList(result);
|
||||||
if (resultSequence != activeShortcut) {
|
if (result_sequence != active_shortcut) {
|
||||||
qLog(Warning) << "Tried setting global shortcut" << activeShortcut
|
qLog(Warning) << "Tried setting global shortcut" << active_shortcut
|
||||||
<< "but KGlobalAccel returned" << resultSequence;
|
<< "but KGlobalAccel returned" << result_sequence;
|
||||||
|
|
||||||
if (!resultSequence.isEmpty()) {
|
if (!result_sequence.isEmpty()) {
|
||||||
if (!isCorrectMediaKeyShortcut(shortcut)) {
|
if (!IsCorrectMediaKeyShortcut(shortcut)) {
|
||||||
// there is some conflict with our preferred shortcut so we use
|
// there is some conflict with our preferred shortcut so we use
|
||||||
// the new shortcut that kglobalaccel suggests
|
// the new shortcut that kglobalaccel suggests
|
||||||
shortcut.action->setShortcut(resultSequence[0]);
|
shortcut.action->setShortcut(result_sequence[0]);
|
||||||
} else {
|
} else {
|
||||||
// media keys are properly handled by plasma through the
|
// media keys are properly handled by plasma through the
|
||||||
// media player plasmoid so we don't do anything in those cases
|
// media player plasmoid so we don't do anything in those cases
|
||||||
@ -231,7 +227,7 @@ bool KGlobalAccelShortcutBackend::registerShortcut(
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QKeySequence> KGlobalAccelShortcutBackend::shortcutList(
|
QList<QKeySequence> KGlobalAccelShortcutBackend::ToKeySequenceList(
|
||||||
const QList<int>& seq) {
|
const QList<int>& seq) {
|
||||||
QList<QKeySequence> ret;
|
QList<QKeySequence> ret;
|
||||||
for (int i : seq) {
|
for (int i : seq) {
|
||||||
@ -240,22 +236,22 @@ QList<QKeySequence> KGlobalAccelShortcutBackend::shortcutList(
|
|||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void KGlobalAccelShortcutBackend::unregisterAction(const QString& name,
|
void KGlobalAccelShortcutBackend::UnregisterAction(const QString& name,
|
||||||
QAction* action) {
|
QAction* action) {
|
||||||
Q_ASSERT(action);
|
Q_ASSERT(action);
|
||||||
|
|
||||||
QStringList actionId = id(name, action);
|
QStringList action_id = GetId(name, action);
|
||||||
nameToAction_.remove(id_ActionUnique(actionId), action);
|
name_to_action_.remove(IdActionUniqueName(action_id), action);
|
||||||
iface_->unRegister(actionId);
|
iface_->unRegister(action_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void KGlobalAccelShortcutBackend::onShortcutPressed(
|
void KGlobalAccelShortcutBackend::OnShortcutPressed(
|
||||||
const QString& componentUnique, const QString& actionUnique,
|
const QString& component_unique, const QString& action_unique,
|
||||||
qlonglong timestamp) const {
|
qlonglong timestamp) const {
|
||||||
QAction* action = nullptr;
|
QAction* action = nullptr;
|
||||||
const QList<QAction*> candidates = nameToAction_.values(actionUnique);
|
const QList<QAction*> candidates = name_to_action_.values(action_unique);
|
||||||
for (QAction* a : candidates) {
|
for (QAction* a : candidates) {
|
||||||
if (compUniqueName() == componentUnique) {
|
if (ComponentUniqueName() == component_unique) {
|
||||||
action = a;
|
action = a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,7 +20,7 @@ class KGlobalAccelShortcutBackend : public GlobalShortcutBackend {
|
|||||||
public:
|
public:
|
||||||
explicit KGlobalAccelShortcutBackend(GlobalShortcuts* parent);
|
explicit KGlobalAccelShortcutBackend(GlobalShortcuts* parent);
|
||||||
|
|
||||||
static bool isKGlobalAccelAvailable();
|
static bool IsKGlobalAccelAvailable();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool DoRegister() override;
|
bool DoRegister() override;
|
||||||
@ -31,36 +31,35 @@ class KGlobalAccelShortcutBackend : public GlobalShortcutBackend {
|
|||||||
#ifdef HAVE_DBUS
|
#ifdef HAVE_DBUS
|
||||||
enum SetShortcutFlag { SetPresent = 2, NoAutoloading = 4, IsDefault = 8 };
|
enum SetShortcutFlag { SetPresent = 2, NoAutoloading = 4, IsDefault = 8 };
|
||||||
|
|
||||||
bool acquireComponent();
|
bool AcquireComponent();
|
||||||
|
|
||||||
bool acquireInterface();
|
bool AcquireInterface();
|
||||||
|
|
||||||
static QStringList id(const QString& name, const QAction* action);
|
static QStringList GetId(const QString& name, const QAction* action);
|
||||||
|
|
||||||
static QList<int> intList(const QList<QKeySequence>& seq);
|
static QList<int> ToIntList(const QList<QKeySequence>& seq);
|
||||||
|
|
||||||
bool registerAction(const QString& name, QAction* action,
|
bool RegisterAction(const QString& name, QAction* action);
|
||||||
QStringList& actionId);
|
|
||||||
|
|
||||||
bool registerShortcut(const GlobalShortcuts::Shortcut& shortcut);
|
bool RegisterShortcut(const GlobalShortcuts::Shortcut& shortcut);
|
||||||
|
|
||||||
static QList<QKeySequence> shortcutList(const QList<int>& seq);
|
static QList<QKeySequence> ToKeySequenceList(const QList<int>& seq);
|
||||||
|
|
||||||
void unregisterAction(const QString& name, QAction* action);
|
void UnregisterAction(const QString& name, QAction* action);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
|
|
||||||
void onShortcutPressed(const QString& componentUnique,
|
void OnShortcutPressed(const QString& component_unique,
|
||||||
const QString& actionUnique,
|
const QString& action_unique,
|
||||||
qlonglong timestamp) const;
|
qlonglong timestamp) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static const char* Service;
|
static const char* kService;
|
||||||
static const char* Path;
|
static const char* kPath;
|
||||||
|
|
||||||
OrgKdeKGlobalAccelInterface* iface_;
|
OrgKdeKGlobalAccelInterface* iface_;
|
||||||
OrgKdeKglobalaccelComponentInterface* component_;
|
OrgKdeKglobalaccelComponentInterface* component_;
|
||||||
QMultiHash<QString, QAction*> nameToAction_;
|
QMultiHash<QString, QAction*> name_to_action_;
|
||||||
#endif // HAVE_DBUS
|
#endif // HAVE_DBUS
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user