summaryrefslogtreecommitdiffstats
path: root/ksquirrel/sq_pixmapcache.cpp
blob: ea2485178fba487db525739003e813a0684bb9f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/***************************************************************************
                          sq_pixmapcache.cpp  -  description
                             -------------------
    begin                :  Sep 28 2004
    copyright            : (C) 2004 by Baryshev Dmitry
    email                : ksquirrel.iv@gmail.com
 ***************************************************************************/

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

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "sq_pixmapcache.h"
#include "sq_thumbnailsize.h"
#include "sq_dirthumbs.h"

SQ_PixmapCache * SQ_PixmapCache::m_instance = 0;

SQ_PixmapCache::SQ_PixmapCache(TQObject *tqparent, int limit) 
    : TQObject(tqparent), TQMap<KURL, SQ_Thumbnail>()
{
    m_instance = this;
    cache_limit = limit << 10;

    dir = new SQ_DirThumbs;

    valid_full = false;
}

SQ_PixmapCache::~SQ_PixmapCache()
{
    delete dir;
}

/*
 *  Write all entries to disk and clear cache.
 */
void SQ_PixmapCache::sync()
{
    // nothing to sync
    if(empty())
        return;

    iterator itEnd = end();

    // go through array and sync each entry
    for(iterator it = begin();it != itEnd;++it)
        syncEntry(it.key(), it.data());

    // remove all entries from cache
    clear();
}

/*
 *  Write one entry to disk and remove it from cache.
 */
void SQ_PixmapCache::syncEntry(const KURL &key, SQ_Thumbnail &thumb)
{
    // let SQ_DirThumbs save thumbnail
    dir->saveThumbnail(key, thumb);
}

/*
 *  Insert new entry to cache
 */
void SQ_PixmapCache::insert(const KURL &key, const SQ_Thumbnail &thumb)
{
    // thumbnail is null ?
    if(thumb.thumbnail.isNull())
        return;

    // calc new cache size
    last_full += entrySize(thumb);

    // add new entry
    TQMap<KURL, SQ_Thumbnail>::insert(key, thumb);
}

/*
 *  Remove entry from cache.
 */
void SQ_PixmapCache::removeEntry(const KURL &key)
{
    iterator it = find(key);

    // no item to remove ?
    if(it == end())
        return;

    last_full -= entrySize(it.data());

    TQMap<KURL, SQ_Thumbnail>::remove(key);
}

/*
 *  Remove entry from cache and from disk.
 */
void SQ_PixmapCache::removeEntryFull(const KURL &key)
{
    // remove from memory
    removeEntry(key);

    // remove from disk
    dir->removeFile(key);
}

/*
 *  Check if pixmap, represented by 'key', is already in cache.
 */
bool SQ_PixmapCache::contains2(const KURL &key, SQ_Thumbnail &th)
{
    iterator it = find(key);

    // item found
    if(it != end())
    {
        th = it.data();
        return true;
    }

    // not found
    return false;
}

/*
 *  Calculate total size used by cache (not exact!)
 */
int SQ_PixmapCache::totalSize()
{
    if(valid_full)
        return last_full;

    int total = 0;

    iterator itEnd = end();

    for(iterator it = begin();it != itEnd;++it)
    {
        total += entrySize(it.data());
    }

    last_full = total;
    valid_full = true;

    return total;
}

void SQ_PixmapCache::clear()
{
    valid_full = false;

    TQMap<KURL, SQ_Thumbnail>::clear();
}

int SQ_PixmapCache::entrySize(const SQ_Thumbnail &th) const
{
    return th.thumbnail.width() * th.thumbnail.height() * 4;
}