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 "kmbroadcaststatus.h"
00011 #include "kmfoldermgr.h"
00012 
00013 #include <kapplication.h>
00014 #include <klocale.h>
00015 #include <kmessagebox.h>
00016 #include <kdebug.h>
00017 #include <kconfig.h>
00018 
00019 #include <qfileinfo.h>
00020 
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <errno.h>
00024 #include <assert.h>
00025 
00026 //-----------------------------------------------------------------------------
00027 KMAcctLocal::KMAcctLocal(KMAcctMgr* aOwner, const QString& aAccountName):
00028   KMAccount(aOwner, aAccountName)
00029 {
00030   mLock = procmail_lockfile;
00031 }
00032 
00033 
00034 //-----------------------------------------------------------------------------
00035 KMAcctLocal::~KMAcctLocal()
00036 {
00037 }
00038 
00039 
00040 //-----------------------------------------------------------------------------
00041 QString KMAcctLocal::type(void) const
00042 {
00043   return "local";
00044 }
00045 
00046 
00047 //-----------------------------------------------------------------------------
00048 void KMAcctLocal::init() {
00049   KMAccount::init();
00050 }
00051 
00052 
00053 //-----------------------------------------------------------------------------
00054 void KMAcctLocal::pseudoAssign( const KMAccount * a )
00055 {
00056   KMAccount::pseudoAssign( a );
00057 
00058   const KMAcctLocal * l = dynamic_cast<const KMAcctLocal*>( a );
00059   if ( !l ) return;
00060 
00061   setLocation( l->location() );
00062   setLockType( l->lockType() );
00063   setProcmailLockFileName( l->procmailLockFileName() );
00064 }
00065 
00066 //-----------------------------------------------------------------------------
00067 void KMAcctLocal::processNewMail(bool)
00068 {
00069   QTime t;
00070   hasNewMail = false;
00071 
00072   if ( precommand().isEmpty() ) {
00073     QFileInfo fi( location() );
00074     if ( fi.size() == 0 ) {
00075       KMBroadcastStatus::instance()->setStatusMsgTransmissionCompleted( 0 );
00076       checkDone(hasNewMail, 0);
00077       return;
00078     }
00079   }
00080 
00081   KMFolderMbox mailFolder(0, location());
00082   mailFolder.setLockType( mLock );
00083   if ( mLock == procmail_lockfile)
00084     mailFolder.setProcmailLockFileName( mProcmailLockFileName );
00085 
00086   long num = 0;
00087   long i;
00088   int rc;
00089   KMMessage* msg;
00090   bool addedOk;
00091 
00092   if (!mFolder) {
00093     checkDone(hasNewMail, -1);
00094     KMBroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
00095     return;
00096   }
00097 
00098   KMBroadcastStatus::instance()->reset();
00099   KMBroadcastStatus::instance()->setStatusMsg(
00100     i18n("Preparing transmission from \"%1\"...").arg(mName));
00101 
00102   // run the precommand
00103   if (!runPrecommand(precommand()))
00104   {
00105     kdDebug(5006) << "cannot run precommand " << precommand() << endl;
00106     checkDone(hasNewMail, -1);
00107   }
00108 
00109   mailFolder.setAutoCreateIndex(FALSE);
00110 
00111   rc = mailFolder.open();
00112   if (rc)
00113   {
00114     QString aStr;
00115     aStr = i18n("Cannot open file:");
00116     aStr += mailFolder.path()+"/"+mailFolder.name();
00117     KMessageBox::sorry(0, aStr);
00118     kdDebug(5006) << "cannot open file " << mailFolder.path() << "/"
00119       << mailFolder.name() << endl;
00120     checkDone(hasNewMail, -1);
00121     KMBroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
00122     return;
00123   }
00124 
00125   if (mailFolder.isReadOnly()) { // mailFolder is locked
00126     kdDebug(5006) << "mailFolder could not be locked" << endl;
00127     mailFolder.close();
00128     checkDone(hasNewMail, -1);
00129     QString errMsg = i18n( "Transmission failed: Could not lock %1." )
00130       .arg( mailFolder.location() );
00131     KMBroadcastStatus::instance()->setStatusMsg( errMsg );
00132     return;
00133   }
00134 
00135   mFolder->open();
00136 
00137   num = mailFolder.count();
00138 
00139   addedOk = true;
00140   t.start();
00141 
00142   // prepare the static parts of the status message:
00143   QString statusMsgStub = i18n("Moving message %3 of %2 from %1.")
00144     .arg(mailFolder.location()).arg(num);
00145 
00146   KMBroadcastStatus::instance()->setStatusProgressEnable( "L" + mName, true );
00147   for (i=0; i<num; i++)
00148   {
00149 
00150     if (!addedOk) break;
00151 
00152     /* This causes mail eating
00153     if (KMBroadcastStatus::instance()->abortRequested()) break; */
00154 
00155     QString statusMsg = statusMsgStub.arg(i);
00156     KMBroadcastStatus::instance()->setStatusMsg( statusMsg );
00157     KMBroadcastStatus::instance()->setStatusProgressPercent( "L" + mName,
00158       (i*100) / num );
00159 
00160     msg = mailFolder.take(0);
00161     if (msg)
00162     {
00163 #if 0
00164       // debug code, don't remove
00165       QFile fileD0( "testdat_xx-0-0" );
00166       if( fileD0.open( IO_WriteOnly ) ) {
00167         QCString s = msg->asString();
00168         uint l = s.length();
00169         if ( l > 0 ) {
00170           QDataStream ds( &fileD0 );
00171           ds.writeRawBytes( s.data(), l );
00172         }
00173         fileD0.close();  // If data is 0 we just create a zero length file.
00174       }
00175 #endif
00176       msg->setStatus(msg->headerField("Status").latin1(),
00177         msg->headerField("X-Status").latin1());
00178       msg->setEncryptionStateChar( msg->headerField( "X-KMail-EncryptionState" ).at(0) );
00179       msg->setSignatureStateChar( msg->headerField( "X-KMail-SignatureState" ).at(0));
00180 
00181       addedOk = processNewMsg(msg);
00182 
00183       if (addedOk)
00184         hasNewMail = true;
00185     }
00186 
00187     if (t.elapsed() >= 200) { //hardwired constant
00188       kapp->processEvents();
00189       t.start();
00190     }
00191 
00192   }
00193   KMBroadcastStatus::instance()->setStatusProgressEnable( "L" + mName, false );
00194   KMBroadcastStatus::instance()->reset();
00195 
00196   if (addedOk)
00197   {
00198     kmkernel->folderMgr()->syncAllFolders();
00199     rc = mailFolder.expunge();
00200     if (rc)
00201       KMessageBox::queuedMessageBox( 0, KMessageBox::Information,
00202                                      i18n( "<qt>Cannot remove mail from "
00203                                            "mailbox <b>%1</b>:<br>%2</qt>" )
00204                                      .arg( mailFolder.location() )
00205                                      .arg( strerror( rc ) ) );
00206     KMBroadcastStatus::instance()->setStatusMsgTransmissionCompleted( num );
00207   }
00208   // else warning is written already
00209 
00210   mailFolder.close();
00211   mFolder->close();
00212 
00213   checkDone(hasNewMail, num);
00214 
00215   return;
00216 }
00217 
00218 
00219 //-----------------------------------------------------------------------------
00220 void KMAcctLocal::readConfig(KConfig& config)
00221 {
00222   KMAccount::readConfig(config);
00223   mLocation = config.readPathEntry("Location", mLocation);
00224   QString locktype = config.readEntry("LockType", "procmail_lockfile" );
00225 
00226   if( locktype == "procmail_lockfile" ) {
00227     mLock = procmail_lockfile;
00228     mProcmailLockFileName = config.readEntry("ProcmailLockFile",
00229       mLocation + ".lock");
00230   } else if( locktype == "mutt_dotlock" )
00231     mLock = mutt_dotlock;
00232   else if( locktype == "mutt_dotlock_privileged" )
00233     mLock = mutt_dotlock_privileged;
00234   else if( locktype == "none" )
00235     mLock = lock_none;
00236   else mLock = FCNTL;
00237 }
00238 
00239 
00240 //-----------------------------------------------------------------------------
00241 void KMAcctLocal::writeConfig(KConfig& config)
00242 {
00243   KMAccount::writeConfig(config);
00244 
00245   config.writePathEntry("Location", mLocation);
00246 
00247   QString st = "fcntl";
00248   if (mLock == procmail_lockfile) st = "procmail_lockfile";
00249   else if (mLock == mutt_dotlock) st = "mutt_dotlock";
00250   else if (mLock == mutt_dotlock_privileged) st = "mutt_dotlock_privileged";
00251   else if (mLock == lock_none) st = "none";
00252   config.writeEntry("LockType", st);
00253 
00254   if (mLock == procmail_lockfile) {
00255     config.writeEntry("ProcmailLockFile", mProcmailLockFileName);
00256   }
00257 
00258 }
00259 
00260 
00261 //-----------------------------------------------------------------------------
00262 void KMAcctLocal::setLocation(const QString& aLocation)
00263 {
00264     mLocation = aLocation;
00265 }
00266 
00267 void KMAcctLocal::setProcmailLockFileName(const QString& s)
00268 {
00269     mProcmailLockFileName = s;
00270 }
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