Commit 30299e00 authored by Alexander Lohnau's avatar Alexander Lohnau 💬 Committed by Elvis Angelaccio
Browse files

Reuse KFileItemActions instance during lifetime of dolphin

With frameworks/kio!411 the
plugin instances can be reused during the lifetime of the
KFileItemActions object. This improves performance and also allows
the plugins to emit errors, even if they run async.
parent dd7f3fb7
Pipeline #68533 passed with stage
in 4 minutes and 56 seconds
......@@ -33,7 +33,6 @@
#include <KPluginMetaData>
#include <KStandardAction>
#include <KToolBar>
#include <kio_version.h>
#include <QApplication>
#include <QClipboard>
......@@ -44,7 +43,8 @@
DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
const QPoint& pos,
const KFileItem& fileInfo,
const QUrl& baseUrl) :
const QUrl& baseUrl,
KFileItemActions *fileItemActions) :
QMenu(parent),
m_pos(pos),
m_mainWindow(parent),
......@@ -57,7 +57,8 @@ DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
m_copyToMenu(parent),
m_customActions(),
m_command(None),
m_removeAction(nullptr)
m_removeAction(nullptr),
m_fileItemActions(fileItemActions)
{
// The context menu either accesses the URLs of the selected items
// or the items itself. To increase the performance both lists are cached.
......@@ -180,7 +181,7 @@ void DolphinContextMenu::openTrashItemContextMenu()
}
}
void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions &fileItemActions)
void DolphinContextMenu::addDirectoryItemContextMenu()
{
// insert 'Open in new window' and 'Open in new tab' entries
const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
......@@ -192,7 +193,7 @@ void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions &fileItemA
}
// Insert 'Open With' entries
addOpenWithActions(fileItemActions);
addOpenWithActions();
// set up 'Create New' menu
DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow);
......@@ -221,21 +222,15 @@ void DolphinContextMenu::openItemContextMenu()
QAction* openParentInNewTabAction = nullptr;
const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
KFileItemActions fileItemActions;
fileItemActions.setParentWidget(m_mainWindow);
#if KIO_VERSION >= QT_VERSION_CHECK(5, 82, 0)
connect(&fileItemActions, &KFileItemActions::error, this, [this](const QString &errorMessage) {
m_mainWindow->activeViewContainer()->showMessage(errorMessage, DolphinViewContainer::Error);
});
#endif
fileItemActions.setItemListProperties(selectedItemsProps);
m_fileItemActions->setItemListProperties(selectedItemsProps);
if (m_selectedItems.count() == 1) {
// single files
if (m_fileInfo.isDir()) {
addDirectoryItemContextMenu(fileItemActions);
addDirectoryItemContextMenu();
} else if (m_context & TimelineContext || m_context & SearchContext) {
addOpenWithActions(fileItemActions);
addOpenWithActions();
openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
i18nc("@action:inmenu",
......@@ -258,7 +253,7 @@ void DolphinContextMenu::openItemContextMenu()
addSeparator();
} else {
// Insert 'Open With" entries
addOpenWithActions(fileItemActions);
addOpenWithActions();
}
if (m_fileInfo.isLink()) {
addAction(m_mainWindow->actionCollection()->action(QStringLiteral("show_target")));
......@@ -280,12 +275,12 @@ void DolphinContextMenu::openItemContextMenu()
addAction(m_mainWindow->actionCollection()->action(QStringLiteral("open_in_new_tabs")));
}
// Insert 'Open With" entries
addOpenWithActions(fileItemActions);
addOpenWithActions();
}
insertDefaultItemActions(selectedItemsProps);
addAdditionalActions(fileItemActions, selectedItemsProps);
addAdditionalActions(selectedItemsProps);
// insert 'Copy To' and 'Move To' sub menus
if (ContextMenuSettings::showCopyMoveMenu()) {
......@@ -317,14 +312,7 @@ void DolphinContextMenu::openViewportContextMenu()
const DolphinView* view = m_mainWindow->activeViewContainer()->view();
const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
KFileItemActions fileItemActions;
fileItemActions.setParentWidget(m_mainWindow);
#if KIO_VERSION >= QT_VERSION_CHECK(5, 82, 0)
connect(&fileItemActions, &KFileItemActions::error, this, [this](const QString &errorMessage) {
m_mainWindow->activeViewContainer()->showMessage(errorMessage, DolphinViewContainer::Error);
});
#endif
fileItemActions.setItemListProperties(baseUrlProperties);
m_fileItemActions->setItemListProperties(baseUrlProperties);
// Set up and insert 'Create New' menu
KNewFileMenu* newFileMenu = m_mainWindow->newFileMenu();
......@@ -335,7 +323,7 @@ void DolphinContextMenu::openViewportContextMenu()
// Show "open with" menu items even if the dir is empty, because there are legitimate
// use cases for this, such as opening an empty dir in Kate or VSCode or something
addOpenWithActions(fileItemActions);
addOpenWithActions();
QAction* pasteAction = createPasteAction();
if (pasteAction) {
......@@ -360,7 +348,7 @@ void DolphinContextMenu::openViewportContextMenu()
addSeparator();
}
addAdditionalActions(fileItemActions, baseUrlProperties);
addAdditionalActions(baseUrlProperties);
addCustomActions();
addSeparator();
......@@ -487,10 +475,10 @@ KFileItem DolphinContextMenu::baseFileItem()
return *m_baseFileItem;
}
void DolphinContextMenu::addOpenWithActions(KFileItemActions& fileItemActions)
void DolphinContextMenu::addOpenWithActions()
{
// insert 'Open With...' action or sub menu
fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp->desktopFileName()));
m_fileItemActions->addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp->desktopFileName()));
}
void DolphinContextMenu::addCustomActions()
......@@ -498,7 +486,7 @@ void DolphinContextMenu::addCustomActions()
addActions(m_customActions);
}
void DolphinContextMenu::addAdditionalActions(KFileItemActions &fileItemActions, const KFileItemListProperties &props)
void DolphinContextMenu::addAdditionalActions(const KFileItemListProperties &props)
{
addSeparator();
......@@ -506,7 +494,7 @@ void DolphinContextMenu::addAdditionalActions(KFileItemActions &fileItemActions,
if (props.isDirectory() && props.isLocal() && ContextMenuSettings::showOpenTerminal()) {
additionalActions << m_mainWindow->actionCollection()->action(QStringLiteral("open_terminal"));
}
fileItemActions.addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions);
m_fileItemActions->addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions);
const DolphinView* view = m_mainWindow->activeViewContainer()->view();
const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
......
......@@ -9,6 +9,7 @@
#include <KFileCopyToMenu>
#include <KFileItem>
#include <KFileItemActions>
#include <QMenu>
#include <QUrl>
......@@ -57,7 +58,8 @@ public:
DolphinContextMenu(DolphinMainWindow* parent,
const QPoint& pos,
const KFileItem& fileInfo,
const QUrl& baseUrl);
const QUrl& baseUrl,
KFileItemActions *fileItemActions);
~DolphinContextMenu() override;
......@@ -104,7 +106,7 @@ private:
/**
* Adds "Open With" actions
*/
void addOpenWithActions(KFileItemActions& fileItemActions);
void addOpenWithActions();
/**
* Adds custom actions e.g. like the "[x] Expandable Folders"-action
......@@ -116,7 +118,7 @@ private:
/**
* Add services, custom actions, plugins and version control items to the menu
*/
void addAdditionalActions(KFileItemActions &fileItemActions, const KFileItemListProperties &props);
void addAdditionalActions(const KFileItemListProperties &props);
struct Entry
{
......@@ -155,7 +157,8 @@ private:
Command m_command;
DolphinRemoveAction* m_removeAction; // Action that represents either 'Move To Trash' or 'Delete'
void addDirectoryItemContextMenu(KFileItemActions &fileItemActions);
void addDirectoryItemContextMenu();
KFileItemActions *m_fileItemActions;
};
......
......@@ -65,6 +65,8 @@
#include <KWindowSystem>
#include <KXMLGUIFactory>
#include <kio_version.h>
#include <QApplication>
#include <QClipboard>
#include <QCloseEvent>
......@@ -207,6 +209,13 @@ DolphinMainWindow::DolphinMainWindow() :
connect(KSycoca::self(), QOverload<>::of(&KSycoca::databaseChanged), this, &DolphinMainWindow::updateOpenPreferredSearchToolAction);
QTimer::singleShot(0, this, &DolphinMainWindow::updateOpenPreferredSearchToolAction);
m_fileItemActions.setParentWidget(this);
#if KIO_VERSION >= QT_VERSION_CHECK(5, 82, 0)
connect(&m_fileItemActions, &KFileItemActions::error, this, [this](const QString &errorMessage) {
showErrorMessage(errorMessage);
});
#endif
}
DolphinMainWindow::~DolphinMainWindow()
......@@ -1125,7 +1134,7 @@ void DolphinMainWindow::openContextMenu(const QPoint& pos,
const QUrl& url,
const QList<QAction*>& customActions)
{
QPointer<DolphinContextMenu> contextMenu = new DolphinContextMenu(this, pos, item, url);
QPointer<DolphinContextMenu> contextMenu = new DolphinContextMenu(this, pos, item, url, &m_fileItemActions);
contextMenu.data()->setCustomActions(customActions);
const DolphinContextMenu::Command command = contextMenu.data()->open();
......
......@@ -11,6 +11,7 @@
#include "dolphintabwidget.h"
#include <config-baloo.h>
#include <KFileItemActions>
#include <kio/fileundomanager.h>
#include <kxmlguiwindow.h>
......@@ -671,6 +672,8 @@ private:
KToolBarPopupAction* m_forwardAction;
QMenu m_searchTools;
KFileItemActions m_fileItemActions;
};
inline DolphinViewContainer* DolphinMainWindow::activeViewContainer() const
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment