00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "./accountlist.h"
00019
00020 AccountList::AccountList( QObject* parent ) : QObject( parent )
00021 {
00022 init();
00023
00024 }
00025
00026 AccountList::~AccountList(){}
00027
00028 Account* AccountList::addAccount( const QString& name )
00029 {
00030
00031 Account* acc = new Account( name, this, this );
00032
00033
00034 connect( acc, SIGNAL( sigRefreshReady( QString ) ), this, SLOT( slotCheckRefreshState( QString ) ) );
00035 connect( acc, SIGNAL( sigMessageWindowOpened() ), this, SLOT( slotMessageWindowOpened() ) );
00036 connect( acc, SIGNAL( sigMessageWindowClosed() ), this, SLOT( slotMessageWindowClosed() ) );
00037 connect( acc, SIGNAL( sigDeleteReady(QString) ), this, SLOT( slotCheckDeletionState(QString) ) );
00038 connect( acc, SIGNAL( sigShowBodiesReady(QString) ), this, SLOT( slotCheckShowBodiesState(QString) ) );
00039
00040
00041 accounts.append( acc );
00042
00043 return acc;
00044 }
00045
00046 void AccountList::print() const
00047 {
00048
00049 QListIterator<QPointer<Account> > iter( accounts );
00050 while( iter.hasNext() )
00051 {
00052 Account* acc = iter.next();
00053 acc->print();
00054 cout << "**********************" << endl;
00055 }
00056 }
00057
00058 int AccountList::numberAccounts() const
00059 {
00060 return accounts.size();
00061 }
00062
00063 Account* AccountList::getAccount( int index ) const
00064 {
00065
00066 if( index > accounts.size() ) return NULL;
00067
00068
00069 return accounts.at( index );
00070 }
00071
00072 void AccountList::loadSetup()
00073 {
00074
00075 KConfigGroup* confAccounts = new KConfigGroup( KGlobal::config(), CONFIG_GROUP_ACCOUNTS );
00076 QStringList accountsConfig = confAccounts->readEntry( CONFIG_ENTRY_ACCOUNTS_LIST, QStringList() );
00077
00078
00079
00080 QList<QPointer<Account> >::iterator iter;
00081 iter = accounts.begin();
00082 while( iter != accounts.end() )
00083 {
00084 QPointer<Account> accDel = *iter;
00085 QString accName = accDel->getName();
00086
00087 if( !accountsConfig.contains( accName ) )
00088 {
00089 iter = accounts.erase( iter );
00090 delete accDel;
00091 }
00092 else
00093 {
00094 iter++;
00095 }
00096 }
00097
00098
00099 QStringListIterator iterAccountsConfig( accountsConfig );
00100 while( iterAccountsConfig.hasNext() )
00101 {
00102 QString accName = iterAccountsConfig.next();
00103
00104 if( !hasAccount( accName ) )
00105 {
00106 addAccount( accName );
00107 }
00108 }
00109
00110
00111 iter = accounts.begin();
00112 while( iter != accounts.end() )
00113 {
00114 Account* acc = *iter;
00115
00116 acc->load();
00117
00118 iter++;
00119 }
00120
00121
00122 KConfigGroup* confGeneral = new KConfigGroup( KGlobal::config(), CONFIG_GROUP_GENERAL );
00123 keepMailsNew = confGeneral->readEntry( CONFIG_ENTRY_KEEP_NEW, DEFAULT_KEEP_NEW );
00124
00125 delete confAccounts;
00126 delete confGeneral;
00127
00128
00129 refreshFilterSetup();
00130 }
00131
00132 bool AccountList::hasAccount( QString accountName ) const
00133 {
00134 QListIterator<QPointer< Account> > iter( accounts );
00135 while( iter.hasNext() )
00136 {
00137 Account* acc = iter.next();
00138 if( acc->getName() == accountName ) return true;
00139 }
00140 return false;
00141 }
00142
00143 void AccountList::refreshMailLists( FilterLog* log )
00144 {
00145
00146 if( accounts.count() == 0 )
00147 {
00148 emit sigRefreshReady();
00149 return;
00150 }
00151
00152
00153
00154 accountRefreshMap.clear();
00155
00156
00157
00158
00159
00160 QList<QPointer<Account> >::const_iterator iter;
00161 for( iter = accounts.constBegin(); iter != accounts.constEnd(); ++iter )
00162 {
00163 QPointer<Account> account = *iter;
00164
00165
00166 accountRefreshMap.insert( account->getName(), true );
00167 }
00168
00169
00170 for( iter = accounts.constBegin(); iter != accounts.constEnd(); ++iter )
00171 {
00172 Account* account = *iter;
00173
00174 account->refreshMailList( log );
00175 }
00176 }
00177
00178 void AccountList::slotCheckRefreshState( QString account )
00179 {
00180 bool accountRefreshing = false;
00181 AccountTaskMap_Type::Iterator it;
00182
00183
00184 accountRefreshMap[ account ] = false;
00185
00186
00187
00188 for ( it = accountRefreshMap.begin(); it != accountRefreshMap.end(); ++it )
00189 {
00190
00191 if( *it == true )
00192 accountRefreshing = true;
00193 }
00194
00195
00196 if( !accountRefreshing )
00197 {
00198 emit sigRefreshReady();
00199 }
00200
00201 }
00202
00203 void AccountList::init()
00204 {
00205
00206 ctrOpenMessageWindows = 0;
00207 }
00208
00209 void AccountList::slotMessageWindowOpened( )
00210 {
00211
00212 ctrOpenMessageWindows++;
00213
00214
00215
00216
00217 if( ctrOpenMessageWindows == 1 )
00218 emit sigMessageWindowOpened();
00219 }
00220
00221 void AccountList::slotMessageWindowClosed( )
00222 {
00223
00224 ctrOpenMessageWindows--;
00225 if( ctrOpenMessageWindows < 0 )
00226 ctrOpenMessageWindows = 0;
00227
00228
00229
00230 if( ctrOpenMessageWindows == 0 )
00231 emit sigAllMessageWindowsClosed();
00232 }
00233
00234 bool AccountList::keepNew( )
00235 {
00236 return keepMailsNew;
00237 }
00238
00239 int AccountList::getNumberMails( ) const
00240 {
00241 QListIterator<QPointer<Account> > it( accounts );
00242 int number = 0;
00243
00244
00245 while( it.hasNext() )
00246 {
00247
00248 QPointer<Account> account = it.next();
00249
00250
00251 number += account->getNumberMails();
00252
00253 }
00254
00255 return number;
00256 }
00257
00258 void AccountList::deleteMails()
00259 {
00260
00261
00262 QListIterator<QPointer<Account> > itAcc( accounts );
00263
00264
00265
00266 accountDeletionMap.clear();
00267
00268
00269
00270
00271
00272 while( itAcc.hasNext() )
00273 {
00274
00275 QPointer<Account> account = itAcc.next();
00276
00277
00278 accountDeletionMap.insert( account->getName(), true );
00279
00280 }
00281
00282
00283 itAcc.toFront();
00284 while( itAcc.hasNext() )
00285 {
00286
00287 QPointer<Account> account = itAcc.next();
00288
00289 account->deleteMails();
00290 }
00291
00292
00293 }
00294
00295 void AccountList::slotCheckDeletionState( QString account )
00296 {
00297 bool accountDeleting = false;
00298 AccountTaskMap_Type::Iterator it;
00299
00300
00301 accountDeletionMap[ account ] = false;
00302
00303
00304
00305 for ( it = accountDeletionMap.begin(); it != accountDeletionMap.end(); ++it )
00306 {
00307 if( *it == true )
00308 accountDeleting = true;
00309 }
00310
00311
00312 if( !accountDeleting )
00313 emit sigDeleteReady();
00314 }
00315
00316 int AccountList::numberDeletedMailsLastRefresh( )
00317 {
00318 QListIterator<QPointer<Account> > it( accounts );
00319 int number = 0;
00320
00321
00322 while( it.hasNext() )
00323 {
00324 QPointer<Account> account = it.next();
00325
00326 number += account->numberDeletedMailsLastRefresh();
00327 }
00328
00329 return number;
00330 }
00331
00332 int AccountList::numberDeletedMailsStart( )
00333 {
00334 QListIterator<QPointer<Account> > it( accounts );
00335 int number = 0;
00336
00337
00338 while( it.hasNext() )
00339 {
00340 QPointer<Account> account = it.next();
00341
00342
00343 number += account->numberDeletedMailsStart();
00344 }
00345
00346
00347 return number;
00348 }
00349
00350 int AccountList::numberMovedMailsLastRefresh( )
00351 {
00352 QListIterator<QPointer<Account> > it( accounts );
00353 int number = 0;
00354
00355
00356 while( it.hasNext() )
00357 {
00358 QPointer<Account> account = it.next();
00359
00360 number += account->numberMovedMailsLastRefresh();
00361 }
00362
00363 return number;
00364 }
00365
00366 int AccountList::numberMovedMailsStart( )
00367 {
00368 QListIterator<QPointer<Account> > it( accounts );
00369 int number = 0;
00370
00371
00372 while( it.hasNext() )
00373 {
00374 QPointer<Account> account = it.next();
00375
00376 number += account->numberMovedMailsStart();
00377 }
00378
00379 return number;
00380 }
00381
00382 int AccountList::numberIgnoredMails( )
00383 {
00384 QListIterator<QPointer<Account> > it( accounts );
00385 int number = 0;
00386
00387
00388 while( it.hasNext() )
00389 {
00390 QPointer<Account> account = it.next();
00391
00392 number += account->numberIgnoredMails();
00393 }
00394
00395 return number;
00396 }
00397
00398 void AccountList::refreshFilterSetup( )
00399 {
00400 QListIterator<QPointer<Account> > it( accounts );
00401
00402
00403 while( it.hasNext() )
00404 {
00405 QPointer<Account> account = it.next();
00406
00407
00408 account->reloadFilterSettings();
00409 }
00410
00411 }
00412
00413
00414 void AccountList::saveOptions ()
00415 {
00416
00417 QDomDocument doc( "KShowmail" );
00418
00419
00420 QDomElement root = doc.createElement( ROOT_ELEMENT );
00421
00422
00423
00424
00425 int i = 0;
00426 QListIterator<QPointer<Account> > it( accounts );
00427
00428
00429 while( it.hasNext() )
00430 {
00431 QPointer<Account> account = it.next();
00432
00433
00434 QDomElement accElem = doc.createElement( QString( ACCOUNT_ELEMENT ) + QString( "%1" ).arg( i++ ) );
00435 account->saveOptions( doc, accElem );
00436 root.appendChild( accElem );
00437
00438 }
00439
00440
00441 doc.appendChild( root );
00442
00443
00444 QString cachefilename = KStandardDirs::locateLocal( "appdata", MAIL_FILE );
00445 QFile file( cachefilename );
00446
00447 if( file.error() != QFile::NoError )
00448 {
00449 kdError() << "Couldn't save mail cache. " << file.errorString() << endl;
00450 return;
00451 }
00452
00453
00454 if( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
00455 {
00456 kdError() << "Couldn't save mail cache. " << file.errorString() << endl;
00457 return;
00458 }
00459
00460
00461 QTextStream stream( &file );
00462 doc.save( stream, 2 );
00463 stream.flush();
00464
00465
00466 file.close();
00467
00468 }
00469
00470 void AccountList::showMails()
00471 {
00472
00473
00474 QListIterator<QPointer<Account> > itAcc( accounts );
00475
00476
00477
00478 AccountShowBodiesMap.clear();
00479
00480
00481
00482
00483
00484 while( itAcc.hasNext() )
00485 {
00486
00487 QPointer<Account> account = itAcc.next();
00488
00489
00490 AccountShowBodiesMap.insert( account->getName(), true );
00491
00492 }
00493
00494
00495 itAcc.toFront();
00496 while( itAcc.hasNext() )
00497 {
00498
00499 QPointer<Account> account = itAcc.next();
00500
00501 account->showMails();
00502 }
00503
00504
00505 }
00506
00507 void AccountList::slotCheckShowBodiesState( QString account )
00508 {
00509 bool accountDownloading = false;
00510 AccountTaskMap_Type::Iterator it;
00511
00512
00513 AccountShowBodiesMap[ account ] = false;
00514
00515
00516
00517 for ( it = AccountShowBodiesMap.begin(); it != AccountShowBodiesMap.end(); ++it )
00518 {
00519 if( *it == true )
00520 accountDownloading = true;
00521 }
00522
00523
00524
00525 if( !accountDownloading )
00526 {
00527 emit sigShowBodiesReady();
00528 ctrOpenMessageWindows = 0;
00529 }
00530 }
00531
00532 int AccountList::getNumberNewMails( )
00533 {
00534 QListIterator<QPointer<Account> > it( accounts );
00535 int number = 0;
00536
00537
00538 while( it.hasNext() )
00539 {
00540 QPointer<Account> account = it.next();
00541
00542 if( account->isActive() )
00543 number += account->getNumberNewMails();
00544
00545 }
00546
00547 return number;
00548 }
00549
00550 void AccountList::readStoredMails( )
00551 {
00552
00553 QString MailFileName = KStandardDirs::locateLocal( "appdata", MAIL_FILE );
00554 QFile file( MailFileName );
00555 bool fileOpen = file.open( QFile::ReadOnly );
00556
00557
00558 if( !fileOpen )
00559 {
00560 kdError() << "AccountList::readStoredMails: File " << MailFileName << " could not be opened." << endl;
00561 return;
00562 }
00563
00564
00565 QDomDocument doc( MAIL_FILE_DOCTYPE );
00566 QString* errorMsg = new QString();
00567
00568 bool success = doc.setContent( &file );
00569 if( !success )
00570 {
00571 kdError() << "AccountList::readStoredMails: Invalid content in " << MAIL_FILE << ". " << *errorMsg << endl;
00572 }
00573
00574
00575 QDomElement accs = doc.namedItem ( ROOT_ELEMENT ).toElement();
00576
00577
00578 QDomNode accNode = accs.firstChild();
00579
00580
00581 while( !accNode.isNull() )
00582 {
00583
00584 QDomElement accElem = accNode.toElement();
00585
00586
00587 QString accName = accElem.attribute( ATTRIBUTE_ACCOUNT_NAME );
00588
00589
00590 Account* account = getAccount( accName );
00591
00592 if( account != NULL ) {
00593
00594
00595 account->readStoredMails( accElem );
00596 }
00597
00598
00599 accNode = accNode.nextSibling();
00600 }
00601
00602
00603 file.close();
00604 }
00605
00606 Account* AccountList::getAccount( QString name ) const {
00607
00608 QListIterator<QPointer<Account> > it( accounts );
00609
00610 while( it.hasNext() )
00611 {
00612 QPointer<Account> account = it.next();
00613
00614 if( account->getName() == name )
00615 return account;
00616
00617 }
00618
00619 return NULL;
00620 }
00621
00622 QList<QPointer<Account> > AccountList::getAllAccounts() const
00623 {
00624
00625 QList<QPointer<Account> > list;
00626
00627 list.append( accounts );
00628
00629 return list;
00630 }
00631
00632 QList<Mail> AccountList::getAllMails() const
00633 {
00634 QList<Mail> listMails;
00635
00636 QListIterator<QPointer<Account> > itAcc( accounts );
00637 while( itAcc.hasNext() ) {
00638
00639 QPointer<Account> acc = itAcc.next();
00640
00641 listMails.append( acc->getAllMails() );
00642 }
00643
00644 return listMails;
00645 }
00646
00647 void AccountList::cancelTasks()
00648 {
00649 QListIterator<QPointer<Account> > it( accounts );
00650
00651 while( it.hasNext() )
00652 {
00653 QPointer<Account> account = it.next();
00654 account->cancelTask();
00655 }
00656 }
00657
00658 QList< AccountViewItem > AccountList::getAllAccountViewItems() const
00659 {
00660 QList<AccountViewItem> listItems;
00661
00662 QListIterator<QPointer<Account> > itAcc( accounts );
00663 while( itAcc.hasNext() ) {
00664
00665 QPointer<Account> acc = itAcc.next();
00666
00667 listItems.append( acc->getViewItem() );
00668 }
00669
00670 return listItems;
00671
00672 }
00673