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"
27 #include <QtGui/QKeySequence>
30 GlobalShortcut::GlobalShortcut()
42 GlobalShortcut::GlobalShortcut(
43 const QString
&uniqueName
,
44 const QString
&friendlyName
,
45 GlobalShortcutContext
*context
)
50 ,_uniqueName(uniqueName
)
51 ,_friendlyName(friendlyName
)
55 context
->addShortcut(this);
59 GlobalShortcut::~GlobalShortcut()
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
));
87 bool GlobalShortcut::isActive() const
93 bool GlobalShortcut::isFresh() const
99 bool GlobalShortcut::isPresent() const
105 bool GlobalShortcut::isSessionShortcut() const
107 return uniqueName().startsWith("_k_session:");
111 void GlobalShortcut::setIsFresh(bool value
)
117 void GlobalShortcut::setIsPresent(bool value
)
119 // (de)activate depending on old/new value
127 GlobalShortcutContext
*GlobalShortcut::context()
133 GlobalShortcutContext
const *GlobalShortcut::context() const
139 QString
GlobalShortcut::uniqueName() const
145 QString
GlobalShortcut::friendlyName() const
147 return _friendlyName
;
151 void GlobalShortcut::setFriendlyName(const QString
&name
)
153 _friendlyName
= name
;
157 QList
<int> GlobalShortcut::keys() const
163 void GlobalShortcut::setKeys(const QList
<int> newKeys
)
165 bool active
= _isRegistered
;
171 _keys
= QList
<int>();
173 Q_FOREACH(int key
, newKeys
)
175 if (key
!=0 && !GlobalShortcutsRegistry::self()->getShortcutByKey(key
))
181 kDebug() << _uniqueName
<< "skipping because key" << QKeySequence(key
).toString() << "is already taken";
193 QList
<int> GlobalShortcut::defaultKeys() const
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
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()
230 // The keys are not grabbed currently
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;