archivemailwidget.cpp 11.6 KB
Newer Older
1
/*
2
   SPDX-FileCopyrightText: 2015-2020 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3

4
   SPDX-License-Identifier: GPL-2.0-or-later
5
6
7
8
9
*/

#include "archivemailwidget.h"
#include "addarchivemaildialog.h"
#include "archivemailagentutil.h"
10
#include "archivemailkernel.h"
11

Laurent Montel's avatar
Laurent Montel committed
12
13
14
#include <KIO/OpenUrlJob>
#include <KIO/JobUiDelegate>

15
#include "kmail-version.h"
16
17

#include <MailCommon/MailUtil>
18
#include <MailCommon/MailKernel>
19
20
21
22

#include <QLocale>
#include <KLocalizedString>
#include <KSharedConfig>
23
#include <KConfigGroup>
24
25
#include <KMessageBox>
#include <QMenu>
26
#include <KAboutData>
27

Laurent Montel's avatar
Laurent Montel committed
28
namespace {
29
30
inline QString archiveMailCollectionPattern()
{
Laurent Montel's avatar
Laurent Montel committed
31
    return QStringLiteral("ArchiveMailCollection \\d+");
32
}
33
34

static const char myConfigGroupName[] = "ArchiveMailDialog";
35
36
37
}

ArchiveMailItem::ArchiveMailItem(QTreeWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
38
    : QTreeWidgetItem(parent)
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{
}

ArchiveMailItem::~ArchiveMailItem()
{
    delete mInfo;
}

void ArchiveMailItem::setInfo(ArchiveMailInfo *info)
{
    mInfo = info;
}

ArchiveMailInfo *ArchiveMailItem::info() const
{
    return mInfo;
}

Laurent Montel's avatar
Laurent Montel committed
57
ArchiveMailWidget::ArchiveMailWidget(const KSharedConfigPtr &config, QWidget *parent, const QVariantList &args)
58
    : Akonadi::AgentConfigurationBase(config, parent, args)
59
{
60
    ArchiveMailKernel *archiveMailKernel = ArchiveMailKernel::self();
61
62
63
    CommonKernel->registerKernelIf(archiveMailKernel);   //register KernelIf early, it is used by the Filter classes
    CommonKernel->registerSettingsIf(archiveMailKernel);   //SettingsIf is used in FolderTreeWidget

Laurent Montel's avatar
Laurent Montel committed
64
65
66
    QWidget *w = new QWidget(parent);
    mWidget.setupUi(w);
    parent->layout()->addWidget(w);
67

68
69
    QStringList headers;
    headers << i18n("Name") << i18n("Last archive") << i18n("Next archive in") << i18n("Storage directory");
70
71
72
73
74
75
76
77
    mWidget.treeWidget->setHeaderLabels(headers);
    mWidget.treeWidget->setObjectName(QStringLiteral("treewidget"));
    mWidget.treeWidget->setSortingEnabled(true);
    mWidget.treeWidget->setRootIsDecorated(false);
    mWidget.treeWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
    mWidget.treeWidget->setContextMenuPolicy(Qt::CustomContextMenu);

    connect(mWidget.treeWidget, &QWidget::customContextMenuRequested,
Laurent Montel's avatar
Laurent Montel committed
78
            this, &ArchiveMailWidget::slotCustomContextMenuRequested);
79

80
81
82
83
84
85
    connect(mWidget.removeItem, &QAbstractButton::clicked, this, &ArchiveMailWidget::slotRemoveItem);
    connect(mWidget.modifyItem, &QAbstractButton::clicked, this, &ArchiveMailWidget::slotModifyItem);
    connect(mWidget.addItem, &QAbstractButton::clicked, this, &ArchiveMailWidget::slotAddItem);
    connect(mWidget.treeWidget, &QTreeWidget::itemChanged, this, &ArchiveMailWidget::slotItemChanged);
    connect(mWidget.treeWidget, &QTreeWidget::itemSelectionChanged, this, &ArchiveMailWidget::updateButtons);
    connect(mWidget.treeWidget, &QTreeWidget::itemDoubleClicked, this, &ArchiveMailWidget::slotModifyItem);
86
    updateButtons();
87
88
89
90
91
92
93

    KAboutData aboutData(
        QStringLiteral("archivemailagent"),
        i18n("Archive Mail Agent"),
        QStringLiteral(KDEPIM_VERSION),
        i18n("Archive emails automatically."),
        KAboutLicense::GPL_V2,
Laurent Montel's avatar
Laurent Montel committed
94
        i18n("Copyright (C) 2014-2020 Laurent Montel"));
95
96
97
98
99
100
    aboutData.addAuthor(i18n("Laurent Montel"),
                        i18n("Maintainer"), QStringLiteral("montel@kde.org"));

    aboutData.setTranslator(i18nc("NAME OF TRANSLATORS", "Your names"),
                            i18nc("EMAIL OF TRANSLATORS", "Your emails"));
    setKAboutData(aboutData);
101
102
103
104
105
106
}

ArchiveMailWidget::~ArchiveMailWidget()
{
}

Laurent Montel's avatar
Laurent Montel committed
107
void ArchiveMailWidget::slotCustomContextMenuRequested(const QPoint &)
108
{
109
110
    const QList<QTreeWidgetItem *> listItems = mWidget.treeWidget->selectedItems();
    QMenu menu(parentWidget());
Laurent Montel's avatar
Laurent Montel committed
111
    menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add..."), this, &ArchiveMailWidget::slotAddItem);
112
113
    if (!listItems.isEmpty()) {
        if (listItems.count() == 1) {
Laurent Montel's avatar
Laurent Montel committed
114
            menu.addAction(i18n("Open Containing Folder..."), this, &ArchiveMailWidget::slotOpenFolder);
115
116
        }
        menu.addSeparator();
Laurent Montel's avatar
Laurent Montel committed
117
        menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18n("Delete"), this, &ArchiveMailWidget::slotRemoveItem);
118
119
120
121
122
123
    }
    menu.exec(QCursor::pos());
}

