Commit 32245181 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

Fix Bug 411836 - Folder settings for expired message stores bad values (maildir)

FIXED-IN: 5.12.2
BUG: 411836
parent e93fe44f
......@@ -160,7 +160,7 @@ int ExpireCollectionAttribute::daysToExpire(int number, ExpireCollectionAttribut
return -1;
}
void ExpireCollectionAttribute::daysToExpire(int &unreadDays, int &readDays)
void ExpireCollectionAttribute::daysToExpire(int &unreadDays, int &readDays) const
{
unreadDays = ExpireCollectionAttribute::daysToExpire(unreadExpireAge(), unreadExpireUnits());
readDays = ExpireCollectionAttribute::daysToExpire(readExpireAge(), readExpireUnits());
......@@ -172,10 +172,10 @@ QByteArray ExpireCollectionAttribute::serialized() const
QDataStream s(&result, QIODevice::WriteOnly);
s << mExpireToFolderId;
s << (int)mExpireAction;
s << (int)mReadExpireUnits;
s << static_cast<int>(mExpireAction);
s << static_cast<int>(mReadExpireUnits);
s << mReadExpireAge;
s << (int)mUnreadExpireUnits;
s << static_cast<int>(mUnreadExpireUnits);
s << mUnreadExpireAge;
s << mExpireMessages;
......@@ -188,14 +188,26 @@ void ExpireCollectionAttribute::deserialize(const QByteArray &data)
s >> mExpireToFolderId;
int action;
s >> action;
mExpireAction = (ExpireCollectionAttribute::ExpireAction)action;
mExpireAction = static_cast<ExpireCollectionAttribute::ExpireAction>(action);
int valUnitRead;
s >> valUnitRead;
mReadExpireUnits = (ExpireCollectionAttribute::ExpireUnits)valUnitRead;
mReadExpireUnits = static_cast<ExpireCollectionAttribute::ExpireUnits>(valUnitRead);
s >> mReadExpireAge;
int valUnitUread;
s >> valUnitUread;
mUnreadExpireUnits = (ExpireCollectionAttribute::ExpireUnits)valUnitUread;
int valUnitUnread;
s >> valUnitUnread;
mUnreadExpireUnits = static_cast<ExpireCollectionAttribute::ExpireUnits>(valUnitUnread);
s >> mUnreadExpireAge;
s >> mExpireMessages;
}
QDebug operator <<(QDebug d, const ExpireCollectionAttribute &t)
{
d << " mExpireMessages " << t.isAutoExpire();
d << " mUnreadExpireAge " << t.unreadExpireAge();
d << " mReadExpireAge " << t.readExpireAge();
d << " mUnreadExpireUnits " << t.unreadExpireUnits();
d << " mReadExpireUnits " << t.readExpireUnits();
d << " mExpireAction " << t.expireAction();
d << " mExpireToFolderId " << t.expireToFolderId();
return d;
}
......@@ -24,6 +24,7 @@
#include <Attribute>
#include <Collection>
#include <QDebug>
namespace MailCommon {
class MAILCOMMON_EXPORT ExpireCollectionAttribute : public Akonadi::Attribute
......@@ -37,16 +38,16 @@ public:
* must always be the last in the list (for bounds checking).
*/
enum ExpireUnits {
ExpireNever,
ExpireDays,
ExpireWeeks,
ExpireMonths,
ExpireMaxUnits
ExpireNever = 0,
ExpireDays = 1,
ExpireWeeks = 2,
ExpireMonths = 3,
ExpireMaxUnits = 4
};
enum ExpireAction {
ExpireDelete,
ExpireMove
ExpireDelete = 0,
ExpireMove = 1
};
QByteArray type() const override;
......@@ -54,7 +55,7 @@ public:
QByteArray serialized() const override;
void deserialize(const QByteArray &data) override;
void daysToExpire(int &unreadDays, int &readDays);
void daysToExpire(int &unreadDays, int &readDays) const;
/**
* Sets whether this folder automatically expires messages.
......@@ -140,4 +141,5 @@ private:
Akonadi::Collection::Id mExpireToFolderId;
};
}
MAILCOMMON_EXPORT QDebug operator <<(QDebug d, const MailCommon::ExpireCollectionAttribute &t);
#endif /* EXPIRATIONCOLLECTIONATTRIBUTE_H */
......@@ -131,43 +131,40 @@ void CollectionExpiryPage::load(const Akonadi::Collection &collection)
mCollection = collection;
init();
bool mustDeleteExpirationAttribute = false;
MailCommon::ExpireCollectionAttribute *attr = MailCommon::Util::expirationCollectionAttribute(mCollection, mustDeleteExpirationAttribute);
// Load the values from the folder
bool expiryGloballyOn = attr->isAutoExpire();
int daysToExpireRead, daysToExpireUnread;
attr->daysToExpire(daysToExpireUnread, daysToExpireRead);
if (expiryGloballyOn
&& attr->readExpireUnits() != ExpireCollectionAttribute::ExpireNever
&& daysToExpireRead >= 0) {
expireReadMailCB->setChecked(true);
expireReadMailSB->setValue(daysToExpireRead);
}
if (expiryGloballyOn
&& attr->unreadExpireUnits() != ExpireCollectionAttribute::ExpireNever
&& daysToExpireUnread >= 0) {
expireUnreadMailCB->setChecked(true);
expireUnreadMailSB->setValue(daysToExpireUnread);
}
if (attr->expireAction() == ExpireCollectionAttribute::ExpireDelete) {
deletePermanentlyRB->setChecked(true);
} else {
moveToRB->setChecked(true);
}
const MailCommon::ExpireCollectionAttribute *attr = collection.attribute<MailCommon::ExpireCollectionAttribute>();
if (attr) {
// Load the values from the folder
bool expiryGloballyOn = attr->isAutoExpire();
int daysToExpireRead, daysToExpireUnread;
attr->daysToExpire(daysToExpireUnread, daysToExpireRead);
if (expiryGloballyOn
&& attr->readExpireUnits() != ExpireCollectionAttribute::ExpireNever
&& daysToExpireRead >= 0) {
expireReadMailCB->setChecked(true);
expireReadMailSB->setValue(daysToExpireRead);
}
if (expiryGloballyOn
&& attr->unreadExpireUnits() != ExpireCollectionAttribute::ExpireNever
&& daysToExpireUnread >= 0) {
expireUnreadMailCB->setChecked(true);
expireUnreadMailSB->setValue(daysToExpireUnread);
}
Akonadi::Collection::Id destFolderID = attr->expireToFolderId();
if (destFolderID > 0) {
Akonadi::Collection destFolder = Kernel::self()->collectionFromId(destFolderID);
if (destFolder.isValid()) {
folderSelector->setCollection(destFolder);
if (attr->expireAction() == ExpireCollectionAttribute::ExpireDelete) {
deletePermanentlyRB->setChecked(true);
} else {
moveToRB->setChecked(true);
}
}
if (mustDeleteExpirationAttribute) {
delete attr;
Akonadi::Collection::Id destFolderID = attr->expireToFolderId();
if (destFolderID > 0) {
Akonadi::Collection destFolder = Kernel::self()->collectionFromId(destFolderID);
if (destFolder.isValid()) {
folderSelector->setCollection(destFolder);
}
}
} else {
deletePermanentlyRB->setChecked(true);
}
slotUpdateControls();
mChanged = false;
......
......@@ -97,21 +97,16 @@ void FolderCollectionMonitor::expireAllCollection(const QAbstractItemModel *mode
continue;
}
bool mustDeleteExpirationAttribute = false;
MailCommon::ExpireCollectionAttribute *attr
= MailCommon::Util::expirationCollectionAttribute(
collection, mustDeleteExpirationAttribute);
const MailCommon::ExpireCollectionAttribute *attr = collection.attribute<MailCommon::ExpireCollectionAttribute>();
if (attr) {
if (attr->isAutoExpire()) {
MailCommon::Util::expireOldMessages(collection, immediate);
}
if (model->rowCount(index) > 0) {
expireAllCollection(model, immediate, index);
}
if (attr->isAutoExpire()) {
MailCommon::Util::expireOldMessages(collection, immediate);
}
if (mustDeleteExpirationAttribute) {
delete attr;
if (model->rowCount(index) > 0) {
expireAllCollection(model, immediate, index);
}
}
}
}
......
......@@ -84,28 +84,27 @@ void ExpireJob::execute()
mMaxUnreadTime = 0;
mMaxReadTime = 0;
int unreadDays, readDays;
bool mustDeleteExpirationAttribute = false;
MailCommon::ExpireCollectionAttribute *expirationAttribute
= MailCommon::Util::expirationCollectionAttribute(
mSrcFolder, mustDeleteExpirationAttribute);
const MailCommon::ExpireCollectionAttribute *expirationAttribute = mSrcFolder.attribute<MailCommon::ExpireCollectionAttribute>();
if (expirationAttribute) {
expirationAttribute->daysToExpire(unreadDays, readDays);
if (mustDeleteExpirationAttribute) {
delete expirationAttribute;
}
expirationAttribute->daysToExpire(unreadDays, readDays);
if (unreadDays > 0) {
qCDebug(MAILCOMMON_LOG) << "ExpireJob: deleting unread older than" << unreadDays << "days";
mMaxUnreadTime = QDateTime::currentDateTime().toSecsSinceEpoch() - unreadDays * 3600 * 24;
}
if (readDays > 0) {
qCDebug(MAILCOMMON_LOG) << "ExpireJob: deleting read older than" << readDays << "days";
mMaxReadTime = QDateTime::currentDateTime().toSecsSinceEpoch() - readDays * 3600 * 24;
}
if (unreadDays > 0) {
qCDebug(MAILCOMMON_LOG) << "ExpireJob: deleting unread older than" << unreadDays << "days";
mMaxUnreadTime = QDateTime::currentDateTime().toSecsSinceEpoch() - unreadDays * 3600 * 24;
}
if (readDays > 0) {
qCDebug(MAILCOMMON_LOG) << "ExpireJob: deleting read older than" << readDays << "days";
mMaxReadTime = QDateTime::currentDateTime().toSecsSinceEpoch() - readDays * 3600 * 24;
}
if ((mMaxUnreadTime == 0) && (mMaxReadTime == 0)) {
qCDebug(MAILCOMMON_LOG) << "ExpireJob: nothing to do";
if ((mMaxUnreadTime == 0) && (mMaxReadTime == 0)) {
qCDebug(MAILCOMMON_LOG) << "ExpireJob: nothing to do";
deleteLater();
return;
}
} else {
deleteLater();
return;
}
......@@ -167,47 +166,42 @@ void ExpireJob::done()
// The command shouldn't kill us because it opens the folder
mCancellable = false;
bool mustDeleteExpirationAttribute = false;
MailCommon::ExpireCollectionAttribute *expirationAttribute
= MailCommon::Util::expirationCollectionAttribute(
mSrcFolder, mustDeleteExpirationAttribute);
if (expirationAttribute->expireAction() == MailCommon::ExpireCollectionAttribute::ExpireDelete) {
// Expire by deletion, i.e. move to null target folder
qCDebug(MAILCOMMON_LOG) << "ExpireJob: finished expiring in folder"
<< mSrcFolder.name()
<< count << "messages to remove.";
Akonadi::ItemDeleteJob *job = new Akonadi::ItemDeleteJob(mRemovedMsgs, this);
connect(job, &Akonadi::ItemDeleteJob::result, this, &ExpireJob::slotExpireDone);
moving = true;
str = i18np("Removing 1 old message from folder %2...",
"Removing %1 old messages from folder %2...",
count, mSrcFolder.name());
} else {
// Expire by moving
mMoveToFolder = Kernel::self()->collectionFromId(expirationAttribute->expireToFolderId());
if (!mMoveToFolder.isValid()) {
str = i18n("Cannot expire messages from folder %1: destination "
"folder %2 not found",
mSrcFolder.name(), expirationAttribute->expireToFolderId());
qCWarning(MAILCOMMON_LOG) << str;
} else {
const MailCommon::ExpireCollectionAttribute *expirationAttribute = mSrcFolder.attribute<MailCommon::ExpireCollectionAttribute>();
if (expirationAttribute) {
if (expirationAttribute->expireAction() == MailCommon::ExpireCollectionAttribute::ExpireDelete) {
// Expire by deletion, i.e. move to null target folder
qCDebug(MAILCOMMON_LOG) << "ExpireJob: finished expiring in folder"
<< mSrcFolder.name()
<< mRemovedMsgs.count() << "messages to move to"
<< mMoveToFolder.name();
Akonadi::ItemMoveJob *job = new Akonadi::ItemMoveJob(mRemovedMsgs, mMoveToFolder, this);
connect(job, &Akonadi::ItemMoveJob::result, this, &ExpireJob::slotMoveDone);
<< count << "messages to remove.";
Akonadi::ItemDeleteJob *job = new Akonadi::ItemDeleteJob(mRemovedMsgs, this);
connect(job, &Akonadi::ItemDeleteJob::result, this, &ExpireJob::slotExpireDone);
moving = true;
str = i18np("Moving 1 old message from folder %2 to folder %3...",
"Moving %1 old messages from folder %2 to folder %3...",
count, mSrcFolder.name(), mMoveToFolder.name());
str = i18np("Removing 1 old message from folder %2...",
"Removing %1 old messages from folder %2...",
count, mSrcFolder.name());
} else {
// Expire by moving
mMoveToFolder = Kernel::self()->collectionFromId(expirationAttribute->expireToFolderId());
if (!mMoveToFolder.isValid()) {
str = i18n("Cannot expire messages from folder %1: destination "
"folder %2 not found",
mSrcFolder.name(), expirationAttribute->expireToFolderId());
qCWarning(MAILCOMMON_LOG) << str;
} else {
qCDebug(MAILCOMMON_LOG) << "ExpireJob: finished expiring in folder"
<< mSrcFolder.name()
<< mRemovedMsgs.count() << "messages to move to"
<< mMoveToFolder.name();
Akonadi::ItemMoveJob *job = new Akonadi::ItemMoveJob(mRemovedMsgs, mMoveToFolder, this);
connect(job, &Akonadi::ItemMoveJob::result, this, &ExpireJob::slotMoveDone);
moving = true;
str = i18np("Moving 1 old message from folder %2 to folder %3...",
"Moving %1 old messages from folder %2 to folder %3...",
count, mSrcFolder.name(), mMoveToFolder.name());
}
}
}
if (mustDeleteExpirationAttribute) {
delete expirationAttribute;
}
}
if (!str.isEmpty()) {
BroadcastStatus::instance()->setStatusMsg(str);
......@@ -255,51 +249,46 @@ void ExpireJob::slotExpireDone(KJob *job)
QString msg;
const int error = job->error();
bool mustDeleteExpirationAttribute = false;
MailCommon::ExpireCollectionAttribute *expirationAttribute
= MailCommon::Util::expirationCollectionAttribute(
mSrcFolder, mustDeleteExpirationAttribute);
switch (error) {
case KJob::NoError:
if (expirationAttribute->expireAction() == MailCommon::ExpireCollectionAttribute::ExpireDelete) {
msg = i18np("Removed 1 old message from folder %2.",
"Removed %1 old messages from folder %2.",
mRemovedMsgs.count(),
mSrcFolder.name());
} else {
msg = i18np("Moved 1 old message from folder %2 to folder %3.",
"Moved %1 old messages from folder %2 to folder %3.",
mRemovedMsgs.count(), mSrcFolder.name(), mMoveToFolder.name());
}
break;
case Akonadi::Job::UserCanceled:
if (expirationAttribute->expireAction() == MailCommon::ExpireCollectionAttribute::ExpireDelete) {
msg = i18n("Removing old messages from folder %1 was canceled.",
mSrcFolder.name());
} else {
msg = i18n("Moving old messages from folder %1 to folder %2 was "
"canceled.",
mSrcFolder.name(), mMoveToFolder.name());
}
break;
default: //any other error
if (expirationAttribute->expireAction() == MailCommon::ExpireCollectionAttribute::ExpireDelete) {
msg = i18n("Removing old messages from folder %1 failed.",
mSrcFolder.name());
} else {
msg = i18n("Moving old messages from folder %1 to folder %2 failed.",
mSrcFolder.name(), mMoveToFolder.name());
const MailCommon::ExpireCollectionAttribute *expirationAttribute = mSrcFolder.attribute<MailCommon::ExpireCollectionAttribute>();
if (expirationAttribute) {
switch (error) {
case KJob::NoError:
if (expirationAttribute->expireAction() == MailCommon::ExpireCollectionAttribute::ExpireDelete) {
msg = i18np("Removed 1 old message from folder %2.",
"Removed %1 old messages from folder %2.",
mRemovedMsgs.count(),
mSrcFolder.name());
} else {
msg = i18np("Moved 1 old message from folder %2 to folder %3.",
"Moved %1 old messages from folder %2 to folder %3.",
mRemovedMsgs.count(), mSrcFolder.name(), mMoveToFolder.name());
}
break;
case Akonadi::Job::UserCanceled:
if (expirationAttribute->expireAction() == MailCommon::ExpireCollectionAttribute::ExpireDelete) {
msg = i18n("Removing old messages from folder %1 was canceled.",
mSrcFolder.name());
} else {
msg = i18n("Moving old messages from folder %1 to folder %2 was "
"canceled.",
mSrcFolder.name(), mMoveToFolder.name());
}
break;
default: //any other error
if (expirationAttribute->expireAction() == MailCommon::ExpireCollectionAttribute::ExpireDelete) {
msg = i18n("Removing old messages from folder %1 failed.",
mSrcFolder.name());
} else {
msg = i18n("Moving old messages from folder %1 to folder %2 failed.",
mSrcFolder.name(), mMoveToFolder.name());
}
break;
}
break;
}
BroadcastStatus::instance()->setStatusMsg(msg);
if (mustDeleteExpirationAttribute) {
delete expirationAttribute;
BroadcastStatus::instance()->setStatusMsg(msg);
}
deleteLater();
}
......
......@@ -277,32 +277,3 @@ QStringList MailCommon::Util::foundMailer()
}
return lst;
}
MailCommon::ExpireCollectionAttribute *MailCommon::Util::expirationCollectionAttribute(const Akonadi::Collection &collection, bool &mustDeleteExpirationAttribute)
{
Akonadi::Collection mutableCollection = collection;
MailCommon::ExpireCollectionAttribute *attr = nullptr;
if (mutableCollection.hasAttribute<MailCommon::ExpireCollectionAttribute>()) {
attr = mutableCollection.attribute<MailCommon::ExpireCollectionAttribute>();
mustDeleteExpirationAttribute = false;
} else {
attr = new MailCommon::ExpireCollectionAttribute();
KConfigGroup configGroup(KernelIf->config(),
MailCommon::FolderSettings::configGroupName(collection));
if (configGroup.hasKey("ExpireMessages")) {
attr->setAutoExpire(configGroup.readEntry("ExpireMessages", false));
attr->setReadExpireAge(configGroup.readEntry("ReadExpireAge", 3));
attr->setReadExpireUnits((MailCommon::ExpireCollectionAttribute::ExpireUnits)configGroup.readEntry("ReadExpireUnits", (int)MailCommon::ExpireCollectionAttribute::ExpireMonths));
attr->setUnreadExpireAge(configGroup.readEntry("UnreadExpireAge", 12));
attr->setUnreadExpireUnits((MailCommon::ExpireCollectionAttribute::ExpireUnits)configGroup.readEntry("UnreadExpireUnits", (int)MailCommon::ExpireCollectionAttribute::ExpireNever));
attr->setExpireAction(configGroup.readEntry("ExpireAction", "Delete") == QLatin1String("Move")
? MailCommon::ExpireCollectionAttribute::ExpireMove
: MailCommon::ExpireCollectionAttribute::ExpireDelete);
attr->setExpireToFolderId(configGroup.readEntry("ExpireToFolder", -1));
}
mustDeleteExpirationAttribute = true;
}
return attr;
}
......@@ -91,7 +91,6 @@ Q_REQUIRED_RESULT MAILCOMMON_EXPORT QString convertFolderPathToCollectionStr(con
MAILCOMMON_EXPORT void foundMailer(QStringList &lst, const QString &name);
Q_REQUIRED_RESULT MAILCOMMON_EXPORT QStringList foundMailer();
Q_REQUIRED_RESULT MAILCOMMON_EXPORT bool isLocalCollection(const QString &resource);
Q_REQUIRED_RESULT MAILCOMMON_EXPORT MailCommon::ExpireCollectionAttribute *expirationCollectionAttribute(const Akonadi::Collection &collection, bool &mustDeleteExpirationAttribute);
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment