~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (1.1.11)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: package-import@ubuntu.com-20140128182336-3xenud1kbnwmf3mz
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 *  Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
 
2
 *  Copyright (C) 2004-2013 Savoir-Faire Linux Inc.
3
3
 *
4
4
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
5
5
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
36
36
#include <fstream>
37
37
#include <sys/stat.h> // for mkdir
38
38
#include <ctime>
39
 
#include "scoped_lock.h"
 
39
#include <cstring>
40
40
#include "fileutils.h"
41
41
#include "logger.h"
42
42
#include "call.h"
43
43
 
44
44
namespace sfl {
45
45
 
 
46
History::History() : historyItemsMutex_(), items_(), path_() {}
 
47
 
 
48
 
46
49
using std::map;
47
50
using std::string;
48
51
using std::vector;
49
52
 
50
 
History::History() : historyItemsMutex_(), items_(), path_("")
51
 
{
52
 
    pthread_mutex_init(&historyItemsMutex_, NULL);
53
 
}
54
 
 
55
 
History::~History()
56
 
{
57
 
    pthread_mutex_destroy(&historyItemsMutex_);
58
 
}
59
 
 
60
53
bool History::load(int limit)
61
54
{
 
55
    // load only once
 
56
    if (!items_.empty())
 
57
        return true;
 
58
 
62
59
    ensurePath();
63
60
    std::ifstream infile(path_.c_str());
64
61
    if (!infile) {
65
62
        DEBUG("No history file to load");
66
63
        return false;
67
64
    }
 
65
 
68
66
    while (!infile.eof()) {
69
67
        HistoryItem item(infile);
70
68
        addEntry(item, limit);
71
69
    }
 
70
 
72
71
    return true;
73
72
}
74
73
 
75
74
bool History::save()
76
75
{
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());
79
78
    ensurePath();
80
79
    std::sort(items_.begin(), items_.end());
81
80
    std::ofstream outfile(path_.c_str());
82
81
    if (outfile.fail())
83
82
        return false;
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;
87
85
    return true;
88
86
}
89
87
 
90
88
void History::addEntry(const HistoryItem &item, int oldest)
91
89
{
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);
95
93
}
97
95
void History::ensurePath()
98
96
{
99
97
    if (path_.empty()) {
 
98
#ifdef __ANDROID__
 
99
                path_ = fileutils::get_home_dir() + DIR_SEPARATOR_STR  + "history";
 
100
#else
100
101
        const string xdg_data = fileutils::get_home_dir() + DIR_SEPARATOR_STR +
101
102
                                ".local/share/sflphone";
102
 
 
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());
 
112
                Logger::strErr();
112
113
                return;
113
114
            }
114
115
        }
115
116
        // Load user's history
116
117
        path_ = userdata + DIR_SEPARATOR_STR + "history";
 
118
#endif
117
119
    }
118
120
}
119
121
 
120
122
vector<map<string, string> > History::getSerialized()
121
123
{
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());
127
128
 
128
129
    return result;
129
130
}
146
147
 
147
148
void History::clear()
148
149
{
149
 
    sfl::ScopedLock lock(historyItemsMutex_);
 
150
    std::lock_guard<std::mutex> lock(historyItemsMutex_);
150
151
    items_.clear();
151
152
}
152
153
 
153
154
bool History::empty()
154
155
{
155
 
    sfl::ScopedLock lock(historyItemsMutex_);
 
156
    std::lock_guard<std::mutex> lock(historyItemsMutex_);
156
157
    return items_.empty();
157
158
}
158
159
 
159
160
 
160
161
size_t History::numberOfItems()
161
162
{
162
 
    sfl::ScopedLock lock(historyItemsMutex_);
 
163
    std::lock_guard<std::mutex> lock(historyItemsMutex_);
163
164
    return items_.size();
164
165
}
165
166