From 2417a37669a5f1d683a9495f5f145044231dd044 Mon Sep 17 00:00:00 2001
From: Nicolas Jager <nicolas.jager@savoirfairelinux.com>
Date: Mon, 30 Oct 2017 13:44:41 -0400
Subject: [PATCH] NewAccountModel : add profileUpdated signal
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

- Emitted when LocalProfileEditor::save() is called. Help clients
  to get warn when a profile was edited trough the legacy code.

- Use this signal the same way when the profile is edited trough
  the new lrc api.

Change-Id: I19d43cc10c7b42ea21a419a05e6ae8ba73ee24fa
Reviewed-by: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
---
 src/api/newaccountmodel.h      |  5 +++++
 src/localprofilecollection.cpp |  5 +++++
 src/newaccountmodel.cpp        | 12 ++++++++++++
 src/profilemodel.h             |  3 +++
 4 files changed, 25 insertions(+)

diff --git a/src/api/newaccountmodel.h b/src/api/newaccountmodel.h
index ea4b644a..a51cafea 100644
--- a/src/api/newaccountmodel.h
+++ b/src/api/newaccountmodel.h
@@ -84,6 +84,11 @@ Q_SIGNALS:
      * @param accountID
      */
     void accountRemoved(const std::string& accountID);
+    /**
+     * Connect this signal to know when an account was updated.
+     * @param accountID
+     */
+    void profileUpdated(const std::string& accountID);
 
 private:
     std::unique_ptr<NewAccountModelPimpl> pimpl_;
diff --git a/src/localprofilecollection.cpp b/src/localprofilecollection.cpp
index 6023a684..98a8d629 100644
--- a/src/localprofilecollection.cpp
+++ b/src/localprofilecollection.cpp
@@ -32,6 +32,7 @@
 #include "account.h"
 #include "accountmodel.h"
 #include "person.h"
+#include "profilemodel.h"
 
 class LocalProfileEditor final : public CollectionEditor<Profile>
 {
@@ -86,6 +87,10 @@ bool LocalProfileEditor::save(const Profile* pro)
 
     file.write(result);
     file.close();
+
+    // we need to bind the legacy lrc to the new one. We doing that by using profileUpdated
+    emit ProfileModel::instance().profileUpdated(pro);
+
     return true;
 }
 
diff --git a/src/newaccountmodel.cpp b/src/newaccountmodel.cpp
index b713f245..b215be38 100644
--- a/src/newaccountmodel.cpp
+++ b/src/newaccountmodel.cpp
@@ -30,6 +30,8 @@
 #include "database.h"
 
 #include "accountmodel.h"
+#include "profilemodel.h"
+#include "profile.h"
 
 // Dbus
 #include "dbus/configurationmanager.h"
@@ -74,6 +76,8 @@ public Q_SLOTS:
      * @param account
      */
     void slotAccountRemoved(Account* account);
+
+    void slotProfileUpdated(const Profile* profile);
 };
 
 NewAccountModel::NewAccountModel(Database& database,
@@ -127,6 +131,7 @@ NewAccountModelPimpl::NewAccountModelPimpl(NewAccountModel& linked,
 
     // NOTE: because we still use the legacy LRC for configuration, we are still using old signals
     connect(&AccountModel::instance(), &AccountModel::accountRemoved, this,  &NewAccountModelPimpl::slotAccountRemoved);
+    connect(&ProfileModel::instance(), &ProfileModel::profileUpdated, this,  &NewAccountModelPimpl::slotProfileUpdated);
 }
 
 NewAccountModelPimpl::~NewAccountModelPimpl()
@@ -197,6 +202,13 @@ NewAccountModelPimpl::slotAccountRemoved(Account* account)
     emit linked.accountRemoved(accountId);
 }
 
+
+void
+NewAccountModelPimpl::slotProfileUpdated(const Profile* profile)
+{
+    emit linked.profileUpdated(profile->accounts().first()->id().toStdString());
+}
+
 } // namespace lrc
 
 #include "api/moc_newaccountmodel.cpp"
diff --git a/src/profilemodel.h b/src/profilemodel.h
index 3b56aa57..593f59ae 100644
--- a/src/profilemodel.h
+++ b/src/profilemodel.h
@@ -80,6 +80,9 @@ private:
    virtual bool addItemCallback(const Profile* item) override;
    virtual bool removeItemCallback(const Profile* item) override;
 
+Q_SIGNALS:
+    bool profileUpdated(const Profile* item);
+
 public Q_SLOTS:
    bool remove(const QModelIndex& idx);
    bool add(Person* person = nullptr);
-- 
GitLab