]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Merge branch 'Applications/18.04'
[dolphin.git] / src / dolphincontextmenu.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and *
3 * Cvetoslav Ludmiloff *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphincontextmenu.h"
22
23 #include "dolphin_generalsettings.h"
24 #include "dolphinmainwindow.h"
25 #include "dolphinnewfilemenu.h"
26 #include "dolphinremoveaction.h"
27 #include "dolphinviewcontainer.h"
28 #include "panels/places/placesitem.h"
29 #include "panels/places/placesitemmodel.h"
30 #include "trash/dolphintrash.h"
31 #include "views/dolphinview.h"
32 #include "views/viewmodecontroller.h"
33
34 #include <KAbstractFileItemActionPlugin>
35 #include <KActionCollection>
36 #include <KFileItemActions>
37 #include <KFileItemListProperties>
38 #include <KIO/EmptyTrashJob>
39 #include <KIO/JobUiDelegate>
40 #include <KIO/Paste>
41 #include <KIO/RestoreJob>
42 #include <KJobWidgets>
43 #include <KLocalizedString>
44 #include <KMimeTypeTrader>
45 #include <KNewFileMenu>
46 #include <KPluginMetaData>
47 #include <KService>
48 #include <KStandardAction>
49 #include <KToolBar>
50
51 #include <QApplication>
52 #include <QClipboard>
53 #include <QKeyEvent>
54 #include <QMenu>
55 #include <QMenuBar>
56 #include <QMimeDatabase>
57
58 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
59 const QPoint& pos,
60 const KFileItem& fileInfo,
61 const QUrl& baseUrl) :
62 QMenu(parent),
63 m_pos(pos),
64 m_mainWindow(parent),
65 m_fileInfo(fileInfo),
66 m_baseUrl(baseUrl),
67 m_baseFileItem(nullptr),
68 m_selectedItems(),
69 m_selectedItemsProperties(nullptr),
70 m_context(NoContext),
71 m_copyToMenu(parent),
72 m_customActions(),
73 m_command(None),
74 m_removeAction(nullptr)
75 {
76 // The context menu either accesses the URLs of the selected items
77 // or the items itself. To increase the performance both lists are cached.
78 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
79 m_selectedItems = view->selectedItems();
80 }
81
82 DolphinContextMenu::~DolphinContextMenu()
83 {
84 delete m_selectedItemsProperties;
85 m_selectedItemsProperties = nullptr;
86 }
87
88 void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
89 {
90 m_customActions = actions;
91 }
92
93 DolphinContextMenu::Command DolphinContextMenu::open()
94 {
95 // get the context information
96 if (m_baseUrl.scheme() == QLatin1String("trash")) {
97 m_context |= TrashContext;
98 }
99
100 if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
101 m_context |= ItemContext;
102 // TODO: handle other use cases like devices + desktop files
103 }
104
105 // open the corresponding popup for the context
106 if (m_context & TrashContext) {
107 if (m_context & ItemContext) {
108 openTrashItemContextMenu();
109 } else {
110 openTrashContextMenu();
111 }
112 } else if (m_context & ItemContext) {
113 openItemContextMenu();
114 } else {
115 Q_ASSERT(m_context == NoContext);
116 openViewportContextMenu();
117 }
118
119 return m_command;
120 }
121
122 void DolphinContextMenu::keyPressEvent(QKeyEvent *ev)
123 {
124 if (m_removeAction && ev->key() == Qt::Key_Shift) {
125 m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed);
126 }
127 QMenu::keyPressEvent(ev);
128 }
129
130 void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev)
131 {
132 if (m_removeAction && ev->key() == Qt::Key_Shift) {
133 m_removeAction->update(DolphinRemoveAction::ShiftState::Released);
134 }
135 QMenu::keyReleaseEvent(ev);
136 }
137
138 void DolphinContextMenu::openTrashContextMenu()
139 {
140 Q_ASSERT(m_context & TrashContext);
141
142 QAction* emptyTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
143 emptyTrashAction->setEnabled(!Trash::isEmpty());
144 addAction(emptyTrashAction);
145
146 addCustomActions();
147
148 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
149 addAction(propertiesAction);
150
151 addShowMenuBarAction();
152
153 if (exec(m_pos) == emptyTrashAction) {
154 Trash::empty(m_mainWindow);
155 }
156 }
157
158 void DolphinContextMenu::openTrashItemContextMenu()
159 {
160 Q_ASSERT(m_context & TrashContext);
161 Q_ASSERT(m_context & ItemContext);
162
163 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
164 addAction(restoreAction);
165
166 QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
167 addAction(deleteAction);
168
169 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
170 addAction(propertiesAction);
171
172 if (exec(m_pos) == restoreAction) {
173 QList<QUrl> selectedUrls;
174 selectedUrls.reserve(m_selectedItems.count());
175 foreach (const KFileItem &item, m_selectedItems) {
176 selectedUrls.append(item.url());
177 }
178
179 KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
180 KJobWidgets::setWindow(job, m_mainWindow);
181 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
182 }
183 }
184
185 void DolphinContextMenu::openItemContextMenu()
186 {
187 Q_ASSERT(!m_fileInfo.isNull());
188
189 QAction* openParentAction = nullptr;
190 QAction* openParentInNewWindowAction = nullptr;
191 QAction* openParentInNewTabAction = nullptr;
192 QAction* addToPlacesAction = nullptr;
193 const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
194
195 KFileItemActions fileItemActions;
196 fileItemActions.setParentWidget(m_mainWindow);
197 fileItemActions.setItemListProperties(selectedItemsProps);
198
199 if (m_selectedItems.count() == 1) {
200 if (m_fileInfo.isDir()) {
201 // insert 'Open in new window' and 'Open in new tab' entries
202 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window")));
203 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab")));
204
205 // Insert 'Open With' entries
206 addOpenWithActions(fileItemActions);
207
208 // insert 'Add to Places' entry
209 if (!placeExists(m_fileInfo.url())) {
210 addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
211 i18nc("@action:inmenu Add selected folder to places",
212 "Add to Places"));
213 }
214
215 addSeparator();
216
217 // set up 'Create New' menu
218 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow);
219 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
220 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
221 newFileMenu->checkUpToDate();
222 newFileMenu->setPopupFiles(m_fileInfo.url());
223 newFileMenu->setEnabled(selectedItemsProps.supportsWriting());
224 connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
225 connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
226
227 QMenu* menu = newFileMenu->menu();
228 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
229 menu->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
230 addMenu(menu);
231
232 addSeparator();
233 } else if (m_baseUrl.scheme().contains(QStringLiteral("search")) || m_baseUrl.scheme().contains(QStringLiteral("timeline"))) {
234 openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
235 i18nc("@action:inmenu",
236 "Open Path"),
237 this);
238 addAction(openParentAction);
239
240 openParentInNewWindowAction = new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
241 i18nc("@action:inmenu",
242 "Open Path in New Window"),
243 this);
244 addAction(openParentInNewWindowAction);
245
246 openParentInNewTabAction = new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
247 i18nc("@action:inmenu",
248 "Open Path in New Tab"),
249 this);
250 addAction(openParentInNewTabAction);
251
252 addSeparator();
253 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo).isEmpty()) {
254 // Insert 'Open With" entries
255 addOpenWithActions(fileItemActions);
256
257 // insert 'Open in new window' and 'Open in new tab' entries
258 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window")));
259 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab")));
260
261 addSeparator();
262 } else {
263 // Insert 'Open With" entries
264 addOpenWithActions(fileItemActions);
265 }
266 if (m_fileInfo.isLink()) {
267 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("show_target")));
268 addSeparator();
269 }
270 } else {
271 bool selectionHasOnlyDirs = true;
272 foreach (const KFileItem& item, m_selectedItems) {
273 const QUrl& url = DolphinView::openItemAsFolderUrl(item);
274 if (url.isEmpty()) {
275 selectionHasOnlyDirs = false;
276 break;
277 }
278 }
279
280 if (selectionHasOnlyDirs) {
281 // insert 'Open in new tab' entry
282 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
283 }
284 // Insert 'Open With" entries
285 addOpenWithActions(fileItemActions);
286 }
287
288 insertDefaultItemActions(selectedItemsProps);
289
290 addSeparator();
291
292 fileItemActions.addServiceActionsTo(this);
293 fileItemActions.addPluginActionsTo(this);
294
295 addVersionControlPluginActions();
296
297 // insert 'Copy To' and 'Move To' sub menus
298 if (GeneralSettings::showCopyMoveMenu()) {
299 m_copyToMenu.setUrls(m_selectedItems.urlList());
300 m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting());
301 m_copyToMenu.setAutoErrorHandlingEnabled(true);
302 m_copyToMenu.addActionsTo(this);
303 }
304
305 // insert 'Properties...' entry
306 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
307 addAction(propertiesAction);
308
309 QAction* activatedAction = exec(m_pos);
310 if (activatedAction) {
311 if (activatedAction == addToPlacesAction) {
312 const QUrl selectedUrl(m_fileInfo.url());
313 if (selectedUrl.isValid()) {
314 PlacesItemModel model;
315 const QString text = selectedUrl.fileName();
316 model.createPlacesItem(text, selectedUrl, KIO::iconNameForUrl(selectedUrl));
317 }
318 } else if (activatedAction == openParentAction) {
319 m_command = OpenParentFolder;
320 } else if (activatedAction == openParentInNewWindowAction) {
321 m_command = OpenParentFolderInNewWindow;
322 } else if (activatedAction == openParentInNewTabAction) {
323 m_command = OpenParentFolderInNewTab;
324 }
325 }
326 }
327
328 void DolphinContextMenu::openViewportContextMenu()
329 {
330 // setup 'Create New' menu
331 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
332 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
333 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
334 newFileMenu->checkUpToDate();
335 newFileMenu->setPopupFiles(m_baseUrl);
336 addMenu(newFileMenu->menu());
337 addSeparator();
338
339 // Insert 'Open With' entries
340 const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
341 KFileItemActions fileItemActions;
342 fileItemActions.setParentWidget(m_mainWindow);
343 fileItemActions.setItemListProperties(baseUrlProperties);
344 addOpenWithActions(fileItemActions);
345
346 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
347 // "open_in_new_tab" here, as the current selection should get ignored.
348 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_window")));
349 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_tab")));
350
351 // Insert 'Add to Places' entry if exactly one item is selected
352 QAction* addToPlacesAction = nullptr;
353 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
354 addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
355 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
356 }
357
358 addSeparator();
359
360 QAction* pasteAction = createPasteAction();
361 addAction(pasteAction);
362 addSeparator();
363
364 // Insert service actions
365 fileItemActions.addServiceActionsTo(this);
366 fileItemActions.addPluginActionsTo(this);
367
368 addVersionControlPluginActions();
369
370 addCustomActions();
371
372 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
373 addAction(propertiesAction);
374
375 addShowMenuBarAction();
376
377 QAction* action = exec(m_pos);
378 if (addToPlacesAction && (action == addToPlacesAction)) {
379 const DolphinViewContainer* container = m_mainWindow->activeViewContainer();
380 const QUrl url = container->url();
381 if (url.isValid()) {
382 PlacesItemModel model;
383 QString icon;
384 if (container->isSearchModeEnabled()) {
385 icon = QStringLiteral("folder-saved-search-symbolic");
386 } else {
387 icon = KIO::iconNameForUrl(url);
388 }
389 model.createPlacesItem(container->placesText(), url, icon);
390 }
391 }
392 }
393
394 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
395 {
396 const KActionCollection* collection = m_mainWindow->actionCollection();
397
398 // Insert 'Cut', 'Copy' and 'Paste'
399 addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
400 addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
401 addAction(createPasteAction());
402
403 addSeparator();
404
405 // Insert 'Rename'
406 addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile)));
407
408 // Insert 'Move to Trash' and/or 'Delete'
409 if (properties.supportsDeleting()) {
410 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
411 !properties.isLocal());
412 const bool showMoveToTrashAction = (properties.isLocal() &&
413 properties.supportsMoving());
414
415 if (showDeleteAction && showMoveToTrashAction) {
416 delete m_removeAction;
417 m_removeAction = nullptr;
418 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash)));
419 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
420 } else if (showDeleteAction && !showMoveToTrashAction) {
421 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
422 } else {
423 if (!m_removeAction) {
424 m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
425 }
426 addAction(m_removeAction);
427 m_removeAction->update();
428 }
429 }
430 }
431
432 void DolphinContextMenu::addShowMenuBarAction()
433 {
434 const KActionCollection* ac = m_mainWindow->actionCollection();
435 QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
436 if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
437 addSeparator();
438 addAction(showMenuBar);
439 }
440 }
441
442 bool DolphinContextMenu::placeExists(const QUrl& url) const
443 {
444 Q_UNUSED(url)
445 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
446 // can be expensive because the model asks Solid for the devices which are
447 // available, which can take some time.
448 // TODO: Consider restoring this check if the handling of Places and devices
449 // will be decoupled in the future.
450 return false;
451 }
452
453 QAction* DolphinContextMenu::createPasteAction()
454 {
455 QAction* action = nullptr;
456 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
457 if (isDir && (m_selectedItems.count() == 1)) {
458 const QMimeData *mimeData = QApplication::clipboard()->mimeData();
459 bool canPaste;
460 const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileInfo);
461 action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this);
462 action->setEnabled(canPaste);
463 connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
464 } else {
465 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
466 }
467
468 return action;
469 }
470
471 KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
472 {
473 if (!m_selectedItemsProperties) {
474 m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
475 }
476 return *m_selectedItemsProperties;
477 }
478
479 KFileItem DolphinContextMenu::baseFileItem()
480 {
481 if (!m_baseFileItem) {
482 m_baseFileItem = new KFileItem(m_baseUrl);
483 }
484 return *m_baseFileItem;
485 }
486
487 void DolphinContextMenu::addOpenWithActions(KFileItemActions& fileItemActions)
488 {
489 // insert 'Open With...' action or sub menu
490 fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp->desktopFileName()));
491 }
492
493 void DolphinContextMenu::addVersionControlPluginActions()
494 {
495 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
496 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
497 if (!versionControlActions.isEmpty()) {
498 addActions(versionControlActions);
499 addSeparator();
500 }
501 }
502
503 void DolphinContextMenu::addCustomActions()
504 {
505 addActions(m_customActions);
506 }
507