diff --git a/ComunicWatcher.pro b/ComunicWatcher.pro index 27eaa7b..fafb4fb 100644 --- a/ComunicWatcher.pro +++ b/ComunicWatcher.pro @@ -18,14 +18,16 @@ SOURCES += \ apirequest.cpp \ apiresponse.cpp \ main.cpp \ - loginwindow.cpp + loginwindow.cpp \ + refreshservice.cpp HEADERS += \ accounthelper.h \ apirequest.h \ apiresponse.h \ config.h \ - loginwindow.h + loginwindow.h \ + refreshservice.h FORMS += \ loginwindow.ui diff --git a/accounthelper.cpp b/accounthelper.cpp index 55b9d88..d450432 100644 --- a/accounthelper.cpp +++ b/accounthelper.cpp @@ -1,10 +1,9 @@ #include "accounthelper.h" #include "apirequest.h" -AccountHelper::AccountHelper() -{ +#include -} +#define LOGIN_TOKEN_VALUE "login_token" void AccountHelper::LoginUser(const QString &email, const QString &password, const vLoginCallback cb) { @@ -21,6 +20,7 @@ void AccountHelper::LoginUser(const QString &email, const QString &password, con case 200: // Save login token token = res.getObject().value("tokens").toObject().value("token1").toString(); + QSettings().setValue(LOGIN_TOKEN_VALUE, token); cb(LoginResult::SUCCESS); break; @@ -40,3 +40,18 @@ void AccountHelper::LoginUser(const QString &email, const QString &password, con }); } + +bool AccountHelper::SignedIn() +{ + return QSettings().value(LOGIN_TOKEN_VALUE).isValid(); +} + +QString AccountHelper::GetLoginToken() +{ + return QSettings().value(LOGIN_TOKEN_VALUE).toString(); +} + +void AccountHelper::RemoveLoginToken() +{ + QSettings().remove(LOGIN_TOKEN_VALUE); +} diff --git a/accounthelper.h b/accounthelper.h index fc5f0f6..8c03a75 100644 --- a/accounthelper.h +++ b/accounthelper.h @@ -22,8 +22,6 @@ typedef std::function vLoginCallback; class AccountHelper { public: - AccountHelper(); - /** * Attempt to login user * @@ -31,5 +29,20 @@ public: * @param password User password */ static void LoginUser(const QString &email, const QString &password, const vLoginCallback cb); + + /** + * Check out whether a user is currently signed in or not + */ + static bool SignedIn(); + + /** + * Retrieve user login token + */ + static QString GetLoginToken(); + + /** + * Destroy the login token of a user + */ + static void RemoveLoginToken(); }; diff --git a/loginwindow.cpp b/loginwindow.cpp index 9abef70..c8c4b73 100644 --- a/loginwindow.cpp +++ b/loginwindow.cpp @@ -1,6 +1,7 @@ #include "accounthelper.h" #include "apirequest.h" #include "loginwindow.h" +#include "refreshservice.h" #include "ui_loginwindow.h" #include @@ -68,6 +69,9 @@ void LoginWindow::onResponse(LoginResult res) case SUCCESS: QMessageBox::information(this, "ok", "success"); + RefreshService::startService(); + close(); + deleteLater(); return; case BAD_PASSWORD: diff --git a/main.cpp b/main.cpp index 1476162..de533fc 100644 --- a/main.cpp +++ b/main.cpp @@ -1,19 +1,23 @@ -#include "loginwindow.h" - #include +#include "loginwindow.h" +#include "refreshservice.h" + int main(int argc, char *argv[]) { QApplication a(argc, argv); // Initialize configuration - QCoreApplication::setOrganizationName("Comuniquons"); + QCoreApplication::setOrganizationName("Communiquons"); QCoreApplication::setOrganizationDomain("communiquons.org"); QCoreApplication::setApplicationName("ComunicWatcher"); - LoginWindow w; - w.show(); + if(!AccountHelper::SignedIn()) { + (new LoginWindow())->show(); + } + else + RefreshService::startService(); return a.exec(); } diff --git a/refreshservice.cpp b/refreshservice.cpp new file mode 100644 index 0000000..cb5c393 --- /dev/null +++ b/refreshservice.cpp @@ -0,0 +1,29 @@ +#include "refreshservice.h" + +RefreshService *RefreshService::svc = nullptr; + + +void RefreshService::startService() +{ + if(svc != nullptr) + return; + + svc = new RefreshService(); +} + +void RefreshService::stopService() +{ + if(svc != nullptr) + svc->deleteLater(); + svc = nullptr; +} + +RefreshService::RefreshService() +{ + qDebug("Start refresh service"); +} + +RefreshService::~RefreshService() +{ + qDebug("Stop refresh service"); +} diff --git a/refreshservice.h b/refreshservice.h new file mode 100644 index 0000000..730b451 --- /dev/null +++ b/refreshservice.h @@ -0,0 +1,25 @@ +/** + * Refresh service + * + * @author Pierre Hubert + */ + +#pragma once + +#include + +class RefreshService : public QObject +{ + Q_OBJECT + +public: + static void startService(); + static void stopService(); + +private: + RefreshService(); + ~RefreshService(); + + // Class members + static RefreshService *svc; +};