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"
8using namespace Qt::Literals::StringLiterals;
9
10#include "transport.h"
11#include "transportmanager.h"
12#include "transporttype.h"
13#include "ui_addtransportdialog.h"
14
15#include <QDialogButtonBox>
16
17#include <QPushButton>
18
19using namespace MailTransport;
20
21/**
22 @internal
23*/
24class MailTransport::AddTransportDialogNGPrivate
25{
26public:
27 explicit AddTransportDialogNGPrivate(AddTransportDialogNG *qq)
28 : q(qq)
29 {
30 }
31
32 [[nodiscard]] QString selectedType() const;
33
34 /**
35 Enables the OK button if a type is selected.
36 */
37 void updateOkButton(); // slot
38 void doubleClicked(); // slot
39 void writeConfig();
40 void readConfig();
41
42 AddTransportDialogNG *const q;
43 QPushButton *okButton = nullptr;
44 ::Ui::AddTransportDialog ui;
45};
46
47void AddTransportDialogNGPrivate::writeConfig()
48{
49 KConfigGroup group(KSharedConfig::openStateConfig(), QStringLiteral("AddTransportDialog"));
50 group.writeEntry("Size", q->size());
51}
52
53void AddTransportDialogNGPrivate::readConfig()
54{
55 KConfigGroup group(KSharedConfig::openStateConfig(), QStringLiteral("AddTransportDialog"));
56 const QSize sizeDialog = group.readEntry("Size", QSize(300, TransportManager::self()->types().size() > 1 ? 300 : 160));
57 if (sizeDialog.isValid()) {
58 q->resize(sizeDialog);
59 }
60}
61
62QString AddTransportDialogNGPrivate::selectedType() const
63{
64 const QList<QTreeWidgetItem *> sel = ui.typeListView->selectedItems();
65 if (!sel.empty()) {
66 return sel.first()->data(0, Qt::UserRole).toString();
67 }
68 return {};
69}
70
71void AddTransportDialogNGPrivate::doubleClicked()
72{
73 if (!selectedType().isEmpty() && !ui.name->text().trimmed().isEmpty()) {
74 q->accept();
75 }
76}
77
78void AddTransportDialogNGPrivate::updateOkButton()
79{
80 // Make sure a type is selected before allowing the user to continue.
81 okButton->setEnabled(!selectedType().isEmpty() && !ui.name->text().trimmed().isEmpty());
82}
83
85 : QDialog(parent)
86 , d(new AddTransportDialogNGPrivate(this))
87{
88 // Setup UI.
89 {
90 auto mainLayout = new QVBoxLayout(this);
91 auto widget = new QWidget(this);
92 d->ui.setupUi(widget);
93 mainLayout->addWidget(widget);
94 setWindowTitle(i18nc("@title:window", "Create Outgoing Account"));
96 d->okButton = buttonBox->button(QDialogButtonBox::Ok);
97 d->okButton->setText(i18nc("create and configure a mail transport", "Create and Configure"));
98 d->okButton->setEnabled(false);
99 d->okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
100 mainLayout->addWidget(buttonBox);
101 connect(buttonBox, &QDialogButtonBox::accepted, this, &AddTransportDialogNG::accept);
103 }
104
105 // Populate type list.
106 const auto transportTypes = TransportManager::self()->types();
107 for (const TransportType &type : transportTypes) {
108 auto treeItem = new QTreeWidgetItem(d->ui.typeListView);
109 treeItem->setText(0, type.name());
110 treeItem->setText(1, type.description());
111 treeItem->setToolTip(1, type.description());
112 treeItem->setData(0, Qt::UserRole, type.identifier()); // the transport type
113 if (type.identifier() == "SMTP"_L1) {
114 treeItem->setSelected(true); // select SMTP by default
115 }
116 }
117 d->ui.typeListView->resizeColumnToContents(0);
118
119 // if we only have one type, don't bother the user with this
120 if (d->ui.typeListView->invisibleRootItem()->childCount() == 1) {
121 d->ui.descLabel->hide();
122 d->ui.typeListView->hide();
123 }
124
126 d->ui.typeListView->setFocus();
127
128 // Connect user input.
129 connect(d->ui.typeListView, &QTreeWidget::itemClicked, this, [this]() {
130 d->updateOkButton();
131 });
132 connect(d->ui.typeListView, &QTreeWidget::itemSelectionChanged, this, [this]() {
133 d->updateOkButton();
134 });
135 connect(d->ui.typeListView, &QTreeWidget::itemDoubleClicked, this, [this]() {
136 d->doubleClicked();
137 });
138 connect(d->ui.name, &QLineEdit::textChanged, this, [this]() {
139 d->updateOkButton();
140 });
141 d->readConfig();
142}
143
145{
146 d->writeConfig();
147}
148
149void AddTransportDialogNG::accept()
150{
151 if (d->selectedType().isEmpty()) {
152 return;
153 }
154
155 // Create a new transport and configure it.
157 transport->setName(d->ui.name->text().trimmed());
158 const QString identifier = d->selectedType();
159 transport->setIdentifier(identifier);
160 transport->forceUniqueName();
161 TransportManager::self()->initializeTransport(identifier, transport);
162 if (TransportManager::self()->configureTransport(identifier, transport, this)) {
163 // The user clicked OK and the transport settings were saved.
165 if (d->ui.setDefault->isChecked()) {
167 }
169 }
170}
171
172#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)
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 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.