• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

LICENSES/H03-May-2022-

autotests/H03-May-2022-2,1421,632

docs/H03-May-2022-143124

po/H04-Dec-2021-30,61425,692

src/H03-May-2022-4,1942,796

.git-blame-ignore-revsH A D04-Dec-2021137 54

.gitignoreH A D04-Dec-2021305 2928

.gitlab-ci.ymlH A D04-Dec-2021284 75

.kde-ci.ymlH A D04-Dec-2021308 1210

KF5PackageConfig.cmake.inH A D04-Dec-2021906 2617

KF5PackageMacros.cmakeH A D04-Dec-20217.9 KiB153137

README.mdH A D04-Dec-20218.7 KiB14394

metainfo.yamlH A D04-Dec-2021380 2018

qrc.cmakeH A D04-Dec-2021506 1814

README.md

1# KPackage Framework
2
3Installation and loading of additional content (ex: scripts, images...) as packages
4
5## Introduction
6
7The KPackage framework lets the user install and load packages of non binary content such as scripted extensions or graphic assets, as if they were traditional plugins.
8
9# Using KPackage
10
11The frameworks consists of 3 classes: PackageStructure, PackageLoader and Package
12The central class is Package, that represents an installed package and is used to access its file, install, update or uninstall it.
13
14In order to use the framework the developer has to define a package structure. (the package type, that will be depending on the particular application, and the type of content that will be provided, may be a sound theme, graphics assets, qml files etc).
15
16The package loader is used to load instances of Package of a given type.
17
18## Package class
19
20Package is the central class of the framework, and it represents a package of a certain type, it can be either "abstract", not pointing to a particular package on disk (in which case the only things allowed will be installing and uninstalling) or it can point to a particular package installed on disk: its path() not being empty and valid.
21
22A Package defines a particular filesystem structure for installed addons, made up of allowed subdirectories in which content will be organized and several optional named files, if you want to mandate a particular named file in every package (for instance if your addons need a file named "main.qml" in order to be considered valid)
23
24A fixed structure is encouraged by the api of Package in order to encourage plugin creators to distribute packages made with a standard and expected structure, encouraging a common path installation prefix, and strongly discouraging cross references between different packages such as ../ relative paths and symlinks across packages.
25
26An example filesystem structure (that in the end, depends from the PackageStructure) may be:
27
28```
29
30(root)
31|-- code
32|   `-- main.js
33|-- config
34|   `-- config.xml
35|-- images
36|   `-- background.png
37`-- metadata.desktop
38
39```
40
41The special, main and always required for every packagestructure file is the "metadata" file, which describes the package with values such as name, description, pluginname etc. It is in any format accepted by KPluginMetadata, meaning at the moment either a .desktop or json files. The metadata is accessible with Package::metadata().
42
43All the other files are under the contents/ subdirectory: a folder under addDirectoryDefinition will be registered under contents/.
44
45If the developer wants that those extension package require to have a particular file or folder, he will use setRequired() on a particular key: in that case if a package misses that file or folder, isValid() will return false and all the filePath resolution won't work.
46
47To access a file within the package, the filePath(key, filename) method is used. In order to use that the caller doesn't have to care where the package is installed and absolute paths of any kind, it will pass a relative one, and the prober absolute path will be resolved and returned.
48
49The key parameter is one of those that has been registered by the structure beforehand by addFileDefinition or addDirectoryDefinition. Files asked with an unknown keys won't be resolved.
50
51Accessing a file under a directory will look like package.filePath("ui", "main.qml") while accessing a particular named file will look like package.filePath("mainscript")
52
53If as file path is passed a relative path with ".." elements in it or an absolute path, the resolution will fail as well, in order to discourage cross references (same thing will happen if the resolved file is a symlink), unless the package structure explicitly allowed it with setAllowExternalPaths()
54Here is a minimal example of a PackageStructure::initPackage.
55
56```
57...
58void MyStructure::initPackage(KPackage::Package *package)
59{
60    package->setDefaultPackageRoot(QStringLiteral("myapp" "/packages/"));
61
62    package->addDirectoryDefinition("images", QStringLiteral("images"), i18n("Images"));
63    QStringList mimetypes;
64    mimetypes << QStringLiteral("image/svg+xml") << QStringLiteral("image/png") << QStringLiteral("image/jpeg");
65    package->setMimeTypes("images", mimetypes);
66    package->addDirectoryDefinition("code", QStringLiteral("code"), i18n("Javascript files"));
67    package->addFileDefinition("mainscript", QStringLiteral("scripts/main.js"), i18n("Main Script File"));
68    //this way, the package will not be considered valid if mainscript is not present
69    package->setRequired("mainscript", true);
70}
71...
72K_PLUGIN_CLASS_WITH_JSON(MyStructure, "myapp-packagestructure-mystructure.json")
73```
74
75The line K_PLUGIN_CLASS_WITH_JSON is important in order to export the PackageStructure subclass MyStructure as a standalone plugin library using the KPluginLoader architecture, in order to be loadable and recognizable by a PackageLoader instance from any process (without the need to explicitly link to a library containing the MyStructure implementation).
76
77In order to build the plugin, it is also needed a .desktop file describing the metadata for the plugin:
78
79```
80[Desktop Entry]
81Name=My package type
82Type=Service
83X-KDE-ServiceTypes=KPackage/PackageStructure
84X-KDE-Library=myapp_packagestructure_mystructure
85
86X-KDE-PluginInfo-Author=John Doe
87X-KDE-PluginInfo-Email=john@example.com
88#X-KDE-PluginInfo-Name will be used for loading, so PackageLoader::loadPackage("MyApp/MyStructure);
89X-KDE-PluginInfo-Name=MyApp/MyStructure
90X-KDE-PluginInfo-Version=1
91X-KDE-ParentApp=org.kde.myapp
92
93```
94
95And an own CMakeLists.txt
96
97```
98#build the PackageStructure implementation as a standalone library
99add_library(myapp_packagestructure_mystructure MODULE mystructure.cpp)
100
101target_link_libraries(myapp_packagestructure_mystructure
102   KF5::I18n
103   KF5::Package
104)
105
106#Qt plugins needs metadata in json format baked into the library
107kcoreaddons_desktop_to_json(myapp_packagestructure_mystructure myapp-packagestructure-mystructure.desktop)
108
109#install the plugin where PackageLoader looks for them
110install(TARGETS myapp_packagestructure_mystructure DESTINATION ${KDE_INSTALL_PLUGINDIR}/kpackage/packagestructure)
111
112```
113
114The c++ implementation with its cmake and desktop files are recommended to be in their own subdirectory, for separation respect to the code of the parent application.
115
116
117## Package structures
118
119Package structures are instance of PackageStructure and are shipeed as plugins.
120
121PackageStructure::initPackage will be executed once when any package is created, and this initializes the Package instance on what are the allowed subfolders and named files for this type. The most important thing to do in initPackage is setting a packageRoot path, that will be a common prefix under which all packages will be installed, relative to the xdg data paths. For instance the packages of type "plasmoid" will be under "plasma/plasmoids" which means searching under ~/.local/share/plasma/plasmoids and /usr/share/plasma/plasmoids
122
123This is the only required function to be reimplemented for a given PackageStructure, the other ones being optional only when particular needs ensue.
124
125PackageStructure::pathChanged gets executed when the base path of the Package changes (that means, what package on the filesystem the class is pointing to, as this can change at runetime with Package::setPath). Reimplement this only if you need to have extra check/operations when such a thing happens.
126
127## Package loader
128
129The PackageLoader is used to list, find and load Package instances.
130
131The most important functions of this class are listPackages/findPackages to list and search packages of a certain type (package structure), for example listing all plasmoids or all plasmoids that have "clock" in their description PackageLoader::loadPackage loads a "blank" package of a certain type. The returned package will be a Package instance that has been initialized to that given structure, pointing to a particular package in the filesystem only if the optional parameter packagePath has been passed. If not, it will be an invalid package, still usable to install and uninstall packages of this type or it can always load a package on the filesystem with the setPath Package method, relative to the packageroot.
132
133Upon setPath, subdirectories of the packageroot will be searched both in the global system installation and in the user home, preferring the home, such as ~/.local/share/plasma/plasmoids and /usr/share/plasma/plasmoids. If one has to iterate through packages, creating a single Package instance then loading different ones with subsequent calls of setPath is preferable over creating multiple instances for performance reasons.
134Example of code loading a package:
135
136```
137KPackage::Package p = KPackage::PackageLoader::self()->loadPackage("Plasma/Applet", "org.kde.plasma.analogclock");
138if (p.isValid()) {
139    qDebug() << p.filePath("mainscript");
140}
141```
142
143