X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/6e88d7cef072e1b504f5d428a2ce7d8aff736a8d..957cc92f46203f1403a812cbc224123f6b5ed036:/src/dolphincontextmenu.cpp?ds=sidebyside diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index 439de930a..d15e668b1 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -1,28 +1,15 @@ -/*************************************************************************** - * 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_generalsettings.h" #include "dolphinmainwindow.h" #include "dolphinnewfilemenu.h" +#include "dolphinplacesmodelsingleton.h" #include "dolphinremoveaction.h" #include "dolphinviewcontainer.h" #include "panels/places/placesitem.h" @@ -31,7 +18,6 @@ #include "views/dolphinview.h" #include "views/viewmodecontroller.h" -#include #include #include #include @@ -41,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -51,7 +36,6 @@ #include #include #include -#include #include #include @@ -95,8 +79,13 @@ void DolphinContextMenu::setCustomActions(const QList& actions) 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; } if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) { @@ -174,7 +163,7 @@ void DolphinContextMenu::openTrashItemContextMenu() if (exec(m_pos) == restoreAction) { QList selectedUrls; selectedUrls.reserve(m_selectedItems.count()); - foreach (const KFileItem &item, m_selectedItems) { + for (const KFileItem &item : qAsConst(m_selectedItems)) { selectedUrls.append(item.url()); } @@ -184,6 +173,36 @@ void DolphinContextMenu::openTrashItemContextMenu() } } +void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions &fileItemActions) +{ + // insert 'Open in new window' and 'Open in new tab' entries + + const KFileItemListProperties& selectedItemsProps = selectedItemsProperties(); + + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab"))); + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window"))); + + // Insert 'Open With' entries + addOpenWithActions(fileItemActions); + + // set up '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(QList() << 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(); +} + void DolphinContextMenu::openItemContextMenu() { Q_ASSERT(!m_fileInfo.isNull()); @@ -191,7 +210,6 @@ void DolphinContextMenu::openItemContextMenu() QAction* openParentAction = nullptr; QAction* openParentInNewWindowAction = nullptr; QAction* openParentInNewTabAction = nullptr; - QAction* addToPlacesAction = nullptr; const KFileItemListProperties& selectedItemsProps = selectedItemsProperties(); KFileItemActions fileItemActions; @@ -199,40 +217,10 @@ void DolphinContextMenu::openItemContextMenu() fileItemActions.setItemListProperties(selectedItemsProps); if (m_selectedItems.count() == 1) { + // single files if (m_fileInfo.isDir()) { - // 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 'Open With' entries - addOpenWithActions(fileItemActions); - - // 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(); - - // set up '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(); - } else if (m_baseUrl.scheme().contains(QStringLiteral("search")) || m_baseUrl.scheme().contains(QStringLiteral("timeline"))) { + addDirectoryItemContextMenu(fileItemActions); + } else if (m_context & TimelineContext || m_context & SearchContext) { addOpenWithActions(fileItemActions); openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), @@ -253,15 +241,6 @@ void DolphinContextMenu::openItemContextMenu() this); addAction(openParentInNewTabAction); - addSeparator(); - } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo).isEmpty()) { - // Insert 'Open With" entries - addOpenWithActions(fileItemActions); - - // 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"))); - addSeparator(); } else { // Insert 'Open With" entries @@ -272,8 +251,9 @@ void DolphinContextMenu::openItemContextMenu() addSeparator(); } } else { + // multiple files bool selectionHasOnlyDirs = true; - foreach (const KFileItem& item, m_selectedItems) { + for (const auto &item : qAsConst(m_selectedItems)) { const QUrl& url = DolphinView::openItemAsFolderUrl(item); if (url.isEmpty()) { selectionHasOnlyDirs = false; @@ -291,6 +271,15 @@ void DolphinContextMenu::openItemContextMenu() insertDefaultItemActions(selectedItemsProps); + // insert 'Add to Places' entry if appropriate + if (m_selectedItems.count() == 1) { + if (m_fileInfo.isDir()) { + if (!placeExists(m_fileInfo.url())) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places"))); + } + } + } + addSeparator(); fileItemActions.addServiceActionsTo(this); @@ -313,14 +302,7 @@ void DolphinContextMenu::openItemContextMenu() 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(); - model.createPlacesItem(text, selectedUrl, KIO::iconNameForUrl(selectedUrl)); - } - } else if (activatedAction == openParentAction) { + if (activatedAction == openParentAction) { m_command = OpenParentFolder; } else if (activatedAction == openParentInNewWindowAction) { m_command = OpenParentFolderInNewWindow; @@ -332,14 +314,7 @@ void DolphinContextMenu::openItemContextMenu() void DolphinContextMenu::openViewportContextMenu() { - // setup 'Create New' menu - KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu(); const DolphinView* view = m_mainWindow->activeViewContainer()->view(); - newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown()); - newFileMenu->checkUpToDate(); - newFileMenu->setPopupFiles(m_baseUrl); - addMenu(newFileMenu->menu()); - addSeparator(); // Insert 'Open With' entries KFileItem baseItem = view->rootItem(); @@ -351,24 +326,25 @@ void DolphinContextMenu::openViewportContextMenu() KFileItemActions fileItemActions; fileItemActions.setParentWidget(m_mainWindow); fileItemActions.setItemListProperties(baseUrlProperties); - addOpenWithActions(fileItemActions); - // 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("file_new"))); - addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_tab"))); - - // Insert 'Add to Places' entry if exactly one item is selected - QAction* addToPlacesAction = nullptr; - if (!placeExists(m_mainWindow->activeViewContainer()->url())) { - addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")), - i18nc("@action:inmenu Add current folder to places", "Add to Places")); - } + // Set up and insert 'Create New' menu + KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu(); + newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown()); + newFileMenu->checkUpToDate(); + newFileMenu->setPopupFiles(QList() << m_baseUrl); + addMenu(newFileMenu->menu()); - addSeparator(); + // 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(fileItemActions); QAction* pasteAction = createPasteAction(); addAction(pasteAction); + + // Insert 'Add to Places' entry if it's not already in the places panel + if (!placeExists(m_mainWindow->activeViewContainer()->url())) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("add_to_places"))); + } addSeparator(); // Insert 'Sort By' and 'View Mode' @@ -385,36 +361,28 @@ void DolphinContextMenu::openViewportContextMenu() addCustomActions(); + addSeparator(); + 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(); - const QUrl url = container->url(); - if (url.isValid()) { - PlacesItemModel model; - QString icon; - if (container->isSearchModeEnabled()) { - icon = QStringLiteral("folder-saved-search-symbolic"); - } else { - icon = KIO::iconNameForUrl(url); - } - model.createPlacesItem(container->placesText(), url, icon); - } - } + exec(m_pos); } void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties) { 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))); + QAction* copyPathAction = collection->action(QString("copy_location")); + copyPathAction->setEnabled(m_selectedItems.size() == 1); + addAction(copyPathAction); addAction(createPasteAction()); + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("duplicate"))); addSeparator(); @@ -455,13 +423,11 @@ void DolphinContextMenu::addShowMenuBarAction() bool DolphinContextMenu::placeExists(const QUrl& url) const { - Q_UNUSED(url) - // 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; + const KFilePlacesModel* placesModel = DolphinPlacesModelSingleton::instance().placesModel(); + + const auto& matchedPlaces = placesModel->match(placesModel->index(0,0), KFilePlacesModel::UrlRole, url, 1, Qt::MatchExactly); + + return !matchedPlaces.isEmpty(); } QAction* DolphinContextMenu::createPasteAction()