From 553879249eb31279c805936a31dd898eeb5d90f8 Mon Sep 17 00:00:00 2001
From: Alexandre Bourget <alexandre.bourget@savoirfairelinux.com>
Date: Tue, 2 Oct 2007 17:26:20 -0400
Subject: [PATCH] Automatic update of registration when receiving new ACCOUNT
 DETAILS.

It will register if it wasn't, and was now set to 'Enabled',

It will unregister if it was already registered, and we switched to 'Disabled'
---
 src/managerimpl.cpp | 38 ++++++++++++++++++++++----------------
 src/managerimpl.h   |  3 +++
 2 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/src/managerimpl.cpp b/src/managerimpl.cpp
index 4ca594b666..8169663785 100644
--- a/src/managerimpl.cpp
+++ b/src/managerimpl.cpp
@@ -429,11 +429,7 @@ ManagerImpl::initRegisterAccounts()
     if ( iter->second) {
       iter->second->loadConfig();
       if ( iter->second->isEnabled() ) {
-        if ( iter->second->init() ) {
-            iter->second->registerVoIPLink();
-        }
-        // init only the first account -- naahh..
-        //break;
+	iter->second->registerVoIPLink();
       }
     }
     iter++;
@@ -1713,6 +1709,26 @@ ManagerImpl::setAccountDetails( const ::DBus::String& accountID,
   }
   
   saveConfig();
+
+  /*
+   * register if it was just enabled, and we hadn't registered
+   * unregister if it was enabled/registered, and we want it closed
+   */
+  Account* acc = getAccount(accountID);
+
+  acc->loadConfig();
+  if (acc->isEnabled()) {
+    // Verify we aren't already registered, then register
+    if (acc->getRegistrationState() == VoIPLink::Unregistered) {
+      acc->registerVoIPLink();
+    }
+  } else {
+    // Verify we are already registered, then unregister
+    if (acc->getRegistrationState() == VoIPLink::Registered) {
+      acc->unregisterVoIPLink();
+    }
+  }
+
   /** @todo Make the daemon use the new settings */
   if (_dbus) _dbus->getConfigurationManager()->accountsChanged();
 }                   
@@ -1729,10 +1745,6 @@ ManagerImpl::addAccount(const std::map< ::DBus::String, ::DBus::String >& detail
   AccountID newAccountID = accountID.str();
   /** @todo Verify the uniqueness, in case a program adds accounts, two in a row. */
 
-  setAccountDetails(accountID.str(), details);
-
-  saveConfig();
-
   if (accountType == "SIP") {
     newAccount = AccountCreator::createAccount(AccountCreator::SIP_ACCOUNT, newAccountID);
   }
@@ -1745,13 +1757,7 @@ ManagerImpl::addAccount(const std::map< ::DBus::String, ::DBus::String >& detail
   }
   _accountMap[newAccountID] = newAccount;
 
-  // Get it up and running...
-  newAccount->loadConfig();
-  if (newAccount->isEnabled()) {
-    if (newAccount->init()) {
-      newAccount->registerVoIPLink();
-    }
-  }
+  setAccountDetails(accountID.str(), details);
 }
 
 void 
diff --git a/src/managerimpl.h b/src/managerimpl.h
index 0e45e2c21b..bb7e0ba365 100644
--- a/src/managerimpl.h
+++ b/src/managerimpl.h
@@ -236,6 +236,9 @@ public:
 
   /**
    * Save the details of an existing account, given the account ID
+   *
+   * This will load the configuration map with the given data.
+   * It will also register/unregister links where the 'Enabled' switched.
    */
   void setAccountDetails( const ::DBus::String& accountID, 
                    const std::map< ::DBus::String, ::DBus::String >& details );
-- 
GitLab