]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Merge branch 'Applications/16.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 "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 <KService>
41 #include <KLocalizedString>
42 #include <KStandardAction>
43 #include <KToolBar>
44
45 #include <QApplication>
46 #include <QClipboard>
47 #include <QKeyEvent>
48 #include <QMenuBar>
49 #include <QMenu>
50
51 #include <panels/places/placesitem.h>
52 #include <panels/places/placesitemmodel.h>
53
54
55 #include "views/dolphinview.h"
56 #include "views/viewmodecontroller.h"
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(0),
68 m_selectedItems(),
69 m_selectedItemsProperties(0),
70 m_context(NoContext),
71 m_copyToMenu(parent),
72 m_customActions(),
73 m_command(None),
74 m_removeAction(0)
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 = 0;
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();
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();
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 KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig);
144 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
145 addAction(emptyTrashAction);
146
147 addCustomActions();
148
149 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
150 addAction(propertiesAction);
151
152 addShowMenuBarAction();
153
154 if (exec(m_pos) == emptyTrashAction) {
155 KIO::JobUiDelegate uiDelegate;
156 uiDelegate.setWindow(m_mainWindow);
157 if (uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) {
158 KIO::Job* job = KIO::emptyTrash();
159 KJobWidgets::setWindow(job, m_mainWindow);
160 job->ui()->setAutoErrorHandlingEnabled(true);
161 }
162 }
163 }
164
165 void DolphinContextMenu::openTrashItemContextMenu()
166 {
167 Q_ASSERT(m_context & TrashContext);
168 Q_ASSERT(m_context & ItemContext);
169
170 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
171 addAction(restoreAction);
172
173 QAction* deleteAction = m_mainWindow->actionCollection()->action(QStringLiteral("delete"));
174 addAction(deleteAction);
175
176 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
177 addAction(propertiesAction);
178
179 if (exec(m_pos) == restoreAction) {
180 QList<QUrl> selectedUrls;
181 selectedUrls.reserve(m_selectedItems.count());
182 foreach (const KFileItem &item, m_selectedItems) {
183 selectedUrls.append(item.url());
184 }
185
186 KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
187 KJobWidgets::setWindow(job, m_mainWindow);
188 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
189 }
190 }
191
192 void DolphinContextMenu::openItemContextMenu()
193 {
194 Q_ASSERT(!m_fileInfo.isNull());
195
196 QAction* openParentAction = 0;
197 QAction* openParentInNewWindowAction = 0;
198 QAction* openParentInNewTabAction = 0;
199 QAction* addToPlacesAction = 0;
200 const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
201
202 if (m_selectedItems.count() == 1) {
203 if (m_fileInfo.isDir()) {
204 // setup 'Create New' menu
205 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow);
206 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
207 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
208 newFileMenu->checkUpToDate();
209 newFileMenu->setPopupFiles(m_fileInfo.url());
210 newFileMenu->setEnabled(selectedItemsProps.supportsWriting());
211 connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
212 connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
213
214 QMenu* menu = newFileMenu->menu();
215 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
216 menu->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
217 addMenu(menu);
218 addSeparator();
219
220 // insert 'Open in new window' and 'Open in new tab' entries
221 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window")));
222 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab")));
223
224 // insert 'Add to Places' entry
225 if (!placeExists(m_fileInfo.url())) {
226 addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
227 i18nc("@action:inmenu Add selected folder to places",
228 "Add to Places"));
229 }
230
231 addSeparator();
232 } else if (m_baseUrl.scheme().contains(QStringLiteral("search")) || m_baseUrl.scheme().contains(QStringLiteral("timeline"))) {
233 openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
234 i18nc("@action:inmenu",
235 "Open Path"),
236 this);
237 addAction(openParentAction);
238
239 openParentInNewWindowAction = new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
240 i18nc("@action:inmenu",
241 "Open Path in New Window"),
242 this);
243 addAction(openParentInNewWindowAction);
244
245 openParentInNewTabAction = new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
246 i18nc("@action:inmenu",
247 "Open Path in New Tab"),
248 this);
249 addAction(openParentInNewTabAction);
250
251 addSeparator();
252 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo).isEmpty()) {
253 // insert 'Open in new window' and 'Open in new tab' entries
254 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window")));
255 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab")));
256
257 addSeparator();
258 }
259 } else {
260 bool selectionHasOnlyDirs = true;
261 foreach (const KFileItem& item, m_selectedItems) {
262 const QUrl& url = DolphinView::openItemAsFolderUrl(item);
263 if (url.isEmpty()) {
264 selectionHasOnlyDirs = false;
265 break;
266 }
267 }
268
269 if (selectionHasOnlyDirs) {
270 // insert 'Open in new tab' entry
271 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
272 addSeparator();
273 }
274 }
275
276 insertDefaultItemActions(selectedItemsProps);
277
278 addSeparator();
279
280 KFileItemActions fileItemActions;
281 fileItemActions.setItemListProperties(selectedItemsProps);
282 addServiceActions(fileItemActions);
283
284 addFileItemPluginActions();
285
286 addVersionControlPluginActions();
287
288 // insert 'Copy To' and 'Move To' sub menus
289 if (GeneralSettings::showCopyMoveMenu()) {
290 m_copyToMenu.setUrls(m_selectedItems.urlList());
291 m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting());
292 m_copyToMenu.setAutoErrorHandlingEnabled(true);
293 m_copyToMenu.addActionsTo(this);
294 }
295
296 // insert 'Properties...' entry
297 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
298 addAction(propertiesAction);
299
300 QAction* activatedAction = exec(m_pos);
301 if (activatedAction) {
302 if (activatedAction == addToPlacesAction) {
303 const QUrl selectedUrl(m_fileInfo.url());
304 if (selectedUrl.isValid()) {
305 PlacesItemModel model;
306 const QString text = selectedUrl.fileName();
307 PlacesItem* item = model.createPlacesItem(text, selectedUrl);
308 model.appendItemToGroup(item);
309 model.saveBookmarks();
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 = 0;
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 addFileItemPluginActions();
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 if (container->url().isValid()) {
371 PlacesItemModel model;
372 PlacesItem* item = model.createPlacesItem(container->placesText(),
373 container->url());
374 model.appendItemToGroup(item);
375 model.saveBookmarks();
376 }
377 }
378 }
379
380 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
381 {
382 const KActionCollection* collection = m_mainWindow->actionCollection();
383
384 // Insert 'Cut', 'Copy' and 'Paste'
385 addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
386 addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
387 addAction(createPasteAction());
388
389 addSeparator();
390
391 // Insert 'Rename'
392 QAction* renameAction = collection->action(QStringLiteral("rename"));
393 addAction(renameAction);
394
395 // Insert 'Move to Trash' and/or 'Delete'
396 if (properties.supportsDeleting()) {
397 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
398 !properties.isLocal());
399 const bool showMoveToTrashAction = (properties.isLocal() &&
400 properties.supportsMoving());
401
402 if (showDeleteAction && showMoveToTrashAction) {
403 delete m_removeAction;
404 m_removeAction = 0;
405 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("move_to_trash")));
406 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("delete")));
407 } else if (showDeleteAction && !showMoveToTrashAction) {
408 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("delete")));
409 } else {
410 if (!m_removeAction) {
411 m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
412 }
413 addAction(m_removeAction);
414 m_removeAction->update();
415 }
416 }
417 }
418
419 void DolphinContextMenu::addShowMenuBarAction()
420 {
421 const KActionCollection* ac = m_mainWindow->actionCollection();
422 QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
423 if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
424 addSeparator();
425 addAction(showMenuBar);
426 }
427 }
428
429 bool DolphinContextMenu::placeExists(const QUrl& url) const
430 {
431 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
432 // can be expensive because the model asks Solid for the devices which are
433 // available, which can take some time.
434 // TODO: Consider restoring this check if the handling of Places and devices
435 // will be decoupled in the future.
436 return false;
437 }
438
439 QAction* DolphinContextMenu::createPasteAction()
440 {
441 QAction* action = 0;
442 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
443 if (isDir && (m_selectedItems.count() == 1)) {
444 const QMimeData *mimeData = QApplication::clipboard()->mimeData();
445 bool canPaste;
446 const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileInfo);
447 action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this);
448 action->setEnabled(canPaste);
449 connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
450 } else {
451 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
452 }
453
454 return action;
455 }
456
457 KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
458 {
459 if (!m_selectedItemsProperties) {
460 m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
461 }
462 return *m_selectedItemsProperties;
463 }
464
465 KFileItem DolphinContextMenu::baseFileItem()
466 {
467 if (!m_baseFileItem) {
468 m_baseFileItem = new KFileItem(m_baseUrl);
469 }
470 return *m_baseFileItem;
471 }
472
473 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
474 {
475 fileItemActions.setParentWidget(m_mainWindow);
476
477 // insert 'Open With...' action or sub menu
478 fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != 'dolphin'"));
479
480 // insert 'Actions' sub menu
481 fileItemActions.addServiceActionsTo(this);
482 }
483
484 void DolphinContextMenu::addFileItemPluginActions()
485 {
486 KFileItemListProperties props;
487 if (m_selectedItems.isEmpty()) {
488 props.setItems(KFileItemList() << baseFileItem());
489 } else {
490 props = selectedItemsProperties();
491 }
492
493 QString commonMimeType = props.mimeType();
494 if (commonMimeType.isEmpty()) {
495 commonMimeType = QStringLiteral("application/octet-stream");
496 }
497
498 const KService::List pluginServices = KMimeTypeTrader::self()->query(commonMimeType, QStringLiteral("KFileItemAction/Plugin"), QStringLiteral("exist Library"));
499 if (pluginServices.isEmpty()) {
500 return;
501 }
502
503 const KConfig config(QStringLiteral("kservicemenurc"), KConfig::NoGlobals);
504 const KConfigGroup showGroup = config.group("Show");
505
506 foreach (const KService::Ptr& service, pluginServices) {
507 if (!showGroup.readEntry(service->desktopEntryName(), true)) {
508 // The plugin has been disabled
509 continue;
510 }
511
512 KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance<KAbstractFileItemActionPlugin>();
513 if (abstractPlugin) {
514 abstractPlugin->setParent(this);
515 addActions(abstractPlugin->actions(props, m_mainWindow));
516 }
517 }
518 }
519
520 void DolphinContextMenu::addVersionControlPluginActions()
521 {
522 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
523 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
524 if (!versionControlActions.isEmpty()) {
525 addActions(versionControlActions);
526 addSeparator();
527 }
528 }
529
530 void DolphinContextMenu::addCustomActions()
531 {
532 addActions(m_customActions);
533 }
534