From 93445d3db7821d6d2e1fd163c4750f805165beac Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Blin?=
 <sebastien.blin@savoirfairelinux.com>
Date: Wed, 27 Jun 2018 09:39:09 -0400
Subject: [PATCH] newdevicemodel: set the whole config to avoid account
 deletion

When LRC call setAccountDetails without all config, this can leads
to the deletion of the account. Avoid this by giving the whole
config

Change-Id: Ida7f4c62bf23895ab33d3096c320c2561a52a620
Reviewed-by: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
---
 src/api/account.h       |  2 ++
 src/newaccountmodel.cpp |  4 ++++
 src/newdevicemodel.cpp  | 15 ++++++++++++---
 3 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/api/account.h b/src/api/account.h
index d2c08b0e..d9170888 100644
--- a/src/api/account.h
+++ b/src/api/account.h
@@ -102,6 +102,8 @@ struct ConfProperties_t {
     std::string             password;
     std::string             realm;
     std::string             localInterface;
+    std::string             deviceId;
+    std::string             deviceName;
     bool                    publishedSameAsLocal;
     int                     localPort;
     int                     publishedPort;
diff --git a/src/newaccountmodel.cpp b/src/newaccountmodel.cpp
index e1d42d96..78b9c9fb 100644
--- a/src/newaccountmodel.cpp
+++ b/src/newaccountmodel.cpp
@@ -492,6 +492,8 @@ account::Info::fromDetails(const MapStringString& details)
     confProperties.password                             = toStdString(details[ConfProperties::PASSWORD]);
     confProperties.realm                                = toStdString(details[ConfProperties::REALM]);
     confProperties.localInterface                       = toStdString(details[ConfProperties::LOCAL_INTERFACE]);
+    confProperties.deviceId                             = toStdString(details[ConfProperties::RING_DEVICE_ID]);
+    confProperties.deviceName                           = toStdString(details[ConfProperties::RING_DEVICE_NAME]);
     confProperties.publishedSameAsLocal                 = toBool(details[ConfProperties::PUBLISHED_SAMEAS_LOCAL]);
     confProperties.localPort                            = toInt(details[ConfProperties::LOCAL_PORT]);
     confProperties.publishedPort                        = toInt(details[ConfProperties::PUBLISHED_PORT]);
@@ -586,6 +588,8 @@ account::ConfProperties_t::toDetails() const
     details[ConfProperties::ROUTE]                      = toQString(this->routeset);
     details[ConfProperties::PASSWORD]                   = toQString(this->password);
     details[ConfProperties::REALM]                      = toQString(this->realm);
+    details[ConfProperties::RING_DEVICE_ID]             = toQString(this->deviceId);
+    details[ConfProperties::RING_DEVICE_NAME]           = toQString(this->deviceName);
     details[ConfProperties::LOCAL_INTERFACE]            = toQString(this->localInterface);
     details[ConfProperties::PUBLISHED_SAMEAS_LOCAL]     = toQString(this->publishedSameAsLocal);
     details[ConfProperties::LOCAL_PORT]                 = toQString(this->localPort);
diff --git a/src/newdevicemodel.cpp b/src/newdevicemodel.cpp
index dc916a71..47c7a5f2 100644
--- a/src/newdevicemodel.cpp
+++ b/src/newdevicemodel.cpp
@@ -22,6 +22,7 @@
 #include <mutex>
 
 // LRC
+#include "api/newaccountmodel.h"
 #include "callbackshandler.h"
 #include "dbus/configurationmanager.h"
 
@@ -107,9 +108,17 @@ NewDeviceModel::revokeDevice(const std::string& id, const std::string& password)
 void
 NewDeviceModel::setCurrentDeviceName(const std::string& newName)
 {
-    MapStringString details = {};
-    details[DRing::Account::ConfProperties::RING_DEVICE_NAME] = newName.c_str();
-    ConfigurationManager::instance().setAccountDetails(owner.id.c_str(), details);
+    // Update deamon config
+    auto config = owner.accountModel->getAccountConfig(owner.id);
+    config.deviceName = newName;
+    owner.accountModel->setAccountConfig(owner.id, config);
+    // Update model
+    std::lock_guard<std::mutex> lock(pimpl_->devicesMtx_);
+    for (auto& device : pimpl_->devices_) {
+        if (device.id == config.deviceId) {
+            device.name = newName;
+        }
+    }
 }
 
 NewDeviceModelPimpl::NewDeviceModelPimpl(const NewDeviceModel& linked, const CallbacksHandler& callbacksHandler)
-- 
GitLab