From 07bede8a562a07555ab093458d596260ca8c9749 Mon Sep 17 00:00:00 2001 From: Nicolas Fella Date: Tue, 16 Jun 2020 14:06:30 +0200 Subject: [PATCH] Use properties to get user details Convert imperative QML code to declarative code using property bindings Signed-off-by: Nicolas Fella --- src/gui/tray/UserModel.cpp | 27 ++++++++++++++++----------- src/gui/tray/UserModel.h | 16 +++++++++++++--- src/gui/tray/Window.qml | 17 +++++------------ 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/gui/tray/UserModel.cpp b/src/gui/tray/UserModel.cpp index 2c656de67..2d60920b3 100644 --- a/src/gui/tray/UserModel.cpp +++ b/src/gui/tray/UserModel.cpp @@ -42,6 +42,9 @@ User::User(AccountStatePtr &account, const bool &isCurrent, QObject *parent) [=]() { if (isConnected()) {slotRefresh();} }); connect(_account.data(), &AccountState::hasFetchedNavigationApps, this, &User::slotRebuildNavigationAppList); + connect(_account->account().data(), &Account::accountChangedDisplayName, this, &User::nameChanged); + + connect(FolderMan::instance(), &FolderMan::folderListChanged, this, &User::hasLocalFolderChanged); } void User::slotBuildNotificationDisplay(const ActivityList &list) @@ -149,6 +152,7 @@ void User::slotRefreshNotifications() void User::slotRebuildNavigationAppList() { + emit serverHasTalkChanged(); // Rebuild App list UserAppsModel::instance()->buildAppList(); } @@ -399,7 +403,7 @@ void User::setCurrentUser(const bool &isCurrent) _isCurrentUser = isCurrent; } -Folder *User::getFolder() +Folder *User::getFolder() const { foreach (Folder *folder, FolderMan::instance()->map()) { if (folder->accountState() == _account.data()) { @@ -472,6 +476,11 @@ QImage User::avatar(bool whiteBg) const } } +bool User::hasLocalFolder() const +{ + return getFolder() != nullptr; +} + bool User::serverHasTalk() const { return _account->hasTalk(); @@ -544,7 +553,7 @@ Q_INVOKABLE int UserModel::numUsers() return _users.size(); } -Q_INVOKABLE int UserModel::currentUserId() +Q_INVOKABLE int UserModel::currentUserId() const { return _currentUserId; } @@ -579,15 +588,6 @@ QImage UserModel::avatarById(const int &id) return _users[id]->avatar(true); } -Q_INVOKABLE QString UserModel::currentUserName() -{ - if (_users.count() >= 1) { - return _users[_currentUserId]->name(); - } else { - return QString("No users"); - } -} - Q_INVOKABLE QString UserModel::currentUserServer() { if (_users.count() >= 1) { @@ -782,6 +782,11 @@ AccountAppList UserModel::appList() const } } +User *UserModel::currentUser() const +{ + return _users[currentUserId()]; +} + /*-------------------------------------------------------------------------------------*/ ImageProvider::ImageProvider() diff --git a/src/gui/tray/UserModel.h b/src/gui/tray/UserModel.h index 8245888f9..3a28e65e3 100644 --- a/src/gui/tray/UserModel.h +++ b/src/gui/tray/UserModel.h @@ -17,6 +17,10 @@ namespace OCC { class User : public QObject { Q_OBJECT + Q_PROPERTY(QString name READ name NOTIFY nameChanged) + Q_PROPERTY(QString server READ server CONSTANT) + Q_PROPERTY(bool hasLocalFolder READ hasLocalFolder NOTIFY hasLocalFolderChanged) + Q_PROPERTY(bool serverHasTalk READ serverHasTalk NOTIFY serverHasTalkChanged) public: User(AccountStatePtr &account, const bool &isCurrent = false, QObject* parent = 0); @@ -25,11 +29,12 @@ public: bool isConnected() const; bool isCurrentUser() const; void setCurrentUser(const bool &isCurrent); - Folder *getFolder(); + Folder *getFolder() const; ActivityListModel *getActivityModel(); void openLocalFolder(); QString name() const; QString server(bool shortened = true) const; + bool hasLocalFolder() const; bool serverHasTalk() const; bool hasActivities() const; AccountAppList appList() const; @@ -41,6 +46,9 @@ public: signals: void guiLog(const QString &, const QString &); + void nameChanged(); + void hasLocalFolderChanged(); + void serverHasTalkChanged(); public slots: void slotItemCompleted(const QString &folder, const SyncFileItemPtr &item); @@ -79,6 +87,7 @@ private: class UserModel : public QAbstractListModel { Q_OBJECT + Q_PROPERTY(User* currentUser READ currentUser NOTIFY newUserSelected) public: static UserModel *instance(); virtual ~UserModel() {}; @@ -92,18 +101,19 @@ public: QImage avatarById(const int &id); + User *currentUser() const; + Q_INVOKABLE void fetchCurrentActivityModel(); Q_INVOKABLE void openCurrentAccountLocalFolder(); Q_INVOKABLE void openCurrentAccountTalk(); Q_INVOKABLE void openCurrentAccountServer(); Q_INVOKABLE QImage currentUserAvatar(); Q_INVOKABLE int numUsers(); - Q_INVOKABLE QString currentUserName(); Q_INVOKABLE QString currentUserServer(); Q_INVOKABLE bool currentUserHasActivities(); Q_INVOKABLE bool currentUserHasLocalFolder(); Q_INVOKABLE bool currentServerHasTalk(); - Q_INVOKABLE int currentUserId(); + Q_INVOKABLE int currentUserId() const; Q_INVOKABLE bool isUserConnected(const int &id); Q_INVOKABLE void switchCurrentUser(const int &id); Q_INVOKABLE void login(const int &id); diff --git a/src/gui/tray/Window.qml b/src/gui/tray/Window.qml index 7d1f54cae..8128e4dd5 100644 --- a/src/gui/tray/Window.qml +++ b/src/gui/tray/Window.qml @@ -30,10 +30,6 @@ Window { onVisibleChanged: { currentAccountAvatar.source = "" currentAccountAvatar.source = "image://avatars/currentUser" - currentAccountUser.text = UserModel.currentUserName(); - currentAccountServer.text = UserModel.currentUserServer(); - openLocalFolderButton.visible = UserModel.currentUserHasLocalFolder(); - trayWindowTalkButton.visible = UserModel.currentServerHasTalk(); currentAccountStateIndicator.source = "" currentAccountStateIndicator.source = UserModel.isUserConnected(UserModel.currentUserId()) ? "qrc:///client/theme/colored/state-ok.svg" : "qrc:///client/theme/colored/state-offline.svg" @@ -48,15 +44,11 @@ Window { onRefreshCurrentUserGui: { currentAccountAvatar.source = "" currentAccountAvatar.source = "image://avatars/currentUser" - currentAccountUser.text = UserModel.currentUserName(); - currentAccountServer.text = UserModel.currentUserServer(); currentAccountStateIndicator.source = "" currentAccountStateIndicator.source = UserModel.isUserConnected(UserModel.currentUserId()) ? "qrc:///client/theme/colored/state-ok.svg" : "qrc:///client/theme/colored/state-offline.svg" } onNewUserSelected: { accountMenu.close(); - openLocalFolderButton.visible = UserModel.currentUserHasLocalFolder(); - trayWindowTalkButton.visible = UserModel.currentServerHasTalk(); } } @@ -327,8 +319,9 @@ Window { Layout.leftMargin: 6 Label { id: currentAccountUser + width: Style.currentAccountLabelWidth - text: UserModel.currentUserName() + text: UserModel.currentUser.name elide: Text.ElideRight color: "white" font.pixelSize: Style.topLinePixelSize @@ -337,7 +330,7 @@ Window { Label { id: currentAccountServer width: Style.currentAccountLabelWidth - text: UserModel.currentUserServer() + text: UserModel.currentUser.server elide: Text.ElideRight color: "white" font.pixelSize: Style.subLinePixelSize @@ -364,7 +357,7 @@ Window { HeaderButton { id: openLocalFolderButton - visible: UserModel.currentUserHasLocalFolder() + visible: UserModel.currentUser.hasLocalFolder icon.source: "qrc:///client/theme/white/folder.svg" onClicked: UserModel.openCurrentAccountLocalFolder() } @@ -372,7 +365,7 @@ Window { HeaderButton { id: trayWindowTalkButton - visible: UserModel.currentServerHasTalk() + visible: UserModel.currentUser.serverHasTalk icon.source: "qrc:///client/theme/white/talk-app.svg" onClicked: UserModel.openCurrentAccountTalk() }