dkimmanagerkeywidget.cpp 4.45 KB
Newer Older
Laurent Montel's avatar
Laurent Montel committed
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   SPDX-FileCopyrightText: 2018-2021 Laurent Montel <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3

4
   SPDX-License-Identifier: LGPL-2.0-or-later
Laurent Montel's avatar
Laurent Montel committed
5
6
7
*/

#include "dkimmanagerkeywidget.h"
Laurent Montel's avatar
Laurent Montel committed
8
#include "dkimmanagerkey.h"
Laurent Montel's avatar
Laurent Montel committed
9

Laurent Montel's avatar
Laurent Montel committed
10
#include <KLocalizedString>
Laurent Montel's avatar
Laurent Montel committed
11
#include <KMessageBox>
Laurent Montel's avatar
Laurent Montel committed
12
13
14
15
16
17
18
#include <KTreeWidgetSearchLine>
#include <QApplication>
#include <QClipboard>
#include <QHeaderView>
#include <QMenu>
#include <QTreeWidget>
#include <QVBoxLayout>
Laurent Montel's avatar
Laurent Montel committed
19

Laurent Montel's avatar
Laurent Montel committed
20
21
22
using namespace MessageViewer;
DKIMManagerKeyWidget::DKIMManagerKeyWidget(QWidget *parent)
    : QWidget(parent)
Laurent Montel's avatar
Laurent Montel committed
23
    , mTreeWidget(new QTreeWidget(this))
Laurent Montel's avatar
Laurent Montel committed
24
{
Laurent Montel's avatar
Laurent Montel committed
25
    auto mainLayout = new QVBoxLayout(this);
Laurent Montel's avatar
Laurent Montel committed
26
    mainLayout->setObjectName(QStringLiteral("mainlayout"));
Laurent Montel's avatar
Laurent Montel committed
27
    mainLayout->setContentsMargins({});
Laurent Montel's avatar
Laurent Montel committed
28
29

    mTreeWidget->setObjectName(QStringLiteral("treewidget"));
Laurent Montel's avatar
Laurent Montel committed
30
    mTreeWidget->setRootIsDecorated(false);
Laurent Montel's avatar
Laurent Montel committed
31
    mTreeWidget->setHeaderLabels({i18n("SDID"), i18n("Selector"), i18n("DKIM Key")});
Laurent Montel's avatar
Laurent Montel committed
32
    mTreeWidget->setContextMenuPolicy(Qt::CustomContextMenu);
Laurent Montel's avatar
Laurent Montel committed
33
    mTreeWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
Laurent Montel's avatar
Laurent Montel committed
34
    mTreeWidget->setAlternatingRowColors(true);
Laurent Montel's avatar
Laurent Montel committed
35

Laurent Montel's avatar
Laurent Montel committed
36
    auto searchLineEdit = new KTreeWidgetSearchLine(this, mTreeWidget);
Laurent Montel's avatar
Laurent Montel committed
37
38
39
40
41
    searchLineEdit->setObjectName(QStringLiteral("searchlineedit"));
    searchLineEdit->setClearButtonEnabled(true);
    mainLayout->addWidget(searchLineEdit);

    mainLayout->addWidget(mTreeWidget);
42
    connect(mTreeWidget, &QTreeWidget::customContextMenuRequested, this, &DKIMManagerKeyWidget::slotCustomContextMenuRequested);
Laurent Montel's avatar
Laurent Montel committed
43
44
45
46
47
}

DKIMManagerKeyWidget::~DKIMManagerKeyWidget()
{
}
Laurent Montel's avatar
Laurent Montel committed
48

Laurent Montel's avatar
Laurent Montel committed
49
50
51
52
53
54
55
56
57
58
QByteArray DKIMManagerKeyWidget::saveHeaders() const
{
    return mTreeWidget->header()->saveState();
}

void DKIMManagerKeyWidget::restoreHeaders(const QByteArray &header)
{
    mTreeWidget->header()->restoreState(header);
}

