[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    =?utf-8?q?=5Blibdebconf-kde=5D_src=3A_Refactor_DebconfFrontend_i?=
From:       Modestas Vainius <modax () debian ! org>
Date:       2011-06-29 16:29:28
Message-ID: 20110629162928.E1385A60C4 () git ! kde ! org
[Download RAW message or body]

Git commit 0984ffcc090022d56e7a4b5aa9d045a39ef8d176 by Modestas Vainius.
Committed on 26/06/2011 at 23:00.
Pushed by modax into branch 'master'.

Refactor DebconfFrontend into abstract class without I/O specifics.

UNIX socket specific stuff is moved to the new derivative class
DebconfFrontendSocket. These changes open possibilities to implement multiple
I/O backends for DebconfFrontend.

M  +1    -1    src/DebconfGui.cpp     
M  +50   -29   src/debconf.cpp     
M  +72   -16   src/debconf.h     

http://commits.kde.org/libdebconf-kde/0984ffcc090022d56e7a4b5aa9d045a39ef8d176

diff --git a/src/DebconfGui.cpp b/src/DebconfGui.cpp
index cba37e3..a84a2c6 100644
--- a/src/DebconfGui.cpp
+++ b/src/DebconfGui.cpp
@@ -92,7 +92,7 @@ DebconfGui::DebconfGui(const QString &socketName, QWidget *parent)
    d_ptr(new DebconfGuiPrivate)
 {
     Q_D(DebconfGui);
-    d->frontend = new DebconfFrontend(socketName, this);
+    d->frontend = new DebconfFrontendSocket(socketName, this);
     d->elementProgress = 0;
     d->parentWidget = 0;
     d->setupUi(this);
diff --git a/src/debconf.cpp b/src/debconf.cpp
index cb2c1e0..332ca01 100644
--- a/src/debconf.cpp
+++ b/src/debconf.cpp
@@ -72,35 +72,13 @@ const DebconfFrontend::Cmd DebconfFrontend::commands[] = {
     { "PROGRESS", &DebconfFrontend::cmd_progress },
     { 0, 0 } };
 
-DebconfFrontend::DebconfFrontend(const QString &socketName, QObject *parent)
-  : QObject(parent), m_socket(0)
+DebconfFrontend::DebconfFrontend(QObject *parent)
+  : QObject(parent)
 {
-    m_server = new QLocalServer(this);
-    QFile::remove(socketName);
-    m_server->listen(socketName);
-    connect(m_server, SIGNAL(newConnection()), this, SLOT(newConnection()));
-}
-
-void DebconfFrontend::newConnection()
-{
-    kDebug();
-    if (m_socket) {
-        QLocalSocket *socket = m_server->nextPendingConnection();
-        socket->disconnectFromServer();
-        socket->deleteLater();
-        return;
-    }
-
-    m_socket = m_server->nextPendingConnection();
-    if (m_socket) {
-        connect(m_socket, SIGNAL(readyRead()), this, SLOT(process()));
-        connect(m_socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
-    }
 }
 
 DebconfFrontend::~DebconfFrontend()
 {
-    QFile::remove(m_server->fullServerName());
 }
 
 void DebconfFrontend::disconnected()
@@ -156,8 +134,6 @@ DebconfFrontend::TypeKey DebconfFrontend::type(const QString &string) const
 void DebconfFrontend::reset()
 {
     emit backup(false);
-    m_socket->deleteLater();
-    m_socket = 0;
     m_data.clear();
     m_subst.clear();
     m_values.clear();
@@ -166,7 +142,7 @@ void DebconfFrontend::reset()
 void DebconfFrontend::say(const QString &string)
 {
     kDebug() << "DEBCONF ---> " << string;
-    QTextStream out(m_socket);
+    QTextStream out(getWriteDevice());
     out << string << "\n";
 }
 
@@ -253,7 +229,6 @@ void DebconfFrontend::back()
 
 void DebconfFrontend::cancel()
 {
-    m_socket->disconnectFromServer();
     reset();
 }
 
@@ -302,7 +277,7 @@ void DebconfFrontend::cmd_subst(const QString &param)
 
 bool DebconfFrontend::process()
 {
-    QTextStream in(m_socket);
+    QTextStream in(getReadDevice());
     QString line = in.readLine();
 
     if (line.isEmpty()) {
@@ -324,5 +299,51 @@ bool DebconfFrontend::process()
     return false;
 }
 
+DebconfFrontendSocket::DebconfFrontendSocket(const QString &socketName, QObject *parent)
+  : DebconfFrontend(parent), m_socket(0)
+{
+    m_server = new QLocalServer(this);
+    QFile::remove(socketName);
+    m_server->listen(socketName);
+    connect(m_server, SIGNAL(newConnection()), this, SLOT(newConnection()));
+}
+
+DebconfFrontendSocket::~DebconfFrontendSocket()
+{
+    QFile::remove(m_server->fullServerName());
 }
+
+void DebconfFrontendSocket::newConnection()
+{
+    kDebug();
+    if (m_socket) {
+        QLocalSocket *socket = m_server->nextPendingConnection();
+        socket->disconnectFromServer();
+        socket->deleteLater();
+        return;
+    }
+
+    m_socket = m_server->nextPendingConnection();
+    if (m_socket) {
+        connect(m_socket, SIGNAL(readyRead()), this, SLOT(process()));
+        connect(m_socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
+    }
+}
+
+void DebconfFrontendSocket::reset()
+{
+    m_socket->deleteLater();
+    m_socket = 0;
+
+    DebconfFrontend::reset();
+}
+
+void DebconfFrontendSocket::cancel()
+{
+    m_socket->disconnectFromServer();
+    DebconfFrontend::cancel();
+}
+
+}
+
 #include "debconf.moc"
diff --git a/src/debconf.h b/src/debconf.h
index 4fc6096..2b85630 100644
--- a/src/debconf.h
+++ b/src/debconf.h
@@ -60,6 +60,10 @@
 
 namespace DebconfKde {
 
+/**
+  * An abstract class which talks Debconf Passthrough frontend protocol. It
+  * does not implement underlying I/O method specifics.
+  */
 class DebconfFrontend : public QObject {
     Q_OBJECT
     Q_ENUMS(PropertyKey)
@@ -87,8 +91,8 @@ public:
         UnknownTypeKey = -1
     } TypeKey;
 
-    explicit DebconfFrontend(const QString &socketName, QObject *parent = 0);
-    ~DebconfFrontend();
+    explicit DebconfFrontend(QObject *parent = 0);
+    virtual ~DebconfFrontend();
 
     QString value(const QString &key) const;
     void setValue(const QString &key, const QString &value);
@@ -97,6 +101,9 @@ public:
 
     TypeKey type(const QString &string) const;
 
+    /**
+      * Send \p string to Debconf over established connection
+      */
     void say(const QString &string);
 
     /**
@@ -110,7 +117,7 @@ public:
     /**
      * Closes the current connection, canceling all questions
      */
-    void cancel();
+    virtual void cancel();
 
 signals:
     void go(const QString &title, const QStringList &input);
@@ -118,21 +125,33 @@ signals:
     void finished();
     void backup(bool capable);
 
-private slots:
+protected Q_SLOTS:
     /**
-     * This slot is called when the socket has new data
+     * This slot should be called when there is new data available.
      */
-    bool process();
+    virtual bool process();
+
     /**
-     * Called when a new connection is received on the socket
-     * If we are already handlyng a connection we refuse the others
+     * This slot should be called when connection with Debconf is terminated.
      */
-    void newConnection();
+    virtual void disconnected();
+
+protected:
+    /**
+      * This method must be overriden by the derivative class and return
+      * current QIODevice which should be used to read data from Debconf.
+      */
+    virtual QIODevice* getReadDevice() const = 0;
+    /**
+      * This method must be overriden by the derivative class and return
+      * current QIODevice which should be used to write data to Debconf.
+      */
+    virtual QIODevice* getWriteDevice() const = 0;
     /**
-     * When the client disconnects we need to hide the GUI and clean
-     * our internal data
+     * This is called to clean up internal data once connection with
+     * Debconf is terminated.
      */
-    void disconnected();
+    virtual void reset();
 
 private:
     void cmd_capb(const QString &caps);
@@ -150,8 +169,6 @@ private:
     };
     static const Cmd commands[];
 
-    void reset();
-
     // TODO this is apparently very much untested
     QString substitute(const QString &key, const QString &rest) const;
     /**
@@ -163,8 +180,6 @@ private:
     typedef QHash<PropertyKey, QString> Properties;
     typedef QHash<QString, QString> Substitutions;
 
-    QLocalSocket *m_socket;
-    QLocalServer *m_server;
     QHash<QString, Properties>    m_data;
     QHash<QString, Substitutions> m_subst;
     QHash<QString, QString>       m_values;
@@ -172,6 +187,47 @@ private:
     QStringList m_input;
 };
 
+/**
+  * DebconfFrontend which communicates with Debconf over UNIX socket.
+  */
+class DebconfFrontendSocket : public DebconfFrontend {
+    Q_OBJECT
+
+public:
+    /**
+      * Instantiates the class and starts listening for new connections on the
+      * socket at \p socketName path. Please note that any file at \p socketName
+      * will be removed if it exists prior to the call of this constructor.
+      */
+    explicit DebconfFrontendSocket(const QString &socketName, QObject *parent = 0);
+
+    /**
+      * Removes socket when the object is destroyed.
+      */
+    virtual ~DebconfFrontendSocket();
+
+    /**
+      * Overriden to trigger termination of the current connection.
+      */
+    void cancel();
+
+protected:
+    inline QIODevice* getReadDevice() const { return m_socket; }
+    inline QIODevice* getWriteDevice() const { return m_socket; }
+    void reset();
+
+private Q_SLOTS:
+    /**
+     * Called when a new connection is received on the socket
+     * If we are already handlyng a connection we refuse the others
+     */
+    void newConnection();
+
+private:
+    QLocalServer *m_server;
+    QLocalSocket *m_socket;
+};
+
 }
 
 #endif


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic