]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Port KonqCopyToMenu to KFileCopyToMenu
[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 }
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(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 KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance<KAbstractFileItemActionPlugin>();
515 if (abstractPlugin) {
516 abstractPlugin->setParent(this);
517 addActions(abstractPlugin->actions(props, m_mainWindow));
518 }
519 }
520 }
521
522 void DolphinContextMenu::addVersionControlPluginActions()
523 {
524 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
525 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
526 if (!versionControlActions.isEmpty()) {
527 addActions(versionControlActions);
528 addSeparator();
529 }
530 }
531
532 void DolphinContextMenu::addCustomActions()
533 {
534 addActions(m_customActions);
535 }
536