MailTransport

addtransportdialogng.cpp
1 /*
2  SPDX-FileCopyrightText: 2009 Constantin Berzan <[email protected]>
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 
17 using namespace MailTransport;
18 
19 /**
20  @internal
21 */
22 class MailTransport::AddTransportDialogNGPrivate
23 {
24 public:
25  explicit AddTransportDialogNGPrivate(AddTransportDialogNG *qq)
26  : q(qq)
27  {
28  }
29 
30  Q_REQUIRED_RESULT 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 
45 void AddTransportDialogNGPrivate::writeConfig()
46 {
47  KConfigGroup group(KSharedConfig::openStateConfig(), "AddTransportDialog");
48  group.writeEntry("Size", q->size());
49 }
50 
51 void AddTransportDialogNGPrivate::readConfig()
52 {
53  KConfigGroup group(KSharedConfig::openStateConfig(), "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 
60 QString 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 
69 void AddTransportDialogNGPrivate::doubleClicked()
70 {
71  if (!selectedType().isEmpty() && !ui.name->text().trimmed().isEmpty()) {
72  q->accept();
73  }
74 }
75 
76 void 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.
104  const auto transportTypes = TransportManager::self()->types();
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() == QLatin1String("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 
123  updateGeometry();
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 
147 void 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.
162  TransportManager::self()->addTransport(transport);
163  if (d->ui.setDefault->isChecked()) {
164  TransportManager::self()->setDefaultTransport(transport->id());
165  }
166  QDialog::accept();
167  }
168 }
169 
170 #include "moc_addtransportdialogng.cpp"
Transport * createTransport() const
Creates a new, empty Transport object.
T & first()
UserRole
QWidget(QWidget *parent, Qt::WindowFlags f)
bool isValid() const const
virtual void reject()
void addTransport(Transport *transport)
Adds the given transport.
void readConfig()
Q_SCRIPTABLE void setDefaultTransport(int id)
Sets the default transport.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QStringList types(Mode mode=Writing)
bool empty() const const
A representation of a transport type.
Definition: transporttype.h:30
void textChanged(const QString &text)
void setWindowTitle(const QString &)
static TransportManager * self()
Returns the TransportManager instance.
static KSharedConfig::Ptr openStateConfig(const QString &fileName=QString())
virtual void accept()
void itemSelectionChanged()
Key_Return
void itemClicked(QTreeWidgetItem *item, int column)
Represents the settings of a specific mail transport.
Definition: transport.h:32
void updateGeometry()
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void itemDoubleClicked(QTreeWidgetItem *item, int column)
void forceUniqueName()
Makes sure the transport has a unique name.
Definition: transport.cpp:70
~AddTransportDialogNG() override
Destroys the AddTransportDialogNG.
TransportType::List types() const
Returns a list of all available transport types.
AddTransportDialogNG(QWidget *parent=nullptr)
Creates a new AddTransportDialogNG.
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 6 2023 03:57:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.