MailTransport

addtransportdialogng.cpp
1/*
2 SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "addtransportdialogng.h"
8#include "transport.h"
9#include "transportmanager.h"
10#include "transporttype.h"
11#include "ui_addtransportdialog.h"
12
13#include <QDialogButtonBox>
14
15#include <QPushButton>
16
17using namespace MailTransport;
18
19/**
20 @internal
21*/
22class MailTransport::AddTransportDialogNGPrivate
23{
24public:
25 explicit AddTransportDialogNGPrivate(AddTransportDialogNG *qq)
26 : q(qq)
27 {
28 }
29
30 [[nodiscard]] QString selectedType() const;
31
32 /**
33 Enables the OK button if a type is selected.
34 */
35 void updateOkButton(); // slot
36 void doubleClicked(); // slot
37 void writeConfig();
38 void readConfig();
39
40 AddTransportDialogNG *const q;
41 QPushButton *okButton = nullptr;
42 ::Ui::AddTransportDialog ui;
43};
44
45void AddTransportDialogNGPrivate::writeConfig()
46{
47 KConfigGroup group(KSharedConfig::openStateConfig(), QStringLiteral("AddTransportDialog"));
48 group.writeEntry("Size", q->size());
49}
50
51void AddTransportDialogNGPrivate::readConfig()
52{
53 KConfigGroup group(KSharedConfig::openStateConfig(), QStringLiteral("AddTransportDialog"));
54 const QSize sizeDialog = group.readEntry("Size", QSize(300, TransportManager::self()->types().size() > 1 ? 300 : 160));
55 if (sizeDialog.isValid()) {
56 q->resize(sizeDialog);
57 }
58}
59
60QString AddTransportDialogNGPrivate::selectedType() const
61{
62 const QList<QTreeWidgetItem *> sel = ui.typeListView->selectedItems();
63 if (!sel.empty()) {
64 return sel.first()->data(0, Qt::UserRole).toString();
65 }
66 return {};
67}
68
69void AddTransportDialogNGPrivate::doubleClicked()
70{
71 if (!selectedType().isEmpty() && !ui.name->text().trimmed().isEmpty()) {
72 q->accept();
73 }
74}
75
76void AddTransportDialogNGPrivate::updateOkButton()
77{
78 // Make sure a type is selected before allowing the user to continue.
79 okButton->setEnabled(!selectedType().isEmpty() && !ui.name->text().trimmed().isEmpty());
80}
81
83 : QDialog(parent)
84 , d(new AddTransportDialogNGPrivate(this))
85{
86 // Setup UI.
87 {
88 auto mainLayout = new QVBoxLayout(this);
89 auto widget = new QWidget(this);
90 d->ui.setupUi(widget);
91 mainLayout->addWidget(widget);
92 setWindowTitle(i18nc("@title:window", "Create Outgoing Account"));
94 d->okButton = buttonBox->button(QDialogButtonBox::Ok);
95 d->okButton->setText(i18nc("create and configure a mail transport", "Create and Configure"));
96 d->okButton->setEnabled(false);
97 d->okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
98 mainLayout->addWidget(buttonBox);
99 connect(buttonBox, &QDialogButtonBox::accepted, this, &AddTransportDialogNG::accept);
101 }
102
103 // Populate type list.
105 for (const TransportType &type : transportTypes) {
106 auto treeItem = new QTreeWidgetItem(d->ui.typeListView);
107 treeItem->setText(0, type.name());
108 treeItem->setText(1, type.description());
109 treeItem->setToolTip(1, type.description());
110 treeItem->setData(0, Qt::UserRole, type.identifier()); // the transport type
111 if (type.identifier() == QLatin1StringView("SMTP")) {
112 treeItem->setSelected(true); // select SMTP by default
113 }
114 }
115 d->ui.typeListView->resizeColumnToContents(0);
116
117 // if we only have one type, don't bother the user with this
118 if (d->ui.typeListView->invisibleRootItem()->childCount() == 1) {
119 d->ui.descLabel->hide();
120 d->ui.typeListView->hide();
121 }
122
124 d->ui.typeListView->setFocus();
125
126 // Connect user input.
127 connect(d->ui.typeListView, &QTreeWidget::itemClicked, this, [this]() {
128 d->updateOkButton();
129 });
130 connect(d->ui.typeListView, &QTreeWidget::itemSelectionChanged, this, [this]() {
131 d->updateOkButton();
132 });
133 connect(d->ui.typeListView, &QTreeWidget::itemDoubleClicked, this, [this]() {
134 d->doubleClicked();
135 });
136 connect(d->ui.name, &QLineEdit::textChanged, this, [this]() {
137 d->updateOkButton();
138 });
139 d->readConfig();
140}
141
143{
144 d->writeConfig();
145}
146
147void AddTransportDialogNG::accept()
148{
149 if (d->selectedType().isEmpty()) {
150 return;
151 }
152
153 // Create a new transport and configure it.
155 transport->setName(d->ui.name->text().trimmed());
156 const QString identifier = d->selectedType();
157 transport->setIdentifier(identifier);
158 transport->forceUniqueName();
159 TransportManager::self()->initializeTransport(identifier, transport);
160 if (TransportManager::self()->configureTransport(identifier, transport, this)) {
161 // The user clicked OK and the transport settings were saved.
163 if (d->ui.setDefault->isChecked()) {
165 }
167 }
168}
169
170#include "moc_addtransportdialogng.cpp"
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
AddTransportDialogNG(QWidget *parent=nullptr)
Creates a new AddTransportDialogNG.
~AddTransportDialogNG() override
Destroys the AddTransportDialogNG.
TransportType::List types() const
Returns a list of all available transport types.
Q_SCRIPTABLE void setDefaultTransport(int id)
Sets the default transport.
void addTransport(Transport *transport)
Adds the given transport.
static TransportManager * self()
Returns the TransportManager instance.
Transport * createTransport() const
Creates a new, empty Transport object.
A representation of a transport type.
Represents the settings of a specific mail transport.
Definition transport.h:33
void forceUniqueName()
Makes sure the transport has a unique name.
Definition transport.cpp:66
QString i18nc(const char *context, const char *text, const TYPE &arg...)
virtual void accept()
virtual void reject()
void textChanged(const QString &text)
bool empty() const const
T & first()
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T qobject_cast(QObject *object)
bool isValid() const const
UserRole
Key_Return
void itemClicked(QTreeWidgetItem *item, int column)
void itemDoubleClicked(QTreeWidgetItem *item, int column)
void itemSelectionChanged()
QWidget(QWidget *parent, Qt::WindowFlags f)
void setEnabled(bool)
void updateGeometry()
void setWindowTitle(const QString &)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:12:37 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.