File indexing completed on 2024-11-17 04:44:58

0001 /*
0002     SPDX-FileCopyrightText: 2015-2016 Krzysztof Nowicki <krissn@op.pl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ewsmtaconfigdialog.h"
0008 
0009 #include <QAbstractItemView>
0010 #include <QDialogButtonBox>
0011 #include <QPushButton>
0012 
0013 #include <KWindowSystem>
0014 
0015 #include <Akonadi/AgentFilterProxyModel>
0016 #include <Akonadi/AgentInstance>
0017 #include <Akonadi/AgentInstanceModel>
0018 #include <Akonadi/AgentInstanceWidget>
0019 
0020 #include "ewsmtaresource.h"
0021 #include "ewsmtasettings.h"
0022 #include "ewsres_mta_debug.h"
0023 #include "ui_ewsmtaconfigdialog.h"
0024 
0025 EwsMtaConfigDialog::EwsMtaConfigDialog(EwsMtaResource *parentResource, WId wId)
0026     : QDialog()
0027     , mButtonBox(new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this))
0028     , mParentResource(parentResource)
0029 {
0030     if (wId) {
0031         setAttribute(Qt::WA_NativeWindow, true);
0032         KWindowSystem::setMainWindow(windowHandle(), wId);
0033     }
0034 
0035     auto mainWidget = new QWidget(this);
0036     auto mainLayout = new QVBoxLayout(this);
0037     mainLayout->addWidget(mainWidget);
0038     QPushButton *okButton = mButtonBox->button(QDialogButtonBox::Ok);
0039     okButton->setDefault(true);
0040     okButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0041     connect(mButtonBox, &QDialogButtonBox::accepted, this, &EwsMtaConfigDialog::accept);
0042     connect(mButtonBox, &QDialogButtonBox::rejected, this, &EwsMtaConfigDialog::reject);
0043     mainLayout->addWidget(mButtonBox);
0044 
0045     setWindowTitle(i18nc("@title:window", "Microsoft Exchange Mail Transport Configuration"));
0046 
0047     mUi = new Ui::SetupServerView;
0048     mUi->setupUi(mainWidget);
0049     mUi->accountName->setText(parentResource->name());
0050 
0051     Akonadi::AgentFilterProxyModel *model = mUi->resourceWidget->agentFilterProxyModel();
0052     model->addCapabilityFilter(QStringLiteral("X-EwsMailTransport"));
0053     mUi->resourceWidget->view()->setSelectionMode(QAbstractItemView::SingleSelection);
0054 
0055     for (int i = 0, total = model->rowCount(); i < total; ++i) {
0056         QModelIndex index = model->index(i, 0);
0057         QVariant v = model->data(index, Akonadi::AgentInstanceModel::InstanceIdentifierRole);
0058         if (v.toString() == EwsMtaSettings::ewsResource()) {
0059             mUi->resourceWidget->view()->setCurrentIndex(index);
0060         }
0061     }
0062 
0063     connect(okButton, &QPushButton::clicked, this, &EwsMtaConfigDialog::save);
0064 }
0065 
0066 EwsMtaConfigDialog::~EwsMtaConfigDialog()
0067 {
0068     delete mUi;
0069 }
0070 
0071 void EwsMtaConfigDialog::save()
0072 {
0073     if (!mUi->resourceWidget->selectedAgentInstances().isEmpty()) {
0074         EwsMtaSettings::setEwsResource(mUi->resourceWidget->selectedAgentInstances().constFirst().identifier());
0075         mParentResource->setName(mUi->accountName->text());
0076         EwsMtaSettings::self()->save();
0077     } else {
0078         qCWarning(EWSRES_MTA_LOG) << "Any agent instance selected";
0079     }
0080 }
0081 
0082 #include "moc_ewsmtaconfigdialog.cpp"