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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
|
/*
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 <tqdir.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kstandarddirs.h>
#include <kdebug.h>
#include "knglobals.h"
#include "knconfigmanager.h"
#include "knfolder.h"
#include "utilities.h"
#include "knfoldermanager.h"
#include "knarticlemanager.h"
#include "kncleanup.h"
#include "knmemorymanager.h"
#include "knmainwidget.h"
KNFolderManager::KNFolderManager(KNArticleManager *a) : a_rtManager(a)
{
//standard folders
TQString dir(locateLocal("data","knode/")+"folders/");
if (dir.isNull()) {
KNHelper::displayInternalFileError();
return;
}
KNFolder *f;
f=new KNFolder(0, i18n("Local Folders"), "root");
mFolderList.append( f );
f->readInfo();
f=new KNFolder(1, i18n("Drafts"), "drafts", root());
mFolderList.append( f );
f->readInfo();
f=new KNFolder(2, i18n("Outbox"), "outbox", root());
mFolderList.append( f );
f->readInfo();
f=new KNFolder(3, i18n("Sent"), "sent", root());
mFolderList.append( f );
f->readInfo();
l_astId=3;
//custom folders
loadCustomFolders();
setCurrentFolder(0);
}
KNFolderManager::~KNFolderManager()
{
for ( TQValueList<KNFolder*>::Iterator it = mFolderList.begin(); it != mFolderList.end(); ++it )
delete (*it);
}
void KNFolderManager::setCurrentFolder(KNFolder *f)
{
c_urrentFolder=f;
a_rtManager->setFolder(f);
kdDebug(5003) << "KNFolderManager::setCurrentFolder() : folder changed" << endl;
if(f && !f->isRootFolder()) {
if( loadHeaders(f) )
a_rtManager->showHdrs();
else
KMessageBox::error(knGlobals.topWidget, i18n("Cannot load index-file."));
}
}
bool KNFolderManager::loadHeaders(KNFolder *f)
{
if( !f || f->isRootFolder() )
return false;
if (f->isLoaded())
return true;
// we want to delete old stuff first => reduce vm fragmentation
knGlobals.memoryManager()->prepareLoad(f);
if (f->loadHdrs()) {
knGlobals.memoryManager()->updateCacheEntry( f );
return true;
}
return false;
}
bool KNFolderManager::unloadHeaders(KNFolder *f, bool force)
{
if(!f || !f->isLoaded())
return false;
if (!force && (c_urrentFolder == f))
return false;
if (f->unloadHdrs(force))
knGlobals.memoryManager()->removeCacheEntry(f);
else
return false;
return true;
}
KNFolder* KNFolderManager::folder(int i)
{
for ( TQValueList<KNFolder*>::Iterator it = mFolderList.begin(); it != mFolderList.end(); ++it )
if ( (*it)->id() == i )
return (*it);
return 0;
}
KNFolder* KNFolderManager::newFolder(KNFolder *p)
{
if (!p)
p = root();
KNFolder *f=new KNFolder(++l_astId, i18n("New folder"), p);
mFolderList.append( f );
emit folderAdded(f);
return f;
}
bool KNFolderManager::deleteFolder(KNFolder *f)
{
if(!f || f->isRootFolder() || f->isStandardFolder() || f->lockedArticles()>0)
return false;
TQValueList<KNFolder*> del;
KNCollection *p;
// find all subfolders of the folder we want to delete
for ( TQValueList<KNFolder*>::Iterator it = mFolderList.begin(); it != mFolderList.end(); ++it ) {
p = (*it)->parent();
while ( p ) {
if ( p == f ) {
if ( (*it)->lockedArticles() > 0 )
return false;
del.append( (*it) );
break;
}
p = p->parent();
}
}
emit folderRemoved(f);
del.append(f);
for ( TQValueList<KNFolder*>::Iterator it = del.begin(); it != del.end(); ++it ) {
if ( c_urrentFolder == (*it) )
c_urrentFolder = 0;
if ( unloadHeaders( (*it), true ) ) {
(*it)->deleteFiles();
mFolderList.remove( (*it) );
delete (*it);
} else
return false;
}
return true;
}
void KNFolderManager::emptyFolder(KNFolder *f)
{
if(!f || f->isRootFolder())
return;
knGlobals.memoryManager()->removeCacheEntry(f);
f->deleteAll();
}
bool KNFolderManager::moveFolder(KNFolder *f, KNFolder *p)
{
if(!f || p==f->parent()) // nothing to be done
return true;
// is "p" a child of "f" ?
KNCollection *p2=p?p->parent():0;
while(p2) {
if(p2==f)
break;
p2=p2->parent();
}
if( (p2 && p2==f) || f==p || f->isStandardFolder() || f->isRootFolder()) // no way ;-)
return false;
emit folderRemoved(f);
// reparent
f->setParent(p);
f->saveInfo();
emit folderAdded(f);
if(c_urrentFolder==f)
emit folderActivated(f);
return true;
}
int KNFolderManager::unsentForAccount(int accId)
{
int cnt=0;
for ( TQValueList<KNFolder*>::Iterator it = mFolderList.begin(); it != mFolderList.end(); ++it ) {
for ( int idx = 0; idx < (*it)->length(); ++idx ) {
KNLocalArticle *a = (*it)->at( idx );
if ( a->serverId() == accId && a->doPost() && !a->posted() )
cnt++;
}
}
return cnt;
}
void KNFolderManager::compactFolder(KNFolder *f)
{
if(!f || f->isRootFolder())
return;
KNCleanUp cup;
cup.compactFolder(f);
}
void KNFolderManager::compactAll(KNCleanUp *cup)
{
for ( TQValueList<KNFolder*>::Iterator it = mFolderList.begin(); it != mFolderList.end(); ++it )
if ( !(*it)->isRootFolder() && (*it)->lockedArticles() == 0 )
cup->appendCollection( (*it) );
}
void KNFolderManager::compactAll()
{
KNCleanUp *cup = new KNCleanUp();
compactAll( cup );
cup->start();
knGlobals.configManager()->cleanup()->setLastCompactDate();
delete cup;
}
void KNFolderManager::importFromMBox(KNFolder *f)
{
if(!f || f->isRootFolder())
return;
f->setNotUnloadable(true);
if (!f->isLoaded() && !loadHeaders(f)) {
f->setNotUnloadable(false);
return;
}
KNLoadHelper helper(knGlobals.topWidget);
KNFile *file = helper.getFile(i18n("Import MBox Folder"));
KNLocalArticle::List list;
KNLocalArticle *art;
TQString s;
int artStart=0, artEnd=0;
bool done=true;
if (file) {
knGlobals.top->setCursorBusy(true);
knGlobals.seStatusMsg(i18n(" Importing articles..."));
knGlobals.top->secureProcessEvents();
if (!file->atEnd()) { // search for the first article...
s = file->readLine();
if (s.left(5) == "From ") {
artStart = file->at();
done = false;
} else {
artStart = file->findString("\n\nFrom ");
if (artStart != -1) {
file->at(artStart+1);
s = file->readLine();
artStart = file->at();
done = false;
}
}
}
knGlobals.top->secureProcessEvents();
if (!done) {
while (!file->atEnd()) {
artEnd = file->findString("\n\nFrom ");
if (artEnd != -1) {
file->at(artStart); // seek the first character of the article
int size=artEnd-artStart;
TQCString buff(size+10);
int readBytes=file->readBlock(buff.data(), size);
if (readBytes != -1) {
buff.at(readBytes)='\0'; //terminate string
art = new KNLocalArticle(0);
art->setEditDisabled(true);
art->setContent(buff);
art->parse();
list.append(art);
}
file->at(artEnd+1);
s = file->readLine();
artStart = file->at();
} else {
if ((int)file->size() > artStart) {
file->at(artStart); // seek the first character of the article
int size=file->size()-artStart;
TQCString buff(size+10);
int readBytes=file->readBlock(buff.data(), size);
if (readBytes != -1) {
buff.at(readBytes)='\0'; //terminate string
art = new KNLocalArticle(0);
art->setEditDisabled(true);
art->setContent(buff);
art->parse();
list.append(art);
}
}
}
if (list.count()%75 == 0)
knGlobals.top->secureProcessEvents();
}
}
knGlobals.seStatusMsg(i18n(" Storing articles..."));
knGlobals.top->secureProcessEvents();
if (!list.isEmpty())
knGlobals.articleManager()->moveIntoFolder(list, f);
knGlobals.seStatusMsg(TQString::null);
knGlobals.top->setCursorBusy(false);
}
f->setNotUnloadable(false);
}
void KNFolderManager::exportToMBox(KNFolder *f)
{
if(!f || f->isRootFolder())
return;
f->setNotUnloadable(true);
if (!f->isLoaded() && !loadHeaders(f)) {
f->setNotUnloadable(false);
return;
}
KNSaveHelper helper(f->name()+".mbox", knGlobals.topWidget);
TQFile *file = helper.getFile(i18n("Export Folder"));
if (file) {
knGlobals.top->setCursorBusy(true);
knGlobals.seStatusMsg(i18n(" Exporting articles..."));
knGlobals.top->secureProcessEvents();
TQTextStream ts(file);
ts.setEncoding(TQTextStream::Latin1);
KNLocalArticle *a;
for(int idx=0; idx<f->length(); idx++) {
a=f->at(idx);
a->setNotUnloadable(true);
if (a->hasContent() || knGlobals.articleManager()->loadArticle(a)) {
ts << "From aaa@aaa Mon Jan 01 00:00:00 1997\n";
a->toStream(ts, true);
ts << "\n";
}
a->setNotUnloadable(false);
if (idx%75 == 0)
knGlobals.top->secureProcessEvents();
}
knGlobals.seStatusMsg(TQString::null);
knGlobals.top->setCursorBusy(false);
}
}
void KNFolderManager::syncFolders()
{
TQString dir(locateLocal("data","knode/")+"folders/");
if (dir.isNull()) {
KNHelper::displayInternalFileError();
return;
}
//sync
for ( TQValueList<KNFolder*>::Iterator it = mFolderList.begin(); it != mFolderList.end(); ++it ) {
if ( !(*it)->isRootFolder() )
(*it)->syncIndex();
(*it)->saveInfo();
}
}
int KNFolderManager::loadCustomFolders()
{
int cnt=0;
TQString dir(locateLocal("data","knode/")+"folders/");
KNFolder *f;
if (dir.isNull()) {
KNHelper::displayInternalFileError();
return 0;
}
TQDir d(dir);
TQStringList entries(d.entryList("custom_*.info")); // ignore info files of standard folders
for(TQStringList::Iterator it=entries.begin(); it != entries.end(); ++it) {
f=new KNFolder();
if(f->readInfo(d.absFilePath(*it))) {
if(f->id()>l_astId)
l_astId=f->id();
mFolderList.append( f );
cnt++;
}
else
delete f;
}
// set parents
if(cnt>0) {
for ( TQValueList<KNFolder*>::Iterator it = mFolderList.begin(); it != mFolderList.end(); ++it ) {
if ( !(*it)->isRootFolder() ) { // the root folder has no parent
KNFolder *par = folder( (*it)->parentId() );
if ( !par )
par = root();
(*it)->setParent( par );
}
}
}
return cnt;
}
#include "knfoldermanager.moc"
|