Skip to content
Snippets Groups Projects
Commit a40d6c00 authored by Emmanuel Milou's avatar Emmanuel Milou
Browse files

[#1214] Save sucessfully a map of history items

parent 9895cefe
No related branches found
No related tags found
No related merge requests found
...@@ -19,9 +19,10 @@ ...@@ -19,9 +19,10 @@
*/ */
#include <historyitem.h> #include <historyitem.h>
#include <sstream>
HistoryItem::HistoryItem (int timestamp, CallType call_type, std::string to, std::string from, std::string 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), _account_id (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 () ...@@ -29,3 +30,45 @@ HistoryItem::~HistoryItem ()
{ {
// TODO // 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;
}
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#define _HISTORY_ITEM #define _HISTORY_ITEM
#include <string> #include <string>
#include <config/config.h>
typedef enum CallType { typedef enum CallType {
CALL_MISSED, CALL_MISSED,
...@@ -36,7 +37,7 @@ class HistoryItem { ...@@ -36,7 +37,7 @@ class HistoryItem {
/* /*
* Constructor * Constructor
*/ */
HistoryItem (int, CallType, std::string, std::string, std::string=""); HistoryItem (int, CallType, std::string, std::string, std::string, std::string="");
/* /*
* Destructor * Destructor
...@@ -47,6 +48,8 @@ class HistoryItem { ...@@ -47,6 +48,8 @@ class HistoryItem {
return _timestamp; return _timestamp;
} }
bool save (Conf::ConfigTree **history);
private: private:
/* /*
...@@ -65,6 +68,7 @@ class HistoryItem { ...@@ -65,6 +68,7 @@ class HistoryItem {
*/ */
std::string _to; std::string _to;
std::string _from; std::string _from;
std::string _caller_id;
/* /*
* The account the call was made with * The account the call was made with
......
...@@ -27,47 +27,53 @@ HistoryManager::HistoryManager () : _history_loaded (false), _history_path (""){ ...@@ -27,47 +27,53 @@ HistoryManager::HistoryManager () : _history_loaded (false), _history_path (""){
} }
HistoryManager::~HistoryManager () { HistoryManager::~HistoryManager () {
// TODO
// Clear the history map
_history_items.clear ();
} }
bool HistoryManager::init (void) bool HistoryManager::init (void)
{ {
Conf::ConfigTree history_list;
create_history_path (); 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; bool exist;
exist = _history_list.populateFromFile (_history_path); exist = history_list->populateFromFile (_history_path);
_history_loaded = (exist == 2 ) ? false : true; _history_loaded = (exist == 2 ) ? false : true;
return exist; return exist;
} }
int HistoryManager::load_history_items_map (void) int HistoryManager::load_history_items_map (Conf::ConfigTree *history_list)
{ {
short nb_items = 0; short nb_items = 0;
Conf::TokenList sections; Conf::TokenList sections;
HistoryItem *item; HistoryItem *item;
Conf::TokenList::iterator iter; Conf::TokenList::iterator iter;
std::string to, from, accountID; std::string to, from, caller_id, accountID;
int timestamp; int timestamp;
CallType type; CallType type;
sections = _history_list.getSections(); sections = history_list->getSections();
iter = sections.begin(); iter = sections.begin();
while(iter != sections.end()) { while(iter != sections.end()) {
type = (CallType) getConfigInt (*iter, "type"); type = (CallType) getConfigInt (*iter, "type", history_list);
timestamp = getConfigInt (*iter, "timestamp"); timestamp = getConfigInt (*iter, "timestamp", history_list);
to = getConfigString (*iter, "to"); to = getConfigString (*iter, "to", history_list);
from = getConfigString (*iter, "from"); 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); add_new_history_entry (item);
nb_items ++; nb_items ++;
...@@ -78,10 +84,33 @@ int HistoryManager::load_history_items_map (void) ...@@ -78,10 +84,33 @@ int HistoryManager::load_history_items_map (void)
return nb_items; return nb_items;
} }
int HistoryManager::save_history_to_file (void) bool HistoryManager::save_history_to_file (Conf::ConfigTree *history_list)
{ {
// TODO return history_list->saveConfigTree(_history_path.data());
return 0; }
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) void HistoryManager::add_new_history_entry (HistoryItem *new_item)
...@@ -111,10 +140,10 @@ int HistoryManager::create_history_path (void) { ...@@ -111,10 +140,10 @@ int HistoryManager::create_history_path (void) {
// throw an Conf::ConfigTreeItemException if not found // throw an Conf::ConfigTreeItemException if not found
int int
HistoryManager::getConfigInt(const std::string& section, const std::string& name) HistoryManager::getConfigInt(const std::string& section, const std::string& name, Conf::ConfigTree *history_list)
{ {
try { try {
return _history_list.getConfigTreeItemIntValue(section, name); return history_list->getConfigTreeItemIntValue(section, name);
} catch (Conf::ConfigTreeItemException& e) { } catch (Conf::ConfigTreeItemException& e) {
throw e; throw e;
} }
...@@ -122,11 +151,10 @@ HistoryManager::getConfigInt(const std::string& section, const std::string& name ...@@ -122,11 +151,10 @@ HistoryManager::getConfigInt(const std::string& section, const std::string& name
} }
std::string std::string
HistoryManager::getConfigString(const std::string& section, const std::string& HistoryManager::getConfigString(const std::string& section, const std::string& name, Conf::ConfigTree *history_list)
name)
{ {
try { try {
return _history_list.getConfigTreeItemValue(section, name); return history_list->getConfigTreeItemValue(section, name);
} catch (Conf::ConfigTreeItemException& e) { } catch (Conf::ConfigTreeItemException& e) {
throw e; throw e;
} }
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#define _HISTORY_MANAGER #define _HISTORY_MANAGER
#include "historyitem.h" #include "historyitem.h"
#include <config/config.h>
#include <global.h> #include <global.h>
#include <user_cfg.h> #include <user_cfg.h>
...@@ -46,18 +45,26 @@ class HistoryManager { ...@@ -46,18 +45,26 @@ class HistoryManager {
/* /*
* Load the history from a file to the dedicated data structure * 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 * @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 * 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) { inline bool is_loaded (void) {
return _history_loaded; return _history_loaded;
} }
...@@ -66,6 +73,9 @@ class HistoryManager { ...@@ -66,6 +73,9 @@ class HistoryManager {
_history_path = filename; _history_path = filename;
} }
/*
*@return int The number of items found in the history file
*/
inline int get_history_size (void) { inline int get_history_size (void) {
return _history_items.size (); return _history_items.size ();
} }
...@@ -73,8 +83,8 @@ class HistoryManager { ...@@ -73,8 +83,8 @@ class HistoryManager {
private: private:
int getConfigInt(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); std::string getConfigString(const std::string& section, const std::string& name, Conf::ConfigTree *history_list);
/* /*
* Set the path to the history file * Set the path to the history file
...@@ -101,11 +111,6 @@ class HistoryManager { ...@@ -101,11 +111,6 @@ class HistoryManager {
*/ */
bool _history_loaded; bool _history_loaded;
/*
* The history tree. It contains the call history
*/
Conf::ConfigTree _history_list;
friend class HistoryTest; friend class HistoryTest;
}; };
......
[CALL1] [144562436]
type=1 from=514-276-5468
timestamp=12847367465832 id=Savoir-faire Linux
timestamp=144562436
to= to=
from=5143848557 type=0
id=Chez wam
[CALL2] [747638685]
type=2
timestamp=4542456564676
to=136
from= from=
id=Emmanuel Milou id=Emmanuel Milou
timestamp=747638685
to=136
type=2
[CALL3] [775354456]
type=0 from=5143848557
timestamp=565768563455 id=Chez wam
timestamp=775354456
to= to=
from=514-276-5468 type=1
id=Savoir-faire Linux
[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
...@@ -50,9 +50,10 @@ void HistoryTest::test_create_history_path () { ...@@ -50,9 +50,10 @@ void HistoryTest::test_create_history_path () {
void HistoryTest::test_load_history_from_file () void HistoryTest::test_load_history_from_file ()
{ {
bool res; bool res;
Conf::ConfigTree history_list;
history->create_history_path (); 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 (history->is_loaded ());
CPPUNIT_ASSERT (res == true); CPPUNIT_ASSERT (res == true);
...@@ -62,15 +63,40 @@ void HistoryTest::test_load_history_items_map () ...@@ -62,15 +63,40 @@ void HistoryTest::test_load_history_items_map ()
{ {
std::string path; std::string path;
int nb_items; int nb_items;
Conf::ConfigTree history_list;
history->set_history_path (HISTORY_SAMPLE); history->set_history_path (HISTORY_SAMPLE);
history->load_history_from_file (); history->load_history_from_file (&history_list);
nb_items = history->load_history_items_map (); nb_items = history->load_history_items_map (&history_list);
std::cout << nb_items << std::endl;
CPPUNIT_ASSERT (nb_items == HISTORY_SAMPLE_SIZE); CPPUNIT_ASSERT (nb_items == HISTORY_SAMPLE_SIZE);
CPPUNIT_ASSERT (history->get_history_size () == 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(){ void HistoryTest::tearDown(){
// Delete the history object // Delete the history object
delete history; history=0; delete history; history=0;
......
...@@ -45,6 +45,8 @@ class HistoryTest : public CppUnit::TestCase { ...@@ -45,6 +45,8 @@ class HistoryTest : public CppUnit::TestCase {
CPPUNIT_TEST (test_create_history_path); CPPUNIT_TEST (test_create_history_path);
CPPUNIT_TEST (test_load_history_from_file); CPPUNIT_TEST (test_load_history_from_file);
CPPUNIT_TEST (test_load_history_items_map); 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 (); CPPUNIT_TEST_SUITE_END ();
public: public:
...@@ -62,6 +64,10 @@ class HistoryTest : public CppUnit::TestCase { ...@@ -62,6 +64,10 @@ class HistoryTest : public CppUnit::TestCase {
void test_load_history_items_map (); 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. * Code factoring - Common resources can be released here.
* This method is called by unitcpp after each test * This method is called by unitcpp after each test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment