Skip to content
Snippets Groups Projects
Commit f7a6a712 authored by Tristan Matthews's avatar Tristan Matthews
Browse files

* #7264: historymanager cleanup

parent fe877988
Branches
Tags
No related merge requests found
......@@ -34,7 +34,6 @@
#define HISTORY_ITEM_H_
#include <string>
#include <config/config.h>
typedef enum CallType {
CALL_MISSED,
......@@ -42,6 +41,10 @@ typedef enum CallType {
CALL_OUTGOING
} CallType;
namespace Conf {
class ConfigTree;
}
class HistoryItem {
public:
......
......@@ -30,10 +30,19 @@
* as that of the covered work.
*/
#include <historymanager.h>
#include <errno.h>
#include "historymanager.h"
#include <cerrno>
#include <cc++/file.h>
#include <time.h>
#include <ctime>
#include "config/config.h"
namespace {
int get_unix_timestamp_equivalent(int days) {
// Number of seconds in one day: 60 x 60 x 24
static const int DAY_UNIX_TIMESTAMP = 86400;
return days * DAY_UNIX_TIMESTAMP;
}
}
HistoryManager::HistoryManager() :
history_items_(), history_loaded_(false), history_path_("")
......@@ -121,21 +130,19 @@ int HistoryManager::save_history_items_map(Conf::ConfigTree *history_list)
void HistoryManager::add_new_history_entry(const HistoryItem &new_item)
{
// Add it in the map
history_items_.push_back(new_item);
}
int HistoryManager::create_history_path(std::string path)
void HistoryManager::create_history_path(const std::string &path)
{
std::string userdata, xdg_env, xdg_data;
xdg_data = std::string(HOMEDIR) + DIR_SEPARATOR_STR + ".local/share/sflphone";
std::string xdg_data = std::string(HOMEDIR) + DIR_SEPARATOR_STR + ".local/share/sflphone";
if (path.empty()) {
std::string userdata;
// If the environment variable is set (not null and not empty), we'll use it to save the history
// Else we 'll the standard one, ie: XDG_DATA_HOME = $HOMEDIR/.local/share/sflphone
if (XDG_DATA_HOME != NULL) {
xdg_env = std::string(XDG_DATA_HOME);
std::string xdg_env(XDG_DATA_HOME);
(xdg_env.length() > 0) ? userdata = xdg_env : userdata = xdg_data;
} else
userdata = xdg_data;
......@@ -144,41 +151,26 @@ int HistoryManager::create_history_path(std::string path)
// If directory creation failed
if (errno != EEXIST) {
DEBUG("HistoryManager: Cannot create directory: %m");
return -1;
return;
}
}
// Load user's history
history_path_ = userdata + DIR_SEPARATOR_STR + "history";
set_history_path(userdata + DIR_SEPARATOR_STR + "history");
} else
set_history_path(path);
return 0;
}
// throw an Conf::ConfigTreeItemException if not found
int
HistoryManager::getConfigInt(const std::string& section, const std::string& name, Conf::ConfigTree *history_list)
{
try {
return history_list->getConfigTreeItemIntValue(section, name);
} catch (const Conf::ConfigTreeItemException& e) {
throw;
}
return 0;
}
std::string
HistoryManager::getConfigString(const std::string& section, const std::string& name, Conf::ConfigTree *history_list)
{
try {
return history_list->getConfigTreeItemValue(section, name);
} catch (const Conf::ConfigTreeItemException& e) {
throw;
}
return "";
}
std::vector<std::string> HistoryManager::get_history_serialized() const
......@@ -190,20 +182,15 @@ std::vector<std::string> HistoryManager::get_history_serialized() const
return serialized;
}
int HistoryManager::set_serialized_history(const std::vector<std::string> &history, int limit)
{
int history_limit;
time_t current_timestamp;
DEBUG("HistoryManager: Set serialized history");
history_items_.clear();
// We want to save only the items recent enough (ie compared to CONFIG_HISTORY_LIMIT)
// Get the current timestamp
time_t current_timestamp;
time(&current_timestamp);
history_limit = get_unix_timestamp_equivalent(limit);
int history_limit = get_unix_timestamp_equivalent(limit);
int items_added = 0;
for (std::vector<std::string>::const_iterator iter = history.begin(); iter != history.end(); ++iter) {
......
......@@ -36,8 +36,6 @@
#include "historyitem.h"
#include <global.h>
#define DAY_UNIX_TIMESTAMP 86400 // Number of seconds in one day: 60 x 60 x 24
class HistoryManager {
public:
......@@ -101,10 +99,6 @@ class HistoryManager {
int set_serialized_history(const std::vector<std::string> &history, int limit);
private:
int get_unix_timestamp_equivalent(int days) const {
return days * DAY_UNIX_TIMESTAMP;
}
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);
......@@ -113,7 +107,7 @@ class HistoryManager {
*
* @param path A specific file to use; if empty, use the global one
*/
int create_history_path(std::string path="");
void create_history_path(const std::string &path="");
/*
* Add a new history item in the data structure
*/
......
......@@ -66,11 +66,9 @@ void HistoryTest::test_create_history_path()
{
DEBUG("-------------------- HistoryTest::test_create_history_path --------------------\n");
int result;
std::string path(HISTORY_SAMPLE);
result = history->create_history_path(path);
CPPUNIT_ASSERT(result == 0);
history->create_history_path(path);
CPPUNIT_ASSERT(!history->is_loaded());
CPPUNIT_ASSERT(history->history_path_ == path);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment