]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphincontextmenu.cpp
Merge branch 'release/22.04'
[dolphin.git] / src / dolphincontextmenu.cpp
index eb3f641e5ae6ffe78e237c30e07d5b9cfbf67feb..65d84106067b2b7123ac80d09d7e23e65297fc27 100644 (file)
 #include "dolphinplacesmodelsingleton.h"
 #include "dolphinremoveaction.h"
 #include "dolphinviewcontainer.h"
-#include "panels/places/placesitem.h"
-#include "panels/places/placesitemmodel.h"
+#include "global.h"
 #include "trash/dolphintrash.h"
 #include "views/dolphinview.h"
 #include "views/viewmodecontroller.h"
 
 #include <KActionCollection>
-#include <KFileItemActions>
 #include <KFileItemListProperties>
+#include <KHamburgerMenu>
 #include <KIO/EmptyTrashJob>
 #include <KIO/JobUiDelegate>
 #include <KIO/Paste>
@@ -30,7 +29,6 @@
 #include <KLocalizedString>
 #include <KNewFileMenu>
 #include <KPluginMetaData>
-#include <KService>
 #include <KStandardAction>
 #include <KToolBar>
 
 #include <QMimeDatabase>
 
 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
-                                       const QPoint& pos,
                                        const KFileItem& fileInfo,
-                                       const QUrl& baseUrl) :
+                                       const KFileItemList &selectedItems,
+                                       const QUrl& baseUrl,
+                                       KFileItemActions *fileItemActions) :
     QMenu(parent),
-    m_pos(pos),
     m_mainWindow(parent),
     m_fileInfo(fileInfo),
     m_baseUrl(baseUrl),
     m_baseFileItem(nullptr),
-    m_selectedItems(),
+    m_selectedItems(selectedItems),
     m_selectedItemsProperties(nullptr),
     m_context(NoContext),
     m_copyToMenu(parent),
-    m_customActions(),
-    m_command(None),
-    m_removeAction(nullptr)
+    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);
 
-    installEventFilter(this);
+    addAllActions();
 }
 
 DolphinContextMenu::~DolphinContextMenu()
@@ -74,13 +68,11 @@ DolphinContextMenu::~DolphinContextMenu()
     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
     const auto scheme = m_baseUrl.scheme();
     if (scheme == QLatin1String("trash")) {
@@ -99,78 +91,55 @@ 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 {
-        openViewportContextMenu();
+        addViewportContextMenu();
     }
-
-    return m_command;
 }
 
-void DolphinContextMenu::childEvent(QChildEvent* event)
+bool DolphinContextMenu::eventFilter(QObject* object, QEvent* event)
 {
-    if(event->added()) {
-        event->child()->installEventFilter(this);
-    }
-    QMenu::childEvent(event);
-}
+    Q_UNUSED(object)
 
-bool DolphinContextMenu::eventFilter(QObject* dest, QEvent* event)
-{
     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) {
+
+        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;
         }
     }
-    return QMenu::eventFilter(dest, event);
+
+    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);
+    QAction *emptyTrashAction = addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), [this](){
+        Trash::empty(m_mainWindow);
+    });
     emptyTrashAction->setEnabled(!Trash::isEmpty());
-    addAction(emptyTrashAction);
-
-    addCustomActions();
 
     QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
     addAction(propertiesAction);
-
-    addShowMenuBarAction();
-
-    if (exec(m_pos) == emptyTrashAction) {
-        Trash::empty(m_mainWindow);
-    }
 }
 
-void DolphinContextMenu::openTrashItemContextMenu()
+void DolphinContextMenu::addTrashItemContextMenu()
 {
     Q_ASSERT(m_context & TrashContext);
     Q_ASSERT(m_context & ItemContext);
 
-    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));
-    addAction(deleteAction);
-
-    QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
-    addAction(propertiesAction);
-
-    if (exec(m_pos) == 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)) {
@@ -180,10 +149,16 @@ void DolphinContextMenu::openTrashItemContextMenu()
         KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
         KJobWidgets::setWindow(job, m_mainWindow);
         job->uiDelegate()->setAutoErrorHandlingEnabled(true);
-    }
+    });
+
+    QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
+    addAction(deleteAction);
+
+    QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
+    addAction(propertiesAction);
 }
 
-void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions &fileItemActions)
+void DolphinContextMenu::addDirectoryItemContextMenu()
 {
     // insert 'Open in new window' and 'Open in new tab' entries
     const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
@@ -195,12 +170,10 @@ void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions &fileItemA
     }
 
     // Insert 'Open With' entries
-    addOpenWithActions(fileItemActions);
+    addOpenWithActions();
 
     // 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());
@@ -209,54 +182,54 @@ void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions &fileItemA
 
     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->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
     addMenu(menu);
 
     addSeparator();
 }
 
-void DolphinContextMenu::openItemContextMenu()
+void DolphinContextMenu::addItemContextMenu()
 {
     Q_ASSERT(!m_fileInfo.isNull());
 
-    QAction* openParentAction = nullptr;
-    QAction* openParentInNewWindowAction = nullptr;
-    QAction* openParentInNewTabAction = nullptr;
     const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
 
-    KFileItemActions fileItemActions;
-    fileItemActions.setParentWidget(m_mainWindow);
-    fileItemActions.setItemListProperties(selectedItemsProps);
+    m_fileItemActions->setItemListProperties(selectedItemsProps);
 
     if (m_selectedItems.count() == 1) {
         // single files
         if (m_fileInfo.isDir()) {
-            addDirectoryItemContextMenu(fileItemActions);
+            addDirectoryItemContextMenu();
         } else if (m_context & TimelineContext || m_context & SearchContext) {
-            addOpenWithActions(fileItemActions);
+            addOpenWithActions();
 
-            openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
+            addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
                                            i18nc("@action:inmenu",
                                                  "Open Path"),
-                                           this);
-            addAction(openParentAction);
+                                           [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());
+            });
 
