Plasma

package.cpp
1 /*
2  SPDX-FileCopyrightText: 2007 Aaron Seigo <[email protected]>
3  SPDX-FileCopyrightText: 2010 Marco Martin <[email protected]>
4  SPDX-FileCopyrightText: 2010 Kevin Ottens <[email protected]>
5  SPDX-FileCopyrightText: 2009 Rob Scheepmaker
6 
7  SPDX-License-Identifier: LGPL-2.0-or-later
8 */
9 
10 #include "package.h"
11 
12 #include <QTemporaryDir>
13 
14 #include <KArchive>
15 #include <KDesktopFile>
16 #include <KJob>
17 #include <KTar>
18 #include <QDebug>
19 #include <kzip.h>
20 
21 #include "config-plasma.h"
22 
23 #include <QMimeDatabase>
24 #include <QStandardPaths>
25 
26 #include "packagestructure.h"
27 #include "pluginloader.h"
28 #include "private/package_p.h"
29 #include "private/packagestructure_p.h"
30 
31 namespace Plasma
32 {
33 PackagePrivate::PackagePrivate()
34  : internalPackage(nullptr)
35  , fallbackPackage(nullptr)
36  , structure(nullptr)
37 {
38 }
39 
40 PackagePrivate::~PackagePrivate()
41 {
42 }
43 
44 Package::Package(PackageStructure *structure)
45  : d(new Plasma::PackagePrivate())
46 {
47  d->structure = structure;
48 
49  if (!structure) {
50  d->internalPackage = new KPackage::Package();
51  return;
52  }
53 
54  if (!structure->d->internalStructure) {
55  d->structure->d->internalStructure = new KPackage::PackageStructure;
56  }
57 
58  d->internalPackage = new KPackage::Package(structure->d->internalStructure);
59  PackageStructureWrapper::s_packagesMap[d->internalPackage] = this;
60  structure->initPackage(this);
61 }
62 
64  : d(new Plasma::PackagePrivate())
65 {
66  d->internalPackage = new KPackage::Package(*other.d->internalPackage);
67  d->structure = other.d->structure;
68  PackageStructureWrapper::s_packagesMap[d->internalPackage] = this;
69 }
70 
72  : d(new Plasma::PackagePrivate())
73 {
74  d->internalPackage = new KPackage::Package(other);
75  PackageStructureWrapper::s_packagesMap[d->internalPackage] = this;
76 }
77 
78 Package::~Package()
79 {
80  PackageStructureWrapper::s_packagesMap.remove(d->internalPackage);
81  delete d->internalPackage;
82 }
83 
85 {
86  if (&rhs != this) {
87  d->internalPackage = new KPackage::Package(*rhs.d->internalPackage);
88  d->structure = rhs.d->structure;
89  PackageStructureWrapper::s_packagesMap[d->internalPackage] = this;
90  }
91 
92  return *this;
93 }
94 
96 {
97  return d->internalPackage->hasValidStructure();
98 }
99 
100 bool Package::isValid() const
101 {
102  return d->internalPackage->isValid();
103 }
104 
105 QString Package::name(const char *key) const
106 {
107  return d->internalPackage->name(key);
108 }
109 
110 bool Package::isRequired(const char *key) const
111 {
112  return d->internalPackage->isRequired(key);
113 }
114 
115 QStringList Package::mimeTypes(const char *key) const
116 {
117  return d->internalPackage->mimeTypes(key);
118 }
119 
121 {
122  return d->internalPackage->defaultPackageRoot();
123 }
124 
125 void Package::setDefaultPackageRoot(const QString &packageRoot)
126 {
127  d->internalPackage->setDefaultPackageRoot(packageRoot);
128 }
129 
131 {
132  d->fallbackPackage = new Package(package);
133  d->internalPackage->setFallbackPackage(*package.d->internalPackage);
134 }
135 
137 {
138  if (d->fallbackPackage) {
139  return (*d->fallbackPackage);
140  } else {
141  return Package();
142  }
143 }
144 
146 {
147  return d->servicePrefix;
148 }
149 
151 {
152  d->servicePrefix = servicePrefix;
153 }
154 
156 {
157  return *d->internalPackage;
158 }
159 
161 {
162  return d->internalPackage->allowExternalPaths();
163 }
164 
166 {
167  d->internalPackage->setAllowExternalPaths(allow);
168 }
169 
171 {
172  return KPluginInfo::fromMetaData(d->internalPackage->metadata());
173 }
174 
175 QString Package::filePath(const char *fileType, const QString &filename) const
176 {
177  return d->internalPackage->filePath(fileType, filename);
178 }
179 
180 QStringList Package::entryList(const char *key) const
181 {
182  return d->internalPackage->entryList(key);
183 }
184 
186 {
187  if (path == d->internalPackage->path()) {
188  return;
189  }
190 
191  d->internalPackage->setPath(path);
192 }
193 
194 const QString Package::path() const
195 {
196  return d->internalPackage->path();
197 }
198 
200 {
201  return d->internalPackage->contentsPrefixPaths();
202 }
203 
205 {
206  d->internalPackage->setContentsPrefixPaths(prefixPaths);
207 }
208 
210 {
211  return QString::fromLocal8Bit(d->internalPackage->cryptographicHash(QCryptographicHash::Sha1));
212 }
213 
214 void Package::addDirectoryDefinition(const char *key, const QString &path, const QString &name)
215 {
216  d->internalPackage->addDirectoryDefinition(key, path, name);
217 }
218 
219 void Package::addFileDefinition(const char *key, const QString &path, const QString &name)
220 {
221  d->internalPackage->addFileDefinition(key, path, name);
222 }
223 
224 void Package::removeDefinition(const char *key)
225 {
226  d->internalPackage->removeDefinition(key);
227 }
228 
229 void Package::setRequired(const char *key, bool required)
230 {
231  d->internalPackage->setRequired(key, required);
232 }
233 
235 {
236  d->internalPackage->setDefaultMimeTypes(mimeTypes);
237 }
238 
240 {
241  d->internalPackage->setMimeTypes(key, mimeTypes);
242 }
243 
245 {
246  QList<const char *> dirs;
247  const auto directories = d->internalPackage->directories();
248  for (const auto &data : directories) {
249  dirs << data.constData();
250  }
251 
252  return dirs;
253 }
254 
256 {
257  QList<const char *> dirs;
258  const auto directories = d->internalPackage->requiredDirectories();
259  for (const auto &data : directories) {
260  dirs << data.constData();
261  }
262 
263  return dirs;
264 }
265 
267 {
269  const auto lstFiles = d->internalPackage->files();
270  for (const auto &data : lstFiles) {
271  files << data.constData();
272  }
273 
274  return files;
275 }
276 
278 {
280  const auto lstFiles = d->internalPackage->requiredFiles();
281  for (const auto &data : lstFiles) {
282  files << data.constData();
283  }
284 
285  return files;
286 }
287 
288 KJob *Package::install(const QString &sourcePackage, const QString &packageRoot)
289 {
290  const QString src = sourcePackage;
291  const QString dest = packageRoot.isEmpty() ? defaultPackageRoot() : packageRoot;
292  // qCDebug(LOG_PLASMA) << "Source: " << src;
293  // qCDebug(LOG_PLASMA) << "PackageRoot: " << dest;
294  KJob *j = d->structure->install(this, src, dest);
295  return j;
296 }
297 
298 KJob *Package::uninstall(const QString &packageName, const QString &packageRoot)
299 {
300  setPath(packageRoot + packageName);
301 
302  return d->structure->uninstall(this, packageRoot);
303 }
304 
305 } // Namespace
bool allowExternalPaths() const
Definition: package.cpp:160
static KPluginInfo fromMetaData(const KPluginMetaData &meta)
void setAllowExternalPaths(bool allow)
Sets whether or not external paths/symlinks can be followed by a package.
Definition: package.cpp:165
void setMimeTypes(const char *key, QStringList mimeTypes)
Define mimeTypes for a given part of the structure The path must already have been added using addDir...
Definition: package.cpp:239
QStringList entryList(const char *key) const
Get the list of files of a given type.
Definition: package.cpp:180
void setRequired(const char *key, bool required)
Sets whether or not a given part of the structure is required or not.
Definition: package.cpp:229
KJob * install(const QString &sourcePackage, const QString &packageRoot=QString())
Installs a package matching this package structure.
Definition: package.cpp:288
Namespace for everything in libplasma.
Definition: datamodel.cpp:14
KPackage::Package kPackage() const
Definition: package.cpp:155
QString fromLocal8Bit(const char *str, int size)
const QString path() const
Definition: package.cpp:194
void setContentsPrefixPaths(const QStringList &prefixPaths)
Sets the prefixes that all the contents in this package should appear under.
Definition: package.cpp:204
QString filePath(const char *key, const QString &filename=QString()) const
Get the path to a given file based on the key and an optional filename.
Definition: package.cpp:175
bool isEmpty() const const
void setDefaultPackageRoot(const QString &packageRoot)
Sets preferred package root.
Definition: package.cpp:125
Plasma::Package fallbackPackage() const
Definition: package.cpp:136
Package & operator=(const Package &rhs)
Assignment operator.
Definition: package.cpp:84
bool isValid() const
Definition: package.cpp:100
KJob * uninstall(const QString &packageName, const QString &packageRoot)
Uninstalls a package matching this package structure.
Definition: package.cpp:298
object representing an installed Plasma package
Definition: package.h:74
bool hasValidStructure() const
Definition: package.cpp:95
QString servicePrefix() const
Definition: package.cpp:145
Package(PackageStructure *structure=nullptr)
Default constructor.
Definition: package.cpp:44
bool isRequired(const char *key) const
Definition: package.cpp:110
void setFallbackPackage(const Plasma::Package &package)
Sets the fallback package root path If a file won&#39;t be found in this package, it will search it in th...
Definition: package.cpp:130
QList< const char * > requiredDirectories() const
Definition: package.cpp:255
QList< const char * > requiredFiles() const
Definition: package.cpp:277
QString contentsHash() const
Definition: package.cpp:209
void removeDefinition(const char *key)
Removes a definition from the structure of the package.
Definition: package.cpp:224
QStringList mimeTypes(const char *key) const
Definition: package.cpp:115
void setDefaultMimeTypes(QStringList mimeTypes)
Defines the default mimeTypes for any definitions that do not have associated mimeTypes.
Definition: package.cpp:234
KPluginInfo metadata() const
Definition: package.cpp:170
void addDirectoryDefinition(const char *key, const QString &path, const QString &name)
Adds a directory to the structure of the package.
Definition: package.cpp:214
QStringList contentsPrefixPaths() const
Definition: package.cpp:199
void setPath(const QString &path)
Sets the path to the root of this package.
Definition: package.cpp:185
void setServicePrefix(const QString &servicePrefix)
Sets service prefix.
Definition: package.cpp:150
QList< const char * > files() const
Definition: package.cpp:266
QList< const char * > directories() const
Definition: package.cpp:244
void addFileDefinition(const char *key, const QString &path, const QString &name)
Adds a file to the structure of the package.
Definition: package.cpp:219
QString name(const char *key) const
Definition: package.cpp:105
QString defaultPackageRoot() const
Definition: package.cpp:120
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue May 11 2021 22:41:05 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.