Commit 77bfda11 authored by Laurent Montel's avatar Laurent Montel 😁

Adapt to plugin support

parent b010a5f2
......@@ -21,11 +21,9 @@ set(mailtransport_lib_srcs
set(mailtransport_widgets_srcs
widgets/transportconfigwidget.cpp
widgets/addtransportdialog.cpp
widgets/transportcombobox.cpp
widgets/transportlistview.cpp
widgets/transportmanagementwidget.cpp
widgets/transportconfigdialog.cpp
widgets/addtransportdialogng.cpp
)
......
......@@ -17,16 +17,7 @@
<whatsthis>The name that will be used when referring to this server.</whatsthis>
<default code="true">i18n("Unnamed")</default>
</entry>
<entry name="type" type="Enum">
<choices>
<choice name="SMTP">
<label>SMTP Server</label>
</choice>
<choice name="Akonadi">
<label>Akonadi Resource</label>
</choice>
</choices>
<label>Transport type</label>
<entry name="identifier" type="String">
<default>SMTP</default>
</entry>
<entry name="host" type="String">
......
......@@ -161,3 +161,15 @@ QVector<MailTransport::TransportAbstractPlugin *> TransportPluginManager::plugin
{
return d->pluginsList();
}
MailTransport::TransportAbstractPlugin *TransportPluginManager::plugin(const QString &identifier)
{
for (MailTransport::TransportAbstractPlugin *p : pluginsList()) {
for (const MailTransport::TransportAbstractPluginInfo &info : p->names()) {
if (info.identifier == identifier) {
return p;
}
}
}
return {};
}
......@@ -33,7 +33,10 @@ public:
~TransportPluginManager();
static TransportPluginManager *self();
MailTransport::TransportAbstractPlugin *plugin(const QString &identifier);
QVector<MailTransport::TransportAbstractPlugin *> pluginsList() const;
private:
TransportPluginManagerPrivate *const d;
};
......
......@@ -19,7 +19,6 @@
#include "transportmgr.h"
#include <widgets/transportconfigdialog.h>
#include <transportmanager.h>
#include <widgets/transportmanagementwidget.h>
#include <transportjob.h>
......@@ -93,7 +92,8 @@ void TransportMgr::editBtnClicked()
if (index < 0) {
return;
}
TransportManager::self()->configureTransport(TransportManager::self()->transportById(index), this);
const auto transport = TransportManager::self()->transportById(index);
TransportManager::self()->configureTransport(transport->identifier(), transport, this);
}
void TransportMgr::sendBtnClicked()
......
......@@ -159,8 +159,8 @@ void Transport::usrRead()
// Set TransportType.
{
d->transportType = TransportType();
d->transportType.d->mType = type();
qCDebug(MAILTRANSPORT_LOG) << "type" << type();
d->transportType.d->mIdentifier = identifier();
qCDebug(MAILTRANSPORT_LOG) << "type" << identifier();
// Now we have the type and possibly agentType. Get the name, description
// etc. from TransportManager.
const TransportType::List &types = TransportManager::self()->types();
......@@ -339,5 +339,5 @@ void Transport::setTransportType(const TransportType &type)
{
Q_ASSERT(type.isValid());
d->transportType = type;
setType(type.type());
setIdentifier(type.identifier());
}
......@@ -25,9 +25,9 @@
#include "transportjob.h"
#include "transporttype.h"
#include "transporttype_p.h"
#include "widgets/addtransportdialog.h"
#include "widgets/transportconfigdialog.h"
#include "widgets/transportconfigwidget.h"
#include "plugins/transportpluginmanager.h"
#include "plugins/transportabstractplugin.h"
#include "widgets/addtransportdialogng.h"
#include "smtp/smtpconfigwidget.h"
#include <QApplication>
......@@ -272,7 +272,7 @@ bool TransportManager::showTransportCreationDialog(QWidget *parent, ShowConditio
}
}
QPointer<AddTransportDialog> dialog = new AddTransportDialog(parent);
QPointer<AddTransportDialogNG> dialog = new AddTransportDialogNG(parent);
const bool accepted = (dialog->exec() == QDialog::Accepted);
delete dialog;
return accepted;
......@@ -280,20 +280,13 @@ bool TransportManager::showTransportCreationDialog(QWidget *parent, ShowConditio
bool TransportManager::configureTransport(const QString &identifier, Transport *transport, QWidget *parent)
{
//FIXME
TransportAbstractPlugin *plugin = TransportPluginManager::self()->plugin(identifier);
if (plugin) {
return plugin->configureTransport(identifier, transport, parent);
}
return false;
}
bool TransportManager::configureTransport(Transport *transport, QWidget *parent)
{
QPointer<TransportConfigDialog> transportConfigDialog
= new TransportConfigDialog(transport, parent);
transportConfigDialog->setWindowTitle(i18n("Configure account"));
bool okClicked = (transportConfigDialog->exec() == QDialog::Accepted);
delete transportConfigDialog;
return okClicked;
}
TransportJob *TransportManager::createTransportJob(int transportId)
{
Transport *t = transportById(transportId, false);
......@@ -302,9 +295,9 @@ TransportJob *TransportManager::createTransportJob(int transportId)
}
t = t->clone(); // Jobs delete their transports.
t->updatePasswordState();
switch (t->type()) {
case Transport::EnumType::SMTP:
return new SmtpJob(t, this);
TransportAbstractPlugin *plugin = TransportPluginManager::self()->plugin(t->identifier());
if (plugin) {
return plugin->createTransportJob(t->identifier());
}
Q_ASSERT(false);
return nullptr;
......@@ -470,13 +463,13 @@ void TransportManagerPrivate::fillTypes()
{
Q_ASSERT(types.isEmpty());
// SMTP.
{
TransportType type;
type.d->mType = Transport::EnumType::SMTP;
type.d->mName = i18nc("@option SMTP transport", "SMTP");
type.d->mDescription = i18n("An SMTP server on the Internet");
types << type;
for (MailTransport::TransportAbstractPlugin * plugin : MailTransport::TransportPluginManager::self()->pluginsList()) {
for (const MailTransport::TransportAbstractPluginInfo &info : plugin->names()) {
TransportType type;
type.d->mName = info.name;
type.d->mDescription = info.description;
types << type;
}
}
}
......@@ -702,3 +695,5 @@ void TransportManagerPrivate::jobResult(KJob *job)
}
#include "moc_transportmanager.cpp"
#include <MailTransport/TransportAbstractPlugin>
......@@ -185,8 +185,6 @@ public:
@return True if the user clicked Ok, false if the user cancelled.
@since 4.4
*/
bool configureTransport(Transport *transport, QWidget *parent);
bool configureTransport(const QString &identifier, Transport *transport, QWidget *parent);
public Q_SLOTS:
......
......@@ -47,17 +47,12 @@ TransportType &TransportType::operator=(const TransportType &other)
bool TransportType::operator==(const TransportType &other) const
{
return d->mType == other.d->mType;
return d->mIdentifier == other.d->mIdentifier;
}
bool TransportType::isValid() const
{
return d->mType >= 0;
}
TransportBase::EnumType::type TransportType::type() const
{
return static_cast<TransportBase::EnumType::type>(d->mType);
return !d->mIdentifier.isEmpty();
}
QString TransportType::name() const
......
......@@ -82,12 +82,6 @@ public:
*/
bool isValid() const;
/**
@internal
Returns the type of the transport.
*/
TransportBase::EnumType::type type() const;
/**
Returns the i18n'ed name of the transport type.
*/
......
......@@ -32,19 +32,16 @@ class TransportType::Private : public QSharedData
public:
Private()
{
mType = -1;
}
Private(const Private &other)
: QSharedData(other)
{
mType = other.mType;
mName = other.mName;
mDescription = other.mDescription;
mIdentifier = other.mIdentifier;
}
int mType;
QString mName;
QString mDescription;
QString mIdentifier;
......
/*
Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "addtransportdialog.h"
#include "transport.h"
#include "transportconfigwidget.h"
#include "transportmanager.h"
#include "transporttype.h"
#include "ui_addtransportdialog.h"
#include "mailtransport_debug.h"
#include <QDialogButtonBox>
#include <QPushButton>
using namespace MailTransport;
/**
@internal
*/
class AddTransportDialog::Private
{
public:
Private(AddTransportDialog *qq)
: q(qq)
, okButton(nullptr)
{
}
/**
Returns the currently selected type in the type selection widget, or
an invalid type if none is selected.
*/
TransportType selectedType() const;
/**
Enables the OK button if a type is selected.
*/
void updateOkButton(); // slot
void doubleClicked(); //slot
void writeConfig();
void readConfig();
AddTransportDialog *const q;
QPushButton *okButton;
::Ui::AddTransportDialog ui;
};
void AddTransportDialog::Private::writeConfig()
{
KConfigGroup group(KSharedConfig::openConfig(), "AddTransportDialog");
group.writeEntry("Size", q->size());
}
void AddTransportDialog::Private::readConfig()
{
KConfigGroup group(KSharedConfig::openConfig(), "AddTransportDialog");
const QSize sizeDialog = group.readEntry("Size", QSize(300, TransportManager::self()->types().size() > 1 ? 200 : 80));
if (sizeDialog.isValid()) {
q->resize(sizeDialog);
}
}
TransportType AddTransportDialog::Private::selectedType() const
{
QList<QTreeWidgetItem *> sel = ui.typeListView->selectedItems();
if (!sel.empty()) {
return sel.first()->data(0, Qt::UserRole).value<TransportType>();
}
return TransportType();
}
void AddTransportDialog::Private::doubleClicked()
{
if (selectedType().isValid() && !ui.name->text().trimmed().isEmpty()) {
q->accept();
}
}
void AddTransportDialog::Private::updateOkButton()
{
// Make sure a type is selected before allowing the user to continue.
okButton->setEnabled(selectedType().isValid() && !ui.name->text().trimmed().isEmpty());
}
AddTransportDialog::AddTransportDialog(QWidget *parent)
: QDialog(parent)
, d(new Private(this))
{
// Setup UI.
{
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QWidget *widget = new QWidget(this);
d->ui.setupUi(widget);
mainLayout->addWidget(widget);
setWindowTitle(i18n("Create Outgoing Account"));
QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
d->okButton = buttonBox->button(QDialogButtonBox::Ok);
d->okButton->setText(i18nc("create and configure a mail transport", "Create and Configure"));
d->okButton->setEnabled(false);
d->okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
mainLayout->addWidget(buttonBox);
connect(buttonBox, &QDialogButtonBox::accepted, this, &AddTransportDialog::accept);
connect(buttonBox, &QDialogButtonBox::rejected, this, &AddTransportDialog::reject);
}
// Populate type list.
const auto transportTypes = TransportManager::self()->types();
for (const TransportType &type : transportTypes) {
QTreeWidgetItem *treeItem = new QTreeWidgetItem(d->ui.typeListView);
treeItem->setText(0, type.name());
treeItem->setText(1, type.description());
treeItem->setData(0, Qt::UserRole, QVariant::fromValue(type)); // the transport type
if (type.type() == TransportBase::EnumType::SMTP) {
treeItem->setSelected(true); // select SMTP by default
}
}
d->ui.typeListView->resizeColumnToContents(0);
// if we only have one type, don't bother the user with this
if (d->ui.typeListView->invisibleRootItem()->childCount() == 1) {
d->ui.descLabel->hide();
d->ui.typeListView->hide();
}
updateGeometry();
d->ui.typeListView->setFocus();
// Connect user input.
connect(d->ui.typeListView, SIGNAL(itemClicked(QTreeWidgetItem *,int)),
this, SLOT(updateOkButton()));
connect(d->ui.typeListView, SIGNAL(itemSelectionChanged()),
this, SLOT(updateOkButton()));
connect(d->ui.typeListView, SIGNAL(itemDoubleClicked(QTreeWidgetItem *,int)),
this, SLOT(doubleClicked()));
connect(d->ui.name, SIGNAL(textChanged(QString)),
this, SLOT(updateOkButton()));
d->readConfig();
}
AddTransportDialog::~AddTransportDialog()
{
d->writeConfig();
delete d;
}
void AddTransportDialog::accept()
{
if (!d->selectedType().isValid()) {
return;
}
// Create a new transport and configure it.
Transport *transport = TransportManager::self()->createTransport();
transport->setTransportType(d->selectedType());
transport->setName(d->ui.name->text().trimmed());
transport->forceUniqueName();
if (TransportManager::self()->configureTransport(transport, this)) {
// The user clicked OK and the transport settings were saved.
TransportManager::self()->addTransport(transport);
if (d->ui.setDefault->isChecked()) {
TransportManager::self()->setDefaultTransport(transport->id());
}
QDialog::accept();
}
}
#include "moc_addtransportdialog.cpp"
/*
Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef MAILTRANSPORT_ADDTRANSPORTDIALOG_H
#define MAILTRANSPORT_ADDTRANSPORTDIALOG_H
#include <QDialog>
namespace MailTransport {
/**
@internal
A dialog for creating a new transport. It asks the user for the transport
type and name, and then proceeds to configure the new transport.
To create a new transport from applications, use
TransportManager::showNewTransportDialog().
@author Constantin Berzan <exit3219@gmail.com>
@since 4.4
*/
class AddTransportDialog : public QDialog
{
Q_OBJECT
public:
/**
Creates a new AddTransportDialog.
*/
explicit AddTransportDialog(QWidget *parent = nullptr);
/**
Destroys the AddTransportDialog.
*/
virtual ~AddTransportDialog();
/* reimpl */
void accept() override;
private:
class Private;
Private *const d;
Q_PRIVATE_SLOT(d, void updateOkButton())
Q_PRIVATE_SLOT(d, void doubleClicked())
};
} // namespace MailTransport
#endif // MAILTRANSPORT_ADDTRANSPORTDIALOG_H
......@@ -62,10 +62,9 @@ void TransportComboBox::setCurrentTransport(int transportId)
}
}
TransportBase::EnumType::type TransportComboBox::transportType() const
QString TransportComboBox::transportType() const
{
int transtype = TransportManager::self()->transportById(currentTransportId())->type();
return static_cast<TransportBase::EnumType::type>(transtype);
return TransportManager::self()->transportById(currentTransportId())->identifier();
}
void TransportComboBox::updateComboboxList()
......
......@@ -59,7 +59,7 @@ public:
/**
Returns the type of the selected transport.
*/
TransportBase::EnumType::type transportType() const;
QString transportType() const;
protected:
void setTransportList(const QVector<int> &transportList);
......
/*
Copyright (c) 2006 - 2007 Volker Krause <vkrause@kde.org>
Copyright (c) 2007 KovoKs <kovoks@kovoks.nl>
Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com>
Based on KMail code by:
Copyright (c) 2001-2002 Michael Haeckel <haeckel@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "transportconfigdialog.h"
#include "transport.h"
#include "transportconfigwidget.h"
#include "transportmanager.h"
#include "transporttype.h"
#include "smtp/smtpconfigwidget.h"
#include <QDialogButtonBox>
#include <QLabel>
#include <QString>
#include <QVBoxLayout>
#include <QPushButton>
#include "mailtransport_debug.h"
#include <KLocalizedString>
using namespace MailTransport;
class MailTransport::TransportConfigDialog::Private
{
public:
Private(TransportConfigDialog *qq)
: transport(nullptr)
, configWidget(nullptr)
, q(qq)
, okButton(nullptr)
{
}
Transport *transport;
QWidget *configWidget;
TransportConfigDialog *q;
QPushButton *okButton;
// slots
void okClicked();
void slotTextChanged(const QString &text);
void slotEnabledOkButton(bool);
};
void TransportConfigDialog::Private::slotEnabledOkButton(bool b)
{
okButton->setEnabled(b);
}
void TransportConfigDialog::Private::okClicked()
{
if (TransportConfigWidget *w = dynamic_cast<TransportConfigWidget *>(configWidget)) {
// It is not an Akonadi transport.
w->apply();
transport->save();
}
}
void TransportConfigDialog::Private::slotTextChanged(const QString &text)
{
okButton->setEnabled(!text.isEmpty());
}
TransportConfigDialog::TransportConfigDialog(Transport *transport, QWidget *parent)
: QDialog(parent)
, d(new Private(this))
{
Q_ASSERT(transport);
d->transport = transport;
QVBoxLayout *mainLayout = new QVBoxLayout(this);
bool pathIsEmpty = false;
switch (transport->type()) {
case Transport::EnumType::SMTP:
d->configWidget = new SMTPConfigWidget(transport, this);
break;
case Transport::EnumType::Akonadi:
qCWarning(MAILTRANSPORT_LOG) << "Tried to configure an Akonadi transport.";
d->configWidget = new QLabel(i18n("This outgoing account cannot be configured."), this);
break;
default:
Q_ASSERT(false);
d->configWidget = nullptr;
break;