32 #include <libkdepim/misc/broadcaststatus.h>
33 using KPIM::BroadcastStatus;
38 #include <Akonadi/ItemDeleteJob>
39 #include <Akonadi/ItemModifyJob>
40 #include <Akonadi/ItemFetchJob>
41 #include <Akonadi/ItemFetchScope>
42 #include <Akonadi/ItemMoveJob>
43 #include <Akonadi/KMime/MessageParts>
44 #include <Akonadi/KMime/MessageStatus>
45 #include <akonadi/kmime/messageflags.h>
46 #include <KMime/Message>
61 namespace MailCommon {
64 :
ScheduledJob( folder, immediate ), mMaxUnreadTime( 0 ), mMaxReadTime( 0 ), mMoveToFolder( 0 )
82 int unreadDays, readDays;
83 bool mustDeleteExpirationAttribute =
false;
89 expirationAttribute->
daysToExpire( unreadDays, readDays );
90 if ( mustDeleteExpirationAttribute ) {
91 delete expirationAttribute;
94 if ( unreadDays > 0 ) {
95 kDebug() <<
"ExpireJob: deleting unread older than"<< unreadDays <<
"days";
96 mMaxUnreadTime = time(0) - unreadDays * 3600 * 24;
99 kDebug() <<
"ExpireJob: deleting read older than"<< readDays <<
"days";
100 mMaxReadTime = time(0) - readDays * 3600 * 24;
103 if ( ( mMaxUnreadTime == 0 ) && ( mMaxReadTime == 0 ) ) {
104 kDebug() <<
"ExpireJob: nothing to do";
108 kDebug() <<
"ExpireJob: starting to expire in folder" <<
mSrcFolder.name();
113 void ExpireJob::slotDoWork()
115 Akonadi::ItemFetchJob *job =
new Akonadi::ItemFetchJob(
mSrcFolder,
this );
116 job->fetchScope().fetchPayloadPart( Akonadi::MessagePart::Envelope );
117 connect( job, SIGNAL(
result(KJob*)), SLOT(itemFetchResult(KJob*)) );
120 void ExpireJob::itemFetchResult( KJob *job )
122 if ( job->error() ) {
123 kWarning() << job->errorString();
128 foreach (
const Akonadi::Item &item, qobject_cast<Akonadi::ItemFetchJob*>( job )->items() ) {
129 if ( !item.hasPayload<KMime::Message::Ptr>() ) {
133 const KMime::Message::Ptr mb = item.payload<KMime::Message::Ptr>();
134 Akonadi::MessageStatus status;
135 status.setStatusFromFlags( item.flags() );
136 if ( ( status.isImportant() || status.isToAct() || status.isWatched() ) &&
137 SettingsIf->excludeImportantMailFromExpiry() ) {
141 time_t maxTime = status.isRead() ? mMaxReadTime : mMaxUnreadTime;
143 if ( !mb->date(
false ) ) {
147 if ( mb->date()->dateTime().dateTime().toTime_t() < maxTime ) {
148 mRemovedMsgs.append( item );
155 void ExpireJob::done()
160 if ( !mRemovedMsgs.isEmpty() ) {
161 int count = mRemovedMsgs.count();
165 bool mustDeleteExpirationAttribute =
false;
173 kDebug() <<
"ExpireJob: finished expiring in folder"
175 << count <<
"messages to remove.";
176 Akonadi::ItemDeleteJob *job =
new Akonadi::ItemDeleteJob( mRemovedMsgs,
this );
177 connect( job, SIGNAL(
result(KJob*)),
this, SLOT(slotExpireDone(KJob*)) );
179 str = i18np(
"Removing 1 old message from folder %2...",
180 "Removing %1 old messages from folder %2...",
185 if ( !mMoveToFolder.isValid() ) {
186 str = i18n(
"Cannot expire messages from folder %1: destination "
187 "folder %2 not found",
191 kDebug() <<
"ExpireJob: finished expiring in folder"
193 << mRemovedMsgs.count() <<
"messages to move to"
194 << mMoveToFolder.name();
195 Akonadi::ItemMoveJob *job =
new Akonadi::ItemMoveJob( mRemovedMsgs, mMoveToFolder,
this );
196 connect( job, SIGNAL(
result(KJob*)),
this, SLOT(slotMoveDone(KJob*)) );
198 str = i18np(
"Moving 1 old message from folder %2 to folder %3...",
199 "Moving %1 old messages from folder %2 to folder %3...",
200 count,
mSrcFolder.name(), mMoveToFolder.name() );
203 if ( mustDeleteExpirationAttribute ) {
204 delete expirationAttribute;
207 if ( !str.isEmpty() ) {
208 BroadcastStatus::instance()->setStatusMsg( str );
216 void ExpireJob::slotMoveDone( KJob *job )
218 if ( job->error() ) {
219 kError() << job->error() << job->errorString();
221 Akonadi::ItemMoveJob *itemjob =
dynamic_cast<Akonadi::ItemMoveJob *
>( job );
224 if ( !lst.isEmpty() ) {
226 Q_FOREACH( Akonadi::Item item, lst ) {
227 if (!item.hasFlag(Akonadi::MessageFlags::Seen) ) {
228 item.setFlag( Akonadi::MessageFlags::Seen );
232 if ( !newLst.isEmpty() ) {
233 Akonadi::ItemModifyJob *modifyJob =
new Akonadi::ItemModifyJob( newLst,
this );
234 modifyJob->disableRevisionCheck();
235 connect( modifyJob, SIGNAL(
result(KJob*)),
this, SLOT(slotExpireDone(KJob*)) );
237 slotExpireDone( job );
241 slotExpireDone( job );
245 void ExpireJob::slotExpireDone( KJob *job )
247 if ( job->error() ) {
248 kError() << job->error() << job->errorString();
252 const int error = job->error();
253 bool mustDeleteExpirationAttribute =
false;
262 msg = i18np(
"Removed 1 old message from folder %2.",
263 "Removed %1 old messages from folder %2.",
264 mRemovedMsgs.count(),
267 msg = i18np(
"Moved 1 old message from folder %2 to folder %3.",
268 "Moved %1 old messages from folder %2 to folder %3.",
269 mRemovedMsgs.count(),
mSrcFolder.name(), mMoveToFolder.name() );
273 case Akonadi::Job::UserCanceled:
275 msg = i18n(
"Removing old messages from folder %1 was canceled.",
278 msg = i18n(
"Moving old messages from folder %1 to folder %2 was "
286 msg = i18n(
"Removing old messages from folder %1 failed.",
289 msg = i18n(
"Moving old messages from folder %1 to folder %2 failed.",
295 BroadcastStatus::instance()->setStatusMsg( msg );
296 if ( mustDeleteExpirationAttribute ) {
297 delete expirationAttribute;
304 #include "expirejob.moc"
static ExpireCollectionAttribute * expirationCollectionAttribute(const Akonadi::Collection &collection, bool &mustDeleteExpirationAttribute)
virtual void execute()
Has to be reimplemented.
virtual void kill()
Interrupt the job.
void result(FolderJob *job)
Emitted when the job finishes all processing.
static int daysToExpire(int number, ExpireCollectionAttribute::ExpireUnits units)
ExpireAction expireAction() const
What should expiry do? Delete or move to another folder?
ExpireJob(const Akonadi::Collection &folder, bool immediate)
Akonadi::Collection collectionFromId(const Akonadi::Collection::Id &id) const
Returns the collection associated with the given id, or an invalid collection if not found...
Base class for scheduled jobs.
Akonadi::Collection mSrcFolder
Akonadi::Collection::Id expireToFolderId() const
If expiry should move to folder, return the ID of that folder.
virtual void kill()
Interrupt the job.