Can get current user ID

This commit is contained in:
Pierre HUBERT 2018-11-29 17:35:27 +01:00
parent 2396047204
commit d405b0e8e1
8 changed files with 193 additions and 24 deletions

View File

@ -14,7 +14,8 @@ SOURCES += \
helpers/configurationhelper.cpp \ helpers/configurationhelper.cpp \
data/accountlogintokens.cpp \ data/accountlogintokens.cpp \
widgets/mainwindow.cpp \ widgets/mainwindow.cpp \
widgets/aboutthisappdialog.cpp widgets/aboutthisappdialog.cpp \
controllers/initcontroller.cpp
HEADERS += \ HEADERS += \
helpers/accounthelper.h \ helpers/accounthelper.h \
@ -30,7 +31,8 @@ HEADERS += \
helpers/configurationhelper.h \ helpers/configurationhelper.h \
data/accountlogintokens.h \ data/accountlogintokens.h \
widgets/mainwindow.h \ widgets/mainwindow.h \
widgets/aboutthisappdialog.h widgets/aboutthisappdialog.h \
controllers/initcontroller.h
FORMS += \ FORMS += \
widgets/loginwidget.ui \ widgets/loginwidget.ui \

View File

@ -0,0 +1,64 @@
#include <QCoreApplication>
#include <QProgressDialog>
#include <QMessageBox>
#include "../helpers/accounthelper.h"
#include "widgets/loginwidget.h"
#include "widgets/mainwindow.h"
#include "initcontroller.h"
#include "config.h"
InitController::InitController() : QObject()
{
mAccountHelper = new AccountHelper;
connect(mAccountHelper, &AccountHelper::refreshCurrentUserIDResult, this, &InitController::getUserIDCallback);
}
InitController::~InitController()
{
mAccountHelper->deleteLater();
if(mProgressdialog != nullptr)
mProgressdialog->deleteLater();
}
void InitController::init()
{
//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(!mAccountHelper->signedIn()) {
LoginWidget *widget = new LoginWidget();
widget->show();
return;
}
//Display startup splash screen
mProgressdialog = new QProgressDialog(QObject::tr("Starting up..."), QString(), 0, 3);
mProgressdialog->show();
//First, we need to refresh current user ID
mProgressdialog->setLabelText(tr("Get current user ID..."));
mProgressdialog->setValue(1);
mAccountHelper->refreshCurrentUserID();
}
void InitController::getUserIDCallback(bool success)
{
if(!success){
QMessageBox::warning(mProgressdialog, tr("Error"), tr("Could not get current user ID! Please check your internet connection..."));
deleteLater();
return;
}
mProgressdialog->setLabelText(tr("Open main window..."));
mProgressdialog->setValue(2);
(new MainWindow())->show();
mProgressdialog->hide();
deleteLater();
}

View File

@ -0,0 +1,41 @@
/**
* Initialization controller
*
* Used to initialize the application
*
* @author Pierre HUBERT
*/
#ifndef INITCONTROLLER_H
#define INITCONTROLLER_H
#include <QObject>
class QProgressDialog;
class AccountHelper;
class InitController : public QObject
{
Q_OBJECT
public:
InitController();
~InitController();
/**
* Initialize the application. This operation can be
* run several times
*/
void init();
private slots:
void getUserIDCallback(bool success);
private:
AccountHelper *mAccountHelper = nullptr;
QProgressDialog *mProgressdialog = nullptr;
};
#endif // INITCONTROLLER_H

View File

@ -6,6 +6,9 @@
#include "../data/accountlogintokens.h" #include "../data/accountlogintokens.h"
#include "../helpers/apihelper.h" #include "../helpers/apihelper.h"
//Current user ID
static int mUserID = -1;
AccountHelper::AccountHelper(QObject *parent) : QObject(parent) AccountHelper::AccountHelper(QObject *parent) : QObject(parent)
{ {
mAPIHelper = new APIHelper; mAPIHelper = new APIHelper;
@ -39,6 +42,15 @@ void AccountHelper::login(const AccountLoginRequest &info)
} }
void AccountHelper::refreshCurrentUserID()
{
APIRequest *request = new APIRequest;
request->setURI("user/getCurrentUserID");
connect(request, &APIRequest::error, this, &AccountHelper::getUserIdCallbackError);
connect(request, &APIRequest::success, this, &AccountHelper::getUserIdCallbackSuccess);
mAPIHelper->execute(request);
}
void AccountHelper::loginError(int code) void AccountHelper::loginError(int code)
{ {
//Delete API request //Delete API request
@ -80,3 +92,35 @@ void AccountHelper::requestLoginResult(const QJsonDocument &document)
//Success //Success
emit loginResult(LoginResult::LOGIN_SUCCESS); emit loginResult(LoginResult::LOGIN_SUCCESS);
} }
void AccountHelper::getUserIdCallbackError()
{
qobject_cast<APIRequest *>(sender())->deleteLater();
emit refreshCurrentUserIDResult(false);
}
void AccountHelper::getUserIdCallbackSuccess(const QJsonDocument &document)
{
qobject_cast<APIRequest *>(sender())->deleteLater();
int userID = document.object().value("userID").toInt(-1);
if(userID < 0){
qWarning("Could not get user ID!");
emit refreshCurrentUserIDResult(false);
return;
}
mUserID = userID;
emit refreshCurrentUserIDResult(true);
}
int AccountHelper::getUserID()
{
if(mUserID < 1)
qWarning("An attempt to get user ID has been made while user ID has not been refreshed yet!");
return mUserID;
}

