/* This file is part of Clementine. Copyright 2011, David Sansome Clementine is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Clementine is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Clementine. If not, see . */ #ifndef CACHEDLIST_H #define CACHEDLIST_H #include #include template class CachedList { public: // Use a CachedList when you want to download and save a list of things from a // remote service, updating it only periodically. // T must be a registered metatype and must support being stored in // QSettings. This usually means you have to implement QDataStream streaming // operators, and use qRegisterMetaTypeStreamOperators. typedef QList ListType; CachedList(const char* settings_group, const QString& name, int cache_duration_secs) : settings_group_(settings_group), name_(name), cache_duration_secs_(cache_duration_secs) { } void Load() { QSettings s; s.beginGroup(settings_group_); last_updated_ = s.value("last_refreshed_" + name_).toDateTime(); data_.clear(); const int count = s.beginReadArray(name_ + "_data"); for (int i=0 ; i(); } s.endArray(); } void Save() const { QSettings s; s.beginGroup(settings_group_); s.setValue("last_refreshed_" + name_, last_updated_); s.beginWriteArray(name_ + "_data", data_.size()); for (int i=0 ; i cache_duration_secs_; } const ListType& Data() const { return data_; } operator ListType() const { return data_; } // Q_FOREACH support typedef typename ListType::const_iterator const_iterator; const_iterator begin() const { return data_.begin(); } const_iterator end() const { return data_.end(); } private: const char* settings_group_; const QString name_; const int cache_duration_secs_; QDateTime last_updated_; ListType data_; }; #endif // CACHEDLIST_H