File indexing completed on 2024-02-25 03:59:21

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2001, 2002 Ellis Whitehead <ellis@kde.org>
0004     SPDX-FileCopyrightText: 2006 Hamish Rodda <rodda@kde.org>
0005     SPDX-FileCopyrightText: 2007 Andreas Hartmetz <ahartmetz@gmail.com>
0006 
0007     SPDX-License-Identifier: LGPL-2.0-or-later
0008 */
0009 
0010 #ifndef KGLOBALACCEL_P_H
0011 #define KGLOBALACCEL_P_H
0012 
0013 #include <QHash>
0014 #include <QKeySequence>
0015 #include <QList>
0016 #include <QStringList>
0017 
0018 #include "kglobalaccel.h"
0019 #include "kglobalaccel_component_interface.h"
0020 #include "kglobalaccel_interface.h"
0021 
0022 enum SetShortcutFlag {
0023     SetPresent = 2,
0024     NoAutoloading = 4,
0025     IsDefault = 8,
0026 };
0027 
0028 class KGlobalAccelPrivate
0029 {
0030 public:
0031     enum ShortcutType {
0032         /// The shortcut will immediately become active but may be reset to "default".
0033         ActiveShortcut = 0x1,
0034         /// The shortcut is a default shortcut - it becomes active when somebody decides to
0035         /// reset shortcuts to default.
0036         DefaultShortcut = 0x2,
0037     };
0038 
0039     Q_DECLARE_FLAGS(ShortcutTypes, ShortcutType)
0040     enum Removal {
0041         SetInactive = 0, ///< Forget the action in this class and mark it as not present in the KDED module
0042         UnRegister, ///< Remove any trace of the action in this class and in the KDED module
0043     };
0044     KGlobalAccelPrivate(KGlobalAccel *);
0045 
0046     /// Propagate any shortcut changes to the KDED module that does the bookkeeping
0047     /// and the key grabbing.
0048     ///@todo KF6
0049     void updateGlobalShortcut(/*const would be better*/ QAction *action,
0050                               KGlobalAccelPrivate::ShortcutTypes actionFlags,
0051                               KGlobalAccel::GlobalShortcutLoading globalFlags);
0052 
0053     /// Register the action in this class and in the KDED module
0054     bool doRegister(QAction *action); //"register" is a C keyword :p
0055     /// cf. the RemoveAction enum
0056     void remove(QAction *action, Removal r);
0057 
0058     //"private" helpers
0059     QString componentUniqueForAction(const QAction *action);
0060     QString componentFriendlyForAction(const QAction *action);
0061     QStringList makeActionId(const QAction *action);
0062     QList<int> intListFromShortcut(const QList<QKeySequence> &cut);
0063     QList<QKeySequence> shortcutFromIntList(const QList<int> &list);
0064 
0065     void cleanup();
0066 
0067     // private slot implementations
0068     QAction *findAction(const QString &, const QString &);
0069     void invokeAction(const QString &, const QString &, qlonglong);
0070     void invokeDeactivate(const QString &, const QString &);
0071     void shortcutGotChanged(const QStringList &, const QList<int> &);
0072     void shortcutsChanged(const QStringList &, const QList<QKeySequence> &);
0073     void serviceOwnerChanged(const QString &name, const QString &oldOwner, const QString &newOwner);
0074     void reRegisterAll();
0075 
0076     // for all actions with (isEnabled() && globalShortcutAllowed())
0077     QMultiHash<QString, QAction *> nameToAction;
0078     QSet<QAction *> actions;
0079 
0080     org::kde::KGlobalAccel *iface();
0081 
0082     //! Get the component @p componentUnique. If @p remember is true the instance is cached and we
0083     //! subscribe to signals about changes to the component.
0084     org::kde::kglobalaccel::Component *getComponent(const QString &componentUnique, bool remember);
0085 
0086     //! Our owner
0087     KGlobalAccel *q;
0088 
0089     //! The components the application is using
0090     QHash<QString, org::kde::kglobalaccel::Component *> components;
0091     QMap<const QAction *, QList<QKeySequence>> actionDefaultShortcuts;
0092     QMap<const QAction *, QList<QKeySequence>> actionShortcuts;
0093 
0094     bool setShortcutWithDefault(QAction *action, const QList<QKeySequence> &shortcut, KGlobalAccel::GlobalShortcutLoading loadFlag);
0095 
0096     void unregister(const QStringList &actionId);
0097     void setInactive(const QStringList &actionId);
0098 
0099 private:
0100     org::kde::KGlobalAccel *m_iface = nullptr;
0101     QPointer<QAction> m_lastActivatedAction;
0102     QDBusServiceWatcher *m_watcher;
0103 };
0104 
0105 Q_DECLARE_OPERATORS_FOR_FLAGS(KGlobalAccelPrivate::ShortcutTypes)
0106 
0107 #endif