7 #include "accountmanager.h" 10 #include "kmacctfolder.h" 11 #include "kmacctmaildir.h" 12 #include "kmacctlocal.h" 13 #include "popaccount.h" 14 #include "kmacctimap.h" 15 #include "networkaccount.h" 16 #include "kmacctcachedimap.h" 17 #include "broadcaststatus.h" 18 #include "kmfiltermgr.h" 19 #include "globalsettings.h" 21 #include <dcopclient.h> 22 #include <tdelocale.h> 23 #include <tdemessagebox.h> 25 #include <tdeconfig.h> 26 #include <tdeapplication.h> 29 #include <tqvaluelist.h> 31 using namespace KMail;
35 :TQObject(), mNewMailArrived( false ), mInteractive( false ),
36 mTotalNewMailsArrived( 0 ), mDisplaySummary( false )
38 mAcctChecking.clear();
43 AccountManager::~AccountManager()
52 TDEConfig* config = KMKernel::config();
55 TDEConfigGroupSaver saver(config,
"General");
56 config->writeEntry(
"accounts", mAcctList.count());
59 TQStringList accountGroups =
60 config->groupList().grep( TQRegExp(
"Account \\d+" ) );
61 for ( TQStringList::Iterator it = accountGroups.begin() ;
62 it != accountGroups.end() ; ++it )
63 config->deleteGroup( *it );
67 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it, ++i ) {
68 groupName.sprintf(
"Account %d", i);
69 TDEConfigGroupSaver saver(config, groupName);
70 (*it)->writeConfig(*config);
72 if (withSync) config->sync();
79 TDEConfig* config = KMKernel::config();
81 TQString acctType, acctName;
86 for ( AccountList::Iterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it )
90 TDEConfigGroup general(config,
"General");
91 num = general.readNumEntry(
"accounts", 0);
93 for (i=1; i<=num; i++)
95 groupName.sprintf(
"Account %d", i);
96 TDEConfigGroupSaver saver(config, groupName);
97 acctType = config->readEntry(
"Type");
99 if (acctType ==
"advanced pop" || acctType ==
"experimental pop")
101 acctName = config->readEntry(
"Name");
102 id = config->readUnsignedNumEntry(
"Id", 0);
103 if (acctName.isEmpty()) acctName = i18n(
"Account %1").arg(i);
104 acct =
create(acctType, acctName,
id);
107 acct->readConfig(*config);
113 void AccountManager::singleCheckMail(KMAccount *account,
bool interactive)
115 mNewMailArrived =
false;
116 mInteractive = interactive;
122 account->readTimerConfig();
125 mAcctTodo.append(account);
127 if (account->checkingMail())
129 kdDebug(5006) <<
"account " << account->name() <<
" busy, queuing" << endl;
133 processNextCheck(
false);
137 void AccountManager::processNextCheck(
bool _newMail )
139 kdDebug(5006) <<
"processNextCheck, remaining " << mAcctTodo.count() << endl;
141 mNewMailArrived =
true;
143 for ( AccountList::Iterator it( mAcctChecking.begin() ), end( mAcctChecking.end() ); it != end; ) {
144 KMAccount* acct = *it;
146 if ( acct->checkingMail() )
149 kdDebug(5006) <<
"account " << acct->name() <<
" finished check" << endl;
150 mAcctChecking.remove( acct );
151 kmkernel->filterMgr()->deref();
152 disconnect( acct, TQ_SIGNAL( finishedCheck(
bool, CheckStatus ) ),
153 this, TQ_SLOT( processNextCheck(
bool ) ) );
155 if ( mAcctChecking.isEmpty() ) {
157 if ( mDisplaySummary )
158 KPIM::BroadcastStatus::instance()->setStatusMsgTransmissionCompleted(
159 mTotalNewMailsArrived );
160 emit
checkedMail( mNewMailArrived, mInteractive, mTotalNewInFolder );
161 mTotalNewMailsArrived = 0;
162 mTotalNewInFolder.clear();
163 mDisplaySummary =
false;
165 if ( mAcctTodo.isEmpty() )
return;
167 TQString accountHostName;
169 KMAccount *curAccount = 0;
170 for ( AccountList::Iterator it ( mAcctTodo.begin() ), last ( mAcctTodo.end() ); it != last; ) {
171 KMAccount *acct = *it;
173 if ( !acct->checkingMail() && acct->mailCheckCanProceed() ) {
175 mAcctTodo.remove( acct );
179 if ( !curAccount )
return;
181 if ( curAccount->type() !=
"imap" && curAccount->type() !=
"cachedimap" &&
182 curAccount->folder() == 0 ) {
183 TQString tmp = i18n(
"Account %1 has no mailbox defined:\n" 184 "mail checking aborted;\n" 185 "check your account settings.")
186 .arg(curAccount->name());
187 KMessageBox::information(0,tmp);
188 emit
checkedMail(
false, mInteractive, mTotalNewInFolder );
189 mTotalNewMailsArrived = 0;
190 mTotalNewInFolder.clear();
194 if ( curAccount->type() ==
"imap" || curAccount->type() ==
"cachedimap" || curAccount->type() ==
"pop" )
197 const int NetWorkStatusUnknown = 1;
198 const int NetWorkStatusOnline = 8;
203 TQDataStream stream( params, IO_WriteOnly );
204 stream << static_cast<NetworkAccount*>( curAccount )->host();
206 if ( kapp->dcopClient()->call(
"kded",
"networkstatus",
"status(TQString)",
207 params, replyType, reply ) && ( replyType ==
"int" ) )
210 TQDataStream stream2( reply, IO_ReadOnly );
212 kdDebug() << k_funcinfo <<
"networkstatus status = " << result << endl;
214 if ( ( result != NetWorkStatusUnknown ) && ( result != NetWorkStatusOnline ) )
216 emit
checkedMail(
false, mInteractive, mTotalNewInFolder );
222 connect( curAccount, TQ_SIGNAL( finishedCheck(
bool, CheckStatus ) ),
223 this, TQ_SLOT( processNextCheck(
bool ) ) );
225 KPIM::BroadcastStatus::instance()->setStatusMsg(
226 i18n(
"Checking account %1 for new mail").arg(curAccount->name()));
228 kdDebug(5006) <<
"processing next mail check for " << curAccount->name() << endl;
230 curAccount->setCheckingMail(
true );
231 mAcctChecking.append( curAccount );
232 kmkernel->filterMgr()->ref();
233 curAccount->processNewMail( mInteractive );
243 if ( aType ==
"local" ) {
244 act =
new KMAcctLocal(
this, aName.isEmpty() ? i18n(
"Local Account") : aName, id);
245 act->setFolder( kmkernel->inboxFolder() );
246 }
else if ( aType ==
"maildir" ) {
247 act =
new KMAcctMaildir(
this, aName.isEmpty() ? i18n(
"Local Account") : aName, id);
248 act->setFolder( kmkernel->inboxFolder() );
249 }
else if ( aType ==
"pop" ) {
250 act =
new KMail::PopAccount(
this, aName.isEmpty() ? i18n(
"POP Account") : aName, id);
251 act->setFolder( kmkernel->inboxFolder() );
252 }
else if ( aType ==
"imap" ) {
253 act =
new KMAcctImap(
this, aName.isEmpty() ? i18n(
"IMAP Account") : aName, id);
254 }
else if (aType ==
"cachedimap") {
255 act =
new KMAcctCachedImap(
this, aName.isEmpty() ? i18n(
"IMAP Account") : aName, id);
258 kdWarning(5006) <<
"Attempt to instantiate a non-existing account type!" << endl;
261 connect( act, TQ_SIGNAL( newMailsProcessed(
const TQMap<TQString, int> & ) ),
271 mAcctList.append( account );
278 account->installTimer();
286 if ( aName.isEmpty() )
return 0;
288 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
289 if ( (*it)->name() == aName )
return (*it);
298 if (
id == 0)
return 0;
299 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
300 if ( (*it)->id() == id )
return (*it);
309 if ( !mAcctList.empty() ) {
310 mPtrListInterfaceProxyIterator = mAcctList.begin();
311 return *mPtrListInterfaceProxyIterator;
320 ++mPtrListInterfaceProxyIterator;
321 if ( mPtrListInterfaceProxyIterator == mAcctList.end() )
324 return *mPtrListInterfaceProxyIterator;
332 mAcctList.remove( acct );
340 mNewMailArrived =
false;
342 if ( mAcctList.isEmpty() ) {
343 KMessageBox::information( 0,i18n(
"You need to add an account in the network " 344 "section of the settings in order to receive mail.") );
347 mDisplaySummary =
true;
349 mTotalNewMailsArrived=0;
350 mTotalNewInFolder.clear();
352 for ( AccountList::Iterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
353 if ( !(*it)->checkExclude() )
354 singleCheckMail( (*it), _interactive);
360 void AccountManager::singleInvalidateIMAPFolders(KMAccount *account) {
361 account->invalidateIMAPFolders();
367 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it )
368 singleInvalidateIMAPFolders( *it );
373 TQStringList AccountManager::getAccounts()
const 375 TQStringList strList;
376 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
377 strList.append( (*it)->name() );
383 void AccountManager::intCheckMail(
int item,
bool _interactive)
385 mNewMailArrived =
false;
386 mTotalNewMailsArrived = 0;
387 mTotalNewInFolder.clear();
388 if ( KMAccount *acct = mAcctList[ item ] )
389 singleCheckMail( acct, _interactive );
390 mDisplaySummary =
false;
397 for ( TQMap<TQString, int>::const_iterator it = newInFolder.begin();
398 it != newInFolder.end(); ++it ) {
399 mTotalNewMailsArrived += it.data();
400 if ( mTotalNewInFolder.find( it.key() ) == mTotalNewInFolder.end() )
401 mTotalNewInFolder[it.key()] = it.data();
403 mTotalNewInFolder[it.key()] += it.data();
408 uint AccountManager::createId()
410 TQValueList<uint> usedIds;
411 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
412 usedIds << (*it)->id();
419 newId = kapp->random();
420 }
while ( usedIds.find(newId) != usedIds.end() );
428 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
429 (*it)->cancelMailCheck();
437 for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
438 NetworkAccount *acct =
dynamic_cast<NetworkAccount*
>( (*it) );
440 acct->readPassword();
444 #include "accountmanager.moc" void add(KMAccount *account)
Adds an account to the list of accounts.
KMAccount * create(const TQString &type, const TQString &name=TQString(), uint id=0)
Create a new account of given type with given name.
Simple wrapper class that contains the kmail account handling stuff that is usually not required outs...
void addToTotalNewMailCount(const TQMap< TQString, int > &newInFolder)
this slot increases the count of new mails to show a total number after checking in multiple accounts...
const KMAccount * next() const
Next account of the list.
void addAccount(KMAccount *)
Add given account to the list.
KMAccount * findByName(const TQString &name) const
Find account by name.
void readPasswords()
Read passwords of all accounts from the wallet.
void checkMail(bool interactive=true)
Processes all accounts looking for new mail.
void invalidateIMAPFolders()
Delete all IMAP folders and resync them.
bool hasAccounts() const
Returns TRUE if accounts are associated with this folder.
AccountManager()
Initializes the account manager.
void accountAdded(KMAccount *account)
emitted when an account is added
void cancelMailCheck()
Called on exit (KMMainWin::queryExit)
bool remove(KMAccount *)
Physically remove account.
void writeConfig(bool withSync=true)
Write accounts to config.
void readConfig(void)
Completely reload accounts from config.
KMAccount * find(const uint id) const
Find account by id.
void checkedMail(bool newMail, bool interactive, const TQMap< TQString, int > &newInFolder)
Emitted if new mail has been collected.
const KMAccount * first() const
First account of the list.
void accountRemoved(KMAccount *account)
emitted when an account is removed
KMail account for pop mail account.