kmail

kmfolder.cpp

00001 /* -*- mode: C++; c-file-style: "gnu" -*-
00002  * kmail: KDE mail client
00003  * Copyright (c) 1996-1998 Stefan Taferner <taferner@kde.org>
00004  *
00005  * This program is free software; you can redistribute it and/or modify
00006  * it under the terms of the GNU General Public License as published by
00007  * the Free Software Foundation; either version 2 of the License, or
00008  * (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  */
00020 #include <config.h>
00021 
00022 #include "kmfolder.h"
00023 #include "kmfolderdir.h"
00024 #include "kmfoldermbox.h"
00025 #include "folderstorage.h"
00026 #include "kmfoldercachedimap.h"
00027 #include "kmfoldersearch.h"
00028 #include "kmfolderimap.h"
00029 #include "kmfoldermgr.h"
00030 #include <libkpimidentities/identitymanager.h>
00031 #include <libkpimidentities/identity.h>
00032 #include "expirejob.h"
00033 #include "compactionjob.h"
00034 #include "kmfoldertree.h"
00035 #include "kmailicalifaceimpl.h"
00036 #include "kmaccount.h"
00037 
00038 #include <errno.h>
00039 
00040 #include <kdebug.h>
00041 #include <klocale.h>
00042 #include <kshortcut.h>
00043 #include <kmessagebox.h>
00044 #include <qfile.h>
00045 #include <qfileinfo.h>
00046 
00047 
00048 KMFolder::KMFolder( KMFolderDir* aParent, const QString& aFolderName,
00049                              KMFolderType aFolderType, bool withIndex, bool exportedSernums )
00050   : KMFolderNode( aParent, aFolderName ), mStorage(0),
00051     mChild( 0 ),
00052     mIsSystemFolder( false ),
00053     mHasIndex( withIndex ),
00054     mExportsSernums( exportedSernums ),
00055     mMoveInProgress( false ),
00056     mExpireMessages( false ), mUnreadExpireAge( 28 ),
00057     mReadExpireAge( 14 ), mUnreadExpireUnits( expireNever ),
00058     mReadExpireUnits( expireNever ),
00059     mExpireAction( ExpireDelete ),
00060     mUseCustomIcons( false ), mMailingListEnabled( false ),
00061     mAcctList( 0 ),
00062     mIdentity( 0 ), // default identity
00063     mPutRepliesInSameFolder( false ),
00064     mIgnoreNewMail( false )
00065 {
00066   if( aFolderType == KMFolderTypeCachedImap )
00067     mStorage = new KMFolderCachedImap( this, aFolderName.latin1() );
00068   else if( aFolderType == KMFolderTypeImap )
00069     mStorage = new KMFolderImap( this, aFolderName.latin1() );
00070   else if( aFolderType == KMFolderTypeMaildir )
00071     mStorage = new KMFolderMaildir( this, aFolderName.latin1() );
00072   else if( aFolderType == KMFolderTypeSearch )
00073     mStorage = new KMFolderSearch( this, aFolderName.latin1() );
00074   else
00075     mStorage = new KMFolderMbox( this, aFolderName.latin1() );
00076 
00077   assert( mStorage );
00078 
00079   QFileInfo dirinfo;
00080   dirinfo.setFile( mStorage->location() );
00081   if ( !dirinfo.exists() ) {
00082     int rc = mStorage->create();
00083     QString msg = i18n("<qt>Error while creating file <b>%1</b>:<br>%2</qt>").arg(aFolderName).arg(strerror(rc));
00084     if ( rc ) {
00085       KMessageBox::information(0, msg);
00086     }
00087   }
00088 
00089   if ( aParent ) {
00090     connect( mStorage, SIGNAL( msgAdded( KMFolder*, Q_UINT32 ) ),
00091              aParent->manager(), SIGNAL( msgAdded( KMFolder*, Q_UINT32 ) ) );
00092     connect( mStorage, SIGNAL( msgRemoved( KMFolder*, Q_UINT32 ) ),
00093              parent()->manager(), SIGNAL( msgRemoved( KMFolder*, Q_UINT32 ) ) );
00094     connect( this, SIGNAL( msgChanged( KMFolder*, Q_UINT32, int ) ),
00095              parent()->manager(), SIGNAL( msgChanged( KMFolder*, Q_UINT32, int ) ) );
00096     connect( this, SIGNAL( msgHeaderChanged( KMFolder*,  int ) ),
00097              parent()->manager(), SIGNAL( msgHeaderChanged( KMFolder*, int ) ) );
00098     connect( mStorage, SIGNAL( invalidated( KMFolder* ) ),
00099              parent()->manager(), SIGNAL( folderInvalidated( KMFolder* ) ) );
00100   }
00101 
00102   // Resend all mStorage signals
00103   connect( mStorage, SIGNAL( changed() ), SIGNAL( changed() ) );
00104   connect( mStorage, SIGNAL( cleared() ), SIGNAL( cleared() ) );
00105   connect( mStorage, SIGNAL( expunged( KMFolder* ) ),
00106            SIGNAL( expunged( KMFolder* ) ) );
00107   connect( mStorage, SIGNAL( nameChanged() ), SIGNAL( nameChanged() ) );
00108   connect( mStorage, SIGNAL( msgRemoved( KMFolder*, Q_UINT32 ) ),
00109            SIGNAL( msgRemoved( KMFolder*, Q_UINT32 ) ) );
00110   connect( mStorage, SIGNAL( msgRemoved( int, QString ) ),
00111            SIGNAL( msgRemoved( int, QString ) ) );
00112   connect( mStorage, SIGNAL( msgRemoved( KMFolder* ) ),
00113            SIGNAL( msgRemoved( KMFolder* ) ) );
00114   connect( mStorage, SIGNAL( msgAdded( int ) ), SIGNAL( msgAdded( int ) ) );
00115   connect( mStorage, SIGNAL( msgAdded( KMFolder*, Q_UINT32 ) ),
00116            SIGNAL( msgAdded( KMFolder*, Q_UINT32 ) ) );
00117   connect( mStorage, SIGNAL( msgChanged( KMFolder*, Q_UINT32 , int ) ),
00118            SIGNAL( msgChanged( KMFolder*, Q_UINT32 , int ) ) );
00119   connect( mStorage, SIGNAL( msgHeaderChanged( KMFolder*, int ) ),
00120            SIGNAL( msgHeaderChanged( KMFolder*, int ) ) );
00121   connect( mStorage, SIGNAL( statusMsg( const QString& ) ),
00122            SIGNAL( statusMsg( const QString& ) ) );
00123   connect( mStorage, SIGNAL( numUnreadMsgsChanged( KMFolder* ) ),
00124            SIGNAL( numUnreadMsgsChanged( KMFolder* ) ) );
00125   connect( mStorage, SIGNAL( removed( KMFolder*, bool ) ),
00126            SIGNAL( removed( KMFolder*, bool ) ) );
00127 
00128   connect( mStorage, SIGNAL( contentsTypeChanged( KMail::FolderContentsType ) ),
00129                 this, SLOT( slotContentsTypeChanged( KMail::FolderContentsType ) ) );
00130   
00131   connect( mStorage, SIGNAL( folderSizeChanged() ),
00132            this, SLOT( slotFolderSizeChanged() ) );
00133 
00134   //FIXME: Centralize all the readConfig calls somehow - Zack
00135   // Meanwhile, readConfig must be done before registerWithMessageDict, since
00136   // that one can call writeConfig in some circumstances - David
00137   mStorage->readConfig();
00138 
00139    // trigger from here, since it needs a fully constructed FolderStorage
00140   if ( mExportsSernums )
00141     mStorage->registerWithMessageDict();
00142   if ( !mHasIndex )
00143     mStorage->setAutoCreateIndex( false );
00144 
00145   if ( mId == 0 && aParent )
00146     mId = aParent->manager()->createId();
00147 }
00148 
00149 KMFolder::~KMFolder()
00150 {
00151   delete mAcctList;
00152   if ( mHasIndex ) mStorage->deregisterFromMessageDict();
00153   delete mStorage;
00154 }
00155 
00156 void KMFolder::readConfig( KConfig* config )
00157 {
00158   if ( !config->readEntry("SystemLabel").isEmpty() )
00159     mSystemLabel = config->readEntry("SystemLabel");
00160   mExpireMessages = config->readBoolEntry("ExpireMessages", false);
00161   mReadExpireAge = config->readNumEntry("ReadExpireAge", 3);
00162   mReadExpireUnits = (ExpireUnits)config->readNumEntry("ReadExpireUnits", expireMonths);
00163   mUnreadExpireAge = config->readNumEntry("UnreadExpireAge", 12);
00164   mUnreadExpireUnits = (ExpireUnits)config->readNumEntry("UnreadExpireUnits", expireNever);
00165   mExpireAction = config->readEntry("ExpireAction", "Delete") == "Move" ? ExpireMove : ExpireDelete;
00166   mExpireToFolderId = config->readEntry("ExpireToFolder");
00167 
00168   mUseCustomIcons = config->readBoolEntry("UseCustomIcons", false );
00169   mNormalIconPath = config->readEntry("NormalIconPath" );
00170   mUnreadIconPath = config->readEntry("UnreadIconPath" );
00171 
00172   mMailingListEnabled = config->readBoolEntry("MailingListEnabled");
00173   mMailingList.readConfig( config );
00174 
00175   mIdentity = config->readUnsignedNumEntry("Identity",0);
00176 
00177   setUserWhoField( config->readEntry("WhoField"), false );
00178   uint savedId = config->readUnsignedNumEntry("Id", 0);
00179   // make sure that we don't overwrite a valid id
00180   if ( savedId != 0 && mId == 0 )
00181     mId = savedId;
00182   mPutRepliesInSameFolder = config->readBoolEntry( "PutRepliesInSameFolder", false );
00183   mIgnoreNewMail = config->readBoolEntry( "IgnoreNewMail", false );
00184 
00185   if ( mUseCustomIcons )
00186     emit iconsChanged();
00187 
00188   QString shortcut( config->readEntry( "Shortcut" ) );
00189   if ( !shortcut.isEmpty() ) {
00190     KShortcut sc( shortcut );
00191     setShortcut( sc );
00192   }
00193 }
00194 
00195 void KMFolder::writeConfig( KConfig* config ) const
00196 {
00197   config->writeEntry("SystemLabel", mSystemLabel);
00198   config->writeEntry("ExpireMessages", mExpireMessages);
00199   config->writeEntry("ReadExpireAge", mReadExpireAge);
00200   config->writeEntry("ReadExpireUnits", mReadExpireUnits);
00201   config->writeEntry("UnreadExpireAge", mUnreadExpireAge);
00202   config->writeEntry("UnreadExpireUnits", mUnreadExpireUnits);
00203   config->writeEntry("ExpireAction", mExpireAction == ExpireDelete ? "Delete" : "Move");
00204   config->writeEntry("ExpireToFolder", mExpireToFolderId);
00205 
00206   config->writeEntry("UseCustomIcons", mUseCustomIcons);
00207   config->writeEntry("NormalIconPath", mNormalIconPath);
00208   config->writeEntry("UnreadIconPath", mUnreadIconPath);
00209 
00210   config->writeEntry("MailingListEnabled", mMailingListEnabled);
00211   mMailingList.writeConfig( config );
00212 
00213   if ( mIdentity != 0 && ( !mStorage || !mStorage->account() || mIdentity != mStorage->account()->identityId() ) )
00214       config->writeEntry("Identity", mIdentity);
00215   else
00216       config->deleteEntry("Identity");
00217 
00218   config->writeEntry("WhoField", mUserWhoField);
00219   config->writeEntry("Id", mId);
00220   config->writeEntry( "PutRepliesInSameFolder", mPutRepliesInSameFolder );
00221   config->writeEntry( "IgnoreNewMail", mIgnoreNewMail );
00222   if ( !mShortcut.isNull() )
00223     config->writeEntry( "Shortcut", mShortcut.toString() );
00224   else
00225     config->deleteEntry( "Shortcut" );
00226 }
00227 
00228 KMFolderType KMFolder::folderType() const
00229 {
00230   return mStorage ? mStorage->folderType() : KMFolderTypeUnknown;
00231 }
00232 
00233 QString KMFolder::fileName() const
00234 {
00235   return mStorage ? mStorage->fileName() : QString::null;
00236 }
00237 
00238 QString KMFolder::location() const
00239 {
00240   return mStorage ? mStorage->location() : QString::null;
00241 }
00242 
00243 QString KMFolder::indexLocation() const
00244 {
00245   return mStorage ? mStorage->indexLocation() : QString::null;
00246 }
00247 
00248 QString KMFolder::subdirLocation() const
00249 {
00250   QString sLocation( path() );
00251 
00252   if( !sLocation.isEmpty() )
00253     sLocation += '/';
00254   sLocation += '.' + FolderStorage::dotEscape( fileName() ) + ".directory";
00255 
00256   return sLocation;
00257 }
00258 
00259 KMFolderDir* KMFolder::createChildFolder()
00260 {
00261   if( mChild )
00262     return mChild;
00263 
00264   QString childName = "." + fileName() + ".directory";
00265   QString childDir = path() + "/" + childName;
00266   if (access(QFile::encodeName(childDir), W_OK) != 0) // Not there or not writable
00267   {
00268     if (mkdir(QFile::encodeName(childDir), S_IRWXU) != 0
00269       && chmod(QFile::encodeName(childDir), S_IRWXU) != 0) {
00270       QString wmsg = QString(" '%1': %2").arg(childDir).arg(strerror(errno));
00271       KMessageBox::information(0,i18n("Failed to create folder") + wmsg);
00272       return 0;
00273     }
00274   }
00275 
00276   KMFolderDirType newType = KMStandardDir;
00277   if( folderType() == KMFolderTypeCachedImap )
00278     newType = KMDImapDir;
00279   else if( folderType() == KMFolderTypeImap )
00280     newType = KMImapDir;
00281 
00282   mChild = new KMFolderDir( this, parent(), childName, newType );
00283   if( !mChild )
00284     return 0;
00285   mChild->reload();
00286   parent()->append( mChild );
00287   return mChild;
00288 }
00289 
00290 void KMFolder::setChild( KMFolderDir* aChild )
00291 {
00292   mChild = aChild;
00293   mStorage->updateChildrenState();
00294 }
00295 
00296 bool KMFolder::noContent() const
00297 {
00298   return mStorage ? mStorage->noContent() : true;
00299 }
00300 
00301 void KMFolder::setNoContent( bool aNoContent )
00302 {
00303   mStorage->setNoContent( aNoContent );
00304 }
00305 
00306 bool KMFolder::noChildren() const
00307 {
00308   return mStorage->noChildren();
00309 }
00310 
00311 void KMFolder::setNoChildren( bool aNoChildren )
00312 {
00313   mStorage->setNoChildren( aNoChildren );
00314 }
00315 
00316 KMMessage* KMFolder::getMsg( int idx )
00317 {
00318   return mStorage->getMsg( idx );
00319 }
00320 
00321 KMMsgInfo* KMFolder::unGetMsg( int idx )
00322 {
00323   return mStorage->unGetMsg( idx );
00324 }
00325 
00326 bool KMFolder::isMessage( int idx )
00327 {
00328   return mStorage->isMessage( idx );
00329 }
00330 
00331 DwString KMFolder::getDwString( int idx )
00332 {
00333   return mStorage->getDwString( idx );
00334 }
00335 
00336 void KMFolder::ignoreJobsForMessage( KMMessage* m )
00337 {
00338   mStorage->ignoreJobsForMessage( m );
00339 }
00340 
00341 FolderJob* KMFolder::createJob( KMMessage *msg, FolderJob::JobType jt,
00342                                 KMFolder *folder, QString partSpecifier,
00343                                 const AttachmentStrategy *as ) const
00344 {
00345   return mStorage->createJob( msg, jt, folder, partSpecifier, as );
00346 }
00347 
00348 FolderJob* KMFolder::createJob( QPtrList<KMMessage>& msgList,
00349                                 const QString& sets,
00350                                 FolderJob::JobType jt, KMFolder *folder ) const
00351 {
00352   return mStorage->createJob( msgList, sets, jt, folder );
00353 }
00354 
00355 const KMMsgBase* KMFolder::getMsgBase( int idx ) const
00356 {
00357   return mStorage->getMsgBase( idx );
00358 }
00359 
00360 KMMsgBase* KMFolder::getMsgBase( int idx )
00361 {
00362   return mStorage->getMsgBase( idx );
00363 }
00364 
00365 const KMMsgBase* KMFolder::operator[]( int idx ) const
00366 {
00367   return mStorage->operator[]( idx );
00368 }
00369 
00370 KMMsgBase* KMFolder::operator[]( int idx )
00371 {
00372   return mStorage->operator[]( idx );
00373 }
00374 
00375 KMMessage* KMFolder::take( int idx )
00376 {
00377   return mStorage->take( idx );
00378 }
00379 
00380 void KMFolder::take( QPtrList<KMMessage> msgList ) // TODO const ref
00381 {
00382   mStorage->take( msgList );
00383 }
00384 
00385 int KMFolder::addMsg( KMMessage* msg, int* index_return )
00386 {
00387   return mStorage->addMsg( msg, index_return );
00388 }
00389 
00390 int KMFolder::addMsgKeepUID( KMMessage* msg, int* index_return )
00391 {
00392   return mStorage->addMsgKeepUID( msg, index_return );
00393 }
00394 
00395 int KMFolder::addMsg( QPtrList<KMMessage>& list, QValueList<int>& index_return )
00396 {
00397   return mStorage->addMsg( list, index_return );
00398 }
00399 
00400 void KMFolder::emitMsgAddedSignals( int idx )
00401 {
00402   mStorage->emitMsgAddedSignals( idx );
00403 }
00404 
00405 void KMFolder::removeMsg( int i, bool imapQuiet )
00406 {
00407   mStorage->removeMsg( i, imapQuiet );
00408 }
00409 
00410 void KMFolder::removeMsg( QPtrList<KMMessage> msgList, bool imapQuiet ) // TODO const ref
00411 {
00412   mStorage->removeMsg( msgList, imapQuiet );
00413 }
00414 
00415 int KMFolder::expungeOldMsg( int days )
00416 {
00417   return mStorage->expungeOldMsg( days );
00418 }
00419 
00420 int KMFolder::moveMsg( KMMessage* msg, int* index_return )
00421 {
00422   return mStorage->moveMsg( msg, index_return );
00423 }
00424 
00425 int KMFolder::moveMsg(QPtrList<KMMessage> q, int* index_return )
00426 {
00427   return mStorage->moveMsg( q, index_return );
00428 }
00429 
00430 int KMFolder::find( const KMMsgBase* msg ) const
00431 {
00432   return mStorage ? mStorage->find( msg ) : -1;
00433 }
00434 
00435 int KMFolder::find( const KMMessage* msg ) const
00436 {
00437   return mStorage ? mStorage->find( msg ) : -1;
00438 }
00439 
00440 int KMFolder::count( bool cache ) const
00441 {
00442   return mStorage->count( cache );
00443 }
00444 
00445 int KMFolder::countUnread()
00446 {
00447   return mStorage->countUnread();
00448 }
00449 
00450 int KMFolder::countUnreadRecursive()
00451 {
00452   KMFolder *folder;
00453   int count = countUnread();
00454   KMFolderDir *dir = child();
00455   if (!dir)
00456     return count;
00457 
00458   QPtrListIterator<KMFolderNode> it(*dir);
00459   for ( ; it.current(); ++it )
00460     if (!it.current()->isDir()) {
00461       folder = static_cast<KMFolder*>(it.current());
00462       count += folder->countUnreadRecursive();
00463     }
00464 
00465   return count;
00466 }
00467 
00468 void KMFolder::msgStatusChanged( const KMMsgStatus oldStatus,
00469                                  const KMMsgStatus newStatus, int idx )
00470 {
00471   mStorage->msgStatusChanged( oldStatus, newStatus, idx );
00472 }
00473 
00474 int KMFolder::open(const char *owner)
00475 {
00476   return mStorage->open(owner);
00477 }
00478 
00479 int KMFolder::canAccess()
00480 {
00481   return mStorage->canAccess();
00482 }
00483 
00484 void KMFolder::close( const char *owner, bool force )
00485 {
00486   // do not emit closed() in here - as this would regain too early
00487   mStorage->close( owner, force );
00488 }
00489 
00490 void KMFolder::sync()
00491 {
00492   mStorage->sync();
00493 }
00494 
00495 bool KMFolder::isOpened() const
00496 {
00497   return mStorage->isOpened();
00498 }
00499 
00500 void KMFolder::markNewAsUnread()
00501 {
00502   mStorage->markNewAsUnread();
00503 }
00504 
00505 void KMFolder::markUnreadAsRead()
00506 {
00507   mStorage->markUnreadAsRead();
00508 }
00509 
00510 void KMFolder::remove()
00511 {
00512   /* The storage needs to be open before remove is called, otherwise
00513      it will not unregister the corresponding serial numbers from
00514      the message dict, since its message list is empty, and the .ids
00515      file contents are not loaded. That can lead to lookups in the
00516      dict returning stale pointers to the folder later. */
00517   mStorage->open("kmfolder_remove");
00518   mStorage->remove();
00519 }
00520 
00521 int KMFolder::expunge()
00522 {
00523   return mStorage->expunge();
00524 }
00525 
00526 int KMFolder::rename( const QString& newName, KMFolderDir *aParent )
00527 {
00528   return mStorage->rename( newName, aParent );
00529 }
00530 
00531 bool KMFolder::dirty() const
00532 {
00533   return mStorage->dirty();
00534 }
00535 
00536 void KMFolder::setDirty( bool f )
00537 {
00538   mStorage->setDirty( f );
00539 }
00540 
00541 bool KMFolder::needsCompacting() const
00542 {
00543   return mStorage->needsCompacting();
00544 }
00545 
00546 void KMFolder::setNeedsCompacting( bool f )
00547 {
00548   mStorage->setNeedsCompacting( f );
00549 }
00550 
00551 void KMFolder::quiet( bool beQuiet )
00552 {
00553   mStorage->quiet( beQuiet );
00554 }
00555 
00556 bool KMFolder::isReadOnly() const
00557 {
00558   return mStorage->isReadOnly();
00559 }
00560 
00561 QString KMFolder::label() const
00562 {
00563   if ( !mSystemLabel.isEmpty() )
00564      return mSystemLabel;
00565   if ( !mLabel.isEmpty() )
00566      return mLabel;
00567   if ( isSystemFolder() )
00568      return i18n( name().utf8() );
00569   return name();
00570 }
00571 
00572 //-----------------------------------------------------------------------------
00573 QString KMFolder::prettyURL() const
00574 {
00575   QString parentUrl;
00576   if ( parent() )
00577     parentUrl = parent()->prettyURL();
00578   if ( !parentUrl.isEmpty() )
00579     return parentUrl + '/' + label();
00580   else
00581     return label();
00582 }
00583 
00584 //--------------------------------------------------------------------------
00585 QString KMFolder::mailingListPostAddress() const
00586 {
00587   if ( mMailingList.features() & MailingList::Post ) {
00588     KURL::List::const_iterator it;
00589     KURL::List post = mMailingList.postURLS();
00590     for( it = post.begin(); it != post.end(); ++it ) {
00591       // We check for isEmpty because before 3.3 postAddress was just an
00592       // email@kde.org and that leaves protocol() field in the kurl class
00593       if ( (*it).protocol() == "mailto" || (*it).protocol().isEmpty() )
00594         return (*it).path();
00595     }
00596   }
00597   return QString::null;
00598 }
00599 
00600 void KMFolder::setMailingListEnabled( bool enabled )
00601 {
00602   mMailingListEnabled = enabled;
00603   mStorage->writeConfig();
00604 }
00605 
00606 void KMFolder::setMailingList( const MailingList& mlist )
00607 {
00608   mMailingList = mlist;
00609   mStorage->writeConfig();
00610 }
00611 
00612 void KMFolder::setIdentity( uint identity )
00613 {
00614   mIdentity = identity;
00615   kmkernel->slotRequestConfigSync();
00616 }
00617 
00618 uint KMFolder::identity() const
00619 {
00620   // if we don't have one set ourselves, check our account
00621   kdDebug() << "FOO: " << mIdentity << " :: " << mStorage << endl;
00622   if ( !mIdentity && mStorage )
00623     if ( KMAccount *act = mStorage->account() )
00624       return act->identityId();
00625   return mIdentity;
00626 }
00627 
00628 void KMFolder::setWhoField(const QString& aWhoField )
00629 {
00630   mWhoField = aWhoField;
00631 #if 0
00632   // This isn't saved in the config anyway
00633   mStorage->writeConfig();
00634 #endif
00635 }
00636 
00637 void KMFolder::setUserWhoField( const QString& whoField, bool writeConfig )
00638 {
00639   if ( mUserWhoField == whoField )
00640     return;
00641   if ( whoField.isEmpty() )
00642   {
00643     // default setting
00644     const KPIM::Identity & identity =
00645       kmkernel->identityManager()->identityForUoidOrDefault( mIdentity );
00646 
00647     if ( isSystemFolder() && folderType() != KMFolderTypeImap ) {
00648       // local system folders
00649       if ( this == kmkernel->inboxFolder() ||
00650            this == kmkernel->trashFolder() )
00651         mWhoField = "From";
00652       if ( this == kmkernel->outboxFolder() ||
00653            this == kmkernel->sentFolder() ||
00654            this == kmkernel->draftsFolder() ||
00655            this == kmkernel->templatesFolder() )
00656         mWhoField = "To";
00657     } else if ( identity.drafts() == idString() ||
00658                 identity.templates() == idString() ||
00659                 identity.fcc() == idString() )
00660       // drafts, templates or sent of the identity
00661       mWhoField = "To";
00662     else
00663       mWhoField = "From";
00664   } else if ( whoField == "From" || whoField == "To" )
00665     // set the whoField according to the user-setting
00666     mWhoField = whoField;
00667   else {
00668     // this should not happen...
00669     kdDebug(5006) << "Illegal setting " << whoField << " for userWhoField!"
00670                   << endl;
00671     return; // don't use the value
00672   }
00673   mUserWhoField = whoField;
00674 
00675   if (writeConfig)
00676     mStorage->writeConfig();
00677   emit viewConfigChanged();
00678 }
00679 
00680 void KMFolder::correctUnreadMsgsCount()
00681 {
00682   mStorage->correctUnreadMsgsCount();
00683 }
00684 
00685 QString KMFolder::idString() const
00686 {
00687   KMFolderNode* folderNode = parent();
00688   if (!folderNode)
00689     return "";
00690   while ( folderNode->parent() )
00691     folderNode = folderNode->parent();
00692   QString myPath = path();
00693   int pathLen = myPath.length() - folderNode->path().length();
00694   QString relativePath = myPath.right( pathLen );
00695   if (!relativePath.isEmpty())
00696     relativePath = relativePath.right( relativePath.length() - 1 ) + "/";
00697   QString escapedName = name();
00698   /* Escape [ and ] as they are disallowed for kconfig sections and that is
00699      what the idString is primarily used for. */
00700   escapedName.replace( "[", "%(" );
00701   escapedName.replace( "]", "%)" );
00702   return relativePath + escapedName;
00703 }
00704 
00705 void KMFolder::setAutoExpire( bool enabled )
00706 {
00707   if( enabled != mExpireMessages ) {
00708     mExpireMessages = enabled;
00709     mStorage->writeConfig();
00710   }
00711 }
00712 
00713 void KMFolder::setUnreadExpireAge( int age )
00714 {
00715   if( age >= 0 && age != mUnreadExpireAge ) {
00716     mUnreadExpireAge = age;
00717     mStorage->writeConfig();
00718   }
00719 }
00720 
00721 void KMFolder::setUnreadExpireUnits( ExpireUnits units )
00722 {
00723   if (units >= expireNever && units < expireMaxUnits)
00724     mUnreadExpireUnits = units;
00725     mStorage->writeConfig();
00726 }
00727 
00728 void KMFolder::setReadExpireAge( int age )
00729 {
00730   if( age >= 0 && age != mReadExpireAge ) {
00731     mReadExpireAge = age;
00732     mStorage->writeConfig();
00733   }
00734 }
00735 
00736 void KMFolder::setReadExpireUnits( ExpireUnits units )
00737 {
00738   if (units >= expireNever && units <= expireMaxUnits)
00739     mReadExpireUnits = units;
00740     mStorage->writeConfig();
00741 }
00742 
00743 
00744 void KMFolder::setExpireAction( ExpireAction a )
00745 {
00746   if ( a != mExpireAction ) {
00747     mExpireAction = a;
00748     mStorage->writeConfig();
00749   }
00750 }
00751 
00752 void KMFolder::setExpireToFolderId( const QString& id )
00753 {
00754   if ( id != mExpireToFolderId ) {
00755     mExpireToFolderId = id;
00756     mStorage->writeConfig();
00757   }
00758 }
00759 
00760 
00761 static int daysToExpire( int number, ExpireUnits units )
00762 {
00763   switch (units) {
00764   case expireDays: // Days
00765     return number;
00766   case expireWeeks: // Weeks
00767     return number * 7;
00768   case expireMonths: // Months - this could be better rather than assuming 31day months.
00769     return number * 31;
00770   default: // this avoids a compiler warning (not handled enumeration values)
00771     ;
00772   }
00773   return -1;
00774 }
00775 
00776 void KMFolder::daysToExpire(int& unreadDays, int& readDays) {
00777   unreadDays = ::daysToExpire( getUnreadExpireAge(), getUnreadExpireUnits() );
00778   readDays = ::daysToExpire( getReadExpireAge(), getReadExpireUnits() );
00779 }
00780 
00781 void KMFolder::expireOldMessages( bool immediate )
00782 {
00783   KMail::ScheduledExpireTask* task = new KMail::ScheduledExpireTask(this, immediate);
00784   kmkernel->jobScheduler()->registerTask( task );
00785   if ( immediate ) {
00786     // #82259: compact after expiring.
00787     compact( CompactLater );
00788   }
00789 }
00790 
00791 void KMFolder::compact( CompactOptions options )
00792 {
00793   if ( options == CompactLater ) {
00794     KMail::ScheduledCompactionTask* task = new KMail::ScheduledCompactionTask(this, false);
00795     kmkernel->jobScheduler()->registerTask( task );
00796   } else {
00797     mStorage->compact( options == CompactSilentlyNow );
00798   }
00799 }
00800 
00801 KMFolder* KMFolder::trashFolder() const
00802 {
00803   return mStorage ? mStorage->trashFolder() : 0;
00804 }
00805 
00806 int KMFolder::writeIndex( bool createEmptyIndex )
00807 {
00808   return mStorage->writeIndex( createEmptyIndex );
00809 }
00810 
00811 void KMFolder::setStatus( int idx, KMMsgStatus status, bool toggle )
00812 {
00813   mStorage->setStatus( idx, status, toggle );
00814 }
00815 
00816 void KMFolder::setStatus( QValueList<int>& ids, KMMsgStatus status,
00817                           bool toggle )
00818 {
00819   mStorage->setStatus( ids, status, toggle);
00820 }
00821 
00822 void KMFolder::setIconPaths( const QString &normalPath,
00823                              const QString &unreadPath )
00824 {
00825   mNormalIconPath = normalPath;
00826   mUnreadIconPath = unreadPath;
00827   mStorage->writeConfig();
00828   emit iconsChanged();
00829 }
00830 
00831 void KMFolder::removeJobs()
00832 {
00833   mStorage->removeJobs();
00834 }
00835 
00836 int KMFolder::updateIndex()
00837 {
00838   return mStorage->updateIndex();
00839 }
00840 
00841 void KMFolder::reallyAddMsg( KMMessage* aMsg )
00842 {
00843   mStorage->reallyAddMsg( aMsg );
00844 }
00845 
00846 void KMFolder::reallyAddCopyOfMsg( KMMessage* aMsg )
00847 {
00848   mStorage->reallyAddCopyOfMsg( aMsg );
00849 }
00850 
00851 void KMFolder::setShortcut( const KShortcut &sc )
00852 {
00853   if ( mShortcut != sc ) {
00854     mShortcut = sc;
00855     emit shortcutChanged( this );
00856   }
00857 }
00858 
00859 bool KMFolder::isMoveable() const
00860 {
00861   return !isSystemFolder();
00862 }
00863 
00864 void KMFolder::slotContentsTypeChanged( KMail::FolderContentsType type )
00865 {
00866   kmkernel->iCalIface().folderContentsTypeChanged( this, type );
00867   emit iconsChanged();
00868 }
00869 
00870 void KMFolder::slotFolderSizeChanged()
00871 {
00872   emit folderSizeChanged( this );
00873   KMFolder* papa = parent()->manager()->parentFolder( this );
00874   if ( papa && papa != this ) {
00875     papa->slotFolderSizeChanged();
00876   }
00877 }
00878 
00879 
00880 #include "kmfolder.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys