MailTransport

transportmanagementwidgetng.cpp
1/*
2 SPDX-FileCopyrightText: 2024 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 void defaultClicked();
29 void removeClicked();
30 void renameClicked();
31 void editClicked();
32 void addClicked();
33 void updateButtonState();
34 void slotCustomContextMenuRequested(const QPoint &);
35};
36
37TransportManagementWidgetNgPrivate::TransportManagementWidgetNgPrivate(TransportManagementWidgetNg *parent)
38 : q(parent)
39{
40}
41
42TransportManagementWidgetNg::TransportManagementWidgetNg(QWidget *parent)
43 : QWidget(parent)
44 , d(new TransportManagementWidgetNgPrivate(this))
45{
46 d->ui.setupUi(this);
47 d->updateButtonState();
48
49 d->ui.transportTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
50 connect(d->ui.transportTreeView, &QTreeView::doubleClicked, this, [this]() {
51 d->editClicked();
52 });
53 connect(d->ui.addButton, &QPushButton::clicked, this, [this]() {
54 d->addClicked();
55 });
56 connect(d->ui.editButton, &QPushButton::clicked, this, [this]() {
57 d->editClicked();
58 });
59 connect(d->ui.renameButton, &QPushButton::clicked, this, [this]() {
60 d->renameClicked();
61 });
62 connect(d->ui.removeButton, &QPushButton::clicked, this, [this]() {
63 d->removeClicked();
64 });
65 connect(d->ui.defaultButton, &QPushButton::clicked, this, [this]() {
66 d->defaultClicked();
67 });
68 connect(d->ui.transportTreeView, &QTreeView::customContextMenuRequested, this, [this](QPoint p) {
69 d->slotCustomContextMenuRequested(p);
70 });
71 connect(d->ui.transportTreeView->selectionModel(), &QItemSelectionModel::selectionChanged, this, [this]() {
72 d->updateButtonState();
73 });
74 connect(d->ui.transportOnCurrentActivity, &QCheckBox::checkStateChanged, this, [this](Qt::CheckState state) {
75 d->ui.transportTreeView->setEnablePlasmaActivities(state == Qt::Checked);
76 });
77 d->ui.transportOnCurrentActivity->setVisible(false);
78}
79
80bool TransportManagementWidgetNg::enablePlasmaActivities() const
81{
82 return d->ui.transportTreeView->enablePlasmaActivities();
83}
84
85void TransportManagementWidgetNg::setEnablePlasmaActivities(bool newEnablePlasmaActivities)
86{
87 d->ui.transportOnCurrentActivity->setVisible(newEnablePlasmaActivities);
88}
89
90void TransportManagementWidgetNg::setTransportActivitiesAbstract(TransportActivitiesAbstract *activitiesAbstract)
91{
92 d->ui.transportTreeView->setTransportActivitiesAbstract(activitiesAbstract);
93}
94
96
97void TransportManagementWidgetNgPrivate::updateButtonState()
98{
99 const auto nbItems{ui.transportTreeView->selectionModel()->selectedRows().count()};
100 if (nbItems == 0) {
101 ui.editButton->setEnabled(false);
102 ui.renameButton->setEnabled(false);
103 ui.removeButton->setEnabled(false);
104 ui.defaultButton->setEnabled(false);
105 } else {
106 ui.editButton->setEnabled(nbItems == 1);
107 ui.renameButton->setEnabled(nbItems == 1);
108 ui.removeButton->setEnabled(nbItems >= 1);
109 if (nbItems == 1) {
110 const QModelIndex index = ui.transportTreeView->selectionModel()->selectedRows().constFirst();
111 if (index.data(TransportModel::TransportRoles::DefaultRole).toInt() == TransportManager::self()->defaultTransportId()) {
112 ui.defaultButton->setEnabled(false);
113 } else {
114 ui.defaultButton->setEnabled(true);
115 }
116 } else {
117 ui.defaultButton->setEnabled(false);
118 }
119 }
120}
121
122void TransportManagementWidgetNgPrivate::addClicked()
123{
125}
126
127void TransportManagementWidgetNgPrivate::editClicked()
128{
129 if (!ui.transportTreeView->selectionModel()->hasSelection()) {
130 return;
131 }
132 const QModelIndex index = ui.transportTreeView->selectionModel()->selectedRows().constFirst();
133 const QModelIndex modelIndex = ui.transportTreeView->model()->index(index.row(), TransportModel::TransportRoles::TransportIdentifierRole);
134 Transport *transport = TransportManager::self()->transportById(modelIndex.data().toInt());
135 TransportManager::self()->configureTransport(transport->identifier(), transport, q);
136}
137
138void TransportManagementWidgetNgPrivate::renameClicked()
139{
140 if (!ui.transportTreeView->selectionModel()->hasSelection()) {
141 return;
142 }
143 const QModelIndex index = ui.transportTreeView->selectionModel()->selectedRows().constFirst();
144 ui.transportTreeView->edit(index);
145}
146
147void TransportManagementWidgetNgPrivate::removeClicked()
148{
149 if (!ui.transportTreeView->selectionModel()->hasSelection()) {
150 return;
151 }
152 const auto nbAccount{ui.transportTreeView->selectionModel()->selectedRows().count()};
153
154 const QString msg = (nbAccount == 1)
155 ? i18n("Do you want to remove outgoing account '%1'?", ui.transportTreeView->selectionModel()->selectedRows().constFirst().data().toString())
156 : i18np("Do you really want to remove this %1 outgoing account?", "Do you really want to remove these %1 outgoing accounts?", nbAccount);
157
158 const int rc =
159 KMessageBox::questionTwoActions(q, msg, i18nc("@title:window", "Remove outgoing account?"), KStandardGuiItem::remove(), KStandardGuiItem::cancel());
160 if (rc == KMessageBox::ButtonCode::SecondaryAction) {
161 return;
162 }
163
165 lst.reserve(nbAccount);
166 for (const QModelIndex &index : ui.transportTreeView->selectionModel()->selectedRows()) {
167 const QModelIndex modelIndex = ui.transportTreeView->model()->index(index.row(), TransportModel::TransportRoles::TransportIdentifierRole);
168 lst << modelIndex.data().toInt();
169 }
170 for (const auto id : std::as_const(lst)) {
172 }
173}
174
175void TransportManagementWidgetNgPrivate::defaultClicked()
176{
177 if (!ui.transportTreeView->selectionModel()->hasSelection()) {
178 return;
179 }
180
181 const QModelIndex index = ui.transportTreeView->selectionModel()->selectedRows().constFirst();
182 const QModelIndex modelIndex = ui.transportTreeView->model()->index(index.row(), TransportModel::TransportRoles::TransportIdentifierRole);
184}
185
186void TransportManagementWidgetNgPrivate::slotCustomContextMenuRequested(const QPoint &pos)
187{
188 QMenu menu(q);
189 menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18nc("@action:inmenu", "Add…"), q, [this]() {
190 addClicked();
191 });
192 const QModelIndex index = ui.transportTreeView->indexAt(pos);
193 if (index.isValid()) {
194 menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18nc("@action:inmenu", "Modify…"), q, [this]() {
195 editClicked();
196 });
197 menu.addAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18nc("@action:inmenu", "Rename"), q, [this]() {
198 renameClicked();
199 });
200 menu.addSeparator();
201 menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18nc("@action:inmenu", "Remove"), q, [this]() {
202 removeClicked();
203 });
204 const QModelIndex index = ui.transportTreeView->selectionModel()->selectedRows().constFirst();
205 if (index.data(TransportModel::TransportRoles::DefaultRole).toInt() != TransportManager::self()->defaultTransportId()) {
206 menu.addSeparator();
207 menu.addAction(i18n("Set as Default"), q, [this]() {
208 defaultClicked();
209 });
210 }
211 }
212 menu.exec(ui.transportTreeView->viewport()->mapToGlobal(pos));
213}
214
215#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)
CheckState
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 Nov 22 2024 12:07:33 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.