kmail

kmaccount.cpp

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