2
* Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
4
* Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
5
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU General Public License as published by
9
* the Free Software Foundation; either version 3 of the License, or
10
* (at your option) any later version.
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
* GNU General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
* Additional permission under GNU GPL version 3 section 7:
23
* If you modify this program, or any covered work, by linking or
24
* combining it with the OpenSSL project's OpenSSL library (or a
25
* modified version of that library), containing parts covered by the
26
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
27
* grants you additional permission to convey the resulting work.
28
* Corresponding Source for a non-source form of such a combination
29
* shall include the source code for the parts of OpenSSL used as well
30
* as that of the covered work.
33
#include <historymanager.h>
35
#include <cc++/file.h>
38
HistoryManager::HistoryManager() :
39
history_items_(), history_loaded_(false), history_path_("")
42
int HistoryManager::load_history(int limit, const std::string &path)
44
Conf::ConfigTree history_list;
45
create_history_path(path);
46
load_history_from_file(&history_list);
47
return load_history_items_map(&history_list, limit);
50
bool HistoryManager::save_history()
52
Conf::ConfigTree history_list;
53
save_history_items_map(&history_list);
54
return save_history_to_file(&history_list);
57
bool HistoryManager::load_history_from_file(Conf::ConfigTree *history_list)
59
DEBUG("HistoryManager: Load history from file %s", history_path_.c_str());
61
int exist = history_list->populateFromFile(history_path_.c_str());
62
history_loaded_ = (exist == 2) ? false : true;
64
return history_loaded_;
67
int HistoryManager::load_history_items_map(Conf::ConfigTree *history_list, int limit)
71
// We want to save only the items recent enough (ie compared to CONFIG_HISTORY_LIMIT)
72
// Get the current timestamp
73
time_t current_timestamp;
74
time(¤t_timestamp);
75
int history_limit = get_unix_timestamp_equivalent(limit);
77
Conf::TokenList sections(history_list->getSections());
79
for (Conf::TokenList::iterator iter = sections.begin(); iter != sections.end(); ++iter) {
80
CallType type = (CallType) getConfigInt(*iter, "type", history_list);
81
string timestamp_start(getConfigString(*iter, "timestamp_start", history_list));
82
string timestamp_stop(getConfigString(*iter, "timestamp_stop", history_list));
83
string name(getConfigString(*iter, "name", history_list));
84
string number(getConfigString(*iter, "number", history_list));
85
string callID(getConfigString(*iter, "id", history_list));
86
string accountID(getConfigString(*iter, "accountid", history_list));
87
string recording_file(getConfigString(*iter, "recordfile", history_list));
88
string confID(getConfigString(*iter, "confid", history_list));
89
string timeAdded(getConfigString(*iter, "timeadded", history_list));
91
// Make a check on the start timestamp to know it is loadable according to CONFIG_HISTORY_LIMIT
92
if (atoi(timestamp_start.c_str()) >= ((int) current_timestamp - history_limit)) {
93
HistoryItem item(timestamp_start, type, timestamp_stop, name, number, callID, accountID, recording_file, confID, timeAdded);
94
add_new_history_entry(item);
103
bool HistoryManager::save_history_to_file(Conf::ConfigTree *history_list)
105
DEBUG("HistoryManager: Saving history in XDG directory: %s", history_path_.c_str());
106
return history_list->saveConfigTree(history_path_.data());
109
int HistoryManager::save_history_items_map(Conf::ConfigTree *history_list)
112
for (std::vector<HistoryItem>::iterator iter = history_items_.begin(); iter != history_items_.end(); ++iter) {
113
if (iter->save(&history_list))
116
DEBUG("can't save NULL history item\n");
122
void HistoryManager::add_new_history_entry(const HistoryItem &new_item)
125
history_items_.push_back(new_item);
128
int HistoryManager::create_history_path(std::string path)
130
std::string userdata, xdg_env, xdg_data;
132
xdg_data = std::string(HOMEDIR) + DIR_SEPARATOR_STR + ".local/share/sflphone";
135
// If the environment variable is set (not null and not empty), we'll use it to save the history
136
// Else we 'll the standard one, ie: XDG_DATA_HOME = $HOMEDIR/.local/share/sflphone
137
if (XDG_DATA_HOME != NULL) {
138
xdg_env = std::string(XDG_DATA_HOME);
139
(xdg_env.length() > 0) ? userdata = xdg_env : userdata = xdg_data;
143
if (mkdir(userdata.data(), 0755) != 0) {
144
// If directory creation failed
145
if (errno != EEXIST) {
146
DEBUG("HistoryManager: Cannot create directory: %m");
151
// Load user's history
152
history_path_ = userdata + DIR_SEPARATOR_STR + "history";
154
set_history_path(path);
159
// throw an Conf::ConfigTreeItemException if not found
161
HistoryManager::getConfigInt(const std::string& section, const std::string& name, Conf::ConfigTree *history_list)
164
return history_list->getConfigTreeItemIntValue(section, name);
165
} catch (const Conf::ConfigTreeItemException& e) {
173
HistoryManager::getConfigString(const std::string& section, const std::string& name, Conf::ConfigTree *history_list)
176
return history_list->getConfigTreeItemValue(section, name);
177
} catch (const Conf::ConfigTreeItemException& e) {
184
std::vector<std::string> HistoryManager::get_history_serialized() const
186
std::vector<std::string> serialized;
187
for (std::vector<HistoryItem>::const_iterator iter = history_items_.begin(); iter != history_items_.end(); ++iter)
188
serialized.push_back(iter->serialize());
194
int HistoryManager::set_serialized_history(const std::vector<std::string> &history, int limit)
197
time_t current_timestamp;
199
DEBUG("HistoryManager: Set serialized history");
201
history_items_.clear();
203
// We want to save only the items recent enough (ie compared to CONFIG_HISTORY_LIMIT)
204
// Get the current timestamp
205
time(¤t_timestamp);
206
history_limit = get_unix_timestamp_equivalent(limit);
209
for (std::vector<std::string>::const_iterator iter = history.begin() ; iter != history.end() ; ++iter) {
210
HistoryItem new_item(*iter);
211
int item_timestamp = atoi(new_item.get_timestamp().c_str());
213
if (item_timestamp >= ((int) current_timestamp - history_limit)) {
214
add_new_history_entry(new_item);