Commit c369264d authored by Elvis Angelaccio's avatar Elvis Angelaccio

Port service menus to KFileItemAction

The Compress and Extract service menus shown in Dolphin's context menu are now
provided by two new KFileItemAction plugins. This allows us to do a lot of
useful checks before actually showing the actions in the context menu.

We can also stop hardcoding the supported mimetypes for the Extract
menu. This is actually required in order to support the Extract menu when more
than one archive is selected. In this case Dolphin sends to the kfileitemaction
plugins the
generic 'application/octet-stream' mimetype, so we have to manually check
anyway the mimetype of any given URL (to be sure that Ark supports it).

This also paves the way to dynamically generate the service menus (e.g. based
on some kind of user preset).

BUG: 189177
BUG: 268163
FIXED-IN: 16.08.0

CCBUG: 348582

Differential Revision: D1761
parent 65895fc6
......@@ -39,13 +39,7 @@ configure_file(
${CMAKE_CURRENT_BINARY_DIR}/ark_dndextract.desktop
)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/ark_servicemenu.desktop.cmake
${CMAKE_CURRENT_BINARY_DIR}/ark_servicemenu.desktop
)
install(TARGETS ark ${KDE_INSTALL_TARGETS_DEFAULT_ARGS})
install(FILES ark_addtoservicemenu.desktop ${CMAKE_CURRENT_BINARY_DIR}/ark_servicemenu.desktop DESTINATION ${KDE_INSTALL_KSERVICES5DIR}/ServiceMenus)
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/org.kde.ark.desktop DESTINATION ${KDE_INSTALL_APPDIR})
install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/org.kde.ark.appdata.xml DESTINATION ${KDE_INSTALL_METAINFODIR})
install(FILES arkui.rc DESTINATION ${KDE_INSTALL_KXMLGUI5DIR}/ark)
......@@ -59,3 +53,25 @@ kcoreaddons_desktop_to_json(extracthere ${CMAKE_CURRENT_BINARY_DIR}/ark_dndextra
target_link_libraries(extracthere kerfuffle KF5::Parts KF5::KIOWidgets KF5::KIOFileWidgets)
install(TARGETS extracthere DESTINATION ${KDE_INSTALL_PLUGINDIR}/kf5/kio_dnd)
# compressfileitemaction plugin
add_library(compressfileitemaction MODULE compressfileitemaction.cpp)
target_link_libraries(compressfileitemaction
kerfuffle
KF5::I18n
KF5::KIOWidgets)
install(TARGETS compressfileitemaction DESTINATION ${KDE_INSTALL_PLUGINDIR})
# extractfileitemaction plugin
add_library(extractfileitemaction MODULE extractfileitemaction.cpp)
target_link_libraries(extractfileitemaction
kerfuffle
KF5::I18n
KF5::KIOWidgets)
install(TARGETS extractfileitemaction DESTINATION ${KDE_INSTALL_PLUGINDIR})
[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=all/all;
Actions=compressHereAsTar;compressHereAsZip;compressTo;
X-KDE-Require=Write
X-KDE-Submenu=Compress
X-KDE-Submenu[ar]=اضغط
X-KDE-Submenu[ast]=Comprimir
X-KDE-Submenu[bg]=Компресиране
X-KDE-Submenu[bs]=Kompresuj
X-KDE-Submenu[ca]=Compressió
X-KDE-Submenu[ca@valencia]=Compressió
X-KDE-Submenu[cs]=Zkomprimovat
X-KDE-Submenu[da]=Komprimér
X-KDE-Submenu[de]=Komprimieren
X-KDE-Submenu[el]=Συμπίεση
X-KDE-Submenu[en_GB]=Compress
X-KDE-Submenu[es]=Comprimir
X-KDE-Submenu[et]=Pakkimine
X-KDE-Submenu[eu]=Konprimatu
X-KDE-Submenu[fi]=Pakkaa
X-KDE-Submenu[fr]=Compresser
X-KDE-Submenu[ga]=Comhbhrúigh
X-KDE-Submenu[gl]=Comprimir
X-KDE-Submenu[he]=דחוס
X-KDE-Submenu[hne]=संपीडित करव
X-KDE-Submenu[hr]=Zapakiraj
X-KDE-Submenu[hu]=Tömörítés
X-KDE-Submenu[ia]=Comprime
X-KDE-Submenu[id]=Kompres
X-KDE-Submenu[it]=Comprimi
X-KDE-Submenu[ja]=圧縮
X-KDE-Submenu[kk]=Сығу
X-KDE-Submenu[km]=បង្ហាប់
X-KDE-Submenu[ko]=압축하기
X-KDE-Submenu[lt]=Suspausti
X-KDE-Submenu[lv]=Saspiest
X-KDE-Submenu[mr]=संक्षिप्त करा
X-KDE-Submenu[nb]=Komprimer
X-KDE-Submenu[nds]=Komprimeren
X-KDE-Submenu[nl]=Comprimeren
X-KDE-Submenu[nn]=Komprimer
X-KDE-Submenu[pa]=ਕੰਪਰੈੱਸ
X-KDE-Submenu[pl]=Spakuj
X-KDE-Submenu[pt]=Comprimir
X-KDE-Submenu[pt_BR]=Compactar
X-KDE-Submenu[ro]=Comprimă
X-KDE-Submenu[ru]=Упаковать
X-KDE-Submenu[sk]=Komprimovať
X-KDE-Submenu[sl]=Stisni
X-KDE-Submenu[sq]=Ngjish
X-KDE-Submenu[sr]=Компресуј
X-KDE-Submenu[sr@ijekavian]=Компресуј
X-KDE-Submenu[sr@ijekavianlatin]=Kompresuj
X-KDE-Submenu[sr@latin]=Kompresuj
X-KDE-Submenu[sv]=Komprimera
X-KDE-Submenu[th]=บีบข้อมูล
X-KDE-Submenu[tr]=Sıkıştır
X-KDE-Submenu[ug]=پرېس
X-KDE-Submenu[uk]=Стиснути
X-KDE-Submenu[wa]=Rastrinde
X-KDE-Submenu[x-test]=xxCompressxx
X-KDE-Submenu[zh_CN]=压缩
X-KDE-Submenu[zh_TW]=壓縮
X-KDE-StartupNotify=false
X-KDE-Priority=TopLevel
[Desktop Action compressHereAsTar]
Name=Here (as TAR.GZ)
Name[ast]=Equí (como TAR.GZ)
Name[ca]=Aquí (com a TAR.GZ)
Name[ca@valencia]=Ací (com a TAR.GZ)
Name[cs]=Zde (jako TAR.GZ)
Name[da]=Her (som TAR.GZ)
Name[de]=Hier (Als TAR.GZ-Archiv)
Name[en_GB]=Here (as TAR.GZ)
Name[es]=Aquí (como TAR.GZ)
Name[et]=Siia (TAR.GZ-arhiivina)
Name[fi]=Tähän (TAR.GZ-arkistoksi)
Name[fr]=Ici (en tar.gz)
Name[gl]=Aquí (como TAR.GZ)
Name[he]=לכאן (בפורמט TAR.GZ)
Name[hu]=Ide (tar.gz-ként)
Name[it]=Qui (come TAR.GZ)
Name[ko]=여기(TAR.GZ로)
Name[lt]=Čia (TAR.GZ)
Name[nl]=Hier (als TAR.GZ)
Name[pl]=Tutaj (jako TAR.GZ)
Name[pt]=Aqui (como TAR.GZ)
Name[pt_BR]=Aqui (como TAR.GZ)
Name[sk]=Sem (ako TAR.GZ)
Name[sl]=Sem (kot TAR.GZ)
Name[sr]=овде (као тар.гз)
Name[sr@ijekavian]=овде (као тар.гз)
Name[sr@ijekavianlatin]=ovde (kao tar.gz)
Name[sr@latin]=ovde (kao tar.gz)
Name[sv]=Här (som TAR.GZ)
Name[tr]=Buraya (TAR.GZ olarak)
Name[uk]=Сюди (як TAR.GZ)
Name[x-test]=xxHere (as TAR.GZ)xx
Name[zh_CN]=此处 (作为 TAR.GZ)
Icon=ark
Exec=ark --changetofirstpath --add --autofilename tar.gz %F
[Desktop Action compressHereAsZip]
Name=Here (as ZIP)
Name[ast]=Equí (como ZIP)
Name[ca]=Aquí (com a ZIP)
Name[ca@valencia]=Ací (com a ZIP)
Name[cs]=Zde (jako ZIP)
Name[da]=Her (som ZIP)
Name[de]=Hier (Als ZIP-Archiv)
Name[en_GB]=Here (as ZIP)
Name[es]=Aquí (como ZIP)
Name[et]=Siia (ZIP-arhiivina)
Name[fi]=Tähän (ZIP-arkistoksi)
Name[fr]=Ici (en zip)
Name[gl]=Aquí (como ZIP)
Name[he]=לכאן (בפורמט ZIP)
Name[hu]=Ide (zip-ként)
Name[it]=Qui (come ZIP)
Name[ko]=여기(ZIP으로)
Name[lt]=Čia (ZIP)
Name[nl]=Hier (als ZIP)
Name[pl]=Tutaj (jako ZIP)
Name[pt]=Aqui (como ZIP)
Name[pt_BR]=Aqui (como ZIP)
Name[sk]=Sem (ako ZIP)
Name[sl]=Sem (kot ZIP)
Name[sr]=овде (као ЗИП)
Name[sr@ijekavian]=овде (као ЗИП)
Name[sr@ijekavianlatin]=ovde (kao ZIP)
Name[sr@latin]=ovde (kao ZIP)
Name[sv]=Här (som ZIP)
Name[tr]=Buraya (ZIP olarak)
Name[uk]=Сюди (як ZIP)
Name[x-test]=xxHere (as ZIP)xx
Name[zh_CN]=此处 (作为 ZIP)
Icon=ark
Exec=ark --changetofirstpath --add --autofilename zip %F
[Desktop Action compressTo]
Name=Compress to...
Name[ast]=Comprimir en...
Name[ca]=Comprimeix a...
Name[ca@valencia]=Comprimeix a...
Name[cs]=Zkomprimovat do...
Name[da]=Komprimér til...
Name[de]=Komprimieren nach ...
Name[en_GB]=Compress to...
Name[es]=Comprimir en...
Name[et]=Paki asukohta...
Name[fi]=Pakkaa…
Name[fr]=Compresser vers...
Name[gl]=Comprimir en…
Name[he]=דחוס ל...
Name[hu]=Tömörítés ide…
Name[ia]=Comprime a...
Name[it]=Comprimi in...
Name[ko]=다음으로 압축하기...
Name[lt]=Suspausti...
Name[nl]=Comprimeren naar...
Name[pl]=Spakuj do...
Name[pt]=Comprimir Para...
Name[pt_BR]=Compactar para...
Name[sk]=Komprimovať do...
Name[sl]=Stisni v ...
Name[sr]=другде...
Name[sr@ijekavian]=другдје...
Name[sr@ijekavianlatin]=drugdje...
Name[sr@latin]=drugde...
Name[sv]=Komprimera till...
Name[tr]=Sıkıştır...
Name[uk]=Стиснути…
Name[x-test]=xxCompress to...xx
Name[zh_CN]=压缩到...
Icon=ark
Exec=ark --add --changetofirstpath --dialog %F
[Desktop Entry]
Type=Service
ServiceTypes=KonqPopupMenu/Plugin
MimeType=@SUPPORTED_ARK_MIMETYPES@
Actions=arkAutoExtractHere;arkExtractTo;arkExtractHere;
X-KDE-Priority=TopLevel
X-KDE-StartupNotify=false
X-KDE-Submenu=Extract
X-KDE-Submenu[ar]=استخرِج
X-KDE-Submenu[ast]=Estrayer
X-KDE-Submenu[bg]=Извличане
X-KDE-Submenu[bs]=Raspakivanje
X-KDE-Submenu[ca]=Extreu
X-KDE-Submenu[ca@valencia]=Extreu
X-KDE-Submenu[cs]=Rozbalit
X-KDE-Submenu[da]=Pak ud
X-KDE-Submenu[de]=Entpacken
X-KDE-Submenu[el]=Εξαγωγή
X-KDE-Submenu[en_GB]=Extract
X-KDE-Submenu[es]=Extraer
X-KDE-Submenu[et]=Lahtipakkimine
X-KDE-Submenu[eu]=Atera
X-KDE-Submenu[fi]=Pura
X-KDE-Submenu[fr]=Extraire
X-KDE-Submenu[ga]=Bain amach
X-KDE-Submenu[gl]=Extraer
X-KDE-Submenu[he]=חלץ
X-KDE-Submenu[hr]=Otpakiraj
X-KDE-Submenu[hu]=Kibontás
X-KDE-Submenu[ia]=Extrahe
X-KDE-Submenu[it]=Estrai
X-KDE-Submenu[ja]=展開
X-KDE-Submenu[kk]=Тарқату
X-KDE-Submenu[km]=ស្រង់ចេញ​
X-KDE-Submenu[ko]=압축 풀기
X-KDE-Submenu[lt]=Išpakuoti
X-KDE-Submenu[mr]=पुर्ववत करा
X-KDE-Submenu[nb]=Pakk ut
X-KDE-Submenu[nds]=Utpacken
X-KDE-Submenu[nl]=Uitpakken
X-KDE-Submenu[pa]=ਇੱਥੇ ਖਿਲਾਰੋ
X-KDE-Submenu[pl]=Wypakuj
X-KDE-Submenu[pt]=Extrair
X-KDE-Submenu[pt_BR]=Extrair
X-KDE-Submenu[ro]=Extrage
X-KDE-Submenu[ru]=Распаковать
X-KDE-Submenu[sk]=Rozbaliť
X-KDE-Submenu[sl]=Razširi
X-KDE-Submenu[sr]=Распакуј
X-KDE-Submenu[sr@ijekavian]=Распакуј
X-KDE-Submenu[sr@ijekavianlatin]=Raspakuj
X-KDE-Submenu[sr@latin]=Raspakuj
X-KDE-Submenu[sv]=Packa upp
X-KDE-Submenu[tr]=Çıkart
X-KDE-Submenu[ug]=ئايرىش
X-KDE-Submenu[uk]=Видобути
X-KDE-Submenu[x-test]=xxExtractxx
X-KDE-Submenu[zh_CN]=解压缩
X-KDE-Submenu[zh_TW]=解開
[Desktop Action arkExtractHere]
Name=Extract archive here
Name[ast]=Estrayer equí l'archivu
Name[ca]=Extreu l'arxiu aquí
Name[ca@valencia]=Extreu l'arxiu ací
Name[cs]=Rozbalit archiv sem
Name[da]=Udpak arkiv her
Name[de]=Archiv hierher entpacken
Name[en_GB]=Extract archive here
Name[es]=Extraer archivo comprimido aquí
Name[et]=Paki arhiiv siia lahti
Name[fi]=Pura arkisto tähän
Name[fr]=Extraire l'archive ici
Name[gl]=Extraer o arquivo aquí
Name[he]=חלץ ארכיון לכאן
Name[ia]=Extrahe archivo hic
Name[it]=Estrai l'archivio qui
Name[ko]=여기에 압축 풀기
Name[lt]=Išpakuoti čia
Name[nl]=Archief hier uitpakken
Name[pl]=Wypakuj archiwum tutaj
Name[pt]=Extrair o pacote para aqui
Name[pt_BR]=Extrair arquivo aqui
Name[sk]=Rozbaliť archív sem
Name[sl]=Razširi arhiv sem
Name[sr]=Распакуј архиву овде
Name[sr@ijekavian]=Распакуј архиву овдје
Name[sr@ijekavianlatin]=Raspakuj arhivu ovdje
Name[sr@latin]=Raspakuj arhivu ovde
Name[sv]=Packa upp arkiv här
Name[uk]=Видобути архів сюди
Name[x-test]=xxExtract archive herexx
Name[zh_CN]=在此解压缩归档
Icon=ark
Exec=ark --batch --autodestination %F
[Desktop Action arkExtractTo]
Name=Extract archive to...
Name[ast]=Estrayer l'archivu en...
Name[ca]=Extreu l'arxiu a...
Name[ca@valencia]=Extreu l'arxiu a...
Name[cs]=Rozbalit archiv do...
Name[da]=Udpak arkiv til...
Name[de]=Archiv entpacken nach ...
Name[en_GB]=Extract archive to...
Name[es]=Extraer archivo comprimido en...
Name[et]=Paki arhiiv lahti asukohta...
Name[fi]=Pura arkisto…
Name[fr]=Extraire l'archive vers...
Name[gl]=Extraer o arquivo en…
Name[he]=חלץ ל...
Name[ia]=Extrahe archivo in...
Name[it]=Estrai l'archivio in...
Name[ko]=다음 경로에 압축 풀기...
Name[lt]=Išpakuoti į...
Name[nl]=Archief uitpakken naar...
Name[pl]=Wypakuj archiwum do...
Name[pt]=Extrair o pacote para...
Name[pt_BR]=Extrair arquivo para...
Name[sk]=Rozbaliť archív do...
Name[sl]=Razširi arhiv v ...
Name[sr]=Распакуј архиву у...
Name[sr@ijekavian]=Распакуј архиву у...
Name[sr@ijekavianlatin]=Raspakuj arhivu u...
Name[sr@latin]=Raspakuj arhivu u...
Name[sv]=Packa upp arkiv i...
Name[uk]=Видобути архів до…
Name[x-test]=xxExtract archive to...xx
Name[zh_CN]=解压缩归档到...
Icon=ark
Exec=ark --batch --autodestination --dialog %F
[Desktop Action arkAutoExtractHere]
Name=Extract archive here, autodetect subfolder
Name[ast]=Estrayer equí l'archivu, autodeteutar socarpeta
Name[ca]=Extreu l'arxiu aquí, detecta automàticament la subcarpeta
Name[ca@valencia]=Extreu l'arxiu ací, detecta automàticament la subcarpeta
Name[cs]=Rozbalit archiv sem, automaticky detekovat podsložku
Name[da]=Udpak arkiv her og find automatisk undermappe
Name[de]=Archiv hierher entpacken, Unterordner selbständig ermitteln
Name[en_GB]=Extract archive here, autodetect subfolder
Name[es]=Extraer archivo comprimido aquí, autodetectar subcarpeta
Name[et]=Paki arhiiv siia lahti, tuvasta automaatselt alamkataloog
Name[fi]=Pura arkisto tähän, tunnista alikansio automaattisesti
Name[fr]=Extraire l'archive ici, auto-détecter les sous-dossiers
Name[gl]=Extraer o arquivo aquí, detectar o subcartafol
Name[he]=חלץ לכאן, בחר אוטומטית תיקיה לחילוץ
Name[ia]=Extrahe archivo hic, auto-releva subdossier
Name[it]=Estrai l'archivio qui, autorileva la sottocartella
Name[ko]=하위 폴더를 감지해서 여기에 압축 풀기
Name[lt]=Išpakuoti čia, į poaplankį
Name[nl]=Archief hier uitpakken, submap autodetecteren
Name[pl]=Wypakuj archiwum tutaj, wykryj podkatalogi
Name[pt]=Extrair o pacote aqui com detecção da sub-pasta
Name[pt_BR]=Extrair aqui detectando subpasta
Name[sk]=Rozbaliť archív sem, automaticky určiť podpriečinok
Name[sl]=Razširi arhiv sem, samodejno zaznaj podmapo
Name[sr]=Распакуј архиву овде, погоди потфасциклу
Name[sr@ijekavian]=Распакуј архиву овдје, погоди потфасциклу
Name[sr@ijekavianlatin]=Raspakuj arhivu ovdje, pogodi potfasciklu
Name[sr@latin]=Raspakuj arhivu ovde, pogodi potfasciklu
Name[sv]=Packa upp arkiv här, detektera underkatalog automatiskt
Name[uk]=Видобути архів сюди, автоматично визначити підтеку
Name[x-test]=xxExtract archive here, autodetect subfolderxx
Name[zh_CN]=在此解压缩归档,自动探测子文件夹
Icon=ark
Exec=ark --batch --autodestination --autosubfolder %F
/*
* ark -- archiver for the KDE project
*
* Copyright (C) 2016 Elvis Angelaccio <elvis.angelaccio@kdemail.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "compressfileitemaction.h"
#include <QMenu>
#include <QMimeDatabase>
#include <KPluginFactory>
#include <KLocalizedString>
#include <KRun>
#include "pluginmanager.h"
K_PLUGIN_FACTORY_WITH_JSON(CompressFileItemActionFactory, "compressfileitemaction.json", registerPlugin<CompressFileItemAction>();)
using namespace Kerfuffle;
CompressFileItemAction::CompressFileItemAction(QObject* parent, const QVariantList&)
: KAbstractFileItemActionPlugin(parent)
, m_pluginManager(new PluginManager(this))
{}
QList<QAction*> CompressFileItemAction::actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget)
{
// #268163: don't offer compression on already compressed archives.
if (m_pluginManager->supportedMimeTypes().contains(fileItemInfos.mimeType())) {
return {};
}
QList<QAction*> actions;
const QIcon icon = QIcon::fromTheme(QStringLiteral("ark"));
QMenu *compressMenu = new QMenu(parentWidget);
compressMenu->addAction(createAction(icon,
i18nc("@action:inmenu Part of Compress submenu in Dolphin context menu", "Here (as TAR.GZ)"),
parentWidget,
fileItemInfos.urlList(),
QStringLiteral("ark --changetofirstpath --add --autofilename tar.gz %F")));
const QMimeType zipMime = QMimeDatabase().mimeTypeForName(QStringLiteral("application/zip"));
// Don't offer zip compression if no zip plugin is available.
if (!m_pluginManager->preferredWritePluginsFor(zipMime).isEmpty()) {
compressMenu->addAction(createAction(icon,
i18nc("@action:inmenu Part of Compress submenu in Dolphin context menu", "Here (as ZIP)"),
parentWidget,
fileItemInfos.urlList(),
QStringLiteral("ark --changetofirstpath --add --autofilename zip %F")));
}
compressMenu->addAction(createAction(icon,
i18nc("@action:inmenu Part of Compress submenu in Dolphin context menu", "Compress to..."),
parentWidget,
fileItemInfos.urlList(),
QStringLiteral("ark --add --changetofirstpath --dialog %F")));
QAction *compressMenuAction = new QAction(i18nc("@action:inmenu Compress submenu in Dolphin context menu", "Compress"), parentWidget);
compressMenuAction->setMenu(compressMenu);
// #189177: disable compress menu in read-only folders.
if (!fileItemInfos.supportsWriting()) {
compressMenuAction->setEnabled(false);
}
actions << compressMenuAction;
return actions;
}
QAction *CompressFileItemAction::createAction(const QIcon& icon, const QString& name, QWidget *parent, const QList<QUrl>& urls, const QString& exec)
{
QAction *action = new QAction(icon, name, parent);
connect(action, &QAction::triggered, this, [exec, urls, parent]() {
KRun::run(exec, urls, parent);
});
return action;
}
#include "compressfileitemaction.moc"
/*
* ark -- archiver for the KDE project
*
* Copyright (C) 2016 Elvis Angelaccio <elvis.angelaccio@kdemail.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef COMPRESSFILEITEMACTION_H
#define COMPRESSFILEITEMACTION_H
#include <KAbstractFileItemActionPlugin>
#include <KFileItemListProperties>
class QAction;
class QWidget;
class KFileItemListProperties;
namespace Kerfuffle
{
class PluginManager;
}
class CompressFileItemAction : public KAbstractFileItemActionPlugin
{
Q_OBJECT
public:
CompressFileItemAction(QObject* parent, const QVariantList& args);
virtual QList<QAction*> actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget) Q_DECL_OVERRIDE;
private:
QAction *createAction(const QIcon& icon, const QString& name, QWidget *parent, const QList<QUrl>& urls, const QString& exec);
Kerfuffle::PluginManager *m_pluginManager;
};
#endif
{
"KPlugin": {
"Icon": "ark",
"MimeTypes": [
"application/octet-stream",
"inode/directory"
],
"Name": "'Compress' service menu",
"ServiceTypes": [
"KFileItemAction/Plugin"
]
},
"MimeType": "application/octet-stream;inode/directory;",
"X-KDE-Require": "Write"
}
/*
* ark -- archiver for the KDE project
*
* Copyright (C) 2016 Elvis Angelaccio <elvis.angelaccio@kdemail.net>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "extractfileitemaction.h"
#include <QMenu>
#include <KPluginFactory>
#include <KLocalizedString>
#include <KRun>
#include "mimetypes.h"
#include "pluginmanager.h"
K_PLUGIN_FACTORY_WITH_JSON(ExtractFileItemActionFactory, "extractfileitemaction.json", registerPlugin<ExtractFileItemAction>();)
using namespace Kerfuffle;
ExtractFileItemAction::ExtractFileItemAction(QObject* parent, const QVariantList&)
: KAbstractFileItemActionPlugin(parent)
, m_pluginManager(new PluginManager(this))
{}
QList<QAction*> ExtractFileItemAction::actions(const KFileItemListProperties& fileItemInfos, QWidget* parentWidget)
{
QList<QAction*> actions;
const QIcon icon = QIcon::fromTheme(QStringLiteral("ark"));
QList<QUrl> supportedUrls;
// Filter URLs by supported mimetypes.
foreach (const QUrl &url, fileItemInfos.urlList()) {
const QMimeType mimeType = determineMimeType(url.fileName());
if (!m_pluginManager->preferredPluginsFor(mimeType).isEmpty()) {
supportedUrls << url;
}
}
if (supportedUrls.isEmpty()) {
return {};
}
QMenu *extractMenu = new QMenu(parentWidget);
extractMenu->addAction(createAction(icon,
i18nc("@action:inmenu Part of Extract submenu in Dolphin context menu", "Extract archive here"),
parentWidget,
supportedUrls,
QStringLiteral("ark --batch --autodestination %F")));
extractMenu->addAction(createAction(icon,
i18nc("@action:inmenu Part of Extract submenu in Dolphin context menu", "Extract archive to..."),
parentWidget,
supportedUrls,
QStringLiteral("ark --batch --autodestination --dialog %F")));
extractMenu->addAction(createAction(icon,
i18nc("@action:inmenu Part of Extract submenu in Dolphin context menu", "Extract archive here, autodetect subfolder"),
parentWidget,
supportedUrls,
QStringLiteral("ark --batch --autodestination --autosubfolder %F")));
QAction *extractMenuAction = new QAction(i18nc("@action:inmenu Extract submenu in Dolphin context menu", "Extract"), parentWidget);
extractMenuAction->setMenu(extractMenu);
// #189177: disable extract menu in read-only folders.
if (!fileItemInfos.supportsWriting()) {
extractMenuAction->setEnabled(false);
}
actions << extractMenuAction;
return actions;
}
QAction *ExtractFileItemAction::createAction(const QIcon& icon, const QString& name, QWidget *parent, const QList<QUrl>& urls, const QString& exec)
{
QAction *action = new QAction(icon, name, parent);
connect(action, &QAction::triggered, this, [exec, urls, parent]() {
KRun::run(exec, urls, parent);
});
return action;
}