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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
/**
* kmacctimap.cpp
*
* Copyright (c) 2000-2002 Michael Haeckel <haeckel@kde.org>
*
* This file is based on popaccount.cpp by Don Sanders
*
* 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; version 2 of the License
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "kmacctimap.h"
using KMail::SieveConfig;
#include "kmmessage.h"
#include "broadcaststatus.h"
using KPIM::BroadcastStatus;
#include "kmfoldertree.h"
#include "kmfoldermgr.h"
#include "kmfolderimap.h"
#include "kmmainwin.h"
#include "kmmsgdict.h"
#include "kmfilter.h"
#include "kmfiltermgr.h"
#include "folderstorage.h"
#include "imapjob.h"
#include "actionscheduler.h"
using KMail::ActionScheduler;
using KMail::ImapJob;
using KMail::ImapAccountBase;
#include "progressmanager.h"
using KPIM::ProgressItem;
using KPIM::ProgressManager;
#include <kio/scheduler.h>
#include <kio/slave.h>
#include <kmessagebox.h>
#include <kdebug.h>
#include <stylesheet.h>
#include <errno.h>
//-----------------------------------------------------------------------------
KMAcctImap::KMAcctImap(AccountManager* aOwner, const TQString& aAccountName, uint id):
KMail::ImapAccountBase(aOwner, aAccountName, id),
mCountRemainChecks( 0 ),
mErrorTimer( 0, "mErrorTimer" )
{
mFolder = 0;
mScheduler = 0;
mNoopTimer.start( 60000 ); // // send a noop every minute
mOpenFolders.setAutoDelete(true);
connect(kmkernel->imapFolderMgr(), TQT_SIGNAL(changed()),
this, TQT_SLOT(slotUpdateFolderList()));
connect(&mErrorTimer, TQT_SIGNAL(timeout()), TQT_SLOT(slotResetConnectionError()));
TQString serNumUri = locateLocal( "data", "kmail/unfiltered." +
TQString("%1").arg(KAccount::id()) );
KConfig config( serNumUri );
TQStringList serNums = config.readListEntry( "unfiltered" );
mFilterSerNumsToSave.setAutoDelete( false );
for ( TQStringList::ConstIterator it = serNums.begin();
it != serNums.end(); ++it ) {
mFilterSerNums.append( (*it).toUInt() );
mFilterSerNumsToSave.insert( *it, (const int *)1 );
}
}
//-----------------------------------------------------------------------------
KMAcctImap::~KMAcctImap()
{
killAllJobs( true );
TQString serNumUri = locateLocal( "data", "kmail/unfiltered." +
TQString("%1").arg(KAccount::id()) );
KConfig config( serNumUri );
TQStringList serNums;
TQDictIterator<int> it( mFilterSerNumsToSave );
for( ; it.current(); ++it )
serNums.append( it.currentKey() );
config.writeEntry( "unfiltered", serNums );
}
//-----------------------------------------------------------------------------
TQString KMAcctImap::type() const
{
return "imap";
}
//-----------------------------------------------------------------------------
void KMAcctImap::pseudoAssign( const KMAccount * a ) {
killAllJobs( true );
if (mFolder)
{
mFolder->setContentState(KMFolderImap::imapNoInformation);
mFolder->setSubfolderState(KMFolderImap::imapNoInformation);
}
ImapAccountBase::pseudoAssign( a );
}
//-----------------------------------------------------------------------------
void KMAcctImap::setImapFolder(KMFolderImap *aFolder)
{
mFolder = aFolder;
mFolder->setImapPath( "/" );
}
//-----------------------------------------------------------------------------
bool KMAcctImap::handleError( int errorCode, const TQString &errorMsg, KIO::Job* job, const TQString& context, bool abortSync )
{
/* TODO check where to handle this one better. */
if ( errorCode == KIO::ERR_DOES_NOT_EXIST ) {
// folder is gone, so reload the folderlist
if ( mFolder )
mFolder->listDirectory();
return true;
}
return ImapAccountBase::handleError( errorCode, errorMsg, job, context, abortSync );
}
//-----------------------------------------------------------------------------
void KMAcctImap::killAllJobs( bool disconnectSlave )
{
TQMap<KIO::Job*, jobData>::Iterator it = mapJobData.begin();
for ( ; it != mapJobData.end(); ++it)
{
TQPtrList<KMMessage> msgList = (*it).msgList;
TQPtrList<KMMessage>::Iterator it2 = msgList.begin();
for ( ; it2 != msgList.end(); ++it2 ) {
KMMessage *msg = *it2;
if ( msg->transferInProgress() ) {
kdDebug(5006) << "KMAcctImap::killAllJobs - resetting mail" << endl;
msg->setTransferInProgress( false );
}
}
if ((*it).parent)
{
// clear folder state
KMFolderImap *fld = static_cast<KMFolderImap*>((*it).parent->storage());
fld->setCheckingValidity(false);
fld->quiet(false);
fld->setContentState(KMFolderImap::imapNoInformation);
fld->setSubfolderState(KMFolderImap::imapNoInformation);
fld->sendFolderComplete(FALSE);
fld->removeJobs();
}
if ( (*it).progressItem )
{
(*it).progressItem->setComplete();
}
}
if (mSlave && mapJobData.begin() != mapJobData.end())
{
mSlave->kill();
mSlave = 0;
}
// remove the jobs
mapJobData.clear();
// KMAccount::deleteFolderJobs(); doesn't work here always, it deletes jobs from
// its own mJobList instead of our mJobList...
KMAccount::deleteFolderJobs();
TQPtrListIterator<ImapJob> it2( mJobList );
while ( it2.current() ) {
ImapJob *job = it2.current();
++it2;
job->kill();
}
mJobList.clear();
// make sure that no new-mail-check is blocked
if (mCountRemainChecks > 0)
{
checkDone( false, CheckOK ); // returned 0 new messages
mCountRemainChecks = 0;
}
if ( disconnectSlave && slave() ) {
KIO::Scheduler::disconnectSlave( slave() );
mSlave = 0;
}
}
//-----------------------------------------------------------------------------
void KMAcctImap::ignoreJobsForMessage( KMMessage* msg )
{
if (!msg) return;
TQPtrListIterator<ImapJob> it( mJobList );
while ( it.current() )
{
ImapJob *job = it.current();
++it;
if ( job->msgList().first() == msg )
{
job->kill();
}
}
}
//-----------------------------------------------------------------------------
void KMAcctImap::ignoreJobsForFolder( KMFolder* folder )
{
TQPtrListIterator<ImapJob> it( mJobList );
while ( it.current() )
{
ImapJob *job = it.current();
++it;
if ( !job->msgList().isEmpty() && job->msgList().first()->parent() == folder )
{
job->kill();
}
}
}
//-----------------------------------------------------------------------------
void KMAcctImap::removeSlaveJobsForFolder( KMFolder* folder )
{
// Make sure the folder is not referenced in any kio slave jobs
TQMap<KIO::Job*, jobData>::Iterator it = mapJobData.begin();
while ( it != mapJobData.end() ) {
TQMap<KIO::Job*, jobData>::Iterator i = it;
it++;
if ( (*i).parent ) {
if ( (*i).parent == folder ) {
mapJobData.remove(i);
}
}
}
}
//-----------------------------------------------------------------------------
void KMAcctImap::cancelMailCheck()
{
// Make list of folders to reset, like in killAllJobs
TQValueList<KMFolderImap*> folderList;
TQMap<KIO::Job*, jobData>::Iterator it = mapJobData.begin();
for (; it != mapJobData.end(); ++it) {
if ( (*it).cancellable && (*it).parent ) {
folderList << static_cast<KMFolderImap*>((*it).parent->storage());
}
}
// Kill jobs
// FIXME
// ImapAccountBase::cancelMailCheck();
killAllJobs( true );
// emit folderComplete, this is important for
// KMAccount::checkingMail() to be reset, in case we restart checking mail later.
for( TQValueList<KMFolderImap*>::Iterator it = folderList.begin(); it != folderList.end(); ++it ) {
KMFolderImap *fld = *it;
fld->sendFolderComplete(FALSE);
}
}
//-----------------------------------------------------------------------------
void KMAcctImap::processNewMail(bool interactive)
{
kdDebug() << "processNewMail " << mCheckingSingleFolder << ",status="<<makeConnection()<<endl;
if (!mFolder || !mFolder->folder() || !mFolder->folder()->child() ||
makeConnection() == ImapAccountBase::Error)
{
mCountRemainChecks = 0;
mCheckingSingleFolder = false;
checkDone( false, CheckError );
return;
}
// if necessary then initialize the list of folders which should be checked
if( mMailCheckFolders.isEmpty() )
{
slotUpdateFolderList();
// if no folders should be checked then the check is finished
if( mMailCheckFolders.isEmpty() )
{
checkDone( false, CheckOK );
mCheckingSingleFolder = false;
return;
}
}
// Ok, we're really checking, get a progress item;
Q_ASSERT( !mMailCheckProgressItem );
mMailCheckProgressItem =
ProgressManager::createProgressItem(
"MailCheckAccount" + name(),
i18n("Checking account: %1" ).arg( TQStyleSheet::escape( name() ) ),
TQString(), // status
true, // can be canceled
useSSL() || useTLS() );
mMailCheckProgressItem->setTotalItems( mMailCheckFolders.count() );
connect ( mMailCheckProgressItem,
TQT_SIGNAL( progressItemCanceled( KPIM::ProgressItem*) ),
this,
TQT_SLOT( slotMailCheckCanceled() ) );
TQValueList<TQGuardedPtr<KMFolder> >::Iterator it;
// first get the current count of unread-messages
mCountRemainChecks = 0;
mCountUnread = 0;
mUnreadBeforeCheck.clear();
for (it = mMailCheckFolders.begin(); it != mMailCheckFolders.end(); ++it)
{
KMFolder *folder = *it;
if (folder && !folder->noContent())
{
mUnreadBeforeCheck[folder->idString()] = folder->countUnread();
}
}
bool gotError = false;
// then check for new mails
for (it = mMailCheckFolders.begin(); it != mMailCheckFolders.end(); ++it)
{
KMFolder *folder = *it;
if (folder && !folder->noContent())
{
KMFolderImap *imapFolder = static_cast<KMFolderImap*>(folder->storage());
if ( imapFolder->getContentState() != KMFolderImap::imapListingInProgress
&& imapFolder->getContentState() != KMFolderImap::imapDownloadInProgress )
{
// connect the result-signals for new-mail-notification
mCountRemainChecks++;
if (imapFolder->isSelected()) {
connect(imapFolder, TQT_SIGNAL(folderComplete(KMFolderImap*, bool)),
this, TQT_SLOT(postProcessNewMail(KMFolderImap*, bool)));
imapFolder->getFolder();
} else if ( kmkernel->filterMgr()->atLeastOneIncomingFilterAppliesTo( id() ) &&
imapFolder->folder()->isSystemFolder() &&
imapFolder->imapPath() == "/INBOX/" ) {
imapFolder->open("acctimap"); // will be closed in the folderSelected slot
// first get new headers before we select the folder
imapFolder->setSelected( true );
connect( imapFolder, TQT_SIGNAL( folderComplete( KMFolderImap*, bool ) ),
this, TQT_SLOT( slotFolderSelected( KMFolderImap*, bool) ) );
imapFolder->getFolder();
}
else {
connect(imapFolder, TQT_SIGNAL(numUnreadMsgsChanged(KMFolder*)),
this, TQT_SLOT(postProcessNewMail(KMFolder*)));
bool ok = imapFolder->processNewMail(interactive);
if (!ok)
{
// there was an error so cancel
mCountRemainChecks--;
gotError = true;
if ( mMailCheckProgressItem ) {
mMailCheckProgressItem->incCompletedItems();
mMailCheckProgressItem->updateProgress();
}
}
}
}
}
} // end for
if ( gotError )
slotUpdateFolderList();
// for the case the account is down and all folders report errors
if ( mCountRemainChecks == 0 )
{
mCountLastUnread = 0; // => mCountUnread - mCountLastUnread == new count
ImapAccountBase::postProcessNewMail();
mUnreadBeforeCheck.clear();
mCheckingSingleFolder = false;
}
}
//-----------------------------------------------------------------------------
void KMAcctImap::postProcessNewMail(KMFolderImap* folder, bool)
{
disconnect(folder, TQT_SIGNAL(folderComplete(KMFolderImap*, bool)),
this, TQT_SLOT(postProcessNewMail(KMFolderImap*, bool)));
postProcessNewMail(static_cast<KMFolder*>(folder->folder()));
}
void KMAcctImap::postProcessNewMail( KMFolder * folder )
{
disconnect( folder->storage(), TQT_SIGNAL(numUnreadMsgsChanged(KMFolder*)),
this, TQT_SLOT(postProcessNewMail(KMFolder*)) );
if ( mMailCheckProgressItem ) {
mMailCheckProgressItem->incCompletedItems();
mMailCheckProgressItem->updateProgress();
mMailCheckProgressItem->seStatus( folder->prettyURL() + i18n(" completed") );
}
mCountRemainChecks--;
// count the unread messages
const TQString folderId = folder->idString();
int newInFolder = folder->countUnread();
if ( mUnreadBeforeCheck.find( folderId ) != mUnreadBeforeCheck.end() )
newInFolder -= mUnreadBeforeCheck[folderId];
if ( newInFolder > 0 ) {
addToNewInFolder( folderId, newInFolder );
mCountUnread += newInFolder;
}
// Filter messages
TQValueListIterator<TQ_UINT32> filterIt = mFilterSerNums.begin();
TQValueList<TQ_UINT32> inTransit;
if (ActionScheduler::isEnabled() ||
kmkernel->filterMgr()->atLeastOneOnlineImapFolderTarget()) {
KMFilterMgr::FilterSet set = KMFilterMgr::Inbound;
TQValueList<KMFilter*> filters = kmkernel->filterMgr()->filters();
if (!mScheduler) {
mScheduler = new KMail::ActionScheduler( set, filters );
mScheduler->setAccountId( id() );
connect( mScheduler, TQT_SIGNAL(filtered(TQ_UINT32)), this, TQT_SLOT(slotFiltered(TQ_UINT32)) );
} else {
mScheduler->setFilterList( filters );
}
}
while (filterIt != mFilterSerNums.end()) {
int idx = -1;
KMFolder *folder = 0;
KMMessage *msg = 0;
KMMsgDict::instance()->getLocation( *filterIt, &folder, &idx );
// It's possible that the message has been deleted or moved into a
// different folder, or that the serNum is stale
if ( !folder ) {
mFilterSerNumsToSave.remove( TQString( "%1" ).arg( *filterIt ) );
++filterIt;
continue;
}
KMFolderImap *imapFolder = dynamic_cast<KMFolderImap*>(folder->storage());
if (!imapFolder ||
!imapFolder->folder()->isSystemFolder() ||
!(imapFolder->imapPath() == "/INBOX/") ) { // sanity checking
mFilterSerNumsToSave.remove( TQString( "%1" ).arg( *filterIt ) );
++filterIt;
continue;
}
if (idx != -1) {
msg = folder->getMsg( idx );
if (!msg) { // sanity checking
mFilterSerNumsToSave.remove( TQString( "%1" ).arg( *filterIt ) );
++filterIt;
continue;
}
if (ActionScheduler::isEnabled() ||
kmkernel->filterMgr()->atLeastOneOnlineImapFolderTarget()) {
mScheduler->execFilters( msg );
} else {
if (msg->transferInProgress()) {
inTransit.append( *filterIt );
++filterIt;
continue;
}
msg->setTransferInProgress(true);
if ( !msg->isComplete() ) {
FolderJob *job = folder->createJob(msg);
connect(job, TQT_SIGNAL(messageRetrieved(KMMessage*)),
TQT_SLOT(slotFilterMsg(KMMessage*)));
job->start();
} else {
mFilterSerNumsToSave.remove( TQString( "%1" ).arg( *filterIt ) );
if (slotFilterMsg(msg) == 2) break;
}
}
}
++filterIt;
}
mFilterSerNums = inTransit;
if (mCountRemainChecks == 0)
{
// all checks are done
mCountLastUnread = 0; // => mCountUnread - mCountLastUnread == new count
// when we check only one folder (=selected) and we have new mails
// then do not display a summary as the normal status message is better
bool showStatus = ( mCheckingSingleFolder && mCountUnread > 0 ) ? false : true;
ImapAccountBase::postProcessNewMail( showStatus );
mUnreadBeforeCheck.clear();
mCheckingSingleFolder = false;
}
}
//-----------------------------------------------------------------------------
void KMAcctImap::slotFiltered(TQ_UINT32 serNum)
{
mFilterSerNumsToSave.remove( TQString( "%1" ).arg( serNum ) );
}
//-----------------------------------------------------------------------------
void KMAcctImap::slotUpdateFolderList()
{
if ( !mFolder || !mFolder->folder() || !mFolder->folder()->child() )
{
kdWarning(5006) << "KMAcctImap::slotUpdateFolderList return" << endl;
return;
}
TQStringList strList;
mMailCheckFolders.clear();
kmkernel->imapFolderMgr()->createFolderList(&strList, &mMailCheckFolders,
mFolder->folder()->child(), TQString(), false);
// the new list
TQValueList<TQGuardedPtr<KMFolder> > includedFolders;
// check for excluded folders
TQValueList<TQGuardedPtr<KMFolder> >::Iterator it;
for (it = mMailCheckFolders.begin(); it != mMailCheckFolders.end(); ++it)
{
KMFolderImap* folder = static_cast<KMFolderImap*>(((KMFolder*)(*it))->storage());
if (folder->includeInMailCheck())
includedFolders.append(*it);
}
mMailCheckFolders = includedFolders;
}
//-----------------------------------------------------------------------------
void KMAcctImap::listDirectory()
{
mFolder->listDirectory();
}
//-----------------------------------------------------------------------------
void KMAcctImap::readConfig(KConfig& config)
{
ImapAccountBase::readConfig( config );
}
//-----------------------------------------------------------------------------
void KMAcctImap::slotMailCheckCanceled()
{
if( mMailCheckProgressItem )
mMailCheckProgressItem->setComplete();
cancelMailCheck();
}
//-----------------------------------------------------------------------------
FolderStorage* KMAcctImap::rootFolder() const
{
return mFolder;
}
ImapAccountBase::ConnectionState KMAcctImap::makeConnection()
{
if ( mSlaveConnectionError )
{
mErrorTimer.start(100, true); // Clear error flag
return Error;
}
return ImapAccountBase::makeConnection();
}
void KMAcctImap::slotResetConnectionError()
{
mSlaveConnectionError = false;
kdDebug(5006) << k_funcinfo << endl;
}
void KMAcctImap::slotFolderSelected( KMFolderImap* folder, bool )
{
folder->setSelected( false );
disconnect( folder, TQT_SIGNAL( folderComplete( KMFolderImap*, bool ) ),
this, TQT_SLOT( slotFolderSelected( KMFolderImap*, bool) ) );
postProcessNewMail( static_cast<KMFolder*>(folder->folder()) );
folder->close( "acctimap" );
}
void KMAcctImap::execFilters(TQ_UINT32 serNum)
{
if ( !kmkernel->filterMgr()->atLeastOneFilterAppliesTo( id() ) ) return;
TQValueListIterator<TQ_UINT32> findIt = mFilterSerNums.find( serNum );
if ( findIt != mFilterSerNums.end() )
return;
mFilterSerNums.append( serNum );
mFilterSerNumsToSave.insert( TQString( "%1" ).arg( serNum ), (const int *)1 );
}
int KMAcctImap::slotFilterMsg( KMMessage *msg )
{
if ( !msg ) {
// messageRetrieved(0) is always possible
return -1;
}
msg->setTransferInProgress(false);
TQ_UINT32 serNum = msg->getMsgSerNum();
if ( serNum )
mFilterSerNumsToSave.remove( TQString( "%1" ).arg( serNum ) );
int filterResult = kmkernel->filterMgr()->process(msg,
KMFilterMgr::Inbound,
true,
id() );
if (filterResult == 2) {
// something went horribly wrong (out of space?)
kmkernel->emergencyExit( i18n("Unable to process messages: " ) + TQString::fromLocal8Bit(strerror(errno)));
return 2;
}
if (msg->parent()) { // unGet this msg
int idx = -1;
KMFolder * p = 0;
KMMsgDict::instance()->getLocation( msg, &p, &idx );
assert( p == msg->parent() ); assert( idx >= 0 );
p->unGetMsg( idx );
}
return filterResult;
}
#include "kmacctimap.moc"
|