addtransportdialog.cpp 6.54 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
    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"

Laurent Montel's avatar
Laurent Montel committed
27
#include "mailtransport_debug.h"
Laurent Montel's avatar
Laurent Montel committed
28
#include <QDialogButtonBox>
29

Laurent Montel's avatar
Laurent Montel committed
30
#include <QPushButton>
31 32
#include <agentinstance.h>
#include <agentinstancecreatejob.h>
33 34 35 36 37 38 39 40

using namespace MailTransport;

/**
  @internal
*/
class AddTransportDialog::Private
{
Laurent Montel's avatar
Laurent Montel committed
41 42
public:
    Private(AddTransportDialog *qq)
Laurent Montel's avatar
Laurent Montel committed
43
        : q(qq), okButton(Q_NULLPTR)
44 45 46
    {
    }

47 48 49 50
    /**
      Returns the currently selected type in the type selection widget, or
      an invalid type if none is selected.
    */
51 52
    TransportType selectedType() const;

53 54 55
    /**
      Enables the OK button if a type is selected.
    */
56
    void updateOkButton(); // slot
57
    void doubleClicked(); //slot
Laurent Montel's avatar
Laurent Montel committed
58 59
    void writeConfig();
    void readConfig();
60 61

    AddTransportDialog *const q;
Laurent Montel's avatar
Laurent Montel committed
62
    QPushButton *okButton;
Luboš Luňák's avatar
Luboš Luňák committed
63
    ::Ui::AddTransportDialog ui;
64 65
};

Laurent Montel's avatar
Laurent Montel committed
66 67
void AddTransportDialog::Private::writeConfig()
{
Laurent Montel's avatar
Laurent Montel committed
68 69
    KConfigGroup group(KSharedConfig::openConfig(), "AddTransportDialog");
    group.writeEntry("Size", q->size());
Laurent Montel's avatar
Laurent Montel committed
70 71 72 73
}

void AddTransportDialog::Private::readConfig()
{
Laurent Montel's avatar
Laurent Montel committed
74
    KConfigGroup group(KSharedConfig::openConfig(), "AddTransportDialog");
75
    const QSize sizeDialog = group.readEntry("Size", QSize(300, TransportManager::self()->types().size() > 1 ? 200 : 80));
Laurent Montel's avatar
Laurent Montel committed
76 77 78
    if (sizeDialog.isValid()) {
        q->resize(sizeDialog);
    }
Laurent Montel's avatar
Laurent Montel committed
79 80
}

81 82
TransportType AddTransportDialog::Private::selectedType() const
{
Laurent Montel's avatar
Laurent Montel committed
83 84 85 86 87
    QList<QTreeWidgetItem *> sel = ui.typeListView->selectedItems();
    if (!sel.empty()) {
        return sel.first()->data(0, Qt::UserRole).value<TransportType>();
    }
    return TransportType();
88 89
}

90 91
void AddTransportDialog::Private::doubleClicked()
{
Laurent Montel's avatar
Laurent Montel committed
92 93 94
    if (selectedType().isValid() && !ui.name->text().trimmed().isEmpty()) {
        q->accept();
    }
95 96
}

97
void AddTransportDialog::Private::updateOkButton()
98
{
Laurent Montel's avatar
Laurent Montel committed
99 100
    // Make sure a type is selected before allowing the user to continue.
    okButton->setEnabled(selectedType().isValid() && !ui.name->text().trimmed().isEmpty());
101 102
}

Laurent Montel's avatar
Laurent Montel committed
103 104
AddTransportDialog::AddTransportDialog(QWidget *parent)
    : QDialog(parent), d(new Private(this))
105
{
Laurent Montel's avatar
Laurent Montel committed
106 107
    // Setup UI.
    {
Laurent Montel's avatar
Laurent Montel committed
108
        QVBoxLayout *mainLayout = new QVBoxLayout(this);
Laurent Montel's avatar
Laurent Montel committed
109 110 111 112
        QWidget *widget = new QWidget(this);
        d->ui.setupUi(widget);
        mainLayout->addWidget(widget);
        setWindowTitle(i18n("Create Outgoing Account"));
Laurent Montel's avatar
Laurent Montel committed
113
        QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
Laurent Montel's avatar
Laurent Montel committed
114 115 116 117 118
        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);
Laurent Montel's avatar
Laurent Montel committed
119 120
        connect(buttonBox, &QDialogButtonBox::accepted, this, &AddTransportDialog::accept);
        connect(buttonBox, &QDialogButtonBox::rejected, this, &AddTransportDialog::reject);
Laurent Montel's avatar
Laurent Montel committed
121 122 123
    }

    // Populate type list.
124 125
    const auto transportTypes = TransportManager::self()->types();
    foreach (const TransportType &type, transportTypes) {
Laurent Montel's avatar
Laurent Montel committed
126 127 128 129
        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
130 131
        if (type.type() == TransportBase::EnumType::SMTP)
            treeItem->setSelected(true); // select SMTP by default
Laurent Montel's avatar
Laurent Montel committed
132 133
    }
    d->ui.typeListView->resizeColumnToContents(0);
134 135 136 137 138 139 140

    // 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();
    }

Laurent Montel's avatar
Laurent Montel committed
141 142 143 144
    updateGeometry();
    d->ui.typeListView->setFocus();

    // Connect user input.
Laurent Montel's avatar
Laurent Montel committed
145
    connect(d->ui.typeListView, SIGNAL(itemClicked(QTreeWidgetItem*,int)),
Laurent Montel's avatar
Laurent Montel committed
146 147 148
            this, SLOT(updateOkButton()));
    connect(d->ui.typeListView, SIGNAL(itemSelectionChanged()),
            this, SLOT(updateOkButton()));
Laurent Montel's avatar
Laurent Montel committed
149
    connect(d->ui.typeListView, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
Laurent Montel's avatar
Laurent Montel committed
150 151 152 153
            this, SLOT(doubleClicked()));
    connect(d->ui.name, SIGNAL(textChanged(QString)),
            this, SLOT(updateOkButton()));
    d->readConfig();
154 155 156 157
}

AddTransportDialog::~AddTransportDialog()
{
Laurent Montel's avatar
Laurent Montel committed
158 159
    d->writeConfig();
    delete d;
160 161 162 163
}

void AddTransportDialog::accept()
{
Laurent Montel's avatar
Laurent Montel committed
164 165
    if (!d->selectedType().isValid()) {
        return;
166
    }
Laurent Montel's avatar
Laurent Montel committed
167 168 169 170 171 172 173 174 175

    // Create a new transport and configure it.
    Transport *transport = TransportManager::self()->createTransport();
    transport->setTransportType(d->selectedType());
    if (d->selectedType().type() == Transport::EnumType::Akonadi) {
        // Create a resource instance if Akonadi-type transport.
        using namespace Akonadi;
        AgentInstanceCreateJob *cjob = new AgentInstanceCreateJob(d->selectedType().agentType());
        if (!cjob->exec()) {
Laurent Montel's avatar
Laurent Montel committed
176
            qCWarning(MAILTRANSPORT_LOG) << "Failed to create agent instance of type"
Laurent Montel's avatar
Laurent Montel committed
177
                                         << d->selectedType().agentType().identifier();
Laurent Montel's avatar
Laurent Montel committed
178 179 180
            return;
        }
        transport->setHost(cjob->instance().identifier());
181
    }
Laurent Montel's avatar
Laurent Montel committed
182 183 184 185 186 187 188 189 190 191
    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();
    }
192 193
}

194
#include "moc_addtransportdialog.cpp"