]> git.zarvox.org Git - imoo.git/commitdiff
ribbon: Extract account management into a separate class
authorDrew Fisher <drew.m.fisher@gmail.com>
Thu, 24 Apr 2014 07:50:40 +0000 (00:50 -0700)
committerDrew Fisher <drew.m.fisher@gmail.com>
Thu, 24 Apr 2014 07:50:40 +0000 (00:50 -0700)
ribbon/main.cpp
ribbon/ribbon.pro
ribbon/ribbonaccount.cpp [new file with mode: 0644]
ribbon/ribbonaccount.h [new file with mode: 0644]
ribbon/ribbonaccounts.cpp [new file with mode: 0644]
ribbon/ribbonaccounts.h [new file with mode: 0644]
ribbon/ribbonmanager.cpp
ribbon/ribbonmanager.h

index 3103aff74528d76cef418a34cac3ebc3de6dba88..28a88868b167bd13e0f5f145a5f9f44430dfe09c 100644 (file)
@@ -3,7 +3,6 @@
 #include <QDebug>
 
 #include <QObject>
-#include <QPushButton>
 
 #include <signal.h>
 
@@ -21,18 +20,11 @@ int main(int argc, char** argv)
         */
        signal(SIGCHLD, SIG_IGN);
 
-       QPushButton* b = new QPushButton("Sign in");
-       b->resize(100,50);
-       b->show();
-
        RibbonManager* purple = new RibbonManager();
        purple->init();
 
        qDebug() << "libpurple initialized, version" << purple->get_version();
 
-       QObject::connect(b, SIGNAL(clicked()),
-                       purple, SLOT(sign_in_user()));
-
        RibbonServer* server = new RibbonServer();
        server->listen(QHostAddress::Any, 8888);
 
index bd0af891b5fbddea7d60e0097eccd468bc95d79f..48d44ec71e4aaac6a2ee58b1f459115f11b36b33 100644 (file)
@@ -17,10 +17,14 @@ QMAKE_CXXFLAGS += -Werror -W
 
 # Input
 HEADERS += ribbonmanager.h \
+           ribbonaccount.h \
+           ribbonaccounts.h \
            ribbonserver.h \
            ribbonsocket.h
 
 SOURCES += ribbonmanager.cpp \
+           ribbonaccount.cpp \
+           ribbonaccounts.cpp \
            ribbonserver.cpp \
            ribbonsocket.cpp \
            main.cpp
