From a919eaa082ba6120f420e3f1531f8faebabc06da Mon Sep 17 00:00:00 2001
From: Tristan Matthews <tristan.matthews@savoirfairelinux.com>
Date: Fri, 2 Mar 2012 18:32:59 -0500
Subject: [PATCH] sipaccount: simplify IP2IP code

---
 daemon/src/dbus/configurationmanager.cpp |  6 +++---
 daemon/src/managerimpl.cpp               | 21 ++++++++++----------
 daemon/src/managerimpl.h                 |  2 ++
 daemon/src/sip/sipaccount.cpp            | 25 ++++++++++++++----------
 daemon/src/sip/sipaccount.h              |  5 +++++
 daemon/src/sip/sipvoiplink.cpp           |  4 ++--
 6 files changed, 38 insertions(+), 25 deletions(-)

diff --git a/daemon/src/dbus/configurationmanager.cpp b/daemon/src/dbus/configurationmanager.cpp
index f58680578c..40374d9943 100644
--- a/daemon/src/dbus/configurationmanager.cpp
+++ b/daemon/src/dbus/configurationmanager.cpp
@@ -50,7 +50,7 @@ ConfigurationManager::ConfigurationManager(DBus::Connection& connection) :
 std::map<std::string, std::string> ConfigurationManager::getIp2IpDetails()
 {
     std::map<std::string, std::string> ip2ipAccountDetails;
-    SIPAccount *sipaccount = static_cast<SIPAccount *>(Manager::instance().getAccount(SIPAccount::IP2IP_PROFILE));
+    SIPAccount *sipaccount = Manager::instance().getIP2IPAccount();
 
     if (!sipaccount) {
         ERROR("ConfigurationManager: could not find account");
@@ -100,7 +100,7 @@ std::map<std::string, std::string> ConfigurationManager::getTlsSettings()
 {
     std::map<std::string, std::string> tlsSettings;
 
-    SIPAccount *sipaccount = (SIPAccount *) Manager::instance().getAccount(SIPAccount::IP2IP_PROFILE);
+    SIPAccount *sipaccount = Manager::instance().getIP2IPAccount();
 
     if (!sipaccount)
         return tlsSettings;
@@ -110,7 +110,7 @@ std::map<std::string, std::string> ConfigurationManager::getTlsSettings()
 
 void ConfigurationManager::setTlsSettings(const std::map<std::string, std::string>& details)
 {
-    SIPAccount * sipaccount = (SIPAccount *) Manager::instance().getAccount(SIPAccount::IP2IP_PROFILE);
+    SIPAccount * sipaccount = Manager::instance().getIP2IPAccount();
 
     if (!sipaccount) {
         DEBUG("ConfigurationManager: Error: No valid account in set TLS settings");
diff --git a/daemon/src/managerimpl.cpp b/daemon/src/managerimpl.cpp
index cec8d75465..7ace5bc58a 100644
--- a/daemon/src/managerimpl.cpp
+++ b/daemon/src/managerimpl.cpp
@@ -1236,7 +1236,7 @@ void ManagerImpl::saveConfig()
 {
     DEBUG("Manager: Saving Configuration to XDG directory %s", path_.c_str());
     AudioLayer *audiolayer = getAudioDriver();
-    if(audiolayer != NULL) {
+    if (audiolayer != NULL) {
         audioPreference.setVolumemic(audiolayer->getCaptureGain());
         audioPreference.setVolumespkr(audiolayer->getPlaybackGain());
     }
@@ -1244,13 +1244,8 @@ void ManagerImpl::saveConfig()
     try {
         Conf::YamlEmitter emitter(path_.c_str());
 
-        for (AccountMap::iterator iter = accountMap_.begin(); iter != accountMap_.end(); ++iter) {
-            // Skip the "" account ID (which refer to the IP2IP account)
-            if (iter->first.empty())
-                continue;
-            else
-                iter->second->serialize(&emitter);
-        }
+        for (AccountMap::iterator iter = accountMap_.begin(); iter != accountMap_.end(); ++iter)
+            iter->second->serialize(&emitter);
 
         preferences.serialize(&emitter);
         voipPreferences.serialize(&emitter);
@@ -2663,14 +2658,20 @@ bool ManagerImpl::accountExists(const std::string &accountID)
     return accountMap_.find(accountID) != accountMap_.end();
 }
 
+SIPAccount*
+ManagerImpl::getIP2IPAccount()
+{
+    return static_cast<SIPAccount*>(accountMap_[SIPAccount::IP2IP_PROFILE]);
+}
+
 Account*
 ManagerImpl::getAccount(const std::string& accountID)
 {
     AccountMap::const_iterator iter = accountMap_.find(accountID);
     if (iter != accountMap_.end())
         return iter->second;
-
-    return getAccount(SIPAccount::IP2IP_PROFILE);
+    else
+        return accountMap_[SIPAccount::IP2IP_PROFILE];
 }
 
 std::string ManagerImpl::getAccountIdFromNameAndServer(const std::string& userName, const std::string& server) const
diff --git a/daemon/src/managerimpl.h b/daemon/src/managerimpl.h
index c8daee1dca..19ff01b301 100644
--- a/daemon/src/managerimpl.h
+++ b/daemon/src/managerimpl.h
@@ -73,6 +73,7 @@ class DNSService;
 #endif
 
 class Account;
+class SIPAccount;
 
 /** Define a type for a AccountMap container */
 typedef std::map<std::string, Account*> AccountMap;
@@ -1064,6 +1065,7 @@ class ManagerImpl {
          * @return Account*	 The account pointer or 0
          */
         Account* getAccount(const std::string& accountID);
+        SIPAccount* getIP2IPAccount();
 
         /** Return the std::string from a CallID
          * Protected by mutex
diff --git a/daemon/src/sip/sipaccount.cpp b/daemon/src/sip/sipaccount.cpp
index cb4123cc61..01d8427ef1 100644
--- a/daemon/src/sip/sipaccount.cpp
+++ b/daemon/src/sip/sipaccount.cpp
@@ -428,7 +428,7 @@ void SIPAccount::setAccountDetails(std::map<std::string, std::string> details)
 
     // TLS settings
     // The TLS listener is unique and globally defined through IP2IP_PROFILE
-    if (accountID_ == IP2IP_PROFILE)
+    if (isIP2IP())
         tlsListenerPort_ = atoi(details[CONFIG_TLS_LISTENER_PORT].c_str());
 
     tlsEnable_ = details[CONFIG_TLS_ENABLE];
@@ -462,7 +462,7 @@ std::map<std::string, std::string> SIPAccount::getAccountDetails() const
 
     a[CONFIG_ACCOUNT_ID] = accountID_;
     // The IP profile does not allow to set an alias
-    a[CONFIG_ACCOUNT_ALIAS] = (accountID_ == IP2IP_PROFILE) ? IP2IP_PROFILE : alias_;
+    a[CONFIG_ACCOUNT_ALIAS] = isIP2IP() ? IP2IP_PROFILE : alias_;
 
     a[CONFIG_ACCOUNT_ENABLE] = enabled_ ? "true" : "false";
     a[CONFIG_ACCOUNT_TYPE] = type_;
@@ -477,7 +477,7 @@ std::map<std::string, std::string> SIPAccount::getAccountDetails() const
     std::string registrationStateCode;
     std::string registrationStateDescription;
 
-    if (accountID_ == IP2IP_PROFILE)
+    if (isIP2IP())
         registrationStateDescription = "Direct IP call";
     else {
         state = registrationState_;
@@ -488,7 +488,7 @@ std::map<std::string, std::string> SIPAccount::getAccountDetails() const
         registrationStateDescription = registrationStateDetailed_.second;
     }
 
-    a[CONFIG_REGISTRATION_STATUS] = (accountID_ == IP2IP_PROFILE) ? "READY": mapStateNumberToString(state);
+    a[CONFIG_REGISTRATION_STATUS] = isIP2IP() ? "READY": mapStateNumberToString(state);
     a[CONFIG_REGISTRATION_STATE_CODE] = registrationStateCode;
     a[CONFIG_REGISTRATION_STATE_DESCRIPTION] = registrationStateDescription;
 
@@ -565,7 +565,7 @@ void SIPAccount::registerVoIPLink()
 
     // In our definition of the ip2ip profile (aka Direct IP Calls),
     // no registration should be performed
-    if (accountID_ == IP2IP_PROFILE)
+    if (isIP2IP())
         return;
 
     try {
@@ -577,7 +577,7 @@ void SIPAccount::registerVoIPLink()
 
 void SIPAccount::unregisterVoIPLink()
 {
-    if (accountID_ == IP2IP_PROFILE)
+    if (isIP2IP())
         return;
 
     try {
@@ -842,7 +842,7 @@ void SIPAccount::keepAliveRegistrationCb(UNUSED pj_timer_heap_t *th, pj_timer_en
     }
 
     // IP2IP default does not require keep-alive
-    if (sipAccount->getAccountID() == IP2IP_PROFILE)
+    if (sipAccount->isIP2IP())
         return; 
 
     // TLS is connection oriented and does not require keep-alive 
@@ -975,7 +975,7 @@ std::string SIPAccount::getUserAgentName() const
 
 std::map<std::string, std::string> SIPAccount::getIp2IpDetails() const
 {
-    assert(accountID_ == IP2IP_PROFILE);
+    assert(isIP2IP());
     std::map<std::string, std::string> ip2ipAccountDetails;
     ip2ipAccountDetails[CONFIG_ACCOUNT_ID] = IP2IP_PROFILE;
     ip2ipAccountDetails[CONFIG_SRTP_KEY_EXCHANGE] = srtpKeyExchange_;
@@ -1001,7 +1001,7 @@ std::map<std::string, std::string> SIPAccount::getIp2IpDetails() const
 std::map<std::string, std::string> SIPAccount::getTlsSettings() const
 {
     std::map<std::string, std::string> tlsSettings;
-    assert(accountID_ == IP2IP_PROFILE);
+    assert(isIP2IP());
 
     std::stringstream portstr;
     portstr << tlsListenerPort_;
@@ -1051,7 +1051,7 @@ void set_opt(const std::map<std::string, std::string> &details, const char *key,
 
 void SIPAccount::setTlsSettings(const std::map<std::string, std::string>& details)
 {
-    assert(accountID_ == IP2IP_PROFILE);
+    assert(isIP2IP());
     set_opt(details, CONFIG_TLS_LISTENER_PORT, tlsListenerPort_);
     set_opt(details, CONFIG_TLS_ENABLE, tlsEnable_);
     set_opt(details, CONFIG_TLS_CA_LIST_FILE, tlsCaListFile_);
@@ -1071,3 +1071,8 @@ VoIPLink* SIPAccount::getVoIPLink()
 {
     return link_;
 }
+
+bool SIPAccount::isIP2IP() const
+{
+    return accountID_ == IP2IP_PROFILE;
+}
diff --git a/daemon/src/sip/sipaccount.h b/daemon/src/sip/sipaccount.h
index ddf5390995..7f2596db83 100644
--- a/daemon/src/sip/sipaccount.h
+++ b/daemon/src/sip/sipaccount.h
@@ -122,6 +122,11 @@ class SIPAccount : public Account {
             registrationStateDetailed_ = details;
         }
 
+        /**
+         * Returns true if this is the IP2IP account
+         */
+        bool isIP2IP() const;
+
         /**
          * Serialize internal state of this account for configuration
          * @param YamlEmitter the configuration engine which generate the configuration file
diff --git a/daemon/src/sip/sipvoiplink.cpp b/daemon/src/sip/sipvoiplink.cpp
index 6c329dde49..9415cbe46f 100644
--- a/daemon/src/sip/sipvoiplink.cpp
+++ b/daemon/src/sip/sipvoiplink.cpp
@@ -1185,7 +1185,7 @@ SIPVoIPLink::getSIPCall(const std::string& id)
 
 bool SIPVoIPLink::SIPNewIpToIpCall(const std::string& id, const std::string& to)
 {
-    SIPAccount *account = dynamic_cast<SIPAccount *>(Manager::instance().getAccount(SIPAccount::IP2IP_PROFILE));
+    SIPAccount *account = Manager::instance().getIP2IPAccount();
 
     if (!account)
         return false;
@@ -1308,7 +1308,7 @@ void SIPVoIPLink::createDefaultSipUdpTransport()
     pj_uint16_t port = 0;
     int counter = 0;
 
-    SIPAccount *account = dynamic_cast<SIPAccount *>(Manager::instance().getAccount(SIPAccount::IP2IP_PROFILE));
+    SIPAccount *account = Manager::instance().getIP2IPAccount();
 
     pjsip_transport *transport = NULL;
     static const int DEFAULT_TRANSPORT_ATTEMPTS = 5;
-- 
GitLab