async-dbus-proxy.h
1 /*
2  * This file is part of signon
3  *
4  * Copyright (C) 2009-2010 Nokia Corporation.
5  * Copyright (C) 2013 Canonical Ltd.
6  *
7  * Contact: Aurel Popirtac <ext-aurel.popirtac@nokia.com>
8  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * version 2.1 as published by the Free Software Foundation.
13  *
14  * This library is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22  * 02110-1301 USA
23  */
24 
25 #ifndef SIGNON_ASYNC_DBUS_PROXY_H
26 #define SIGNON_ASYNC_DBUS_PROXY_H
27 
28 #include <QDBusError>
29 #include <QObject>
30 #include <QQueue>
31 #include <QVariant>
32 
33 class QDBusAbstractInterface;
34 class QDBusConnection;
35 class QDBusObjectPath;
36 class QDBusPendingCallWatcher;
37 
38 /*
39  * @cond IMPL
40  */
41 namespace SignOn {
42 
43 class AsyncDBusProxy;
44 class Connection;
45 class DBusInterface;
46 
47 class PendingCall: public QObject
48 {
49  Q_OBJECT
50 
51 public:
52  ~PendingCall();
53 
54  bool cancel();
55 
56 Q_SIGNALS:
57  void finished(QDBusPendingCallWatcher *watcher);
58  void success(QDBusPendingCallWatcher *watcher);
59  void error(const QDBusError &error);
60  void requeueRequested();
61 
62 private Q_SLOTS:
63  void onFinished(QDBusPendingCallWatcher *watcher);
64  void onInterfaceDestroyed();
65  void fail(const QDBusError &error);
66 
67 private:
68  friend class AsyncDBusProxy;
69  PendingCall(const QString &method,
70  const QList<QVariant> &args,
71  QObject *parent = 0);
72  void doCall(QDBusAbstractInterface *interface);
73 
74 private:
75  QString m_method;
76  QList<QVariant> m_args;
77  QDBusPendingCallWatcher *m_watcher;
78  bool m_interfaceWasDestroyed;
79 };
80 
81 class AsyncDBusProxy: public QObject
82 {
83  Q_OBJECT
84 
85 public:
86  AsyncDBusProxy(const QString &service,
87  const char *interface,
88  QObject *clientObject);
89  ~AsyncDBusProxy();
90 
91  void setObjectPath(const QDBusObjectPath &objectPath);
92  void setError(const QDBusError &error);
93 
94  PendingCall *queueCall(const QString &method,
95  const QList<QVariant> &args,
96  const char *replySlot = 0,
97  const char *errorSlot = 0);
98  PendingCall *queueCall(const QString &method,
99  const QList<QVariant> &args,
100  QObject *receiver,
101  const char *replySlot,
102  const char *errorSlot);
103  bool connect(const char *name, QObject *receiver, const char *slot);
104 
105 public Q_SLOTS:
106  void setConnection(const QDBusConnection &connection);
107  void setDisconnected();
108 
109 Q_SIGNALS:
110  void connectionNeeded();
111  void objectPathNeeded();
112 
113 private:
114  enum Status {
115  Incomplete,
116  Ready,
117  Invalid
118  };
119  void setStatus(Status status);
120  void update();
121  void enqueue(PendingCall *call);
122 
123 private Q_SLOTS:
124  void onCallFinished(QDBusPendingCallWatcher *watcher);
125  void onRequeueRequested();
126 
127 private:
128  QString m_serviceName;
129  const char *m_interfaceName;
130  QString m_path;
131  QDBusConnection *m_connection;
132  QObject *m_clientObject;
133  QQueue<PendingCall *> m_operationsQueue;
134  QQueue<Connection *> m_connectionsQueue;
135  DBusInterface *m_interface;
136  Status m_status;
137  QDBusError m_lastError;
138 };
139 
140 class SignondAsyncDBusProxy: public AsyncDBusProxy
141 {
142  Q_OBJECT
143 public:
144  SignondAsyncDBusProxy(const char *interface,
145  QObject *clientObject);
146  ~SignondAsyncDBusProxy();
147 
148 private:
149  void setupConnection();
150 };
151 
152 } //SignOn
153 
154 /*
155  * @endcond IMPL
156  */
157 
158 #endif // SIGNON_ASYNC_DBUS_PROXY_H