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

FileItemActions: Make plugins more intuitive and interactive

- Use new KIO functionality to display errors
- Use the Jobs for the operations instead of a separate process,
 this allows us to give better feedback and more precise error messages
- Focus newly created archives in file manager

See also: https://phabricator.kde.org/T12164
parent ef64cfdb
......@@ -97,11 +97,12 @@ target_link_libraries(compressfileitemaction
# extractfileitemaction plugin
kcoreaddons_add_plugin(extractfileitemaction
SOURCES extractfileitemaction.cpp
SOURCES extractfileitemaction.cpp batchextract.cpp ${CMAKE_CURRENT_BINARY_DIR}/ark_debug.cpp
JSON extractfileitemaction.json
INSTALL_NAMESPACE "kf5/kfileitemaction")
target_link_libraries(extractfileitemaction
kerfuffle
KF5::I18n
KF5::KIOWidgets)
KF5::KIOWidgets
KF5::KIOFileWidgets)
......@@ -2,6 +2,7 @@
* ark -- archiver for the KDE project
*
* Copyright (C) 2016 Elvis Angelaccio <elvis.angelaccio@kde.org>
* Copyright (C) 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
......@@ -31,9 +32,15 @@
#include <KPluginFactory>
#include <KService>
#include <kio_version.h>
#include <algorithm>
#include <KIO/OpenFileManagerWindowJob>
#include <QDir>
#include <QGuiApplication>
#include "pluginmanager.h"
#include "addtoarchive.h"
K_PLUGIN_CLASS_WITH_JSON(CompressFileItemAction, "compressfileitemaction.json")
......@@ -70,7 +77,7 @@ QList<QAction*> CompressFileItemAction::actions(const KFileItemListProperties& f
i18nc("@action:inmenu Part of Compress submenu in Dolphin context menu", "Here (as TAR.GZ)"),
parentWidget,
urlList,
QStringLiteral("ark --changetofirstpath --add --autofilename tar.gz %F")));
QStringLiteral("tar.gz")));
const QMimeType zipMime = QMimeDatabase().mimeTypeForName(QStringLiteral("application/zip"));
// Don't offer zip compression if no zip plugin is available.
......@@ -79,14 +86,14 @@ QList<QAction*> CompressFileItemAction::actions(const KFileItemListProperties& f
i18nc("@action:inmenu Part of Compress submenu in Dolphin context menu", "Here (as ZIP)"),
parentWidget,
urlList,
QStringLiteral("ark --changetofirstpath --add --autofilename zip %F")));
QStringLiteral("zip")));
}
compressMenu->addAction(createAction(icon,
i18nc("@action:inmenu Part of Compress submenu in Dolphin context menu", "Compress to..."),
parentWidget,
urlList,
QStringLiteral("ark --add --changetofirstpath --dialog %F")));
QString()));
QAction *compressMenuAction = new QAction(i18nc("@action:inmenu Compress submenu in Dolphin context menu", "Compress"), parentWidget);
compressMenuAction->setMenu(compressMenu);
......@@ -97,16 +104,34 @@ QList<QAction*> CompressFileItemAction::actions(const KFileItemListProperties& f
return actions;
}
QAction *CompressFileItemAction::createAction(const QIcon& icon, const QString& name, QWidget *parent, const QList<QUrl>& urls, const QString& exec)
QAction *CompressFileItemAction::createAction(const QIcon& icon, const QString& name, QWidget *parent, const QList<QUrl>& urls, const QString& fileExtension)
{
QAction *action = new QAction(icon, name, parent);
connect(action, &QAction::triggered, this, [exec, urls, parent]() {
KService::Ptr service(new KService(QStringLiteral("ark"), exec, QString()));
KIO::ApplicationLauncherJob *job = new KIO::ApplicationLauncherJob(service);
job->setUrls(urls);
job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, parent));
job->start();
connect(action, &QAction::triggered, this, [fileExtension, urls, name, parent, this]() {
auto *addToArchiveJob = new AddToArchive(parent);
addToArchiveJob->setChangeToFirstPath(true);
for (const QUrl &url : urls) {
addToArchiveJob->addInput(url);
}
if (!fileExtension.isEmpty()) {
addToArchiveJob->setAutoFilenameSuffix(fileExtension);
} else {
if (!addToArchiveJob->showAddDialog()) {
delete addToArchiveJob;
return;
}
}
addToArchiveJob->start();
connect(addToArchiveJob, &KJob::finished, this, [this, addToArchiveJob](){
if (addToArchiveJob->error() == 0) {
KIO::highlightInFileManager({QUrl::fromLocalFile(addToArchiveJob->fileName())});
} else if (!addToArchiveJob->errorString().isEmpty()) {
#if KIO_VERSION >= QT_VERSION_CHECK(5, 82, 0)
Q_EMIT error(addToArchiveJob->errorString());
#endif
}
});
});
return action;
......
......@@ -2,6 +2,7 @@
* ark -- archiver for the KDE project
*
* Copyright (C) 2016 Elvis Angelaccio <elvis.angelaccio@kde.org>
* Copyright (C) 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
......@@ -45,7 +46,7 @@ public:
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);
QAction *createAction(const QIcon& icon, const QString& name, QWidget *parent, const QList<QUrl>& urls, const QString& fileExtension);
Kerfuffle::PluginManager *m_pluginManager;
};
......
......@@ -2,6 +2,7 @@
* ark -- archiver for the KDE project
*
* Copyright (C) 2016 Elvis Angelaccio <elvis.angelaccio@kde.org>
* Copyright (C) 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
......@@ -23,15 +24,18 @@
#include <QFileInfo>
#include <QMenu>
#include <QGuiApplication>
#include <KDialogJobUiDelegate>
#include <KIO/ApplicationLauncherJob>
#include <KIO/OpenFileManagerWindowJob>
#include <KLocalizedString>
#include <KPluginFactory>
#include <KService>
#include <kio_version.h>
#include "mimetypes.h"
#include "pluginmanager.h"
#include "batchextract.h"
K_PLUGIN_CLASS_WITH_JSON(ExtractFileItemAction, "extractfileitemaction.json")
......@@ -72,7 +76,7 @@ QList<QAction*> ExtractFileItemAction::actions(const KFileItemListProperties& fi
i18nc("@action:inmenu Part of Extract submenu in Dolphin context menu", "Extract archive to..."),
parentWidget,
supportedUrls,
QStringLiteral("ark --batch --autodestination --dialog %F"));
AdditionalJobOptions::ShowDialog);
// #189177: disable "extract here" actions in read-only folders.
if (readOnlyParentDir) {
......@@ -84,7 +88,7 @@ QList<QAction*> ExtractFileItemAction::actions(const KFileItemListProperties& fi
i18nc("@action:inmenu Part of Extract submenu in Dolphin context menu", "Extract archive here"),
parentWidget,
supportedUrls,
QStringLiteral("ark --batch --autodestination %F")));
AdditionalJobOptions::None));
extractMenu->addAction(extractToAction);
......@@ -92,7 +96,7 @@ QList<QAction*> ExtractFileItemAction::actions(const KFileItemListProperties& fi
i18nc("@action:inmenu Part of Extract submenu in Dolphin context menu", "Extract archive here, autodetect subfolder"),
parentWidget,
supportedUrls,
QStringLiteral("ark --batch --autodestination --autosubfolder %F")));
AdditionalJobOptions::AutoSubfolder));
QAction *extractMenuAction = new QAction(i18nc("@action:inmenu Extract submenu in Dolphin context menu", "Extract"), parentWidget);
......@@ -105,18 +109,33 @@ QList<QAction*> ExtractFileItemAction::actions(const KFileItemListProperties& fi
return actions;
}
QAction *ExtractFileItemAction::createAction(const QIcon& icon, const QString& name, QWidget *parent, const QList<QUrl>& urls, const QString& exec)
QAction *ExtractFileItemAction::createAction(const QIcon& icon, const QString& name, QWidget *parent, const QList<QUrl>& urls, AdditionalJobOptions option)
{
QAction *action = new QAction(icon, name, parent);
connect(action, &QAction::triggered, this, [exec, urls, parent]() {
KService::Ptr service(new KService(QStringLiteral("ark"), exec, QString()));
KIO::ApplicationLauncherJob *job = new KIO::ApplicationLauncherJob(service);
job->setUrls(urls);
job->setUiDelegate(new KDialogJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, parent));
job->start();
connect(action, &QAction::triggered, this, [urls,name, option, parent,this]() {
auto *batchExtractJob = new BatchExtract(parent);
batchExtractJob->setDestinationFolder(QFileInfo(urls.first().toLocalFile()).path());
batchExtractJob->setOpenDestinationAfterExtraction(true);
if (option == AutoSubfolder) {
batchExtractJob->setAutoSubfolder(true);
} else if (option == ShowDialog) {
if (!batchExtractJob->showExtractDialog()) {
delete batchExtractJob;
return;
}
}
for (const QUrl &url : urls) {
batchExtractJob->addInput(url);
}
batchExtractJob->start();
connect(batchExtractJob, &KJob::finished, this, [this, batchExtractJob](){
if (!batchExtractJob->errorString().isEmpty()) {
#if KIO_VERSION >= QT_VERSION_CHECK(5, 82, 0)
Q_EMIT error(batchExtractJob->errorString());
#endif
}
});
});
return action;
}
......
......@@ -2,6 +2,7 @@
* ark -- archiver for the KDE project
*
* Copyright (C) 2016 Elvis Angelaccio <elvis.angelaccio@kde.org>
* Copyright (C) 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
......@@ -45,7 +46,12 @@ public:
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);
enum AdditionalJobOptions {
None,
ShowDialog,
AutoSubfolder,
};
QAction *createAction(const QIcon& icon, const QString& name, QWidget *parent, const QList<QUrl>& urls, AdditionalJobOptions option);
Kerfuffle::PluginManager *m_pluginManager;
};
......
......@@ -62,6 +62,11 @@ public:
void setChangeToFirstPath(bool value);
QString detectBaseName(const QVector<Archive::Entry*> &entries) const;
QString fileName()
{
return m_filename;
}
public Q_SLOTS:
bool addInput(const QUrl &url);
void setAutoFilenameSuffix(const QString& suffix);
......
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