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

[#1214] Load history from file into memory, add unit tests

parent 73f4cc71
No related branches found
No related tags found
No related merge requests found
......@@ -20,9 +20,9 @@
#include <historyitem.h>
HistoryItem::HistoryItem (int timestamp, CallType call_type, std::string peer, std::string account_id)
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)
{
// TODO
}
HistoryItem::~HistoryItem ()
......
......@@ -36,13 +36,16 @@ class HistoryItem {
/*
* Constructor
*/
HistoryItem (int, CallType, std::string, std::string="");
HistoryItem (int, CallType, std::string, std::string, std::string="");
/*
* Destructor
*/
~HistoryItem ();
inline int get_timestamp () {
return _timestamp;
}
private:
......@@ -58,9 +61,10 @@ class HistoryItem {
CallType _call_type;
/*
* The information about the callee/caller, depending on the type of call
* The information about the callee/caller, depending on the type of call. One field may be empty.
*/
std::string _peer;
std::string _to;
std::string _from;
/*
* The account the call was made with
......
......@@ -22,26 +22,60 @@
#include <errno.h>
#include <cc++/file.h>
HistoryManager::HistoryManager () {
HistoryManager::HistoryManager () : _history_loaded (false), _history_path (""){
bool exist;
}
// Load the path to the file
if (create_history_path () ==1){
exist = _history_config.populateFromFile (_history_path);
HistoryManager::~HistoryManager () {
// TODO
}
_history_loaded = (exist == 2 ) ? false : true;
bool HistoryManager::init (void)
{
create_history_path ();
load_history_from_file ();
}
HistoryManager::~HistoryManager () {
// TODO
bool HistoryManager::load_history_from_file (void)
{
bool exist;
exist = _history_list.populateFromFile (_history_path);
_history_loaded = (exist == 2 ) ? false : true;
return exist;
}
int HistoryManager::load_history_from_file (void)
int HistoryManager::load_history_items_map (void)
{
//
return 0;
short nb_items = 0;
Conf::TokenList sections;
HistoryItem *item;
Conf::TokenList::iterator iter;
std::string to, from, accountID;
int timestamp;
CallType type;
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");
item = new HistoryItem (timestamp, type, to, from);
add_new_history_entry (item);
nb_items ++;
iter ++;
}
return nb_items;
}
int HistoryManager::save_history_to_file (void)
......@@ -50,9 +84,10 @@ int HistoryManager::save_history_to_file (void)
return 0;
}
void HistoryManager::add_new_history_entry (HistoryItem new_item)
void HistoryManager::add_new_history_entry (HistoryItem *new_item)
{
// TODO
// Add it in the map
_history_items [new_item->get_timestamp ()] = new_item;
}
int HistoryManager::create_history_path (void) {
......@@ -73,3 +108,28 @@ int HistoryManager::create_history_path (void) {
_history_path = path + DIR_SEPARATOR_STR + "history";
return 0;
}
// throw an Conf::ConfigTreeItemException if not found
int
HistoryManager::getConfigInt(const std::string& section, const std::string& name)
{
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)
{
try {
return _history_list.getConfigTreeItemValue(section, name);
} catch (Conf::ConfigTreeItemException& e) {
throw e;
}
return "";
}
......@@ -26,7 +26,7 @@
#include <global.h>
#include <user_cfg.h>
typedef std::map <std::string, HistoryItem*> HistoryItemMap;
typedef std::map <int, HistoryItem*> HistoryItemMap;
class HistoryManager {
......@@ -41,17 +41,41 @@ class HistoryManager {
*/
~HistoryManager ();
bool init (void);
/*
* Load the history from a file to the dedicated data structure
*/
int load_history_from_file (void);
bool load_history_from_file (void);
/*
* @return int The number of history items loaded
*/
int load_history_items_map (void);
/*
* Inverse method, ie save a data structure containing the history into a file
*/
int save_history_to_file (void);
inline bool is_loaded (void) {
return _history_loaded;
}
inline void set_history_path (std::string filename) {
_history_path = filename;
}
inline int get_history_size (void) {
return _history_items.size ();
}
private:
int getConfigInt(const std::string& section, const std::string& name);
std::string getConfigString(const std::string& section, const std::string& name);
/*
* Set the path to the history file
*/
......@@ -59,7 +83,7 @@ class HistoryManager {
/*
* Add a new history item in the data structure
*/
void add_new_history_entry (HistoryItem new_item);
void add_new_history_entry (HistoryItem *new_item);
/*
* Map containing the history items
......@@ -80,7 +104,9 @@ class HistoryManager {
/*
* The history tree. It contains the call history
*/
Conf::ConfigTree _history_config;
Conf::ConfigTree _history_list;
friend class HistoryTest;
};
#endif //_HISTORY_MANAGER
[CALL1]
type=1
timestamp=12847367465832
to=
from=5143848557
id=Chez wam
[CALL2]
type=2
timestamp=4542456564676
to=136
from=
id=Emmanuel Milou
[CALL3]
type=0
timestamp=565768563455
to=
from=514-276-5468
id=Savoir-faire Linux
/*
* Copyright (C) 2009 Savoir-Faire Linux inc.
* Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <sstream>
#include "historyTest.h"
using std::cout;
using std::endl;
#define HOMEDIR (getenv ("HOME"))
#define HISTORY_SAMPLE "history-sample"
#define HISTORY_SAMPLE_SIZE 3
void HistoryTest::setUp(){
// Instanciate the cleaner singleton
history = new HistoryManager ();
}
void HistoryTest::test_create_history_path () {
int result;
std::string path;
path = HOMEDIR;
path += "/.sflphone/history";
result = history->create_history_path ();
CPPUNIT_ASSERT (result == 0);
CPPUNIT_ASSERT (!history->is_loaded ());
CPPUNIT_ASSERT (history->_history_path == path);
}
void HistoryTest::test_load_history_from_file ()
{
bool res;
history->create_history_path ();
res = history->load_history_from_file ();
CPPUNIT_ASSERT (history->is_loaded ());
CPPUNIT_ASSERT (res == true);
}
void HistoryTest::test_load_history_items_map ()
{
std::string path;
int nb_items;
history->set_history_path (HISTORY_SAMPLE);
history->load_history_from_file ();
nb_items = history->load_history_items_map ();
std::cout << nb_items << std::endl;
CPPUNIT_ASSERT (nb_items == HISTORY_SAMPLE_SIZE);
CPPUNIT_ASSERT (history->get_history_size () == HISTORY_SAMPLE_SIZE);
}
void HistoryTest::tearDown(){
// Delete the history object
delete history; history=0;
}
/*
* Copyright (C) 2009 Savoir-Faire Linux inc.
* Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
// Cppunit import
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestCase.h>
#include <cppunit/TestSuite.h>
#include <assert.h>
// Application import
#include "history/historymanager.h"
/*
* @file historyTest.h
* @brief Regroups unitary tests related to the phone number cleanup function.
*/
#ifndef _HISTORY_TEST_
#define _HISTORY_TEST_
class HistoryTest : public CppUnit::TestCase {
/**
* Use cppunit library macros to add unit test the factory
*/
CPPUNIT_TEST_SUITE (HistoryTest);
CPPUNIT_TEST (test_create_history_path);
CPPUNIT_TEST (test_load_history_from_file);
CPPUNIT_TEST (test_load_history_items_map);
CPPUNIT_TEST_SUITE_END ();
public:
HistoryTest() : CppUnit::TestCase("History Tests") {}
/*
* Code factoring - Common resources can be initialized here.
* This method is called by unitcpp before each test
*/
void setUp();
void test_create_history_path ();
void test_load_history_from_file ();
void test_load_history_items_map ();
/*
* Code factoring - Common resources can be released here.
* This method is called by unitcpp after each test
*/
inline void tearDown ();
private:
HistoryManager *history;
};
/* Register our test module */
CPPUNIT_TEST_SUITE_REGISTRATION( HistoryTest );
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment