File indexing completed on 2024-09-22 04:47:57
0001 /* 0002 SPDX-FileCopyrightText: 2009 Constantin Berzan <exit3219@gmail.com> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "addtransportdialogng.h" 0008 #include "transport.h" 0009 #include "transportmanager.h" 0010 #include "transporttype.h" 0011 #include "ui_addtransportdialog.h" 0012 0013 #include <QDialogButtonBox> 0014 0015 #include <QPushButton> 0016 0017 using namespace MailTransport; 0018 0019 /** 0020 @internal 0021 */ 0022 class MailTransport::AddTransportDialogNGPrivate 0023 { 0024 public: 0025 explicit AddTransportDialogNGPrivate(AddTransportDialogNG *qq) 0026 : q(qq) 0027 { 0028 } 0029 0030 [[nodiscard]] QString selectedType() const; 0031 0032 /** 0033 Enables the OK button if a type is selected. 0034 */ 0035 void updateOkButton(); // slot 0036 void doubleClicked(); // slot 0037 void writeConfig(); 0038 void readConfig(); 0039 0040 AddTransportDialogNG *const q; 0041 QPushButton *okButton = nullptr; 0042 ::Ui::AddTransportDialog ui; 0043 }; 0044 0045 void AddTransportDialogNGPrivate::writeConfig() 0046 { 0047 KConfigGroup group(KSharedConfig::openStateConfig(), QStringLiteral("AddTransportDialog")); 0048 group.writeEntry("Size", q->size()); 0049 } 0050 0051 void AddTransportDialogNGPrivate::readConfig() 0052 { 0053 KConfigGroup group(KSharedConfig::openStateConfig(), QStringLiteral("AddTransportDialog")); 0054 const QSize sizeDialog = group.readEntry("Size", QSize(300, TransportManager::self()->types().size() > 1 ? 300 : 160)); 0055 if (sizeDialog.isValid()) { 0056 q->resize(sizeDialog); 0057 } 0058 } 0059 0060 QString AddTransportDialogNGPrivate::selectedType() const 0061 { 0062 const QList<QTreeWidgetItem *> sel = ui.typeListView->selectedItems(); 0063 if (!sel.empty()) { 0064 return sel.first()->data(0, Qt::UserRole).toString(); 0065 } 0066 return {}; 0067 } 0068 0069 void AddTransportDialogNGPrivate::doubleClicked() 0070 { 0071 if (!selectedType().isEmpty() && !ui.name->text().trimmed().isEmpty()) { 0072 q->accept(); 0073 } 0074 } 0075 0076 void AddTransportDialogNGPrivate::updateOkButton() 0077 { 0078 // Make sure a type is selected before allowing the user to continue. 0079 okButton->setEnabled(!selectedType().isEmpty() && !ui.name->text().trimmed().isEmpty()); 0080 } 0081 0082 AddTransportDialogNG::AddTransportDialogNG(QWidget *parent) 0083 : QDialog(parent) 0084 , d(new AddTransportDialogNGPrivate(this)) 0085 { 0086 // Setup UI. 0087 { 0088 auto mainLayout = new QVBoxLayout(this); 0089 auto widget = new QWidget(this); 0090 d->ui.setupUi(widget); 0091 mainLayout->addWidget(widget); 0092 setWindowTitle(i18nc("@title:window", "Create Outgoing Account")); 0093 auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this); 0094 d->okButton = buttonBox->button(QDialogButtonBox::Ok); 0095 d->okButton->setText(i18nc("create and configure a mail transport", "Create and Configure")); 0096 d->okButton->setEnabled(false); 0097 d->okButton->setShortcut(Qt::CTRL | Qt::Key_Return); 0098 mainLayout->addWidget(buttonBox); 0099 connect(buttonBox, &QDialogButtonBox::accepted, this, &AddTransportDialogNG::accept); 0100 connect(buttonBox, &QDialogButtonBox::rejected, this, &AddTransportDialogNG::reject); 0101 } 0102 0103 // Populate type list. 0104 const auto transportTypes = TransportManager::self()->types(); 0105 for (const TransportType &type : transportTypes) { 0106 auto treeItem = new QTreeWidgetItem(d->ui.typeListView); 0107 treeItem->setText(0, type.name()); 0108 treeItem->setText(1, type.description()); 0109 treeItem->setToolTip(1, type.description()); 0110 treeItem->setData(0, Qt::UserRole, type.identifier()); // the transport type 0111 if (type.identifier() == QLatin1StringView("SMTP")) { 0112 treeItem->setSelected(true); // select SMTP by default 0113 } 0114 } 0115 d->ui.typeListView->resizeColumnToContents(0); 0116 0117 // if we only have one type, don't bother the user with this 0118 if (d->ui.typeListView->invisibleRootItem()->childCount() == 1) { 0119 d->ui.descLabel->hide(); 0120 d->ui.typeListView->hide(); 0121 } 0122 0123 updateGeometry(); 0124 d->ui.typeListView->setFocus(); 0125 0126 // Connect user input. 0127 connect(d->ui.typeListView, &QTreeWidget::itemClicked, this, [this]() { 0128 d->updateOkButton(); 0129 }); 0130 connect(d->ui.typeListView, &QTreeWidget::itemSelectionChanged, this, [this]() { 0131 d->updateOkButton(); 0132 }); 0133 connect(d->ui.typeListView, &QTreeWidget::itemDoubleClicked, this, [this]() { 0134 d->doubleClicked(); 0135 }); 0136 connect(d->ui.name, &QLineEdit::textChanged, this, [this]() { 0137 d->updateOkButton(); 0138 }); 0139 d->readConfig(); 0140 } 0141 0142 AddTransportDialogNG::~AddTransportDialogNG() 0143 { 0144 d->writeConfig(); 0145 } 0146 0147 void AddTransportDialogNG::accept() 0148 { 0149 if (d->selectedType().isEmpty()) { 0150 return; 0151 } 0152 0153 // Create a new transport and configure it. 0154 Transport *transport = TransportManager::self()->createTransport(); 0155 transport->setName(d->ui.name->text().trimmed()); 0156 const QString identifier = d->selectedType(); 0157 transport->setIdentifier(identifier); 0158 transport->forceUniqueName(); 0159 TransportManager::self()->initializeTransport(identifier, transport); 0160 if (TransportManager::self()->configureTransport(identifier, transport, this)) { 0161 // The user clicked OK and the transport settings were saved. 0162 TransportManager::self()->addTransport(transport); 0163 if (d->ui.setDefault->isChecked()) { 0164 TransportManager::self()->setDefaultTransport(transport->id()); 0165 } 0166 QDialog::accept(); 0167 } 0168 } 0169 0170 #include "moc_addtransportdialogng.cpp"