a90344989d3f6436d39b90fd8a0bf98771cb5cc6
[platform/upstream/dbus.git] / qt / qdbusconnection_p.h
1 /* qdbusconnection_p.h QDBusConnection private object
2  *
3  * Copyright (C) 2005 Harald Fernengel <harry@kdevelop.org>
4  * Copyright (C) 2006 Trolltech AS. All rights reserved.
5  *    Author: Thiago Macieira <thiago.macieira@trolltech.com>
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software Foundation
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  *
23  */
24
25 //
26 //  W A R N I N G
27 //  -------------
28 //
29 // This file is not part of the public API.  This header file may
30 // change from version to version without notice, or even be
31 // removed.
32 //
33 // We mean it.
34 //
35 //
36
37 #ifndef QDBUSCONNECTION_P_H
38 #define QDBUSCONNECTION_P_H
39
40 #include "qdbuserror.h"
41
42 #include <QtCore/qatomic.h>
43 #include <QtCore/qeventloop.h>
44 #include <QtCore/qhash.h>
45 #include <QtCore/qmutex.h>
46 #include <QtCore/qobject.h>
47 #include <QtCore/qpointer.h>
48 #include <QtCore/qreadwritelock.h>
49 #include <QtCore/qvarlengtharray.h>
50 #include <QtCore/qvector.h>
51
52 #include <dbus/dbus.h>
53
54 #include "qdbusmessage.h"
55
56 class QDBusMessage;
57 class QSocketNotifier;
58 class QTimerEvent;
59 class QDBusObjectPrivate;
60 class CallDeliveryEvent;
61 class QMetaMethod;
62 class QDBusInterfacePrivate;
63 class QDBusMetaObject;
64 class QDBusAbstractInterface;
65 class QDBusBusService;
66
67 typedef struct DBusConnection;
68 typedef struct DBusServer;
69
70 class QDBusConnectionPrivate: public QObject
71 {
72     Q_OBJECT
73 public:
74     // structs and enums
75     enum ConnectionMode { InvalidMode, ServerMode, ClientMode };
76
77     struct Watcher
78     {
79         Watcher(): watch(0), read(0), write(0) {}
80         DBusWatch *watch;
81         QSocketNotifier *read;
82         QSocketNotifier *write;
83     };
84
85     struct SignalHook
86     {
87         inline SignalHook() : obj(0), midx(-1) { }
88         QString sender, path, signature;
89         QObject* obj;
90         int midx;
91         QList<int> params;
92     };
93
94     struct ObjectTreeNode
95     {
96         struct Data
97         {
98             QString name;
99             ObjectTreeNode *node;
100
101             inline bool operator<(const QString &other) const
102             { return name < other; }
103         };
104
105         inline ObjectTreeNode() : obj(0), flags(0) { }
106         inline ~ObjectTreeNode() { clear(); }
107         inline void clear()
108         {
109             foreach (const Data &entry, children) {
110                 entry.node->clear();
111                 delete entry.node;
112             }
113             children.clear();
114         }
115
116         QObject* obj;
117         int flags;
118         QVector<Data> children;
119     };
120
121 public:
122     // typedefs
123     typedef QMultiHash<int, Watcher> WatcherHash;
124     typedef QHash<int, DBusTimeout *> TimeoutHash;
125     typedef QMultiHash<QString, SignalHook> SignalHookHash;
126     typedef QHash<QString, QDBusMetaObject* > MetaObjectHash;
127     
128 public:
129     // public methods
130     QDBusConnectionPrivate(QObject *parent = 0);
131     ~QDBusConnectionPrivate();
132
133     void bindToApplication();
134
135     void setConnection(DBusConnection *connection);
136     void setServer(DBusServer *server);
137     void closeConnection();
138     void timerEvent(QTimerEvent *e);
139
140     QString getNameOwner(const QString &service);    
141
142     int send(const QDBusMessage &message) const;
143     QDBusMessage sendWithReply(const QDBusMessage &message, int mode);
144     int sendWithReplyAsync(const QDBusMessage &message, QObject *receiver,
145                            const char *method);
146     void connectSignal(const QString &key, const SignalHook &hook);
147     void registerObject(const ObjectTreeNode *node);
148     void connectRelay(const QString &service, const QString &path, const QString &interface,
149                       QDBusAbstractInterface *receiver, const char *signal);
150     void disconnectRelay(const QString &service, const QString &path, const QString &interface,
151                          QDBusAbstractInterface *receiver, const char *signal);
152     
153     bool handleSignal(const QString &key, const QDBusMessage &msg);
154     bool handleSignal(const QDBusMessage &msg);
155     bool handleObjectCall(const QDBusMessage &message);
156     bool handleError();
157
158     bool activateSignal(const SignalHook& hook, const QDBusMessage &msg);
159     bool activateCall(QObject* object, int flags, const QDBusMessage &msg);
160     bool activateObject(const ObjectTreeNode *node, const QDBusMessage &msg);
161     bool activateInternalFilters(const ObjectTreeNode *node, const QDBusMessage &msg);
162
163     void postCallDeliveryEvent(CallDeliveryEvent *data);
164     CallDeliveryEvent *postedCallDeliveryEvent();
165     void deliverCall(const CallDeliveryEvent &data) const;
166
167     QDBusInterfacePrivate *findInterface(const QString &service, const QString &path,
168                                          const QString &interface);
169
170 protected:
171     virtual void customEvent(QEvent *event);
172
173 private:
174     QDBusMetaObject *findMetaObject(const QString &service, const QString &path,
175                                     const QString &interface);        
176
177 public slots:
178     // public slots
179     void doDispatch();
180     void socketRead(int);
181     void socketWrite(int);
182     void objectDestroyed(QObject *o);
183     void relaySignal(QObject *obj, const char *interface, const char *name, const QVariantList &args);
184
185 public:
186     // public member variables
187     QString name;               // this connection's name
188     
189     DBusError error;
190     QDBusError lastError;
191
192     QAtomic ref;
193     QReadWriteLock lock;
194     ConnectionMode mode;
195     DBusConnection *connection;
196     DBusServer *server;
197     QDBusBusService *busService;
198
199     WatcherHash watchers;
200     TimeoutHash timeouts;
201     SignalHookHash signalHooks;
202     QList<DBusTimeout *> pendingTimeouts;
203
204     ObjectTreeNode rootNode;
205     MetaObjectHash cachedMetaObjects;
206
207     QMutex callDeliveryMutex;
208     CallDeliveryEvent *callDeliveryState; // protected by the callDeliveryMutex mutex
209
210 public:
211     // static methods
212     static int messageMetaType;
213     static int registerMessageMetaType();
214     static int findSlot(QObject *obj, const QByteArray &normalizedName, QList<int>& params);
215     static bool prepareHook(QDBusConnectionPrivate::SignalHook &hook, QString &key,
216                             const QString &service, const QString &path,
217                             const QString &interface, const QString &name,
218                             QObject *receiver, const char *signal, int minMIdx,
219                             bool buildSignature);
220     static DBusHandlerResult messageFilter(DBusConnection *, DBusMessage *, void *);
221     static void messageResultReceived(DBusPendingCall *, void *);
222 };
223
224 class QDBusReplyWaiter: public QEventLoop
225 {
226     Q_OBJECT
227 public:
228     QDBusMessage replyMsg;
229
230 public slots:
231     void reply(const QDBusMessage &msg);
232 };
233
234 // in qdbusmisc.cpp
235 extern int qDBusParametersForMethod(const QMetaMethod &mm, QList<int>& metaTypes);
236 extern int qDBusNameToTypeId(const char *name);
237 extern bool qDBusCheckAsyncTag(const char *tag);
238
239 // in qdbusinternalfilters.cpp
240 extern QString qDBusIntrospectObject(const QDBusConnectionPrivate::ObjectTreeNode *node);
241 extern void qDBusIntrospectObject(const QDBusConnectionPrivate::ObjectTreeNode *node,
242                                   const QDBusMessage &msg);
243 extern void qDBusPropertyGet(const QDBusConnectionPrivate::ObjectTreeNode *node,
244                              const QDBusMessage &msg);
245 extern void qDBusPropertySet(const QDBusConnectionPrivate::ObjectTreeNode *node,
246                              const QDBusMessage &msg);
247
248 #endif