Commit b884003b authored by Kai Uwe Broulik's avatar Kai Uwe Broulik 🍇

[Icon Applet] Parse jump list actions and open with actions on demand

Doing this on startup can easily add up to 20ms startup time for every icon pointing to a file.
It is not an issue with application links that don't have open with actions fortunately.

Differential Revision: https://phabricator.kde.org/D18925
parent 66b734df
......@@ -274,56 +274,7 @@ void IconApplet::populateFromDesktopFile(const QString &path)
delete m_openContainingFolderAction;
m_openContainingFolderAction = nullptr;
m_openWithActions.clear();
if (desktopFile.hasLinkType()) {
const QUrl &linkUrl = QUrl(desktopFile.readUrl());
if (!m_fileItemActions) {
m_fileItemActions = new KFileItemActions(this);
}
KFileItemListProperties itemProperties(KFileItemList({KFileItem(linkUrl)}));
m_fileItemActions->setItemListProperties(itemProperties);
if (!m_openWithMenu) {
m_openWithMenu.reset(new QMenu());
}
m_openWithMenu->clear();
m_fileItemActions->addOpenWithActionsTo(m_openWithMenu.data());
m_openWithActions = m_openWithMenu->actions();
if (KProtocolManager::supportsListing(linkUrl)) {
if (!m_openContainingFolderAction) {
m_openContainingFolderAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("Open Containing Folder"), this);
connect(m_openContainingFolderAction, &QAction::triggered, this, [this] {
KIO::highlightInFileManager({m_openContainingFolderAction->property("linkUrl").toUrl()});
});
}
m_openContainingFolderAction->setProperty("linkUrl", linkUrl);
}
}
m_jumpListActions.clear();
foreach (const QString &actionName, desktopFile.readActions()) {
const KConfigGroup &actionGroup = desktopFile.actionGroup(actionName);
if (!actionGroup.isValid() || !actionGroup.exists()) {
continue;
}
const QString &name = actionGroup.readEntry(QStringLiteral("Name"));
const QString &exec = actionGroup.readEntry(QStringLiteral("Exec"));
if (name.isEmpty() || exec.isEmpty()) {
continue;
}
QAction *action = new QAction(QIcon::fromTheme(actionGroup.readEntry("Icon")), name, this);
connect(action, &QAction::triggered, this, [this, exec] {
KRun::run(exec, {}, nullptr, m_name, m_iconName);
});
m_jumpListActions << action;
}
m_localPath = path;
......@@ -374,6 +325,36 @@ QString IconApplet::genericName() const
QList<QAction *> IconApplet::contextualActions()
{
QList<QAction *> actions;
if (m_localPath.isEmpty()) {
return actions;
}
KDesktopFile desktopFile(m_localPath);
if (m_jumpListActions.isEmpty()) {
const QStringList actions = desktopFile.readActions();
for (const QString &actionName : actions) {
const KConfigGroup &actionGroup = desktopFile.actionGroup(actionName);
if (!actionGroup.isValid() || !actionGroup.exists()) {
continue;
}
const QString name = actionGroup.readEntry(QStringLiteral("Name"));
const QString exec = actionGroup.readEntry(QStringLiteral("Exec"));
if (name.isEmpty() || exec.isEmpty()) {
continue;
}
QAction *action = new QAction(QIcon::fromTheme(actionGroup.readEntry("Icon")), name, this);
connect(action, &QAction::triggered, this, [this, exec] {
KRun::run(exec, {}, nullptr, m_name, m_iconName);
});
m_jumpListActions << action;
}
}
actions << m_jumpListActions;
if (!actions.isEmpty()) {
......@@ -384,6 +365,35 @@ QList<QAction *> IconApplet::contextualActions()
actions << m_separatorAction;
}
if (desktopFile.hasLinkType()) {
const QUrl linkUrl = QUrl(desktopFile.readUrl());
if (m_openWithActions.isEmpty()) {
if (!m_fileItemActions) {
m_fileItemActions = new KFileItemActions(this);
}
KFileItemListProperties itemProperties(KFileItemList({KFileItem(linkUrl)}));
m_fileItemActions->setItemListProperties(itemProperties);
if (!m_openWithMenu) {
m_openWithMenu.reset(new QMenu());
}
m_openWithMenu->clear();
m_fileItemActions->addOpenWithActionsTo(m_openWithMenu.data());
m_openWithActions = m_openWithMenu->actions();
}
if (!m_openContainingFolderAction) {
if (KProtocolManager::supportsListing(linkUrl)) {
m_openContainingFolderAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")), i18n("Open Containing Folder"), this);
connect(m_openContainingFolderAction, &QAction::triggered, this, [this, linkUrl] {
KIO::highlightInFileManager({linkUrl});
});
}
}
}
actions << m_openWithActions;
if (m_openContainingFolderAction) {
......
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