• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

knode

  • sources
  • kde-4.12
  • kdepim
  • knode
knmemorymanager.cpp
Go to the documentation of this file.
1 /*
2  KNode, the KDE newsreader
3  Copyright (c) 1999-2005 the KNode authors.
4  See file AUTHORS for details
5 
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
13 */
14 
15 #include "knmemorymanager.h"
16 
17 #include "knfolder.h"
18 #include "knglobals.h"
19 #include "knarticlemanager.h"
20 #include "kngroupmanager.h"
21 #include "knfoldermanager.h"
22 #include "settings.h"
23 
24 #include <KDebug>
25 
26 
27 KNMemoryManager::KNMemoryManager()
28  : c_ollCacheSize(0), a_rtCacheSize(0)
29 {
30 }
31 
32 
33 KNMemoryManager::~KNMemoryManager()
34 {
35  qDeleteAll( mColList );
36  qDeleteAll( mArtList );
37 }
38 
39 
40 void KNMemoryManager::updateCacheEntry( KNArticleCollection::Ptr c )
41 {
42  CollectionItem *ci;
43  int oldSize=0;
44 
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";
49  }
50  else {
51  ci=new CollectionItem(c);
52  kDebug(5003) <<"KNMemoryManager::updateCacheEntry() : collection (" << c->name() <<") added";
53  }
54 
55  mColList.append(ci);
56  c_ollCacheSize += (ci->storageSize - oldSize);
57  checkMemoryUsageCollections();
58 }
59 
60 
61 void KNMemoryManager::removeCacheEntry( KNArticleCollection::Ptr c )
62 {
63  CollectionItem *ci;
64  ci=findCacheEntry(c, true);
65 
66  if(ci) {
67  c_ollCacheSize -= ci->storageSize;
68  delete ci;
69 
70  kDebug(5003) <<"KNMemoryManager::removeCacheEntry() : collection removed (" << c->name() <<"),"
71  << mColList.count() << "collections left in cache";
72  }
73 }
74 
75 
76 void KNMemoryManager::prepareLoad( KNArticleCollection::Ptr c )
77 {
78  CollectionItem ci(c);
79 
80  c_ollCacheSize += ci.storageSize;
81  checkMemoryUsageCollections();
82  c_ollCacheSize -= ci.storageSize;
83 }
84 
85 
86 void KNMemoryManager::updateCacheEntry( KNArticle::Ptr a )
87 {
88  ArticleItem *ai;
89  int oldSize=0;
90 
91  if( (ai=findCacheEntry(a, true)) ) {
92  oldSize=ai->storageSize;
93  ai->sync();
94  kDebug(5003) <<"KNMemoryManager::updateCacheEntry() : article updated";
95  }
96  else {
97  ai=new ArticleItem(a);
98  kDebug(5003) <<"KNMemoryManager::updateCacheEntry() : article added";
99  }
100 
101  mArtList.append(ai);
102  a_rtCacheSize += (ai->storageSize - oldSize);
103  checkMemoryUsageArticles();
104 }
105 
106 
107 void KNMemoryManager::removeCacheEntry( KNArticle::Ptr a )
108 {
109  ArticleItem *ai;
110 
111  if( (ai=findCacheEntry(a, true)) ) {
112  a_rtCacheSize -= ai->storageSize;
113  delete ai;
114 
115  kDebug(5003) <<"KNMemoryManager::removeCacheEntry() : article removed,"
116  << mArtList.count() << "articles left in cache";
117 
118  }
119 }
120 
121 
122 KNMemoryManager::CollectionItem * KNMemoryManager::findCacheEntry( KNArticleCollection::Ptr c, bool take )
123 {
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;
130  }
131  }
132 
133  return 0;
134 }
135 
136 
137 KNMemoryManager::ArticleItem* KNMemoryManager::findCacheEntry( KNArticle::Ptr a, bool take )
138 {
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;
145  }
146  }
147 
148  return 0;
149 }
150 
151 
152 void KNMemoryManager::checkMemoryUsageCollections()
153 {
154  int maxSize = knGlobals.settings()->collCacheSize() * 1024;
155  KNArticleCollection::Ptr c;
156 
157  if (c_ollCacheSize > maxSize) {
158  CollectionItem::List tempList( mColList ); // work on a copy, KNGroup-/Foldermanager will
159  // modify the original list
160 
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;
167 
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
173  }
174  }
175 
176  kDebug(5003) <<"KNMemoryManager::checkMemoryUsageCollections() :"
177  << mColList.count() << "collections in cache => Usage :"
178  << ( c_ollCacheSize*100.0 / maxSize ) << "%";
179 }
180 
181 
182 void KNMemoryManager::checkMemoryUsageArticles()
183 {
184  int maxSize = knGlobals.settings()->artCacheSize() * 1024;
185 
186  if (a_rtCacheSize > maxSize) {
187  ArticleItem::List tempList( mArtList ); // work on a copy, KNArticlemanager will
188  // modify the original list
189 
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
197  }
198  }
199 
200  kDebug(5003) <<"KNMemoryManager::checkMemoryUsageArticles() :"
201  << mArtList.count() << "articles in cache => Usage :"
202  << ( a_rtCacheSize*100.0 / maxSize ) << "%";
203 }
204 
205 
206 void KNMemoryManager::ArticleItem::sync()
207 {
208  storageSize=art->storageSize();
209 }
210 
211 
212 void KNMemoryManager::CollectionItem::sync()
213 {
214  storageSize=col->length()*1024; // rule of thumb : ~1k per header
215 }
216 
KNMemoryManager::mColList
CollectionItem::List mColList
Definition: knmemorymanager.h:81
KNMemoryManager::updateCacheEntry
void updateCacheEntry(KNArticleCollection::Ptr c)
Collection-Handling.
Definition: knmemorymanager.cpp:40
KNMemoryManager::ArticleItem
Article cache item.
Definition: knmemorymanager.h:45
knmemorymanager.h
knfolder.h
KNMemoryManager::checkMemoryUsageCollections
void checkMemoryUsageCollections()
Definition: knmemorymanager.cpp:152
KNMemoryManager::~KNMemoryManager
~KNMemoryManager()
Definition: knmemorymanager.cpp:33
kngroupmanager.h
knarticlemanager.h
KNMemoryManager::ArticleItem::sync
void sync()
Definition: knmemorymanager.cpp:206
KNMemoryManager::ArticleItem::art
KNArticle::Ptr art
Definition: knmemorymanager.h:51
KNMemoryManager::c_ollCacheSize
int c_ollCacheSize
Definition: knmemorymanager.h:83
KNMemoryManager::CollectionItem::storageSize
int storageSize
Definition: knmemorymanager.h:70
KNMemoryManager::removeCacheEntry
void removeCacheEntry(KNArticleCollection::Ptr c)
Definition: knmemorymanager.cpp:61
KNCollection::CTgroup
Definition: kncollection.h:33
knglobals.h
KNMemoryManager::findCacheEntry
CollectionItem * findCacheEntry(KNArticleCollection::Ptr c, bool take=false)
Definition: knmemorymanager.cpp:122
knfoldermanager.h
settings.h
KNMemoryManager::CollectionItem
Group/folder cache item.
Definition: knmemorymanager.h:59
KNArticle::Ptr
boost::shared_ptr< KNArticle > Ptr
Shared pointer to a KNArticle. To be used instead of raw KNArticle*.
Definition: knarticle.h:39
KNCollection::CTfolder
Definition: kncollection.h:34
KNArticleCollection::Ptr
boost::shared_ptr< KNArticleCollection > Ptr
Shared pointer to a KNArticle.
Definition: knarticlecollection.h:82
KNMemoryManager::checkMemoryUsageArticles
void checkMemoryUsageArticles()
Definition: knmemorymanager.cpp:182
KNMemoryManager::KNMemoryManager
KNMemoryManager()
Definition: knmemorymanager.cpp:27
KNMemoryManager::prepareLoad
void prepareLoad(KNArticleCollection::Ptr c)
try to free enough memory for this collection
Definition: knmemorymanager.cpp:76
knGlobals
#define knGlobals
Keep compatibility with the old way.
Definition: knglobals.h:28
KNMemoryManager::ArticleItem::storageSize
int storageSize
Definition: knmemorymanager.h:52
KNMemoryManager::CollectionItem::sync
void sync()
Definition: knmemorymanager.cpp:212
KNMemoryManager::a_rtCacheSize
int a_rtCacheSize
Definition: knmemorymanager.h:83
KNMemoryManager::mArtList
ArticleItem::List mArtList
Definition: knmemorymanager.h:82
QList< KNMemoryManager::CollectionItem * >
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:58:36 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

knode

Skip menu "knode"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal