diff --git a/kate/CMakeLists.txt b/kate/CMakeLists.txt --- a/kate/CMakeLists.txt +++ b/kate/CMakeLists.txt @@ -20,6 +20,7 @@ kateconfigdialog.cpp kateconfigplugindialogpage.cpp katedocmanager.cpp + katefileactions.cpp katemainwindow.cpp katepluginmanager.cpp kateviewmanager.cpp diff --git a/kate/katefileactions.h b/kate/katefileactions.h new file mode 100644 --- /dev/null +++ b/kate/katefileactions.h @@ -0,0 +1,79 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2018 Gregor Mi + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . +**/ + +#ifndef KATE_FILEACTIONS_H +#define KATE_FILEACTIONS_H + +#include +#include + +#include + +class QWidget; +namespace KTextEditor +{ + class Document; +} + +namespace KateFileActions +{ + /** + * Shows a Rename dialog to rename the file associated with the document. + * The document will be closed an reopened. + * + * Nothing is done if the document is nullptr or has no associated file. + * + * TODO: code was copied and adapted from ../addons/filetree/katefiletree.cpp + * (-> DUPLICATE CODE, the new code here should be also used there!) + */ + void renameDocumentFile(QWidget* parent, KTextEditor::Document* document); + + /** + * Asks the user if the file should really be deleted. If yes, the file + * is deleted from disk and the document closed. + * + * Nothing is done if the document is nullptr or has no associated file. + * + * TODO: code was copied and adapted from ../addons/filetree/katefiletree.cpp + * (-> DUPLICATE CODE, the new code here should be also used there!) + */ + void removeDocumentFile(QWidget* parent, KTextEditor::Document* document); + + /** + * @returns a list of supported diff tools and if they are installed or not. + * pair.first is the filename of the executable and pair.second + * is true if the program is installed. + */ + std::vector> supportedDiffTools(); + + /** + * Runs an external program to compare the underlying files of two given documents. + * + * @param diffExecutable tested to work with "kdiff3", "kompare", and "meld" + * (@see supportedDiffTools()) + * + * TODO: handling when documentA or B is empty + * TODO: message if diffExecutable is not installed + */ + void compareWithExternalProgram(KTextEditor::Document* documentA, KTextEditor::Document* documentB, const QString& diffExecutable); +} + +#endif diff --git a/kate/katefileactions.cpp b/kate/katefileactions.cpp new file mode 100644 --- /dev/null +++ b/kate/katefileactions.cpp @@ -0,0 +1,139 @@ +/* This file is part of the KDE project + * + * Copyright (C) 2018 Gregor Mi + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) version 3, or any + * later version accepted by the membership of KDE e.V. (or its + * successor approved by the membership of KDE e.V.), which shall + * act as a proxy defined in Section 6 of version 3 of the license. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see . +**/ + +#include "katefileactions.h" + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include +#include + +void KateFileActions::renameDocumentFile(QWidget* parent, KTextEditor::Document* doc) +{ + if (!doc) { + return; + } + + const QUrl oldFileUrl = doc->url(); + + if (oldFileUrl.isEmpty()) { // NEW + return; + } + + const QString oldFileName = doc->url().fileName(); + bool ok; + + QString newFileName = QInputDialog::getText(parent, // ADAPTED + i18n("Rename file"), i18n("New file name"), QLineEdit::Normal, oldFileName, &ok); + if (!ok) { + return; + } + + QUrl newFileUrl = oldFileUrl.adjusted(QUrl::RemoveFilename | QUrl::StripTrailingSlash); + newFileUrl.setPath(newFileUrl.path() + QLatin1Char('/') + newFileName); + + if (!newFileUrl.isValid()) { + return; + } + + if (!doc->closeUrl()) { + return; + } + + doc->waitSaveComplete(); + + KIO::CopyJob* job = KIO::move(oldFileUrl, newFileUrl); + QSharedPointer sc(new QMetaObject::Connection()); + auto success = [doc, sc] (KIO::Job*, const QUrl&, const QUrl &realNewFileUrl, const QDateTime&, bool, bool) { + doc->openUrl(realNewFileUrl); + doc->documentSavedOrUploaded(doc, true); + QObject::disconnect(*sc); + }; + *sc = parent->connect(job, &KIO::CopyJob::copyingDone, doc, success); + + if (!job->exec()) { + KMessageBox::sorry(parent, i18n("File \"%1\" could not be moved to \"%2\"", oldFileUrl.toDisplayString(), newFileUrl.toDisplayString())); + doc->openUrl(oldFileUrl); + } +} + +void KateFileActions::removeDocumentFile(QWidget* parent, KTextEditor::Document* doc) +{ + if (!doc) { + return; + } + + const auto& url = doc->url(); + + if (url.isEmpty()) { // NEW + return; + } + + bool go = (KMessageBox::warningContinueCancel(parent, + i18n("Do you really want to delete file \"%1\"?", url.toDisplayString()), + i18n("Delete file"), + KStandardGuiItem::yes(), KStandardGuiItem::no(), QLatin1String("filetreedeletefile") + ) == KMessageBox::Continue); + + if (!go) { + return; + } + + if (!KTextEditor::Editor::instance()->application()->closeDocument(doc)) { + return; // no extra message, the internals of ktexteditor should take care of that. + } + + if (url.isValid()) { + KIO::DeleteJob *job = KIO::del(url); + if (!job->exec()) { + KMessageBox::sorry(parent, i18n("File \"%1\" could not be deleted.", url.toDisplayString())); + } + } +} + +std::vector> KateFileActions::supportedDiffTools() +{ + std::vector> resultList; + + resultList.push_back({ QStringLiteral("kdiff3"), true }); + resultList.push_back({ QStringLiteral("kompare"), true }); + resultList.push_back({ QStringLiteral("meld"), true }); + + return resultList; +} + +void KateFileActions::compareWithExternalProgram(KTextEditor::Document* documentA, KTextEditor::Document* documentB, const QString& diffExecutable) +{ + qDebug() << documentA->url() << documentB->url(); + + KProcess process; + process << diffExecutable << documentA->url().toLocalFile() << documentB->url().toLocalFile(); + process.startDetached(); +} diff --git a/kate/kateviewspace.cpp b/kate/kateviewspace.cpp --- a/kate/kateviewspace.cpp +++ b/kate/kateviewspace.cpp @@ -23,6 +23,7 @@ #include "kateviewmanager.h" #include "katedocmanager.h" #include "kateapp.h" +#include "katefileactions.h" #include "katesessionmanager.h" #include "katedebug.h" #include "katetabbar.h" @@ -32,17 +33,15 @@ #include #include #include -// remove #ifdef, once Kate depends on KF 5.24 -#include -#if KIO_VERSION >= QT_VERSION_CHECK(5, 24, 0) #include -#endif +#include #include #include #include #include #include +#include #include #include #include @@ -595,29 +594,83 @@ menu.addSeparator(); QAction *aCopyPath = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy &Path")); QAction *aOpenFolder = menu.addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("&Open Containing Folder")); + QAction *aRenameFile = menu.addAction(QIcon::fromTheme(QLatin1String("edit-rename")), i18nc("@action:inmenu", "Rename File...")); + QAction *aDeleteFile = menu.addAction(QIcon::fromTheme(QLatin1String("edit-delete-shred")), i18nc("@action:inmenu", "Delete File")); + QAction *aFileProperties = menu.addAction(QIcon::fromTheme(QStringLiteral("dialog-object-properties")), i18n("File Properties")); + menu.addSeparator(); + QMenu *mCompareWithActive = new QMenu(i18n("Compare active document with this using..."), &menu); + mCompareWithActive->setIcon(QIcon::fromTheme(QStringLiteral("kompare"))); + menu.addMenu(mCompareWithActive); if (KateApp::self()->documentManager()->documentList().count() < 2) { aCloseOthers->setEnabled(false); } + if (doc->url().isEmpty()) { aCopyPath->setEnabled(false); aOpenFolder->setEnabled(false); + aRenameFile->setEnabled(false); + aDeleteFile->setEnabled(false); + aFileProperties->setEnabled(false); + mCompareWithActive->setEnabled(false); + } + + auto activeDocument = KTextEditor::Editor::instance()->application()->activeMainWindow()->activeView()->document(); // used for mCompareWithActive which is used with another tab which is not active + // both documents must have urls and must not be the same to have the compare feature enabled + if (activeDocument->url().isEmpty() || activeDocument == doc) { + mCompareWithActive->setEnabled(false); + } + + if (mCompareWithActive->isEnabled()) { + auto&& supportedDiffTools = KateFileActions::supportedDiffTools(); + if (std::all_of(supportedDiffTools.begin(), supportedDiffTools.end(), + [](std::pair& tool) { return !tool.second; })) { + QAction *naAction = mCompareWithActive->addAction(i18n("None of the supported diff tools is installed. Click for help.")); + naAction->setData(QStringLiteral("help")); + } else { + for (auto&& diffTool : KateFileActions::supportedDiffTools()) { + if (diffTool.second) { + QAction *compareAction = mCompareWithActive->addAction(diffTool.first); + compareAction->setData(diffTool.first); + } + } + } } QAction *choice = menu.exec(globalPos); + if (!choice) { + return; + } + if (choice == aCloseTab) { closeTabRequest(id); } else if (choice == aCloseOthers) { KateApp::self()->documentManager()->closeOtherDocuments(doc); } else if (choice == aCopyPath) { QApplication::clipboard()->setText(doc->url().toDisplayString(QUrl::PreferLocalFile)); } else if (choice == aOpenFolder) { -#if KIO_VERSION >= QT_VERSION_CHECK(5, 24, 0) KIO::highlightInFileManager({doc->url()}); -#else - QDesktopServices::openUrl(doc->url().adjusted(QUrl::RemoveFilename)); -#endif + } else if (choice == aFileProperties) { + KFileItem fileItem(doc->url()); + QDialog* dlg = new KPropertiesDialog(fileItem); + dlg->setAttribute(Qt::WA_DeleteOnClose); + dlg->show(); + } else if (choice == aRenameFile) { + KateFileActions::renameDocumentFile(this, doc); + } + else if (choice == aDeleteFile) { + KateFileActions::removeDocumentFile(this, doc); + } + else if (choice->parent() == mCompareWithActive) { + QString actionData = choice->data().toString(); // "help" or name of the executable of the diff program + if (actionData == QStringLiteral("help")) { + QMessageBox::information(this, i18n("Help on Diff Tool"), + i18n("To use this feature, please install one of these programs: TODO: list of supportedPrograms"), + QMessageBox::StandardButton::Ok); + } else { + KateFileActions::compareWithExternalProgram(activeDocument, doc, actionData); + } } }