]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphincontextmenu.cpp
The &-shortcut from another action is not set until the action has been shown at...
[dolphin.git] / src / dolphincontextmenu.cpp
index a36d1aeb06a174227942a67cd668db9c68940636..a3d913d6a02a8c9f026ff06c19d7cf121432a691 100644 (file)
@@ -67,14 +67,17 @@ DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
     m_copyToMenu(parent),
     m_customActions(),
     m_popup(new KMenu(m_mainWindow)),
+    m_command(None),
     m_shiftPressed(false),
     m_removeAction(0)
 {
     // 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_selectedUrls = view->selectedUrls();
     m_selectedItems = view->selectedItems();
+    foreach (const KFileItem &item, m_selectedItems) {
+        m_selectedUrls.append(item.url());
+    }
 
     if (m_keyInfo != 0) {
         if (m_keyInfo->isKeyPressed(Qt::Key_Shift) || m_keyInfo->isKeyLatched(Qt::Key_Shift)) {
@@ -99,7 +102,7 @@ void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
     m_customActions = actions;
 }
 
-void DolphinContextMenu::open()
+DolphinContextMenu::Command DolphinContextMenu::open()
 {
     // get the context information
     if (m_baseUrl.protocol() == QLatin1String("trash")) {
@@ -124,6 +127,8 @@ void DolphinContextMenu::open()
         Q_ASSERT(m_context == NoContext);
         openViewportContextMenu();
     }
+
+    return m_command;
 }
 
 void DolphinContextMenu::initializeModifierKeyInfo()
@@ -219,35 +224,54 @@ void DolphinContextMenu::openItemContextMenu()
 {
     Q_ASSERT(!m_fileInfo.isNull());
 
+    QAction* openParentInNewWindowAction = 0;
+    QAction* openParentInNewTabAction = 0;
     QAction* addToPlacesAction = 0;
-    if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1)) {
-        // setup 'Create New' menu
-        DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_popup.data(), m_mainWindow);
-        const DolphinView* view = m_mainWindow->activeViewContainer()->view();
-        newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
-        newFileMenu->checkUpToDate();
-        newFileMenu->setPopupFiles(m_fileInfo.url());
-        newFileMenu->setEnabled(capabilities().supportsWriting());
-
-        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"));
+    if (m_selectedUrls.count() == 1) {
+        if (m_fileInfo.isDir()) {
+            // setup 'Create New' menu
+            DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_popup.data(), m_mainWindow);
+            const DolphinView* view = m_mainWindow->activeViewContainer()->view();
+            newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles());
+            newFileMenu->checkUpToDate();
+            newFileMenu->setPopupFiles(m_fileInfo.url());
+            newFileMenu->setEnabled(capabilities().supportsWriting());
+
+            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"));
+            }
+
+            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();
         }
-
-        m_popup->addSeparator();
     }
+
     addShowMenubarAction();
     insertDefaultItemActions();
 
@@ -271,12 +295,17 @@ void DolphinContextMenu::openItemContextMenu()
     m_popup->addAction(propertiesAction);
 
     QAction* activatedAction = m_popup->exec(QCursor::pos());
-
-    if ((addToPlacesAction != 0) && (activatedAction == addToPlacesAction)) {
-        const KUrl selectedUrl(m_fileInfo.url());
-        if (selectedUrl.isValid()) {
-            DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
-                                                                selectedUrl);
+    if (activatedAction != 0) {
+        if (activatedAction == addToPlacesAction) {
+            const KUrl selectedUrl(m_fileInfo.url());
+            if (selectedUrl.isValid()) {
+                DolphinSettings::instance().placesModel()->addPlace(placesName(selectedUrl),
+                                                                    selectedUrl);
+            }
+        } else if (activatedAction == openParentInNewWindowAction) {
+            m_command = OpenParentFolderInNewWindow;
+        } else if (activatedAction == openParentInNewTabAction) {
+            m_command = OpenParentFolderInNewTab;
         }
     }
 }
@@ -294,9 +323,18 @@ void DolphinContextMenu::openViewportContextMenu()
     m_popup->addMenu(newFileMenu->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 '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();
 
     QAction* pasteAction = createPasteAction();
@@ -312,13 +350,6 @@ void DolphinContextMenu::openViewportContextMenu()
 
     addVersionControlActions();
 
-    // 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"));
-    }
-
     addCustomActions();
 
     QAction* propertiesAction = m_popup->addAction(i18nc("@action:inmenu", "Properties"));
@@ -455,8 +486,18 @@ void DolphinContextMenu::updateRemoveAction()
 {
     const KActionCollection* collection = m_mainWindow->actionCollection();
     const bool moveToTrash = capabilities().isLocal() && !m_shiftPressed;
-    const QAction* action = moveToTrash ? collection->action("move_to_trash") : collection->action("delete");
-    m_removeAction->setText(action->text());
+
+    // 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());
 }