1 | #ifndef COMPONENT_H |
2 | #define COMPONENT_H |
3 | /* Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz> |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Library General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2 of the License, or (at your option) any later version. |
9 | |
10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Library General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Library General Public License |
16 | along with this library; see the file COPYING.LIB. If not, write to |
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
18 | Boston, MA 02110-1301, USA. |
19 | */ |
20 | |
21 | #include "globalshortcut.h" |
22 | #include "kglobalshortcutinfo.h" |
23 | |
24 | #include "kconfiggroup.h" |
25 | |
26 | #include <QtCore/QObject> |
27 | #include <QtCore/QHash> |
28 | |
29 | class GlobalShortcut; |
30 | class GlobalShortcutContext; |
31 | class GlobalShortcutsRegistry; |
32 | |
33 | |
34 | namespace KdeDGlobalAccel { |
35 | |
36 | /** |
37 | * @author Michael Jansen <kde@michael-jansen.biz> |
38 | */ |
39 | class Component : public QObject |
40 | { |
41 | Q_OBJECT |
42 | |
43 | Q_CLASSINFO("D-Bus Interface" , "org.kde.kglobalaccel.Component" ) |
44 | |
45 | Q_SCRIPTABLE Q_PROPERTY( QString friendlyName READ friendlyName ) |
46 | Q_SCRIPTABLE Q_PROPERTY( QString uniqueName READ uniqueName ) |
47 | |
48 | public: |
49 | |
50 | //! Creates a new component. The component will be registered with @p |
51 | //! registry if specified and registered with dbus. |
52 | Component( |
53 | const QString &uniqueName, |
54 | const QString &friendlyName, |
55 | GlobalShortcutsRegistry *registry = NULL); |
56 | |
57 | ~Component(); |
58 | |
59 | bool activateGlobalShortcutContext(const QString &uniqueName); |
60 | |
61 | void activateShortcuts(); |
62 | |
63 | //! Returns all shortcuts in context @context |
64 | QList<GlobalShortcut *> allShortcuts(const QString &context = "default" ) const; |
65 | |
66 | //! Creates the new global shortcut context @p context |
67 | bool createGlobalShortcutContext(const QString &context, const QString &friendlyName="" ); |
68 | |
69 | //! Return the current context |
70 | GlobalShortcutContext* currentContext(); |
71 | |
72 | //! Return uniqueName converted to a valid dbus path |
73 | QDBusObjectPath dbusPath() const; |
74 | |
75 | //! Deactivate all currently active shortcuts |
76 | void deactivateShortcuts(bool temporarily=false); |
77 | |
78 | //! Returns the friendly name |
79 | QString friendlyName() const; |
80 | |
81 | //! Returns the currently active shortcut for key |
82 | GlobalShortcut *getShortcutByKey(int key) const; |
83 | |
84 | //! Returns the shortcut context @p name or NULL |
85 | GlobalShortcutContext *shortcutContext(const QString &name); |
86 | GlobalShortcutContext const *shortcutContext(const QString &name) const; |
87 | |
88 | /** |
89 | * Returns the list of shortcuts (different context) registered with @p |
90 | * key. |
91 | */ |
92 | QList<GlobalShortcut *> getShortcutsByKey(int key) const; |
93 | |
94 | //! Returns the shortcut by unique name. Only the active context is |
95 | //! searched. |
96 | GlobalShortcut *getShortcutByName( |
97 | const QString &uniqueName, |
98 | const QString &context = "default" ) const; |
99 | |
100 | /** |
101 | * Check if @a key is available for component @p component |
102 | */ |
103 | bool isShortcutAvailable( |
104 | int key, |
105 | const QString &component, |
106 | const QString &context) const; |
107 | |
108 | //! Load the settings from config group @p config |
109 | void loadSettings(KConfigGroup &config); |
110 | |
111 | //! Sets the human readable name for this component. |
112 | void setFriendlyName(const QString &); |
113 | |
114 | QString uniqueName() const; |
115 | |
116 | //! Unregister @a shortcut. This will remove its siblings from all contexts |
117 | void unregisterShortcut(const QString &uniqueName); |
118 | |
119 | void writeSettings(KConfigGroup &config) const; |
120 | |
121 | public Q_SLOTS: |
122 | |
123 | // For dbus Q_SCRIPTABLE has to be on slots. Scriptable methods are not |
124 | // exported. |
125 | |
126 | /** |
127 | * Remove all currently not used global shortcuts registrations for this |
128 | * component and if nothing is left the component too. |
129 | * |
130 | * If the method returns true consider all information previously acquired |
131 | * from this component as void. |
132 | * |
133 | * The method will cleanup in all contexts. |
134 | * |
135 | * @return @c true if a change was made, @c false if not. |
136 | */ |
137 | Q_SCRIPTABLE bool cleanUp(); |
138 | |
139 | /** |
140 | * Check if the component is currently active. |
141 | * |
142 | * A component is active if at least one of it's global shortcuts is |
143 | * currently present. |
144 | */ |
145 | Q_SCRIPTABLE bool isActive() const; |
146 | |
147 | //! Get all shortcutnames living in @a context |
148 | Q_SCRIPTABLE QStringList shortcutNames(const QString &context = "default" ) const; |
149 | |
150 | //! Returns all shortcut in @a context |
151 | Q_SCRIPTABLE QList<KGlobalShortcutInfo> allShortcutInfos(const QString &context = "default" ) const; |
152 | |
153 | //! Returns the shortcut contexts available for the component. |
154 | Q_SCRIPTABLE QStringList getShortcutContexts() const; |
155 | |
156 | //! Start the global shortcuts kcm and show this component. |
157 | Q_SCRIPTABLE bool showKCM(); |
158 | |
159 | void emitGlobalShortcutPressed(const GlobalShortcut &shortcut); |
160 | |
161 | Q_SCRIPTABLE void invokeShortcut(const QString &shortcutName, const QString &context = "default" ); |
162 | |
163 | Q_SIGNALS: |
164 | |
165 | //! Signals that a action for this component was triggered |
166 | Q_SCRIPTABLE void globalShortcutPressed(const QString &componentUnique, const QString &shortcutUnique, qlonglong timestamp); |
167 | |
168 | private: |
169 | |
170 | QString _uniqueName; |
171 | //the name as it would be found in a magazine article about the application, |
172 | //possibly localized if a localized name exists. |
173 | QString _friendlyName; |
174 | |
175 | GlobalShortcutsRegistry *_registry; |
176 | |
177 | GlobalShortcutContext *_current; |
178 | QHash<QString, GlobalShortcutContext *> _contexts; |
179 | }; |
180 | |
181 | } |
182 | |
183 | |
184 | #endif /* #ifndef COMPONENT_H */ |
185 | |