void ArchiveMailWidget::updateButtons()
{
124
    const QList<QTreeWidgetItem *> listItems = mWidget.treeWidget->selectedItems();
125
    if (listItems.isEmpty()) {
126
127
        mWidget.removeItem->setEnabled(false);
        mWidget.modifyItem->setEnabled(false);
128
    } else if (listItems.count() == 1) {
129
130
        mWidget.removeItem->setEnabled(true);
        mWidget.modifyItem->setEnabled(true);
131
    } else {
132
133
        mWidget.removeItem->setEnabled(true);
        mWidget.modifyItem->setEnabled(false);
134
135
136
137
138
139
    }
}

void ArchiveMailWidget::needReloadConfig()
{
    //TODO add messagebox which informs that we save settings here.
140
    mWidget.treeWidget->clear();
141
142
143
144
145
    load();
}

void ArchiveMailWidget::load()
{
146
147
148
149
    const auto group = config()->group(myConfigGroupName);
    mWidget.treeWidget->header()->restoreState(group.readEntry("HeaderState", QByteArray()));

    const QStringList collectionList = config()->groupList().filter(QRegularExpression(archiveMailCollectionPattern()));
150
151
    const int numberOfCollection = collectionList.count();
    for (int i = 0; i < numberOfCollection; ++i) {
Laurent Montel's avatar
Laurent Montel committed
152
153
        KConfigGroup collectionGroup = config()->group(collectionList.at(i));
        ArchiveMailInfo *info = new ArchiveMailInfo(collectionGroup);
154
155
156
157
158
159
160
161
162
163
164
        if (info->isValid()) {
            createOrUpdateItem(info);
        } else {
            delete info;
        }
    }
}

