File indexing completed on 2022-08-04 15:34:39

0001 /**
0002  * Copyright (C) 2002-2004 Scott Wheeler <wheeler@kde.org>
0003  * Copyright (C) 2008, 2013 Michael Pyne <mpyne@kde.org>
0004  *
0005  * This program is free software; you can redistribute it and/or modify it under
0006  * the terms of the GNU General Public License as published by the Free Software
0007  * Foundation; either version 2 of the License, or (at your option) any later
0008  * version.
0009  *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License along with
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.
0016  */
0017 
0018 #ifndef JUK_CACHE_H
0019 #define JUK_CACHE_H
0020 
0021 #include <QDataStream>
0022 #include <QFile>
0023 #include <QBuffer>
0024 #include <QVector>
0025 
0026 class Playlist;
0027 class PlaylistCollection;
0028 class FileHandle;
0029 
0030 typedef QVector<Playlist *> PlaylistList;
0031 
0032 /**
0033  * A simple QDataStream subclass that has an extra field to indicate the cache
0034  * version.
0035  */
0036 
0037 class CacheDataStream : public QDataStream
0038 {
0039 public:
0040     CacheDataStream(QIODevice *d) : QDataStream(d), m_cacheVersion(0) {}
0041     CacheDataStream() : m_cacheVersion(0) { }
0042 
0043     int cacheVersion() const { return m_cacheVersion; }
0044     void setCacheVersion(int v) { m_cacheVersion = v; }
0045 
0046 private:
0047     int m_cacheVersion;
0048 };
0049 
0050 
0051 class Cache
0052 {
0053 public:
0054     static Cache *instance();
0055 
0056     static void loadPlaylists(PlaylistCollection *collection);
0057     static void savePlaylists(const PlaylistList &playlists);
0058 
0059     static void ensureAppDataStorageExists();
0060     static bool cacheFileExists();
0061 
0062     static QString fileHandleCacheFileName();
0063     static QString playlistsCacheFileName();
0064 
0065     bool prepareToLoadCachedItems();
0066     FileHandle loadNextCachedItem();
0067 
0068     /**
0069      * QDataStream version for serialized list of playlists
0070      * 1, 2: Who knows?
0071      * 3: Current.
0072      */
0073     static const int playlistListCacheVersion;
0074 
0075     /**
0076      * QDataStream version for serialized list of playlist items in a playlist
0077      * 1: Original cache version
0078      * 2: KDE 4.0.1+, explicitly sets QDataStream encoding.
0079      */
0080     static const int playlistItemsCacheVersion;
0081 
0082 private:
0083     // private to force access through instance()
0084     Cache();
0085 
0086 private:
0087     QFile m_loadFile;
0088     QBuffer m_loadFileBuffer;
0089     CacheDataStream m_loadDataStream;
0090 };
0091 
0092 #endif
0093 
0094 // vim: set et sw=4 tw=0 sta: