00001
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(AccountManager* 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) {
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( mName, 0 );
00112 checkDone( mHasNewMail, CheckOK );
00113 return false;
00114 }
00115 }
00116
00117 mMailFolder = new KMFolder( 0, location(), KMFolderTypeMbox,
00118 false , false );
00119 KMFolderMbox* mboxStorage =
00120 static_cast<KMFolderMbox*>(mMailFolder->storage());
00121 mboxStorage->setLockType( mLock );
00122 if ( mLock == procmail_lockfile)
00123 mboxStorage->setProcmailLockFileName( mProcmailLockFileName );
00124
00125 if (!mFolder) {
00126 checkDone( mHasNewMail, CheckError );
00127 BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
00128 return false;
00129 }
00130
00131
00132 BroadcastStatus::instance()->setStatusMsg(
00133 i18n("Preparing transmission from \"%1\"...").arg(mName));
00134
00135
00136 Q_ASSERT( !mMailCheckProgressItem );
00137 mMailCheckProgressItem = KPIM::ProgressManager::createProgressItem(
00138 "MailCheck" + mName,
00139 mName,
00140 i18n("Preparing transmission from \"%1\"...").arg(mName),
00141 false,
00142 false );
00143
00144
00145 if (!runPrecommand(precommand()))
00146 {
00147 kdDebug(5006) << "cannot run precommand " << precommand() << endl;
00148 checkDone( mHasNewMail, CheckError );
00149 BroadcastStatus::instance()->setStatusMsg( i18n( "Running precommand failed." ));
00150 return false;
00151 }
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
00183 mStatusMsgStub = i18n("Moving message %3 of %2 from %1.")
00184 .arg(mMailFolder->location()).arg( mNumMsgs );
00185
00186
00187 return true;
00188 }
00189
00190
00191
00192 bool KMAcctLocal::fetchMsg()
00193 {
00194 KMMessage* msg;
00195
00196
00197
00198
00199 const QString statusMsg = mStatusMsgStub.arg( mMsgsFetched );
00200
00201 mMailCheckProgressItem->incCompletedItems();
00202 mMailCheckProgressItem->updateProgress();
00203 mMailCheckProgressItem->setStatus( statusMsg );
00204
00205 msg = mMailFolder->take(0);
00206 if (msg)
00207 {
00208 #if 0
00209
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();
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 ) {
00255 BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( mName, mNumMsgs );
00256 mMailCheckProgressItem->setStatus(
00257 i18n( "Fetched 1 message from mailbox %1.",
00258 "Fetched %n messages from mailbox %1.",
00259 mNumMsgs ).arg( mMailFolder->location() ) );
00260 mMailCheckProgressItem->setComplete();
00261 mMailCheckProgressItem = 0;
00262 }
00263 }
00264
00265
00266 mMailFolder->close();
00267 delete mMailFolder; mMailFolder = 0;
00268
00269 mFolder->close();
00270
00271 checkDone( mHasNewMail, CheckOK );
00272 }
00273
00274
00275
00276 void KMAcctLocal::readConfig(KConfig& config)
00277 {
00278 KMAccount::readConfig(config);
00279 mLocation = config.readPathEntry("Location", mLocation);
00280 QString locktype = config.readEntry("LockType", "procmail_lockfile" );
00281
00282 if( locktype == "procmail_lockfile" ) {
00283 mLock = procmail_lockfile;
00284 mProcmailLockFileName = config.readEntry("ProcmailLockFile",
00285 mLocation + ".lock");
00286 } else if( locktype == "mutt_dotlock" )
00287 mLock = mutt_dotlock;
00288 else if( locktype == "mutt_dotlock_privileged" )
00289 mLock = mutt_dotlock_privileged;
00290 else if( locktype == "none" )
00291 mLock = lock_none;
00292 else mLock = FCNTL;
00293 }
00294
00295
00296
00297 void KMAcctLocal::writeConfig(KConfig& config)
00298 {
00299 KMAccount::writeConfig(config);
00300
00301 config.writePathEntry("Location", mLocation);
00302
00303 QString st = "fcntl";
00304 if (mLock == procmail_lockfile) st = "procmail_lockfile";
00305 else if (mLock == mutt_dotlock) st = "mutt_dotlock";
00306 else if (mLock == mutt_dotlock_privileged) st = "mutt_dotlock_privileged";
00307 else if (mLock == lock_none) st = "none";
00308 config.writeEntry("LockType", st);
00309
00310 if (mLock == procmail_lockfile) {
00311 config.writeEntry("ProcmailLockFile", mProcmailLockFileName);
00312 }
00313
00314 }
00315
00316
00317
00318 void KMAcctLocal::setLocation(const QString& aLocation)
00319 {
00320 mLocation = aLocation;
00321 }
00322
00323 void KMAcctLocal::setProcmailLockFileName(const QString& s)
00324 {
00325 mProcmailLockFileName = s;
00326 }