File indexing completed on 2023-09-24 04:14:57

0001 /*
0002     SPDX-FileCopyrightText: 2007-2011 Aaron Seigo <aseigo@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef PLASMA_PACKAGE_H
0008 #define PLASMA_PACKAGE_H
0009 
0010 #include <QStringList>
0011 
0012 #include <plasma/plasma.h>
0013 #include <plasma/plasma_export.h>
0014 
0015 #if PLASMA_ENABLE_DEPRECATED_SINCE(5, 94)
0016 #include <KPluginInfo>
0017 #endif
0018 
0019 class KJob;
0020 
0021 // not 5.6, as last Plasma API using this class only got removed later
0022 #if PLASMA_ENABLE_DEPRECATED_SINCE(5, 28)
0023 
0024 namespace KPackage
0025 {
0026 class Package;
0027 }
0028 
0029 namespace Plasma
0030 {
0031 /**
0032  * @class Package plasma/package.h <Plasma/Package>
0033  *
0034  * @short object representing an installed Plasma package
0035  *
0036  * Package defines what is in a package and provides easy access to the contents.
0037  *
0038  * To define a package, one might write the following code:
0039  *
0040  @code
0041     Package package;
0042 
0043     package.addDirectoryDefinition("images", "pics/", i18n("Images"));
0044     QStringList mimeTypes;
0045     mimeTypes << "image/svg" << "image/png" << "image/jpeg";
0046     package.setMimeTypes("images", mimeTypes);
0047 
0048     package.addDirectoryDefinition("scripts", "code/", i18n("Executable Scripts"));
0049     mimeTypes.clear();
0050     mimeTypes << "text/\*";
0051     package.setMimeTypes("scripts", mimeTypes);
0052 
0053     package.addFileDefinition("mainscript", "code/main.js", i18n("Main Script File"));
0054     package.setRequired("mainscript", true);
0055  @endcode
0056  * One may also choose to create a subclass of PackageStructure and include the setup
0057  * in the constructor.
0058  *
0059  * Either way, Package creates a self-documenting contract between the packager and
0060  * the application without exposing package internals such as actual on-disk structure
0061  * of the package or requiring that all contents be explicitly known ahead of time.
0062  *
0063  * Subclassing PackageStructure does have provide a number of potential const benefits:
0064  *    * the package can be notified of path changes via the virtual pathChanged() method
0065  *    * the subclass may implement mechanisms to install and remove packages using the
0066  *      virtual install and uninstall methods
0067  *    * subclasses can be compiled as plugins for easy re-use
0068  *
0069  * @deprecated Since 5.6, use KPackage::Package instead
0070  **/
0071 // TODO: write documentation on USING a package
0072 
0073 class PackagePrivate;
0074 class PackageStructure;
0075 
0076 class PLASMA_EXPORT Package
0077 {
0078 public:
0079     /**
0080      * Default constructor
0081      *
0082      * @param structure if a NULL pointer is passed in, this will creates an empty (invalid) Package;
0083      * otherwise the structure is allowed to set up the Package's initial layout
0084      * @since 4.6
0085      */
0086     PLASMA_DEPRECATED_VERSION(5, 6, "Use KPackage API")
0087     explicit Package(PackageStructure *structure = nullptr);
0088 
0089     /**
0090      * Copy constructore
0091      * @since 4.6
0092      */
0093     PLASMA_DEPRECATED_VERSION(5, 6, "Use KPackage API")
0094     Package(const KPackage::Package &other);
0095 
0096     /**
0097      * Copy constructore
0098      * @since 4.6
0099      */
0100     PLASMA_DEPRECATED_VERSION(5, 6, "Use KPackage API")
0101     Package(const Package &other);
0102 
0103     ~Package();
0104 
0105     /**
0106      * Assignment operator
0107      * @since 4.6
0108      */
0109     Package &operator=(const Package &rhs);
0110 
0111     /**
0112      * @return true if this package has a valid PackageStructure associatedw it with it.
0113      * A package may not be valid, but have a valid structure. Useful when dealing with
0114      * Package objects in a semi-initialized state (e.g. before calling setPath())
0115      * @since 5.1
0116      */
0117     bool hasValidStructure() const;
0118 
0119     /**
0120      * @return true if all the required components exist
0121      **/
0122     bool isValid() const;
0123 
0124     /**
0125      * Sets the path to the root of this package
0126      * @param path an absolute path, or a relative path to the default package root
0127      * @since 4.3
0128      */
0129     void setPath(const QString &path);
0130 
0131     /**
0132      * @return the path to the root of this particular package
0133      */
0134     const QString path() const;
0135 
0136     /**
0137      * Get the path to a given file based on the key and an optional filename.
0138      * Example: finding the main script in a scripting package:
0139      *      filePath("mainscript")
0140      *
0141      * Example: finding a specific image in the images directory:
0142      *      filePath("images", "myimage.png")
0143      *
0144      * @param key the key of the file type to look for,
0145      * @param filename optional name of the file to locate within the package
0146      * @return path to the file on disk. QString() if not found.
0147      **/
0148     QString filePath(const char *key, const QString &filename = QString()) const;
0149 
0150     /**
0151      * Get the list of files of a given type.
0152      *
0153      * @param fileType the type of file to look for, as defined in the
0154      *               package structure.
0155      * @return list of files by name, suitable for passing to filePath
0156      **/
0157     QStringList entryList(const char *key) const;
0158 
0159     /**
0160      * @return user visible name for the given entry
0161      **/
0162     QString name(const char *key) const;
0163 
0164     /**
0165      * @return true if the item at path exists and is required
0166      **/
0167     bool isRequired(const char *key) const;
0168 
0169     /**
0170      * @return the mimeTypes associated with the path, if any
0171      **/
0172     QStringList mimeTypes(const char *key) const;
0173 
0174     /**
0175      * @return the prefix paths inserted between the base path and content entries, in order of priority.
0176      *         When searching for a file, all paths will be tried in order.
0177      * @since 4.6
0178      */
0179     QStringList contentsPrefixPaths() const;
0180 
0181     /**
0182      * @return preferred package root. This defaults to plasma/plasmoids/
0183      */
0184     QString defaultPackageRoot() const;
0185 
0186     /**
0187      * @return service prefix used in desktop files. This defaults to plasma-applet-
0188      */
0189     QString servicePrefix() const;
0190 
0191     /**
0192      * @return true if paths/symlinks outside the package itself should be followed.
0193      * By default this is set to false for security reasons.
0194      */
0195     bool allowExternalPaths() const;
0196 
0197     /**
0198      * @return the package metadata object.
0199      */
0200     KPluginInfo metadata() const;
0201 
0202     /**
0203      * @return a SHA1 hash digest of the contents of the package in hexadecimal form
0204      * @since 4.4
0205      */
0206     QString contentsHash() const;
0207 
0208     /**
0209      * Adds a directory to the structure of the package. It is added as
0210      * a not-required element with no associated mimeTypes.
0211      *
0212      * Starting in 4.6, if an entry with the given key
0213      * already exists, the path is added to it as a search alternative.
0214      *
0215      * @param key  used as an internal label for this directory
0216      * @param path the path within the package for this directory
0217      * @param name the user visible (translated) name for the directory
0218      **/
0219     void addDirectoryDefinition(const char *key, const QString &path, const QString &name);
0220 
0221     /**
0222      * Adds a file to the structure of the package. It is added as
0223      * a not-required element with no associated mimeTypes.
0224      *
0225      * Starting in 4.6, if an entry with the given key
0226      * already exists, the path is added to it as a search alternative.
0227      *
0228      * @param key  used as an internal label for this file
0229      * @param path the path within the package for this file
0230      * @param name the user visible (translated) name for the file
0231      **/
0232     void addFileDefinition(const char *key, const QString &path, const QString &name);
0233 
0234     /**
0235      * Removes a definition from the structure of the package.
0236      * @since 4.6
0237      * @param key the internal label of the file or directory to remove
0238      */
0239     void removeDefinition(const char *key);
0240 
0241     /**
0242      * Sets whether or not a given part of the structure is required or not.
0243      * The path must already have been added using addDirectoryDefinition
0244      * or addFileDefinition.
0245      *
0246      * @param key the entry within the package
0247      * @param required true if this entry is required, false if not
0248      */
0249     void setRequired(const char *key, bool required);
0250 
0251     /**
0252      * Defines the default mimeTypes for any definitions that do not have
0253      * associated mimeTypes. Handy for packages with only one or predominantly
0254      * one file type.
0255      *
0256      * @param mimeTypes a list of mimeTypes
0257      **/
0258     void setDefaultMimeTypes(QStringList mimeTypes);
0259 
0260     /**
0261      * Define mimeTypes for a given part of the structure
0262      * The path must already have been added using addDirectoryDefinition
0263      * or addFileDefinition.
0264      *
0265      * @param key the entry within the package
0266      * @param mimeTypes a list of mimeTypes
0267      **/
0268     void setMimeTypes(const char *key, QStringList mimeTypes);
0269 
0270     /**
0271      * Sets the prefixes that all the contents in this package should
0272      * appear under. This defaults to "contents/" and is added automatically
0273      * between the base path and the entries as defined by the package
0274      * structure. Multiple entries can be added.
0275      * In this case each file request will be searched in all prefixes in order,
0276      * and the first found will be returned.
0277      *
0278      * @param prefix paths the directory prefix to use
0279      * @since 4.6
0280      */
0281     void setContentsPrefixPaths(const QStringList &prefixPaths);
0282 
0283     /**
0284      * Sets service prefix.
0285      */
0286     void setServicePrefix(const QString &servicePrefix);
0287 
0288     /**
0289      * Sets whether or not external paths/symlinks can be followed by a package
0290      * @param allow true if paths/symlinks outside of the package should be followed,
0291      *             false if they should be rejected.
0292      */
0293     void setAllowExternalPaths(bool allow);
0294 
0295     /**
0296      * Sets preferred package root.
0297      */
0298     void setDefaultPackageRoot(const QString &packageRoot);
0299 
0300     /**
0301      * Sets the fallback package root path
0302      * If a file won't be found in this package, it will search it in the package
0303      * with the same structure identified by path
0304      * It is intended to be used by the packageStructure
0305      * @param path package root path @see setPath
0306      */
0307     void setFallbackPackage(const Plasma::Package &package);
0308 
0309     /**
0310      * @return The fallback package root path
0311      */
0312     Plasma::Package fallbackPackage() const;
0313 
0314     // Content structure description methods
0315     /**
0316      * @return all directories registered as part of this Package's structure
0317      */
0318     QList<const char *> directories() const;
0319 
0320     /**
0321      * @return all directories registered as part of this Package's required structure
0322      */
0323     QList<const char *> requiredDirectories() const;
0324 
0325     /**
0326      * @return all files registered as part of this Package's structure
0327      */
0328     QList<const char *> files() const;
0329 
0330     /**
0331      * @return all files registered as part of this Package's required structure
0332      */
0333     QList<const char *> requiredFiles() const;
0334 
0335     /**
0336      * Installs a package matching this package structure. By default installs a
0337      * native Plasma::Package.
0338      *
0339      * @return KJob to track installation progress and result
0340      **/
0341     KJob *install(const QString &sourcePackage, const QString &packageRoot = QString());
0342 
0343     /**
0344      * Uninstalls a package matching this package structure.
0345      *
0346      * @return KJob to track removal progress and result
0347      */
0348     KJob *uninstall(const QString &packageName, const QString &packageRoot);
0349 
0350     /**
0351      * @returns the wrapped KPackage::Package instance, which deprecated this class
0352      *
0353      * @since 5.28
0354      */
0355     KPackage::Package kPackage() const;
0356 
0357 private:
0358     QExplicitlySharedDataPointer<PackagePrivate> d;
0359     friend class PackagePrivate;
0360     friend class PackageStructure;
0361     friend class AppletPrivate;
0362     friend class Applet;
0363     friend class Corona;
0364 };
0365 
0366 }
0367 Q_DECLARE_METATYPE(Plasma::Package)
0368 
0369 #endif // PLASMA_ENABLE_DEPRECATED_SINCE(5, 28)
0370 
0371 #endif