| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <QAbstractListModel> |
| 8 |
#include <QObject> |
| 9 |
#include <QQmlEngine> |
| 10 |
#include <QString> |
| 11 |
#include <QVector> |
| 12 |
#include <mtx/responses.hpp> |
| 13 |
#include <mtx/responses/common.hpp> |
| 14 |
|
| 15 |
#include "CacheCryptoStructs.h" |
| 16 |
#include "CacheStructs.h" |
| 17 |
|
| 18 |
class TimelineModel; |
| 19 |
|
| 20 |
namespace verification { |
| 21 |
Q_NAMESPACE |
| 22 |
QML_NAMED_ELEMENT(VerificationStatus) |
| 23 |
|
| 24 |
enum Status |
| 25 |
{ |
| 26 |
SELF, |
| 27 |
VERIFIED, |
| 28 |
UNVERIFIED, |
| 29 |
BLOCKED, |
| 30 |
NOT_APPLICABLE |
| 31 |
}; |
| 32 |
Q_ENUM_NS(Status) |
| 33 |
} |
| 34 |
|
| 35 |
class DeviceVerificationFlow; |
| 36 |
class TimelineViewManager; |
| 37 |
|
| 38 |
class UserProfileFetchProxy final : public QObject |
| 39 |
{ |
| 40 |
Q_OBJECT |
| 41 |
|
| 42 |
public: |
| 43 |
UserProfileFetchProxy(QObject *p = nullptr) |
| 44 |
: QObject(p) |
| 45 |
{ |
| 46 |
} |
| 47 |
|
| 48 |
signals: |
| 49 |
void profileFetched(mtx::responses::Profile); |
| 50 |
void failedToFetchProfile(); |
| 51 |
}; |
| 52 |
|
| 53 |
class DeviceInfo |
| 54 |
{ |
| 55 |
public: |
| 56 |
DeviceInfo(const QString deviceID, |
| 57 |
const QString displayName, |
| 58 |
verification::Status verification_status_, |
| 59 |
const QString lastIp_, |
| 60 |
const size_t lastTs_) |
| 61 |
: device_id(deviceID) |
| 62 |
, display_name(displayName) |
| 63 |
, verification_status(verification_status_) |
| 64 |
, lastIp(lastIp_) |
| 65 |
, lastTs(lastTs_) |
| 66 |
{ |
| 67 |
} |
| 68 |
DeviceInfo(const QString deviceID, |
| 69 |
const QString displayName, |
| 70 |
verification::Status verification_status_) |
| 71 |
: device_id(deviceID) |
| 72 |
, display_name(displayName) |
| 73 |
, verification_status(verification_status_) |
| 74 |
, lastTs(0) |
| 75 |
{ |
| 76 |
} |
| 77 |
DeviceInfo() |
| 78 |
: verification_status(verification::UNVERIFIED) |
| 79 |
{ |
| 80 |
} |
| 81 |
|
| 82 |
QString device_id; |
| 83 |
QString display_name; |
| 84 |
|
| 85 |
verification::Status verification_status; |
| 86 |
QString lastIp; |
| 87 |
qlonglong lastTs; |
| 88 |
}; |
| 89 |
|
| 90 |
class DeviceInfoModel final : public QAbstractListModel |
| 91 |
{ |
| 92 |
Q_OBJECT |
| 93 |
public: |
| 94 |
enum Roles |
| 95 |
{ |
| 96 |
DeviceId, |
| 97 |
DeviceName, |
| 98 |
VerificationStatus, |
| 99 |
LastIp, |
| 100 |
LastTs, |
| 101 |
}; |
| 102 |
|
| 103 |
explicit DeviceInfoModel(QObject *parent = nullptr) |
| 104 |
{ |
| 105 |
(void)parent; |
| 106 |
connect(this, &DeviceInfoModel::queueReset, this, &DeviceInfoModel::reset); |
| 107 |
}; |
| 108 |
QHash<int, QByteArray> roleNames() const override; |
| 109 |
int rowCount(const QModelIndex &parent = QModelIndex()) const override |
| 110 |
{ |
| 111 |
(void)parent; |
| 112 |
return (int)deviceList_.size(); |
| 113 |
} |
| 114 |
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
| 115 |
|
| 116 |
signals: |
| 117 |
void queueReset(const std::vector<DeviceInfo> &deviceList); |
| 118 |
public slots: |
| 119 |
void reset(const std::vector<DeviceInfo> &deviceList); |
| 120 |
|
| 121 |
private: |
| 122 |
std::vector<DeviceInfo> deviceList_; |
| 123 |
|
| 124 |
friend class UserProfile; |
| 125 |
}; |
| 126 |
|
| 127 |
class RoomInfoModel final : public QAbstractListModel |
| 128 |
{ |
| 129 |
Q_OBJECT |
| 130 |
public: |
| 131 |
enum Roles |
| 132 |
{ |
| 133 |
RoomId, |
| 134 |
RoomName, |
| 135 |
AvatarUrl, |
| 136 |
}; |
| 137 |
|
| 138 |
explicit RoomInfoModel(const std::map<std::string, RoomInfo> &, QObject *parent = nullptr); |
| 139 |
QHash<int, QByteArray> roleNames() const override; |
| 140 |
int rowCount(const QModelIndex &parent = QModelIndex()) const override |
| 141 |
{ |
| 142 |
(void)parent; |
| 143 |
return (int)roomInfos_.size(); |
| 144 |
} |
| 145 |
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; |
| 146 |
|
| 147 |
private: |
| 148 |
std::vector<std::pair<std::string, RoomInfo>> roomInfos_; |
| 149 |
}; |
| 150 |
|
| 151 |
class UserProfile final : public QObject |
| 152 |
{ |
| 153 |
Q_OBJECT |
| 154 |
Q_PROPERTY(QString displayName READ displayName NOTIFY displayNameChanged) |
| 155 |
Q_PROPERTY(QString userid READ userid CONSTANT) |
| 156 |
Q_PROPERTY(QString avatarUrl READ avatarUrl NOTIFY avatarUrlChanged) |
| 157 |
Q_PROPERTY(DeviceInfoModel *deviceList READ deviceList NOTIFY devicesChanged) |
| 158 |
Q_PROPERTY(RoomInfoModel *sharedRooms READ sharedRooms CONSTANT) |
| 159 |
Q_PROPERTY(bool isGlobalUserProfile READ isGlobalUserProfile CONSTANT) |
| 160 |
Q_PROPERTY(int userVerified READ getUserStatus NOTIFY userStatusChanged) |
| 161 |
Q_PROPERTY(bool isLoading READ isLoading NOTIFY loadingChanged) |
| 162 |
Q_PROPERTY(bool userVerificationEnabled READ userVerificationEnabled NOTIFY userStatusChanged) |
| 163 |
Q_PROPERTY(bool ignored READ ignored WRITE setIgnored NOTIFY ignoredChanged) |
| 164 |
Q_PROPERTY(bool isSelf READ isSelf CONSTANT) |
| 165 |
Q_PROPERTY(TimelineModel *room READ room CONSTANT) |
| 166 |
public: |
| 167 |
UserProfile(const QString &roomid, |
| 168 |
const QString &userid, |
| 169 |
TimelineViewManager *manager_, |
| 170 |
TimelineModel *parent = nullptr); |
| 171 |
|
| 172 |
DeviceInfoModel *deviceList(); |
| 173 |
RoomInfoModel *sharedRooms(); |
| 174 |
|
| 175 |
QString userid(); |
| 176 |
QString displayName(); |
| 177 |
QString avatarUrl(); |
| 178 |
bool isGlobalUserProfile() const; |
| 179 |
crypto::Trust getUserStatus(); |
| 180 |
bool userVerificationEnabled() const; |
| 181 |
bool isSelf() const; |
| 182 |
bool isLoading() const; |
| 183 |
TimelineModel *room() const { return model; } |
| 184 |
|
| 185 |
Q_INVOKABLE void verify(QString device = QLatin1String("")); |
| 186 |
Q_INVOKABLE void unverify(const QString &device = QLatin1String("")); |
| 187 |
Q_INVOKABLE void fetchDeviceList(const QString &userID); |
| 188 |
Q_INVOKABLE void refreshDevices(); |
| 189 |
Q_INVOKABLE void banUser(); |
| 190 |
Q_INVOKABLE void signOutDevice(const QString &deviceID); |
| 191 |
Q_INVOKABLE void kickUser(); |
| 192 |
Q_INVOKABLE void startChat(); |
| 193 |
Q_INVOKABLE void startChat(bool encryptionEnabled); |
| 194 |
Q_INVOKABLE void changeUsername(const QString &username); |
| 195 |
Q_INVOKABLE void changeDeviceName(const QString &deviceID, const QString &deviceName); |
| 196 |
Q_INVOKABLE void changeAvatar(); |
| 197 |
Q_INVOKABLE void openGlobalProfile(); |
| 198 |
|
| 199 |
void setIgnored(bool ignored); |
| 200 |
bool ignored() const; |
| 201 |
|
| 202 |
signals: |
| 203 |
void userStatusChanged(); |
| 204 |
void loadingChanged(); |
| 205 |
void displayNameChanged(); |
| 206 |
void avatarUrlChanged(); |
| 207 |
void displayError(const QString &errorMessage); |
| 208 |
void globalUsernameRetrieved(const QString &globalUser); |
| 209 |
void devicesChanged(); |
| 210 |
void ignoredChanged(); |
| 211 |
void failedToFetchProfile(); |
| 212 |
|
| 213 |
|
| 214 |
void verificationStatiChanged(); |
| 215 |
|
| 216 |
public slots: |
| 217 |
void updateAvatarUrl(); |
| 218 |
|
| 219 |
private slots: |
| 220 |
void setGlobalUsername(const QString &globalUser); |
| 221 |
void updateVerificationStatus(); |
| 222 |
|
| 223 |
private: |
| 224 |
void updateRoomMemberState(mtx::events::state::Member member); |
| 225 |
void getGlobalProfileData(); |
| 226 |
|
| 227 |
private: |
| 228 |
QString roomid_, userid_; |
| 229 |
QString globalUsername; |
| 230 |
QString globalAvatarUrl; |
| 231 |
DeviceInfoModel deviceList_; |
| 232 |
crypto::Trust isUserVerified = crypto::Trust::Unverified; |
| 233 |
bool hasMasterKey = false; |
| 234 |
bool isLoading_ = false; |
| 235 |
TimelineViewManager *manager; |
| 236 |
TimelineModel *model; |
| 237 |
RoomInfoModel *sharedRooms_ = nullptr; |
| 238 |
}; |