From ea961a6d41976ecefcc19de5d61cea5fedb53a5f Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 14 Jun 2020 14:44:32 +0200 Subject: [PATCH] Get user ID --- accounthelper.cpp | 13 +++++++++++++ accounthelper.h | 7 +++++++ whoamidialog.cpp | 17 +++++++++++++++++ whoamidialog.h | 3 +++ whoamidialog.ui | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+) diff --git a/accounthelper.cpp b/accounthelper.cpp index d450432..ff02286 100644 --- a/accounthelper.cpp +++ b/accounthelper.cpp @@ -55,3 +55,16 @@ void AccountHelper::RemoveLoginToken() { 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)); + }); + +} diff --git a/accounthelper.h b/accounthelper.h index 8c03a75..0c43829 100644 --- a/accounthelper.h +++ b/accounthelper.h @@ -19,6 +19,8 @@ enum LoginResult { typedef std::function vLoginCallback; +typedef std::function vGetUserIDCallback; + class AccountHelper { public: @@ -44,5 +46,10 @@ public: * Destroy the login token of a user */ static void RemoveLoginToken(); + + /** + * Get current user ID + */ + static void GetUserID(const vGetUserIDCallback cb); }; diff --git a/whoamidialog.cpp b/whoamidialog.cpp index 6df49d6..2062d1a 100644 --- a/whoamidialog.cpp +++ b/whoamidialog.cpp @@ -1,3 +1,6 @@ +#include + +#include "accounthelper.h" #include "whoamidialog.h" #include "ui_whoamidialog.h" @@ -6,9 +9,23 @@ WhoAmIDialog::WhoAmIDialog(QWidget *parent) : ui(new Ui::WhoAmIDialog) { ui->setupUi(this); + + AccountHelper::GetUserID([this](int id) { + this->onGotUserId(id); + }); } WhoAmIDialog::~WhoAmIDialog() { 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)); +} diff --git a/whoamidialog.h b/whoamidialog.h index 5ce00a6..e9e4032 100644 --- a/whoamidialog.h +++ b/whoamidialog.h @@ -15,6 +15,9 @@ public: ~WhoAmIDialog(); private: + void onGotUserId(int userID); + + // Class members Ui::WhoAmIDialog *ui; }; diff --git a/whoamidialog.ui b/whoamidialog.ui index 9449c6d..cdfdf12 100644 --- a/whoamidialog.ui +++ b/whoamidialog.ui @@ -17,6 +17,46 @@ :/logo_large.png:/logo_large.png + + + + 180 + 80 + 160 + 80 + + + + + + + User ID + + + + + + + Loading + + + + + + + User name + + + + + + + Loading + + + + +