KGlobalAccel

globalshortcut.cpp
1 /*
2  SPDX-FileCopyrightText: 2008 Michael Jansen <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "globalshortcut.h"
8 
9 #ifdef __GNUC__
10 #warning Remove private header kglobalshortcutinfo_p.once this kglobalaccel daemon is merged with the kglobalaccel framework
11 #endif
12 #include "kglobalshortcutinfo_p.h"
13 
14 #include "component.h"
15 #include "globalshortcutcontext.h"
16 #include "globalshortcutsregistry.h"
17 #include "logging_p.h"
18 
19 #include <QKeySequence>
20 
21 GlobalShortcut::GlobalShortcut()
22  : _isPresent(false)
23  , _isRegistered(false)
24  , _isFresh(true)
25  , _context(nullptr)
26  , _uniqueName()
27  , _friendlyName()
28  , _keys()
29  , _defaultKeys()
30 {
31 }
32 
33 GlobalShortcut::GlobalShortcut(const QString &uniqueName, const QString &friendlyName, GlobalShortcutContext *context)
34  : _isPresent(false)
35  , _isRegistered(false)
36  , _isFresh(true)
37  , _context(context)
38  , _uniqueName(uniqueName)
39  , _friendlyName(friendlyName)
40  , _keys()
41  , _defaultKeys()
42 {
43  context->addShortcut(this);
44 }
45 
46 GlobalShortcut::~GlobalShortcut()
47 {
48  setInactive();
49 }
50 
51 GlobalShortcut::operator KGlobalShortcutInfo() const
52 {
54  info.d->uniqueName = _uniqueName;
55  info.d->friendlyName = _friendlyName;
56  info.d->contextUniqueName = context()->uniqueName();
57  info.d->contextFriendlyName = context()->friendlyName();
58  info.d->componentUniqueName = context()->component()->uniqueName();
59  info.d->componentFriendlyName = context()->component()->friendlyName();
60  for (int key : qAsConst(_keys)) {
61  info.d->keys.append(QKeySequence(key));
62  }
63  for (int key : qAsConst(_defaultKeys)) {
64  info.d->defaultKeys.append(QKeySequence(key));
65  }
66  return info;
67 }
68 
70 {
71  return _isRegistered;
72 }
73 
75 {
76  return _isFresh;
77 }
78 
80 {
81  return _isPresent;
82 }
83 
85 {
86  return uniqueName().startsWith(QLatin1String("_k_session:"));
87 }
88 
89 void GlobalShortcut::setIsFresh(bool value)
90 {
91  _isFresh = value;
92 }
93 
94 void GlobalShortcut::setIsPresent(bool value)
95 {
96  // (de)activate depending on old/new value
97  _isPresent = value;
98  value ? setActive() : setInactive();
99 }
100 
102 {
103  return _context;
104 }
105 
107 {
108  return _context;
109 }
110 
112 {
113  return _uniqueName;
114 }
115 
117 {
118  return _context->component()->unregisterShortcut(uniqueName());
119 }
120 
122 {
123  return _friendlyName;
124 }
125 
127 {
128  _friendlyName = name;
129 }
130 
132 {
133  return _keys;
134 }
135 
137 {
138  bool active = _isRegistered;
139  if (active) {
140  setInactive();
141  }
142 
143  _keys = QList<int>();
144 
145  for (int key : qAsConst(newKeys)) {
146  if (key != 0 && !GlobalShortcutsRegistry::self()->getShortcutByKey(key)) {
147  _keys.append(key);
148  } else {
149  qCDebug(KGLOBALACCELD) << _uniqueName << "skipping because key" << QKeySequence(key).toString() << "is already taken";
150  _keys.append(0);
151  }
152  }
153 
154  if (active) {
155  setActive();
156  }
157 }
158 
160 {
161  return _defaultKeys;
162 }
163 
165 {
166  _defaultKeys = newKeys;
167 }
168 
170 {
171  if (!_isPresent || _isRegistered) {
172  // The corresponding application is not present or the keys are
173  // already grabbed
174  return;
175  }
176 
177  for (int key : qAsConst(_keys)) {
178  if (key != 0 && !GlobalShortcutsRegistry::self()->registerKey(key, this)) {
179  qCDebug(KGLOBALACCELD) << uniqueName() << ": Failed to register " << QKeySequence(key).toString();
180  }
181  }
182 
183  _isRegistered = true;
184 }
185 
187 {
188  if (!_isRegistered) {
189  // The keys are not grabbed currently
190  return;
191  }
192 
193  for (int key : qAsConst(_keys)) {
194  if (key != 0 && !GlobalShortcutsRegistry::self()->unregisterKey(key, this)) {
195  qCDebug(KGLOBALACCELD) << uniqueName() << ": Failed to unregister " << QKeySequence(key).toString();
196  }
197  }
198 
199  _isRegistered = false;
200 }
bool isFresh() const
Check if the shortcut is fresh/new. Is an internal state.
QString & append(QChar ch)
bool isPresent() const
Check if the shortcut is present. It application is running.
GlobalShortcutContext * context()
Returns the context the shortcuts belongs to.
void setInactive()
Sets the shortcut inactive. No longer grabs the keys.
QString uniqueName() const
Returns the unique name aka id for the shortcuts.
void addShortcut(GlobalShortcut *shortcut)
Adds shortcut to the context.
void append(const T &value)
QString friendlyName() const
Returns the friendly name.
void setFriendlyName(const QString &)
Sets the friendly name for the shortcut. For display.
void unRegister()
Remove this shortcut and it&#39;s siblings.
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
QList< int > keys() const
Returns a list of keys associated with this shortcut.
bool isActive() const
Check if the shortcut is active. It&#39;s keys are grabbed.
QString toString(QKeySequence::SequenceFormat format) const const
QList< int > defaultKeys() const
Returns the default keys for this shortcut.
GlobalShortcut * getShortcutByKey(int key) const
Get the shortcut corresponding to key.
void setKeys(const QList< int >)
Sets the keys activated with this shortcut. The old keys are freed.
void setActive()
Activates the shortcut. The keys are grabbed.
void setDefaultKeys(const QList< int >)
Sets the default keys for this shortcut.
bool isSessionShortcut() const
Returns true if the shortcut is a session shortcut.
QString uniqueName() const
Get the name for the context.
QString friendlyName() const
Return the friendly display name for this shortcut.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon May 17 2021 22:53:55 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.