]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Dolphin: port from KonqOperations::doDrop to the new KIO::DropJob
[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.setItems(m_selectedItems);
290 m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting());
291 m_copyToMenu.addActionsTo(this);
292 }
293
294 // insert 'Properties...' entry
295 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
296 addAction(propertiesAction);
297
298 QAction* activatedAction = exec(m_pos);
299 if (activatedAction) {
300 if (activatedAction == addToPlacesAction) {
301 const QUrl selectedUrl(m_fileInfo.url());
302 if (selectedUrl.isValid()) {
303 PlacesItemModel model;
304 const QString text = selectedUrl.fileName();
305 PlacesItem* item = model.createPlacesItem(text, selectedUrl);
306 model.appendItemToGroup(item);
307 }
308 } else if (activatedAction == openParentAction) {
309 m_command = OpenParentFolder;
310 } else if (activatedAction == openParentInNewWindowAction) {
311 m_command = OpenParentFolderInNewWindow;
312 } else if (activatedAction == openParentInNewTabAction) {
313 m_command = OpenParentFolderInNewTab;
314 }
315 }
316 }
317
318 void DolphinContextMenu::openViewportContextMenu()
319 {
320 // setup 'Create New' menu
321 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
322 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
323 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
324 newFileMenu->checkUpToDate();
325 newFileMenu->setPopupFiles(m_baseUrl);
326 addMenu(newFileMenu->menu());
327 addSeparator();
328
329 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
330 // "open_in_new_tab" here, as the current selection should get ignored.
331 addAction(m_mainWindow->actionCollection()->action("new_window"));
332 addAction(m_mainWindow->actionCollection()->action("new_tab"));
333
334 // Insert 'Add to Places' entry if exactly one item is selected
335 QAction* addToPlacesAction = 0;
336 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
337 addToPlacesAction = addAction(QIcon::fromTheme("bookmark-new"),
338 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
339 }
340
341 addSeparator();
342
343 QAction* pasteAction = createPasteAction();
344 addAction(pasteAction);
345 addSeparator();
346
347 // Insert service actions
348 const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
349 KFileItemActions fileItemActions;
350 fileItemActions.setItemListProperties(baseUrlProperties);
351 addServiceActions(fileItemActions);
352
353 addFileItemPluginActions();
354
355 addVersionControlPluginActions();
356
357 addCustomActions();
358
359 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
360 addAction(propertiesAction);
361
362 addShowMenuBarAction();
363
364 QAction* action = exec(m_pos);
365 if (addToPlacesAction && (action == addToPlacesAction)) {
366 const DolphinViewContainer* container = m_mainWindow->activeViewContainer();
367 if (container->url().isValid()) {
368 PlacesItemModel model;
369 PlacesItem* item = model.createPlacesItem(container->placesText(),
370 container->url());
371 model.appendItemToGroup(item);
372 }
373 }
374 }
375
376 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
377 {
378 const KActionCollection* collection = m_mainWindow->actionCollection();
379
380 // Insert 'Cut', 'Copy' and 'Paste'
381 addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
382 addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
383 addAction(createPasteAction());
384
385 addSeparator();
386
387 // Insert 'Rename'
388 QAction* renameAction = collection->action("rename");
389 addAction(renameAction);
390
391 // Insert 'Move to Trash' and/or 'Delete'
392 if (properties.supportsDeleting()) {
393 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
394 !properties.isLocal());
395 const bool showMoveToTrashAction = (properties.isLocal() &&
396 properties.supportsMoving());
397
398 if (showDeleteAction && showMoveToTrashAction) {
399 delete m_removeAction;
400 m_removeAction = 0;
401 addAction(m_mainWindow->actionCollection()->action("move_to_trash"));
402 addAction(m_mainWindow->actionCollection()->action("delete"));
403 } else if (showDeleteAction && !showMoveToTrashAction) {
404 addAction(m_mainWindow->actionCollection()->action("delete"));
405 } else {
406 if (!m_removeAction) {
407 m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
408 }
409 addAction(m_removeAction);
410 m_removeAction->update();
411 }
412 }
413 }
414
415 void DolphinContextMenu::addShowMenuBarAction()
416 {
417 const KActionCollection* ac = m_mainWindow->actionCollection();
418 QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
419 if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
420 addSeparator();
421 addAction(showMenuBar);
422 }
423 }
424
425 bool DolphinContextMenu::placeExists(const QUrl& url) const
426 {
427 PlacesItemModel model;
428
429 const int count = model.count();
430 for (int i = 0; i < count; ++i) {
431 const QUrl placeUrl = model.placesItem(i)->url();
432 if (placeUrl.matches(url, QUrl::StripTrailingSlash)) {
433 return true;
434 }
435 }
436
437 return false;
438 }
439
440 QAction* DolphinContextMenu::createPasteAction()
441 {
442 QAction* action = 0;
443 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
444 if (isDir && (m_selectedItems.count() == 1)) {
445 const QMimeData *mimeData = QApplication::clipboard()->mimeData();
446 bool canPaste;
447 const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileInfo);
448 action = new QAction(QIcon::fromTheme("edit-paste"), text, this);
449 action->setEnabled(canPaste);
450 connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
451 } else {
452 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
453 }
454
455 return action;
456 }
457
458 KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
459 {
460 if (!m_selectedItemsProperties) {
461 m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
462 }
463 return *m_selectedItemsProperties;
464 }
465
466 KFileItem DolphinContextMenu::baseFileItem()
467 {
468 if (!m_baseFileItem) {
469 m_baseFileItem = new KFileItem(m_baseUrl);
470 }
471 return *m_baseFileItem;
472 }
473
474 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
475 {
476 fileItemActions.setParentWidget(m_mainWindow);
477
478 // insert 'Open With...' action or sub menu
479 fileItemActions.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
480
481 // insert 'Actions' sub menu
482 fileItemActions.addServiceActionsTo(this);
483 }
484
485 void DolphinContextMenu::addFileItemPluginActions()
486 {
487 KFileItemListProperties props;
488 if (m_selectedItems.isEmpty()) {
489 props.setItems(KFileItemList() << baseFileItem());
490 } else {
491 props = selectedItemsProperties();
492 }
493
494 QString commonMimeType = props.mimeType();
495 if (commonMimeType.isEmpty()) {
496 commonMimeType = QLatin1String("application/octet-stream");
497 }
498
499 const KService::List pluginServices = KMimeTypeTrader::self()->query(commonMimeType, "KFileItemAction/Plugin", "exist Library");
500 if (pluginServices.isEmpty()) {
501 return;
502 }
503
504 const KConfig config("kservicemenurc", KConfig::NoGlobals);
505 const KConfigGroup showGroup = config.group("Show");
506
507 foreach (const KService::Ptr& service, pluginServices) {
508 if (!showGroup.readEntry(service->desktopEntryName(), true)) {
509 // The plugin has been disabled
510 continue;
511 }
512
513 KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance<KAbstractFileItemActionPlugin>();
514 if (abstractPlugin) {
515 abstractPlugin->setParent(this);
516 addActions(abstractPlugin->actions(props, m_mainWindow));
517 }
518 }
519 }
520
521 void DolphinContextMenu::addVersionControlPluginActions()
522 {
523 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
524 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
525 if (!versionControlActions.isEmpty()) {
526 foreach (QAction* action, versionControlActions) {
527 addAction(action);
528 }
529 addSeparator();
530 }
531 }
532
533 void DolphinContextMenu::addCustomActions()
534 {
535 foreach (QAction* action, m_customActions) {
536 addAction(action);
537 }
538 }
539