Commit ea6a8ff0 authored by Méven Car's avatar Méven Car
Browse files

Add ForgetFileItemAction

BUG: 447312
parent 4e3ed5db
Pipeline #183743 passed with stage
in 1 minute and 51 seconds
......@@ -36,3 +36,12 @@ target_link_libraries(recentlyused
KF5::ActivitiesStats)
install(TARGETS recentlyused DESTINATION ${KDE_INSTALL_PLUGINDIR}/kf${QT_MAJOR_VERSION}/kio)
# forgetfileitemaction plugin
kcoreaddons_add_plugin(forgetfileitemaction
SOURCES forgetfileitemaction.cpp
INSTALL_NAMESPACE "kf${QT_MAJOR_VERSION}/kfileitemaction")
target_link_libraries(forgetfileitemaction
KF5::I18n
KF5::KIOWidgets)
/*
* SPDX-FileCopyrightText: 2022 Méven Car <meven.car@kdemail.net>
*
* SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "forgetfileitemaction.h"
#include <QAction>
#include <KFileItem>
#include <KLocalizedString>
#include <KPluginFactory>
#include <KIO/SimpleJob>
#include <KDirNotify>
K_PLUGIN_CLASS_WITH_JSON(ForgetFileItemAction, "forgetfileitemaction.json")
ForgetFileItemAction::ForgetFileItemAction(QObject *parent, const QVariantList &args)
: KAbstractFileItemActionPlugin{parent}
{
Q_UNUSED(args)
}
QList<QAction *> ForgetFileItemAction::actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget)
{
Q_UNUSED(parentWidget)
const KFileItemList &fileItems = fileItemInfos.items();
const auto url = fileItems.first().url();
if (url.scheme() != QLatin1String("recentlyused")) {
return {};
}
if (fileItems.count() == 1) {
const auto path = url.path();
// exclude "root" folders of recentlyused:/
if (path == QStringLiteral("/") ||
path == QStringLiteral("/files") ||
path == QStringLiteral("/locations")) {
return {};
}
}
QString text;
if (fileItemInfos.isFile()) {
text = i18ncp("@action:inmenu", "Forget File", "Forget Files", fileItems.size());
} else {
text = i18ncp("@action:inmenu", "Forget Location", "Forget Locations", fileItems.size());
}
QAction* forgetFileAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-clear-history")), text, this);
forgetFileAction->setWhatsThis(i18nc("@info:whatsthis", "Remove the selected file(s) or location(s) from the recently used "
"list in Dolphin and in Plasma's menus. This does not remove or move the resource(s)."));
connect(forgetFileAction, &QAction::triggered, this, [this, fileItems, fileItemInfos](){
QList<QUrl> urls;
for (const auto &item: fileItems) {
urls << item.targetUrl();
}
QByteArray packedArgs;
QDataStream stream(&packedArgs, QIODevice::WriteOnly);
stream << int(1); // Forget, see kio-extras/recentlyused/recentlyused.cpp special
stream << urls;
auto job = KIO::special(QUrl(QStringLiteral("recentlyused:/")), packedArgs);
connect(job, &KJob::finished, this, [fileItems](const KJob *job) {
if (!job->error()) {
org::kde::KDirNotify::emitFilesRemoved({fileItems.urlList()});
}
});
});
return {forgetFileAction};
}
#include "forgetfileitemaction.moc"
/*
* SPDX-FileCopyrightText: 2022 Méven Car <meven.car@kdemail.net>
*
* SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#ifndef FORGETFILEITEMACTION_H
#define FORGETFILEITEMACTION_H
#include <KAbstractFileItemActionPlugin>
#include <KFileItemListProperties>
class QAction;
class QWidget;
class ForgetFileItemAction : public KAbstractFileItemActionPlugin
{
Q_OBJECT
public:
explicit ForgetFileItemAction(QObject *parent, const QVariantList &args);
QList<QAction *> actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget) override;
private:
QAction *createAction(const QIcon &icon, const QString &name, QWidget *parent, const QList<QUrl> &urls, const QString &exec);
};
#endif // FORGETFILEITEMACTION_H
{
"KPlugin": {
"Icon": "edit-clear-history",
"MimeTypes": [
"application/octet-stream",
"inode/directory"
],
"Name": "Forget items from Recent Documents and Recent Locations",
"ServiceTypes": [
"KFileItemAction/Plugin"
]
},
"X-KDE-Show-In-Submenu": "true",
"X-KDE-Protocol": "recentlyused"
}
......@@ -163,7 +163,7 @@ ResultModel *runQuery(const QUrl &url)
return new ResultModel(query);
}
KIO::UDSEntry RecentlyUsed::udsEntryFromResource(const QString &resource, const QString &mimeType)
KIO::UDSEntry RecentlyUsed::udsEntryFromResource(const QString &resource, const QString &mimeType, int row)
{
qCDebug(KIO_RECENTLYUSED_LOG) << "udsEntryFromResource" << resource;
......@@ -179,8 +179,14 @@ KIO::UDSEntry RecentlyUsed::udsEntryFromResource(const QString &resource, const
if (sp->exec()) {
uds = sp->statResult();
}
uds.fastInsert(KIO::UDSEntry::UDS_URL, resourceUrl.toString());
// replace name with a technical unique name
const auto name = uds.stringValue(KIO::UDSEntry::UDS_NAME);
uds.replace(KIO::UDSEntry::UDS_NAME, QStringLiteral("%1-%2").arg(name).arg(row));
uds.reserve(uds.count() + 4);
uds.fastInsert(KIO::UDSEntry::UDS_DISPLAY_NAME, name);
uds.fastInsert(KIO::UDSEntry::UDS_MIME_TYPE, mimeType);
uds.fastInsert(KIO::UDSEntry::UDS_TARGET_URL, resourceUrl.toString());
uds.fastInsert(KIO::UDSEntry::UDS_LOCAL_PATH, resource);
return uds;
}
......@@ -199,12 +205,12 @@ void RecentlyUsed::listDir(const QUrl &url)
KIO::UDSEntryList udslist;
udslist.reserve(model->rowCount());
for (int r = 0; r < model->rowCount(); ++r) {
QModelIndex index = model->index(r, 0);
for (int row = 0; row < model->rowCount(); ++row) {
QModelIndex index = model->index(row, 0);
QString resource = model->data(index, ResultModel::ResourceRole).toString();
QString mimeType = model->data(index, ResultModel::MimeType).toString();
udslist << udsEntryFromResource(resource, mimeType);
udslist << udsEntryFromResource(resource, mimeType, row);
}
listEntries(udslist);
......
......@@ -7,7 +7,7 @@
#ifndef RECENTLYUSED_H
#define RECENTLYUSED_H
#include <kio/slavebase.h>
#include <KIO/SlaveBase>
/**
* Implements recentlyused:/ ioslave
......@@ -78,9 +78,8 @@
*
* @brief The RecentlyUsed implements an ioslave to access recently used files or directories
*/
class RecentlyUsed : public QObject, public KIO::SlaveBase
class RecentlyUsed : public KIO::SlaveBase
{
Q_OBJECT
public:
RecentlyUsed(const QByteArray &pool, const QByteArray &app);
~RecentlyUsed() override;
......@@ -106,7 +105,12 @@ protected:
void special(const QByteArray &data) override;
private:
KIO::UDSEntry udsEntryFromResource(const QString &resource, const QString &mimeType);
/*
* Creates a udsEntry for recentlyused:/
*
* row is the row number in the ResultModel for the recentlyused:/ url being queried
*/
KIO::UDSEntry udsEntryFromResource(const QString &resource, const QString &mimeType, int row);
KIO::UDSEntry udsEntryForRoot(const QString &dirName, const QString &iconName);
};
......
......@@ -3,7 +3,6 @@
"recentlyused": {
"Class": ":local",
"Icon": "document-open-recent",
"ProxiedBy": "file",
"X-DocPath": "kioslave5/recentlyused/index.html",
"copyFromFile": false,
"copyToFile": false,
......
Supports Markdown
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