kmail Library API Documentation

kmacctmaildir.cpp

00001 // kmacctmaildir.cpp
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <qfileinfo.h>
00008 #include "kmacctmaildir.h"
00009 #include "kmfoldermaildir.h"
00010 #include "kmacctfolder.h"
00011 #include "broadcaststatus.h"
00012 using KPIM::BroadcastStatus;
00013 #include "progressmanager.h"
00014 using KPIM::ProgressManager;
00015 
00016 #include <kapplication.h>
00017 #include <klocale.h>
00018 #include <kmessagebox.h>
00019 #include <kdebug.h>
00020 #include <kconfig.h>
00021 
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 #include <errno.h>
00025 #include <assert.h>
00026 
00027 #ifdef HAVE_PATHS_H
00028 #include <paths.h>  /* defines _PATH_MAILDIR */
00029 #endif
00030 
00031 #undef None
00032 
00033 //-----------------------------------------------------------------------------
00034 KMAcctMaildir::KMAcctMaildir(KMAcctMgr* aOwner, const QString& aAccountName, uint id):
00035   KMAccount(aOwner, aAccountName, id)
00036 {
00037 }
00038 
00039 
00040 //-----------------------------------------------------------------------------
00041 KMAcctMaildir::~KMAcctMaildir()
00042 {
00043   mLocation = "";
00044 }
00045 
00046 
00047 //-----------------------------------------------------------------------------
00048 QString KMAcctMaildir::type(void) const
00049 {
00050   return "maildir";
00051 }
00052 
00053 
00054 //-----------------------------------------------------------------------------
00055 void KMAcctMaildir::init() {
00056   KMAccount::init();
00057 
00058   mLocation = getenv("MAIL");
00059   if (mLocation.isNull()) {
00060     mLocation = getenv("HOME");
00061     mLocation += "/Maildir/";
00062   }
00063 }
00064 
00065 
00066 //-----------------------------------------------------------------------------
00067 void KMAcctMaildir::pseudoAssign( const KMAccount * a )
00068 {
00069   KMAccount::pseudoAssign( a );
00070 
00071   const KMAcctMaildir * m = dynamic_cast<const KMAcctMaildir*>( a );
00072   if ( !m ) return;
00073 
00074   setLocation( m->location() );
00075 }
00076 
00077 //-----------------------------------------------------------------------------
00078 void KMAcctMaildir::processNewMail(bool)
00079 {
00080   QTime t;
00081   hasNewMail = false;
00082 
00083   if ( precommand().isEmpty() ) {
00084     QFileInfo fi( location() );
00085     if ( !fi.exists() ) {
00086       checkDone( hasNewMail, CheckOK );
00087       BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( 0 );
00088       return;
00089     }
00090   }
00091 
00092   KMFolder mailFolder(0, location(), KMFolderTypeMaildir);
00093 
00094   long num = 0;
00095   long i;
00096   int rc;
00097   KMMessage* msg;
00098   bool addedOk;
00099 
00100   if (!mFolder) {
00101     checkDone( hasNewMail, CheckError );
00102     BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
00103     return;
00104   }
00105 
00106   BroadcastStatus::instance()->setStatusMsg(
00107     i18n("Preparing transmission from \"%1\"...").arg(mName));
00108 
00109   Q_ASSERT( !mMailCheckProgressItem );
00110   mMailCheckProgressItem = KPIM::ProgressManager::createProgressItem(
00111     "MailCheck" + mName,
00112     mName,
00113     i18n("Preparing transmission from \"%1\"...").arg(mName),
00114     false, // cannot be canceled
00115     false ); // no tls/ssl
00116 
00117   // run the precommand
00118   if (!runPrecommand(precommand()))
00119   {
00120     kdDebug(5006) << "cannot run precommand " << precommand() << endl;
00121     checkDone( hasNewMail, CheckError );
00122   }
00123 
00124   mailFolder.setAutoCreateIndex(FALSE);
00125 
00126   rc = mailFolder.open();
00127   if (rc)
00128   {
00129     QString aStr = i18n("<qt>Cannot open folder <b>%1</b>.</qt>").arg( mailFolder.location() );
00130     KMessageBox::sorry(0, aStr);
00131     kdDebug(5006) << "cannot open folder " << mailFolder.location() << endl;
00132     checkDone( hasNewMail, CheckError );
00133     BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
00134     return;
00135   }
00136 
00137   mFolder->open();
00138 
00139 
00140   num = mailFolder.count();
00141 
00142   addedOk = true;
00143   t.start();
00144 
00145   // prepare the static parts of the status message:
00146   QString statusMsgStub = i18n("Moving message %3 of %2 from %1.")
00147     .arg(mailFolder.location()).arg(num);
00148 
00149   mMailCheckProgressItem->setTotalItems( num );
00150 
00151   for (i=0; i<num; i++)
00152   {
00153 
00154     if( kmkernel->mailCheckAborted() ) {
00155       BroadcastStatus::instance()->setStatusMsg( i18n("Transmission aborted.") );
00156       num = i;
00157       addedOk = false;
00158     }
00159     if (!addedOk) break;
00160 
00161     QString statusMsg = statusMsgStub.arg(i);
00162     mMailCheckProgressItem->incCompletedItems();
00163     mMailCheckProgressItem->updateProgress();
00164     mMailCheckProgressItem->setStatus( statusMsg );
00165 
00166     msg = mailFolder.take(0);
00167     if (msg)
00168     {
00169       msg->setStatus(msg->headerField("Status").latin1(),
00170         msg->headerField("X-Status").latin1());
00171       msg->setEncryptionStateChar( msg->headerField( "X-KMail-EncryptionState" ).at(0));
00172       msg->setSignatureStateChar( msg->headerField( "X-KMail-SignatureState" ).at(0));
00173 
00174       addedOk = processNewMsg(msg);
00175       if (addedOk)
00176         hasNewMail = true;
00177     }
00178 
00179     if (t.elapsed() >= 200) { //hardwired constant
00180       kapp->processEvents();
00181       t.start();
00182     }
00183 
00184   }
00185 
00186   if( mMailCheckProgressItem ) { // do this only once...
00187     BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( num );
00188     // FIXME Message reused from KMAcctExpPop, due to feature freeze
00189     mMailCheckProgressItem->setStatus(
00190       i18n( "Fetched 1 message from %1. Terminating transmission...",
00191             "Fetched %n messages from %1. Terminating transmission...",
00192             num )
00193       .arg( "localhost" ) );
00194     mMailCheckProgressItem->setComplete();
00195     mMailCheckProgressItem = 0;
00196   }
00197   if (addedOk)
00198   {
00199     BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( num );
00200   }
00201   // else warning is written already
00202 
00203   mailFolder.close();
00204   mFolder->close();
00205 
00206   checkDone( hasNewMail, CheckOK );
00207 
00208   return;
00209 }
00210 
00211 
00212 //-----------------------------------------------------------------------------
00213 void KMAcctMaildir::readConfig(KConfig& config)
00214 {
00215   KMAccount::readConfig(config);
00216   mLocation = config.readPathEntry("Location", mLocation);
00217 }
00218 
00219 
00220 //-----------------------------------------------------------------------------
00221 void KMAcctMaildir::writeConfig(KConfig& config)
00222 {
00223   KMAccount::writeConfig(config);
00224   config.writePathEntry("Location", mLocation);
00225 }
00226 
00227 //-----------------------------------------------------------------------------
00228 void KMAcctMaildir::setLocation(const QString& aLocation)
00229 {
00230   mLocation = aLocation;
00231 }
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