dbus/qt server.cpp,NONE,1.1 server.h,NONE,1.1 Makefile.am,1.4,1.5 connection.cpp,1.3,1.4 connection.h,1.4,1.5 integrator.cpp,1.2,1.3 integrator.h,1.2,1.3

Zack Rusin zack@pdx.freedesktop.org
Tue, 25 Nov 2003 07:30:05 -0800


Update of /cvs/dbus/dbus/qt
In directory pdx:/tmp/cvs-serv23408/qt

Modified Files:
	Makefile.am connection.cpp connection.h integrator.cpp 
	integrator.h 
Added Files:
	server.cpp server.h 
Log Message:
Adding DBusServer wrapper. Switching some thingies, looking pretty and
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.


--- NEW FILE: server.cpp ---
// -*- 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"

--- NEW FILE: server.h ---
// -*- 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

Index: Makefile.am
===================================================================
RCS file: /cvs/dbus/dbus/qt/Makefile.am,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- Makefile.am	24 Nov 2003 05:21:12 -0000	1.4
+++ Makefile.am	25 Nov 2003 15:30:03 -0000	1.5
@@ -5,11 +5,12 @@
 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: connection.cpp
===================================================================
RCS file: /cvs/dbus/dbus/qt/connection.cpp,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- connection.cpp	24 Nov 2003 19:11:55 -0000	1.3
+++ connection.cpp	25 Nov 2003 15:30:03 -0000	1.4
@@ -38,9 +38,6 @@
 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 @@
 {
   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 @@
   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: connection.h
===================================================================
RCS file: /cvs/dbus/dbus/qt/connection.h,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- connection.h	24 Nov 2003 19:11:55 -0000	1.4
+++ connection.h	25 Nov 2003 15:30:03 -0000	1.5
@@ -65,6 +65,7 @@
   private:
     friend class Internal::Integrator;
     DBusConnection* connection() const;
+    Connection( DBusConnection *connection, QObject *parent );
   private:
     struct Private;
     Private *d;

Index: integrator.cpp
===================================================================
RCS file: /cvs/dbus/dbus/qt/integrator.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- integrator.cpp	24 Nov 2003 19:11:55 -0000	1.2
+++ integrator.cpp	25 Nov 2003 15:30:03 -0000	1.3
@@ -92,6 +92,13 @@
 {
 }
 
+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 @@
   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 @@
   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: integrator.h
===================================================================
RCS file: /cvs/dbus/dbus/qt/integrator.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- integrator.h	24 Nov 2003 19:11:55 -0000	1.2
+++ integrator.h	25 Nov 2003 15:30:03 -0000	1.3
@@ -60,10 +60,12 @@
     {
       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 @@
 
       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;
     };
   }
 }