kmail

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 #include <qstylesheet.h>
00025 
00026 #include <stdlib.h>
00027 #include <stdio.h>
00028 #include <errno.h>
00029 #include <assert.h>
00030 
00031 //-----------------------------------------------------------------------------
00032 KMAcctLocal::KMAcctLocal(AccountManager* aOwner, const QString& aAccountName, uint id):
00033   KMAccount(aOwner, aAccountName, id), mHasNewMail( false ),
00034   mProcessingNewMail( false ), mAddedOk( true ), mNumMsgs( 0 ),
00035   mMsgsFetched( 0 ), mMailFolder( 0 )
00036 {
00037   mLock = procmail_lockfile;
00038 }
00039 
00040 
00041 //-----------------------------------------------------------------------------
00042 KMAcctLocal::~KMAcctLocal()
00043 {
00044 }
00045 
00046 
00047 //-----------------------------------------------------------------------------
00048 QString KMAcctLocal::type(void) const
00049 {
00050   return "local";
00051 }
00052 
00053 
00054 //-----------------------------------------------------------------------------
00055 void KMAcctLocal::init() {
00056   KMAccount::init();
00057 }
00058 
00059 
00060 //-----------------------------------------------------------------------------
00061 void KMAcctLocal::pseudoAssign( const KMAccount * a )
00062 {
00063   KMAccount::pseudoAssign( a );
00064 
00065   const KMAcctLocal * l = dynamic_cast<const KMAcctLocal*>( a );
00066   if ( !l ) return;
00067 
00068   setLocation( l->location() );
00069   setLockType( l->lockType() );
00070   setProcmailLockFileName( l->procmailLockFileName() );
00071 }
00072 
00073 //-----------------------------------------------------------------------------
00074 void KMAcctLocal::processNewMail(bool)
00075 {
00076   if ( mProcessingNewMail )
00077     return;
00078 
00079   mHasNewMail = false;
00080   mProcessingNewMail = true;
00081 
00082   if ( !preProcess() ) {
00083     mProcessingNewMail = false;
00084     return;
00085   }
00086 
00087   QTime t;
00088   t.start();
00089 
00090   for ( mMsgsFetched = 0; mMsgsFetched < mNumMsgs; ++mMsgsFetched )
00091   {
00092     if ( !fetchMsg() )
00093       break;
00094 
00095     if (t.elapsed() >= 200) { //hardwired constant
00096       kapp->processEvents();
00097       t.start();
00098     }
00099   }
00100 
00101   postProcess();
00102   mProcessingNewMail = false;
00103 }
00104 
00105 
00106 //-----------------------------------------------------------------------------
00107 bool KMAcctLocal::preProcess()
00108 {
00109   if ( precommand().isEmpty() ) {
00110     QFileInfo fi( location() );
00111     if ( fi.size() == 0 ) {
00112       BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( mName, 0 );
00113       checkDone( mHasNewMail, CheckOK );
00114       return false;
00115     }
00116   }
00117 
00118   mMailFolder = new KMFolder( 0, location(), KMFolderTypeMbox,
00119                               false /* no index */, false /* don't export sernums */ );
00120   KMFolderMbox* mboxStorage =
00121     static_cast<KMFolderMbox*>(mMailFolder->storage());
00122   mboxStorage->setLockType( mLock );
00123   if ( mLock == procmail_lockfile)
00124     mboxStorage->setProcmailLockFileName( mProcmailLockFileName );
00125 
00126   if (!mFolder) {
00127     checkDone( mHasNewMail, CheckError );
00128     BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
00129     return false;
00130   }
00131 
00132   //BroadcastStatus::instance()->reset();
00133   BroadcastStatus::instance()->setStatusMsg(
00134     i18n("Preparing transmission from \"%1\"...").arg(mName));
00135 
00136 
00137   Q_ASSERT( !mMailCheckProgressItem );
00138   QString escapedName = QStyleSheet::escape( mName );
00139   mMailCheckProgressItem = KPIM::ProgressManager::createProgressItem(
00140     "MailCheck" + mName,
00141     escapedName,
00142     i18n("Preparing transmission from \"%1\"...").arg( escapedName ),
00143     false, // cannot be canceled
00144     false ); // no tls/ssl
00145 
00146   // run the precommand
00147   if (!runPrecommand(precommand()))
00148   {
00149     kdDebug(5006) << "cannot run precommand " << precommand() << endl;
00150     checkDone( mHasNewMail, CheckError );
00151     BroadcastStatus::instance()->setStatusMsg( i18n( "Running precommand failed." ));
00152     return false;
00153   }
00154   
00155   const int rc = mMailFolder->open();
00156   if ( rc != 0 ) {
00157     QString aStr;
00158     aStr = i18n("Cannot open file:");
00159     aStr += mMailFolder->path()+"/"+mMailFolder->name();
00160     KMessageBox::sorry(0, aStr);
00161     kdDebug(5006) << "cannot open file " << mMailFolder->path() << "/"
00162       << mMailFolder->name() << endl;
00163     checkDone( mHasNewMail, CheckError );
00164     BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
00165     return false;
00166   }
00167 
00168   if (!mboxStorage->isLocked()) {
00169     kdDebug(5006) << "mailFolder could not be locked" << endl;
00170     mMailFolder->close();
00171     checkDone( mHasNewMail, CheckError );
00172     QString errMsg = i18n( "Transmission failed: Could not lock %1." )
00173       .arg( mMailFolder->location() );
00174     BroadcastStatus::instance()->setStatusMsg( errMsg );
00175     return false;
00176   }
00177 
00178   mFolder->open();
00179 
00180   mNumMsgs = mMailFolder->count();
00181 
00182   mMailCheckProgressItem->setTotalItems( mNumMsgs );
00183 
00184   // prepare the static parts of the status message:
00185   mStatusMsgStub = i18n("Moving message %3 of %2 from %1.")
00186     .arg(mMailFolder->location()).arg( mNumMsgs );
00187 
00188   //BroadcastStatus::instance()->setStatusProgressEnable( "L" + mName, true );
00189   return true;
00190 }
00191 
00192 
00193 //-----------------------------------------------------------------------------
00194 bool KMAcctLocal::fetchMsg()
00195 {
00196   KMMessage* msg;
00197 
00198   /* This causes mail eating
00199   if (kmkernel->mailCheckAborted()) break; */
00200 
00201   const QString statusMsg = mStatusMsgStub.arg( mMsgsFetched );
00202   //BroadcastStatus::instance()->setStatusMsg( statusMsg );
00203   mMailCheckProgressItem->incCompletedItems();
00204   mMailCheckProgressItem->updateProgress();
00205   mMailCheckProgressItem->setStatus( statusMsg );
00206 
00207   msg = mMailFolder->take(0);
00208   if (msg)
00209   {
00210 #if 0
00211     // debug code, don't remove
00212     QFile fileD0( "testdat_xx-0-0" );
00213     if( fileD0.open( IO_WriteOnly ) ) {
00214       QCString s = msg->asString();
00215       uint l = s.length();
00216       if ( l > 0 ) {
00217         QDataStream ds( &fileD0 );
00218         ds.writeRawBytes( s.data(), l );
00219       }
00220       fileD0.close();  // If data is 0 we just create a zero length file.
00221     }
00222 #endif
00223     msg->setStatus(msg->headerField("Status").latin1(),
00224       msg->headerField("X-Status").latin1());
00225     msg->setEncryptionStateChar( msg->headerField( "X-KMail-EncryptionState" ).at(0) );
00226     msg->setSignatureStateChar( msg->headerField( "X-KMail-SignatureState" ).at(0));
00227     msg->setComplete(true);
00228     msg->updateAttachmentState();
00229 
00230     mAddedOk = processNewMsg(msg);
00231 
00232     if (mAddedOk)
00233       mHasNewMail = true;
00234 
00235     return mAddedOk;
00236   }
00237   return true;
00238 }
00239 
00240 
00241 //-----------------------------------------------------------------------------
00242 void KMAcctLocal::postProcess()
00243 {
00244   if (mAddedOk)
00245   {
00246     kmkernel->folderMgr()->syncAllFolders();
00247     const int rc = mMailFolder->expunge();
00248     if ( rc != 0 ) {
00249       KMessageBox::queuedMessageBox( 0, KMessageBox::Information,
00250                                      i18n( "<qt>Cannot remove mail from "
00251                                            "mailbox <b>%1</b>:<br>%2</qt>" )
00252                                      .arg( mMailFolder->location() )
00253                                      .arg( strerror( rc ) ) );
00254     }
00255 
00256     if( mMailCheckProgressItem ) { // do this only once...
00257       BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( mName, mNumMsgs );
00258       mMailCheckProgressItem->setStatus(
00259         i18n( "Fetched 1 message from mailbox %1.",
00260               "Fetched %n messages from mailbox %1.",
00261               mNumMsgs ).arg( mMailFolder->location() ) );
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 Home | KDE Accessibility Home | Description of Access Keys