kmail

kmacctcachedimap.cpp

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 <kmessagebox.h>
00052 #include <kdebug.h>
00053 #include <kstandarddirs.h>
00054 #include <kapplication.h>
00055 #include <kconfig.h>
00056 
00057 #include <qstylesheet.h>
00058 
00059 KMAcctCachedImap::KMAcctCachedImap( AccountManager* aOwner,
00060                     const QString& aAccountName, uint id )
00061   : KMail::ImapAccountBase( aOwner, aAccountName, id ), mFolder( 0 ),
00062     mAnnotationCheckPassed(false)
00063 {
00064   // Never EVER set this for the cached IMAP account
00065   mAutoExpunge = false;
00066 }
00067 
00068 
00069 //-----------------------------------------------------------------------------
00070 KMAcctCachedImap::~KMAcctCachedImap()
00071 {
00072   killAllJobsInternal( true );
00073 }
00074 
00075 
00076 //-----------------------------------------------------------------------------
00077 QString KMAcctCachedImap::type() const
00078 {
00079   return "cachedimap";
00080 }
00081 
00082 void KMAcctCachedImap::init() {
00083   ImapAccountBase::init();
00084 }
00085 
00086 //-----------------------------------------------------------------------------
00087 void KMAcctCachedImap::pseudoAssign( const KMAccount * a ) {
00088   killAllJobs( true );
00089   if (mFolder)
00090   {
00091     mFolder->setContentState(KMFolderCachedImap::imapNoInformation);
00092     mFolder->setSubfolderState(KMFolderCachedImap::imapNoInformation);
00093   }
00094   ImapAccountBase::pseudoAssign( a );
00095 }
00096 
00097 //-----------------------------------------------------------------------------
00098 void KMAcctCachedImap::setImapFolder(KMFolderCachedImap *aFolder)
00099 {
00100   mFolder = aFolder;
00101   mFolder->setImapPath( "/" );
00102   mFolder->setAccount( this );
00103 }
00104 
00105 
00106 //-----------------------------------------------------------------------------
00107 void KMAcctCachedImap::setAutoExpunge( bool /*aAutoExpunge*/ )
00108 {
00109   // Never EVER set this for the cached IMAP account
00110   mAutoExpunge = false;
00111 }
00112 
00113 //-----------------------------------------------------------------------------
00114 void KMAcctCachedImap::killAllJobs( bool disconnectSlave )
00115 {
00116   //kdDebug(5006) << "killAllJobs: disconnectSlave=" << disconnectSlave << "  " << mapJobData.count() << " jobs in map." << endl;
00117   QValueList<KMFolderCachedImap*> folderList = killAllJobsInternal( disconnectSlave );
00118   for( QValueList<KMFolderCachedImap*>::Iterator it = folderList.begin(); it != folderList.end(); ++it ) {
00119     KMFolderCachedImap *fld = *it;
00120     fld->resetSyncState();
00121     fld->setContentState(KMFolderCachedImap::imapNoInformation);
00122     fld->setSubfolderState(KMFolderCachedImap::imapNoInformation);
00123     fld->sendFolderComplete(FALSE);
00124   }
00125 }
00126 
00127 //-----------------------------------------------------------------------------
00128 // Common between killAllJobs and the destructor - which shouldn't call sendFolderComplete
00129 QValueList<KMFolderCachedImap*> KMAcctCachedImap::killAllJobsInternal( bool disconnectSlave )
00130 {
00131   // Make list of folders to reset. This must be done last, since folderComplete
00132   // can trigger the next queued mail check already.
00133   QValueList<KMFolderCachedImap*> folderList;
00134   QMap<KIO::Job*, jobData>::Iterator it = mapJobData.begin();
00135   for (; it != mapJobData.end(); ++it) {
00136     if ((*it).parent)
00137       folderList << static_cast<KMFolderCachedImap*>((*it).parent->storage());
00138     // Kill the job - except if it's the one that already died and is calling us
00139     if ( !it.key()->error() && mSlave ) {
00140       it.key()->kill();
00141       mSlave = 0; // killing a job, kills the slave
00142     }
00143   }
00144   mapJobData.clear();
00145 
00146   // Clear the joblist. Make SURE to stop the job emitting "finished"
00147   for( QPtrListIterator<CachedImapJob> it( mJobList ); it.current(); ++it )
00148     it.current()->setPassiveDestructor( true );
00149   KMAccount::deleteFolderJobs();
00150 
00151   if ( disconnectSlave && mSlave ) {
00152     KIO::Scheduler::disconnectSlave( mSlave );
00153     mSlave = 0;
00154   }
00155   return folderList;
00156 }
00157 
00158 //-----------------------------------------------------------------------------
00159 void KMAcctCachedImap::cancelMailCheck()
00160 {
00161   // Make list of folders to reset, like in killAllJobs
00162   QValueList<KMFolderCachedImap*> folderList;
00163   QMap<KIO::Job*, jobData>::Iterator it = mapJobData.begin();
00164   for (; it != mapJobData.end(); ++it) {
00165     if ( (*it).cancellable && (*it).parent )
00166       folderList << static_cast<KMFolderCachedImap*>((*it).parent->storage());
00167   }
00168   // Kill jobs
00169   ImapAccountBase::cancelMailCheck();
00170   // Reset sync states and emit folderComplete, this is important for
00171   // KMAccount::checkingMail() to be reset, in case we restart checking mail later.
00172   for( QValueList<KMFolderCachedImap*>::Iterator it = folderList.begin(); it != folderList.end(); ++it ) {
00173     KMFolderCachedImap *fld = *it;
00174     fld->resetSyncState();
00175     fld->setContentState(KMFolderCachedImap::imapNoInformation);
00176     fld->setSubfolderState(KMFolderCachedImap::imapNoInformation);
00177     fld->sendFolderComplete(FALSE);
00178   }
00179 }
00180 
00181 //-----------------------------------------------------------------------------
00182 void KMAcctCachedImap::killJobsForItem(KMFolderTreeItem * fti)
00183 {
00184   QMap<KIO::Job *, jobData>::Iterator it = mapJobData.begin();
00185   while (it != mapJobData.end())
00186   {
00187     if (it.data().parent == fti->folder())
00188     {
00189       killAllJobs();
00190       break;
00191     }
00192     else ++it;
00193   }
00194 }
00195 
00196 // Reimplemented from ImapAccountBase because we only check one folder at a time
00197 void KMAcctCachedImap::slotCheckQueuedFolders()
00198 {
00199     mMailCheckFolders.clear();
00200     mMailCheckFolders.append( mFoldersQueuedForChecking.front() );
00201     mFoldersQueuedForChecking.pop_front();
00202     if ( mFoldersQueuedForChecking.isEmpty() )
00203       disconnect( this, SIGNAL( finishedCheck( bool, CheckStatus ) ),
00204                   this, SLOT( slotCheckQueuedFolders() ) );
00205 
00206     kmkernel->acctMgr()->singleCheckMail(this, true);
00207     mMailCheckFolders.clear();
00208 }
00209 
00210 void KMAcctCachedImap::processNewMail( bool /*interactive*/ )
00211 {
00212   assert( mFolder );
00213 
00214   if ( mMailCheckFolders.isEmpty() )
00215     processNewMail( mFolder, true );
00216   else {
00217     KMFolder* f = mMailCheckFolders.front();
00218     mMailCheckFolders.pop_front();
00219     processNewMail( static_cast<KMFolderCachedImap *>( f->storage() ), false );
00220   }
00221 }
00222 
00223 void KMAcctCachedImap::processNewMail( KMFolderCachedImap* folder,
00224                                        bool recurse )
00225 {
00226   assert( folder );
00227   // This should never be set for a cached IMAP account
00228   mAutoExpunge = false;
00229   mCountLastUnread = 0;
00230   mUnreadBeforeCheck.clear();
00231   // stop sending noops during sync, that will keep the connection open
00232   mNoopTimer.stop();
00233 
00234   // reset namespace todo
00235   if ( folder == mFolder ) {
00236     QStringList nsToList = namespaces()[PersonalNS];
00237     QStringList otherNSToCheck = namespaces()[OtherUsersNS];
00238     otherNSToCheck += namespaces()[SharedNS];
00239     for ( QStringList::Iterator it = otherNSToCheck.begin(); 
00240           it != otherNSToCheck.end(); ++it ) {
00241       if ( (*it).isEmpty() ) {
00242         // empty namespaces are included in the "normal" listing
00243         // as the folders are created under the root folder
00244         nsToList += *it;
00245       }
00246     }
00247     folder->setNamespacesToList( nsToList );
00248   }
00249 
00250   Q_ASSERT( !mMailCheckProgressItem );
00251   mMailCheckProgressItem = KPIM::ProgressManager::createProgressItem(
00252     "MailCheck" + QString::number( id() ),
00253     QStyleSheet::escape( folder->label() ), // will be changed immediately in serverSync anyway
00254     QString::null,
00255     true, // can be cancelled
00256     useSSL() || useTLS() );
00257   connect( mMailCheckProgressItem, SIGNAL( progressItemCanceled( KPIM::ProgressItem* ) ),
00258            this, SLOT( slotProgressItemCanceled( KPIM::ProgressItem* ) ) );
00259 
00260   folder->setAccount(this);
00261   connect(folder, SIGNAL(folderComplete(KMFolderCachedImap*, bool)),
00262           this, SLOT(postProcessNewMail(KMFolderCachedImap*, bool)));
00263   folder->serverSync( recurse );
00264 }
00265 
00266 void KMAcctCachedImap::postProcessNewMail( KMFolderCachedImap* folder, bool )
00267 {
00268   mNoopTimer.start( 60000 ); // send a noop every minute to avoid "connection broken" errors
00269   disconnect(folder, SIGNAL(folderComplete(KMFolderCachedImap*, bool)),
00270              this, SLOT(postProcessNewMail(KMFolderCachedImap*, bool)));
00271   mMailCheckProgressItem->setComplete();
00272   mMailCheckProgressItem = 0;
00273 
00274   if ( folder == mFolder ) {
00275     // We remove everything from the deleted folders list after a full sync.
00276     // Even if it fails (no permission), because on the next sync we want the folder to reappear,
00277     //  instead of the user being stuck with "can't delete" every time.
00278     // And we do it for _all_ deleted folders, even those that were deleted on the server in the first place (slotListResult).
00279     //  Otherwise this might have side effects much later (e.g. when regaining permissions to a folder we could see before)
00280 
00281 #if 0 // this opens a race: delete a folder during a sync (after the sync checked that folder), and it'll be forgotten...
00282     mDeletedFolders.clear();
00283 #endif
00284     mPreviouslyDeletedFolders.clear();
00285   }
00286 
00287   KMail::ImapAccountBase::postProcessNewMail();
00288 }
00289 
00290 void KMAcctCachedImap::addUnreadMsgCount( const KMFolderCachedImap *folder,
00291                                           int countUnread )
00292 {
00293   if ( folder->imapPath() != "/INBOX/" ) {
00294     // new mail in INBOX is processed with KMAccount::processNewMsg() and
00295     // therefore doesn't need to be counted here
00296     const QString folderId = folder->folder()->idString();
00297     int newInFolder = countUnread;
00298     if ( mUnreadBeforeCheck.find( folderId ) != mUnreadBeforeCheck.end() )
00299       newInFolder -= mUnreadBeforeCheck[folderId];
00300     if ( newInFolder > 0 )
00301       addToNewInFolder( folderId, newInFolder );
00302   }
00303   mCountUnread += countUnread;
00304 }
00305 
00306 void KMAcctCachedImap::addLastUnreadMsgCount( const KMFolderCachedImap *folder,
00307                                               int countLastUnread )
00308 {
00309   mUnreadBeforeCheck[folder->folder()->idString()] = countLastUnread;
00310   mCountLastUnread += countLastUnread;
00311 }
00312 
00313 //
00314 //
00315 // read/write config
00316 //
00317 //
00318 
00319 void KMAcctCachedImap::readConfig( /*const*/ KConfig/*Base*/ & config ) {
00320   ImapAccountBase::readConfig( config );
00321   // Apparently this method is only ever called once (from KMKernel::init) so this is ok
00322   mPreviouslyDeletedFolders = config.readListEntry( "deleted-folders" );
00323   mDeletedFolders.clear(); // but just in case...
00324   const QStringList oldPaths = config.readListEntry( "renamed-folders-paths" );
00325   const QStringList newNames = config.readListEntry( "renamed-folders-names" );
00326   QStringList::const_iterator it = oldPaths.begin();
00327   QStringList::const_iterator nameit = newNames.begin();
00328   for( ; it != oldPaths.end() && nameit != newNames.end(); ++it, ++nameit ) {
00329     addRenamedFolder( *it, QString::null, *nameit );
00330   }
00331 }
00332 
00333 void KMAcctCachedImap::writeConfig( KConfig/*Base*/ & config ) /*const*/ {
00334   ImapAccountBase::writeConfig( config );
00335   config.writeEntry( "deleted-folders", mDeletedFolders + mPreviouslyDeletedFolders );
00336   config.writeEntry( "renamed-folders-paths", mRenamedFolders.keys() );
00337   const QValueList<RenamedFolder> values = mRenamedFolders.values();
00338   QStringList lstNames;
00339   QValueList<RenamedFolder>::const_iterator it = values.begin();
00340   for ( ; it != values.end() ; ++it )
00341     lstNames.append( (*it).mNewName );
00342   config.writeEntry( "renamed-folders-names", lstNames );
00343 }
00344 
00345 void KMAcctCachedImap::invalidateIMAPFolders()
00346 {
00347   invalidateIMAPFolders( mFolder );
00348 }
00349 
00350 void KMAcctCachedImap::invalidateIMAPFolders( KMFolderCachedImap* folder )
00351 {
00352   if( !folder || !folder->folder() )
00353     return;
00354 
00355   folder->setAccount(this);
00356 
00357   QStringList strList;
00358   QValueList<QGuardedPtr<KMFolder> > folderList;
00359   kmkernel->dimapFolderMgr()->createFolderList( &strList, &folderList,
00360                         folder->folder()->child(), QString::null,
00361                         false );
00362   QValueList<QGuardedPtr<KMFolder> >::Iterator it;
00363   mCountLastUnread = 0;
00364   mUnreadBeforeCheck.clear();
00365 
00366   for( it = folderList.begin(); it != folderList.end(); ++it ) {
00367     KMFolder *f = *it;
00368     if( f && f->folderType() == KMFolderTypeCachedImap ) {
00369       KMFolderCachedImap *cfolder = static_cast<KMFolderCachedImap*>(f->storage());
00370       // This invalidates the folder completely
00371       cfolder->setUidValidity("INVALID");
00372       cfolder->writeUidCache();
00373       processNewMailSingleFolder( f );
00374     }
00375   }
00376   folder->setUidValidity("INVALID");
00377   folder->writeUidCache();
00378 
00379   processNewMailSingleFolder( folder->folder() );
00380 }
00381 
00382 //-----------------------------------------------------------------------------
00383 void KMAcctCachedImap::addDeletedFolder( KMFolder* folder )
00384 {
00385   if ( !folder || folder->folderType() != KMFolderTypeCachedImap )
00386     return;
00387   KMFolderCachedImap* storage = static_cast<KMFolderCachedImap*>(folder->storage());
00388   addDeletedFolder( storage->imapPath() );
00389   kdDebug(5006) << k_funcinfo << storage->imapPath() << endl;
00390 
00391   // Add all child folders too
00392   if( folder->child() ) {
00393     KMFolderNode *node = folder->child()->first();
00394     while( node ) {
00395       if( !node->isDir() ) {
00396         addDeletedFolder( static_cast<KMFolder*>( node ) ); // recurse
00397       }
00398       node = folder->child()->next();
00399     }
00400   }
00401 }
00402 
00403 void KMAcctCachedImap::addDeletedFolder( const QString& imapPath )
00404 {
00405   mDeletedFolders << imapPath;
00406 }
00407 
00408 QStringList KMAcctCachedImap::deletedFolderPaths( const QString& subFolderPath ) const
00409 {
00410   QStringList lst;
00411   for ( QStringList::const_iterator it = mDeletedFolders.begin(); it != mDeletedFolders.end(); ++it ) {
00412     if ( (*it).startsWith( subFolderPath ) )
00413       // We must reverse the order, so that sub sub sub folders are deleted first
00414       lst.prepend( *it );
00415   }
00416   for ( QStringList::const_iterator it = mPreviouslyDeletedFolders.begin(); it != mPreviouslyDeletedFolders.end(); ++it ) {
00417     if ( (*it).startsWith( subFolderPath ) )
00418       lst.prepend( *it );
00419   }
00420   kdDebug(5006) << "KMAcctCachedImap::deletedFolderPaths for " << subFolderPath << " returning: " << lst << endl;
00421   Q_ASSERT( !lst.isEmpty() );
00422   return lst;
00423 }
00424 
00425 bool KMAcctCachedImap::isDeletedFolder( const QString& subFolderPath ) const
00426 {
00427   return mDeletedFolders.find( subFolderPath ) != mDeletedFolders.end();
00428 }
00429 
00430 bool KMAcctCachedImap::isPreviouslyDeletedFolder( const QString& subFolderPath ) const
00431 {
00432   return mPreviouslyDeletedFolders.find( subFolderPath ) != mPreviouslyDeletedFolders.end();
00433 }
00434 
00435 void KMAcctCachedImap::removeDeletedFolder( const QString& subFolderPath )
00436 {
00437   mDeletedFolders.remove( subFolderPath );
00438   mPreviouslyDeletedFolders.remove( subFolderPath );
00439 }
00440 
00441 void KMAcctCachedImap::addRenamedFolder( const QString& subFolderPath, const QString& oldLabel, const QString& newName )
00442 {
00443   mRenamedFolders.insert( subFolderPath, RenamedFolder( oldLabel, newName ) );
00444 }
00445 
00446 void KMAcctCachedImap::removeRenamedFolder( const QString& subFolderPath )
00447 {
00448   mRenamedFolders.remove( subFolderPath );
00449 }
00450 
00451 void KMAcctCachedImap::slotProgressItemCanceled( ProgressItem* )
00452 {
00453   bool abortConnection = !mSlaveConnected;
00454   killAllJobs( abortConnection );
00455   if ( abortConnection ) {
00456     // If we were trying to connect, tell kmfoldercachedimap so that it moves on
00457     emit connectionResult( KIO::ERR_USER_CANCELED, QString::null );
00458   }
00459 }
00460 
00461 FolderStorage* const KMAcctCachedImap::rootFolder() const
00462 {
00463   return mFolder;
00464 }
00465 
00466 
00467 QString KMAcctCachedImap::renamedFolder( const QString& imapPath ) const
00468 {
00469   QMap<QString, RenamedFolder>::ConstIterator renit = mRenamedFolders.find( imapPath );
00470   if ( renit != mRenamedFolders.end() )
00471     return (*renit).mNewName;
00472   return QString::null;
00473 }
00474 
00475 #include "kmacctcachedimap.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys