~ubuntu-branches/ubuntu/maverick/sflphone/maverick

« back to all changes in this revision

Viewing changes to sflphone-common/src/history/historymanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-06-03 15:59:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100603155946-ybe8d8o8zx8lp0m8
Tags: upstream-0.9.8.3
ImportĀ upstreamĀ versionĀ 0.9.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
 
3
 *
 
4
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 3 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
19
 *
 
20
 *  Additional permission under GNU GPL version 3 section 7:
 
21
 *
 
22
 *  If you modify this program, or any covered work, by linking or
 
23
 *  combining it with the OpenSSL project's OpenSSL library (or a
 
24
 *  modified version of that library), containing parts covered by the
 
25
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 
26
 *  grants you additional permission to convey the resulting work.
 
27
 *  Corresponding Source for a non-source form of such a combination
 
28
 *  shall include the source code for the parts of OpenSSL used as well
 
29
 *  as that of the covered work.
 
30
 */
 
31
 
 
32
#include <historymanager.h>
 
33
#include <errno.h>
 
34
#include <cc++/file.h>
 
35
#include <time.h>
 
36
 
 
37
HistoryManager::HistoryManager ()
 
38
        :       _history_loaded (false),
 
39
        _history_path ("")
 
40
{
 
41
 
 
42
}
 
43
 
 
44
HistoryManager::~HistoryManager ()
 
45
{
 
46
         HistoryItemMap::iterator iter = _history_items.begin();
 
47
 
 
48
         HistoryItem * item;
 
49
         while(iter != _history_items.end()){
 
50
                 item = iter->second;
 
51
                 delete item;
 
52
                 iter++;
 
53
         }
 
54
 
 
55
    // Clear the history map
 
56
    _history_items.clear ();
 
57
 
 
58
}
 
59
 
 
60
int HistoryManager::load_history (int limit, std::string path)
 
61
{
 
62
    Conf::ConfigTree history_list;
 
63
 
 
64
    create_history_path (path);
 
65
    load_history_from_file (&history_list);
 
66
    return load_history_items_map (&history_list, limit);
 
67
}
 
68
 
 
69
bool HistoryManager::save_history (void)
 
70
{
 
71
    Conf::ConfigTree history_list;
 
72
 
 
73
    save_history_items_map (&history_list);
 
74
    return save_history_to_file (&history_list);
 
75
}
 
76
 
 
77
bool HistoryManager::load_history_from_file (Conf::ConfigTree *history_list)
 
78
{
 
79
    bool exist;
 
80
 
 
81
    exist = history_list->populateFromFile (_history_path);
 
82
    _history_loaded = (exist == 2) ? false : true;
 
83
 
 
84
    return exist;
 
85
}
 
86
 
 
87
int HistoryManager::load_history_items_map (Conf::ConfigTree *history_list, int limit)
 
88
{
 
89
 
 
90
    short nb_items = 0;
 
91
    Conf::TokenList sections;
 
92
    HistoryItem *item;
 
93
    Conf::TokenList::iterator iter;
 
94
    std::string number, name, accountID, timestamp_start, timestamp_stop;
 
95
    CallType type;
 
96
    int history_limit;
 
97
    time_t current_timestamp;
 
98
 
 
99
    // We want to save only the items recent enough (ie compared to CONFIG_HISTORY_LIMIT)
 
100
    // Get the current timestamp
 
101
    (void) time (&current_timestamp);
 
102
    history_limit = get_unix_timestamp_equivalent (limit);
 
103
 
 
104
    sections = history_list->getSections();
 
105
    iter = sections.begin();
 
106
 
 
107
    while (iter != sections.end()) {
 
108
 
 
109
        type = (CallType) getConfigInt (*iter, "type", history_list);
 
110
        timestamp_stop = getConfigString (*iter, "timestamp_stop", history_list);
 
111
        name = getConfigString (*iter, "name", history_list);
 
112
        number = getConfigString (*iter, "number", history_list);
 
113
        accountID = getConfigString (*iter, "accountid", history_list);
 
114
        timestamp_start = *iter;
 
115
 
 
116
        // Make a check on the start timestamp to know it is loadable according to CONFIG_HISTORY_LIMIT
 
117
 
 
118
        if (atoi (timestamp_start.c_str ()) >= ( (int) current_timestamp - history_limit)) {
 
119
            item = new HistoryItem (timestamp_start, type, timestamp_stop, name, number, accountID);
 
120
            add_new_history_entry (item);
 
121
            nb_items ++;
 
122
        }
 
123
 
 
124
        iter ++;
 
125
    }
 
126
 
 
127
    return nb_items;
 
128
}
 
129
 
 
130
 
 
131
bool HistoryManager::save_history_to_file (Conf::ConfigTree *history_list)
 
132
{
 
133
    _debug ("Saving history in XDG directory: %s", _history_path.data());
 
134
    return  history_list->saveConfigTree (_history_path.data());
 
135
}
 
136
 
 
137
 
 
138
int HistoryManager::save_history_items_map (Conf::ConfigTree *history_list)
 
