MailTransport

transportmanagementwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
3
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#include "transportmanagementwidget.h"
11#include "transport.h"
12#include "transportmanager.h"
13#include "ui_transportmanagementwidget.h"
14
15#include <KLocalizedString>
16#include <KMessageBox>
17
18#include <QMenu>
19
20using namespace MailTransport;
21
22class MailTransport::TransportManagementWidgetPrivate
23{
24public:
25 explicit TransportManagementWidgetPrivate(TransportManagementWidget *parent);
26
27 Ui::TransportManagementWidget ui;
29
30 // Slots
31 void defaultClicked();
32 void removeClicked();
33 void renameClicked();
34 void editClicked();
35 void addClicked();
36 void updateButtonState();
37 void slotCustomContextMenuRequested(const QPoint &);
38};
39
40TransportManagementWidgetPrivate::TransportManagementWidgetPrivate(TransportManagementWidget *parent)
41 : q(parent)
42{
43}
44
45TransportManagementWidget::TransportManagementWidget(QWidget *parent)
46 : QWidget(parent)
47 , d(new TransportManagementWidgetPrivate(this))
48{
49 d->ui.setupUi(this);
50 d->updateButtonState();
51
52 d->ui.transportList->setContextMenuPolicy(Qt::CustomContextMenu);
53 connect(d->ui.transportList, &QTreeWidget::itemDoubleClicked, this, [this]() {
54 d->editClicked();
55 });
56 connect(d->ui.addButton, &QPushButton::clicked, this, [this]() {
57 d->addClicked();
58 });
59 connect(d->ui.editButton, &QPushButton::clicked, this, [this]() {
60 d->editClicked();
61 });
62 connect(d->ui.renameButton, &QPushButton::clicked, this, [this]() {
63 d->renameClicked();
64 });
65 connect(d->ui.removeButton, &QPushButton::clicked, this, [this]() {
66 d->removeClicked();
67 });
68 connect(d->ui.defaultButton, &QPushButton::clicked, this, [this]() {
69 d->defaultClicked();
70 });
71 connect(d->ui.transportList, &QTreeWidget::customContextMenuRequested, this, [this](QPoint p) {
72 d->slotCustomContextMenuRequested(p);
73 });
74 connect(d->ui.transportList, &QTreeWidget::itemSelectionChanged, this, [this]() {
75 d->updateButtonState();
76 });
77}
78
80
81void TransportManagementWidgetPrivate::updateButtonState()
82{
83 const auto nbItems{ui.transportList->selectedItems().count()};
84 if (nbItems == 0) {
85 ui.editButton->setEnabled(false);
86 ui.renameButton->setEnabled(false);
87 ui.removeButton->setEnabled(false);
88 ui.defaultButton->setEnabled(false);
89 } else {
90 ui.editButton->setEnabled(nbItems == 1);
91 ui.renameButton->setEnabled(nbItems == 1);
92 ui.removeButton->setEnabled(nbItems >= 1);
93 if (nbItems == 1) {
94 if (ui.transportList->currentItem()->data(0, Qt::UserRole) == TransportManager::self()->defaultTransportId()) {
95 ui.defaultButton->setEnabled(false);
96 } else {
97 ui.defaultButton->setEnabled(true);
98 }
99 } else {
100 ui.defaultButton->setEnabled(false);
101 }
102 }
103}
104
105void TransportManagementWidgetPrivate::addClicked()
106{
108}
109
110void TransportManagementWidgetPrivate::editClicked()
111{
112 if (ui.transportList->selectedItems().isEmpty()) {
113 return;
114 }
115
116 const int currentId = ui.transportList->selectedItems().constFirst()->data(0, Qt::UserRole).toInt();
117 Transport *transport = TransportManager::self()->transportById(currentId);
118 TransportManager::self()->configureTransport(transport->identifier(), transport, q);
119}
120
121void TransportManagementWidgetPrivate::renameClicked()
122{
123 if (ui.transportList->selectedItems().isEmpty()) {
124 return;
125 }
126
127 ui.transportList->editItem(ui.transportList->selectedItems().constFirst(), 0);
128}
129
130void TransportManagementWidgetPrivate::removeClicked()
131{
132 const auto selectedItems{ui.transportList->selectedItems()};
133 if (selectedItems.isEmpty()) {
134 return;
135 }
136 const auto nbAccount{selectedItems.count()};
137 const QString msg = (selectedItems.count() == 1)
138 ? i18n("Do you want to remove outgoing account '%1'?", ui.transportList->selectedItems().constFirst()->text(0))
139 : i18np("Do you really want to remove this %1 outgoing account?", "Do you really want to remove these %1 outgoing accounts?", nbAccount);
140
141 const int rc = KMessageBox::questionTwoActions(q, msg, i18n("Remove outgoing account?"), KStandardGuiItem::remove(), KStandardGuiItem::cancel());
142 if (rc == KMessageBox::ButtonCode::SecondaryAction) {
143 return;
144 }
145
147 lst.reserve(nbAccount);
148 for (QTreeWidgetItem *selecteditem : selectedItems) {
149 lst << selecteditem->data(0, Qt::UserRole).toInt();
150 }
151 for (const auto id : std::as_const(lst)) {
153 }
154}
155
156void TransportManagementWidgetPrivate::defaultClicked()
157{
158 if (ui.transportList->selectedItems().isEmpty()) {
159 return;
160 }
161
162 TransportManager::self()->setDefaultTransport(ui.transportList->selectedItems().constFirst()->data(0, Qt::UserRole).toInt());
163}
164
165void TransportManagementWidgetPrivate::slotCustomContextMenuRequested(const QPoint &pos)
166{
167 QMenu menu(q);
168 menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18nc("@action:inmenu", "Add..."), q, [this]() {
169 addClicked();
170 });
171 QTreeWidgetItem *item = ui.transportList->itemAt(pos);
172 if (item) {
173 menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18nc("@action:inmenu", "Modify..."), q, [this]() {
174 editClicked();
175 });
176 menu.addAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18nc("@action:inmenu", "Rename"), q, [this]() {
177 renameClicked();
178 });
179 menu.addSeparator();
180 menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18nc("@action:inmenu", "Remove"), q, [this]() {
181 removeClicked();
182 });
184 menu.addSeparator();
185 menu.addAction(i18n("Set as Default"), q, [this]() {
186 defaultClicked();
187 });
188 }
189 }
190 menu.exec(ui.transportList->viewport()->mapToGlobal(pos));
191}
192
193#include "moc_transportmanagementwidget.cpp"
~TransportManagementWidget() override
Destroys the widget.
Q_SCRIPTABLE void removeTransport(int id)
Deletes the specified transport.
Q_SCRIPTABLE void setDefaultTransport(int id)
Sets the default transport.
Transport * transportById(Transport::Id id, bool def=true) const
Returns the Transport object with the given id.
static TransportManager * self()
Returns the TransportManager instance.
bool showTransportCreationDialog(QWidget *parent, ShowCondition showCondition=Always)
Shows a dialog for creating and configuring a new transport.
bool configureTransport(const QString &identifier, Transport *transport, QWidget *parent)
Open a configuration dialog for an existing transport.
Q_SCRIPTABLE int defaultTransportId() const
Returns the default transport identifier.
Represents the settings of a specific mail transport.
Definition transport.h:33
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
KGuiItem remove()
KGuiItem cancel()
void clicked(bool checked)
QIcon fromTheme(const QString &name)
pointer data()
void reserve(qsizetype size)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
CustomContextMenu
UserRole
void itemDoubleClicked(QTreeWidgetItem *item, int column)
void itemSelectionChanged()
virtual QVariant data(int column, int role) const const
void customContextMenuRequested(const QPoint &pos)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 3 2024 11:43:20 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.