]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
d4cf196b7cd05e3d7d5a5ff7919d534a6dec14b6
[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(0),
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(0)
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 PlacesItem* item = model.createPlacesItem(text, selectedUrl, KIO::iconNameForUrl(selectedUrl));
310 model.appendItemToGroup(item);
311 model.saveBookmarks();
312 }
313 } else if (activatedAction == openParentAction) {
314 m_command = OpenParentFolder;
315 } else if (activatedAction == openParentInNewWindowAction) {
316 m_command = OpenParentFolderInNewWindow;
317 } else if (activatedAction == openParentInNewTabAction) {
318 m_command = OpenParentFolderInNewTab;
319 }
320 }
321 }
322
323 void DolphinContextMenu::openViewportContextMenu()
324 {
325 // setup 'Create New' menu
326 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
327 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
328 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
329 newFileMenu->checkUpToDate();
330 newFileMenu->setPopupFiles(m_baseUrl);
331 addMenu(newFileMenu->menu());
332 addSeparator();
333
334 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
335 // "open_in_new_tab" here, as the current selection should get ignored.
336 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_window")));
337 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_tab")));
338
339 // Insert 'Add to Places' entry if exactly one item is selected
340 QAction* addToPlacesAction = 0;
341 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
342 addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
343 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
344 }
345
346 addSeparator();
347
348 QAction* pasteAction = createPasteAction();
349 addAction(pasteAction);
350 addSeparator();
351
352 // Insert service actions
353 const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
354 KFileItemActions fileItemActions;
355 fileItemActions.setItemListProperties(baseUrlProperties);
356 addServiceActions(fileItemActions);
357
358 fileItemActions.addPluginActionsTo(this);
359
360 addVersionControlPluginActions();
361
362 addCustomActions();
363
364 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
365 addAction(propertiesAction);
366
367 addShowMenuBarAction();
368
369 QAction* action = exec(m_pos);
370 if (addToPlacesAction && (action == addToPlacesAction)) {
371 const DolphinViewContainer* container = m_mainWindow->activeViewContainer();
372 const QUrl url = container->url();
373 if (url.isValid()) {
374 PlacesItemModel model;
375 QString icon;
376 if (container->isSearchModeEnabled()) {
377 icon = QStringLiteral("folder-saved-search-symbolic");
378 } else {
379 icon = KIO::iconNameForUrl(url);
380 }
381 PlacesItem* item = model.createPlacesItem(container->placesText(), url, icon);
382 model.appendItemToGroup(item);
383 model.saveBookmarks();
384 }
385 }
386 }
387
388 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
389 {
390 const KActionCollection* collection = m_mainWindow->actionCollection();
391
392 // Insert 'Cut', 'Copy' and 'Paste'
393 addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
394 addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
395 addAction(createPasteAction());
396
397 addSeparator();
398
399 // Insert 'Rename'
400 addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile)));
401
402 // Insert 'Move to Trash' and/or 'Delete'
403 if (properties.supportsDeleting()) {
404 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
405 !properties.isLocal());
406 const bool showMoveToTrashAction = (properties.isLocal() &&
407 properties.supportsMoving());
408
409 if (showDeleteAction && showMoveToTrashAction) {
410 delete m_removeAction;
411 m_removeAction = 0;
412 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash)));
413 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
414 } else if (showDeleteAction && !showMoveToTrashAction) {
415 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
416 } else {
417 if (!m_removeAction) {
418 m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
419 }
420 addAction(m_removeAction);
421 m_removeAction->update();
422 }
423 }
424 }
425
426 void DolphinContextMenu::addShowMenuBarAction()
427 {
428 const KActionCollection* ac = m_mainWindow->actionCollection();
429 QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
430 if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
431 addSeparator();
432 addAction(showMenuBar);
433 }
434 }
435
436 bool DolphinContextMenu::placeExists(const QUrl& url) const
437 {
438 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
439 // can be expensive because the model asks Solid for the devices which are
440 // available, which can take some time.
441 // TODO: Consider restoring this check if the handling of Places and devices
442 // will be decoupled in the future.
443 return false;
444 }
445
446 QAction* DolphinContextMenu::createPasteAction()
447 {
448 QAction* action = 0;
449 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
450 if (isDir && (m_selectedItems.count() == 1)) {
451 const QMimeData *mimeData = QApplication::clipboard()->mimeData();
452 bool canPaste;
453 const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileInfo);
454 action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this);
455 action->setEnabled(canPaste);
456 connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
457 } else {
458 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
459 }
460
461 return action;
462 }
463
464 KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
465 {
466 if (!m_selectedItemsProperties) {
467 m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
468 }
469 return *m_selectedItemsProperties;
470 }
471
472 KFileItem DolphinContextMenu::baseFileItem()
473 {
474 if (!m_baseFileItem) {
475 m_baseFileItem = new KFileItem(m_baseUrl);
476 }
477 return *m_baseFileItem;
478 }
479
480 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
481 {
482 fileItemActions.setParentWidget(m_mainWindow);
483
484 // insert 'Open With...' action or sub menu
485 fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != 'dolphin'"));
486
487 // insert 'Actions' sub menu
488 fileItemActions.addServiceActionsTo(this);
489 }
490
491 void DolphinContextMenu::addVersionControlPluginActions()
492 {
493 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
494 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
495 if (!versionControlActions.isEmpty()) {
496 addActions(versionControlActions);
497 addSeparator();
498 }
499 }
500
501 void DolphinContextMenu::addCustomActions()
502 {
503 addActions(m_customActions);
504 }
505