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

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 "submoduleactions.h"
0008 #include "commands/addsubmodulecommand.h"
0009 #include "core/kmessageboxhelper.h"
0010 #include "dialogs/runnerdialog.h"
0011 #include "dialogs/submoduleinfodialog.h"
0012 #include "gitmanager.h"
0013 #include "models/submodulesmodel.h"
0014 
0015 #include "libkommitwidgets_appdebug.h"
0016 #include <QAction>
0017 
0018 #include <KLocalizedString>
0019 #include <KMessageBox>
0020 #include <QDir>
0021 
0022 const QString &SubmoduleActions::subModuleName() const
0023 {
0024     return mSubModuleName;
0025 }
0026 
0027 void SubmoduleActions::setSubModuleName(const QString &newSubModuleName)
0028 {
0029     mSubModuleName = newSubModuleName.trimmed();
0030 
0031     setActionEnabled(_actionInit, true);
0032     setActionEnabled(_actionUpdate, true);
0033     //    setActionEnabled(_actionDeinit, true);
0034     setActionEnabled(_actionSync, true);
0035 }
0036 
0037 SubmoduleActions::SubmoduleActions(Git::Manager *git, QWidget *parent)
0038     : AbstractActions(git, parent)
0039 {
0040     _actionCreate = addActionHidden(i18n("Add..."), this, &SubmoduleActions::create);
0041     _actionInit = addAction(i18n("Init..."), this, &SubmoduleActions::init, false);
0042     _actionUpdate = addAction(i18n("Update..."), this, &SubmoduleActions::update, false);
0043     //    _actionDeinit = addAction(i18n("Remove..."), this, &SubmoduleActions::deinit, false);
0044     _actionSync = addAction(i18n("Sync..."), this, &SubmoduleActions::sync, false);
0045 
0046     _actionCreate->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
0047 }
0048 
0049 void SubmoduleActions::init()
0050 {
0051     RunnerDialog d(mGit, mParent);
0052     d.run({QStringLiteral("submodule"), QStringLiteral("init"), mSubModuleName});
0053     d.exec();
0054 }
0055 
0056 void SubmoduleActions::update()
0057 {
0058     RunnerDialog d(mGit, mParent);
0059     d.run({QStringLiteral("submodule"), QStringLiteral("update"), mSubModuleName});
0060     d.exec();
0061 }
0062 
0063 void SubmoduleActions::create()
0064 {
0065     SubmoduleInfoDialog d(mGit, mParent);
0066     if (d.exec() == QDialog::Accepted) {
0067         RunnerDialog runner(mGit);
0068         runner.run(d.command());
0069         runner.exec();
0070         mGit->submodulesModel()->load();
0071     }
0072 }
0073 
0074 void SubmoduleActions::deinit()
0075 {
0076     if (KMessageBoxHelper::removeQuestion(mParent, i18n("Are you sure to remove the selected submodule?"), i18n("Remove Submodule"))) {
0077         qCDebug(KOMMIT_WIDGETS_LOG()) << mGit->runGit(
0078             {QStringLiteral("submodule"), QStringLiteral("deinit"), QStringLiteral("-f"), QStringLiteral("--"), mSubModuleName});
0079         qCDebug(KOMMIT_WIDGETS_LOG()) << mGit->runGit({QStringLiteral("rm"), mSubModuleName});
0080 
0081         QDir d(mGit->path() + QStringLiteral("/.git/modules/") + mSubModuleName);
0082         if (!d.removeRecursively()) {
0083             KMessageBox::error(mParent, i18n("Unable to remove the module directory"));
0084             return;
0085         }
0086         qCDebug(KOMMIT_WIDGETS_LOG()) << d.path();
0087         mGit->runGit({QStringLiteral("config"), QStringLiteral("--remove-section"), QStringLiteral("submodule.") + mSubModuleName});
0088 
0089         mGit->submodulesModel()->load();
0090         KMessageBox::information(mParent, i18n("The submodule %1 removed", mSubModuleName));
0091     }
0092 }
0093 
0094 void SubmoduleActions::sync()
0095 {
0096     RunnerDialog d(mGit, mParent);
0097     d.run({QStringLiteral("submodule"), QStringLiteral("sync"), mSubModuleName});
0098     d.exec();
0099 }
0100 
0101 #include "moc_submoduleactions.cpp"