MailTransport

transportmanagementwidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2006-2007 Volker Krause <[email protected]>
3 
4  Based on KMail code by:
5  SPDX-FileCopyrightText: 2001-2003 Marc Mutz <[email protected]>
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 
20 using namespace MailTransport;
21 
22 class MailTransport::TransportManagementWidgetPrivate
23 {
24 public:
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 
40 TransportManagementWidgetPrivate::TransportManagementWidgetPrivate(TransportManagementWidget *parent)
41  : q(parent)
42 {
43 }
44 
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 
81 void 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 
105 void TransportManagementWidgetPrivate::addClicked()
106 {
108 }
109 
110 void TransportManagementWidgetPrivate::editClicked()
111 {
112  if (ui.transportList->selectedItems().isEmpty()) {
113  return;
114  }
115 
116  const int currentId = ui.transportList->selectedItems().at(0)->data(0, Qt::UserRole).toInt();
117  Transport *transport = TransportManager::self()->transportById(currentId);
118  TransportManager::self()->configureTransport(transport->identifier(), transport, q);
119 }
120 
121 void TransportManagementWidgetPrivate::renameClicked()
122 {
123  if (ui.transportList->selectedItems().isEmpty()) {
124  return;
125  }
126 
127  ui.transportList->editItem(ui.transportList->selectedItems().at(0), 0);
128 }
129 
130 void 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().at(0)->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  for (QTreeWidgetItem *selecteditem : selectedItems) {
146  TransportManager::self()->removeTransport(selecteditem->data(0, Qt::UserRole).toInt());
147  }
148 }
149 
150 void TransportManagementWidgetPrivate::defaultClicked()
151 {
152  if (ui.transportList->selectedItems().isEmpty()) {
153  return;
154  }
155 
156  TransportManager::self()->setDefaultTransport(ui.transportList->selectedItems().at(0)->data(0, Qt::UserRole).toInt());
157 }
158 
159 void TransportManagementWidgetPrivate::slotCustomContextMenuRequested(const QPoint &pos)
160 {
161  QMenu menu(q);
162  menu.addAction(QIcon::fromTheme(QStringLiteral("list-add")), i18n("Add..."), q, [this]() {
163  addClicked();
164  });
165  QTreeWidgetItem *item = ui.transportList->itemAt(pos);
166  if (item) {
167  menu.addAction(QIcon::fromTheme(QStringLiteral("document-edit")), i18n("Modify..."), q, [this]() {
168  editClicked();
169  });
170  menu.addAction(QIcon::fromTheme(QStringLiteral("edit-rename")), i18n("Rename"), q, [this]() {
171  renameClicked();
172  });
173  menu.addSeparator();
174  menu.addAction(QIcon::fromTheme(QStringLiteral("list-remove")), i18n("Remove"), q, [this]() {
175  removeClicked();
176  });
178  menu.addSeparator();
179  menu.addAction(i18n("Set as Default"), q, [this]() {
180  defaultClicked();
181  });
182  }
183  }
184  menu.exec(ui.transportList->viewport()->mapToGlobal(pos));
185 }
186 
187 #include "moc_transportmanagementwidget.cpp"
UserRole
Q_SCRIPTABLE int defaultTransportId() const
Returns the default transport identifier.
Transport * transportById(int id, bool def=true) const
Returns the Transport object with the given id.
void customContextMenuRequested(const QPoint &pos)
void clicked(bool checked)
QIcon fromTheme(const QString &name)
Q_SCRIPTABLE void setDefaultTransport(int id)
Sets the default transport.
CustomContextMenu
~TransportManagementWidget() override
Destroys the widget.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
KGuiItem cancel()
TransportManagementWidget(QWidget *parent=nullptr)
Creates a new TransportManagementWidget.
QString i18n(const char *text, const TYPE &arg...)
A widget to manage mail transports.
virtual QVariant data(int column, int role) const const
static TransportManager * self()
Returns the TransportManager instance.
KGuiItem remove()
void itemSelectionChanged()
ButtonCode questionTwoActions(QWidget *parent, const QString &text, const QString &title, const KGuiItem &primaryAction, const KGuiItem &secondaryAction, const QString &dontAskAgainName=QString(), Options options=Notify)
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
Represents the settings of a specific mail transport.
Definition: transport.h:32
bool showTransportCreationDialog(QWidget *parent, ShowCondition showCondition=Always)
Shows a dialog for creating and configuring a new transport.
void itemDoubleClicked(QTreeWidgetItem *item, int column)
bool configureTransport(const QString &identifier, Transport *transport, QWidget *parent)
Open a configuration dialog for an existing transport.
Q_SCRIPTABLE void removeTransport(int id)
Deletes the specified transport.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Mar 23 2023 04:19:12 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.