Adding DBusServer wrapper. Switching some thingies, looking pretty and
authorZack Rusin <zack@kde.org>
Tue, 25 Nov 2003 15:30:03 +0000 (15:30 +0000)
committerZack Rusin <zack@kde.org>
Tue, 25 Nov 2003 15:30:03 +0000 (15:30 +0000)
being cool... Anyway, we're done at a very basic level. I have to go back
to something else now, but i'll try to commit an example sometime soon.

ChangeLog
qt/Makefile.am
qt/connection.cpp
qt/connection.h
qt/integrator.cpp
qt/integrator.h
qt/server.cpp [new file with mode: 0644]
qt/server.h [new file with mode: 0644]

index 9947af2..4ce9684 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2003-11-25  Zack Rusin  <zack@kde.org>
+
+       * qt/integrator.h, qt/integrator.cpp: Adding handling of DBusServer,
+
+       * qt/server.h, qt/server.cpp, qt/Makefile.am: Adding DBusServer 
+       wrappers,
+
+       * qt/connection.h, qt/connection.cpp: Adjusting to changes in 
+       the Integrator and to better fit with the server,
+
 2003-11-24  Zack Rusin  <zack@kde.org>
 
        * qt/connection.h, qt/connection.cpp: removing initDbus method since
index 168920b..80d0a9b 100644 (file)
@@ -5,11 +5,12 @@ dbusincludedir=$(includedir)/dbus-1.0/dbus
 lib_LTLIBRARIES=libdbus-qt-1.la
 
 dbusinclude_HEADERS=                           \
-       dbus-qt.h message.h connection.h
+       dbus-qt.h message.h connection.h        \
+       server.h
 
 libdbus_qt_1_la_SOURCES =                      \
        dbus-qthread.cpp message.cpp connection.cpp \
-       integrator.cpp
+       integrator.cpp server.cpp
 
 libdbus_qt_1_la_LIBADD= $(DBUS_QT_LIBS) $(top_builddir)/dbus/libdbus-1.la
 libdbus_qt_1_la_LDFLAGS= -version-info 1:0
index 830987d..72b67ac 100644 (file)
@@ -38,9 +38,6 @@ struct Connection::Private
 Connection::Connection( const QString& host )
 {
   d = new Private;
-  d->integrator = new Integrator( this );
-  connect( d->integrator, SIGNAL(readReady()),
-           SLOT(dispatchRead()) );
 
   if ( !host.isEmpty() )
     init( host );
@@ -50,6 +47,9 @@ void Connection::init( const QString& host )
 {
   dbus_error_init( &d->error );
   d->connection = dbus_connection_open( host.ascii(), &d->error );
+  d->integrator = new Integrator( d->connection, this );
+  connect( d->integrator, SIGNAL(readReady()),
+           SLOT(dispatchRead()) );
   //dbus_connection_allocate_data_slot( &d->connectionSlot );
   //dbus_connection_set_data( d->connection, d->connectionSlot, 0, 0 );
 }
@@ -90,6 +90,16 @@ DBusConnection* Connection::connection() const
   return d->connection;
 }
 
+Connection::Connection( DBusConnection *connection, QObject *parent  )
+  : QObject( parent )
+{
+  d = new Private;
+  d->connection = connection;
+  d->integrator = new Integrator( d->connection, this );
+  connect( d->integrator, SIGNAL(readReady()),
+           SLOT(dispatchRead()) );
+}
+
 /////////////////////////////////////////////////////////
 
 #include "connection.moc"
index c864cd7..d1bdd01 100644 (file)
@@ -65,6 +65,7 @@ namespace DBusQt {
   private:
     friend class Internal::Integrator;
     DBusConnection* connection() const;
+    Connection( DBusConnection *connection, QObject *parent );
   private:
     struct Private;
     Private *d;
index ca793a0..4d42a76 100644 (file)
@@ -92,6 +92,13 @@ void dbusWakeupMain( void* )
 {
 }
 
+void dbusNewConnection( DBusServer     *server,
+                        DBusConnection *new_connection,
+                        void           *data )
+{
+  Integrator *itg = static_cast<Integrator*>( data );
+  itg->handleConnection( new_connection );
+}
 /////////////////////////////////////////////////////////////
 
 Timeout::Timeout( QObject *parent, DBusTimeout *t )
@@ -112,26 +119,47 @@ void Timeout::start()
   m_timer->start( dbus_timeout_get_interval( m_timeout ) );
 }
 
