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