diff --git a/ribbon/ribbonaccount.cpp b/ribbon/ribbonaccount.cpp
new file mode 100644 (file)
index 0000000..6b66953
--- /dev/null
@@ -0,0 +1,23 @@
+#include "ribbonaccount.h"
+
+RibbonAccount RibbonAccount::fromString(QString s)
+{
+       RibbonAccount value;
+       int idx = s.indexOf(":");
+       value.protocol = s.left(idx).toUtf8();
+       value.username = s.mid(idx+1).toUtf8();
+       return value;
+}
+
+bool RibbonAccount::operator<(const RibbonAccount other) const
+{
+       if (protocol < other.protocol) return true;
+       if (protocol > other.protocol) return false;
+       return username < other.username;
+}
+
+QString RibbonAccount::toString()
+{
+       return QString("%1:%2").arg(QString::fromUtf8(protocol))
+               .arg(QString::fromUtf8(username));
+}
diff --git a/ribbon/ribbonaccount.h b/ribbon/ribbonaccount.h
new file mode 100644 (file)
index 0000000..ebfd2ac
--- /dev/null
@@ -0,0 +1,16 @@
+#ifndef RIBBONACCOUNT_H
+#define RIBBONACCOUNT_H
+
+#include <QByteArray>
+#include <QString>
+
+class RibbonAccount {
+public:
+       static RibbonAccount fromString(QString);
+       bool operator<(const RibbonAccount other) const;
+       QString toString();
+       QByteArray protocol;
+       QByteArray username;
+};
+
+#endif // RIBBONACCOUNT_H
diff --git a/ribbon/ribbonaccounts.cpp b/ribbon/ribbonaccounts.cpp
new file mode 100644 (file)
index 0000000..216b7c2
--- /dev/null
@@ -0,0 +1,38 @@
+#include "ribbonaccounts.h"
+
+#include <QDebug>
+
+RibbonAccounts::RibbonAccounts(RibbonManager* parent) : QObject((QObject*)parent)
+{
+       _manager = parent;
+}
+
+RibbonAccounts::~RibbonAccounts()
+{
+}
+
+void RibbonAccounts::handleExternalEvent(QString method, QVariantMap params)
+{
+       qDebug() << "called" << method << "on" << params;
+       if (method == "create_account") {
+               createAccount(params["protocol"].toByteArray(),
+                             params["username"].toByteArray(),
+                             params["password"].toByteArray());
+       }
+}
+
+void RibbonAccounts::createAccount(QByteArray protocol, QByteArray username, QByteArray password)
+{
+       PurpleAccount *account = purple_account_new(username.data(), protocol.data());
+
+       account->ui_data = _manager;
+       purple_account_set_password(account, password.data());
+       purple_accounts_add(account);
+       purple_account_set_enabled(account, UI_ID, TRUE);
+
+       // Save a handle to this account.
+       RibbonAccount r_acct;
+       r_acct.protocol = protocol;
+       r_acct.username = username;
+       accounts[r_acct] = account;
+}
diff --git a/ribbon/ribbonaccounts.h b/ribbon/ribbonaccounts.h
new file mode 100644 (file)
index 0000000..178f782
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef RIBBONACCOUNTS_H
+#define RIBBONACCOUNTS_H
+
+#include <QObject>
+#include <QString>
+#include <QVariantMap>
+#include <QList>
+
+#include <libpurple/purple.h>
+#include "ribbonmanager.h"
+#include "ribbonaccount.h"
+
+class RibbonAccounts : public QObject {
+       Q_OBJECT
+public:
+       RibbonAccounts(RibbonManager* parent = 0);
+       ~RibbonAccounts();
+public slots:
+       void handleExternalEvent(QString method, QVariantMap params);
+private:
+       RibbonManager* _manager;
+       QMap<RibbonAccount, PurpleAccount*> accounts;
+       void createAccount(QByteArray protocol, QByteArray username, QByteArray password);
+};
+
+#endif /* RIBBONACCOUNTS_H */
index 373d3e74e9f49f601e1be483186d263c22b7a451..8969060e2be19e2c7eccf62140dbee46b4745514 100644 (file)
@@ -1,4 +1,5 @@
 #include "ribbonmanager.h"
+#include "ribbonaccounts.h"
 
 #include <QDebug>
 #include <QFile>
@@ -240,6 +241,7 @@ static void connect_to_signals(void)
 
 RibbonManager::RibbonManager(QObject* parent) : QObject(parent)
 {
+       accounts = new RibbonAccounts(this);
 }
 
 RibbonManager::~RibbonManager()
@@ -294,24 +296,7 @@ void RibbonManager::handleExternalEvent(QString destination, QString method, QVa
 {
        // TODO: handle external event
        qDebug() << "TODO: do something with" << destination << "." << method << "(" << context << ")";
-}
-
-void RibbonManager::sign_in_user(void)
-{
-       QFile file("account.txt");
-       if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
-               qDebug() << "Couldn't load account information from account.txt!";
-               return;
+       if (destination == "accounts") {
+               accounts->handleExternalEvent(method, context);
        }
-       QByteArray protocol = file.readLine().trimmed();//this could be prpl-aim, prpl-yahoo, prpl-msn, prpl-icq, etc.
-       QByteArray username = file.readLine().trimmed();
-       QByteArray password = file.readLine().trimmed();
-       // Allocate and enable an account.
-       PurpleAccount *account = purple_account_new(username.data(), protocol.data());
-       // ui_data needed for upcalls
-       account->ui_data = this;
-
-       purple_account_set_password(account, password.data());
-       purple_accounts_add(account);
-       purple_account_set_enabled(account, UI_ID, TRUE);
 }
index 7d9de966afe32627c1b3bba48a69845498c205c8..be47e6adee41e9479fd6171591fbe364ca21b479 100644 (file)
@@ -5,6 +5,9 @@
 #include <QVariantMap>
 #include <libpurple/purple.h>
 
+// Forward declarations
+class RibbonAccounts;
+
 #define UI_ID "ribbon"
 
 class RibbonManager : public QObject {
@@ -24,13 +27,13 @@ public:
 
 public slots:
        void handleExternalEvent(QString destination, QString method, QVariantMap context);
-       void sign_in_user(void);
 
 signals:
        void event(QString source, QString signal, QVariantMap context);
 
 private:
        // TODO: add a map of currently-open conversations?
+       RibbonAccounts* accounts;
        Q_DISABLE_COPY(RibbonManager)
 };