File indexing completed on 2024-05-05 12:14:17

0001 /*
0002     SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "globalshortcutcontext.h"
0008 
0009 #include "globalshortcut.h"
0010 
0011 #include "kglobalaccel.h"
0012 #include "sequencehelpers_p.h"
0013 
0014 GlobalShortcutContext::GlobalShortcutContext(const QString &uniqueName, const QString &friendlyName, Component *component)
0015 
0016     : _uniqueName(uniqueName)
0017     , _friendlyName(friendlyName)
0018     , _component(component)
0019 {
0020 }
0021 
0022 GlobalShortcutContext::~GlobalShortcutContext()
0023 {
0024     qDeleteAll(_actionsMap);
0025     _actionsMap.clear();
0026 }
0027 
0028 void GlobalShortcutContext::addShortcut(GlobalShortcut *shortcut)
0029 {
0030     _actionsMap.insert(shortcut->uniqueName(), shortcut);
0031 }
0032 
0033 QList<KGlobalShortcutInfo> GlobalShortcutContext::allShortcutInfos() const
0034 {
0035     QList<KGlobalShortcutInfo> rc;
0036     for (GlobalShortcut *shortcut : std::as_const(_actionsMap)) {
0037         rc.append(static_cast<KGlobalShortcutInfo>(*shortcut));
0038     }
0039     return rc;
0040 }
0041 
0042 Component const *GlobalShortcutContext::component() const
0043 {
0044     return _component;
0045 }
0046 
0047 Component *GlobalShortcutContext::component()
0048 {
0049     return _component;
0050 }
0051 
0052 QString GlobalShortcutContext::friendlyName() const
0053 {
0054     return _friendlyName;
0055 }
0056 
0057 GlobalShortcut *GlobalShortcutContext::getShortcutByKey(const QKeySequence &key, KGlobalAccel::MatchType type) const
0058 {
0059     if (key.isEmpty()) {
0060         return nullptr;
0061     }
0062     QKeySequence keyMangled = Utils::mangleKey(key);
0063     for (GlobalShortcut *sc : std::as_const(_actionsMap)) {
0064         const auto keys = sc->keys();
0065         for (const QKeySequence &other : keys) {
0066             QKeySequence otherMangled = Utils::mangleKey(other);
0067             switch (type) {
0068             case KGlobalAccel::MatchType::Equal:
0069                 if (otherMangled == keyMangled) {
0070                     return sc;
0071                 }
0072                 break;
0073             case KGlobalAccel::MatchType::Shadows:
0074                 if (!other.isEmpty() && Utils::contains(keyMangled, otherMangled)) {
0075                     return sc;
0076                 }
0077                 break;
0078             case KGlobalAccel::MatchType::Shadowed:
0079                 if (!other.isEmpty() && Utils::contains(otherMangled, keyMangled)) {
0080                     return sc;
0081                 }
0082                 break;
0083             }
0084         }
0085     }
0086     return nullptr;
0087 }
0088 
0089 GlobalShortcut *GlobalShortcutContext::takeShortcut(GlobalShortcut *shortcut)
0090 {
0091     // Try to take the shortcut. Result could be nullptr if the shortcut doesn't
0092     // belong to this component.
0093     return _actionsMap.take(shortcut->uniqueName());
0094 }
0095 
0096 QString GlobalShortcutContext::uniqueName() const
0097 {
0098     return _uniqueName;
0099 }
0100 
0101 bool GlobalShortcutContext::isShortcutAvailable(const QKeySequence &key) const
0102 {
0103     for (auto it = _actionsMap.cbegin(), endIt = _actionsMap.cend(); it != endIt; ++it) {
0104         const GlobalShortcut *sc = it.value();
0105         if (Utils::matchSequences(key, sc->keys())) {
0106             return false;
0107         }
0108     }
0109     return true;
0110 }