1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 /* integrator.h: integrates D-BUS into Qt event loop
4 * Copyright (C) 2003 Zack Rusin <zack@kde.org>
6 * Licensed under the Academic Free License version 2.0
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.
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.
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
23 #include "integrator.h"
24 #include "connection.h"
27 #include <qsocketnotifier.h>
36 Watch(): readSocket( 0 ), writeSocket( 0 ) { }
39 QSocketNotifier *readSocket;
40 QSocketNotifier *writeSocket;
43 //////////////////////////////////////////////////////////////
44 dbus_bool_t dbusAddWatch( DBusWatch *watch, void *data )
46 Integrator *con = static_cast<Integrator*>( data );
47 con->addWatch( watch );
50 void dbusRemoveWatch( DBusWatch *watch, void *data )
52 Integrator *con = static_cast<Integrator*>( data );
53 con->removeWatch( watch );
56 void dbusToggleWatch( DBusWatch *watch, void *data )
58 Integrator *itg = static_cast<Integrator*>( data );
59 if ( dbus_watch_get_enabled( watch ) )
60 itg->addWatch( watch );
62 itg->removeWatch( watch );
65 dbus_bool_t dbusAddTimeout( DBusTimeout *timeout, void *data )
67 if ( !dbus_timeout_get_enabled(timeout) )
70 Integrator *itg = static_cast<Integrator*>( data );
71 itg->addTimeout( timeout );
75 void dbusRemoveTimeout( DBusTimeout *timeout, void *data )
77 Integrator *itg = static_cast<Integrator*>( data );
78 itg->removeTimeout( timeout );
81 void dbusToggleTimeout( DBusTimeout *timeout, void *data )
83 Integrator *itg = static_cast<Integrator*>( data );
85 if ( dbus_timeout_get_enabled( timeout ) )
86 itg->addTimeout( timeout );
88 itg->removeTimeout( timeout );
91 void dbusWakeupMain( void* )
95 void dbusNewConnection( DBusServer *server,
96 DBusConnection *new_connection,
99 Integrator *itg = static_cast<Integrator*>( data );
100 itg->handleConnection( new_connection );
102 /////////////////////////////////////////////////////////////
104 Timeout::Timeout( QObject *parent, DBusTimeout *t )
105 : QObject( parent ), m_timeout( t )
107 m_timer = new QTimer( this );
108 connect( m_timer, SIGNAL(timeout()),
109 SLOT(slotTimeout()) );
112 void Timeout::slotTimeout()
114 emit timeout( m_timeout );
117 void Timeout::start()
119 m_timer->start( dbus_timeout_get_interval( m_timeout ) );
122 Integrator::Integrator( DBusConnection *conn, QObject *parent )
123 : QObject( parent ), m_connection( conn )
125 m_timeouts.setAutoDelete( true );
127 dbus_connection_set_watch_functions( m_connection,
132 dbus_connection_set_timeout_functions( m_connection,
137 dbus_connection_set_wakeup_main_function( m_connection,
142 Integrator::Integrator( DBusServer *server, QObject *parent )
143 : QObject( parent ), m_server( server )
145 m_connection = reinterpret_cast<DBusConnection*>( m_server );
146 m_timeouts.setAutoDelete( true );
148 dbus_server_set_watch_functions( m_server,
153 dbus_server_set_timeout_functions( m_server,
158 dbus_server_set_new_connection_function( m_server,
163 void Integrator::slotRead( int fd )
165 QIntDictIterator<Watch> it( m_watches );
166 for ( ; it.current(); ++it )
167 dbus_watch_handle ( it.current()->watch, DBUS_WATCH_READABLE );
172 void Integrator::slotWrite( int fd )
174 QIntDictIterator<Watch> it( m_watches );
175 for ( ; it.current(); ++it )
176 dbus_watch_handle ( it.current()->watch, DBUS_WATCH_WRITABLE );
179 void Integrator::slotTimeout( DBusTimeout *timeout )
181 dbus_timeout_handle( timeout );
184 void Integrator::addWatch( DBusWatch *watch )
186 if ( !dbus_watch_get_enabled( watch ) )
189 Watch *qtwatch = new Watch;
190 qtwatch->watch = watch;
192 int flags = dbus_watch_get_flags( watch );
193 int fd = dbus_watch_get_fd( watch );
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)) );
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)) );
205 m_watches.insert( fd, qtwatch );
208 void Integrator::removeWatch( DBusWatch *watch )
210 int key = dbus_watch_get_fd( watch );
212 Watch *qtwatch = m_watches.take( key );
215 delete qtwatch->readSocket; qtwatch->readSocket = 0;
216 delete qtwatch->writeSocket; qtwatch->writeSocket = 0;
221 void Integrator::addTimeout( DBusTimeout *timeout )
223 Timeout *mt = new Timeout( this, timeout );
224 m_timeouts.insert( timeout, mt );
225 connect( mt, SIGNAL(timeout(DBusTimeout*)),
226 SLOT(slotTimeout(DBusTimeout*)) );
230 void Integrator::removeTimeout( DBusTimeout *timeout )
232 m_timeouts.remove( timeout );
235 void Integrator::handleConnection( DBusConnection *c )
237 Connection *con = new Connection( c, this );
238 emit newConnection( con );
241 }//end namespace Internal
242 }//end namespace DBusQt
244 #include "integrator.moc"