transportmanagementwidget.cpp 5.65 KB
Newer Older
1
/*
2
  SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
3

Allen Winter's avatar
Allen Winter committed
4
  Based on KMail code by:
5
  SPDX-FileCopyrightText: 2001-2003 Marc Mutz <mutz@kde.org>
6

7
  SPDX-License-Identifier: LGPL-2.0-or-later
8
9
10
11
*/

#include "transportmanagementwidget.h"
#include "transport.h"
Laurent Montel's avatar
Laurent Montel committed
12
13
#include "transportmanager.h"
#include "ui_transportmanagementwidget.h"
14

15
#include <KMessageBox>
Laurent Montel's avatar
Laurent Montel committed
16
#include <QMenu>
17

18
using namespace MailTransport;
19

20
class MailTransport::TransportManagementWidgetPrivate
21
{
Laurent Montel's avatar
Laurent Montel committed
22
public:
23
    explicit TransportManagementWidgetPrivate(TransportManagementWidget *parent);
24

25
    Ui::TransportManagementWidget ui;
Laurent Montel's avatar
Laurent Montel committed
26
    TransportManagementWidget *const q;
27
28
29
30
31
32
33
34

    // Slots
    void defaultClicked();
    void removeClicked();
    void renameClicked();
    void editClicked();
    void addClicked();
    void updateButtonState();
Laurent Montel's avatar
Laurent Montel committed
35
    void slotCustomContextMenuRequested(const QPoint &);
36
37
};

