1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 /* connection.cpp: Qt wrapper for DBusConnection
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 "connection.h"
25 using namespace DBusQt;
27 #include "integrator.h"
28 using Internal::Integrator;
30 struct Connection::Private
32 Private( Connection *qq );
33 void setConnection( DBusConnection *c );
34 DBusConnection *connection;
37 Integrator *integrator;
42 Connection::Private::Private( Connection *qq )
43 : connection( 0 ), connectionSlot( 0 ), integrator( 0 ),
44 timeout( -1 ), q( qq )
46 dbus_error_init( &error );
49 void Connection::Private::setConnection( DBusConnection *c )
52 qDebug( "error: %s, %s", error.name, error.message );
53 dbus_error_free( &error );
57 integrator = new Integrator( c, q );
58 connect( integrator, SIGNAL(readReady()), q, SLOT(dispatchRead()) );
61 Connection::Connection( QObject *parent )
64 d = new Private( this );
67 Connection::Connection( const QString& host, QObject *parent )
70 d = new Private( this );
72 if ( !host.isEmpty() )
76 Connection::Connection( DBusBusType type, QObject* parent )
79 d = new Private( this );
80 d->setConnection( dbus_bus_get(type, &d->error) );
83 void Connection::init( const QString& host )
85 d->setConnection( dbus_connection_open( host.ascii(), &d->error) );
86 //dbus_connection_allocate_data_slot( &d->connectionSlot );
87 //dbus_connection_set_data( d->connection, d->connectionSlot, 0, 0 );
90 bool Connection::isConnected() const
92 return dbus_connection_get_is_connected( d->connection );
95 bool Connection::isAuthenticated() const
97 return dbus_connection_get_is_authenticated( d->connection );
100 void Connection::open( const QString& host )
102 if ( host.isEmpty() ) return;
107 void Connection::close()
109 dbus_connection_disconnect( d->connection );
112 void Connection::flush()
114 dbus_connection_flush( d->connection );
117 void Connection::dispatchRead()
119 while ( dbus_connection_dispatch( d->connection ) == DBUS_DISPATCH_DATA_REMAINS )
123 DBusConnection* Connection::connection() const
125 return d->connection;
128 Connection::Connection( DBusConnection *connection, QObject *parent )
131 d = new Private(this);
132 d->setConnection(connection);
135 void Connection::send( const Message &m )
137 dbus_connection_send(d->connection, m.message(), 0);
140 void Connection::sendWithReply( const Message& )
144 Message Connection::sendWithReplyAndBlock( const Message &m )
147 reply = dbus_connection_send_with_reply_and_block( d->connection, m.message(), d->timeout, &d->error );
148 if (dbus_error_is_set(&d->error)) {
149 qDebug("error: %s, %s", d->error.name, d->error.message);
150 dbus_error_free(&d->error);
152 return Message( reply );
155 void* Connection::virtual_hook( int, void* )
159 void Connection::dbus_connection_setup_with_qt_main (DBusConnection *connection)
161 d->setConnection( connection );
166 /////////////////////////////////////////////////////////
168 #include "connection.moc"