MailTransport

transportmanagementwidgetng.cpp
1/*
2 SPDX-FileCopyrightText: Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "transportmanagementwidgetng.h"
8#include "transport.h"
9#include "transportmanager.h"
10#include "transportmodel.h"
11#include "ui_transportmanagementwidgetng.h"
12
13#include <KLocalizedString>
14#include <KMessageBox>
15
16#include <QMenu>
17
18using namespace MailTransport;
19
20class MailTransport::TransportManagementWidgetNgPrivate
21{
22public:
23 explicit TransportManagementWidgetNgPrivate(TransportManagementWidgetNg *parent);
24
25 Ui::TransportManagementWidgetNg ui;
27
28 // Slots
29 void defaultClicked();
30 void removeClicked();
31 void renameClicked();
32 void editClicked();
33 void addClicked();
34 void updateButtonState();
35 void slotCustomContextMenuRequested(const QPoint &);
36};
37
38TransportManagementWidgetNgPrivate::TransportManagementWidgetNgPrivate(TransportManagementWidgetNg *parent)
39 : q(parent)
40{
41}
42
43TransportManagementWidgetNg::TransportManagementWidgetNg(QWidget *parent)
44 : QWidget(parent)
45 , d(new TransportManagementWidgetNgPrivate(this))
46{
47 d->ui.setupUi(this);
48 d->updateButtonState();
49
50 d->ui.transportTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
51 connect(d->ui.transportTreeView, &QTreeView::doubleClicked, this, [this]() {
52 d->editClicked();
53 });
54 connect(d->ui.addButton, &QPushButton::clicked, this, [this]() {
55 d->addClicked();
56 });
57 connect(d->ui.editButton, &QPushButton::clicked, this, [this]() {
58 d->editClicked();
59 });
60 connect(d->ui.renameButton, &QPushButton::clicked, this, [this]() {
61 d->renameClicked();
62 });
63 connect(d->ui.removeButton, &QPushButton::clicked, this, [this]() {
64 d->removeClicked();
65 });
66 connect(d->ui.defaultButton, &QPushButton::clicked, this, [this]() {
67 d->defaultClicked();
68 });
69 connect(d->ui.transportTreeView, &QTreeView::customContextMenuRequested, this, [this](QPoint p) {
70 d->slotCustomContextMenuRequested(p);
71 });
72 connect(d->ui.transportTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, [this]() {
73 d->updateButtonState();
74 });
75}
76
78
79void TransportManagementWidgetNgPrivate::updateButtonState()
80{
81 const auto nbItems{ui.transportTreeView->selectionModel()->selectedRows().count()};
82 if (nbItems == 0) {
83 ui.editButton->setEnabled(false);
84 ui.renameButton->setEnabled(false);
85 ui.removeButton->setEnabled(false);
86 ui.defaultButton->setEnabled(false);
87 } else {
88 ui.editButton->setEnabled(nbItems == 1);
89 ui.renameButton->setEnabled(nbItems == 1);
90 ui.removeButton->setEnabled(nbItems >= 1);
91 if (nbItems == 1) {
92 const QModelIndex index = ui.transportTreeView->selectionModel()->selectedRows().constFirst();
93 if (index.data(TransportModel::TransportRoles::DefaultRole).toInt() == TransportManager::self()->defaultTransportId()) {
94 ui.defaultButton->setEnabled(false);
95 } else {
96 ui.defaultButton->setEnabled(true);
97 }
98 } else {
99 ui.defaultButton->setEnabled(false);
100 }
101 }
102}
103
104void TransportManagementWidgetNgPrivate::addClicked()
105{
107}
108
109void TransportManagementWidgetNgPrivate::editClicked()
110{
111 if (!ui.transportTreeView->selectionModel()->hasSelection()) {
112 return;
113 }
114 const QModelIndex index = ui.transportTreeView->selectionModel()->selectedRows().constFirst();
115 const QModelIndex modelIndex = ui.transportTreeView->model()->index(index.row(), TransportModel::TransportRoles::TransportIdentifierRole);
116 Transport *transport = TransportManager::self()->transportById(modelIndex.data().toInt());
117 TransportManager::self()->configureTransport(transport->identifier(), transport, q);
118}
119
120void TransportManagementWidgetNgPrivate::renameClicked()
121{
122 if (!ui.transportTreeView->selectionModel()->hasSelection()) {
123 return;
124 }
125 const QModelIndex index = ui.transportTreeView->selectionModel()->selectedRows().constFirst();
126 ui.transportTreeView->edit(index);
127}
128
129void TransportManagementWidgetNgPrivate::removeClicked()
130{
131 if (!ui.transportTreeView->selectionModel()->hasSelection()) {
132 return;
133 }
134 const auto nbAccount{ui.transportTreeView->selectionModel()->selectedRows().count()};
135
136 const QString msg = (nbAccount == 1)
137 ? i18n("Do you want to remove outgoing account '%1'?", ui.transportTreeView->selectionModel()->selectedRows().constFirst().data().toString())
138 : i18np("Do you really want to remove this %1 outgoing account?", "Do you really want to remove these %1 outgoing accounts?", nbAccount);
139
140 const int rc = KMessageBox::questionTwoActions(q, msg, i18n("Remove outgoing account?"), KStandardGuiItem::remove(), KStandardGuiItem::cancel());
141 if (rc == KMessageBox::ButtonCode::SecondaryAction) {
142 return;
143 }
144
146 lst.reserve(nbAccount);
147 for (const QModelIndex &index : ui.transportTreeView->selectionModel()->selectedRows()) {
148 const QModelIndex modelIndex = ui.transportTreeView->model()->index(index.row(), TransportModel::TransportRoles::TransportIdentifierRole);
149 lst << modelIndex.data().toInt();
150 }
151 for (const auto id : std::as_const(lst)) {
153 }
154}
155
156void TransportManagementWidgetNgPrivate::defaultClicked()
157{
158 if (!ui.transportTreeView->selectionModel()->hasSelection()) {
159 return;
160 }
161
162 const QModelIndex index = ui.transportTreeView->selectionModel()->selectedRows().constFirst();
163 const QModelIndex modelIndex = ui.transportTreeView->model()->index(index.row(), TransportModel::TransportRoles::TransportIdentifierRole);
165}
166
167void TransportManagementWidgetNgPrivate::slotCustomContextMenuRequested(const QPoint &pos)
168{
169 QMenu menu(q);
170 menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18nc("@action:inmenu", "Add…"), q, [this]() {
171 addClicked();
172 });
173 const QModelIndex index = ui.transportTreeView->indexAt(pos);
174 if (index.isValid()) {
175 menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18nc("@action:inmenu", "Modify…"), q, [this]() {
176 editClicked();
177 });
178 menu.addAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18nc("@action:inmenu", "Rename"), q, [this]() {
179 renameClicked();
180 });
181 menu.addSeparator();
182 menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18nc("@action:inmenu", "Remove"), q, [this]() {
183 removeClicked();
184 });
185 const QModelIndex index = ui.transportTreeView->selectionModel()->selectedRows().constFirst();
186 if (index.data(TransportModel::TransportRoles::DefaultRole).toInt() != TransportManager::self()->defaultTransportId()) {
187 menu.addSeparator();
188 menu.addAction(i18n("Set as Default"), q, [this]() {
189 defaultClicked();
190 });
191 }
192 }
193 menu.exec(ui.transportTreeView->viewport()->mapToGlobal(pos));
194}
195
196#include "moc_transportmanagementwidgetng.cpp"
~TransportManagementWidgetNg() 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)
void doubleClicked(const QModelIndex &index)
QIcon fromTheme(const QString &name)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
void reserve(qsizetype size)
QVariant data(int role) const const
bool isValid() const const
int row() const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
CustomContextMenu
int toInt(bool *ok) 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 17 2024 11:49:02 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.