kmail

kmacctcachedimap.cpp

Go to the documentation of this file.
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   // Never EVER set this for the cached IMAP account
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 /*aAutoExpunge*/ )
00109 {
00110   // Never EVER set this for the cached IMAP account
00111   mAutoExpunge = false;
00112 }
00113 
00114 //-----------------------------------------------------------------------------
00115 void KMAcctCachedImap::killAllJobs( bool disconnectSlave )
00116 {
00117   //kdDebug(5006) << "killAllJobs: disconnectSlave=" << disconnectSlave << "  " << mapJobData.count() << " jobs in map." << endl;
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 // Common between killAllJobs and the destructor - which shouldn't call sendFolderComplete
00130 QValueList<KMFolderCachedImap*> KMAcctCachedImap::killAllJobsInternal( bool disconnectSlave )
00131 {
00132   // Make list of folders to reset. This must be done last, since folderComplete
00133   // can trigger the next queued mail check already.
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     // Kill the job - except if it's the one that already died and is calling us
00140     if ( !it.key()->error() && mSlave ) {
00141       it.key()->kill();
00142       mSlave = 0; // killing a job, kills the slave
00143     }
00144   }
00145   mapJobData.clear();
00146 
00147   // Clear the joblist. Make SURE to stop the job emitting "finished"
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   // Make list of folders to reset, like in killAllJobs
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   // Kill jobs
00170   ImapAccountBase::cancelMailCheck();
00171   // Reset sync states and emit folderComplete, this is important for
00172   // KMAccount::checkingMail() to be reset, in case we restart checking mail later.
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 // Reimplemented from ImapAccountBase because we only check one folder at a time
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 /*interactive*/ )
00212 {
00213   assert( mFolder );  // George says "better to crash then lose mail"
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 ); // George says "better to crash then lose mail"
00228 
00229   // This should never be set for a cached IMAP account
00230   mAutoExpunge = false;
00231   mCountLastUnread = 0;
00232   mUnreadBeforeCheck.clear();
00233   // stop sending noops during sync, that will keep the connection open
00234   mNoopTimer.stop();
00235 
00236   // reset namespace todo
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         // empty namespaces are included in the "normal" listing
00245         // as the folders are created under the root folder
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() ), // will be changed immediately in serverSync anyway
00256     QString::null,
00257     true, // can be cancelled
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 ); // send a noop every minute to avoid "connection broken" errors
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     // We remove everything from the deleted folders list after a full sync.
00278     // Even if it fails (no permission), because on the next sync we want the folder to reappear,
00279     //  instead of the user being stuck with "can't delete" every time.
00280     // And we do it for _all_ deleted folders, even those that were deleted on the server in the first place (slotListResult).
00281     //  Otherwise this might have side effects much later (e.g. when regaining permissions to a folder we could see before)
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     // new mail in INBOX is processed with KMAccount::processNewMsg() and
00297     // therefore doesn't need to be counted here
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 // read/write config
00318 //
00319 //
00320 
00321 void KMAcctCachedImap::readConfig( /*const*/ KConfig/*Base*/ & config ) {
00322   ImapAccountBase::readConfig( config );
00323   // Apparently this method is only ever called once (from KMKernel::init) so this is ok
00324   mPreviouslyDeletedFolders = config.readListEntry( "deleted-folders" );
00325   mDeletedFolders.clear(); // but just in case...
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/*Base*/ & config ) /*const*/ {
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       // This invalidates the folder completely
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   // Add all child folders too
00396   if( folder->child() ) {
00397     KMFolderNode *node = folder->child()->first();
00398     while( node ) {
00399       if( !node->isDir() ) {
00400         addDeletedFolder( static_cast<KMFolder*>( node ) ); // recurse
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       // We must reverse the order, so that sub sub sub folders are deleted first
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     // If we were trying to connect, tell kmfoldercachedimap so that it moves on
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"