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)),
95 components[componentUnique] = component;
103 KGlobalAccelPrivate::KGlobalAccelPrivate(
KGlobalAccel *q)
104 : isUsingForeignComponentName(false),
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,
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> >();
156 if (KGlobal::hasMainComponent()) {
157 d->readComponentData( KGlobal::mainComponent() );
163 KGlobalAccel::~KGlobalAccel()
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) {
251 action->d->componentData = mainComponent;
256 actions.insert(action);
257 iface.doRegister(actionId);
263 if (!action || action->objectName().isEmpty()) {
267 const bool isRegistered = actions.contains(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()) {
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));
333 if (isConfigurationAction && (flags & KAction::NoAutoloading)) {
344 iface.setForeignShortcut(actionId, result);
346 if (scResult != activeShortcut) {
349 action->d->setActiveGlobalShortcutNoEnable(scResult);
354 iface.setShortcut(actionId, intListFromShortcut(defaultShortcut),
355 setterFlags | IsDefault);
364 Q_ASSERT(!action->objectName().isEmpty());
365 ret.append(action->objectName());
366 ret.append(componentFriendlyForAction(action));
367 const QString actionText = KGlobal::locale()->removeAcceleratorMarker(action->text());
368 ret.append(actionText);
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,
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,
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");
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"