summaryrefslogtreecommitdiffstats
path: root/knode/knmemorymanager.cpp
blob: 37a6392871c72c12fd104eabafd898f67986350b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
    KNode, the KDE newsreader
    Copyright (c) 1999-2005 the KNode authors.
    See file AUTHORS for details

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US
*/

#include <kdebug.h>

#include "knmemorymanager.h"
#include "knfolder.h"
#include "knglobals.h"
#include "knconfigmanager.h"
#include "knarticlemanager.h"
#include "kngroupmanager.h"
#include "knfoldermanager.h"


KNMemoryManager::KNMemoryManager()
  : c_ollCacheSize(0), a_rtCacheSize(0)
{
}


KNMemoryManager::~KNMemoryManager()
{
  for ( TQValueList<CollectionItem*>::Iterator it = mColList.begin(); it != mColList.end(); ++it )
    delete (*it);
  for ( TQValueList<ArticleItem*>::Iterator it = mArtList.begin(); it != mArtList.end(); ++it )
    delete (*it);
}


void KNMemoryManager::updateCacheEntry(KNArticleCollection *c)
{
  CollectionItem *ci;
  int oldSize=0;

  if( (ci=findCacheEntry(c, true)) ) { // item is taken from the list
    oldSize=ci->storageSize;
    ci->sync();
    kdDebug(5003) << "KNMemoryManager::updateCacheEntry() : collection (" << c->name() << ") updated" << endl;
  }
  else {
    ci=new CollectionItem(c);
    kdDebug(5003) << "KNMemoryManager::updateCacheEntry() : collection (" << c->name() << ") added" << endl;
  }

  mColList.append(ci);
  c_ollCacheSize += (ci->storageSize - oldSize);
  checkMemoryUsageCollections();
}


void KNMemoryManager::removeCacheEntry(KNArticleCollection *c)
{
  CollectionItem *ci;
  ci=findCacheEntry(c, true);

  if(ci) {
    c_ollCacheSize -= ci->storageSize;
    delete ci;

    kdDebug(5003) << "KNMemoryManager::removeCacheEntry() : collection removed (" << c->name() << "), "
                  << mColList.count() << " collections left in cache" << endl;
  }
}


void KNMemoryManager::prepareLoad(KNArticleCollection *c)
{
  CollectionItem ci(c);

  c_ollCacheSize += ci.storageSize;
  checkMemoryUsageCollections();
  c_ollCacheSize -= ci.storageSize;
}


void KNMemoryManager::updateCacheEntry(KNArticle *a)
{
  ArticleItem *ai;
  int oldSize=0;

  if( (ai=findCacheEntry(a, true)) ) {
    oldSize=ai->storageSize;
    ai->sync();
    kdDebug(5003) << "KNMemoryManager::updateCacheEntry() : article updated" << endl;
  }
  else {
    ai=new ArticleItem(a);
    kdDebug(5003) << "KNMemoryManager::updateCacheEntry() : article added" << endl;
  }

  mArtList.append(ai);
  a_rtCacheSize += (ai->storageSize - oldSize);
  checkMemoryUsageArticles();
}


void KNMemoryManager::removeCacheEntry(KNArticle *a)
{
  ArticleItem *ai;

  if( (ai=findCacheEntry(a, true)) ) {
    a_rtCacheSize -= ai->storageSize;
    delete ai;

    kdDebug(5003) << "KNMemoryManager::removeCacheEntry() : article removed, "
                  << mArtList.count() << " articles left in cache" << endl;

  }
}


KNMemoryManager::CollectionItem* KNMemoryManager::findCacheEntry(KNArticleCollection *c, bool take)
{
  for ( TQValueList<CollectionItem*>::Iterator it = mColList.begin(); it != mColList.end(); ++it ) {
    if ( (*it)->col == c ) {
      CollectionItem *ret = (*it);
      if ( take )
        mColList.remove( it );
      return ret;
    }
  }

  return 0;
}


KNMemoryManager::ArticleItem* KNMemoryManager::findCacheEntry(KNArticle *a, bool take)
{
  for ( TQValueList<ArticleItem*>::Iterator it = mArtList.begin(); it != mArtList.end(); ++it ) {
    if ( (*it)->art == a ) {
      ArticleItem *ret = (*it);
      if ( take )
        mArtList.remove( it );
      return ret;
    }
  }

  return 0;
}


void KNMemoryManager::checkMemoryUsageCollections()
{
  int maxSize = knGlobals.configManager()->readNewsGeneral()->collCacheSize() * 1024;
  KNArticleCollection *c=0;

  if (c_ollCacheSize > maxSize) {
    TQValueList<CollectionItem*> tempList( mColList ); // work on a copy, KNGroup-/Foldermanager will
                                                      // modify the original list

    for ( TQValueList<CollectionItem*>::Iterator it = tempList.begin(); it != tempList.end(); ) {
      if ( c_ollCacheSize <= maxSize )
        break;
      // unloadHeaders() will remove the cache entry and thus invalidate the iterator!
      c = (*it)->col;
      ++it;

      if (c->type() == KNCollection::CTgroup)
        knGlobals.groupManager()->unloadHeaders(static_cast<KNGroup*>(c), false);   // *try* to unload
      else
        if (c->type() == KNCollection::CTfolder)
          knGlobals.folderManager()->unloadHeaders(static_cast<KNFolder*>(c), false);   // *try* to unload
    }
  }

  kdDebug(5003) << "KNMemoryManager::checkMemoryUsageCollections() : "
                << mColList.count() << " collections in cache => Usage : "
                << ( c_ollCacheSize*100.0 / maxSize ) << "%" << endl;
}


void KNMemoryManager::checkMemoryUsageArticles()
{
  int maxSize = knGlobals.configManager()->readNewsGeneral()->artCacheSize() * 1024;

  if (a_rtCacheSize > maxSize) {
    TQValueList<ArticleItem*> tempList( mArtList ); // work on a copy, KNArticlemanager will
                                                   // modify the original list

    for ( TQValueList<ArticleItem*>::Iterator it = mArtList.begin(); it != mArtList.end(); ) {
      if ( a_rtCacheSize <= maxSize )
        break;
      // unloadArticle() will remove the cache entry and thus invalidate the iterator!
      KNArticle *art = (*it)->art;
      ++it;
      knGlobals.articleManager()->unloadArticle( art, false );   // *try* to unload
    }
  }

  kdDebug(5003) << "KNMemoryManager::checkMemoryUsageArticles() : "
                << mArtList.count() << " articles in cache => Usage : "
                << ( a_rtCacheSize*100.0 / maxSize ) << "%" << endl;
}


void KNMemoryManager::ArticleItem::sync()
{
  storageSize=art->storageSize();
}


void KNMemoryManager::CollectionItem::sync()
{
  storageSize=col->length()*1024; // rule of thumb : ~1k per header
}