bring Qt3 library back. Some apps that are not in the KDE trunk are using it.
[platform/upstream/dbus.git] / qt3 / 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   return dbus_connection_get_is_connected( d->connection );
93 }
94
95 bool Connection::isAuthenticated() const
96 {
97   return dbus_connection_get_is_authenticated( d->connection );
98 }
99
100 void Connection::open( const QString& host )
101 {
102   if ( host.isEmpty() ) return;
103
104   init( host );
105 }
106
107 void Connection::close()
108 {
109   dbus_connection_disconnect( d->connection );
110 }
111
112 void Connection::flush()
113 {
114   dbus_connection_flush( d->connection );
115 }
116
117 void Connection::dispatchRead()
118 {
119   while ( dbus_connection_dispatch( d->connection ) == DBUS_DISPATCH_DATA_REMAINS )
120     ;
121 }
122
123 DBusConnection* Connection::connection() const
124 {
125   return d->connection;
126 }
127
128 Connection::Connection( DBusConnection *connection, QObject *parent  )
129   : QObject( parent )
130 {
131   d = new Private(this);
132   d->setConnection(connection);
133 }
134
135 void Connection::send( const Message &m )
136 {
137     dbus_connection_send(d->connection, m.message(), 0);
138 }
139
140 void Connection::sendWithReply( const Message& )
141 {
142 }
143
144 Message Connection::sendWithReplyAndBlock( const Message &m )
145 {
146   DBusMessage *reply;
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);
151   }
152   return Message( reply );
153 }
154
155 void* Connection::virtual_hook( int, void*  )
156 {
157 }
158
159 void Connection::dbus_connection_setup_with_qt_main (DBusConnection *connection)
160 {
161   d->setConnection( connection );
162 }
163
164
165
166 /////////////////////////////////////////////////////////
167
168 #include "connection.moc"