Profile management

This commit is contained in:
Fabio Bas 2015-07-05 16:39:55 +02:00
parent 31890450dc
commit 7f5f290c93
21 changed files with 883 additions and 18 deletions

View file

@ -1,10 +1,15 @@
#include "userinfobox.h"
#include "pixmapgenerator.h"
#include "abstractclient.h"
#include "dlg_edit_user.h"
#include "dlg_edit_password.h"
#include "dlg_edit_avatar.h"
#include <QLabel>
#include <QDateTime>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include "pending_command.h"
#include "pb/session_commands.pb.h"
@ -14,8 +19,8 @@ const qint64 SIXTY = 60;
const qint64 HOURS_IN_A_DAY = 24;
const qint64 DAYS_IN_A_YEAR = 365;
UserInfoBox::UserInfoBox(AbstractClient *_client, bool _fullInfo, QWidget *parent, Qt::WindowFlags flags)
: QWidget(parent, flags), client(_client), fullInfo(_fullInfo)
UserInfoBox::UserInfoBox(AbstractClient *_client, bool _editable, QWidget *parent, Qt::WindowFlags flags)
: QWidget(parent, flags), client(_client), editable(_editable)
{
QFont nameFont = nameLabel.font();
nameFont.setBold(true);
@ -41,6 +46,19 @@ UserInfoBox::UserInfoBox(AbstractClient *_client, bool _fullInfo, QWidget *paren
mainLayout->addWidget(&accountAgeLebel1, 6, 0, 1, 1);
mainLayout->addWidget(&accountAgeLabel2, 6, 2, 1, 1);
mainLayout->setColumnStretch(2, 10);
if(editable)
{
QHBoxLayout * buttonsLayout = new QHBoxLayout;
buttonsLayout->addWidget(&editButton);
buttonsLayout->addWidget(&passwordButton);
buttonsLayout->addWidget(&avatarButton);
mainLayout->addLayout(buttonsLayout, 7, 0, 1, 3);
connect(&editButton, SIGNAL(clicked()), this, SLOT(actEdit()));
connect(&passwordButton, SIGNAL(clicked()), this, SLOT(actPassword()));
connect(&avatarButton, SIGNAL(clicked()), this, SLOT(actAvatar()));
}
setWindowTitle(tr("User information"));
setLayout(mainLayout);
@ -54,6 +72,10 @@ void UserInfoBox::retranslateUi()
countryLabel1.setText(tr("Location:"));
userLevelLabel1.setText(tr("User level:"));
accountAgeLebel1.setText(tr("Account Age:"));
editButton.setText(tr("Edit"));
passwordButton.setText(tr("Change password"));
avatarButton.setText(tr("Change avatar"));
}
void UserInfoBox::updateInfo(const ServerInfo_User &user)
@ -140,3 +162,126 @@ void UserInfoBox::processResponse(const Response &r)
setFixedSize(sizeHint());
show();
}
void UserInfoBox::actEdit()
{
Command_GetUserInfo cmd;
PendingCommand *pend = client->prepareSessionCommand(cmd);
connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(actEditInternal(const Response &)));
client->sendCommand(pend);
}
void UserInfoBox::actEditInternal(const Response &r)
{
const Response_GetUserInfo &response = r.GetExtension(Response_GetUserInfo::ext);
const ServerInfo_User &user = response.user_info();
QString email = QString::fromStdString(user.email());
int gender = user.gender();
QString country = QString::fromStdString(user.country());
QString realName = QString::fromStdString(user.real_name());
DlgEditUser dlg(this, email, gender, country, realName);
if(!dlg.exec())
return;
Command_AccountEdit cmd;
cmd.set_real_name(dlg.getRealName().toStdString());
cmd.set_email(dlg.getEmail().toStdString());
cmd.set_gender((ServerInfo_User_Gender) dlg.getGender());
cmd.set_country(dlg.getCountry().toStdString());
PendingCommand *pend = client->prepareSessionCommand(cmd);
connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(processEditResponse(const Response &)));
client->sendCommand(pend);
}
void UserInfoBox::actPassword()
{
DlgEditPassword dlg(this);
if(!dlg.exec())
return;
Command_AccountPassword cmd;
cmd.set_old_password(dlg.getOldPassword().toStdString());
cmd.set_new_password(dlg.getNewPassword().toStdString());
PendingCommand *pend = client->prepareSessionCommand(cmd);
connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(processPasswordResponse(const Response &)));
client->sendCommand(pend);
}
void UserInfoBox::actAvatar()
{
DlgEditAvatar dlg(this);
if(!dlg.exec())
return;
Command_AccountImage cmd;
cmd.set_image(dlg.getImage().data(), dlg.getImage().size());
PendingCommand *pend = client->prepareSessionCommand(cmd);
connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(processAvatarResponse(const Response &)));
client->sendCommand(pend);
}
void UserInfoBox::processEditResponse(const Response &r)
{
switch (r.response_code()) {
case Response::RespOk:
updateInfo(nameLabel.text());
QMessageBox::information(this, tr("Information"), tr("User information updated."));
break;
case Response::RespFunctionNotAllowed:
QMessageBox::critical(this, tr("Error"), tr("This server does not permit you to update your user informations."));
break;
case Response::RespInternalError:
default:
QMessageBox::critical(this, tr("Error"), tr("An error occured while trying to update your user informations."));
break;
}
}
void UserInfoBox::processPasswordResponse(const Response &r)
{
switch (r.response_code()) {
case Response::RespOk:
QMessageBox::information(this, tr("Information"), tr("Password changed."));
break;
case Response::RespFunctionNotAllowed:
QMessageBox::critical(this, tr("Error"), tr("This server does not permit you to change your password."));
break;
case Response::RespPasswordTooShort:
QMessageBox::critical(this, tr("Error"), tr("The new password is too short."));
break;
case Response::RespWrongPassword:
QMessageBox::critical(this, tr("Error"), tr("The old password is incorrect."));
break;
case Response::RespInternalError:
default:
QMessageBox::critical(this, tr("Error"), tr("An error occured while trying to update your user informations."));
break;
}
}
void UserInfoBox::processAvatarResponse(const Response &r)
{
switch (r.response_code()) {
case Response::RespOk:
updateInfo(nameLabel.text());
QMessageBox::information(this, tr("Information"), tr("Avatar updated."));
break;
case Response::RespFunctionNotAllowed:
QMessageBox::critical(this, tr("Error"), tr("This server does not permit you to update your avatar."));
break;
case Response::RespInternalError:
default:
QMessageBox::critical(this, tr("Error"), tr("An error occured while trying to updater your avatar."));
break;
}
}