~ubuntu-branches/ubuntu/warty/gwenview/warty

« back to all changes in this revision

Viewing changes to src/gvcache.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christopher Martin
  • Date: 2004-06-13 18:55:04 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040613185504-net8ekxoswwvyxs9
Tags: 1.1.3-1
New upstream release. Translations now included.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim: set tabstop=4 shiftwidth=4 noexpandtab
 
2
/*
 
3
Gwenview - A simple image viewer for KDE
 
4
Copyright 2000-2004 Aur�lien G�teau
 
5
 
 
6
 This program is free software; you can redistribute it and/or
 
7
 modify it under the terms of the GNU General Public License
 
8
 as published by the Free Software Foundation; either version 2
 
9
 of the License, or (at your option) any later version.
 
10
 
 
11
 This program is distributed in the hope that it will be useful,
 
12
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 GNU General Public License for more details.
 
15
 
 
16
 You should have received a copy of the GNU General Public License
 
17
 along with this program; if not, write to the Free Software
 
18
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
19
 
 
20
*/
 
21
 
 
22
#include "gvcache.h"
 
23
 
 
24
// Qt
 
25
 
 
26
// KDE
 
27
#include <kconfig.h>
 
28
#include <kdebug.h>
 
29
 
 
30
#include <kdeversion.h>
 
31
#if KDE_VERSION < 0x30200
 
32
#include <libgvcompat/kurlcompat.h>
 
33
#endif
 
34
// Local
 
35
 
 
36
const char CONFIG_CACHE_MAXSIZE[]="maxSize";
 
37
 
 
38
GVCache::GVCache()
 
39
: mMaxSize( DEFAULT_MAXSIZE )
 
40
{
 
41
}
 
42
 
 
43
GVCache* GVCache::instance() {
 
44
        static GVCache manager;
 
45
        return &manager;
 
46
}
 
47
 
 
48
void GVCache::addFile( const KURL& url, const QByteArray& file, const QDateTime& timestamp ) {
 
49
        updateAge();
 
50
        bool insert = true;
 
51
        if( mImages.contains( url )) {
 
52
                ImageData& data = mImages[ url ];
 
53
                if( data.timestamp == timestamp ) {
 
54
                        data.addFile( file );
 
55
                        insert = false;
 
56
                }
 
57
        }
 
58
        if( insert ) mImages[ url ] = ImageData( url, file, timestamp );
 
59
        checkMaxSize();
 
60
}
 
61
 
 
62
void GVCache::addImage( const KURL& url, const QImage& im, const QCString& format, const QDateTime& timestamp ) {
 
63
        updateAge();
 
64
        bool insert = true;
 
65
        if( mImages.contains( url )) {
 
66
                ImageData& data = mImages[ url ];
 
67
                if( data.timestamp == timestamp ) {
 
68
                        data.addImage( im, format );
 
69
                        insert = false;
 
70
                }
 
71
        }
 
72
        if( insert ) mImages[ url ] = ImageData( url, im, format, timestamp );
 
73
        checkMaxSize();
 
74
}
 
75
 
 
76
QDateTime GVCache::timestamp( const KURL& url ) const {
 
77
        if( mImages.contains( url )) return mImages[ url ].timestamp;
 
78
        return QDateTime();
 
79
}
 
80
 
 
81
QByteArray GVCache::file( const KURL& url ) const {
 
82
        if( mImages.contains( url )) {
 
83
                const ImageData& data = mImages[ url ];
 
84
                if( data.file.isNull()) return QByteArray();
 
85
                data.age = 0;
 
86
                return data.file;
 
87
        }
 
88
        return QByteArray();
 
89
}
 
90
 
 
91
QImage GVCache::image( const KURL& url, QCString& format ) const {
 
92
        if( mImages.contains( url )) {
 
93
                const ImageData& data = mImages[ url ];
 
94
                if( data.image.isNull()) return QImage();
 
95
                format = data.format;
 
96
                data.age = 0;
 
97
                return data.image;
 
98
        }
 
99
        return QImage();
 
100
}
 
