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 "remotesactions.h"
0008 #include "libkommitwidgets_appdebug.h"
0009 
0010 #include <KLocalizedString>
0011 #include <KMessageBox>
0012 
0013 #include <QInputDialog>
0014 
0015 #include "commands/commandaddremote.h"
0016 #include "core/kmessageboxhelper.h"
0017 #include "dialogs/remoteinfodialog.h"
0018 #include "dialogs/runnerdialog.h"
0019 #include "gitmanager.h"
0020 #include "models/remotesmodel.h"
0021 
0022 RemotesActions::RemotesActions(Git::Manager *git, QWidget *parent)
0023     : AbstractActions(git, parent)
0024 {
0025     _actionCreate = addActionHidden(i18n("New..."), this, &RemotesActions::create);
0026     _actionRemove = addActionDisabled(i18n("Remove..."), this, &RemotesActions::remove);
0027     _actionRename = addActionDisabled(i18n("Rename..."), this, &RemotesActions::rename);
0028     _actionChangeUrl = addActionDisabled(i18n("Change url..."), this, &RemotesActions::changeUrl);
0029     _actionUpdate = nullptr;
0030 }
0031 
0032 const QString &RemotesActions::remoteName() const
0033 {
0034     return mRemoteName;
0035 }
0036 
0037 void RemotesActions::setRemoteName(const QString &newRemoteName)
0038 {
0039     mRemoteName = newRemoteName;
0040 
0041     setActionEnabled(_actionRemove, true);
0042     setActionEnabled(_actionRename, true);
0043     setActionEnabled(_actionChangeUrl, true);
0044 }
0045 
0046 void RemotesActions::create()
0047 {
0048     RemoteInfoDialog d{mParent};
0049     if (d.exec() == QDialog::Accepted) {
0050         //        _git->addRemote(d.remoteName(), d.remoteUrl());
0051 
0052         RunnerDialog runner(mGit);
0053         runner.run(d.command());
0054         runner.exec();
0055         mGit->remotesModel()->load();
0056     }
0057 }
0058 
0059 void RemotesActions::remove()
0060 {
0061     if (KMessageBoxHelper::removeQuestion(mParent, i18n("Are you sure to remove the selected remote?"), i18n("Remove remote?"))) {
0062         if (!mGit->removeRemote(mRemoteName)) {
0063             KMessageBox::information(mParent, i18n("Unable to remove the selected remote"));
0064             return;
0065         }
0066         mGit->remotesModel()->load();
0067     }
0068 }
0069 
0070 void RemotesActions::changeUrl()
0071 {
0072     auto remote = mGit->remotesModel()->findByName(mRemoteName);
0073 
0074     if (!remote)
0075         return;
0076     const auto newUrl = QInputDialog::getText(mParent, i18n("Change url"), i18n("URL"), QLineEdit::Normal, remote->pushUrl());
0077     if (!newUrl.isEmpty()) {
0078         mGit->remotesModel()->setUrl(mRemoteName, newUrl);
0079         KMessageBox::information(mParent, i18n("Url for remote changed successfully"));
0080     }
0081 }
0082 
0083 void RemotesActions::rename()
0084 {
0085     const auto newName = QInputDialog::getText(mParent, i18n("Rename"), i18n("New name"), QLineEdit::Normal, mRemoteName);
0086 
0087     if (!newName.isEmpty()) {
0088         mGit->remotesModel()->rename(mRemoteName, newName);
0089         mRemoteName = newName;
0090     }
0091 }
0092 
0093 void RemotesActions::update()
0094 {
0095     // TODO implement it.
0096     qCWarning(KOMMIT_WIDGETS_LOG()) << "RemotesActions::update not implemented";
0097 }
0098 
0099 #include "moc_remotesactions.cpp"