]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphincontextmenu.cpp
Merge branch 'release/20.12'
[dolphin.git] / src / dolphincontextmenu.cpp
index bedc835c2af7e04c3d9c139956d6f401de26fa7a..eabd81e225080999c57e7328f9e0a9fc717c8aff 100644 (file)
@@ -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 <KAbstractFileItemActionPlugin>
 #include <KActionCollection>
 #include <KFileItemActions>
 #include <KFileItemListProperties>
@@ -41,7 +27,6 @@
 #include <KIO/RestoreJob>
 #include <KJobWidgets>
 #include <KLocalizedString>
-#include <KMimeTypeTrader>
 #include <KNewFileMenu>
 #include <KPluginMetaData>
 #include <KService>
@@ -51,7 +36,6 @@
 #include <QApplication>
 #include <QClipboard>
 #include <QKeyEvent>
-#include <QMenu>
 #include <QMenuBar>
 #include <QMimeDatabase>
 
@@ -77,10 +61,14 @@ DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
     // or the items itself. To increase the performance both lists are cached.
     const DolphinView* view = m_mainWindow->activeViewContainer()->view();
     m_selectedItems = view->selectedItems();
+
+    installEventFilter(this);
 }
 
 DolphinContextMenu::~DolphinContextMenu()
 {
+    delete m_baseFileItem;
+    m_baseFileItem = nullptr;
     delete m_selectedItemsProperties;
     m_selectedItemsProperties = nullptr;
 }
@@ -93,8 +81,13 @@ void DolphinContextMenu::setCustomActions(const QList<QAction*>& 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()) {
@@ -112,27 +105,34 @@ DolphinContextMenu::Command DolphinContextMenu::open()
     } else if (m_context & ItemContext) {
         openItemContextMenu();
     } else {
-        Q_ASSERT(m_context == NoContext);
         openViewportContextMenu();
     }
 
     return m_command;
 }
 
-void DolphinContextMenu::keyPressEvent(QKeyEvent *ev)
+void DolphinContextMenu::childEvent(QChildEvent* event)
 {
-    if (m_removeAction && ev->key() == Qt::Key_Shift) {
-        m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed);
+    if(event->added()) {
+        event->child()->installEventFilter(this);
     }
-    QMenu::keyPressEvent(ev);
+    QMenu::childEvent(event);
 }
 
-void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev)
+bool DolphinContextMenu::eventFilter(QObject* dest, QEvent* event)
 {
-    if (m_removeAction && ev->key() == Qt::Key_Shift) {
-        m_removeAction->update(DolphinRemoveAction::ShiftState::Released);
+    if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
+        QKeyEvent* keyEvent = static_cast<QKeyEvent*>(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);
+            }
+            return true;
+        }
     }
-    QMenu::keyReleaseEvent(ev);
+    return QMenu::eventFilter(dest, event);
 }
 
 void DolphinContextMenu::openTrashContextMenu()
@@ -160,7 +160,7 @@ void DolphinContextMenu::openTrashItemContextMenu()
     Q_ASSERT(m_context & TrashContext);
     Q_ASSERT(m_context & ItemContext);
 
-    QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow);
+    QAction* restoreAction = new QAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), m_mainWindow);
     addAction(restoreAction);
 
     QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
