00001
00002
00003 #include "kmacctlocal.h"
00004 #include "kmfoldermbox.h"
00005 #include "kmacctfolder.h"
00006 #include "broadcaststatus.h"
00007
00008 using KPIM::BroadcastStatus;
00009 #include "progressmanager.h"
00010 using KPIM::ProgressManager;
00011
00012 #include "kmfoldermgr.h"
00013
00014 #include <klocale.h>
00015 #include <kmessagebox.h>
00016 #include <kdebug.h>
00017 #include <kconfiggroup.h>
00018
00019 #include <QFileInfo>
00020 #include <QTime>
00021
00022 #include <stdlib.h>
00023 #include <stdio.h>
00024 #include <errno.h>
00025 #include <assert.h>
00026
00027
00028 KMAcctLocal::KMAcctLocal(AccountManager* aOwner, const QString& aAccountName, uint id):
00029 KMAccount(aOwner, aAccountName, id), mHasNewMail( false ),
00030 mProcessingNewMail( false ), mAddedOk( true ), mNumMsgs( 0 ),
00031 mMsgsFetched( 0 ), mMailFolder( 0 )
00032 {
00033 mLock = procmail_lockfile;
00034 }
00035
00036
00037
00038 KMAcctLocal::~KMAcctLocal()
00039 {
00040 }
00041
00042
00043
00044 void KMAcctLocal::init() {
00045 KMAccount::init();
00046 }
00047
00048
00049
00050 void KMAcctLocal::pseudoAssign( const KMAccount * a )
00051 {
00052 KMAccount::pseudoAssign( a );
00053
00054 const KMAcctLocal * l = dynamic_cast<const KMAcctLocal*>( a );
00055 if ( !l ) return;
00056
00057 setLocation( l->location() );
00058 setLockType( l->lockType() );
00059 setProcmailLockFileName( l->procmailLockFileName() );
00060 }
00061
00062
00063 void KMAcctLocal::processNewMail(bool)
00064 {
00065 if ( mProcessingNewMail )
00066 return;
00067
00068 mHasNewMail = false;
00069 mProcessingNewMail = true;
00070
00071 if ( !preProcess() ) {
00072 mProcessingNewMail = false;
00073 return;
00074 }
00075
00076 QTime t;
00077 t.start();
00078
00079 for ( mMsgsFetched = 0; mMsgsFetched < mNumMsgs; ++mMsgsFetched )
00080 {
00081 if ( !fetchMsg() )
00082 break;
00083
00084 if (t.elapsed() >= 200) {
00085 qApp->processEvents();
00086 t.start();
00087 }
00088 }
00089
00090 postProcess();
00091 mProcessingNewMail = false;
00092 }
00093
00094
00095
00096 bool KMAcctLocal::preProcess()
00097 {
00098 if ( precommand().isEmpty() ) {
00099 QFileInfo fi( location() );
00100 if ( fi.size() == 0 ) {
00101 BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( mName, 0 );
00102 checkDone( mHasNewMail, CheckOK );
00103 return false;
00104 }
00105 }
00106
00107 mMailFolder = new KMFolder( 0, location(), KMFolderTypeMbox,
00108 false , false );
00109 KMFolderMbox* mboxStorage =
00110 static_cast<KMFolderMbox*>(mMailFolder->storage());
00111 mboxStorage->setLockType( mLock );
00112 if ( mLock == procmail_lockfile)
00113 mboxStorage->setProcmailLockFileName( mProcmailLockFileName );
00114
00115 if (!mFolder) {
00116 checkDone( mHasNewMail, CheckError );
00117 BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
00118 return false;
00119 }
00120
00121
00122 BroadcastStatus::instance()->setStatusMsg(
00123 i18n("Preparing transmission from \"%1\"...", mName));
00124
00125
00126 Q_ASSERT( !mMailCheckProgressItem );
00127 mMailCheckProgressItem = KPIM::ProgressManager::createProgressItem(
00128 "MailCheck" + mName,
00129 mName,
00130 i18n("Preparing transmission from \"%1\"...", mName),
00131 false,
00132 false );
00133
00134
00135 if (!runPrecommand(precommand()))
00136 {
00137 kDebug(5006) <<"cannot run precommand" << precommand();
00138 checkDone( mHasNewMail, CheckError );
00139 BroadcastStatus::instance()->setStatusMsg( i18n( "Running precommand failed." ));
00140 return false;
00141 }
00142
00143 const int rc = mMailFolder->open( "acctlocalMail" );
00144 if ( rc != 0 ) {
00145 QString aStr;
00146 aStr = i18n("Cannot open file:");
00147 aStr += mMailFolder->path() + '/' + mMailFolder->name();
00148 KMessageBox::sorry(0, aStr);
00149 kDebug(5006) <<"cannot open file" << mMailFolder->path() <<"/"
00150 << mMailFolder->name();
00151 checkDone( mHasNewMail, CheckError );
00152 BroadcastStatus::instance()->setStatusMsg( i18n( "Transmission failed." ));
00153 return false;
00154 }
00155
00156 if (!mboxStorage->isLocked()) {
00157 kDebug(5006) <<"mailFolder could not be locked";
00158 mMailFolder->close( "acctlocalMail" );
00159 checkDone( mHasNewMail, CheckError );
00160 QString errMsg = i18n( "Transmission failed: Could not lock %1.",
00161 mMailFolder->location() );
00162 BroadcastStatus::instance()->setStatusMsg( errMsg );
00163 return false;
00164 }
00165
00166 mFolder->open( "acctlocalFold" );
00167
00168 mNumMsgs = mMailFolder->count();
00169
00170 mMailCheckProgressItem->setTotalItems( mNumMsgs );
00171
00172
00173 return true;
00174 }
00175
00176
00177
00178 bool KMAcctLocal::fetchMsg()
00179 {
00180 KMMessage* msg;
00181
00182
00183
00184
00185 const QString statusMsg = i18n( "Moving message %1 of %2 from %3.",
00186 mMsgsFetched, mNumMsgs,
00187 mMailFolder->location() );
00188
00189 mMailCheckProgressItem->incCompletedItems();
00190 mMailCheckProgressItem->updateProgress();
00191 mMailCheckProgressItem->setStatus( statusMsg );
00192
00193 msg = mMailFolder->take(0);
00194 if (msg)
00195 {
00196 #if 0
00197
00198 QFile fileD0( "testdat_xx-0-0" );
00199 if( fileD0.open( QIODevice::WriteOnly ) ) {
00200 QByteArray s = msg->asString();
00201 uint l = s.length();
00202 if ( l > 0 ) {
00203 QDataStream ds( &fileD0 );
00204 ds.writeRawData( s.data(), l );
00205 }
00206 fileD0.close();
00207 }
00208 #endif
00209 msg->setStatus( msg->headerField( "Status" ).toLatin1(),
00210 msg->headerField( "X-Status" ).toLatin1());
00211 if ( !msg->headerField( "X-KMail-EncryptionState" ).isEmpty() )
00212 msg->setEncryptionStateChar( msg->headerField( "X-KMail-EncryptionState" ).at(0) );
00213 if ( !msg->headerField( "X-KMail-SignatureState" ).isEmpty() )
00214 msg->setSignatureStateChar( msg->headerField( "X-KMail-SignatureState" ).at(0));
00215 msg->setComplete(true);
00216 msg->updateAttachmentState();
00217
00218 mAddedOk = processNewMsg(msg);
00219
00220 if (mAddedOk)
00221 mHasNewMail = true;
00222
00223 return mAddedOk;
00224 }
00225 return true;
00226 }
00227
00228
00229
00230 void KMAcctLocal::postProcess()
00231 {
00232 if (mAddedOk)
00233 {
00234 kmkernel->folderMgr()->syncAllFolders();
00235 const int rc = mMailFolder->expunge();
00236 if ( rc != 0 ) {
00237 KMessageBox::queuedMessageBox( 0, KMessageBox::Information,
00238 i18n( "<qt>Cannot remove mail from "
00239 "mailbox <b>%1</b>:<br />%2</qt>",
00240 mMailFolder->location(),
00241 strerror( rc ) ) );
00242 }
00243
00244 if( mMailCheckProgressItem ) {
00245 BroadcastStatus::instance()->setStatusMsgTransmissionCompleted( mName, mNumMsgs );
00246 mMailCheckProgressItem->setStatus(
00247 i18np( "Fetched 1 message from mailbox %2.",
00248 "Fetched %1 messages from mailbox %2.",
00249 mNumMsgs, mMailFolder->location() ) );
00250 mMailCheckProgressItem->setComplete();
00251 mMailCheckProgressItem = 0;
00252 }
00253 }
00254
00255
00256 mMailFolder->close( "acctLocalMail" );
00257 delete mMailFolder; mMailFolder = 0;
00258
00259 mFolder->close( "acctlocalFold" );
00260
00261 checkDone( mHasNewMail, CheckOK );
00262 }
00263
00264
00265
00266 void KMAcctLocal::readConfig(KConfigGroup& config)
00267 {
00268 KMAccount::readConfig(config);
00269 mLocation = config.readPathEntry("Location", mLocation);
00270 QString locktype = config.readEntry("LockType", "procmail_lockfile" );
00271
00272 if( locktype == "procmail_lockfile" ) {
00273 mLock = procmail_lockfile;
00274 mProcmailLockFileName = config.readEntry("ProcmailLockFile",
00275 mLocation + ".lock");
00276 } else if( locktype == "mutt_dotlock" )
00277 mLock = mutt_dotlock;
00278 else if( locktype == "mutt_dotlock_privileged" )
00279 mLock = mutt_dotlock_privileged;
00280 else if( locktype == "none" )
00281 mLock = lock_none;
00282 else mLock = FCNTL;
00283 }
00284
00285
00286
00287 void KMAcctLocal::writeConfig(KConfigGroup& config)
00288 {
00289 KMAccount::writeConfig(config);
00290
00291 config.writePathEntry("Location", mLocation);
00292
00293 QString st = "fcntl";
00294 if (mLock == procmail_lockfile) st = "procmail_lockfile";
00295 else if (mLock == mutt_dotlock) st = "mutt_dotlock";
00296 else if (mLock == mutt_dotlock_privileged) st = "mutt_dotlock_privileged";
00297 else if (mLock == lock_none) st = "none";
00298 config.writeEntry("LockType", st);
00299
00300 if (mLock == procmail_lockfile) {
00301 config.writeEntry("ProcmailLockFile", mProcmailLockFileName);
00302 }
00303
00304 }
00305
00306
00307
00308 void KMAcctLocal::setLocation(const QString& aLocation)
00309 {
00310 mLocation = aLocation;
00311 }
00312
00313 void KMAcctLocal::setProcmailLockFileName(const QString& s)
00314 {
00315 mProcmailLockFileName = s;
00316 }