2 KNode, the KDE newsreader
3 Copyright (c) 1999-2005 the KNode authors.
4 See file AUTHORS for details
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 You should have received a copy of the GNU General Public License
11 along with this program; if not, write to the Free Software Foundation,
12 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
15 #include "knmemorymanager.h"
18 #include "knglobals.h"
19 #include "knarticlemanager.h"
20 #include "kngroupmanager.h"
21 #include "knfoldermanager.h"
27 KNMemoryManager::KNMemoryManager()
28 : c_ollCacheSize(0), a_rtCacheSize(0)
33 KNMemoryManager::~KNMemoryManager()
35 qDeleteAll( mColList
);
36 qDeleteAll( mArtList
);
40 void KNMemoryManager::updateCacheEntry( KNArticleCollection::Ptr c
)
45 if( (ci
=findCacheEntry(c
, true)) ) { // item is taken from the list
46 oldSize
=ci
->storageSize
;
48 kDebug(5003) <<"KNMemoryManager::updateCacheEntry() : collection (" << c
->name() <<") updated";
51 ci
=new CollectionItem(c
);
52 kDebug(5003) <<"KNMemoryManager::updateCacheEntry() : collection (" << c
->name() <<") added";
56 c_ollCacheSize
+= (ci
->storageSize
- oldSize
);
57 checkMemoryUsageCollections();
61 void KNMemoryManager::removeCacheEntry( KNArticleCollection::Ptr c
)
64 ci
=findCacheEntry(c
, true);
67 c_ollCacheSize
-= ci
->storageSize
;
70 kDebug(5003) <<"KNMemoryManager::removeCacheEntry() : collection removed (" << c
->name() <<"),"
71 << mColList
.count() << "collections left in cache";
76 void KNMemoryManager::prepareLoad( KNArticleCollection::Ptr c
)
80 c_ollCacheSize
+= ci
.storageSize
;
81 checkMemoryUsageCollections();
82 c_ollCacheSize
-= ci
.storageSize
;
86 void KNMemoryManager::updateCacheEntry( KNArticle::Ptr a
)
91 if( (ai
=findCacheEntry(a
, true)) ) {
92 oldSize
=ai
->storageSize
;
94 kDebug(5003) <<"KNMemoryManager::updateCacheEntry() : article updated";
97 ai
=new ArticleItem(a
);
98 kDebug(5003) <<"KNMemoryManager::updateCacheEntry() : article added";
102 a_rtCacheSize
+= (ai
->storageSize
- oldSize
);
103 checkMemoryUsageArticles();
107 void KNMemoryManager::removeCacheEntry( KNArticle::Ptr a
)
111 if( (ai
=findCacheEntry(a
, true)) ) {
112 a_rtCacheSize
-= ai
->storageSize
;
115 kDebug(5003) <<"KNMemoryManager::removeCacheEntry() : article removed,"
116 << mArtList
.count() << "articles left in cache";
122 KNMemoryManager::CollectionItem
* KNMemoryManager::findCacheEntry( KNArticleCollection::Ptr c
, bool take
)
124 for ( CollectionItem::List::Iterator it
= mColList
.begin(); it
!= mColList
.end(); ++it
) {
125 if ( (*it
)->col
== c
) {
126 CollectionItem
*ret
= (*it
);
128 mColList
.erase( it
);
137 KNMemoryManager::ArticleItem
* KNMemoryManager::findCacheEntry( KNArticle::Ptr a
, bool take
)
139 for ( ArticleItem::List::Iterator it
= mArtList
.begin(); it
!= mArtList
.end(); ++it
) {
140 if ( (*it
)->art
== a
) {
141 ArticleItem
*ret
= (*it
);
143 mArtList
.erase( it
);
152 void KNMemoryManager::checkMemoryUsageCollections()
154 int maxSize
= knGlobals
.settings()->collCacheSize() * 1024;
155 KNArticleCollection::Ptr c
;
157 if (c_ollCacheSize
> maxSize
) {
158 CollectionItem::List
tempList( mColList
); // work on a copy, KNGroup-/Foldermanager will
159 // modify the original list
161 for ( CollectionItem::List::Iterator it
= tempList
.begin(); it
!= tempList
.end(); ) {
162 if ( c_ollCacheSize
<= maxSize
)
164 // unloadHeaders() will remove the cache entry and thus invalidate the iterator!
168 if (c
->type() == KNCollection::CTgroup
)
169 knGlobals
.groupManager()->unloadHeaders( boost::static_pointer_cast
<KNGroup
>( c
), false ); // *try* to unload
171 if (c
->type() == KNCollection::CTfolder
)
172 knGlobals
.folderManager()->unloadHeaders( boost::static_pointer_cast
<KNFolder
>( c
), false ); // *try* to unload
176 kDebug(5003) <<"KNMemoryManager::checkMemoryUsageCollections() :"
177 << mColList
.count() << "collections in cache => Usage :"
178 << ( c_ollCacheSize
*100.0 / maxSize
) << "%";
182 void KNMemoryManager::checkMemoryUsageArticles()
184 int maxSize
= knGlobals
.settings()->artCacheSize() * 1024;
186 if (a_rtCacheSize
> maxSize
) {
187 ArticleItem::List
tempList( mArtList
); // work on a copy, KNArticlemanager will
188 // modify the original list
190 for ( ArticleItem::List::Iterator it
= mArtList
.begin(); it
!= mArtList
.end(); ) {
191 if ( a_rtCacheSize
<= maxSize
)
193 // unloadArticle() will remove the cache entry and thus invalidate the iterator!
194 KNArticle::Ptr art
= (*it
)->art
;
196 knGlobals
.articleManager()->unloadArticle( art
, false ); // *try* to unload
200 kDebug(5003) <<"KNMemoryManager::checkMemoryUsageArticles() :"
201 << mArtList
.count() << "articles in cache => Usage :"
202 << ( a_rtCacheSize
*100.0 / maxSize
) << "%";
206 void KNMemoryManager::ArticleItem::sync()
208 storageSize
=art
->storageSize();
212 void KNMemoryManager::CollectionItem::sync()
214 storageSize
=col
->length()*1024; // rule of thumb : ~1k per header