]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
0ecd7376d5688fbf7ba87ba45daecb77b61fb03a
[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 <QKeyEvent>
50 #include <QMenuBar>
51 #include <QMenu>
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(0),
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 = 0;
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();
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();
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("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this);
145 KConfig trashConfig("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("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->ui()->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("delete");
176 addAction(deleteAction);
177
178 QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
179 addAction(propertiesAction);
180
181 if (exec(m_pos) == restoreAction) {
182 QList<QUrl> selectedUrls;
183 foreach (const KFileItem &item, m_selectedItems) {
184 selectedUrls.append(item.url());
185 }
186
187 KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
188 KJobWidgets::setWindow(job, m_mainWindow);
189 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
190 }
191 }
192
193 void DolphinContextMenu::openItemContextMenu()
194 {
195 Q_ASSERT(!m_fileInfo.isNull());
196
197 QAction* openParentAction = 0;
198 QAction* openParentInNewWindowAction = 0;
199 QAction* openParentInNewTabAction = 0;
200 QAction* addToPlacesAction = 0;
201 const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
202
203 if (m_selectedItems.count() == 1) {
204 if (m_fileInfo.isDir()) {
205 // setup 'Create New' menu
206 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow);
207 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
208 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
209 newFileMenu->checkUpToDate();
210 newFileMenu->setPopupFiles(m_fileInfo.url());
211 newFileMenu->setEnabled(selectedItemsProps.supportsWriting());
212 connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
213 connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
214
215 QMenu* menu = newFileMenu->menu();
216 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
217 menu->setIcon(QIcon::fromTheme("document-new"));
218 addMenu(menu);
219 addSeparator();
220
221 // insert 'Open in new window' and 'Open in new tab' entries
222 addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
223 addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
224
225 // insert 'Add to Places' entry
226 if (!placeExists(m_fileInfo.url())) {
227 addToPlacesAction = addAction(QIcon::fromTheme("bookmark-new"),
228 i18nc("@action:inmenu Add selected folder to places",
229 "Add to Places"));
230 }
231
232 addSeparator();
233 } else if (m_baseUrl.scheme().contains("search") || m_baseUrl.scheme().contains("timeline")) {
234 openParentAction = new QAction(QIcon::fromTheme("document-open-folder"),
235 i18nc("@action:inmenu",
236 "Open Path"),
237 this);
238 addAction(openParentAction);
239
240 openParentInNewWindowAction = new QAction(QIcon::fromTheme("window-new"),
241 i18nc("@action:inmenu",
242 "Open Path in New Window"),
243 this);
244 addAction(openParentInNewWindowAction);
245
246 openParentInNewTabAction = new QAction(QIcon::fromTheme("tab-new"),
247 i18nc("@action:inmenu",
248 "Open Path in New Tab"),
249 this);
250 addAction(openParentInNewTabAction);
251
252 addSeparator();
253 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo).isEmpty()) {
254 // insert 'Open in new window' and 'Open in new tab' entries
255 addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
256 addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
257
258 addSeparator();
259 }
260 } else {
261 bool selectionHasOnlyDirs = true;
262 foreach (const KFileItem& item, m_selectedItems) {
263 const QUrl& url = DolphinView::openItemAsFolderUrl(item);
264 if (url.isEmpty()) {
265 selectionHasOnlyDirs = false;
266 break;
267 }
268 }
269
270 if (selectionHasOnlyDirs) {
271 // insert 'Open in new tab' entry
272 addAction(m_mainWindow->actionCollection()->action("open_in_new_tabs"));
273 addSeparator();
274 }
275 }
276
277 insertDefaultItemActions(selectedItemsProps);
278
279 addSeparator();
280
281 KFileItemActions fileItemActions;
282 fileItemActions.setItemListProperties(selectedItemsProps);
283 addServiceActions(fileItemActions);
284
285 addFileItemPluginActions();
286
287 addVersionControlPluginActions();
288
289 // insert 'Copy To' and 'Move To' sub menus
290 if (GeneralSettings::showCopyMoveMenu()) {
291 m_copyToMenu.setItems(m_selectedItems);
292 m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting());
293 m_copyToMenu.addActionsTo(this);
294 }
295
296 // insert 'Properties...' entry
297 QAction* propertiesAction = m_mainWindow->actionCollection()->action("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 }
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 }
375 }
376 }
377
378 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
379 {
380 const KActionCollection* collection = m_mainWindow->actionCollection();
381
382 // Insert 'Cut', 'Copy' and 'Paste'
383 addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
384 addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
385 addAction(createPasteAction());
386
387 addSeparator();
388
389 // Insert 'Rename'
390 QAction* renameAction = collection->action("rename");
391 addAction(renameAction);
392
393 // Insert 'Move to Trash' and/or 'Delete'
394 if (properties.supportsDeleting()) {
395 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
396 !properties.isLocal());
397 const bool showMoveToTrashAction = (properties.isLocal() &&
398 properties.supportsMoving());
399
400 if (showDeleteAction && showMoveToTrashAction) {
401 delete m_removeAction;
402 m_removeAction = 0;
403 addAction(m_mainWindow->actionCollection()->action("move_to_trash"));
404 addAction(m_mainWindow->actionCollection()->action("delete"));
405 } else if (showDeleteAction && !showMoveToTrashAction) {
406 addAction(m_mainWindow->actionCollection()->action("delete"));
407 } else {
408 if (!m_removeAction) {
409 m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
410 }
411 addAction(m_removeAction);
412 m_removeAction->update();
413 }
414 }
415 }
416
417 void DolphinContextMenu::addShowMenuBarAction()
418 {
419 const KActionCollection* ac = m_mainWindow->actionCollection();
420 QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
421 if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
422 addSeparator();
423 addAction(showMenuBar);
424 }
425 }
426
427 bool DolphinContextMenu::placeExists(const QUrl& url) const
428 {
429 PlacesItemModel model;
430
431 const int count = model.count();
432 for (int i = 0; i < count; ++i) {
433 const QUrl placeUrl = model.placesItem(i)->url();
434 if (placeUrl.matches(url, QUrl::StripTrailingSlash)) {
435 return true;
436 }
437 }
438
439 return false;
440 }
441
442 QAction* DolphinContextMenu::createPasteAction()
443 {
444 QAction* action = 0;
445 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
446 if (isDir && (m_selectedItems.count() == 1)) {
447 const QMimeData *mimeData = QApplication::clipboard()->mimeData();
448 bool canPaste;
449 const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileInfo);
450 action = new QAction(QIcon::fromTheme("edit-paste"), text, this);
451 action->setEnabled(canPaste);
452 connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
453 } else {
454 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
455 }
456
457 return action;
458 }
459
460 KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
461 {
462 if (!m_selectedItemsProperties) {
463 m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
464 }
465 return *m_selectedItemsProperties;
466 }
467
468 KFileItem DolphinContextMenu::baseFileItem()
469 {
470 if (!m_baseFileItem) {
471 m_baseFileItem = new KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_baseUrl);
472 }
473 return *m_baseFileItem;
474 }
475
476 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
477 {
478 fileItemActions.setParentWidget(m_mainWindow);
479
480 // insert 'Open With...' action or sub menu
481 fileItemActions.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'");
482
483 // insert 'Actions' sub menu
484 fileItemActions.addServiceActionsTo(this);
485 }
486
487 void DolphinContextMenu::addFileItemPluginActions()
488 {
489 KFileItemListProperties props;
490 if (m_selectedItems.isEmpty()) {
491 props.setItems(KFileItemList() << baseFileItem());
492 } else {
493 props = selectedItemsProperties();
494 }
495
496 QString commonMimeType = props.mimeType();
497 if (commonMimeType.isEmpty()) {
498 commonMimeType = QLatin1String("application/octet-stream");
499 }
500
501 const KService::List pluginServices = KMimeTypeTrader::self()->query(commonMimeType, "KFileItemAction/Plugin", "exist Library");
502 if (pluginServices.isEmpty()) {
503 return;
504 }
505
506 const KConfig config("kservicemenurc", KConfig::NoGlobals);
507 const KConfigGroup showGroup = config.group("Show");
508
509 foreach (const KService::Ptr& service, pluginServices) {
510 if (!showGroup.readEntry(service->desktopEntryName(), true)) {
511 // The plugin has been disabled
512 continue;
513 }
514
515 // Old API (kdelibs-4.6.0 only)
516 KFileItemActionPlugin* plugin = service->createInstance<KFileItemActionPlugin>();
517 if (plugin) {
518 plugin->setParent(this);
519 addActions(plugin->actions(props, m_mainWindow));
520 }
521 // New API (kdelibs >= 4.6.1)
522 KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance<KAbstractFileItemActionPlugin>();
523 if (abstractPlugin) {
524 abstractPlugin->setParent(this);
525 addActions(abstractPlugin->actions(props, m_mainWindow));
526 }
527 }
528 }
529
530 void DolphinContextMenu::addVersionControlPluginActions()
531 {
532 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
533 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
534 if (!versionControlActions.isEmpty()) {
535 foreach (QAction* action, versionControlActions) {
536 addAction(action);
537 }
538 addSeparator();
539 }
540 }
541
542 void DolphinContextMenu::addCustomActions()
543 {
544 foreach (QAction* action, m_customActions) {
545 addAction(action);
546 }
547 }
548