Commit 07090fcd authored by Elvis Angelaccio's avatar Elvis Angelaccio

Drop duplicated code moved to PluginManager

Most of the functions is mimetypes.cpp has been refactored and simplified in the
new PluginManager class, so we can drop them.
CreateDialog now respects the plugin disabled in the arkrc configuration
file.

Differential Revision: D1502
parent 616dc350
......@@ -23,7 +23,7 @@
#include "ark_debug.h"
#include "batchextract.h"
#include "kerfuffle/archive_kerfuffle.h"
#include "mimetypes.h"
#include "pluginmanager.h"
#include <QAction>
#include <QDebug>
......@@ -64,7 +64,8 @@ QList<QAction *> ExtractHereDndPlugin::setup(const KFileItemListProperties& popu
const QString extractHereMessage = i18nc("@action:inmenu Context menu shown when an archive is being drag'n'dropped", "Extract here");
if (!Kerfuffle::supportedMimeTypes().contains(popupMenuInfo.mimeType())) {
Kerfuffle::PluginManager pluginManager;
if (!pluginManager.supportedMimeTypes().contains(popupMenuInfo.mimeType())) {
qCWarning(ARK) << popupMenuInfo.mimeType() << "is not a supported mimetype";
return actionList;
}
......
......@@ -24,10 +24,10 @@
#include "mainwindow.h"
#include "ark_debug.h"
#include "kerfuffle/archive_kerfuffle.h"
#include "kerfuffle/createdialog.h"
#include "kerfuffle/settingspage.h"
#include "mimetypes.h"
#include "archive_kerfuffle.h"
#include "createdialog.h"
#include "settingspage.h"
#include "pluginmanager.h"
#include "part/interface.h"
#include <KPluginFactory>
......@@ -223,9 +223,9 @@ void MainWindow::openArchive()
Q_ASSERT(iface);
Q_UNUSED(iface);
Kerfuffle::PluginManager pluginManager;
QPointer<QFileDialog> dlg = new QFileDialog(this, i18nc("to open an archive", "Open Archive"));
dlg->setMimeTypeFilters(Kerfuffle::supportedMimeTypes());
dlg->setMimeTypeFilters(pluginManager.supportedMimeTypes());
dlg->setFileMode(QFileDialog::ExistingFile);
dlg->setAcceptMode(QFileDialog::AcceptOpen);
......
......@@ -24,7 +24,7 @@
*/
#include "createdialog.h"
#include "mimetypes.h"
#include "pluginmanager.h"
#include <KCollapsibleGroupBox>
......@@ -46,6 +46,9 @@ private Q_SLOTS:
void testEncryption_data();
void testEncryption();
void testHeaderEncryptionTooltip();
private:
PluginManager m_pluginManager;
};
void CreateDialogTest::testBasicWidgets_data()
......@@ -61,7 +64,7 @@ void CreateDialogTest::testBasicWidgets_data()
QTest::newRow("tarlzop") << QStringLiteral("application/x-tzo");
QTest::newRow("tarlzip") << QStringLiteral("application/x-lzip-compressed-tar");
const auto writeMimeTypes = supportedWriteMimeTypes();
const auto writeMimeTypes = m_pluginManager.supportedWriteMimeTypes();
if (writeMimeTypes.contains(QStringLiteral("application/zip"))) {
QTest::newRow("zip") << QStringLiteral("application/zip");
......@@ -118,19 +121,19 @@ void CreateDialogTest::testEncryption_data()
QTest::newRow("tar") << QStringLiteral("application/x-compressed-tar") << false << false;
if (Kerfuffle::supportedWriteMimeTypes().contains(QStringLiteral("application/zip"))) {
if (m_pluginManager.supportedWriteMimeTypes().contains(QStringLiteral("application/zip"))) {
QTest::newRow("zip") << QStringLiteral("application/zip") << true << false;
} else {
qDebug() << "zip format not available in CreateDialog, skipping test.";
}
if (Kerfuffle::supportedWriteMimeTypes().contains(QStringLiteral("application/x-7z-compressed"))) {
if (m_pluginManager.supportedWriteMimeTypes().contains(QStringLiteral("application/x-7z-compressed"))) {
QTest::newRow("7z") << QStringLiteral("application/x-7z-compressed") << true << true;
} else {
qDebug() << "7z format not available in CreateDialog, skipping test.";
}
if (Kerfuffle::supportedWriteMimeTypes().contains(QStringLiteral("application/x-rar"))) {
if (m_pluginManager.supportedWriteMimeTypes().contains(QStringLiteral("application/x-rar"))) {
QTest::newRow("rar") << QStringLiteral("application/x-rar") << true << true;
} else {
qDebug() << "rar format not available in CreateDialog, skipping test.";
......@@ -186,7 +189,7 @@ void CreateDialogTest::testEncryption()
void CreateDialogTest::testHeaderEncryptionTooltip()
{
if (!Kerfuffle::supportedWriteMimeTypes().contains(QStringLiteral("application/zip"))) {
if (!m_pluginManager.supportedWriteMimeTypes().contains(QStringLiteral("application/zip"))) {
QSKIP("zip format not available in CreateDialog, skipping test.", SkipSingle);
}
......
......@@ -65,8 +65,7 @@ CreateDialog::CreateDialog(QWidget *parent,
setWindowTitle(caption);
setModal(true);
m_supportedMimeTypes = Kerfuffle::supportedWriteMimeTypes();
m_writePlugins = Kerfuffle::supportedWritePlugins();
m_supportedMimeTypes = m_pluginManger.supportedWriteMimeTypes();
m_vlayout = new QVBoxLayout();
setLayout(m_vlayout);
......@@ -121,7 +120,7 @@ void CreateDialog::slotFileNameEdited(const QString &fileName)
void CreateDialog::slotUpdateWidgets(int index)
{
const QMimeType mimeType = QMimeDatabase().mimeTypeForName(m_supportedMimeTypes.at(index));
const KPluginMetaData metadata = preferredPluginFor(mimeType, m_writePlugins);
const KPluginMetaData metadata = m_pluginManger.preferredPluginFor(mimeType)->metaData();
const ArchiveFormat archiveFormat = ArchiveFormat::fromMetadata(mimeType, metadata);
Q_ASSERT(archiveFormat.isValid());
......@@ -229,7 +228,7 @@ void CreateDialog::accept()
void CreateDialog::slotEncryptionToggled()
{
const KPluginMetaData metadata = preferredPluginFor(currentMimeType(), m_writePlugins);
const KPluginMetaData metadata = m_pluginManger.preferredPluginFor(currentMimeType())->metaData();
const ArchiveFormat archiveFormat = ArchiveFormat::fromMetadata(currentMimeType(), metadata);
Q_ASSERT(archiveFormat.isValid());
......
......@@ -32,9 +32,9 @@
#define CREATEDIALOG_H
#include "kerfuffle_export.h"
#include "pluginmanager.h"
#include <KConfigGroup>
#include <KPluginMetaData>
#include <QDialog>
#include <QMimeType>
......@@ -89,7 +89,7 @@ private:
QVBoxLayout *m_vlayout;
KConfigGroup m_config;
QStringList m_supportedMimeTypes;
QVector<KPluginMetaData> m_writePlugins;
PluginManager m_pluginManger;
private slots:
void slotFileNameEdited(const QString &text);
......
......@@ -108,142 +108,4 @@ QMimeType determineMimeType(const QString& filename)
return mimeFromContent;
}
QStringList supportedMimeTypes()
{
const QVector<KPluginMetaData> offers = KPluginLoader::findPlugins(QStringLiteral("kerfuffle"), [](const KPluginMetaData& metaData) {
return metaData.serviceTypes().contains(QStringLiteral("Kerfuffle/Plugin"));
});
QSet<QString> supported;
foreach (const KPluginMetaData& pluginMetadata, offers) {
const auto executables = pluginMetadata.rawData()[QStringLiteral("X-KDE-Kerfuffle-ReadOnlyExecutables")].toArray();
if (!findExecutables(executables)) {
qCDebug(ARK) << "Could not find all the read-only executables of" << pluginMetadata.pluginId() << "- ignoring mimetypes:" << pluginMetadata.mimeTypes();
continue;
}
supported += pluginMetadata.mimeTypes().toSet();
}
// Remove entry for lrzipped tar if lrzip executable not found in path.
if (QStandardPaths::findExecutable(QStringLiteral("lrzip")).isEmpty()) {
supported.remove(QStringLiteral("application/x-lrzip-compressed-tar"));
}
qCDebug(ARK) << "Returning supported mimetypes" << supported;
return sortByComment(supported);
}
QStringList supportedWriteMimeTypes()
{
const QVector<KPluginMetaData> offers = KPluginLoader::findPlugins(QStringLiteral("kerfuffle"), [](const KPluginMetaData& metaData) {
return metaData.serviceTypes().contains(QStringLiteral("Kerfuffle/Plugin")) &&
metaData.rawData()[QStringLiteral("X-KDE-Kerfuffle-ReadWrite")].toBool();
});
QSet<QString> supported;
foreach (const KPluginMetaData& pluginMetadata, offers) {
const auto executables = pluginMetadata.rawData()[QStringLiteral("X-KDE-Kerfuffle-ReadWriteExecutables")].toArray();
if (!findExecutables(executables)) {
qCDebug(ARK) << "Could not find all the read-write executables of" << pluginMetadata.pluginId() << "- ignoring mimetypes:" << pluginMetadata.mimeTypes();
continue;
}
supported += pluginMetadata.mimeTypes().toSet();
}
// Remove entry for lrzipped tar if lrzip executable not found in path.
if (QStandardPaths::findExecutable(QStringLiteral("lrzip")).isEmpty()) {
supported.remove(QStringLiteral("application/x-lrzip-compressed-tar"));
}
qCDebug(ARK) << "Returning supported write mimetypes" << supported;
return sortByComment(supported);
}
QVector<KPluginMetaData> supportedWritePlugins()
{
const auto readWritePlugins = KPluginLoader::findPlugins(QStringLiteral("kerfuffle"), [](const KPluginMetaData& metaData) {
return metaData.serviceTypes().contains(QStringLiteral("Kerfuffle/Plugin")) &&
metaData.rawData()[QStringLiteral("X-KDE-Kerfuffle-ReadWrite")].toBool();
});
// Drop plugins whose executables are not available.
QVector<KPluginMetaData> supportedPlugins;
foreach (const auto& plugin, readWritePlugins) {
const QJsonArray rwExecutables = plugin.rawData()[QStringLiteral("X-KDE-Kerfuffle-ReadWriteExecutables")].toArray();
// Libarchive does not depend on executables.
if (rwExecutables.isEmpty()) {
supportedPlugins << plugin;
continue;
}
if (findExecutables(rwExecutables)) {
supportedPlugins << plugin;
}
}
return supportedPlugins;
}
KPluginMetaData preferredPluginFor(const QMimeType &mimeType, const QVector<KPluginMetaData> &plugins)
{
QVector<KPluginMetaData> filteredPlugins;
foreach (const KPluginMetaData& plugin, plugins) {
if (plugin.mimeTypes().contains(mimeType.name())) {
filteredPlugins << plugin;
}
}
qSort(filteredPlugins.begin(), filteredPlugins.end(), comparePlugins);
qCDebug(ARK) << "Preferred plugin for" << mimeType.name() << "is" << filteredPlugins.first().pluginId();
return filteredPlugins.first();
}
QStringList sortByComment(const QSet<QString> &mimeTypeSet)
{
QMap<QString,QString> map;
// Convert QStringList to QMap to sort by comment.
foreach (const QString &mimeType, mimeTypeSet) {
QMimeType mime(QMimeDatabase().mimeTypeForName(mimeType));
map[mime.comment().toLower()] = mime.name();
}
// Convert back to QStringList.
QStringList mimetypeList;
foreach (const QString &value, map) {
mimetypeList.append(value);
}
return mimetypeList;
}
bool findExecutables(const QJsonArray& executables)
{
foreach (const auto& value, executables) {
const QString executable = value.toString();
if (executable.isEmpty()) {
continue;
}
if (QStandardPaths::findExecutable(executable).isEmpty()) {
qCDebug(ARK) << "Could not find executable" << executable;
return false;
}
}
return true;
}
bool comparePlugins(const KPluginMetaData& p1, const KPluginMetaData& p2)
{
return (p1.rawData()[QStringLiteral("X-KDE-Priority")].toInt()) > (p2.rawData()[QStringLiteral("X-KDE-Priority")].toInt());
}
} // namespace Kerfuffle
......@@ -28,43 +28,11 @@
#include "kerfuffle_export.h"
#include <KPluginMetaData>
#include <QJsonArray>
#include <QMimeType>
#include <QSet>
namespace Kerfuffle
{
KERFUFFLE_EXPORT QMimeType determineMimeType(const QString& filename);
KERFUFFLE_EXPORT QStringList supportedMimeTypes();
KERFUFFLE_EXPORT QStringList supportedWriteMimeTypes();
/**
* @return The list of available read-write plugins.
*/
KERFUFFLE_EXPORT QVector<KPluginMetaData> supportedWritePlugins();
/**
* @return The preferred plugin to handle @p mimeType, out of the given list of @p plugins.
*/
KERFUFFLE_EXPORT KPluginMetaData preferredPluginFor(const QMimeType& mimeType, const QVector<KPluginMetaData>& plugins);
/**
* @return A list with the supported read-only mimetypes, alphabetically sorted according to their comment.
*/
QStringList sortByComment(const QSet<QString> &mimeTypeSet);
/**
* @return Whether all the @p executables are available in the system PATH.
*/
bool findExecutables(const QJsonArray& executables);
/**
* @return Whether @p p1 has higher priority than @p p2.
*/
bool comparePlugins(const KPluginMetaData& p1, const KPluginMetaData& p2);
} // namespace Kerfuffle
}
#endif // MIMETYPES_H
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