]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Merge remote-tracking branch 'origin/master' into frameworks
[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 <kfileitemactionplugin.h>
31 #include <kabstractfileitemactionplugin.h>
32 #include <KFileItemActions>
33 #include <KFileItemListProperties>
34 #include <KIO/RestoreJob>
35 #include <KIO/EmptyTrashJob>
36 #include <KIO/JobUiDelegate>
37 #include <KIO/Paste>
38 #include <KJobWidgets>
39 #include <KMimeTypeTrader>
40 #include <KNewFileMenu>
41 #include <konq_operations.h>
42 #include <KService>
43 #include <KLocalizedString>
44 #include <KStandardAction>
45 #include <KToolBar>
46
47 #include <QApplication>
48 #include <QClipboard>
49 #include <QMenuBar>
50 #include <QMenu>
51
52 #include <panels/places/placesitem.h>
53 #include <panels/places/placesitemmodel.h>
54
55
56 #include "views/dolphinview.h"
57 #include "views/viewmodecontroller.h"
58
59 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
60 const QPoint& pos,
61 const KFileItem& fileInfo,
62 const QUrl& baseUrl) :
63 QMenu(parent),
64 m_pos(pos),
65 m_mainWindow(parent),
66 m_fileInfo(fileInfo),
67 m_baseUrl(baseUrl),
68 m_baseFileItem(0),
69 m_selectedItems(),
70 m_selectedItemsProperties(0),
71 m_context(NoContext),
72 m_copyToMenu(parent),
73 m_customActions(),
74 m_command(None),
75 m_removeAction(0)
76 {
77 // The context menu either accesses the URLs of the selected items
78 // or the items itself. To increase the performance both lists are cached.
79 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
80 m_selectedItems = view->selectedItems();
81 }
82
83 DolphinContextMenu::~DolphinContextMenu()
84 {
85 delete m_selectedItemsProperties;
86 m_selectedItemsProperties = 0;
87 }
88
89 void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
90 {
91 m_customActions = actions;
92 }
93
94 DolphinContextMenu::Command DolphinContextMenu::open()
95 {
96 // get the context information
97 if (m_baseUrl.scheme() == QLatin1String("trash")) {
98 m_context |= TrashContext;
99 }
100
101 if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
102 m_context |= ItemContext;
103 // TODO: handle other use cases like devices + desktop files
104 }
105
106 // open the corresponding popup for the context
107 if (m_context & TrashContext) {
108 if (m_context & ItemContext) {
109 openTrashItemContextMenu();
110 } else {
111 openTrashContextMenu();
112 }
113 } else if (m_context & ItemContext) {
114 openItemContextMenu();
115 } else {
116 Q_ASSERT(m_context == NoContext);
117 openViewportContextMenu();
118 }
119
120 return m_command;
121 }
122
123 void DolphinContextMenu::keyPressEvent(QKeyEvent *ev)
124 {
125 if (m_removeAction && ev->key() == Qt::Key_Shift) {
126 m_removeAction->update();
127 }
128 QMenu::keyPressEvent(ev);
129 }
130
131 void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev)
132 {
133 if (m_removeAction && ev->key() == Qt::Key_Shift) {
134 m_removeAction->update();
135 }
136 QMenu::keyReleaseEvent(ev);
137 }
138
139 void DolphinContextMenu::openTrashContextMenu()
140 {
141 Q_ASSERT(m_context & TrashContext);
142
143 QAction* emptyTrashAction = new QAction(QIcon::fromTheme("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
144 KConfig trashConfig("trashrc", KConfig::SimpleConfig);
145 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
146 addAction(emptyTrashAction);
147
148 addCustomActions();
149
150 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
151 addAction(propertiesAction);
152
153 addShowMenuBarAction();
154
155 if (exec(m_pos) == emptyTrashAction) {
156 KIO::JobUiDelegate uiDelegate;
157 uiDelegate.setWindow(m_mainWindow);
158 if (uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) {
159 KIO::Job* job = KIO::emptyTrash();
160 KJobWidgets::setWindow(job, m_mainWindow);
161 job->ui()->setAutoErrorHandlingEnabled(true);
162 }
163 }
164 }
165
166 void DolphinContextMenu::openTrashItemContextMenu()
167 {
168 Q_ASSERT(m_context & TrashContext);
169 Q_ASSERT(m_context & ItemContext);
170
171 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
172 addAction(restoreAction);
173
174 QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
175 addAction(deleteAction);
176
177 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
178 addAction(propertiesAction);
179
180 if (exec(m_pos) == restoreAction) {
181 QList<QUrl> selectedUrls;
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("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("open_in_new_window"));
222 addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
223
224 // insert 'Add to Places' entry
225 if (!placeExists(m_fileInfo.url())) {
226 addToPlacesAction = addAction(QIcon::fromTheme("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("search") || m_baseUrl.scheme().contains("timeline")) {
233 openParentAction = new QAction(QIcon::fromTheme("document-open-folder"),
234 i18nc("@action:inmenu",
235 "Open Path"),
236 this);
237 addAction(openParentAction);
238
239 openParentInNewWindowAction = new QAction(QIcon::fromTheme("window-new"),
240 i18nc("@action:inmenu",
241 "Open Path in New Window"),
242 this);
243 addAction(openParentInNewWindowAction);
244
245 openParentInNewTabAction = new QAction(QIcon::fromTheme("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("open_in_new_window"));
255 addAction(m_mainWindow->actionCollection()->action("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("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.setItems(m_selectedItems);
291 m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting());
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 }
309 } else if (activatedAction == openParentAction) {
310 m_command = OpenParentFolder;
311 } else if (activatedAction == openParentInNewWindowAction) {
312 m_command = OpenParentFolderInNewWindow;
313 } else if (activatedAction == openParentInNewTabAction) {
314 m_command = OpenParentFolderInNewTab;
315 }
316 }
317 }
318
319 void DolphinContextMenu::openViewportContextMenu()
320 {
321 // setup 'Create New' menu
322 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
323 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
324 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
325 newFileMenu->checkUpToDate();
326 newFileMenu->setPopupFiles(m_baseUrl);
327 addMenu(newFileMenu->menu());
328 addSeparator();
329
330 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
331 // "open_in_new_tab" here, as the current selection should get ignored.
332 addAction(m_mainWindow->actionCollection()->action("new_window"));
333 addAction(m_mainWindow->actionCollection()->action("new_tab"));
334
335 // Insert 'Add to Places' entry if exactly one item is selected
336 QAction* addToPlacesAction = 0;
337 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
338 addToPlacesAction = addAction(QIcon::fromTheme("bookmark-new"),
339 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
340 }
341
342 addSeparator();
343
344 QAction* pasteAction = createPasteAction();
345 addAction(pasteAction);
346 addSeparator();
347
348 // Insert service actions
349 const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
350 KFileItemActions fileItemActions;
351 fileItemActions.setItemListProperties(baseUrlProperties);
352 addServiceActions(fileItemActions);
353
354 addFileItemPluginActions();
355
356 addVersionControlPluginActions();
357
358 addCustomActions();
359
360 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
361 addAction(propertiesAction);
362
363 addShowMenuBarAction();
364
365 QAction* action = exec(m_pos);
366 if (addToPlacesAction && (action == addToPlacesAction)) {
367 const DolphinViewContainer* container = m_mainWindow->activeViewContainer();
368 if (container->url().isValid()) {
369 PlacesItemModel model;
370 PlacesItem* item = model.createPlacesItem(container->placesText(),
371 container->url());
372 model.appendItemToGroup(item);
373 }
374 }
375 }
376
377 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
378 {
379 const KActionCollection* collection = m_mainWindow->actionCollection();
380
381 // Insert 'Cut', 'Copy' and 'Paste'
382 addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
383 addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
384 addAction(createPasteAction());
385
386 addSeparator();
387
388 // Insert 'Rename'
389 QAction* renameAction = collection->action("rename");
390 addAction(renameAction);
391
392 // Insert 'Move to Trash' and/or 'Delete'
393 if (properties.supportsDeleting()) {
394 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
395 !properties.isLocal());
396 const bool showMoveToTrashAction = (properties.isLocal() &&
397 properties.supportsMoving());
398
399 if (showDeleteAction && showMoveToTrashAction) {
400 delete m_removeAction;
401 m_removeAction = 0;
402 addAction(m_mainWindow->actionCollection()->action("move_to_trash"));
403 addAction(m_mainWindow->actionCollection()->action("delete"));
404 } else if (showDeleteAction && !showMoveToTrashAction) {
405 addAction(m_mainWindow->actionCollection()->action("delete"));
406 } else {
407 if (!m_removeAction) {
408 m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
409 }
410 addAction(m_removeAction);
411 m_removeAction->update();
412 }
413 }
414 }
415
416 void DolphinContextMenu::addShowMenuBarAction()
417 {
418 const KActionCollection* ac = m_mainWindow->actionCollection();
419 QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
420 if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
421 addSeparator();
422 addAction(showMenuBar);
423 }
424 }
425
426 bool DolphinContextMenu::placeExists(const QUrl& url) const
427 {
428 PlacesItemModel model;
429
430 const int count = model.count();
431 for (int i = 0; i < count; ++i) {
432 const QUrl placeUrl = model.placesItem(i)->url();
433 if (placeUrl.matches(url, QUrl::StripTrailingSlash)) {
434 return true;
435 }
436 }
437
438 return false;
439 }
440
441 QAction* DolphinContextMenu::createPasteAction()
442 {
443 QAction* action = 0;
444 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
445 if (isDir && (m_selectedItems.count() == 1)) {
446 const QMimeData *mimeData = QApplication::clipboard()->mimeData();
447 bool canPaste;
448 const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileInfo);
449 action = new QAction(QIcon::fromTheme("edit-paste"), text, this);
450 action->setEnabled(canPaste);
451 connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
452 } else {
453 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
454 }
455
456 return action;
457 }
458
459 KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
460 {
461 if (!m_selectedItemsProperties) {
462 m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
463 }
464 return *m_selectedItemsProperties;
465 }
466
467 KFileItem DolphinContextMenu::baseFileItem()
468 {
469 if (!m_baseFileItem) {
470 m_baseFileItem = new KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_baseUrl);
471 }
472 return *m_baseFileItem;
473 }
474
475 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
476 {
477 fileItemActions.setParentWidget(m_mainWindow);
478
479 // insert 'Open With...' action or sub menu
480 fileItemActions.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
481
482 // insert 'Actions' sub menu
483 fileItemActions.addServiceActionsTo(this);
484 }
485
486 void DolphinContextMenu::addFileItemPluginActions()
487 {
488 KFileItemListProperties props;
489 if (m_selectedItems.isEmpty()) {
490 props.setItems(KFileItemList() << baseFileItem());
491 } else {
492 props = selectedItemsProperties();
493 }
494
495 QString commonMimeType = props.mimeType();
496 if (commonMimeType.isEmpty()) {
497 commonMimeType = QLatin1String("application/octet-stream");
498 }
499
500 const KService::List pluginServices = KMimeTypeTrader::self()->query(commonMimeType, "KFileItemAction/Plugin", "exist Library");
501 if (pluginServices.isEmpty()) {
502 return;
503 }
504
505 const KConfig config("kservicemenurc", KConfig::NoGlobals);
506 const KConfigGroup showGroup = config.group("Show");
507
508 foreach (const KService::Ptr& service, pluginServices) {
509 if (!showGroup.readEntry(service->desktopEntryName(), true)) {
510 // The plugin has been disabled
511 continue;
512 }
513
514 // Old API (kdelibs-4.6.0 only)
515 KFileItemActionPlugin* plugin = service->createInstance<KFileItemActionPlugin>();
516 if (plugin) {
517 plugin->setParent(this);
518 addActions(plugin->actions(props, m_mainWindow));
519 }
520 // New API (kdelibs >= 4.6.1)
521 KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance<KAbstractFileItemActionPlugin>();
522 if (abstractPlugin) {
523 abstractPlugin->setParent(this);
524 addActions(abstractPlugin->actions(props, m_mainWindow));
525 }
526 }
527 }
528
529 void DolphinContextMenu::addVersionControlPluginActions()
530 {
531 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
532 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
533 if (!versionControlActions.isEmpty()) {
534 foreach (QAction* action, versionControlActions) {
535 addAction(action);
536 }
537 addSeparator();
538 }
539 }
540
541 void DolphinContextMenu::addCustomActions()
542 {
543 foreach (QAction* action, m_customActions) {
544 addAction(action);
545 }
546 }
547