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 "stashactions.h"
0008 
0009 #include <KLocalizedString>
0010 #include <KMessageBox>
0011 
0012 #include <QInputDialog>
0013 
0014 #include "core/kmessageboxhelper.h"
0015 #include "gitmanager.h"
0016 #include "models/stashesmodel.h"
0017 #include "windows/diffwindow.h"
0018 
0019 StashActions::StashActions(Git::Manager *git, QWidget *parent)
0020     : AbstractActions(git, parent)
0021 {
0022     _actionNew = addActionHidden(i18n("New stash..."), this, &StashActions::create);
0023     _actionDiff = addAction(i18n("Diff with working dir"), this, &StashActions::diff);
0024     _actionPop = addAction(i18n("Pop..."), this, &StashActions::pop, false, true);
0025     _actionApply = addAction(i18n("Apply..."), this, &StashActions::apply, false, true);
0026     _actionDrop = addAction(i18n("Remove..."), this, &StashActions::drop, false, true);
0027 
0028     _actionDrop->setIcon(QIcon::fromTheme(QStringLiteral("list-remove")));
0029     _actionNew->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
0030 }
0031 
0032 void StashActions::apply()
0033 {
0034     if (KMessageBoxHelper::applyQuestion(mParent, i18n("Are you sure to apply the selected stash?"), i18n("Apply stash %1", mStash->message()))) {
0035         if (!mGit->applyStash(mStash))
0036             KMessageBox::information(mParent, i18n("Unable to apply the selected stash"));
0037     }
0038 }
0039 
0040 void StashActions::drop()
0041 {
0042     if (KMessageBoxHelper::removeQuestion(mParent, i18n("Are you sure to drop the selected stash?"), i18n("Drop stash %1", mStash->message()))) {
0043         if (!mGit->removeStash(mStash)) {
0044             KMessageBox::information(mParent, i18n("Unable to remove the selected stash"));
0045             return;
0046         }
0047         mGit->stashesModel()->load();
0048     }
0049 }
0050 
0051 void StashActions::pop()
0052 {
0053     if (KMessageBoxHelper::applyQuestion(mParent, i18n("Are you sure to pop the selected stash?"), i18n("Pop stash %1", mStash->message()))) {
0054         if (!mGit->popStash(mStash)) {
0055             KMessageBox::information(mParent, i18n("Unable to remove the selected stash"));
0056             return;
0057         }
0058         mGit->stashesModel()->load();
0059     }
0060 }
0061 
0062 void StashActions::diff()
0063 {
0064     auto d = new DiffWindow(mGit, mStash->message(), QLatin1String());
0065     d->showModal();
0066 }
0067 
0068 void StashActions::create()
0069 {
0070     if (mGit->changedFiles().empty()) {
0071         KMessageBox::information(mParent, i18n("You don't have any changes!"), i18n("Stash"));
0072         return;
0073     }
0074     bool ok;
0075 
0076     const auto name = QInputDialog::getText(mParent, i18n("Create new stash"), i18n("Name of stash"), QLineEdit::Normal, QString(), &ok);
0077 
0078     if (ok) {
0079         mGit->createStash(name);
0080         mGit->stashesModel()->load();
0081     }
0082 }
0083 
0084 QSharedPointer<Git::Stash> StashActions::stash() const
0085 {
0086     return mStash;
0087 }
0088 
0089 void StashActions::setStash(QSharedPointer<Git::Stash> stash)
0090 {
0091     mStash = stash;
0092 
0093     setActionEnabled(_actionApply, !stash.isNull());
0094     setActionEnabled(_actionDiff, !stash.isNull());
0095     setActionEnabled(_actionDrop, !stash.isNull());
0096     setActionEnabled(_actionPop, !stash.isNull());
0097 
0098 }
0099 
0100 #include "moc_stashactions.cpp"