void ArchiveMailWidget::createOrUpdateItem(ArchiveMailInfo *info, ArchiveMailItem *item)
{
    if (!item) {
165
        item = new ArchiveMailItem(mWidget.treeWidget);
166
167
168
169
170
171
172
173
    }
    item->setText(ArchiveMailWidget::Name, i18n("Folder: %1", MailCommon::Util::fullCollectionPath(Akonadi::Collection(info->saveCollectionId()))));
    item->setCheckState(ArchiveMailWidget::Name, info->isEnabled() ? Qt::Checked : Qt::Unchecked);
    item->setText(ArchiveMailWidget::StorageDirectory, info->url().toLocalFile());
    if (info->lastDateSaved().isValid()) {
        item->setText(ArchiveMailWidget::LastArchiveDate, QLocale().toString(info->lastDateSaved(), QLocale::ShortFormat));
        updateDiffDate(item, info);
    } else {
174
        item->setBackground(ArchiveMailWidget::NextArchive, Qt::green);
175
176
177
178
179
180
181
    }
    item->setInfo(info);
}

void ArchiveMailWidget::updateDiffDate(ArchiveMailItem *item, ArchiveMailInfo *info)
{
    const QDate diffDate = ArchiveMailAgentUtil::diffDate(info);
Laurent Montel's avatar
Laurent Montel committed
182
    const qint64 diff = QDate::currentDate().daysTo(diffDate);
183
184
185
    item->setText(ArchiveMailWidget::NextArchive, i18np("Tomorrow", "%1 days", diff));
    if (diff < 0) {
        if (info->isEnabled()) {
186
            item->setBackground(ArchiveMailWidget::NextArchive, Qt::red);
187
        } else {
188
            item->setBackground(ArchiveMailWidget::NextArchive, Qt::lightGray);
189
190
191
192
193
194
        }
    } else {
        item->setToolTip(ArchiveMailWidget::NextArchive, i18n("Archive will be done %1", QLocale().toString(diffDate, QLocale::ShortFormat)));
    }
}

195
bool ArchiveMailWidget::save() const
196
197
{
    if (!mChanged) {
198
        return false;
199
200
201
    }

    // first, delete all filter groups:
202
    const QStringList filterGroups = config()->groupList().filter(QRegularExpression(archiveMailCollectionPattern()));
203

Laurent Montel's avatar
Laurent Montel committed
204
    for (const QString &group : filterGroups) {
205
        config()->deleteGroup(group);
206
207
    }

208
    const int numberOfItem(mWidget.treeWidget->topLevelItemCount());
209
    for (int i = 0; i < numberOfItem; ++i) {
210
        ArchiveMailItem *mailItem = static_cast<ArchiveMailItem *>(mWidget.treeWidget->topLevelItem(i));
211
        if (mailItem->info()) {
212
            KConfigGroup group = config()->group(ArchiveMailAgentUtil::archivePattern.arg(mailItem->info()->saveCollectionId()));
213
214
215
            mailItem->info()->writeConfig(group);
        }
    }
216
217
218
219
220

    auto group = config()->group(myConfigGroupName);
    group.writeEntry("HeaderState", mWidget.treeWidget->header()->saveState());

    return true;
221
222
223
224
}

