1
/* This file is part of Clementine.
2
Copyright 2011, David Sansome <me@davidsansome.com>
4
Clementine is free software: you can redistribute it and/or modify
5
it under the terms of the GNU General Public License as published by
6
the Free Software Foundation, either version 3 of the License, or
7
(at your option) any later version.
9
Clementine is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
27
// Use a CachedList when you want to download and save a list of things from a
28
// remote service, updating it only periodically.
29
// T must be a registered metatype and must support being stored in
30
// QSettings. This usually means you have to implement QDataStream streaming
31
// operators, and use qRegisterMetaTypeStreamOperators.
33
typedef QList<T> ListType;
35
CachedList(const char* settings_group, const QString& name,
36
int cache_duration_secs)
37
: settings_group_(settings_group),
39
cache_duration_secs_(cache_duration_secs) {
44
s.beginGroup(settings_group_);
46
last_updated_ = s.value("last_refreshed_" + name_).toDateTime();
49
const int count = s.beginReadArray(name_ + "_data");
50
for (int i=0 ; i<count ; ++i) {
52
data_ << s.value("value").value<T>();
59
s.beginGroup(settings_group_);
61
s.setValue("last_refreshed_" + name_, last_updated_);
63
s.beginWriteArray(name_ + "_data", data_.size());
64
for (int i=0 ; i<data_.size() ; ++i) {
66
s.setValue("value", QVariant::fromValue(data_[i]));
71
void Update(const ListType& data) {
73
last_updated_ = QDateTime::currentDateTime();
77
bool IsStale() const {
78
return last_updated_.isNull() ||
79
last_updated_.secsTo(QDateTime::currentDateTime()) > cache_duration_secs_;
82
const ListType& Data() const { return data_; }
83
operator ListType() const { return data_; }
86
typedef typename ListType::const_iterator const_iterator;
87
const_iterator begin() const { return data_.begin(); }
88
const_iterator end() const { return data_.end(); }
91
const char* settings_group_;
93
const int cache_duration_secs_;
95
QDateTime last_updated_;
99
#endif // CACHEDLIST_H