]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Revert "Fix some compile warnings"
[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 "dolphin_generalsettings.h"
24 #include "dolphinmainwindow.h"
25 #include "dolphinnewfilemenu.h"
26 #include "dolphinremoveaction.h"
27 #include "dolphinviewcontainer.h"
28 #include "panels/places/placesitem.h"
29 #include "panels/places/placesitemmodel.h"
30 #include "views/dolphinview.h"
31 #include "views/viewmodecontroller.h"
32
33 #include <KAbstractFileItemActionPlugin>
34 #include <KActionCollection>
35 #include <KFileItemActions>
36 #include <KFileItemListProperties>
37 #include <KIO/EmptyTrashJob>
38 #include <KIO/JobUiDelegate>
39 #include <KIO/Paste>
40 #include <KIO/RestoreJob>
41 #include <KJobWidgets>
42 #include <KLocalizedString>
43 #include <KMimeTypeTrader>
44 #include <KNewFileMenu>
45 #include <KPluginMetaData>
46 #include <KService>
47 #include <KStandardAction>
48 #include <KToolBar>
49
50 #include <QApplication>
51 #include <QClipboard>
52 #include <QKeyEvent>
53 #include <QMenu>
54 #include <QMenuBar>
55 #include <QMimeDatabase>
56
57 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
58 const QPoint& pos,
59 const KFileItem& fileInfo,
60 const QUrl& baseUrl) :
61 QMenu(parent),
62 m_pos(pos),
63 m_mainWindow(parent),
64 m_fileInfo(fileInfo),
65 m_baseUrl(baseUrl),
66 m_baseFileItem(nullptr),
67 m_selectedItems(),
68 m_selectedItemsProperties(nullptr),
69 m_context(NoContext),
70 m_copyToMenu(parent),
71 m_customActions(),
72 m_command(None),
73 m_removeAction(nullptr)
74 {
75 // The context menu either accesses the URLs of the selected items
76 // or the items itself. To increase the performance both lists are cached.
77 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
78 m_selectedItems = view->selectedItems();
79 }
80
81 DolphinContextMenu::~DolphinContextMenu()
82 {
83 delete m_selectedItemsProperties;
84 m_selectedItemsProperties = nullptr;
85 }
86
87 void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
88 {
89 m_customActions = actions;
90 }
91
92 DolphinContextMenu::Command DolphinContextMenu::open()
93 {
94 // get the context information
95 if (m_baseUrl.scheme() == QLatin1String("trash")) {
96 m_context |= TrashContext;
97 }
98
99 if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) {
100 m_context |= ItemContext;
101 // TODO: handle other use cases like devices + desktop files
102 }
103
104 // open the corresponding popup for the context
105 if (m_context & TrashContext) {
106 if (m_context & ItemContext) {
107 openTrashItemContextMenu();
108 } else {
109 openTrashContextMenu();
110 }
111 } else if (m_context & ItemContext) {
112 openItemContextMenu();
113 } else {
114 Q_ASSERT(m_context == NoContext);
115 openViewportContextMenu();
116 }
117
118 return m_command;
119 }
120
121 void DolphinContextMenu::keyPressEvent(QKeyEvent *ev)
122 {
123 if (m_removeAction && ev->key() == Qt::Key_Shift) {
124 m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed);
125 }
126 QMenu::keyPressEvent(ev);
127 }
128
129 void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev)
130 {
131 if (m_removeAction && ev->key() == Qt::Key_Shift) {
132 m_removeAction->update(DolphinRemoveAction::ShiftState::Released);
133 }
134 QMenu::keyReleaseEvent(ev);
135 }
136
137 void DolphinContextMenu::openTrashContextMenu()
138 {
139 Q_ASSERT(m_context & TrashContext);
140
141 QAction* emptyTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
142 KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig);
143 emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
144 addAction(emptyTrashAction);
145
146 addCustomActions();
147
148 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
149 addAction(propertiesAction);
150
151 addShowMenuBarAction();
152
153 if (exec(m_pos) == emptyTrashAction) {
154 KIO::JobUiDelegate uiDelegate;
155 uiDelegate.setWindow(m_mainWindow);
156 if (uiDelegate.askDeleteConfirmation(QList<QUrl>(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) {
157 KIO::Job* job = KIO::emptyTrash();
158 KJobWidgets::setWindow(job, m_mainWindow);
159 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
160 }
161 }
162 }
163
164 void DolphinContextMenu::openTrashItemContextMenu()
165 {
166 Q_ASSERT(m_context & TrashContext);
167 Q_ASSERT(m_context & ItemContext);
168
169 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
170 addAction(restoreAction);
171
172 QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
173 addAction(deleteAction);
174
175 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
176 addAction(propertiesAction);
177
178 if (exec(m_pos) == restoreAction) {
179 QList<QUrl> selectedUrls;
180 selectedUrls.reserve(m_selectedItems.count());
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 = nullptr;
196 QAction* openParentInNewWindowAction = nullptr;
197 QAction* openParentInNewTabAction = nullptr;
198 QAction* addToPlacesAction = nullptr;
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(QStringLiteral("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(QStringLiteral("open_in_new_window")));
221 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab")));
222
223 // insert 'Add to Places' entry
224 if (!placeExists(m_fileInfo.url())) {
225 addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("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(QStringLiteral("search")) || m_baseUrl.scheme().contains(QStringLiteral("timeline"))) {
232 openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
233 i18nc("@action:inmenu",
234 "Open Path"),
235 this);
236 addAction(openParentAction);
237
238 openParentInNewWindowAction = new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
239 i18nc("@action:inmenu",
240 "Open Path in New Window"),
241 this);
242 addAction(openParentInNewWindowAction);
243
244 openParentInNewTabAction = new QAction(QIcon::fromTheme(QStringLiteral("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(QStringLiteral("open_in_new_window")));
254 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("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(QStringLiteral("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 fileItemActions.addPluginActionsTo(this);
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(QStringLiteral("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 model.createPlacesItem(text, selectedUrl, KIO::iconNameForUrl(selectedUrl));
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(QStringLiteral("new_window")));
332 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_tab")));
333
334 // Insert 'Add to Places' entry if exactly one item is selected
335 QAction* addToPlacesAction = nullptr;
336 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
337 addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("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 fileItemActions.addPluginActionsTo(this);
354
355 addVersionControlPluginActions();
356
357 addCustomActions();
358
359 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("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 const QUrl url = container->url();
368 if (url.isValid()) {
369 PlacesItemModel model;
370 QString icon;
371 if (container->isSearchModeEnabled()) {
372 icon = QStringLiteral("folder-saved-search-symbolic");
373 } else {
374 icon = KIO::iconNameForUrl(url);
375 }
376 model.createPlacesItem(container->placesText(), url, icon);
377 }
378 }
379 }
380
381 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
382 {
383 const KActionCollection* collection = m_mainWindow->actionCollection();
384
385 // Insert 'Cut', 'Copy' and 'Paste'
386 addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
387 addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
388 addAction(createPasteAction());
389
390 addSeparator();
391
392 // Insert 'Rename'
393 addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile)));
394
395 // Insert 'Move to Trash' and/or 'Delete'
396 if (properties.supportsDeleting()) {
397 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
398 !properties.isLocal());
399 const bool showMoveToTrashAction = (properties.isLocal() &&
400 properties.supportsMoving());
401
402 if (showDeleteAction && showMoveToTrashAction) {
403 delete m_removeAction;
404 m_removeAction = nullptr;
405 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash)));
406 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
407 } else if (showDeleteAction && !showMoveToTrashAction) {
408 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
409 } else {
410 if (!m_removeAction) {
411 m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
412 }
413 addAction(m_removeAction);
414 m_removeAction->update();
415 }
416 }
417 }
418
419 void DolphinContextMenu::addShowMenuBarAction()
420 {
421 const KActionCollection* ac = m_mainWindow->actionCollection();
422 QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
423 if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
424 addSeparator();
425 addAction(showMenuBar);
426 }
427 }
428
429 bool DolphinContextMenu::placeExists(const QUrl& url) const
430 {
431 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
432 // can be expensive because the model asks Solid for the devices which are
433 // available, which can take some time.
434 // TODO: Consider restoring this check if the handling of Places and devices
435 // will be decoupled in the future.
436 return false;
437 }
438
439 QAction* DolphinContextMenu::createPasteAction()
440 {
441 QAction* action = nullptr;
442 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
443 if (isDir && (m_selectedItems.count() == 1)) {
444 const QMimeData *mimeData = QApplication::clipboard()->mimeData();
445 bool canPaste;
446 const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileInfo);
447 action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this);
448 action->setEnabled(canPaste);
449 connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
450 } else {
451 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
452 }
453
454 return action;
455 }
456
457 KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
458 {
459 if (!m_selectedItemsProperties) {
460 m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
461 }
462 return *m_selectedItemsProperties;
463 }
464
465 KFileItem DolphinContextMenu::baseFileItem()
466 {
467 if (!m_baseFileItem) {
468 m_baseFileItem = new KFileItem(m_baseUrl);
469 }
470 return *m_baseFileItem;
471 }
472
473 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
474 {
475 fileItemActions.setParentWidget(m_mainWindow);
476
477 // insert 'Open With...' action or sub menu
478 fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != 'dolphin'"));
479
480 // insert 'Actions' sub menu
481 fileItemActions.addServiceActionsTo(this);
482 }
483
484 void DolphinContextMenu::addVersionControlPluginActions()
485 {
486 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
487 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
488 if (!versionControlActions.isEmpty()) {
489 addActions(versionControlActions);
490 addSeparator();
491 }
492 }
493
494 void DolphinContextMenu::addCustomActions()
495 {
496 addActions(m_customActions);
497 }
498