delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / kdedglobalaccel / component.cpp
blobedf2579332b6e044efc1022432a493eb37adb11d
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 "component.h"
21 #include "globalshortcut.h"
22 #include "globalshortcutcontext.h"
23 #include "globalshortcutsregistry.h"
25 #include <kdebug.h>
27 #include <QtCore/QStringList>
28 #include <QtGui/QKeySequence>
30 static QList<int> keysFromString(const QString &str)
32 QList<int> ret;
33 if (str == "none") {
34 return ret;
36 QStringList strList = str.split('\t');
37 foreach (const QString &s, strList) {
38 int key = QKeySequence(s)[0];
39 if (key != -1) { //sanity check just in case
40 ret.append(key);
43 return ret;
47 static QString stringFromKeys(const QList<int> &keys)
49 if (keys.isEmpty()) {
50 return "none";
52 QString ret;
53 foreach (int key, keys) {
54 ret.append(QKeySequence(key).toString());
55 ret.append('\t');
57 ret.chop(1);
58 return ret;
61 namespace KdeDGlobalAccel {
63 Component::Component(
64 const QString &uniqueName,
65 const QString &friendlyName,
66 GlobalShortcutsRegistry *registry)
67 : _uniqueName(uniqueName)
68 ,_friendlyName(friendlyName)
69 ,_registry(registry)
71 // Make sure we do no get uniquenames still containing the context
72 Q_ASSERT(uniqueName.indexOf("|")==-1);
74 // Register ourselve with the registry
75 if (_registry)
77 _registry->addComponent(this);
80 createGlobalShortcutContext("default", "Default Context");
81 _current = _contexts.value("default");
85 Component::~Component()
87 // Remove ourselve from the registry
88 if (_registry)
90 _registry->takeComponent(this);
93 // We delete all shortcuts from all contexts
94 qDeleteAll(_contexts);
98 bool Component::activateGlobalShortcutContext(
99 const QString &uniqueName)
101 if (!_contexts.value(uniqueName))
103 createGlobalShortcutContext(uniqueName, "TODO4");
104 return false;
107 // Deactivate the current contexts shortcuts
108 deactivateShortcuts();
110 // Switch the context
111 _current = _contexts.value(uniqueName);
113 return true;
117 void Component::activateShortcuts()
119 Q_FOREACH (GlobalShortcut *shortcut, _current->_actions)
121 shortcut->setActive();
126 QList<GlobalShortcut*> Component::allShortcuts(const QString &contextName) const
128 GlobalShortcutContext *context = _contexts.value(contextName);
129 if (context)
131 return context->_actions.values();
133 else
135 Q_ASSERT(false); // Unknown context
136 return QList<GlobalShortcut*> ();
141 QList<KGlobalShortcutInfo> Component::allShortcutInfos(const QString &contextName) const
143 QList<KGlobalShortcutInfo> rc;
145 GlobalShortcutContext *context = _contexts.value(contextName);
146 if (!context)
148 Q_ASSERT(false); // Unknown context
149 return rc;
152 return context->allShortcutInfos();
156 bool Component::createGlobalShortcutContext(
157 const QString &uniqueName,
158 const QString &friendlyName)
160 if (_contexts.value(uniqueName))
162 kDebug() << "Shortcut Context " << uniqueName << "already exists for component " << _uniqueName;
163 return false;
165 _contexts.insert(uniqueName, new GlobalShortcutContext(uniqueName, friendlyName, this));
166 return true;
170 GlobalShortcutContext *Component::currentContext()
172 return _current;
176 QDBusObjectPath Component::dbusPath() const
178 QString dbusPath = _uniqueName;
179 // Clean up for dbus usage: any non-alphanumeric char should be turned into '_'
180 const int len = dbusPath.length();
181 for ( int i = 0; i < len; ++i )
183 if ( !dbusPath[i].isLetterOrNumber() )
184 dbusPath[i] = QLatin1Char('_');
186 // QDBusObjectPath could be a little bit easier to handle :-)
187 return QDBusObjectPath( _registry->dbusPath().path() + "/component/" + dbusPath);
191 void Component::deactivateShortcuts(bool temporarily)
193 Q_FOREACH (GlobalShortcut *shortcut, _current->_actions)
195 if (temporarily
196 && uniqueName() == "kwin"
197 && shortcut->uniqueName() == "Block Global Shortcuts")
199 continue;
201 shortcut->setInactive();
206 QString Component::friendlyName() const
208 return _friendlyName;
212 GlobalShortcut *Component::getShortcutByKey(int key) const
214 return _current->getShortcutByKey(key);
218 QList<GlobalShortcut *> Component::getShortcutsByKey(int key) const
220 QList <GlobalShortcut *> rc;
221 Q_FOREACH(GlobalShortcutContext *context, _contexts)
223 GlobalShortcut *sc = context->getShortcutByKey(key);
224 if (sc) rc.append(sc);
226 return rc;
230 GlobalShortcut *Component::getShortcutByName(const QString &uniqueName, const QString &context) const
232 if (!_contexts.value(context))
234 return NULL;
237 return _contexts.value(context)->_actions.value(uniqueName);
241 QStringList Component::getShortcutContexts() const
243 return _contexts.keys();
247 bool Component::isShortcutAvailable(
248 int key,
249 const QString &component,
250 const QString &context) const
252 kDebug() << QKeySequence(key).toString() << component;
254 // if this component asks for the key. only check the keys in the same
255 // context
256 if (component==uniqueName())
258 Q_FOREACH(GlobalShortcut *sc, shortcutContext(context)->_actions)
260 if (sc->keys().contains(key)) return false;
263 else
265 Q_FOREACH(GlobalShortcutContext *ctx, _contexts)
267 Q_FOREACH(GlobalShortcut *sc, ctx->_actions)
269 if (sc->keys().contains(key)) return false;
273 return true;
277 void Component::loadSettings(KConfigGroup &configGroup)
279 // GlobalShortcutsRegistry::loadSettings handles contexts.
280 Q_FOREACH (const QString &confKey, configGroup.keyList())
282 const QStringList entry = configGroup.readEntry(confKey, QStringList());
283 if (entry.size() != 3)
285 continue;
288 // The shortcut will register itself with us
289 GlobalShortcut *shortcut = new GlobalShortcut(
290 confKey,
291 entry[2],
292 _current);
294 QList<int> keys = keysFromString(entry[0]);
295 shortcut->setDefaultKeys(keysFromString(entry[1]));
296 shortcut->setIsFresh(false);
298 Q_FOREACH (int key, keys)
300 if (key != 0)
302 if (GlobalShortcutsRegistry::self()->getShortcutByKey(key))
304 // The shortcut is already used. The config file is
305 // broken. Ignore the request.
306 keys.removeAll(key);
307 kWarning() << "Shortcut found twice in kglobalshortcutsrc.";
311 shortcut->setKeys(keys);
316 void Component::setFriendlyName(const QString &name)
318 _friendlyName = name;
322 GlobalShortcutContext *Component::shortcutContext( const QString &contextName )
324 return _contexts.value(contextName);
327 GlobalShortcutContext const *Component::shortcutContext( const QString &contextName ) const
329 return _contexts.value(contextName);
332 QStringList Component::shortcutNames( const QString &contextName) const
334 GlobalShortcutContext *context = _contexts.value(contextName);
335 if (!context)
337 Q_ASSERT(false); // Unknown context
338 return QStringList();
341 return context->_actions.keys();
345 QString Component::uniqueName() const
347 return _uniqueName;
351 void Component::writeSettings(KConfigGroup& configGroup) const
353 // If we don't delete the current content global shortcut
354 // registrations will never not deleted after forgetGlobalShortcut()
355 configGroup.deleteGroup();
358 // Now wrote all contexts
359 Q_FOREACH( GlobalShortcutContext *context, _contexts)
361 KConfigGroup contextGroup;
363 if (context->uniqueName() == "default")
365 contextGroup = configGroup;
366 // Write the friendly name
367 contextGroup.writeEntry("_k_friendly_name", friendlyName());
369 else
371 contextGroup = KConfigGroup(&configGroup, context->uniqueName());
372 // Write the friendly name
373 contextGroup.writeEntry("_k_friendly_name", context->friendlyName());
376 // kDebug() << "writing group " << _uniqueName << ":" << context->uniqueName();
378 Q_FOREACH(const GlobalShortcut *shortcut, context->_actions)
380 // kDebug() << "writing" << shortcut->uniqueName();
382 // We do not write fresh shortcuts.
383 // We do not write session shortcuts
384 if (shortcut->isFresh() || shortcut->isSessionShortcut())
386 continue;
388 // kDebug() << "really writing" << shortcut->uniqueName();
390 QStringList entry(stringFromKeys(shortcut->keys()));
391 entry.append(stringFromKeys(shortcut->defaultKeys()));
392 entry.append(shortcut->friendlyName());
394 contextGroup.writeEntry(shortcut->uniqueName(), entry);
399 #include "moc_component.cpp"
401 } // namespace KdeDGlobalAccel