-
Notifications
You must be signed in to change notification settings - Fork 1
/
safeapifactory.cpp
49 lines (41 loc) · 1.23 KB
/
safeapifactory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "safeapifactory.h"
SafeApiFactory::SafeApiFactory(QString host, QObject *parent) :
QObject(parent)
{
this->host = host;
}
SafeApi *SafeApiFactory::newApi()
{
auto api = new SafeApi(this->host, this);
if(QDateTime::currentDateTime().toTime_t() - this->sharedState.tokenTimestamp < TOKEN_LIFESPAN / 2) {
api->setState(this->sharedState);
return api;
}
if(this->authUser(this->m_login, this->password)) {
return api;
}
return NULL;
}
bool SafeApiFactory::authUser(QString login, QString password)
{
QEventLoop loop;
auto api = new SafeApi(this->host);
bool success;
connect(api, &SafeApi::authUserComplete, [&](ulong id, QString user_id){
qDebug() << "Authentication complete (user id:" << user_id << ")";
this->sharedState = api->state();
this->m_login = login;
this->password = password;
success = true;
loop.exit();
});
connect(api, &SafeApi::errorRaised, [&](ulong id, quint16 code, QString text){
this->sharedState.clear();
api = NULL;
qWarning() << "Authentication error:" << text;
loop.exit();
});
api->authUser(login, password);
loop.exec();
return success;
}