Original DBUS bindings for TQt
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

connection.cpp 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
  2. /* connection.cpp: TQt 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. using namespace DBusQt;
  25. #include "integrator.h"
  26. using Internal::Integrator;
  27. struct Connection::Private
  28. {
  29. Private( Connection *qq );
  30. void setConnection( DBusConnection *c );
  31. DBusConnection *connection;
  32. int connectionSlot;
  33. DBusError error;
  34. Integrator *integrator;
  35. int timeout;
  36. Connection *q;
  37. };
  38. Connection::Private::Private( Connection *qq )
  39. : connection( 0 ), connectionSlot( 0 ), integrator( 0 ),
  40. timeout( -1 ), q( qq )
  41. {
  42. dbus_error_init( &error );
  43. }
  44. void Connection::Private::setConnection( DBusConnection *c )
  45. {
  46. if (!c) {
  47. tqDebug( "error: %s, %s", error.name, error.message );
  48. dbus_error_free( &error );
  49. return;
  50. }
  51. connection = c;
  52. integrator = new Integrator( c, q );
  53. connect( integrator, TQT_SIGNAL(readReady()), q, TQT_SLOT(dispatchRead()) );
  54. }
  55. Connection::Connection( TQObject *parent )
  56. : TQObject( parent )
  57. {
  58. d = new Private( this );
  59. }
  60. Connection::Connection( const TQString& host, TQObject *parent )
  61. : TQObject( parent )
  62. {
  63. d = new Private( this );
  64. if ( !host.isEmpty() )
  65. init( host );
  66. }
  67. Connection::Connection( DBusBusType type, TQObject* parent )
  68. : TQObject( parent )
  69. {
  70. d = new Private( this );
  71. d->setConnection( dbus_bus_get(type, &d->error) );
  72. }
  73. void Connection::init( const TQString& host )
  74. {
  75. d->setConnection( dbus_connection_open( host.ascii(), &d->error) );
  76. //dbus_connection_allocate_data_slot( &d->connectionSlot );
  77. //dbus_connection_set_data( d->connection, d->connectionSlot, 0, 0 );
  78. }
  79. bool Connection::isConnected() const
  80. {
  81. return dbus_connection_get_is_connected( d->connection );
  82. }
  83. bool Connection::isAuthenticated() const
  84. {
  85. return dbus_connection_get_is_authenticated( d->connection );
  86. }
  87. void Connection::open( const TQString& host )
  88. {
  89. if ( host.isEmpty() ) return;
  90. init( host );
  91. }
  92. void Connection::close()
  93. {
  94. dbus_connection_close( d->connection );
  95. }
  96. void Connection::flush()
  97. {
  98. dbus_connection_flush( d->connection );
  99. }
  100. void Connection::dispatchRead()
  101. {
  102. while ( dbus_connection_dispatch( d->connection ) == DBUS_DISPATCH_DATA_REMAINS )
  103. ;
  104. }
  105. DBusConnection* Connection::connection() const
  106. {
  107. return d->connection;
  108. }
  109. Connection::Connection( DBusConnection *connection, TQObject *parent )
  110. : TQObject( parent )
  111. {
  112. d = new Private(this);
  113. d->setConnection(connection);
  114. }
  115. void Connection::send( const Message &m )
  116. {
  117. dbus_connection_send(d->connection, m.message(), 0);
  118. }
  119. void Connection::sendWithReply( const Message& )
  120. {
  121. }
  122. Message Connection::sendWithReplyAndBlock( const Message &m )
  123. {
  124. DBusMessage *reply;
  125. reply = dbus_connection_send_with_reply_and_block( d->connection, m.message(), d->timeout, &d->error );
  126. if (dbus_error_is_set(&d->error)) {
  127. tqDebug("error: %s, %s", d->error.name, d->error.message);
  128. dbus_error_free(&d->error);
  129. }
  130. return Message( reply );
  131. }
  132. void* Connection::virtual_hook( int, void* )
  133. {
  134. return 0;
  135. }
  136. void Connection::dbus_connection_setup_with_qt_main (DBusConnection *connection)
  137. {
  138. d->setConnection( connection );
  139. }
  140. /////////////////////////////////////////////////////////
  141. #include "connection.moc"