~ubuntu-branches/ubuntu/saucy/clementine/saucy

« back to all changes in this revision

Viewing changes to src/core/cachedlist.h

  • Committer: Package Import Robot
  • Author(s): Thomas PIERSON
  • Date: 2012-01-01 20:43:39 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20120101204339-lsb6nndwhfy05sde
Tags: 1.0.1+dfsg-1
New upstream release. (Closes: #653926, #651611, #657391)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This file is part of Clementine.
 
2
   Copyright 2011, David Sansome <me@davidsansome.com>
 
3
 
 
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.
 
8
 
 
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.
 
13
 
 
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/>.
 
16
*/
 
17
 
 
18
#ifndef CACHEDLIST_H
 
19
#define CACHEDLIST_H
 
20
 
 
21
#include <QDateTime>
 
22
#include <QSettings>
 
23
 
 
24
template <typename T>
 
25
class CachedList {
 
26
public:
 
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.
 
32
 
 
33
  typedef QList<T> ListType;
 
34
 
 
35
  CachedList(const char* settings_group, const QString& name,
 
36
             int cache_duration_secs)
 
37
    : settings_group_(settings_group),
 
38
      name_(name),
 
39
      cache_duration_secs_(cache_duration_secs) {
 
40
  }
 
41
 
 
42
  void Load() {
 
43
    QSettings s;
 
44
    s.beginGroup(settings_group_);
 
45
 
 
46
    last_updated_ = s.value("last_refreshed_" + name_).toDateTime();
 
47
    data_.clear();
 
48
 
 
49
    const int count = s.beginReadArray(name_ + "_data");
 
50
    for (int i=0 ; i<count ; ++i) {
 
51
      s.setArrayIndex(i);
 
52
      data_ << s.value("value").value<T>();
 
53
    }
 
54
    s.endArray();
 
55
  }
 
56
 
 
57
  void Save() const {
 
58
    QSettings s;
 
59
    s.beginGroup(settings_group_);
 
60
 
 
61
    s.setValue("last_refreshed_" + name_, last_updated_);
 
62
 
 
63
    s.beginWriteArray(name_ + "_data", data_.size());
 
64
    for (int i=0 ; i<data_.size() ; ++i) {
 
65
      s.setArrayIndex(i);
 
66
      s.setValue("value", QVariant::fromValue(data_[i]));
 
67
    }
 
68
    s.endArray();
 
69
  }
 
70
 
 
71
  void Update(const ListType& data) {
 
72
    data_ = data;
 
73
    last_updated_ = QDateTime::currentDateTime();
 
74
    Save();
 
75
  }
 
76
 
 
77
  bool IsStale() const {
 
78
    return last_updated_.isNull() ||
 
79
           last_updated_.secsTo(QDateTime::currentDateTime()) > cache_duration_secs_;
 
80
  }
 
81
 
 
82
  const ListType& Data() const { return data_; }
 
83
  operator ListType() const { return data_; }
 
84
 
 
85
  // Q_FOREACH support
 
86
  typedef typename ListType::const_iterator const_iterator;
 
87
  const_iterator begin() const { return data_.begin(); }
 
88
  const_iterator end() const { return data_.end(); }
 
89
 
 
90
private:
 
91
  const char* settings_group_;
 
92
  const QString name_;
 
93
  const int cache_duration_secs_;
 
94
 
 
95
  QDateTime last_updated_;
 
96
  ListType data_;
 
97
};
 
98
 
 
99
#endif // CACHEDLIST_H