From f2ba34ca51e52fabed436a2966cb653f0337ecae Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Thu, 12 Aug 2021 16:36:52 -0400
Subject: [PATCH] accounts: introduce currentaccount

A structure to represent the currently selected account and it's
properties as observables.

Change-Id: I6b4ae92e15b492a4c4d61ec5da019e2c7e937401
---
 CMakeLists.txt                                |  6 +-
 src/currentaccount.cpp                        | 65 +++++++++++++++++++
 src/currentaccount.h                          | 49 ++++++++++++++
 src/mainview/components/AccountComboBox.qml   | 32 ++-------
 .../components/WelcomePageQrDialog.qml        |  8 +--
 src/qmlregister.cpp                           |  3 +
 6 files changed, 129 insertions(+), 34 deletions(-)
 create mode 100644 src/currentaccount.cpp
 create mode 100644 src/currentaccount.h

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 70e8291f4..ec83cb502 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -88,7 +88,8 @@ set(COMMON_SOURCES
     ${SRC_DIR}/filestosendlistmodel.cpp
     ${SRC_DIR}/wizardviewstepmodel.cpp
     ${SRC_DIR}/avatarregistry.cpp
-    ${SRC_DIR}/currentconversation.cpp)
+    ${SRC_DIR}/currentconversation.cpp
+    ${SRC_DIR}/currentaccount.cpp)
 
 set(COMMON_HEADERS
     ${SRC_DIR}/avatarimageprovider.h
@@ -147,7 +148,8 @@ set(COMMON_HEADERS
     ${SRC_DIR}/filestosendlistmodel.h
     ${SRC_DIR}/wizardviewstepmodel.h
     ${SRC_DIR}/avatarregistry.h
-    ${SRC_DIR}/currentconversation.h)
+    ${SRC_DIR}/currentconversation.h
+    ${SRC_DIR}/currentaccount.h)
 
 set(QML_LIBS
     Qt5::Quick
diff --git a/src/currentaccount.cpp b/src/currentaccount.cpp
new file mode 100644
index 000000000..1d6459607
--- /dev/null
+++ b/src/currentaccount.cpp
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2021 by Savoir-faire Linux
+ * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "currentaccount.h"
+
+CurrentAccount::CurrentAccount(LRCInstance* lrcInstance, QObject* parent)
+    : QObject(parent)
+    , lrcInstance_(lrcInstance)
+{
+    connect(&lrcInstance_->accountModel(),
+            &NewAccountModel::accountStatusChanged,
+            this,
+            &CurrentAccount::onAccountUpdated);
+
+    connect(&lrcInstance_->accountModel(),
+            &NewAccountModel::profileUpdated,
+            this,
+            &CurrentAccount::onAccountUpdated);
+
+    connect(lrcInstance_, &LRCInstance::currentAccountIdChanged, [this] { updateData(); });
+    updateData();
+}
+
+void
+CurrentAccount::onAccountUpdated(const QString& id)
+{
+    // filter for our currently set id
+    if (id_ != id)
+        return;
+    updateData();
+}
+
+void
+CurrentAccount::updateData()
+{
+    set_id(lrcInstance_->get_currentAccountId());
+    try {
+        const auto& accInfo = lrcInstance_->getAccountInfo(id_);
+        set_uri(accInfo.profileInfo.uri);
+        set_registeredName(accInfo.registeredName);
+        set_alias(accInfo.profileInfo.alias);
+        set_bestId(lrcInstance_->accountModel().bestIdForAccount(id_));
+        set_bestName(lrcInstance_->accountModel().bestNameForAccount(id_));
+        set_hasAvatarSet(!accInfo.profileInfo.avatar.isEmpty());
+        set_status(accInfo.status);
+        set_type(accInfo.profileInfo.type);
+    } catch (...) {
+        qWarning() << "Can't update current account data for" << id_;
+    }
+}
diff --git a/src/currentaccount.h b/src/currentaccount.h
new file mode 100644
index 000000000..a20fce987
--- /dev/null
+++ b/src/currentaccount.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2021 by Savoir-faire Linux
+ * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include "lrcinstance.h"
+
+#include <QObject>
+#include <QString>
+
+class CurrentAccount final : public QObject
+{
+    Q_OBJECT
+    QML_RO_PROPERTY(QString, id)
+    QML_RO_PROPERTY(QString, uri)
+    QML_RO_PROPERTY(QString, registeredName)
+    QML_RO_PROPERTY(QString, alias)
+    QML_RO_PROPERTY(QString, bestId)
+    QML_RO_PROPERTY(QString, bestName)
+    QML_RO_PROPERTY(bool, hasAvatarSet)
+    QML_RO_PROPERTY(lrc::api::account::Status, status)
+    QML_RO_PROPERTY(lrc::api::profile::Type, type)
+
+public:
+    explicit CurrentAccount(LRCInstance* lrcInstance, QObject* parent = nullptr);
+    ~CurrentAccount() = default;
+
+private Q_SLOTS:
+    void updateData();
+    void onAccountUpdated(const QString& id);
+
+private:
+    LRCInstance* lrcInstance_;
+};
diff --git a/src/mainview/components/AccountComboBox.qml b/src/mainview/components/AccountComboBox.qml
index 9a20ca23d..5208acfd0 100644
--- a/src/mainview/components/AccountComboBox.qml
+++ b/src/mainview/components/AccountComboBox.qml
@@ -103,20 +103,6 @@ Label {
         }
     }
 