101
 
 
102
void GVCache::updateAge() {
 
103
        for( QMap< KURL, ImageData >::Iterator it = mImages.begin();
 
104
             it != mImages.end();
 
105
             ++it ) {
 
106
                (*it).age++;
 
107
        }
 
108
}
 
109
 
 
110
void GVCache::checkMaxSize() {
 
111
        for(;;) {
 
112
                int size = 0;
 
113
                QMap< KURL, ImageData >::Iterator max;
 
114
                long long max_cost = -1;
 
115
                for( QMap< KURL, ImageData >::Iterator it = mImages.begin();
 
116
                     it != mImages.end();
 
117
                     ++it ) {
 
118
                        size += (*it).size();
 
119
                        long long cost = (*it).cost();
 
120
                        if( cost > max_cost ) {
 
121
                                max_cost = cost;
 
122
                                max = it;
 
123
                        }
 
124
                }
 
125
                if( size <= mMaxSize ) {
 
126
                        break;
 
127
                }
 
128
                if( !(*max).reduceSize()) mImages.remove( max );
 
129
        }
 
130
}
 
131
 
 
132
void GVCache::readConfig(KConfig* config,const QString& group) {
 
133
        KConfigGroupSaver saver( config, group );
 
134
        mMaxSize = config->readNumEntry( CONFIG_CACHE_MAXSIZE, mMaxSize );
 
135
        checkMaxSize();
 
136
}
 
137
 
 
138
GVCache::ImageData::ImageData( const KURL& url, const QByteArray& f, const QDateTime& t )
 
139
: file( f )
 
140
, timestamp( t )
 
141
, age( 0 )
 
142
, local_url( url.isLocalFile())
 
143
{
 
144
        file.detach(); // explicit sharing
 
145
}
 
146
 
 
147
GVCache::ImageData::ImageData( const KURL& url, const QImage& im, const QCString& f, const QDateTime& t )
 
148
: image( im )
 
149
, format( f )
 
150
, timestamp( t )
 
151
, age( 0 )
 
152
, local_url( url.isLocalFile())
 
153
{
 
154
}
 
155
 
 
156
void GVCache::ImageData::addFile( const QByteArray& f ) {
 
157
        file = f;
 
158
        file.detach(); // explicit sharing
 
159
        age = 0;
 
160
}
 
161
 
 
162
void GVCache::ImageData::addImage( const QImage& i, const QCString& f ) {
 
163
        image = i;
 
164
        format = f;
 
165
        age = 0;
 
166
}
 
167
 
 
168
int GVCache::ImageData::size() const {
 
169
        int ret = 0;
 
170
        if( !file.isNull()) ret += file.size();
 
171
        if( !image.isNull()) ret += image.height() * image.width() * image.depth() / 8;
 
172
        return ret;
 
173
}
 
174
 
 
175
bool GVCache::ImageData::reduceSize() {
 
176
        if( !file.isNull() && local_url && !image.isNull()) {
 
177
                file = QByteArray();
 
178
                return true;
 
179
        }
 
180
        if( !file.isNull() && !image.isNull()) {
 
181
                image = QImage();
 
182
                return true;
 
183
        }
 
184
        return false; // reducing here would mean clearing everything
 
185
}
 
186
 
 
187
long long GVCache::ImageData::cost() const {
 
188
        long long s = size();
 
189
        if( local_url && !file.isNull()) {
 
190
                s *= 100; // heavy penalty for storing local files
 
191
        }
 
192
        static const int mod[] = { 50, 30, 20, 16, 12, 10 };
 
193
        if( age <= 5 ) {
 
194
                return s * 10 / mod[ age ];
 
195
        } else {
 
196
                return s * ( age - 5 );
 
197
        }
 
198
}