kmail Library API Documentation

kmacctlocal.cpp

00001 // kmacctlocal.cpp
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include "kmacctlocal.h"
00008 #include "kmfoldermbox.h"
00009 #include "kmacctfolder.h"
00010 #include "broadcaststatus.h"
00011 using KPIM::BroadcastStatus;
00012 #include "progressmanager.h"
00013 using KPIM::ProgressManager;
00014 
00015 #include "kmfoldermgr.h"
00016 
00017 #include <kapplication.h>
00018 #include <klocale.h>
00019 #include <kmessagebox.h>
00020 #include <kdebug.h>
00021 #include <kconfig.h>
00022 
00023 #include <qfileinfo.h>
00024 
00025 #include <stdlib.h>
00026 #include <stdio.h>
00027 #include <errno.h>
00028 #include <assert.h>
00029 
00030 //-----------------------------------------------------------------------------
00031 KMAcctLocal::KMAcctLocal(KMAcctMgr* aOwner, const QString& aAccountName, uint id):
00032   KMAccount(aOwner, aAccountName, id), mHasNewMail( false ),
00033   mProcessingNewMail( false ), mAddedOk( true ), mNumMsgs( 0 ),
00034   mMsgsFetched( 0 ), mMailFolder( 0 )
00035 {
00036   mLock = procmail_lockfile;
00037 }
00038 
00039 
00040 //-----------------------------------------------------------------------------
00041 KMAcctLocal::~KMAcctLocal()
00042 {
00043 }
00044 
00045 
00046 //-----------------------------------------------------------------------------
00047 QString KMAcctLocal::type(void) const
00048 {
00049   return "local";
00050 }
00051 
00052 
00053 //-----------------------------------------------------------------------------
00054 void KMAcctLocal::init() {
00055   KMAccount::init();
00056 }
00057 
00058 
00059 //-----------------------------------------------------------------------------
00060 void KMAcctLocal::pseudoAssign( const KMAccount * a )
00061 {
00062   KMAccount::pseudoAssign( a );
00063 
00064   const KMAcctLocal * l = dynamic_cast<const KMAcctLocal*>( a );
00065   if ( !l ) return;
00066 
00067   setLocation( l->location() );
00068   setLockType( l->lockType() );
00069   setProcmailLockFileName( l->procmailLockFileName() );
00070 }
00071 
00072 //-----------------------------------------------------------------------------
00073 void KMAcctLocal::processNewMail(bool)
00074 {
00075   if ( mProcessingNewMail )
00076     return;
00077 
00078   mHasNewMail = false;
00079   mProcessingNewMail = true;
00080 
00081   if ( !preProcess() ) {
00082     mProcessingNewMail = false;
00083     return;
00084   }
00085 
00086   QTime t;
00087   t.start();
00088 
00089   for ( mMsgsFetched = 0; mMsgsFetched < mNumMsgs; ++mMsgsFetched )
00090   {
00091     if ( !fetchMsg() )
00092       break;
00093 
00094     if (t.elapsed() >= 200) { //hardwired constant
00095       kapp->processEvents();
00096       t.start();
00097     }
00098   }
00099 
00100   postProcess();
00101   mProcessingNewMail = false;
00102 }
00103 
00104 
00105 //-----------------------------------------------------------------------------
00106 bool KMAcctLocal::preProcess()
00107 {
00108   if ( precommand().isEmpty() ) {
00109     QFileInfo fi( location() );
00110     if ( fi.size() == 0 ) {
00111       BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( 0 );
00112       checkDone( mHasNewMail, CheckOK );
00113       return false;
00114     }
00115   }
00116 
00117   mMailFolder = new KMFolder( 0, location(), KMFolderTypeMbox );
00118   KMFolderMbox* mboxStorage =
00119     static_cast<KMFolderMbox*>(mMailFolder->storage());
00120   mboxStorage->setLockType( mLock );
00121   if ( mLock == procmail_lockfile)
00122     mboxStorage->setProcmailLockFileName( mProcmailLockFileName );
00123 
00124   if (!mFolder) {
00125     checkDone( mHasNewMail, CheckError );
00126     BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
00127     return false;
00128   }
00129 
00130   //BroadcastStatus::instance()->reset();
00131   BroadcastStatus::instance()->setStatusMsg(
00132     i18n("Preparing transmission from \"%1\"...").arg(mName));
00133 
00134 
00135   Q_ASSERT( !mMailCheckProgressItem );
00136   mMailCheckProgressItem = KPIM::ProgressManager::createProgressItem(
00137     "MailCheck" + mName,
00138     mName,
00139     i18n("Preparing transmission from \"%1\"...").arg(mName),
00140     false, // cannot be canceled
00141     false ); // no tls/ssl
00142 
00143   // run the precommand
00144   if (!runPrecommand(precommand()))
00145   {
00146     kdDebug(5006) << "cannot run precommand " << precommand() << endl;
00147     checkDone( mHasNewMail, CheckError );
00148     return false;
00149   }
00150 
00151   mMailFolder->setAutoCreateIndex(FALSE);
00152 
00153   const int rc = mMailFolder->open();
00154   if ( rc != 0 ) {
00155     QString aStr;
00156     aStr = i18n("Cannot open file:");
00157     aStr += mMailFolder->path()+"/"+mMailFolder->name();
00158     KMessageBox::sorry(0, aStr);
00159     kdDebug(5006) << "cannot open file " << mMailFolder->path() << "/"
00160       << mMailFolder->name() << endl;
00161     checkDone( mHasNewMail, CheckError );
00162     BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
00163     return false;
00164   }
00165 
00166   if (!mboxStorage->isLocked()) {
00167     kdDebug(5006) << "mailFolder could not be locked" << endl;
00168     mMailFolder->close();
00169     checkDone( mHasNewMail, CheckError );
00170     QString errMsg = i18n( "Transmission failed: Could not lock %1." )
00171       .arg( mMailFolder->location() );
00172     BroadcastStatus::instance()->setStatusMsg( errMsg );
00173     return false;
00174   }
00175 
00176   mFolder->open();
00177 
00178   mNumMsgs = mMailFolder->count();
00179 
00180   mMailCheckProgressItem->setTotalItems( mNumMsgs );
00181 
00182   // prepare the static parts of the status message:
00183   mStatusMsgStub = i18n("Moving message %3 of %2 from %1.")
00184     .arg(mMailFolder->location()).arg( mNumMsgs );
00185 
00186   //BroadcastStatus::instance()->setStatusProgressEnable( "L" + mName, true );
00187   return true;
00188 }
00189 
00190 
00191 //-----------------------------------------------------------------------------
00192 bool KMAcctLocal::fetchMsg()
00193 {
00194   KMMessage* msg;
00195 
00196   /* This causes mail eating
00197   if (kmkernel->mailCheckAborted()) break; */
00198 
00199   const QString statusMsg = mStatusMsgStub.arg( mMsgsFetched );
00200   //BroadcastStatus::instance()->setStatusMsg( statusMsg );
00201   mMailCheckProgressItem->incCompletedItems();
00202   mMailCheckProgressItem->updateProgress();
00203   mMailCheckProgressItem->setStatus( statusMsg );
00204 
00205   msg = mMailFolder->take(0);
00206   if (msg)
00207   {
00208 #if 0
00209     // debug code, don't remove
00210     QFile fileD0( "testdat_xx-0-0" );
00211     if( fileD0.open( IO_WriteOnly ) ) {
00212       QCString s = msg->asString();
00213       uint l = s.length();
00214       if ( l > 0 ) {
00215         QDataStream ds( &fileD0 );
00216         ds.writeRawBytes( s.data(), l );
00217       }
00218       fileD0.close();  // If data is 0 we just create a zero length file.
00219     }
00220 #endif
00221     msg->setStatus(msg->headerField("Status").latin1(),
00222       msg->headerField("X-Status").latin1());
00223     msg->setEncryptionStateChar( msg->headerField( "X-KMail-EncryptionState" ).at(0) );
00224     msg->setSignatureStateChar( msg->headerField( "X-KMail-SignatureState" ).at(0));
00225     msg->setComplete(true);
00226     msg->updateAttachmentState();
00227 
00228     mAddedOk = processNewMsg(msg);
00229 
00230     if (mAddedOk)
00231       mHasNewMail = true;
00232 
00233     return mAddedOk;
00234   }
00235   return true;
00236 }
00237 
00238 
00239 //-----------------------------------------------------------------------------
00240 void KMAcctLocal::postProcess()
00241 {
00242   if (mAddedOk)
00243   {
00244     kmkernel->folderMgr()->syncAllFolders();
00245     const int rc = mMailFolder->expunge();
00246     if ( rc != 0 ) {
00247       KMessageBox::queuedMessageBox( 0, KMessageBox::Information,
00248                                      i18n( "<qt>Cannot remove mail from "
00249                                            "mailbox <b>%1</b>:<br>%2</qt>" )
00250                                      .arg( mMailFolder->location() )
00251                                      .arg( strerror( rc ) ) );
00252     }
00253 
00254     if( mMailCheckProgressItem ) { // do this only once...
00255       BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( mNumMsgs );
00256       // FIXME Message reused from KMAcctExpPop, due to feature freeze
00257       mMailCheckProgressItem->setStatus(
00258         i18n( "Fetched 1 message from %1. Terminating transmission...",
00259               "Fetched %n messages from %1. Terminating transmission...",
00260               mNumMsgs )
00261         .arg( "localhost" ) );
00262       mMailCheckProgressItem->setComplete();
00263       mMailCheckProgressItem = 0;
00264     }
00265   }
00266   // else warning is written already
00267 
00268   mMailFolder->close();
00269   delete mMailFolder; mMailFolder = 0;
00270 
00271   mFolder->close();
00272 
00273   checkDone( mHasNewMail, CheckOK );
00274 }
00275 
00276 
00277 //-----------------------------------------------------------------------------
00278 void KMAcctLocal::readConfig(KConfig& config)
00279 {
00280   KMAccount::readConfig(config);
00281   mLocation = config.readPathEntry("Location", mLocation);
00282   QString locktype = config.readEntry("LockType", "procmail_lockfile" );
00283 
00284   if( locktype == "procmail_lockfile" ) {
00285     mLock = procmail_lockfile;
00286     mProcmailLockFileName = config.readEntry("ProcmailLockFile",
00287       mLocation + ".lock");
00288   } else if( locktype == "mutt_dotlock" )
00289     mLock = mutt_dotlock;
00290   else if( locktype == "mutt_dotlock_privileged" )
00291     mLock = mutt_dotlock_privileged;
00292   else if( locktype == "none" )
00293     mLock = lock_none;
00294   else mLock = FCNTL;
00295 }
00296 
00297 
00298 //-----------------------------------------------------------------------------
00299 void KMAcctLocal::writeConfig(KConfig& config)
00300 {
00301   KMAccount::writeConfig(config);
00302 
00303   config.writePathEntry("Location", mLocation);
00304 
00305   QString st = "fcntl";
00306   if (mLock == procmail_lockfile) st = "procmail_lockfile";
00307   else if (mLock == mutt_dotlock) st = "mutt_dotlock";
00308   else if (mLock == mutt_dotlock_privileged) st = "mutt_dotlock_privileged";
00309   else if (mLock == lock_none) st = "none";
00310   config.writeEntry("LockType", st);
00311 
00312   if (mLock == procmail_lockfile) {
00313     config.writeEntry("ProcmailLockFile", mProcmailLockFileName);
00314   }
00315 
00316 }
00317 
00318 
00319 //-----------------------------------------------------------------------------
00320 void KMAcctLocal::setLocation(const QString& aLocation)
00321 {
00322     mLocation = aLocation;
00323 }
00324 
00325 void KMAcctLocal::setProcmailLockFileName(const QString& s)
00326 {
00327     mProcmailLockFileName = s;
00328 }
KDE Logo
This file is part of the documentation for kmail Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu May 3 20:22:55 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003