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::slotCheckQueuedFolders()
00184 {
00185 mMailCheckFolders.clear();
00186 mMailCheckFolders.append( mFoldersQueuedForChecking.front() );
00187 mFoldersQueuedForChecking.pop_front();
00188 if ( mFoldersQueuedForChecking.isEmpty() )
00189 disconnect( this, SIGNAL( finishedCheck( bool, CheckStatus ) ),
00190 this, SLOT( slotCheckQueuedFolders() ) );
00191
00192 kmkernel->acctMgr()->singleCheckMail(this, true);
00193 mMailCheckFolders.clear();
00194 }
00195
00196 void KMAcctCachedImap::processNewMail( bool )
00197 {
00198 assert( mFolder );
00199
00200 if ( mMailCheckFolders.isEmpty() )
00201 processNewMail( mFolder, true );
00202 else {
00203 KMFolder* f = mMailCheckFolders.front();
00204 mMailCheckFolders.pop_front();
00205
00206
00207
00208 if ( f ) {
00209 processNewMail( static_cast<KMFolderCachedImap *>( f->storage() ), !checkingSingleFolder() );
00210 }
00211 }
00212 }
00213
00214 void KMAcctCachedImap::processNewMail( KMFolderCachedImap* folder,
00215 bool recurse )
00216 {
00217 assert( folder );
00218
00219
00220 mAutoExpunge = false;
00221 mCountLastUnread = 0;
00222 mUnreadBeforeCheck.clear();
00223
00224 mNoopTimer.stop();
00225
00226
00227 if ( folder == mFolder && !namespaces().isEmpty() ) {
00228 QStringList nsToList = namespaces()[PersonalNS];
00229 QStringList otherNSToCheck = namespaces()[OtherUsersNS];
00230 otherNSToCheck += namespaces()[SharedNS];
00231 for ( QStringList::Iterator it = otherNSToCheck.begin();
00232 it != otherNSToCheck.end(); ++it ) {
00233 if ( (*it).isEmpty() ) {
00234
00235
00236 nsToList += *it;
00237 }
00238 }
00239 folder->setNamespacesToList( nsToList );
00240 }
00241
00242 Q_ASSERT( !mMailCheckProgressItem );
00243 mMailCheckProgressItem = KPIM::ProgressManager::createProgressItem(
00244 "MailCheck" + QString::number( id() ),
00245 QStyleSheet::escape( folder->label() ),
00246 QString::null,
00247 true,
00248 useSSL() || useTLS() );
00249 connect( mMailCheckProgressItem, SIGNAL( progressItemCanceled( KPIM::ProgressItem* ) ),
00250 this, SLOT( slotProgressItemCanceled( KPIM::ProgressItem* ) ) );
00251
00252 folder->setAccount(this);
00253 connect(folder, SIGNAL(folderComplete(KMFolderCachedImap*, bool)),
00254 this, SLOT(postProcessNewMail(KMFolderCachedImap*, bool)));
00255 folder->serverSync( recurse );
00256 }
00257
00258 void KMAcctCachedImap::postProcessNewMail( KMFolderCachedImap* folder, bool )
00259 {
00260 mNoopTimer.start( 60000 );
00261 disconnect(folder, SIGNAL(folderComplete(KMFolderCachedImap*, bool)),
00262 this, SLOT(postProcessNewMail(KMFolderCachedImap*, bool)));
00263 if ( mMailCheckProgressItem ) {
00264 mMailCheckProgressItem->setComplete();
00265 mMailCheckProgressItem = 0;
00266 }
00267
00268 if ( folder == mFolder ) {
00269
00270
00271
00272
00273
00274
00275 #if 0 // this opens a race: delete a folder during a sync (after the sync checked that folder), and it'll be forgotten...
00276 mDeletedFolders.clear();
00277 #endif
00278 mPreviouslyDeletedFolders.clear();
00279 }
00280
00281 KMail::ImapAccountBase::postProcessNewMail();
00282 }
00283
00284 void KMAcctCachedImap::addUnreadMsgCount( const KMFolderCachedImap *folder,
00285 int countUnread )
00286 {
00287 if ( folder->imapPath() != "/INBOX/" ) {
00288
00289
00290 const QString folderId = folder->folder()->idString();
00291 int newInFolder = countUnread;
00292 if ( mUnreadBeforeCheck.find( folderId ) != mUnreadBeforeCheck.end() )
00293 newInFolder -= mUnreadBeforeCheck[folderId];
00294 if ( newInFolder > 0 )
00295 addToNewInFolder( folderId, newInFolder );
00296 }
00297 mCountUnread += countUnread;
00298 }
00299
00300 void KMAcctCachedImap::addLastUnreadMsgCount( const KMFolderCachedImap *folder,
00301 int countLastUnread )
00302 {
00303 mUnreadBeforeCheck[folder->folder()->idString()] = countLastUnread;
00304 mCountLastUnread += countLastUnread;
00305 }
00306
00307
00308
00309
00310
00311
00312
00313 void KMAcctCachedImap::readConfig( KConfig & config ) {
00314 ImapAccountBase::readConfig( config );
00315
00316 mPreviouslyDeletedFolders = config.readListEntry( "deleted-folders" );
00317 mDeletedFolders.clear();
00318 const QStringList oldPaths = config.readListEntry( "renamed-folders-paths" );
00319 const QStringList newNames = config.readListEntry( "renamed-folders-names" );
00320 QStringList::const_iterator it = oldPaths.begin();
00321 QStringList::const_iterator nameit = newNames.begin();
00322 for( ; it != oldPaths.end() && nameit != newNames.end(); ++it, ++nameit ) {
00323 addRenamedFolder( *it, QString::null, *nameit );
00324 }
00325 mGroupwareType = (GroupwareType)config.readNumEntry( "groupwareType", GroupwareKolab );
00326 }
00327
00328 void KMAcctCachedImap::writeConfig( KConfig & config ) {
00329 ImapAccountBase::writeConfig( config );
00330 config.writeEntry( "deleted-folders", mDeletedFolders + mPreviouslyDeletedFolders );
00331 config.writeEntry( "renamed-folders-paths", mRenamedFolders.keys() );
00332 const QValueList<RenamedFolder> values = mRenamedFolders.values();
00333 QStringList lstNames;
00334 QValueList<RenamedFolder>::const_iterator it = values.begin();
00335 for ( ; it != values.end() ; ++it )
00336 lstNames.append( (*it).mNewName );
00337 config.writeEntry( "renamed-folders-names", lstNames );
00338 config.writeEntry( "groupwareType", mGroupwareType );
00339 }
00340
00341 void KMAcctCachedImap::invalidateIMAPFolders()
00342 {
00343 invalidateIMAPFolders( mFolder );
00344 }
00345
00346 void KMAcctCachedImap::invalidateIMAPFolders( KMFolderCachedImap* folder )
00347 {
00348 if( !folder || !folder->folder() )
00349 return;
00350
00351 folder->setAccount(this);
00352
00353 QStringList strList;
00354 QValueList<QGuardedPtr<KMFolder> > folderList;
00355 kmkernel->dimapFolderMgr()->createFolderList( &strList, &folderList,
00356 folder->folder()->child(), QString::null,
00357 false );
00358 QValueList<QGuardedPtr<KMFolder> >::Iterator it;
00359 mCountLastUnread = 0;
00360 mUnreadBeforeCheck.clear();
00361
00362 for( it = folderList.begin(); it != folderList.end(); ++it ) {
00363 KMFolder *f = *it;
00364 if( f && f->folderType() == KMFolderTypeCachedImap ) {
00365 KMFolderCachedImap *cfolder = static_cast<KMFolderCachedImap*>(f->storage());
00366
00367 cfolder->setUidValidity("INVALID");
00368 cfolder->writeUidCache();
00369 }
00370 }
00371 folder->setUidValidity("INVALID");
00372 folder->writeUidCache();
00373
00374 processNewMailInFolder( folder->folder(), Recursive );
00375 }
00376
00377
00378 void KMAcctCachedImap::addDeletedFolder( KMFolder* folder )
00379 {
00380 if ( !folder || folder->folderType() != KMFolderTypeCachedImap )
00381 return;
00382 KMFolderCachedImap* storage = static_cast<KMFolderCachedImap*>(folder->storage());
00383 addDeletedFolder( storage->imapPath() );
00384 kdDebug(5006) << k_funcinfo << storage->imapPath() << endl;
00385
00386
00387 if( folder->child() ) {
00388 KMFolderNode *node = folder->child()->first();
00389 while( node ) {
00390 if( !node->isDir() ) {
00391 addDeletedFolder( static_cast<KMFolder*>( node ) );
00392 }
00393 node = folder->child()->next();
00394 }
00395 }
00396 }
00397
00398 void KMAcctCachedImap::addDeletedFolder( const QString& imapPath )
00399 {
00400 mDeletedFolders << imapPath;
00401 }
00402
00403 QStringList KMAcctCachedImap::deletedFolderPaths( const QString& subFolderPath ) const
00404 {
00405 QStringList lst;
00406 for ( QStringList::const_iterator it = mDeletedFolders.begin(); it != mDeletedFolders.end(); ++it ) {
00407 if ( (*it).startsWith( subFolderPath ) )
00408
00409 lst.prepend( *it );
00410 }
00411 for ( QStringList::const_iterator it = mPreviouslyDeletedFolders.begin(); it != mPreviouslyDeletedFolders.end(); ++it ) {
00412 if ( (*it).startsWith( subFolderPath ) )
00413 lst.prepend( *it );
00414 }
00415 kdDebug(5006) << "KMAcctCachedImap::deletedFolderPaths for " << subFolderPath << " returning: " << lst << endl;
00416 Q_ASSERT( !lst.isEmpty() );
00417 return lst;
00418 }
00419
00420 bool KMAcctCachedImap::isDeletedFolder( const QString& subFolderPath ) const
00421 {
00422 return mDeletedFolders.find( subFolderPath ) != mDeletedFolders.end();
00423 }
00424
00425 bool KMAcctCachedImap::isPreviouslyDeletedFolder( const QString& subFolderPath ) const
00426 {
00427 return mPreviouslyDeletedFolders.find( subFolderPath ) != mPreviouslyDeletedFolders.end();
00428 }
00429
00430 void KMAcctCachedImap::removeDeletedFolder( const QString& subFolderPath )
00431 {
00432 mDeletedFolders.remove( subFolderPath );
00433 mPreviouslyDeletedFolders.remove( subFolderPath );
00434 }
00435
00436 void KMAcctCachedImap::addRenamedFolder( const QString& subFolderPath, const QString& oldLabel, const QString& newName )
00437 {
00438 mRenamedFolders.insert( subFolderPath, RenamedFolder( oldLabel, newName ) );
00439 }
00440
00441 void KMAcctCachedImap::removeRenamedFolder( const QString& subFolderPath )
00442 {
00443 mRenamedFolders.remove( subFolderPath );
00444 }
00445
00446 void KMAcctCachedImap::slotProgressItemCanceled( ProgressItem* )
00447 {
00448 bool abortConnection = !mSlaveConnected;
00449 killAllJobs( abortConnection );
00450 if ( abortConnection ) {
00451
00452 emit connectionResult( KIO::ERR_USER_CANCELED, QString::null );
00453 }
00454 }
00455
00456 FolderStorage* KMAcctCachedImap::rootFolder() const
00457 {
00458 return mFolder;
00459 }
00460
00461
00462 QString KMAcctCachedImap::renamedFolder( const QString& imapPath ) const
00463 {
00464 QMap<QString, RenamedFolder>::ConstIterator renit = mRenamedFolders.find( imapPath );
00465 if ( renit != mRenamedFolders.end() )
00466 return (*renit).mNewName;
00467 return QString::null;
00468 }
00469
00470 #include "kmacctcachedimap.moc"