cache.h 2.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * Copyright (C) 2002-2004 Scott Wheeler <wheeler@kde.org>
 * Copyright (C) 2008, 2013 Michael Pyne <mpyne@kde.org>
 *
 * This program 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 2 of the License, or (at your option) any later
 * version.
 *
 * This program 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
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17

18 19
#ifndef JUK_CACHE_H
#define JUK_CACHE_H
20

Yuri Chornoivan's avatar
Yuri Chornoivan committed
21 22 23
#include <QDataStream>
#include <QFile>
#include <QBuffer>
24

25 26
class Playlist;
class PlaylistCollection;
27
class FileHandle;
28

29
template<class T>
30
class QVector;
31

32
typedef QVector<Playlist *> PlaylistList;
33

34 35 36 37 38 39 40 41 42
/**
 * A simple QDataStream subclass that has an extra field to indicate the cache
 * version.
 */

class CacheDataStream : public QDataStream
{
public:
    CacheDataStream(QIODevice *d) : QDataStream(d), m_cacheVersion(0) {}
43
    CacheDataStream() : m_cacheVersion(0) { }
44 45 46

    int cacheVersion() const { return m_cacheVersion; }
    void setCacheVersion(int v) { m_cacheVersion = v; }
47

48 49
private:
    int m_cacheVersion;
50 51
};

52

53
class Cache
54 55 56 57 58 59 60
{
public:
    static Cache *instance();

    static void loadPlaylists(PlaylistCollection *collection);
    static void savePlaylists(const PlaylistList &playlists);

61
    static void ensureAppDataStorageExists();
62 63
    static bool cacheFileExists();

64 65 66
    static QString fileHandleCacheFileName();
    static QString playlistsCacheFileName();

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    bool prepareToLoadCachedItems();
    FileHandle loadNextCachedItem();

    /**
     * QDataStream version for serialized list of playlists
     * 1, 2: Who knows?
     * 3: Current.
     */
    static const int playlistListCacheVersion;

    /**
     * QDataStream version for serialized list of playlist items in a playlist
     * 1: Original cache version
     * 2: KDE 4.0.1+, explicitly sets QDataStream encoding.
     */
    static const int playlistItemsCacheVersion;

84 85
private:
    // private to force access through instance()
86 87 88 89 90 91 92 93
    Cache();

private:
    QFile m_loadFile;
    QBuffer m_loadFileBuffer;
    CacheDataStream m_loadDataStream;
};

94
#endif
95 96

// vim: set et sw=4 tw=0 sta: