X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/4eea1dc5a15f7b8acc3f0f0c2313c7ff2c029b27..d57990e681397d3dc2d2ebf737eced730dd3ecf7:/src/dolphincontextmenu.cpp diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index 58e2f4de5..e1c67aad1 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -1,102 +1,83 @@ -/*************************************************************************** - * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) and * - * Cvetoslav Ludmiloff * - * * - * This program is free software; you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation; either version 2 of the License, or * - * (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * - ***************************************************************************/ +/* + * SPDX-FileCopyrightText: 2006 Peter Penz (peter.penz@gmx.at) and Cvetoslav Ludmiloff + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ #include "dolphincontextmenu.h" +#include "dolphin_contextmenusettings.h" +#include "dolphin_generalsettings.h" #include "dolphinmainwindow.h" #include "dolphinnewfilemenu.h" -#include "dolphinviewcontainer.h" -#include "dolphin_generalsettings.h" +#include "dolphinplacesmodelsingleton.h" #include "dolphinremoveaction.h" +#include "dolphinviewcontainer.h" +#include "global.h" +#include "trash/dolphintrash.h" +#include "views/dolphinview.h" #include -#include -#include #include -#include +#include #include #include #include +#include #include -#include -#include -#include -#include #include +#include #include -#include #include #include #include -#include -#include -#include - -#include -#include - - -#include "views/dolphinview.h" -#include "views/viewmodecontroller.h" - -DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent, - const QPoint& pos, - const KFileItem& fileInfo, - const QUrl& baseUrl) : - QMenu(parent), - m_pos(pos), - m_mainWindow(parent), - m_fileInfo(fileInfo), - m_baseUrl(baseUrl), - m_baseFileItem(0), - m_selectedItems(), - m_selectedItemsProperties(0), - m_context(NoContext), - m_copyToMenu(parent), - m_customActions(), - m_command(None), - m_removeAction(0) +#include + +DolphinContextMenu::DolphinContextMenu(DolphinMainWindow *parent, + const KFileItem &fileInfo, + const KFileItemList &selectedItems, + const QUrl &baseUrl, + KFileItemActions *fileItemActions) + : QMenu(parent) + , m_mainWindow(parent) + , m_fileInfo(fileInfo) + , m_baseUrl(baseUrl) + , m_baseFileItem(nullptr) + , m_selectedItems(selectedItems) + , m_selectedItemsProperties(nullptr) + , m_context(NoContext) + , m_copyToMenu(parent) + , m_removeAction(nullptr) + , m_fileItemActions(fileItemActions) { - // The context menu either accesses the URLs of the selected items - // or the items itself. To increase the performance both lists are cached. - const DolphinView* view = m_mainWindow->activeViewContainer()->view(); - m_selectedItems = view->selectedItems(); + QApplication::instance()->installEventFilter(this); + + addAllActions(); } DolphinContextMenu::~DolphinContextMenu() { + delete m_baseFileItem; + m_baseFileItem = nullptr; delete m_selectedItemsProperties; - m_selectedItemsProperties = 0; + m_selectedItemsProperties = nullptr; } -void DolphinContextMenu::setCustomActions(const QList& actions) +void DolphinContextMenu::addAllActions() { - m_customActions = actions; -} + static_cast(m_mainWindow->actionCollection()->action(QStringLiteral("hamburger_menu")))->addToMenu(this); -DolphinContextMenu::Command DolphinContextMenu::open() -{ // get the context information - if (m_baseUrl.scheme() == QLatin1String("trash")) { + const auto scheme = m_baseUrl.scheme(); + if (scheme == QLatin1String("trash")) { m_context |= TrashContext; + } else if (scheme.contains(QLatin1String("search"))) { + m_context |= SearchContext; + } else if (scheme.contains(QLatin1String("timeline"))) { + m_context |= TimelineContext; + } else if (scheme == QStringLiteral("recentlyused")) { + m_context |= RecentlyUsedContext; } if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) { @@ -107,357 +88,366 @@ DolphinContextMenu::Command DolphinContextMenu::open() // open the corresponding popup for the context if (m_context & TrashContext) { if (m_context & ItemContext) { - openTrashItemContextMenu(); + addTrashItemContextMenu(); } else { - openTrashContextMenu(); + addTrashContextMenu(); } } else if (m_context & ItemContext) { - openItemContextMenu(); + addItemContextMenu(); } else { - Q_ASSERT(m_context == NoContext); - openViewportContextMenu(); + addViewportContextMenu(); } - - return m_command; } -void DolphinContextMenu::keyPressEvent(QKeyEvent *ev) +bool DolphinContextMenu::eventFilter(QObject *object, QEvent *event) { - if (m_removeAction && ev->key() == Qt::Key_Shift) { - m_removeAction->update(); - } - QMenu::keyPressEvent(ev); -} + Q_UNUSED(object) -void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev) -{ - if (m_removeAction && ev->key() == Qt::Key_Shift) { - m_removeAction->update(); + if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) { + QKeyEvent *keyEvent = static_cast(event); + + if (m_removeAction && keyEvent->key() == Qt::Key_Shift) { + if (event->type() == QEvent::KeyPress) { + m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed); + } else { + m_removeAction->update(DolphinRemoveAction::ShiftState::Released); + } + } } - QMenu::keyReleaseEvent(ev); + + return false; } -void DolphinContextMenu::openTrashContextMenu() +void DolphinContextMenu::addTrashContextMenu() { Q_ASSERT(m_context & TrashContext); - QAction* emptyTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this); - KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig); - emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true)); - addAction(emptyTrashAction); + QAction *emptyTrashAction = addAction(QIcon::fromTheme(QStringLiteral("edit-delete")), i18nc("@action:inmenu", "Empty Trash"), this, [this]() { + Trash::empty(m_mainWindow); + }); + emptyTrashAction->setEnabled(!Trash::isEmpty()); - addCustomActions(); + // Insert 'Sort By' and 'View Mode' + if (ContextMenuSettings::showSortBy() || ContextMenuSettings::showViewMode()) { + addSeparator(); + } + if (ContextMenuSettings::showSortBy()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("sort"))); + } + if (ContextMenuSettings::showViewMode()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode"))); + } - QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); + addSeparator(); + QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); addAction(propertiesAction); - - addShowMenuBarAction(); - - if (exec(m_pos) == emptyTrashAction) { - KIO::JobUiDelegate uiDelegate; - uiDelegate.setWindow(m_mainWindow); - if (uiDelegate.askDeleteConfirmation(QList(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) { - KIO::Job* job = KIO::emptyTrash(); - KJobWidgets::setWindow(job, m_mainWindow); - job->uiDelegate()->setAutoErrorHandlingEnabled(true); - } - } } -void DolphinContextMenu::openTrashItemContextMenu() +void DolphinContextMenu::addTrashItemContextMenu() { Q_ASSERT(m_context & TrashContext); Q_ASSERT(m_context & ItemContext); - QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow); - addAction(restoreAction); + addAction(QIcon::fromTheme(QStringLiteral("edit-reset")), + i18ncp("@action:inmenu Restore the selected files that are in the trash to the place they lived at the moment they were trashed. Minimize the " + "length of this string if possible.", + "Restore to Former Location", + "Restore to Former Locations", + m_selectedItems.count()), + this, + [this]() { + QList selectedUrls; + selectedUrls.reserve(m_selectedItems.count()); + for (const KFileItem &item : std::as_const(m_selectedItems)) { + selectedUrls.append(item.url()); + } + + KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls); + KJobWidgets::setWindow(job, m_mainWindow); + job->uiDelegate()->setAutoErrorHandlingEnabled(true); + }); + + addSeparator(); + + addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Cut))); + addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Copy))); - QAction* deleteAction = m_mainWindow->actionCollection()->action(QStringLiteral("delete")); + addSeparator(); + + QAction *deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)); addAction(deleteAction); - QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); - addAction(propertiesAction); + addSeparator(); - if (exec(m_pos) == restoreAction) { - QList selectedUrls; - selectedUrls.reserve(m_selectedItems.count()); - foreach (const KFileItem &item, m_selectedItems) { - selectedUrls.append(item.url()); - } + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("properties"))); +} + +void DolphinContextMenu::addDirectoryItemContextMenu() +{ + // insert 'Open in new window' and 'Open in new tab' entries + const KFileItemListProperties &selectedItemsProps = selectedItemsProperties(); + if (ContextMenuSettings::showOpenInNewTab()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab"))); + } + if (ContextMenuSettings::showOpenInNewWindow()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window"))); + } - KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls); - KJobWidgets::setWindow(job, m_mainWindow); - job->uiDelegate()->setAutoErrorHandlingEnabled(true); + if (ContextMenuSettings::showOpenInSplitView()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_split_view"))); } + + // Insert 'Open With' entries + addOpenWithActions(); + + // set up 'Create New' menu + QAction *newDirAction = m_mainWindow->actionCollection()->action(QStringLiteral("create_dir")); + QAction *newFileAction = m_mainWindow->actionCollection()->action(QStringLiteral("create_file")); + DolphinNewFileMenu *newFileMenu = new DolphinNewFileMenu(newDirAction, newFileAction, this); + newFileMenu->checkUpToDate(); + newFileMenu->setWorkingDirectory(m_fileInfo.url()); + newFileMenu->setEnabled(selectedItemsProps.supportsWriting()); + connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater); + connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater); + + QMenu *menu = newFileMenu->menu(); + menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New")); + menu->setIcon(QIcon::fromTheme(QStringLiteral("list-add"))); + addMenu(menu); + + addSeparator(); } -void DolphinContextMenu::openItemContextMenu() +void DolphinContextMenu::addOpenParentFolderActions() +{ + addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18nc("@action:inmenu", "Open Path"), [this]() { + const QUrl url = m_fileInfo.targetUrl(); + const QUrl parentUrl = KIO::upUrl(url); + m_mainWindow->changeUrl(parentUrl); + m_mainWindow->activeViewContainer()->view()->markUrlsAsSelected({url}); + m_mainWindow->activeViewContainer()->view()->markUrlAsCurrent(url); + }); + + addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "Open Path in New Tab"), [this]() { + const QUrl url = m_fileInfo.targetUrl(); + const QUrl parentUrl = KIO::upUrl(url); + DolphinTabPage *tabPage = m_mainWindow->openNewTab(parentUrl); + tabPage->activeViewContainer()->view()->markUrlsAsSelected({url}); + tabPage->activeViewContainer()->view()->markUrlAsCurrent(url); + }); + + addAction(QIcon::fromTheme(QStringLiteral("window-new")), i18nc("@action:inmenu", "Open Path in New Window"), [this]() { + Dolphin::openNewWindow({m_fileInfo.targetUrl()}, m_mainWindow, Dolphin::OpenNewWindowFlag::Select); + }); +} + +void DolphinContextMenu::addItemContextMenu() { Q_ASSERT(!m_fileInfo.isNull()); - QAction* openParentAction = 0; - QAction* openParentInNewWindowAction = 0; - QAction* openParentInNewTabAction = 0; - QAction* addToPlacesAction = 0; - const KFileItemListProperties& selectedItemsProps = selectedItemsProperties(); + const KFileItemListProperties &selectedItemsProps = selectedItemsProperties(); + + m_fileItemActions->setItemListProperties(selectedItemsProps); if (m_selectedItems.count() == 1) { + // single files if (m_fileInfo.isDir()) { - // setup 'Create New' menu - DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow); - const DolphinView* view = m_mainWindow->activeViewContainer()->view(); - newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown()); - newFileMenu->checkUpToDate(); - newFileMenu->setPopupFiles(m_fileInfo.url()); - newFileMenu->setEnabled(selectedItemsProps.supportsWriting()); - connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater); - connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater); - - QMenu* menu = newFileMenu->menu(); - menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New")); - menu->setIcon(QIcon::fromTheme(QStringLiteral("document-new"))); - addMenu(menu); - addSeparator(); + addDirectoryItemContextMenu(); + } else if (m_context & TimelineContext || m_context & SearchContext || m_context & RecentlyUsedContext) { + addOpenWithActions(); - // insert 'Open in new window' and 'Open in new tab' entries - addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window"))); - addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab"))); - - // insert 'Add to Places' entry - if (!placeExists(m_fileInfo.url())) { - addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")), - i18nc("@action:inmenu Add selected folder to places", - "Add to Places")); - } - - addSeparator(); - } else if (m_baseUrl.scheme().contains(QStringLiteral("search")) || m_baseUrl.scheme().contains(QStringLiteral("timeline"))) { - openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), - i18nc("@action:inmenu", - "Open Path"), - this); - addAction(openParentAction); - - openParentInNewWindowAction = new QAction(QIcon::fromTheme(QStringLiteral("window-new")), - i18nc("@action:inmenu", - "Open Path in New Window"), - this); - addAction(openParentInNewWindowAction); - - openParentInNewTabAction = new QAction(QIcon::fromTheme(QStringLiteral("tab-new")), - i18nc("@action:inmenu", - "Open Path in New Tab"), - this); - addAction(openParentInNewTabAction); + addOpenParentFolderActions(); addSeparator(); - } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo).isEmpty()) { - // insert 'Open in new window' and 'Open in new tab' entries - addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window"))); - addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab"))); - + } else { + // Insert 'Open With" entries + addOpenWithActions(); + } + if (m_fileInfo.isLink()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("show_target"))); addSeparator(); } } else { + // multiple files bool selectionHasOnlyDirs = true; - foreach (const KFileItem& item, m_selectedItems) { - const QUrl& url = DolphinView::openItemAsFolderUrl(item); + for (const auto &item : std::as_const(m_selectedItems)) { + const QUrl &url = DolphinView::openItemAsFolderUrl(item); if (url.isEmpty()) { selectionHasOnlyDirs = false; break; } } - if (selectionHasOnlyDirs) { + if (selectionHasOnlyDirs && ContextMenuSettings::showOpenInNewTab()) { // insert 'Open in new tab' entry addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tabs"))); - addSeparator(); } + // Insert 'Open With" entries + addOpenWithActions(); } insertDefaultItemActions(selectedItemsProps); - addSeparator(); - - KFileItemActions fileItemActions; - fileItemActions.setItemListProperties(selectedItemsProps); - addServiceActions(fileItemActions); - - addFileItemPluginActions(fileItemActions); - - addVersionControlPluginActions(); + addAdditionalActions(selectedItemsProps); // insert 'Copy To' and 'Move To' sub menus - if (GeneralSettings::showCopyMoveMenu()) { + if (ContextMenuSettings::showCopyMoveMenu()) { m_copyToMenu.setUrls(m_selectedItems.urlList()); m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting()); m_copyToMenu.setAutoErrorHandlingEnabled(true); m_copyToMenu.addActionsTo(this); } - // insert 'Properties...' entry - QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); - addAction(propertiesAction); + if (m_mainWindow->isSplitViewEnabledInCurrentTab()) { + if (ContextMenuSettings::showCopyToOtherSplitView()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("copy_to_inactive_split_view"))); + } - QAction* activatedAction = exec(m_pos); - if (activatedAction) { - if (activatedAction == addToPlacesAction) { - const QUrl selectedUrl(m_fileInfo.url()); - if (selectedUrl.isValid()) { - PlacesItemModel model; - const QString text = selectedUrl.fileName(); - PlacesItem* item = model.createPlacesItem(text, selectedUrl, KIO::iconNameForUrl(selectedUrl)); - model.appendItemToGroup(item); - model.saveBookmarks(); - } - } else if (activatedAction == openParentAction) { - m_command = OpenParentFolder; - } else if (activatedAction == openParentInNewWindowAction) { - m_command = OpenParentFolderInNewWindow; - } else if (activatedAction == openParentInNewTabAction) { - m_command = OpenParentFolderInNewTab; + if (ContextMenuSettings::showMoveToOtherSplitView()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("move_to_inactive_split_view"))); } } + + // insert 'Properties...' entry + addSeparator(); + QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); + addAction(propertiesAction); } -void DolphinContextMenu::openViewportContextMenu() +void DolphinContextMenu::addViewportContextMenu() { - // setup 'Create New' menu - KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu(); - const DolphinView* view = m_mainWindow->activeViewContainer()->view(); - newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown()); + const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem()); + m_fileItemActions->setItemListProperties(baseUrlProperties); + + // Set up and insert 'Create New' menu + KNewFileMenu *newFileMenu = m_mainWindow->newFileMenu(); newFileMenu->checkUpToDate(); - newFileMenu->setPopupFiles(m_baseUrl); + newFileMenu->setWorkingDirectory(m_baseUrl); addMenu(newFileMenu->menu()); - addSeparator(); - // Insert 'New Window' and 'New Tab' entries. Don't use "open_in_new_window" and - // "open_in_new_tab" here, as the current selection should get ignored. - addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_window"))); - addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_tab"))); + // Show "open with" menu items even if the dir is empty, because there are legitimate + // use cases for this, such as opening an empty dir in Kate or VSCode or something + addOpenWithActions(); - // Insert 'Add to Places' entry if exactly one item is selected - QAction* addToPlacesAction = 0; - if (!placeExists(m_mainWindow->activeViewContainer()->url())) { - addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")), - i18nc("@action:inmenu Add current folder to places", "Add to Places")); + QAction *pasteAction = createPasteAction(); + if (pasteAction) { + addAction(pasteAction); } + // Insert 'Add to Places' entry if it's not already in the places panel + if (ContextMenuSettings::showAddToPlaces() && !placeExists(m_mainWindow->activeViewContainer()->url())) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places"))); + } addSeparator(); - QAction* pasteAction = createPasteAction(); - addAction(pasteAction); - addSeparator(); - - // Insert service actions - const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem()); - KFileItemActions fileItemActions; - fileItemActions.setItemListProperties(baseUrlProperties); - addServiceActions(fileItemActions); - - addFileItemPluginActions(fileItemActions); + // Insert 'Sort By' and 'View Mode' + if (ContextMenuSettings::showSortBy()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("sort"))); + } + if (ContextMenuSettings::showViewMode()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode"))); + } + if (ContextMenuSettings::showSortBy() || ContextMenuSettings::showViewMode()) { + addSeparator(); + } - addVersionControlPluginActions(); + addAdditionalActions(baseUrlProperties); - addCustomActions(); + addSeparator(); - QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); + QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); addAction(propertiesAction); - - addShowMenuBarAction(); - - QAction* action = exec(m_pos); - if (addToPlacesAction && (action == addToPlacesAction)) { - const DolphinViewContainer* container = m_mainWindow->activeViewContainer(); - if (container->url().isValid()) { - PlacesItemModel model; - PlacesItem* item = model.createPlacesItem(container->placesText(), - container->url(), - KIO::iconNameForUrl(container->url())); - model.appendItemToGroup(item); - model.saveBookmarks(); - } - } } -void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties) +void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties &properties) { - const KActionCollection* collection = m_mainWindow->actionCollection(); + const KActionCollection *collection = m_mainWindow->actionCollection(); - // Insert 'Cut', 'Copy' and 'Paste' + // Insert 'Cut', 'Copy', 'Copy Location' and 'Paste' addAction(collection->action(KStandardAction::name(KStandardAction::Cut))); addAction(collection->action(KStandardAction::name(KStandardAction::Copy))); - addAction(createPasteAction()); + if (ContextMenuSettings::showCopyLocation()) { + QAction *copyPathAction = collection->action(QStringLiteral("copy_location")); + copyPathAction->setEnabled(m_selectedItems.size() == 1); + addAction(copyPathAction); + } + QAction *pasteAction = createPasteAction(); + if (pasteAction) { + addAction(pasteAction); + } - addSeparator(); + // Insert 'Duplicate Here' + if (ContextMenuSettings::showDuplicateHere()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("duplicate"))); + } // Insert 'Rename' - QAction* renameAction = collection->action(QStringLiteral("rename")); - addAction(renameAction); + addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile))); + + // Insert 'Add to Places' entry if appropriate + if (ContextMenuSettings::showAddToPlaces() && m_selectedItems.count() == 1 && m_fileInfo.isDir() && !placeExists(m_fileInfo.url())) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places"))); + } + + addSeparator(); // Insert 'Move to Trash' and/or 'Delete' - if (properties.supportsDeleting()) { - const bool showDeleteAction = (KSharedConfig::openConfig()->group("KDE").readEntry("ShowDeleteCommand", false) || - !properties.isLocal()); - const bool showMoveToTrashAction = (properties.isLocal() && - properties.supportsMoving()); - - if (showDeleteAction && showMoveToTrashAction) { - delete m_removeAction; - m_removeAction = 0; - addAction(m_mainWindow->actionCollection()->action(QStringLiteral("move_to_trash"))); - addAction(m_mainWindow->actionCollection()->action(QStringLiteral("delete"))); - } else if (showDeleteAction && !showMoveToTrashAction) { - addAction(m_mainWindow->actionCollection()->action(QStringLiteral("delete"))); - } else { - if (!m_removeAction) { - m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection()); - } - addAction(m_removeAction); - m_removeAction->update(); + const bool showDeleteAction = (KSharedConfig::openConfig()->group(QStringLiteral("KDE")).readEntry("ShowDeleteCommand", false) || !properties.isLocal()); + const bool showMoveToTrashAction = (properties.isLocal() && properties.supportsMoving()); + + if (showDeleteAction && showMoveToTrashAction) { + delete m_removeAction; + m_removeAction = nullptr; + addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::MoveToTrash))); + addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile))); + } else if (showDeleteAction && !showMoveToTrashAction) { + addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile))); + } else { + if (!m_removeAction) { + m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection()); } + addAction(m_removeAction); + m_removeAction->update(); } } -void DolphinContextMenu::addShowMenuBarAction() +bool DolphinContextMenu::placeExists(const QUrl &url) const { - const KActionCollection* ac = m_mainWindow->actionCollection(); - QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar)); - if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) { - addSeparator(); - addAction(showMenuBar); - } -} + const KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel(); -bool DolphinContextMenu::placeExists(const QUrl& url) const -{ - // Creating up a PlacesItemModel to find out if 'url' is one of the Places - // can be expensive because the model asks Solid for the devices which are - // available, which can take some time. - // TODO: Consider restoring this check if the handling of Places and devices - // will be decoupled in the future. - return false; + QModelIndex url_index = placesModel->closestItem(url); + return url_index.isValid() && placesModel->url(url_index).matches(url, QUrl::StripTrailingSlash); } -QAction* DolphinContextMenu::createPasteAction() +QAction *DolphinContextMenu::createPasteAction() { - QAction* action = 0; - const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir(); - if (isDir && (m_selectedItems.count() == 1)) { + QAction *action = nullptr; + KFileItem destItem; + if (!m_fileInfo.isNull() && m_selectedItems.count() <= 1) { + destItem = m_fileInfo; + } else { + destItem = baseFileItem(); + } + + if (!destItem.isNull() && destItem.isDir()) { const QMimeData *mimeData = QApplication::clipboard()->mimeData(); bool canPaste; - const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileInfo); - action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this); - action->setEnabled(canPaste); - connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder); - } else { - action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste)); + const QString text = KIO::pasteActionText(mimeData, &canPaste, destItem); + if (canPaste) { + if (destItem == m_fileInfo) { + // if paste destination is a selected folder + action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this); + connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder); + } else { + action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste)); + } + } } return action; } -KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const +KFileItemListProperties &DolphinContextMenu::selectedItemsProperties() const { if (!m_selectedItemsProperties) { m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems); @@ -468,39 +458,57 @@ KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const KFileItem DolphinContextMenu::baseFileItem() { if (!m_baseFileItem) { - m_baseFileItem = new KFileItem(m_baseUrl); + const DolphinView *view = m_mainWindow->activeViewContainer()->view(); + KFileItem baseItem = view->rootItem(); + if (baseItem.isNull() || baseItem.url() != m_baseUrl) { + m_baseFileItem = new KFileItem(m_baseUrl); + } else { + m_baseFileItem = new KFileItem(baseItem); + } } return *m_baseFileItem; } -void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions) +void DolphinContextMenu::addOpenWithActions() { - fileItemActions.setParentWidget(m_mainWindow); - // insert 'Open With...' action or sub menu - fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != 'dolphin'")); - - // insert 'Actions' sub menu - fileItemActions.addServiceActionsTo(this); + m_fileItemActions->insertOpenWithActionsTo(nullptr, this, QStringList{qApp->desktopFileName()}); + + // For a single file, hint in "Open with" menu that middle-clicking would open it in the secondary app. + // (Unless middle-clicking would open it as a folder in a new tab (e.g. archives).) + const QUrl &url = DolphinView::openItemAsFolderUrl(m_fileInfo, GeneralSettings::browseThroughArchives()); + if (m_selectedItems.count() == 1 && url.isEmpty()) { + if (QAction *openWithSubMenu = findChild(QStringLiteral("openWith_submenu"))) { + Q_ASSERT(openWithSubMenu->menu()); + Q_ASSERT(!openWithSubMenu->menu()->isEmpty()); + + auto *secondaryApp = openWithSubMenu->menu()->actions().first(); + // Add it like a keyboard shortcut, Qt uses \t as a separator. + if (!secondaryApp->text().contains(QLatin1Char('\t'))) { + secondaryApp->setText(secondaryApp->text() + QLatin1Char('\t') + + i18nc("@action:inmenu Shortcut, middle click to trigger menu item, keep short", "Middle Click")); + } + } + } } -void DolphinContextMenu::addFileItemPluginActions(KFileItemActions& fileItemActions) +void DolphinContextMenu::addAdditionalActions(const KFileItemListProperties &props) { - fileItemActions.addPluginActionsTo(this); -} + addSeparator(); -void DolphinContextMenu::addVersionControlPluginActions() -{ - const DolphinView* view = m_mainWindow->activeViewContainer()->view(); - const QList versionControlActions = view->versionControlActions(m_selectedItems); + QList additionalActions; + if (props.isLocal() && ContextMenuSettings::showOpenTerminal()) { + additionalActions << m_mainWindow->actionCollection()->action(QStringLiteral("open_terminal_here")); + } + m_fileItemActions->addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions); + + const DolphinView *view = m_mainWindow->activeViewContainer()->view(); + const QList versionControlActions = view->versionControlActions(m_selectedItems); if (!versionControlActions.isEmpty()) { + addSeparator(); addActions(versionControlActions); addSeparator(); } } -void DolphinContextMenu::addCustomActions() -{ - addActions(m_customActions); -} - +#include "moc_dolphincontextmenu.cpp"