2
* Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
2
* Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
4
4
* Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
5
5
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
37
37
#include <sys/stat.h> // for mkdir
39
#include "scoped_lock.h"
40
40
#include "fileutils.h"
41
41
#include "logger.h"
46
History::History() : historyItemsMutex_(), items_(), path_() {}
50
History::History() : historyItemsMutex_(), items_(), path_("")
52
pthread_mutex_init(&historyItemsMutex_, NULL);
57
pthread_mutex_destroy(&historyItemsMutex_);
60
53
bool History::load(int limit)
63
60
std::ifstream infile(path_.c_str());
65
62
DEBUG("No history file to load");
68
66
while (!infile.eof()) {
69
67
HistoryItem item(infile);
70
68
addEntry(item, limit);
75
74
bool History::save()
77
sfl::ScopedLock lock(historyItemsMutex_);
76
std::lock_guard<std::mutex> lock(historyItemsMutex_);
78
77
DEBUG("Saving history in XDG directory: %s", path_.c_str());
80
79
std::sort(items_.begin(), items_.end());
81
80
std::ofstream outfile(path_.c_str());
82
81
if (outfile.fail())
84
for (vector<HistoryItem>::const_iterator iter = items_.begin();
85
iter != items_.end(); ++iter)
86
outfile << *iter << std::endl;
83
for (const auto &item : items_)
84
outfile << item << std::endl;
90
88
void History::addEntry(const HistoryItem &item, int oldest)
92
sfl::ScopedLock lock(historyItemsMutex_);
90
std::lock_guard<std::mutex> lock(historyItemsMutex_);
93
91
if (item.hasPeerNumber() and item.youngerThan(oldest))
94
92
items_.push_back(item);
97
95
void History::ensurePath()
99
97
if (path_.empty()) {
99
path_ = fileutils::get_home_dir() + DIR_SEPARATOR_STR + "history";
100
101
const string xdg_data = fileutils::get_home_dir() + DIR_SEPARATOR_STR +
101
102
".local/share/sflphone";
103
103
// If the environment variable is set (not null and not empty), we'll use it to save the history
104
104
// Else we 'll the standard one, ie: XDG_DATA_HOME = $HOME/.local/share/sflphone
105
105
string xdg_env(XDG_DATA_HOME);
108
108
if (mkdir(userdata.data(), 0755) != 0) {
109
109
// If directory creation failed
110
110
if (errno != EEXIST) {
111
DEBUG("Cannot create directory: %m");
111
DEBUG("Cannot create directory: %s", userdata.c_str());
115
116
// Load user's history
116
117
path_ = userdata + DIR_SEPARATOR_STR + "history";
120
122
vector<map<string, string> > History::getSerialized()
122
sfl::ScopedLock lock(historyItemsMutex_);
124
std::lock_guard<std::mutex> lock(historyItemsMutex_);
123
125
vector<map<string, string> > result;
124
for (vector<HistoryItem>::const_iterator iter = items_.begin();
125
iter != items_.end(); ++iter)
126
result.push_back(iter->toMap());
126
for (const auto &item : items_)
127
result.push_back(item.toMap());
147
148
void History::clear()
149
sfl::ScopedLock lock(historyItemsMutex_);
150
std::lock_guard<std::mutex> lock(historyItemsMutex_);
153
154
bool History::empty()
155
sfl::ScopedLock lock(historyItemsMutex_);
156
std::lock_guard<std::mutex> lock(historyItemsMutex_);
156
157
return items_.empty();
160
161
size_t History::numberOfItems()
162
sfl::ScopedLock lock(historyItemsMutex_);
163
std::lock_guard<std::mutex> lock(historyItemsMutex_);
163
164
return items_.size();