* dbus/dbus-sysdeps.c: Make tcp socket connection error somewhat more
[platform/upstream/dbus.git] / qt / src / 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 struct QDBusMetaObject;
64 class QDBusAbstractInterface;
65 class QDBusBusService;
66
67 class QDBusConnectionPrivate: public QObject
68 {
69     Q_OBJECT
70 public:
71     // structs and enums
72     enum ConnectionMode { InvalidMode, ServerMode, ClientMode };
73
74     struct Watcher
75     {
76         Watcher(): watch(0), read(0), write(0) {}
77         DBusWatch *watch;
78         QSocketNotifier *read;
79         QSocketNotifier *write;
80     };
81
82     struct SignalHook
83     {
84         inline SignalHook() : obj(0), midx(-1) { }
85         QString sender, path, signature;
86         QObject* obj;
87         int midx;
88         QList<int> params;
89     };
90
91     struct ObjectTreeNode
92     {
93         struct Data
94         {
95             QString name;
96             ObjectTreeNode *node;
97
98             inline bool operator<(const QString &other) const
99             { return name < other; }
100         };
101
102         inline ObjectTreeNode() : obj(0), flags(0) { }
103         inline ~ObjectTreeNode() { clear(); }
104         inline void clear()
105         {
106             foreach (const Data &entry, children) {
107                 entry.node->clear();
108                 delete entry.node;
109             }
110             children.clear();
111         }
112
113         QObject* obj;
114         int flags;
115         QVector<Data> children;
116     };
117
118 public:
119     // typedefs
120     typedef QMultiHash<int, Watcher> WatcherHash;
121     typedef QHash<int, DBusTimeout *> TimeoutHash;
122     typedef QMultiHash<QString, SignalHook> SignalHookHash;
123     typedef QHash<QString, QDBusMetaObject* > MetaObjectHash;
124     
125 public:
126     // public methods
127     QDBusConnectionPrivate(QObject *parent = 0);
128     ~QDBusConnectionPrivate();
129
130     void bindToApplication();
131
132     void setConnection(DBusConnection *connection);
133     void setServer(DBusServer *server);
134     void closeConnection();
135     void timerEvent(QTimerEvent *e);
136
137     QString getNameOwner(const QString &service);    
138
139     int send(const QDBusMessage &message) const;
140     QDBusMessage sendWithReply(const QDBusMessage &message, int mode);
141     int sendWithReplyAsync(const QDBusMessage &message, QObject *receiver,
142                            const char *method);
143     void connectSignal(const QString &key, const SignalHook &hook);
144     void registerObject(const ObjectTreeNode *node);
145     void connectRelay(const QString &service, const QString &path, const QString &interface,
146                       QDBusAbstractInterface *receiver, const char *signal);
147     void disconnectRelay(const QString &service, const QString &path, const QString &interface,
148                          QDBusAbstractInterface *receiver, const char *signal);
149     
150     bool handleSignal(const QString &key, const QDBusMessage &msg);
151     bool handleSignal(const QDBusMessage &msg);
152     bool handleObjectCall(const QDBusMessage &message);
153     bool handleError();
154
155     bool activateSignal(const SignalHook& hook, const QDBusMessage &msg);
156     bool activateCall(QObject* object, int flags, const QDBusMessage &msg);
157     bool activateObject(const ObjectTreeNode *node, const QDBusMessage &msg);
158     bool activateInternalFilters(const ObjectTreeNode *node, const QDBusMessage &msg);
159
160     void postCallDeliveryEvent(CallDeliveryEvent *data);
161     CallDeliveryEvent *postedCallDeliveryEvent();
162     void deliverCall(const CallDeliveryEvent &data) const;
163
164     QDBusInterfacePrivate *findInterface(const QString &service, const QString &path,
165                                          const QString &interface);
166
167 protected:
168     virtual void customEvent(QEvent *event);
169
170 private:
171     QDBusMetaObject *findMetaObject(const QString &service, const QString &path,
172                                     const QString &interface);        
173
174 public slots:
175     // public slots
176     void doDispatch();
177     void socketRead(int);
178     void socketWrite(int);
179     void objectDestroyed(QObject *o);
180     void relaySignal(QObject *obj, const char *interface, const char *name, const QVariantList &args);
181
182 public:
183     // public member variables
184     QString name;               // this connection's name
185     
186     DBusError error;
187     QDBusError lastError;
188
189     QAtomic ref;
190     QReadWriteLock lock;
191     ConnectionMode mode;
192     DBusConnection *connection;
193     DBusServer *server;
194     QDBusBusService *busService;
195
196     WatcherHash watchers;
197     TimeoutHash timeouts;
198     SignalHookHash signalHooks;
199     QList<DBusTimeout *> pendingTimeouts;
200
201     ObjectTreeNode rootNode;
202     MetaObjectHash cachedMetaObjects;
203
204     QMutex callDeliveryMutex;
205     CallDeliveryEvent *callDeliveryState; // protected by the callDeliveryMutex mutex
206
207 public:
208     // static methods
209     static int messageMetaType;
210     static int registerMessageMetaType();
211     static int findSlot(QObject *obj, const QByteArray &normalizedName, QList<int>& params);
212     static bool prepareHook(QDBusConnectionPrivate::SignalHook &hook, QString &key,
213                             const QString &service, const QString &path,
214                             const QString &interface, const QString &name,
215                             QObject *receiver, const char *signal, int minMIdx,
216                             bool buildSignature);
217     static DBusHandlerResult messageFilter(DBusConnection *, DBusMessage *, void *);
218     static void messageResultReceived(DBusPendingCall *, void *);
219 };
220
221 class QDBusReplyWaiter: public QEventLoop
222 {
223     Q_OBJECT
224 public:
225     QDBusMessage replyMsg;
226
227 public slots:
228     void reply(const QDBusMessage &msg);
229 };
230
231 // in qdbusmisc.cpp
232 extern int qDBusParametersForMethod(const QMetaMethod &mm, QList<int>& metaTypes);
233 extern int qDBusNameToTypeId(const char *name);
234 extern bool qDBusCheckAsyncTag(const char *tag);
235
236 // in qdbusinternalfilters.cpp
237 extern QString qDBusIntrospectObject(const QDBusConnectionPrivate::ObjectTreeNode *node);
238 extern void qDBusIntrospectObject(const QDBusConnectionPrivate::ObjectTreeNode *node,
239                                   const QDBusMessage &msg);
240 extern void qDBusPropertyGet(const QDBusConnectionPrivate::ObjectTreeNode *node,
241                              const QDBusMessage &msg);
242 extern void qDBusPropertySet(const QDBusConnectionPrivate::ObjectTreeNode *node,
243                              const QDBusMessage &msg);
244
245 #endif