File indexing completed on 2022-12-06 18:50:09

0001 /*
0002     SPDX-License-Identifier: GPL-2.0-or-later
0003     SPDX-FileCopyrightText: 2009 Alexander Rieder <alexanderrieder@gmail.com>
0004     SPDX-FileCopyrightText: 2019-2022 Alexander Semke <alexander.semke@web.de>
0005 */
0006 
0007 #include "backendchoosedialog.h"
0008 #include "lib/backend.h"
0009 #include "settings.h"
0010 
0011 #include <KIconLoader>
0012 #include <KLocalizedString>
0013 #include <KSharedConfig>
0014 #include <KWindowConfig>
0015 
0016 #include <QDesktopServices>
0017 #include <QIcon>
0018 #include <QPushButton>
0019 #include <QWindow>
0020 
0021 BackendChooseDialog::BackendChooseDialog(QWidget* parent) : QDialog(parent)
0022 {
0023     QWidget* w = new QWidget(this);
0024     m_ui.setupUi(w);
0025 
0026     QGridLayout* layout = new QGridLayout;
0027     setLayout(layout);
0028     layout->addWidget(w);
0029 
0030     m_ui.backendList->setIconSize(QSize(KIconLoader::SizeMedium, KIconLoader::SizeMedium));
0031     m_ui.backendList->setSortingEnabled(true);
0032     connect(m_ui.backendList, &QListWidget::currentItemChanged, this, &BackendChooseDialog::updateContent);
0033     connect(m_ui.backendList, &QListWidget::itemDoubleClicked, this, [=]() {
0034         if (m_ui.buttonBox->button(QDialogButtonBox::Ok)->isEnabled())
0035             accept();
0036     });
0037 
0038     m_ui.buttonBox->button(QDialogButtonBox::Ok);
0039     m_ui.buttonBox->button(QDialogButtonBox::Ok)->setIcon(QApplication::style()->standardIcon(QStyle::SP_DialogOkButton));
0040     m_ui.buttonBox->button(QDialogButtonBox::Cancel)->setIcon(QApplication::style()->standardIcon(QStyle::SP_DialogCancelButton));
0041 
0042     for (auto* backend : Cantor::Backend::availableBackends())
0043     {
0044         qDebug() << backend->name() << backend->isEnabled() << backend->requirementsFullfilled();
0045         if(!backend->isEnabled())
0046             if (backend->requirementsFullfilled())
0047                 continue;
0048 
0049         QListWidgetItem* item = new QListWidgetItem(m_ui.backendList);
0050         item->setText(backend->name());
0051         item->setIcon(QIcon::fromTheme(backend->icon()));
0052         m_ui.backendList->addItem(item);
0053         if(m_ui.backendList->currentItem() == nullptr)
0054             m_ui.backendList->setCurrentItem(item);
0055 
0056         if(backend->name()==Settings::self()->defaultBackend())
0057             m_ui.backendList->setCurrentItem(item);
0058     }
0059 
0060     int height = m_ui.backendList->iconSize().height() * m_ui.backendList->count();
0061     m_ui.backendList->setMinimumSize(0, height);
0062 
0063 
0064     setWindowTitle(i18n("Select the Backend"));
0065     setWindowIcon(QIcon::fromTheme(QLatin1String("run-build")));
0066 
0067     connect(m_ui.buttonBox, &QDialogButtonBox::accepted, this, &BackendChooseDialog::accept);
0068     connect(m_ui.buttonBox, &QDialogButtonBox::rejected, this, &BackendChooseDialog::close);
0069     connect(this, &BackendChooseDialog::accepted, this, &BackendChooseDialog::onAccept);
0070 
0071     // open URLs in the external browser
0072     m_ui.descriptionView->setOpenLinks(false);
0073     connect(m_ui.descriptionView, &QTextBrowser::anchorClicked, this, [=](const QUrl &link)
0074     {
0075         QDesktopServices::openUrl(QUrl(link));
0076     });
0077 
0078     //restore saved settings if available
0079     create(); // ensure there's a window created
0080     KConfigGroup conf(KSharedConfig::openConfig(), "BackendChooseDialog");
0081     if (conf.exists()) {
0082         KWindowConfig::restoreWindowSize(windowHandle(), conf);
0083         resize(windowHandle()->size()); // workaround for QTBUG-40584
0084     } else
0085         resize(QSize(500, 200).expandedTo(minimumSize()));
0086 }
0087 
0088 BackendChooseDialog::~BackendChooseDialog() {
0089     KConfigGroup conf(KSharedConfig::openConfig(), "BackendChooseDialog");
0090     KWindowConfig::saveWindowSize(windowHandle(), conf);
0091 }
0092 
0093 void BackendChooseDialog::onAccept()
0094 {
0095     m_backend = m_ui.backendList->currentItem()->text();
0096     if(m_ui.makeDefault->isChecked())
0097     {
0098         Settings::self()->setDefaultBackend(m_backend);
0099         Settings::self()->save();
0100     }
0101 }
0102 
0103 void BackendChooseDialog::updateContent()
0104 {
0105     auto* current = Cantor::Backend::getBackend( m_ui.backendList->currentItem()->text() );
0106     if (current)
0107     {
0108         QString desc;
0109         QString reason;
0110         QString header = i18n("<h1>%1</h1>"
0111                               "<div><b>Recommended version:</b> %2</div>",
0112                               current->name(), current->version());
0113         QString info = i18n("<hr><div>%1</div><br>"
0114                             "<div>See <a href=\"%2\">%2</a> for more information.</div>",
0115                             current->description(), current->url());
0116 
0117         if (current->requirementsFullfilled(&reason))
0118         {
0119             desc = header + info;
0120             m_ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(true);
0121             m_ui.makeDefault->setEnabled(true);
0122         }
0123         else
0124         {
0125             QString reasonMsg = i18n("<hr><div><b><font color='#e50000'>Some requirements are not fulfilled: </font></b>%1</div>", reason);
0126             desc = header + reasonMsg + info;
0127             m_ui.buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false);
0128             m_ui.makeDefault->setEnabled(false);
0129         }
0130         m_ui.descriptionView->setHtml(desc);
0131     }
0132 }
0133 
0134 QString BackendChooseDialog::backendName()
0135 {
0136     return m_backend;
0137 }