forked from CPP-Final-Project/Chat_Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserItem.h
113 lines (93 loc) · 2.84 KB
/
UserItem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#pragma once
#include <QDateTime>
#include <QIcon>
#include <plog/Log.h>
#include <QFile>
#include <QResource>
class UserItem : public QObject, public QEnableSharedFromThis<UserItem>
{
Q_OBJECT;
public:
struct userLoginStruct {
QString user_nickname;
QString user_password;
};
UserItem() : QObject(Q_NULLPTR) {};
UserItem(
QString user_nickname_,
qint32 user_rating_ = 0,
QByteArray user_data_ = {},
QString user_password_ = "") :
QObject(nullptr),
user_nickname(std::move(user_nickname_)),
user_password(std::move(user_password_)),
user_rating(user_rating_),
user_avatar(getUserAvatarIcon(user_nickname_, user_data_))
{};
~UserItem() override = default;
UserItem(const UserItem&) = delete;
UserItem(UserItem&&) = delete;
const UserItem& operator =(const UserItem&) = delete;
UserItem& operator = (UserItem&&) = delete;
auto shared() { return sharedFromThis(); }
[[nodiscard]] auto getUserNickname() const { return user_nickname; }
[[nodiscard]] auto getUserPassword() const { return user_password; }
auto setUserPassword(const QString val) { user_password = val; }
[[nodiscard]] auto const& getUserAvatar() const { return user_avatar; }
auto setUserAvatar(const QIcon& val) { user_avatar = val; Q_EMIT avatar_changed(); }
[[nodiscard]] auto getUserRating() const { return user_rating; }
auto setUserRating(const int val) { user_rating = val; Q_EMIT rating_changed(); }
[[nodiscard]] auto getUserLoginStruct() const
{
return userLoginStruct{ user_nickname, user_password };
}
Q_SIGNALS:
void avatar_changed();
void rating_changed();
private:
QIcon getUserAvatarIcon(const QString& user_nickname_, const QByteArray& user_avatar_data_)
{
QIcon default_icon;
if (!user_nickname_.isNull()) {
QString path = ":/ChatClient/images/" + user_nickname_ + ".png";
QResource res_path(path);
QFile file(path);
if (res_path.isValid()) {
default_icon = QIcon(path);
PLOGI << "Avatar "<< user_nickname <<" exist in.qrc";
}
else {
if (file.open(QIODevice::WriteOnly))
{
if (!user_avatar_data_.isEmpty()) {
file.write(user_avatar_data_);
file.close();
default_icon = QIcon(path);
PLOGI << "Image Create successfully.";
}
else {
default_icon = QIcon(":/ChatClient/images/avatar.png");
PLOGW << "No data in QByte array. used standart avatar";
}
}
else
{
default_icon = QIcon(":/ChatClient/images/avatar.png");
PLOGW << "Failed to save image. used standart avatar";
}
}
}
else {
default_icon = QIcon(":/ChatClient/images/avatar.png");
PLOGW << "No data comes from server. Used standart avatar";
}
return default_icon;
}
private:
QString user_nickname;
QString user_password;
QIcon user_avatar{};
qint32 user_rating{ 0 };
};
using UserItemPtr = QSharedPointer<UserItem>;
Q_DECLARE_METATYPE(UserItemPtr);