Main Page | Modules | Namespace List | Class Hierarchy | Data Structures | Directories | File List | Namespace Members | Data Fields | Related Pages

connection.cpp

00001 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002 /* connection.cpp: Qt wrapper for DBusConnection
00003  *
00004  * Copyright (C) 2003  Zack Rusin <zack@kde.org>
00005  *
00006  * Licensed under the Academic Free License version 2.0
00007  *
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 #include "connection.h"
00024 
00025 using namespace DBusQt;
00026 
00027 #include "integrator.h"
00028 using Internal::Integrator;
00029 
00030 struct Connection::Private
00031 {
00032   Private( Connection *qq );
00033   void setConnection( DBusConnection *c );
00034   DBusConnection *connection;
00035   int connectionSlot;
00036   DBusError error;
00037   Integrator *integrator;
00038   int timeout;
00039   Connection *q;
00040 };
00041 
00042 Connection::Private::Private( Connection *qq )
00043   : connection( 0 ), connectionSlot( 0 ), integrator( 0 ),
00044     timeout( -1 ), q( qq )
00045 {
00046   dbus_error_init( &error );
00047 }
00048 
00049 void Connection::Private::setConnection( DBusConnection *c )
00050 {
00051   if (!c) {
00052     qDebug( "error: %s, %s", error.name, error.message );
00053     dbus_error_free( &error );
00054     return;
00055   }
00056   connection = c;
00057   integrator = new Integrator( c, q );
00058   connect( integrator, SIGNAL(readReady()), q, SLOT(dispatchRead()) );
00059 }
00060 
00061 Connection::Connection( QObject *parent )
00062   : QObject( parent )
00063 {
00064   d = new Private( this );
00065 }
00066 
00067 Connection::Connection( const QString& host, QObject *parent )
00068   : QObject( parent )
00069 {
00070   d = new Private( this );
00071 
00072   if ( !host.isEmpty() )
00073     init( host );
00074 }
00075 
00076 Connection::Connection( DBusBusType type, QObject* parent )
00077   : QObject( parent )
00078 {
00079   d = new Private( this );
00080   d->setConnection( dbus_bus_get(type, &d->error) );
00081 }
00082 
00083 void Connection::init( const QString& host )
00084 {
00085   d->setConnection( dbus_connection_open( host.ascii(), &d->error) );
00086   //dbus_connection_allocate_data_slot( &d->connectionSlot );
00087   //dbus_connection_set_data( d->connection, d->connectionSlot, 0, 0 );
00088 }
00089 
00090 bool Connection::isConnected() const
00091 {
00092   return dbus_connection_get_is_connected( d->connection );
00093 }
00094 
00095 bool Connection::isAuthenticated() const
00096 {
00097   return dbus_connection_get_is_authenticated( d->connection );
00098 }
00099 
00100 void Connection::open( const QString& host )
00101 {
00102   if ( host.isEmpty() ) return;
00103 
00104   init( host );
00105 }
00106 
00107 void Connection::close()
00108 {
00109   dbus_connection_disconnect( d->connection );
00110 }
00111 
00112 void Connection::flush()
00113 {
00114   dbus_connection_flush( d->connection );
00115 }
00116 
00117 void Connection::dispatchRead()
00118 {
00119   while ( dbus_connection_dispatch( d->connection ) == DBUS_DISPATCH_DATA_REMAINS )
00120     ;
00121 }
00122 
00123 DBusConnection* Connection::connection() const
00124 {
00125   return d->connection;
00126 }
00127 
00128 Connection::Connection( DBusConnection *connection, QObject *parent  )
00129   : QObject( parent )
00130 {
00131   d = new Private(this);
00132   d->setConnection(connection);
00133 }
00134 
00135 void Connection::send( const Message &m )
00136 {
00137     dbus_connection_send(d->connection, m.message(), 0);
00138 }
00139 
00140 void Connection::sendWithReply( const Message& )
00141 {
00142 }
00143 
00144 Message Connection::sendWithReplyAndBlock( const Message &m )
00145 {
00146   DBusMessage *reply;
00147   reply = dbus_connection_send_with_reply_and_block( d->connection, m.message(), d->timeout, &d->error );
00148   if (dbus_error_is_set(&d->error)) {
00149       qDebug("error: %s, %s", d->error.name, d->error.message);
00150       dbus_error_free(&d->error);
00151   }
00152   return Message( reply );
00153 }
00154 
00155 void* Connection::virtual_hook( int, void*  )
00156 {
00157   return (void *)NULL;
00158 }
00159 
00160 void Connection::dbus_connection_setup_with_qt_main (DBusConnection *connection)
00161 {
00162   d->setConnection( connection );
00163 }
00164 
00165 
00166 
00168 
00169 #include "connection.moc"

Generated on Tue Sep 13 01:28:06 2005 for D-BUS by  doxygen 1.4.4