From 652f000d23f1ca38860c58162cc37642ff5f1763 Mon Sep 17 00:00:00 2001
From: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
Date: Thu, 8 Jul 2010 09:35:15 -0400
Subject: [PATCH] [#3647] Fix yaml emitter

---
 sflphone-common/src/config/yamlemitter.cpp | 75 ++++++++++++++++------
 sflphone-common/src/config/yamlemitter.h   |  4 +-
 sflphone-common/src/config/yamlparser.cpp  |  0
 sflphone-common/src/config/yamlparser.h    |  0
 sflphone-common/src/managerimpl.cpp        |  7 +-
 sflphone-common/src/preferences.cpp        |  2 +-
 sflphone-common/src/sip/sipaccount.cpp     |  6 ++
 7 files changed, 69 insertions(+), 25 deletions(-)
 mode change 100755 => 100644 sflphone-common/src/config/yamlparser.cpp
 mode change 100755 => 100644 sflphone-common/src/config/yamlparser.h
 mode change 100755 => 100644 sflphone-common/src/managerimpl.cpp
 mode change 100755 => 100644 sflphone-common/src/sip/sipaccount.cpp

diff --git a/sflphone-common/src/config/yamlemitter.cpp b/sflphone-common/src/config/yamlemitter.cpp
index 7cd1eacba1..4aae4eda36 100755
--- a/sflphone-common/src/config/yamlemitter.cpp
+++ b/sflphone-common/src/config/yamlemitter.cpp
@@ -62,7 +62,8 @@ void YamlEmitter::open()
   yaml_document_initialize(&document, NULL, NULL, NULL, 0, 0);
 
   // Init the main configuration mapping
-  topLevelMapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE);
+  if((topLevelMapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
+    throw YamlEmitterException("Could not create top level mapping");
 }
 
 void YamlEmitter::close() 
@@ -94,21 +95,37 @@ void YamlEmitter::serializeData()
 }
 
 
-void YamlEmitter::writeAccount(MappingNode *map)
+void YamlEmitter::serializeAccount(MappingNode *map)
 {
 
   std::string accountstr("accounts");
  
+  int accountid, accountmapping;
+
+  _debug("YamlEmitter: Serialize account");
+
+  if(map->getType() != MAPPING)
+    throw YamlEmitterException("Node type is not a mapping while writing account");
+
   if(isFirstAccount) {
     // accountSequence need to be static outside this scope since reused each time an account is written
-    accountSequence = yaml_document_add_sequence (&document, NULL, YAML_BLOCK_SEQUENCE_STYLE);
-    int accountid = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)accountstr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE);
-    yaml_document_append_mapping_pair (&document, topLevelMapping, accountid, accountSequence);
+    if((accountid = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)accountstr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
+      throw YamlEmitterException("Could not add preference scalar to document");
+
+    if((accountSequence = yaml_document_add_sequence (&document, NULL, YAML_BLOCK_SEQUENCE_STYLE)) == 0)
+      throw YamlEmitterException("Could not add sequence to document");
+
+    if(!yaml_document_append_mapping_pair (&document, topLevelMapping, accountid, accountSequence))
+      throw YamlEmitterException("Could not add mapping pair to top level mapping");
+       
     isFirstAccount = false;
   }
 
