cache.cpp 2.42 KB
Newer Older
1
/***************************************************************************
2
                          cache.cpp  -  description
3
                             -------------------
4
    begin                : Sat Sep 7 2002
5
    copyright            : (C) 2002 by Scott Wheeler
6
    email                : wheeler@kde.org
7
 ***************************************************************************/
8 9 10 11 12 13 14 15 16 17

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

18 19 20
#include <kstandarddirs.h>
#include <kdebug.h>

21
#include "cache.h"
22
#include "cachedtag.h"
23 24

Cache *Cache::cache = 0;
25

26
////////////////////////////////////////////////////////////////////////////////
27
// public methods
28 29
////////////////////////////////////////////////////////////////////////////////

30
Cache *Cache::instance()
31
{
32
    if(cache == 0) {
33
	cache = new Cache();
34 35
	cache->load();
    }
36
    return(cache);
37 38
}

39
void Cache::save()
40
{
41
    QString cacheFileName = KGlobal::dirs()->saveLocation("appdata") + "cache";
42

43
    QFile f(cacheFileName);
44

45 46 47 48 49 50 51 52 53
    if(!f.open(IO_WriteOnly))
	return;

    QDataStream s(&f);

    for(QDictIterator<Tag>it(*this); it.current(); ++it) {
	s << it.current()->absFilePath()
	  << *(it.current());
    }
54

55
    f.close();
56 57
}

58 59 60 61 62
////////////////////////////////////////////////////////////////////////////////
// protected methods
////////////////////////////////////////////////////////////////////////////////

Cache::Cache() : QDict<Tag>()
63 64 65 66
{

}

67
Cache::~Cache()
68
{
69
    delete(cache);
70
}
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

void Cache::load()
{
    QString cacheFileName = KGlobal::dirs()->saveLocation("appdata") + "cache";

    QFile f(cacheFileName);

    if(!f.open(IO_ReadOnly))
	return;

    QDataStream s(&f);

    while(!s.atEnd()) {

	QString fileName;
	s >> fileName;

	CachedTag *t = new CachedTag(fileName);
	s >> *t;

91 92 93
	// Check the modification time of the file to make sure that 
	// the cache is current.

94 95 96 97 98 99
	if(!t->current())
	    delete(t);
    }

    f.close();
}