Committing Haralds stuff together with some of my backlog for connection.
[platform/upstream/dbus.git] / qt / connection.cpp
1 // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 /* connection.cpp: Qt wrapper for DBusConnection
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 "connection.h"
24
25 using namespace DBusQt;
26
27 #include "integrator.h"
28 using Internal::Integrator;
29
30 struct Connection::Private
31 {
32   Private( Connection *qq );
33   void setConnection( DBusConnection *c );
34   DBusConnection *connection;
35   int connectionSlot;
36   DBusError error;
37   Integrator *integrator;
38   int timeout;
39   Connection *q;
40 };
41
42 Connection::Private::Private( Connection *qq )
43   : connection( 0 ), connectionSlot( 0 ), integrator( 0 ),
44     timeout( -1 ), q( qq )
45 {
46   dbus_error_init( &error );
47 }
48
49 void Connection::Private::setConnection( DBusConnection *c )
50 {
51   if (!c) {
52     qDebug( "error: %s, %s", error.name, error.message );
53     dbus_error_free( &error );
54     return;
55   }
56   connection = c;
57   integrator = new Integrator( c, q );
58   connect( integrator, SIGNAL(readReady()), q, SLOT(dispatchRead()) );
59 }
60
61 Connection::Connection( QObject *parent )
62   : QObject( parent )
63 {
64   d = new Private( this );
65 }
66
67 Connection::Connection( const QString& host, QObject *parent )
68   : QObject( parent )
69 {
70   d = new Private( this );
71
72   if ( !host.isEmpty() )
73     init( host );
74 }
75
76 Connection::Connection(DBusBusType type, QObject* parent)
77   : QObject( parent )
78 {
79   d = new Private(this);
80   d->setConnection( dbus_bus_get(type, &d->error) );
81 }
82
83 void Connection::init( const QString& host )
84 {
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 );
88 }
89
90 bool Connection::isConnected() const
91 {
92 }
93
94 bool Connection::isAuthenticated() const
95 {
96 }
97
98 void Connection::open( const QString& host )
99 {
100   if ( host.isEmpty() ) return;
101
102   init( host );
103 }
104
105 void Connection::close()
106 {
107   dbus_connection_disconnect( d->connection );
108 }
109
110 void Connection::flush()
111 {
112   dbus_connection_flush( d->connection );
113 }
114
115 void Connection::dispatchRead()
116 {
117   while ( dbus_connection_dispatch( d->connection ) == DBUS_DISPATCH_DATA_REMAINS )
118     ;
119 }
120
121 DBusConnection* Connection::connection() const
122 {
123   return d->connection;
124 }
125
126 Connection::Connection( DBusConnection *connection, QObject *parent  )
127   : QObject( parent )
128 {
129   d = new Private(this);
130   d->setConnection(connection);
131 }
132
133 void Connection::send( const Message &m )
134 {
135     dbus_connection_send(d->connection, m.message(), 0);
136 }
137
138 void Connection::sendWithReply( const Message& )
139 {
140 }
141
142 Message Connection::sendWithReplyAndBlock( const Message &m )
143 {
144   DBusMessage *reply;
145   reply = dbus_connection_send_with_reply_and_block( d->connection, m.message(), d->timeout, &d->error );
146   if (dbus_error_is_set(&d->error)) {
147       qDebug("error: %s, %s", d->error.name, d->error.message);
148       dbus_error_free(&d->error);
149   }
150   return Message( reply );
151 }
152
153 void* Connection::virtual_hook( int, void*  )
154 {
155 }
156
157 /////////////////////////////////////////////////////////
158
159 #include "connection.moc"