00001
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
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
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
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
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 mNewMailArrived |= _newMail;
00133
00134 for ( AccountList::Iterator it( mAcctChecking.begin() ), end( mAcctChecking.end() ); it != end; ) {
00135 KMAccount* acct = *it;
00136 ++it;
00137 if ( acct->checkingMail() )
00138 continue;
00139
00140 kdDebug(5006) << "account " << acct->name() << " finished check" << endl;
00141 mAcctChecking.remove( acct );
00142 kmkernel->filterMgr()->deref();
00143 disconnect( acct, SIGNAL( finishedCheck( bool, CheckStatus ) ),
00144 this, SLOT( processNextCheck( bool ) ) );
00145 }
00146 if ( mAcctChecking.isEmpty() ) {
00147
00148 if ( mDisplaySummary )
00149 KPIM::BroadcastStatus::instance()->setStatusMsgTransmissionCompleted(
00150 mTotalNewMailsArrived );
00151 emit checkedMail( mNewMailArrived, mInteractive, mTotalNewInFolder );
00152 mTotalNewMailsArrived = 0;
00153 mTotalNewInFolder.clear();
00154 mDisplaySummary = false;
00155 }
00156 if ( mAcctTodo.isEmpty() ) return;
00157
00158 QString accountHostName;
00159
00160 KMAccount *curAccount = 0;
00161 for ( AccountList::Iterator it ( mAcctTodo.begin() ), last ( mAcctTodo.end() ); it != last; ) {
00162 KMAccount *acct = *it;
00163 ++it;
00164 if ( !acct->checkingMail() && acct->mailCheckCanProceed() ) {
00165 curAccount = acct;
00166 mAcctTodo.remove( acct );
00167 break;
00168 }
00169 }
00170 if ( !curAccount ) return;
00171
00172 if ( curAccount->type() != "imap" && curAccount->type() != "cachedimap" &&
00173 curAccount->folder() == 0 ) {
00174 QString tmp = i18n("Account %1 has no mailbox defined:\n"
00175 "mail checking aborted;\n"
00176 "check your account settings.")
00177 .arg(curAccount->name());
00178 KMessageBox::information(0,tmp);
00179 emit checkedMail( false, mInteractive, mTotalNewInFolder );
00180 mTotalNewMailsArrived = 0;
00181 mTotalNewInFolder.clear();
00182 return;
00183 }
00184
00185 connect( curAccount, SIGNAL( finishedCheck( bool, CheckStatus ) ),
00186 this, SLOT( processNextCheck( bool ) ) );
00187
00188 KPIM::BroadcastStatus::instance()->setStatusMsg(
00189 i18n("Checking account %1 for new mail").arg(curAccount->name()));
00190
00191 kdDebug(5006) << "processing next mail check for " << curAccount->name() << endl;
00192
00193 curAccount->setCheckingMail( true );
00194 mAcctChecking.append( curAccount );
00195 kmkernel->filterMgr()->ref();
00196 curAccount->processNewMail( mInteractive );
00197 }
00198
00199
00200 KMAccount* AccountManager::create( const QString &aType, const QString &aName, uint id )
00201 {
00202 KMAccount* act = 0;
00203 if ( id == 0 )
00204 id = createId();
00205
00206 if ( aType == "local" ) {
00207 act = new KMAcctLocal(this, aName.isEmpty() ? i18n("Local Account") : aName, id);
00208 act->setFolder( kmkernel->inboxFolder() );
00209 } else if ( aType == "maildir" ) {
00210 act = new KMAcctMaildir(this, aName.isEmpty() ? i18n("Local Account") : aName, id);
00211 act->setFolder( kmkernel->inboxFolder() );
00212 } else if ( aType == "pop" ) {
00213 act = new KMail::PopAccount(this, aName.isEmpty() ? i18n("POP Account") : aName, id);
00214 act->setFolder( kmkernel->inboxFolder() );
00215 } else if ( aType == "imap" ) {
00216 act = new KMAcctImap(this, aName.isEmpty() ? i18n("IMAP Account") : aName, id);
00217 } else if (aType == "cachedimap") {
00218 act = new KMAcctCachedImap(this, aName.isEmpty() ? i18n("IMAP Account") : aName, id);
00219 }
00220 if ( !act ) {
00221 kdWarning(5006) << "Attempt to instantiate a non-existing account type!" << endl;
00222 return 0;
00223 }
00224 connect( act, SIGNAL( newMailsProcessed( const QMap<QString, int> & ) ),
00225 this, SLOT( addToTotalNewMailCount( const QMap<QString, int> & ) ) );
00226 return act;
00227 }
00228
00229
00230
00231 void AccountManager::add( KMAccount *account )
00232 {
00233 if ( account ) {
00234 mAcctList.append( account );
00235 emit accountAdded( account );
00236 account->installTimer();
00237 }
00238 }
00239
00240
00241
00242 KMAccount* AccountManager::findByName(const QString &aName) const
00243 {
00244 if ( aName.isEmpty() ) return 0;
00245
00246 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
00247 if ( (*it)->name() == aName ) return (*it);
00248 }
00249 return 0;
00250 }
00251
00252
00253
00254 KMAccount* AccountManager::find( const uint id ) const
00255 {
00256 if (id == 0) return 0;
00257 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
00258 if ( (*it)->id() == id ) return (*it);
00259 }
00260 return 0;
00261 }
00262
00263
00264
00265 KMAccount* AccountManager::first()
00266 {
00267 if ( !mAcctList.empty() ) {
00268 mPtrListInterfaceProxyIterator = mAcctList.begin();
00269 return *mPtrListInterfaceProxyIterator;
00270 } else {
00271 return 0;
00272 }
00273 }
00274
00275
00276 KMAccount* AccountManager::next()
00277 {
00278 ++mPtrListInterfaceProxyIterator;
00279 if ( mPtrListInterfaceProxyIterator == mAcctList.end() )
00280 return 0;
00281 else
00282 return *mPtrListInterfaceProxyIterator;
00283 }
00284
00285
00286 bool AccountManager::remove( KMAccount* acct )
00287 {
00288 if( !acct )
00289 return false;
00290 mAcctList.remove( acct );
00291 emit accountRemoved( acct );
00292 return true;
00293 }
00294
00295
00296 void AccountManager::checkMail( bool _interactive )
00297 {
00298 mNewMailArrived = false;
00299
00300 if ( mAcctList.isEmpty() ) {
00301 KMessageBox::information( 0,i18n("You need to add an account in the network "
00302 "section of the settings in order to receive mail.") );
00303 return;
00304 }
00305 mDisplaySummary = true;
00306
00307 mTotalNewMailsArrived=0;
00308 mTotalNewInFolder.clear();
00309
00310 for ( AccountList::Iterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
00311 if ( !(*it)->checkExclude() )
00312 singleCheckMail( (*it), _interactive);
00313 }
00314 }
00315
00316
00317
00318 void AccountManager::singleInvalidateIMAPFolders(KMAccount *account) {
00319 account->invalidateIMAPFolders();
00320 }
00321
00322
00323 void AccountManager::invalidateIMAPFolders()
00324 {
00325 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it )
00326 singleInvalidateIMAPFolders( *it );
00327 }
00328
00329
00330
00331 QStringList AccountManager::getAccounts() const
00332 {
00333 QStringList strList;
00334 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
00335 strList.append( (*it)->name() );
00336 }
00337 return strList;
00338 }
00339
00340
00341 void AccountManager::intCheckMail(int item, bool _interactive)
00342 {
00343 mNewMailArrived = false;
00344 mTotalNewMailsArrived = 0;
00345 mTotalNewInFolder.clear();
00346 if ( KMAccount *acct = mAcctList[ item ] )
00347 singleCheckMail( acct, _interactive );
00348 mDisplaySummary = false;
00349 }
00350
00351
00352
00353 void AccountManager::addToTotalNewMailCount( const QMap<QString, int> & newInFolder )
00354 {
00355 for ( QMap<QString, int>::const_iterator it = newInFolder.begin();
00356 it != newInFolder.end(); ++it ) {
00357 mTotalNewMailsArrived += it.data();
00358 if ( mTotalNewInFolder.find( it.key() ) == mTotalNewInFolder.end() )
00359 mTotalNewInFolder[it.key()] = it.data();
00360 else
00361 mTotalNewInFolder[it.key()] += it.data();
00362 }
00363 }
00364
00365
00366 uint AccountManager::createId()
00367 {
00368 QValueList<uint> usedIds;
00369 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
00370 usedIds << (*it)->id();
00371 }
00372
00373 usedIds << 0;
00374 int newId;
00375 do
00376 {
00377 newId = kapp->random();
00378 } while ( usedIds.find(newId) != usedIds.end() );
00379
00380 return newId;
00381 }
00382
00383
00384 void AccountManager::cancelMailCheck()
00385 {
00386 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
00387 (*it)->cancelMailCheck();
00388 }
00389 }
00390
00391
00392
00393 void AccountManager::readPasswords()
00394 {
00395 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
00396 NetworkAccount *acct = dynamic_cast<NetworkAccount*>( (*it) );
00397 if ( acct )
00398 acct->readPassword();
00399 }
00400 }
00401
00402 #include "accountmanager.moc"