Unity 8
dashconnection.cpp
1 /*
2  * Copyright (C) 2014 Canonical, Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it under
5  * the terms of the GNU Lesser General Public License version 3, as published by
6  * the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
10  * SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #include "dashconnection.h"
18 
19 #include <QDBusInterface>
20 #include <QDBusPendingCall>
21 
22 /* The default implementation of AbstractDBusServiceMonitor creates a QDBusInterface when the service
23  * appears on the bus.
24  *
25  * On construction QDBusInterface synchronously introspects the service, which will block the GUI
26  * thread of this process if the service is busy. QDBusAbstractInterface does not perform this
27  * introspection, so let's subclass that and avoid the blocking scenario.
28  *
29  * However we lose Qt's wrapping of the DBus service with a MetaObject, with the result that we
30  * cannot easily connect to DBus signals with the usual connect() calls. So this approach only
31  * suited to push communication with the DBus service.
32  */
33 class AsyncDBusInterface : public QDBusAbstractInterface
34 {
35 public:
36  AsyncDBusInterface(const QString &service, const QString &path,
37  const QString &interface, const QDBusConnection &connection,
38  QObject *parent = 0)
39  : QDBusAbstractInterface(service, path, interface.toLatin1().data(), connection, parent)
40  {}
41  ~AsyncDBusInterface() = default;
42 };
43 
44 
45 DashConnection::DashConnection(const QString &service, const QString &path, const QString &interface, QObject *parent):
46  AbstractDBusServiceMonitor(service, path, interface, SessionBus, parent)
47 {
48 
49 }
50 
51 /* Override the default implementation to create a non-blocking DBus interface (see note above). */
52 QDBusAbstractInterface* DashConnection::createInterface(const QString &service, const QString &path,
53  const QString &interface, const QDBusConnection &connection)
54 {
55  return new AsyncDBusInterface(service, path, interface, connection);
56 }
57 
58 void DashConnection::setCurrentScope(int index, bool animate, bool isSwipe)
59 {
60  if (dbusInterface()) {
61  dbusInterface()->asyncCall(QStringLiteral("SetCurrentScope"), index, animate, isSwipe);
62  }
63 }