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 "fileactions.h" 0008 0009 #include "dialogs/fileblamedialog.h" 0010 #include "dialogs/filehistorydialog.h" 0011 #include "dialogs/fileviewerdialog.h" 0012 #include "dialogs/searchdialog.h" 0013 #include "windows/diffwindow.h" 0014 #include "windows/mergewindow.h" 0015 0016 #include <entities/file.h> 0017 #include <gitmanager.h> 0018 0019 #include <KIO/ApplicationLauncherJob> 0020 #include <KIO/OpenUrlJob> 0021 #include <KLocalizedString> 0022 #include <kjobtrackerinterface.h> 0023 0024 #include <kio_version.h> 0025 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0) 0026 #include <KIO/JobUiDelegateFactory> 0027 #else 0028 #include <KIO/JobUiDelegate> 0029 #endif 0030 0031 #include <QAction> 0032 #include <QDebug> 0033 #include <QDesktopServices> 0034 #include <QFileDialog> 0035 #include <QMenu> 0036 #include <QMimeDatabase> 0037 #include <QStandardPaths> 0038 0039 const QString &FileActions::place() const 0040 { 0041 return mPlace; 0042 } 0043 0044 void FileActions::setPlace(const QString &newPlace) 0045 { 0046 mPlace = newPlace; 0047 0048 setActionEnabled(_actionView, !mFilePath.isEmpty()); 0049 setActionEnabled(_actionOpenWith, !mFilePath.isEmpty()); 0050 setActionEnabled(_actionDiffWithHead, !mFilePath.isEmpty()); 0051 setActionEnabled(_actionMergeWithHead, !mFilePath.isEmpty()); 0052 setActionEnabled(_actionSaveAs, !mFilePath.isEmpty()); 0053 setActionEnabled(_actionHistory, !mFilePath.isEmpty()); 0054 setActionEnabled(_actionBlame, !mFilePath.isEmpty()); 0055 setActionEnabled(_actionSearch, !mFilePath.isEmpty()); 0056 } 0057 0058 const QString &FileActions::filePath() const 0059 { 0060 return mFilePath; 0061 } 0062 0063 void FileActions::setFilePath(const QString &newFilePath) 0064 { 0065 mFilePath = newFilePath; 0066 0067 setActionEnabled(_actionView, !mPlace.isEmpty()); 0068 setActionEnabled(_actionOpen, !mPlace.isEmpty()); 0069 setActionEnabled(_actionOpenWith, !mPlace.isEmpty()); 0070 setActionEnabled(_actionDiffWithHead, !mPlace.isEmpty()); 0071 setActionEnabled(_actionMergeWithHead, !mPlace.isEmpty()); 0072 setActionEnabled(_actionSaveAs, !mPlace.isEmpty()); 0073 setActionEnabled(_actionHistory, !mPlace.isEmpty()); 0074 setActionEnabled(_actionBlame, !mPlace.isEmpty()); 0075 setActionEnabled(_actionSearch, !mPlace.isEmpty()); 0076 } 0077 0078 FileActions::FileActions(Git::Manager *git, QWidget *parent) 0079 : AbstractActions(git, parent) 0080 { 0081 mOpenWithMenu = new QMenu(parent); 0082 0083 _actionView = addAction(i18n("Preview"), this, &FileActions::viewFile, false, true); 0084 _actionOpen = addAction(i18n("Open"), this, &FileActions::openFile, false, true); 0085 _actionOpenWith = addAction(i18n("Open with..."), this, &FileActions::openWith, false, true); 0086 0087 _actionDiffWithHead = addAction(i18n("Diff with working dir"), this, &FileActions::diffWithHead, false, true); 0088 _actionMergeWithHead = addAction(i18n("Merge with working dir"), this, &FileActions::mergeWithHead, false, true); 0089 0090 _actionSaveAs = addAction(i18n("Save as..."), this, &FileActions::saveAsFile, false, true); 0091 _actionHistory = addAction(i18n("Log"), this, &FileActions::logFile, false, true); 0092 _actionBlame = addAction(i18n("Blame"), this, &FileActions::blameFile, false, true); 0093 _actionSearch = addAction(i18n("Search..."), this, &FileActions::search, false, true); 0094 } 0095 0096 void FileActions::popup(const QPoint &pos) 0097 { 0098 mMenu->popup(pos); 0099 } 0100 0101 void FileActions::viewFile() 0102 { 0103 auto d = new FileViewerDialog(mGit, mPlace, mFilePath, mParent); 0104 d->setWindowModality(Qt::ApplicationModal); 0105 d->setAttribute(Qt::WA_DeleteOnClose, true); 0106 d->show(); 0107 } 0108 0109 void FileActions::saveAsFile() 0110 0111 { 0112 const auto fileName = QFileDialog::getSaveFileName(mParent); 0113 if (!fileName.isEmpty()) { 0114 Git::File file{mGit, mPlace, mFilePath}; 0115 file.save(fileName); 0116 } 0117 } 0118 0119 void FileActions::logFile() 0120 { 0121 const Git::File file{mGit, mPlace, mFilePath}; 0122 FileHistoryDialog d(mGit, file, mParent); 0123 d.exec(); 0124 } 0125 0126 void FileActions::blameFile() 0127 { 0128 const Git::File file(mGit, mPlace, mFilePath); 0129 FileBlameDialog d(mGit, file, mParent); 0130 d.exec(); 0131 } 0132 0133 void FileActions::search() 0134 { 0135 SearchDialog d(mFilePath, mGit, mParent); 0136 d.exec(); 0137 } 0138 0139 void FileActions::openFile() 0140 { 0141 const Git::File file(mGit, mPlace, mFilePath); 0142 auto tempFilePath = file.saveAsTemp(); // TODO: remove temp file after openning 0143 if (tempFilePath.isEmpty()) 0144 return; 0145 0146 auto url = QUrl::fromLocalFile(tempFilePath); 0147 qDebug() << "Openning " << tempFilePath << url; 0148 QDesktopServices::openUrl(url); 0149 0150 // TODO: needs to be checked 0151 /*KIO::OpenUrlJob *job = new KIO::OpenUrlJob(url); 0152 0153 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0) 0154 job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr)); 0155 #else 0156 job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr)); 0157 #endif 0158 qDebug() << "Starting"; 0159 if(!job->exec()) { 0160 qDebug() << job->errorString(); 0161 if(!QDesktopServices::openUrl(url)) 0162 qDebug() << "Could not launch external editor."; 0163 }*/ 0164 } 0165 0166 void FileActions::openWith() 0167 { 0168 const Git::File file(mGit, mPlace, mFilePath); 0169 auto tempFilePath = file.saveAsTemp(); // TODO: remove temp file after openning 0170 if (tempFilePath.isEmpty()) 0171 return; 0172 0173 KIO::ApplicationLauncherJob *job = new KIO::ApplicationLauncherJob(); 0174 job->setUrls({QUrl::fromLocalFile(tempFilePath)}); 0175 0176 #if KIO_VERSION >= QT_VERSION_CHECK(5, 98, 0) 0177 job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr)); 0178 #else 0179 job->setUiDelegate(new KIO::JobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr)); 0180 #endif 0181 0182 job->start(); 0183 } 0184 0185 void FileActions::diffWithHead() 0186 { 0187 QSharedPointer<Git::File> oldFile{new Git::File{mGit, mPlace, mFilePath}}; 0188 QSharedPointer<Git::File> newFile{new Git::File{mGit->path() + QLatin1Char('/') + mFilePath}}; 0189 0190 auto d = new DiffWindow(oldFile, newFile); 0191 d->showModal(); 0192 } 0193 0194 void FileActions::mergeWithHead() 0195 { 0196 auto d = new MergeWindow(mGit, MergeWindow::NoParams); 0197 0198 Git::File f{mGit, mPlace, mFilePath}; 0199 auto tempFile = f.saveAsTemp(); 0200 0201 d->setFilePathBase(tempFile); 0202 d->setFilePathLocal(mGit->path() + QLatin1Char('/') + mFilePath); 0203 d->setFilePathRemote(tempFile); 0204 d->setFilePathResult(mGit->path() + QLatin1Char('/') + mFilePath); 0205 d->load(); 0206 0207 d->exec(); 0208 QFile::remove(tempFile); 0209 } 0210 0211 #include "moc_fileactions.cpp"