]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphincontextmenu.cpp
Merge branch 'master' into kf6
[dolphin.git] / src / dolphincontextmenu.cpp
index 539c280353a193f104a67980b5e158a6b5496b2b..e1c7b7ad2f83d3bfb31c11c4b608f0f18e248b12 100644 (file)
-/***************************************************************************
- *   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 "dolphinplacesmodelsingleton.h"
+#include "dolphinremoveaction.h"
 #include "dolphinviewcontainer.h"
-#include "dolphin_generalsettings.h"
+#include "global.h"
+#include "trash/dolphintrash.h"
+#include "views/dolphinview.h"
+#include "views/viewmodecontroller.h"
 
 #include <KActionCollection>
-#include <KDesktopFile>
-#include <kfileitemactionplugin.h>
-#include <kabstractfileitemactionplugin.h>
-#include <KFileItemActions>
 #include <KFileItemListProperties>
-#include <KGlobal>
-#include <KIconLoader>
-#include <KIO/NetAccess>
-#include <KMenu>
-#include <KMenuBar>
-#include <KMessageBox>
-#include <KMimeTypeTrader>
-#include <KModifierKeyInfo>
+#include <KHamburgerMenu>
+#include <KIO/EmptyTrashJob>
+#include <KIO/JobUiDelegate>
+#include <KIO/Paste>
+#include <KIO/RestoreJob>
+#include <KJobWidgets>
+#include <KLocalizedString>
 #include <KNewFileMenu>
-#include <konqmimedata.h>
-#include <konq_operations.h>
-#include <KService>
-#include <KLocale>
-#include <KPropertiesDialog>
 #include <KStandardAction>
-#include <KStandardDirs>
-#include <KToolBar>
-
-#include <panels/places/placesitem.h>
-#include <panels/places/placesitemmodel.h>
 
 #include <QApplication>
 #include <QClipboard>
-#include <QDir>
-
-#include "views/dolphinview.h"
-#include "views/viewmodecontroller.h"
-
-K_GLOBAL_STATIC(KModifierKeyInfo, m_keyInfo)
-
-DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
-                                       const QPoint& pos,
-                                       const KFileItem& fileInfo,
-                                       const KUrl& baseUrl) :
-    QObject(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_popup(0),
-    m_command(None),
-    m_shiftPressed(false),
-    m_removeAction(0)
+#include <QKeyEvent>
+
+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();
-
-    if (m_keyInfo) {
-        if (m_keyInfo->isKeyPressed(Qt::Key_Shift) || m_keyInfo->isKeyLatched(Qt::Key_Shift)) {
-            m_shiftPressed = true;
-        }
-        connect(m_keyInfo, SIGNAL(keyPressed(Qt::Key,bool)),
-                this, SLOT(slotKeyModifierPressed(Qt::Key,bool)));
-    }
-
-    m_removeAction = new QAction(this);
-    connect(m_removeAction, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered()));
+    QApplication::instance()->installEventFilter(this);
 
-    m_popup = new KMenu(m_mainWindow);
+    addAllActions();
 }
 
 DolphinContextMenu::~DolphinContextMenu()
 {
+    delete m_baseFileItem;
+    m_baseFileItem = nullptr;
     delete m_selectedItemsProperties;
-    m_selectedItemsProperties = 0;
-
-    delete m_popup;
-    m_popup = 0;
+    m_selectedItemsProperties = nullptr;
 }
 
-void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
+void DolphinContextMenu::addAllActions()
 {
-    m_customActions = actions;
-}
+    static_cast<KHamburgerMenu *>(m_mainWindow->actionCollection()->action(QStringLiteral("hamburger_menu")))->addToMenu(this);
 
-DolphinContextMenu::Command DolphinContextMenu::open()
-{
     // get the context information
-    if (m_baseUrl.protocol() == 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()) {
@@ -129,318 +86,323 @@ 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::initializeModifierKeyInfo()
+bool DolphinContextMenu::eventFilter(QObject *object, QEvent *event)
 {
-    // Access m_keyInfo, so that it gets instantiated by
-    // K_GLOBAL_STATIC
-    KModifierKeyInfo* keyInfo = m_keyInfo;
-    Q_UNUSED(keyInfo);
-}
+    Q_UNUSED(object)
 
-void DolphinContextMenu::slotKeyModifierPressed(Qt::Key key, bool pressed)
-{
-    m_shiftPressed = (key == Qt::Key_Shift) && pressed;
-    updateRemoveAction();
-}
+    if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
+        QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
 
-void DolphinContextMenu::slotRemoveActionTriggered()
-{
-    const KActionCollection* collection = m_mainWindow->actionCollection();
-    if (moveToTrash()) {
-        collection->action("move_to_trash")->trigger();
-    } else {
-        collection->action("delete")->trigger();
+        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);
+            }
+        }
     }
+
+    return false;
 }
 
-void DolphinContextMenu::openTrashContextMenu()
+void DolphinContextMenu::addTrashContextMenu()
 {
     Q_ASSERT(m_context & TrashContext);
 
-    QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), m_popup);
-    KConfig trashConfig("trashrc", KConfig::SimpleConfig);
-    emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true));
-    m_popup->addAction(emptyTrashAction);
-
-    addCustomActions();
-
-    QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
-    m_popup->addAction(propertiesAction);
-
-    addShowMenuBarAction();
+    QAction *emptyTrashAction = addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), [this]() {
+        Trash::empty(m_mainWindow);
+    });
+    emptyTrashAction->setEnabled(!Trash::isEmpty());
 
-    if (m_popup->exec(m_pos) == emptyTrashAction) {
-        KonqOperations::emptyTrash(m_mainWindow);
-    }
+    QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
+    addAction(propertiesAction);
 }
 
-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);
-    m_popup->addAction(restoreAction);
+    addAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), [this]() {
+        QList<QUrl> selectedUrls;
+        selectedUrls.reserve(m_selectedItems.count());
+        for (const KFileItem &item : qAsConst(m_selectedItems)) {
+            selectedUrls.append(item.url());
+        }
 
-    QAction* deleteAction = m_mainWindow->actionCollection()->action("delete");
-    m_popup->addAction(deleteAction);
+        KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
+        KJobWidgets::setWindow(job, m_mainWindow);
+        job->uiDelegate()->setAutoErrorHandlingEnabled(true);
+    });
 
-    QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
-    m_popup->addAction(propertiesAction);
+    QAction *deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
+    addAction(deleteAction);
 
-    if (m_popup->exec(m_pos) == restoreAction) {
-        KUrl::List selectedUrls;
-        foreach (const KFileItem &item, m_selectedItems) {
-            selectedUrls.append(item.url());
-        }
+    QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
+    addAction(propertiesAction);
+}
 
-        KonqOperations::restoreTrashedItems(selectedUrls, m_mainWindow);
+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")));
     }
+
+    // Insert 'Open With' entries
+    addOpenWithActions();
+
+    // set up 'Create New' menu
+    DolphinNewFileMenu *newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection()->action(QStringLiteral("create_dir")), m_mainWindow);
+    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::addItemContextMenu()
 {
     Q_ASSERT(!m_fileInfo.isNull());
 
-    QAction* openParentInNewWindowAction = 0;
-    QAction* openParentInNewTabAction = 0;
-    QAction* addToPlacesAction = 0;
+    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);
-            const DolphinView* view = m_mainWindow->activeViewContainer()->view();
-            newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
-            newFileMenu->checkUpToDate();
-            newFileMenu->setPopupFiles(m_fileInfo.url());
-            newFileMenu->setEnabled(selectedItemsProperties().supportsWriting());
-            connect(newFileMenu, SIGNAL(fileCreated(KUrl)), newFileMenu, SLOT(deleteLater()));
-            connect(newFileMenu, SIGNAL(directoryCreated(KUrl)), newFileMenu, SLOT(deleteLater()));
-
-            KMenu* menu = newFileMenu->menu();
-            menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
-            menu->setIcon(KIcon("document-new"));
-            m_popup->addMenu(menu);
-            m_popup->addSeparator();
-
-            // insert 'Open in new window' and 'Open in new tab' entries
-            m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_window"));
-            m_popup->addAction(m_mainWindow->actionCollection()->action("open_in_new_tab"));
-
-            // insert 'Add to Places' entry
-            if (!placeExists(m_fileInfo.url())) {
-                addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
-                                                       i18nc("@action:inmenu Add selected folder to places",
-                                                             "Add to Places"));
-            }
+            addDirectoryItemContextMenu();
+        } else if (m_context & TimelineContext || m_context & SearchContext) {
+            addOpenWithActions();
 
-            m_popup->addSeparator();
-        } else if (m_baseUrl.protocol().contains("search")) {
-            openParentInNewWindowAction = new QAction(KIcon("window-new"),
-                                                    i18nc("@action:inmenu",
-                                                          "Open Path in New Window"),
-                                                    this);
-            m_popup->addAction(openParentInNewWindowAction);
-
-            openParentInNewTabAction = new QAction(KIcon("tab-new"),
-                                                   i18nc("@action:inmenu",
-                                                         "Open Path in New Tab"),
-                                                   this);
-            m_popup->addAction(openParentInNewTabAction);
-
-            m_popup->addSeparator();
-        }
-    }
+            addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18nc("@action:inmenu", "Open Path"), [this]() {
+                m_mainWindow->changeUrl(KIO::upUrl(m_fileInfo.url()));
+                m_mainWindow->activeViewContainer()->view()->markUrlsAsSelected({m_fileInfo.url()});
+                m_mainWindow->activeViewContainer()->view()->markUrlAsCurrent(m_fileInfo.url());
+            });
 
-    insertDefaultItemActions();
+            addAction(QIcon::fromTheme(QStringLiteral("window-new")), i18nc("@action:inmenu", "Open Path in New Window"), [this]() {
+                Dolphin::openNewWindow({m_fileInfo.url()}, m_mainWindow, Dolphin::OpenNewWindowFlag::Select);
+            });
 
-    m_popup->addSeparator();
+            addAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "Open Path in New Tab"), [this]() {
+                m_mainWindow->openNewTab(KIO::upUrl(m_fileInfo.url()));
+            });
 
-    KFileItemActions fileItemActions;
-    fileItemActions.setItemListProperties(selectedItemsProperties());
-    addServiceActions(fileItemActions);
+            addSeparator();
+        } 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;
+        for (const auto &item : qAsConst(m_selectedItems)) {
+            const QUrl &url = DolphinView::openItemAsFolderUrl(item);
+            if (url.isEmpty()) {
+                selectionHasOnlyDirs = false;
+                break;
+            }
+        }
 
-    addFileItemPluginActions();
+        if (selectionHasOnlyDirs && ContextMenuSettings::showOpenInNewTab()) {
+            // insert 'Open in new tab' entry
+            addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
+        }
+        // Insert 'Open With" entries
+        addOpenWithActions();
+    }
 
-    addVersionControlPluginActions();
+    insertDefaultItemActions(selectedItemsProps);
+
+    addAdditionalActions(selectedItemsProps);
 
     // insert 'Copy To' and 'Move To' sub menus
-    if (GeneralSettings::showCopyMoveMenu()) {
-        m_copyToMenu.setItems(m_selectedItems);
-        m_copyToMenu.setReadOnly(!selectedItemsProperties().supportsWriting());
-        m_copyToMenu.addActionsTo(m_popup);
+    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("properties");
-    m_popup->addAction(propertiesAction);
-
-    QAction* activatedAction = m_popup->exec(m_pos);
-    if (activatedAction) {
-        if (activatedAction == addToPlacesAction) {
-            const KUrl selectedUrl(m_fileInfo.url());
-            if (selectedUrl.isValid()) {
-                PlacesItemModel model;
-                PlacesItem* item = model.createPlacesItem(placesName(selectedUrl),
-                                                          selectedUrl);
-                model.appendItemToGroup(item);
-            }
-        } else if (activatedAction == openParentInNewWindowAction) {
-            m_command = OpenParentFolderInNewWindow;
-        } else if (activatedAction == openParentInNewTabAction) {
-            m_command = OpenParentFolderInNewTab;
+    if (m_mainWindow->isSplitViewEnabledInCurrentTab()) {
+        if (ContextMenuSettings::showCopyToOtherSplitView()) {
+            addAction(m_mainWindow->actionCollection()->action(QStringLiteral("copy_to_inactive_split_view")));
+        }
+
+        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());
-    newFileMenu->checkUpToDate();
-    newFileMenu->setPopupFiles(m_baseUrl);
-    m_popup->addMenu(newFileMenu->menu());
-    m_popup->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.
-    m_popup->addAction(m_mainWindow->actionCollection()->action("new_window"));
-    m_popup->addAction(m_mainWindow->actionCollection()->action("new_tab"));
-
-    // Insert 'Add to Places' entry if exactly one item is selected
-    QAction* addToPlacesAction = 0;
-    if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
-        addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"),
-                                             i18nc("@action:inmenu Add current folder to places", "Add to Places"));
-    }
-
-    m_popup->addSeparator();
+    const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
+    m_fileItemActions->setItemListProperties(baseUrlProperties);
 
-    QAction* pasteAction = createPasteAction();
-    m_popup->addAction(pasteAction);
-    m_popup->addSeparator();
+    // Set up and insert 'Create New' menu
+    KNewFileMenu *newFileMenu = m_mainWindow->newFileMenu();
+    newFileMenu->checkUpToDate();
+    newFileMenu->setWorkingDirectory(m_baseUrl);
+    addMenu(newFileMenu->menu());
 
-    // Insert service actions
-    const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
-    KFileItemActions fileItemActions;
-    fileItemActions.setItemListProperties(baseUrlProperties);
-    addServiceActions(fileItemActions);
+    // 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();
 
-    addFileItemPluginActions();
+    QAction *pasteAction = createPasteAction();
+    if (pasteAction) {
+        addAction(pasteAction);
+    }
 
-    addVersionControlPluginActions();
+    // 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();
 
-    addCustomActions();
+    // 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();
+    }
 
-    QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties");
-    m_popup->addAction(propertiesAction);
+    addAdditionalActions(baseUrlProperties);
 
-    addShowMenuBarAction();
+    addSeparator();
 
-    QAction* action = m_popup->exec(m_pos);
-    if (addToPlacesAction && (action == addToPlacesAction)) {
-        const KUrl url = m_mainWindow->activeViewContainer()->url();
-        if (url.isValid()) {
-            PlacesItemModel model;
-            PlacesItem* item = model.createPlacesItem(placesName(url), url);
-            model.appendItemToGroup(item);
-        }
-    }
+    QAction *propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
+    addAction(propertiesAction);
 }
 
-void DolphinContextMenu::insertDefaultItemActions()
+void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties &properties)
 {
-    const KActionCollection* collection = m_mainWindow->actionCollection();
-
-    // Insert 'Cut', 'Copy' and 'Paste'
-    m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
-    m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
-    m_popup->addAction(createPasteAction());
+    const KActionCollection *collection = m_mainWindow->actionCollection();
+
+    // Insert 'Cut', 'Copy', 'Copy Location' and 'Paste'
+    addAction(collection->action(KStandardAction::name(KStandardAction::Cut)));
+    addAction(collection->action(KStandardAction::name(KStandardAction::Copy)));
+    if (ContextMenuSettings::showCopyLocation()) {
+        QAction *copyPathAction = collection->action(QString("copy_location"));
+        copyPathAction->setEnabled(m_selectedItems.size() == 1);
+        addAction(copyPathAction);
+    }
+    QAction *pasteAction = createPasteAction();
+    if (pasteAction) {
+        addAction(pasteAction);
+    }
 
-    m_popup->addSeparator();
+    // Insert 'Duplicate Here'
+    if (ContextMenuSettings::showDuplicateHere()) {
+        addAction(m_mainWindow->actionCollection()->action(QStringLiteral("duplicate")));
+    }
 
     // Insert 'Rename'
-    QAction* renameAction = collection->action("rename");
-    m_popup->addAction(renameAction);
+    addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile)));
 
-    // Insert 'Move to Trash' and/or 'Delete'
-    if (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false)) {
-        m_popup->addAction(collection->action("move_to_trash"));
-        m_popup->addAction(collection->action("delete"));
-    } else {
-        m_popup->addAction(m_removeAction);
-        updateRemoveAction();
+    // 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")));
     }
-}
 
-void DolphinContextMenu::addShowMenuBarAction()
-{
-    const KActionCollection* ac = m_mainWindow->actionCollection();
-    QAction* showMenuBar = ac->action(KStandardAction::name(KStandardAction::ShowMenubar));
-    if (!m_mainWindow->menuBar()->isVisible() && !m_mainWindow->toolBar()->isVisible()) {
-        m_popup->addSeparator();
-        m_popup->addAction(showMenuBar);
-    }
-}
+    addSeparator();
 
-QString DolphinContextMenu::placesName(const KUrl& url) const
-{
-    QString name = url.fileName();
-    if (name.isEmpty()) {
-        name = url.host();
+    // Insert 'Move to Trash' and/or 'Delete'
+    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 = 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();
     }
-    return name;
 }
 
-bool DolphinContextMenu::placeExists(const KUrl& url) const
+bool DolphinContextMenu::placeExists(const QUrl &url) const
 {
-    PlacesItemModel model;
+    const KFilePlacesModel *placesModel = DolphinPlacesModelSingleton::instance().placesModel();
 
-    const int count = model.count();
-    for (int i = 0; i < count; ++i) {
-        const KUrl placeUrl = model.placesItem(i)->url();
-        if (placeUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
-            return true;
-        }
-    }
+    const auto &matchedPlaces = placesModel->match(placesModel->index(0, 0), KFilePlacesModel::UrlRole, url, 1, Qt::MatchExactly);
 
-    return false;
+    return !matchedPlaces.isEmpty();
 }
 
-QActionDolphinContextMenu::createPasteAction()
+QAction *DolphinContextMenu::createPasteAction()
 {
-    QAction* action = 0;
-    const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
-    if (isDir && (m_selectedItems.count() == 1)) {
-        action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this);
-        const QMimeData* mimeData = QApplication::clipboard()->mimeData();
-        const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData);
-        action->setEnabled(!pasteData.isEmpty() && selectedItemsProperties().supportsWriting());
-        connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder()));
+    QAction *action = nullptr;
+    KFileItem destItem;
+    if (!m_fileInfo.isNull() && m_selectedItems.count() <= 1) {
+        destItem = m_fileInfo;
     } else {
-        action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
+        destItem = baseFileItem();
+    }
+
+    if (!destItem.isNull() && destItem.isDir()) {
+        const QMimeData *mimeData = QApplication::clipboard()->mimeData();
+        bool canPaste;
+        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;
 }
 
-KFileItemListPropertiesDolphinContextMenu::selectedItemsProperties() const
+KFileItemListProperties &DolphinContextMenu::selectedItemsProperties() const
 {
     if (!m_selectedItemsProperties) {
         m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems);
@@ -451,106 +413,39 @@ KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const
 KFileItem DolphinContextMenu::baseFileItem()
 {
     if (!m_baseFileItem) {
-        m_baseFileItem = new KFileItem(KFileItem::Unknown, KFileItem::Unknown, 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(m_popup, "DesktopEntryName != 'dolphin'");
-
-    // insert 'Actions' sub menu
-    fileItemActions.addServiceActionsTo(m_popup);
+    m_fileItemActions->insertOpenWithActionsTo(nullptr, this, QStringList{qApp->desktopFileName()});
 }
 
-void DolphinContextMenu::addFileItemPluginActions()
+void DolphinContextMenu::addAdditionalActions(const KFileItemListProperties &props)
 {
-    KFileItemListProperties props;
-    if (m_selectedItems.isEmpty()) {
-        props.setItems(KFileItemList() << baseFileItem());
-    } else {
-        props = selectedItemsProperties();
-    }
-
-    QString commonMimeType = props.mimeType();
-    if (commonMimeType.isEmpty()) {
-        commonMimeType = QLatin1String("application/octet-stream");
-    }
+    addSeparator();
 
-    const KService::List pluginServices = KMimeTypeTrader::self()->query(commonMimeType, "KFileItemAction/Plugin", "exist Library");
-    if (pluginServices.isEmpty()) {
-        return;
+    QList<QAction *> additionalActions;
+    if (props.isLocal() && ContextMenuSettings::showOpenTerminal()) {
+        additionalActions << m_mainWindow->actionCollection()->action(QStringLiteral("open_terminal_here"));
     }
+    m_fileItemActions->addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions);
 
-    const KConfig config("kservicemenurc", KConfig::NoGlobals);
-    const KConfigGroup showGroup = config.group("Show");
-
-    foreach (const KSharedPtr<KService>& service, pluginServices) {
-        if (!showGroup.readEntry(service->desktopEntryName(), true)) {
-            // The plugin has been disabled
-            continue;
-        }
-
-        // Old API (kdelibs-4.6.0 only)
-        KFileItemActionPlugin* plugin = service->createInstance<KFileItemActionPlugin>();
-        if (plugin) {
-            plugin->setParent(m_popup);
-            m_popup->addActions(plugin->actions(props, m_mainWindow));
-        }
-        // New API (kdelibs >= 4.6.1)
-        KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance<KAbstractFileItemActionPlugin>();
-        if (abstractPlugin) {
-            abstractPlugin->setParent(m_popup);
-            m_popup->addActions(abstractPlugin->actions(props, m_mainWindow));
-        }
-    }
-}
-
-void DolphinContextMenu::addVersionControlPluginActions()
-{
-    const DolphinView* view = m_mainWindow->activeViewContainer()->view();
-    const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
+    const DolphinView *view = m_mainWindow->activeViewContainer()->view();
+    const QList<QAction *> versionControlActions = view->versionControlActions(m_selectedItems);
     if (!versionControlActions.isEmpty()) {
-        foreach (QAction* action, versionControlActions) {
-            m_popup->addAction(action);
-        }
-        m_popup->addSeparator();
-    }
-}
-
-void DolphinContextMenu::addCustomActions()
-{
-    foreach (QAction* action, m_customActions) {
-        m_popup->addAction(action);
+        addActions(versionControlActions);
+        addSeparator();
     }
 }
 
-void DolphinContextMenu::updateRemoveAction()
-{
-    const KActionCollection* collection = m_mainWindow->actionCollection();
-
-    // Using m_removeAction->setText(action->text()) does not apply the &-shortcut.
-    // This is only done until the original action has been shown at least once. To
-    // bypass this issue, the text and &-shortcut is applied manually.
-    const QAction* action = 0;
-    if (moveToTrash()) {
-        action = collection->action("move_to_trash");
-        m_removeAction->setText(i18nc("@action:inmenu", "&Move to Trash"));
-    } else {
-        action = collection->action("delete");
-        m_removeAction->setText(i18nc("@action:inmenu", "&Delete"));
-    }
-    m_removeAction->setIcon(action->icon());
-    m_removeAction->setShortcuts(action->shortcuts());
-}
-
-bool DolphinContextMenu::moveToTrash() const
-{
-    return !m_shiftPressed;
-}
-
-#include "dolphincontextmenu.moc"
+#include "moc_dolphincontextmenu.cpp"