@@ -172,7 +172,7 @@ void DolphinContextMenu::openTrashItemContextMenu()
     if (exec(m_pos) == restoreAction) {
         QList<QUrl> selectedUrls;
         selectedUrls.reserve(m_selectedItems.count());
-        foreach (const KFileItem &item, m_selectedItems) {
+        for (const KFileItem &item : qAsConst(m_selectedItems)) {
             selectedUrls.append(item.url());
         }
 
@@ -182,6 +182,37 @@ 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<QUrl>() << 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")));
+     menu->setParent(this, Qt::Popup);
+     addMenu(menu);
+
+     addSeparator();
+}
+
 void DolphinContextMenu::openItemContextMenu()
 {
     Q_ASSERT(!m_fileInfo.isNull());
@@ -189,44 +220,19 @@ void DolphinContextMenu::openItemContextMenu()
     QAction* openParentAction = nullptr;
     QAction* openParentInNewWindowAction = nullptr;
     QAction* openParentInNewTabAction = nullptr;
-    QAction* addToPlacesAction = nullptr;
     const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
 
+    KFileItemActions fileItemActions;
+    fileItemActions.setParentWidget(this);
+    fileItemActions.setItemListProperties(selectedItemsProps);
+
     if (m_selectedItems.count() == 1) {
-        if (m_fileInfo.isLink()) {
-            addAction(m_mainWindow->actionCollection()->action(QStringLiteral("show_target")));
-            addSeparator();
-        }
+        // 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(fileItemActions);
+        } else if (m_context & TimelineContext || m_context & SearchContext) {
+            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")));
-
-            // 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"),
@@ -246,16 +252,18 @@ void DolphinContextMenu::openItemContextMenu()
             addAction(openParentInNewTabAction);
 
             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(fileItemActions);
+        }
+        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) {
+        for (const auto &item : qAsConst(m_selectedItems)) {
             const QUrl& url = DolphinView::openItemAsFolderUrl(item);
             if (url.isEmpty()) {
                 selectionHasOnlyDirs = false;
@@ -266,21 +274,14 @@ void DolphinContextMenu::openItemContextMenu()
         if (selectionHasOnlyDirs) {
             // insert 'Open in new tab' entry
             addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
-            addSeparator();
         }
+        // Insert 'Open With" entries
+        addOpenWithActions(fileItemActions);
     }
 
     insertDefaultItemActions(selectedItemsProps);
 
-    addSeparator();
-
-    KFileItemActions fileItemActions;
-    fileItemActions.setItemListProperties(selectedItemsProps);
-    addServiceActions(fileItemActions);
-
-    fileItemActions.addPluginActionsTo(this);
-
-    addVersionControlPluginActions();
+    addAdditionalActions(fileItemActions, selectedItemsProps);
 
     // insert 'Copy To' and 'Move To' sub menus
     if (GeneralSettings::showCopyMoveMenu()) {
@@ -291,19 +292,13 @@ void DolphinContextMenu::openItemContextMenu()
     }
 
     // insert 'Properties...' entry
+    addSeparator();
     QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
     addAction(propertiesAction);
 
     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;
@@ -315,103 +310,102 @@ void DolphinContextMenu::openItemContextMenu()
 
 void DolphinContextMenu::openViewportContextMenu()
 {
-    // setup 'Create New' menu
-    KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
     const DolphinView* view = m_mainWindow->activeViewContainer()->view();
+
+    const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
+    KFileItemActions fileItemActions;
+    fileItemActions.setParentWidget(m_mainWindow);
+    fileItemActions.setItemListProperties(baseUrlProperties);
+
+    // Set up and insert 'Create New' menu
+    KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
     newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
     newFileMenu->checkUpToDate();
-    newFileMenu->setPopupFiles(m_baseUrl);
+    newFileMenu->setPopupFiles(QList<QUrl>() << 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(fileItemActions);
 
-    // 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"));
+    QAction* pasteAction = createPasteAction();
+    if (pasteAction) {
+        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();
 
-    QAction* pasteAction = createPasteAction();
-    addAction(pasteAction);
-    addSeparator();
-
-    // Insert service actions
-    const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
-    KFileItemActions fileItemActions;
-    fileItemActions.setItemListProperties(baseUrlProperties);
-    addServiceActions(fileItemActions);
-
-    fileItemActions.addPluginActionsTo(this);
-
-    addVersionControlPluginActions();
+    // Insert 'Sort By' and 'View Mode'
+    addAction(m_mainWindow->actionCollection()->action(QStringLiteral("sort")));
+    addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode")));
 
+    addAdditionalActions(fileItemActions, baseUrlProperties);
     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)));
-    addAction(createPasteAction());
-
-    addSeparator();
+    QAction* copyPathAction = collection->action(QString("copy_location"));
+    copyPathAction->setEnabled(m_selectedItems.size() == 1);
+    addAction(copyPathAction);
+    QAction* pasteAction = createPasteAction();
+    if (pasteAction) {
+        addAction(pasteAction);
+    }
+    addAction(m_mainWindow->actionCollection()->action(QStringLiteral("duplicate")));
 
     // Insert 'Rename'
     addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile)));
 
-    // 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 = 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());
+    // 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")));
             }
-            addAction(m_removeAction);
-            m_removeAction->update();
         }
     }
+
+    addSeparator();
+
+    // 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();
+    }
 }
 
 void DolphinContextMenu::addShowMenuBarAction()
@@ -426,28 +420,36 @@ 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()
 {
     QAction* action = nullptr;
-    const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
-    if (isDir && (m_selectedItems.count() == 1)) {
+    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;
@@ -464,24 +466,38 @@ 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(KFileItemActions& fileItemActions)
 {
-    fileItemActions.setParentWidget(m_mainWindow);
-
     // insert 'Open With...' action or sub menu
     fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp->desktopFileName()));
+}
 
-    // insert 'Actions' sub menu
-    fileItemActions.addServiceActionsTo(this);
+void DolphinContextMenu::addCustomActions()
+{
+    addActions(m_customActions);
 }
 
-void DolphinContextMenu::addVersionControlPluginActions()
+void DolphinContextMenu::addAdditionalActions(KFileItemActions &fileItemActions, const KFileItemListProperties &props)
 {
+    addSeparator();
+
+    QList<QAction *> additionalActions;
+    if (props.isDirectory() && props.isLocal()) {
+        additionalActions << m_mainWindow->actionCollection()->action(QStringLiteral("open_terminal"));
+    }
+    fileItemActions.addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions);
+
     const DolphinView* view = m_mainWindow->activeViewContainer()->view();
     const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
     if (!versionControlActions.isEmpty()) {
@@ -490,8 +506,3 @@ void DolphinContextMenu::addVersionControlPluginActions()
     }
 }
 
-void DolphinContextMenu::addCustomActions()
-{
-    addActions(m_customActions);
-}
-