File indexing completed on 2025-01-05 05:14:40

0001 /*
0002 SPDX-FileCopyrightText: 2021 Hamed Masafi <hamed.masfi@gmail.com>
0003 
0004 SPDX-License-Identifier: GPL-3.0-or-later
0005 */
0006 
0007 #include "settingsmanager.h"
0008 
0009 #include "KommitSettings.h"
0010 #include "gitmanager.h"
0011 #include "kommitwidgetsglobaloptions.h"
0012 
0013 #include <QCalendar>
0014 
0015 #include <KConfigDialog>
0016 
0017 #include <QWidget>
0018 
0019 SettingsManager::SettingsManager(Git::Manager *git, QWidget *parentWidget)
0020     : QObject(parentWidget)
0021     , mGit(git)
0022     , mParentWidget(parentWidget)
0023 {
0024 }
0025 
0026 void SettingsManager::settingsChanged()
0027 {
0028     KommitSettings::setCalendarType(pageBase.kcfg_calendarTypeIndex->currentText());
0029     KommitSettings::self()->save();
0030 
0031     mGit->setConfig(QStringLiteral("http.proxy"), pageGit.lineEditGitProxy->text(), Git::Manager::ConfigGlobal);
0032 
0033     if (KommitSettings::registerDiffTool()) {
0034         mGit->setConfig(QStringLiteral("difftool.kommitdiff.cmd"), QStringLiteral("kommitdiff \"$LOCAL\" \"$REMOTE\""), Git::Manager::ConfigGlobal);
0035     } else {
0036         mGit->unsetConfig(QStringLiteral("difftool.kommitdiff.cmd"), Git::Manager::ConfigGlobal);
0037     }
0038     if (KommitSettings::registerMergeTool()) {
0039         mGit->setConfig(QStringLiteral("mergetool.kommitmerge.cmd"),
0040                         QStringLiteral("kommitmerge \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\""),
0041                         Git::Manager::ConfigGlobal);
0042         mGit->setConfig(QStringLiteral("mergetool.kommitmerge.trustExitCode"), QStringLiteral("true"), Git::Manager::ConfigGlobal);
0043     } else {
0044         mGit->unsetConfig(QStringLiteral("mergetool.kommitmerge.cmd"), Git::Manager::ConfigGlobal);
0045         mGit->unsetConfig(QStringLiteral("mergetool.kommitmerge.trustExitCode"), Git::Manager::ConfigGlobal);
0046     }
0047 
0048     applyToLib();
0049     Q_EMIT settingsUpdated();
0050 }
0051 
0052 void SettingsManager::show()
0053 {
0054     exec(mParentWidget);
0055 }
0056 
0057 QWidget *SettingsManager::createBasePage()
0058 {
0059     auto w = new QWidget;
0060     pageBase.setupUi(w);
0061 
0062     auto availableCalendars = QCalendar::availableCalendars();
0063     std::sort(availableCalendars.begin(), availableCalendars.end());
0064     pageBase.kcfg_calendarTypeIndex->addItems(availableCalendars);
0065     pageBase.kcfg_calendarTypeIndex->setCurrentText(KommitSettings::calendarType());
0066     return w;
0067 }
0068 QWidget *SettingsManager::createDiffPage()
0069 {
0070     auto w = new QWidget;
0071     pageDiff.setupUi(w);
0072     return w;
0073 }
0074 QWidget *SettingsManager::createGitPage()
0075 {
0076     auto w = new QWidget;
0077     pageGit.setupUi(w);
0078     pageGit.lineEditGitProxy->setText(mGit->config(QStringLiteral("http.proxy"), Git::Manager::ConfigGlobal));
0079     return w;
0080 }
0081 
0082 void SettingsManager::exec(QWidget *parentWidget)
0083 {
0084     const auto name = QStringLiteral("settings");
0085     auto dialog = KConfigDialog::exists(name);
0086 
0087     if (dialog) {
0088         dialog->setParent(parentWidget);
0089         dialog->exec();
0090         return;
0091     }
0092 
0093     dialog = new KConfigDialog(parentWidget, name, KommitSettings::self());
0094 
0095     dialog->addPage(createBasePage(), i18n("General"), QStringLiteral("package_setting"));
0096     dialog->addPage(createDiffPage(), i18n("Diff"), QStringLiteral("package_setting"));
0097     dialog->addPage(createGitPage(), i18n("Git"), QStringLiteral("package_setting"));
0098 
0099     connect(dialog, &KConfigDialog::settingsChanged, this, &SettingsManager::settingsChanged);
0100     dialog->show();
0101 }
0102 
0103 void SettingsManager::applyToLib()
0104 {
0105     auto opt = KommitWidgetsGlobalOptions::instance();
0106     auto set = KommitSettings::self();
0107     opt->setCalendar(QCalendar{set->calendarType()});
0108     opt->setColor(Git::ChangeStatus::Added, set->diffAddedColor());
0109     opt->setColor(Git::ChangeStatus::Modified, set->diffModifiedColor());
0110     opt->setColor(Git::ChangeStatus::Removed, set->diffRemovedColor());
0111 }
0112 
0113 #include "moc_settingsmanager.cpp"