Backport r950340 | aacid | 2009-04-06 23:21:18 +0200 (Mon, 06 Apr 2009) | 4 lines
[kdepim.git] / kmail / kmfolder.cpp
blobe3c9e1d52aa9be3bdb9312f9eaf27f206817dc98
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2 * kmail: KDE mail client
3 * Copyright (c) 1996-1998 Stefan Taferner <taferner@kde.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "kmfolder.h"
22 #include "kmfolderdir.h"
23 #include "kmfoldermbox.h"
24 #include "folderstorage.h"
25 #include "kmfoldercachedimap.h"
26 #include "kmfoldersearch.h"
27 #include "kmfolderimap.h"
28 #include "kmfoldermgr.h"
29 #include <kpimidentities/identitymanager.h>
30 #include <kpimidentities/identity.h>
31 #include "expirejob.h"
32 #include "compactionjob.h"
33 #include "kmailicalifaceimpl.h"
34 #include "kmaccount.h"
36 #include <errno.h>
37 #include <unistd.h> // W_OK
39 #include <kdebug.h>
40 #include <kde_file.h> // KDE_mkdir
41 #include <klocale.h>
42 #include <kshortcut.h>
43 #include <kmessagebox.h>
45 #include <QFile>
46 #include <QFileInfo>
47 #include <QList>
49 KMFolder::KMFolder( KMFolderDir* aParent, const QString& aFolderName,
50 KMFolderType aFolderType, bool withIndex, bool exportedSernums )
51 : KMFolderNode( aParent, aFolderName ), mStorage(0),
52 mChild( 0 ),
53 mIsSystemFolder( false ),
54 mHasIndex( withIndex ),
55 mExportsSernums( exportedSernums ),
56 mMoveInProgress( false ),
57 mExpireMessages( false ), mUnreadExpireAge( 28 ),
58 mReadExpireAge( 14 ), mUnreadExpireUnits( expireNever ),
59 mReadExpireUnits( expireNever ),
60 mExpireAction( ExpireDelete ),
61 mUseCustomIcons( false ), mMailingListEnabled( false ),
62 mAcctList( 0 ),
63 mPutRepliesInSameFolder( false ),
64 mIgnoreNewMail( false )
66 mIdentity = kmkernel->identityManager()->defaultIdentity().uoid();
67 if( aFolderType == KMFolderTypeCachedImap )
68 mStorage = new KMFolderCachedImap( this, aFolderName.toLatin1() );
69 else if( aFolderType == KMFolderTypeImap )
70 mStorage = new KMFolderImap( this, aFolderName.toLatin1() );
71 else if( aFolderType == KMFolderTypeMaildir )
72 mStorage = new KMFolderMaildir( this, aFolderName.toLatin1() );
73 else if( aFolderType == KMFolderTypeSearch )
74 mStorage = new KMFolderSearch( this, aFolderName.toLatin1() );
75 else
76 mStorage = new KMFolderMbox( this, aFolderName.toLatin1() );
78 assert( mStorage );
80 QFileInfo dirinfo;
81 dirinfo.setFile( mStorage->location() );
82 if ( !dirinfo.exists() ) {
83 int rc = mStorage->create();
84 QString msg = i18n("<qt>Error while creating file <b>%1</b>:<br />%2</qt>", aFolderName, strerror(rc));
85 if ( rc ) {
86 KMessageBox::information(0, msg);
90 if ( aParent ) {
91 connect( mStorage, SIGNAL( msgAdded( KMFolder*, quint32 ) ),
92 aParent->manager(), SIGNAL( msgAdded( KMFolder*, quint32 ) ) );
93 connect( mStorage, SIGNAL( msgRemoved( KMFolder*, quint32 ) ),
94 parent()->manager(), SIGNAL( msgRemoved( KMFolder*, quint32 ) ) );
95 connect( this, SIGNAL( msgChanged( KMFolder*, quint32, int ) ),
96 parent()->manager(), SIGNAL( msgChanged( KMFolder*, quint32, int ) ) );
97 connect( this, SIGNAL( msgHeaderChanged( KMFolder*, int ) ),
98 parent()->manager(), SIGNAL( msgHeaderChanged( KMFolder*, int ) ) );
99 connect( mStorage, SIGNAL( invalidated( KMFolder* ) ),
100 parent()->manager(), SIGNAL( folderInvalidated( KMFolder* ) ) );
103 // Resend all mStorage signals
104 connect( mStorage, SIGNAL( changed() ), SIGNAL( changed() ) );
105 connect( mStorage, SIGNAL( cleared() ), SIGNAL( cleared() ) );
106 connect( mStorage, SIGNAL( expunged( KMFolder* ) ),
107 SIGNAL( expunged( KMFolder* ) ) );
108 connect( mStorage, SIGNAL( nameChanged() ), SIGNAL( nameChanged() ) );
109 connect( mStorage, SIGNAL( msgRemoved( KMFolder*, quint32 ) ),
110 SIGNAL( msgRemoved( KMFolder*, quint32 ) ) );
111 connect( mStorage, SIGNAL( msgRemoved( int, const QString& ) ),
112 SIGNAL( msgRemoved( int, const QString& ) ) );
113 connect( mStorage, SIGNAL( msgRemoved( KMFolder* ) ),
114 SIGNAL( msgRemoved( KMFolder* ) ) );
115 connect( mStorage, SIGNAL( msgAdded( int ) ), SIGNAL( msgAdded( int ) ) );
116 connect( mStorage, SIGNAL( msgAdded( KMFolder*, quint32 ) ),
117 SIGNAL( msgAdded( KMFolder*, quint32 ) ) );
118 connect( mStorage, SIGNAL( msgChanged( KMFolder*, quint32, int ) ),
119 SIGNAL( msgChanged( KMFolder*, quint32, int ) ) );
120 connect( mStorage, SIGNAL( msgHeaderChanged( KMFolder*, int ) ),
121 SIGNAL( msgHeaderChanged( KMFolder*, int ) ) );
122 connect( mStorage, SIGNAL( statusMsg( const QString& ) ),
123 SIGNAL( statusMsg( const QString& ) ) );
124 connect( mStorage, SIGNAL( numUnreadMsgsChanged( KMFolder* ) ),
125 SIGNAL( numUnreadMsgsChanged( KMFolder* ) ) );
126 connect( mStorage, SIGNAL( removed( KMFolder*, bool ) ),
127 SIGNAL( removed( KMFolder*, bool ) ) );
128 connect( mStorage, SIGNAL(noContentChanged()),
129 SIGNAL(noContentChanged()) );
131 connect( mStorage, SIGNAL( contentsTypeChanged( KMail::FolderContentsType ) ),
132 this, SLOT( slotContentsTypeChanged( KMail::FolderContentsType ) ) );
134 connect( mStorage, SIGNAL( folderSizeChanged() ),
135 this, SLOT( slotFolderSizeChanged() ) );
137 connect( kmkernel->identityManager(), SIGNAL( changed() ),
138 this, SLOT( slotIdentitiesChanged() ) );
140 //FIXME: Centralize all the readConfig calls somehow - Zack
141 // Meanwhile, readConfig must be done before registerWithMessageDict, since
142 // that one can call writeConfig in some circumstances - David
143 mStorage->readConfig();
145 // trigger from here, since it needs a fully constructed FolderStorage
146 if ( mExportsSernums )
147 mStorage->registerWithMessageDict();
148 if ( !mHasIndex )
149 mStorage->setAutoCreateIndex( false );
151 if ( mId == 0 && aParent )
152 mId = aParent->manager()->createId();
155 KMFolder::~KMFolder()
157 mStorage->close( "~KMFolder", true );
158 delete mAcctList;
159 if ( mHasIndex ) mStorage->deregisterFromMessageDict();
160 delete mStorage;
163 bool KMFolder::hasDescendant( KMFolder *fld ) const
165 if ( !fld )
166 return false;
167 KMFolderDir * pdir = fld->parent();
168 if ( !pdir )
169 return false;
170 fld = pdir->owner();
171 if ( fld == this )
172 return true;
173 return hasDescendant( fld );
176 KMFolder * KMFolder::ownerFolder() const
178 if ( !mParent )
179 return 0;
180 return mParent->owner();
184 void KMFolder::readConfig( KConfigGroup & configGroup )
186 // KConfigGroup configGroup(config, "");
187 if ( !configGroup.readEntry( "SystemLabel" ).isEmpty() )
188 mSystemLabel = configGroup.readEntry( "SystemLabel" );
189 mExpireMessages = configGroup.readEntry( "ExpireMessages", false );
190 mReadExpireAge = configGroup.readEntry( "ReadExpireAge", 3 );
191 mReadExpireUnits = (ExpireUnits)configGroup.readEntry( "ReadExpireUnits", (int)expireMonths );
192 mUnreadExpireAge = configGroup.readEntry( "UnreadExpireAge", 12 );
193 mUnreadExpireUnits = (ExpireUnits)
194 configGroup.readEntry( "UnreadExpireUnits", (int)expireNever );
195 mExpireAction = configGroup.readEntry( "ExpireAction", "Delete") == "Move" ? ExpireMove : ExpireDelete;
196 mExpireToFolderId = configGroup.readEntry( "ExpireToFolder" );
198 mUseCustomIcons = configGroup.readEntry( "UseCustomIcons", false );
199 mNormalIconPath = configGroup.readEntry( "NormalIconPath" );
200 mUnreadIconPath = configGroup.readEntry( "UnreadIconPath" );
202 mMailingListEnabled = configGroup.readEntry( "MailingListEnabled", false );
203 mMailingList.readConfig( configGroup );
205 mUseDefaultIdentity = configGroup.readEntry( "UseDefaultIdentity", true );
206 uint defaultIdentity = kmkernel->identityManager()->defaultIdentity().uoid();
207 mIdentity = configGroup.readEntry("Identity", defaultIdentity );
208 slotIdentitiesChanged();
210 setUserWhoField( configGroup.readEntry( "WhoField" ), false );
211 uint savedId = configGroup.readEntry( "Id", 0 );
212 // make sure that we don't overwrite a valid id
213 if ( savedId != 0 && mId == 0 )
214 mId = savedId;
215 mPutRepliesInSameFolder = configGroup.readEntry( "PutRepliesInSameFolder", false );
216 mIgnoreNewMail = configGroup.readEntry( "IgnoreNewMail", false );
218 if ( mUseCustomIcons )
219 emit iconsChanged();
221 QString shortcut( configGroup.readEntry( "Shortcut" ) );
222 if ( !shortcut.isEmpty() ) {
223 KShortcut sc( shortcut );
224 setShortcut( sc );
228 void KMFolder::writeConfig( KConfigGroup & configGroup ) const
230 // KConfigGroup configGroup(config, "");
231 configGroup.writeEntry("SystemLabel", mSystemLabel);
232 configGroup.writeEntry("ExpireMessages", mExpireMessages);
233 configGroup.writeEntry("ReadExpireAge", mReadExpireAge);
234 configGroup.writeEntry("ReadExpireUnits", (int)mReadExpireUnits);
235 configGroup.writeEntry("UnreadExpireAge", mUnreadExpireAge);
236 configGroup.writeEntry("UnreadExpireUnits", (int)mUnreadExpireUnits);
237 configGroup.writeEntry("ExpireAction", mExpireAction == ExpireDelete ? "Delete" : "Move");
238 configGroup.writeEntry("ExpireToFolder", mExpireToFolderId);
240 configGroup.writeEntry("UseCustomIcons", mUseCustomIcons);
241 configGroup.writeEntry("NormalIconPath", mNormalIconPath);
242 configGroup.writeEntry("UnreadIconPath", mUnreadIconPath);
244 configGroup.writeEntry("MailingListEnabled", mMailingListEnabled);
245 mMailingList.writeConfig( configGroup );
248 configGroup.writeEntry( "UseDefaultIdentity", mUseDefaultIdentity );
249 if ( !mUseDefaultIdentity && ( !mStorage || !mStorage->account() ||
250 mIdentity != mStorage->account()->identityId() ) )
251 configGroup.writeEntry("Identity", mIdentity);
252 else
253 configGroup.deleteEntry("Identity");
255 configGroup.writeEntry("WhoField", mUserWhoField);
256 configGroup.writeEntry("Id", mId);
257 configGroup.writeEntry( "PutRepliesInSameFolder", mPutRepliesInSameFolder );
258 configGroup.writeEntry( "IgnoreNewMail", mIgnoreNewMail );
259 if ( !mShortcut.isEmpty() )
260 configGroup.writeEntry( "Shortcut", mShortcut.toString() );
261 else
262 configGroup.deleteEntry( "Shortcut" );
265 KMFolderType KMFolder::folderType() const
267 return mStorage ? mStorage->folderType() : KMFolderTypeUnknown;
270 QString KMFolder::fileName() const
272 return mStorage ? mStorage->fileName() : QString();
275 QString KMFolder::location() const
277 return mStorage ? mStorage->location() : QString();
280 QString KMFolder::indexLocation() const
282 return mStorage ? mStorage->indexLocation() : QString();
285 QString KMFolder::sortedLocation() const
287 return mStorage ? mStorage->sortedLocation() : QString();
290 QString KMFolder::idsLocation() const
292 return mStorage ? mStorage->idsLocation() : QString();
295 QString KMFolder::subdirLocation() const
297 QString sLocation( path() );
299 if( !sLocation.isEmpty() )
300 sLocation += '/';
301 sLocation += '.' + FolderStorage::dotEscape( fileName() ) + ".directory";
303 return sLocation;
306 KMFolderDir* KMFolder::createChildFolder()
308 if ( mChild ) {
309 return mChild;
312 QString childName = '.' + fileName() + ".directory";
313 QString childDir = path() + '/' + childName;
314 if ( access( QFile::encodeName( childDir ), W_OK ) != 0 ) {
315 // childDir does not exist or is not writable, so create it.
316 if ( KDE_mkdir( QFile::encodeName(childDir), S_IRWXU ) != 0 &&
317 chmod( QFile::encodeName(childDir), S_IRWXU) != 0 ) {
318 QString wmsg = QString( " '%1': %2" ).arg( childDir ).arg( strerror( errno ) );
319 KMessageBox::information( 0, i18n( "Failed to create folder" ) + wmsg );
320 return 0;
324 KMFolderDirType newType = KMStandardDir;
325 if ( folderType() == KMFolderTypeCachedImap ) {
326 newType = KMDImapDir;
327 } else if ( folderType() == KMFolderTypeImap ) {
328 newType = KMImapDir;
331 mChild = new KMFolderDir( this, parent(), childName, newType );
332 if( !mChild )
333 return 0;
334 mChild->reload();
335 parent()->append( mChild );
336 return mChild;
339 void KMFolder::setChild( KMFolderDir* aChild )
341 mChild = aChild;
342 mStorage->updateChildrenState();
345 bool KMFolder::noContent() const
347 return mStorage ? mStorage->noContent() : true;
350 void KMFolder::setNoContent( bool aNoContent )
352 mStorage->setNoContent( aNoContent );
355 bool KMFolder::noChildren() const
357 return mStorage->noChildren();
360 void KMFolder::setNoChildren( bool aNoChildren )
362 mStorage->setNoChildren( aNoChildren );
365 KMMessage* KMFolder::getMsg( int idx )
367 return mStorage->getMsg( idx );
370 KMMsgInfo* KMFolder::unGetMsg( int idx )
372 return mStorage->unGetMsg( idx );
375 bool KMFolder::isMessage( int idx )
377 return mStorage->isMessage( idx );
380 DwString KMFolder::getDwString( int idx )
382 return mStorage->getDwString( idx );
385 void KMFolder::ignoreJobsForMessage( KMMessage* m )
387 mStorage->ignoreJobsForMessage( m );
390 FolderJob* KMFolder::createJob( KMMessage *msg, FolderJob::JobType jt,
391 KMFolder *folder, const QString &partSpecifier,
392 const AttachmentStrategy *as ) const
394 return mStorage->createJob( msg, jt, folder, partSpecifier, as );
397 FolderJob* KMFolder::createJob( QList<KMMessage*>& msgList,
398 const QString& sets,
399 FolderJob::JobType jt, KMFolder *folder ) const
401 return mStorage->createJob( msgList, sets, jt, folder );
404 const KMMsgBase* KMFolder::getMsgBase( int idx ) const
406 return mStorage->getMsgBase( idx );
409 KMMsgBase* KMFolder::getMsgBase( int idx )
411 return mStorage->getMsgBase( idx );
414 const KMMsgBase* KMFolder::operator[]( int idx ) const
416 return mStorage->operator[]( idx );
419 KMMsgBase* KMFolder::operator[]( int idx )
421 return mStorage->operator[]( idx );
424 KMMessage* KMFolder::take( int idx )
426 return mStorage->take( idx );
429 void KMFolder::takeMessages( const QList<KMMessage*>& msgList )
431 mStorage->takeMessages( msgList );
434 int KMFolder::addMsg( KMMessage* msg, int* index_return )
436 return mStorage->addMsg( msg, index_return );
439 int KMFolder::addMsgKeepUID( KMMessage* msg, int* index_return )
441 return mStorage->addMsgKeepUID( msg, index_return );
444 int KMFolder::addMessages( QList<KMMessage*>& list, QList<int>& index_return )
446 return mStorage->addMessages( list, index_return );
449 void KMFolder::emitMsgAddedSignals( int idx )
451 mStorage->emitMsgAddedSignals( idx );
454 void KMFolder::removeMsg( int i, bool imapQuiet )
456 mStorage->removeMsg( i, imapQuiet );
459 void KMFolder::removeMessages( QList<KMMessage*> msgList, bool imapQuiet ) // TODO const ref
461 mStorage->removeMessages( msgList, imapQuiet );
464 int KMFolder::expungeOldMsg( int days )
466 return mStorage->expungeOldMsg( days );
469 int KMFolder::moveMsg( KMMessage* msg, int* index_return )
471 return mStorage->moveMsg( msg, index_return );
474 int KMFolder::moveMsg(QList<KMMessage*> q, int* index_return )
476 return mStorage->moveMsg( q, index_return );
479 int KMFolder::find( const KMMsgBase* msg ) const
481 return mStorage->find( msg );
484 int KMFolder::find( const KMMessage* msg ) const
486 return mStorage->find( msg );
489 int KMFolder::count( bool cache ) const
491 return mStorage->count( cache );
494 int KMFolder::countUnread()
496 return mStorage->countUnread();
499 int KMFolder::countUnreadRecursive()
501 KMFolder *folder;
502 int count = countUnread();
503 KMFolderDir *dir = child();
504 if (!dir)
505 return count;
507 QList<KMFolderNode*>::const_iterator it;
508 for ( it = dir->constBegin(); it != dir->constEnd(); ++it )
509 if ( !( (*it)->isDir() ) ) {
510 folder = static_cast<KMFolder*>( (*it) );
511 count += folder->countUnreadRecursive();
514 return count;
517 void KMFolder::msgStatusChanged( const MessageStatus& oldStatus,
518 const MessageStatus& newStatus, int idx )
520 mStorage->msgStatusChanged( oldStatus, newStatus, idx );
523 void KMFolder::msgTagListChanged( int idx )
525 emit msgHeaderChanged( this, idx );
529 int KMFolder::open( const char *owner )
531 return mStorage->open( owner );
534 bool KMFolder::canAccess() const
536 return mStorage->canAccess();
539 void KMFolder::close( const char *owner, bool force )
541 // do not emit closed() in here - as this would regain too early
542 mStorage->close( owner, force );
545 void KMFolder::sync()
547 mStorage->sync();
550 bool KMFolder::isOpened() const
552 return mStorage->isOpened();
555 void KMFolder::markNewAsUnread()
557 mStorage->markNewAsUnread();
560 void KMFolder::markUnreadAsRead()
562 mStorage->markUnreadAsRead();
565 void KMFolder::remove()
567 /* The storage needs to be open before remove is called, otherwise
568 it will not unregister the corresponding serial numbers from
569 the message dict, since its message list is empty, and the .ids
570 file contents are not loaded. That can lead to lookups in the
571 dict returning stale pointers to the folder later. */
572 mStorage->open("folderremoval");
573 mStorage->remove();
576 int KMFolder::expunge()
578 return mStorage->expunge();
581 int KMFolder::rename( const QString& newName, KMFolderDir *aParent )
583 return mStorage->rename( newName, aParent );
586 bool KMFolder::dirty() const
588 return mStorage->dirty();
591 void KMFolder::setDirty( bool f )
593 mStorage->setDirty( f );
596 bool KMFolder::needsCompacting() const
598 return mStorage->needsCompacting();
601 void KMFolder::setNeedsCompacting( bool f )
603 mStorage->setNeedsCompacting( f );
606 void KMFolder::quiet( bool beQuiet )
608 mStorage->quiet( beQuiet );
611 bool KMFolder::isReadOnly() const
613 return mStorage->isReadOnly();
616 bool KMFolder::canDeleteMessages() const
618 return mStorage->canDeleteMessages();
621 QString KMFolder::label() const
623 if ( !mSystemLabel.isEmpty() )
624 return mSystemLabel;
625 if ( !mLabel.isEmpty() )
626 return mLabel;
627 if ( isSystemFolder() )
628 return i18n( name().toUtf8() );
629 return name();
632 //-----------------------------------------------------------------------------
633 QString KMFolder::prettyUrl() const
635 QString parentUrl;
636 if ( parent() )
637 parentUrl = parent()->prettyUrl();
638 if ( !parentUrl.isEmpty() )
639 return parentUrl + '/' + label();
640 else
641 return label();
644 //--------------------------------------------------------------------------
645 QString KMFolder::mailingListPostAddress() const
647 if ( mMailingList.features() & MailingList::Post ) {
648 KUrl::List::const_iterator it;
649 KUrl::List post = mMailingList.postURLS();
650 for( it = post.constBegin(); it != post.constEnd(); ++it ) {
651 // We check for isEmpty because before 3.3 postAddress was just an
652 // email@kde.org and that leaves protocol() field in the kurl class
653 if ( (*it).protocol() == "mailto" || (*it).protocol().isEmpty() )
654 return (*it).path();
657 return QString();
660 void KMFolder::setMailingListEnabled( bool enabled )
662 mMailingListEnabled = enabled;
663 mStorage->writeConfig();
666 void KMFolder::setMailingList( const MailingList& mlist )
668 mMailingList = mlist;
669 mStorage->writeConfig();
672 void KMFolder::setUseDefaultIdentity( bool useDefaultIdentity )
674 mUseDefaultIdentity = useDefaultIdentity;
675 if ( mUseDefaultIdentity )
676 mIdentity = kmkernel->identityManager()->defaultIdentity().uoid();
677 kmkernel->slotRequestConfigSync();
680 void KMFolder::setIdentity( uint identity )
682 mIdentity = identity;
683 kmkernel->slotRequestConfigSync();
686 uint KMFolder::identity() const
688 // if we don't have one set ourselves, check our account
689 if ( mUseDefaultIdentity && mStorage )
690 if ( KMAccount *act = mStorage->account() )
691 return act->identityId();
692 return mIdentity;
695 void KMFolder::setWhoField(const QString& aWhoField )
697 mWhoField = aWhoField;
698 #if 0
699 // This isn't saved in the config anyway
700 mStorage->writeConfig();
701 #endif
704 void KMFolder::setUserWhoField( const QString& whoField, bool writeConfig )
706 if ( mUserWhoField == whoField )
707 return;
708 if ( whoField.isEmpty() )
710 // default setting
711 const KPIMIdentities::Identity & identity =
712 kmkernel->identityManager()->identityForUoidOrDefault( mIdentity );
714 if ( isSystemFolder() && folderType() != KMFolderTypeImap ) {
715 // local system folders
716 if ( this == kmkernel->inboxFolder() ||
717 this == kmkernel->trashFolder() )
718 mWhoField = "From";
719 if ( this == kmkernel->outboxFolder() ||
720 this == kmkernel->sentFolder() ||
721 this == kmkernel->templatesFolder() ||
722 this == kmkernel->draftsFolder() )
723 mWhoField = "To";
724 } else if ( identity.drafts() == idString() ||
725 identity.templates() == idString() ||
726 identity.fcc() == idString() )
727 // drafts, templates or sent of the identity
728 mWhoField = "To";
729 else
730 mWhoField = "From";
731 } else if ( whoField == "From" || whoField == "To" )
732 // set the whoField according to the user-setting
733 mWhoField = whoField;
734 else {
735 // this should not happen...
736 kDebug(5006) <<"Illegal setting" << whoField << "for userWhoField!";
737 return; // don't use the value
739 mUserWhoField = whoField;
741 if (writeConfig)
742 mStorage->writeConfig();
743 emit viewConfigChanged();
746 void KMFolder::correctUnreadMsgsCount()
748 mStorage->correctUnreadMsgsCount();
751 QString KMFolder::idString() const
753 KMFolderNode* folderNode = parent();
754 if (!folderNode)
755 return "";
756 while ( folderNode->parent() )
757 folderNode = folderNode->parent();
758 QString myPath = path();
759 int pathLen = myPath.length() - folderNode->path().length();
760 QString relativePath = myPath.right( pathLen );
761 if (!relativePath.isEmpty())
762 relativePath = relativePath.right( relativePath.length() - 1 ) + '/';
763 QString escapedName = name();
764 /* Escape [ and ] as they are disallowed for kconfig sections and that is
765 what the idString is primarily used for. */
766 escapedName.replace( '[', "%(" );
767 escapedName.replace( ']', "%)" );
768 return relativePath + escapedName;
771 void KMFolder::setAutoExpire( bool enabled )
773 if( enabled != mExpireMessages ) {
774 mExpireMessages = enabled;
775 mStorage->writeConfig();
779 void KMFolder::setUnreadExpireAge( int age )
781 if( age >= 0 && age != mUnreadExpireAge ) {
782 mUnreadExpireAge = age;
783 mStorage->writeConfig();
787 void KMFolder::setUnreadExpireUnits( ExpireUnits units )
789 if (units >= expireNever && units < expireMaxUnits)
790 mUnreadExpireUnits = units;
791 mStorage->writeConfig();
794 void KMFolder::setReadExpireAge( int age )
796 if( age >= 0 && age != mReadExpireAge ) {
797 mReadExpireAge = age;
798 mStorage->writeConfig();
802 void KMFolder::setReadExpireUnits( ExpireUnits units )
804 if (units >= expireNever && units <= expireMaxUnits)
805 mReadExpireUnits = units;
806 mStorage->writeConfig();
810 void KMFolder::setExpireAction( ExpireAction a )
812 if ( a != mExpireAction ) {
813 mExpireAction = a;
814 mStorage->writeConfig();
818 void KMFolder::setExpireToFolderId( const QString& id )
820 if ( id != mExpireToFolderId ) {
821 mExpireToFolderId = id;
822 mStorage->writeConfig();
827 static int daysToExpire( int number, ExpireUnits units )
829 switch (units) {
830 case expireDays: // Days
831 return number;
832 case expireWeeks: // Weeks
833 return number * 7;
834 case expireMonths: // Months - this could be better rather than assuming 31day months.
835 return number * 31;
836 default: // this avoids a compiler warning (not handled enumeration values)
839 return -1;
842 void KMFolder::daysToExpire(int& unreadDays, int& readDays) {
843 unreadDays = ::daysToExpire( getUnreadExpireAge(), getUnreadExpireUnits() );
844 readDays = ::daysToExpire( getReadExpireAge(), getReadExpireUnits() );
847 void KMFolder::expireOldMessages( bool immediate )
849 KMail::ScheduledExpireTask* task = new KMail::ScheduledExpireTask(this, immediate);
850 kmkernel->jobScheduler()->registerTask( task );
851 if ( immediate ) {
852 // #82259: compact after expiring.
853 compact( CompactLater );
857 void KMFolder::compact( CompactOptions options )
859 if ( options == CompactLater ) {
860 KMail::ScheduledCompactionTask* task = new KMail::ScheduledCompactionTask(this, false);
861 kmkernel->jobScheduler()->registerTask( task );
862 } else {
863 mStorage->compact( options == CompactSilentlyNow );
867 KMFolder* KMFolder::trashFolder() const
869 return mStorage ? mStorage->trashFolder() : 0;
872 int KMFolder::writeIndex( bool createEmptyIndex )
874 return mStorage->writeIndex( createEmptyIndex );
877 void KMFolder::setStatus( int idx, const MessageStatus& status, bool toggle )
879 mStorage->setStatus( idx, status, toggle );
882 void KMFolder::setStatus( QList<int>& ids, const MessageStatus& status,
883 bool toggle )
885 mStorage->setStatus( ids, status, toggle);
888 void KMFolder::setIconPaths( const QString &normalPath,
889 const QString &unreadPath )
891 mNormalIconPath = normalPath;
892 mUnreadIconPath = unreadPath;
893 mStorage->writeConfig();
894 emit iconsChanged();
897 void KMFolder::removeJobs()
899 mStorage->removeJobs();
902 int KMFolder::updateIndex()
904 return mStorage->updateIndex();
907 void KMFolder::reallyAddMsg( KMMessage* aMsg )
909 mStorage->reallyAddMsg( aMsg );
912 void KMFolder::reallyAddCopyOfMsg( KMMessage* aMsg )
914 mStorage->reallyAddCopyOfMsg( aMsg );
917 void KMFolder::setShortcut( const KShortcut &sc )
919 if ( mShortcut != sc ) {
920 mShortcut = sc;
921 emit shortcutChanged( this );
925 bool KMFolder::isMoveable() const
927 return !isSystemFolder() && mStorage->isMoveable();
930 void KMFolder::slotContentsTypeChanged( KMail::FolderContentsType type )
932 kmkernel->iCalIface().folderContentsTypeChanged( this, type );
933 emit iconsChanged();
936 void KMFolder::slotFolderSizeChanged()
938 emit folderSizeChanged( this );
939 KMFolder* papa = parent()->manager()->parentFolder( this );
940 if ( papa && papa != this ) {
941 papa->slotFolderSizeChanged();
945 void KMFolder::slotIdentitiesChanged()
947 uint defaultIdentity = kmkernel->identityManager()->defaultIdentity().uoid();
949 // The default identity may have changed, therefore set it again
950 // if necessary
951 if ( mUseDefaultIdentity )
952 mIdentity = defaultIdentity;
954 // Fall back to the default identity if the one used currently is invalid
955 if ( kmkernel->identityManager()->identityForUoid( mIdentity ).isNull() ) {
956 mIdentity = defaultIdentity;
957 mUseDefaultIdentity = true;
962 #include "kmfolder.moc"