Changeset View
Changeset View
Standalone View
Standalone View
addons/project/kateprojecttreeviewcontextmenu.cpp
Show All 15 Lines | |||||
16 | * along with this library; see the file COPYING.LIB. If not, write to | 16 | * along with this library; see the file COPYING.LIB. If not, write to | ||
17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | * Boston, MA 02110-1301, USA. | 18 | * Boston, MA 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "kateprojecttreeviewcontextmenu.h" | 21 | #include "kateprojecttreeviewcontextmenu.h" | ||
22 | 22 | | |||
23 | #include <klocalizedstring.h> | 23 | #include <klocalizedstring.h> | ||
24 | #include <KIO/OpenFileManagerWindowJob> | ||||
24 | #include <KMimeTypeTrader> | 25 | #include <KMimeTypeTrader> | ||
25 | #include <KRun> | 26 | #include <KRun> | ||
26 | #include <KNS3/KMoreTools> | 27 | #include <KNS3/KMoreTools> | ||
27 | #include <KNS3/KMoreToolsMenuFactory> | 28 | #include <KNS3/KMoreToolsMenuFactory> | ||
28 | 29 | | |||
29 | #include <QMenu> | | |||
30 | #include <QStandardPaths> | | |||
31 | #include <QFileInfo> | | |||
32 | #include <QDir> | | |||
33 | #include <QIcon> | | |||
34 | #include <QProcess> | | |||
35 | #include <QApplication> | 30 | #include <QApplication> | ||
36 | #include <QClipboard> | 31 | #include <QClipboard> | ||
37 | #include <QMimeType> | 32 | #include <QDebug> | ||
33 | #include <QDir> | ||||
34 | #include <QFileInfo> | ||||
35 | #include <QIcon> | ||||
36 | #include <QMenu> | ||||
38 | #include <QMimeDatabase> | 37 | #include <QMimeDatabase> | ||
38 | #include <QMimeType> | ||||
39 | #include <QProcess> | ||||
40 | #include <QStandardPaths> | ||||
39 | 41 | | |||
40 | KateProjectTreeViewContextMenu::KateProjectTreeViewContextMenu() | 42 | KateProjectTreeViewContextMenu::KateProjectTreeViewContextMenu() | ||
41 | { | 43 | { | ||
42 | } | 44 | } | ||
43 | 45 | | |||
44 | KateProjectTreeViewContextMenu::~KateProjectTreeViewContextMenu() | 46 | KateProjectTreeViewContextMenu::~KateProjectTreeViewContextMenu() | ||
45 | { | 47 | { | ||
46 | } | 48 | } | ||
Show All 13 Lines | 60 | if (git.waitForStarted() && git.waitForFinished(-1)) { | |||
60 | isGit = files.contains(fi.fileName()); | 62 | isGit = files.contains(fi.fileName()); | ||
61 | } | 63 | } | ||
62 | return isGit; | 64 | return isGit; | ||
63 | } | 65 | } | ||
64 | 66 | | |||
65 | void KateProjectTreeViewContextMenu::exec(const QString &filename, const QPoint &pos, QWidget *parent) | 67 | void KateProjectTreeViewContextMenu::exec(const QString &filename, const QPoint &pos, QWidget *parent) | ||
66 | { | 68 | { | ||
67 | /** | 69 | /** | ||
68 | * create context menu | 70 | * Create context menu | ||
69 | */ | 71 | */ | ||
70 | QMenu menu; | 72 | QMenu menu; | ||
71 | 73 | | |||
74 | /** | ||||
75 | * Copy Path | ||||
76 | */ | ||||
72 | QAction *copyAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy File Path")); | 77 | QAction *copyAction = menu.addAction(QIcon::fromTheme(QStringLiteral("edit-copy")), i18n("Copy File Path")); | ||
73 | 78 | | |||
74 | /** | 79 | /** | ||
75 | * handle "open with" | 80 | * Handle "open with", | ||
76 | * find correct mimetype to query for possible applications | 81 | * find correct mimetype to query for possible applications | ||
77 | */ | 82 | */ | ||
78 | QMenu *openWithMenu = menu.addMenu(i18n("Open With")); | 83 | QMenu *openWithMenu = menu.addMenu(i18n("Open With")); | ||
79 | QMimeType mimeType = QMimeDatabase().mimeTypeForFile(filename); | 84 | QMimeType mimeType = QMimeDatabase().mimeTypeForFile(filename); | ||
80 | KService::List offers = KMimeTypeTrader::self()->query(mimeType.name(), QStringLiteral("Application")); | 85 | KService::List offers = KMimeTypeTrader::self()->query(mimeType.name(), QStringLiteral("Application")); | ||
81 | 86 | // For each one, insert a menu item... | |||
82 | /** | | |||
83 | * for each one, insert a menu item... | | |||
84 | */ | | |||
85 | for (KService::List::Iterator it = offers.begin(); it != offers.end(); ++it) { | 87 | for (KService::List::Iterator it = offers.begin(); it != offers.end(); ++it) { | ||
86 | KService::Ptr service = *it; | 88 | KService::Ptr service = *it; | ||
87 | if (service->name() == QStringLiteral("Kate")) { | 89 | if (service->name() == QStringLiteral("Kate")) { | ||
88 | continue; // omit Kate | 90 | continue; // omit Kate | ||
89 | } | 91 | } | ||
90 | QAction *action = openWithMenu->addAction(QIcon::fromTheme(service->icon()), service->name()); | 92 | QAction *action = openWithMenu->addAction(QIcon::fromTheme(service->icon()), service->name()); | ||
91 | action->setData(service->entryPath()); | 93 | action->setData(service->entryPath()); | ||
92 | } | 94 | } | ||
95 | // Perhaps disable menu, if no entries | ||||
96 | openWithMenu->setEnabled(!openWithMenu->isEmpty()); | ||||
93 | 97 | | |||
94 | /** | 98 | /** | ||
95 | * perhaps disable menu, if no entries! | 99 | * Open Containing folder | ||
96 | */ | 100 | */ | ||
97 | openWithMenu->setEnabled(!openWithMenu->isEmpty()); | 101 | auto openContaingFolderAction = menu.addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("&Open Containing Folder")); | ||
98 | 102 | | |||
103 | /** | ||||
104 | * Git menu | ||||
105 | */ | ||||
99 | KMoreToolsMenuFactory menuFactory(QLatin1String("kate/addons/project/git-tools")); | 106 | KMoreToolsMenuFactory menuFactory(QLatin1String("kate/addons/project/git-tools")); | ||
100 | | ||||
101 | QMenu gitMenu; // must live as long as the maybe filled menu items should live | 107 | QMenu gitMenu; // must live as long as the maybe filled menu items should live | ||
102 | | ||||
103 | if (isGit(filename)) { | 108 | if (isGit(filename)) { | ||
104 | | ||||
105 | menuFactory.fillMenuFromGroupingNames(&gitMenu, { QLatin1String("git-clients-and-actions") }, | 109 | menuFactory.fillMenuFromGroupingNames(&gitMenu, { QLatin1String("git-clients-and-actions") }, | ||
106 | QUrl::fromLocalFile(filename)); | 110 | QUrl::fromLocalFile(filename)); | ||
107 | | ||||
108 | menu.addSection(i18n("Git:")); | 111 | menu.addSection(i18n("Git:")); | ||
109 | Q_FOREACH(auto action, gitMenu.actions()) { | 112 | Q_FOREACH(auto action, gitMenu.actions()) { | ||
110 | menu.addAction(action); | 113 | menu.addAction(action); | ||
111 | } | 114 | } | ||
112 | } | 115 | } | ||
113 | 116 | | |||
114 | /** | 117 | /** | ||
115 | * run menu and handle the triggered action | 118 | * run menu and handle the triggered action | ||
116 | */ | 119 | */ | ||
117 | if (QAction *action = menu.exec(pos)) { | 120 | if (QAction* const action = menu.exec(pos)) { | ||
118 | 121 | if (action == copyAction) { | |||
119 | // handle apps | | |||
120 | if (copyAction == action) { | | |||
121 | QApplication::clipboard()->setText(filename); | 122 | QApplication::clipboard()->setText(filename); | ||
122 | } else { | 123 | } | ||
124 | else if (action->parentWidget() == openWithMenu) { | ||||
123 | // handle "open with" | 125 | // handle "open with" | ||
124 | const QString openWith = action->data().toString(); | 126 | const QString openWith = action->data().toString(); | ||
125 | if (KService::Ptr app = KService::serviceByDesktopPath(openWith)) { | 127 | if (KService::Ptr app = KService::serviceByDesktopPath(openWith)) { | ||
126 | QList<QUrl> list; | 128 | QList<QUrl> list; | ||
127 | list << QUrl::fromLocalFile(filename); | 129 | list << QUrl::fromLocalFile(filename); | ||
128 | KRun::runService(*app, list, parent); | 130 | KRun::runService(*app, list, parent); | ||
129 | } | 131 | } | ||
130 | } | 132 | } | ||
133 | else if (action == openContaingFolderAction) { | ||||
134 | KIO::highlightInFileManager({ QUrl::fromLocalFile(filename) }); | ||||
135 | } | ||||
136 | else { | ||||
137 | // One of the git actions was triggered | ||||
138 | } | ||||
131 | } | 139 | } | ||
132 | } | 140 | } |