diff --git a/core/account.cpp b/core/account.cpp index e26e702..9fd614b 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -2,9 +2,11 @@ using namespace Core; -Account::Account(const QString& p_jid, const QString& p_password, QObject* parent): +Account::Account(const QString& p_login, const QString& p_server, const QString& p_password, const QString& p_name, QObject* parent): QObject(parent), - jid(p_jid), + name(p_name), + login(p_login), + server(p_server), password(p_password), client() { diff --git a/core/account.h b/core/account.h index 2766d87..c6af16b 100644 --- a/core/account.h +++ b/core/account.h @@ -11,11 +11,13 @@ namespace Core class Account : public QObject { public: - Account(const QString& p_jid, const QString& p_password, QObject* parent = 0); + Account(const QString& p_login, const QString& p_server, const QString& p_password, const QString& p_name, QObject* parent = 0); ~Account(); private: - QString jid; + QString name; + QString login; + QString server; QString password; QXmppClient client; }; diff --git a/core/squawk.cpp b/core/squawk.cpp index e681769..667c885 100644 --- a/core/squawk.cpp +++ b/core/squawk.cpp @@ -1,15 +1,47 @@ #include "squawk.h" +#include -using namespace Core; - -Squawk::Squawk(QObject* parent): +Core::Squawk::Squawk(QObject* parent): QObject(parent), accounts() { } -Squawk::~Squawk() +Core::Squawk::~Squawk() { - + Accounts::const_iterator itr = accounts.begin(); + Accounts::const_iterator end = accounts.end(); + for (; itr != end; ++itr) { + (*itr)->deleteLater(); + } +} + +void Core::Squawk::start() +{ +} + +void Core::Squawk::newAccountRequest(const QMap& map) +{ + QString name = map.value("name").toString(); + QString login = map.value("login").toString(); + QString server = map.value("server").toString(); + QString password = map.value("password").toString(); + + addAccount(login, server, password, name); +} + +void Core::Squawk::addAccount(const QString& login, const QString& server, const QString& password, const QString& name) +{ + Account* acc = new Account(login, server, password, name); + accounts.push_back(acc); + + QMap map = { + {"login", login}, + {"server", server}, + {"name", name}, + {"password", password}, + {"state", 0} + }; + emit newAccount(map); } diff --git a/core/squawk.h b/core/squawk.h index cd56bf2..76c5d6e 100644 --- a/core/squawk.h +++ b/core/squawk.h @@ -2,6 +2,9 @@ #define CORE_SQUAWK_H #include +#include +#include +#include #include #include "account.h" @@ -16,10 +19,20 @@ public: Squawk(QObject* parent = 0); ~Squawk(); +signals: + void newAccount(const QMap&); + +public slots: + void start(); + void newAccountRequest(const QMap& map); + private: - typedef std::deque Accounts; + typedef std::deque Accounts; Accounts accounts; + +private: + void addAccount(const QString& login, const QString& server, const QString& password, const QString& name); }; } diff --git a/main.cpp b/main.cpp index 8b905c4..ff710d7 100644 --- a/main.cpp +++ b/main.cpp @@ -15,6 +15,12 @@ int main(int argc, char *argv[]) squawk->moveToThread(coreThread); QObject::connect(coreThread, SIGNAL(finished()), squawk, SLOT(deleteLater())); + QObject::connect(coreThread, SIGNAL(started()), squawk, SLOT(start())); + + QObject::connect(&w, SIGNAL(newAccountRequest(const QMap&)), squawk, SLOT(newAccountRequest(const QMap&))); + + QObject::connect(squawk, SIGNAL(newAccount(const QMap&)), &w, SLOT(newAccount(const QMap&))); + //QObject::connect(this, &Controller::operate, worker, &Worker::doWork); //QObject::connect(worker, &Worker::resultReady, this, &Controller::handleResults); coreThread->start(); diff --git a/ui/CMakeLists.txt b/ui/CMakeLists.txt index 4dccfc3..7374c75 100644 --- a/ui/CMakeLists.txt +++ b/ui/CMakeLists.txt @@ -12,6 +12,8 @@ find_package(Qt5Widgets CONFIG REQUIRED) set(squawkUI_SRC squawk.cpp accounts.cpp + account.cpp + models/accounts.cpp ) # Tell CMake to create the helloworld executable diff --git a/ui/account.cpp b/ui/account.cpp new file mode 100644 index 0000000..16a4548 --- /dev/null +++ b/ui/account.cpp @@ -0,0 +1,23 @@ +#include "account.h" +#include "ui_account.h" + +Account::Account() + : m_ui ( new Ui::Account ) +{ + m_ui->setupUi ( this ); +} + +Account::~Account() +{ +} + +QMap Account::value() const +{ + QMap map; + map["login"] = m_ui->login->text(); + map["password"] = m_ui->password->text(); + map["server"] = m_ui->server->text(); + map["name"] = m_ui->name->text(); + + return map; +} diff --git a/ui/account.h b/ui/account.h new file mode 100644 index 0000000..ee8850d --- /dev/null +++ b/ui/account.h @@ -0,0 +1,29 @@ +#ifndef ACCOUNT_H +#define ACCOUNT_H + +#include +#include +#include +#include +#include + +namespace Ui +{ +class Account; +} + +class Account : public QDialog +{ + Q_OBJECT + +public: + Account(); + ~Account(); + + QMap value() const; + +private: + QScopedPointer m_ui; +}; + +#endif // ACCOUNT_H diff --git a/ui/account.ui b/ui/account.ui new file mode 100644 index 0000000..5dc7f9a --- /dev/null +++ b/ui/account.ui @@ -0,0 +1,154 @@ + + + Account + + + + 0 + 0 + 395 + 272 + + + + Account + + + + 6 + + + 6 + + + 6 + + + 6 + + + + + 6 + + + 6 + + + + + Your account login + + + + + + + Server + + + + + + + A server address of your account. Like 404.city or macaw.me + + + + + + + Login + + + + + + + Password + + + + + + + Password of your account + + + + + + + + + QLineEdit::Password + + + false + + + + + + + Name + + + + + + + Just a name how would you call this account, doesn't affect anything + + + + + + + + + Qt::Horizontal + + + QDialogButtonBox::Cancel|QDialogButtonBox::Ok + + + + + + + + + buttonBox + accepted() + Account + accept() + + + 20 + 20 + + + 20 + 20 + + + + + buttonBox + rejected() + Account + reject() + + + 20 + 20 + + + 20 + 20 + + + + + diff --git a/ui/accounts.cpp b/ui/accounts.cpp index d348f98..c6d9380 100644 --- a/ui/accounts.cpp +++ b/ui/accounts.cpp @@ -1,10 +1,43 @@ #include "accounts.h" #include "ui_accounts.h" +#include + Accounts::Accounts(QWidget *parent) : - m_ui(new Ui::Accounts) + m_ui(new Ui::Accounts), + tableModel() { m_ui->setupUi(this); + + connect(m_ui->addButton, SIGNAL(clicked(bool)), this, SLOT(onAddButton(bool))); + m_ui->tableView->setModel(&tableModel); } Accounts::~Accounts() = default; + +void Accounts::onAddButton(bool clicked) +{ + Account* acc = new Account(); + connect(acc, SIGNAL(accepted()), this, SLOT(onAccountAccepted())); + connect(acc, SIGNAL(rejected()), this, SLOT(onAccountRejected())); + acc->exec(); +} + +void Accounts::onAccountAccepted() +{ + Account* acc = static_cast(sender()); + QMap map = acc->value(); + + emit newAccount(map); +} + +void Accounts::onAccountRejected() +{ + Account* acc = static_cast(sender()); + acc->deleteLater(); +} + +void Accounts::addAccount(const QMap& map) +{ + tableModel.addAccount(map); +} diff --git a/ui/accounts.h b/ui/accounts.h index 95dc125..61fbe03 100644 --- a/ui/accounts.h +++ b/ui/accounts.h @@ -4,14 +4,14 @@ #include #include +#include "account.h" +#include "models/accounts.h" + namespace Ui { class Accounts; } -/** - * @todo write docs - */ class Accounts : public QWidget { Q_OBJECT @@ -19,8 +19,19 @@ public: explicit Accounts(QWidget *parent = nullptr); ~Accounts() override; + void addAccount(const QMap&); + +signals: + void newAccount(const QMap&); + +private slots: + void onAddButton(bool clicked = 0); + void onAccountAccepted(); + void onAccountRejected(); + private: QScopedPointer m_ui; + Models::Accounts tableModel; }; #endif // ACCOUNTS_H diff --git a/ui/accounts.ui b/ui/accounts.ui index 3440a56..483b0d7 100644 --- a/ui/accounts.ui +++ b/ui/accounts.ui @@ -6,8 +6,8 @@ 0 0 - 768 - 235 + 539 + 249 @@ -15,20 +15,71 @@ - 0 + 6 - 0 + 6 - 0 + 6 - 0 + 6 - 0 + 6 + + + + Qt::Horizontal + + + + + + + false + + + Delete + + + + + + + + 400 + 0 + + + + true + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + + false + + + Qt::SolidLine + + + true + + + false + + + false + + + @@ -38,12 +89,25 @@ + + false + Edit - + + + + false + + + Change password + + + + Qt::Vertical @@ -56,16 +120,16 @@ - - + + + + false + - Delete + Connect - - - diff --git a/ui/models/accounts.cpp b/ui/models/accounts.cpp new file mode 100644 index 0000000..5b976e3 --- /dev/null +++ b/ui/models/accounts.cpp @@ -0,0 +1,77 @@ +#include "accounts.h" + +std::deque Models::Accounts::columns = { + "name", + "server", + "state" +}; + +Models::Accounts::Accounts(QObject* parent): + QAbstractTableModel(parent), + accs() +{ + +} + +Models::Accounts::~Accounts() +{ + +} + +QVariant Models::Accounts::data ( const QModelIndex& index, int role ) const +{ + QVariant answer; + switch (role) { + case Qt::DisplayRole: { + const Account& acc = accs[index.row()]; + switch (index.column()) { + case 0: + answer = acc.name; + break; + case 1: + answer = acc.server; + break; + case 2: + answer = acc.state; + break; + } + } + break; + default: + break; + } + + return answer; +} + +int Models::Accounts::columnCount ( const QModelIndex& parent ) const +{ + return columns.size(); +} + +int Models::Accounts::rowCount ( const QModelIndex& parent ) const +{ + return accs.size(); +} + +QVariant Models::Accounts::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role == Qt::DisplayRole && orientation == Qt::Horizontal) { + return columns[section]; + } + return QVariant(); +} + + +void Models::Accounts::addAccount(const QMap& map) +{ + beginInsertRows(QModelIndex(), accs.size(), accs.size()); + accs.push_back({ + map.value("name").toString(), + map.value("server").toString(), + map.value("login").toString(), + map.value("password").toString(), + map.value("state").toInt() + }); + endInsertRows(); +} diff --git a/ui/models/accounts.h b/ui/models/accounts.h new file mode 100644 index 0000000..1637b2e --- /dev/null +++ b/ui/models/accounts.h @@ -0,0 +1,43 @@ +#ifndef MODELS_ACCOUNTS_H +#define MODELS_ACCOUNTS_H + +#include +#include + +namespace Models +{ + struct Account; + +class Accounts : public QAbstractTableModel +{ + Q_OBJECT +public: + Accounts(QObject* parent = 0); + ~Accounts(); + + void addAccount(const QMap& map); + + + QVariant data ( const QModelIndex& index, int role ) const override; + int columnCount ( const QModelIndex& parent ) const override; + int rowCount ( const QModelIndex& parent ) const override; + QVariant headerData(int section, Qt::Orientation orientation, int role) const override; + +private: + std::deque accs; + + static std::deque columns; + +}; + +struct Account { + QString name; + QString server; + QString login; + QString password; + int state; +}; + +} + +#endif // MODELS_ACCOUNT_H diff --git a/ui/squawk.cpp b/ui/squawk.cpp index 0f70fce..a32f35c 100644 --- a/ui/squawk.cpp +++ b/ui/squawk.cpp @@ -4,7 +4,8 @@ Squawk::Squawk(QWidget *parent) : QMainWindow(parent), m_ui(new Ui::Squawk), - accounts(0) + accounts(0), + accountsCache() { m_ui->setupUi(this); @@ -21,9 +22,20 @@ void Squawk::onAccounts() accounts = new Accounts(this); accounts->setAttribute(Qt::WA_DeleteOnClose); connect(accounts, SIGNAL(destroyed(QObject*)), this, SLOT(onAccountsClosed(QObject*))); + connect(accounts, SIGNAL(newAccount(const QMap&)), this, SIGNAL(newAccountRequest(const QMap&))); + + AC::const_iterator itr = accountsCache.begin(); + AC::const_iterator end = accountsCache.end(); + + for (; itr != end; ++itr) { + accounts->addAccount(*itr); + } + accounts->show(); } else { - accounts->focusWidget(); + accounts->show(); + accounts->raise(); + accounts->activateWindow(); } } @@ -41,3 +53,11 @@ void Squawk::onAccountsClosed(QObject* parent) { accounts = 0; } + +void Squawk::newAccount(const QMap& account) +{ + accountsCache.push_back(account); + if (accounts != 0) { + accounts->addAccount(account); + } +} diff --git a/ui/squawk.h b/ui/squawk.h index 41b9d3f..72c1935 100644 --- a/ui/squawk.h +++ b/ui/squawk.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "accounts.h" @@ -18,11 +19,19 @@ class Squawk : public QMainWindow public: explicit Squawk(QWidget *parent = nullptr); ~Squawk() override; - + +signals: + void newAccountRequest(const QMap&); + +public slots: + void newAccount(const QMap& account); + private: + typedef std::deque> AC; QScopedPointer m_ui; Accounts* accounts; + AC accountsCache; protected: void closeEvent(QCloseEvent * event) override;