File indexing completed on 2019-04-16 12:08:05

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