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(KMAcctMgr* 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( 0 );
00112 checkDone( mHasNewMail, CheckOK );
00113
return false;
00114 }
00115 }
00116
00117 mMailFolder =
new KMFolder( 0,
location(), KMFolderTypeMbox );
00118 KMFolderMbox* mboxStorage =
00119 static_cast<KMFolderMbox*>(mMailFolder->storage());
00120 mboxStorage->setLockType( mLock );
00121
if ( mLock == procmail_lockfile)
00122 mboxStorage->setProcmailLockFileName( mProcmailLockFileName );
00123
00124
if (!mFolder) {
00125 checkDone( mHasNewMail, CheckError );
00126 BroadcastStatus::instance()->setStatusMsg( i18n(
"Transmission failed." ));
00127
return false;
00128 }
00129
00130
00131 BroadcastStatus::instance()->setStatusMsg(
00132 i18n(
"Preparing transmission from \"%1\"...").arg(mName));
00133
00134
00135 Q_ASSERT( !mMailCheckProgressItem );
00136 mMailCheckProgressItem = KPIM::ProgressManager::createProgressItem(
00137
"MailCheck" + mName,
00138 mName,
00139 i18n(
"Preparing transmission from \"%1\"...").arg(mName),
00140
false,
00141
false );
00142
00143
00144
if (!runPrecommand(precommand()))
00145 {
00146 kdDebug(5006) <<
"cannot run precommand " << precommand() << endl;
00147 checkDone( mHasNewMail, CheckError );
00148 }
00149
00150 mMailFolder->setAutoCreateIndex(FALSE);
00151
00152
const int rc = mMailFolder->open();
00153
if ( rc != 0 ) {
00154
QString aStr;
00155 aStr = i18n(
"Cannot open file:");
00156 aStr += mMailFolder->path()+
"/"+mMailFolder->name();
00157 KMessageBox::sorry(0, aStr);
00158 kdDebug(5006) <<
"cannot open file " << mMailFolder->path() <<
"/"
00159 << mMailFolder->name() << endl;
00160 checkDone( mHasNewMail, CheckError );
00161 BroadcastStatus::instance()->setStatusMsg( i18n(
"Transmission failed." ));
00162
return false;
00163 }
00164
00165
if (!mboxStorage->isLocked()) {
00166 kdDebug(5006) <<
"mailFolder could not be locked" << endl;
00167 mMailFolder->close();
00168 checkDone( mHasNewMail, CheckError );
00169
QString errMsg = i18n(
"Transmission failed: Could not lock %1." )
00170 .arg( mMailFolder->location() );
00171 BroadcastStatus::instance()->setStatusMsg( errMsg );
00172
return false;
00173 }
00174
00175 mFolder->open();
00176
00177 mNumMsgs = mMailFolder->count();
00178
00179 mMailCheckProgressItem->setTotalItems( mNumMsgs );
00180
00181
00182 mStatusMsgStub = i18n(
"Moving message %3 of %2 from %1.")
00183 .arg(mMailFolder->location()).arg( mNumMsgs );
00184
00185
00186
return true;
00187 }
00188
00189
00190
00191
bool KMAcctLocal::fetchMsg()
00192 {
00193 KMMessage* msg;
00194
00195
00196
00197
00198
const QString statusMsg = mStatusMsgStub.arg( mMsgsFetched );
00199
00200 mMailCheckProgressItem->incCompletedItems();
00201 mMailCheckProgressItem->updateProgress();
00202 mMailCheckProgressItem->setStatus( statusMsg );
00203
00204 msg = mMailFolder->take(0);
00205
if (msg)
00206 {
00207
#if 0
00208
00209
QFile fileD0(
"testdat_xx-0-0" );
00210
if( fileD0.open( IO_WriteOnly ) ) {
00211
QCString s = msg->asString();
00212 uint l = s.length();
00213
if ( l > 0 ) {
00214
QDataStream ds( &fileD0 );
00215 ds.writeRawBytes( s.data(), l );
00216 }
00217 fileD0.close();
00218 }
00219
#endif
00220
msg->setStatus(msg->headerField(
"Status").latin1(),
00221 msg->headerField(
"X-Status").latin1());
00222 msg->setEncryptionStateChar( msg->headerField(
"X-KMail-EncryptionState" ).at(0) );
00223 msg->setSignatureStateChar( msg->headerField(
"X-KMail-SignatureState" ).at(0));
00224 msg->setComplete(
true);
00225 msg->updateAttachmentState();
00226
00227 mAddedOk = processNewMsg(msg);
00228
00229
if (mAddedOk)
00230 mHasNewMail =
true;
00231
00232
return mAddedOk;
00233 }
00234
return true;
00235 }
00236
00237
00238
00239
void KMAcctLocal::postProcess()
00240 {
00241
if (mAddedOk)
00242 {
00243 kmkernel->folderMgr()->syncAllFolders();
00244
const int rc = mMailFolder->expunge();
00245
if ( rc != 0 ) {
00246 KMessageBox::queuedMessageBox( 0, KMessageBox::Information,
00247 i18n(
"<qt>Cannot remove mail from "
00248
"mailbox <b>%1</b>:<br>%2</qt>" )
00249 .arg( mMailFolder->location() )
00250 .arg( strerror( rc ) ) );
00251 }
00252
00253
if( mMailCheckProgressItem ) {
00254 BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( mNumMsgs );
00255
00256 mMailCheckProgressItem->setStatus(
00257 i18n(
"Fetched 1 message from %1. Terminating transmission...",
00258
"Fetched %n messages from %1. Terminating transmission...",
00259 mNumMsgs )
00260 .arg(
"localhost" ) );
00261 mMailCheckProgressItem->setComplete();
00262 mMailCheckProgressItem = 0;
00263 }
00264 }
00265
00266
00267 mMailFolder->close();
00268
delete mMailFolder; mMailFolder = 0;
00269
00270 mFolder->close();
00271
00272 checkDone( mHasNewMail, CheckOK );
00273 }
00274
00275
00276
00277
void KMAcctLocal::readConfig(KConfig& config)
00278 {
00279 KMAccount::readConfig(config);
00280 mLocation = config.readPathEntry(
"Location", mLocation);
00281
QString locktype = config.readEntry(
"LockType",
"procmail_lockfile" );
00282
00283
if( locktype ==
"procmail_lockfile" ) {
00284 mLock = procmail_lockfile;
00285 mProcmailLockFileName = config.readEntry(
"ProcmailLockFile",
00286 mLocation +
".lock");
00287 }
else if( locktype ==
"mutt_dotlock" )
00288 mLock = mutt_dotlock;
00289
else if( locktype ==
"mutt_dotlock_privileged" )
00290 mLock = mutt_dotlock_privileged;
00291
else if( locktype ==
"none" )
00292 mLock = lock_none;
00293
else mLock = FCNTL;
00294 }
00295
00296
00297
00298
void KMAcctLocal::writeConfig(KConfig& config)
00299 {
00300 KMAccount::writeConfig(config);
00301
00302 config.writePathEntry(
"Location", mLocation);
00303
00304
QString st =
"fcntl";
00305
if (mLock == procmail_lockfile) st =
"procmail_lockfile";
00306
else if (mLock == mutt_dotlock) st =
"mutt_dotlock";
00307
else if (mLock == mutt_dotlock_privileged) st =
"mutt_dotlock_privileged";
00308
else if (mLock == lock_none) st =
"none";
00309 config.writeEntry(
"LockType", st);
00310
00311
if (mLock == procmail_lockfile) {
00312 config.writeEntry(
"ProcmailLockFile", mProcmailLockFileName);
00313 }
00314
00315 }
00316
00317
00318
00319
void KMAcctLocal::setLocation(
const QString& aLocation)
00320 {
00321 mLocation = aLocation;
00322 }
00323
00324
void KMAcctLocal::setProcmailLockFileName(
const QString& s)
00325 {
00326 mProcmailLockFileName = s;
00327 }