59
void DKIMManagerKeyWidget::slotCustomContextMenuRequested(const QPoint &pos)
Laurent Montel's avatar
Laurent Montel committed
60
{
61
    QTreeWidgetItem *item = mTreeWidget->itemAt(pos);
Laurent Montel's avatar
Laurent Montel committed
62
    QMenu menu(this);
Laurent Montel's avatar
Laurent Montel committed
63
    if (item) {
Laurent Montel's avatar
Laurent Montel committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
        const int selectedItemsCount{mTreeWidget->selectedItems().count()};
        if (selectedItemsCount == 1) {
            menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy Key"), this, [item]() {
                QApplication::clipboard()->setText(item->text(2));
            });
            menu.addSeparator();
        }
        menu.addAction(QIcon::fromTheme(QStringLiteral("edit-delete")),
                       i18np("Remove Key", "Remove Keys", selectedItemsCount),
                       this,
                       [this, selectedItemsCount]() {
                           if (KMessageBox::Yes
                               == KMessageBox::warningYesNo(this,
                                                            i18np("Do you want to delete this key?", "Do you want to delete these keys?", selectedItemsCount),
                                                            i18np("Delete Key", "Delete Keys", selectedItemsCount))) {
                               const auto selectedItems = mTreeWidget->selectedItems();
                               for (QTreeWidgetItem *item : selectedItems) {
                                   delete item;
                               }
                           }
                       });
Laurent Montel's avatar
Laurent Montel committed
85
86
        menu.addSeparator();
    }
Laurent Montel's avatar
Laurent Montel committed
87
88
    if (mTreeWidget->topLevelItemCount() > 0) {
        menu.addAction(i18n("Delete All"), this, [this]() {
Laurent Montel's avatar
Laurent Montel committed
89
            if (KMessageBox::Yes == KMessageBox::warningYesNo(this, i18n("Do you want to delete all keys?"), i18n("Delete Keys"))) {
Laurent Montel's avatar
Laurent Montel committed
90
91
92
93
94
95
96
                mTreeWidget->clear();
            }
        });
    }
    if (!menu.isEmpty()) {
        menu.exec(QCursor::pos());
    }
Laurent Montel's avatar
Laurent Montel committed
97
98
}

Laurent Montel's avatar
Laurent Montel committed
99
100
void DKIMManagerKeyWidget::loadKeys()
{
101
102
    const QVector<MessageViewer::KeyInfo> lst = DKIMManagerKey::self()->keys();
    for (const MessageViewer::KeyInfo &key : lst) {
Laurent Montel's avatar
Laurent Montel committed
103
        auto item = new QTreeWidgetItem(mTreeWidget);
104
105
106
        item->setText(0, key.domain);
        item->setText(1, key.selector);
        item->setText(2, key.keyValue);
Laurent Montel's avatar
Laurent Montel committed
107
        item->setToolTip(2, key.keyValue);
108
    }
Laurent Montel's avatar
Laurent Montel committed
109
110
111
112
}

void DKIMManagerKeyWidget::saveKeys()
{
Laurent Montel's avatar
Laurent Montel committed
113
    QVector<MessageViewer::KeyInfo> lst;
Laurent Montel's avatar
Laurent Montel committed
114
    lst.reserve(mTreeWidget->topLevelItemCount());
Laurent Montel's avatar
Laurent Montel committed
115
    for (int i = 0, total = mTreeWidget->topLevelItemCount(); i < total; ++i) {
Laurent Montel's avatar
Laurent Montel committed
116
        QTreeWidgetItem *item = mTreeWidget->topLevelItem(i);
Laurent Montel's avatar
Laurent Montel committed
117
        const MessageViewer::KeyInfo info{item->text(2), item->text(1), item->text(0)};
Laurent Montel's avatar
Laurent Montel committed
118
119
120
        lst.append(info);
    }
    DKIMManagerKey::self()->saveKeys(lst);
Laurent Montel's avatar
Laurent Montel committed
121
}
Laurent Montel's avatar
Laurent Montel committed
122
123
124

void DKIMManagerKeyWidget::resetKeys()
{
Laurent Montel's avatar
Laurent Montel committed
125
    mTreeWidget->clear();
Laurent Montel's avatar
Laurent Montel committed
126
127
    loadKeys();
}