File indexing completed on 2024-04-28 11:43:52

0001 /*
0002     SPDX-FileCopyrightText: 2010 Ryan Rix <ry@n.rix.si>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef KPACKAGE_LOADER_H
0008 #define KPACKAGE_LOADER_H
0009 
0010 #include <kpackage/package.h>
0011 
0012 #include <kpackage/package_export.h>
0013 
0014 namespace KPackage
0015 {
0016 class PackageLoaderPrivate;
0017 
0018 /**
0019  * @class PackageLoader kpackage/packageloader.h <KPackage/PackageLoader>
0020  *
0021  * This is an abstract base class which defines an interface to which the package
0022  * loading logic can communicate with a parent application. The plugin loader
0023  * must be set before any plugins are loaded, otherwise (for safety reasons), the
0024  * default PackageLoader implementation will be used. The reimplemented version should
0025  * not do more than simply returning a loaded plugin. It should not init() it, and it should not
0026  * hang on to it.
0027  *
0028  * @author Ryan Rix <ry@n.rix.si>
0029  **/
0030 class KPACKAGE_EXPORT PackageLoader
0031 {
0032 public:
0033     /**
0034      * Load a Package plugin.
0035      *
0036      * @param packageFormat the format of the package to load
0037      * @param packagePath the package name: the path of the package relative to the
0038      *        packageFormat root path. If not specified it will have to be set manually
0039      *        with Package::setPath() by the caller.
0040      *
0041      * @return a Package object matching name, or an invalid package on failure
0042      **/
0043     Package loadPackage(const QString &packageFormat, const QString &packagePath = QString());
0044 
0045     /**
0046      * List all available packages of a certain type
0047      *
0048      * @param packageFormat the format of the packages to list
0049      * @param packageRoot the root folder where the packages are installed.
0050      *          If not specified the default from the packageformat will be taken.
0051      *
0052      * @return metadata for all the matching packages
0053      */
0054     QList<KPluginMetaData> listPackages(const QString &packageFormat, const QString &packageRoot = QString());
0055 
0056     /**
0057      * List package of a certain type that match a certain filter function
0058      *
0059      * @param packageFormat the format of the packages to list
0060      * @param packageRoot the root folder where the packages are installed.
0061      *          If not specified the default from the packageformat will be taken.
0062      * @param filter a filter function that will be called on each package:
0063      *          will return true for the matching ones
0064      *
0065      * @return metadata for all the matching packages
0066      * @since 5.10
0067      */
0068     QList<KPluginMetaData> findPackages(const QString &packageFormat,
0069                                         const QString &packageRoot = QString(),
0070                                         std::function<bool(const KPluginMetaData &)> filter = std::function<bool(const KPluginMetaData &)>());
0071 
0072     /**
0073      * Loads a PackageStructure for a given format. The structure can then be used as
0074      * paramenter for a Package instance constructor
0075      *
0076      * @note The returned pointer is managed by KPackage, and should never be deleted
0077      *
0078      * @param packageFormat the package format, such as "KPackage/GenericQML"
0079      * @return the structure instance (ownership retained by KPackage)
0080      */
0081     KPackage::PackageStructure *loadPackageStructure(const QString &packageFormat);
0082 
0083     /**
0084      * Adds a new known package structure that can be used by the functions to load packages such
0085      * as loadPackage, findPackages etc
0086      * @param packageFormat the package format, such as "KPackage/GenericQML"
0087      * @param structure the package structure we want to be able to load packages from
0088      * @since 5.10
0089      */
0090     void addKnownPackageStructure(const QString &packageFormat, KPackage::PackageStructure *structure);
0091 
0092 #if KPACKAGE_BUILD_DEPRECATED_SINCE(5, 86)
0093     /**
0094      * Set the plugin loader which will be queried for all loads.
0095      *
0096      * @param loader A subclass of PackageLoader which will be supplied
0097      * by the application
0098      * @deprecated Since 5.86, deprecated for lack of usage. Use default package loader instead
0099      **/
0100     KPACKAGE_DEPRECATED_VERSION(5, 86, "deprecated for lack of usage. Use default package loader instead")
0101     static void setPackageLoader(PackageLoader *loader);
0102 #endif
0103 
0104     /**
0105      * Return the active plugin loader
0106      **/
0107     static PackageLoader *self();
0108 
0109 protected:
0110 #if KPACKAGE_BUILD_DEPRECATED_SINCE(5, 86)
0111     /**
0112      * A re-implementable method that allows subclasses to override
0113      * the default behaviour of loadPackage. If the service requested is not recognized,
0114      * then the implementation should return an empty and invalid Package().
0115      * This method is called
0116      * by loadPackage prior to attempting to load a Package using the standard
0117      * plugin mechanisms.
0118      *
0119      * @param packageFormat the format of the package to load
0120      *
0121      * @return a Package instance with the proper PackageStructure
0122      * @deprecated Since 5.86, deprecated for lack of usage. Use default package loading instead
0123      **/
0124     KPACKAGE_DEPRECATED_VERSION(5, 86, "deprecated for lack of usage. Use default package loading instead")
0125     virtual Package internalLoadPackage(const QString &packageFormat);
0126 #endif
0127 
0128     PackageLoader();
0129     virtual ~PackageLoader();
0130 
0131 private:
0132     friend class Package;
0133     PackageLoaderPrivate *const d;
0134     Q_DISABLE_COPY(PackageLoader)
0135 };
0136 
0137 }
0138 
0139 Q_DECLARE_METATYPE(KPackage::PackageLoader *)
0140 
0141 #endif