Commit 62d5eaa8 authored by Laurent Montel's avatar Laurent Montel 😁
Browse files

USe new widget

parent c9d83a6c
Pipeline #27470 passed with stage
in 27 minutes and 8 seconds
......@@ -17,24 +17,15 @@
*/
#include "collectionexpirypage.h"
#include "collectionexpirywidget.h"
#include "attributes/expirecollectionattribute.h"
#include "folder/folderrequester.h"
#include "folder/foldersettings.h"
#include "util/mailutil.h"
#include "kernel/mailkernel.h"
#include "mailcommon_debug.h"
#include <CollectionModifyJob>
#include <KPluralHandlingSpinBox>
#include <KLocalizedString>
#include <KMessageBox>
#include <QCheckBox>
#include <QGroupBox>
#include <QPushButton>
#include <QRadioButton>
#include <QVBoxLayout>
using namespace Akonadi;
......@@ -60,113 +51,41 @@ bool CollectionExpiryPage::canHandle(const Akonadi::Collection &col) const
void CollectionExpiryPage::init()
{
QVBoxLayout *globalVBox = new QVBoxLayout(this);
QGridLayout *daysBox = new QGridLayout;
expireReadMailCB = new QCheckBox;
expireReadMailCB->setText(i18n("Expire read messages after"));
connect(expireReadMailCB, &QCheckBox::toggled, this, &CollectionExpiryPage::slotUpdateControls);
daysBox->addWidget(expireReadMailCB, 0, 0, Qt::AlignLeft);
expireReadMailSB = new KPluralHandlingSpinBox;
expireReadMailSB->setMaximum(999999);
expireReadMailSB->setValue(30);
expireReadMailSB->setSuffix(ki18ncp("Expire messages after %1", " day", " days"));
daysBox->addWidget(expireReadMailSB, 0, 1);
connect(expireReadMailSB, QOverload<int>::of(&KPluralHandlingSpinBox::valueChanged), this, &CollectionExpiryPage::slotChanged);
expireUnreadMailCB = new QCheckBox;
expireUnreadMailCB->setText(i18n("Expire unread messages after"));
connect(expireUnreadMailCB, &QCheckBox::toggled, this, &CollectionExpiryPage::slotUpdateControls);
daysBox->addWidget(expireUnreadMailCB, 1, 0, Qt::AlignLeft);
expireUnreadMailSB = new KPluralHandlingSpinBox;
expireUnreadMailSB->setMaximum(99999);
expireUnreadMailSB->setValue(30);
expireUnreadMailSB->setSuffix(ki18ncp("Expire messages after %1", " day", " days"));
daysBox->addWidget(expireUnreadMailSB, 1, 1);
connect(expireUnreadMailSB, QOverload<int>::of(&KPluralHandlingSpinBox::valueChanged), this, &CollectionExpiryPage::slotChanged);
daysBox->setColumnStretch(3, 1);
globalVBox->addLayout(daysBox);
globalVBox->addSpacing(30);
QGroupBox *actionsGroup = new QGroupBox(this);
actionsGroup->setFlat(true); // for mutual exclusion of the radio buttons
QHBoxLayout *moveToHBox = new QHBoxLayout();
moveToHBox->setContentsMargins(0, 0, 0, 0);
moveToHBox->setSpacing(6);
moveToRB = new QRadioButton(actionsGroup);
moveToRB->setText(i18n("Move expired messages to:"));
connect(moveToRB, &QRadioButton::toggled, this, &CollectionExpiryPage::slotUpdateControls);
moveToHBox->addWidget(moveToRB);
folderSelector = new FolderRequester(this);
folderSelector->setMustBeReadWrite(true);
folderSelector->setShowOutbox(false);
moveToHBox->addWidget(folderSelector);
globalVBox->addLayout(moveToHBox);
connect(folderSelector, &FolderRequester::folderChanged, this, &CollectionExpiryPage::slotChanged);
deletePermanentlyRB = new QRadioButton(actionsGroup);
deletePermanentlyRB->setText(i18n("Delete expired messages permanently"));
connect(deletePermanentlyRB, &QRadioButton::toggled, this, &CollectionExpiryPage::slotUpdateControls);
globalVBox->addWidget(deletePermanentlyRB);
globalVBox->addSpacing(30);
expireNowPB = new QPushButton(i18n("Save Settings and Expire Now"), this);
connect(expireNowPB, &QPushButton::clicked, this, &CollectionExpiryPage::slotSaveAndExpire);
globalVBox->addWidget(expireNowPB, 0, Qt::AlignRight);
mCollectionExpiryWidget = new CollectionExpiryWidget(this);
connect(mCollectionExpiryWidget, &CollectionExpiryWidget::saveAndExpireRequested, this, &CollectionExpiryPage::slotSaveAndExpire);
connect(mCollectionExpiryWidget, &CollectionExpiryWidget::configChanged, this, &CollectionExpiryPage::slotConfigChanged);
globalVBox->addWidget(mCollectionExpiryWidget);
globalVBox->addStretch(100); // eat all superfluous space
}
void CollectionExpiryPage::slotConfigChanged(bool changed)
{
mChanged = changed;
}
void CollectionExpiryPage::load(const Akonadi::Collection &collection)
{
mCollection = collection;
init();
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);
}
MailCommon::CollectionExpirySettings settings;
settings.expiryGloballyOn = attr->isAutoExpire();
settings.daysToExpireRead = daysToExpireUnread;
settings.daysToExpireUnread = daysToExpireRead;
settings.mUnreadExpireUnits = attr->readExpireUnits();
settings.mReadExpireUnits = attr->unreadExpireUnits();
settings.mExpireAction = attr->expireAction();
settings.mExpireToFolderId = attr->expireToFolderId();
if (attr->expireAction() == ExpireCollectionAttribute::ExpireDelete) {
deletePermanentlyRB->setChecked(true);
} else {
moveToRB->setChecked(true);
}
Akonadi::Collection::Id destFolderID = attr->expireToFolderId();
if (destFolderID > 0) {
Akonadi::Collection destFolder = Kernel::self()->collectionFromId(destFolderID);
if (destFolder.isValid()) {
folderSelector->setCollection(destFolder);
}
}
mCollectionExpiryWidget->load(settings);
} else {
deletePermanentlyRB->setChecked(true);
mCollectionExpiryWidget->load({});
}
slotUpdateControls();
mChanged = false;
}
......@@ -179,54 +98,7 @@ void CollectionExpiryPage::save(Akonadi::Collection &collection)
void CollectionExpiryPage::saveAndExpire(Akonadi::Collection &collection, bool saveSettings, bool expireNow)
{
const bool enableGlobally = expireReadMailCB->isChecked() || expireUnreadMailCB->isChecked();
const Akonadi::Collection expireToFolder = folderSelector->collection();
if (enableGlobally && moveToRB->isChecked() && !expireToFolder.isValid()) {
KMessageBox::error(this, i18n("Please select a folder to expire messages into.\nIf this is not done, expired messages will be permanently deleted."),
i18n("No Folder Selected"));
deletePermanentlyRB->setChecked(true);
expireNow = false; // settings are not valid
}
MailCommon::ExpireCollectionAttribute *attribute = nullptr;
if (expireToFolder.isValid() && moveToRB->isChecked()) {
if (expireToFolder.id() == collection.id()) {
KMessageBox::error(this, i18n("Please select a different folder than the current folder to expire messages into.\nIf this is not done, expired messages will be permanently deleted."),
i18n("Wrong Folder Selected"));
deletePermanentlyRB->setChecked(true);
expireNow = false; // settings are not valid
} else {
attribute = collection.attribute<MailCommon::ExpireCollectionAttribute>(Akonadi::Collection::AddIfMissing);
attribute->setExpireToFolderId(expireToFolder.id());
}
}
if (!attribute) {
attribute = collection.attribute<MailCommon::ExpireCollectionAttribute>(Akonadi::Collection::AddIfMissing);
}
attribute->setAutoExpire(enableGlobally);
// we always write out days now
attribute->setReadExpireAge(expireReadMailSB->value());
attribute->setUnreadExpireAge(expireUnreadMailSB->value());
attribute->setReadExpireUnits(expireReadMailCB->isChecked() ? MailCommon::ExpireCollectionAttribute::ExpireDays
: MailCommon::ExpireCollectionAttribute::ExpireNever);
attribute->setUnreadExpireUnits(expireUnreadMailCB->isChecked() ? MailCommon::ExpireCollectionAttribute::ExpireDays
: MailCommon::ExpireCollectionAttribute::ExpireNever);
if (deletePermanentlyRB->isChecked()) {
attribute->setExpireAction(ExpireCollectionAttribute::ExpireDelete);
} else {
attribute->setExpireAction(ExpireCollectionAttribute::ExpireMove);
}
if (saveSettings) {
Akonadi::CollectionModifyJob *job = new Akonadi::CollectionModifyJob(collection, this);
job->setProperty("expireNow", expireNow);
connect(job, &Akonadi::CollectionModifyJob::result, this, &CollectionExpiryPage::slotCollectionModified);
} else {
if (expireNow) {
MailCommon::Util::expireOldMessages(collection, true /*immediate*/);
}
}
mCollectionExpiryWidget->save(collection, saveSettings, expireNow);
mChanged = false;
}
......@@ -235,34 +107,6 @@ void CollectionExpiryPage::slotSaveAndExpire()
saveAndExpire(mCollection, true, true); // save and start expire job
}
void CollectionExpiryPage::slotCollectionModified(KJob *job)
{
if (job->error()) {
qCDebug(MAILCOMMON_LOG) << " Error when we modified collection";
return;
}
// trigger immediate expiry if there is something to do
if (job->property("expireNow").toBool()) {
MailCommon::Util::expireOldMessages(mCollection, true /*immediate*/);
}
}
void CollectionExpiryPage::slotUpdateControls()
{
const bool showExpiryActions = expireReadMailCB->isChecked() || expireUnreadMailCB->isChecked();
moveToRB->setEnabled(showExpiryActions);
folderSelector->setEnabled(showExpiryActions && moveToRB->isChecked());
deletePermanentlyRB->setEnabled(showExpiryActions);
expireReadMailSB->setEnabled(expireReadMailCB->isChecked());
expireUnreadMailSB->setEnabled(expireUnreadMailCB->isChecked());
expireNowPB->setEnabled(showExpiryActions);
mChanged = true;
}
void CollectionExpiryPage::slotChanged()
{
mChanged = true;
......
......@@ -50,29 +50,13 @@ public:
void load(const Akonadi::Collection &collection) override;
void save(Akonadi::Collection &collection) override;
protected:
private:
void saveAndExpire(Akonadi::Collection &collection, bool saveSettings, bool _expirenow);
void init();
protected Q_SLOTS:
void slotUpdateControls();
void slotCollectionModified(KJob *job);
void slotChanged();
void slotSaveAndExpire();
private:
void saveAndExpire(Akonadi::Collection &collection, bool saveSettings, bool _expirenow);
private:
void slotConfigChanged(bool changed);
CollectionExpiryWidget *mCollectionExpiryWidget = nullptr;
QCheckBox *expireReadMailCB = nullptr;
KPluralHandlingSpinBox *expireReadMailSB = nullptr;
QCheckBox *expireUnreadMailCB = nullptr;
KPluralHandlingSpinBox *expireUnreadMailSB = nullptr;
QRadioButton *moveToRB = nullptr;
FolderRequester *folderSelector = nullptr;
QRadioButton *deletePermanentlyRB = nullptr;
QPushButton *expireNowPB = nullptr;
Akonadi::Collection mCollection;
bool mChanged = false;
};
......
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