File indexing completed on 2020-08-09 10:58:02

0001 /*
0002     SPDX-FileCopyrightText: 2008 Michael Jansen <kde@michael-jansen.biz>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef GLOBALSHORTCUTSREGISTRY_H
0008 #define GLOBALSHORTCUTSREGISTRY_H
0009 
0010 #include <KSharedConfig>
0011 
0012 #include <QObject>
0013 #include <QHash>
0014 #include <QDBusObjectPath>
0015 
0016 
0017 class GlobalShortcut;
0018 class KGlobalAccelInterface;
0019 
0020 namespace KdeDGlobalAccel
0021     {
0022     class Component;
0023     }
0024 
0025 /**
0026  * Global Shortcut Registry.
0027  *
0028  * Shortcuts are registered by component. A component is for example kmail or
0029  * amarok.
0030  *
0031  * A component can have contexts. Currently on plasma is planned to support
0032  * that feature. A context enables plasma to keep track of global shortcut
0033  * settings when switching containments.
0034  *
0035  * A shortcut (WIN+x) can be registered by one component only. The component
0036  * is allowed to register it more than once in different contexts.
0037  *
0038  * @author Michael Jansen <kde@michael-jansen.biz>
0039  */
0040 class GlobalShortcutsRegistry : public QObject
0041     {
0042     Q_OBJECT
0043 
0044     Q_CLASSINFO("D-Bus Interface", "org.kde.KdedGlobalAccel.GlobalShortcutsRegistry")
0045 
0046 public:
0047     /**
0048      * Use GlobalShortcutsRegistry::self()
0049      *
0050      * @internal
0051      */
0052     GlobalShortcutsRegistry();
0053     ~GlobalShortcutsRegistry();
0054 
0055     /**
0056      * Activate all shortcuts having their application present.
0057      */
0058     void activateShortcuts();
0059 
0060     /**
0061      * Return a list of all main components
0062      */
0063     QList<KdeDGlobalAccel::Component *> allMainComponents() const;
0064 
0065     /**
0066      * Return the root dbus path for the registry.
0067      */
0068     QDBusObjectPath dbusPath() const;
0069 
0070     /**
0071      * Deactivate all currently active shortcuts.
0072      */
0073     void deactivateShortcuts(bool temporarily=false);
0074 
0075 
0076     /**
0077      * Get the shortcut corresponding to key. Only active shortcut are
0078      * considered.
0079      */
0080     GlobalShortcut *getActiveShortcutByKey(int key) const;
0081 
0082     /**
0083      */
0084     KdeDGlobalAccel::Component *getComponent(const QString &uniqueName);
0085 
0086     /**
0087      * Get the shortcut corresponding to key. Active and inactive shortcuts
0088      * are considered. But if the matching application uses contexts only one
0089      * shortcut is returned.
0090      *
0091      * @see getShortcutsByKey(int key)
0092      */
0093     GlobalShortcut *getShortcutByKey(int key) const;
0094 
0095     /**
0096      * Get the shortcuts corresponding to key. Active and inactive shortcuts
0097      * are considered.
0098      *
0099      * @see getShortcutsByKey(int key)
0100      */
0101     QList<GlobalShortcut*> getShortcutsByKey(int key) const;
0102 
0103     /**
0104      * Checks if @p shortcut is available for @p component.
0105      *
0106      * It is available if not used by another component in any context or used
0107      * by @p component only in not active contexts.
0108      */
0109     bool isShortcutAvailable(
0110             int shortcut,
0111             const QString &component,
0112             const QString &context) const;
0113 
0114     static GlobalShortcutsRegistry *self();
0115 
0116     bool registerKey(int key, GlobalShortcut *shortcut);
0117 
0118     void setAccelManager(KGlobalAccelInterface *manager);
0119 
0120     void setDBusPath(const QDBusObjectPath &path);
0121 
0122     bool unregisterKey(int key, GlobalShortcut *shortcut);
0123 
0124 public Q_SLOTS:
0125 
0126     void clear();
0127 
0128     void loadSettings();
0129 
0130     void writeSettings() const;
0131 
0132     // Grab the keys
0133     void grabKeys();
0134 
0135     // Ungrab the keys
0136     void ungrabKeys();
0137 
0138 private:
0139 
0140     friend class KdeDGlobalAccel::Component;
0141     friend class KGlobalAccelInterface;
0142 
0143     KdeDGlobalAccel::Component *addComponent(KdeDGlobalAccel::Component *component);
0144     KdeDGlobalAccel::Component *takeComponent(KdeDGlobalAccel::Component *component);
0145 
0146     //called by the implementation to inform us about key presses
0147     //returns true if the key was handled
0148     bool keyPressed(int keyQt);
0149 
0150     QHash<int, GlobalShortcut*> _active_keys;
0151     QHash<QString, KdeDGlobalAccel::Component *> _components;
0152 
0153     KGlobalAccelInterface *_manager;
0154 
0155     mutable KConfig _config;
0156 
0157     QDBusObjectPath _dbusPath;
0158     };
0159 
0160 
0161 #endif /* #ifndef GLOBALSHORTCUTSREGISTRY_H */