From: Drew Fisher Date: Sat, 26 Apr 2014 08:15:17 +0000 (-0700) Subject: ribbon: extract connections into a class X-Git-Url: http://git.zarvox.org/static/%24c%5B5%5D?a=commitdiff_plain;h=720d12ef7a3f60be0f31d2083c986186f6190609;p=imoo.git ribbon: extract connections into a class TODO: much cleanup --- diff --git a/ribbon/ribbon.pro b/ribbon/ribbon.pro index 8f5cd7a..8310a56 100644 --- a/ribbon/ribbon.pro +++ b/ribbon/ribbon.pro @@ -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 \ diff --git a/ribbon/ribbonaccounts.cpp b/ribbon/ribbonaccounts.cpp index 59835e4..e87a5cd 100644 --- a/ribbon/ribbonaccounts.cpp +++ b/ribbon/ribbonaccounts.cpp @@ -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; diff --git a/ribbon/ribbonaccounts.h b/ribbon/ribbonaccounts.h index d56312d..019f9d9 100644 --- a/ribbon/ribbonaccounts.h +++ b/ribbon/ribbonaccounts.h @@ -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: diff --git a/ribbon/ribbonblist.cpp b/ribbon/ribbonblist.cpp index c9bb691..8438495 100644 --- a/ribbon/ribbonblist.cpp +++ b/ribbon/ribbonblist.cpp @@ -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 index 0000000..57a5166 --- /dev/null +++ b/ribbon/ribbonconnections.cpp @@ -0,0 +1,103 @@ +#include "ribbonconnections.h" +#include "ribbonmanager.h" +#include "ribbonaccounts.h" + +#include + +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(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(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(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(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 index 0000000..bdd7d32 --- /dev/null +++ b/ribbon/ribbonconnections.h @@ -0,0 +1,23 @@ +#ifndef RIBBONCONNECTIONS_H +#define RIBBONCONNECTIONS_H + +#include +#include +#include + +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 diff --git a/ribbon/ribbonmanager.cpp b/ribbon/ribbonmanager.cpp index 7a56a3b..ffdd018 100644 --- a/ribbon/ribbonmanager.cpp +++ b/ribbon/ribbonmanager.cpp @@ -1,6 +1,7 @@ #include "ribbonmanager.h" #include "ribbonaccounts.h" #include "ribbonblist.h" +#include "ribbonconnections.h" #include "ribbonconversations.h" #include @@ -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(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(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(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(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(); } diff --git a/ribbon/ribbonmanager.h b/ribbon/ribbonmanager.h index 171dfef..4438ac2 100644 --- a/ribbon/ribbonmanager.h +++ b/ribbon/ribbonmanager.h @@ -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) };