File indexing completed on 2024-04-28 04:39:10

0001 /*
0002     SPDX-FileCopyrightText: 2007 Andreas Pakulat <apaku@gmx.de>
0003     SPDX-FileCopyrightText: 2014 Kevin Funk <kfunk@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "qmakebuilderpreferences.h"
0009 
0010 #include <QIcon>
0011 
0012 #include <KIO/DeleteJob>
0013 #include <KLocalizedString>
0014 #include <KMessageBox>
0015 #include <KMessageBox_KDevCompat>
0016 #include <KJobWidgets>
0017 
0018 #include "ui_qmakeconfig.h"
0019 #include "qmakebuilddirchooser.h"
0020 #include "qmakebuilddirchooserdialog.h"
0021 #include "qmakeconfig.h"
0022 #include <debug.h>
0023 
0024 #include <interfaces/iproject.h>
0025 
0026 QMakeBuilderPreferences::QMakeBuilderPreferences(KDevelop::IPlugin* plugin,
0027                                                  const KDevelop::ProjectConfigOptions& options, QWidget* parent)
0028     : KDevelop::ConfigPage(plugin, nullptr, parent)
0029     , m_project(options.project)
0030 {
0031     m_prefsUi = new Ui::QMakeConfig;
0032     m_prefsUi->setupUi(this);
0033 
0034     m_chooserUi = new QMakeBuildDirChooser(m_project);
0035     auto groupBoxLayout = new QVBoxLayout(m_prefsUi->groupBox);
0036     groupBoxLayout->addWidget(m_chooserUi);
0037 
0038     m_chooserUi->kcfg_buildDir->setEnabled(false); // build directory MUST NOT be changed here
0039     connect(m_chooserUi, &QMakeBuildDirChooser::changed, this, &QMakeBuilderPreferences::changed);
0040     connect(m_chooserUi, &QMakeBuildDirChooser::changed, this, &QMakeBuilderPreferences::validate);
0041 
0042     connect(m_prefsUi->buildDirCombo, &QComboBox::currentTextChanged, this, &QMakeBuilderPreferences::loadOtherConfig);
0043     connect(m_prefsUi->buildDirCombo, QOverload<int>::of(&QComboBox::currentIndexChanged), this,
0044             &QMakeBuilderPreferences::changed);
0045     connect(m_prefsUi->addButton, &QAbstractButton::pressed, this, &QMakeBuilderPreferences::addBuildConfig);
0046     connect(m_prefsUi->removeButton, &QAbstractButton::pressed, this, &QMakeBuilderPreferences::removeBuildConfig);
0047 
0048     reset(); // load initial values
0049 }
0050 
0051 QMakeBuilderPreferences::~QMakeBuilderPreferences()
0052 {
0053     // not a QObject !
0054     delete m_chooserUi;
0055 }
0056 
0057 void QMakeBuilderPreferences::reset()
0058 {
0059     qCDebug(KDEV_QMAKEBUILDER) << "loading data";
0060     // refresh combobox
0061     KConfigGroup cg(m_project->projectConfiguration(), QMakeConfig::CONFIG_GROUP);
0062     const QString buildPath = cg.readEntry(QMakeConfig::BUILD_FOLDER, QString());
0063 
0064     // update build list (this will trigger loadOtherConfig if signals are still connected)
0065     disconnect(m_prefsUi->buildDirCombo, &QComboBox::currentTextChanged, this, &QMakeBuilderPreferences::loadOtherConfig);
0066     m_prefsUi->buildDirCombo->clear();
0067     m_prefsUi->buildDirCombo->insertItems(0, cg.groupList());
0068     if (m_prefsUi->buildDirCombo->contains(buildPath)) {
0069         m_prefsUi->buildDirCombo->setCurrentItem(buildPath);
0070         m_chooserUi->loadConfig(buildPath);
0071     }
0072     qCDebug(KDEV_QMAKEBUILDER) << "Loaded" << cg.groupList() << buildPath;
0073     m_prefsUi->removeButton->setEnabled(m_prefsUi->buildDirCombo->count() > 1);
0074     connect(m_prefsUi->buildDirCombo, &QComboBox::currentTextChanged, this, &QMakeBuilderPreferences::loadOtherConfig);
0075 
0076     validate();
0077 }
0078 
0079 QString QMakeBuilderPreferences::name() const
0080 {
0081     return i18nc("@title:tab", "QMake");
0082 }
0083 
0084 void QMakeBuilderPreferences::apply()
0085 {
0086     qCDebug(KDEV_QMAKEBUILDER) << "Saving data";
0087     QString errormsg;
0088 
0089     if (m_chooserUi->validate(&errormsg)) {
0090         // data is valid: save, once in the build dir's data and also as current data
0091         m_chooserUi->saveConfig();
0092         KConfigGroup config(m_project->projectConfiguration(), QMakeConfig::CONFIG_GROUP);
0093         m_chooserUi->saveConfig(config);
0094         config.writeEntry(QMakeConfig::BUILD_FOLDER, m_chooserUi->buildDir());
0095     } else {
0096         // invalid data: message box
0097         KMessageBox::error(nullptr, errormsg, QStringLiteral("Data is invalid!"));
0098         // FIXME dialog behaves like if save really happened (dialog closes if user click ok) even if changed signal is
0099         // emitted
0100     }
0101 }
0102 
0103 void QMakeBuilderPreferences::validate()
0104 {
0105     m_chooserUi->validate();
0106 }
0107 
0108 void QMakeBuilderPreferences::loadOtherConfig(const QString& config)
0109 {
0110     qCDebug(KDEV_QMAKEBUILDER) << "Loading config " << config;
0111     m_chooserUi->loadConfig(config);
0112     apply(); // since current config has changed, it must be saved immediately
0113 }
0114 
0115 void QMakeBuilderPreferences::addBuildConfig()
0116 {
0117     qCDebug(KDEV_QMAKEBUILDER) << "Adding a new config.";
0118     // for more simplicity, just launch regular dialog
0119     auto dlg = new QMakeBuildDirChooserDialog(m_project);
0120     if (dlg->exec() == QDialog::Accepted) {
0121         m_prefsUi->buildDirCombo->setCurrentItem(dlg->buildDir(), true);
0122         m_prefsUi->removeButton->setEnabled(m_prefsUi->buildDirCombo->count() > 1);
0123         // TODO run qmake
0124     }
0125 }
0126 
0127 void QMakeBuilderPreferences::removeBuildConfig()
0128 {
0129     qCDebug(KDEV_QMAKEBUILDER) << "Removing config" << m_prefsUi->buildDirCombo->currentText();
0130     QString removed = m_prefsUi->buildDirCombo->currentText();
0131     KConfigGroup cg(m_project->projectConfiguration(), QMakeConfig::CONFIG_GROUP);
0132 
0133     m_prefsUi->buildDirCombo->removeItem(m_prefsUi->buildDirCombo->currentIndex());
0134     m_prefsUi->removeButton->setEnabled(m_prefsUi->buildDirCombo->count() > 1);
0135     cg.group(removed).deleteGroup(KConfigBase::Persistent);
0136 
0137     if (QDir(removed).exists()) {
0138         int ret = KMessageBox::warningTwoActions(
0139             this,
0140             i18n("The %1 directory is about to be removed in KDevelop's list.\n"
0141                  "Do you want KDevelop to delete it in the file system as well?",
0142                  removed),
0143             {}, KStandardGuiItem::del(),
0144             KGuiItem(i18nc("@action:button", "Do Not Delete"), QStringLiteral("dialog-cancel")));
0145         if (ret == KMessageBox::PrimaryAction) {
0146             auto deleteJob = KIO::del(QUrl::fromLocalFile(removed));
0147             KJobWidgets::setWindow(deleteJob, this);
0148             if (!deleteJob->exec())
0149                 KMessageBox::error(this, i18n("Could not remove: %1.", removed));
0150         }
0151     }
0152 }
0153 
0154 #include "moc_qmakebuilderpreferences.cpp"