• Skip to content
  • Skip to link menu
  • KDE API Reference
  • KDE Home
  • Contact Us
 

Nepomuk-Core

  • KTp
service-availability-checker.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2011 Collabora Ltd. <info@collabora.com>
3  @author George Kiagiadakis <george.kiagiadakis@collabora.com>
4 
5  This library is free software; you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation; either version 2.1 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "service-availability-checker.h"
19 
20 #include <QtDBus/QDBusConnectionInterface>
21 #include <QtDBus/QDBusServiceWatcher>
22 #include <QtDBus/QDBusPendingCall>
23 #include <QtDBus/QDBusReply>
24 
25 #include <KDebug>
26 
27 namespace KTp
28 {
29 
30 struct ServiceAvailabilityChecker::Private
31 {
32  QString serviceName;
33  bool serviceAvailable;
34  bool serviceActivatable;
35 };
36 
37 ServiceAvailabilityChecker::ServiceAvailabilityChecker(const QString & serviceName, QObject *parent)
38  : QObject(parent), d(new Private)
39 {
40  d->serviceName = serviceName;
41  d->serviceAvailable = false;
42  d->serviceActivatable = false;
43 
44  QDBusServiceWatcher *serviceWatcher = new QDBusServiceWatcher(serviceName,
45  QDBusConnection::sessionBus(),
46  QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration,
47  this);
48  connect(serviceWatcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)),
49  this, SLOT(onServiceOwnerChanged(QString,QString,QString)));
50 
51  introspect();
52 }
53 
54 ServiceAvailabilityChecker::~ServiceAvailabilityChecker()
55 {
56  delete d;
57 }
58 
59 bool ServiceAvailabilityChecker::isAvailable() const
60 {
61  return d->serviceAvailable || d->serviceActivatable;
62 }
63 
64 void ServiceAvailabilityChecker::introspect()
65 {
66  QDBusConnectionInterface *dbusIface = QDBusConnection::sessionBus().interface();
67 
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"));
73 
74  call = dbusIface->asyncCall(QLatin1String("ListNames"));
75  watcher = new QDBusPendingCallWatcher(call, this);
76  connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
77  this, SLOT(onCallFinished(QDBusPendingCallWatcher*)));
78 }
79 
80 void ServiceAvailabilityChecker::onCallFinished(QDBusPendingCallWatcher *watcher)
81 {
82  QDBusReply<QStringList> reply = *watcher;
83  if (!reply.isValid()) {
84  kDebug() << "Got error while introspecting service availability:" << reply.error();
85  } else {
86  if (watcher->objectName() == QLatin1String("ListActivatableNamesWatcher")) {
87  d->serviceActivatable = reply.value().contains(d->serviceName);
88  } else {
89  if (!d->serviceAvailable) {
90  d->serviceAvailable = reply.value().contains(d->serviceName);
91  }
92  //else onServiceOwnerChanged() has been emitted before the introspection finished
93  //so the reply we got here may be incorrect, claiming that the service is not available
94  }
95  }
96 
97  watcher->deleteLater();
98 }
99 
100 void ServiceAvailabilityChecker::onServiceOwnerChanged(const QString & service,
101  const QString & oldOwner, const QString & newOwner)
102 {
103  Q_UNUSED(oldOwner);
104 
105  if (service == d->serviceName) {
106  d->serviceAvailable = !newOwner.isEmpty();
107  }
108 }
109 
110 }
111 
112 #include "service-availability-checker.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2013 The KDE developers.
Generated on Fri Mar 22 2013 10:58:52 by doxygen 1.8.1.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

ktp-common-internals API Reference

Skip menu "ktp-common-internals API Reference"
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal