summaryrefslogtreecommitdiff
path: root/libdbus-qeventloop/dbusconnectioneventloop.h
blob: 626bffb5c52f65025fa43a61fbb24c07011922a5 (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
/*************************************************************************
This file is part of libresourceqt

Copyright (C) 2010 Nokia Corporation.

This library is free software; you can redistribute
it and/or modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation
version 2.1 of the License.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA.
*************************************************************************/

#ifndef DBUSCONNECTIONEVENTLOOP_H
#define DBUSCONNECTIONEVENTLOOP_H

#include <QObject>
#include <QList>
#include <QMultiHash>
#include <QHash>

#include <dbus/dbus.h>

//#define _DISPLAY_DEBUG_

#ifdef _DISPLAY_DEBUG_
#include <stdlib.h>

#define MYDEBUG()		MyDebug deb(__FUNCTION__)
#define MYDEBUGC(...)	qDebug(__VA_ARGS__)

class MyDebug
{
public:
    MyDebug(const char* func) {
        funcName = strdup(func);
        qDebug("--> Entering: %s() ...", funcName);
    }
    ~MyDebug() {
        qDebug("<-- Leaving: %s() ...", funcName);
        free(funcName);
    }

private:
    char* funcName;
};
#else
#define MYDEBUG()
#define MYDEBUGC(...)
#endif

class QSocketNotifier;
class QTimerEvent;

/**
* This class is handling dbus notifications with QT events. QEventLoop must
*  be handled in order to handle dbus events.
* Usage: DBUSConnectionEventLoop myLoop; myLoop.addConnection(bus);
*/
class DBUSConnectionEventLoop : public QObject
{
    Q_OBJECT
private:
    static DBUSConnectionEventLoop classInstance;

    Q_DISABLE_COPY(DBUSConnectionEventLoop)
    DBUSConnectionEventLoop();

public:
    virtual ~DBUSConnectionEventLoop();

    /**
     * Add new dbus connection into handler.
     * \return true if everything went well.
     */
    static bool addConnection(DBusConnection* conn);
    static void removeConnection(DBusConnection* conn);

    static DBUSConnectionEventLoop &getInstance(void) {
      return classInstance;
    }

private:
    bool internalAddConnection(DBusConnection* conn);
    void internalRemoveConnection(DBusConnection* conn);

    /**
     * Helper class for dbus watcher
     */
    class Watcher
    {
    public:
        Watcher() : watch(0), read(0), write(0) {}

        DBusWatch* 			watch;
        QSocketNotifier*	read;
        QSocketNotifier*	write;
    };

    typedef QMultiHash<int, Watcher> 	Watchers;
    typedef QHash<int, DBusTimeout*> 	Timeouts;
    typedef QList<DBusConnection*>		Connections;

    /**
     * DBusWatcher objects
     */
    Watchers 	watchers;

    /**
     * DBusTimeout objects
     */
    Timeouts 	timeouts;

    /**
     * DBusConnection objects
     */
    Connections	connections;

private slots:
    void readSocket(int fd);
    void writeSocket(int fd);
    void dispatch();

protected:
    void timerEvent(QTimerEvent *e);
    void cleanup();

    static dbus_bool_t addWatch(DBusWatch *watch, void *data);
    static void removeWatch(DBusWatch *watch, void *data);
    static void toggleWatch(DBusWatch *watch, void *data);
    static dbus_bool_t addTimeout(DBusTimeout *timeout, void *data);
    static void removeTimeout(DBusTimeout *timeout, void *data);
    static void toggleTimeout(DBusTimeout *timeout, void *data);
    static void wakeupMain(void *data);
};

#endif // DBUSCONNECTIONEVENTLOOP_H