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

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 "changedfileactions.h"
0008 #include "gitmanager.h"
0009 #include "windows/diffwindow.h"
0010 
0011 #include <QAction>
0012 
0013 #include <KLocalizedString>
0014 
0015 ChangedFileActions::ChangedFileActions(Git::Manager *git, QWidget *parent)
0016     : AbstractActions(git, parent)
0017 {
0018     _actionDiff = addAction(i18n("Diff..."), this, &ChangedFileActions::diff, false, true);
0019     _actionRevert = addAction(i18n("Revert..."), this, &ChangedFileActions::revert, false, true);
0020 
0021     auto f = _actionDiff->font();
0022     f.setBold(true);
0023     _actionDiff->setFont(f);
0024 }
0025 
0026 const QString &ChangedFileActions::filePath() const
0027 {
0028     return mFilePath;
0029 }
0030 
0031 void ChangedFileActions::setFilePath(const QString &newFilePath)
0032 {
0033     mFilePath = newFilePath;
0034     mOriginalFilePath = QString();
0035 
0036     setActionEnabled(_actionDiff, true);
0037     setActionEnabled(_actionRevert, true);
0038 }
0039 
0040 void ChangedFileActions::setFilePaths(const QString &originalFilePath, const QString &renamedFilePath)
0041 {
0042     mOriginalFilePath = originalFilePath;
0043     mFilePath = renamedFilePath;
0044 
0045     setActionEnabled(_actionDiff, true);
0046     setActionEnabled(_actionRevert, true);
0047 }
0048 
0049 void ChangedFileActions::diff()
0050 {
0051     QSharedPointer<Git::File> original;
0052 
0053     if (mOriginalFilePath == QString())
0054         original.reset(new Git::File{mGit, mGit->currentBranch(), mFilePath});
0055     else
0056         original.reset(new Git::File{mGit, mGit->currentBranch(), mOriginalFilePath});
0057 
0058     QSharedPointer<Git::File> changed{new Git::File{mGit->path() + QLatin1Char('/') + mFilePath}};
0059 
0060     auto diffWin = new DiffWindow(original, changed);
0061     diffWin->showModal();
0062 }
0063 
0064 void ChangedFileActions::revert()
0065 {
0066     mGit->revertFile(mFilePath);
0067     Q_EMIT reloadNeeded();
0068 }
0069 
0070 #include "moc_changedfileactions.cpp"