-  int accountmapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE);
-  yaml_document_append_sequence_item (&document, accountSequence, accountmapping);
+  if((accountmapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
+    throw YamlEmitterException("Could not add account mapping to document");
+
+  if(!yaml_document_append_sequence_item (&document, accountSequence, accountmapping))
+     throw YamlEmitterException("Could not append account mapping to sequence");
 
   Mapping *internalmap = map->getMapping();
   Mapping::iterator iter = internalmap->begin();
@@ -120,17 +137,25 @@ void YamlEmitter::writeAccount(MappingNode *map)
 
 }
 
-void YamlEmitter::writePreference(MappingNode *map)
+void YamlEmitter::serializePreference(MappingNode *map)
 {
   std::string preferencestr("preferences");
 
-  if(map->getType() == MAPPING)
+  int preferenceid, preferencemapping;
+
+  _debug("YamlEmitter: Serialize preference");
+
+  if(map->getType() != MAPPING)
     throw YamlEmitterException("Node type is not a mapping while writing preferences");
 
-  int preferenceid = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)preferencestr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE);
-  int preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE);
+  if((preferenceid = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)preferencestr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
+    throw YamlEmitterException("Could not add scalar to document");
 
-  yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping);
+  if((preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
+    throw YamlEmitterException("Could not add mapping to document");
+
+  if(!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
+    throw YamlEmitterException("Could not add mapping pair to top leve mapping");
 
   Mapping *internalmap = map->getMapping();
   Mapping::iterator iter = internalmap->begin();
@@ -148,18 +173,32 @@ void YamlEmitter::addMappingItem(int mappingid, Key key, YamlNode *node)
 
   if(node->getType() == SCALAR) {
 
+    int temp1, temp2;
+
     ScalarNode *sclr = (ScalarNode *)node;
 
-    int temp1 = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)key.c_str(), -1, YAML_PLAIN_SCALAR_STYLE);
-    int temp2 = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)sclr->getValue().c_str(), -1, YAML_PLAIN_SCALAR_STYLE);
-    yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2);
+    if((temp1 = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)key.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
+      throw YamlEmitterException("Could not add scalar to document");
+
+    if((temp2 = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)sclr->getValue().c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
+      throw YamlEmitterException("Could not add scalar to document");
+
+    if(!yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2))
+      throw YamlEmitterException("Could not append mapping pair to mapping");
 
   }
   else if(node->getType() == MAPPING){
 
-    int temp1 = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)key.c_str(), -1, YAML_PLAIN_SCALAR_STYLE);
-    int temp2 = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE);
-    yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2);
+    int temp1, temp2;
+
+    if((temp1 = yaml_document_add_scalar(&document, NULL, (yaml_char_t *)key.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
+      throw YamlEmitterException("Could not add scalar to document");
+
+    if((temp2 = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
+      throw YamlEmitterException("Could not add scalar to document");
+
+    if(!yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2))
+      throw YamlEmitterException("Could not add mapping pair to mapping");
 
     MappingNode *map = (MappingNode *)node;
     Mapping *internalmap = map->getMapping();
diff --git a/sflphone-common/src/config/yamlemitter.h b/sflphone-common/src/config/yamlemitter.h
index 47bce14a5f..62cb3745d2 100755
--- a/sflphone-common/src/config/yamlemitter.h
+++ b/sflphone-common/src/config/yamlemitter.h
@@ -75,9 +75,9 @@ class YamlEmitter {
 
   void write();
 
-  void writeAccount(MappingNode *map);
+  void serializeAccount(MappingNode *map);
 
-  void writePreference(MappingNode *map);
+  void serializePreference(MappingNode *map);
 
   void writeAddressbook();
 
diff --git a/sflphone-common/src/config/yamlparser.cpp b/sflphone-common/src/config/yamlparser.cpp
old mode 100755
new mode 100644
diff --git a/sflphone-common/src/config/yamlparser.h b/sflphone-common/src/config/yamlparser.h
old mode 100755
new mode 100644
diff --git a/sflphone-common/src/managerimpl.cpp b/sflphone-common/src/managerimpl.cpp
old mode 100755
new mode 100644
index bc208691da..959716c5a1
--- a/sflphone-common/src/managerimpl.cpp
+++ b/sflphone-common/src/managerimpl.cpp
@@ -1413,15 +1413,12 @@ bool ManagerImpl::saveConfig (void) {
 	setConfig(AUDIO, VOLUME_SPKR, getSpkrVolume());
 	setConfig(AUDIO, VOLUME_MICRO, getMicVolume());
 
+
 	AccountMap::iterator iter = _accountMap.begin();
 
 	try{
 	  emitter = new Conf::YamlEmitter("/tmp/sequenceEmiter.txt");
 
-	  // emitter->writeAccount(&accountmap);
-	  // emitter->writeAccount(&accountmap);
-	  emitter->serializeData();
-
 	  while(iter != _accountMap.end()) {
 	    iter->second->serialize(emitter);
 	    iter++;
@@ -1429,6 +1426,8 @@ bool ManagerImpl::saveConfig (void) {
 
 	  preferences.serialize(emitter);
 
+	  emitter->serializeData();
+
 	  delete emitter;
 	}
 	catch (Conf::YamlEmitterException &e) {
diff --git a/sflphone-common/src/preferences.cpp b/sflphone-common/src/preferences.cpp
index f3de3357fd..d0190ae96c 100644
--- a/sflphone-common/src/preferences.cpp
+++ b/sflphone-common/src/preferences.cpp
@@ -86,7 +86,7 @@ void Preferences::serialize(Conf::YamlEmitter *emiter)
   preferencemap.setKeyValue(zeroConfenableKey, &zeroConfenable);
   preferencemap.setKeyValue(md5HashKey, &md5Hash);
 
-  
+  emiter->serializePreference(&preferencemap);
 }
 
 void Preferences::unserialize(Conf::MappingNode *map)
diff --git a/sflphone-common/src/sip/sipaccount.cpp b/sflphone-common/src/sip/sipaccount.cpp
old mode 100755
new mode 100644
index 49667c5105..ff3a5e6ea6
--- a/sflphone-common/src/sip/sipaccount.cpp
+++ b/sflphone-common/src/sip/sipaccount.cpp
@@ -240,6 +240,12 @@ void SIPAccount::serialize(Conf::YamlEmitter *emitter) {
   tlsmap.setKeyValue(verifyClientKey, &verifyclient);
   tlsmap.setKeyValue(verifyServerKey, &verifyserver);
 
+  try{
+    emitter->serializeAccount(&accountmap);
+  }
+  catch (Conf::YamlEmitterException &e) {
+    _error("ConfigTree: %s", e.what());
+  }
 }
 
 
-- 
GitLab