kmail

kmfoldermaildir.cpp

00001 // -*- mode: C++; c-file-style: "gnu" -*-
00002 // kmfoldermaildir.cpp
00003 // Author: Kurt Granroth <granroth@kde.org>
00004 
00005 #ifdef HAVE_CONFIG_H
00006 #include <config.h>
00007 #endif
00008 
00009 #include <qdir.h>
00010 #include <qregexp.h>
00011 
00012 #include <libkdepim/kfileio.h>
00013 #include "kmfoldermaildir.h"
00014 #include "kmfoldermgr.h"
00015 #include "kmfolder.h"
00016 #include "undostack.h"
00017 #include "maildirjob.h"
00018 #include "kcursorsaver.h"
00019 #include "jobscheduler.h"
00020 using KMail::MaildirJob;
00021 #include "compactionjob.h"
00022 #include "kmmsgdict.h"
00023 #include "util.h"
00024 
00025 #include <kapplication.h>
00026 #include <kdebug.h>
00027 #include <klocale.h>
00028 #include <kstaticdeleter.h>
00029 #include <kmessagebox.h>
00030 
00031 #include <dirent.h>
00032 #include <errno.h>
00033 #include <stdlib.h>
00034 #include <sys/stat.h>
00035 #include <sys/types.h>
00036 #include <unistd.h>
00037 #include <assert.h>
00038 #include <limits.h>
00039 #include <unistd.h>
00040 #include <fcntl.h>
00041 
00042 #ifndef MAX_LINE
00043 #define MAX_LINE 4096
00044 #endif
00045 #ifndef INIT_MSGS
00046 #define INIT_MSGS 8
00047 #endif
00048 
00049 
00050 //-----------------------------------------------------------------------------
00051 KMFolderMaildir::KMFolderMaildir(KMFolder* folder, const char* name)
00052   : KMFolderIndex(folder, name)
00053 {
00054 
00055 }
00056 
00057 
00058 //-----------------------------------------------------------------------------
00059 KMFolderMaildir::~KMFolderMaildir()
00060 {
00061   if (mOpenCount>0) close("~foldermaildir", true);
00062   if (kmkernel->undoStack()) kmkernel->undoStack()->folderDestroyed( folder() );
00063 }
00064 
00065 //-----------------------------------------------------------------------------
00066 int KMFolderMaildir::canAccess()
00067 {
00068 
00069   assert(!folder()->name().isEmpty());
00070 
00071   QString sBadFolderName;
00072   if (access(QFile::encodeName(location()), R_OK | W_OK | X_OK) != 0) {
00073     sBadFolderName = location();
00074   } else if (access(QFile::encodeName(location() + "/new"), R_OK | W_OK | X_OK) != 0) {
00075     sBadFolderName = location() + "/new";
00076   } else if (access(QFile::encodeName(location() + "/cur"), R_OK | W_OK | X_OK) != 0) {
00077     sBadFolderName = location() + "/cur";
00078   } else if (access(QFile::encodeName(location() + "/tmp"), R_OK | W_OK | X_OK) != 0) {
00079     sBadFolderName = location() + "/tmp";
00080   }
00081 
00082   if ( !sBadFolderName.isEmpty() ) {
00083     int nRetVal = QFile::exists(sBadFolderName) ? EPERM : ENOENT;
00084     KCursorSaver idle(KBusyPtr::idle());
00085     if ( nRetVal == ENOENT )
00086       KMessageBox::sorry(0, i18n("Error opening %1; this folder is missing.")
00087                          .arg(sBadFolderName));
00088     else
00089       KMessageBox::sorry(0, i18n("Error opening %1; either this is not a valid "
00090                                  "maildir folder, or you do not have sufficient access permissions.")
00091                          .arg(sBadFolderName));
00092     return nRetVal;
00093   }
00094 
00095   return 0;
00096 }
00097 
00098 //-----------------------------------------------------------------------------
00099 int KMFolderMaildir::open(const char *)
00100 {
00101   int rc = 0;
00102 
00103   mOpenCount++;
00104   kmkernel->jobScheduler()->notifyOpeningFolder( folder() );
00105 
00106   if (mOpenCount > 1) return 0;  // already open
00107 
00108   assert(!folder()->name().isEmpty());
00109 
00110   rc = canAccess();
00111   if ( rc != 0 ) {
00112       return rc;
00113   }
00114 
00115   if (!folder()->path().isEmpty())
00116   {
00117     if (KMFolderIndex::IndexOk != indexStatus()) // test if contents file has changed
00118     {
00119       QString str;
00120       mIndexStream = 0;
00121       str = i18n("Folder `%1' changed; recreating index.")
00122           .arg(name());
00123       emit statusMsg(str);
00124     } else {
00125       mIndexStream = fopen(QFile::encodeName(indexLocation()), "r+"); // index file
00126       if ( mIndexStream ) {
00127         fcntl(fileno(mIndexStream), F_SETFD, FD_CLOEXEC);
00128         updateIndexStreamPtr();
00129       }
00130     }
00131 
00132     if (!mIndexStream)
00133       rc = createIndexFromContents();
00134     else
00135       readIndex();
00136   }
00137   else
00138   {
00139     mAutoCreateIndex = false;
00140     rc = createIndexFromContents();
00141   }
00142 
00143   mChanged = false;
00144 
00145   //readConfig();
00146 
00147   return rc;
00148 }
00149 
00150 
00151 //-----------------------------------------------------------------------------
00152 int KMFolderMaildir::createMaildirFolders( const QString & folderPath )
00153 {
00154   // Make sure that neither a new, cur or tmp subfolder exists already.
00155   QFileInfo dirinfo;
00156   dirinfo.setFile( folderPath + "/new" );
00157   if ( dirinfo.exists() ) return EEXIST;
00158   dirinfo.setFile( folderPath + "/cur" );
00159   if ( dirinfo.exists() ) return EEXIST;
00160   dirinfo.setFile( folderPath + "/tmp" );
00161   if ( dirinfo.exists() ) return EEXIST;
00162 
00163   // create the maildir directory structure
00164   if ( ::mkdir( QFile::encodeName( folderPath ), S_IRWXU ) > 0 ) {
00165     kdDebug(5006) << "Could not create folder " << folderPath << endl;
00166     return errno;
00167   }
00168   if ( ::mkdir( QFile::encodeName( folderPath + "/new" ), S_IRWXU ) > 0 ) {
00169     kdDebug(5006) << "Could not create folder " << folderPath << "/new" << endl;
00170     return errno;
00171   }
00172   if ( ::mkdir( QFile::encodeName( folderPath + "/cur" ), S_IRWXU ) > 0 ) {
00173     kdDebug(5006) << "Could not create folder " << folderPath << "/cur" << endl;
00174     return errno;
00175   }
00176   if ( ::mkdir( QFile::encodeName( folderPath + "/tmp" ), S_IRWXU ) > 0 ) {
00177     kdDebug(5006) << "Could not create folder " << folderPath << "/tmp" << endl;
00178     return errno;
00179   }
00180 
00181   return 0; // no error
00182 }
00183 
00184 //-----------------------------------------------------------------------------
00185 int KMFolderMaildir::create()
00186 {
00187   int rc;
00188   int old_umask;
00189 
00190   assert(!folder()->name().isEmpty());
00191   assert(mOpenCount == 0);
00192 
00193   rc = createMaildirFolders( location() );
00194   if ( rc != 0 )
00195     return rc;
00196 
00197   // FIXME no path == no index? - till
00198   if (!folder()->path().isEmpty())
00199   {
00200     old_umask = umask(077);
00201     mIndexStream = fopen(QFile::encodeName(indexLocation()), "w+"); //sven; open RW
00202     updateIndexStreamPtr(true);
00203     umask(old_umask);
00204 
00205     if (!mIndexStream) return errno;
00206     fcntl(fileno(mIndexStream), F_SETFD, FD_CLOEXEC);
00207   }
00208   else
00209   {
00210     mAutoCreateIndex = false;
00211   }
00212 
00213   mOpenCount++;
00214   mChanged = false;
00215 
00216   rc = writeIndex();
00217   return rc;
00218 }
00219 
00220 
00221 //-----------------------------------------------------------------------------
00222 void KMFolderMaildir::reallyDoClose(const char *)
00223 {
00224   if (mAutoCreateIndex)
00225   {
00226       updateIndex();
00227       writeConfig();
00228   }
00229 
00230   mMsgList.clear(true);
00231 
00232     if (mIndexStream) {
00233     fclose(mIndexStream);
00234     updateIndexStreamPtr(true);
00235     }
00236 
00237   mOpenCount   = 0;
00238   mIndexStream = 0;
00239   mUnreadMsgs  = -1;
00240 
00241   mMsgList.reset(INIT_MSGS);
00242 }
00243 
00244 //-----------------------------------------------------------------------------
00245 void KMFolderMaildir::sync()
00246 {
00247   if (mOpenCount > 0)
00248     if (!mIndexStream || fsync(fileno(mIndexStream))) {
00249     kmkernel->emergencyExit( i18n("Could not sync maildir folder.") );
00250     }
00251 }
00252 
00253 //-----------------------------------------------------------------------------
00254 int KMFolderMaildir::expungeContents()
00255 {
00256   // nuke all messages in this folder now
00257   QDir d(location() + "/new");
00258   // d.setFilter(QDir::Files); coolo: QFile::remove returns false for non-files
00259   QStringList files(d.entryList());
00260   QStringList::ConstIterator it(files.begin());
00261   for ( ; it != files.end(); ++it)
00262     QFile::remove(d.filePath(*it));
00263 
00264   d.setPath(location() + "/cur");
00265   files = d.entryList();
00266   for (it = files.begin(); it != files.end(); ++it)
00267     QFile::remove(d.filePath(*it));
00268 
00269   return 0;
00270 }
00271 
00272 int KMFolderMaildir::compact( unsigned int startIndex, int nbMessages, const QStringList& entryList, bool& done )
00273 {
00274   QString subdirNew(location() + "/new/");
00275   QString subdirCur(location() + "/cur/");
00276 
00277   unsigned int stopIndex = nbMessages == -1 ? mMsgList.count() :
00278                            QMIN( mMsgList.count(), startIndex + nbMessages );
00279   //kdDebug(5006) << "KMFolderMaildir: compacting from " << startIndex << " to " << stopIndex << endl;
00280   for(unsigned int idx = startIndex; idx < stopIndex; ++idx) {
00281     KMMsgInfo* mi = (KMMsgInfo*)mMsgList.at(idx);
00282     if (!mi)
00283       continue;
00284 
00285     QString filename(mi->fileName());
00286     if (filename.isEmpty())
00287       continue;
00288 
00289     // first, make sure this isn't in the 'new' subdir
00290     if ( entryList.contains( filename ) )
00291       moveInternal(subdirNew + filename, subdirCur + filename, mi);
00292 
00293     // construct a valid filename.  if it's already valid, then
00294     // nothing happens
00295     filename = constructValidFileName( filename, mi->status() );
00296 
00297     // if the name changed, then we need to update the actual filename
00298     if (filename != mi->fileName())
00299     {
00300       moveInternal(subdirCur + mi->fileName(), subdirCur + filename, mi);
00301       mi->setFileName(filename);
00302       setDirty( true );
00303     }
00304 
00305 #if 0
00306     // we can't have any New messages at this point
00307     if (mi->isNew())
00308     {
00309       mi->setStatus(KMMsgStatusUnread);
00310       setDirty( true );
00311     }
00312 #endif
00313   }
00314   done = ( stopIndex == mMsgList.count() );
00315   return 0;
00316 }
00317 
00318 //-----------------------------------------------------------------------------
00319 int KMFolderMaildir::compact( bool silent )
00320 {
00321   KMail::MaildirCompactionJob* job = new KMail::MaildirCompactionJob( folder(), true /*immediate*/ );
00322   int rc = job->executeNow( silent );
00323   // Note that job autodeletes itself.
00324   return rc;
00325 }
00326 
00327 //-------------------------------------------------------------
00328 FolderJob*
00329 KMFolderMaildir::doCreateJob( KMMessage *msg, FolderJob::JobType jt,
00330                               KMFolder *folder, QString, const AttachmentStrategy* ) const
00331 {
00332   MaildirJob *job = new MaildirJob( msg, jt, folder );
00333   job->setParentFolder( this );
00334   return job;
00335 }
00336 
00337 //-------------------------------------------------------------
00338 FolderJob*
00339 KMFolderMaildir::doCreateJob( QPtrList<KMMessage>& msgList, const QString& sets,
00340                               FolderJob::JobType jt, KMFolder *folder ) const
00341 {
00342   MaildirJob *job = new MaildirJob( msgList, sets, jt, folder );
00343   job->setParentFolder( this );
00344   return job;
00345 }
00346 
00347 //-------------------------------------------------------------
00348 int KMFolderMaildir::addMsg(KMMessage* aMsg, int* index_return)
00349 {
00350   if (!canAddMsgNow(aMsg, index_return)) return 0;
00351   return addMsgInternal( aMsg, index_return );
00352 }
00353 
00354 //-------------------------------------------------------------
00355 int KMFolderMaildir::addMsgInternal( KMMessage* aMsg, int* index_return,
00356                                      bool stripUid )
00357 {
00358 /*
00359 QFile fileD0( "testdat_xx-kmfoldermaildir-0" );
00360 if( fileD0.open( IO_WriteOnly ) ) {
00361     QDataStream ds( &fileD0 );
00362     ds.writeRawBytes( aMsg->asString(), aMsg->asString().length() );
00363     fileD0.close();  // If data is 0 we just create a zero length file.
00364 }
00365 */
00366   long len;
00367   unsigned long size;
00368   bool opened = false;
00369   KMFolder* msgParent;
00370   QCString msgText;
00371   int idx(-1);
00372   int rc;
00373 
00374   // take message out of the folder it is currently in, if any
00375   msgParent = aMsg->parent();
00376   if (msgParent)
00377   {
00378     if (msgParent==folder() && !kmkernel->folderIsDraftOrOutbox(folder()))
00379         return 0;
00380 
00381     idx = msgParent->find(aMsg);
00382     msgParent->getMsg( idx );
00383   }
00384 
00385   aMsg->setStatusFields();
00386   if (aMsg->headerField("Content-Type").isEmpty())  // This might be added by
00387     aMsg->removeHeaderField("Content-Type");        // the line above
00388 
00389 
00390   const QString uidHeader = aMsg->headerField( "X-UID" );
00391   if ( !uidHeader.isEmpty() && stripUid )
00392     aMsg->removeHeaderField( "X-UID" );
00393 
00394   msgText = aMsg->asString(); // TODO use asDwString instead
00395   len = msgText.length();
00396 
00397   // Re-add the uid so that the take can make use of it, in case the
00398   // message is currently in an imap folder
00399   if ( !uidHeader.isEmpty() && stripUid )
00400     aMsg->setHeaderField( "X-UID", uidHeader );
00401 
00402   if (len <= 0)
00403   {
00404     kdDebug(5006) << "Message added to folder `" << name() << "' contains no data. Ignoring it." << endl;
00405     return 0;
00406   }
00407 
00408   // make sure the filename has the correct extension
00409   QString filename = constructValidFileName( aMsg->fileName(), aMsg->status() );
00410 
00411   QString tmp_file(location() + "/tmp/");
00412   tmp_file += filename;
00413 
00414   if (!KPIM::kCStringToFile(msgText, tmp_file, false, false, false))
00415     kmkernel->emergencyExit( i18n("Message could not be added to the folder, possibly disk space is low.") );
00416 
00417   QFile file(tmp_file);
00418   size = msgText.length();
00419 
00420   if (!isOpened())
00421   {
00422     opened = true;
00423     rc = open("maildir");
00424     kdDebug(5006) << "KMFolderMaildir::addMsg-open: " << rc << " of folder: " << label() << endl;
00425     if (rc) return rc;
00426   }
00427 
00428   // now move the file to the correct location
00429   QString new_loc(location() + "/cur/");
00430   new_loc += filename;
00431   if (moveInternal(tmp_file, new_loc, filename, aMsg->status()).isNull())
00432   {
00433     file.remove();
00434     if (opened) close("maildir");
00435     return -1;
00436   }
00437 
00438   if (msgParent)
00439     if (idx >= 0) msgParent->take(idx);
00440 
00441   // just to be sure it does not end up in the index
00442   if ( stripUid ) aMsg->setUID( 0 );
00443 
00444   if (filename != aMsg->fileName())
00445     aMsg->setFileName(filename);
00446 
00447   if (aMsg->isUnread() || aMsg->isNew() || folder() == kmkernel->outboxFolder())
00448   {
00449     if (mUnreadMsgs == -1)
00450       mUnreadMsgs = 1;
00451     else
00452       ++mUnreadMsgs;
00453     if ( !mQuiet ) {
00454       kdDebug( 5006 ) << "FolderStorage::msgStatusChanged" << endl;
00455       emit numUnreadMsgsChanged( folder() );
00456     }else{
00457       if ( !mEmitChangedTimer->isActive() ) {
00458 //        kdDebug( 5006 )<< "QuietTimer started" << endl;
00459         mEmitChangedTimer->start( 3000 );
00460       }
00461       mChanged = true;
00462     }
00463   }
00464   ++mTotalMsgs;
00465 
00466   if ( aMsg->attachmentState() == KMMsgAttachmentUnknown &&
00467        aMsg->readyToShow() )
00468     aMsg->updateAttachmentState();
00469 
00470   // store information about the position in the folder file in the message
00471   aMsg->setParent(folder());
00472   aMsg->setMsgSize(size);
00473   idx = mMsgList.append( &aMsg->toMsgBase(), mExportsSernums );
00474   if (aMsg->getMsgSerNum() <= 0)
00475     aMsg->setMsgSerNum();
00476   else
00477     replaceMsgSerNum( aMsg->getMsgSerNum(), &aMsg->toMsgBase(), idx );
00478 
00479   // write index entry if desired
00480   if (mAutoCreateIndex)
00481   {
00482     assert(mIndexStream != 0);
00483     clearerr(mIndexStream);
00484     fseek(mIndexStream, 0, SEEK_END);
00485     off_t revert = ftell(mIndexStream);
00486 
00487     int len;
00488     KMMsgBase * mb = &aMsg->toMsgBase();
00489     const uchar *buffer = mb->asIndexString(len);
00490     fwrite(&len,sizeof(len), 1, mIndexStream);
00491     mb->setIndexOffset( ftell(mIndexStream) );
00492     mb->setIndexLength( len );
00493     if(fwrite(buffer, len, 1, mIndexStream) != 1)
00494     kdDebug(5006) << "Whoa! " << __FILE__ << ":" << __LINE__ << endl;
00495 
00496     fflush(mIndexStream);
00497     int error = ferror(mIndexStream);
00498 
00499     if ( mExportsSernums )
00500       error |= appendToFolderIdsFile( idx );
00501 
00502     if (error) {
00503       kdDebug(5006) << "Error: Could not add message to folder (No space left on device?)" << endl;
00504       if (ftell(mIndexStream) > revert) {
00505     kdDebug(5006) << "Undoing changes" << endl;
00506     truncate( QFile::encodeName(indexLocation()), revert );
00507       }
00508       kmkernel->emergencyExit(i18n("KMFolderMaildir::addMsg: abnormally terminating to prevent data loss."));
00509       // exit(1); // don't ever use exit(), use the above!
00510 
00511       /* This code may not be 100% reliable
00512       bool busy = kmkernel->kbp()->isBusy();
00513       if (busy) kmkernel->kbp()->idle();
00514       KMessageBox::sorry(0,
00515         i18n("Unable to add message to folder.\n"
00516          "(No space left on device or insufficient quota?)\n"
00517          "Free space and sufficient quota are required to continue safely."));
00518       if (busy) kmkernel->kbp()->busy();
00519       if (opened) close();
00520       */
00521       return error;
00522     }
00523   }
00524 
00525   // some "paper work"
00526   if (index_return)
00527     *index_return = idx;
00528 
00529   emitMsgAddedSignals(idx);
00530   needsCompact = true;
00531 
00532   if (opened) close("maildir" );
00533 /*
00534 QFile fileD1( "testdat_xx-kmfoldermaildir-1" );
00535 if( fileD1.open( IO_WriteOnly ) ) {
00536     QDataStream ds( &fileD1 );
00537     ds.writeRawBytes( aMsg->asString(), aMsg->asString().length() );
00538     fileD1.close();  // If data is 0 we just create a zero length file.
00539 }
00540 */
00541   return 0;
00542 }
00543 
00544 KMMessage* KMFolderMaildir::readMsg(int idx)
00545 {
00546   KMMsgInfo* mi = (KMMsgInfo*)mMsgList[idx];
00547   KMMessage *msg = new KMMessage(*mi); // note that mi is deleted by the line below
00548   mMsgList.set(idx,&msg->toMsgBase()); // done now so that the serial number can be computed
00549   msg->setComplete( true );
00550   msg->fromDwString(getDwString(idx));
00551   return msg;
00552 }
00553 
00554 DwString KMFolderMaildir::getDwString(int idx)
00555 {
00556   KMMsgInfo* mi = (KMMsgInfo*)mMsgList[idx];
00557   QString abs_file(location() + "/cur/");
00558   abs_file += mi->fileName();
00559   QFileInfo fi( abs_file );
00560 
00561   if (fi.exists() && fi.isFile() && fi.isWritable() && fi.size() > 0)
00562   {
00563     FILE* stream = fopen(QFile::encodeName(abs_file), "r+");
00564     if (stream) {
00565       size_t msgSize = fi.size();
00566       char* msgText = new char[ msgSize + 1 ];
00567       fread(msgText, msgSize, 1, stream);
00568       fclose( stream );
00569       msgText[msgSize] = '\0';
00570       size_t newMsgSize = KMail::Util::crlf2lf( msgText, msgSize );
00571       DwString str;
00572       // the DwString takes possession of msgText, so we must not delete it
00573       str.TakeBuffer( msgText, msgSize + 1, 0, newMsgSize );
00574       return str;
00575     }
00576   }
00577   kdDebug(5006) << "Could not open file r+ " << abs_file << endl;
00578   return DwString();
00579 }
00580 
00581 
00582 void KMFolderMaildir::readFileHeaderIntern(const QString& dir, const QString& file, KMMsgStatus status)
00583 {
00584   // we keep our current directory to restore it later
00585   char path_buffer[PATH_MAX];
00586   if(!::getcwd(path_buffer, PATH_MAX - 1))
00587     return;
00588 
00589   ::chdir(QFile::encodeName(dir));
00590 
00591   // messages in the 'cur' directory are Read by default.. but may
00592   // actually be some other state (but not New)
00593   if (status == KMMsgStatusRead)
00594   {
00595     if (file.find(":2,") == -1)
00596       status = KMMsgStatusUnread;
00597     else if (file.right(5) == ":2,RS")
00598       status |= KMMsgStatusReplied;
00599   }
00600 
00601   // open the file and get a pointer to it
00602   QFile f(file);
00603   if ( f.open( IO_ReadOnly ) == false ) {
00604     kdWarning(5006) << "The file '" << QFile::encodeName(dir) << "/" << file
00605                     << "' could not be opened for reading the message. "
00606                        "Please check ownership and permissions."
00607                     << endl;
00608     return;
00609   }
00610 
00611   char line[MAX_LINE];
00612   bool atEof    = false;
00613   bool inHeader = true;
00614   QCString *lastStr = 0;
00615 
00616   QCString dateStr, fromStr, toStr, subjStr;
00617   QCString xmarkStr, replyToIdStr, msgIdStr, referencesStr;
00618   QCString statusStr, replyToAuxIdStr, uidStr;
00619   QCString contentTypeStr, charset;
00620 
00621   // iterate through this file until done
00622   while (!atEof)
00623   {
00624     // if the end of the file has been reached or if there was an error
00625     if ( f.atEnd() || ( -1 == f.readLine(line, MAX_LINE) ) )
00626       atEof = true;
00627 
00628     // are we done with this file?  if so, compile our info and store
00629     // it in a KMMsgInfo object
00630     if (atEof || !inHeader)
00631     {
00632       msgIdStr = msgIdStr.stripWhiteSpace();
00633       if( !msgIdStr.isEmpty() ) {
00634         int rightAngle;
00635         rightAngle = msgIdStr.find( '>' );
00636         if( rightAngle != -1 )
00637           msgIdStr.truncate( rightAngle + 1 );
00638       }
00639 
00640       replyToIdStr = replyToIdStr.stripWhiteSpace();
00641       if( !replyToIdStr.isEmpty() ) {
00642         int rightAngle;
00643         rightAngle = replyToIdStr.find( '>' );
00644         if( rightAngle != -1 )
00645           replyToIdStr.truncate( rightAngle + 1 );
00646       }
00647 
00648       referencesStr = referencesStr.stripWhiteSpace();
00649       if( !referencesStr.isEmpty() ) {
00650         int leftAngle, rightAngle;
00651         leftAngle = referencesStr.findRev( '<' );
00652         if( ( leftAngle != -1 )
00653             && ( replyToIdStr.isEmpty() || ( replyToIdStr[0] != '<' ) ) ) {
00654           // use the last reference, instead of missing In-Reply-To
00655           replyToIdStr = referencesStr.mid( leftAngle );
00656         }
00657 
00658         // find second last reference
00659         leftAngle = referencesStr.findRev( '<', leftAngle - 1 );
00660         if( leftAngle != -1 )
00661           referencesStr = referencesStr.mid( leftAngle );
00662         rightAngle = referencesStr.findRev( '>' );
00663         if( rightAngle != -1 )
00664           referencesStr.truncate( rightAngle + 1 );
00665 
00666         // Store the second to last reference in the replyToAuxIdStr
00667         // It is a good candidate for threading the message below if the
00668         // message In-Reply-To points to is not kept in this folder,
00669         // but e.g. in an Outbox
00670         replyToAuxIdStr = referencesStr;
00671         rightAngle = referencesStr.find( '>' );
00672         if( rightAngle != -1 )
00673           replyToAuxIdStr.truncate( rightAngle + 1 );
00674       }
00675 
00676       statusStr = statusStr.stripWhiteSpace();
00677       if (!statusStr.isEmpty())
00678       {
00679         // only handle those states not determined by the file suffix
00680         if (statusStr[0] == 'S')
00681           status |= KMMsgStatusSent;
00682         else if (statusStr[0] == 'F')
00683           status |= KMMsgStatusForwarded;
00684         else if (statusStr[0] == 'D')
00685           status |= KMMsgStatusDeleted;
00686         else if (statusStr[0] == 'Q')
00687           status |= KMMsgStatusQueued;
00688         else if (statusStr[0] == 'G')
00689           status |= KMMsgStatusFlag;
00690       }
00691 
00692       contentTypeStr = contentTypeStr.stripWhiteSpace();
00693       charset = "";
00694       if ( !contentTypeStr.isEmpty() )
00695       {
00696         int cidx = contentTypeStr.find( "charset=" );
00697         if ( cidx != -1 ) {
00698           charset = contentTypeStr.mid( cidx + 8 );
00699           if ( !charset.isEmpty() && ( charset[0] == '"' ) ) {
00700             charset = charset.mid( 1 );
00701           }
00702           cidx = 0;
00703           while ( (unsigned int) cidx < charset.length() ) {
00704             if ( charset[cidx] == '"' || ( !isalnum(charset[cidx]) &&
00705                  charset[cidx] != '-' && charset[cidx] != '_' ) )
00706               break;
00707             ++cidx;
00708           }
00709           charset.truncate( cidx );
00710           // kdDebug() << "KMFolderMaildir::readFileHeaderIntern() charset found: " <<
00711           //              charset << " from " << contentTypeStr << endl;
00712         }
00713       }
00714 
00715       KMMsgInfo *mi = new KMMsgInfo(folder());
00716       mi->init( subjStr.stripWhiteSpace(),
00717                 fromStr.stripWhiteSpace(),
00718                 toStr.stripWhiteSpace(),
00719                 0, status,
00720                 xmarkStr.stripWhiteSpace(),
00721                 replyToIdStr, replyToAuxIdStr, msgIdStr,
00722                 file.local8Bit(),
00723                 KMMsgEncryptionStateUnknown, KMMsgSignatureStateUnknown,
00724                 KMMsgMDNStateUnknown, charset, f.size() );
00725 
00726       dateStr = dateStr.stripWhiteSpace();
00727       if (!dateStr.isEmpty())
00728         mi->setDate(dateStr);
00729       if ( !uidStr.isEmpty() )
00730          mi->setUID( uidStr.toULong() );
00731       mi->setDirty(false);
00732       mMsgList.append( mi, mExportsSernums );
00733 
00734       // if this is a New file and is in 'new', we move it to 'cur'
00735       if (status & KMMsgStatusNew)
00736       {
00737         QString newDir(location() + "/new/");
00738         QString curDir(location() + "/cur/");
00739         moveInternal(newDir + file, curDir + file, mi);
00740       }
00741 
00742       break;
00743     }
00744 
00745     // Is this a long header line?
00746     if (inHeader && line[0] == '\t' || line[0] == ' ')
00747     {
00748       int i = 0;
00749       while (line[i] == '\t' || line[i] == ' ')
00750         i++;
00751       if (line[i] < ' ' && line[i] > 0)
00752         inHeader = false;
00753       else
00754         if (lastStr)
00755           *lastStr += line + i;
00756     }
00757     else
00758       lastStr = 0;
00759 
00760     if (inHeader && (line[0] == '\n' || line[0] == '\r'))
00761       inHeader = false;
00762     if (!inHeader)
00763       continue;
00764 
00765     if (strncasecmp(line, "Date:", 5) == 0)
00766     {
00767       dateStr = QCString(line+5);
00768       lastStr = &dateStr;
00769     }
00770     else if (strncasecmp(line, "From:", 5) == 0)
00771     {
00772       fromStr = QCString(line+5);
00773       lastStr = &fromStr;
00774     }
00775     else if (strncasecmp(line, "To:", 3) == 0)
00776     {
00777       toStr = QCString(line+3);
00778       lastStr = &toStr;
00779     }
00780     else if (strncasecmp(line, "Subject:", 8) == 0)
00781     {
00782       subjStr = QCString(line+8);
00783       lastStr = &subjStr;
00784     }
00785     else if (strncasecmp(line, "References:", 11) == 0)
00786     {
00787       referencesStr = QCString(line+11);
00788       lastStr = &referencesStr;
00789     }
00790     else if (strncasecmp(line, "Message-Id:", 11) == 0)
00791     {
00792       msgIdStr = QCString(line+11);
00793       lastStr = &msgIdStr;
00794     }
00795     else if (strncasecmp(line, "X-KMail-Mark:", 13) == 0)
00796     {
00797       xmarkStr = QCString(line+13);
00798     }
00799     else if (strncasecmp(line, "X-Status:", 9) == 0)
00800     {
00801       statusStr = QCString(line+9);
00802     }
00803     else if (strncasecmp(line, "In-Reply-To:", 12) == 0)
00804     {
00805       replyToIdStr = QCString(line+12);
00806       lastStr = &replyToIdStr;
00807     }
00808     else if (strncasecmp(line, "X-UID:", 6) == 0)
00809     {
00810       uidStr = QCString(line+6);
00811       lastStr = &uidStr;
00812     }
00813     else if (strncasecmp(line, "Content-Type:", 13) == 0)
00814     {
00815       contentTypeStr = QCString(line+13);
00816       lastStr = &contentTypeStr;
00817     }
00818 
00819   }
00820 
00821   if (status & KMMsgStatusNew || status & KMMsgStatusUnread ||
00822       (folder() == kmkernel->outboxFolder()))
00823   {
00824     mUnreadMsgs++;
00825    if (mUnreadMsgs == 0) ++mUnreadMsgs;
00826   }
00827 
00828   ::chdir(path_buffer);
00829 }
00830 
00831 int KMFolderMaildir::createIndexFromContents()
00832 {
00833   mUnreadMsgs = 0;
00834 
00835   mMsgList.clear(true);
00836   mMsgList.reset(INIT_MSGS);
00837 
00838   mChanged = false;
00839 
00840   // first, we make sure that all the directories are here as they
00841   // should be
00842   QFileInfo dirinfo;
00843 
00844   dirinfo.setFile(location() + "/new");
00845   if (!dirinfo.exists() || !dirinfo.isDir())
00846   {
00847     kdDebug(5006) << "Directory " << location() << "/new doesn't exist or is a file"<< endl;
00848     return 1;
00849   }
00850   QDir newDir(location() + "/new");
00851   newDir.setFilter(QDir::Files);
00852 
00853   dirinfo.setFile(location() + "/cur");
00854   if (!dirinfo.exists() || !dirinfo.isDir())
00855   {
00856     kdDebug(5006) << "Directory " << location() << "/cur doesn't exist or is a file"<< endl;
00857     return 1;
00858   }
00859   QDir curDir(location() + "/cur");
00860   curDir.setFilter(QDir::Files);
00861 
00862   // then, we look for all the 'cur' files
00863   const QFileInfoList *list = curDir.entryInfoList();
00864   QFileInfoListIterator it(*list);
00865   QFileInfo *fi;
00866 
00867   while ((fi = it.current()))
00868   {
00869     readFileHeaderIntern(curDir.path(), fi->fileName(), KMMsgStatusRead);
00870     ++it;
00871   }
00872 
00873   // then, we look for all the 'new' files
00874   list = newDir.entryInfoList();
00875   it = *list;
00876 
00877   while ((fi=it.current()))
00878   {
00879     readFileHeaderIntern(newDir.path(), fi->fileName(), KMMsgStatusNew);
00880     ++it;
00881   }
00882 
00883   if ( autoCreateIndex() ) {
00884     emit statusMsg(i18n("Writing index file"));
00885     writeIndex();
00886   }
00887   else mHeaderOffset = 0;
00888 
00889   correctUnreadMsgsCount();
00890 
00891   if (kmkernel->outboxFolder() == folder() && count() > 0)
00892     KMessageBox::information(0, i18n("Your outbox contains messages which were "
00893     "most-likely not created by KMail;\nplease remove them from there if you "
00894     "do not want KMail to send them."));
00895 
00896   needsCompact = true;
00897 
00898   invalidateFolder();
00899   return 0;
00900 }
00901 
00902 KMFolderIndex::IndexStatus KMFolderMaildir::indexStatus()
00903 {
00904   QFileInfo new_info(location() + "/new");
00905   QFileInfo cur_info(location() + "/cur");
00906   QFileInfo index_info(indexLocation());
00907 
00908   if (!index_info.exists())
00909     return KMFolderIndex::IndexMissing;
00910 
00911   // Check whether the directories are more than 5 seconds newer than the index
00912   // file. The 5 seconds are added to reduce the number of false alerts due
00913   // to slightly out of sync clocks of the NFS server and the local machine.
00914   return ((new_info.lastModified() > index_info.lastModified().addSecs(5)) ||
00915           (cur_info.lastModified() > index_info.lastModified().addSecs(5)))
00916          ? KMFolderIndex::IndexTooOld
00917          : KMFolderIndex::IndexOk;
00918 }
00919 
00920 //-----------------------------------------------------------------------------
00921 void KMFolderMaildir::removeMsg(int idx, bool)
00922 {
00923   KMMsgBase* msg = mMsgList[idx];
00924   if (!msg || !msg->fileName()) return;
00925 
00926   removeFile(msg->fileName());
00927 
00928   KMFolderIndex::removeMsg(idx);
00929 }
00930 
00931 //-----------------------------------------------------------------------------
00932 KMMessage* KMFolderMaildir::take(int idx)
00933 {
00934   // first, we do the high-level stuff.. then delete later
00935   KMMessage *msg = KMFolderIndex::take(idx);
00936 
00937   if (!msg || !msg->fileName()) {
00938     return 0;
00939   }
00940 
00941   if ( removeFile(msg->fileName()) ) {
00942     return msg;
00943   } else {
00944     return 0;
00945   }
00946 }
00947 
00948 // static
00949 bool KMFolderMaildir::removeFile( const QString & folderPath,
00950                                   const QString & filename )
00951 {
00952   // we need to look in both 'new' and 'cur' since it's possible to
00953   // delete a message before the folder is compacted. Since the file
00954   // naming and moving is done in ::compact, we can't assume any
00955   // location at this point.
00956   QCString abs_file( QFile::encodeName( folderPath + "/cur/" + filename ) );
00957   if ( ::unlink( abs_file ) == 0 )
00958     return true;
00959 
00960   if ( errno == ENOENT ) { // doesn't exist
00961     abs_file = QFile::encodeName( folderPath + "/new/" + filename );
00962     if ( ::unlink( abs_file ) == 0 )
00963       return true;
00964   }
00965 
00966   kdDebug(5006) << "Can't delete " << abs_file << " " << perror << endl;
00967   return false;
00968 }
00969 
00970 bool KMFolderMaildir::removeFile( const QString & filename )
00971 {
00972   return removeFile( location(), filename );
00973 }
00974 
00975 #include <sys/types.h>
00976 #include <dirent.h>
00977 static bool removeDirAndContentsRecursively( const QString & path )
00978 {
00979   bool success = true;
00980 
00981   QDir d;
00982   d.setPath( path );
00983   d.setFilter( QDir::Files | QDir::Dirs | QDir::Hidden | QDir::NoSymLinks );
00984 
00985   const QFileInfoList *list = d.entryInfoList();
00986   QFileInfoListIterator it( *list );
00987   QFileInfo *fi;
00988 
00989   while ( (fi = it.current()) != 0 ) {
00990     if( fi->isDir() ) {
00991       if ( fi->fileName() != "." && fi->fileName() != ".." )
00992         success = success && removeDirAndContentsRecursively( fi->absFilePath() );
00993     } else {
00994       success = success && d.remove( fi->absFilePath() );
00995     }
00996     ++it;
00997   }
00998 
00999   if ( success ) {
01000     success = success && d.rmdir( path ); // nuke ourselves, we should be empty now
01001   }
01002   return success;
01003 }
01004 
01005 //-----------------------------------------------------------------------------
01006 int KMFolderMaildir::removeContents()
01007 {
01008   // NOTE: Don' use KIO::netaccess, it has reentrancy problems and multiple
01009   // mailchecks going on trigger them, when removing dirs
01010   if ( !removeDirAndContentsRecursively( location() + "/new/" ) ) return 1;
01011   if ( !removeDirAndContentsRecursively( location() + "/cur/" ) ) return 1;
01012   if ( !removeDirAndContentsRecursively( location() + "/tmp/" ) ) return 1;
01013   /* The subdirs are removed now. Check if there is anything else in the dir
01014    * and only if not delete the dir itself. The user could have data stored
01015    * that would otherwise be deleted. */
01016   QDir dir(location());
01017   if ( dir.count() == 2 ) { // only . and ..
01018     if ( !removeDirAndContentsRecursively( location() ), 0 ) return 1;
01019   }
01020   return 0;
01021 }
01022 
01023 static QRegExp *suffix_regex = 0;
01024 static KStaticDeleter<QRegExp> suffix_regex_sd;
01025 
01026 //-----------------------------------------------------------------------------
01027 // static
01028 QString KMFolderMaildir::constructValidFileName( const QString & filename,
01029                                                  KMMsgStatus status )
01030 {
01031   QString aFileName( filename );
01032 
01033   if (aFileName.isEmpty())
01034   {
01035     aFileName.sprintf("%ld.%d.", (long)time(0), getpid());
01036     aFileName += KApplication::randomString(5);
01037   }
01038 
01039   if (!suffix_regex)
01040       suffix_regex_sd.setObject(suffix_regex, new QRegExp(":2,?R?S?$"));
01041 
01042   aFileName.truncate(aFileName.findRev(*suffix_regex));
01043 
01044   // only add status suffix if the message is neither new nor unread
01045   if (! ((status & KMMsgStatusNew) || (status & KMMsgStatusUnread)) )
01046   {
01047     QString suffix( ":2," );
01048     if (status & KMMsgStatusReplied)
01049       suffix += "RS";
01050     else
01051       suffix += "S";
01052     aFileName += suffix;
01053   }
01054 
01055   return aFileName;
01056 }
01057 
01058 //-----------------------------------------------------------------------------
01059 QString KMFolderMaildir::moveInternal(const QString& oldLoc, const QString& newLoc, KMMsgInfo *mi)
01060 {
01061   QString filename(mi->fileName());
01062   QString ret(moveInternal(oldLoc, newLoc, filename, mi->status()));
01063 
01064   if (filename != mi->fileName())
01065     mi->setFileName(filename);
01066 
01067   return ret;
01068 }
01069 
01070 //-----------------------------------------------------------------------------
01071 QString KMFolderMaildir::moveInternal(const QString& oldLoc, const QString& newLoc, QString& aFileName, KMMsgStatus status)
01072 {
01073   QString dest(newLoc);
01074   // make sure that our destination filename doesn't already exist
01075   while (QFile::exists(dest))
01076   {
01077     aFileName = constructValidFileName( QString(), status );
01078 
01079     QFileInfo fi(dest);
01080     dest = fi.dirPath(true) + "/" + aFileName;
01081     setDirty( true );
01082   }
01083 
01084   QDir d;
01085   if (d.rename(oldLoc, dest) == false)
01086     return QString::null;
01087   else
01088     return dest;
01089 }
01090 
01091 //-----------------------------------------------------------------------------
01092 void KMFolderMaildir::msgStatusChanged(const KMMsgStatus oldStatus,
01093   const KMMsgStatus newStatus, int idx)
01094 {
01095   // if the status of any message changes, then we need to compact
01096   needsCompact = true;
01097 
01098   KMFolderIndex::msgStatusChanged(oldStatus, newStatus, idx);
01099 }
01100 
01101 #include "kmfoldermaildir.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys