1 /******************************************************************************
2 * Copyright 2007 by Aaron Seigo <aseigo@kde.org> *
4 * This library is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU Library General Public *
6 * License as published by the Free Software Foundation; either *
7 * version 2 of the License, or (at your option) any later version. *
9 * This library is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
12 * Library General Public License for more details. *
14 * You should have received a copy of the GNU Library General Public License *
15 * along with this library; see the file COPYING.LIB. If not, write to *
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301, USA. *
18 *******************************************************************************/
20 #ifndef PLASMA_PACKAGESTRUCTURE_H
21 #define PLASMA_PACKAGESTRUCTURE_H
23 #include <QtCore/QStringList>
24 #include <QtCore/QSharedData>
26 #include <kgenericfactory.h>
28 #include <ksharedptr.h>
30 #include <plasma/version.h>
31 #include "packagemetadata.h"
38 class PackageStructurePrivate
;
41 * @class PackageStructure plasma/packagestructure.h <Plasma/PackageStructure>
43 * @short A description of the expected file structure of a given package type
45 * PackageStructure defines what is in a package. This information is used
46 * to create packages and provides a way to programatically refer to contents.
48 * An example usage of this class might be:
51 PackageStructure structure;
53 structure.addDirectoryDefinition("images", "pics/", i18n("Images"));
54 QStringList mimetypes;
55 mimetypes << "image/svg" << "image/png" << "image/jpeg";
56 structure.setMimetypes("images", mimetypes);
58 structure.addDirectoryDefinition("scripts", "code/", i18n("Executable Scripts"));
60 mimetypes << "text/\*";
61 structure.setMimetypes("scripts", mimetypes);
63 structure.addFileDefinition("mainscript", "code/main.js", i18n("Main Script File"));
64 structure.setRequired("mainscript", true);
66 * One may also choose to create a subclass of PackageStructure and include the setup
69 * Either way, PackageStructure creates a sort of "contract" between the packager and
70 * the application which is also self-documenting.
72 class PLASMA_EXPORT PackageStructure
: public QObject
, public QSharedData
77 typedef KSharedPtr
<PackageStructure
> Ptr
;
80 * Default constructor for a package structure definition
82 * @arg type the type of package. This is often application specific.
84 explicit PackageStructure(QObject
*parent
= 0,
85 const QString
&type
= i18nc("A non-functional package", "Invalid"));
90 virtual ~PackageStructure();
95 PackageStructure
&operator=(const PackageStructure
&rhs
);
98 * Loads a package format by name.
100 * @arg format If not empty, attempts to locate the given format, either
101 * from built-ins or via plugins.
102 * @return a package that matches the format, if available. The caller
103 * is responsible for deleting the object.
105 static PackageStructure::Ptr
load(const QString
&packageFormat
);
108 * Type of package this structure describes
110 QString
type() const;
113 * The directories defined for this package
115 QList
<const char*> directories() const;
118 * The required directories defined for this package
120 QList
<const char*> requiredDirectories() const;
123 * The individual files, by key, that are defined for this package
125 QList
<const char*> files() const;
128 * The individual required files, by key, that are defined for this package
130 QList
<const char*> requiredFiles() const;
133 * Adds a directory to the structure of the package. It is added as
134 * a not-required element with no associated mimetypes.
136 * @param key used as an internal label for this directory
137 * @param path the path within the package for this directory
138 * @param name the user visible (translated) name for the directory
140 void addDirectoryDefinition(const char *key
, const QString
&path
, const QString
&name
);
143 * Adds a file to the structure of the package. It is added as
144 * a not-required element with no associated mimetypes.
146 * @param key used as an internal label for this file
147 * @param path the path within the package for this file
148 * @param name the user visible (translated) name for the file
150 void addFileDefinition(const char *key
, const QString
&path
, const QString
&name
);
153 * @return path relative to the package root for the given entry
155 QString
path(const char *key
) const;
158 * @return user visible name for the given entry
160 QString
name(const char *key
) const;
163 * Sets whether or not a given part of the structure is required or not.
164 * The path must already have been added using addDirectoryDefinition
165 * or addFileDefinition.
167 * @param key the entry within the package
168 * @param required true if this entry is required, false if not
170 void setRequired(const char *key
, bool required
);
173 * @return true if the item at path exists and is required
175 bool isRequired(const char *key
) const;
178 * Defines the default mimetypes for any definitions that do not have
179 * associated mimetypes. Handy for packages with only one or predominantly
182 * @param mimetypes a list of mimetypes
184 void setDefaultMimetypes(QStringList mimetypes
);
187 * Define mimetypes for a given part of the structure
188 * The path must already have been added using addDirectoryDefinition
189 * or addFileDefinition.
191 * @param key the entry within the package
192 * @param mimetypes a list of mimetypes
194 void setMimetypes(const char *key
, QStringList mimetypes
);
197 * @return the mimetypes associated with the path, if any
199 QStringList
mimetypes(const char *key
) const;
202 * Sets the path to the package. Useful for package formats
203 * which do not have well defined contents prior to installation.
205 void setPath(const QString
&path
);
208 * @return the path to the package, or QString() if none
210 QString
path() const;
213 * Read a package structure from a config file.
215 void read(const KConfigBase
*config
);
218 * Write this package structure to a config file.
220 void write(KConfigBase
*config
) const;
223 * Installs a package matching this package structure. By default installs a
224 * native Plasma::Package.
226 * @param archivePath path to the package archive file
227 * @param packageRoot path to the directory where the package should be
229 * @return true on successful installation, false otherwise
231 virtual bool installPackage(const QString
&archivePath
, const QString
&packageRoot
);
234 * Uninstalls a package matching this package structure.
236 * @arg packageName the name of the package to remove
237 * @arg packageRoot path to the directory where the package should be installed to
238 * @return true on successful removal of the package, false otherwise
240 virtual bool uninstallPackage(const QString
&packageName
, const QString
&packageRoot
);
243 * When called, the package plugin should display a window to the user
244 * that they can use to browser, select and then install widgets supported by
245 * this package plugin with.
247 * The user interface may be an in-process dialog or an out-of-process application.
249 * When the process is complete, the newWidgetBrowserFinished() signal must be
252 * @param parent the parent widget to use for the widget
254 virtual void createNewWidgetBrowser(QWidget
*parent
= 0);
257 * @return the prefix inserted between the base path and content entries
259 QString
contentsPrefix() const;
262 * @return preferred package root. This defaults to plasma/plasmoids/
264 QString
defaultPackageRoot() const;
267 * @return service prefix used in desktop files. This defaults to plasma-applet-
269 QString
servicePrefix() const;
272 * Sets service prefix.
274 void setServicePrefix(const QString
&servicePrefix
);
277 * @return the package metadata object.
279 virtual PackageMetadata
metadata();
282 * @return true if paths/symlinks outside the package itself should be followed.
283 * By default this is set to false for security reasons.
285 bool allowExternalPaths() const;
289 * Emitted when the new widget browser process completes.
291 void newWidgetBrowserFinished();
295 * Sets whether or not external paths/symlinks can be followed by a package
296 * @arg allow true if paths/symlinks outside of the package should be followed,
297 * false if they should be rejected.
299 void setAllowExternalPaths(bool allow
);
302 * Sets the prefix that all the contents in this package should
303 * appear under. This defaults to "contents/" and is added automatically
304 * between the base path and the entries as defined by the package
307 * @arg prefix the directory prefix to use
309 void setContentsPrefix(const QString
&prefix
);
312 * Sets preferred package root.
314 void setDefaultPackageRoot(const QString
&packageRoot
);
317 * Called whenever the path changes so that subclasses may take
318 * package specific actions.
320 virtual void pathChanged();
323 PackageStructurePrivate
* const d
;
327 * Register an applet when it is contained in a loadable module
329 #define K_EXPORT_PLASMA_PACKAGESTRUCTURE(libname, classname) \
330 K_PLUGIN_FACTORY(factory, registerPlugin<classname>();) \
331 K_EXPORT_PLUGIN(factory("plasma_packagestructure_" #libname)) \
332 K_EXPORT_PLUGIN_VERSION(PLASMA_VERSION)
334 } // Plasma namespace