kmail Library API Documentation

kmaccount.cpp

00001 // KMail Account
00002 #include <config.h>
00003 
00004 #include "kmaccount.h"
00005 
00006 #include "kmacctmgr.h"
00007 #include "kmacctfolder.h"
00008 #include "kmfoldermgr.h"
00009 #include "kmfiltermgr.h"
00010 #include "kmsender.h"
00011 #include "kmkernel.h"
00012 #include "kmbroadcaststatus.h"
00013 #include "kmfoldercachedimap.h"
00014 
00015 using KMail::FolderJob;
00016 
00017 #include <kapplication.h>
00018 #include <klocale.h>
00019 #include <kmessagebox.h>
00020 #include <kdebug.h>
00021 #include <kconfig.h>
00022 
00023 #include <qeventloop.h>
00024 
00025 #include <stdlib.h>
00026 #include <unistd.h>
00027 #include <errno.h>
00028 
00029 #include <assert.h>
00030 
00031 //----------------------
00032 #include "kmaccount.moc"
00033 
00034 //-----------------------------------------------------------------------------
00035 KMPrecommand::KMPrecommand(const QString &precommand, QObject *parent)
00036   : QObject(parent), mPrecommand(precommand)
00037 {
00038   KMBroadcastStatus::instance()->setStatusMsg(
00039       i18n("Executing precommand %1").arg(precommand ));
00040 
00041   mPrecommandProcess.setUseShell(true);
00042   mPrecommandProcess << precommand;
00043 
00044   connect(&mPrecommandProcess, SIGNAL(processExited(KProcess *)),
00045           SLOT(precommandExited(KProcess *)));
00046 }
00047 
00048 //-----------------------------------------------------------------------------
00049 KMPrecommand::~KMPrecommand()
00050 {
00051 }
00052 
00053 
00054 //-----------------------------------------------------------------------------
00055 bool KMPrecommand::start()
00056 {
00057   bool ok = mPrecommandProcess.start( KProcess::NotifyOnExit );
00058   if (!ok) KMessageBox::error(0, i18n("Couldn't execute precommand '%1'.")
00059     .arg(mPrecommand));
00060   return ok;
00061 }
00062 
00063 
00064 //-----------------------------------------------------------------------------
00065 void KMPrecommand::precommandExited(KProcess *p)
00066 {
00067   int exitCode = p->normalExit() ? p->exitStatus() : -1;
00068   if (exitCode)
00069     KMessageBox::error(0, i18n("The precommand exited with code %1:\n%2")
00070       .arg(exitCode).arg(strerror(exitCode)));
00071   emit finished(!exitCode);
00072 }
00073 
00074 
00075 //-----------------------------------------------------------------------------
00076 KMAccount::KMAccount(KMAcctMgr* aOwner, const QString& aName)
00077   : KAccount( 0, aName ),
00078     mName(aName),
00079     mTrash(KMKernel::self()->trashFolder()->idString()),
00080     mOwner(aOwner),
00081     mFolder(0),
00082     mTimer(0),
00083     mInterval(0),
00084     mResource(false),
00085     mExclude(false),
00086     mCheckingMail(false),
00087     mPrecommandSuccess(true),
00088     mHasInbox(false)
00089 {
00090   assert(aOwner != 0);
00091 
00092   connect(&mReceiptTimer,SIGNAL(timeout()),SLOT(sendReceipts()));
00093 }
00094 
00095 void KMAccount::init() {
00096   mTrash = kmkernel->trashFolder()->idString();
00097   mResource = false;
00098   mExclude = false;
00099   mInterval = 0;
00100 }
00101 
00102 //-----------------------------------------------------------------------------
00103 KMAccount::~KMAccount()
00104 {
00105   if (!kmkernel->shuttingDown() && mFolder) mFolder->removeAccount(this);
00106   if (mTimer) deinstallTimer();
00107 }
00108 
00109 
00110 //-----------------------------------------------------------------------------
00111 void KMAccount::setName(const QString& aName)
00112 {
00113   mName = (aName.isEmpty()) ? i18n("Unnamed") : aName;
00114 }
00115 
00116 
00117 //-----------------------------------------------------------------------------
00118 void KMAccount::clearPasswd()
00119 {
00120 }
00121 
00122 
00123 //-----------------------------------------------------------------------------
00124 void KMAccount::setFolder(KMFolder* aFolder, bool addAccount)
00125 {
00126   if(!aFolder)
00127     {
00128     kdDebug(5006) << "KMAccount::setFolder() : aFolder == 0" << endl;
00129     mFolder = 0;
00130     return;
00131     }
00132   mFolder = (KMAcctFolder*)aFolder;
00133   if (addAccount) mFolder->addAccount(this);
00134 }
00135 
00136 
00137 //-----------------------------------------------------------------------------
00138 void KMAccount::readConfig(KConfig& config)
00139 {
00140   QString folderName;
00141 
00142   mFolder = 0;
00143   folderName = config.readEntry("Folder");
00144   setCheckInterval(config.readNumEntry("check-interval", 0));
00145   setTrash(config.readEntry("trash", kmkernel->trashFolder()->idString()));
00146   setResource(config.readBoolEntry("resource", false) );
00147   setCheckExclude(config.readBoolEntry("check-exclude", false));
00148   setPrecommand(config.readPathEntry("precommand"));
00149 
00150   if (!folderName.isEmpty())
00151   {
00152     setFolder(kmkernel->folderMgr()->findIdString(folderName), true);
00153   }
00154 
00155   if( mResource ) {
00156       int numResourceEntries = config.readNumEntry( "numResourceEntries", 0 );
00157       int count = 0;
00158       for( int i = 0; i < numResourceEntries; i++, count++ ) {
00159           QDateTime start = config.readDateTimeEntry( QString( "resource%1-start" ).arg( i ) );
00160           QDateTime end = config.readDateTimeEntry( QString( "resource%1-end" ).arg( i ) );
00161           mIntervals.append( qMakePair(start,end) );
00162       }
00163   }
00164 }
00165 
00166 
00167 //-----------------------------------------------------------------------------
00168 void KMAccount::writeConfig(KConfig& config)
00169 {
00170   config.writeEntry("Type", type());
00171   config.writeEntry("Name", mName);
00172   config.writeEntry("Folder", mFolder ? mFolder->idString() : QString::null);
00173   config.writeEntry("check-interval", mInterval);
00174   config.writeEntry("resource", mResource);
00175   config.writeEntry("check-exclude", mExclude);
00176   config.writePathEntry("precommand", mPrecommand);
00177   config.writeEntry("trash", mTrash);
00178 
00179   // Write the resource management data
00180   if( mResource ) {
00181       config.writeEntry("numResourceEntries", mIntervals.count() );
00182       int count = 0;
00183       for( QValueList<QPair< QDateTime, QDateTime> >::Iterator it = mIntervals.begin(); it != mIntervals.end(); ++it, count++ ) {
00184           config.writeEntry( QString( "resource%1-start" ).arg( count ),
00185                              (*it).first );
00186           config.writeEntry( QString( "resource%1-end" ).arg( count ),
00187                              (*it).second );
00188       }
00189   }
00190 }
00191 
00192 
00193 //-----------------------------------------------------------------------------
00194 void KMAccount::sendReceipt(KMMessage* aMsg)
00195 {
00196   KConfig* cfg = KMKernel::config();
00197   bool sendReceipts;
00198 
00199   KConfigGroupSaver saver(cfg, "General");
00200 
00201   sendReceipts = cfg->readBoolEntry("send-receipts", false);
00202   if (!sendReceipts) return;
00203 
00204   KMMessage *newMsg = aMsg->createDeliveryReceipt();
00205   if (newMsg) {
00206     mReceipts.append(newMsg);
00207     mReceiptTimer.start(0,true);
00208   }
00209 }
00210 
00211 
00212 //-----------------------------------------------------------------------------
00213 bool KMAccount::processNewMsg(KMMessage* aMsg)
00214 {
00215   int rc, processResult;
00216 
00217   assert(aMsg != 0);
00218 
00219   // Save this one for readding
00220   KMFolderCachedImap* parent = 0;
00221   if( type() == "cachedimap" )
00222     parent = static_cast<KMFolderCachedImap*>( aMsg->parent() );
00223 
00224   // checks whether we should send delivery receipts
00225   // and sends them.
00226   sendReceipt(aMsg);
00227 
00228   // Set status of new messages that are marked as old to read, otherwise
00229   // the user won't see which messages newly arrived.
00230   if (aMsg->isOld())
00231     aMsg->setStatus(KMMsgStatusUnread);  // -sanders
00232   //    aMsg->setStatus(KMMsgStatusRead);
00233   else aMsg->setStatus(KMMsgStatusNew);
00234 /*
00235 QFile fileD0( "testdat_xx-kmaccount-0" );
00236 if( fileD0.open( IO_WriteOnly ) ) {
00237     QDataStream ds( &fileD0 );
00238     ds.writeRawBytes( aMsg->asString(), aMsg->asString().length() );
00239     fileD0.close();  // If data is 0 we just create a zero length file.
00240 }
00241 */
00242   // 0==message moved; 1==processing ok, no move; 2==critical error, abort!
00243 
00244   // Automatic resource handling: Give the Groupware code a chance to
00245   // answer this message automatically, but only if this is a resource
00246   // account.
00247   if( resource() ) {
00248 #if 0
00249       if( kmkernel->groupware().incomingResourceMessage( this, aMsg ) )
00250           // If it was a resource message, we have already answered it.
00251           aMsg->setStatus( KMMsgStatusReplied );
00252 #endif
00253   }
00254 
00255   processResult = kmkernel->filterMgr()->process(aMsg,KMFilterMgr::Inbound);
00256   if (processResult == 2) {
00257     perror("Critical error: Unable to collect mail (out of space?)");
00258     KMessageBox::information(0,(i18n("Critical error: "
00259       "Unable to collect mail: ")) + QString::fromLocal8Bit(strerror(errno)));
00260     return false;
00261   }
00262   else if (processResult == 1)
00263   {
00264     if( type() == "cachedimap" )
00265       parent->addMsgInternal( aMsg, false );
00266     else {
00267       // TODO: Perhaps it would be best, if this if was handled by a virtual
00268       // method, so the if( !dimap ) above could die?
00269       kmkernel->filterMgr()->tempOpenFolder(mFolder);
00270       rc = mFolder->addMsg(aMsg);
00271 /*
00272 QFile fileD0( "testdat_xx-kmaccount-1" );
00273 if( fileD0.open( IO_WriteOnly ) ) {
00274     QDataStream ds( &fileD0 );
00275     ds.writeRawBytes( aMsg->asString(), aMsg->asString().length() );
00276     fileD0.close();  // If data is 0 we just create a zero length file.
00277 }
00278 */
00279       if (rc) {
00280         perror("failed to add message");
00281         KMessageBox::information(0, i18n("Failed to add message:\n") +
00282                                  QString(strerror(rc)));
00283         return false;
00284       }
00285       int count = mFolder->count();
00286       // If count == 1, the message is immediately displayed
00287       if (count != 1) mFolder->unGetMsg(count - 1);
00288     }
00289   }
00290   return true; //Everything's fine - message has been added by filter  }
00291 }
00292 
00293 
00294 //-----------------------------------------------------------------------------
00295 void KMAccount::setCheckInterval(int aInterval)
00296 {
00297   if (aInterval <= 0)
00298   {
00299     mInterval = 0;
00300     deinstallTimer();
00301   }
00302   else
00303   {
00304     mInterval = aInterval;
00305     installTimer();
00306   }
00307 }
00308 
00309 //----------------------------------------------------------------------------
00310 void KMAccount::deleteFolderJobs()
00311 {
00312   mJobList.setAutoDelete(true);
00313   mJobList.clear();
00314   mJobList.setAutoDelete(false);
00315 }
00316 
00317 //----------------------------------------------------------------------------
00318 void KMAccount::ignoreJobsForMessage( KMMessage* msg )
00319 {
00320   //FIXME: remove, make folders handle those
00321   for( QPtrListIterator<FolderJob> it(mJobList); it.current(); ++it ) {
00322     if ( it.current()->msgList().first() == msg) {
00323       FolderJob *job = it.current();
00324       mJobList.remove( job );
00325       delete job;
00326       break;
00327     }
00328   }
00329 }
00330 
00331 //-----------------------------------------------------------------------------
00332 void KMAccount::setCheckExclude(bool aExclude)
00333 {
00334   mExclude = aExclude;
00335 }
00336 
00337 
00338 //-----------------------------------------------------------------------------
00339 void KMAccount::setResource(bool aResource)
00340 {
00341    Q_UNUSED( aResource );
00342 #if 0
00343   mResource = aResource;
00344 #endif
00345   mResource = false;
00346 }
00347 
00348 
00349 //-----------------------------------------------------------------------------
00350 void KMAccount::installTimer()
00351 {
00352   if (mInterval <= 0) return;
00353   if(!mTimer)
00354   {
00355     mTimer = new QTimer();
00356     connect(mTimer,SIGNAL(timeout()),SLOT(mailCheck()));
00357   }
00358   else
00359   {
00360     mTimer->stop();
00361   }
00362   mTimer->start(mInterval*60000);
00363 }
00364 
00365 
00366 //-----------------------------------------------------------------------------
00367 void KMAccount::deinstallTimer()
00368 {
00369   delete mTimer;
00370   mTimer = 0;
00371 }
00372 
00373 //-----------------------------------------------------------------------------
00374 bool KMAccount::runPrecommand(const QString &precommand)
00375 {
00376   // Run the pre command if there is one
00377   if ( precommand.isEmpty() )
00378     return true;
00379 
00380   KMPrecommand precommandProcess(precommand, this);
00381 
00382   KMBroadcastStatus::instance()->setStatusMsg(
00383       i18n("Executing precommand %1").arg(precommand ));
00384 
00385   connect(&precommandProcess, SIGNAL(finished(bool)),
00386           SLOT(precommandExited(bool)));
00387 
00388   kdDebug(5006) << "Running precommand " << precommand << endl;
00389   if (!precommandProcess.start()) return false;
00390 
00391   kapp->eventLoop()->enterLoop();
00392 
00393   return mPrecommandSuccess;
00394 }
00395 
00396 //-----------------------------------------------------------------------------
00397 void KMAccount::precommandExited(bool success)
00398 {
00399   mPrecommandSuccess = success;
00400   kapp->eventLoop()->exitLoop();
00401 }
00402 
00403 //-----------------------------------------------------------------------------
00404 void KMAccount::mailCheck()
00405 {
00406   if (mTimer)
00407     mTimer->stop();
00408   kmkernel->acctMgr()->singleCheckMail(this, false);
00409 }
00410 
00411 //-----------------------------------------------------------------------------
00412 void KMAccount::sendReceipts()
00413 {
00414   QValueList<KMMessage*>::Iterator it;
00415   for(it = mReceipts.begin(); it != mReceipts.end(); ++it)
00416     kmkernel->msgSender()->send(*it); //might process events
00417   mReceipts.clear();
00418 }
00419 
00420 //-----------------------------------------------------------------------------
00421 QString KMAccount::encryptStr(const QString &aStr)
00422 {
00423   QString result;
00424   for (uint i = 0; i < aStr.length(); i++)
00425     result += (aStr[i].unicode() < 0x20) ? aStr[i] :
00426       QChar(0x1001F - aStr[i].unicode());
00427   return result;
00428 }
00429 
00430 //-----------------------------------------------------------------------------
00431 QString KMAccount::importPassword(const QString &aStr)
00432 {
00433   unsigned int i, val;
00434   unsigned int len = aStr.length();
00435   QCString result;
00436   result.resize(len+1);
00437 
00438   for (i=0; i<len; i++)
00439   {
00440     val = aStr[i] - ' ';
00441     val = (255-' ') - val;
00442     result[i] = (char)(val + ' ');
00443   }
00444   result[i] = '\0';
00445 
00446   return encryptStr(result);
00447 }
00448 
00449 
00455 void KMAccount::addInterval( const QPair<QDateTime,QDateTime>& iv )
00456 {
00457     mIntervals.append( iv );
00458 }
00459 
00460 
00464 QValueList<QPair<QDateTime, QDateTime> > KMAccount::intervals() const
00465 {
00466     return mIntervals;
00467 }
00468 
00469 
00474 void KMAccount::clearIntervals()
00475 {
00476     mIntervals.clear();
00477 }
00478 
00479 
00484 void KMAccount::clearOldIntervals()
00485 {
00486     QDateTime now = QDateTime::currentDateTime();
00487     for( QValueList<QPair<QDateTime, QDateTime> >::iterator it = mIntervals.begin(); it != mIntervals.end(); ++it ) {
00488         if( (*it).second < now )
00489             mIntervals.erase( it );
00490     }
00491 }
00492 
00493 
00494 void KMAccount::setIntervals( const QValueList<QPair<QDateTime, QDateTime> >& newIntervals )
00495 {
00496     mIntervals = newIntervals;
00497 }
00498 
00499 
00500 void KMAccount::invalidateIMAPFolders()
00501 {
00502   // Default: Don't do anything. The IMAP account will handle it
00503 }
00504 
00505 void KMAccount::pseudoAssign( const KMAccount * a ) {
00506   if ( !a ) return;
00507 
00508   setName( a->name() );
00509   setCheckInterval( a->checkInterval() );
00510   setResource( a->resource() );
00511   setIntervals( a->intervals() );
00512   setCheckExclude( a->checkExclude() );
00513   setFolder( a->folder() );
00514   setPrecommand( a->precommand() );
00515   setTrash( a->trash() );
00516 }
00517 
00518 //-----------------------------------------------------------------------------
00519 void KMAccount::checkDone( bool newmail, int newmailCount )
00520 {
00521   mCheckingMail = false;
00522   // Reset the timeout for automatic mailchecking. The user might have
00523   // triggered the check manually.
00524   if (mTimer)
00525     mTimer->start(mInterval*60000);
00526   emit newMailsProcessed(newmailCount);
00527   emit finishedCheck(newmail);
00528 }
00529 
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