summaryrefslogtreecommitdiffstats
path: root/qt/qdbusconnection_p.h
blob: cd2e48e205ab871c1f8cb2e6e54e69b280d1283c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/* qdbusconnection_p.h QDBusConnection private object
 *
 * Copyright (C) 2005 Harald Fernengel <harry@kdevelop.org>
 * Copyright (C) 2006 Trolltech AS. All rights reserved.
 *    Author: Thiago Macieira <thiago.macieira@trolltech.com>
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

//
//  W A R N I N G
//  -------------
//
// This file is not part of the public API.  This header file may
// change from version to version without notice, or even be
// removed.
//
// We mean it.
//
//

#ifndef QDBUSCONNECTION_P_H
#define QDBUSCONNECTION_P_H

#include "qdbuserror.h"

#include <QtCore/qatomic.h>
#include <QtCore/qmutex.h>
#include <QtCore/qhash.h>
#include <QtCore/qobject.h>
#include <QtCore/qpointer.h>
#include <QtCore/qvarlengtharray.h>
#include <QtCore/qeventloop.h>
#include <QtCore/qmutex.h>

#include <dbus/dbus.h>

#include "qdbusmessage.h"
#include "qdbusintrospection.h"

class QDBusMessage;
class QSocketNotifier;
class QTimerEvent;
class QDBusObjectPrivate;
class CallDeliveryEvent;

typedef struct DBusConnection;
typedef struct DBusServer;

class QDBusConnectionPrivate: public QObject
{
    Q_OBJECT
public:
    // structs and enums
    enum ConnectionMode { InvalidMode, ServerMode, ClientMode };

    struct Watcher
    {
        Watcher(): watch(0), read(0), write(0) {}
        DBusWatch *watch;
        QSocketNotifier *read;
        QSocketNotifier *write;
    };

    struct SignalHook
    {
        QString interface, name, signature;
        QPointer<QObject> obj;
        int midx;
        QList<int> params;
    };

    struct ObjectData
    {
        QPointer<QObject> obj;
        int flags;
    };

public:
    // typedefs
    typedef QMultiHash<int, Watcher> WatcherHash;
    typedef QHash<int, DBusTimeout *> TimeoutHash;
    typedef QMultiHash<QString, SignalHook> SignalHookHash;
    typedef QHash<QString, ObjectData> ObjectDataHash;
    typedef QHash<QString, ObjectDataHash> ObjectHookHash;
    typedef QHash<QString, QSharedDataPointer<QDBusIntrospection::Interface> > KnownInterfacesHash;
    typedef QHash<QString, QDBusIntrospection::Object* > KnownObjectsHash;

public:
    // public methods
    QDBusConnectionPrivate(QObject *parent = 0);
    ~QDBusConnectionPrivate();

    void bindToApplication();

    void setConnection(DBusConnection *connection);
    void setServer(DBusServer *server);
    void closeConnection();
    void timerEvent(QTimerEvent *e);

    bool handleSignal(const QString &path, const QDBusMessage &msg);
    bool send(const QDBusMessage &message) const;
    int sendWithReplyAsync(const QDBusMessage &message, QObject *receiver,
                           const char *method) const;
    
    bool handleSignal(const QDBusMessage &msg);
    bool handleObjectCall(const QDBusMessage &message);
    bool handleError();

    void disposeOfLocked(QDBusIntrospection::Object* obj);
    void disposeOf(QDBusObjectPrivate* obj);
    QSharedDataPointer<QDBusIntrospection::Interface> findInterface(const QString& name);
    QDBusIntrospection::Object* findObject(const QString& service,
                                           const QString& path);
                                                                                  
    bool activateReply(QObject *object, int idx, const QList<int>& metaTypes,
                       const QDBusMessage &msg);
    bool activateSignal(const SignalHook& hook, const QDBusMessage &msg);
    bool activateCall(QObject* object, int flags, const QDBusMessage &msg);
    bool activateAdaptor(QObject *object, int flags, const QDBusMessage &msg);
    bool activateObject(const ObjectData& data, const QDBusMessage &msg);
    void deliverCall(const CallDeliveryEvent &data) const;

protected:
    virtual void customEvent(QEvent *event);

public slots:
    // public slots
    void socketRead(int);
    void socketWrite(int);
    void objectDestroyed(QObject *o);

public:
    // public member variables
    DBusError error;
    QDBusError lastError;

    QAtomic ref;
    QMutex mutex;
    ConnectionMode mode;
    DBusConnection *connection;
    DBusServer *server;

    WatcherHash watchers;
    TimeoutHash timeouts;
    SignalHookHash signalHooks;
    ObjectHookHash objectHooks;
    QList<DBusTimeout *> pendingTimeouts;

public:
    // public mutable member variables
    mutable KnownInterfacesHash knownInterfaces;
    mutable KnownObjectsHash knownObjects;

public:
    // static methods
    static int messageMetaType;
    static int registerMessageMetaType();
    static int findSlot(QObject *obj, const char *slotName, QList<int>& params);
};

class QDBusReplyWaiter: public QEventLoop
{
    Q_OBJECT
public:
    QDBusMessage replyMsg;

public slots:
    void reply(const QDBusMessage &msg);
};    

#endif