diff --git a/ComunicWatcher.pro b/ComunicWatcher.pro index 4e05da2..a6139ca 100644 --- a/ComunicWatcher.pro +++ b/ComunicWatcher.pro @@ -24,6 +24,8 @@ SOURCES += \ notificationsnumber.cpp \ refreshservice.cpp \ trayicon.cpp \ + user.cpp \ + userhelper.cpp \ whoamidialog.cpp \ wsclient.cpp @@ -38,6 +40,8 @@ HEADERS += \ notificationsnumber.h \ refreshservice.h \ trayicon.h \ + user.h \ + userhelper.h \ whoamidialog.h \ wsclient.h diff --git a/apirequest.cpp b/apirequest.cpp index 5467b18..ad045fe 100644 --- a/apirequest.cpp +++ b/apirequest.cpp @@ -27,6 +27,11 @@ void APIRequest::addString(const QString &name, const QString &value) mArgs[name] = value; } +void APIRequest::addInt(const QString &name, int value) +{ + mArgs[name] = QString::number(value); +} + void APIRequest::exec() { QUrl url; diff --git a/apirequest.h b/apirequest.h index b65da1f..9329630 100644 --- a/apirequest.h +++ b/apirequest.h @@ -22,6 +22,7 @@ public: * Add a new parameter to this request */ void addString(const QString &name, const QString &value); + void addInt(const QString &name, int value); /** * Execute the request diff --git a/user.cpp b/user.cpp new file mode 100644 index 0000000..61c7902 --- /dev/null +++ b/user.cpp @@ -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; +} diff --git a/user.h b/user.h new file mode 100644 index 0000000..f17473a --- /dev/null +++ b/user.h @@ -0,0 +1,36 @@ +/** + * User information + * + * This class contains information about a user + * + * @author Pierre Hubert + */ + +#pragma once + +#include + +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; +}; + diff --git a/userhelper.cpp b/userhelper.cpp new file mode 100644 index 0000000..a2d780a --- /dev/null +++ b/userhelper.cpp @@ -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); + }); +} diff --git a/userhelper.h b/userhelper.h new file mode 100644 index 0000000..1b77815 --- /dev/null +++ b/userhelper.h @@ -0,0 +1,24 @@ +#pragma once + +#include "user.h" + +#include + +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); + +}; + diff --git a/whoamidialog.cpp b/whoamidialog.cpp index 2062d1a..8967c4b 100644 --- a/whoamidialog.cpp +++ b/whoamidialog.cpp @@ -13,6 +13,8 @@ WhoAmIDialog::WhoAmIDialog(QWidget *parent) : AccountHelper::GetUserID([this](int id) { this->onGotUserId(id); }); + + connect(&mUserHelper, &UserHelper::onGotUserInfo, this, &WhoAmIDialog::getUserCallback); } WhoAmIDialog::~WhoAmIDialog() @@ -20,6 +22,16 @@ WhoAmIDialog::~WhoAmIDialog() 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) { if(userID < 1) { @@ -28,4 +40,6 @@ void WhoAmIDialog::onGotUserId(int userID) } this->ui->user_id->setText(QString::number(userID)); + + mUserHelper.getUserInfo(userID); } diff --git a/whoamidialog.h b/whoamidialog.h index e9e4032..7822d34 100644 --- a/whoamidialog.h +++ b/whoamidialog.h @@ -1,5 +1,7 @@ #pragma once +#include "userhelper.h" + #include namespace Ui { @@ -14,10 +16,14 @@ public: explicit WhoAmIDialog(QWidget *parent = nullptr); ~WhoAmIDialog(); +private slots: + void getUserCallback(const User &u); + private: void onGotUserId(int userID); // Class members Ui::WhoAmIDialog *ui; + UserHelper mUserHelper; };