-    Connections {
-        target: AccountListModel
-
-        function onModelReset() {
-            avatar.imageId = LRCInstance.currentAccountId
-            avatar.presenceStatus = AccountListModel.data(AccountListModel.index(0, 0),
-                                                          AccountList.Status)
-            userAliasText.text = AccountListModel.data(AccountListModel.index(0,0),
-                                                       AccountList.Alias)
-            usernameText.text = AccountListModel.data(AccountListModel.index(0,0),
-                                                      AccountList.Username)
-        }
-    }
-
     RowLayout {
         anchors.fill: parent
         anchors.leftMargin: 15
@@ -130,11 +116,9 @@ Label {
             Layout.preferredHeight: JamiTheme.accountListAvatarSize
             Layout.alignment: Qt.AlignVCenter
 
-            imageId: LRCInstance.currentAccountId
             mode: Avatar.Mode.Account
-
-            presenceStatus: AccountListModel.data(
-                                AccountListModel.index(0, 0), AccountList.Status)
+            imageId: CurrentAccount.id
+            presenceStatus: CurrentAccount.status
         }
 
         ColumnLayout {
@@ -143,28 +127,26 @@ Label {
             spacing: 2
 
             Text {
-                id: userAliasText
+                id: bestNameText
 
                 Layout.fillWidth: true
                 Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
 
-                text: AccountListModel.data(AccountListModel.index(0,0),
-                                            AccountList.Alias)
+                text: CurrentAccount.bestName
                 font.pointSize: JamiTheme.textFontSize
                 color: JamiTheme.textColor
                 elide: Text.ElideRight
             }
 
             Text {
-                id: usernameText
+                id: bestIdText
 
                 Layout.fillWidth: true
                 Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
 
-                visible: text.length && text != userAliasText.text
+                visible: text.length && text !== bestNameText.text
 
-                text:  AccountListModel.data(AccountListModel.index(0,0),
-                                             AccountList.Username)
+                text:  CurrentAccount.bestId
                 font.pointSize: JamiTheme.textFontSize
                 color: JamiTheme.faddedLastInteractionFontColor
                 elide: Text.ElideRight
diff --git a/src/mainview/components/WelcomePageQrDialog.qml b/src/mainview/components/WelcomePageQrDialog.qml
index f0e80738c..6bed1e1fe 100644
--- a/src/mainview/components/WelcomePageQrDialog.qml
+++ b/src/mainview/components/WelcomePageQrDialog.qml
@@ -22,7 +22,6 @@ import QtQuick.Layouts 1.14
 
 import net.jami.Models 1.0
 import net.jami.Adapters 1.0
-import net.jami.Constants 1.0
 
 import "../../commoncomponents"
 
@@ -49,12 +48,7 @@ ModalPopup {
             smooth: false
 
             fillMode: Image.PreserveAspectFit
-            source: {
-                if (LRCInstance.currentAccountId &&
-                        LRCInstance.currentAccountType === Profile.Type.JAMI)
-                    return "image://qrImage/account_" + LRCInstance.currentAccountId
-                return ""
-            }
+            source: "image://qrImage/account_" + CurrentAccount.id
         }
     }
 }
diff --git a/src/qmlregister.cpp b/src/qmlregister.cpp
index 07b9ce582..de7c8e6c8 100644
--- a/src/qmlregister.cpp
+++ b/src/qmlregister.cpp
@@ -28,6 +28,7 @@
 #include "utilsadapter.h"
 #include "conversationsadapter.h"
 #include "currentconversation.h"
+#include "currentaccount.h"
 
 #include "accountlistmodel.h"
 #include "accountstomigratelistmodel.h"
@@ -116,6 +117,7 @@ registerTypes(QQmlEngine* engine,
     auto settingsAdapter = new SettingsAdapter(appSettingsManager, lrcInstance, parent);
     auto pluginAdapter = new PluginAdapter(lrcInstance, parent);
     auto currentConversation = new CurrentConversation(lrcInstance, parent);
+    auto currentAccount = new CurrentAccount(lrcInstance, parent);
 
     // qml adapter registration
     QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, callAdapter, "CallAdapter");
@@ -128,6 +130,7 @@ registerTypes(QQmlEngine* engine,
     QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, settingsAdapter, "SettingsAdapter");
     QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, pluginAdapter, "PluginAdapter");
     QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, currentConversation, "CurrentConversation");
+    QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, currentAccount, "CurrentAccount");
 
     // TODO: remove these
     QML_REGISTERSINGLETONTYPE_CUSTOM(NS_MODELS, AVModel, &lrcInstance->avModel())
-- 
GitLab