24 #include "kglobalaccel_p.h" 28 #include <QtDBus/QDBusInterface> 29 #include <QtDBus/QDBusMetaType> 31 #include <QtGui/QX11Info> 40 #include "kaction_p.h" 44 org::kde::kglobalaccel::Component *KGlobalAccelPrivate::getComponent(
const QString &componentUnique,
bool remember =
false)
47 if (components.contains(componentUnique)) {
48 return components[componentUnique];
52 org::kde::KGlobalAccel kglobalaccel(
53 "org.kde.kglobalaccel",
55 QDBusConnection::sessionBus());
56 if (!kglobalaccel.isValid()) {
57 kDebug() <<
"Failed to connect to the kglobalaccel daemon" << QDBusConnection::sessionBus().lastError();
63 QDBusReply<QDBusObjectPath> reply = kglobalaccel.getComponent(componentUnique);
64 if (!reply.isValid()) {
66 if (reply.error().name() ==
"org.kde.kglobalaccel.NoSuchComponent") {
72 kDebug() <<
"Failed to get dbus path for component " << componentUnique << reply.error();
77 org::kde::kglobalaccel::Component *component =
new org::kde::kglobalaccel::Component(
78 "org.kde.kglobalaccel",
80 QDBusConnection::sessionBus(),
84 if (!component->isValid()) {
85 kDebug() <<
"Failed to get component" << componentUnique << QDBusConnection::sessionBus().lastError();
92 q->connect(component, SIGNAL(globalShortcutPressed(QString,QString,qlonglong)),
93 SLOT(_k_invokeAction(QString,QString,qlonglong)));
95 components[componentUnique] = component;
103 KGlobalAccelPrivate::KGlobalAccelPrivate(
KGlobalAccel *q)
104 : isUsingForeignComponentName(false),
105 #ifndef KDE_NO_DEPRECATED
108 iface(
"org.kde.kglobalaccel",
"/kglobalaccel", QDBusConnection::sessionBus()),
113 QDBusConnectionInterface* bus = QDBusConnection::sessionBus().interface();
114 if (!bus->isServiceRegistered(
"org.kde.kglobalaccel")) {
117 "kglobalaccel.desktop",
122 kError() <<
"Couldn't start kglobalaccel from kglobalaccel.desktop: " << error << endl;
125 QDBusServiceWatcher *watcher =
new QDBusServiceWatcher(iface.service(),
126 QDBusConnection::sessionBus(),
127 QDBusServiceWatcher::WatchForOwnerChange,
129 q->connect(watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)),
130 q, SLOT(_k_serviceOwnerChanged(QString,QString,QString)));
134 void KGlobalAccelPrivate::readComponentData(
const KComponentData &componentData)
145 KGlobalAccel::KGlobalAccel()
146 : d(new KGlobalAccelPrivate(this))
148 qDBusRegisterMetaType<QList<int> >();
149 qDBusRegisterMetaType<QList<QStringList> >();
150 qDBusRegisterMetaType<KGlobalShortcutInfo>();
151 qDBusRegisterMetaType<QList<KGlobalShortcutInfo> >();
153 connect(&d->iface, SIGNAL(yourShortcutGotChanged(QStringList,
QList<int>)),
154 SLOT(_k_shortcutGotChanged(QStringList,
QList<int>)));
163 KGlobalAccel::~KGlobalAccel()
170 const QString &contextUnique,
171 const QString &contextFriendly,
174 Q_UNUSED(contextFriendly);
176 self()->d->iface.activateGlobalShortcutContext(component.
aboutData()->
programName(), contextUnique);
183 org::kde::kglobalaccel::Component* component =
self()->getComponent(componentUnique);
184 if (!component)
return false;
186 return component->cleanUp();
193 org::kde::kglobalaccel::Component* component =
self()->getComponent(componentUnique);
194 if (!component)
return false;
196 return component->isActive();
200 #ifndef KDE_NO_DEPRECATED 208 org::kde::kglobalaccel::Component *KGlobalAccel::getComponent(
const QString &componentUnique)
210 return d->getComponent(componentUnique);
214 #ifndef KDE_NO_DEPRECATED 217 d->enabled = enabled;
222 #ifndef KDE_NO_DEPRECATED 225 d->readComponentData(kcd);
226 d->isUsingForeignComponentName =
true;
238 void KGlobalAccelPrivate::doRegister(
KAction *action)
240 if (!action || action->objectName().isEmpty()) {
244 const bool isRegistered = actions.contains(action);
250 if (isUsingForeignComponentName) {
253 QStringList actionId = makeActionId(action);
256 actions.insert(action);
257 iface.doRegister(actionId);
263 if (!action || action->objectName().isEmpty()) {
267 const bool isRegistered = actions.contains(action);
272 QStringList actionId = makeActionId(action);
275 actions.remove(action);
277 if (removal == UnRegister) {
280 iface.unRegister(actionId);
284 if (!action->property(
"isConfigurationAction").toBool()) {
286 action->objectName().startsWith(QLatin1String(
"_k_session:"))
287 ? iface.unRegister(actionId)
288 : iface.setInactive(actionId);
294 void KGlobalAccelPrivate::updateGlobalShortcut(
KAction *action, uint flags)
298 if (!action || action->objectName().isEmpty()) {
302 QStringList actionId = makeActionId(action);
306 uint setterFlags = 0;
308 setterFlags |= NoAutoloading;
312 bool isConfigurationAction = isUsingForeignComponentName
313 || action->property(
"isConfigurationAction").toBool();
314 uint activeSetterFlags = setterFlags;
317 if (!isConfigurationAction) {
318 activeSetterFlags |= SetPresent;
324 intListFromShortcut(activeShortcut),
328 getComponent(componentUniqueForAction(action),
true);
331 const KShortcut scResult(shortcutFromIntList(result));
344 iface.setForeignShortcut(actionId, result);
346 if (scResult != activeShortcut) {
349 action->d->setActiveGlobalShortcutNoEnable(scResult);
354 iface.setShortcut(actionId, intListFromShortcut(defaultShortcut),
355 setterFlags | IsDefault);
360 QStringList KGlobalAccelPrivate::makeActionId(
const KAction *action)
362 QStringList ret(componentUniqueForAction(action));
364 Q_ASSERT(!action->objectName().isEmpty());
365 ret.append(action->objectName());
366 ret.append(componentFriendlyForAction(action));
368 ret.append(actionText);
376 ret.append(
cut.primary()[0]);
377 ret.append(
cut.alternate()[0]);
378 while (!ret.isEmpty() && ret.last() == 0)
387 if (list.count() > 0)
389 if (list.count() > 1)
395 QString KGlobalAccelPrivate::componentUniqueForAction(
const KAction *action)
397 Q_ASSERT(action->d->componentData.isValid());
398 return action->d->componentData.componentName();
402 QString KGlobalAccelPrivate::componentFriendlyForAction(
const KAction *action)
404 Q_ASSERT(action->d->componentData.isValid());
405 return action->d->componentData.aboutData()->programName();
409 void KGlobalAccelPrivate::_k_invokeAction(
410 const QString &componentUnique,
411 const QString &actionUnique,
416 if (isUsingForeignComponentName ) {
422 foreach (
KAction *
const a, candidates) {
423 if (componentUniqueForAction(a) == componentUnique) {
432 if (!action || !action->isEnabled() || action->property(
"isConfigurationAction").toBool()) {
442 QX11Info::setAppTime(timestamp);
444 QX11Info::setAppUserTime(timestamp);
453 void KGlobalAccelPrivate::_k_shortcutGotChanged(
const QStringList &actionId,
460 action->d->setActiveGlobalShortcutNoEnable(shortcutFromIntList(keys));
463 void KGlobalAccelPrivate::_k_serviceOwnerChanged(
const QString &
name,
const QString &oldOwner,
464 const QString &newOwner)
467 if (
name == QLatin1String(
"org.kde.kglobalaccel") && !newOwner.isEmpty()) {
469 kDebug(123) <<
"detected kglobalaccel restarting, re-registering all shortcut keys";
474 void KGlobalAccelPrivate::reRegisterAll()
486 nameToAction.clear();
488 foreach(
KAction *
const action, allActions) {
495 #ifndef KDE_NO_DEPRECATED 498 return d->iface.allMainComponents();
503 #ifndef KDE_NO_DEPRECATED 506 return d->iface.allActionsForComponent(actionId);
512 #ifndef KDE_NO_DEPRECATED 515 return self()->d->iface.action(seq[0]);
522 return self()->d->iface.getGlobalShortcutsByKey(seq[0]);
528 return self()->d->iface.isGlobalShortcutAvailable(seq[0], comp);
533 #ifndef KDE_NO_DEPRECATED 535 const QKeySequence &seq)
537 if (actionIdentifier.size() < 4) {
540 QString title =
i18n(
"Conflict with Global Shortcut");
541 QString
message =
i18n(
"The '%1' key combination has already been allocated " 542 "to the global action \"%2\" in %3.\n" 543 "Do you want to reassign it from that action to the current one?",
557 const QKeySequence &seq)
559 if (shortcuts.isEmpty()) {
564 QString component = shortcuts[0].componentFriendlyName();
567 if (shortcuts.size()==1) {
568 message =
i18n(
"The '%1' key combination is registered by application %2 for action %3:",
571 shortcuts[0].friendlyName());
575 actionList +=
i18n(
"In context '%1' for action '%2'\n",
579 message =
i18n(
"The '%1' key combination is registered by application %2.\n%3",
585 QString title =
i18n(
"Conflict With Registered Global Shortcut");
596 const QStringList actionId =
self()->d->iface.action(seq[0]);
597 if (actionId.size() < 4)
599 QList<int> sc =
self()->d->iface.shortcut(actionId);
601 for (
int i = 0; i < sc.count(); i++)
605 self()->d->iface.setForeignShortcut(actionId, sc);
608 #include "kglobalaccel.moc" void message(KMessage::MessageType messageType, const QString &text, const QString &caption=QString())
QString i18n(const char *text)
const KAboutData * aboutData() const
KAction * cut(const QObject *recvr, const char *slot, QObject *parent)
Cut selected area and store it in the clipboard.
#define K_GLOBAL_STATIC(TYPE, NAME)
const char * name(StandardAction id)
This will return the internal name of a given standard action.
static bool cleanComponent(const QString &componentUnique)
Clean the shortcuts for component componentUnique.
static QDebug kError(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
Defines platform-independent classes for keyboard shortcut handling.
The shortcut is a default shortcut - it becomes active when somebody decides to reset shortcuts to de...
static QDebug kDebug(bool cond, int area=KDE_DEFAULT_DEBUG_AREA)
Components Unique Name (ID)
Represents a keyboard shortcut.
QString componentName() const
static int timestampCompare(unsigned long time1, unsigned long time2)
Compares two X timestamps, taking into account wrapping and 64bit architectures.
KGuiItem remove()
Returns the 'Remove' gui item.
An abstract class for GUI data such as ToolTip and Icon.
static QStringList findActionNameSystemwide(const QKeySequence &seq)
Look up the action in global settings (using its main component's name and text()) and set the shortc...
static QList< KGlobalShortcutInfo > getGlobalShortcutsByKey(const QKeySequence &seq)
Returns a list of global shortcuts registered for the shortcut @seq.
static KGlobalAccel * self()
Returns (and creates if necessary) the singleton instance.
void overrideMainComponentData(const KComponentData &componentData)
Set the KComponentData for which to manipulate shortcuts.
The shortcut will immediately become active but may be reset to "default".
void setAlternate(const QKeySequence &keySeq)
Set the alternate key sequence of this shortcut to the given key sequence.
bool isEnabled() const
No effect.
Actions Friendly Translated Name.
static void activateGlobalShortcutContext(const QString &contextUnique, const QString &contextFriendly, const KComponentData &component=KGlobal::mainComponent())
Set global shortcut context.
QString contextFriendlyName
Class to encapsulate user-driven action or event.
const KComponentData & mainComponent()
void setEnabled(bool enabled)
No effect.
void setPrimary(const QKeySequence &keySeq)
Set the primary key sequence of this shortcut to the given key sequence.
static bool promptStealShortcutSystemwide(QWidget *parent, const QList< KGlobalShortcutInfo > &shortcuts, const QKeySequence &seq)
Show a messagebox to inform the user that a global shorcut is already occupied, and ask to take it aw...
QString programName() const
static void stealShortcutSystemwide(const QKeySequence &seq)
Take away the given shortcut from the named action it belongs to.
static bool isComponentActive(const QString &componentName)
Check if component is active.
static bool isGlobalShortcutAvailable(const QKeySequence &seq, const QString &component=QString())
Check if the shortcut @seq is available for the component.
static int warningContinueCancel(QWidget *parent, const QString &text, const QString &caption=QString(), const KGuiItem &buttonContinue=KStandardGuiItem::cont(), const KGuiItem &buttonCancel=KStandardGuiItem::cancel(), const QString &dontAskAgainName=QString(), Options options=Notify)
Display a "warning" dialog.
Components Friendly Translated Name.
Configurable global shortcut support.
Prevent autoloading of saved global shortcut for action.
QString removeAcceleratorMarker(const QString &label) const
QList< QStringList > allActionsForComponent(const QStringList &actionId)
QList< QStringList > allMainComponents()
Return the unique and common names of all main components that have global shortcuts.