]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphincontextmenu.cpp
Show a message if Konsole part is not installed
[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 "trash/dolphintrash.h"
31 #include "views/dolphinview.h"
32 #include "views/viewmodecontroller.h"
33
34 #include <KAbstractFileItemActionPlugin>
35 #include <KActionCollection>
36 #include <KFileItemActions>
37 #include <KFileItemListProperties>
38 #include <KIO/EmptyTrashJob>
39 #include <KIO/JobUiDelegate>
40 #include <KIO/Paste>
41 #include <KIO/RestoreJob>
42 #include <KJobWidgets>
43 #include <KLocalizedString>
44 #include <KMimeTypeTrader>
45 #include <KNewFileMenu>
46 #include <KPluginMetaData>
47 #include <KService>
48 #include <KStandardAction>
49 #include <KToolBar>
50
51 #include <QApplication>
52 #include <QClipboard>
53 #include <QKeyEvent>
54 #include <QMenu>
55 #include <QMenuBar>
56 #include <QMimeDatabase>
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(nullptr),
68 m_selectedItems(),
69 m_selectedItemsProperties(nullptr),
70 m_context(NoContext),
71 m_copyToMenu(parent),
72 m_customActions(),
73 m_command(None),
74 m_removeAction(nullptr)
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 = nullptr;
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(DolphinRemoveAction::ShiftState::Pressed);
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(DolphinRemoveAction::ShiftState::Released);
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(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
143 emptyTrashAction->setEnabled(!Trash::isEmpty());
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 Trash::empty(m_mainWindow);
155 }
156 }
157
158 void DolphinContextMenu::openTrashItemContextMenu()
159 {
160 Q_ASSERT(m_context & TrashContext);
161 Q_ASSERT(m_context & ItemContext);
162
163 QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
164 addAction(restoreAction);
165
166 QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
167 addAction(deleteAction);
168
169 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
170 addAction(propertiesAction);
171
172 if (exec(m_pos) == restoreAction) {
173 QList<QUrl> selectedUrls;
174 selectedUrls.reserve(m_selectedItems.count());
175 foreach (const KFileItem &item, m_selectedItems) {
176 selectedUrls.append(item.url());
177 }
178
179 KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
180 KJobWidgets::setWindow(job, m_mainWindow);
181 job->uiDelegate()->setAutoErrorHandlingEnabled(true);
182 }
183 }
184
185 void DolphinContextMenu::openItemContextMenu()
186 {
187 Q_ASSERT(!m_fileInfo.isNull());
188
189 QAction* openParentAction = nullptr;
190 QAction* openParentInNewWindowAction = nullptr;
191 QAction* openParentInNewTabAction = nullptr;
192 QAction* addToPlacesAction = nullptr;
193 const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
194
195 if (m_selectedItems.count() == 1) {
196 if (m_fileInfo.isDir()) {
197 // setup 'Create New' menu
198 DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow);
199 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
200 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
201 newFileMenu->checkUpToDate();
202 newFileMenu->setPopupFiles(m_fileInfo.url());
203 newFileMenu->setEnabled(selectedItemsProps.supportsWriting());
204 connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
205 connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater);
206
207 QMenu* menu = newFileMenu->menu();
208 menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
209 menu->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
210 addMenu(menu);
211 addSeparator();
212
213 // insert 'Open in new window' and 'Open in new tab' entries
214 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window")));
215 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab")));
216
217 // insert 'Add to Places' entry
218 if (!placeExists(m_fileInfo.url())) {
219 addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
220 i18nc("@action:inmenu Add selected folder to places",
221 "Add to Places"));
222 }
223
224 addSeparator();
225 } else if (m_baseUrl.scheme().contains(QStringLiteral("search")) || m_baseUrl.scheme().contains(QStringLiteral("timeline"))) {
226 openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
227 i18nc("@action:inmenu",
228 "Open Path"),
229 this);
230 addAction(openParentAction);
231
232 openParentInNewWindowAction = new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
233 i18nc("@action:inmenu",
234 "Open Path in New Window"),
235 this);
236 addAction(openParentInNewWindowAction);
237
238 openParentInNewTabAction = new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
239 i18nc("@action:inmenu",
240 "Open Path in New Tab"),
241 this);
242 addAction(openParentInNewTabAction);
243
244 addSeparator();
245 } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo).isEmpty()) {
246 // insert 'Open in new window' and 'Open in new tab' entries
247 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window")));
248 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab")));
249
250 addSeparator();
251 }
252 } else {
253 bool selectionHasOnlyDirs = true;
254 foreach (const KFileItem& item, m_selectedItems) {
255 const QUrl& url = DolphinView::openItemAsFolderUrl(item);
256 if (url.isEmpty()) {
257 selectionHasOnlyDirs = false;
258 break;
259 }
260 }
261
262 if (selectionHasOnlyDirs) {
263 // insert 'Open in new tab' entry
264 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
265 addSeparator();
266 }
267 }
268
269 insertDefaultItemActions(selectedItemsProps);
270
271 addSeparator();
272
273 KFileItemActions fileItemActions;
274 fileItemActions.setItemListProperties(selectedItemsProps);
275 addServiceActions(fileItemActions);
276
277 fileItemActions.addPluginActionsTo(this);
278
279 addVersionControlPluginActions();
280
281 // insert 'Copy To' and 'Move To' sub menus
282 if (GeneralSettings::showCopyMoveMenu()) {
283 m_copyToMenu.setUrls(m_selectedItems.urlList());
284 m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting());
285 m_copyToMenu.setAutoErrorHandlingEnabled(true);
286 m_copyToMenu.addActionsTo(this);
287 }
288
289 // insert 'Properties...' entry
290 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
291 addAction(propertiesAction);
292
293 QAction* activatedAction = exec(m_pos);
294 if (activatedAction) {
295 if (activatedAction == addToPlacesAction) {
296 const QUrl selectedUrl(m_fileInfo.url());
297 if (selectedUrl.isValid()) {
298 PlacesItemModel model;
299 const QString text = selectedUrl.fileName();
300 model.createPlacesItem(text, selectedUrl, KIO::iconNameForUrl(selectedUrl));
301 }
302 } else if (activatedAction == openParentAction) {
303 m_command = OpenParentFolder;
304 } else if (activatedAction == openParentInNewWindowAction) {
305 m_command = OpenParentFolderInNewWindow;
306 } else if (activatedAction == openParentInNewTabAction) {
307 m_command = OpenParentFolderInNewTab;
308 }
309 }
310 }
311
312 void DolphinContextMenu::openViewportContextMenu()
313 {
314 // setup 'Create New' menu
315 KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
316 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
317 newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
318 newFileMenu->checkUpToDate();
319 newFileMenu->setPopupFiles(m_baseUrl);
320 addMenu(newFileMenu->menu());
321 addSeparator();
322
323 // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and
324 // "open_in_new_tab" here, as the current selection should get ignored.
325 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_window")));
326 addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_tab")));
327
328 // Insert 'Add to Places' entry if exactly one item is selected
329 QAction* addToPlacesAction = nullptr;
330 if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
331 addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")),
332 i18nc("@action:inmenu Add current folder to places", "Add to Places"));
333 }
334
335 addSeparator();
336
337 QAction* pasteAction = createPasteAction();
338 addAction(pasteAction);
339 addSeparator();
340
341 // Insert service actions
342 const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
343 KFileItemActions fileItemActions;
344 fileItemActions.setItemListProperties(baseUrlProperties);
345 addServiceActions(fileItemActions);
346
347 fileItemActions.addPluginActionsTo(this);
348
349 addVersionControlPluginActions();
350
351 addCustomActions();
352
353 QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
354 addAction(propertiesAction);
355
356 addShowMenuBarAction();
357
358 QAction* action = exec(m_pos);
359 if (addToPlacesAction && (action == addToPlacesAction)) {
360 const DolphinViewContainer* container = m_mainWindow->activeViewContainer();
361 const QUrl url = container->url();
362 if (url.isValid()) {
363 PlacesItemModel model;
364 QString icon;
365 if (container->isSearchModeEnabled()) {
366 icon = QStringLiteral("folder-saved-search-symbolic");
367 } else {
368 icon = KIO::iconNameForUrl(url);
369 }
370 model.createPlacesItem(container->placesText(), url, icon);
371 }
372 }
373 }
374
375 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
376 {
377 const KActionCollection* collection = m_mainWindow->actionCollection();
378
379 // Insert 'Cut', 'Copy' and 'Paste'
380 addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
381 addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
382 addAction(createPasteAction());
383
384 addSeparator();
385
386 // Insert 'Rename'
387 addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile)));
388
389 // Insert 'Move to Trash' and/or 'Delete'
390 if (properties.supportsDeleting()) {
391 const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) ||
392 !properties.isLocal());
393 const bool showMoveToTrashAction = (properties.isLocal() &&
394 properties.supportsMoving());
395
396 if (showDeleteAction && showMoveToTrashAction) {
397 delete m_removeAction;
398 m_removeAction = nullptr;
399 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash)));
400 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
401 } else if (showDeleteAction && !showMoveToTrashAction) {
402 addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)));
403 } else {
404 if (!m_removeAction) {
405 m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection());
406 }
407 addAction(m_removeAction);
408 m_removeAction->update();
409 }
410 }
411 }
412
413 void DolphinContextMenu::addShowMenuBarAction()
414 {
415 const KActionCollection* ac = m_mainWindow->actionCollection();
416 QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
417 if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
418 addSeparator();
419 addAction(showMenuBar);
420 }
421 }
422
423 bool DolphinContextMenu::placeExists(const QUrl& url) const
424 {
425 Q_UNUSED(url)
426 // Creating up a PlacesItemModel to find out if 'url' is one of the Places
427 // can be expensive because the model asks Solid for the devices which are
428 // available, which can take some time.
429 // TODO: Consider restoring this check if the handling of Places and devices
430 // will be decoupled in the future.
431 return false;
432 }
433
434 QAction* DolphinContextMenu::createPasteAction()
435 {
436 QAction* action = nullptr;
437 const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
438 if (isDir && (m_selectedItems.count() == 1)) {
439 const QMimeData *mimeData = QApplication::clipboard()->mimeData();
440 bool canPaste;
441 const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileInfo);
442 action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this);
443 action->setEnabled(canPaste);
444 connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
445 } else {
446 action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
447 }
448
449 return action;
450 }
451
452 KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
453 {
454 if (!m_selectedItemsProperties) {
455 m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
456 }
457 return *m_selectedItemsProperties;
458 }
459
460 KFileItem DolphinContextMenu::baseFileItem()
461 {
462 if (!m_baseFileItem) {
463 m_baseFileItem = new KFileItem(m_baseUrl);
464 }
465 return *m_baseFileItem;
466 }
467
468 void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions)
469 {
470 fileItemActions.setParentWidget(m_mainWindow);
471
472 // insert 'Open With...' action or sub menu
473 fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != 'dolphin'"));
474
475 // insert 'Actions' sub menu
476 fileItemActions.addServiceActionsTo(this);
477 }
478
479 void DolphinContextMenu::addVersionControlPluginActions()
480 {
481 const DolphinView* view = m_mainWindow->activeViewContainer()->view();
482 const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
483 if (!versionControlActions.isEmpty()) {
484 addActions(versionControlActions);
485 addSeparator();
486 }
487 }
488
489 void DolphinContextMenu::addCustomActions()
490 {
491 addActions(m_customActions);
492 }
493