Use properties to get user details

Convert imperative QML code to declarative code using property bindings

Signed-off-by: Nicolas Fella <nicolas.fella@gmx.de>
This commit is contained in:
Nicolas Fella 2020-06-16 14:06:30 +02:00 committed by Kevin Ottens
parent a12205f322
commit 07bede8a56
3 changed files with 34 additions and 26 deletions

View file

@ -42,6 +42,9 @@ User::User(AccountStatePtr &account, const bool &isCurrent, QObject *parent)
[=]() { if (isConnected()) {slotRefresh();} }); [=]() { if (isConnected()) {slotRefresh();} });
connect(_account.data(), &AccountState::hasFetchedNavigationApps, connect(_account.data(), &AccountState::hasFetchedNavigationApps,
this, &User::slotRebuildNavigationAppList); 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) void User::slotBuildNotificationDisplay(const ActivityList &list)
@ -149,6 +152,7 @@ void User::slotRefreshNotifications()
void User::slotRebuildNavigationAppList() void User::slotRebuildNavigationAppList()
{ {
emit serverHasTalkChanged();
// Rebuild App list // Rebuild App list
UserAppsModel::instance()->buildAppList(); UserAppsModel::instance()->buildAppList();
} }
@ -399,7 +403,7 @@ void User::setCurrentUser(const bool &isCurrent)
_isCurrentUser = isCurrent; _isCurrentUser = isCurrent;
} }
Folder *User::getFolder() Folder *User::getFolder() const
{ {
foreach (Folder *folder, FolderMan::instance()->map()) { foreach (Folder *folder, FolderMan::instance()->map()) {
if (folder->accountState() == _account.data()) { 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 bool User::serverHasTalk() const
{ {
return _account->hasTalk(); return _account->hasTalk();
@ -544,7 +553,7 @@ Q_INVOKABLE int UserModel::numUsers()
return _users.size(); return _users.size();
} }
Q_INVOKABLE int UserModel::currentUserId() Q_INVOKABLE int UserModel::currentUserId() const
{ {
return _currentUserId; return _currentUserId;
} }
@ -579,15 +588,6 @@ QImage UserModel::avatarById(const int &id)
return _users[id]->avatar(true); 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() Q_INVOKABLE QString UserModel::currentUserServer()
{ {
if (_users.count() >= 1) { if (_users.count() >= 1) {
@ -782,6 +782,11 @@ AccountAppList UserModel::appList() const
} }
} }
User *UserModel::currentUser() const
{
return _users[currentUserId()];
}
/*-------------------------------------------------------------------------------------*/ /*-------------------------------------------------------------------------------------*/
ImageProvider::ImageProvider() ImageProvider::ImageProvider()

View file

@ -17,6 +17,10 @@ namespace OCC {
class User : public QObject class User : public QObject
{ {
Q_OBJECT 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: public:
User(AccountStatePtr &account, const bool &isCurrent = false, QObject* parent = 0); User(AccountStatePtr &account, const bool &isCurrent = false, QObject* parent = 0);
@ -25,11 +29,12 @@ public:
bool isConnected() const; bool isConnected() const;
bool isCurrentUser() const; bool isCurrentUser() const;
void setCurrentUser(const bool &isCurrent); void setCurrentUser(const bool &isCurrent);
Folder *getFolder(); Folder *getFolder() const;
ActivityListModel *getActivityModel(); ActivityListModel *getActivityModel();
void openLocalFolder(); void openLocalFolder();
QString name() const; QString name() const;
QString server(bool shortened = true) const; QString server(bool shortened = true) const;
bool hasLocalFolder() const;
bool serverHasTalk() const; bool serverHasTalk() const;
bool hasActivities() const; bool hasActivities() const;
AccountAppList appList() const; AccountAppList appList() const;
@ -41,6 +46,9 @@ public:
signals: signals:
void guiLog(const QString &, const QString &); void guiLog(const QString &, const QString &);
void nameChanged();
void hasLocalFolderChanged();
void serverHasTalkChanged();
public slots: public slots:
void slotItemCompleted(const QString &folder, const SyncFileItemPtr &item); void slotItemCompleted(const QString &folder, const SyncFileItemPtr &item);
@ -79,6 +87,7 @@ private:
class UserModel : public QAbstractListModel class UserModel : public QAbstractListModel
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(User* currentUser READ currentUser NOTIFY newUserSelected)
public: public:
static UserModel *instance(); static UserModel *instance();
virtual ~UserModel() {}; virtual ~UserModel() {};
@ -92,18 +101,19 @@ public:
QImage avatarById(const int &id); QImage avatarById(const int &id);
User *currentUser() const;
Q_INVOKABLE void fetchCurrentActivityModel(); Q_INVOKABLE void fetchCurrentActivityModel();
Q_INVOKABLE void openCurrentAccountLocalFolder(); Q_INVOKABLE void openCurrentAccountLocalFolder();
Q_INVOKABLE void openCurrentAccountTalk(); Q_INVOKABLE void openCurrentAccountTalk();
Q_INVOKABLE void openCurrentAccountServer(); Q_INVOKABLE void openCurrentAccountServer();
Q_INVOKABLE QImage currentUserAvatar(); Q_INVOKABLE QImage currentUserAvatar();
Q_INVOKABLE int numUsers(); Q_INVOKABLE int numUsers();
Q_INVOKABLE QString currentUserName();
Q_INVOKABLE QString currentUserServer(); Q_INVOKABLE QString currentUserServer();
Q_INVOKABLE bool currentUserHasActivities(); Q_INVOKABLE bool currentUserHasActivities();
Q_INVOKABLE bool currentUserHasLocalFolder(); Q_INVOKABLE bool currentUserHasLocalFolder();
Q_INVOKABLE bool currentServerHasTalk(); Q_INVOKABLE bool currentServerHasTalk();
Q_INVOKABLE int currentUserId(); Q_INVOKABLE int currentUserId() const;
Q_INVOKABLE bool isUserConnected(const int &id); Q_INVOKABLE bool isUserConnected(const int &id);
Q_INVOKABLE void switchCurrentUser(const int &id); Q_INVOKABLE void switchCurrentUser(const int &id);
Q_INVOKABLE void login(const int &id); Q_INVOKABLE void login(const int &id);

View file

@ -30,10 +30,6 @@ Window {
onVisibleChanged: { onVisibleChanged: {
currentAccountAvatar.source = "" currentAccountAvatar.source = ""
currentAccountAvatar.source = "image://avatars/currentUser" 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 = ""
currentAccountStateIndicator.source = UserModel.isUserConnected(UserModel.currentUserId()) ? "qrc:///client/theme/colored/state-ok.svg" : "qrc:///client/theme/colored/state-offline.svg" 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: { onRefreshCurrentUserGui: {
currentAccountAvatar.source = "" currentAccountAvatar.source = ""
currentAccountAvatar.source = "image://avatars/currentUser" currentAccountAvatar.source = "image://avatars/currentUser"
currentAccountUser.text = UserModel.currentUserName();
currentAccountServer.text = UserModel.currentUserServer();
currentAccountStateIndicator.source = "" currentAccountStateIndicator.source = ""
currentAccountStateIndicator.source = UserModel.isUserConnected(UserModel.currentUserId()) ? "qrc:///client/theme/colored/state-ok.svg" : "qrc:///client/theme/colored/state-offline.svg" currentAccountStateIndicator.source = UserModel.isUserConnected(UserModel.currentUserId()) ? "qrc:///client/theme/colored/state-ok.svg" : "qrc:///client/theme/colored/state-offline.svg"
} }
onNewUserSelected: { onNewUserSelected: {
accountMenu.close(); accountMenu.close();
openLocalFolderButton.visible = UserModel.currentUserHasLocalFolder();
trayWindowTalkButton.visible = UserModel.currentServerHasTalk();
} }
} }
@ -327,8 +319,9 @@ Window {
Layout.leftMargin: 6 Layout.leftMargin: 6
Label { Label {
id: currentAccountUser id: currentAccountUser
width: Style.currentAccountLabelWidth width: Style.currentAccountLabelWidth
text: UserModel.currentUserName() text: UserModel.currentUser.name
elide: Text.ElideRight elide: Text.ElideRight
color: "white" color: "white"
font.pixelSize: Style.topLinePixelSize font.pixelSize: Style.topLinePixelSize
@ -337,7 +330,7 @@ Window {
Label { Label {
id: currentAccountServer id: currentAccountServer
width: Style.currentAccountLabelWidth width: Style.currentAccountLabelWidth
text: UserModel.currentUserServer() text: UserModel.currentUser.server
elide: Text.ElideRight elide: Text.ElideRight
color: "white" color: "white"
font.pixelSize: Style.subLinePixelSize font.pixelSize: Style.subLinePixelSize
@ -364,7 +357,7 @@ Window {
HeaderButton { HeaderButton {
id: openLocalFolderButton id: openLocalFolderButton
visible: UserModel.currentUserHasLocalFolder() visible: UserModel.currentUser.hasLocalFolder
icon.source: "qrc:///client/theme/white/folder.svg" icon.source: "qrc:///client/theme/white/folder.svg"
onClicked: UserModel.openCurrentAccountLocalFolder() onClicked: UserModel.openCurrentAccountLocalFolder()
} }
@ -372,7 +365,7 @@ Window {
HeaderButton { HeaderButton {
id: trayWindowTalkButton id: trayWindowTalkButton
visible: UserModel.currentServerHasTalk() visible: UserModel.currentUser.serverHasTalk
icon.source: "qrc:///client/theme/white/talk-app.svg" icon.source: "qrc:///client/theme/white/talk-app.svg"
onClicked: UserModel.openCurrentAccountTalk() onClicked: UserModel.openCurrentAccountTalk()
} }