bring Qt3 library back. Some apps that are not in the KDE trunk are using it.
[platform/upstream/dbus.git] / qt3 / integrator.cpp
1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 /* integrator.h: integrates D-BUS into Qt event loop
3  *
4  * Copyright (C) 2003  Zack Rusin <zack@kde.org>
5  *
6  * Licensed under the Academic Free License version 2.0
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include "integrator.h"
24 #include "connection.h"
25
26 #include <qtimer.h>
27 #include <qsocketnotifier.h>
28 #include <qintdict.h>
29 #include <qptrlist.h>
30
31 namespace DBusQt
32 {
33 namespace Internal {
34
35 struct Watch {
36   Watch(): readSocket( 0 ), writeSocket( 0 ) { }
37
38   DBusWatch *watch;
39   QSocketNotifier *readSocket;
40   QSocketNotifier *writeSocket;
41 };
42
43 //////////////////////////////////////////////////////////////
44 dbus_bool_t dbusAddWatch( DBusWatch *watch, void *data )
45 {
46   Integrator *con = static_cast<Integrator*>( data );
47   con->addWatch( watch );
48   return true;
49 }
50 void dbusRemoveWatch( DBusWatch *watch, void *data )
51 {
52   Integrator *con = static_cast<Integrator*>( data );
53   con->removeWatch( watch );
54 }
55
56 void dbusToggleWatch( DBusWatch *watch, void *data )
57 {
58   Integrator *itg = static_cast<Integrator*>( data );
59   if ( dbus_watch_get_enabled( watch ) )
60     itg->addWatch( watch );
61   else
62     itg->removeWatch( watch );
63 }
64
65 dbus_bool_t dbusAddTimeout( DBusTimeout *timeout, void *data )
66 {
67   if ( !dbus_timeout_get_enabled(timeout) )
68     return true;
69
70   Integrator *itg = static_cast<Integrator*>( data );
71   itg->addTimeout( timeout );
72   return true;
73 }
74
75 void dbusRemoveTimeout( DBusTimeout *timeout, void *data )
76 {
77   Integrator *itg = static_cast<Integrator*>( data );
78   itg->removeTimeout( timeout );
79 }
80
81 void dbusToggleTimeout( DBusTimeout *timeout, void *data )
82 {
83   Integrator *itg = static_cast<Integrator*>( data );
84
85   if ( dbus_timeout_get_enabled( timeout ) )
86     itg->addTimeout( timeout );
87   else
88     itg->removeTimeout( timeout );
89 }
90
91 void dbusWakeupMain( void* )
92 {
93 }
94
95 void dbusNewConnection( DBusServer     *server,
96                         DBusConnection *new_connection,
97                         void           *data )
98 {
99   Integrator *itg = static_cast<Integrator*>( data );
100   itg->handleConnection( new_connection );
101 }
102 /////////////////////////////////////////////////////////////
103
104 Timeout::Timeout( QObject *parent, DBusTimeout *t )
105   : QObject( parent ),  m_timeout( t )
106 {
107   m_timer = new QTimer( this );
108   connect( m_timer,  SIGNAL(timeout()),
109            SLOT(slotTimeout()) );
110 }
111
112 void Timeout::slotTimeout()
113 {
114   emit timeout( m_timeout );
115 }
116
117 void Timeout::start()
118 {
119   m_timer->start( dbus_timeout_get_interval( m_timeout ) );
120 }
121
122 Integrator::Integrator( DBusConnection *conn, QObject *parent )
123   : QObject( parent ), m_connection( conn )
124 {
125   m_timeouts.setAutoDelete( true );
126
127   dbus_connection_set_watch_functions( m_connection,
128                                        dbusAddWatch,
129                                        dbusRemoveWatch,
130                                        dbusToggleWatch,
131                                        this, 0 );
132   dbus_connection_set_timeout_functions( m_connection,
133                                          dbusAddTimeout,
134                                          dbusRemoveTimeout,
135                                          dbusToggleTimeout,
136                                          this, 0 );
137   dbus_connection_set_wakeup_main_function( m_connection,
138                                             dbusWakeupMain,
139                                             this, 0 );
140 }
141
142 Integrator::Integrator( DBusServer *server, QObject *parent )
143   : QObject( parent ), m_server( server )
144 {
145   m_connection = reinterpret_cast<DBusConnection*>( m_server );
146   m_timeouts.setAutoDelete( true );
147
148   dbus_server_set_watch_functions( m_server,
149                                    dbusAddWatch,
150                                    dbusRemoveWatch,
151                                    dbusToggleWatch,
152                                    this, 0 );
153   dbus_server_set_timeout_functions( m_server,
154                                      dbusAddTimeout,
155                                      dbusRemoveTimeout,
156                                      dbusToggleTimeout,
157                                      this, 0 );
158   dbus_server_set_new_connection_function( m_server,
159                                            dbusNewConnection,
160                                            this,  0 );
161 }
162
163 void Integrator::slotRead( int fd )
164 {
165   QIntDictIterator<Watch>       it( m_watches );
166   for ( ; it.current(); ++it )
167     dbus_watch_handle ( it.current()->watch, DBUS_WATCH_READABLE );
168
169   emit readReady();
170 }
171
172 void Integrator::slotWrite( int fd )
173 {
174   QIntDictIterator<Watch>       it( m_watches );
175   for ( ; it.current(); ++it )
176     dbus_watch_handle ( it.current()->watch, DBUS_WATCH_WRITABLE );
177 }
178
179 void Integrator::slotTimeout( DBusTimeout *timeout )
180 {
181   dbus_timeout_handle( timeout );
182 }
183
184 void Integrator::addWatch( DBusWatch *watch )
185 {
186   if ( !dbus_watch_get_enabled( watch ) )
187     return;
188
189   Watch *qtwatch = new Watch;
190   qtwatch->watch = watch;
191
192   int flags = dbus_watch_get_flags( watch );
193   int fd = dbus_watch_get_fd( watch );
194
195   if ( flags & DBUS_WATCH_READABLE ) {
196     qtwatch->readSocket = new QSocketNotifier( fd, QSocketNotifier::Read, this );
197     QObject::connect( qtwatch->readSocket, SIGNAL(activated(int)), SLOT(slotRead(int)) );
198   }
199
200   if (flags & DBUS_WATCH_WRITABLE) {
201     qtwatch->writeSocket = new QSocketNotifier( fd, QSocketNotifier::Write, this );
202     QObject::connect( qtwatch->writeSocket, SIGNAL(activated(int)), SLOT(slotWrite(int)) );
203   }
204
205   m_watches.insert( fd, qtwatch );
206 }
207
208 void Integrator::removeWatch( DBusWatch *watch )
209 {
210   int key = dbus_watch_get_fd( watch );
211
212   Watch *qtwatch = m_watches.take( key );
213
214   if ( qtwatch ) {
215     delete qtwatch->readSocket;  qtwatch->readSocket = 0;
216     delete qtwatch->writeSocket; qtwatch->writeSocket = 0;
217     delete qtwatch;
218   }
219 }
220
221 void Integrator::addTimeout( DBusTimeout *timeout )
222 {
223   Timeout *mt = new Timeout( this, timeout );
224   m_timeouts.insert( timeout, mt );
225   connect( mt, SIGNAL(timeout(DBusTimeout*)),
226            SLOT(slotTimeout(DBusTimeout*)) );
227   mt->start();
228 }
229
230 void Integrator::removeTimeout( DBusTimeout *timeout )
231 {
232   m_timeouts.remove( timeout );
233 }
234
235 void Integrator::handleConnection( DBusConnection *c )
236 {
237   Connection *con = new Connection( c, this );
238   emit newConnection( con );
239 }
240
241 }//end namespace Internal
242 }//end namespace DBusQt
243
244 #include "integrator.moc"