]> git.zarvox.org Git - imoo.git/commitdiff
ribbon: extract connections into a class
authorDrew Fisher <drew.m.fisher@gmail.com>
Sat, 26 Apr 2014 08:15:17 +0000 (01:15 -0700)
committerDrew Fisher <drew.m.fisher@gmail.com>
Sat, 26 Apr 2014 08:15:17 +0000 (01:15 -0700)
TODO: much cleanup

ribbon/ribbon.pro
ribbon/ribbonaccounts.cpp
ribbon/ribbonaccounts.h
ribbon/ribbonblist.cpp
ribbon/ribbonconnections.cpp [new file with mode: 0644]
ribbon/ribbonconnections.h [new file with mode: 0644]
ribbon/ribbonmanager.cpp
ribbon/ribbonmanager.h

index 8f5cd7aa42db92cc267e296c54c3c5af16b9fe0f..8310a56df99f5212986394fc9f514a9f00996911 100644 (file)
@@ -20,6 +20,7 @@ HEADERS += ribbonmanager.h \
            ribbonaccount.h \
            ribbonaccounts.h \
            ribbonblist.h \
+           ribbonconnections.h \
            ribbonconversations.h \
            ribbonserver.h \
            ribbonsocket.h
@@ -28,6 +29,7 @@ SOURCES += ribbonmanager.cpp \
            ribbonaccount.cpp \
            ribbonaccounts.cpp \
            ribbonblist.cpp \
+           ribbonconnections.cpp \
            ribbonconversations.cpp \
            ribbonserver.cpp \
            ribbonsocket.cpp \
index 59835e45b0f9d1b2917ce1bf223a08b0de5ff67e..e87a5cd651e663745f7a2b92c942bd9b173cf20f 100644 (file)
@@ -11,6 +11,14 @@ RibbonAccounts::~RibbonAccounts()
 {
 }
 