-            openParentInNewWindowAction = new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
+            addAction(QIcon::fromTheme(QStringLiteral("window-new")),
                                                     i18nc("@action:inmenu",
                                                           "Open Path in New Window"),
-                                                    this);
-            addAction(openParentInNewWindowAction);
+                                                    [this](){
+                Dolphin::openNewWindow({m_fileInfo.url()}, m_mainWindow, Dolphin::OpenNewWindowFlag::Select);
+            });
 
-            openParentInNewTabAction = new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
+            addAction(QIcon::fromTheme(QStringLiteral("tab-new")),
                                                    i18nc("@action:inmenu",
                                                          "Open Path in New Tab"),
-                                                   this);
-            addAction(openParentInNewTabAction);
+                                                   [this](){
+                m_mainWindow->openNewTab(KIO::upUrl(m_fileInfo.url()));
+            });
 
             addSeparator();
         } else {
             // Insert 'Open With" entries
-            addOpenWithActions(fileItemActions);
+            addOpenWithActions();
         }
         if (m_fileInfo.isLink()) {
             addAction(m_mainWindow->actionCollection()->action(QStringLiteral("show_target")));
@@ -278,12 +251,12 @@ void DolphinContextMenu::openItemContextMenu()
             addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
         }
         // Insert 'Open With" entries
-        addOpenWithActions(fileItemActions);
+        addOpenWithActions();
     }
 
     insertDefaultItemActions(selectedItemsProps);
 
-    addAdditionalActions(fileItemActions, selectedItemsProps);
+    addAdditionalActions(selectedItemsProps);
 
     // insert 'Copy To' and 'Move To' sub menus
     if (ContextMenuSettings::showCopyMoveMenu()) {
@@ -297,38 +270,22 @@ void DolphinContextMenu::openItemContextMenu()
     addSeparator();
     QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
     addAction(propertiesAction);
-
-    QAction* activatedAction = exec(m_pos);
-    if (activatedAction) {
-        if (activatedAction == openParentAction) {
-            m_command = OpenParentFolder;
-        } else if (activatedAction == openParentInNewWindowAction) {
-            m_command = OpenParentFolderInNewWindow;
-        } else if (activatedAction == openParentInNewTabAction) {
-            m_command = OpenParentFolderInNewTab;
-        }
-    }
 }
 
-void DolphinContextMenu::openViewportContextMenu()
+void DolphinContextMenu::addViewportContextMenu()
 {
-    const DolphinView* view = m_mainWindow->activeViewContainer()->view();
-
     const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
-    KFileItemActions fileItemActions;
-    fileItemActions.setParentWidget(m_mainWindow);
-    fileItemActions.setItemListProperties(baseUrlProperties);
+    m_fileItemActions->setItemListProperties(baseUrlProperties);
 
     // Set up and insert 'Create New' menu
     KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
-    newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown());
     newFileMenu->checkUpToDate();
     newFileMenu->setPopupFiles(QList<QUrl>() << m_baseUrl);
     addMenu(newFileMenu->menu());
 
     // 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);
+    addOpenWithActions();
 
     QAction* pasteAction = createPasteAction();
     if (pasteAction) {
@@ -353,17 +310,12 @@ void DolphinContextMenu::openViewportContextMenu()
         addSeparator();
     }
 
-    addAdditionalActions(fileItemActions, baseUrlProperties);
-    addCustomActions();
+    addAdditionalActions(baseUrlProperties);
 
     addSeparator();
 
     QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
     addAction(propertiesAction);
-
-    addShowMenuBarAction();
-
-    exec(m_pos);
 }
 
 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
@@ -423,16 +375,6 @@ void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties&
     }
 }
 
-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()) {
-        addSeparator();
-        addAction(showMenuBar);
-    }
-}
-
 bool DolphinContextMenu::placeExists(const QUrl& url) const
 {
     const KFilePlacesModel* placesModel = DolphinPlacesModelSingleton::instance().placesModel();
@@ -492,26 +434,21 @@ KFileItem DolphinContextMenu::baseFileItem()
     return *m_baseFileItem;
 }
 
-void DolphinContextMenu::addOpenWithActions(KFileItemActions& fileItemActions)
+void DolphinContextMenu::addOpenWithActions()
 {
     // insert 'Open With...' action or sub menu
-    fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp->desktopFileName()));
-}
-
-void DolphinContextMenu::addCustomActions()
-{
-    addActions(m_customActions);
+    m_fileItemActions->insertOpenWithActionsTo(nullptr, this, QStringList{qApp->desktopFileName()});
 }
 
-void DolphinContextMenu::addAdditionalActions(KFileItemActions &fileItemActions, const KFileItemListProperties &props)
+void DolphinContextMenu::addAdditionalActions(const KFileItemListProperties &props)
 {
     addSeparator();
 
     QList<QAction *> additionalActions;
-    if (props.isDirectory() && props.isLocal()) {
+    if (props.isDirectory() && props.isLocal() && ContextMenuSettings::showOpenTerminal()) {
         additionalActions << m_mainWindow->actionCollection()->action(QStringLiteral("open_terminal"));
     }
-    fileItemActions.addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions);
+    m_fileItemActions->addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions);
 
     const DolphinView* view = m_mainWindow->activeViewContainer()->view();
     const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);