-Integrator::Integrator( Connection *parent )
-  : QObject( parent ), m_parent( parent )
+Integrator::Integrator( DBusConnection *conn, QObject *parent )
+  : QObject( parent ), m_connection( conn )
 {
   m_timeouts.setAutoDelete( true );
 
-  dbus_connection_set_watch_functions( m_parent->connection(),
+  dbus_connection_set_watch_functions( m_connection,
                                        dbusAddWatch,
                                        dbusRemoveWatch,
                                        dbusToggleWatch,
                                        this, 0 );
-  dbus_connection_set_timeout_functions( m_parent->connection(),
+  dbus_connection_set_timeout_functions( m_connection,
                                          dbusAddTimeout,
                                          dbusRemoveTimeout,
                                          dbusToggleTimeout,
                                          this, 0 );
-  dbus_connection_set_wakeup_main_function( m_parent->connection(),
+  dbus_connection_set_wakeup_main_function( m_connection,
                                            dbusWakeupMain,
                                            this, 0 );
 }
 
+Integrator::Integrator( DBusServer *server, QObject *parent )
+  : QObject( parent ), m_server( server )
+{
+  m_connection = reinterpret_cast<DBusConnection*>( m_server );
+  m_timeouts.setAutoDelete( true );
+
+  dbus_server_set_watch_functions( m_server,
+                                   dbusAddWatch,
+                                   dbusRemoveWatch,
+                                   dbusToggleWatch,
+                                   this, 0 );
+  dbus_server_set_timeout_functions( m_server,
+                                     dbusAddTimeout,
+                                     dbusRemoveTimeout,
+                                     dbusToggleTimeout,
+                                     this, 0 );
+  dbus_server_set_new_connection_function( m_server,
+                                           dbusNewConnection,
+                                           this,  0 );
+}
+
 void Integrator::slotRead( int fd )
 {
   Q_UNUSED( fd );
@@ -199,6 +227,12 @@ void Integrator::removeTimeout( DBusTimeout *timeout )
   m_timeouts.remove( timeout );
 }
 
+void Integrator::handleConnection( DBusConnection *c )
+{
+  Connection *con = new Connection( c, this );
+  emit newConnection( con );
+}
+
 }//end namespace Internal
 }//end namespace DBusQt
 
index 71336e9..ef17d3d 100644 (file)
@@ -60,10 +60,12 @@ namespace DBusQt
     {
       Q_OBJECT
     public:
-      Integrator( Connection* parent );
+      Integrator( DBusConnection *connection, QObject *parent );
+      Integrator( DBusServer *server, QObject *parent );
 
     signals:
       void readReady();
+      void newConnection( Connection* );
 
     protected slots:
       void slotRead( int );
@@ -76,10 +78,13 @@ namespace DBusQt
 
       void addTimeout( DBusTimeout* );
       void removeTimeout( DBusTimeout* );
+
+      void handleConnection( DBusConnection* );
     private:
       QIntDict<Watch> m_watches;
       QPtrDict<Timeout> m_timeouts;
-      Connection* m_parent;
+      DBusConnection *m_connection;
+      DBusServer *m_server;
     };
   }
 }
diff --git a/qt/server.cpp b/qt/server.cpp
new file mode 100644 (file)
index 0000000..6db6e1f
--- /dev/null
@@ -0,0 +1,85 @@
+// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
+/* server.h: Qt wrapper for DBusServer
+ *
+ * Copyright (C) 2003  Zack Rusin <zack@kde.org>
+ *
+ * Licensed under the Academic Free License version 1.2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+#include "server.h"
+#include "connection.h"
+
+#include "integrator.h"
+using DBusQt::Internal::Integrator;
+
+namespace DBusQt
+{
+
+struct Server::Private {
+  Private() : integrator( 0 ), server( 0 )
+    {}
+
+  Integrator *integrator;
+  DBusServer *server;
+  DBusError error;
+};
+
+Server::Server( const QString& addr, QObject *parent )
+  : QObject( parent )
+{
+  d = new Private;
+
+  if ( !addr.isEmpty() ) {
+    init( addr );
+  }
+}
+
+Server::~Server()
+{
+  delete d;
+}
+
+bool Server::isConnected() const
+{
+  return dbus_server_get_is_connected( d->server );
+}
+
+QString Server::address() const
+{
+  //FIXME: leak?
+  return dbus_server_get_address( d->server );
+}
+
+void Server::listen( const QString& addr )
+{
+  if ( !d->server ) {
+    init( addr );
+  }
+}
+
+void Server::init( const QString& addr )
+{
+  d->server = dbus_server_listen( addr.ascii(),  &d->error );
+  d->integrator = new Integrator( d->server, this );
+  connect( d->integrator, SIGNAL(newConnection(Connection*)),
+           SIGNAL(newConnection(Connection*)) );
+}
+
+}
+
+
+#include "server.moc"
diff --git a/qt/server.h b/qt/server.h
new file mode 100644 (file)
index 0000000..5806d66
--- /dev/null
@@ -0,0 +1,57 @@
+// -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
+/* server.h: Qt wrapper for DBusServer
+ *
+ * Copyright (C) 2003  Zack Rusin <zack@kde.org>
+ *
+ * Licensed under the Academic Free License version 1.2
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+#ifndef DBUS_QT_SERVER_H
+#define DBUS_QT_SERVER_H
+
+#include <qobject.h>
+
+#include "dbus/dbus.h"
+
+namespace DBusQt
+{
+  class Connection;
+  class Server : public QObject
+  {
+    Q_OBJECT
+  public:
+    Server( const QString& addr = QString::null, QObject *parent=0 );
+    ~Server();
+
+    bool isConnected() const;
+    QString address() const;
+
+  public slots:
+    void listen( const QString& addr );
+    void disconnect();
+  signals:
+    void newConnection( Connection* );
+
+  private:
+    void init( const QString& addr );
+  private:
+    struct Private;
+    Private *d;
+  };
+}
+
+#endif