139
{
 
140
    HistoryItemMap::iterator iter;
 
141
    HistoryItem *item;
 
142
    int items_saved = 0;
 
143
 
 
144
    iter = _history_items.begin ();
 
145
 
 
146
    while (iter != _history_items.end ()) {
 
147
        item = iter->second;
 
148
 
 
149
        if (item) {
 
150
            if (item->save (&history_list))
 
151
                items_saved ++;
 
152
        } else {
 
153
            std::cout << "[DEBUG]: can't save NULL history item." << std::endl;
 
154
        }
 
155
 
 
156
        iter ++;
 
157
    }
 
158
 
 
159
    return items_saved;
 
160
}
 
161
 
 
162
void HistoryManager::add_new_history_entry (HistoryItem *new_item)
 
163
{
 
164
    // Add it in the map
 
165
    _history_items [new_item->get_timestamp () ] = new_item;
 
166
}
 
167
 
 
168
int HistoryManager::create_history_path (std::string path)
 
169
{
 
170
 
 
171
    std::string userdata, xdg_env, xdg_data;
 
172
 
 
173
    xdg_data = std::string (HOMEDIR) + DIR_SEPARATOR_STR + ".local/share/sflphone";
 
174
 
 
175
    if (path == "") {
 
176
 
 
177
        // If the environment variable is set (not null and not empty), we'll use it to save the history
 
178
        // Else we 'll the standard one, ie: XDG_DATA_HOME = $HOMEDIR/.local/share/sflphone
 
179
        if (XDG_DATA_HOME != NULL) {
 
180
            xdg_env = std::string (XDG_DATA_HOME);
 
181
            (xdg_env.length() > 0) ?    userdata = xdg_env
 
182
                                                : userdata = xdg_data;
 
183
        } else
 
184
            userdata = xdg_data;
 
185
 
 
186
        if (mkdir (userdata.data(), 0755) != 0) {
 
187
            // If directory     creation failed
 
188
            if (errno != EEXIST) {
 
189
                _debug ("Cannot create directory: %s", strerror (errno));
 
190
                return -1;
 
191
            }
 
192
        }
 
193
 
 
194
        // Load user's history
 
195
        _history_path = userdata + DIR_SEPARATOR_STR + "history";
 
196
    } else
 
197
        set_history_path (path);
 
198
 
 
199
 
 
200
    return 0;
 
201
}
 
202
 
 
203
// throw an Conf::ConfigTreeItemException if not found
 
204
int
 
205
HistoryManager::getConfigInt (const std::string& section, const std::string& name, Conf::ConfigTree *history_list)
 
206
{
 
207
    try {
 
208
        return history_list->getConfigTreeItemIntValue (section, name);
 
209
    } catch (Conf::ConfigTreeItemException& e) {
 
210
        throw e;
 
211
    }
 
212
 
 
213
    return 0;
 
214
}
 
215
 
 
216
std::string
 
217
HistoryManager::getConfigString (const std::string& section, const std::string& name, Conf::ConfigTree *history_list)
 
218
{
 
219
    try {
 
220
        return history_list->getConfigTreeItemValue (section, name);
 
221
    } catch (Conf::ConfigTreeItemException& e) {
 
222
        throw e;
 
223
    }
 
224
 
 
225
    return "";
 
226
}
 
227
 
 
228
std::map <std::string, std::string> HistoryManager::get_history_serialized (void)
 
229
{
 
230
    std::map <std::string, std::string> serialized;
 
231
    HistoryItemMap::iterator iter;
 
232
    HistoryItem *current;
 
233
    std::string res, key;
 
234
 
 
235
    iter = _history_items.begin ();
 
236
 
 
237
    while (iter != _history_items.end()) {
 
238
        current = iter->second;
 
239
 
 
240
        if (current) {
 
241
            key = current->get_timestamp ();
 
242
            res = current->serialize ();
 
243
            serialized [key] = res;
 
244
        }
 
245
 
 
246
        iter ++;
 
247
    }
 
248
 
 
249
    return serialized;
 
250
}
 
251
 
 
252
 
 
253
int HistoryManager::set_serialized_history (std::map <std::string, std::string> history, int limit)
 
254
{
 
255
    std::map <std::string, std::string>::iterator iter;
 
256
    HistoryItem *new_item;
 
257
    int items_added = 0;
 
258
    int history_limit;
 
259
    time_t current_timestamp;
 
260
 
 
261
    // Clear the existing history
 
262
    _history_items.clear ();
 
263
 
 
264
    // We want to save only the items recent enough (ie compared to CONFIG_HISTORY_LIMIT)
 
265
    // Get the current timestamp
 
266
    (void) time (&current_timestamp);
 
267
    history_limit = get_unix_timestamp_equivalent (limit);
 
268
    iter = history.begin ();
 
269
 
 
270
    while (iter != history.end ()) {
 
271
        if (atoi (iter->first.c_str ()) >= ( (int) current_timestamp - history_limit)) {
 
272
            new_item = new HistoryItem (iter->first, iter->second);
 
273
            add_new_history_entry (new_item);
 
274
            items_added ++;
 
275
        }
 
276
 
 
277
        iter ++;
 
278
    }
 
279
 
 
280
    return items_added;
 
281
}
 
282
 
 
283