View File

@ -39,6 +39,19 @@ public:
*/ */
void login(const AccountLoginRequest &info); void login(const AccountLoginRequest &info);
/**
* Refresh current user ID
*/
void refreshCurrentUserID();
/**
* Get current cached user ID
*
* @return The ID of the user ID / note : invalid value if user ID
* is not available yet
*/
static int getUserID();
signals: signals:
/** /**
@ -48,6 +61,13 @@ signals:
*/ */
void loginResult(LoginResult result); void loginResult(LoginResult result);
/**
* Refresh current user ID result
*
* @param success TRUE in case of success / FALSE else
*/
void refreshCurrentUserIDResult(bool success);
public slots: public slots:
private slots: private slots:
@ -56,6 +76,10 @@ private slots:
void loginError(int code); void loginError(int code);
void requestLoginResult(const QJsonDocument &document); void requestLoginResult(const QJsonDocument &document);
//Get current user id callbacks
void getUserIdCallbackError();
void getUserIdCallbackSuccess(const QJsonDocument &document);
private: private:
APIHelper *mAPIHelper; APIHelper *mAPIHelper;
}; };

View File

@ -2,6 +2,9 @@
#include <QJsonDocument> #include <QJsonDocument>
#include "apihelper.h" #include "apihelper.h"
#include "accounthelper.h"
#include "configurationhelper.h"
#include "../data/accountlogintokens.h"
#include "../config.h" #include "../config.h"
APIHelper::APIHelper(QObject *parent) : QObject(parent) APIHelper::APIHelper(QObject *parent) : QObject(parent)
@ -20,6 +23,13 @@ void APIHelper::execute(APIRequest *request)
request->addString("serviceName", API_SERVICE_NAME); request->addString("serviceName", API_SERVICE_NAME);
request->addString("serviceToken", API_SERVICE_TOKEN); request->addString("serviceToken", API_SERVICE_TOKEN);
//Add account tokens if available
if(AccountHelper().signedIn()){
AccountLoginTokens tokens = ConfigurationHelper().getAccountTokens();
request->addString("userToken1", tokens.token1());
request->addString("userToken2", tokens.token2());
}
//Prepare request //Prepare request
//See this SO question to learn more : https://stackoverflow.com/questions/2599423 //See this SO question to learn more : https://stackoverflow.com/questions/2599423
QUrlQuery queryData; QUrlQuery queryData;

View File

@ -4,28 +4,13 @@
#include <QApplication> #include <QApplication>
#include "helpers/accounthelper.h" #include "controllers/initcontroller.h"
#include "widgets/loginwidget.h"
#include "widgets/mainwindow.h"
#include "config.h"
int main(int argc, char** argv){ int main(int argc, char** argv){
QApplication app(argc, argv); QApplication app(argc, argv);
//Define some basic values //Initialize app
QCoreApplication::setOrganizationName(ORGANIZATION_NAME); (new InitController())->init();
QCoreApplication::setOrganizationDomain(ORGANIZATION_DOMAIN);
QCoreApplication::setApplicationName(APPLICATION_NAME);
//Determine whether user is signed in or not
if(AccountHelper().signedIn()){
MainWindow *mainWindow = new MainWindow;
mainWindow->show();
}
else {
LoginWidget *widget = new LoginWidget();
widget->show();
}
return app.exec(); return app.exec();
} }

View File

@ -2,7 +2,7 @@
#include "loginwidget.h" #include "loginwidget.h"
#include "ui_loginwidget.h" #include "ui_loginwidget.h"
#include "mainwindow.h" #include "../controllers/initcontroller.h"
#include "../utils/accountutils.h" #include "../utils/accountutils.h"
#include "../helpers/accounthelper.h" #include "../helpers/accounthelper.h"
@ -26,9 +26,8 @@ void LoginWidget::loginResult(LoginResult result)
if(result == LoginResult::LOGIN_SUCCESS){ if(result == LoginResult::LOGIN_SUCCESS){
qDebug("User successfully signed in."); qDebug("User successfully signed in.");
//Open the window //Restart application
MainWindow *mainWindow = new MainWindow(); (new InitController())->init();
mainWindow->show();
close(); close();
return; return;