Start service

This commit is contained in:
Pierre HUBERT 2020-06-13 10:19:49 +02:00
parent b9b509aca1
commit 7244068ad5
7 changed files with 104 additions and 12 deletions

View File

@ -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

View File

@ -1,10 +1,9 @@
#include "accounthelper.h"
#include "apirequest.h"
AccountHelper::AccountHelper()
{
#include <QSettings>
}
#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);
}

View File

@ -22,8 +22,6 @@ typedef std::function<void (LoginResult)> 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();
};

View File

@ -1,6 +1,7 @@
#include "accounthelper.h"
#include "apirequest.h"
#include "loginwindow.h"
#include "refreshservice.h"
#include "ui_loginwindow.h"
#include <QMessageBox>
@ -68,6 +69,9 @@ void LoginWindow::onResponse(LoginResult res)
case SUCCESS:
QMessageBox::information(this, "ok", "success");
RefreshService::startService();
close();
deleteLater();
return;
case BAD_PASSWORD:

View File

@ -1,19 +1,23 @@
#include "loginwindow.h"
#include <QApplication>
#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();
}

29
refreshservice.cpp Normal file
View File

@ -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");
}

25
refreshservice.h Normal file
View File

@ -0,0 +1,25 @@
/**
* Refresh service
*
* @author Pierre Hubert
*/
#pragma once
#include <QObject>
class RefreshService : public QObject
{
Q_OBJECT
public:
static void startService();
static void stopService();
private:
RefreshService();
~RefreshService();
// Class members
static RefreshService *svc;
};