From: Drew Fisher Date: Sat, 26 Apr 2014 06:29:44 +0000 (-0700) Subject: ribbon: extract accounts and conversations into separate classes X-Git-Url: http://git.zarvox.org/shortlog/2012?a=commitdiff_plain;h=a9b809347b08328ff0f0df2e6f623db1ba543b3f;p=imoo.git ribbon: extract accounts and conversations into separate classes --- diff --git a/ribbon/ribbon.pro b/ribbon/ribbon.pro index 48d44ec..2e19c40 100644 --- a/ribbon/ribbon.pro +++ b/ribbon/ribbon.pro @@ -19,12 +19,14 @@ QMAKE_CXXFLAGS += -Werror -W HEADERS += ribbonmanager.h \ ribbonaccount.h \ ribbonaccounts.h \ + ribbonconversations.h \ ribbonserver.h \ ribbonsocket.h SOURCES += ribbonmanager.cpp \ ribbonaccount.cpp \ ribbonaccounts.cpp \ + ribbonconversations.cpp \ ribbonserver.cpp \ ribbonsocket.cpp \ main.cpp diff --git a/ribbon/ribbonaccounts.cpp b/ribbon/ribbonaccounts.cpp index 216b7c2..59835e4 100644 --- a/ribbon/ribbonaccounts.cpp +++ b/ribbon/ribbonaccounts.cpp @@ -18,6 +18,8 @@ void RibbonAccounts::handleExternalEvent(QString method, QVariantMap params) createAccount(params["protocol"].toByteArray(), params["username"].toByteArray(), params["password"].toByteArray()); + } else if (method == "destroy_account") { + destroyAccount(RibbonAccount::fromString(params["account_id"].toString())); } } @@ -36,3 +38,12 @@ void RibbonAccounts::createAccount(QByteArray protocol, QByteArray username, QBy r_acct.username = username; accounts[r_acct] = account; } + +void RibbonAccounts::destroyAccount(RibbonAccount r_acct) +{ + if (accounts.contains(r_acct)) { + qDebug() << "Deleting" << r_acct.toString(); + PurpleAccount* account = accounts.take(r_acct); + purple_account_destroy(account); + } +} diff --git a/ribbon/ribbonaccounts.h b/ribbon/ribbonaccounts.h index 178f782..d56312d 100644 --- a/ribbon/ribbonaccounts.h +++ b/ribbon/ribbonaccounts.h @@ -17,10 +17,13 @@ public: ~RibbonAccounts(); public slots: void handleExternalEvent(QString method, QVariantMap params); +signals: + void event(QString source, QString signal, QVariantMap context); private: RibbonManager* _manager; QMap accounts; void createAccount(QByteArray protocol, QByteArray username, QByteArray password); + void destroyAccount(RibbonAccount r_acct); }; #endif /* RIBBONACCOUNTS_H */ diff --git a/ribbon/ribbonconversations.cpp b/ribbon/ribbonconversations.cpp new file mode 100644 index 0000000..c081e3a --- /dev/null +++ b/ribbon/ribbonconversations.cpp @@ -0,0 +1,40 @@ +#include "ribbonconversations.h" +#include "ribbonmanager.h" + +#include +#include + +static void static_received_im_msg(PurpleAccount *account, char *sender, char *message, + PurpleConversation *conv, PurpleMessageFlags flags, void* data) +{ + Q_UNUSED(data); + RibbonManager* r = static_cast(account->ui_data); + r->conversations()->received_im_msg(account, sender, message, conv, flags); +} + +RibbonConversations::RibbonConversations(RibbonManager* parent) : QObject((QObject*)parent) +{ + _manager = parent; +} + +RibbonConversations::~RibbonConversations() +{ +} + +void RibbonConversations::init() +{ + static int handle; + void *conversations_handle = purple_conversations_get_handle(); + purple_signal_connect(conversations_handle, "received-im-msg", &handle, + PURPLE_CALLBACK(static_received_im_msg), NULL); +} + +void RibbonConversations::received_im_msg(PurpleAccount *account, + char *sender, char *message, PurpleConversation *conv, + PurpleMessageFlags flags) { + Q_UNUSED(flags); + if (conv==NULL) { + conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, sender); + } + qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss") << sender << purple_conversation_get_name(conv) << message; +} diff --git a/ribbon/ribbonconversations.h b/ribbon/ribbonconversations.h new file mode 100644 index 0000000..4b72448 --- /dev/null +++ b/ribbon/ribbonconversations.h @@ -0,0 +1,25 @@ +#ifndef RIBBONCONVERSATIONS_H +#define RIBBONCONVERSATIONS_H + +#include +#include +#include +#include + +class RibbonManager; + +class RibbonConversations : public QObject { + Q_OBJECT +public: + RibbonConversations(RibbonManager* parent = 0); + ~RibbonConversations(); + void init(); + void received_im_msg(PurpleAccount *account, char *sender, char *message, + PurpleConversation *conv, PurpleMessageFlags flags); +signals: + void event(QString source, QString signal, QVariantMap context); +private: + RibbonManager* _manager; +}; + +#endif /* RIBBONCONVERSATIONS_H */ diff --git a/ribbon/ribbonmanager.cpp b/ribbon/ribbonmanager.cpp index 8969060..074afd5 100644 --- a/ribbon/ribbonmanager.cpp +++ b/ribbon/ribbonmanager.cpp @@ -1,5 +1,6 @@ #include "ribbonmanager.h" #include "ribbonaccounts.h" +#include "ribbonconversations.h" #include #include @@ -209,14 +210,6 @@ static void signed_on(PurpleConnection *gc, void* data) printf("Account connected: \"%s\" (%s)\n", purple_account_get_username(account), purple_account_get_protocol_id(account)); } -static void received_im_msg(PurpleAccount *account, char *sender, char *message, - PurpleConversation *conv, PurpleMessageFlags flags, void* data) -{ - Q_UNUSED(data); - RibbonManager* r = static_cast(account->ui_data); - r->received_im_msg(account, sender, message, conv, flags); -} - static void buddy_signed_on(PurpleBuddy* buddy, void* data) { Q_UNUSED(data); @@ -228,20 +221,19 @@ static void connect_to_signals(void) static int handle; void *blist_handle = purple_blist_get_handle(); void *connections_handle = purple_connections_get_handle(); - void *conversations_handle = purple_conversations_get_handle(); purple_signal_connect(connections_handle, "signed-on", &handle, PURPLE_CALLBACK(signed_on), NULL); - purple_signal_connect(conversations_handle, "received-im-msg", &handle, - PURPLE_CALLBACK(received_im_msg), NULL); purple_signal_connect(blist_handle, "buddy-signed-on", &handle, PURPLE_CALLBACK(buddy_signed_on), NULL); - //purple_signal_connect( } RibbonManager::RibbonManager(QObject* parent) : QObject(parent) { - accounts = new RibbonAccounts(this); + _accounts = new RibbonAccounts(this); + _conversations = new RibbonConversations(this); + QObject::connect(_conversations, SIGNAL(event(QString,QString,QVariantMap)), + this, SIGNAL(event(QString,QString,QVariantMap))); } RibbonManager::~RibbonManager() @@ -276,6 +268,9 @@ void RibbonManager::init(void) /* Hopefully this is an okay time for this? */ connect_to_signals(); + + // TODO: add more children init() calls when refactoring + _conversations->init(); } const char* RibbonManager::get_version(void) @@ -283,20 +278,11 @@ const char* RibbonManager::get_version(void) return purple_core_get_version(); } -void RibbonManager::received_im_msg(PurpleAccount *account, char *sender, char *message, - PurpleConversation *conv, PurpleMessageFlags flags) { - Q_UNUSED(flags); - if (conv==NULL) { - conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, sender); - } - qDebug() << QDateTime::currentDateTime().toString("hh:mm:ss") << sender << purple_conversation_get_name(conv) << message; -} - void RibbonManager::handleExternalEvent(QString destination, QString method, QVariantMap context) { // TODO: handle external event qDebug() << "TODO: do something with" << destination << "." << method << "(" << context << ")"; if (destination == "accounts") { - accounts->handleExternalEvent(method, context); + _accounts->handleExternalEvent(method, context); } } diff --git a/ribbon/ribbonmanager.h b/ribbon/ribbonmanager.h index be47e6a..58e460b 100644 --- a/ribbon/ribbonmanager.h +++ b/ribbon/ribbonmanager.h @@ -7,6 +7,7 @@ // Forward declarations class RibbonAccounts; +class RibbonConversations; #define UI_ID "ribbon" @@ -18,13 +19,12 @@ public: // Called to configure libpurple and connect to purple signals void init(void); + RibbonAccounts* accounts() { return _accounts; } + RibbonConversations* conversations() { return _conversations; } // Exposes purple_get_version() const char* get_version(void); - // Event handlers - void received_im_msg(PurpleAccount* account, char* sender, char* message, PurpleConversation* conv, PurpleMessageFlags flags); - public slots: void handleExternalEvent(QString destination, QString method, QVariantMap context); @@ -32,8 +32,8 @@ signals: void event(QString source, QString signal, QVariantMap context); private: - // TODO: add a map of currently-open conversations? - RibbonAccounts* accounts; + RibbonAccounts* _accounts; + RibbonConversations* _conversations; Q_DISABLE_COPY(RibbonManager) };