kmail Library API Documentation

kmacctmgr.cpp

00001 // KMail Account Manager
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include "kmacctmgr.h"
00008 
00009 #include "kmacctmaildir.h"
00010 #include "kmacctlocal.h"
00011 #include "kmacctexppop.h"
00012 #include "kmacctimap.h"
00013 #include "networkaccount.h"
00014 using KMail::NetworkAccount;
00015 #include "kmacctcachedimap.h"
00016 #include "kmbroadcaststatus.h"
00017 #include "kmfiltermgr.h"
00018 
00019 #include <klocale.h>
00020 #include <kmessagebox.h>
00021 #include <kdebug.h>
00022 #include <kconfig.h>
00023 
00024 #include <qregexp.h>
00025 
00026 //-----------------------------------------------------------------------------
00027 KMAcctMgr::KMAcctMgr(): QObject()
00028 {
00029   mAcctList.setAutoDelete(TRUE);
00030   mAcctChecking.clear();
00031   mAcctTodo.clear();
00032   mTotalNewMailsArrived=0;
00033 }
00034 
00035 
00036 //-----------------------------------------------------------------------------
00037 KMAcctMgr::~KMAcctMgr()
00038 {
00039   writeConfig(FALSE);
00040 }
00041 
00042 
00043 //-----------------------------------------------------------------------------
00044 void KMAcctMgr::writeConfig(bool withSync)
00045 {
00046   KConfig* config = KMKernel::config();
00047   QString groupName;
00048 
00049   KConfigGroupSaver saver(config, "General");
00050   config->writeEntry("accounts", mAcctList.count());
00051 
00052   // first delete all account groups in the config file:
00053   QStringList accountGroups =
00054     config->groupList().grep( QRegExp( "Account \\d+" ) );
00055   for ( QStringList::Iterator it = accountGroups.begin() ;
00056     it != accountGroups.end() ; ++it )
00057     config->deleteGroup( *it );
00058 
00059   // now write new account groups:
00060   int i = 1;
00061   for ( QPtrListIterator<KMAccount> it(mAcctList) ;
00062     it.current() ; ++it, ++i ) {
00063     groupName.sprintf("Account %d", i);
00064     KConfigGroupSaver saver(config, groupName);
00065     (*it)->writeConfig(*config);
00066   }
00067   if (withSync) config->sync();
00068 }
00069 
00070 
00071 //-----------------------------------------------------------------------------
00072 void KMAcctMgr::readConfig(void)
00073 {
00074   KConfig* config = KMKernel::config();
00075   KMAccount* acct;
00076   QString acctType, acctName;
00077   QCString groupName;
00078   int i, num;
00079 
00080   mAcctList.clear();
00081 
00082   KConfigGroup general(config, "General");
00083   num = general.readNumEntry("accounts", 0);
00084 
00085   for (i=1; i<=num; i++)
00086   {
00087     groupName.sprintf("Account %d", i);
00088     KConfigGroupSaver saver(config, groupName);
00089     acctType = config->readEntry("Type");
00090     // Provide backwards compatibility
00091     if (acctType == "advanced pop" || acctType == "experimental pop")
00092       acctType = "pop";
00093     acctName = config->readEntry("Name");
00094     if (acctName.isEmpty()) acctName = i18n("Account %1").arg(i);
00095     acct = create(acctType, acctName);
00096     if (!acct) continue;
00097     add(acct);
00098     acct->readConfig(*config);
00099   }
00100 }
00101 
00102 
00103 //-----------------------------------------------------------------------------
00104 void KMAcctMgr::singleCheckMail(KMAccount *account, bool _interactive)
00105 {
00106   newMailArrived = false;
00107   interactive = _interactive;
00108 
00109   // queue the account
00110   mAcctTodo.append(account);
00111 
00112   if (account->checkingMail())
00113   {
00114     kdDebug() << "account " << account->name() << " busy, queuing" << endl;
00115     return;
00116   }
00117 
00118   processNextCheck(false);
00119 }
00120 
00121 //-----------------------------------------------------------------------------
00122 void KMAcctMgr::processNextCheck(bool _newMail)
00123 {
00124   kdDebug(5006) << "processNextCheck, remaining " << mAcctTodo.count() << endl;
00125   KMAccount *curAccount = 0;
00126   newMailArrived |= _newMail;
00127 
00128   KMAccount* acct;
00129   for ( acct = mAcctChecking.first(); acct; acct = mAcctChecking.next() )
00130   {
00131     if ( !acct->checkingMail() )
00132     {
00133       // check done
00134       kdDebug(5006) << "account " << acct->name() << " finished check" << endl;
00135       mAcctChecking.removeRef( acct );
00136       kmkernel->filterMgr()->deref();
00137       disconnect( acct, SIGNAL(finishedCheck(bool)),
00138                   this, SLOT(processNextCheck(bool)) );
00139       emit checkedMail(newMailArrived, interactive);
00140     }
00141   }
00142   if (mAcctChecking.isEmpty())
00143   {
00144     // all checks finished, display summary
00145     KMBroadcastStatus::instance()->setStatusMsgTransmissionCompleted(
00146         mTotalNewMailsArrived );
00147     mTotalNewMailsArrived = 0;
00148   }
00149   if (mAcctTodo.isEmpty()) return;
00150   curAccount = mAcctTodo.take(0);
00151 
00152   if (curAccount->type() != "imap" && curAccount->type() != "cachedimap" &&
00153       curAccount->folder() == 0)
00154   {
00155     QString tmp = i18n("Account %1 has no mailbox defined!\n"
00156         "Mail checking aborted.\n"
00157         "Check your account settings!")
00158       .arg(curAccount->name());
00159     KMessageBox::information(0,tmp);
00160     emit checkedMail(false, interactive);
00161     mTotalNewMailsArrived = 0;
00162     return;
00163   }
00164 
00165   connect( curAccount, SIGNAL(finishedCheck(bool)),
00166        this, SLOT(processNextCheck(bool)) );
00167 
00168   KMBroadcastStatus::instance()->setStatusMsg(
00169       i18n("Checking account %1 for new mail").arg(curAccount->name()));
00170 
00171   NetworkAccount *nacct = dynamic_cast<NetworkAccount*>( curAccount );
00172   //If it a NetworkAccount we want to show the user whether the
00173   //connection is encrypted. If it's a local account we don't want
00174   //to put a useless icon in the statusbar and ignore it.
00175   if ( nacct ) {
00176     KMBroadcastStatus::instance()->setUsingSSL( nacct->useSSL() ||
00177                                                 nacct->useTLS() );
00178   }
00179   kdDebug(5006) << "processing next mail check for " << curAccount->name() << endl;
00180 
00181   curAccount->setCheckingMail(true);
00182   mAcctChecking.append(curAccount);
00183   kmkernel->filterMgr()->ref();
00184   curAccount->processNewMail(interactive);
00185 }
00186 
00187 //-----------------------------------------------------------------------------
00188 KMAccount* KMAcctMgr::create(const QString &aType, const QString &aName)
00189 {
00190   KMAccount* act = 0;
00191 
00192   if (aType == "local")
00193     act = new KMAcctLocal(this, aName);
00194 
00195   if (aType == "maildir")
00196     act = new KMAcctMaildir(this, aName);
00197 
00198   else if (aType == "pop")
00199     act = new KMAcctExpPop(this, aName);
00200 
00201   else if (aType == "imap")
00202     act = new KMAcctImap(this, aName);
00203 
00204   else if (aType == "cachedimap")
00205     act = new KMAcctCachedImap(this, aName);
00206 
00207   if (act)
00208   {
00209     act->setFolder(kmkernel->inboxFolder());
00210     connect( act, SIGNAL(newMailsProcessed(int)),
00211     this, SLOT(addToTotalNewMailCount(int)) );
00212   }
00213 
00214   return act;
00215 }
00216 
00217 
00218 //-----------------------------------------------------------------------------
00219 void KMAcctMgr::add(KMAccount *account)
00220 {
00221   if (account)
00222     mAcctList.append(account);
00223 }
00224 
00225 
00226 //-----------------------------------------------------------------------------
00227 KMAccount* KMAcctMgr::find(const QString &aName)
00228 {
00229   KMAccount* cur;
00230 
00231   if (aName.isEmpty()) return 0;
00232 
00233   for (cur=mAcctList.first(); cur; cur=mAcctList.next())
00234   {
00235     if (cur->name() == aName) return cur;
00236   }
00237 
00238   return 0;
00239 }
00240 
00241 
00242 //-----------------------------------------------------------------------------
00243 KMAccount* KMAcctMgr::first(void)
00244 {
00245   return mAcctList.first();
00246 }
00247 
00248 
00249 //-----------------------------------------------------------------------------
00250 KMAccount* KMAcctMgr::next(void)
00251 {
00252   return mAcctList.next();
00253 }
00254 
00255 
00256 //-----------------------------------------------------------------------------
00257 bool KMAcctMgr::remove( KMAccount* acct )
00258 {
00259   //assert(acct != 0);
00260   if( !acct )
00261     return false;
00262   mAcctList.removeRef( acct );
00263   emit accountRemoved( acct );
00264   return true;
00265 }
00266 
00267 //-----------------------------------------------------------------------------
00268 void KMAcctMgr::checkMail(bool _interactive)
00269 {
00270   newMailArrived = false;
00271 
00272   if (mAcctList.isEmpty())
00273   {
00274     KMessageBox::information(0,i18n("You need to add an account in the network "
00275                     "section of the settings in order to "
00276                     "receive mail."));
00277     return;
00278   }
00279 
00280   mTotalNewMailsArrived=0;
00281 
00282   for ( QPtrListIterator<KMAccount> it(mAcctList) ;
00283     it.current() ; ++it )
00284   {
00285     if (!it.current()->checkExclude())
00286       singleCheckMail(it.current(), _interactive);
00287   }
00288 }
00289 
00290 
00291 //-----------------------------------------------------------------------------
00292 void KMAcctMgr::singleInvalidateIMAPFolders(KMAccount *account) {
00293   account->invalidateIMAPFolders();
00294 }
00295 
00296 
00297 void KMAcctMgr::invalidateIMAPFolders()
00298 {
00299   if (mAcctList.isEmpty()) {
00300     KMessageBox::information(0,i18n("You need to add an account in the network "
00301                                    "section of the settings in order to "
00302                                    "receive mail."));
00303     return;
00304   }
00305 
00306   for ( QPtrListIterator<KMAccount> it(mAcctList) ; it.current() ; ++it )
00307     singleInvalidateIMAPFolders(it.current());
00308 }
00309 
00310 
00311 //-----------------------------------------------------------------------------
00312 QStringList  KMAcctMgr::getAccounts(bool noImap) {
00313 
00314   KMAccount *cur;
00315   QStringList strList;
00316   for (cur=mAcctList.first(); cur; cur=mAcctList.next()) {
00317     if (!noImap || cur->type() != "imap") strList.append(cur->name());
00318   }
00319 
00320   return strList;
00321 
00322 }
00323 
00324 //-----------------------------------------------------------------------------
00325 void KMAcctMgr::intCheckMail(int item, bool _interactive)
00326 {
00327   KMAccount* cur;
00328   newMailArrived = false;
00329 
00330   mTotalNewMailsArrived = 0;
00331   int x = 0;
00332   cur = mAcctList.first();
00333   while (cur)
00334   {
00335     x++;
00336     if (x > item) break;
00337     cur=mAcctList.next();
00338   }
00339 
00340   singleCheckMail(cur, _interactive);
00341 }
00342 
00343 
00344 //-----------------------------------------------------------------------------
00345 void KMAcctMgr::addToTotalNewMailCount(int newmails)
00346 {
00347   if ( newmails == -1 ) mTotalNewMailsArrived = -1;
00348   if ( mTotalNewMailsArrived == -1 ) return;
00349   mTotalNewMailsArrived += newmails;
00350 }
00351 
00352 //-----------------------------------------------------------------------------
00353 #include "kmacctmgr.moc"
KDE Logo
This file is part of the documentation for kmail Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:37:19 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003