20 #include <QtDBus/QDBusConnectionInterface>
21 #include <QtDBus/QDBusServiceWatcher>
22 #include <QtDBus/QDBusPendingCall>
23 #include <QtDBus/QDBusReply>
30 struct ServiceAvailabilityChecker::Private
33 bool serviceAvailable;
34 bool serviceActivatable;
38 :
QObject(parent), d(new Private)
40 d->serviceName = serviceName;
41 d->serviceAvailable =
false;
42 d->serviceActivatable =
false;
44 QDBusServiceWatcher *serviceWatcher =
new QDBusServiceWatcher(serviceName,
45 QDBusConnection::sessionBus(),
46 QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration,
48 connect(serviceWatcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)),
49 this, SLOT(onServiceOwnerChanged(QString,QString,QString)));
61 return d->serviceAvailable || d->serviceActivatable;
64 void ServiceAvailabilityChecker::introspect()
66 QDBusConnectionInterface *dbusIface = QDBusConnection::sessionBus().interface();
68 QDBusPendingCall call = dbusIface->asyncCall(QLatin1String(
"ListActivatableNames"));
69 QDBusPendingCallWatcher *watcher =
new QDBusPendingCallWatcher(call,
this);
70 connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
71 this, SLOT(onCallFinished(QDBusPendingCallWatcher*)));
72 watcher->setObjectName(QLatin1String(
"ListActivatableNamesWatcher"));
74 call = dbusIface->asyncCall(QLatin1String(
"ListNames"));
75 watcher =
new QDBusPendingCallWatcher(call,
this);
76 connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
77 this, SLOT(onCallFinished(QDBusPendingCallWatcher*)));
80 void ServiceAvailabilityChecker::onCallFinished(QDBusPendingCallWatcher *watcher)
82 QDBusReply<QStringList> reply = *watcher;
83 if (!reply.isValid()) {
84 kDebug() <<
"Got error while introspecting service availability:" << reply.error();
86 if (watcher->objectName() == QLatin1String(
"ListActivatableNamesWatcher")) {
87 d->serviceActivatable = reply.value().contains(d->serviceName);
89 if (!d->serviceAvailable) {
90 d->serviceAvailable = reply.value().contains(d->serviceName);
97 watcher->deleteLater();
100 void ServiceAvailabilityChecker::onServiceOwnerChanged(
const QString & service,
101 const QString & oldOwner,
const QString & newOwner)
105 if (service == d->serviceName) {
106 d->serviceAvailable = !newOwner.isEmpty();
112 #include "service-availability-checker.moc"