kmail

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(AccountManager* 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( mName, 0 );
00088       return;
00089     }
00090   }
00091 
00092   KMFolder mailFolder(0, location(), KMFolderTypeMaildir,
00093                               false /* no index */, false /* don't export sernums */);
00094 
00095   long num = 0;
00096   long i;
00097   int rc;
00098   KMMessage* msg;
00099   bool addedOk;
00100 
00101   if (!mFolder) {
00102     checkDone( hasNewMail, CheckError );
00103     BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
00104     return;
00105   }
00106 
00107   BroadcastStatus::instance()->setStatusMsg(
00108     i18n("Preparing transmission from \"%1\"...").arg(mName));
00109 
00110   Q_ASSERT( !mMailCheckProgressItem );
00111   mMailCheckProgressItem = KPIM::ProgressManager::createProgressItem(
00112     "MailCheck" + mName,
00113     mName,
00114     i18n("Preparing transmission from \"%1\"...").arg(mName),
00115     false, // cannot be canceled
00116     false ); // no tls/ssl
00117 
00118   // run the precommand
00119   if (!runPrecommand(precommand()))
00120   {
00121     kdDebug(5006) << "cannot run precommand " << precommand() << endl;
00122     checkDone( hasNewMail, CheckError );
00123     BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
00124     return;
00125   }
00126 
00127   rc = mailFolder.open();
00128   if (rc)
00129   {
00130     QString aStr = i18n("<qt>Cannot open folder <b>%1</b>.</qt>").arg( mailFolder.location() );
00131     KMessageBox::sorry(0, aStr);
00132     kdDebug(5006) << "cannot open folder " << mailFolder.location() << endl;
00133     checkDone( hasNewMail, CheckError );
00134     BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
00135     return;
00136   }
00137 
00138   mFolder->open();
00139 
00140 
00141   num = mailFolder.count();
00142 
00143   addedOk = true;
00144   t.start();
00145 
00146   // prepare the static parts of the status message:
00147   QString statusMsgStub = i18n("Moving message %3 of %2 from %1.")
00148     .arg(mailFolder.location()).arg(num);
00149 
00150   mMailCheckProgressItem->setTotalItems( num );
00151 
00152   for (i=0; i<num; i++)
00153   {
00154 
00155     if( kmkernel->mailCheckAborted() ) {
00156       BroadcastStatus::instance()->setStatusMsg( i18n("Transmission aborted.") );
00157       num = i;
00158       addedOk = false;
00159     }
00160     if (!addedOk) break;
00161 
00162     QString statusMsg = statusMsgStub.arg(i);
00163     mMailCheckProgressItem->incCompletedItems();
00164     mMailCheckProgressItem->updateProgress();
00165     mMailCheckProgressItem->setStatus( statusMsg );
00166 
00167     msg = mailFolder.take(0);
00168     if (msg)
00169     {
00170       msg->setStatus(msg->headerField("Status").latin1(),
00171         msg->headerField("X-Status").latin1());
00172       msg->setEncryptionStateChar( msg->headerField( "X-KMail-EncryptionState" ).at(0));
00173       msg->setSignatureStateChar( msg->headerField( "X-KMail-SignatureState" ).at(0));
00174 
00175       addedOk = processNewMsg(msg);
00176       if (addedOk)
00177         hasNewMail = true;
00178     }
00179 
00180     if (t.elapsed() >= 200) { //hardwired constant
00181       kapp->processEvents();
00182       t.start();
00183     }
00184 
00185   }
00186 
00187   if( mMailCheckProgressItem ) { // do this only once...
00188     BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( num );
00189     mMailCheckProgressItem->setStatus(
00190       i18n( "Fetched 1 message from maildir folder %1.",
00191             "Fetched %n messages from maildir folder %1.",
00192             num ).arg(mailFolder.location() ) );
00193 
00194     mMailCheckProgressItem->setComplete();
00195     mMailCheckProgressItem = 0;
00196   }
00197   if (addedOk)
00198   {
00199     BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( mName, 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 Home | KDE Accessibility Home | Description of Access Keys