Commit d0a98dfd authored by Gregor Mi's avatar Gregor Mi

Add 'Open Containing Folder' to Project plugin's tree context menu

Summary:
Plus:

- Reorder Qt include statements alphabetically
- Compact code which adds menu actions
- Make action handling code a bit more resilient by using const
- Cleanup: Remove KIO_VERSION check because it is no longer needed

Screenshot:

{F6414525}

Reviewers: #kate, cullmann

Reviewed By: #kate, cullmann

Subscribers: cullmann, broulik, kwrite-devel

Tags: #kate

Differential Revision: https://phabricator.kde.org/D16822
parent 327ccdf8
......@@ -21,21 +21,23 @@
#include "kateprojecttreeviewcontextmenu.h"
#include <klocalizedstring.h>
#include <KIO/OpenFileManagerWindowJob>
#include <KMimeTypeTrader>
#include <KRun>
#include <KNS3/KMoreTools>
#include <KNS3/KMoreToolsMenuFactory>
#include <QMenu>
#include <QStandardPaths>
#include <QFileInfo>
#include <QDir>
#include <QIcon>
#include <QProcess>
#include <QApplication>
#include <QClipboard>
#include <QMimeType>
#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QIcon>
#include <QMenu>
#include <QMimeDatabase>
#include <QMimeType>
#include <QProcess>
#include <QStandardPaths>
KateProjectTreeViewContextMenu::KateProjectTreeViewContextMenu()
{
......@@ -65,23 +67,23 @@ static bool isGit(const QString &filename)
void KateProjectTreeViewContextMenu::exec(const QString &filename, const QPoint &pos, QWidget *parent)
{
/**
* create context menu
* Create context menu
*/
QMenu menu;
/**
* Copy Path
*/
QAction *copyAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy File Path"));
/**
* handle "open with"
* Handle "open with",
* find correct mimetype to query for possible applications
*/
QMenu *openWithMenu = menu.addMenu(i18n("Open With"));
QMimeType mimeType = QMimeDatabase().mimeTypeForFile(filename);
KService::List offers = KMimeTypeTrader::self()->query(mimeType.name(), QStringLiteral("Application"));
/**
* for each one, insert a menu item...
*/
// For each one, insert a menu item...
for (KService::List::Iterator it = offers.begin(); it != offers.end(); ++it) {
KService::Ptr service = *it;
if (service->name() == QStringLiteral("Kate")) {
......@@ -90,21 +92,22 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QPoint
QAction *action = openWithMenu->addAction(QIcon::fromTheme(service->icon()), service->name());
action->setData(service->entryPath());
}
// Perhaps disable menu, if no entries
openWithMenu->setEnabled(!openWithMenu->isEmpty());
/**
* perhaps disable menu, if no entries!
* Open Containing folder
*/
openWithMenu->setEnabled(!openWithMenu->isEmpty());
auto openContaingFolderAction = menu.addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("&Open Containing Folder"));
/**
* Git menu
*/
KMoreToolsMenuFactory menuFactory(QLatin1String("kate/addons/project/git-tools"));
QMenu gitMenu; // must live as long as the maybe filled menu items should live
if (isGit(filename)) {
menuFactory.fillMenuFromGroupingNames(&gitMenu, { QLatin1String("git-clients-and-actions") },
QUrl::fromLocalFile(filename));
menu.addSection(i18n("Git:"));
Q_FOREACH(auto action, gitMenu.actions()) {
menu.addAction(action);
......@@ -114,12 +117,11 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QPoint
/**
* run menu and handle the triggered action
*/
if (QAction *action = menu.exec(pos)) {
// handle apps
if (copyAction == action) {
if (QAction* const action = menu.exec(pos)) {
if (action == copyAction) {
QApplication::clipboard()->setText(filename);
} else {
}
else if (action->parentWidget() == openWithMenu) {
// handle "open with"
const QString openWith = action->data().toString();
if (KService::Ptr app = KService::serviceByDesktopPath(openWith)) {
......@@ -128,5 +130,11 @@ void KateProjectTreeViewContextMenu::exec(const QString &filename, const QPoint
KRun::runService(*app, list, parent);
}
}
else if (action == openContaingFolderAction) {
KIO::highlightInFileManager({ QUrl::fromLocalFile(filename) });
}
else {
// One of the git actions was triggered
}
}
}
......@@ -32,15 +32,10 @@
#include <KAcceleratorManager>
#include <KConfigGroup>
#include <KLocalizedString>
// remove #ifdef, once Kate depends on KF 5.24
#include <kio_version.h>
#if KIO_VERSION >= QT_VERSION_CHECK(5, 24, 0)
#include <KIO/OpenFileManagerWindowJob>
#endif
#include <QApplication>
#include <QClipboard>
#include <QDesktopServices>
#include <QHelpEvent>
#include <QMenu>
#include <QStackedWidget>
......@@ -613,11 +608,7 @@ void KateViewSpace::showContextMenu(int id, const QPoint & globalPos)
} 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
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment