]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Merge branch 'davidedmundson/highdpi'
[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("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
143 KConfig trashConfig("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("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("delete");
174 addAction(deleteAction);
175
176 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
177 addAction(propertiesAction);
178
179 if (exec(m_pos) == restoreAction) {
180 QList<QUrl> selectedUrls;
181 foreach (const KFileItem &item, m_selectedItems) {
182 selectedUrls.append(item.url());
183 }
184
185 KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
186 KJobWidgets::setWindow(job, m_mainWindow);
187 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
188 }
189 }
190
191 void DolphinContextMenu::openItemContextMenu()
192 {
193 Q_ASSERT(!m_fileInfo.isNull());
194
195 QAction* openParentAction = 0;
196 QAction* openParentInNewWindowAction = 0;
197 QAction* openParentInNewTabAction = 0;
198 QAction* addToPlacesAction = 0;
199 const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
200
201 if (m_selectedItems.count() == 1) {
202 if (m_fileInfo.isDir()) {
203 // setup 'Create New' menu
204 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow);
205 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
206 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
207 newFileMenu->checkUpToDate();
208 newFileMenu->setPopupFiles(m_fileInfo.url());
209 newFileMenu->setEnabled(selectedItemsProps.supportsWriting());
210 connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
211 connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
212
213 QMenu* menu = newFileMenu->menu();
214 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
215 menu->setIcon(QIcon::fromTheme("document-new"));
216 addMenu(menu);
217 addSeparator();
218
219 // insert 'Open in new window' and 'Open in new tab' entries
220 addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
221 addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
222
223 // insert 'Add to Places' entry
224 if (!placeExists(m_fileInfo.url())) {
225 addToPlacesAction = addAction(QIcon::fromTheme("bookmark-new"),
226 i18nc("@action:inmenu Add selected folder to places",
227 "Add to Places"));
228 }
229
230 addSeparator();
231 } else if (m_baseUrl.scheme().contains("search") || m_baseUrl.scheme().contains("timeline")) {
232 openParentAction = new QAction(QIcon::fromTheme("document-open-folder"),
233 i18nc("@action:inmenu",
234 "Open Path"),
235 this);
236 addAction(openParentAction);
237
238 openParentInNewWindowAction = new QAction(QIcon::fromTheme("window-new"),
239 i18nc("@action:inmenu",
240 "Open Path in New Window"),
241 this);
242 addAction(openParentInNewWindowAction);
243
244 openParentInNewTabAction = new QAction(QIcon::fromTheme("tab-new"),
245 i18nc("@action:inmenu",
246 "Open Path in New Tab"),
247 this);
248 addAction(openParentInNewTabAction);
249
250 addSeparator();
251 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo).isEmpty()) {
252 // insert 'Open in new window' and 'Open in new tab' entries
253 addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
254 addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
255
256 addSeparator();
257 }
258 } else {
259 bool selectionHasOnlyDirs = true;
260 foreach (const KFileItem& item, m_selectedItems) {
261 const QUrl& url = DolphinView::openItemAsFolderUrl(item);
262 if (url.isEmpty()) {
263 selectionHasOnlyDirs = false;
264 break;
265 }
266 }
267
268 if (selectionHasOnlyDirs) {
269 // insert 'Open in new tab' entry
270 addAction(m_mainWindow->actionCollection()->action("open_in_new_tabs"));
271 addSeparator();
272 }
273 }
274
275 insertDefaultItemActions(selectedItemsProps);
276
277 addSeparator();
278
279 KFileItemActions fileItemActions;
280 fileItemActions.setItemListProperties(selectedItemsProps);
281 addServiceActions(fileItemActions);
282
283 addFileItemPluginActions();
284
285 addVersionControlPluginActions();
286
287 // insert 'Copy To' and 'Move To' sub menus
288 if (GeneralSettings::showCopyMoveMenu()) {
289 m_copyToMenu.setUrls(m_selectedItems.urlList());
290 m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting());
291 m_copyToMenu.setAutoErrorHandlingEnabled(true);
292 m_copyToMenu.addActionsTo(this);
293 }
294
295 // insert 'Properties...' entry
296 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
297 addAction(propertiesAction);
298
299 QAction* activatedAction = exec(m_pos);
300 if (activatedAction) {
301 if (activatedAction == addToPlacesAction) {
302 const QUrl selectedUrl(m_fileInfo.url());
303 if (selectedUrl.isValid()) {
304 PlacesItemModel model;
305 const QString text = selectedUrl.fileName();
306 PlacesItem* item = model.createPlacesItem(text, selectedUrl);
307 model.appendItemToGroup(item);
308 model.saveBookmarks();
309 }
310 } else if (activatedAction == openParentAction) {
311 m_command = OpenParentFolder;
312 } else if (activatedAction == openParentInNewWindowAction) {
313 m_command = OpenParentFolderInNewWindow;
314 } else if (activatedAction == openParentInNewTabAction) {
315 m_command = OpenParentFolderInNewTab;
316 }
317 }
318 }
319
320 void DolphinContextMenu::openViewportContextMenu()
321 {
322 // setup 'Create New' menu
323 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
324 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
325 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
326 newFileMenu->checkUpToDate();
327 newFileMenu->setPopupFiles(m_baseUrl);
328 addMenu(newFileMenu->menu());
329 addSeparator();
330
331 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
332 // "open_in_new_tab" here, as the current selection should get ignored.
333 addAction(m_mainWindow->actionCollection()->action("new_window"));
334 addAction(m_mainWindow->actionCollection()->action("new_tab"));
335
336 // Insert 'Add to Places' entry if exactly one item is selected
337 QAction* addToPlacesAction = 0;
338 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
339 addToPlacesAction = addAction(QIcon::fromTheme("bookmark-new"),
340 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
341 }
342
343 addSeparator();
344
345 QAction* pasteAction = createPasteAction();
346 addAction(pasteAction);
347 addSeparator();
348
349 // Insert service actions
350 const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
351 KFileItemActions fileItemActions;
352 fileItemActions.setItemListProperties(baseUrlProperties);
353 addServiceActions(fileItemActions);
354
355 addFileItemPluginActions();
356
357 addVersionControlPluginActions();
358
359 addCustomActions();
360
361 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
362 addAction(propertiesAction);
363
364 addShowMenuBarAction();
365
366 QAction* action = exec(m_pos);
367 if (addToPlacesAction && (action == addToPlacesAction)) {
368 const DolphinViewContainer* container = m_mainWindow->activeViewContainer();
369 if (container->url().isValid()) {
370 PlacesItemModel model;
371 PlacesItem* item = model.createPlacesItem(container->placesText(),
372 container->url());
373 model.appendItemToGroup(item);
374 model.saveBookmarks();
375 }
376 }
377 }
378
379 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
380 {
381 const KActionCollection* collection = m_mainWindow->actionCollection();
382
383 // Insert 'Cut', 'Copy' and 'Paste'
384 addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
385 addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
386 addAction(createPasteAction());
387
388 addSeparator();
389
390 // Insert 'Rename'
391 QAction* renameAction = collection->action("rename");
392 addAction(renameAction);
393
394 // Insert 'Move to Trash' and/or 'Delete'
395 if (properties.supportsDeleting()) {
396 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
397 !properties.isLocal());
398 const bool showMoveToTrashAction = (properties.isLocal() &&
399 properties.supportsMoving());
400
401 if (showDeleteAction && showMoveToTrashAction) {
402 delete m_removeAction;
403 m_removeAction = 0;
404 addAction(m_mainWindow->actionCollection()->action("move_to_trash"));
405 addAction(m_mainWindow->actionCollection()->action("delete"));
406 } else if (showDeleteAction && !showMoveToTrashAction) {
407 addAction(m_mainWindow->actionCollection()->action("delete"));
408 } else {
409 if (!m_removeAction) {
410 m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
411 }
412 addAction(m_removeAction);
413 m_removeAction->update();
414 }
415 }
416 }
417
418 void DolphinContextMenu::addShowMenuBarAction()
419 {
420 const KActionCollection* ac = m_mainWindow->actionCollection();
421 QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
422 if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
423 addSeparator();
424 addAction(showMenuBar);
425 }
426 }
427
428 bool DolphinContextMenu::placeExists(const QUrl& url) const
429 {
430 PlacesItemModel model;
431
432 const int count = model.count();
433 for (int i = 0; i < count; ++i) {
434 const QUrl placeUrl = model.placesItem(i)->url();
435 if (placeUrl.matches(url, QUrl::StripTrailingSlash)) {
436 return true;
437 }
438 }
439
440 return false;
441 }
442
443 QAction* DolphinContextMenu::createPasteAction()
444 {
445 QAction* action = 0;
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("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, "DesktopEntryName != 'dolphin'");
483
484 // insert 'Actions' sub menu
485 fileItemActions.addServiceActionsTo(this);
486 }
487
488 void DolphinContextMenu::addFileItemPluginActions()
489 {
490 KFileItemListProperties props;
491 if (m_selectedItems.isEmpty()) {
492 props.setItems(KFileItemList() << baseFileItem());
493 } else {
494 props = selectedItemsProperties();
495 }
496
497 QString commonMimeType = props.mimeType();
498 if (commonMimeType.isEmpty()) {
499 commonMimeType = QLatin1String("application/octet-stream");
500 }
501
502 const KService::List pluginServices = KMimeTypeTrader::self()->query(commonMimeType, "KFileItemAction/Plugin", "exist Library");
503 if (pluginServices.isEmpty()) {
504 return;
505 }
506
507 const KConfig config("kservicemenurc", KConfig::NoGlobals);
508 const KConfigGroup showGroup = config.group("Show");
509
510 foreach (const KService::Ptr& service, pluginServices) {
511 if (!showGroup.readEntry(service->desktopEntryName(), true)) {
512 // The plugin has been disabled
513 continue;
514 }
515
516 KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance<KAbstractFileItemActionPlugin>();
517 if (abstractPlugin) {
518 abstractPlugin->setParent(this);
519 addActions(abstractPlugin->actions(props, m_mainWindow));
520 }
521 }
522 }
523
524 void DolphinContextMenu::addVersionControlPluginActions()
525 {
526 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
527 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
528 if (!versionControlActions.isEmpty()) {
529 addActions(versionControlActions);
530 addSeparator();
531 }
532 }
533
534 void DolphinContextMenu::addCustomActions()
535 {
536 addActions(m_customActions);
537 }
538