From b66eea033bcff5b2adc5eddb9f5c0b44e8ee5e31 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Thu, 5 Jul 2018 10:54:01 -0400
Subject: [PATCH] newaccountmodel: use setAccountsOrder from daemon to save top
 account

Change-Id: I51a895fc495e5a6b841f7425df321fc3adb7bfde
Reviewed-by: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
---
 src/api/newaccountmodel.h |  5 +++++
 src/newaccountmodel.cpp   | 29 ++++++++++++++++++++++++++---
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/src/api/newaccountmodel.h b/src/api/newaccountmodel.h
index 3184e7ac..6e197523 100644
--- a/src/api/newaccountmodel.h
+++ b/src/api/newaccountmodel.h
@@ -163,6 +163,11 @@ public:
                                         const std::string& password = "",
                                         const std::string& pin = "");
 
+    /**
+     * Set an account to the first position
+     */
+    void setTopAccount(const std::string& accountId);
+
 Q_SIGNALS:
     /**
      * Connect this signal to know when an invalid account is here
diff --git a/src/newaccountmodel.cpp b/src/newaccountmodel.cpp
index 7c6d7077..4eabda7b 100644
--- a/src/newaccountmodel.cpp
+++ b/src/newaccountmodel.cpp
@@ -140,11 +140,13 @@ std::vector<std::string>
 NewAccountModel::getAccountList() const
 {
     std::vector<std::string> accountsId;
+    const QStringList accountIds = ConfigurationManager::instance().getAccountList();
 
-    for(auto const& accountInfo: pimpl_->accounts) {
+    for (auto const& id : accountIds) {
+        auto accountInfo = pimpl_->accounts.find(id.toStdString());
         // Do not include accounts flagged for removal
-        if (accountInfo.second.valid)
-            accountsId.emplace_back(accountInfo.first);
+        if (accountInfo != pimpl_->accounts.end() && accountInfo->second.valid)
+            accountsId.emplace_back(id.toStdString());
     }
 
     return accountsId;
@@ -712,6 +714,27 @@ NewAccountModel::createNewAccount(profile::Type type,
     return accountId.toStdString();
 }
 
+void
+NewAccountModel::setTopAccount(const std::string& accountId)
+{
+    bool found = false;
+    std::string order = {};
+
+    const QStringList accountIds = ConfigurationManager::instance().getAccountList();
+    for (auto& id : accountIds)
+    {
+        if (id.toStdString() == accountId) {
+            found = true;
+        } else {
+            order += id.toStdString() + "/";
+        }
+    }
+    if (found) {
+        order = accountId + "/" + order;
+    }
+    ConfigurationManager::instance().setAccountsOrder(order.c_str());
+}
+
 } // namespace lrc
 
 #include "api/moc_newaccountmodel.cpp"
-- 
GitLab