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 
00156 
00157 //-----------------------------------------------------------------------------
00158 void KMAccount::writeConfig(KConfig& config)
00159 {
00160   // ID, Name
00161   KAccount::writeConfig(config);
00162 
00163   config.writeEntry("Type", type());
00164   config.writeEntry("Folder", mFolder ? mFolder->idString() : QString::null);
00165   config.writeEntry("check-interval", mInterval);
00166   config.writeEntry("check-exclude", mExclude);
00167   config.writePathEntry("precommand", mPrecommand);
00168   config.writeEntry("trash", mTrash);
00169 }
00170 
00171 
00172 //-----------------------------------------------------------------------------
00173 void KMAccount::sendReceipt(KMMessage* aMsg)
00174 {
00175   KConfig* cfg = KMKernel::config();
00176   bool sendReceipts;
00177 
00178   KConfigGroupSaver saver(cfg, "General");
00179 
00180   sendReceipts = cfg->readBoolEntry("send-receipts", false);
00181   if (!sendReceipts) return;
00182 
00183   KMMessage *newMsg = aMsg->createDeliveryReceipt();
00184   if (newMsg) {
00185     mReceipts.append(newMsg);
00186     QTimer::singleShot( 0, this, SLOT( sendReceipts() ) );
00187   }
00188 }
00189 
00190 
00191 //-----------------------------------------------------------------------------
00192 bool KMAccount::processNewMsg(KMMessage* aMsg)
00193 {
00194   int rc, processResult;
00195 
00196   assert(aMsg != 0);
00197 
00198   // Save this one for readding
00199   KMFolderCachedImap* parent = 0;
00200   if( type() == "cachedimap" )
00201     parent = static_cast<KMFolderCachedImap*>( aMsg->storage() );
00202 
00203   // checks whether we should send delivery receipts
00204   // and sends them.
00205   sendReceipt(aMsg);
00206 
00207   // Set status of new messages that are marked as old to read, otherwise
00208   // the user won't see which messages newly arrived.
00209   // This is only valid for pop accounts and produces wrong stati for imap.
00210   if ( type() != "cachedimap" && type() != "imap" ) {
00211     if ( aMsg->isOld() )
00212       aMsg->setStatus(KMMsgStatusUnread);  // -sanders
00213     //    aMsg->setStatus(KMMsgStatusRead);
00214     else
00215       aMsg->setStatus(KMMsgStatusNew);
00216   }
00217 /*
00218 QFile fileD0( "testdat_xx-kmaccount-0" );
00219 if( fileD0.open( IO_WriteOnly ) ) {
00220     QDataStream ds( &fileD0 );
00221     ds.writeRawBytes( aMsg->asString(), aMsg->asString().length() );
00222     fileD0.close();  // If data is 0 we just create a zero length file.
00223 }
00224 */
00225   // 0==message moved; 1==processing ok, no move; 2==critical error, abort!
00226 
00227   processResult = kmkernel->filterMgr()->process(aMsg,KMFilterMgr::Inbound,true,id());
00228   if (processResult == 2) {
00229     perror("Critical error: Unable to collect mail (out of space?)");
00230     KMessageBox::information(0,(i18n("Critical error: "
00231       "Unable to collect mail: ")) + QString::fromLocal8Bit(strerror(errno)));
00232     return false;
00233   }
00234   else if (processResult == 1)
00235   {
00236     if( type() == "cachedimap" )
00237       ; // already done by caller: parent->addMsgInternal( aMsg, false );
00238     else {
00239       // TODO: Perhaps it would be best, if this if was handled by a virtual
00240       // method, so the if( !dimap ) above could die?
00241       kmkernel->filterMgr()->tempOpenFolder(mFolder);
00242       rc = mFolder->addMsg(aMsg);
00243 /*
00244 QFile fileD0( "testdat_xx-kmaccount-1" );
00245 if( fileD0.open( IO_WriteOnly ) ) {
00246     QDataStream ds( &fileD0 );
00247     ds.writeRawBytes( aMsg->asString(), aMsg->asString().length() );
00248     fileD0.close();  // If data is 0 we just create a zero length file.
00249 }
00250 */
00251       if (rc) {
00252         perror("failed to add message");
00253         KMessageBox::information(0, i18n("Failed to add message:\n") +
00254                                  QString(strerror(rc)));
00255         return false;
00256       }
00257       int count = mFolder->count();
00258       // If count == 1, the message is immediately displayed
00259       if (count != 1) mFolder->unGetMsg(count - 1);
00260     }
00261   }
00262 
00263   // Count number of new messages for each folder
00264   QString folderId;
00265   if ( processResult == 1 ) {
00266     folderId = ( type() == "cachedimap" ) ? parent->folder()->idString()
00267                                           : mFolder->idString();
00268   }
00269   else {
00270     folderId = aMsg->parent()->idString();
00271   }
00272   addToNewInFolder( folderId, 1 );
00273 
00274   return true; //Everything's fine - message has been added by filter  }
00275 }
00276 
00277 //-----------------------------------------------------------------------------
00278 void KMAccount::setCheckInterval(int aInterval)
00279 {
00280   if (aInterval <= 0)
00281   {
00282     mInterval = 0;
00283     deinstallTimer();
00284   }
00285   else
00286   {
00287     mInterval = aInterval;
00288     installTimer();
00289   }
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   kmkernel->acctMgr()->singleCheckMail(this, false);
00381 }
00382 
00383 //-----------------------------------------------------------------------------
00384 void KMAccount::sendReceipts()
00385 {
00386   QValueList<KMMessage*>::Iterator it;
00387   for(it = mReceipts.begin(); it != mReceipts.end(); ++it)
00388     kmkernel->msgSender()->send(*it); //might process events
00389   mReceipts.clear();
00390 }
00391 
00392 //-----------------------------------------------------------------------------
00393 QString KMAccount::encryptStr(const QString &aStr)
00394 {
00395   QString result;
00396   for (uint i = 0; i < aStr.length(); i++)
00397     result += (aStr[i].unicode() < 0x20) ? aStr[i] :
00398       QChar(0x1001F - aStr[i].unicode());
00399   return result;
00400 }
00401 
00402 //-----------------------------------------------------------------------------
00403 QString KMAccount::importPassword(const QString &aStr)
00404 {
00405   unsigned int i, val;
00406   unsigned int len = aStr.length();
00407   QCString result;
00408   result.resize(len+1);
00409 
00410   for (i=0; i<len; i++)
00411   {
00412     val = aStr[i] - ' ';
00413     val = (255-' ') - val;
00414     result[i] = (char)(val + ' ');
00415   }
00416   result[i] = '\0';
00417 
00418   return encryptStr(result);
00419 }
00420 
00421 void KMAccount::invalidateIMAPFolders()
00422 {
00423   // Default: Don't do anything. The IMAP account will handle it
00424 }
00425 
00426 void KMAccount::pseudoAssign( const KMAccount * a ) {
00427   if ( !a ) return;
00428 
00429   setName( a->name() );
00430   setId( a->id() );
00431   setCheckInterval( a->checkInterval() );
00432   setCheckExclude( a->checkExclude() );
00433   setFolder( a->folder() );
00434   setPrecommand( a->precommand() );
00435   setTrash( a->trash() );
00436 }
00437 
00438 //-----------------------------------------------------------------------------
00439 void KMAccount::checkDone( bool newmail, CheckStatus status )
00440 {
00441     setCheckingMail( false );
00442   // Reset the timeout for automatic mailchecking. The user might have
00443   // triggered the check manually.
00444   if (mTimer)
00445     mTimer->start(mInterval*60000);
00446   if ( mMailCheckProgressItem ) {
00447     mMailCheckProgressItem->setComplete(); // that will delete it
00448     mMailCheckProgressItem = 0;
00449   }
00450 
00451   emit newMailsProcessed( mNewInFolder );
00452   emit finishedCheck( newmail, status );
00453   mNewInFolder.clear();
00454 }
00455 
00456 //-----------------------------------------------------------------------------
00457 void KMAccount::addToNewInFolder( QString folderId, int num )
00458 {
00459   if ( mNewInFolder.find( folderId ) == mNewInFolder.end() )
00460     mNewInFolder[folderId] = num;
00461   else
00462     mNewInFolder[folderId] += num;
00463 }
KDE Home | KDE Accessibility Home | Description of Access Keys