00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "integrator.h"
00024 #include "connection.h"
00025
00026 #include <qtimer.h>
00027 #include <qsocketnotifier.h>
00028 #include <qintdict.h>
00029 #include <qptrlist.h>
00030
00031 namespace DBusQt
00032 {
00033 namespace Internal {
00034
00035 struct Watch {
00036 Watch(): readSocket( 0 ), writeSocket( 0 ) { }
00037
00038 DBusWatch *watch;
00039 QSocketNotifier *readSocket;
00040 QSocketNotifier *writeSocket;
00041 };
00042
00044 dbus_bool_t dbusAddWatch( DBusWatch *watch, void *data )
00045 {
00046 Integrator *con = static_cast<Integrator*>( data );
00047 con->addWatch( watch );
00048 return true;
00049 }
00050 void dbusRemoveWatch( DBusWatch *watch, void *data )
00051 {
00052 Integrator *con = static_cast<Integrator*>( data );
00053 con->removeWatch( watch );
00054 }
00055
00056 void dbusToggleWatch( DBusWatch *watch, void *data )
00057 {
00058 Integrator *itg = static_cast<Integrator*>( data );
00059 if ( dbus_watch_get_enabled( watch ) )
00060 itg->addWatch( watch );
00061 else
00062 itg->removeWatch( watch );
00063 }
00064
00065 dbus_bool_t dbusAddTimeout( DBusTimeout *timeout, void *data )
00066 {
00067 if ( !dbus_timeout_get_enabled(timeout) )
00068 return true;
00069
00070 Integrator *itg = static_cast<Integrator*>( data );
00071 itg->addTimeout( timeout );
00072 return true;
00073 }
00074
00075 void dbusRemoveTimeout( DBusTimeout *timeout, void *data )
00076 {
00077 Integrator *itg = static_cast<Integrator*>( data );
00078 itg->removeTimeout( timeout );
00079 }
00080
00081 void dbusToggleTimeout( DBusTimeout *timeout, void *data )
00082 {
00083 Integrator *itg = static_cast<Integrator*>( data );
00084
00085 if ( dbus_timeout_get_enabled( timeout ) )
00086 itg->addTimeout( timeout );
00087 else
00088 itg->removeTimeout( timeout );
00089 }
00090
00091 void dbusWakeupMain( void* )
00092 {
00093 }
00094
00095 void dbusNewConnection( DBusServer *server,
00096 DBusConnection *new_connection,
00097 void *data )
00098 {
00099 Integrator *itg = static_cast<Integrator*>( data );
00100 itg->handleConnection( new_connection );
00101 }
00103
00104 Timeout::Timeout( QObject *parent, DBusTimeout *t )
00105 : QObject( parent ), m_timeout( t )
00106 {
00107 m_timer = new QTimer( this );
00108 connect( m_timer, SIGNAL(timeout()),
00109 SLOT(slotTimeout()) );
00110 }
00111
00112 void Timeout::slotTimeout()
00113 {
00114 emit timeout( m_timeout );
00115 }
00116
00117 void Timeout::start()
00118 {
00119 m_timer->start( dbus_timeout_get_interval( m_timeout ) );
00120 }
00121
00122 Integrator::Integrator( DBusConnection *conn, QObject *parent )
00123 : QObject( parent ), m_connection( conn )
00124 {
00125 m_timeouts.setAutoDelete( true );
00126
00127 dbus_connection_set_watch_functions( m_connection,
00128 dbusAddWatch,
00129 dbusRemoveWatch,
00130 dbusToggleWatch,
00131 this, 0 );
00132 dbus_connection_set_timeout_functions( m_connection,
00133 dbusAddTimeout,
00134 dbusRemoveTimeout,
00135 dbusToggleTimeout,
00136 this, 0 );
00137 dbus_connection_set_wakeup_main_function( m_connection,
00138 dbusWakeupMain,
00139 this, 0 );
00140 }
00141
00142 Integrator::Integrator( DBusServer *server, QObject *parent )
00143 : QObject( parent ), m_server( server )
00144 {
00145 m_connection = reinterpret_cast<DBusConnection*>( m_server );
00146 m_timeouts.setAutoDelete( true );
00147
00148 dbus_server_set_watch_functions( m_server,
00149 dbusAddWatch,
00150 dbusRemoveWatch,
00151 dbusToggleWatch,
00152 this, 0 );
00153 dbus_server_set_timeout_functions( m_server,
00154 dbusAddTimeout,
00155 dbusRemoveTimeout,
00156 dbusToggleTimeout,
00157 this, 0 );
00158 dbus_server_set_new_connection_function( m_server,
00159 dbusNewConnection,
00160 this, 0 );
00161 }
00162
00163 void Integrator::slotRead( int fd )
00164 {
00165 QIntDictIterator<Watch> it( m_watches );
00166 for ( ; it.current(); ++it )
00167 dbus_watch_handle ( it.current()->watch, DBUS_WATCH_READABLE );
00168
00169 emit readReady();
00170 }
00171
00172 void Integrator::slotWrite( int fd )
00173 {
00174 QIntDictIterator<Watch> it( m_watches );
00175 for ( ; it.current(); ++it )
00176 dbus_watch_handle ( it.current()->watch, DBUS_WATCH_WRITABLE );
00177 }
00178
00179 void Integrator::slotTimeout( DBusTimeout *timeout )
00180 {
00181 dbus_timeout_handle( timeout );
00182 }
00183
00184 void Integrator::addWatch( DBusWatch *watch )
00185 {
00186 if ( !dbus_watch_get_enabled( watch ) )
00187 return;
00188
00189 Watch *qtwatch = new Watch;
00190 qtwatch->watch = watch;
00191
00192 int flags = dbus_watch_get_flags( watch );
00193 int fd = dbus_watch_get_fd( watch );
00194
00195 if ( flags & DBUS_WATCH_READABLE ) {
00196 qtwatch->readSocket = new QSocketNotifier( fd, QSocketNotifier::Read, this );
00197 QObject::connect( qtwatch->readSocket, SIGNAL(activated(int)), SLOT(slotRead(int)) );
00198 }
00199
00200 if (flags & DBUS_WATCH_WRITABLE) {
00201 qtwatch->writeSocket = new QSocketNotifier( fd, QSocketNotifier::Write, this );
00202 QObject::connect( qtwatch->writeSocket, SIGNAL(activated(int)), SLOT(slotWrite(int)) );
00203 }
00204
00205 m_watches.insert( fd, qtwatch );
00206 }
00207
00208 void Integrator::removeWatch( DBusWatch *watch )
00209 {
00210 int key = dbus_watch_get_fd( watch );
00211
00212 Watch *qtwatch = m_watches.take( key );
00213
00214 if ( qtwatch ) {
00215 delete qtwatch->readSocket; qtwatch->readSocket = 0;
00216 delete qtwatch->writeSocket; qtwatch->writeSocket = 0;
00217 delete qtwatch;
00218 }
00219 }
00220
00221 void Integrator::addTimeout( DBusTimeout *timeout )
00222 {
00223 Timeout *mt = new Timeout( this, timeout );
00224 m_timeouts.insert( timeout, mt );
00225 connect( mt, SIGNAL(timeout(DBusTimeout*)),
00226 SLOT(slotTimeout(DBusTimeout*)) );
00227 mt->start();
00228 }
00229
00230 void Integrator::removeTimeout( DBusTimeout *timeout )
00231 {
00232 m_timeouts.remove( timeout );
00233 }
00234
00235 void Integrator::handleConnection( DBusConnection *c )
00236 {
00237 Connection *con = new Connection( c, this );
00238 emit newConnection( con );
00239 }
00240
00241 }
00242 }
00243
00244 #include "integrator.moc"