From 1a3169bc16343a2e8c0cf8b4b58ea4aa9065db98 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 29 Nov 2018 15:53:54 +0100 Subject: [PATCH] Can store login tokens. --- ComunicMessages.pro | 13 ++++++++--- config.h | 12 ++++++++++ data/accountloginrequest.h | 1 + data/accountlogintokens.cpp | 31 +++++++++++++++++++++++++ data/accountlogintokens.h | 36 +++++++++++++++++++++++++++++ helpers/accounthelper.cpp | 18 ++++++++++++++- helpers/configurationhelper.cpp | 34 +++++++++++++++++++++++++++ helpers/configurationhelper.h | 41 +++++++++++++++++++++++++++++++++ main.cpp | 13 +++++++++-- widgets/loginwidget.cpp | 9 ++++++++ widgets/mainwindow.cpp | 14 +++++++++++ widgets/mainwindow.h | 22 ++++++++++++++++++ widgets/mainwindow.ui | 31 +++++++++++++++++++++++++ 13 files changed, 269 insertions(+), 6 deletions(-) create mode 100644 data/accountlogintokens.cpp create mode 100644 data/accountlogintokens.h create mode 100644 helpers/configurationhelper.cpp create mode 100644 helpers/configurationhelper.h create mode 100644 widgets/mainwindow.cpp create mode 100644 widgets/mainwindow.h create mode 100644 widgets/mainwindow.ui diff --git a/ComunicMessages.pro b/ComunicMessages.pro index 21f078f..577c525 100644 --- a/ComunicMessages.pro +++ b/ComunicMessages.pro @@ -10,7 +10,10 @@ SOURCES += \ data/apirequestparameter.cpp \ helpers/apihelper.cpp \ utils/jsonutils.cpp \ - data/apirequestslist.cpp + data/apirequestslist.cpp \ + helpers/configurationhelper.cpp \ + data/accountlogintokens.cpp \ + widgets/mainwindow.cpp HEADERS += \ helpers/accounthelper.h \ @@ -22,7 +25,11 @@ HEADERS += \ data/apirequestparameter.h \ helpers/apihelper.h \ utils/jsonutils.h \ - data/apirequestslist.h + data/apirequestslist.h \ + helpers/configurationhelper.h \ + data/accountlogintokens.h \ + widgets/mainwindow.h FORMS += \ - widgets/loginwidget.ui + widgets/loginwidget.ui \ + widgets/mainwindow.ui diff --git a/config.h b/config.h index 5bb3e73..63d7ccd 100644 --- a/config.h +++ b/config.h @@ -6,6 +6,13 @@ #ifndef CONFIG_H #define CONFIG_H +/** + * Application information + */ +#define ORGANIZATION_NAME "Communiquons" +#define ORGANIZATION_DOMAIN "communiquons.org" +#define APPLICATION_NAME "ComunicMessages" + /** * API credentials */ @@ -14,5 +21,10 @@ #define API_SERVICE_TOKEN "cWHlmMS5A1" +/** + * Settings information + */ +#define SETTINGS_ACCOUNT_LOGIN_TOKEN_1 "account_login_token_1" +#define SETTINGS_ACCOUNT_LOGIN_TOKEN_2 "account_login_token_2" #endif // CONFIG_H diff --git a/data/accountloginrequest.h b/data/accountloginrequest.h index 4590212..f5dbdb2 100644 --- a/data/accountloginrequest.h +++ b/data/accountloginrequest.h @@ -17,6 +17,7 @@ enum LoginResult { INVALID_CREDENTIALS, TOO_MANY_REQUEST, NETWORK_ERROR, + INVALID_SERVER_RESPONSE, OTHER_ERROR }; diff --git a/data/accountlogintokens.cpp b/data/accountlogintokens.cpp new file mode 100644 index 0000000..e6397b6 --- /dev/null +++ b/data/accountlogintokens.cpp @@ -0,0 +1,31 @@ +#include "accountlogintokens.h" + +AccountLoginTokens::AccountLoginTokens() +{ + +} + +bool AccountLoginTokens::isValid() +{ + return token1().length() > 0 && token2().length() > 0; +} + +QString AccountLoginTokens::token1() const +{ + return mToken1; +} + +void AccountLoginTokens::setToken1(const QString &token1) +{ + mToken1 = token1; +} + +QString AccountLoginTokens::token2() const +{ + return mToken2; +} + +void AccountLoginTokens::setToken2(const QString &token2) +{ + mToken2 = token2; +} diff --git a/data/accountlogintokens.h b/data/accountlogintokens.h new file mode 100644 index 0000000..6346978 --- /dev/null +++ b/data/accountlogintokens.h @@ -0,0 +1,36 @@ +/** + * Account Login tokens + * + * @author Pierre HUBERT + */ + +#ifndef ACCOUNTLOGINTOKENS_H +#define ACCOUNTLOGINTOKENS_H + +#include + +class AccountLoginTokens +{ +public: + AccountLoginTokens(); + + /** + * Check if the tokens present in this object + * are valid or not + * + * @return TRUE if the tokens are valid / FALSE else + */ + bool isValid(); + + QString token1() const; + void setToken1(const QString &token1); + + QString token2() const; + void setToken2(const QString &token2); + +private: + QString mToken1; + QString mToken2; +}; + +#endif // ACCOUNTLOGINTOKENS_H diff --git a/helpers/accounthelper.cpp b/helpers/accounthelper.cpp index 7018775..77058af 100644 --- a/helpers/accounthelper.cpp +++ b/helpers/accounthelper.cpp @@ -1,6 +1,9 @@ +#include #include "accounthelper.h" +#include "configurationhelper.h" #include "../data/apirequest.h" +#include "../data/accountlogintokens.h" #include "../helpers/apihelper.h" AccountHelper::AccountHelper(QObject *parent) : QObject(parent) @@ -10,7 +13,7 @@ AccountHelper::AccountHelper(QObject *parent) : QObject(parent) bool AccountHelper::signedIn() { - return false; //TODO : implement + return ConfigurationHelper().getAccountTokens().isValid(); } void AccountHelper::login(const AccountLoginRequest &info) @@ -53,7 +56,20 @@ void AccountHelper::requestLoginResult(const QJsonDocument &document) //Delete API request qobject_cast(sender())->deleteLater(); + //Intend to parse server response + QJsonObject object = document.object().value("tokens").toObject(); + if(object.isEmpty()){ + qWarning("objects 'tokens' of server response is empty!"); + emit loginResult(LoginResult::INVALID_SERVER_RESPONSE); + return; + } + + //Intend to parse get login responses + AccountLoginTokens tokens; + tokens.setToken1(object.value("token1").toString()); + tokens.setToken2(object.value("token2").toString()); + ConfigurationHelper().setAccountTokens(tokens); //Success emit loginResult(LoginResult::LOGIN_SUCCESS); diff --git a/helpers/configurationhelper.cpp b/helpers/configurationhelper.cpp new file mode 100644 index 0000000..117c609 --- /dev/null +++ b/helpers/configurationhelper.cpp @@ -0,0 +1,34 @@ +#include + +#include "configurationhelper.h" +#include "../config.h" + +ConfigurationHelper::ConfigurationHelper() +{ + mSettings = new QSettings(); +} + +ConfigurationHelper::~ConfigurationHelper() +{ + mSettings->deleteLater(); +} + +AccountLoginTokens ConfigurationHelper::getAccountTokens() +{ + //Check if we have both of the tokens + if(!mSettings->value(SETTINGS_ACCOUNT_LOGIN_TOKEN_1).isValid() || + !mSettings->value(SETTINGS_ACCOUNT_LOGIN_TOKEN_1).isValid()) + return AccountLoginTokens(); //Return invalid object : not all settings available + + //Parse and return account login tokens + AccountLoginTokens tokens; + tokens.setToken1(mSettings->value(SETTINGS_ACCOUNT_LOGIN_TOKEN_1).toString()); + tokens.setToken2(mSettings->value(SETTINGS_ACCOUNT_LOGIN_TOKEN_2).toString()); + return tokens; +} + +void ConfigurationHelper::setAccountTokens(const AccountLoginTokens &tokens) +{ + mSettings->setValue(SETTINGS_ACCOUNT_LOGIN_TOKEN_1, tokens.token1()); + mSettings->setValue(SETTINGS_ACCOUNT_LOGIN_TOKEN_2, tokens.token2()); +} diff --git a/helpers/configurationhelper.h b/helpers/configurationhelper.h new file mode 100644 index 0000000..cc08ada --- /dev/null +++ b/helpers/configurationhelper.h @@ -0,0 +1,41 @@ +/** + * Configuration helper + * + * @author Pierre HUBERT + */ + +#ifndef CONFIGURATIONHELPER_H +#define CONFIGURATIONHELPER_H + +#include + +#include "../data/accountlogintokens.h" + +class QSettings; + +class ConfigurationHelper +{ +public: + ConfigurationHelper(); + ~ConfigurationHelper(); + + /** + * Get the login tokens associated with this account + * + * @return The login tokens associated with this account / + * invalid object if none found + */ + AccountLoginTokens getAccountTokens(); + + /** + * Set the login tokens associated with the current account + * + * @param tokens The tokens to set + */ + void setAccountTokens(const AccountLoginTokens &tokens); + +private: + QSettings *mSettings; +}; + +#endif // CONFIGURATIONHELPER_H diff --git a/main.cpp b/main.cpp index b785638..dfc1a6c 100644 --- a/main.cpp +++ b/main.cpp @@ -6,13 +6,22 @@ #include "helpers/accounthelper.h" #include "widgets/loginwidget.h" +#include "widgets/mainwindow.h" +#include "config.h" int main(int argc, char** argv){ QApplication app(argc, argv); + //Define some basic values + QCoreApplication::setOrganizationName(ORGANIZATION_NAME); + QCoreApplication::setOrganizationDomain(ORGANIZATION_DOMAIN); + QCoreApplication::setApplicationName(APPLICATION_NAME); + //Determine whether user is signed in or not - if(AccountHelper().signedIn()) - qFatal("Can not handle signed in users yet!"); + if(AccountHelper().signedIn()){ + MainWindow *mainWindow = new MainWindow; + mainWindow->show(); + } else { LoginWidget *widget = new LoginWidget(); widget->show(); diff --git a/widgets/loginwidget.cpp b/widgets/loginwidget.cpp index 69184dd..88122e7 100644 --- a/widgets/loginwidget.cpp +++ b/widgets/loginwidget.cpp @@ -2,6 +2,7 @@ #include "loginwidget.h" #include "ui_loginwidget.h" +#include "mainwindow.h" #include "../utils/accountutils.h" #include "../helpers/accounthelper.h" @@ -25,6 +26,10 @@ void LoginWidget::loginResult(LoginResult result) if(result == LoginResult::LOGIN_SUCCESS){ qDebug("User successfully signed in."); + //Open the window + MainWindow *mainWindow = new MainWindow(); + mainWindow->show(); + close(); return; } @@ -46,6 +51,10 @@ void LoginWidget::loginResult(LoginResult result) showError(tr("Too many login attempts from your computer. Please try again later...")); break; + case LoginResult::INVALID_SERVER_RESPONSE: + showError(tr("Could not understand server response!")); + break; + case LoginResult::OTHER_ERROR: default: showError(tr("Login attempt did not succeed.")); diff --git a/widgets/mainwindow.cpp b/widgets/mainwindow.cpp new file mode 100644 index 0000000..49d64fc --- /dev/null +++ b/widgets/mainwindow.cpp @@ -0,0 +1,14 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} diff --git a/widgets/mainwindow.h b/widgets/mainwindow.h new file mode 100644 index 0000000..9353441 --- /dev/null +++ b/widgets/mainwindow.h @@ -0,0 +1,22 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/widgets/mainwindow.ui b/widgets/mainwindow.ui new file mode 100644 index 0000000..6545006 --- /dev/null +++ b/widgets/mainwindow.ui @@ -0,0 +1,31 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + ComunicMessages + + + + + + 0 + 0 + 800 + 22 + + + + + + + +