00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "maillist.h"
00019
00020 MailList::MailList( QPointer<Account> account, QObject* parent ) : QObject( parent )
00021 {
00022 acc = account;
00023 }
00024
00025 MailList::~MailList(){
00026
00027
00028 QListIterator<Mail*> iter( mails );
00029 while( iter.hasNext() )
00030 {
00031 Mail* mail = iter.next();
00032 delete mail;
00033 }
00034
00035 }
00036
00037 Mail* MailList::addMail( long number, const QString& unid, bool isNew )
00038 {
00039
00040 Mail* mail = new Mail( number, unid, isNew, acc, this );
00041
00042
00043 mails.append( mail );
00044
00045 return mail;
00046 }
00047
00048 void MailList::print() const
00049 {
00050 QListIterator<Mail*> iter( mails );
00051 while( iter.hasNext() )
00052 {
00053 Mail* mail = iter.next();
00054 mail->print();
00055 cout << "------------------" << endl;
00056 }
00057 }
00058
00059 bool MailList::hasMail(QString uid)
00060 {
00061 QListIterator<Mail*> it( mails );
00062 bool found = false;
00063
00064 while( it.hasNext() && !found )
00065 {
00066 Mail* mail = it.next();
00067
00068
00069 if( mail->getUNID() == uid )
00070 {
00071 found = true;
00072 }
00073 }
00074 return found;
00075
00076 }
00077
00078 bool MailList::isNew(QString uid) const
00079 {
00080 QListIterator<Mail*> it( mails );
00081 bool found = false;
00082 bool newMail = false;
00083
00084 while( it.hasNext() && !found )
00085 {
00086 Mail* mail = it.next();
00087
00088 kdDebug() << "compare: " << uid << " " << mail->getUNID() << endl;
00089
00090 if( mail->getUNID() == uid )
00091 {
00092 found = true;
00093 newMail = mail->isNew();
00094 }
00095 }
00096
00097 return newMail;
00098 }
00099
00100 void MailList::setSize( long number, long size )
00101 {
00102 QListIterator<Mail*> it( mails );
00103 bool found = false;
00104
00105
00106 while( it.hasNext() && !found )
00107 {
00108 Mail* mail = it.next();
00109
00110
00111 if( mail->getNumber() == number )
00112 {
00113 mail->setSize( size );
00114 found = true;
00115 }
00116 }
00117 }
00118
00119 Types::MailNumberList_Type MailList::getNewMails()
00120 {
00121 MailNumberList_Type numberList;
00122
00123 QListIterator<Mail*> it( mails );
00124
00125
00126 while( it.hasNext() )
00127 {
00128 Mail* mail = it.next();
00129
00130
00131 if( mail->isNew() )
00132 numberList.append( mail->getNumber() );
00133 }
00134
00135 return numberList;
00136 }
00137
00138 void MailList::setHeader(int number, QStringList header)
00139 {
00140 QListIterator<Mail*> it( mails );
00141 bool found = false;
00142
00143
00144 while( it.hasNext() && !found )
00145 {
00146 Mail* mail = it.next();
00147
00148
00149 if( mail->getNumber() == number )
00150 {
00151 mail->setHeader( header );
00152 found = true;
00153 }
00154 }
00155
00156 }
00157
00158 QStringList MailList::getUIDsOfOldMails( )
00159 {
00160 QStringList list;
00161 QListIterator<Mail*> it( mails );
00162
00163
00164 while( it.hasNext() )
00165 {
00166
00167 Mail* mail = it.next();
00168
00169
00170 if( !mail->isNew() )
00171 list.append( mail->getUNID() );
00172 }
00173
00174 return list;
00175 }
00176
00177 QStringList MailList::getHeaderOf( QString unid ) throw( CorruptDataException )
00178 {
00179 QListIterator<Mail*> it( mails );
00180
00181
00182 while( it.hasNext() )
00183 {
00184
00185 Mail* mail = it.next();
00186
00187
00188 if( mail->getUNID() == unid )
00189 {
00190 return mail->getHeader();
00191 }
00192 }
00193
00194
00195 throw CorruptDataException( "No mail with unid " + unid + " found in mail list" );
00196 }
00197
00198 void MailList::setHeader( QString unid, QStringList header )
00199 {
00200 QListIterator<Mail*> it( mails );
00201
00202 while( it.hasNext() )
00203 {
00204
00205 Mail* mail = it.next();
00206
00207
00208 if( mail->getUNID() == unid )
00209 {
00210 mail->setHeader( header );
00211 return;
00212 }
00213 }
00214 }
00215
00216 int MailList::getNumberMails() const
00217 {
00218 return mails.count();
00219 }
00220
00221 QPointer<Account> MailList::getAccount() const
00222 {
00223 return acc;
00224 }
00225
00226 void MailList::applyHeaderFilter( HeaderFilter * filter, QString account, MailNumberList_Type& deleteList, MailToDownloadMap_Type& downloadList, int& nmbIgnoredMails, FilterLog* log )
00227 {
00228
00229 MailNumberList_Type mailsToIgnore;
00230
00231
00232 QListIterator<Mail*> it( mails );
00233 while( it.hasNext() )
00234 {
00235
00236 Mail* pElem = it.next();
00237
00238
00239 QString mailbox;
00240 FilterAction_Type action = pElem->applyHeaderFilter( filter, account, mailbox, log );
00241
00242
00243
00244 struct DownloadActionParams_Type params;
00245 switch( action )
00246 {
00247 case FActDelete : deleteList.append( pElem->getNumber() ); break;
00248 case FActMove : params.action = FActMove;
00249 params.mailbox = mailbox;
00250 downloadList.insert( pElem->getNumber(), params );
00251 break;
00252 case FActIgnore : mailsToIgnore.append( pElem->getNumber() ); break;
00253 case FActSpamcheck : params.action = FActSpamcheck;
00254 downloadList.insert( pElem->getNumber(), params );
00255 default : break;
00256 }
00257 }
00258
00259
00260 nmbIgnoredMails = mailsToIgnore.count();
00261 MailNumberList_Type::iterator iter;
00262 for ( iter = mailsToIgnore.begin(); iter != mailsToIgnore.end(); ++iter )
00263 removeMail( *iter );
00264
00265 }
00266
00267 void MailList::removeMail( int number )
00268 {
00269 QMutableListIterator<Mail*> it( mails );
00270
00271
00272 while( it.hasNext() )
00273 {
00274 Mail* mail = it.next();
00275
00276
00277 if( mail->getNumber() == number )
00278 it.remove();
00279 }
00280 }
00281
00282 void MailList::saveMails( QDomDocument& doc, QDomElement& parent )
00283 {
00284
00285 QListIterator<Mail*> it( mails );
00286 while( it.hasNext() )
00287 {
00288 Mail* mail = it.next();
00289 mail->save( doc, parent );
00290 }
00291 }
00292
00293
00294 QString MailList::getSenderOf( int number ) const
00295 {
00296 QListIterator<Mail*> it( mails );
00297 bool found = false;
00298 QString sender;
00299
00300
00301 while( it.hasNext() && !found )
00302 {
00303 Mail* mail = it.next();
00304
00305
00306 if( mail->getNumber() == number )
00307 {
00308 sender = mail->getFrom();
00309 found = true;
00310 }
00311 }
00312 return sender;
00313 }
00314
00315 QString MailList::getDateOf( int number ) const
00316 {
00317 QListIterator<Mail*> it( mails );
00318 bool found = false;
00319 QString date;
00320
00321
00322 while( it.hasNext() && !found )
00323 {
00324 Mail* mail = it.next();
00325
00326
00327 if( mail->getNumber() == number )
00328 {
00329 date = mail->getDateTime().toString( KDateTime::LocalDate );
00330 found = true;
00331 }
00332 }
00333 return date;
00334
00335 }
00336
00337 QString MailList::getSizeOf( int number ) const
00338 {
00339 QListIterator<Mail*> it( mails );
00340 bool found = false;
00341 QString size;
00342
00343
00344 while( it.hasNext() && !found )
00345 {
00346 Mail* mail = it.next();
00347
00348
00349 if( mail->getNumber() == number )
00350 {
00351 size = mail->getSizeSuffix();
00352 found = true;
00353 }
00354 }
00355 return size;
00356
00357 }
00358
00359 QString MailList::getSubjectOf( int number ) const
00360 {
00361 QListIterator<Mail*> it( mails );
00362 bool found = false;
00363 QString subject;
00364
00365
00366 while( it.hasNext() && !found )
00367 {
00368 Mail* mail = it.next();
00369
00370
00371 if( mail->getNumber() == number )
00372 {
00373 subject = mail->getSubject();
00374 found = true;
00375 }
00376 }
00377 return subject;
00378
00379 }
00380
00381 QString MailList::getCharsetFromHeaderOf( int number ) const
00382 {
00383 QListIterator<Mail*> it( mails );
00384 bool found = false;
00385 QString charset;
00386
00387
00388 while( it.hasNext() && !found )
00389 {
00390 Mail* mail = it.next();
00391
00392
00393 if( mail->getNumber() == number )
00394 {
00395 charset = mail->getCharsetFromHeader();
00396 found = true;
00397 }
00398 }
00399 return charset;
00400
00401 }
00402
00403 QStringList MailList::decodeMailBody( const QStringList& body, int number, bool preferHTML ) const
00404 {
00405 QListIterator<Mail*> it( mails );
00406
00407
00408 while( it.hasNext() )
00409 {
00410
00411 Mail* mail = it.next();
00412
00413
00414 if( mail->getNumber() == number )
00415 {
00416 return mail->decodeMailBody( body, preferHTML );
00417 }
00418 }
00419
00420
00421 return QStringList();
00422
00423 }
00424
00425 void MailList::writeToMoveLog( FilterLog * log, int number, QString account, QString mailbox )
00426 {
00427 QListIterator<Mail*> it( mails );
00428 bool found = false;
00429
00430
00431 while( it.hasNext() && !found )
00432 {
00433 Mail* mail = it.next();
00434
00435
00436 if( mail->getNumber() == number )
00437 {
00438 mail->writeToMoveLog( log, account, mailbox );
00439 found = true;
00440 }
00441 }
00442 }
00443
00444 void MailList::setMarkAtNextViewRefresh( int number )
00445 {
00446 QListIterator<Mail*> it( mails );
00447 bool found = false;
00448
00449
00450 while( it.hasNext() && !found )
00451 {
00452 Mail* mail = it.next();
00453
00454
00455 if( mail->getNumber() == number )
00456 {
00457 mail->setMarkAtNextViewRefresh();
00458 found = true;
00459 }
00460 }
00461 }
00462
00463 void MailList::writeToDeleteLog( FilterLog * log, int number, QString account )
00464 {
00465 QListIterator<Mail*> it( mails );
00466 bool found = false;
00467
00468
00469 while( it.hasNext() && !found )
00470 {
00471 Mail* mail = it.next();
00472
00473
00474 if( mail->getNumber() == number )
00475 {
00476 mail->writeToDeleteLog( log, account );
00477 found = true;
00478 }
00479 }
00480 }
00481
00482 int MailList::getNumberNewMails( )
00483 {
00484 QListIterator<Mail*> it( mails );
00485 int number = 0;
00486
00487 while( it.hasNext() )
00488 {
00489 Mail* mail = it.next();
00490
00491
00492 if( mail->isNew() )
00493 number++;
00494 }
00495
00496 return number;
00497 }
00498
00499 long MailList::getTotalSize( )
00500 {
00501 QListIterator<Mail*> it( mails );
00502 long size = 0;
00503
00504 while( it.hasNext() )
00505 {
00506 Mail* mail = it.next();
00507
00508 size += mail->getSize();
00509 }
00510
00511 return size;
00512 }
00513
00514
00515 void MailList::readStoredMails( QDomElement& parent )
00516 {
00517
00518 mails.clear();
00519
00520
00521 QDomNode n = parent.firstChild();
00522
00523
00524 while( !n.isNull() )
00525 {
00526
00527 QDomElement e = n.toElement();
00528
00529
00530 int number = e.attribute( ATTRIBUTE_MAIL_NUMBER ).toInt();
00531 int size = e.attribute( ATTRIBUTE_MAIL_SIZE ).toInt();
00532 QString unid = e.attribute( ATTRIBUTE_MAIL_UID );
00533 QDomElement subelem = e.namedItem( ITEM_MAIL_HEADER ).toElement();
00534 QStringList header = subelem.text().split( HEADER_SEPARATOR );
00535
00536
00537 Mail* mail = addMail( number, unid, false );
00538
00539
00540 mail->setSize( size );
00541 mail->setHeader( header );
00542
00543
00544 n = n.nextSibling();
00545 }
00546 }
00547
00548 QList<Mail> MailList::getAllMails() const
00549 {
00550 QList<Mail> list;
00551 QListIterator<Mail*> it( mails );
00552
00553 while( it.hasNext() )
00554 {
00555 Mail* mail = it.next();
00556
00557 list.append( *mail );
00558 }
00559 return list;
00560 }
00561