From: Drew Fisher Date: Thu, 24 Apr 2014 07:50:40 +0000 (-0700) Subject: ribbon: Extract account management into a separate class X-Git-Url: https://git.zarvox.org/?a=commitdiff_plain;h=976c9b29dd140bd825208bce7457431c10c93ee0;p=imoo.git ribbon: Extract account management into a separate class --- diff --git a/ribbon/main.cpp b/ribbon/main.cpp index 3103aff..28a8886 100644 --- a/ribbon/main.cpp +++ b/ribbon/main.cpp @@ -3,7 +3,6 @@ #include #include -#include #include @@ -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); diff --git a/ribbon/ribbon.pro b/ribbon/ribbon.pro index bd0af89..48d44ec 100644 --- a/ribbon/ribbon.pro +++ b/ribbon/ribbon.pro @@ -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 index 0000000..6b66953 --- /dev/null +++ b/ribbon/ribbonaccount.cpp @@ -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 index 0000000..ebfd2ac --- /dev/null +++ b/ribbon/ribbonaccount.h @@ -0,0 +1,16 @@ +#ifndef RIBBONACCOUNT_H +#define RIBBONACCOUNT_H + +#include +#include + +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 index 0000000..216b7c2 --- /dev/null +++ b/ribbon/ribbonaccounts.cpp @@ -0,0 +1,38 @@ +#include "ribbonaccounts.h" + +#include + +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 index 0000000..178f782 --- /dev/null +++ b/ribbon/ribbonaccounts.h @@ -0,0 +1,26 @@ +#ifndef RIBBONACCOUNTS_H +#define RIBBONACCOUNTS_H + +#include +#include +#include +#include + +#include +#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 accounts; + void createAccount(QByteArray protocol, QByteArray username, QByteArray password); +}; + +#endif /* RIBBONACCOUNTS_H */ diff --git a/ribbon/ribbonmanager.cpp b/ribbon/ribbonmanager.cpp index 373d3e7..8969060 100644 --- a/ribbon/ribbonmanager.cpp +++ b/ribbon/ribbonmanager.cpp @@ -1,4 +1,5 @@ #include "ribbonmanager.h" +#include "ribbonaccounts.h" #include #include @@ -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); } diff --git a/ribbon/ribbonmanager.h b/ribbon/ribbonmanager.h index 7d9de96..be47e6a 100644 --- a/ribbon/ribbonmanager.h +++ b/ribbon/ribbonmanager.h @@ -5,6 +5,9 @@ #include #include +// 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) };