1 | /* Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz> |
2 | |
3 | This library is free software; you can redistribute it and/or |
4 | modify it under the terms of the GNU Library General Public |
5 | License as published by the Free Software Foundation; either |
6 | version 2 of the License, or (at your option) any later version. |
7 | |
8 | This library is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
11 | Library General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU Library General Public License |
14 | along with this library; see the file COPYING.LIB. If not, write to |
15 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
16 | Boston, MA 02110-1301, USA. |
17 | */ |
18 | |
19 | #include "globalshortcut.h" |
20 | |
21 | #include "component.h" |
22 | #include "globalshortcutcontext.h" |
23 | #include "globalshortcutsregistry.h" |
24 | |
25 | #include <kdebug.h> |
26 | |
27 | #include <QKeySequence> |
28 | |
29 | |
30 | GlobalShortcut::GlobalShortcut() |
31 | : _isPresent(false) |
32 | ,_isRegistered(false) |
33 | ,_isFresh(true) |
34 | ,_context(NULL) |
35 | ,_uniqueName() |
36 | ,_friendlyName() |
37 | ,_keys() |
38 | ,_defaultKeys() |
39 | {} |
40 | |
41 | |
42 | GlobalShortcut::GlobalShortcut( |
43 | const QString &uniqueName, |
44 | const QString &friendlyName, |
45 | GlobalShortcutContext *context) |
46 | : _isPresent(false) |
47 | ,_isRegistered(false) |
48 | ,_isFresh(true) |
49 | ,_context(context) |
50 | ,_uniqueName(uniqueName) |
51 | ,_friendlyName(friendlyName) |
52 | ,_keys() |
53 | ,_defaultKeys() |
54 | { |
55 | context->addShortcut(this); |
56 | } |
57 | |
58 | |
59 | GlobalShortcut::~GlobalShortcut() |
60 | { |
61 | setInactive(); |
62 | } |
63 | |
64 | |
65 | GlobalShortcut::operator KGlobalShortcutInfo () const |
66 | { |
67 | KGlobalShortcutInfo info; |
68 | info.d->uniqueName = _uniqueName; |
69 | info.d->friendlyName = _friendlyName; |
70 | info.d->contextUniqueName = context()->uniqueName(); |
71 | info.d->contextFriendlyName = context()->friendlyName(); |
72 | info.d->componentUniqueName = context()->component()->uniqueName(); |
73 | info.d->componentFriendlyName = context()->component()->friendlyName(); |
74 | Q_FOREACH (int key, _keys) |
75 | { |
76 | info.d->keys.append(QKeySequence(key)); |
77 | } |
78 | Q_FOREACH (int key, _defaultKeys) |
79 | { |
80 | info.d->defaultKeys.append(QKeySequence(key)); |
81 | } |
82 | return info; |
83 | } |
84 | |
85 | |
86 | bool GlobalShortcut::isActive() const |
87 | { |
88 | return _isRegistered; |
89 | } |
90 | |
91 | |
92 | bool GlobalShortcut::isFresh() const |
93 | { |
94 | return _isFresh; |
95 | } |
96 | |
97 | |
98 | bool GlobalShortcut::isPresent() const |
99 | { |
100 | return _isPresent; |
101 | } |
102 | |
103 | |
104 | bool GlobalShortcut::isSessionShortcut() const |
105 | { |
106 | return uniqueName().startsWith(QLatin1String("_k_session:" )); |
107 | } |
108 | |
109 | |
110 | void GlobalShortcut::setIsFresh(bool value) |
111 | { |
112 | _isFresh = value; |
113 | } |
114 | |
115 | |
116 | void GlobalShortcut::setIsPresent(bool value) |
117 | { |
118 | // (de)activate depending on old/new value |
119 | _isPresent = value; |
120 | value |
121 | ? setActive() |
122 | : setInactive(); |
123 | } |
124 | |
125 | |
126 | GlobalShortcutContext *GlobalShortcut::context() |
127 | { |
128 | return _context; |
129 | } |
130 | |
131 | |
132 | GlobalShortcutContext const *GlobalShortcut::context() const |
133 | { |
134 | return _context; |
135 | } |
136 | |
137 | |
138 | QString GlobalShortcut::uniqueName() const |
139 | { |
140 | return _uniqueName; |
141 | } |
142 | |
143 | |
144 | void GlobalShortcut::unRegister() |
145 | { |
146 | return _context->component()->unregisterShortcut(uniqueName()); |
147 | } |
148 | |
149 | |
150 | QString GlobalShortcut::friendlyName() const |
151 | { |
152 | return _friendlyName; |
153 | } |
154 | |
155 | |
156 | void GlobalShortcut::setFriendlyName(const QString &name) |
157 | { |
158 | _friendlyName = name; |
159 | } |
160 | |
161 | |
162 | QList<int> GlobalShortcut::keys() const |
163 | { |
164 | return _keys; |
165 | } |
166 | |
167 | |
168 | void GlobalShortcut::setKeys(const QList<int> newKeys) |
169 | { |
170 | bool active = _isRegistered; |
171 | if (active) |
172 | { |
173 | setInactive(); |
174 | } |
175 | |
176 | _keys = QList<int>(); |
177 | |
178 | Q_FOREACH(int key, newKeys) |
179 | { |
180 | if (key!=0 && !GlobalShortcutsRegistry::self()->getShortcutByKey(key)) |
181 | { |
182 | _keys.append(key); |
183 | } |
184 | else |
185 | { |
186 | kDebug() << _uniqueName << "skipping because key" << QKeySequence(key).toString() << "is already taken" ; |
187 | _keys.append(0); |
188 | } |
189 | } |
190 | |
191 | if (active) |
192 | { |
193 | setActive(); |
194 | } |
195 | } |
196 | |
197 | |
198 | QList<int> GlobalShortcut::defaultKeys() const |
199 | { |
200 | return _defaultKeys; |
201 | } |
202 | |
203 | |
204 | void GlobalShortcut::setDefaultKeys(const QList<int> newKeys) |
205 | { |
206 | _defaultKeys = newKeys; |
207 | } |
208 | |
209 | |
210 | void GlobalShortcut::setActive() |
211 | { |
212 | if (!_isPresent || _isRegistered) |
213 | { |
214 | // The corresponding application is not present or the keys are |
215 | // already grabbed |
216 | return; |
217 | } |
218 | |
219 | Q_FOREACH( int key, _keys) |
220 | { |
221 | if (key != 0 && !GlobalShortcutsRegistry::self()->registerKey(key, this)) |
222 | { |
223 | kDebug() << uniqueName() << ": Failed to register " << QKeySequence(key).toString(); |
224 | } |
225 | } |
226 | |
227 | _isRegistered = true; |
228 | } |
229 | |
230 | |
231 | void GlobalShortcut::setInactive() |
232 | { |
233 | if (!_isRegistered) |
234 | { |
235 | // The keys are not grabbed currently |
236 | return; |
237 | } |
238 | |
239 | Q_FOREACH( int key, _keys) |
240 | { |
241 | if (key != 0 && !GlobalShortcutsRegistry::self()->unregisterKey(key, this)) |
242 | { |
243 | kDebug() << uniqueName() << ": Failed to unregister " << QKeySequence(key).toString(); |
244 | } |
245 | } |
246 | |
247 | _isRegistered = false; |
248 | } |
249 | |
250 | |