1
// vim: set tabstop=4 shiftwidth=4 noexpandtab
3
Gwenview - A simple image viewer for KDE
4
Copyright 2000-2004 Aur�lien G�teau
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.
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.
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.
30
#include <kdeversion.h>
31
#if KDE_VERSION < 0x30200
32
#include <libgvcompat/kurlcompat.h>
36
const char CONFIG_CACHE_MAXSIZE[]="maxSize";
39
: mMaxSize( DEFAULT_MAXSIZE )
43
GVCache* GVCache::instance() {
44
static GVCache manager;
48
void GVCache::addFile( const KURL& url, const QByteArray& file, const QDateTime& timestamp ) {
51
if( mImages.contains( url )) {
52
ImageData& data = mImages[ url ];
53
if( data.timestamp == timestamp ) {
58
if( insert ) mImages[ url ] = ImageData( url, file, timestamp );
62
void GVCache::addImage( const KURL& url, const QImage& im, const QCString& format, const QDateTime& timestamp ) {
65
if( mImages.contains( url )) {
66
ImageData& data = mImages[ url ];
67
if( data.timestamp == timestamp ) {
68
data.addImage( im, format );
72
if( insert ) mImages[ url ] = ImageData( url, im, format, timestamp );
76
QDateTime GVCache::timestamp( const KURL& url ) const {
77
if( mImages.contains( url )) return mImages[ url ].timestamp;
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();
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();
102
void GVCache::updateAge() {
103
for( QMap< KURL, ImageData >::Iterator it = mImages.begin();
110
void GVCache::checkMaxSize() {
113
QMap< KURL, ImageData >::Iterator max;
114
long long max_cost = -1;
115
for( QMap< KURL, ImageData >::Iterator it = mImages.begin();
118
size += (*it).size();
119
long long cost = (*it).cost();
120
if( cost > max_cost ) {
125
if( size <= mMaxSize ) {
128
if( !(*max).reduceSize()) mImages.remove( max );
132
void GVCache::readConfig(KConfig* config,const QString& group) {
133
KConfigGroupSaver saver( config, group );
134
mMaxSize = config->readNumEntry( CONFIG_CACHE_MAXSIZE, mMaxSize );
138
GVCache::ImageData::ImageData( const KURL& url, const QByteArray& f, const QDateTime& t )
142
, local_url( url.isLocalFile())
144
file.detach(); // explicit sharing
147
GVCache::ImageData::ImageData( const KURL& url, const QImage& im, const QCString& f, const QDateTime& t )
152
, local_url( url.isLocalFile())
156
void GVCache::ImageData::addFile( const QByteArray& f ) {
158
file.detach(); // explicit sharing
162
void GVCache::ImageData::addImage( const QImage& i, const QCString& f ) {
168
int GVCache::ImageData::size() const {
170
if( !file.isNull()) ret += file.size();
171
if( !image.isNull()) ret += image.height() * image.width() * image.depth() / 8;
175
bool GVCache::ImageData::reduceSize() {
176
if( !file.isNull() && local_url && !image.isNull()) {
180
if( !file.isNull() && !image.isNull()) {
184
return false; // reducing here would mean clearing everything
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
192
static const int mod[] = { 50, 30, 20, 16, 12, 10 };
194
return s * 10 / mod[ age ];
196
return s * ( age - 5 );