From a737855426f3d6cd87fea44380e968c40736845b Mon Sep 17 00:00:00 2001
From: ababi <albert.babi@savoirfairelinux.com>
Date: Wed, 23 Sep 2020 12:54:48 +0200
Subject: [PATCH] misc: refactor account change

- UI sensitive to currentAccountId property, remove getCurrAccId from UtilsAdapter
- centralize signals / functions at MainView and avoid unnecessary propagation between modules
- name revision (avoid syntax such as "needTo" or "slot..")

Gitlab: #85
Gitlab: #86

Change-Id: Ie4060df4b64990bdd3f412d0e491080a4cd20092
---
 src/MainApplicationWindow.qml                 | 12 +----
 src/commoncomponents/PasswordDialog.qml       |  4 +-
 src/mainview/MainView.qml                     | 50 ++++++-------------
 src/mainview/components/AccountComboBox.qml   | 10 ----
 .../components/AccountComboBoxPopup.qml       |  8 ++-
 .../components/ConversationSmartListView.qml  |  4 +-
 .../ConversationSmartListViewItemDelegate.qml |  4 +-
 src/mainview/components/SidePanel.qml         |  8 ---
 src/settingsview/SettingsView.qml             |  4 +-
 .../components/CurrentAccountSettings.qml     |  6 ++-
 .../components/LinkDeviceDialog.qml           |  3 +-
 .../components/NameRegistrationDialog.qml     |  3 +-
 src/utilsadapter.cpp                          |  6 ---
 src/utilsadapter.h                            |  1 -
 src/wizardview/WizardView.qml                 |  2 +-
 15 files changed, 37 insertions(+), 88 deletions(-)

diff --git a/src/MainApplicationWindow.qml b/src/MainApplicationWindow.qml
index 632e0355a..c80d1fda0 100644
--- a/src/MainApplicationWindow.qml
+++ b/src/MainApplicationWindow.qml
@@ -42,11 +42,6 @@ ApplicationWindow {
         }
     }
 
-    function slotNewAccountAdded() {
-        if(mainViewLoader.newAddedAccountIndex !== -1)
-            mainViewLoader.item.newAccountAdded(mainViewLoader.newAddedAccountIndex)
-    }
-
     function startAccountMigration(){
         return accountMigrationDialog.startAccountMigrationOfTopStack()
     }
