async-dbus-proxy.h
00001 /*
00002  * This file is part of signon
00003  *
00004  * Copyright (C) 2009-2010 Nokia Corporation.
00005  * Copyright (C) 2013 Canonical Ltd.
00006  *
00007  * Contact: Aurel Popirtac <ext-aurel.popirtac@nokia.com>
00008  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
00009  *
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public License
00012  * version 2.1 as published by the Free Software Foundation.
00013  *
00014  * This library is distributed in the hope that it will be useful, but
00015  * WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00022  * 02110-1301 USA
00023  */
00024 
00025 #ifndef SIGNON_ASYNC_DBUS_PROXY_H
00026 #define SIGNON_ASYNC_DBUS_PROXY_H
00027 
00028 #include <QDBusError>
00029 #include <QObject>
00030 #include <QQueue>
00031 #include <QVariant>
00032 
00033 class QDBusAbstractInterface;
00034 class QDBusConnection;
00035 class QDBusObjectPath;
00036 class QDBusPendingCallWatcher;
00037 
00038 /*
00039  * @cond IMPL
00040  */
00041 namespace SignOn {
00042 
00043 class AsyncDBusProxy;
00044 class Connection;
00045 class DBusInterface;
00046 
00047 class PendingCall: public QObject
00048 {
00049     Q_OBJECT
00050 
00051 public:
00052     ~PendingCall();
00053 
00054     bool cancel();
00055 
00056 Q_SIGNALS:
00057     void finished(QDBusPendingCallWatcher *watcher);
00058     void success(QDBusPendingCallWatcher *watcher);
00059     void error(const QDBusError &error);
00060     void requeueRequested();
00061 
00062 private Q_SLOTS:
00063     void onFinished(QDBusPendingCallWatcher *watcher);
00064     void onInterfaceDestroyed();
00065     void fail(const QDBusError &error);
00066 
00067 private:
00068     friend class AsyncDBusProxy;
00069     PendingCall(const QString &method,
00070                 const QList<QVariant> &args,
00071                 QObject *parent = 0);
00072     void doCall(QDBusAbstractInterface *interface);
00073 
00074 private:
00075     QString m_method;
00076     QList<QVariant> m_args;
00077     QDBusPendingCallWatcher *m_watcher;
00078     bool m_interfaceWasDestroyed;
00079 };
00080 
00081 class AsyncDBusProxy: public QObject
00082 {
00083     Q_OBJECT
00084 
00085 public:
00086     AsyncDBusProxy(const QString &service,
00087                    const char *interface,
00088                    QObject *clientObject);
00089     ~AsyncDBusProxy();
00090 
00091     void setObjectPath(const QDBusObjectPath &objectPath);
00092     void setError(const QDBusError &error);
00093 
00094     PendingCall *queueCall(const QString &method,
00095                            const QList<QVariant> &args,
00096                            const char *replySlot = 0,
00097                            const char *errorSlot = 0);
00098     PendingCall *queueCall(const QString &method,
00099                            const QList<QVariant> &args,
00100                            QObject *receiver,
00101                            const char *replySlot,
00102                            const char *errorSlot);
00103     bool connect(const char *name, QObject *receiver, const char *slot);
00104 
00105 public Q_SLOTS:
00106     void setConnection(const QDBusConnection &connection);
00107     void setDisconnected();
00108 
00109 Q_SIGNALS:
00110     void connectionNeeded();
00111     void objectPathNeeded();
00112 
00113 private:
00114     enum Status {
00115         Incomplete,
00116         Ready,
00117         Invalid
00118     };
00119     void setStatus(Status status);
00120     void update();
00121     void enqueue(PendingCall *call);
00122 
00123 private Q_SLOTS:
00124     void onCallFinished(QDBusPendingCallWatcher *watcher);
00125     void onRequeueRequested();
00126 
00127 private:
00128     QString m_serviceName;
00129     const char *m_interfaceName;
00130     QString m_path;
00131     QDBusConnection *m_connection;
00132     QObject *m_clientObject;
00133     QQueue<PendingCall *> m_operationsQueue;
00134     QQueue<Connection *> m_connectionsQueue;
00135     DBusInterface *m_interface;
00136     Status m_status;
00137     QDBusError m_lastError;
00138 };
00139 
00140 class SignondAsyncDBusProxy: public AsyncDBusProxy
00141 {
00142     Q_OBJECT
00143 public:
00144     SignondAsyncDBusProxy(const char *interface,
00145                           QObject *clientObject);
00146     ~SignondAsyncDBusProxy();
00147 
00148 private:
00149     void setupConnection();
00150 };
00151 
00152 } //SignOn
00153 
00154 /*
00155  * @endcond IMPL
00156  */
00157 
00158 #endif // SIGNON_ASYNC_DBUS_PROXY_H