33 #include <libkdepim/misc/broadcaststatus.h>
34 using KPIM::BroadcastStatus;
39 #include <Akonadi/ItemDeleteJob>
40 #include <Akonadi/ItemModifyJob>
41 #include <Akonadi/ItemFetchJob>
42 #include <Akonadi/ItemFetchScope>
43 #include <Akonadi/ItemMoveJob>
44 #include <Akonadi/KMime/MessageParts>
45 #include <Akonadi/KMime/MessageStatus>
46 #include <akonadi/kmime/messageflags.h>
47 #include <KMime/Message>
62 namespace MailCommon {
65 :
ScheduledJob( folder, immediate ), mMaxUnreadTime( 0 ), mMaxReadTime( 0 ), mMoveToFolder( 0 )
83 int unreadDays, readDays;
84 bool mustDeleteExpirationAttribute =
false;
90 expirationAttribute->
daysToExpire( unreadDays, readDays );
91 if ( mustDeleteExpirationAttribute ) {
92 delete expirationAttribute;
95 if ( unreadDays > 0 ) {
96 kDebug() <<
"ExpireJob: deleting unread older than"<< unreadDays <<
"days";
97 mMaxUnreadTime = time(0) - unreadDays * 3600 * 24;
100 kDebug() <<
"ExpireJob: deleting read older than"<< readDays <<
"days";
101 mMaxReadTime = time(0) - readDays * 3600 * 24;
104 if ( ( mMaxUnreadTime == 0 ) && ( mMaxReadTime == 0 ) ) {
105 kDebug() <<
"ExpireJob: nothing to do";
109 kDebug() <<
"ExpireJob: starting to expire in folder" <<
mSrcFolder.name();
114 void ExpireJob::slotDoWork()
116 Akonadi::ItemFetchJob *job =
new Akonadi::ItemFetchJob(
mSrcFolder,
this );
117 job->fetchScope().fetchPayloadPart( Akonadi::MessagePart::Envelope );
118 connect( job, SIGNAL(
result(KJob*)), SLOT(itemFetchResult(KJob*)) );
121 void ExpireJob::itemFetchResult( KJob *job )
123 if ( job->error() ) {
124 kWarning() << job->errorString();
129 foreach (
const Akonadi::Item &item, qobject_cast<Akonadi::ItemFetchJob*>( job )->items() ) {
130 if ( !item.hasPayload<KMime::Message::Ptr>() ) {
134 const KMime::Message::Ptr mb = item.payload<KMime::Message::Ptr>();
135 Akonadi::MessageStatus status;
136 status.setStatusFromFlags( item.flags() );
137 if ( ( status.isImportant() || status.isToAct() || status.isWatched() ) &&
138 SettingsIf->excludeImportantMailFromExpiry() ) {
142 time_t maxTime = status.isRead() ? mMaxReadTime : mMaxUnreadTime;
144 if ( !mb->date(
false ) ) {
148 if ( mb->date()->dateTime().dateTime().toTime_t() < maxTime ) {
149 mRemovedMsgs.
append( item );
156 void ExpireJob::done()
161 if ( !mRemovedMsgs.
isEmpty() ) {
162 int count = mRemovedMsgs.
count();
166 bool mustDeleteExpirationAttribute =
false;
174 kDebug() <<
"ExpireJob: finished expiring in folder"
176 << count <<
"messages to remove.";
177 Akonadi::ItemDeleteJob *job =
new Akonadi::ItemDeleteJob( mRemovedMsgs,
this );
178 connect( job, SIGNAL(
result(KJob*)),
this, SLOT(slotExpireDone(KJob*)) );
180 str = i18np(
"Removing 1 old message from folder %2...",
181 "Removing %1 old messages from folder %2...",
186 if ( !mMoveToFolder.isValid() ) {
187 str = i18n(
"Cannot expire messages from folder %1: destination "
188 "folder %2 not found",
192 kDebug() <<
"ExpireJob: finished expiring in folder"
194 << mRemovedMsgs.
count() <<
"messages to move to"
195 << mMoveToFolder.name();
196 Akonadi::ItemMoveJob *job =
new Akonadi::ItemMoveJob( mRemovedMsgs, mMoveToFolder,
this );
197 connect( job, SIGNAL(
result(KJob*)),
this, SLOT(slotMoveDone(KJob*)) );
199 str = i18np(
"Moving 1 old message from folder %2 to folder %3...",
200 "Moving %1 old messages from folder %2 to folder %3...",
201 count,
mSrcFolder.name(), mMoveToFolder.name() );
204 if ( mustDeleteExpirationAttribute ) {
205 delete expirationAttribute;
209 BroadcastStatus::instance()->setStatusMsg( str );
217 void ExpireJob::slotMoveDone( KJob *job )
219 if ( job->error() ) {
220 kError() << job->error() << job->errorString();
222 Akonadi::ItemMoveJob *itemjob =
dynamic_cast<Akonadi::ItemMoveJob *
>( job );
227 Q_FOREACH( Akonadi::Item item, lst ) {
228 if (!item.hasFlag(Akonadi::MessageFlags::Seen) ) {
229 item.setFlag( Akonadi::MessageFlags::Seen );
234 Akonadi::ItemModifyJob *modifyJob =
new Akonadi::ItemModifyJob( newLst,
this );
235 modifyJob->disableRevisionCheck();
236 connect( modifyJob, SIGNAL(
result(KJob*)),
this, SLOT(slotExpireDone(KJob*)) );
238 slotExpireDone( job );
242 slotExpireDone( job );
246 void ExpireJob::slotExpireDone( KJob *job )
248 if ( job->error() ) {
249 kError() << job->error() << job->errorString();
253 const int error = job->error();
254 bool mustDeleteExpirationAttribute =
false;
263 msg = i18np(
"Removed 1 old message from folder %2.",
264 "Removed %1 old messages from folder %2.",
265 mRemovedMsgs.
count(),
268 msg = i18np(
"Moved 1 old message from folder %2 to folder %3.",
269 "Moved %1 old messages from folder %2 to folder %3.",
274 case Akonadi::Job::UserCanceled:
276 msg = i18n(
"Removing old messages from folder %1 was canceled.",
279 msg = i18n(
"Moving old messages from folder %1 to folder %2 was "
287 msg = i18n(
"Removing old messages from folder %1 failed.",
290 msg = i18n(
"Moving old messages from folder %1 to folder %2 failed.",
296 BroadcastStatus::instance()->setStatusMsg( msg );
297 if ( mustDeleteExpirationAttribute ) {
298 delete expirationAttribute;
virtual void execute()
Has to be reimplemented.
MAILCOMMON_EXPORT MailCommon::ExpireCollectionAttribute * expirationCollectionAttribute(const Akonadi::Collection &collection, bool &mustDeleteExpirationAttribute)
virtual void kill()
Interrupt the job.
void result(FolderJob *job)
Emitted when the job finishes all processing.
int count(const T &value) const
void append(const T &value)
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.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void daysToExpire(int &unreadDays, int &readDays)
virtual void kill()
Interrupt the job.