X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/9a55b0d704c1301f5aef569392ca2695d8fc3204..8eb9b508ca87fb1d634d8b8ba62c054ed04466d2:/src/dolphincontextmenu.cpp diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index 8fbcbf3f5..dacdc9c83 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -26,25 +26,28 @@ #include "dolphinviewcontainer.h" #include "dolphin_generalsettings.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include #include #include @@ -53,40 +56,51 @@ #include "views/dolphinview.h" #include "views/viewmodecontroller.h" +K_GLOBAL_STATIC(KModifierKeyInfo, m_keyInfo) + DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent, const KFileItem& fileInfo, const KUrl& baseUrl) : m_mainWindow(parent), - m_capabilities(0), m_fileInfo(fileInfo), m_baseUrl(baseUrl), + m_baseFileItem(0), + m_selectedItems(), + m_selectedItemsProperties(0), m_context(NoContext), m_copyToMenu(parent), m_customActions(), - m_popup(new KMenu(m_mainWindow)), - m_showDeleteCommand(false), + m_popup(0), + m_command(None), m_shiftPressed(false), - m_keyInfo() + 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(); - m_showDeleteCommand = KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false); - - if (m_keyInfo.isKeyPressed(Qt::Key_Shift) || m_keyInfo.isKeyLatched(Qt::Key_Shift)) { - m_shiftPressed = true; + if (m_keyInfo != 0) { + if (m_keyInfo->isKeyPressed(Qt::Key_Shift) || m_keyInfo->isKeyLatched(Qt::Key_Shift)) { + m_shiftPressed = true; + } + connect(m_keyInfo, SIGNAL(keyPressed(Qt::Key, bool)), + this, SLOT(slotKeyModifierPressed(Qt::Key, bool))); } - connect(&m_keyInfo, SIGNAL(keyPressed(Qt::Key, bool)), this, SLOT(deleteOrTrashMenuEntry(Qt::Key, bool))); + m_removeAction = new QAction(this); + connect(m_removeAction, SIGNAL(triggered()), this, SLOT(slotRemoveActionTriggered())); + + m_popup = new KMenu(m_mainWindow); } DolphinContextMenu::~DolphinContextMenu() { - delete m_capabilities; - m_capabilities = 0; + delete m_selectedItemsProperties; + m_selectedItemsProperties = 0; + + delete m_popup; + m_popup = 0; } void DolphinContextMenu::setCustomActions(const QList& actions) @@ -94,10 +108,10 @@ void DolphinContextMenu::setCustomActions(const QList& actions) m_customActions = actions; } -void DolphinContextMenu::open() +DolphinContextMenu::Command DolphinContextMenu::open() { // get the context information - if (m_baseUrl.protocol() == "trash") { + if (m_baseUrl.protocol() == QLatin1String("trash")) { m_context |= TrashContext; } @@ -119,15 +133,39 @@ void DolphinContextMenu::open() Q_ASSERT(m_context == NoContext); openViewportContextMenu(); } + + return m_command; +} + +void DolphinContextMenu::initializeModifierKeyInfo() +{ + // Access m_keyInfo, so that it gets instantiated by + // K_GLOBAL_STATIC + KModifierKeyInfo* keyInfo = m_keyInfo; + Q_UNUSED(keyInfo); +} + +void DolphinContextMenu::slotKeyModifierPressed(Qt::Key key, bool pressed) +{ + m_shiftPressed = (key == Qt::Key_Shift) && pressed; + updateRemoveAction(); +} + +void DolphinContextMenu::slotRemoveActionTriggered() +{ + const KActionCollection* collection = m_mainWindow->actionCollection(); + if (m_shiftPressed) { + collection->action("delete")->trigger(); + } else { + collection->action("move_to_trash")->trigger(); + } } void DolphinContextMenu::openTrashContextMenu() { Q_ASSERT(m_context & TrashContext); - addShowMenubarAction(); - - QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), m_popup.data()); + QAction* emptyTrashAction = new QAction(KIcon("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), m_popup); KConfig trashConfig("trashrc", KConfig::SimpleConfig); emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true)); m_popup->addAction(emptyTrashAction); @@ -145,6 +183,8 @@ void DolphinContextMenu::openTrashContextMenu() QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties"); m_popup->addAction(propertiesAction); + addShowMenubarAction(); + QAction *action = m_popup->exec(QCursor::pos()); if (action == emptyTrashAction) { const QString text(i18nc("@info", "Do you really want to empty the Trash? All items will be deleted.")); @@ -170,8 +210,6 @@ void DolphinContextMenu::openTrashItemContextMenu() Q_ASSERT(m_context & TrashContext); Q_ASSERT(m_context & ItemContext); - addShowMenubarAction(); - QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow); m_popup->addAction(restoreAction); @@ -182,7 +220,12 @@ void DolphinContextMenu::openTrashItemContextMenu() m_popup->addAction(propertiesAction); if (m_popup->exec(QCursor::pos()) == restoreAction) { - KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow); + KUrl::List selectedUrls; + foreach (const KFileItem &item, m_selectedItems) { + selectedUrls.append(item.url()); + } + + KonqOperations::restoreTrashedItems(selectedUrls, m_mainWindow); } } @@ -190,50 +233,71 @@ void DolphinContextMenu::openItemContextMenu() { Q_ASSERT(!m_fileInfo.isNull()); - 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")); - m_popup->addSeparator(); + QAction* openParentInNewWindowAction = 0; + QAction* openParentInNewTabAction = 0; + QAction* addToPlacesAction = 0; + if (m_selectedItems.count() == 1) { + if (m_fileInfo.isDir()) { + // setup 'Create New' menu + DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_popup, m_mainWindow); + const DolphinView* view = m_mainWindow->activeViewContainer()->view(); + newFileMenu->setViewShowsHiddenFiles(view->showHiddenFiles()); + newFileMenu->checkUpToDate(); + newFileMenu->setPopupFiles(m_fileInfo.url()); + newFileMenu->setEnabled(selectedItemsProperties().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(); + } } - addShowMenubarAction(); + insertDefaultItemActions(); m_popup->addSeparator(); KFileItemActions fileItemActions; - fileItemActions.setItemListProperties(capabilities()); + fileItemActions.setItemListProperties(selectedItemsProperties()); addServiceActions(fileItemActions); - addVersionControlActions(); + addFileItemPluginActions(); + + addVersionControlPluginActions(); // insert 'Copy To' and 'Move To' sub menus if (DolphinSettings::instance().generalSettings()->showCopyMoveMenu()) { m_copyToMenu.setItems(m_selectedItems); - m_copyToMenu.setReadOnly(!capabilities().supportsWriting()); - m_copyToMenu.addActionsTo(m_popup.data()); - m_popup->addSeparator(); - } - - // insert 'Add to Places' entry if exactly one item is selected - QAction* addToPlacesAction = 0; - if (m_fileInfo.isDir() && (m_selectedUrls.count() == 1) && !placeExists(m_fileInfo.url())) { - addToPlacesAction = m_popup->addAction(KIcon("bookmark-new"), - i18nc("@action:inmenu Add selected folder to places", "Add to Places")); + m_copyToMenu.setReadOnly(!selectedItemsProperties().supportsWriting()); + m_copyToMenu.addActionsTo(m_popup); } // insert 'Properties...' entry @@ -241,20 +305,23 @@ 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; } } } void DolphinContextMenu::openViewportContextMenu() { - addShowMenubarAction(); - // setup 'Create New' menu KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu(); const DolphinView* view = m_mainWindow->activeViewContainer()->view(); @@ -264,35 +331,41 @@ 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(); m_popup->addAction(pasteAction); m_popup->addSeparator(); - // insert service actions - const KFileItem item(KFileItem::Unknown, KFileItem::Unknown, m_baseUrl); - const KFileItemListProperties baseUrlProperties(KFileItemList() << item); + // Insert service actions + const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem()); KFileItemActions fileItemActions; fileItemActions.setItemListProperties(baseUrlProperties); addServiceActions(fileItemActions); - addVersionControlActions(); + addFileItemPluginActions(); - // 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")); - } + addVersionControlPluginActions(); addCustomActions(); QAction* propertiesAction = m_popup->addAction(i18nc("@action:inmenu", "Properties")); propertiesAction->setIcon(KIcon("document-properties")); + + addShowMenubarAction(); + QAction* action = m_popup->exec(QCursor::pos()); if (action == propertiesAction) { const KUrl& url = m_mainWindow->activeViewContainer()->url(); @@ -312,13 +385,9 @@ void DolphinContextMenu::insertDefaultItemActions() { const KActionCollection* collection = m_mainWindow->actionCollection(); - // Cut action + // Insert 'Cut', 'Copy' and 'Paste' m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Cut))); - - // Copy action m_popup->addAction(collection->action(KStandardAction::name(KStandardAction::Copy))); - - // Paste action m_popup->addAction(createPasteAction()); m_popup->addSeparator(); @@ -327,34 +396,22 @@ void DolphinContextMenu::insertDefaultItemActions() QAction* renameAction = collection->action("rename"); m_popup->addAction(renameAction); - // Insert move to trash and delete. We need to insert both because both can be visible. - m_popup->addAction(collection->action("move_to_trash")); - m_popup->addAction(collection->action("delete")); - - const KUrl& url = m_mainWindow->activeViewContainer()->url(); - if (url.isLocalFile()) { - collection->action("move_to_trash")->setVisible(true); - collection->action("delete")->setVisible(false); + // Insert 'Move to Trash' and/or 'Delete' + if (KGlobal::config()->group("KDE").readEntry("ShowDeleteCommand", false)) { + m_popup->addAction(collection->action("move_to_trash")); + m_popup->addAction(collection->action("delete")); } else { - collection->action("delete")->setVisible(true); + m_popup->addAction(m_removeAction); + updateRemoveAction(); } - - if(m_showDeleteCommand) { - collection->action("delete")->setVisible(true); - } - - if(m_shiftPressed) { - deleteOrTrashMenuEntry(Qt::Key_Shift, m_shiftPressed); - } - } void DolphinContextMenu::addShowMenubarAction() { KAction* showMenuBar = m_mainWindow->showMenuBarAction(); if (!m_mainWindow->menuBar()->isVisible()) { - m_popup->addAction(showMenuBar); m_popup->addSeparator(); + m_popup->addAction(showMenuBar); } } @@ -390,7 +447,7 @@ QAction* DolphinContextMenu::createPasteAction() action = new QAction(KIcon("edit-paste"), i18nc("@action:inmenu", "Paste Into Folder"), this); const QMimeData* mimeData = QApplication::clipboard()->mimeData(); const KUrl::List pasteData = KUrl::List::fromMimeData(mimeData); - action->setEnabled(!pasteData.isEmpty() && capabilities().supportsWriting()); + action->setEnabled(!pasteData.isEmpty() && selectedItemsProperties().supportsWriting()); connect(action, SIGNAL(triggered()), m_mainWindow, SLOT(pasteIntoFolder())); } else { action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste)); @@ -399,12 +456,20 @@ QAction* DolphinContextMenu::createPasteAction() return action; } -KFileItemListProperties& DolphinContextMenu::capabilities() +KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() +{ + if (m_selectedItemsProperties == 0) { + m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems); + } + return *m_selectedItemsProperties; +} + +KFileItem DolphinContextMenu::baseFileItem() { - if (m_capabilities == 0) { - m_capabilities = new KFileItemListProperties(m_selectedItems); + if (m_baseFileItem == 0) { + m_baseFileItem = new KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_baseUrl); } - return *m_capabilities; + return *m_baseFileItem; } void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions) @@ -412,15 +477,54 @@ void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions) fileItemActions.setParentWidget(m_mainWindow); // insert 'Open With...' action or sub menu - fileItemActions.addOpenWithActionsTo(m_popup.data(), "DesktopEntryName != 'dolphin'"); + fileItemActions.addOpenWithActionsTo(m_popup, "DesktopEntryName != 'dolphin'"); // insert 'Actions' sub menu - if (fileItemActions.addServiceActionsTo(m_popup.data())) { - m_popup->addSeparator(); + fileItemActions.addServiceActionsTo(m_popup); +} + +void DolphinContextMenu::addFileItemPluginActions() +{ + KFileItemListProperties props; + if (m_selectedItems.isEmpty()) { + props.setItems(KFileItemList() << baseFileItem()); + } else { + props = selectedItemsProperties(); + } + + QString commonMimeType = props.mimeType(); + if (commonMimeType.isEmpty()) { + commonMimeType = QLatin1String("application/octet-stream"); + } + + const KService::List pluginServices = KMimeTypeTrader::self()->query(commonMimeType, "KFileItemAction/Plugin", "exist Library"); + if (pluginServices.isEmpty()) { + return; + } + + const KConfig config("kservicemenurc", KConfig::NoGlobals); + const KConfigGroup showGroup = config.group("Show"); + + foreach (const KSharedPtr& service, pluginServices) { + if (!showGroup.readEntry(service->desktopEntryName(), true)) { + // The plugin has been disabled + continue; + } + + KFileItemActionPlugin* plugin = service->createInstance(); + if (plugin == 0) { + continue; + } + + plugin->setParent(m_popup); + const QList actions = plugin->actions(props, m_mainWindow); + foreach (QAction* action, actions) { + m_popup->addAction(action); + } } } -void DolphinContextMenu::addVersionControlActions() +void DolphinContextMenu::addVersionControlPluginActions() { const DolphinView* view = m_mainWindow->activeViewContainer()->view(); const QList versionControlActions = view->versionControlActions(m_selectedItems); @@ -439,25 +543,24 @@ void DolphinContextMenu::addCustomActions() } } -void DolphinContextMenu::deleteOrTrashMenuEntry(Qt::Key key, bool pressed) +void DolphinContextMenu::updateRemoveAction() { - if(m_mainWindow->activeViewContainer()->url().isLocalFile() && !m_showDeleteCommand && key == Qt::Key_Shift) { - - // Set the current size as fixed size so that the menu isn't flickering when pressing shift. - m_popup->setFixedSize(m_popup->size()); - if(pressed) { - m_mainWindow->actionCollection()->action("delete")->setVisible(true); - m_mainWindow->actionCollection()->action("move_to_trash")->setVisible(false); - } - else { - m_mainWindow->actionCollection()->action("delete")->setVisible(false); - m_mainWindow->actionCollection()->action("move_to_trash")->setVisible(true); - } - - // This sets the menu back to a dynamic size followed by a forced resize incase the newly made visible action has bigger text. - m_popup->setFixedSize(QSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX)); - m_popup->resize(m_popup->sizeHint()); + const KActionCollection* collection = m_mainWindow->actionCollection(); + const bool moveToTrash = selectedItemsProperties().isLocal() && !m_shiftPressed; + + // 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()); } #include "dolphincontextmenu.moc"