Get information about current user

This commit is contained in:
Pierre HUBERT 2020-06-14 15:00:45 +02:00
parent ea961a6d41
commit 709ed4b7fc
9 changed files with 164 additions and 0 deletions

View File

@ -24,6 +24,8 @@ SOURCES += \
notificationsnumber.cpp \ notificationsnumber.cpp \
refreshservice.cpp \ refreshservice.cpp \
trayicon.cpp \ trayicon.cpp \
user.cpp \
userhelper.cpp \
whoamidialog.cpp \ whoamidialog.cpp \
wsclient.cpp wsclient.cpp
@ -38,6 +40,8 @@ HEADERS += \
notificationsnumber.h \ notificationsnumber.h \
refreshservice.h \ refreshservice.h \
trayicon.h \ trayicon.h \
user.h \
userhelper.h \
whoamidialog.h \ whoamidialog.h \
wsclient.h wsclient.h

View File

@ -27,6 +27,11 @@ void APIRequest::addString(const QString &name, const QString &value)
mArgs[name] = value; mArgs[name] = value;
} }
void APIRequest::addInt(const QString &name, int value)
{
mArgs[name] = QString::number(value);
}
void APIRequest::exec() void APIRequest::exec()
{ {
QUrl url; QUrl url;

View File

@ -22,6 +22,7 @@ public:
* Add a new parameter to this request * Add a new parameter to this request
*/ */
void addString(const QString &name, const QString &value); void addString(const QString &name, const QString &value);
void addInt(const QString &name, int value);
/** /**
* Execute the request * Execute the request

46
user.cpp Normal file
View File

@ -0,0 +1,46 @@
#include "user.h"
User::User()
{
}
bool User::isValid() const
{
return mId > 0;
}
int User::id() const
{
return mId;
}
void User::setId(int id)
{
mId = id;
}
QString User::firstName() const
{
return mFirstName;
}
void User::setFirstName(const QString &firstName)
{
mFirstName = firstName;
}
QString User::lastName() const
{
return mLastName;
}
void User::setLastName(const QString &lastName)
{
mLastName = lastName;
}
QString User::fullName() const
{
return mFirstName + " " + mLastName;
}

36
user.h Normal file
View File

@ -0,0 +1,36 @@
/**
* User information
*
* This class contains information about a user
*
* @author Pierre Hubert
*/
#pragma once
#include <QString>
class User
{
public:
User();
bool isValid() const;
int id() const;
void setId(int id);
QString firstName() const;
void setFirstName(const QString &firstName);
QString lastName() const;
void setLastName(const QString &lastName);
QString fullName() const;
private:
int mId;
QString mFirstName;
QString mLastName;
};

28
userhelper.cpp Normal file
View File

@ -0,0 +1,28 @@
#include "apirequest.h"
#include "userhelper.h"
UserHelper::UserHelper(QObject *parent) : QObject(parent)
{
}
void UserHelper::getUserInfo(int userID)
{
auto req = new APIRequest("user/getInfo");
req->addInt("userID", userID);
req->exec();
connect(req, &APIRequest::done, [=](APIResponse res) {
if(res.isError()) {
emit onGotUserInfo(User());
return;
}
User u;
auto obj = res.getObject();
u.setId(obj.value("userID").toInt());
u.setFirstName(obj.value("firstName").toString());
u.setLastName(obj.value("lastName").toString());
emit onGotUserInfo(u);
});
}

24
userhelper.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include "user.h"
#include <QObject>
class UserHelper : public QObject
{
Q_OBJECT
public:
explicit UserHelper(QObject *parent = nullptr);
/**
* Get information about a user
*
* @param userID Target user ID
*/
void getUserInfo(int userID);
signals:
void onGotUserInfo(const User &u);
};

View File

@ -13,6 +13,8 @@ WhoAmIDialog::WhoAmIDialog(QWidget *parent) :
AccountHelper::GetUserID([this](int id) { AccountHelper::GetUserID([this](int id) {
this->onGotUserId(id); this->onGotUserId(id);
}); });
connect(&mUserHelper, &UserHelper::onGotUserInfo, this, &WhoAmIDialog::getUserCallback);
} }
WhoAmIDialog::~WhoAmIDialog() WhoAmIDialog::~WhoAmIDialog()
@ -20,6 +22,16 @@ WhoAmIDialog::~WhoAmIDialog()
delete ui; delete ui;
} }
void WhoAmIDialog::getUserCallback(const User &u)
{
if(!u.isValid()) {
QMessageBox::warning(this, tr("Error"), tr("Could not get user information!"));
return;
}
this->ui->user_name->setText(u.fullName());
}
void WhoAmIDialog::onGotUserId(int userID) void WhoAmIDialog::onGotUserId(int userID)
{ {
if(userID < 1) { if(userID < 1) {
@ -28,4 +40,6 @@ void WhoAmIDialog::onGotUserId(int userID)
} }
this->ui->user_id->setText(QString::number(userID)); this->ui->user_id->setText(QString::number(userID));
mUserHelper.getUserInfo(userID);
} }

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "userhelper.h"
#include <QDialog> #include <QDialog>
namespace Ui { namespace Ui {
@ -14,10 +16,14 @@ public:
explicit WhoAmIDialog(QWidget *parent = nullptr); explicit WhoAmIDialog(QWidget *parent = nullptr);
~WhoAmIDialog(); ~WhoAmIDialog();
private slots:
void getUserCallback(const User &u);
private: private:
void onGotUserId(int userID); void onGotUserId(int userID);
// Class members // Class members
Ui::WhoAmIDialog *ui; Ui::WhoAmIDialog *ui;
UserHelper mUserHelper;
}; };