]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Context Menu: Only add paste action to folders
authorMéven Car <meven29@gmail.com>
Sat, 14 Nov 2020 06:58:24 +0000 (07:58 +0100)
committerMéven Car <meven29@gmail.com>
Sat, 14 Nov 2020 19:28:01 +0000 (20:28 +0100)
Either the viewport, or the selected folder is used as destination.
The paste action is only added when enabled.

src/dolphincontextmenu.cpp

index d15e668b1cac6f5542233737fbbb0261a8ef713d..dcf4b9b45f789cc3ffc4ffe4ddd344f64701ca33 100644 (file)
@@ -316,13 +316,7 @@ void DolphinContextMenu::openViewportContextMenu()
 {
     const DolphinView* view = m_mainWindow->activeViewContainer()->view();
 
-    // Insert 'Open With' entries
-    KFileItem baseItem = view->rootItem();
-    if (baseItem.isNull() || baseItem.url() != m_baseUrl) {
-        baseItem = baseFileItem();
-    }
-
-    const KFileItemListProperties baseUrlProperties(KFileItemList() << baseItem);
+    const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
     KFileItemActions fileItemActions;
     fileItemActions.setParentWidget(m_mainWindow);
     fileItemActions.setItemListProperties(baseUrlProperties);
@@ -339,7 +333,9 @@ void DolphinContextMenu::openViewportContextMenu()
     addOpenWithActions(fileItemActions);
 
     QAction* pasteAction = createPasteAction();
-    addAction(pasteAction);
+    if (pasteAction) {
+        addAction(pasteAction);
+    }
 
     // Insert 'Add to Places' entry if it's not already in the places panel
     if (!placeExists(m_mainWindow->activeViewContainer()->url())) {
@@ -381,7 +377,10 @@ void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties&
     QAction* copyPathAction = collection->action(QString("copy_location"));
     copyPathAction->setEnabled(m_selectedItems.size() == 1);
     addAction(copyPathAction);
-    addAction(createPasteAction());
+    QAction* pasteAction = createPasteAction();
+    if (pasteAction) {
+        addAction(pasteAction);
+    }
     addAction(m_mainWindow->actionCollection()->action(QStringLiteral("duplicate")));
 
     addSeparator();
@@ -433,16 +432,26 @@ bool DolphinContextMenu::placeExists(const QUrl& url) const
 QAction* DolphinContextMenu::createPasteAction()
 {
     QAction* action = nullptr;
-    const bool isDir = !m_fileInfo.isNull() && m_fileInfo.isDir();
-    if (isDir && (m_selectedItems.count() == 1)) {
-        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);
+    KFileItem destItem;
+    if (!m_fileInfo.isNull()) {
+        destItem = m_fileInfo;
     } else {
-        action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
+        destItem = baseFileItem();
+    }
+
+    if (!destItem.isNull() && destItem.isDir()) {
+        if (m_selectedItems.count() <= 1) {
+            const QMimeData *mimeData = QApplication::clipboard()->mimeData();
+            bool canPaste;
+            const QString text = KIO::pasteActionText(mimeData, &canPaste, destItem);
+            action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this);
+            if (canPaste) {
+                connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
+            } else {
+                // don't add the unavailable action
+                action = nullptr;
+            }
+        }
     }
 
     return action;
@@ -459,7 +468,13 @@ 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;
 }