X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/da795d20e2f2fa57ac9cafdffcfac4d092d8eaea..10d8a5731507881aa12f166a7d233e8c434894eb:/src/dolphincontextmenu.cpp diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index 468b7d69d..1dfbe054d 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -20,59 +20,87 @@ #include "dolphincontextmenu.h" +#include "dolphin_generalsettings.h" #include "dolphinmainwindow.h" -#include "dolphinsettings.h" -#include "dolphinview.h" - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include +#include "dolphinnewfilemenu.h" +#include "dolphinplacesmodelsingleton.h" +#include "dolphinremoveaction.h" +#include "dolphinviewcontainer.h" +#include "panels/places/placesitem.h" +#include "panels/places/placesitemmodel.h" +#include "trash/dolphintrash.h" +#include "views/dolphinview.h" +#include "views/viewmodecontroller.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent, - KFileItem* fileInfo, - const KUrl& baseUrl) : + const QPoint& pos, + const KFileItem& fileInfo, + const QUrl& baseUrl) : + QMenu(parent), + m_pos(pos), m_mainWindow(parent), m_fileInfo(fileInfo), m_baseUrl(baseUrl), - m_context(NoContext) + m_baseFileItem(nullptr), + m_selectedItems(), + m_selectedItemsProperties(nullptr), + m_context(NoContext), + m_copyToMenu(parent), + m_customActions(), + m_command(None), + m_removeAction(nullptr) { // The context menu either accesses the URLs of the selected items // or the items itself. To increase the performance both lists are cached. - DolphinView* view = m_mainWindow->activeView(); - m_selectedUrls = view->selectedUrls(); + const DolphinView* view = m_mainWindow->activeViewContainer()->view(); m_selectedItems = view->selectedItems(); } DolphinContextMenu::~DolphinContextMenu() -{} +{ + delete m_baseFileItem; + m_baseFileItem = nullptr; + delete m_selectedItemsProperties; + m_selectedItemsProperties = nullptr; +} + +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.scheme() == QLatin1String("trash")) { m_context |= TrashContext; } - if (m_fileInfo != 0) { + if (!m_fileInfo.isNull() && !m_selectedItems.isEmpty()) { m_context |= ItemContext; // TODO: handle other use cases like devices + desktop files } @@ -90,36 +118,44 @@ void DolphinContextMenu::open() Q_ASSERT(m_context == NoContext); openViewportContextMenu(); } + + return m_command; } +void DolphinContextMenu::keyPressEvent(QKeyEvent *ev) +{ + if (m_removeAction && ev->key() == Qt::Key_Shift) { + m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed); + } + QMenu::keyPressEvent(ev); +} + +void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev) +{ + if (m_removeAction && ev->key() == Qt::Key_Shift) { + m_removeAction->update(DolphinRemoveAction::ShiftState::Released); + } + QMenu::keyReleaseEvent(ev); +} void DolphinContextMenu::openTrashContextMenu() { Q_ASSERT(m_context & TrashContext); - KMenu* popup = new KMenu(m_mainWindow); - - QAction* emptyTrashAction = new QAction(KIcon("emptytrash"), i18n("Empty Trash"), popup); - KConfig trashConfig("trashrc", KConfig::OnlyLocal); - emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true)); - popup->addAction(emptyTrashAction); - - QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties"); - popup->addAction(propertiesAction); - - if (popup->exec(QCursor::pos()) == emptyTrashAction) { - const QString text(i18n("Do you really want to empty the Trash? All items will get deleted.")); - const bool del = KMessageBox::warningContinueCancel(m_mainWindow, - text, - QString(), - KGuiItem(i18n("Empty Trash"), KIcon("user-trash")) - ) == KMessageBox::Continue; - if (del) { - KonqOperations::emptyTrash(m_mainWindow); - } - } + QAction* emptyTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this); + emptyTrashAction->setEnabled(!Trash::isEmpty()); + addAction(emptyTrashAction); - popup->deleteLater(); + addCustomActions(); + + QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); + addAction(propertiesAction); + + addShowMenuBarAction(); + + if (exec(m_pos) == emptyTrashAction) { + Trash::empty(m_mainWindow); + } } void DolphinContextMenu::openTrashItemContextMenu() @@ -127,384 +163,363 @@ void DolphinContextMenu::openTrashItemContextMenu() Q_ASSERT(m_context & TrashContext); Q_ASSERT(m_context & ItemContext); - KMenu* popup = new KMenu(m_mainWindow); + QAction* restoreAction = new QAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), m_mainWindow); + addAction(restoreAction); - QAction* restoreAction = new QAction(i18n("Restore"), m_mainWindow); - popup->addAction(restoreAction); + QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)); + addAction(deleteAction); - QAction* deleteAction = m_mainWindow->actionCollection()->action("delete"); - popup->addAction(deleteAction); + QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); + addAction(propertiesAction); - QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties"); - popup->addAction(propertiesAction); + if (exec(m_pos) == restoreAction) { + QList selectedUrls; + selectedUrls.reserve(m_selectedItems.count()); + foreach (const KFileItem &item, m_selectedItems) { + selectedUrls.append(item.url()); + } - if (popup->exec(QCursor::pos()) == restoreAction) { - KonqOperations::restoreTrashedItems(m_selectedUrls, m_mainWindow); + KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls); + KJobWidgets::setWindow(job, m_mainWindow); + job->uiDelegate()->setAutoErrorHandlingEnabled(true); } - - popup->deleteLater(); } void DolphinContextMenu::openItemContextMenu() { - Q_ASSERT(m_fileInfo != 0); - - KMenu* popup = new KMenu(m_mainWindow); - insertDefaultItemActions(popup); + Q_ASSERT(!m_fileInfo.isNull()); + + QAction* openParentAction = nullptr; + QAction* openParentInNewWindowAction = nullptr; + QAction* openParentInNewTabAction = nullptr; + QAction* addToPlacesAction = nullptr; + const KFileItemListProperties& selectedItemsProps = selectedItemsProperties(); + + KFileItemActions fileItemActions; + fileItemActions.setParentWidget(m_mainWindow); + fileItemActions.setItemListProperties(selectedItemsProps); + + if (m_selectedItems.count() == 1) { + if (m_fileInfo.isDir()) { + // insert 'Open in new window' and 'Open in new tab' entries + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window"))); + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab"))); + + // Insert 'Open With' entries + addOpenWithActions(fileItemActions); + + // insert 'Add to Places' entry + if (!placeExists(m_fileInfo.url())) { + addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")), + i18nc("@action:inmenu Add selected folder to places", + "Add to Places")); + } - popup->addSeparator(); + addSeparator(); + + // 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(m_fileInfo.url()); + newFileMenu->setEnabled(selectedItemsProps.supportsWriting()); + connect(newFileMenu, &DolphinNewFileMenu::fileCreated, newFileMenu, &DolphinNewFileMenu::deleteLater); + connect(newFileMenu, &DolphinNewFileMenu::directoryCreated, newFileMenu, &DolphinNewFileMenu::deleteLater); + + QMenu* menu = newFileMenu->menu(); + menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New")); + menu->setIcon(QIcon::fromTheme(QStringLiteral("document-new"))); + addMenu(menu); + + 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"), + this); + addAction(openParentAction); + + openParentInNewWindowAction = new QAction(QIcon::fromTheme(QStringLiteral("window-new")), + i18nc("@action:inmenu", + "Open Path in New Window"), + this); + addAction(openParentInNewWindowAction); + + openParentInNewTabAction = new QAction(QIcon::fromTheme(QStringLiteral("tab-new")), + i18nc("@action:inmenu", + "Open Path in New Tab"), + this); + addAction(openParentInNewTabAction); + + addSeparator(); + } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo).isEmpty()) { + // Insert 'Open With" entries + addOpenWithActions(fileItemActions); + + // insert 'Open in new window' and 'Open in new tab' entries + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window"))); + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab"))); + + addSeparator(); + } else { + // Insert 'Open With" entries + addOpenWithActions(fileItemActions); + } + if (m_fileInfo.isLink()) { + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("show_target"))); + addSeparator(); + } + } else { + bool selectionHasOnlyDirs = true; + foreach (const KFileItem& item, m_selectedItems) { + const QUrl& url = DolphinView::openItemAsFolderUrl(item); + if (url.isEmpty()) { + selectionHasOnlyDirs = false; + break; + } + } - // insert 'Bookmark This Folder' entry if exactly one item is selected - QAction* bookmarkAction = 0; - if (m_fileInfo->isDir() && (m_selectedUrls.count() == 1)) { - bookmarkAction = popup->addAction(KIcon("bookmark-folder"), i18n("Bookmark Folder...")); + if (selectionHasOnlyDirs) { + // insert 'Open in new tab' entry + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tabs"))); + } + // Insert 'Open With" entries + addOpenWithActions(fileItemActions); } - // Insert 'Open With...' sub menu - QVector openWithVector; - const QList openWithActions = insertOpenWithItems(popup, openWithVector); + insertDefaultItemActions(selectedItemsProps); - // Insert 'Actions' sub menu - QVector actionsVector; - const QList serviceActions = insertActionItems(popup, actionsVector); - popup->addSeparator(); + addSeparator(); - // insert 'Properties...' entry - QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties"); - popup->addAction(propertiesAction); + fileItemActions.addServiceActionsTo(this); + fileItemActions.addPluginActionsTo(this); - QAction* activatedAction = popup->exec(QCursor::pos()); + addVersionControlPluginActions(); - if ((bookmarkAction != 0) && (activatedAction == bookmarkAction)) { - const KUrl selectedUrl(m_fileInfo->url()); - if (selectedUrl.isValid()) { - DolphinSettings::instance().placesModel()->addPlace(selectedUrl.fileName(), - selectedUrl); - } - } else if (serviceActions.contains(activatedAction)) { - // one of the 'Actions' items has been selected - int id = serviceActions.indexOf(activatedAction); - KDesktopFileActions::executeService(m_selectedUrls, actionsVector[id]); - } else if (openWithActions.contains(activatedAction)) { - // one of the 'Open With' items has been selected - if (openWithActions.last() == activatedAction) { - // the item 'Other...' has been selected - KRun::displayOpenWithDialog(m_selectedUrls, m_mainWindow); - } else { - int id = openWithActions.indexOf(activatedAction); - KService::Ptr servicePtr = openWithVector[id]; - KRun::run(*servicePtr, m_selectedUrls, m_mainWindow); - } + // insert 'Copy To' and 'Move To' sub menus + if (GeneralSettings::showCopyMoveMenu()) { + m_copyToMenu.setUrls(m_selectedItems.urlList()); + m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting()); + m_copyToMenu.setAutoErrorHandlingEnabled(true); + m_copyToMenu.addActionsTo(this); } - openWithVector.clear(); - actionsVector.clear(); - popup->deleteLater(); + // insert 'Properties...' entry + addSeparator(); + QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); + addAction(propertiesAction); + + QAction* activatedAction = exec(m_pos); + if (activatedAction) { + if (activatedAction == addToPlacesAction) { + const QUrl selectedUrl(m_fileInfo.url()); + if (selectedUrl.isValid()) { + PlacesItemModel model; + const QString text = selectedUrl.fileName(); + model.createPlacesItem(text, selectedUrl, KIO::iconNameForUrl(selectedUrl)); + } + } else if (activatedAction == openParentAction) { + m_command = OpenParentFolder; + } else if (activatedAction == openParentInNewWindowAction) { + m_command = OpenParentFolderInNewWindow; + } else if (activatedAction == openParentInNewTabAction) { + m_command = OpenParentFolderInNewTab; + } + } } void DolphinContextMenu::openViewportContextMenu() { - Q_ASSERT(m_fileInfo == 0); - KMenu* popup = new KMenu(m_mainWindow); - // setup 'Create New' menu - KNewMenu* newMenu = m_mainWindow->newMenu(); - newMenu->slotCheckUpToDate(); - newMenu->setPopupFiles(m_baseUrl); - popup->addMenu(newMenu->menu()); - popup->addSeparator(); + KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu(); + const DolphinView* view = m_mainWindow->activeViewContainer()->view(); + newFileMenu->setViewShowsHiddenFiles(view->hiddenFilesShown()); + newFileMenu->checkUpToDate(); + newFileMenu->setPopupFiles(m_baseUrl); + addMenu(newFileMenu->menu()); + addSeparator(); + + // Insert 'Open With' entries + 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); + + // 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("file_new"))); + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_tab"))); - QAction* pasteAction = m_mainWindow->actionCollection()->action(KStandardAction::stdName(KStandardAction::Paste)); - popup->addAction(pasteAction); + // Insert 'Add to Places' entry if exactly one item is selected + QAction* addToPlacesAction = nullptr; + if (!placeExists(m_mainWindow->activeViewContainer()->url())) { + addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")), + i18nc("@action:inmenu Add current folder to places", "Add to Places")); + } - // setup 'View Mode' menu - KMenu* viewModeMenu = new KMenu(i18n("View Mode")); + addSeparator(); - QAction* iconsMode = m_mainWindow->actionCollection()->action("icons"); - viewModeMenu->addAction(iconsMode); + QAction* pasteAction = createPasteAction(); + addAction(pasteAction); + addSeparator(); - QAction* detailsMode = m_mainWindow->actionCollection()->action("details"); - viewModeMenu->addAction(detailsMode); + // Insert 'Sort By' and 'View Mode' + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("sort"))); + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode"))); - QAction* columnsMode = m_mainWindow->actionCollection()->action("columns"); - viewModeMenu->addAction(columnsMode); + addSeparator(); - QAction* previewsMode = m_mainWindow->actionCollection()->action("previews"); - viewModeMenu->addAction(previewsMode); + // Insert service actions + fileItemActions.addServiceActionsTo(this); + fileItemActions.addPluginActionsTo(this); - popup->addMenu(viewModeMenu); - popup->addSeparator(); + addVersionControlPluginActions(); - QAction* bookmarkAction = popup->addAction(KIcon("bookmark-folder"), i18n("Bookmark This Folder...")); - popup->addSeparator(); + addCustomActions(); - QAction* propertiesAction = popup->addAction(i18n("Properties")); + QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); + addAction(propertiesAction); - QAction* activatedAction = popup->exec(QCursor::pos()); - if (activatedAction == propertiesAction) { - const KUrl& url = m_mainWindow->activeView()->url(); - KPropertiesDialog dialog(url); - dialog.exec(); - } else if (activatedAction == bookmarkAction) { - const KUrl& url = m_mainWindow->activeView()->url(); + addShowMenuBarAction(); + + QAction* action = exec(m_pos); + if (addToPlacesAction && (action == addToPlacesAction)) { + const DolphinViewContainer* container = m_mainWindow->activeViewContainer(); + const QUrl url = container->url(); if (url.isValid()) { - DolphinSettings::instance().placesModel()->addPlace(url.fileName(), url); + PlacesItemModel model; + QString icon; + if (container->isSearchModeEnabled()) { + icon = QStringLiteral("folder-saved-search-symbolic"); + } else { + icon = KIO::iconNameForUrl(url); + } + model.createPlacesItem(container->placesText(), url, icon); } } - - popup->deleteLater(); } -void DolphinContextMenu::insertDefaultItemActions(KMenu* popup) +void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties) { - Q_ASSERT(popup != 0); const KActionCollection* collection = m_mainWindow->actionCollection(); - // insert 'Cut', 'Copy' and 'Paste' - QAction* cutAction = collection->action(KStandardAction::stdName(KStandardAction::Cut)); - QAction* copyAction = collection->action(KStandardAction::stdName(KStandardAction::Copy)); - QAction* pasteAction = collection->action(KStandardAction::stdName(KStandardAction::Paste)); - - popup->addAction(cutAction); - popup->addAction(copyAction); - popup->addAction(pasteAction); - popup->addSeparator(); - - // insert 'Rename' - QAction* renameAction = collection->action("rename"); - popup->addAction(renameAction); - - // insert 'Move to Trash' and (optionally) 'Delete' - const KSharedConfig::Ptr globalConfig = KSharedConfig::openConfig("kdeglobals", KConfig::NoGlobals); - const KConfigGroup kdeConfig(globalConfig, "KDE"); - bool showDeleteCommand = kdeConfig.readEntry("ShowDeleteCommand", false); - const KUrl& url = m_mainWindow->activeView()->url(); - if (url.isLocalFile()) { - QAction* moveToTrashAction = collection->action("move_to_trash"); - popup->addAction(moveToTrashAction); + // Insert 'Cut', 'Copy' and 'Paste' + addAction(collection->action(KStandardAction::name(KStandardAction::Cut))); + addAction(collection->action(KStandardAction::name(KStandardAction::Copy))); + addAction(createPasteAction()); + + addSeparator(); + + // Insert 'Rename' + addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile))); + + // Insert 'Move to Trash' and/or 'Delete' + 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 { - showDeleteCommand = true; + if (!m_removeAction) { + m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection()); + } + addAction(m_removeAction); + m_removeAction->update(); } +} - if (showDeleteCommand) { - QAction* deleteAction = collection->action("delete"); - popup->addAction(deleteAction); +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); } } -QList DolphinContextMenu::insertOpenWithItems(KMenu* popup, - QVector& openWithVector) +bool DolphinContextMenu::placeExists(const QUrl& url) const { - // Parts of the following code have been taken - // from the class KonqOperations located in - // libqonq/konq_operations.h of Konqueror. - // (Copyright (C) 2000 David Faure ) - - // Prepare 'Open With' sub menu. Usually a sub menu is created, where all applications - // are listed which are registered to open the item. As last entry "Other..." will be - // attached which allows to select a custom application. If no applications are registered - // no sub menu is created at all, only "Open With..." will be offered. - bool insertOpenWithItems = true; - const QString contextMimeType(m_fileInfo->mimetype()); - - QListIterator mimeIt(m_selectedItems); - while (insertOpenWithItems && mimeIt.hasNext()) { - KFileItem* item = mimeIt.next(); - insertOpenWithItems = (contextMimeType == item->mimetype()); - } + const KFilePlacesModel* placesModel = DolphinPlacesModelSingleton::instance().placesModel(); - QList openWithActions; - if (insertOpenWithItems) { - // fill the 'Open with' sub menu with application types - const KMimeType::Ptr mimePtr = KMimeType::findByUrl(m_fileInfo->url()); - KService::List offers = KMimeTypeTrader::self()->query(mimePtr->name(), - "Application", - "Type == 'Application'"); - if (offers.count() > 0) { - KService::List::Iterator it; - KMenu* openWithMenu = new KMenu(i18n("Open With")); - for (it = offers.begin(); it != offers.end(); ++it) { - // The offer list from the KTrader returns duplicate - // application entries. Although this seems to be a configuration - // problem outside the scope of Dolphin, duplicated entries just - // will be skipped here. - const QString appName((*it)->name()); - if (!containsEntry(openWithMenu, appName)) { - const KIcon icon((*it)->icon()); - QAction* action = openWithMenu->addAction(icon, appName); - openWithVector.append(*it); - openWithActions << action; - } - } + const auto& matchedPlaces = placesModel->match(placesModel->index(0,0), KFilePlacesModel::UrlRole, url, 1, Qt::MatchExactly); - openWithMenu->addSeparator(); - QAction* action = openWithMenu->addAction(i18n("&Other...")); + return !matchedPlaces.isEmpty(); +} - openWithActions << action; - popup->addMenu(openWithMenu); - } else { - // No applications are registered, hence just offer - // a "Open With..." item instead of a sub menu containing - // only one entry. - QAction* action = popup->addAction(i18n("Open With...")); - openWithActions << action; - } +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); } else { - // At least one of the selected items has a different MIME type. In this case - // just show a disabled "Open With..." entry. - QAction* action = popup->addAction(i18n("Open With...")); - action->setEnabled(false); + action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste)); } - return openWithActions; + return action; } -QList DolphinContextMenu::insertActionItems(KMenu* popup, - QVector& actionsVector) +KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const { - // Parts of the following code have been taken - // from the class KonqOperations located in - // libqonq/konq_operations.h of Konqueror. - // (Copyright (C) 2000 David Faure ) - - KMenu* actionsMenu = new KMenu(i18n("Actions")); - - QList serviceActions; - - QStringList dirs = KGlobal::dirs()->findDirs("data", "dolphin/servicemenus/"); - - KMenu* menu = 0; - for (QStringList::ConstIterator dirIt = dirs.begin(); dirIt != dirs.end(); ++dirIt) { - QDir dir(*dirIt); - QStringList filters; - filters << "*.desktop"; - dir.setNameFilters(filters); - QStringList entries = dir.entryList(QDir::Files); - - for (QStringList::ConstIterator entryIt = entries.begin(); entryIt != entries.end(); ++entryIt) { - KConfigGroup cfg(KSharedConfig::openConfig(*dirIt + *entryIt, KConfig::OnlyLocal), "Desktop Entry"); - if ((cfg.hasKey("Actions") || cfg.hasKey("X-KDE-GetActionMenu")) && cfg.hasKey("ServiceTypes")) { - //const QStringList types = cfg.readListEntry("ServiceTypes"); - QStringList types; - types = cfg.readEntry("ServiceTypes", types); - for (QStringList::ConstIterator it = types.begin(); it != types.end(); ++it) { - // check whether the mime type is equal or whether the - // mimegroup (e. g. image/*) is supported - - bool insert = false; - if ((*it) == "all/allfiles") { - // The service type is valid for all files, but not for directories. - // Check whether the selected items only consist of files... - QListIterator mimeIt(m_selectedItems); - insert = true; - while (insert && mimeIt.hasNext()) { - KFileItem* item = mimeIt.next(); - insert = !item->isDir(); - } - } - - if (!insert) { - // Check whether the MIME types of all selected files match - // to the mimetype of the service action. As soon as one MIME - // type does not match, no service menu is shown at all. - QListIterator mimeIt(m_selectedItems); - insert = true; - while (insert && mimeIt.hasNext()) { - KFileItem* item = mimeIt.next(); - const QString mimeType(item->mimetype()); - const QString mimeGroup(mimeType.left(mimeType.indexOf('/'))); - - insert = (*it == mimeType) || - ((*it).right(1) == "*") && - ((*it).left((*it).indexOf('/')) == mimeGroup); - } - } - - if (insert) { - menu = actionsMenu; - - const QString submenuName = cfg.readEntry("X-KDE-Submenu"); - if (!submenuName.isEmpty()) { - menu = new KMenu(submenuName); - actionsMenu->addMenu(menu); - } - - Q3ValueList userServices = - KDesktopFileActions::userDefinedServices(*dirIt + *entryIt, true); - - Q3ValueList::Iterator serviceIt; - for (serviceIt = userServices.begin(); serviceIt != userServices.end(); ++serviceIt) { - KDesktopFileActions::Service service = (*serviceIt); - if (!service.m_strIcon.isEmpty()) { - QAction* action = menu->addAction(KIcon(service.m_strIcon), - service.m_strName); - serviceActions << action; - } else { - QAction *action = menu->addAction(service.m_strName); - serviceActions << action; - } - actionsVector.append(service); - } - } - } - } - } - } - - const int itemsCount = actionsMenu->actions().count(); - if (itemsCount == 0) { - // no actions are available at all, hence show the "Actions" - // submenu disabled - actionsMenu->setEnabled(false); + if (!m_selectedItemsProperties) { + m_selectedItemsProperties = new KFileItemListProperties(m_selectedItems); } + return *m_selectedItemsProperties; +} - if (itemsCount == 1) { - // Exactly one item is available. Instead of showing a sub menu with - // only one item, show the item directly in the root menu. - if (menu == actionsMenu) { - // The item is an action, hence show the action in the root menu. - const QList actions = actionsMenu->actions(); - Q_ASSERT(actions.count() == 1); - - const QString text = actions[0]->text(); - const QIcon icon = actions[0]->icon(); - if (icon.isNull()) { - QAction* action = popup->addAction(text); - serviceActions.clear(); - serviceActions << action; - } else { - QAction* action = popup->addAction(icon, text); - serviceActions.clear(); - serviceActions << action; - } - } else { - // The item is a sub menu, hence show the sub menu in the root menu. - popup->addMenu(menu); - } - actionsMenu->deleteLater(); - actionsMenu = 0; - } else { - popup->addMenu(actionsMenu); +KFileItem DolphinContextMenu::baseFileItem() +{ + if (!m_baseFileItem) { + m_baseFileItem = new KFileItem(m_baseUrl); } + return *m_baseFileItem; +} - return serviceActions; +void DolphinContextMenu::addOpenWithActions(KFileItemActions& fileItemActions) +{ + // insert 'Open With...' action or sub menu + fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp->desktopFileName())); } -bool DolphinContextMenu::containsEntry(const KMenu* menu, - const QString& entryName) const +void DolphinContextMenu::addVersionControlPluginActions() { - Q_ASSERT(menu != 0); - - const QList list = menu->actions(); - const uint count = list.count(); - for (uint i = 0; i < count; ++i) { - const QAction* action = list.at(i); - if (action->text() == entryName) { - return true; - } + const DolphinView* view = m_mainWindow->activeViewContainer()->view(); + const QList versionControlActions = view->versionControlActions(m_selectedItems); + if (!versionControlActions.isEmpty()) { + addActions(versionControlActions); + addSeparator(); } +} - return false; +void DolphinContextMenu::addCustomActions() +{ + addActions(m_customActions); } -#include "dolphincontextmenu.moc"