@@ -105,13 +100,8 @@ ApplicationWindow {
 
             onNeedToShowMainViewWindow: {
                 mainViewLoader.newAddedAccountIndex = accountIndex
-                if (mainViewLoader.source.toString() !== "qrc:/src/mainview/MainView.qml") {
-                    mainViewLoader.loaded.disconnect(slotNewAccountAdded)
-                    mainViewLoader.loaded.connect(slotNewAccountAdded)
+                if (mainViewLoader.source.toString() !== "qrc:/src/mainview/MainView.qml")
                     mainViewLoader.setSource("qrc:/src/mainview/MainView.qml")
-                } else {
-                    slotNewAccountAdded()
-                }
                 wizardView.close()
             }
 
diff --git a/src/commoncomponents/PasswordDialog.qml b/src/commoncomponents/PasswordDialog.qml
index 73027c741..6e58a5839 100644
--- a/src/commoncomponents/PasswordDialog.qml
+++ b/src/commoncomponents/PasswordDialog.qml
@@ -71,7 +71,7 @@ BaseDialog {
         var success = false
         if (path.length > 0) {
             success = AccountAdapter.exportToFile(
-                        UtilsAdapter.getCurrAccId(),
+                        AccountAdapter.currentAccountId,
                         path,
                         currentPasswordEdit.text)
         }
@@ -82,7 +82,7 @@ BaseDialog {
     function savePasswordQML() {
         var success = false
         success = AccountAdapter.savePassword(
-                    UtilsAdapter.getCurrAccId(),
+                    AccountAdapter.currentAccountId,
                     currentPasswordEdit.text,
                     passwordEdit.text)
         if (success) {
diff --git a/src/mainview/MainView.qml b/src/mainview/MainView.qml
index 98c4436c5..f66109394 100644
--- a/src/mainview/MainView.qml
+++ b/src/mainview/MainView.qml
@@ -58,6 +58,18 @@ Window {
     signal closeApp
     signal noAccountIsAvailable
 
+    property string currentAccountId: AccountAdapter.currentAccountId
+    onCurrentAccountIdChanged: {
+        var index = UtilsAdapter.getCurrAccList().indexOf(currentAccountId)
+        mainViewWindowSidePanel.refreshAccountComboBox(index)
+        if (inSettingsView) {
+            settingsView.accountListChanged()
+            settingsView.setSelected(settingsView.selectedMenu, true)
+        } else if (currentAccountIsCalling()) {
+            setCallStackView()
+        }
+    }
+
     function showWelcomeView() {
         mainViewWindowSidePanel.deselectConversationSmartList()
         if (communicationPageMessageWebView.visible || callStackView.visible) {
@@ -121,8 +133,8 @@ Window {
         }
     }
 
-    function newAccountAdded(index) {
-        AccountAdapter.accountChanged(index)
+    function startWizard() {
+        mainViewStackLayout.currentIndex = 1
     }
 
     function currentAccountIsCalling() {
@@ -311,23 +323,6 @@ Window {
                         toggleSettingsView()
                     }
 
-                    onAccountChanged: {
-                        mainViewWindowSidePanel.deselectConversationSmartList()
-
-                        AccountAdapter.accountChanged(index)
-
-                        if (inSettingsView) {
-                            settingsView.slotAccountListChanged()
-                            settingsView.setSelected(settingsView.selectedMenu, true)
-                        } else if (currentAccountIsCalling()) {
-                            setCallStackView()
-                        }
-                    }
-
-                    onNewAccountButtonClicked: {
-                        mainViewWindowSidePanel.needToAddNewAccount()
-                    }
-
                     Component.onCompleted: {
                         AccountAdapter.setQmlObject(this)
                     }
@@ -370,14 +365,6 @@ Window {
             Layout.fillHeight: true
 
             onNeedToShowMainViewWindow: {
-                mainViewLoader.newAddedAccountIndex = accountIndex
-                if (mainViewLoader.source.toString() !== "qrc:/src/mainview/MainView.qml") {
-                    mainViewLoader.loaded.disconnect(slotNewAccountAdded)
-                    mainViewLoader.loaded.connect(slotNewAccountAdded)
-                    mainViewLoader.setSource("qrc:/src/mainview/MainView.qml")
-                } else {
-                    slotNewAccountAdded()
-                }
                 mainViewStackLayout.currentIndex = 0
             }
 
@@ -438,12 +425,11 @@ Window {
         id: mainViewWindowSidePanel
 
         onConversationSmartListNeedToAccessMessageWebView: {
-
             communicationPageMessageWebView.headerUserAliasLabelText = currentUserAlias
             communicationPageMessageWebView.headerUserUserNameLabelText = currentUserDisplayName
 
             callStackView.needToCloseInCallConversationAndPotentialWindow()
-            callStackView.responsibleAccountId = UtilsAdapter.getCurrAccId()
+            callStackView.responsibleAccountId = AccountAdapter.currentAccountId
             callStackView.responsibleConvUid = currentUID
             callStackView.updateCorrespondingUI()
 
@@ -511,10 +497,6 @@ Window {
             mainViewWindowSidePanel.forceReselectConversationSmartListCurrentIndex()
         }
 
-        onNeedToAddNewAccount: {
-            mainViewStackLayout.currentIndex = 1
-        }
-
         Connections {
             target: ConversationsAdapter
 
@@ -747,7 +729,7 @@ Window {
     Shortcut {
         sequence: "Ctrl+Shift+N"
         context: Qt.ApplicationShortcut
-        onActivated: mainViewWindowSidePanel.needToAddNewAccount()
+        onActivated: startWizard()
     }
 
     KeyBoardShortcutTable {
diff --git a/src/mainview/components/AccountComboBox.qml b/src/mainview/components/AccountComboBox.qml
index 787b4d7e8..a826559e3 100644
--- a/src/mainview/components/AccountComboBox.qml
+++ b/src/mainview/components/AccountComboBox.qml
@@ -27,8 +27,6 @@ import "../../commoncomponents"
 ComboBox {
     id: root
 
-    signal accountChanged(int index)
-    signal newAccountButtonClicked
     signal settingBtnClicked
 
     // Reset accountListModel.
@@ -280,14 +278,6 @@ ComboBox {
                 comboBoxPopup.close() :
                 comboBoxPopup.open()
         }
-
-        onAccountNeedToChange: {
-            root.accountChanged(index)
-        }
-
-        onNewAccountButtonClicked: {
-            root.newAccountButtonClicked()
-        }
     }
 
     Shortcut {
diff --git a/src/mainview/components/AccountComboBoxPopup.qml b/src/mainview/components/AccountComboBoxPopup.qml
index 27abb5322..a76055d3a 100644
--- a/src/mainview/components/AccountComboBoxPopup.qml
+++ b/src/mainview/components/AccountComboBoxPopup.qml
@@ -20,6 +20,7 @@ import QtQuick 2.14
 import QtQuick.Controls 2.14
 import QtQuick.Layouts 1.14
 import net.jami.Models 1.0
+import net.jami.Adapters 1.0
 
 import "../../commoncomponents"
 
@@ -28,9 +29,6 @@ Popup {
 
     property bool toogleUpdatePopupHeight: false
 
-    signal accountNeedToChange(int index)
-    signal newAccountButtonClicked
-
     y: accountComboBox.height - 1
     implicitWidth: accountComboBox.width - 1
 
@@ -143,7 +141,7 @@ Popup {
                     itemCoboBackground.color = JamiTheme.releaseColor
                     currentIndex = index
                     comboBoxPopup.close()
-                    comboBoxPopup.accountNeedToChange(index)
+                    AccountAdapter.accountChanged(index)
                 }
                 onEntered: {
                     itemCoboBackground.color = JamiTheme.hoverColor
@@ -169,7 +167,7 @@ Popup {
 
             onClicked: {
                 comboBoxPopup.close()
-                comboBoxPopup.newAccountButtonClicked()
+                mainViewWindow.startWizard()
             }
         }
 
diff --git a/src/mainview/components/ConversationSmartListView.qml b/src/mainview/components/ConversationSmartListView.qml
index 113322d5a..5810d3cc2 100644
--- a/src/mainview/components/ConversationSmartListView.qml
+++ b/src/mainview/components/ConversationSmartListView.qml
@@ -114,7 +114,7 @@ ListView {
         context: Qt.ApplicationShortcut
         enabled: root.visible
         onActivated: {
-            UtilsAdapter.clearConversationHistory(UtilsAdapter.getCurrAccId(),
+            UtilsAdapter.clearConversationHistory(AccountAdapter.currentAccountId,
                                                   UtilsAdapter.getCurrConvId())
         }
     }
@@ -133,7 +133,7 @@ ListView {
         context: Qt.ApplicationShortcut
         enabled: root.visible
         onActivated: {
-            UtilsAdapter.removeConversation(UtilsAdapter.getCurrAccId(),
+            UtilsAdapter.removeConversation(AccountAdapter.currentAccountId,
                                             UtilsAdapter.getCurrConvId(),
                                             false)
         }
diff --git a/src/mainview/components/ConversationSmartListViewItemDelegate.qml b/src/mainview/components/ConversationSmartListViewItemDelegate.qml
index feb051a80..2b31840fe 100644
--- a/src/mainview/components/ConversationSmartListViewItemDelegate.qml
+++ b/src/mainview/components/ConversationSmartListViewItemDelegate.qml
@@ -170,7 +170,7 @@ ItemDelegate {
         }
         onDoubleClicked: {
             if (!InCall) {
-                ConversationsAdapter.selectConversation(UtilsAdapter.getCurrAccId(),
+                ConversationsAdapter.selectConversation(AccountAdapter.currentAccountId,
                                                         UID,
                                                         false)
                 CallAdapter.placeCall()
@@ -188,7 +188,7 @@ ItemDelegate {
                                                  mouse.x, mouse.y)
                 smartListContextMenu.x = relativeMousePos.x
                 smartListContextMenu.y = relativeMousePos.y
-                smartListContextMenu.responsibleAccountId = UtilsAdapter.getCurrAccId()
+                smartListContextMenu.responsibleAccountId = AccountAdapter.currentAccountId
                 smartListContextMenu.responsibleConvUid = UID
                 smartListContextMenu.contactType = ContactType
                 userProfile.responsibleConvUid = UID
diff --git a/src/mainview/components/SidePanel.qml b/src/mainview/components/SidePanel.qml
index 2de2e761e..6db11c38f 100644
--- a/src/mainview/components/SidePanel.qml
+++ b/src/mainview/components/SidePanel.qml
@@ -33,16 +33,8 @@ Rectangle {
     property int pendingRequestCount: 0
     property int totalUnreadMessagesCount: 0
 
-    property string currentAccountId: AccountAdapter.currentAccountId
-    onCurrentAccountIdChanged: {
-        var index = UtilsAdapter.getCurrAccList().indexOf(currentAccountId)
-        refreshAccountComboBox(index)
-    }
-
     signal conversationSmartListNeedToAccessMessageWebView(string currentUserDisplayName, string currentUserAlias, string currentUID, bool callStackViewShouldShow, bool isAudioOnly, int callState)
     signal needToUpdateConversationForAddedContact
-    signal needToAddNewAccount
-
 
     // Hack -> force redraw.
     function forceReselectConversationSmartListCurrentIndex() {
diff --git a/src/settingsview/SettingsView.qml b/src/settingsview/SettingsView.qml
index 80520511c..dc41ecd0b 100644
--- a/src/settingsview/SettingsView.qml
+++ b/src/settingsview/SettingsView.qml
@@ -96,7 +96,7 @@ Rectangle {
         target: LRCInstance
 
         function onAccountListChanged() {
-            slotAccountListChanged()
+            accountListChanged()
         }
     }
 
@@ -111,7 +111,7 @@ Rectangle {
             settingsViewWindowNeedToShowNewWizardWindow()
     }
 
-    function slotAccountListChanged() {
+    function accountListChanged() {
         var accountList = AccountAdapter.model.getAccountList()
         if(accountList.length === 0)
             return
diff --git a/src/settingsview/components/CurrentAccountSettings.qml b/src/settingsview/components/CurrentAccountSettings.qml
index c72a3bdeb..36a2fd87f 100644
--- a/src/settingsview/components/CurrentAccountSettings.qml
+++ b/src/settingsview/components/CurrentAccountSettings.qml
@@ -163,7 +163,8 @@ Rectangle {
                 return
             } else {
                 if (exportPath.length > 0) {
-                    var isSuccessful = AccountAdapter.model.exportToFile(UtilsAdapter.getCurrAccId(), exportPath,"")
+                    var isSuccessful = AccountAdapter.model.exportToFile(AccountAdapter.currentAccountId,
+                                                                         exportPath, "")
                     var title = isSuccessful ? qsTr("Success") : qsTr("Error")
                     var info = isSuccessful ? JamiStrings.backupSuccessful : JamiStrings.backupFailed
 
@@ -218,7 +219,8 @@ Rectangle {
                     labelText: JamiStrings.enableAccount
                     fontPointSize: JamiTheme.headerFontSize
 
-                    onSwitchToggled: AccountAdapter.model.setAccountEnabled(UtilsAdapter.getCurrAccId(), checked)
+                    onSwitchToggled: AccountAdapter.model.setAccountEnabled(
+                                         AccountAdapter.currentAccountId, checked)
                 }
 
                 AccountProfile {
diff --git a/src/settingsview/components/LinkDeviceDialog.qml b/src/settingsview/components/LinkDeviceDialog.qml
index 56581d975..2f8af5dcc 100644
--- a/src/settingsview/components/LinkDeviceDialog.qml
+++ b/src/settingsview/components/LinkDeviceDialog.qml
@@ -54,7 +54,8 @@ BaseDialog {
     }
 
     function slotExportOnRing(){
-        AccountAdapter.model.exportOnRing(UtilsAdapter.getCurrAccId(),passwordEdit.text)
+        AccountAdapter.model.exportOnRing(AccountAdapter.currentAccountId,
+                                          passwordEdit.text)
     }
 
     Timer{
diff --git a/src/settingsview/components/NameRegistrationDialog.qml b/src/settingsview/components/NameRegistrationDialog.qml
index 48d095bb4..e69dd0734 100644
--- a/src/settingsview/components/NameRegistrationDialog.qml
+++ b/src/settingsview/components/NameRegistrationDialog.qml
@@ -53,7 +53,8 @@ BaseDialog {
 
     function slotStartNameRegistration() {
         var password = passwordEdit.text
-        AccountAdapter.model.registerName(UtilsAdapter.getCurrAccId(), password, registerdName)
+        AccountAdapter.model.registerName(AccountAdapter.currentAccountId,
+                                          password, registerdName)
     }
 
     function startSpinner() {
diff --git a/src/utilsadapter.cpp b/src/utilsadapter.cpp
index 81eaaf805..309f8f99b 100644
--- a/src/utilsadapter.cpp
+++ b/src/utilsadapter.cpp
@@ -175,12 +175,6 @@ UtilsAdapter::removeConversation(const QString& accountId, const QString& uid, b
     LRCInstance::getAccountInfo(accountId).conversationModel->removeConversation(uid, banContact);
 }
 
-const QString
-UtilsAdapter::getCurrAccId()
-{
-    return LRCInstance::getCurrAccId();
-}
-
 const QString
 UtilsAdapter::getCurrConvId()
 {
diff --git a/src/utilsadapter.h b/src/utilsadapter.h
index 1d955312a..9a30e12cf 100644
--- a/src/utilsadapter.h
+++ b/src/utilsadapter.h
@@ -55,7 +55,6 @@ public:
     Q_INVOKABLE const QString getBestName(const QString& accountId, const QString& uid);
     Q_INVOKABLE QString getBestId(const QString& accountId);
     Q_INVOKABLE const QString getBestId(const QString& accountId, const QString& uid);
-    Q_INVOKABLE const QString getCurrAccId();
     Q_INVOKABLE const QString getCurrConvId();
     Q_INVOKABLE void makePermanentCurrentConv();
     Q_INVOKABLE const QStringList getCurrAccList();
diff --git a/src/wizardview/WizardView.qml b/src/wizardview/WizardView.qml
index cb272a185..7d3cf5cd7 100644
--- a/src/wizardview/WizardView.qml
+++ b/src/wizardview/WizardView.qml
@@ -269,7 +269,7 @@ Rectangle {
                     } else {
                         if (folderDir.length > 0) {
                             AccountAdapter.exportToFile(
-                                        UtilsAdapter.getCurrAccId(),
+                                        AccountAdapter.currentAccountId,
                                         UtilsAdapter.getAbsPath(folderDir))
                         }
                     }
-- 
GitLab