29 #include "expirejob.h"
31 #include "globalsettings.h"
32 #include "folderstorage.h"
33 #include "broadcaststatus.h"
34 using KPIM::BroadcastStatus;
35 #include "kmcommands.h"
38 #include <tdelocale.h>
40 using namespace KMail;
43 #define EXPIREJOB_NRMESSAGES 100
45 #define EXPIREJOB_TIMERINTERVAL 100
61 ExpireJob::ExpireJob(
KMFolder* folder,
bool immediate )
62 :
ScheduledJob( folder, immediate ), mTimer( this ), mCurrentIndex( 0 ),
63 mFolderOpen( false ), mMoveToFolder( 0 )
67 ExpireJob::~ExpireJob()
71 void ExpireJob::kill()
73 Q_ASSERT( mCancellable );
75 if ( mFolderOpen && mSrcFolder && mSrcFolder->storage() )
76 mSrcFolder->storage()->close(
"expirejob" );
80 void ExpireJob::execute()
86 int unreadDays, readDays;
87 mSrcFolder->daysToExpire( unreadDays, readDays );
88 if (unreadDays >= 0) {
89 kdDebug(5006) <<
"ExpireJob: deleting unread older than "<< unreadDays <<
" days" << endl;
90 mMaxUnreadTime = time(0) - unreadDays * 3600 * 24;
93 kdDebug(5006) <<
"ExpireJob: deleting read older than "<< readDays <<
" days" << endl;
94 mMaxReadTime = time(0) - readDays * 3600 * 24;
97 if ((mMaxUnreadTime == 0) && (mMaxReadTime == 0)) {
98 kdDebug(5006) <<
"ExpireJob: nothing to do" << endl;
104 mOpeningFolder =
true;
105 storage->
open(
"expirejob" );
106 mOpeningFolder =
false;
108 mCurrentIndex = storage->
count()-1;
109 kdDebug(5006) <<
"ExpireJob: starting to expire in folder " << mSrcFolder->location() << endl;
110 connect( &mTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( slotDoWork() ) );
111 mTimer.start( EXPIREJOB_TIMERINTERVAL );
116 void ExpireJob::slotDoWork()
120 int stopIndex = mImmediate ? 0 : TQMAX( 0, mCurrentIndex - EXPIREJOB_NRMESSAGES );
121 #ifdef DEBUG_SCHEDULER
122 kdDebug(5006) <<
"ExpireJob: checking messages " << mCurrentIndex <<
" to " << stopIndex << endl;
124 for( ; mCurrentIndex >= stopIndex; mCurrentIndex-- ) {
125 const KMMsgBase *mb = storage->
getMsgBase( mCurrentIndex );
126 #ifdef DEBUG_SCHEDULER
127 kdDebug(5006) <<
"ExpireJob: checking message " << mCurrentIndex <<
" existence" << endl;
131 #ifdef DEBUG_SCHEDULER
132 kdDebug(5006) <<
"ExpireJob: checking message " << mCurrentIndex <<
" importance" << endl;
134 if ( ( mb->isImportant() || mb->isTodo() || mb->isWatched() )
135 && GlobalSettings::self()->excludeImportantMailFromExpiry() )
138 #ifdef DEBUG_SCHEDULER
139 kdDebug(5006) <<
"ExpireJob: checking message " << mCurrentIndex <<
" time" << endl;
141 time_t maxTime = mb->isUnread() ? mMaxUnreadTime : mMaxReadTime;
143 #ifdef DEBUG_SCHEDULER
144 kdDebug(5006) <<
"ExpireJob: checking message " << mCurrentIndex <<
" time (" << mb->date() <<
" vs " << maxTime <<
")" << endl;
146 if (mb->date() < maxTime) {
147 kdDebug(5006) <<
"ExpireJob: expiring message " << mCurrentIndex <<
" from folder " << mSrcFolder->location() << endl;
148 mRemovedMsgs.append( storage->
getMsgBase( mCurrentIndex ) );
151 if ( stopIndex == 0 )
155 void ExpireJob::done()
162 if ( !mRemovedMsgs.isEmpty() ) {
163 int count = mRemovedMsgs.count();
165 mCancellable =
false;
166 if ( mSrcFolder->expireAction() == KMFolder::ExpireDelete ) {
168 kdDebug(5006) <<
"ExpireJob: finished expiring in folder "
169 << mSrcFolder->location()
170 <<
" " << count <<
" messages to remove." << endl;
171 KMMoveCommand* cmd =
new KMMoveCommand( 0, mRemovedMsgs );
172 connect( cmd, TQ_SIGNAL( completed( KMCommand * ) ),
173 this, TQ_SLOT( slotMessagesMoved( KMCommand * ) ) );
176 str = i18n(
"Removing 1 old message from folder %1...",
177 "Removing %n old messages from folder %1...", count )
178 .arg( mSrcFolder->label() );
182 kmkernel->findFolderById( mSrcFolder->expireToFolderId() );
183 if ( !mMoveToFolder ) {
184 str = i18n(
"Cannot expire messages from folder %1: destination "
185 "folder %2 not found" )
186 .arg( mSrcFolder->label(), mSrcFolder->expireToFolderId() );
187 kdWarning(5006) << str << endl;
189 kdDebug(5006) <<
"ExpireJob: finished expiring in folder "
190 << mSrcFolder->location() <<
" "
191 << mRemovedMsgs.count() <<
" messages to move to "
192 << mMoveToFolder->label() << endl;
193 KMMoveCommand* cmd =
new KMMoveCommand( mMoveToFolder, mRemovedMsgs );
194 connect( cmd, TQ_SIGNAL( completed( KMCommand * ) ),
195 this, TQ_SLOT( slotMessagesMoved( KMCommand * ) ) );
198 str = i18n(
"Moving 1 old message from folder %1 to folder %2...",
199 "Moving %n old messages from folder %1 to folder %2...",
201 .arg( mSrcFolder->label(), mMoveToFolder->label() );
205 if ( !str.isEmpty() )
206 BroadcastStatus::instance()->setStatusMsg( str );
208 TDEConfigGroup group( KMKernel::config(),
"Folder-" + mSrcFolder->idString() );
209 group.writeEntry(
"Current", -1 );
212 mSrcFolder->storage()->close(
"expirejob" );
218 void ExpireJob::slotMessagesMoved( KMCommand *command )
220 mSrcFolder->storage()->close(
"expirejob" );
223 switch ( command->result() ) {
225 if ( mSrcFolder->expireAction() == KMFolder::ExpireDelete ) {
226 msg = i18n(
"Removed 1 old message from folder %1.",
227 "Removed %n old messages from folder %1.",
228 mRemovedMsgs.count() )
229 .arg( mSrcFolder->label() );
232 msg = i18n(
"Moved 1 old message from folder %1 to folder %2.",
233 "Moved %n old messages from folder %1 to folder %2.",
234 mRemovedMsgs.count() )
235 .arg( mSrcFolder->label(), mMoveToFolder->label() );
238 case KMCommand::Failed:
239 if ( mSrcFolder->expireAction() == KMFolder::ExpireDelete ) {
240 msg = i18n(
"Removing old messages from folder %1 failed." )
241 .arg( mSrcFolder->label() );
244 msg = i18n(
"Moving old messages from folder %1 to folder %2 failed." )
245 .arg( mSrcFolder->label(), mMoveToFolder->label() );
248 case KMCommand::Canceled:
249 if ( mSrcFolder->expireAction() == KMFolder::ExpireDelete ) {
250 msg = i18n(
"Removing old messages from folder %1 was canceled." )
251 .arg( mSrcFolder->label() );
254 msg = i18n(
"Moving old messages from folder %1 to folder %2 was "
256 .arg( mSrcFolder->label(), mMoveToFolder->label() );
260 BroadcastStatus::instance()->setStatusMsg( msg );
265 #include "expirejob.moc"
The FolderStorage class is the bass class for the storage related aspects of a collection of mail (a ...
virtual int open(const char *owner)=0
Open folder for access.
virtual int count(bool cache=false) const
Number of messages in this folder.
virtual const KMMsgBase * getMsgBase(int idx) const =0
Provides access to the basic message fields that are also stored in the index.
Base class for scheduled jobs.