1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
// KMail Account Manager
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "accountmanager.h"
#include "kmaccount.h"
#include "kmacctmaildir.h"
#include "kmacctlocal.h"
#include "popaccount.h"
#include "kmacctimap.h"
#include "networkaccount.h"
#include "kmacctcachedimap.h"
#include "broadcaststatus.h"
#include "kmfiltermgr.h"
#include "globalsettings.h"
#include <klocale.h>
#include <kmessagebox.h>
#include <kdebug.h>
#include <kconfig.h>
#include <kapplication.h>
#include <qregexp.h>
#include <qvaluelist.h>
using namespace KMail;
//-----------------------------------------------------------------------------
AccountManager::AccountManager()
:QObject(), mNewMailArrived( false ), mInteractive( false ),
mTotalNewMailsArrived( 0 ), mDisplaySummary( false )
{
mAcctChecking.clear();
mAcctTodo.clear();
}
//-----------------------------------------------------------------------------
AccountManager::~AccountManager()
{
writeConfig( false );
}
//-----------------------------------------------------------------------------
void AccountManager::writeConfig( bool withSync )
{
KConfig* config = KMKernel::config();
QString groupName;
KConfigGroupSaver saver(config, "General");
config->writeEntry("accounts", mAcctList.count());
// first delete all account groups in the config file:
QStringList accountGroups =
config->groupList().grep( QRegExp( "Account \\d+" ) );
for ( QStringList::Iterator it = accountGroups.begin() ;
it != accountGroups.end() ; ++it )
config->deleteGroup( *it );
// now write new account groups:
int i = 1;
for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it, ++i ) {
groupName.sprintf("Account %d", i);
KConfigGroupSaver saver(config, groupName);
(*it)->writeConfig(*config);
}
if (withSync) config->sync();
}
//-----------------------------------------------------------------------------
void AccountManager::readConfig(void)
{
KConfig* config = KMKernel::config();
KMAccount* acct;
QString acctType, acctName;
QCString groupName;
int i, num;
uint id;
for ( AccountList::Iterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it )
delete *it;
mAcctList.clear();
KConfigGroup general(config, "General");
num = general.readNumEntry("accounts", 0);
for (i=1; i<=num; i++)
{
groupName.sprintf("Account %d", i);
KConfigGroupSaver saver(config, groupName);
acctType = config->readEntry("Type");
// Provide backwards compatibility
if (acctType == "advanced pop" || acctType == "experimental pop")
acctType = "pop";
acctName = config->readEntry("Name");
id = config->readUnsignedNumEntry("Id", 0);
if (acctName.isEmpty()) acctName = i18n("Account %1").arg(i);
acct = create(acctType, acctName, id);
if (!acct) continue;
add(acct);
acct->readConfig(*config);
}
}
//-----------------------------------------------------------------------------
void AccountManager::singleCheckMail(KMAccount *account, bool interactive)
{
mNewMailArrived = false;
mInteractive = interactive;
// queue the account
mAcctTodo.append(account);
if (account->checkingMail())
{
kdDebug(5006) << "account " << account->name() << " busy, queuing" << endl;
return;
}
processNextCheck(false);
}
//-----------------------------------------------------------------------------
void AccountManager::processNextCheck( bool _newMail )
{
kdDebug(5006) << "processNextCheck, remaining " << mAcctTodo.count() << endl;
if ( _newMail )
mNewMailArrived = true;
for ( AccountList::Iterator it( mAcctChecking.begin() ), end( mAcctChecking.end() ); it != end; ) {
KMAccount* acct = *it;
++it;
if ( acct->checkingMail() )
continue;
// check done
kdDebug(5006) << "account " << acct->name() << " finished check" << endl;
mAcctChecking.remove( acct );
kmkernel->filterMgr()->deref();
disconnect( acct, SIGNAL( finishedCheck( bool, CheckStatus ) ),
this, SLOT( processNextCheck( bool ) ) );
}
if ( mAcctChecking.isEmpty() ) {
// all checks finished, display summary
if ( mDisplaySummary )
KPIM::BroadcastStatus::instance()->setStatusMsgTransmissionCompleted(
mTotalNewMailsArrived );
emit checkedMail( mNewMailArrived, mInteractive, mTotalNewInFolder );
mTotalNewMailsArrived = 0;
mTotalNewInFolder.clear();
mDisplaySummary = false;
}
if ( mAcctTodo.isEmpty() ) return;
QString accountHostName;
KMAccount *curAccount = 0;
for ( AccountList::Iterator it ( mAcctTodo.begin() ), last ( mAcctTodo.end() ); it != last; ) {
KMAccount *acct = *it;
++it;
if ( !acct->checkingMail() && acct->mailCheckCanProceed() ) {
curAccount = acct;
mAcctTodo.remove( acct );
break;
}
}
if ( !curAccount ) return; // no account or all of them are already checking
if ( curAccount->type() != "imap" && curAccount->type() != "cachedimap" &&
curAccount->folder() == 0 ) {
QString tmp = i18n("Account %1 has no mailbox defined:\n"
"mail checking aborted;\n"
"check your account settings.")
.arg(curAccount->name());
KMessageBox::information(0,tmp);
emit checkedMail( false, mInteractive, mTotalNewInFolder );
mTotalNewMailsArrived = 0;
mTotalNewInFolder.clear();
return;
}
connect( curAccount, SIGNAL( finishedCheck( bool, CheckStatus ) ),
this, SLOT( processNextCheck( bool ) ) );
KPIM::BroadcastStatus::instance()->setStatusMsg(
i18n("Checking account %1 for new mail").arg(curAccount->name()));
kdDebug(5006) << "processing next mail check for " << curAccount->name() << endl;
curAccount->setCheckingMail( true );
mAcctChecking.append( curAccount );
kmkernel->filterMgr()->ref();
curAccount->processNewMail( mInteractive );
}
//-----------------------------------------------------------------------------
KMAccount* AccountManager::create( const QString &aType, const QString &aName, uint id )
{
KMAccount* act = 0;
if ( id == 0 )
id = createId();
if ( aType == "local" ) {
act = new KMAcctLocal(this, aName.isEmpty() ? i18n("Local Account") : aName, id);
act->setFolder( kmkernel->inboxFolder() );
} else if ( aType == "maildir" ) {
act = new KMAcctMaildir(this, aName.isEmpty() ? i18n("Local Account") : aName, id);
act->setFolder( kmkernel->inboxFolder() );
} else if ( aType == "pop" ) {
act = new KMail::PopAccount(this, aName.isEmpty() ? i18n("POP Account") : aName, id);
act->setFolder( kmkernel->inboxFolder() );
} else if ( aType == "imap" ) {
act = new KMAcctImap(this, aName.isEmpty() ? i18n("IMAP Account") : aName, id);
} else if (aType == "cachedimap") {
act = new KMAcctCachedImap(this, aName.isEmpty() ? i18n("IMAP Account") : aName, id);
}
if ( !act ) {
kdWarning(5006) << "Attempt to instantiate a non-existing account type!" << endl;
return 0;
}
connect( act, SIGNAL( newMailsProcessed( const QMap<QString, int> & ) ),
this, SLOT( addToTotalNewMailCount( const QMap<QString, int> & ) ) );
return act;
}
//-----------------------------------------------------------------------------
void AccountManager::add( KMAccount *account )
{
if ( account ) {
mAcctList.append( account );
emit accountAdded( account );
account->installTimer();
}
}
//-----------------------------------------------------------------------------
KMAccount* AccountManager::findByName(const QString &aName) const
{
if ( aName.isEmpty() ) return 0;
for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
if ( (*it)->name() == aName ) return (*it);
}
return 0;
}
//-----------------------------------------------------------------------------
KMAccount* AccountManager::find( const uint id ) const
{
if (id == 0) return 0;
for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
if ( (*it)->id() == id ) return (*it);
}
return 0;
}
//-----------------------------------------------------------------------------
KMAccount* AccountManager::first()
{
if ( !mAcctList.empty() ) {
mPtrListInterfaceProxyIterator = mAcctList.begin();
return *mPtrListInterfaceProxyIterator;
} else {
return 0;
}
}
//-----------------------------------------------------------------------------
KMAccount* AccountManager::next()
{
++mPtrListInterfaceProxyIterator;
if ( mPtrListInterfaceProxyIterator == mAcctList.end() )
return 0;
else
return *mPtrListInterfaceProxyIterator;
}
//-----------------------------------------------------------------------------
bool AccountManager::remove( KMAccount* acct )
{
if( !acct )
return false;
mAcctList.remove( acct );
emit accountRemoved( acct );
return true;
}
//-----------------------------------------------------------------------------
void AccountManager::checkMail( bool _interactive )
{
mNewMailArrived = false;
if ( mAcctList.isEmpty() ) {
KMessageBox::information( 0,i18n("You need to add an account in the network "
"section of the settings in order to receive mail.") );
return;
}
mDisplaySummary = true;
mTotalNewMailsArrived=0;
mTotalNewInFolder.clear();
for ( AccountList::Iterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
if ( !(*it)->checkExclude() )
singleCheckMail( (*it), _interactive);
}
}
//-----------------------------------------------------------------------------
void AccountManager::singleInvalidateIMAPFolders(KMAccount *account) {
account->invalidateIMAPFolders();
}
void AccountManager::invalidateIMAPFolders()
{
for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it )
singleInvalidateIMAPFolders( *it );
}
//-----------------------------------------------------------------------------
QStringList AccountManager::getAccounts() const
{
QStringList strList;
for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
strList.append( (*it)->name() );
}
return strList;
}
//-----------------------------------------------------------------------------
void AccountManager::intCheckMail(int item, bool _interactive)
{
mNewMailArrived = false;
mTotalNewMailsArrived = 0;
mTotalNewInFolder.clear();
if ( KMAccount *acct = mAcctList[ item ] )
singleCheckMail( acct, _interactive );
mDisplaySummary = false;
}
//-----------------------------------------------------------------------------
void AccountManager::addToTotalNewMailCount( const QMap<QString, int> & newInFolder )
{
for ( QMap<QString, int>::const_iterator it = newInFolder.begin();
it != newInFolder.end(); ++it ) {
mTotalNewMailsArrived += it.data();
if ( mTotalNewInFolder.find( it.key() ) == mTotalNewInFolder.end() )
mTotalNewInFolder[it.key()] = it.data();
else
mTotalNewInFolder[it.key()] += it.data();
}
}
//-----------------------------------------------------------------------------
uint AccountManager::createId()
{
QValueList<uint> usedIds;
for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
usedIds << (*it)->id();
}
usedIds << 0; // 0 is default for unknown
int newId;
do
{
newId = kapp->random();
} while ( usedIds.find(newId) != usedIds.end() );
return newId;
}
//-----------------------------------------------------------------------------
void AccountManager::cancelMailCheck()
{
for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
(*it)->cancelMailCheck();
}
}
//-----------------------------------------------------------------------------
void AccountManager::readPasswords()
{
for ( AccountList::ConstIterator it( mAcctList.begin() ), end( mAcctList.end() ); it != end; ++it ) {
NetworkAccount *acct = dynamic_cast<NetworkAccount*>( (*it) );
if ( acct )
acct->readPassword();
}
}
#include "accountmanager.moc"
|