You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
5.3 KiB
153 lines
5.3 KiB
/** |
|
* Copyright (c) 2004 David Faure <faure@kde.org> |
|
* |
|
* This program is free software; you can redistribute it and/or modify |
|
* it under the terms of the GNU General Public License as published by |
|
* the Free Software Foundation; version 2 of the License |
|
* |
|
* This program is distributed in the hope that it will be useful, |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
* GNU General Public License for more details. |
|
* |
|
* You should have received a copy of the GNU General Public License |
|
* along with this program; if not, write to the Free Software |
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
* |
|
* In addition, as a special exception, the copyright holders give |
|
* permission to link the code of this program with any edition of |
|
* the Qt library by Trolltech AS, Norway (or with modified versions |
|
* of Qt that use the same license as Qt), and distribute linked |
|
* combinations including the two. You must obey the GNU General |
|
* Public License in all respects for all of the code used other than |
|
* Qt. If you modify this file, you may extend this exception to |
|
* your version of the file, but you are not obligated to do so. If |
|
* you do not wish to do so, delete this exception statement from |
|
* your version. |
|
*/ |
|
|
|
#include "expirejob.h" |
|
#include "kmfolder.h" |
|
#include "globalsettings.h" |
|
#include "folderstorage.h" |
|
#include "kmbroadcaststatus.h" |
|
|
|
#include <kdebug.h> |
|
#include <klocale.h> |
|
|
|
using namespace KMail; |
|
|
|
// Look at this number of messages in each slotDoWork call |
|
#define EXPIREJOB_NRMESSAGES 100 |
|
// And wait this number of milliseconds before calling it again |
|
#define EXPIREJOB_TIMERINTERVAL 10 * 1000 |
|
|
|
/* |
|
Testcases for folder expiry: |
|
Automatic expiry: |
|
- normal case (ensure folder has old mails and expiry settings, wait for auto-expiry) |
|
- having the folder selected when the expiry job would run (gets delayed) |
|
- selecting a folder while an expiry job is running for it (should interrupt) |
|
- exiting kmail while an expiry job is running (should abort & delete things cleanly) |
|
Manual expiry: |
|
- RMB / expire (for one folder) [KMMainWidget::slotExpireFolder()] |
|
- RMB on Local Folders / Expire All Folders [KMFolderMgr::expireAll()] |
|
- Expire All Folders [KMMainWidget::slotExpireAll()] |
|
*/ |
|
|
|
|
|
ExpireJob::ExpireJob( KMFolder* folder, bool immediate ) |
|
: FolderJob( 0, tOther, folder ), mTimer( this ), mCurrentIndex( 0 ), |
|
mImmediate( immediate ), mFolderOpen( false ) |
|
{ |
|
mCancellable = true; |
|
} |
|
|
|
ExpireJob::~ExpireJob() |
|
{ |
|
} |
|
|
|
void ExpireJob::kill() |
|
{ |
|
// We must close the folder if we opened it and got interrupted |
|
if ( mFolderOpen && mDestFolder && mDestFolder->storage() ) |
|
mDestFolder->storage()->close(); |
|
FolderJob::kill(); |
|
} |
|
|
|
void ExpireJob::execute() |
|
{ |
|
mMaxUnreadTime = 0; |
|
mMaxReadTime = 0; |
|
mCurrentIndex = 0; |
|
|
|
int unreadDays, readDays; |
|
mDestFolder->daysToExpire( unreadDays, readDays ); |
|
if (unreadDays > 0) { |
|
kdDebug(5006) << "ExpireJob: deleting unread older than "<< unreadDays << " days" << endl; |
|
mMaxUnreadTime = time(0) - unreadDays * 3600 * 24; |
|
} |
|
if (readDays > 0) { |
|
kdDebug(5006) << "ExpireJob: deleting read older than "<< readDays << " days" << endl; |
|
mMaxReadTime = time(0) - readDays * 3600 * 24; |
|
} |
|
|
|
if ((mMaxUnreadTime == 0) && (mMaxReadTime == 0)) { |
|
kdDebug(5006) << "ExpireJob: nothing to do" << endl; |
|
delete this; |
|
return; |
|
} |
|
|
|
FolderStorage* storage = mDestFolder->storage(); |
|
storage->open(); |
|
mFolderOpen = true; |
|
mCurrentIndex = storage->count()-1; |
|
kdDebug(5006) << "ExpireJob: starting to expire in folder " << mDestFolder->location() << endl; |
|
connect( &mTimer, SIGNAL( timeout() ), SLOT( slotDoWork() ) ); |
|
mTimer.start( EXPIREJOB_TIMERINTERVAL ); |
|
slotDoWork(); |
|
// do nothing here, we might be deleted! |
|
} |
|
|
|
void ExpireJob::slotDoWork() |
|
{ |
|
// No need to worry about mDestFolder==0 here. The FolderStorage deletes the jobs on destruction. |
|
FolderStorage* storage = mDestFolder->storage(); |
|
int stopIndex = mImmediate ? 0 : QMAX( 0, mCurrentIndex - EXPIREJOB_NRMESSAGES ); |
|
#ifdef DEBUG_SCHEDULER |
|
kdDebug(5006) << "ExpireJob: checking messages " << mCurrentIndex << " to " << stopIndex << endl; |
|
#endif |
|
for( ; mCurrentIndex >= stopIndex; mCurrentIndex-- ) { |
|
const KMMsgBase *mb = storage->getMsgBase( mCurrentIndex ); |
|
if (mb == 0) |
|
continue; |
|
if ( mb->isImportant() |
|
&& GlobalSettings::excludeImportantMailFromExpiry() ) |
|
continue; |
|
|
|
time_t maxTime = mb->isUnread() ? mMaxUnreadTime : mMaxReadTime; |
|
|
|
if (mb->date() < maxTime) { |
|
mRemovedMsgs.append( storage->getMsg( mCurrentIndex ) ); |
|
} |
|
} |
|
if ( stopIndex == 0 ) |
|
done(); |
|
} |
|
|
|
void ExpireJob::done() |
|
{ |
|
kdDebug(5006) << "ExpireJob: finished expiring in folder " << mDestFolder->location() |
|
<< " " << mRemovedMsgs.count() << " messages to remove." << endl; |
|
FolderStorage* storage = mDestFolder->storage(); |
|
storage->removeMsg( mRemovedMsgs ); |
|
storage->close(); |
|
mFolderOpen = false; |
|
if ( !mRemovedMsgs.isEmpty() ) { |
|
QString str = i18n( "Expired 1 message from %1", "Expired %n messages from %1", mRemovedMsgs.count() ); |
|
KMBroadcastStatus::instance()->setStatusMsg( str.arg( mDestFolder->label() ) ); |
|
} |
|
delete this; |
|
} |
|
|
|
#include "expirejob.moc"
|
|
|