diff --git a/kerfuffle/CMakeLists.txt b/kerfuffle/CMakeLists.txt index ad1d8287..0a31b49b 100644 --- a/kerfuffle/CMakeLists.txt +++ b/kerfuffle/CMakeLists.txt @@ -1,62 +1,63 @@ ########### next target ############### set(kerfuffle_SRCS archiveformat.cpp archive_kerfuffle.cpp archiveinterface.cpp extractionsettingspage.cpp previewsettingspage.cpp settingspage.cpp jobs.cpp createdialog.cpp extractiondialog.cpp propertiesdialog.cpp queries.cpp addtoarchive.cpp cliinterface.cpp mimetypes.cpp + plugin.cpp ) kconfig_add_kcfg_files(kerfuffle_SRCS settings.kcfgc) ki18n_wrap_ui(kerfuffle_SRCS createdialog.ui extractiondialog.ui extractionsettings.ui previewsettings.ui propertiesdialog.ui ) ecm_qt_declare_logging_category(kerfuffle_SRCS HEADER ark_debug.h IDENTIFIER ARK CATEGORY_NAME ark.kerfuffle) add_library(kerfuffle SHARED ${kerfuffle_SRCS}) generate_export_header(kerfuffle BASE_NAME kerfuffle) target_link_libraries(kerfuffle PUBLIC KF5::IconThemes KF5::Pty KF5::Service KF5::I18n KF5::WidgetsAddons PRIVATE KF5::KIOCore KF5::KIOWidgets KF5::KIOFileWidgets ) set_target_properties(kerfuffle PROPERTIES VERSION ${KERFUFFLE_VERSION_STRING} SOVERSION ${KERFUFFLE_SOVERSION}) install(TARGETS kerfuffle ${KDE_INSTALL_TARGETS_DEFAULT_ARGS} LIBRARY NAMELINK_SKIP) install(FILES kerfufflePlugin.desktop DESTINATION ${KDE_INSTALL_KSERVICETYPES5DIR}) install(FILES ark.kcfg DESTINATION ${KDE_INSTALL_KCFGDIR}) install(FILES mime/kerfuffle.xml DESTINATION ${KDE_INSTALL_MIMEDIR}) if(SharedMimeInfo_FOUND) update_xdg_mimetypes(${KDE_INSTALL_MIMEDIR}) endif() diff --git a/kerfuffle/plugin.cpp b/kerfuffle/plugin.cpp new file mode 100644 index 00000000..c6d8ec2d --- /dev/null +++ b/kerfuffle/plugin.cpp @@ -0,0 +1,117 @@ +/* + * ark -- archiver for the KDE project + * + * Copyright (C) 2016 Elvis Angelaccio + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ( INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "ark_debug.h" +#include "plugin.h" + +#include +#include + +namespace Kerfuffle +{ + +Plugin::Plugin(QObject *parent, const KPluginMetaData &metaData) + : QObject(parent) + , m_enabled(true) + , m_metaData(metaData) +{ +} + +unsigned int Plugin::priority() const +{ + const int priority = m_metaData.rawData()[QStringLiteral("X-KDE-Priority")].toInt(); + return (priority > 0 ? priority : 0); +} + +bool Plugin::isEnabled() const +{ + return m_enabled; +} + +void Plugin::setEnabled(bool enabled) +{ + m_enabled = enabled; + emit enabledChanged(); +} + +bool Plugin::isReadWrite() const +{ + const bool isDeclaredReadWrite = m_metaData.rawData()[QStringLiteral("X-KDE-Kerfuffle-ReadWrite")].toBool(); + return isDeclaredReadWrite && findExecutables(readWriteExecutables()); +} + +QStringList Plugin::readOnlyExecutables() const +{ + QStringList readOnlyExecutables; + + const QJsonArray array = m_metaData.rawData()[QStringLiteral("X-KDE-Kerfuffle-ReadOnlyExecutables")].toArray(); + foreach (const QJsonValue &value, array) { + readOnlyExecutables << value.toString(); + } + + return readOnlyExecutables; +} + +QStringList Plugin::readWriteExecutables() const +{ + QStringList readWriteExecutables; + + const QJsonArray array = m_metaData.rawData()[QStringLiteral("X-KDE-Kerfuffle-ReadWriteExecutables")].toArray(); + foreach (const QJsonValue &value, array) { + readWriteExecutables << value.toString(); + } + + return readWriteExecutables; +} + +KPluginMetaData Plugin::metaData() const +{ + return m_metaData; +} + +bool Plugin::isValid() const +{ + return isEnabled() && m_metaData.isValid() && findExecutables(readOnlyExecutables()); +} + +bool Plugin::findExecutables(const QStringList &executables) +{ + foreach (const QString &executable, executables) { + if (executable.isEmpty()) { + continue; + } + + if (QStandardPaths::findExecutable(executable).isEmpty()) { + qCDebug(ARK) << "Could not find executable" << executable; + return false; + } + } + + return true; +} + +} diff --git a/kerfuffle/plugin.h b/kerfuffle/plugin.h new file mode 100644 index 00000000..a8e8bb10 --- /dev/null +++ b/kerfuffle/plugin.h @@ -0,0 +1,111 @@ +/* + * ark -- archiver for the KDE project + * + * Copyright (C) 2016 Elvis Angelaccio + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES ( INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * ( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef PLUGIN_H +#define PLUGIN_H + +#include "kerfuffle_export.h" + +#include + +#include + +namespace Kerfuffle +{ + +class KERFUFFLE_EXPORT Plugin : public QObject +{ + Q_OBJECT + + /** + * The priority of the plugin. The higher the better. + */ + Q_PROPERTY(unsigned int priority READ priority CONSTANT) + + /** + * Whether the plugin has been enabled in the settings. + */ + Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged MEMBER m_enabled) + + /** + * Whether the plugin is read-write *at runtime*. + * A plugin could be declared read-write at build-time but "downgraded" to read-only at runtime. + */ + Q_PROPERTY(bool readWrite READ isReadWrite CONSTANT) + + /** + * The list of executables required by the plugin to operate in read-only mode. + */ + Q_PROPERTY(QStringList readOnlyExecutables READ readOnlyExecutables CONSTANT) + + /** + * The list of executables required by the plugin to operate in read-write mode. + * This is an empty list if the plugin is declared as read-only. + */ + Q_PROPERTY(QStringList readWriteExecutables READ readWriteExecutables CONSTANT) + + /** + * The plugin's JSON metadata. This provides easy access to the supported mimetypes list. + */ + Q_PROPERTY(KPluginMetaData metaData READ metaData MEMBER m_metaData CONSTANT) + +public: + explicit Plugin(QObject *parent = Q_NULLPTR, const KPluginMetaData& metaData = KPluginMetaData()); + + + unsigned int priority() const; + bool isEnabled() const; + void setEnabled(bool enabled); + bool isReadWrite() const; + QStringList readOnlyExecutables() const; + QStringList readWriteExecutables() const; + KPluginMetaData metaData() const; + + /** + * @return Whether the plugin is ready to be used. + * This implies isEnabled(), while an enabled plugin may not be valid. + * A read-write plugin downgraded to read-only is still valid. + */ + bool isValid() const; + +signals: + void enabledChanged(); + +private: + + /** + * @return Whether all the given executables are found in $PATH. + */ + static bool findExecutables(const QStringList &executables); + + bool m_enabled; + KPluginMetaData m_metaData; +}; + +} + +#endif