X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/9bf03a3c48f4847b5e0417e3529bf071108cbf1e..5bee1889e1682f1e7ffe55e49beaf4544eaf7157:/src/dolphincontextmenu.cpp diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index cb389b19c..d4cf196b7 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -27,8 +27,7 @@ #include "dolphinremoveaction.h" #include -#include -#include +#include #include #include #include @@ -38,7 +37,7 @@ #include #include #include -#include +#include #include #include #include @@ -46,8 +45,10 @@ #include #include +#include #include #include +#include #include #include @@ -67,7 +68,7 @@ DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent, m_baseUrl(baseUrl), m_baseFileItem(0), m_selectedItems(), - m_selectedItemsProperties(0), + m_selectedItemsProperties(nullptr), m_context(NoContext), m_copyToMenu(parent), m_customActions(), @@ -83,7 +84,7 @@ DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent, DolphinContextMenu::~DolphinContextMenu() { delete m_selectedItemsProperties; - m_selectedItemsProperties = 0; + m_selectedItemsProperties = nullptr; } void DolphinContextMenu::setCustomActions(const QList& actions) @@ -123,7 +124,7 @@ DolphinContextMenu::Command DolphinContextMenu::open() void DolphinContextMenu::keyPressEvent(QKeyEvent *ev) { if (m_removeAction && ev->key() == Qt::Key_Shift) { - m_removeAction->update(); + m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed); } QMenu::keyPressEvent(ev); } @@ -131,7 +132,7 @@ void DolphinContextMenu::keyPressEvent(QKeyEvent *ev) void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev) { if (m_removeAction && ev->key() == Qt::Key_Shift) { - m_removeAction->update(); + m_removeAction->update(DolphinRemoveAction::ShiftState::Released); } QMenu::keyReleaseEvent(ev); } @@ -140,14 +141,14 @@ void DolphinContextMenu::openTrashContextMenu() { Q_ASSERT(m_context & TrashContext); - QAction* emptyTrashAction = new QAction(QIcon::fromTheme("trash-empty"), i18nc("@action:inmenu", "Empty Trash"), this); - KConfig trashConfig("trashrc", KConfig::SimpleConfig); + QAction* emptyTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this); + KConfig trashConfig(QStringLiteral("trashrc"), KConfig::SimpleConfig); emptyTrashAction->setEnabled(!trashConfig.group("Status").readEntry("Empty", true)); addAction(emptyTrashAction); addCustomActions(); - QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties"); + QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); addAction(propertiesAction); addShowMenuBarAction(); @@ -158,7 +159,7 @@ void DolphinContextMenu::openTrashContextMenu() if (uiDelegate.askDeleteConfirmation(QList(), KIO::JobUiDelegate::EmptyTrash, KIO::JobUiDelegate::DefaultConfirmation)) { KIO::Job* job = KIO::emptyTrash(); KJobWidgets::setWindow(job, m_mainWindow); - job->ui()->setAutoErrorHandlingEnabled(true); + job->uiDelegate()->setAutoErrorHandlingEnabled(true); } } } @@ -171,14 +172,15 @@ void DolphinContextMenu::openTrashItemContextMenu() QAction* restoreAction = new QAction(i18nc("@action:inmenu", "Restore"), m_mainWindow); addAction(restoreAction); - QAction* deleteAction = m_mainWindow->actionCollection()->action("delete"); + QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile)); addAction(deleteAction); - QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties"); + QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); addAction(propertiesAction); if (exec(m_pos) == restoreAction) { QList selectedUrls; + selectedUrls.reserve(m_selectedItems.count()); foreach (const KFileItem &item, m_selectedItems) { selectedUrls.append(item.url()); } @@ -193,10 +195,10 @@ void DolphinContextMenu::openItemContextMenu() { Q_ASSERT(!m_fileInfo.isNull()); - QAction* openParentAction = 0; - QAction* openParentInNewWindowAction = 0; - QAction* openParentInNewTabAction = 0; - QAction* addToPlacesAction = 0; + QAction* openParentAction = nullptr; + QAction* openParentInNewWindowAction = nullptr; + QAction* openParentInNewTabAction = nullptr; + QAction* addToPlacesAction = nullptr; const KFileItemListProperties& selectedItemsProps = selectedItemsProperties(); if (m_selectedItems.count() == 1) { @@ -213,36 +215,36 @@ void DolphinContextMenu::openItemContextMenu() QMenu* menu = newFileMenu->menu(); menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New")); - menu->setIcon(QIcon::fromTheme("document-new")); + menu->setIcon(QIcon::fromTheme(QStringLiteral("document-new"))); addMenu(menu); addSeparator(); // insert 'Open in new window' and 'Open in new tab' entries - addAction(m_mainWindow->actionCollection()->action("open_in_new_window")); - addAction(m_mainWindow->actionCollection()->action("open_in_new_tab")); + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window"))); + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab"))); // insert 'Add to Places' entry if (!placeExists(m_fileInfo.url())) { - addToPlacesAction = addAction(QIcon::fromTheme("bookmark-new"), + addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")), i18nc("@action:inmenu Add selected folder to places", "Add to Places")); } addSeparator(); - } else if (m_baseUrl.scheme().contains("search") || m_baseUrl.scheme().contains("timeline")) { - openParentAction = new QAction(QIcon::fromTheme("document-open-folder"), + } else if (m_baseUrl.scheme().contains(QStringLiteral("search")) || m_baseUrl.scheme().contains(QStringLiteral("timeline"))) { + openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18nc("@action:inmenu", "Open Path"), this); addAction(openParentAction); - openParentInNewWindowAction = new QAction(QIcon::fromTheme("window-new"), + openParentInNewWindowAction = new QAction(QIcon::fromTheme(QStringLiteral("window-new")), i18nc("@action:inmenu", "Open Path in New Window"), this); addAction(openParentInNewWindowAction); - openParentInNewTabAction = new QAction(QIcon::fromTheme("tab-new"), + openParentInNewTabAction = new QAction(QIcon::fromTheme(QStringLiteral("tab-new")), i18nc("@action:inmenu", "Open Path in New Tab"), this); @@ -251,8 +253,8 @@ void DolphinContextMenu::openItemContextMenu() addSeparator(); } else if (!DolphinView::openItemAsFolderUrl(m_fileInfo).isEmpty()) { // insert 'Open in new window' and 'Open in new tab' entries - addAction(m_mainWindow->actionCollection()->action("open_in_new_window")); - addAction(m_mainWindow->actionCollection()->action("open_in_new_tab")); + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_window"))); + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tab"))); addSeparator(); } @@ -268,7 +270,7 @@ void DolphinContextMenu::openItemContextMenu() if (selectionHasOnlyDirs) { // insert 'Open in new tab' entry - addAction(m_mainWindow->actionCollection()->action("open_in_new_tabs")); + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tabs"))); addSeparator(); } } @@ -281,19 +283,20 @@ void DolphinContextMenu::openItemContextMenu() fileItemActions.setItemListProperties(selectedItemsProps); addServiceActions(fileItemActions); - addFileItemPluginActions(); + fileItemActions.addPluginActionsTo(this); addVersionControlPluginActions(); // insert 'Copy To' and 'Move To' sub menus if (GeneralSettings::showCopyMoveMenu()) { - m_copyToMenu.setItems(m_selectedItems); + m_copyToMenu.setUrls(m_selectedItems.urlList()); m_copyToMenu.setReadOnly(!selectedItemsProps.supportsWriting()); + m_copyToMenu.setAutoErrorHandlingEnabled(true); m_copyToMenu.addActionsTo(this); } // insert 'Properties...' entry - QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties"); + QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); addAction(propertiesAction); QAction* activatedAction = exec(m_pos); @@ -303,8 +306,9 @@ void DolphinContextMenu::openItemContextMenu() if (selectedUrl.isValid()) { PlacesItemModel model; const QString text = selectedUrl.fileName(); - PlacesItem* item = model.createPlacesItem(text, selectedUrl); + PlacesItem* item = model.createPlacesItem(text, selectedUrl, KIO::iconNameForUrl(selectedUrl)); model.appendItemToGroup(item); + model.saveBookmarks(); } } else if (activatedAction == openParentAction) { m_command = OpenParentFolder; @@ -329,13 +333,13 @@ void DolphinContextMenu::openViewportContextMenu() // 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("new_window")); - addAction(m_mainWindow->actionCollection()->action("new_tab")); + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_window"))); + addAction(m_mainWindow->actionCollection()->action(QStringLiteral("new_tab"))); // Insert 'Add to Places' entry if exactly one item is selected QAction* addToPlacesAction = 0; if (!placeExists(m_mainWindow->activeViewContainer()->url())) { - addToPlacesAction = addAction(QIcon::fromTheme("bookmark-new"), + addToPlacesAction = addAction(QIcon::fromTheme(QStringLiteral("bookmark-new")), i18nc("@action:inmenu Add current folder to places", "Add to Places")); } @@ -351,13 +355,13 @@ void DolphinContextMenu::openViewportContextMenu() fileItemActions.setItemListProperties(baseUrlProperties); addServiceActions(fileItemActions); - addFileItemPluginActions(); + fileItemActions.addPluginActionsTo(this); addVersionControlPluginActions(); addCustomActions(); - QAction* propertiesAction = m_mainWindow->actionCollection()->action("properties"); + QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties")); addAction(propertiesAction); addShowMenuBarAction(); @@ -365,11 +369,18 @@ void DolphinContextMenu::openViewportContextMenu() QAction* action = exec(m_pos); if (addToPlacesAction && (action == addToPlacesAction)) { const DolphinViewContainer* container = m_mainWindow->activeViewContainer(); - if (container->url().isValid()) { + const QUrl url = container->url(); + if (url.isValid()) { PlacesItemModel model; - PlacesItem* item = model.createPlacesItem(container->placesText(), - container->url()); + QString icon; + if (container->isSearchModeEnabled()) { + icon = QStringLiteral("folder-saved-search-symbolic"); + } else { + icon = KIO::iconNameForUrl(url); + } + PlacesItem* item = model.createPlacesItem(container->placesText(), url, icon); model.appendItemToGroup(item); + model.saveBookmarks(); } } } @@ -386,8 +397,7 @@ void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& addSeparator(); // Insert 'Rename' - QAction* renameAction = collection->action("rename"); - addAction(renameAction); + addAction(collection->action(KStandardAction::name(KStandardAction::RenameFile))); // Insert 'Move to Trash' and/or 'Delete' if (properties.supportsDeleting()) { @@ -399,10 +409,10 @@ void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& if (showDeleteAction && showMoveToTrashAction) { delete m_removeAction; m_removeAction = 0; - addAction(m_mainWindow->actionCollection()->action("move_to_trash")); - addAction(m_mainWindow->actionCollection()->action("delete")); + 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("delete")); + addAction(m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile))); } else { if (!m_removeAction) { m_removeAction = new DolphinRemoveAction(this, m_mainWindow->actionCollection()); @@ -425,16 +435,11 @@ void DolphinContextMenu::addShowMenuBarAction() bool DolphinContextMenu::placeExists(const QUrl& url) const { - PlacesItemModel model; - - const int count = model.count(); - for (int i = 0; i < count; ++i) { - const QUrl placeUrl = model.placesItem(i)->url(); - if (placeUrl.matches(url, QUrl::StripTrailingSlash)) { - return true; - } - } - + // 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; } @@ -446,7 +451,7 @@ QAction* DolphinContextMenu::createPasteAction() const QMimeData *mimeData = QApplication::clipboard()->mimeData(); bool canPaste; const QString text = KIO::pasteActionText(mimeData, &canPaste, m_fileInfo); - action = new QAction(QIcon::fromTheme("edit-paste"), text, this); + action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this); action->setEnabled(canPaste); connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder); } else { @@ -467,7 +472,7 @@ KFileItemListProperties& DolphinContextMenu::selectedItemsProperties() const KFileItem DolphinContextMenu::baseFileItem() { if (!m_baseFileItem) { - m_baseFileItem = new KFileItem(KFileItem::Unknown, KFileItem::Unknown, m_baseUrl); + m_baseFileItem = new KFileItem(m_baseUrl); } return *m_baseFileItem; } @@ -477,71 +482,24 @@ void DolphinContextMenu::addServiceActions(KFileItemActions& fileItemActions) fileItemActions.setParentWidget(m_mainWindow); // insert 'Open With...' action or sub menu - fileItemActions.addOpenWithActionsTo(this, "DesktopEntryName != 'dolphin'"); + fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != 'dolphin'")); // insert 'Actions' sub menu fileItemActions.addServiceActionsTo(this); } -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 KService::Ptr& service, pluginServices) { - if (!showGroup.readEntry(service->desktopEntryName(), true)) { - // The plugin has been disabled - continue; - } - - // Old API (kdelibs-4.6.0 only) - KFileItemActionPlugin* plugin = service->createInstance(); - if (plugin) { - plugin->setParent(this); - addActions(plugin->actions(props, m_mainWindow)); - } - // New API (kdelibs >= 4.6.1) - KAbstractFileItemActionPlugin* abstractPlugin = service->createInstance(); - if (abstractPlugin) { - abstractPlugin->setParent(this); - addActions(abstractPlugin->actions(props, m_mainWindow)); - } - } -} - void DolphinContextMenu::addVersionControlPluginActions() { const DolphinView* view = m_mainWindow->activeViewContainer()->view(); const QList versionControlActions = view->versionControlActions(m_selectedItems); if (!versionControlActions.isEmpty()) { - foreach (QAction* action, versionControlActions) { - addAction(action); - } + addActions(versionControlActions); addSeparator(); } } void DolphinContextMenu::addCustomActions() { - foreach (QAction* action, m_customActions) { - addAction(action); - } + addActions(m_customActions); }