settingsdialog.cpp 5.02 KB
Newer Older
1
/*
2
   SPDX-FileCopyrightText: 2018 Daniel Vrátil <dvratil@kde.org>
3

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

#include "settingsdialog.h"
#include "mailkernel.h"
Laurent Montel's avatar
Laurent Montel committed
9
10
#include "unifiedmailbox.h"
#include "unifiedmailboxeditor.h"
11
12
13

#include <QDialogButtonBox>
#include <QItemSelectionModel>
Laurent Montel's avatar
Laurent Montel committed
14
15
16
17
18
#include <QListView>
#include <QPushButton>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QVBoxLayout>
19

Laurent Montel's avatar
Laurent Montel committed
20
#include <KConfigGroup>
21
22
23
#include <KLocalizedString>
#include <KMessageBox>

24
25
#include <memory>

Laurent Montel's avatar
Laurent Montel committed
26
27
namespace
{
28
static constexpr const char *DialogGroup = "UnifiedMailboxSettingsDialog";
29
30
}

Daniel Vrátil's avatar
Daniel Vrátil committed
31
SettingsDialog::SettingsDialog(const KSharedConfigPtr &config, UnifiedMailboxManager &boxManager, WId, QWidget *parent)
32
    : QDialog(parent)
Laurent Montel's avatar
Laurent Montel committed
33
    , mBoxModel(new QStandardItemModel(this))
34
35
    , mBoxManager(boxManager)
    , mKernel(new MailKernel(config, this))
36
    , mConfig(config)
37
{
Laurent Montel's avatar
Laurent Montel committed
38
    setWindowIcon(QIcon::fromTheme(QStringLiteral("kmail")));
39
    auto l = new QVBoxLayout(this);
40
41
42
43
44
45
46
47
48
49
50
51

    auto h = new QHBoxLayout;
    l->addLayout(h);
    auto view = new QListView(this);
    view->setEditTriggers(QListView::NoEditTriggers);
    view->setModel(mBoxModel);
    h->addWidget(view);

    auto v = new QVBoxLayout;
    h->addLayout(v);
    auto addButton = new QPushButton(QIcon::fromTheme(QStringLiteral("list-add-symbolic")), i18n("Add"));
    v->addWidget(addButton);
Laurent Montel's avatar
Laurent Montel committed
52
    connect(addButton, &QPushButton::clicked, this, [this]() {
Laurent Montel's avatar
Laurent Montel committed
53
54
55
        auto mailbox = std::make_unique<UnifiedMailbox>();
        auto editor = new UnifiedMailboxEditor(mailbox.get(), mConfig, this);
        if (editor->exec()) {
Laurent Montel's avatar
Laurent Montel committed
56
            mailbox->setId(mailbox->name()); // assign ID
Laurent Montel's avatar
Laurent Montel committed
57
58
            addBox(mailbox.get());
            mBoxManager.insertBox(std::move(mailbox));
59
            mBoxManager.saveBoxes();
Laurent Montel's avatar
Laurent Montel committed
60
        }
Laurent Montel's avatar
Laurent Montel committed
61
        delete editor;
Laurent Montel's avatar
Laurent Montel committed
62
    });
63
64
65
    auto editButton = new QPushButton(QIcon::fromTheme(QStringLiteral("entry-edit")), i18n("Modify"));
    editButton->setEnabled(false);
    v->addWidget(editButton);
66
67

    const auto modifyMailBox = [this, view]() {
Laurent Montel's avatar
Laurent Montel committed
68
69
70
71
72
73
74
75
76
77
78
79
80
        const auto indexes = view->selectionModel()->selectedIndexes();
        if (!indexes.isEmpty()) {
            auto item = mBoxModel->itemFromIndex(indexes[0]);
            auto mailbox = item->data().value<UnifiedMailbox *>();
            auto editor = new UnifiedMailboxEditor(mailbox, mConfig, this);
            if (editor->exec()) {
                item->setText(mailbox->name());
                item->setIcon(QIcon::fromTheme(mailbox->icon()));
            }
            delete editor;
            mBoxManager.saveBoxes();
        }
    };
81
82

    connect(view, &QListView::doubleClicked, this, modifyMailBox);
Laurent Montel's avatar
Laurent Montel committed
83
    connect(editButton, &QPushButton::clicked, this, modifyMailBox);
84

85
86
87
    auto removeButton = new QPushButton(QIcon::fromTheme(QStringLiteral("list-remove-symbolic")), i18n("Remove"));
    removeButton->setEnabled(false);
    v->addWidget(removeButton);
Laurent Montel's avatar
Laurent Montel committed
88
    connect(removeButton, &QPushButton::clicked, this, [this, view]() {
Laurent Montel's avatar
Laurent Montel committed
89
90
91
92
        const auto indexes = view->selectionModel()->selectedIndexes();
        if (!indexes.isEmpty()) {
            auto item = mBoxModel->itemFromIndex(indexes[0]);
            const auto mailbox = item->data().value<UnifiedMailbox *>();
Laurent Montel's avatar
Laurent Montel committed
93
94
95
96
97
98
            if (KMessageBox::warningYesNo(this,
                                          i18n("Do you really want to remove unified mailbox <b>%1</b>?", mailbox->name()),
                                          i18n("Really Remove?"),
                                          KStandardGuiItem::remove(),
                                          KStandardGuiItem::cancel())
                == KMessageBox::Yes) {
Laurent Montel's avatar
Laurent Montel committed
99
100
                mBoxModel->removeRow(item->row());
                mBoxManager.removeBox(mailbox->id());
101
                mBoxManager.saveBoxes();
Laurent Montel's avatar
Laurent Montel committed
102
103
104
            }
        }
    });
105
106
    v->addStretch(1);

Laurent Montel's avatar
Laurent Montel committed
107
    connect(view->selectionModel(), &QItemSelectionModel::selectionChanged, this, [view, editButton, removeButton]() {
Laurent Montel's avatar
Laurent Montel committed
108
109
110
111
        const bool hasSelection = view->selectionModel()->hasSelection();
        editButton->setEnabled(hasSelection);
        removeButton->setEnabled(hasSelection);
    });
112

113
    auto box = new QDialogButtonBox(QDialogButtonBox::Close, this);
114
115
116
117
    connect(box, &QDialogButtonBox::rejected, this, &SettingsDialog::reject);
    l->addWidget(box);

    loadBoxes();
Laurent Montel's avatar
Laurent Montel committed
118
119
120
121
122
123
124
    readConfig();
}

SettingsDialog::~SettingsDialog()
{
    writeConfig();
}
125

Laurent Montel's avatar
Laurent Montel committed
126
127
128
129
130
131
void SettingsDialog::readConfig()
{
    auto dlgGroup = mConfig->group(DialogGroup);
    const QSize size = dlgGroup.readEntry("Size", QSize(500, 500));
    if (size.isValid()) {
        resize(size);
132
    }
133
134
}

Laurent Montel's avatar
Laurent Montel committed
135
void SettingsDialog::writeConfig()
136
{
137
    auto dlgGroup = mConfig->group(DialogGroup);
Laurent Montel's avatar
Laurent Montel committed
138
139
    dlgGroup.writeEntry("Size", size());
    dlgGroup.sync();
140
141
142
143
144
}

void SettingsDialog::loadBoxes()
{
    mBoxModel->clear();
145
146
    for (const auto &mailboxIt : mBoxManager) {
        addBox(mailboxIt.second.get());
147
148
149
    }
}

150
void SettingsDialog::addBox(UnifiedMailbox *box)
151
{
152
    auto item = new QStandardItem(QIcon::fromTheme(box->icon()), box->name());
153
154
155
    item->setData(QVariant::fromValue(box));
    mBoxModel->appendRow(item);
}