kmail

accountmanager.cpp

00001 // KMail Account Manager
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include "accountmanager.h"
00008 
00009 #include "kmaccount.h"
00010 #include "kmacctmaildir.h"
00011 #include "kmacctlocal.h"
00012 #include "popaccount.h"
00013 #include "kmacctimap.h"
00014 #include "networkaccount.h"
00015 #include "kmacctcachedimap.h"
00016 #include "broadcaststatus.h"
00017 #include "kmfiltermgr.h"
00018 #include "globalsettings.h"
00019 
00020 #include <klocale.h>
00021 #include <kmessagebox.h>
00022 #include <kdebug.h>
00023 #include <kconfig.h>
00024 #include <kapplication.h>
00025 
00026 #include <qregexp.h>
00027 #include <qvaluelist.h>
00028 
00029 using namespace KMail;
00030 
00031 //-----------------------------------------------------------------------------
00032 AccountManager::AccountManager()
00033     :QObject(), mNewMailArrived( false ), mInteractive( false ),
00034      mTotalNewMailsArrived( 0 ), mDisplaySummary( false )
00035 {
00036   mAcctChecking.clear();
00037   mAcctTodo.clear();
00038 }
00039 
00040 //-----------------------------------------------------------------------------
00041 AccountManager::~AccountManager()
00042 {
00043   writeConfig( false );
00044 }
00045 
00046 
00047 //-----------------------------------------------------------------------------
00048 void AccountManager::writeConfig( bool withSync )
00049 {
00050   KConfig* config = KMKernel::config();
00051   QString groupName;
00052 
00053   KConfigGroupSaver saver(config, "General");
00054   config->writeEntry("accounts", mAcctList.count());
00055 
00056   // first delete all account groups in the config file:
00057   QStringList accountGroups =
00058     config->groupList().grep( QRegExp( "Account \\d+" ) );
00059   for ( QStringList::Iterator it = accountGroups.begin() ;
00060     it != accountGroups.end() ; ++it )
00061     config->deleteGroup( *it );
00062 
00063   // now write new account groups:
00064   int i = 1;
00065   for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it, ++i ) {
00066     groupName.sprintf("Account %d", i);
00067     KConfigGroupSaver saver(config, groupName);
00068     (*it)->writeConfig(*config);
00069   }
00070   if (withSync) config->sync();
00071 }
00072 
00073 
00074 //-----------------------------------------------------------------------------
00075 void AccountManager::readConfig(void)
00076 {
00077   KConfig* config = KMKernel::config();
00078   KMAccount* acct;
00079   QString acctType, acctName;
00080   QCString groupName;
00081   int i, num;
00082   uint id;
00083 
00084   for ( AccountList::Iterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it )
00085       delete *it;
00086   mAcctList.clear();
00087 
00088   KConfigGroup general(config, "General");
00089   num = general.readNumEntry("accounts", 0);
00090 
00091   for (i=1; i<=num; i++)
00092   {
00093     groupName.sprintf("Account %d", i);
00094     KConfigGroupSaver saver(config, groupName);
00095     acctType = config->readEntry("Type");
00096     // Provide backwards compatibility
00097     if (acctType == "advanced pop" || acctType == "experimental pop")
00098       acctType = "pop";
00099     acctName = config->readEntry("Name");
00100     id = config->readUnsignedNumEntry("Id", 0);
00101     if (acctName.isEmpty()) acctName = i18n("Account %1").arg(i);
00102     acct = create(acctType, acctName, id);
00103     if (!acct) continue;
00104     add(acct);
00105     acct->readConfig(*config);
00106   }
00107 }
00108 
00109 
00110 //-----------------------------------------------------------------------------
00111 void AccountManager::singleCheckMail(KMAccount *account, bool interactive)
00112 {
00113   mNewMailArrived = false;
00114   mInteractive = interactive;
00115 
00116   // queue the account
00117   mAcctTodo.append(account);
00118 
00119   if (account->checkingMail())
00120   {
00121     kdDebug(5006) << "account " << account->name() << " busy, queuing" << endl;
00122     return;
00123   }
00124 
00125   processNextCheck(false);
00126 }
00127 
00128 //-----------------------------------------------------------------------------
00129 void AccountManager::processNextCheck( bool _newMail )
00130 {
00131   kdDebug(5006) << "processNextCheck, remaining " << mAcctTodo.count() << endl;
00132   if ( _newMail )
00133     mNewMailArrived = true;
00134 
00135   for ( AccountList::Iterator it( mAcctChecking.begin() ), end( mAcctChecking.end() ); it != end;  ) {
00136     KMAccount* acct = *it;
00137     ++it;
00138     if ( acct->checkingMail() )
00139       continue;
00140     // check done
00141     kdDebug(5006) << "account " << acct->name() << " finished check" << endl;
00142     mAcctChecking.remove( acct );
00143     kmkernel->filterMgr()->deref();
00144     disconnect( acct, SIGNAL( finishedCheck( bool, CheckStatus ) ),
00145                       this, SLOT( processNextCheck( bool ) ) );
00146   }
00147   if ( mAcctChecking.isEmpty() ) {
00148     // all checks finished, display summary
00149     if ( mDisplaySummary )
00150       KPIM::BroadcastStatus::instance()->setStatusMsgTransmissionCompleted(
00151           mTotalNewMailsArrived );
00152     emit checkedMail( mNewMailArrived, mInteractive, mTotalNewInFolder );
00153     mTotalNewMailsArrived = 0;
00154     mTotalNewInFolder.clear();
00155     mDisplaySummary = false;
00156   }
00157   if ( mAcctTodo.isEmpty() ) return;
00158 
00159   QString accountHostName;
00160 
00161   KMAccount *curAccount = 0;
00162   for ( AccountList::Iterator it ( mAcctTodo.begin() ), last ( mAcctTodo.end() ); it != last; ) {
00163     KMAccount *acct = *it;
00164     ++it;
00165     if ( !acct->checkingMail() && acct->mailCheckCanProceed() ) {
00166       curAccount = acct;
00167       mAcctTodo.remove( acct );
00168       break;
00169     }
00170   }
00171   if ( !curAccount ) return; // no account or all of them are already checking
00172 
00173   if ( curAccount->type() != "imap" && curAccount->type() != "cachedimap" &&
00174        curAccount->folder() == 0 ) {
00175     QString tmp = i18n("Account %1 has no mailbox defined:\n"
00176         "mail checking aborted;\n"
00177         "check your account settings.")
00178       .arg(curAccount->name());
00179     KMessageBox::information(0,tmp);
00180     emit checkedMail( false, mInteractive, mTotalNewInFolder );
00181     mTotalNewMailsArrived = 0;
00182     mTotalNewInFolder.clear();
00183     return;
00184   }
00185 
00186   connect( curAccount, SIGNAL( finishedCheck( bool, CheckStatus ) ),
00187                 this, SLOT( processNextCheck( bool ) ) );
00188 
00189   KPIM::BroadcastStatus::instance()->setStatusMsg(
00190       i18n("Checking account %1 for new mail").arg(curAccount->name()));
00191 
00192   kdDebug(5006) << "processing next mail check for " << curAccount->name() << endl;
00193 
00194   curAccount->setCheckingMail( true );
00195   mAcctChecking.append( curAccount );
00196   kmkernel->filterMgr()->ref();
00197   curAccount->processNewMail( mInteractive );
00198 }
00199 
00200 //-----------------------------------------------------------------------------
00201 KMAccount* AccountManager::create( const QString &aType, const QString &aName, uint id )
00202 {
00203   KMAccount* act = 0;
00204   if ( id == 0 )
00205     id = createId();
00206 
00207   if ( aType == "local" ) {
00208     act = new KMAcctLocal(this, aName.isEmpty() ? i18n("Local Account") : aName, id);
00209     act->setFolder( kmkernel->inboxFolder() );
00210   } else if ( aType == "maildir" ) {
00211     act = new KMAcctMaildir(this, aName.isEmpty() ? i18n("Local Account") : aName, id);
00212     act->setFolder( kmkernel->inboxFolder() );
00213   } else if ( aType == "pop" ) {
00214     act = new KMail::PopAccount(this, aName.isEmpty() ? i18n("POP Account") : aName, id);
00215     act->setFolder( kmkernel->inboxFolder() );
00216   } else if ( aType == "imap" ) {
00217     act = new KMAcctImap(this, aName.isEmpty() ? i18n("IMAP Account") : aName, id);
00218   } else if (aType == "cachedimap") {
00219     act = new KMAcctCachedImap(this, aName.isEmpty() ? i18n("IMAP Account") : aName, id);
00220   }
00221   if ( !act ) {
00222       kdWarning(5006) << "Attempt to instantiate a non-existing account type!" << endl;
00223       return 0;
00224   }
00225   connect( act, SIGNAL( newMailsProcessed( const QMap<QString, int> & ) ),
00226                 this, SLOT( addToTotalNewMailCount( const QMap<QString, int> & ) ) );
00227   return act;
00228 }
00229 
00230 
00231 //-----------------------------------------------------------------------------
00232 void AccountManager::add( KMAccount *account )
00233 {
00234   if ( account ) {
00235     mAcctList.append( account );
00236     emit accountAdded( account );
00237     account->installTimer();
00238   }
00239 }
00240 
00241 
00242 //-----------------------------------------------------------------------------
00243 KMAccount* AccountManager::findByName(const QString &aName) const
00244 {
00245   if ( aName.isEmpty() ) return 0;
00246 
00247   for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
00248     if ( (*it)->name() == aName ) return (*it);
00249   }
00250   return 0;
00251 }
00252 
00253 
00254 //-----------------------------------------------------------------------------
00255 KMAccount* AccountManager::find( const uint id ) const
00256 {
00257   if (id == 0) return 0;
00258   for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
00259     if ( (*it)->id() == id ) return (*it);
00260   }
00261   return 0;
00262 }
00263 
00264 
00265 //-----------------------------------------------------------------------------
00266 KMAccount* AccountManager::first()
00267 {
00268   if ( !mAcctList.empty() ) {
00269     mPtrListInterfaceProxyIterator = mAcctList.begin();
00270     return *mPtrListInterfaceProxyIterator;
00271   } else {
00272     return 0;
00273   }
00274 }
00275 
00276 //-----------------------------------------------------------------------------
00277 KMAccount* AccountManager::next()
00278 {
00279     ++mPtrListInterfaceProxyIterator;
00280     if ( mPtrListInterfaceProxyIterator == mAcctList.end() )
00281         return 0;
00282     else
00283         return *mPtrListInterfaceProxyIterator;
00284 }
00285 
00286 //-----------------------------------------------------------------------------
00287 bool AccountManager::remove( KMAccount* acct )
00288 {
00289   if( !acct )
00290     return false;
00291   mAcctList.remove( acct );
00292   emit accountRemoved( acct );
00293   return true;
00294 }
00295 
00296 //-----------------------------------------------------------------------------
00297 void AccountManager::checkMail( bool _interactive )
00298 {
00299   mNewMailArrived = false;
00300 
00301   if ( mAcctList.isEmpty() ) {
00302     KMessageBox::information( 0,i18n("You need to add an account in the network "
00303                     "section of the settings in order to receive mail.") );
00304     return;
00305   }
00306   mDisplaySummary = true;
00307 
00308   mTotalNewMailsArrived=0;
00309   mTotalNewInFolder.clear();
00310 
00311   for ( AccountList::Iterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
00312     if ( !(*it)->checkExclude() )
00313       singleCheckMail( (*it), _interactive);
00314   }
00315 }
00316 
00317 
00318 //-----------------------------------------------------------------------------
00319 void AccountManager::singleInvalidateIMAPFolders(KMAccount *account) {
00320   account->invalidateIMAPFolders();
00321 }
00322 
00323 
00324 void AccountManager::invalidateIMAPFolders()
00325 {
00326   for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it )
00327     singleInvalidateIMAPFolders( *it );
00328 }
00329 
00330 
00331 //-----------------------------------------------------------------------------
00332 QStringList  AccountManager::getAccounts() const
00333 {
00334   QStringList strList;
00335   for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
00336     strList.append( (*it)->name() );
00337   }
00338   return strList;
00339 }
00340 
00341 //-----------------------------------------------------------------------------
00342 void AccountManager::intCheckMail(int item, bool _interactive)
00343 {
00344   mNewMailArrived = false;
00345   mTotalNewMailsArrived = 0;
00346   mTotalNewInFolder.clear();
00347   if ( KMAccount *acct = mAcctList[ item ] )
00348     singleCheckMail( acct, _interactive );
00349   mDisplaySummary = false;
00350 }
00351 
00352 
00353 //-----------------------------------------------------------------------------
00354 void AccountManager::addToTotalNewMailCount( const QMap<QString, int> & newInFolder )
00355 {
00356   for ( QMap<QString, int>::const_iterator it = newInFolder.begin();
00357         it != newInFolder.end(); ++it ) {
00358     mTotalNewMailsArrived += it.data();
00359     if ( mTotalNewInFolder.find( it.key() ) == mTotalNewInFolder.end() )
00360       mTotalNewInFolder[it.key()] = it.data();
00361     else
00362       mTotalNewInFolder[it.key()] += it.data();
00363   }
00364 }
00365 
00366 //-----------------------------------------------------------------------------
00367 uint AccountManager::createId()
00368 {
00369   QValueList<uint> usedIds;
00370   for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
00371     usedIds << (*it)->id();
00372   }
00373 
00374   usedIds << 0; // 0 is default for unknown
00375   int newId;
00376   do
00377   {
00378     newId = kapp->random();
00379   } while ( usedIds.find(newId) != usedIds.end() );
00380 
00381   return newId;
00382 }
00383 
00384 //-----------------------------------------------------------------------------
00385 void AccountManager::cancelMailCheck()
00386 {
00387   for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
00388     (*it)->cancelMailCheck();
00389   }
00390 }
00391 
00392 
00393 //-----------------------------------------------------------------------------
00394 void AccountManager::readPasswords()
00395 {
00396   for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
00397     NetworkAccount *acct = dynamic_cast<NetworkAccount*>( (*it) );
00398     if ( acct )
00399       acct->readPassword();
00400   }
00401 }
00402 
00403 #include "accountmanager.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys