]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphincontextmenu.cpp
[DolphinContextMenu] Restore check for whether place already exists
[dolphin.git] / src / dolphincontextmenu.cpp
index befe98677afaca4738f1377573d7f1603900b955..1dfbe054dd12a8afe251bfe44d5b72d5896540d0 100644 (file)
@@ -23,6 +23,7 @@
 #include "dolphin_generalsettings.h"
 #include "dolphinmainwindow.h"
 #include "dolphinnewfilemenu.h"
+#include "dolphinplacesmodelsingleton.h"
 #include "dolphinremoveaction.h"
 #include "dolphinviewcontainer.h"
 #include "panels/places/placesitem.h"
@@ -81,6 +82,8 @@ DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
 
 DolphinContextMenu::~DolphinContextMenu()
 {
+    delete m_baseFileItem;
+    m_baseFileItem = nullptr;
     delete m_selectedItemsProperties;
     m_selectedItemsProperties = nullptr;
 }
@@ -160,7 +163,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));
@@ -231,6 +234,8 @@ void DolphinContextMenu::openItemContextMenu()
 
             addSeparator();
         } else if (m_baseUrl.scheme().contains(QStringLiteral("search")) || m_baseUrl.scheme().contains(QStringLiteral("timeline"))) {
+            addOpenWithActions(fileItemActions);
+
             openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
                                            i18nc("@action:inmenu",
                                                  "Open Path"),
@@ -303,6 +308,7 @@ void DolphinContextMenu::openItemContextMenu()
     }
 
     // insert 'Properties...' entry
+    addSeparator();
     QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
     addAction(propertiesAction);
 
@@ -337,15 +343,25 @@ void DolphinContextMenu::openViewportContextMenu()
     addSeparator();
 
     // Insert 'Open With' entries
-    const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
+    KFileItem baseItem = view->rootItem();
+    if (baseItem.isNull() || baseItem.url() != m_baseUrl) {
+        baseItem = baseFileItem();
+    }
+
+    const KFileItemListProperties baseUrlProperties(KFileItemList() << baseItem);
     KFileItemActions fileItemActions;
     fileItemActions.setParentWidget(m_mainWindow);
     fileItemActions.setItemListProperties(baseUrlProperties);
-    addOpenWithActions(fileItemActions);
+
+    // Don't show "Open With" menu items if the current dir is empty, because there's
+    // generally no app that can do anything interesting with an empty directory
+    if (view->itemsCount() != 0) {
+        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("new_window")));
+    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
@@ -361,6 +377,12 @@ void DolphinContextMenu::openViewportContextMenu()
     addAction(pasteAction);
     addSeparator();
 
+    // Insert 'Sort By' and 'View Mode'
+    addAction(m_mainWindow->actionCollection()->action(QStringLiteral("sort")));
+    addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode")));
+
+    addSeparator();
+
     // Insert service actions
     fileItemActions.addServiceActionsTo(this);
     fileItemActions.addPluginActionsTo(this);
@@ -406,26 +428,24 @@ void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties&
     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());
-            }
-            addAction(m_removeAction);
-            m_removeAction->update();
+    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();
     }
 }
 
@@ -441,13 +461,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()