delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / kdedglobalaccel / globalshortcut.cpp
blobdd60bee3f5c653b69bf67ea57a345f3c8a6cff64
1 /* Copyright (C) 2008 Michael Jansen <kde@michael-jansen.biz>
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.
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.
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.
19 #include "globalshortcut.h"
21 #include "component.h"
22 #include "globalshortcutcontext.h"
23 #include "globalshortcutsregistry.h"
25 #include <kdebug.h>
27 #include <QtGui/QKeySequence>
30 GlobalShortcut::GlobalShortcut()
31 : _isPresent(false)
32 ,_isRegistered(false)
33 ,_isFresh(true)
34 ,_context(NULL)
35 ,_uniqueName()
36 ,_friendlyName()
37 ,_keys()
38 ,_defaultKeys()
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()
55 context->addShortcut(this);
59 GlobalShortcut::~GlobalShortcut()
61 setInactive();
62 _context->takeShortcut(this);
66 GlobalShortcut::operator KGlobalShortcutInfo () const
68 KGlobalShortcutInfo info;
69 info.d->uniqueName = _uniqueName;
70 info.d->friendlyName = _friendlyName;
71 info.d->contextUniqueName = context()->uniqueName();
72 info.d->contextFriendlyName = context()->friendlyName();
73 info.d->componentUniqueName = context()->component()->uniqueName();
74 info.d->componentFriendlyName = context()->component()->friendlyName();
75 Q_FOREACH (int key, _keys)
77 info.d->keys.append(QKeySequence(key));
79 Q_FOREACH (int key, _defaultKeys)
81 info.d->defaultKeys.append(QKeySequence(key));
83 return info;
87 bool GlobalShortcut::isActive() const
89 return _isRegistered;
93 bool GlobalShortcut::isFresh() const
95 return _isFresh;
99 bool GlobalShortcut::isPresent() const
101 return _isPresent;
105 bool GlobalShortcut::isSessionShortcut() const
107 return uniqueName().startsWith("_k_session:");
111 void GlobalShortcut::setIsFresh(bool value)
113 _isFresh = value;
117 void GlobalShortcut::setIsPresent(bool value)
119 // (de)activate depending on old/new value
120 _isPresent = value;
121 value
122 ? setActive()
123 : setInactive();
127 GlobalShortcutContext *GlobalShortcut::context()
129 return _context;
133 GlobalShortcutContext const *GlobalShortcut::context() const
135 return _context;
139 QString GlobalShortcut::uniqueName() const
141 return _uniqueName;
145 QString GlobalShortcut::friendlyName() const
147 return _friendlyName;
151 void GlobalShortcut::setFriendlyName(const QString &name)
153 _friendlyName = name;
157 QList<int> GlobalShortcut::keys() const
159 return _keys;
163 void GlobalShortcut::setKeys(const QList<int> newKeys)
165 bool active = _isRegistered;
166 if (active)
168 setInactive();
171 _keys = QList<int>();
173 Q_FOREACH(int key, newKeys)
175 if (key!=0 && !GlobalShortcutsRegistry::self()->getShortcutByKey(key))
177 _keys.append(key);
179 else
181 kDebug() << _uniqueName << "skipping because key" << QKeySequence(key).toString() << "is already taken";
182 _keys.append(0);
186 if (active)
188 setActive();
193 QList<int> GlobalShortcut::defaultKeys() const
195 return _defaultKeys;
199 void GlobalShortcut::setDefaultKeys(const QList<int> newKeys)
201 _defaultKeys = newKeys;
205 void GlobalShortcut::setActive()
207 if (!_isPresent || _isRegistered)
209 // The corresponding application is not present or the keys are
210 // already grabbed
211 return;
214 Q_FOREACH( int key, _keys)
216 if (key != 0 && !GlobalShortcutsRegistry::self()->registerKey(key, this))
218 kDebug() << uniqueName() << ": Failed to register " << QKeySequence(key).toString();
222 _isRegistered = true;
226 void GlobalShortcut::setInactive()
228 if (!_isRegistered)
230 // The keys are not grabbed currently
231 return;
234 Q_FOREACH( int key, _keys)
236 if (key != 0 && !GlobalShortcutsRegistry::self()->unregisterKey(key, this))
238 kDebug() << uniqueName() << ": Failed to unregister " << QKeySequence(key).toString();
242 _isRegistered = false;