Get user ID

This commit is contained in:
Pierre HUBERT 2020-06-14 14:44:32 +02:00
parent cbbf4b43c8
commit ea961a6d41
5 changed files with 80 additions and 0 deletions

View File

@ -55,3 +55,16 @@ void AccountHelper::RemoveLoginToken()
{ {
QSettings().remove(LOGIN_TOKEN_VALUE); QSettings().remove(LOGIN_TOKEN_VALUE);
} }
void AccountHelper::GetUserID(const vGetUserIDCallback cb)
{
auto req = new APIRequest("account/id");
req->exec();
QObject::connect(req, &APIRequest::done, [=] (APIResponse res) {
if(res.isError())
cb(-1);
else
cb(res.getObject().value("userID").toInt(-1));
});
}

View File

@ -19,6 +19,8 @@ enum LoginResult {
typedef std::function<void (LoginResult)> vLoginCallback; typedef std::function<void (LoginResult)> vLoginCallback;
typedef std::function<void (int)> vGetUserIDCallback;
class AccountHelper class AccountHelper
{ {
public: public:
@ -44,5 +46,10 @@ public:
* Destroy the login token of a user * Destroy the login token of a user
*/ */
static void RemoveLoginToken(); static void RemoveLoginToken();
/**
* Get current user ID
*/
static void GetUserID(const vGetUserIDCallback cb);
}; };

View File

@ -1,3 +1,6 @@
#include <QMessageBox>
#include "accounthelper.h"
#include "whoamidialog.h" #include "whoamidialog.h"
#include "ui_whoamidialog.h" #include "ui_whoamidialog.h"
@ -6,9 +9,23 @@ WhoAmIDialog::WhoAmIDialog(QWidget *parent) :
ui(new Ui::WhoAmIDialog) ui(new Ui::WhoAmIDialog)
{ {
ui->setupUi(this); ui->setupUi(this);
AccountHelper::GetUserID([this](int id) {
this->onGotUserId(id);
});
} }
WhoAmIDialog::~WhoAmIDialog() WhoAmIDialog::~WhoAmIDialog()
{ {
delete ui; delete ui;
} }
void WhoAmIDialog::onGotUserId(int userID)
{
if(userID < 1) {
QMessageBox::warning(this, tr("Error"), tr("Could not get your id!"));
return;
}
this->ui->user_id->setText(QString::number(userID));
}

View File

@ -15,6 +15,9 @@ public:
~WhoAmIDialog(); ~WhoAmIDialog();
private: private:
void onGotUserId(int userID);
// Class members
Ui::WhoAmIDialog *ui; Ui::WhoAmIDialog *ui;
}; };

View File

@ -17,6 +17,46 @@
<iconset resource="ressources.qrc"> <iconset resource="ressources.qrc">
<normaloff>:/logo_large.png</normaloff>:/logo_large.png</iconset> <normaloff>:/logo_large.png</normaloff>:/logo_large.png</iconset>
</property> </property>
<widget class="QWidget" name="gridLayoutWidget">
<property name="geometry">
<rect>
<x>180</x>
<y>80</y>
<width>160</width>
<height>80</height>
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>User ID</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="user_id">
<property name="text">
<string>Loading</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>User name</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="user_name">
<property name="text">
<string>Loading</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget> </widget>
<resources> <resources>
<include location="ressources.qrc"/> <include location="ressources.qrc"/>