From 5e3f5136557b83ee964d6c0cfa4fed422330728d Mon Sep 17 00:00:00 2001 From: Jedi18 Date: Wed, 27 Jan 2021 11:03:08 +0530 Subject: [PATCH 1/9] update room specific username from userprofile --- resources/qml/UserProfile.qml | 34 +++++++++++++++++++++++++++++ src/timeline/TimelineModel.cpp | 4 ++-- src/timeline/TimelineModel.h | 2 +- src/ui/UserProfile.cpp | 39 ++++++++++++++++++++++++++++++++++ src/ui/UserProfile.h | 1 + 5 files changed, 77 insertions(+), 3 deletions(-) diff --git a/resources/qml/UserProfile.qml b/resources/qml/UserProfile.qml index 8328f4a5..2a06b955 100644 --- a/resources/qml/UserProfile.qml +++ b/resources/qml/UserProfile.qml @@ -58,6 +58,40 @@ ApplicationWindow { onClicked: profile.verify() } + Button { + id: changeUsername + + text: (profile.roomid_ == "") ? qsTr("Change global username") : qsTr("Change room username") + Layout.alignment: Qt.AlignHCenter + enabled : profile.isSelf + visible: profile.isSelf + onClicked: changeUsernameDialog.open() + + Dialog { + id: changeUsernameDialog + modal: true + title: (profile.roomid_ == "") ? qsTr("Change global username") : qsTr("Change room username") + onAccepted: profile.changeUsername(usernameEdit.text) + + Column { + anchors.fill: parent + + Text { + text: "New Username" + anchors.horizontalCenter: parent.horizontalCenter + } + TextField { + id: usernameEdit + focus: true + wrapMode: TextEdit.Wrap + anchors.horizontalCenter: parent.horizontalCenter + } + } + + standardButtons: Dialog.Ok | Dialog.Cancel + } + } + Image { Layout.preferredHeight: 16 Layout.preferredWidth: 16 diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index 4346b0b2..79cf5184 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -799,9 +799,9 @@ TimelineModel::viewDecryptedRawMessage(QString id) const } void -TimelineModel::openUserProfile(QString userid) +TimelineModel::openUserProfile(QString userid, bool global) { - emit openProfile(new UserProfile(room_id_, userid, manager_, this)); + emit openProfile(new UserProfile(global ? "" : room_id_, userid, manager_, this)); } void diff --git a/src/timeline/TimelineModel.h b/src/timeline/TimelineModel.h index 35e62eb4..51b8049e 100644 --- a/src/timeline/TimelineModel.h +++ b/src/timeline/TimelineModel.h @@ -212,7 +212,7 @@ public: Q_INVOKABLE void viewRawMessage(QString id) const; Q_INVOKABLE void viewDecryptedRawMessage(QString id) const; - Q_INVOKABLE void openUserProfile(QString userid); + Q_INVOKABLE void openUserProfile(QString userid, bool global = false); Q_INVOKABLE void replyAction(QString id); Q_INVOKABLE void readReceiptsAction(QString id) const; Q_INVOKABLE void redactEvent(QString id); diff --git a/src/ui/UserProfile.cpp b/src/ui/UserProfile.cpp index 08219a38..503f314c 100644 --- a/src/ui/UserProfile.cpp +++ b/src/ui/UserProfile.cpp @@ -4,6 +4,7 @@ #include "DeviceVerificationFlow.h" #include "Logging.h" #include "Utils.h" +#include #include "mtx/responses/crypto.hpp" #include "timeline/TimelineModel.h" #include "timeline/TimelineViewManager.h" @@ -213,6 +214,44 @@ UserProfile::startChat() ChatPage::instance()->startChat(this->userid_); } +void +UserProfile::changeUsername(QString username) +{ + // change room username + mtx::events::state::Member member; + member.display_name = username.toStdString(); + member.avatar_url = + cache::avatarUrl(roomid_, + QString::fromStdString(http::client()->user_id().to_string())) + .toStdString(); + member.membership = mtx::events::state::Membership::Join; + + http::client()->send_state_event(roomid_.toStdString(), + http::client()->user_id().to_string(), + member, + [](mtx::responses::EventId, mtx::http::RequestErr err) { + if (err) + nhlog::net()->error( + "Failed to set room displayname: {}", + err->matrix_error.error); + }); + + /*connect(modal, &EditModal::nameChanged, this, [this](const QString &newName) { + if (roomNameLabel_) + roomNameLabel_->setText(newName); + });*/ + + /*std::string newName = "jedi18"; + // change user name + http::client()->set_displayname( + newName, [this]( mtx::http::RequestErr err) { + if (err) { + nhlog::net()->warn("could not change username"); + return; + } + });*/ +} + void UserProfile::verify(QString device) { diff --git a/src/ui/UserProfile.h b/src/ui/UserProfile.h index 19527310..df90e5a1 100644 --- a/src/ui/UserProfile.h +++ b/src/ui/UserProfile.h @@ -109,6 +109,7 @@ public: // Q_INVOKABLE void ignoreUser(); Q_INVOKABLE void kickUser(); Q_INVOKABLE void startChat(); + Q_INVOKABLE void changeUsername(QString username); signals: void userStatusChanged(); From 87490c29cd8af7c17b5a4591798f1f0ebfa9023c Mon Sep 17 00:00:00 2001 From: Jedi18 Date: Thu, 28 Jan 2021 20:03:50 +0530 Subject: [PATCH 2/9] Username can be edited by double clicking on text, added global user profile menu action in user info widget --- resources/qml/UserProfile.qml | 51 +++++++--------------- src/ChatPage.cpp | 4 ++ src/UserInfoWidget.cpp | 4 ++ src/UserInfoWidget.h | 3 ++ src/timeline/TimelineModel.cpp | 2 +- src/ui/UserProfile.cpp | 79 +++++++++++++++++++++------------- src/ui/UserProfile.h | 9 ++++ 7 files changed, 84 insertions(+), 68 deletions(-) diff --git a/resources/qml/UserProfile.qml b/resources/qml/UserProfile.qml index 2a06b955..0ced5498 100644 --- a/resources/qml/UserProfile.qml +++ b/resources/qml/UserProfile.qml @@ -15,6 +15,7 @@ ApplicationWindow { minimumHeight: 420 palette: colors color: colors.window + title: profile.globalUserProfile ? "Global User Profile" : "Room User Profile" ColumnLayout { id: contentL @@ -33,13 +34,25 @@ ApplicationWindow { onClicked: TimelineManager.openImageOverlay(TimelineManager.timeline.avatarUrl(userid), TimelineManager.timeline.data.id) } - Label { + TextInput { + id: displayUsername + readOnly: !profile.isUsernameEditingAllowed text: profile.displayName - fontSizeMode: Text.HorizontalFit font.pixelSize: 20 color: TimelineManager.userColor(profile.userid, colors.window) font.bold: true Layout.alignment: Qt.AlignHCenter + focus: true + + onEditingFinished: profile.changeUsername(displayUsername.text) + + MouseArea { + enabled: !profile.isUsernameEditingAllowed + anchors.fill: parent + onDoubleClicked: { + profile.allowUsernameEditing(true) + } + } } MatrixText { @@ -58,40 +71,6 @@ ApplicationWindow { onClicked: profile.verify() } - Button { - id: changeUsername - - text: (profile.roomid_ == "") ? qsTr("Change global username") : qsTr("Change room username") - Layout.alignment: Qt.AlignHCenter - enabled : profile.isSelf - visible: profile.isSelf - onClicked: changeUsernameDialog.open() - - Dialog { - id: changeUsernameDialog - modal: true - title: (profile.roomid_ == "") ? qsTr("Change global username") : qsTr("Change room username") - onAccepted: profile.changeUsername(usernameEdit.text) - - Column { - anchors.fill: parent - - Text { - text: "New Username" - anchors.horizontalCenter: parent.horizontalCenter - } - TextField { - id: usernameEdit - focus: true - wrapMode: TextEdit.Wrap - anchors.horizontalCenter: parent.horizontalCenter - } - } - - standardButtons: Dialog.Ok | Dialog.Cancel - } - } - Image { Layout.preferredHeight: 16 Layout.preferredWidth: 16 diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 4e472a3a..0d3c98a8 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -112,6 +112,10 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) connect(sidebarActions_, &SideBarActions::createRoom, this, &ChatPage::createRoom); user_info_widget_ = new UserInfoWidget(sideBar_); + connect(user_info_widget_, &UserInfoWidget::openGlobalUserProfile, this, [this]() { + view_manager_->activeTimeline()->openUserProfile("", true); + }); + user_mentions_popup_ = new popups::UserMentions(); room_list_ = new RoomList(userSettings, sideBar_); connect(room_list_, &RoomList::joinRoom, this, &ChatPage::joinRoom); diff --git a/src/UserInfoWidget.cpp b/src/UserInfoWidget.cpp index f8e94431..5bcb44a9 100644 --- a/src/UserInfoWidget.cpp +++ b/src/UserInfoWidget.cpp @@ -125,6 +125,10 @@ UserInfoWidget::UserInfoWidget(QWidget *parent) ChatPage::instance()->setStatus(text); }); + auto userProfileAction = menu->addAction(tr("User Profile Settings")); + connect( + userProfileAction, &QAction::triggered, this, [this]() { emit openGlobalUserProfile(); }); + #if 0 // disable presence menu until issues in synapse are resolved auto setAutoPresence = menu->addAction(tr("Set presence automatically")); connect(setAutoPresence, &QAction::triggered, this, []() { diff --git a/src/UserInfoWidget.h b/src/UserInfoWidget.h index 03ab2cf0..bfcfbc0b 100644 --- a/src/UserInfoWidget.h +++ b/src/UserInfoWidget.h @@ -51,6 +51,9 @@ protected: void paintEvent(QPaintEvent *event) override; void contextMenuEvent(QContextMenuEvent *) override; +signals: + void openGlobalUserProfile(); + private: Avatar *userAvatar_; diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index 79cf5184..ffb0beec 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -801,7 +801,7 @@ TimelineModel::viewDecryptedRawMessage(QString id) const void TimelineModel::openUserProfile(QString userid, bool global) { - emit openProfile(new UserProfile(global ? "" : room_id_, userid, manager_, this)); + emit openProfile(new UserProfile(global ? "" : room_id_, global ? utils::localUser() : userid, manager_, this)); } void diff --git a/src/ui/UserProfile.cpp b/src/ui/UserProfile.cpp index 503f314c..4b7f054d 100644 --- a/src/ui/UserProfile.cpp +++ b/src/ui/UserProfile.cpp @@ -107,6 +107,12 @@ UserProfile::avatarUrl() return cache::avatarUrl(roomid_, userid_); } +bool +UserProfile::globalUserProfile() const +{ + return (roomid_ == "") && isSelf(); +} + bool UserProfile::getUserStatus() { @@ -217,39 +223,37 @@ UserProfile::startChat() void UserProfile::changeUsername(QString username) { - // change room username - mtx::events::state::Member member; - member.display_name = username.toStdString(); - member.avatar_url = - cache::avatarUrl(roomid_, - QString::fromStdString(http::client()->user_id().to_string())) - .toStdString(); - member.membership = mtx::events::state::Membership::Join; + if (globalUserProfile()) { + // change global + http::client()->set_displayname( + username.toStdString(), [this]( mtx::http::RequestErr err) { + if (err) { + nhlog::net()->warn("could not change username"); + return; + } + }); + } else { + // change room username + mtx::events::state::Member member; + member.display_name = username.toStdString(); + member.avatar_url = + cache::avatarUrl(roomid_, + QString::fromStdString(http::client()->user_id().to_string())) + .toStdString(); + member.membership = mtx::events::state::Membership::Join; - http::client()->send_state_event(roomid_.toStdString(), - http::client()->user_id().to_string(), - member, - [](mtx::responses::EventId, mtx::http::RequestErr err) { - if (err) - nhlog::net()->error( - "Failed to set room displayname: {}", - err->matrix_error.error); - }); + http::client()->send_state_event( + roomid_.toStdString(), + http::client()->user_id().to_string(), + member, + [](mtx::responses::EventId, mtx::http::RequestErr err) { + if (err) + nhlog::net()->error("Failed to set room displayname: {}", + err->matrix_error.error); + }); + } - /*connect(modal, &EditModal::nameChanged, this, [this](const QString &newName) { - if (roomNameLabel_) - roomNameLabel_->setText(newName); - });*/ - - /*std::string newName = "jedi18"; - // change user name - http::client()->set_displayname( - newName, [this]( mtx::http::RequestErr err) { - if (err) { - nhlog::net()->warn("could not change username"); - return; - } - });*/ + allowUsernameEditing(false); } void @@ -267,3 +271,16 @@ UserProfile::unverify(QString device) { cache::markDeviceUnverified(userid_.toStdString(), device.toStdString()); } + +void +UserProfile::allowUsernameEditing(bool allow) +{ + usernameEditing = allow; + emit usernameEditingChanged(); +} + +bool +UserProfile::isUsernameEditingAllowed() const +{ + return usernameEditing; +} diff --git a/src/ui/UserProfile.h b/src/ui/UserProfile.h index df90e5a1..4839e0d8 100644 --- a/src/ui/UserProfile.h +++ b/src/ui/UserProfile.h @@ -83,10 +83,13 @@ class UserProfile : public QObject Q_PROPERTY(QString userid READ userid CONSTANT) Q_PROPERTY(QString avatarUrl READ avatarUrl CONSTANT) Q_PROPERTY(DeviceInfoModel *deviceList READ deviceList CONSTANT) + Q_PROPERTY(bool globalUserProfile READ globalUserProfile CONSTANT) Q_PROPERTY(bool isUserVerified READ getUserStatus NOTIFY userStatusChanged) Q_PROPERTY( bool userVerificationEnabled READ userVerificationEnabled NOTIFY userStatusChanged) Q_PROPERTY(bool isSelf READ isSelf CONSTANT) + Q_PROPERTY( + bool isUsernameEditingAllowed READ isUsernameEditingAllowed NOTIFY usernameEditingChanged) public: UserProfile(QString roomid, QString userid, @@ -98,9 +101,11 @@ public: QString userid(); QString displayName(); QString avatarUrl(); + bool globalUserProfile() const; bool getUserStatus(); bool userVerificationEnabled() const; bool isSelf() const; + bool isUsernameEditingAllowed() const; Q_INVOKABLE void verify(QString device = ""); Q_INVOKABLE void unverify(QString device = ""); @@ -110,15 +115,19 @@ public: Q_INVOKABLE void kickUser(); Q_INVOKABLE void startChat(); Q_INVOKABLE void changeUsername(QString username); + Q_INVOKABLE void allowUsernameEditing(bool allow); signals: void userStatusChanged(); + void usernameEditingChanged(); + private: QString roomid_, userid_; DeviceInfoModel deviceList_; bool isUserVerified = false; bool hasMasterKey = false; + bool usernameEditing = false; TimelineViewManager *manager; TimelineModel *model; }; From b3f29f592b26806d4a82067bbb64f9ffd044d474 Mon Sep 17 00:00:00 2001 From: Jedi18 Date: Thu, 28 Jan 2021 23:35:02 +0530 Subject: [PATCH 3/9] Changed edit method from double clicking to an edit button --- resources/qml/UserProfile.qml | 22 +++++++++++++++------- src/timeline/TimelineModel.cpp | 19 ++++++++++++++++++- src/timeline/TimelineModel.h | 1 + src/ui/UserProfile.cpp | 6 ++++-- src/ui/UserProfile.h | 5 +++-- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/resources/qml/UserProfile.qml b/resources/qml/UserProfile.qml index 0ced5498..5e577099 100644 --- a/resources/qml/UserProfile.qml +++ b/resources/qml/UserProfile.qml @@ -42,15 +42,23 @@ ApplicationWindow { color: TimelineManager.userColor(profile.userid, colors.window) font.bold: true Layout.alignment: Qt.AlignHCenter - focus: true + selectByMouse: true - onEditingFinished: profile.changeUsername(displayUsername.text) + Keys.priority: Keys.BeforeItem + Keys.onReturnPressed: profile.changeUsername(displayUsername.text) - MouseArea { - enabled: !profile.isUsernameEditingAllowed - anchors.fill: parent - onDoubleClicked: { - profile.allowUsernameEditing(true) + ImageButton { + anchors.leftMargin: 5 + anchors.left: displayUsername.right + anchors.verticalCenter: displayUsername.verticalCenter + image: profile.isUsernameEditingAllowed ? ":/icons/icons/ui/checkmark.png" : ":/icons/icons/ui/edit.png" + + onClicked: { + if(profile.isUsernameEditingAllowed) { + profile.changeUsername(displayUsername.text) + }else{ + profile.allowUsernameEditing(true) + } } } } diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index ffb0beec..41c0b40e 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -22,6 +22,7 @@ #include "TimelineViewManager.h" #include "Utils.h" #include "dialogs/RawMessage.h" +#include Q_DECLARE_METATYPE(QModelIndex) @@ -260,6 +261,17 @@ TimelineModel::TimelineModel(TimelineViewManager *manager, QString room_id, QObj connect(&events, &EventStore::updateFlowEventId, this, [this](std::string event_id) { this->updateFlowEventId(event_id); }); + + const auto userid = utils::localUser().toStdString(); + http::client()->get_profile( + userid, [this](const mtx::responses::Profile &res, mtx::http::RequestErr err) { + if (err) { + nhlog::net()->warn("failed to retrieve own profile info"); + return; + } + + globalUsername = QString::fromStdString(res.display_name); + }); } QHash @@ -801,7 +813,12 @@ TimelineModel::viewDecryptedRawMessage(QString id) const void TimelineModel::openUserProfile(QString userid, bool global) { - emit openProfile(new UserProfile(global ? "" : room_id_, global ? utils::localUser() : userid, manager_, this)); + if (global) { + emit openProfile(new UserProfile("",utils::localUser(), + manager_, this, globalUsername)); + } else { + emit openProfile(new UserProfile(room_id_, userid, manager_, this)); + } } void diff --git a/src/timeline/TimelineModel.h b/src/timeline/TimelineModel.h index 51b8049e..e6cb7d3a 100644 --- a/src/timeline/TimelineModel.h +++ b/src/timeline/TimelineModel.h @@ -317,6 +317,7 @@ private: mutable EventStore events; QString room_id_; + QString globalUsername; bool decryptDescription = true; bool m_paginationInProgress = false; diff --git a/src/ui/UserProfile.cpp b/src/ui/UserProfile.cpp index 4b7f054d..a53d25f4 100644 --- a/src/ui/UserProfile.cpp +++ b/src/ui/UserProfile.cpp @@ -12,12 +12,14 @@ UserProfile::UserProfile(QString roomid, QString userid, TimelineViewManager *manager_, - TimelineModel *parent) + TimelineModel *parent, + QString globalUsername) : QObject(parent) , roomid_(roomid) , userid_(userid) , manager(manager_) , model(parent) + , globalUsername(globalUsername) { fetchDeviceList(this->userid_); @@ -98,7 +100,7 @@ UserProfile::userid() QString UserProfile::displayName() { - return cache::displayName(roomid_, userid_); + return globalUserProfile() ? globalUsername : cache::displayName(roomid_, userid_); } QString diff --git a/src/ui/UserProfile.h b/src/ui/UserProfile.h index 4839e0d8..04317766 100644 --- a/src/ui/UserProfile.h +++ b/src/ui/UserProfile.h @@ -94,7 +94,8 @@ public: UserProfile(QString roomid, QString userid, TimelineViewManager *manager_, - TimelineModel *parent = nullptr); + TimelineModel *parent = nullptr, + QString globalUsername = ""); DeviceInfoModel *deviceList(); @@ -119,11 +120,11 @@ public: signals: void userStatusChanged(); - void usernameEditingChanged(); private: QString roomid_, userid_; + QString globalUsername; DeviceInfoModel deviceList_; bool isUserVerified = false; bool hasMasterKey = false; From 3b82b2ff972e4f74904e315008b996202473570c Mon Sep 17 00:00:00 2001 From: Jedi18 Date: Thu, 28 Jan 2021 23:53:56 +0530 Subject: [PATCH 4/9] fix linting --- src/ChatPage.cpp | 2 +- src/timeline/TimelineModel.cpp | 4 ++-- src/ui/UserProfile.cpp | 4 ++-- src/ui/UserProfile.h | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 0d3c98a8..cf50e62a 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -111,7 +111,7 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) connect(sidebarActions_, &SideBarActions::joinRoom, this, &ChatPage::joinRoom); connect(sidebarActions_, &SideBarActions::createRoom, this, &ChatPage::createRoom); - user_info_widget_ = new UserInfoWidget(sideBar_); + user_info_widget_ = new UserInfoWidget(sideBar_); connect(user_info_widget_, &UserInfoWidget::openGlobalUserProfile, this, [this]() { view_manager_->activeTimeline()->openUserProfile("", true); }); diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index 41c0b40e..a25e77fd 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -814,8 +814,8 @@ void TimelineModel::openUserProfile(QString userid, bool global) { if (global) { - emit openProfile(new UserProfile("",utils::localUser(), - manager_, this, globalUsername)); + emit openProfile( + new UserProfile("", utils::localUser(), manager_, this, globalUsername)); } else { emit openProfile(new UserProfile(room_id_, userid, manager_, this)); } diff --git a/src/ui/UserProfile.cpp b/src/ui/UserProfile.cpp index a53d25f4..28757b72 100644 --- a/src/ui/UserProfile.cpp +++ b/src/ui/UserProfile.cpp @@ -4,10 +4,10 @@ #include "DeviceVerificationFlow.h" #include "Logging.h" #include "Utils.h" -#include #include "mtx/responses/crypto.hpp" #include "timeline/TimelineModel.h" #include "timeline/TimelineViewManager.h" +#include UserProfile::UserProfile(QString roomid, QString userid, @@ -228,7 +228,7 @@ UserProfile::changeUsername(QString username) if (globalUserProfile()) { // change global http::client()->set_displayname( - username.toStdString(), [this]( mtx::http::RequestErr err) { + username.toStdString(), [this](mtx::http::RequestErr err) { if (err) { nhlog::net()->warn("could not change username"); return; diff --git a/src/ui/UserProfile.h b/src/ui/UserProfile.h index 04317766..b1172077 100644 --- a/src/ui/UserProfile.h +++ b/src/ui/UserProfile.h @@ -94,7 +94,7 @@ public: UserProfile(QString roomid, QString userid, TimelineViewManager *manager_, - TimelineModel *parent = nullptr, + TimelineModel *parent = nullptr, QString globalUsername = ""); DeviceInfoModel *deviceList(); @@ -126,8 +126,8 @@ private: QString roomid_, userid_; QString globalUsername; DeviceInfoModel deviceList_; - bool isUserVerified = false; - bool hasMasterKey = false; + bool isUserVerified = false; + bool hasMasterKey = false; bool usernameEditing = false; TimelineViewManager *manager; TimelineModel *model; From fa7ad4f2344c72a8754a6d7b4489082fc81bb930 Mon Sep 17 00:00:00 2001 From: Jedi18 Date: Fri, 29 Jan 2021 00:09:11 +0530 Subject: [PATCH 5/9] Shifted fetching of global username fom timeline model to user profile --- src/timeline/TimelineModel.cpp | 15 +-------------- src/timeline/TimelineModel.h | 1 - src/ui/UserProfile.cpp | 29 ++++++++++++++++++++++++++--- src/ui/UserProfile.h | 10 +++++++--- 4 files changed, 34 insertions(+), 21 deletions(-) diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index a25e77fd..73077508 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -22,7 +22,6 @@ #include "TimelineViewManager.h" #include "Utils.h" #include "dialogs/RawMessage.h" -#include Q_DECLARE_METATYPE(QModelIndex) @@ -261,17 +260,6 @@ TimelineModel::TimelineModel(TimelineViewManager *manager, QString room_id, QObj connect(&events, &EventStore::updateFlowEventId, this, [this](std::string event_id) { this->updateFlowEventId(event_id); }); - - const auto userid = utils::localUser().toStdString(); - http::client()->get_profile( - userid, [this](const mtx::responses::Profile &res, mtx::http::RequestErr err) { - if (err) { - nhlog::net()->warn("failed to retrieve own profile info"); - return; - } - - globalUsername = QString::fromStdString(res.display_name); - }); } QHash @@ -814,8 +802,7 @@ void TimelineModel::openUserProfile(QString userid, bool global) { if (global) { - emit openProfile( - new UserProfile("", utils::localUser(), manager_, this, globalUsername)); + emit openProfile(new UserProfile("", utils::localUser(), manager_, this)); } else { emit openProfile(new UserProfile(room_id_, userid, manager_, this)); } diff --git a/src/timeline/TimelineModel.h b/src/timeline/TimelineModel.h index e6cb7d3a..51b8049e 100644 --- a/src/timeline/TimelineModel.h +++ b/src/timeline/TimelineModel.h @@ -317,7 +317,6 @@ private: mutable EventStore events; QString room_id_; - QString globalUsername; bool decryptDescription = true; bool m_paginationInProgress = false; diff --git a/src/ui/UserProfile.cpp b/src/ui/UserProfile.cpp index 28757b72..df404494 100644 --- a/src/ui/UserProfile.cpp +++ b/src/ui/UserProfile.cpp @@ -8,18 +8,17 @@ #include "timeline/TimelineModel.h" #include "timeline/TimelineViewManager.h" #include +#include UserProfile::UserProfile(QString roomid, QString userid, TimelineViewManager *manager_, - TimelineModel *parent, - QString globalUsername) + TimelineModel *parent) : QObject(parent) , roomid_(roomid) , userid_(userid) , manager(manager_) , model(parent) - , globalUsername(globalUsername) { fetchDeviceList(this->userid_); @@ -47,6 +46,23 @@ UserProfile::UserProfile(QString roomid, } deviceList_.reset(deviceList_.deviceList_); }); + + connect(this, + &UserProfile::globalUsernameRetrieved, + this, + &UserProfile::setGlobalUsername, + Qt::QueuedConnection); + + http::client()->get_profile( + userid_.toStdString(), + [this](const mtx::responses::Profile &res, mtx::http::RequestErr err) { + if (err) { + nhlog::net()->warn("failed to retrieve own profile info"); + return; + } + + emit globalUsernameRetrieved(QString::fromStdString(res.display_name)); + }); } QHash @@ -286,3 +302,10 @@ UserProfile::isUsernameEditingAllowed() const { return usernameEditing; } + +void +UserProfile::setGlobalUsername(const QString& globalUser) +{ + globalUsername = globalUser; + emit displayNameChanged(); +} \ No newline at end of file diff --git a/src/ui/UserProfile.h b/src/ui/UserProfile.h index b1172077..2a674732 100644 --- a/src/ui/UserProfile.h +++ b/src/ui/UserProfile.h @@ -79,7 +79,7 @@ private: class UserProfile : public QObject { Q_OBJECT - Q_PROPERTY(QString displayName READ displayName CONSTANT) + Q_PROPERTY(QString displayName READ displayName NOTIFY displayNameChanged) Q_PROPERTY(QString userid READ userid CONSTANT) Q_PROPERTY(QString avatarUrl READ avatarUrl CONSTANT) Q_PROPERTY(DeviceInfoModel *deviceList READ deviceList CONSTANT) @@ -94,8 +94,7 @@ public: UserProfile(QString roomid, QString userid, TimelineViewManager *manager_, - TimelineModel *parent = nullptr, - QString globalUsername = ""); + TimelineModel *parent = nullptr); DeviceInfoModel *deviceList(); @@ -121,6 +120,11 @@ public: signals: void userStatusChanged(); void usernameEditingChanged(); + void displayNameChanged(); + void globalUsernameRetrieved(const QString& globalUser); + +protected slots: + void setGlobalUsername(const QString &globalUser); private: QString roomid_, userid_; From 195bb0499bd8515517be8c9a6bab00a45bfc8dc5 Mon Sep 17 00:00:00 2001 From: Jedi18 Date: Fri, 29 Jan 2021 00:15:40 +0530 Subject: [PATCH 6/9] fix linting 2 --- src/ui/UserProfile.cpp | 4 ++-- src/ui/UserProfile.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ui/UserProfile.cpp b/src/ui/UserProfile.cpp index df404494..e2adffcd 100644 --- a/src/ui/UserProfile.cpp +++ b/src/ui/UserProfile.cpp @@ -7,8 +7,8 @@ #include "mtx/responses/crypto.hpp" #include "timeline/TimelineModel.h" #include "timeline/TimelineViewManager.h" -#include #include +#include UserProfile::UserProfile(QString roomid, QString userid, @@ -304,7 +304,7 @@ UserProfile::isUsernameEditingAllowed() const } void -UserProfile::setGlobalUsername(const QString& globalUser) +UserProfile::setGlobalUsername(const QString &globalUser) { globalUsername = globalUser; emit displayNameChanged(); diff --git a/src/ui/UserProfile.h b/src/ui/UserProfile.h index 2a674732..5ccb54b3 100644 --- a/src/ui/UserProfile.h +++ b/src/ui/UserProfile.h @@ -121,7 +121,7 @@ signals: void userStatusChanged(); void usernameEditingChanged(); void displayNameChanged(); - void globalUsernameRetrieved(const QString& globalUser); + void globalUsernameRetrieved(const QString &globalUser); protected slots: void setGlobalUsername(const QString &globalUser); From 9b5a287d14a18b1bef012a348dd77350fdb70ba3 Mon Sep 17 00:00:00 2001 From: Jedi18 Date: Fri, 29 Jan 2021 11:55:24 +0530 Subject: [PATCH 7/9] made requeste changes --- resources/qml/UserProfile.qml | 10 +++++----- src/ChatPage.cpp | 2 +- src/timeline/TimelineModel.cpp | 6 +----- src/ui/UserProfile.cpp | 8 ++++---- src/ui/UserProfile.h | 4 ++-- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/resources/qml/UserProfile.qml b/resources/qml/UserProfile.qml index 5e577099..79e2b665 100644 --- a/resources/qml/UserProfile.qml +++ b/resources/qml/UserProfile.qml @@ -15,7 +15,7 @@ ApplicationWindow { minimumHeight: 420 palette: colors color: colors.window - title: profile.globalUserProfile ? "Global User Profile" : "Room User Profile" + title: profile.isGlobalUserProfile ? "Global User Profile" : "Room User Profile" ColumnLayout { id: contentL @@ -44,19 +44,19 @@ ApplicationWindow { Layout.alignment: Qt.AlignHCenter selectByMouse: true - Keys.priority: Keys.BeforeItem - Keys.onReturnPressed: profile.changeUsername(displayUsername.text) + onAccepted: profile.changeUsername(displayUsername.text) ImageButton { + visible: profile.isSelf anchors.leftMargin: 5 anchors.left: displayUsername.right anchors.verticalCenter: displayUsername.verticalCenter image: profile.isUsernameEditingAllowed ? ":/icons/icons/ui/checkmark.png" : ":/icons/icons/ui/edit.png" onClicked: { - if(profile.isUsernameEditingAllowed) { + if (profile.isUsernameEditingAllowed) { profile.changeUsername(displayUsername.text) - }else{ + } else { profile.allowUsernameEditing(true) } } diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index cf50e62a..07ed3941 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -113,7 +113,7 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) user_info_widget_ = new UserInfoWidget(sideBar_); connect(user_info_widget_, &UserInfoWidget::openGlobalUserProfile, this, [this]() { - view_manager_->activeTimeline()->openUserProfile("", true); + view_manager_->activeTimeline()->openUserProfile(utils::localUser(), true); }); user_mentions_popup_ = new popups::UserMentions(); diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index 73077508..79cf5184 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -801,11 +801,7 @@ TimelineModel::viewDecryptedRawMessage(QString id) const void TimelineModel::openUserProfile(QString userid, bool global) { - if (global) { - emit openProfile(new UserProfile("", utils::localUser(), manager_, this)); - } else { - emit openProfile(new UserProfile(room_id_, userid, manager_, this)); - } + emit openProfile(new UserProfile(global ? "" : room_id_, userid, manager_, this)); } void diff --git a/src/ui/UserProfile.cpp b/src/ui/UserProfile.cpp index e2adffcd..9e8d45b5 100644 --- a/src/ui/UserProfile.cpp +++ b/src/ui/UserProfile.cpp @@ -116,7 +116,7 @@ UserProfile::userid() QString UserProfile::displayName() { - return globalUserProfile() ? globalUsername : cache::displayName(roomid_, userid_); + return isGlobalUserProfile() ? globalUsername : cache::displayName(roomid_, userid_); } QString @@ -126,9 +126,9 @@ UserProfile::avatarUrl() } bool -UserProfile::globalUserProfile() const +UserProfile::isGlobalUserProfile() const { - return (roomid_ == "") && isSelf(); + return roomid_ == ""; } bool @@ -241,7 +241,7 @@ UserProfile::startChat() void UserProfile::changeUsername(QString username) { - if (globalUserProfile()) { + if (isGlobalUserProfile()) { // change global http::client()->set_displayname( username.toStdString(), [this](mtx::http::RequestErr err) { diff --git a/src/ui/UserProfile.h b/src/ui/UserProfile.h index 5ccb54b3..d450d58f 100644 --- a/src/ui/UserProfile.h +++ b/src/ui/UserProfile.h @@ -83,7 +83,7 @@ class UserProfile : public QObject Q_PROPERTY(QString userid READ userid CONSTANT) Q_PROPERTY(QString avatarUrl READ avatarUrl CONSTANT) Q_PROPERTY(DeviceInfoModel *deviceList READ deviceList CONSTANT) - Q_PROPERTY(bool globalUserProfile READ globalUserProfile CONSTANT) + Q_PROPERTY(bool isGlobalUserProfile READ isGlobalUserProfile CONSTANT) Q_PROPERTY(bool isUserVerified READ getUserStatus NOTIFY userStatusChanged) Q_PROPERTY( bool userVerificationEnabled READ userVerificationEnabled NOTIFY userStatusChanged) @@ -101,7 +101,7 @@ public: QString userid(); QString displayName(); QString avatarUrl(); - bool globalUserProfile() const; + bool isGlobalUserProfile() const; bool getUserStatus(); bool userVerificationEnabled() const; bool isSelf() const; From e09e587796373d4cb2093689c82f9912e5f84c52 Mon Sep 17 00:00:00 2001 From: Jedi18 Date: Fri, 29 Jan 2021 12:06:38 +0530 Subject: [PATCH 8/9] shifted isUsernameEditingAllowed to qml from c++ --- resources/qml/UserProfile.qml | 17 ++++++++++++----- src/ui/UserProfile.cpp | 15 --------------- src/ui/UserProfile.h | 10 ++-------- 3 files changed, 14 insertions(+), 28 deletions(-) diff --git a/resources/qml/UserProfile.qml b/resources/qml/UserProfile.qml index 79e2b665..4442c97d 100644 --- a/resources/qml/UserProfile.qml +++ b/resources/qml/UserProfile.qml @@ -36,7 +36,10 @@ ApplicationWindow { TextInput { id: displayUsername - readOnly: !profile.isUsernameEditingAllowed + + property bool isUsernameEditingAllowed + + readOnly: !isUsernameEditingAllowed text: profile.displayName font.pixelSize: 20 color: TimelineManager.userColor(profile.userid, colors.window) @@ -44,20 +47,24 @@ ApplicationWindow { Layout.alignment: Qt.AlignHCenter selectByMouse: true - onAccepted: profile.changeUsername(displayUsername.text) + onAccepted: { + profile.changeUsername(displayUsername.text) + displayUsername.isUsernameEditingAllowed = false + } ImageButton { visible: profile.isSelf anchors.leftMargin: 5 anchors.left: displayUsername.right anchors.verticalCenter: displayUsername.verticalCenter - image: profile.isUsernameEditingAllowed ? ":/icons/icons/ui/checkmark.png" : ":/icons/icons/ui/edit.png" + image: displayUsername.isUsernameEditingAllowed ? ":/icons/icons/ui/checkmark.png" : ":/icons/icons/ui/edit.png" onClicked: { - if (profile.isUsernameEditingAllowed) { + if (displayUsername.isUsernameEditingAllowed) { profile.changeUsername(displayUsername.text) + displayUsername.isUsernameEditingAllowed = false } else { - profile.allowUsernameEditing(true) + displayUsername.isUsernameEditingAllowed = true } } } diff --git a/src/ui/UserProfile.cpp b/src/ui/UserProfile.cpp index 9e8d45b5..3872294a 100644 --- a/src/ui/UserProfile.cpp +++ b/src/ui/UserProfile.cpp @@ -270,8 +270,6 @@ UserProfile::changeUsername(QString username) err->matrix_error.error); }); } - - allowUsernameEditing(false); } void @@ -290,19 +288,6 @@ UserProfile::unverify(QString device) cache::markDeviceUnverified(userid_.toStdString(), device.toStdString()); } -void -UserProfile::allowUsernameEditing(bool allow) -{ - usernameEditing = allow; - emit usernameEditingChanged(); -} - -bool -UserProfile::isUsernameEditingAllowed() const -{ - return usernameEditing; -} - void UserProfile::setGlobalUsername(const QString &globalUser) { diff --git a/src/ui/UserProfile.h b/src/ui/UserProfile.h index d450d58f..11f588b6 100644 --- a/src/ui/UserProfile.h +++ b/src/ui/UserProfile.h @@ -88,8 +88,6 @@ class UserProfile : public QObject Q_PROPERTY( bool userVerificationEnabled READ userVerificationEnabled NOTIFY userStatusChanged) Q_PROPERTY(bool isSelf READ isSelf CONSTANT) - Q_PROPERTY( - bool isUsernameEditingAllowed READ isUsernameEditingAllowed NOTIFY usernameEditingChanged) public: UserProfile(QString roomid, QString userid, @@ -105,7 +103,6 @@ public: bool getUserStatus(); bool userVerificationEnabled() const; bool isSelf() const; - bool isUsernameEditingAllowed() const; Q_INVOKABLE void verify(QString device = ""); Q_INVOKABLE void unverify(QString device = ""); @@ -115,11 +112,9 @@ public: Q_INVOKABLE void kickUser(); Q_INVOKABLE void startChat(); Q_INVOKABLE void changeUsername(QString username); - Q_INVOKABLE void allowUsernameEditing(bool allow); signals: void userStatusChanged(); - void usernameEditingChanged(); void displayNameChanged(); void globalUsernameRetrieved(const QString &globalUser); @@ -130,9 +125,8 @@ private: QString roomid_, userid_; QString globalUsername; DeviceInfoModel deviceList_; - bool isUserVerified = false; - bool hasMasterKey = false; - bool usernameEditing = false; + bool isUserVerified = false; + bool hasMasterKey = false; TimelineViewManager *manager; TimelineModel *model; }; From 0ebb2947ef8010a2226237d65fb36fe847e8c5b2 Mon Sep 17 00:00:00 2001 From: Jedi18 Date: Sat, 30 Jan 2021 14:08:02 +0530 Subject: [PATCH 9/9] focus and select text on clicking edit button --- resources/qml/UserProfile.qml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/resources/qml/UserProfile.qml b/resources/qml/UserProfile.qml index 4442c97d..26dfc48c 100644 --- a/resources/qml/UserProfile.qml +++ b/resources/qml/UserProfile.qml @@ -65,6 +65,8 @@ ApplicationWindow { displayUsername.isUsernameEditingAllowed = false } else { displayUsername.isUsernameEditingAllowed = true + displayUsername.focus = true + displayUsername.selectAll() } } }