00001
00032 #ifdef HAVE_CONFIG_H
00033 #include <config.h>
00034 #endif
00035
00036 #include "kmacctcachedimap.h"
00037 using KMail::SieveConfig;
00038
00039 #include "kmfoldertree.h"
00040 #include "kmfoldermgr.h"
00041 #include "kmfiltermgr.h"
00042 #include "kmfoldercachedimap.h"
00043 #include "kmmainwin.h"
00044 #include "accountmanager.h"
00045 using KMail::AccountManager;
00046 #include "progressmanager.h"
00047
00048 #include <kio/passdlg.h>
00049 #include <kio/scheduler.h>
00050 #include <kio/slave.h>
00051 #include <kdebug.h>
00052 #include <kstandarddirs.h>
00053 #include <kapplication.h>
00054 #include <kconfig.h>
00055
00056 #include <qstylesheet.h>
00057
00058 KMAcctCachedImap::KMAcctCachedImap( AccountManager* aOwner,
00059 const QString& aAccountName, uint id )
00060 : KMail::ImapAccountBase( aOwner, aAccountName, id ), mFolder( 0 ),
00061 mAnnotationCheckPassed(false),
00062 mGroupwareType( GroupwareKolab ),
00063 mSentCustomLoginCommand(false)
00064 {
00065
00066 mAutoExpunge = false;
00067 }
00068
00069
00070
00071 KMAcctCachedImap::~KMAcctCachedImap()
00072 {
00073 killAllJobsInternal( true );
00074 }
00075
00076
00077
00078 QString KMAcctCachedImap::type() const
00079 {
00080 return "cachedimap";
00081 }
00082
00083 void KMAcctCachedImap::init() {
00084 ImapAccountBase::init();
00085 }
00086
00087
00088 void KMAcctCachedImap::pseudoAssign( const KMAccount * a ) {
00089 killAllJobs( true );
00090 if (mFolder)
00091 {
00092 mFolder->setContentState(KMFolderCachedImap::imapNoInformation);
00093 mFolder->setSubfolderState(KMFolderCachedImap::imapNoInformation);
00094 }
00095 ImapAccountBase::pseudoAssign( a );
00096 }
00097
00098
00099 void KMAcctCachedImap::setImapFolder(KMFolderCachedImap *aFolder)
00100 {
00101 mFolder = aFolder;
00102 mFolder->setImapPath( "/" );
00103 mFolder->setAccount( this );
00104 }
00105
00106
00107
00108 void KMAcctCachedImap::setAutoExpunge( bool )
00109 {
00110
00111 mAutoExpunge = false;
00112 }
00113
00114
00115 void KMAcctCachedImap::killAllJobs( bool disconnectSlave )
00116 {
00117
00118 QValueList<KMFolderCachedImap*> folderList = killAllJobsInternal( disconnectSlave );
00119 for( QValueList<KMFolderCachedImap*>::Iterator it = folderList.begin(); it != folderList.end(); ++it ) {
00120 KMFolderCachedImap *fld = *it;
00121 fld->resetSyncState();
00122 fld->setContentState(KMFolderCachedImap::imapNoInformation);
00123 fld->setSubfolderState(KMFolderCachedImap::imapNoInformation);
00124 fld->sendFolderComplete(false);
00125 }
00126 }
00127
00128
00129
00130 QValueList<KMFolderCachedImap*> KMAcctCachedImap::killAllJobsInternal( bool disconnectSlave )
00131 {
00132
00133
00134 QValueList<KMFolderCachedImap*> folderList;
00135 QMap<KIO::Job*, jobData>::Iterator it = mapJobData.begin();
00136 for (; it != mapJobData.end(); ++it) {
00137 if ((*it).parent)
00138 folderList << static_cast<KMFolderCachedImap*>((*it).parent->storage());
00139
00140 if ( !it.key()->error() && mSlave ) {
00141 it.key()->kill();
00142 mSlave = 0;
00143 }
00144 }
00145 mapJobData.clear();
00146
00147
00148 for( QPtrListIterator<CachedImapJob> it( mJobList ); it.current(); ++it )
00149 it.current()->setPassiveDestructor( true );
00150 KMAccount::deleteFolderJobs();
00151
00152 if ( disconnectSlave && mSlave ) {
00153 KIO::Scheduler::disconnectSlave( mSlave );
00154 mSlave = 0;
00155 }
00156 return folderList;
00157 }
00158
00159
00160 void KMAcctCachedImap::cancelMailCheck()
00161 {
00162
00163 QValueList<KMFolderCachedImap*> folderList;
00164 QMap<KIO::Job*, jobData>::Iterator it = mapJobData.begin();
00165 for (; it != mapJobData.end(); ++it) {
00166 if ( (*it).cancellable && (*it).parent )
00167 folderList << static_cast<KMFolderCachedImap*>((*it).parent->storage());
00168 }
00169
00170 ImapAccountBase::cancelMailCheck();
00171
00172
00173 for( QValueList<KMFolderCachedImap*>::Iterator it = folderList.begin(); it != folderList.end(); ++it ) {
00174 KMFolderCachedImap *fld = *it;
00175 fld->resetSyncState();
00176 fld->setContentState(KMFolderCachedImap::imapNoInformation);
00177 fld->setSubfolderState(KMFolderCachedImap::imapNoInformation);
00178 fld->sendFolderComplete(false);
00179 }
00180 }
00181
00182
00183 void KMAcctCachedImap::killJobsForItem(KMFolderTreeItem * fti)
00184 {
00185 QMap<KIO::Job *, jobData>::Iterator it = mapJobData.begin();
00186 while (it != mapJobData.end())
00187 {
00188 if (it.data().parent == fti->folder())
00189 {
00190 killAllJobs();
00191 break;
00192 }
00193 else ++it;
00194 }
00195 }
00196
00197
00198 void KMAcctCachedImap::slotCheckQueuedFolders()
00199 {
00200 mMailCheckFolders.clear();
00201 mMailCheckFolders.append( mFoldersQueuedForChecking.front() );
00202 mFoldersQueuedForChecking.pop_front();
00203 if ( mFoldersQueuedForChecking.isEmpty() )
00204 disconnect( this, SIGNAL( finishedCheck( bool, CheckStatus ) ),
00205 this, SLOT( slotCheckQueuedFolders() ) );
00206
00207 kmkernel->acctMgr()->singleCheckMail(this, true);
00208 mMailCheckFolders.clear();
00209 }
00210
00211 void KMAcctCachedImap::processNewMail( bool )
00212 {
00213 assert( mFolder );
00214
00215 if ( mMailCheckFolders.isEmpty() )
00216 processNewMail( mFolder, true );
00217 else {
00218 KMFolder* f = mMailCheckFolders.front();
00219 mMailCheckFolders.pop_front();
00220 processNewMail( static_cast<KMFolderCachedImap *>( f->storage() ), false );
00221 }
00222 }
00223
00224 void KMAcctCachedImap::processNewMail( KMFolderCachedImap* folder,
00225 bool recurse )
00226 {
00227 assert( folder );
00228
00229
00230 mAutoExpunge = false;
00231 mCountLastUnread = 0;
00232 mUnreadBeforeCheck.clear();
00233
00234 mNoopTimer.stop();
00235
00236
00237 if ( folder == mFolder ) {
00238 QStringList nsToList = namespaces()[PersonalNS];
00239 QStringList otherNSToCheck = namespaces()[OtherUsersNS];
00240 otherNSToCheck += namespaces()[SharedNS];
00241 for ( QStringList::Iterator it = otherNSToCheck.begin();
00242 it != otherNSToCheck.end(); ++it ) {
00243 if ( (*it).isEmpty() ) {
00244
00245
00246 nsToList += *it;
00247 }
00248 }
00249 folder->setNamespacesToList( nsToList );
00250 }
00251
00252 Q_ASSERT( !mMailCheckProgressItem );
00253 mMailCheckProgressItem = KPIM::ProgressManager::createProgressItem(
00254 "MailCheck" + QString::number( id() ),
00255 QStyleSheet::escape( folder->label() ),
00256 QString::null,
00257 true,
00258 useSSL() || useTLS() );
00259 connect( mMailCheckProgressItem, SIGNAL( progressItemCanceled( KPIM::ProgressItem* ) ),
00260 this, SLOT( slotProgressItemCanceled( KPIM::ProgressItem* ) ) );
00261
00262 folder->setAccount(this);
00263 connect(folder, SIGNAL(folderComplete(KMFolderCachedImap*, bool)),
00264 this, SLOT(postProcessNewMail(KMFolderCachedImap*, bool)));
00265 folder->serverSync( recurse );
00266 }
00267
00268 void KMAcctCachedImap::postProcessNewMail( KMFolderCachedImap* folder, bool )
00269 {
00270 mNoopTimer.start( 60000 );
00271 disconnect(folder, SIGNAL(folderComplete(KMFolderCachedImap*, bool)),
00272 this, SLOT(postProcessNewMail(KMFolderCachedImap*, bool)));
00273 mMailCheckProgressItem->setComplete();
00274 mMailCheckProgressItem = 0;
00275
00276 if ( folder == mFolder ) {
00277
00278
00279
00280
00281
00282
00283 #if 0 // this opens a race: delete a folder during a sync (after the sync checked that folder), and it'll be forgotten...
00284 mDeletedFolders.clear();
00285 #endif
00286 mPreviouslyDeletedFolders.clear();
00287 }
00288
00289 KMail::ImapAccountBase::postProcessNewMail();
00290 }
00291
00292 void KMAcctCachedImap::addUnreadMsgCount( const KMFolderCachedImap *folder,
00293 int countUnread )
00294 {
00295 if ( folder->imapPath() != "/INBOX/" ) {
00296
00297
00298 const QString folderId = folder->folder()->idString();
00299 int newInFolder = countUnread;
00300 if ( mUnreadBeforeCheck.find( folderId ) != mUnreadBeforeCheck.end() )
00301 newInFolder -= mUnreadBeforeCheck[folderId];
00302 if ( newInFolder > 0 )
00303 addToNewInFolder( folderId, newInFolder );
00304 }
00305 mCountUnread += countUnread;
00306 }
00307
00308 void KMAcctCachedImap::addLastUnreadMsgCount( const KMFolderCachedImap *folder,
00309 int countLastUnread )
00310 {
00311 mUnreadBeforeCheck[folder->folder()->idString()] = countLastUnread;
00312 mCountLastUnread += countLastUnread;
00313 }
00314
00315
00316
00317
00318
00319
00320
00321 void KMAcctCachedImap::readConfig( KConfig & config ) {
00322 ImapAccountBase::readConfig( config );
00323
00324 mPreviouslyDeletedFolders = config.readListEntry( "deleted-folders" );
00325 mDeletedFolders.clear();
00326 const QStringList oldPaths = config.readListEntry( "renamed-folders-paths" );
00327 const QStringList newNames = config.readListEntry( "renamed-folders-names" );
00328 QStringList::const_iterator it = oldPaths.begin();
00329 QStringList::const_iterator nameit = newNames.begin();
00330 for( ; it != oldPaths.end() && nameit != newNames.end(); ++it, ++nameit ) {
00331 addRenamedFolder( *it, QString::null, *nameit );
00332 }
00333 mGroupwareType = (GroupwareType)config.readNumEntry( "groupwareType", GroupwareKolab );
00334 }
00335
00336 void KMAcctCachedImap::writeConfig( KConfig & config ) {
00337 ImapAccountBase::writeConfig( config );
00338 config.writeEntry( "deleted-folders", mDeletedFolders + mPreviouslyDeletedFolders );
00339 config.writeEntry( "renamed-folders-paths", mRenamedFolders.keys() );
00340 const QValueList<RenamedFolder> values = mRenamedFolders.values();
00341 QStringList lstNames;
00342 QValueList<RenamedFolder>::const_iterator it = values.begin();
00343 for ( ; it != values.end() ; ++it )
00344 lstNames.append( (*it).mNewName );
00345 config.writeEntry( "renamed-folders-names", lstNames );
00346 config.writeEntry( "groupwareType", mGroupwareType );
00347 }
00348
00349 void KMAcctCachedImap::invalidateIMAPFolders()
00350 {
00351 invalidateIMAPFolders( mFolder );
00352 }
00353
00354 void KMAcctCachedImap::invalidateIMAPFolders( KMFolderCachedImap* folder )
00355 {
00356 if( !folder || !folder->folder() )
00357 return;
00358
00359 folder->setAccount(this);
00360
00361 QStringList strList;
00362 QValueList<QGuardedPtr<KMFolder> > folderList;
00363 kmkernel->dimapFolderMgr()->createFolderList( &strList, &folderList,
00364 folder->folder()->child(), QString::null,
00365 false );
00366 QValueList<QGuardedPtr<KMFolder> >::Iterator it;
00367 mCountLastUnread = 0;
00368 mUnreadBeforeCheck.clear();
00369
00370 for( it = folderList.begin(); it != folderList.end(); ++it ) {
00371 KMFolder *f = *it;
00372 if( f && f->folderType() == KMFolderTypeCachedImap ) {
00373 KMFolderCachedImap *cfolder = static_cast<KMFolderCachedImap*>(f->storage());
00374
00375 cfolder->setUidValidity("INVALID");
00376 cfolder->writeUidCache();
00377 processNewMailSingleFolder( f );
00378 }
00379 }
00380 folder->setUidValidity("INVALID");
00381 folder->writeUidCache();
00382
00383 processNewMailSingleFolder( folder->folder() );
00384 }
00385
00386
00387 void KMAcctCachedImap::addDeletedFolder( KMFolder* folder )
00388 {
00389 if ( !folder || folder->folderType() != KMFolderTypeCachedImap )
00390 return;
00391 KMFolderCachedImap* storage = static_cast<KMFolderCachedImap*>(folder->storage());
00392 addDeletedFolder( storage->imapPath() );
00393 kdDebug(5006) << k_funcinfo << storage->imapPath() << endl;
00394
00395
00396 if( folder->child() ) {
00397 KMFolderNode *node = folder->child()->first();
00398 while( node ) {
00399 if( !node->isDir() ) {
00400 addDeletedFolder( static_cast<KMFolder*>( node ) );
00401 }
00402 node = folder->child()->next();
00403 }
00404 }
00405 }
00406
00407 void KMAcctCachedImap::addDeletedFolder( const QString& imapPath )
00408 {
00409 mDeletedFolders << imapPath;
00410 }
00411
00412 QStringList KMAcctCachedImap::deletedFolderPaths( const QString& subFolderPath ) const
00413 {
00414 QStringList lst;
00415 for ( QStringList::const_iterator it = mDeletedFolders.begin(); it != mDeletedFolders.end(); ++it ) {
00416 if ( (*it).startsWith( subFolderPath ) )
00417
00418 lst.prepend( *it );
00419 }
00420 for ( QStringList::const_iterator it = mPreviouslyDeletedFolders.begin(); it != mPreviouslyDeletedFolders.end(); ++it ) {
00421 if ( (*it).startsWith( subFolderPath ) )
00422 lst.prepend( *it );
00423 }
00424 kdDebug(5006) << "KMAcctCachedImap::deletedFolderPaths for " << subFolderPath << " returning: " << lst << endl;
00425 Q_ASSERT( !lst.isEmpty() );
00426 return lst;
00427 }
00428
00429 bool KMAcctCachedImap::isDeletedFolder( const QString& subFolderPath ) const
00430 {
00431 return mDeletedFolders.find( subFolderPath ) != mDeletedFolders.end();
00432 }
00433
00434 bool KMAcctCachedImap::isPreviouslyDeletedFolder( const QString& subFolderPath ) const
00435 {
00436 return mPreviouslyDeletedFolders.find( subFolderPath ) != mPreviouslyDeletedFolders.end();
00437 }
00438
00439 void KMAcctCachedImap::removeDeletedFolder( const QString& subFolderPath )
00440 {
00441 mDeletedFolders.remove( subFolderPath );
00442 mPreviouslyDeletedFolders.remove( subFolderPath );
00443 }
00444
00445 void KMAcctCachedImap::addRenamedFolder( const QString& subFolderPath, const QString& oldLabel, const QString& newName )
00446 {
00447 mRenamedFolders.insert( subFolderPath, RenamedFolder( oldLabel, newName ) );
00448 }
00449
00450 void KMAcctCachedImap::removeRenamedFolder( const QString& subFolderPath )
00451 {
00452 mRenamedFolders.remove( subFolderPath );
00453 }
00454
00455 void KMAcctCachedImap::slotProgressItemCanceled( ProgressItem* )
00456 {
00457 bool abortConnection = !mSlaveConnected;
00458 killAllJobs( abortConnection );
00459 if ( abortConnection ) {
00460
00461 emit connectionResult( KIO::ERR_USER_CANCELED, QString::null );
00462 }
00463 }
00464
00465 FolderStorage* const KMAcctCachedImap::rootFolder() const
00466 {
00467 return mFolder;
00468 }
00469
00470
00471 QString KMAcctCachedImap::renamedFolder( const QString& imapPath ) const
00472 {
00473 QMap<QString, RenamedFolder>::ConstIterator renit = mRenamedFolders.find( imapPath );
00474 if ( renit != mRenamedFolders.end() )
00475 return (*renit).mNewName;
00476 return QString::null;
00477 }
00478
00479 #include "kmacctcachedimap.moc"