File indexing completed on 2024-05-19 05:00:32

0001 /*
0002  *   SPDX-FileCopyrightText: 2022 Méven Car <meven.car@kdemail.net>
0003  *
0004  *   SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "forgetfileitemaction.h"
0008 
0009 #include <QAction>
0010 
0011 #include <KDirNotify>
0012 #include <KFileItem>
0013 #include <KIO/SimpleJob>
0014 #include <KLocalizedString>
0015 #include <KPluginFactory>
0016 
0017 K_PLUGIN_CLASS_WITH_JSON(ForgetFileItemAction, "forgetfileitemaction.json")
0018 
0019 ForgetFileItemAction::ForgetFileItemAction(QObject *parent, const QVariantList &args)
0020     : KAbstractFileItemActionPlugin{parent}
0021 {
0022     Q_UNUSED(args)
0023 }
0024 
0025 QList<QAction *> ForgetFileItemAction::actions(const KFileItemListProperties &fileItemInfos, QWidget *parentWidget)
0026 {
0027     Q_UNUSED(parentWidget)
0028 
0029     const KFileItemList &fileItems = fileItemInfos.items();
0030     const auto url = fileItems.first().url();
0031 
0032     if (url.scheme() != QLatin1String("recentlyused")) {
0033         return {};
0034     }
0035 
0036     if (fileItems.count() == 1) {
0037         const auto path = url.path();
0038         // exclude "root" folders of recentlyused:/
0039         if (path.endsWith(QStringLiteral("/")) || path == QStringLiteral("/") || path == QStringLiteral("/files") || path == QStringLiteral("/locations")) {
0040             return {};
0041         }
0042     }
0043 
0044     QString text;
0045     if (fileItemInfos.isFile()) {
0046         text = i18ncp("@action:inmenu", "Forget File", "Forget Files", fileItems.size());
0047     } else {
0048         text = i18ncp("@action:inmenu", "Forget Location", "Forget Locations", fileItems.size());
0049     }
0050     QAction *forgetFileAction = new QAction(QIcon::fromTheme(QStringLiteral("edit-clear-history")), text, this);
0051     forgetFileAction->setWhatsThis(i18nc("@info:whatsthis",
0052                                          "Remove the selected file(s) or location(s) from the recently used "
0053                                          "list in Dolphin and in Plasma's menus. This does not remove or move the resource(s)."));
0054 
0055     connect(forgetFileAction, &QAction::triggered, this, [this, fileItems, fileItemInfos]() {
0056         QList<QUrl> urls;
0057         for (const auto &item : fileItems) {
0058             urls << item.targetUrl();
0059         }
0060 
0061         QByteArray packedArgs;
0062         QDataStream stream(&packedArgs, QIODevice::WriteOnly);
0063         stream << int(1); // Forget, see kio-extras/recentlyused/recentlyused.cpp special
0064         stream << urls;
0065 
0066         auto job = KIO::special(QUrl(QStringLiteral("recentlyused:/")), packedArgs);
0067 
0068         connect(job, &KJob::finished, this, [fileItems](const KJob *job) {
0069             if (!job->error()) {
0070                 org::kde::KDirNotify::emitFilesRemoved({fileItems.urlList()});
0071             }
0072         });
0073     });
0074 
0075     return {forgetFileAction};
0076 }
0077 
0078 #include "forgetfileitemaction.moc"
0079 #include "moc_forgetfileitemaction.cpp"