Subject: [PATCH] [msyncd] Add method to enable/disable sync profiles for a specific service Ensure the profile is created if it does not already exist.
Author: Lionel Duboeuf <lionel@ouvaton.org>

---
--- a/msyncd/AccountsHelper.cpp
+++ b/msyncd/AccountsHelper.cpp
@@ -173,6 +173,42 @@
     account->selectService();
 }
 
+void AccountsHelper::syncEnableWithService(Accounts::Account *account, const QString &serviceName, bool enabled)
+{
+    account->selectService();
+    // Always use the current enabled value since signals may be emitted with delays.
+    bool accountEnabled = account->enabled();
+    bool serviceExists = false;
+    const QList<SyncProfile *> profiles = getProfilesByAccountId(account->id());
+    for (SyncProfile *profile : profiles) {
+
+        if (!profile) {
+            continue;
+        }
+
+        // Global account is disabled, unconditionally disable all profiles
+        if(!accountEnabled && profile->isEnabled()) {
+            profile->setEnabled(false);
+            iProfileManager.updateProfile(*profile);
+            emit removeScheduledSync(profile->name());
+        } else if (profile->key(REMOTE_SERVICE_NAME, "") == serviceName) {
+            serviceExists = true;
+            if (profile->isEnabled() != enabled) {
+                profile->setEnabled(enabled);
+                iProfileManager.updateProfile(*profile);
+                emit scheduleUpdated(profile->name());
+            }
+        }
+        delete profile;
+    }
+
+    if (!serviceExists && accountEnabled) {
+        addProfileForAccount(account, serviceName, enabled);
+    }
+
+    account->selectService();
+}
+
 void AccountsHelper::setSyncSchedule(SyncProfile *syncProfile, Accounts::AccountId id, bool aCreateNew)
 {
     FUNCTION_CALL_TRACE(lcButeoTrace);
@@ -336,9 +372,10 @@
     QObject::connect(account, &Accounts::Account::enabledChanged,
                      [this, account] (const QString & serviceName, bool enabled) {
         qCDebug(lcButeoMsyncd) << "Received account enabled changed signal" << serviceName << enabled << account->displayName();
-        syncEnableWithAccount(account);
+        syncEnableWithService(account, serviceName, enabled);
     });
 
+
 #ifndef USE_ACCOUNTSHELPER_SCHEDULER_WATCHER
     qCDebug(lcButeoMsyncd) << "AccountsHelper::registerAccountListener() is disabled!  Not listening to scheduler change signals for account:"
                            << id;
--- a/msyncd/AccountsHelper.h
+++ b/msyncd/AccountsHelper.h
@@ -89,6 +89,7 @@
 
 private:
     void syncEnableWithAccount(Accounts::Account *account);
+    void syncEnableWithService(Accounts::Account *account, const QString &serviceName, bool enabled);
     bool addProfileForAccount(Accounts::Account *account,
                               const QString &serviceName,
                               bool serviceEnabled,