+QVariantMap RibbonAccounts::context(PurpleAccount* acct)
+{
+       QVariantMap m;
+       m["proto"] = purple_account_get_protocol_id(acct);
+       m["account"] = purple_account_get_username(acct);
+       return m;
+}
+
 void RibbonAccounts::handleExternalEvent(QString method, QVariantMap params)
 {
        qDebug() << "called" << method << "on" << params;
index d56312d97e54dae784490e40115707a4069d3be1..019f9d90a654f1f28e8632ee9ab39e46f062b307 100644 (file)
@@ -15,6 +15,7 @@ class RibbonAccounts : public QObject {
 public:
        RibbonAccounts(RibbonManager* parent = 0);
        ~RibbonAccounts();
+       static QVariantMap context(PurpleAccount* acct);
 public slots:
        void handleExternalEvent(QString method, QVariantMap params);
 signals:
index c9bb6913557e9bfc7af487ca1c4a1f0b4111cd3b..8438495daf77839d63872eea083bbcd684c89a60 100644 (file)
@@ -30,6 +30,11 @@ static void buddy_signed_on(PurpleBuddy* buddy, void* data)
 
 void RibbonBlist::init()
 {
+       /* Create and load the buddylist */
+       purple_set_blist(purple_blist_new());
+       purple_blist_load();
+
+       // Register signal callbacks
        static int handle;
        void *blist_handle = purple_blist_get_handle();
        purple_signal_connect(blist_handle, "buddy-signed-on", &handle,
diff --git a/ribbon/ribbonconnections.cpp b/ribbon/ribbonconnections.cpp
new file mode 100644 (file)
index 0000000..57a5166
--- /dev/null
@@ -0,0 +1,103 @@
+#include "ribbonconnections.h"
+#include "ribbonmanager.h"
+#include "ribbonaccounts.h"
+
+#include <QDebug>
+
+static void connect_progress(PurpleConnection *gc, const char* text, size_t step, size_t step_count)
+{
+       PurpleAccount* account = purple_connection_get_account(gc);
+       RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
+       qDebug() << QString("Connection progress (%1:%2):").arg(account->protocol_id, account->username).toUtf8().constData() << text << step << "of" << step_count;
+       // TODO: add enough unique information from account to ctx to handle the message
+       QVariantMap ctx = RibbonAccounts::context(account);
+       ctx["step"] = QVariant((qulonglong)step);
+       ctx["step_count"] = QVariant((qulonglong)step_count);
+       ctx["text"] = QVariant(QString::fromUtf8(text));
+       r->event(QString("connection"), QString("connect_progress"), ctx);
+}
+
+static void connection_notice(PurpleConnection *gc, const char* text)
+{
+       qDebug() << "Connection notice:" << text;
+       PurpleAccount* account = purple_connection_get_account(gc);
+       RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
+       QVariantMap ctx = RibbonAccounts::context(account);
+       ctx["message"] = QVariant(QString::fromUtf8(text));
+       r->event(QString("connection"), QString("connect_notice"), ctx);
+}
+
+static void connection_disconnected(PurpleConnection *gc, const char* text)
+{
+       qDebug() << "Connection disconnected:" << text;
+       PurpleAccount* account = purple_connection_get_account(gc);
+       RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
+       QVariantMap ctx = RibbonAccounts::context(account);
+       ctx["message"] = QVariant(QString::fromUtf8(text));
+       r->event(QString("connection"), QString("connection_disconnected"), ctx);
+}
+
+static void network_connected(void)
+{
+       qWarning() << "Network connection established";
+}
+
+static void network_disconnected(void)
+{
+       qWarning() << "This machine has been disconnected from the internet";
+}
+
+static void report_disconnect_reason(PurpleConnection *gc, PurpleConnectionError reason, const char *text)
+{
+       PurpleAccount *account = purple_connection_get_account(gc);
+       //qDebug() << QString("Connection disconnected: \'%1\' (%2)\n  >Error: %3\n  >Reason: %4\n").arg(purple_account_get_username(account), purple_account_get_protocol_id(account), qint32(reason), text);
+       RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
+       QVariantMap ctx = RibbonAccounts::context(account);
+       ctx["reason_code"] = QVariant(qint32(reason));
+       ctx["reason_text"] = QVariant(QString::fromUtf8(text));
+       r->event(QString("connection"), QString("disconnect_reason"), ctx);
+}
+
+static PurpleConnectionUiOps connection_uiops =
+{
+       connect_progress,          /* connect_progress         */
+       NULL,                      /* connected                */
+       NULL,                      /* disconnected             */
+       connection_notice,         /* notice                   */
+       connection_disconnected,   /* report_disconnect        */
+       network_connected,         /* network_connected        */
+       network_disconnected,      /* network_disconnected     */
+       report_disconnect_reason,  /* report_disconnect_reason */
+       NULL,
+       NULL,
+       NULL
+};
+
+// TODO: note that the void* data can be set at purple_signal_connect time, and
+// set it to the class instance to allow for convenient upcalls
+// Then, actually do upcalls and emit the appropriate signals.
+static void signed_on(PurpleConnection *gc, void* data)
+{
+       Q_UNUSED(data);
+       PurpleAccount *account = purple_connection_get_account(gc);
+       printf("Account connected: \"%s\" (%s)\n", purple_account_get_username(account), purple_account_get_protocol_id(account));
+}
+
+RibbonConnections::RibbonConnections(RibbonManager* parent) : QObject((QObject*)parent)
+{
+       _manager = parent;
+}
+
+RibbonConnections::~RibbonConnections()
+{
+}
+
+void RibbonConnections::init(void)
+{
+       purple_connections_set_ui_ops(&connection_uiops);
+       static int handle;
+       void *connections_handle = purple_connections_get_handle();
+
+       purple_signal_connect(connections_handle, "signed-on", &handle,
+                       PURPLE_CALLBACK(signed_on), this);
+}
diff --git a/ribbon/ribbonconnections.h b/ribbon/ribbonconnections.h
new file mode 100644 (file)
index 0000000..bdd7d32
--- /dev/null
@@ -0,0 +1,23 @@
+#ifndef RIBBONCONNECTIONS_H
+#define RIBBONCONNECTIONS_H
+
+#include <QObject>
+#include <QVariantMap>
+#include <libpurple/purple.h>
+
+class RibbonManager;
+
+class RibbonConnections : public QObject {
+       Q_OBJECT
+public:
+       RibbonConnections(RibbonManager* parent = 0);
+       ~RibbonConnections();
+       void init(void);
+
+signals:
+       void event(QString source, QString signal, QVariantMap context);
+private:
+       RibbonManager* _manager;
+};
+
+#endif // RIBBONCONNECTIONS_H
index 7a56a3b8c37585eec6916924588a8fadda5b1304..ffdd018d081030089bfe0f1dea7fdc81cef5363d 100644 (file)
@@ -1,6 +1,7 @@
 #include "ribbonmanager.h"
 #include "ribbonaccounts.h"
 #include "ribbonblist.h"
+#include "ribbonconnections.h"
 #include "ribbonconversations.h"
 
 #include <QDebug>
@@ -103,87 +104,8 @@ static PurpleEventLoopUiOps glib_eventloops =
 //     NULL
 //};
 
-static QVariantMap ctx_from_account(PurpleAccount* account)
-{
-       QVariantMap ctx;
-       ctx["protocol"] = QVariant(purple_account_get_protocol_id(account));
-       ctx["username"] = QVariant(purple_account_get_username(account));
-       return ctx;
-}
-
-static void connect_progress(PurpleConnection *gc, const char* text, size_t step, size_t step_count)
-{
-       PurpleAccount* account = purple_connection_get_account(gc);
-       RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
-       qDebug() << QString("Connection progress (%1:%2):").arg(account->protocol_id, account->username).toUtf8().constData() << text << step << "of" << step_count;
-       // TODO: add enough unique information from account to ctx to handle the message
-       QVariantMap ctx = ctx_from_account(account);
-       ctx["step"] = QVariant((qulonglong)step);
-       ctx["step_count"] = QVariant((qulonglong)step_count);
-       ctx["text"] = QVariant(QString::fromUtf8(text));
-       r->event(QString("connection"), QString("connect_progress"), ctx);
-}
-
-static void connection_notice(PurpleConnection *gc, const char* text)
-{
-       qDebug() << "Connection notice:" << text;
-       PurpleAccount* account = purple_connection_get_account(gc);
-       RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
-       QVariantMap ctx = ctx_from_account(account);
-       ctx["message"] = QVariant(QString::fromUtf8(text));
-       r->event(QString("connection"), QString("connect_notice"), ctx);
-}
-
-static void connection_disconnected(PurpleConnection *gc, const char* text)
-{
-       qDebug() << "Connection disconnected:" << text;
-       PurpleAccount* account = purple_connection_get_account(gc);
-       RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
-       QVariantMap ctx = ctx_from_account(account);
-       ctx["message"] = QVariant(QString::fromUtf8(text));
-       r->event(QString("connection"), QString("connection_disconnected"), ctx);
-}
-
-static void network_connected(void)
-{
-       qWarning() << "Network connection established";
-}
-
-static void network_disconnected(void)
-{
-       qWarning() << "This machine has been disconnected from the internet";
-}
-
-static void report_disconnect_reason(PurpleConnection *gc, PurpleConnectionError reason, const char *text)
-{
-       PurpleAccount *account = purple_connection_get_account(gc);
-       //qDebug() << QString("Connection disconnected: \'%1\' (%2)\n  >Error: %3\n  >Reason: %4\n").arg(purple_account_get_username(account), purple_account_get_protocol_id(account), qint32(reason), text);
-       RibbonManager* r = static_cast<RibbonManager*>(account->ui_data);
-       QVariantMap ctx = ctx_from_account(account);
-       ctx["reason_code"] = QVariant(qint32(reason));
-       ctx["reason_text"] = QVariant(QString::fromUtf8(text));
-       r->event(QString("connection"), QString("disconnect_reason"), ctx);
-}
-
-static PurpleConnectionUiOps connection_uiops =
-{
-       connect_progress,          /* connect_progress         */
-       NULL,                      /* connected                */
-       NULL,                      /* disconnected             */
-       connection_notice,         /* notice                   */
-       connection_disconnected,   /* report_disconnect        */
-       network_connected,         /* network_connected        */
-       network_disconnected,      /* network_disconnected     */
-       report_disconnect_reason,  /* report_disconnect_reason */
-       NULL,
-       NULL,
-       NULL
-};
-
 static void ribbon_core_init(void) {
        printf("core_init called\n");
-       printf("setting connection callbacks\n");
-       purple_connections_set_ui_ops(&connection_uiops);
 }
 
 static PurpleCoreUiOps ribbon_core_callbacks = {
@@ -201,32 +123,17 @@ static PurpleCoreUiOps ribbon_core_callbacks = {
        NULL
 };
 
-// TODO: note that the void* data can be set at purple_signal_connect time, and
-// set it to the class instance to allow for convenient upcalls
-// Then, actually do upcalls and emit the appropriate signals.
-static void signed_on(PurpleConnection *gc, void* data)
-{
-       Q_UNUSED(data);
-       PurpleAccount *account = purple_connection_get_account(gc);
-       printf("Account connected: \"%s\" (%s)\n", purple_account_get_username(account), purple_account_get_protocol_id(account));
-}
-
-static void connect_to_signals(void)
-{
-       static int handle;
-       void *connections_handle = purple_connections_get_handle();
-
-       purple_signal_connect(connections_handle, "signed-on", &handle,
-                       PURPLE_CALLBACK(signed_on), NULL);
-}
 
 RibbonManager::RibbonManager(QObject* parent) : QObject(parent)
 {
        _accounts = new RibbonAccounts(this);
        _blist = new RibbonBlist(this);
+       _connections = new RibbonConnections(this);
        _conversations = new RibbonConversations(this);
        QObject::connect(_blist, SIGNAL(event(QString,QString,QVariantMap)),
                        this, SIGNAL(event(QString,QString,QVariantMap)));
+       QObject::connect(_connections, SIGNAL(event(QString,QString,QVariantMap)),
+                       this, SIGNAL(event(QString,QString,QVariantMap)));
        QObject::connect(_conversations, SIGNAL(event(QString,QString,QVariantMap)),
                        this, SIGNAL(event(QString,QString,QVariantMap)));
 }
@@ -257,14 +164,10 @@ void RibbonManager::init(void)
                abort();
        }
 
-       /* Create and load the buddylist */
-       purple_set_blist(purple_blist_new());
-       purple_blist_load();
-
-       /* Hopefully this is an okay time for this? */
-       connect_to_signals();
-
+       // set up connections first, since it has to use the callback structure
+       // whereas other things can just connect to glib signals
        // TODO: add more children init() calls when refactoring
+       _connections->init();
        _blist->init();
        _conversations->init();
 }
index 171dfefc09b9cdde0e1950441f5a1ed3dbb2518b..4438ac2f4b4759d7a48e528ae104dea75a4b82fc 100644 (file)
@@ -8,6 +8,7 @@
 // Forward declarations
 class RibbonAccounts;
 class RibbonBlist;
+class RibbonConnections;
 class RibbonConversations;
 
 #define UI_ID "ribbon"
@@ -23,6 +24,7 @@ public:
        RibbonAccounts* accounts() { return _accounts; }
        RibbonBlist* blist() { return _blist; }
        RibbonConversations* conversations() { return _conversations; }
+       RibbonConnections* connections() { return _connections; }
 
        // Exposes purple_get_version()
        const char* get_version(void);
@@ -36,6 +38,7 @@ signals:
 private:
        RibbonAccounts* _accounts;
        RibbonBlist* _blist;
+       RibbonConnections* _connections;
        RibbonConversations* _conversations;
        Q_DISABLE_COPY(RibbonManager)
 };