diff --git a/sflphone-common/src/history/historyitem.cpp b/sflphone-common/src/history/historyitem.cpp index 6aa02aa0ed8b86734a2d598233cf9e1759f54f95..c21498321e22ff6cb504cb4d641b15c5f0b3a4bf 100644 --- a/sflphone-common/src/history/historyitem.cpp +++ b/sflphone-common/src/history/historyitem.cpp @@ -19,9 +19,10 @@ */ #include <historyitem.h> +#include <sstream> -HistoryItem::HistoryItem (int timestamp, CallType call_type, std::string to, std::string from, std::string account_id) - : _timestamp (timestamp), _call_type (call_type), _to (to), _from (from), _account_id (account_id) +HistoryItem::HistoryItem (int timestamp, CallType call_type, std::string to, std::string from, std::string caller_id, std::string account_id) + : _timestamp (timestamp), _call_type (call_type), _to (to), _from (from), _caller_id (caller_id), _account_id (account_id) { } @@ -29,3 +30,45 @@ HistoryItem::~HistoryItem () { // TODO } + +bool HistoryItem::save (Conf::ConfigTree **history){ + + std::stringstream section, call_type, timestamp; + bool res; + + // The section is : "[" + timestamp = "]" + section << _timestamp ; + call_type << _call_type; + timestamp << _timestamp; + + res = ( (*history)->setConfigTreeItem(section.str(), "type", call_type.str()) + && (*history)->setConfigTreeItem(section.str(), "timestamp", timestamp.str()) + && (*history)->setConfigTreeItem(section.str(), "to", _to) + && (*history)->setConfigTreeItem(section.str(), "from", _from) + && (*history)->setConfigTreeItem(section.str(), "id", _caller_id) ); + + return res; +} + + + + + + + + + + + + + + + + + + + + + + + diff --git a/sflphone-common/src/history/historyitem.h b/sflphone-common/src/history/historyitem.h index 39803612139c9fc44e2587f1959b2bc78b875ae1..ff3286731ffe3dbbdc3891e27d1ff3bf8ffff987 100644 --- a/sflphone-common/src/history/historyitem.h +++ b/sflphone-common/src/history/historyitem.h @@ -22,6 +22,7 @@ #define _HISTORY_ITEM #include <string> +#include <config/config.h> typedef enum CallType { CALL_MISSED, @@ -36,7 +37,7 @@ class HistoryItem { /* * Constructor */ - HistoryItem (int, CallType, std::string, std::string, std::string=""); + HistoryItem (int, CallType, std::string, std::string, std::string, std::string=""); /* * Destructor @@ -47,6 +48,8 @@ class HistoryItem { return _timestamp; } + bool save (Conf::ConfigTree **history); + private: /* @@ -65,6 +68,7 @@ class HistoryItem { */ std::string _to; std::string _from; + std::string _caller_id; /* * The account the call was made with diff --git a/sflphone-common/src/history/historymanager.cpp b/sflphone-common/src/history/historymanager.cpp index 3036d700afc4407691b37110a568c0bfdc58e7ee..e237aab67a1c2d2c846f1d6b44edd0887d8f7eb3 100644 --- a/sflphone-common/src/history/historymanager.cpp +++ b/sflphone-common/src/history/historymanager.cpp @@ -27,47 +27,53 @@ HistoryManager::HistoryManager () : _history_loaded (false), _history_path (""){ } HistoryManager::~HistoryManager () { - // TODO + + // Clear the history map + _history_items.clear (); } bool HistoryManager::init (void) { + Conf::ConfigTree history_list; + create_history_path (); - load_history_from_file (); + load_history_from_file (&history_list); + load_history_items_map (&history_list); } -bool HistoryManager::load_history_from_file (void) +bool HistoryManager::load_history_from_file (Conf::ConfigTree *history_list) { bool exist; - - exist = _history_list.populateFromFile (_history_path); + + exist = history_list->populateFromFile (_history_path); _history_loaded = (exist == 2 ) ? false : true; return exist; } -int HistoryManager::load_history_items_map (void) +int HistoryManager::load_history_items_map (Conf::ConfigTree *history_list) { short nb_items = 0; Conf::TokenList sections; HistoryItem *item; Conf::TokenList::iterator iter; - std::string to, from, accountID; + std::string to, from, caller_id, accountID; int timestamp; CallType type; - - sections = _history_list.getSections(); + + sections = history_list->getSections(); iter = sections.begin(); while(iter != sections.end()) { - type = (CallType) getConfigInt (*iter, "type"); - timestamp = getConfigInt (*iter, "timestamp"); - to = getConfigString (*iter, "to"); - from = getConfigString (*iter, "from"); + type = (CallType) getConfigInt (*iter, "type", history_list); + timestamp = getConfigInt (*iter, "timestamp", history_list); + to = getConfigString (*iter, "to", history_list); + from = getConfigString (*iter, "from", history_list); + caller_id = getConfigString (*iter, "id", history_list); - item = new HistoryItem (timestamp, type, to, from); + item = new HistoryItem (timestamp, type, to, from, caller_id); add_new_history_entry (item); nb_items ++; @@ -78,10 +84,33 @@ int HistoryManager::load_history_items_map (void) return nb_items; } -int HistoryManager::save_history_to_file (void) +bool HistoryManager::save_history_to_file (Conf::ConfigTree *history_list) { - // TODO - return 0; + return history_list->saveConfigTree(_history_path.data()); +} + + +int HistoryManager::save_history_items_map (Conf::ConfigTree *history_list) +{ + HistoryItemMap::iterator iter; + HistoryItem *item; + int items_saved = 0; + + iter = _history_items.begin (); + + while (iter != _history_items.end ()) + { + item = iter->second; + if (item) { + if (item->save (&history_list)) + items_saved ++; + } else { + std::cout << "[DEBUG]: can't save NULL history item." << std::endl; + } + iter ++; + } + + return items_saved; } void HistoryManager::add_new_history_entry (HistoryItem *new_item) @@ -110,26 +139,25 @@ int HistoryManager::create_history_path (void) { } // throw an Conf::ConfigTreeItemException if not found - int -HistoryManager::getConfigInt(const std::string& section, const std::string& name) + int +HistoryManager::getConfigInt(const std::string& section, const std::string& name, Conf::ConfigTree *history_list) { - try { - return _history_list.getConfigTreeItemIntValue(section, name); - } catch (Conf::ConfigTreeItemException& e) { - throw e; - } - return 0; + try { + return history_list->getConfigTreeItemIntValue(section, name); + } catch (Conf::ConfigTreeItemException& e) { + throw e; + } + return 0; } -std::string -HistoryManager::getConfigString(const std::string& section, const std::string& - name) + std::string +HistoryManager::getConfigString(const std::string& section, const std::string& name, Conf::ConfigTree *history_list) { - try { - return _history_list.getConfigTreeItemValue(section, name); - } catch (Conf::ConfigTreeItemException& e) { - throw e; - } - return ""; + try { + return history_list->getConfigTreeItemValue(section, name); + } catch (Conf::ConfigTreeItemException& e) { + throw e; + } + return ""; } diff --git a/sflphone-common/src/history/historymanager.h b/sflphone-common/src/history/historymanager.h index 2a89471d2401f6fc2b97d4051f8dcb4532b7b9d6..6089f671a5e0011a0df841b30c66f3b1a4f59fcb 100644 --- a/sflphone-common/src/history/historymanager.h +++ b/sflphone-common/src/history/historymanager.h @@ -22,7 +22,6 @@ #define _HISTORY_MANAGER #include "historyitem.h" -#include <config/config.h> #include <global.h> #include <user_cfg.h> @@ -46,18 +45,26 @@ class HistoryManager { /* * Load the history from a file to the dedicated data structure */ - bool load_history_from_file (void); + bool load_history_from_file (Conf::ConfigTree *history_list); /* * @return int The number of history items loaded */ - int load_history_items_map (void); + int load_history_items_map (Conf::ConfigTree *history_list); /* * Inverse method, ie save a data structure containing the history into a file */ - int save_history_to_file (void); + bool save_history_to_file (Conf::ConfigTree *history_list); + /** + * @return int The number of history entries successfully saved + */ + int save_history_items_map (Conf::ConfigTree *history_list); + + /** + *@return bool True if the history file has been successfully read + */ inline bool is_loaded (void) { return _history_loaded; } @@ -66,6 +73,9 @@ class HistoryManager { _history_path = filename; } + /* + *@return int The number of items found in the history file + */ inline int get_history_size (void) { return _history_items.size (); } @@ -73,8 +83,8 @@ class HistoryManager { private: - int getConfigInt(const std::string& section, const std::string& name); - std::string getConfigString(const std::string& section, const std::string& name); + int getConfigInt(const std::string& section, const std::string& name, Conf::ConfigTree *history_list); + std::string getConfigString(const std::string& section, const std::string& name, Conf::ConfigTree *history_list); /* * Set the path to the history file @@ -101,11 +111,6 @@ class HistoryManager { */ bool _history_loaded; - /* - * The history tree. It contains the call history - */ - Conf::ConfigTree _history_list; - friend class HistoryTest; }; diff --git a/sflphone-common/test/history-sample b/sflphone-common/test/history-sample index 03240c76fc732194984f3cf4eaa53e46666c0be5..d181709de558215b886b3d00ea2ca96a5fe99052 100644 --- a/sflphone-common/test/history-sample +++ b/sflphone-common/test/history-sample @@ -1,20 +1,21 @@ -[CALL1] -type=1 -timestamp=12847367465832 +[144562436] +from=514-276-5468 +id=Savoir-faire Linux +timestamp=144562436 to= -from=5143848557 -id=Chez wam +type=0 -[CALL2] -type=2 -timestamp=4542456564676 -to=136 +[747638685] from= id=Emmanuel Milou +timestamp=747638685 +to=136 +type=2 -[CALL3] -type=0 -timestamp=565768563455 +[775354456] +from=5143848557 +id=Chez wam +timestamp=775354456 to= -from=514-276-5468 -id=Savoir-faire Linux +type=1 + diff --git a/sflphone-common/test/history-sample.tpl b/sflphone-common/test/history-sample.tpl new file mode 100644 index 0000000000000000000000000000000000000000..d181709de558215b886b3d00ea2ca96a5fe99052 --- /dev/null +++ b/sflphone-common/test/history-sample.tpl @@ -0,0 +1,21 @@ +[144562436] +from=514-276-5468 +id=Savoir-faire Linux +timestamp=144562436 +to= +type=0 + +[747638685] +from= +id=Emmanuel Milou +timestamp=747638685 +to=136 +type=2 + +[775354456] +from=5143848557 +id=Chez wam +timestamp=775354456 +to= +type=1 + diff --git a/sflphone-common/test/historyTest.cpp b/sflphone-common/test/historyTest.cpp index 09d413544c43543754bc13ae4fdb803229d5be26..b6639008fd2a2f3d58c9f08243b74fe48a8d3a21 100644 --- a/sflphone-common/test/historyTest.cpp +++ b/sflphone-common/test/historyTest.cpp @@ -38,7 +38,7 @@ void HistoryTest::test_create_history_path () { int result; std::string path; - + path = HOMEDIR; path += "/.sflphone/history"; result = history->create_history_path (); @@ -50,9 +50,10 @@ void HistoryTest::test_create_history_path () { void HistoryTest::test_load_history_from_file () { bool res; + Conf::ConfigTree history_list; history->create_history_path (); - res = history->load_history_from_file (); + res = history->load_history_from_file (&history_list); CPPUNIT_ASSERT (history->is_loaded ()); CPPUNIT_ASSERT (res == true); @@ -62,15 +63,40 @@ void HistoryTest::test_load_history_items_map () { std::string path; int nb_items; - + Conf::ConfigTree history_list; + history->set_history_path (HISTORY_SAMPLE); - history->load_history_from_file (); - nb_items = history->load_history_items_map (); - std::cout << nb_items << std::endl; + history->load_history_from_file (&history_list); + nb_items = history->load_history_items_map (&history_list); CPPUNIT_ASSERT (nb_items == HISTORY_SAMPLE_SIZE); CPPUNIT_ASSERT (history->get_history_size () == HISTORY_SAMPLE_SIZE); } +void HistoryTest::test_save_history_items_map () +{ + std::string path; + int nb_items_loaded, nb_items_saved; + Conf::ConfigTree history_list, history_list2; + + history->set_history_path (HISTORY_SAMPLE); + history->load_history_from_file (&history_list); + nb_items_loaded = history->load_history_items_map (&history_list); + nb_items_saved = history->save_history_items_map (&history_list2); + CPPUNIT_ASSERT (nb_items_loaded == nb_items_saved); +} + +void HistoryTest::test_save_history_to_file () +{ + std::string path; + Conf::ConfigTree history_list, history_list2; + + history->set_history_path (HISTORY_SAMPLE); + history->load_history_from_file (&history_list); + history->load_history_items_map (&history_list); + history->save_history_items_map (&history_list2); + CPPUNIT_ASSERT (history->save_history_to_file (&history_list2)); +} + void HistoryTest::tearDown(){ // Delete the history object delete history; history=0; diff --git a/sflphone-common/test/historyTest.h b/sflphone-common/test/historyTest.h index 7e6eec4a3ae74a5a9df39918925b84bda78cdbd4..c76623a9d64dd620862280a0fa2d8c99ec6c68c7 100644 --- a/sflphone-common/test/historyTest.h +++ b/sflphone-common/test/historyTest.h @@ -45,6 +45,8 @@ class HistoryTest : public CppUnit::TestCase { CPPUNIT_TEST (test_create_history_path); CPPUNIT_TEST (test_load_history_from_file); CPPUNIT_TEST (test_load_history_items_map); + CPPUNIT_TEST (test_save_history_items_map); + CPPUNIT_TEST (test_save_history_to_file); CPPUNIT_TEST_SUITE_END (); public: @@ -62,6 +64,10 @@ class HistoryTest : public CppUnit::TestCase { void test_load_history_items_map (); + void test_save_history_items_map (); + + void test_save_history_to_file (); + /* * Code factoring - Common resources can be released here. * This method is called by unitcpp after each test