history.cpp 4.58 KB
Newer Older
1
/*
2
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
 *  Author: Alexandre Savard <alexandre.savard@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
19
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

#include "history.h"
#include <cerrno>
#include <algorithm>
36
#include <fstream>
37 38
#include <sys/stat.h> // for mkdir
#include <ctime>
39
#include <cstring>
40
#include "fileutils.h"
41 42
#include "logger.h"
#include "call.h"
43

44 45
namespace sfl {

46 47
History::History() : historyItemsMutex_(), items_(), path_() {}

48

49 50 51
using std::map;
using std::string;
using std::vector;
52 53 54 55 56 57 58 59 60

bool History::load(int limit)
{
    ensurePath();
    std::ifstream infile(path_.c_str());
    if (!infile) {
        DEBUG("No history file to load");
        return false;
    }
61 62 63

    int counter = 0;
    while (!infile.eof() and counter < limit) {
64 65
        HistoryItem item(infile);
        addEntry(item, limit);
66
        ++counter;
67 68 69 70 71 72
    }
    return true;
}

bool History::save()
{
73
    std::lock_guard<std::mutex> lock(historyItemsMutex_);
Tristan Matthews's avatar
Tristan Matthews committed
74
    DEBUG("Saving history in XDG directory: %s", path_.c_str());
75 76 77 78 79
    ensurePath();
    std::sort(items_.begin(), items_.end());
    std::ofstream outfile(path_.c_str());
    if (outfile.fail())
        return false;
80 81
    for (const auto &item : items_)
        outfile << item << std::endl;
82 83 84 85 86
    return true;
}

void History::addEntry(const HistoryItem &item, int oldest)
{
87
    std::lock_guard<std::mutex> lock(historyItemsMutex_);
88 89 90 91 92 93 94
    if (item.hasPeerNumber() and item.youngerThan(oldest))
        items_.push_back(item);
}

void History::ensurePath()
{
    if (path_.empty()) {
95 96 97
#ifdef __ANDROID__
		path_ = fileutils::get_home_dir() + DIR_SEPARATOR_STR "history";
#else
98 99
        const string xdg_data = fileutils::get_home_dir() + DIR_SEPARATOR_STR +
                                ".local/share/sflphone";
100
        // If the environment variable is set (not null and not empty), we'll use it to save the history
101
        // Else we 'll the standard one, ie: XDG_DATA_HOME = $HOME/.local/share/sflphone
102
        string xdg_env(XDG_DATA_HOME);
103
        const string userdata = not xdg_env.empty() ? xdg_env : xdg_data;
104 105 106 107

        if (mkdir(userdata.data(), 0755) != 0) {
            // If directory	creation failed
            if (errno != EEXIST) {
108
                DEBUG("Cannot create directory: %s!: %s", userdata.c_str(), strerror(errno));
109 110 111 112 113
                return;
            }
        }
        // Load user's history
        path_ = userdata + DIR_SEPARATOR_STR + "history";
114
#endif
115 116 117
    }
}

118
vector<map<string, string> > History::getSerialized()
119
{
120
    std::lock_guard<std::mutex> lock(historyItemsMutex_);
121
    vector<map<string, string> > result;
122 123
    for (const auto &item : items_)
        result.push_back(item.toMap());
124 125 126 127 128 129 130 131 132 133 134 135

    return result;
}

void History::setPath(const std::string &path)
{
    path_ = path;
}

void History::addCall(Call *call, int limit)
{
    if (!call) {
Tristan Matthews's avatar
Tristan Matthews committed
136
        ERROR("Call is NULL, ignoring");
137 138 139 140 141 142 143 144 145
        return;
    }
    call->time_stop();
    HistoryItem item(call->createHistoryEntry());
    addEntry(item, limit);
}

void History::clear()
{
146
    std::lock_guard<std::mutex> lock(historyItemsMutex_);
147 148
    items_.clear();
}
149 150 151

bool History::empty()
{
152
    std::lock_guard<std::mutex> lock(historyItemsMutex_);
153 154 155 156 157 158
    return items_.empty();
}


size_t History::numberOfItems()
{
159
    std::lock_guard<std::mutex> lock(historyItemsMutex_);
160 161
    return items_.size();
}
162 163

}