void ArchiveMailWidget::slotRemoveItem()
{
225
226
    const QList<QTreeWidgetItem *> listItems = mWidget.treeWidget->selectedItems();
    if (KMessageBox::warningYesNo(parentWidget(), i18n("Do you want to delete the selected items?"), i18n("Remove items")) == KMessageBox::No) {
227
228
229
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
230
    for (QTreeWidgetItem *item : listItems) {
231
232
233
234
235
236
237
238
        delete item;
    }
    mChanged = true;
    updateButtons();
}

void ArchiveMailWidget::slotModifyItem()
{
239
    const QList<QTreeWidgetItem *> listItems = mWidget.treeWidget->selectedItems();
240
241
242
243
244
245
    if (listItems.count() == 1) {
        QTreeWidgetItem *item = listItems.at(0);
        if (!item) {
            return;
        }
        ArchiveMailItem *archiveItem = static_cast<ArchiveMailItem *>(item);
246
        QPointer<AddArchiveMailDialog> dialog = new AddArchiveMailDialog(archiveItem->info(), parentWidget());
247
248
249
250
251
252
253
254
255
256
257
        if (dialog->exec()) {
            ArchiveMailInfo *info = dialog->info();
            createOrUpdateItem(info, archiveItem);
            mChanged = true;
        }
        delete dialog;
    }
}

void ArchiveMailWidget::slotAddItem()
{
258
    QPointer<AddArchiveMailDialog> dialog = new AddArchiveMailDialog(nullptr, parentWidget());
259
260
261
    if (dialog->exec()) {
        ArchiveMailInfo *info = dialog->info();
        if (verifyExistingArchive(info)) {
262
            KMessageBox::error(parentWidget(), i18n("Cannot add a second archive for this folder. Modify the existing one instead."), i18n("Add Archive Mail"));
263
264
265
266
267
268
269
270
271
272
273
274
            delete info;
        } else {
            createOrUpdateItem(info);
            updateButtons();
            mChanged = true;
        }
    }
    delete dialog;
}

bool ArchiveMailWidget::verifyExistingArchive(ArchiveMailInfo *info) const
{
275
    const int numberOfItem(mWidget.treeWidget->topLevelItemCount());
276
    for (int i = 0; i < numberOfItem; ++i) {
277
        ArchiveMailItem *mailItem = static_cast<ArchiveMailItem *>(mWidget.treeWidget->topLevelItem(i));
278
279
280
281
282
283
284
285
286
287
288
289
        ArchiveMailInfo *archiveItemInfo = mailItem->info();
        if (archiveItemInfo) {
            if (info->saveCollectionId() == archiveItemInfo->saveCollectionId()) {
                return true;
            }
        }
    }
    return false;
}

void ArchiveMailWidget::slotOpenFolder()
{
290
    const QList<QTreeWidgetItem *> listItems = mWidget.treeWidget->selectedItems();
291
292
293
294
295
296
297
298
299
    if (listItems.count() == 1) {
        QTreeWidgetItem *item = listItems.first();
        if (!item) {
            return;
        }
        ArchiveMailItem *archiveItem = static_cast<ArchiveMailItem *>(item);
        ArchiveMailInfo *archiveItemInfo = archiveItem->info();
        if (archiveItemInfo) {
            const QUrl url = archiveItemInfo->url();
Laurent Montel's avatar
Laurent Montel committed
300
301
302
303
            KIO::OpenUrlJob *job = new KIO::OpenUrlJob(url);
            job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, parentWidget()));
            job->setRunExecutables(false);
            job->start();
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
        }
    }
}

void ArchiveMailWidget::slotItemChanged(QTreeWidgetItem *item, int col)
{
    if (item) {
        ArchiveMailItem *archiveItem = static_cast<ArchiveMailItem *>(item);
        if (archiveItem->info()) {
            if (col == ArchiveMailWidget::Name) {
                archiveItem->info()->setEnabled(archiveItem->checkState(ArchiveMailWidget::Name) == Qt::Checked);
                mChanged = true;
            } else if (col == ArchiveMailWidget::NextArchive) {
                updateDiffDate(archiveItem, archiveItem->info());
            }
        }
    }
}
322
323
324
325
326
327
328
329
330
331
332
333
334

QSize ArchiveMailWidget::restoreDialogSize() const
{
    auto group = config()->group(myConfigGroupName);
    const QSize size = group.readEntry("Size", QSize(500, 300));
    return size;
}

void ArchiveMailWidget::saveDialogSize(const QSize &size)
{
    auto group = config()->group(myConfigGroupName);
    group.writeEntry("Size", size);
}