]> git.zarvox.org Git - imoo.git/commitdiff
ribbon: extract accounts and conversations into separate classes
authorDrew Fisher <drew.m.fisher@gmail.com>
Sat, 26 Apr 2014 06:29:44 +0000 (23:29 -0700)
committerDrew Fisher <drew.m.fisher@gmail.com>
Sat, 26 Apr 2014 06:29:44 +0000 (23:29 -0700)
ribbon/ribbon.pro
ribbon/ribbonaccounts.cpp
ribbon/ribbonaccounts.h
ribbon/ribbonconversations.cpp [new file with mode: 0644]
ribbon/ribbonconversations.h [new file with mode: 0644]
ribbon/ribbonmanager.cpp
ribbon/ribbonmanager.h

index 48d44ec71e4aaac6a2ee58b1f459115f11b36b33..2e19c40d0f4a8c1efd089c89efa2f359f427f4e9 100644 (file)
@@ -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
index 216b7c29669d0b52d2ed6b24c81caca720bba67a..59835e45b0f9d1b2917ce1bf223a08b0de5ff67e 100644 (file)
@@ -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);
+       }
+}
index 178f782e1ef22223941201b8afbf2bacc73369d5..d56312d97e54dae784490e40115707a4069d3be1 100644 (file)
@@ -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<RibbonAccount, PurpleAccount*> 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 (file)
index 0000000..c081e3a
--- /dev/null
@@ -0,0 +1,40 @@
+#include "ribbonconversations.h"
+#include "ribbonmanager.h"
+
+#include <QDebug>
+#include <QDateTime>
+
+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<RibbonManager*>(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 (file)
index 0000000..4b72448
--- /dev/null
@@ -0,0 +1,25 @@
+#ifndef RIBBONCONVERSATIONS_H
+#define RIBBONCONVERSATIONS_H
+
+#include <QObject>
+#include <QString>
+#include <QVariantMap>
+#include <libpurple/purple.h>
+
+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 */
index 8969060e2be19e2c7eccf62140dbee46b4745514..074afd559d7c8c1f334c485f2c9016826ee8dd66 100644 (file)
@@ -1,5 +1,6 @@
 #include "ribbonmanager.h"
 #include "ribbonaccounts.h"
+#include "ribbonconversations.h"
 
 #include <QDebug>
 #include <QFile>
@@ -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<RibbonManager*>(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);
        }
 }
index be47e6adee41e9479fd6171591fbe364ca21b479..58e460b59214bb477b9864f3dee675fa99882176 100644 (file)
@@ -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)
 };