Factor out the shared parts of the agent action manager setup.
[kdepim.git] / knode / knmemorymanager.cpp
blobf6b116ed3d34bee03517a0b973ad9a49290d02ac
1 /*
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"
17 #include "knfolder.h"
18 #include "knglobals.h"
19 #include "knarticlemanager.h"
20 #include "kngroupmanager.h"
21 #include "knfoldermanager.h"
22 #include "settings.h"
24 #include <KDebug>
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 )
42 CollectionItem *ci;
43 int oldSize=0;
45 if( (ci=findCacheEntry(c, true)) ) { // item is taken from the list
46 oldSize=ci->storageSize;
47 ci->sync();
48 kDebug(5003) <<"KNMemoryManager::updateCacheEntry() : collection (" << c->name() <<") updated";
50 else {
51 ci=new CollectionItem(c);
52 kDebug(5003) <<"KNMemoryManager::updateCacheEntry() : collection (" << c->name() <<") added";
55 mColList.append(ci);
56 c_ollCacheSize += (ci->storageSize - oldSize);
57 checkMemoryUsageCollections();
61 void KNMemoryManager::removeCacheEntry( KNArticleCollection::Ptr c )
63 CollectionItem *ci;
64 ci=findCacheEntry(c, true);
66 if(ci) {
67 c_ollCacheSize -= ci->storageSize;
68 delete ci;
70 kDebug(5003) <<"KNMemoryManager::removeCacheEntry() : collection removed (" << c->name() <<"),"
71 << mColList.count() << "collections left in cache";
76 void KNMemoryManager::prepareLoad( KNArticleCollection::Ptr c )
78 CollectionItem ci(c);
80 c_ollCacheSize += ci.storageSize;
81 checkMemoryUsageCollections();
82 c_ollCacheSize -= ci.storageSize;
86 void KNMemoryManager::updateCacheEntry( KNArticle::Ptr a )
88 ArticleItem *ai;
89 int oldSize=0;
91 if( (ai=findCacheEntry(a, true)) ) {
92 oldSize=ai->storageSize;
93 ai->sync();
94 kDebug(5003) <<"KNMemoryManager::updateCacheEntry() : article updated";
96 else {
97 ai=new ArticleItem(a);
98 kDebug(5003) <<"KNMemoryManager::updateCacheEntry() : article added";
101 mArtList.append(ai);
102 a_rtCacheSize += (ai->storageSize - oldSize);
103 checkMemoryUsageArticles();
107 void KNMemoryManager::removeCacheEntry( KNArticle::Ptr a )
109 ArticleItem *ai;
111 if( (ai=findCacheEntry(a, true)) ) {
112 a_rtCacheSize -= ai->storageSize;
113 delete ai;
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);
127 if ( take )
128 mColList.erase( it );
129 return ret;
133 return 0;
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);
142 if ( take )
143 mArtList.erase( it );
144 return ret;
148 return 0;
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 )
163 break;
164 // unloadHeaders() will remove the cache entry and thus invalidate the iterator!
165 c = (*it)->col;
166 ++it;
168 if (c->type() == KNCollection::CTgroup)
169 knGlobals.groupManager()->unloadHeaders( boost::static_pointer_cast<KNGroup>( c ), false ); // *try* to unload
170 else
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 )
192 break;
193 // unloadArticle() will remove the cache entry and thus invalidate the iterator!
194 KNArticle::Ptr art = (*it)->art;
195 ++it;
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