~ubuntu-branches/ubuntu/precise/sflphone/precise

« back to all changes in this revision

Viewing changes to daemon/src/history/historymanager.cpp

  • Committer: Package Import Robot
  • Author(s): Whoopie
  • Date: 2012-03-22 10:29:10 UTC
  • mfrom: (4.1.12 sid)
  • Revision ID: package-import@ubuntu.com-20120322102910-tb8hugi2su1tguwh
Tags: 1.0.2-1ubuntu1
* Apply some upstream patches to fix FTBFS (LP: #913018):
  - debian/patches/05_glib_includes.patch: fix glib includes.
  - debian/patches/06_use_XkbKeycodeToKeysym.patch: use 
    XkbKeycodeToKeysym instead of (deprecated) XKeycodeToKeysym.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Copyright (C) 2004, 2005, 2006, 2008, 2009, 2010, 2011 Savoir-Faire Linux Inc.
3
 
 *
4
 
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
5
 
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
6
 
 *
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.
11
 
 *
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.
16
 
 *
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.
20
 
 *
21
 
 *  Additional permission under GNU GPL version 3 section 7:
22
 
 *
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.
31
 
 */
32
 
 
33
 
#include <historymanager.h>
34
 
#include <errno.h>
35
 
#include <cc++/file.h>
36
 
#include <time.h>
37
 
 
38
 
HistoryManager::HistoryManager() :
39
 
    history_items_(), history_loaded_(false), history_path_("")
40
 
{}
41
 
 
42
 
int HistoryManager::load_history(int limit, const std::string &path)
43
 
{
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);
48
 
}
49
 
 
50
 
bool HistoryManager::save_history()
51
 
{
52
 
    Conf::ConfigTree history_list;
53
 
    save_history_items_map(&history_list);
54
 
    return save_history_to_file(&history_list);
55
 
}
56
 
 
57
 
bool HistoryManager::load_history_from_file(Conf::ConfigTree *history_list)
58
 
{
59
 
    DEBUG("HistoryManager: Load history from file %s", history_path_.c_str());
60
 
 
61
 
    int exist = history_list->populateFromFile(history_path_.c_str());
62
 
    history_loaded_ = (exist == 2) ? false : true;
63
 
 
64
 
    return history_loaded_;
65
 
}
66
 
 
67
 
int HistoryManager::load_history_items_map(Conf::ConfigTree *history_list, int limit)
68
 
{
69
 
    using std::string;
70
 
 
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(&current_timestamp);
75
 
    int history_limit = get_unix_timestamp_equivalent(limit);
76
 
 
77
 
    Conf::TokenList sections(history_list->getSections());
78
 
    int nb_items = 0;
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));
90
 
 
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);
95
 
            ++nb_items;
96
 
        }
97
 
    }
98
 
 
99
 
    return nb_items;
100
 
}
101
 
 
102
 
 
103
 
bool HistoryManager::save_history_to_file(Conf::ConfigTree *history_list)
104
 
{
105
 
    DEBUG("HistoryManager: Saving history in XDG directory: %s", history_path_.c_str());
106
 
    return history_list->saveConfigTree(history_path_.data());
107
 
}
108
 
 
109
 
int HistoryManager::save_history_items_map(Conf::ConfigTree *history_list)
110
 
{
111
 
    int items_saved = 0;
112
 
    for (std::vector<HistoryItem>::iterator iter = history_items_.begin(); iter != history_items_.end(); ++iter) {
113
 
        if (iter->save(&history_list))
114
 
            ++items_saved;
115
 
        else
116
 
            DEBUG("can't save NULL history item\n");
117
 
    }
118
 
 
119
 
    return items_saved;
120
 
}
121
 
 
122
 
void HistoryManager::add_new_history_entry(const HistoryItem &new_item)
123
 
{
124
 
    // Add it in the map
125
 
    history_items_.push_back(new_item);
126
 
}
127
 
 
128
 
int HistoryManager::create_history_path(std::string path)
129
 
{
130
 
    std::string userdata, xdg_env, xdg_data;
131
 
 
132
 
    xdg_data = std::string(HOMEDIR) + DIR_SEPARATOR_STR + ".local/share/sflphone";
133
 
 
134
 
    if (path.empty()) {
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;
140
 
        } else
141
 
            userdata = xdg_data;
142
 
 
143
 
        if (mkdir(userdata.data(), 0755) != 0) {
144
 
            // If directory     creation failed
145
 
            if (errno != EEXIST) {
146
 
                DEBUG("HistoryManager: Cannot create directory: %m");
147
 
                return -1;
148
 
            }
149
 
        }
150
 
 
151
 
        // Load user's history
152
 
        history_path_ = userdata + DIR_SEPARATOR_STR + "history";
153
 
    } else
154
 
        set_history_path(path);
155
 
 
156
 
    return 0;
157
 
}
158
 
 
159
 
// throw an Conf::ConfigTreeItemException if not found
160
 
int
161
 
HistoryManager::getConfigInt(const std::string& section, const std::string& name, Conf::ConfigTree *history_list)
162
 
{
163
 
    try {
164
 
        return history_list->getConfigTreeItemIntValue(section, name);
165
 
    } catch (const Conf::ConfigTreeItemException& e) {
166
 
        throw;
167
 
    }
168
 
 
169
 
    return 0;
170
 
}
171
 
 
172
 
std::string
173
 
HistoryManager::getConfigString(const std::string& section, const std::string& name, Conf::ConfigTree *history_list)
174
 
{
175
 
    try {
176
 
        return history_list->getConfigTreeItemValue(section, name);
177
 
    } catch (const Conf::ConfigTreeItemException& e) {
178
 
        throw;
179
 
    }
180
 
 
181
 
    return "";
182
 
}
183
 
 
184
 
std::vector<std::string> HistoryManager::get_history_serialized() const
185
 
{
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());
189
 
 
190
 
    return serialized;
191
 
}
192
 
 
193
 
 
194
 
int HistoryManager::set_serialized_history(const std::vector<std::string> &history, int limit)
195
 
{
196
 
    int history_limit;
197
 
    time_t current_timestamp;
198
 
 
199
 
    DEBUG("HistoryManager: Set serialized history");
200
 
 
201
 
    history_items_.clear();
202
 
 
203
 
    // We want to save only the items recent enough (ie compared to CONFIG_HISTORY_LIMIT)
204
 
    // Get the current timestamp
205
 
    time(&current_timestamp);
206
 
    history_limit = get_unix_timestamp_equivalent(limit);
207
 
 
208
 
    int items_added = 0;
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());
212
 
 
213
 
        if (item_timestamp >= ((int) current_timestamp - history_limit)) {
214
 
            add_new_history_entry(new_item);
215
 
            ++items_added;
216
 
        }
217
 
    }
218
 
 
219
 
    return items_added;
220
 
}
221