38
TransportManagementWidgetPrivate::TransportManagementWidgetPrivate(TransportManagementWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
39
    : q(parent)
40
41
42
{
}

Laurent Montel's avatar
Laurent Montel committed
43
TransportManagementWidget::TransportManagementWidget(QWidget *parent)
Laurent Montel's avatar
Laurent Montel committed
44
    : QWidget(parent)
45
    , d(new TransportManagementWidgetPrivate(this))
46
{
Laurent Montel's avatar
Laurent Montel committed
47
48
49
50
    d->ui.setupUi(this);
    d->updateButtonState();

    d->ui.transportList->setContextMenuPolicy(Qt::CustomContextMenu);
Laurent Montel's avatar
Laurent Montel committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    connect(d->ui.transportList, &QTreeWidget::currentItemChanged, this, [this]() {
        d->updateButtonState();
    });
    connect(d->ui.transportList, &QTreeWidget::itemDoubleClicked, this, [this]() {
        d->editClicked();
    });
    connect(d->ui.addButton, &QPushButton::clicked, this, [this]() {
        d->addClicked();
    });
    connect(d->ui.editButton, &QPushButton::clicked, this, [this]() {
        d->editClicked();
    });
    connect(d->ui.renameButton, &QPushButton::clicked, this, [this]() {
        d->renameClicked();
    });
    connect(d->ui.removeButton, &QPushButton::clicked, this, [this]() {
        d->removeClicked();
    });
    connect(d->ui.defaultButton, &QPushButton::clicked, this, [this]() {
        d->defaultClicked();
    });
    connect(d->ui.transportList, &QTreeWidget::customContextMenuRequested, this, [this](QPoint p) {
        d->slotCustomContextMenuRequested(p);
    });
75
76
}

77
TransportManagementWidget::~TransportManagementWidget() = default;
78

79
void TransportManagementWidgetPrivate::updateButtonState()
80
{
Laurent Montel's avatar
Laurent Montel committed
81
82
83
84
85
86
    // TODO figure out current item vs. selected item (in almost every function)
    if (!ui.transportList->currentItem()) {
        ui.editButton->setEnabled(false);
        ui.renameButton->setEnabled(false);
        ui.removeButton->setEnabled(false);
        ui.defaultButton->setEnabled(false);
Allen Winter's avatar
Allen Winter committed
87
    } else {
Laurent Montel's avatar
Laurent Montel committed
88
89
90
        ui.editButton->setEnabled(true);
        ui.renameButton->setEnabled(true);
        ui.removeButton->setEnabled(true);
Laurent Montel's avatar
Laurent Montel committed
91
        if (ui.transportList->currentItem()->data(0, Qt::UserRole) == TransportManager::self()->defaultTransportId()) {
Laurent Montel's avatar
Laurent Montel committed
92
93
94
95
            ui.defaultButton->setEnabled(false);
        } else {
            ui.defaultButton->setEnabled(true);
        }
Allen Winter's avatar
Allen Winter committed
96
    }
97
98
}

99
void TransportManagementWidgetPrivate::addClicked()
100
{
Laurent Montel's avatar
Laurent Montel committed
101
    TransportManager::self()->showTransportCreationDialog(q);
102
103
}

104
void TransportManagementWidgetPrivate::editClicked()
105
{
Laurent Montel's avatar
Laurent Montel committed
106
107
108
    if (!ui.transportList->currentItem()) {
        return;
    }
109

Laurent Montel's avatar
Laurent Montel committed
110
111
    const int currentId = ui.transportList->currentItem()->data(0, Qt::UserRole).toInt();
    Transport *transport = TransportManager::self()->transportById(currentId);
Laurent Montel's avatar
Laurent Montel committed
112
    TransportManager::self()->configureTransport(transport->identifier(), transport, q);
113
114
}

115
void TransportManagementWidgetPrivate::renameClicked()
116
{
Laurent Montel's avatar
Laurent Montel committed
117
118
119
    if (!ui.transportList->currentItem()) {
        return;
    }
120

Laurent Montel's avatar
Laurent Montel committed
121
    ui.transportList->editItem(ui.transportList->currentItem(), 0);
122
123
}

124
void TransportManagementWidgetPrivate::removeClicked()
125
{
Laurent Montel's avatar
Laurent Montel committed
126
127
128
    if (!ui.transportList->currentItem()) {
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
129
130
    const int rc = KMessageBox::questionYesNo(q,
                                              i18n("Do you want to remove outgoing account '%1'?", ui.transportList->currentItem()->text(0)),
131
132
133
                                              i18n("Remove outgoing account?"),
                                              KStandardGuiItem::remove(),
                                              KStandardGuiItem::cancel());
Laurent Montel's avatar
Laurent Montel committed
134
135
136
137
    if (rc == KMessageBox::No) {
        return;
    }

Laurent Montel's avatar
Laurent Montel committed
138
    TransportManager::self()->removeTransport(ui.transportList->currentItem()->data(0, Qt::UserRole).toInt());
139
140
}

141
void TransportManagementWidgetPrivate::defaultClicked()
142
{
Laurent Montel's avatar
Laurent Montel committed
143
144
145
    if (!ui.transportList->currentItem()) {
        return;
    }
146

Laurent Montel's avatar
Laurent Montel committed
147
    TransportManager::self()->setDefaultTransport(ui.transportList->currentItem()->data(0, Qt::UserRole).toInt());
148
149
}

150
void TransportManagementWidgetPrivate::slotCustomContextMenuRequested(const QPoint &pos)
Laurent Montel's avatar
Laurent Montel committed
151
{
Laurent Montel's avatar
Laurent Montel committed
152
    QMenu menu(q);
Laurent Montel's avatar
Laurent Montel committed
153
    menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add..."), q, [this]() {
Laurent Montel's avatar
Laurent Montel committed
154
155
        addClicked();
    });
Laurent Montel's avatar
Laurent Montel committed
156
157
    QTreeWidgetItem *item = ui.transportList->itemAt(pos);
    if (item) {
Laurent Montel's avatar
Laurent Montel committed
158
        menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18n("Modify..."), q, [this]() {
Laurent Montel's avatar
Laurent Montel committed
159
160
            editClicked();
        });
Laurent Montel's avatar
Laurent Montel committed
161
        menu.addAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18n("Rename"), q, [this]() {
Laurent Montel's avatar
Laurent Montel committed
162
163
            renameClicked();
        });
Laurent Montel's avatar
Laurent Montel committed
164
        menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18n("Remove"), q, [this]() {
Laurent Montel's avatar
Laurent Montel committed
165
166
            removeClicked();
        });
Laurent Montel's avatar
Laurent Montel committed
167
        if (item->data(0, Qt::UserRole) != TransportManager::self()->defaultTransportId()) {
Laurent Montel's avatar
Laurent Montel committed
168
            menu.addSeparator();
Laurent Montel's avatar
Laurent Montel committed
169
170
171
            menu.addAction(i18n("Set as Default"), q, [this]() {
                defaultClicked();
            });
Laurent Montel's avatar
Laurent Montel committed
172
        }
Laurent Montel's avatar
Laurent Montel committed
173
    }
Laurent Montel's avatar
Laurent Montel committed
174
    menu.exec(ui.transportList->viewport()->mapToGlobal(pos));
Laurent Montel's avatar
Laurent Montel committed
175
176
}

177
#include "moc_transportmanagementwidget.cpp"