File indexing completed on 2024-05-05 05:50:42

0001 /*
0002     SPDX-FileCopyrightText: 2016 Elvis Angelaccio <elvis.angelaccio@kde.org>
0003 
0004     SPDX-License-Identifier: BSD-2-Clause
0005 */
0006 
0007 #ifndef PLUGINMANAGER_H
0008 #define PLUGINMANAGER_H
0009 
0010 #include "plugin.h"
0011 
0012 #include <QHash>
0013 #include <QMimeType>
0014 #include <QVector>
0015 
0016 namespace Kerfuffle
0017 {
0018 class KERFUFFLE_EXPORT PluginManager : public QObject
0019 {
0020     Q_OBJECT
0021 
0022 public:
0023     /**
0024      * How the list of supported mimetypes can be sorted.
0025      */
0026     enum MimeSortingMode { Unsorted, SortByComment };
0027 
0028     explicit PluginManager(QObject *parent = nullptr);
0029 
0030     /**
0031      * @return The list of all installed plugins.
0032      * An installed plugin is not necessarily available to the app.
0033      * The user could have disabled it from the settings, or the needed executables could not be found.
0034      */
0035     QVector<Plugin *> installedPlugins() const;
0036 
0037     /**
0038      * @return The list of plugins ready to be used. Includes read-only and read-write ones.
0039      */
0040     QVector<Plugin *> availablePlugins() const;
0041 
0042     /**
0043      * @return The list of read-write plugins ready to be used.
0044      */
0045     QVector<Plugin *> availableWritePlugins() const;
0046 
0047     /**
0048      * @return The list of plugins enabled by the user in the settings dialog.
0049      */
0050     QVector<Plugin *> enabledPlugins() const;
0051 
0052     /**
0053      * @return The list of preferred plugins for the given @p mimeType, among all the available ones.
0054      * The list is sorted according to the plugins priority. The list is saved in a cache for efficiency.
0055      * If no plugin is available, returns an empty list.
0056      */
0057     QVector<Plugin *> preferredPluginsFor(const QMimeType &mimeType);
0058 
0059     /**
0060      * @return The list of preferred read-write plugins for the given @p mimeType, among all the available ones.
0061      * The list is sorted according to the plugins priority.
0062      * If no read-write plugin is available, returns an empty list.
0063      */
0064     QVector<Plugin *> preferredWritePluginsFor(const QMimeType &mimeType) const;
0065 
0066     /**
0067      * @return The preferred plugin for the given @p mimeType, among all the available ones.
0068      * If no plugin is available, returns an invalid plugin.
0069      */
0070     Plugin *preferredPluginFor(const QMimeType &mimeType);
0071 
0072     /**
0073      * @return The preferred read-write plugin for the given @p mimeType, among all the available ones.
0074      * If no read-write plugin is available, returns an invalid plugin.
0075      */
0076     Plugin *preferredWritePluginFor(const QMimeType &mimeType) const;
0077 
0078     /**
0079      * @return The list of all mimetypes that Ark can open, sorted according to @p mode.
0080      */
0081     QStringList supportedMimeTypes(MimeSortingMode mode = Unsorted) const;
0082 
0083     /**
0084      * @return The list of all read-write mimetypes supported by Ark, sorted according to @p mode.
0085      */
0086     QStringList supportedWriteMimeTypes(MimeSortingMode mode = Unsorted) const;
0087 
0088     /**
0089      * @return The subset of @p plugins that support either @p mimetype or a parent of @p mimetype.
0090      */
0091     QVector<Plugin *> filterBy(const QVector<Plugin *> &plugins, const QMimeType &mimeType) const;
0092 
0093 private:
0094     void loadPlugins();
0095 
0096     /**
0097      * @param readWrite whether to return only the read-write plugins.
0098      * @return The list of preferred plugins for @p mimeType among the available ones, sorted by priority.
0099      */
0100     QVector<Plugin *> preferredPluginsFor(const QMimeType &mimeType, bool readWrite) const;
0101 
0102     /**
0103      * @return A list with the given @p mimeTypes, alphabetically sorted according to their comment.
0104      */
0105     static QStringList sortByComment(const QSet<QString> &mimeTypes);
0106 
0107     /**
0108      * Workaround for libarchive >= 3.3 not linking against liblzo.
0109      */
0110     static bool libarchiveHasLzo();
0111 
0112     QVector<Plugin *> m_plugins;
0113     QHash<QString, QVector<Plugin *>> m_preferredPluginsCache;
0114 };
0115 
0116 }
0117 
0118 #endif