KPackage::Package Class
Object representing an installed package. More...
Header: | #include <KPackage/Package> |
CMake: | find_package(KF6 REQUIRED COMPONENTS Package) target_link_libraries(mytarget PRIVATE KF6::Package) |
Public Functions
Package(KPackage::PackageStructure *structure = nullptr) | |
void | addDirectoryDefinition(const QByteArray &key, const QString &path) |
void | addFileDefinition(const QByteArray &key, const QString &path) |
bool | allowExternalPaths() const |
QStringList | contentsPrefixPaths() const |
(since 5.21) QByteArray | cryptographicHash(QCryptographicHash::Algorithm algorithm) const |
QString | defaultPackageRoot() const |
QList<QByteArray> | directories() const |
QStringList | entryList(const QByteArray &key) const |
KPackage::Package | fallbackPackage() const |
QString | filePath(const QByteArray &key, const QString &filename = QString()) const |
(since 5.41) QUrl | fileUrl(const QByteArray &key, const QString &filename = QString()) const |
QList<QByteArray> | files() const |
(since 5.1) bool | hasValidStructure() const |
bool | isRequired(const QByteArray &key) const |
bool | isValid() const |
KPluginMetaData | metadata() const |
QStringList | mimeTypes(const QByteArray &key) const |
const QString | path() const |
void | removeDefinition(const QByteArray &key) |
QList<QByteArray> | requiredDirectories() const |
QList<QByteArray> | requiredFiles() const |
void | setAllowExternalPaths(bool allow) |
void | setContentsPrefixPaths(const QStringList &prefixPaths) |
void | setDefaultMimeTypes(const QStringList &mimeTypes) |
void | setDefaultPackageRoot(const QString &packageRoot) |
void | setFallbackPackage(const KPackage::Package &package) |
(since 5.88) void | setMetadata(const KPluginMetaData &data) |
void | setMimeTypes(const QByteArray &key, const QStringList &mimeTypes) |
void | setPath(const QString &path) |
void | setRequired(const QByteArray &key, bool required) |
Detailed Description
Package defines what is in a package and provides easy access to the contents.
To define a package, one might write the following code:
Package package; package.addDirectoryDefinition("images", "pics/"); package.setMimeTypes("images", QStringList{"image/svg", "image/png", "image/jpeg"}); package.addDirectoryDefinition("scripts", "code/"); package.setMimeTypes("scripts", QStringList{"text/\*"}); package.addFileDefinition("mainscript", "code/main.js"); package.setRequired("mainscript", true);
One may also choose to create a subclass of PackageStructure and include the setup in the constructor.
Either way, Package creates a self-documenting contract between the packager and the application without exposing package internals such as actual on-disk structure of the package or requiring that all contents be explicitly known ahead of time.
Subclassing PackageStructure does have provide a number of potential const benefits:
- the package can be notified of path changes via the virtual pathChanged() method
- the subclass may implement mechanisms to install and remove packages using the virtual install and uninstall methods
- subclasses can be compiled as plugins for easy re-use
Member Function Documentation
[explicit]
Package::Package(KPackage::PackageStructure *structure = nullptr)
Default constructor
structure if a null pointer is passed in, this will creates an empty (invalid) Package; otherwise the structure is allowed to set up the Package's initial layout
void Package::addDirectoryDefinition(const QByteArray &key, const QString &path)
Adds a directory to the structure of the package. It is added as a not-required element with no associated mimeTypes. If an entry with the given key already exists, the path is added to it as a search alternative.
key used as an internal label for this directory
path the path within the package for this directory
void Package::addFileDefinition(const QByteArray &key, const QString &path)
Adds a file to the structure of the package. It is added as a not-required element with no associated mimeTypes. If an entry with the given key already exists, the path is added to it as a search alternative.
key used as an internal label for this file
path the path within the package for this file
bool Package::allowExternalPaths() const
Returns true if paths/symlinks outside the package itself should be followed. By default this is set to false for security reasons.
See also setAllowExternalPaths().
QStringList Package::contentsPrefixPaths() const
Returns the prefix paths inserted between the base path and content entries, in order of priority. When searching for a file, all paths will be tried in order.
See also setContentsPrefixPaths().
[since 5.21]
QByteArray Package::cryptographicHash(QCryptographicHash::Algorithm algorithm) const
Returns a hash digest of the contents of the package in hexadecimal form
This function was introduced in 5.21.
QString Package::defaultPackageRoot() const
Returns preferred package root. This defaults to kpackage/generic/
See also setDefaultPackageRoot().
QList<QByteArray> Package::directories() const
Returns all directories registered as part of this Package's structure
QStringList Package::entryList(const QByteArray &key) const
Get the list of files of a given type.
fileType the type of file to look for, as defined in the package structure. Returns list of files by name, suitable for passing to filePath
KPackage::Package Package::fallbackPackage() const
Returns The fallback package root path
See also setFallbackPackage().
QString Package::filePath(const QByteArray &key, const QString &filename = QString()) const
Get the path to a given file based on the key and an optional filename. Example: finding the main script in a scripting package: filePath("mainscript")
Example: finding a specific image in the images directory: filePath("images", "myimage.png")
key the key of the file type to look for,
filename optional name of the file to locate within the package
Returns path to the file on disk. QString() if not found.
[since 5.41]
QUrl Package::fileUrl(const QByteArray &key, const QString &filename = QString()) const
Get the url to a given file based on the key and an optional filename, is the file:// or qrc:// format Example: finding the main script in a scripting package: filePath("mainscript")
Example: finding a specific image in the images directory: filePath("images", "myimage.png")
key the key of the file type to look for,
filename optional name of the file to locate within the package
Returns path to the file on disk. QUrl() if not found.
This function was introduced in 5.41.
QList<QByteArray> Package::files() const
Returns all files registered as part of this Package's structure
[since 5.1]
bool Package::hasValidStructure() const
Returns true
if this package has a valid PackageStructure associatedw it with it.
A package may not be valid, but have a valid structure. Useful when dealing with Package objects in a semi-initialized state (e.g. before calling setPath())
This function was introduced in 5.1.
bool Package::isRequired(const QByteArray &key) const
Returns true if the item at path exists and is required
bool Package::isValid() const
Return true
if all the required components exist
KPluginMetaData Package::metadata() const
Returns the package metadata object.
See also setMetadata().
QStringList Package::mimeTypes(const QByteArray &key) const
Returns the mimeTypes associated with the path, if any
See also setMimeTypes().
const QString Package::path() const
Returns the path to the root of this particular package
See also setPath().
void Package::removeDefinition(const QByteArray &key)
Removes a definition from the structure of the package.
key the internal label of the file or directory to remove
QList<QByteArray> Package::requiredDirectories() const
Returns all directories registered as part of this Package's required structure
QList<QByteArray> Package::requiredFiles() const
Returns all files registered as part of this Package's required structure
void Package::setAllowExternalPaths(bool allow)
Sets whether or not external paths/symlinks can be followed by a package
allow true if paths/symlinks outside of the package should be followed, false if they should be rejected.
See also allowExternalPaths().
void Package::setContentsPrefixPaths(const QStringList &prefixPaths)
Sets the prefixes that all the contents in this package should appear under. This defaults to "contents/" and is added automatically between the base path and the entries as defined by the package structure. Multiple entries can be added. In this case each file request will be searched in all prefixes in order, and the first found will be returned.
prefix paths the directory prefix to use
See also contentsPrefixPaths().
void Package::setDefaultMimeTypes(const QStringList &mimeTypes)
Defines the default mimeTypes for any definitions that do not have associated mimeTypes. Handy for packages with only one or predominantly one file type.
mimeTypes a list of mimeTypes
void Package::setDefaultPackageRoot(const QString &packageRoot)
Sets preferred package root.
See also defaultPackageRoot().
void Package::setFallbackPackage(const KPackage::Package &package)
Sets the fallback package root path If a file won't be found in this package, it will search it in the package with the same structure identified by path It is intended to be used by the packageStructure
path package root path
See also fallbackPackage() and setPath.
[since 5.88]
void Package::setMetadata(const KPluginMetaData &data)
Sets the metadata for the KPackage. This overwrites the current metadata. This should be used in case a kpackage gets loaded by name, based on the path a C++ plugin which has embedded metadata.
This function was introduced in 5.88.
See also metadata().
void Package::setMimeTypes(const QByteArray &key, const QStringList &mimeTypes)
Define mimeTypes for a given part of the structure The path must already have been added using addDirectoryDefinition or addFileDefinition.
key the entry within the package
mimeTypes a list of mimeTypes
See also mimeTypes().
void Package::setPath(const QString &path)
Sets the path to the root of this package
path an absolute path, or a relative path to the default package root
See also path().
void Package::setRequired(const QByteArray &key, bool required)
Sets whether or not a given part of the structure is required or not. The path must already have been added using addDirectoryDefinition or addFileDefinition.
key the entry within the package
required true if this entry is required, false if not
See also isRequired().