plasma: drop methods to install, uninstall, register and get package file

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-10-04 21:53:59 +03:00
parent 045328e4b6
commit 46b529fc21
9 changed files with 5 additions and 475 deletions

View file

@ -2,7 +2,6 @@
<!DOCTYPE kcfg SYSTEM
"http://www.kde.org/standards/kcfg/1.0/kcfg.xsd">
<kcfg>
<group name="GetPackage" />
<group name="GetMetaData" />
<group name="DataEngine">
<entry name="EngineName" type="String">

View file

@ -25,16 +25,12 @@
#include <QFile>
#include <QRegExp>
#include <karchive.h>
#include <kcomponentdata.h>
#include <kdesktopfile.h>
#include <kmimetype.h>
#include <kplugininfo.h>
#include <kstandarddirs.h>
#include <ktar.h>
#include <ktempdir.h>
#include <ktemporaryfile.h>
#include <kzip.h>
#include <kdebug.h>
#include "packagemetadata.h"
@ -344,265 +340,6 @@ QStringList Package::listInstalledPaths(const QString &packageRoot) // static
return packages;
}
bool Package::installPackage(const QString &package,
const QString &packageRoot,
const QString &servicePrefix) // static
{
//TODO: report *what* failed if something does fail
QDir root(packageRoot);
if (!root.exists()) {
KStandardDirs::makeDir(packageRoot);
if (!root.exists()) {
kWarning() << "Could not create package root directory:" << packageRoot;
return false;
}
}
QFileInfo fileInfo(package);
if (!fileInfo.exists()) {
kWarning() << "No such file:" << package;
return false;
}
QString path;
KTempDir tempdir;
bool archivedPackage = false;
if (fileInfo.isDir()) {
// we have a directory, so let's just install what is in there
path = package;
// make sure we end in a slash!
if (path[path.size() - 1] != '/') {
path.append('/');
}
} else {
KArchive *archive = 0;
KMimeType::Ptr mimetype = KMimeType::findByPath(package);
if (mimetype->is("application/zip")) {
archive = new KZip(package);
} else if (mimetype->is("application/x-compressed-tar") ||
mimetype->is("application/x-tar")|| mimetype->is("application/x-bzip-compressed-tar") ||
mimetype->is("application/x-xz-compressed-tar") || mimetype->is("application/x-lzma-compressed-tar")) {
archive = new KTar(package);
} else {
kWarning() << "Could not open package file, unsupported archive format:" << package << mimetype->name();
return false;
}
if (!archive->open(QIODevice::ReadOnly)) {
kWarning() << "Could not open package file:" << package;
delete archive;
return false;
}
archivedPackage = true;
path = tempdir.name();
const KArchiveDirectory *source = archive->directory();
source->copyTo(path);
QStringList entries = source->entries();
if (entries.count() == 1) {
const KArchiveEntry *entry = source->entry(entries[0]);
if (entry->isDirectory()) {
path.append(entry->name()).append("/");
}
}
delete archive;
}
QString metadataPath = path + "metadata.desktop";
if (!QFile::exists(metadataPath)) {
kWarning() << "No metadata file in package" << package << metadataPath;
return false;
}
PackageMetadata meta(metadataPath);
QString targetName = meta.pluginName();
if (targetName.isEmpty()) {
kWarning() << "Package plugin name not specified";
return false;
}
// Ensure that package names are safe so package uninstall can't inject
// bad characters into the paths used for removal.
QRegExp validatePluginName("^[\\w-\\.]+$"); // Only allow letters, numbers, underscore and period.
if (!validatePluginName.exactMatch(targetName)) {
kWarning() << "Package plugin name " << targetName << "contains invalid characters";
return false;
}
targetName = packageRoot + '/' + targetName;
if (QFile::exists(targetName)) {
kWarning() << targetName << "already exists";
return false;
}
if (archivedPackage) {
// it's in a temp dir, so just move it over.
const bool ok = copyFolder(path, targetName);
removeFolder(path);
if (!ok) {
kWarning() << "Could not move package to destination:" << targetName;
return false;
}
} else {
kDebug() << "************************** 12";
// it's a directory containing the stuff, so copy the contents rather
// than move them
const bool ok = copyFolder(path, targetName);
kDebug() << "************************** 13";
if (!ok) {
kWarning() << "Could not copy package to destination:" << targetName;
return false;
}
}
if (archivedPackage) {
// no need to remove the temp dir (which has been successfully moved if it's an archive)
tempdir.setAutoRemove(false);
}
if (!servicePrefix.isEmpty()) {
// and now we register it as a service =)
kDebug() << "************************** 1";
QString metaPath = targetName + "/metadata.desktop";
kDebug() << "************************** 2";
KDesktopFile df(metaPath);
KConfigGroup cg = df.desktopGroup();
kDebug() << "************************** 3";
// Q: should not installing it as a service disqualify it?
// Q: i don't think so since KServiceTypeTrader may not be
// used by the installing app in any case, and the
// package is properly installed - aseigo
//TODO: reduce code duplication with registerPackage below
QString serviceName = servicePrefix + meta.pluginName();
QString service = KStandardDirs::locateLocal("services", serviceName + ".desktop");
kDebug() << "************************** 4";
const bool ok = QFile::copy(metaPath, service);
kDebug() << "************************** 5";
if (ok) {
// the icon in the installed file needs to point to the icon in the
// installation dir!
QString iconPath = targetName + '/' + cg.readEntry("Icon");
QFile icon(iconPath);
if (icon.exists()) {
KDesktopFile df(service);
KConfigGroup cg = df.desktopGroup();
cg.writeEntry("Icon", iconPath);
}
} else {
kWarning() << "Could not register package as service (this is not necessarily fatal):" << serviceName;
}
kDebug() << "************************** 7";
}
return true;
}
bool Package::uninstallPackage(const QString &pluginName,
const QString &packageRoot,
const QString &servicePrefix) // static
{
// We need to remove the package directory and its metadata file.
QString targetName = pluginName;
targetName = packageRoot + '/' + targetName;
if (!QFile::exists(targetName)) {
kWarning() << targetName << "does not exist";
return false;
}
QString serviceName = servicePrefix + pluginName;
QString service = KStandardDirs::locateLocal("services", serviceName + ".desktop");
kDebug() << "Removing service file " << service;
bool ok = QFile::remove(service);
if (!ok) {
kWarning() << "Unable to remove " << service;
}
ok = removeFolder(targetName);
const QString errorString("unknown");
if (!ok) {
kWarning() << "Could not delete package from:" << targetName << " : " << errorString;
return false;
}
return true;
}
bool Package::registerPackage(const PackageMetadata &data, const QString &iconPath)
{
QString serviceName("plasma-applet-" + data.pluginName());
QString service = KStandardDirs::locateLocal("services", serviceName + ".desktop");
if (data.pluginName().isEmpty()) {
return false;
}
data.write(service);
KDesktopFile config(service);
KConfigGroup cg = config.desktopGroup();
const QString type = data.type().isEmpty() ? "Service" : data.type();
cg.writeEntry("Type", type);
const QString serviceTypes = data.serviceType().isNull() ? "Plasma/Applet,Plasma/Containment" : data.serviceType();
cg.writeEntry("X-KDE-ServiceTypes", serviceTypes);
cg.writeEntry("X-KDE-PluginInfo-EnabledByDefault", true);
QFile icon(iconPath);
if (icon.exists()) {
QString installedIcon("plasma_applet_" + data.pluginName() +
iconPath.right(iconPath.length() - iconPath.lastIndexOf("/")));
cg.writeEntry("Icon", installedIcon);
installedIcon = KStandardDirs::locateLocal("icon", installedIcon);
QFile::copy(iconPath, installedIcon);
}
return true;
}
bool Package::createPackage(const PackageMetadata &metadata,
const QString &source,
const QString &destination,
const QString &icon) // static
{
Q_UNUSED(icon)
if (!metadata.isValid()) {
kWarning() << "Metadata file is not complete";
return false;
}
// write metadata in a temporary file
KTemporaryFile metadataFile;
if (!metadataFile.open()) {
return false;
}
metadata.write(metadataFile.fileName());
// put everything into a zip archive
KZip creation(destination);
creation.setCompression(KZip::NoCompression);
if (!creation.open(QIODevice::WriteOnly)) {
return false;
}
creation.addLocalFile(metadataFile.fileName(), "metadata.desktop");
creation.addLocalDirectory(source, "contents");
creation.close();
return true;
}
PackagePrivate::PackagePrivate(const PackageStructure::Ptr st, const QString &p)
: structure(st),
service(0)

View file

@ -143,59 +143,6 @@ class PLASMA_EXPORT Package
**/
static QStringList listInstalledPaths(const QString &packageRoot);
/**
* Installs a package.
*
* @param package path to the Plasmagik package
* @param packageRoot path to the directory where the package should be
* installed to
* @param servicePrefix the prefix for the desktop file, so as not to interfere
* with unrelated services (eg: "plasma-applet-"). If no prefix
* is set (e.g. a QString() is passed in), then the package will NOT
* be registered as a service
* @return true on successful installation, false otherwise
**/
static bool installPackage(const QString &package,
const QString &packageRoot,
const QString &servicePrefix);
/**
* Uninstalls a package.
*
* @param package path to the Plasmagik package
* @param packageRoot path to the directory where the package should be
* installed to
* @param servicePrefix the prefix for the desktop file, so as not to interfere
* with unrelated services (eg: "plasma-applet-")
* @return true on successful uninstallation, false otherwise
**/
static bool uninstallPackage(const QString &package,
const QString &packageRoot,
const QString &servicePrefix);
/**
* Registers a package described by the given desktop file
*
* @param the full path to the desktop file (must be KPluginInfo compatible)
* @return true on success, false on failure
*/
static bool registerPackage(const PackageMetadata &data, const QString &iconPath);
/**
* Creates a package based on the metadata from the files contained
* in the source directory
*
* @param metadata description of the package to create
* @param source path to local directory containing the individual
* files to be added to the package
* @param destination path to the package that should be created
* @param icon path to the package icon
**/
static bool createPackage(const PackageMetadata &metadata,
const QString &source,
const QString &destination,
const QString &icon = QString());
private:
PackagePrivate * const d;

View file

@ -549,16 +549,6 @@ void PackageStructure::setContentsPrefixPaths(const QStringList &prefixPaths)
}
}
bool PackageStructure::installPackage(const QString &package, const QString &packageRoot)
{
return Package::installPackage(package, packageRoot, d->servicePrefix);
}
bool PackageStructure::uninstallPackage(const QString &packageName, const QString &packageRoot)
{
return Package::uninstallPackage(packageName, packageRoot, d->servicePrefix);
}
QString PackageStructure::defaultPackageRoot() const
{
return d->packageRoot;
@ -597,55 +587,8 @@ PackageMetadata PackageStructure::metadata()
{
if (!d->metadata && !d->path.isEmpty()) {
QFileInfo fileInfo(d->path);
if (fileInfo.isDir()) {
d->createPackageMetadata(d->path);
} else if (fileInfo.exists()) {
KArchive *archive = 0;
KMimeType::Ptr mimetype = KMimeType::findByPath(d->path);
if (mimetype->is("application/zip")) {
archive = new KZip(d->path);
} else if (mimetype->is("application/x-compressed-tar") || mimetype->is("application/x-gzip") ||
mimetype->is("application/x-xz-compressed-tar") || mimetype->is("application/x-lzma-compressed-tar") ||
mimetype->is("application/x-tar")|| mimetype->is("application/x-bzip-compressed-tar")) {
archive = new KTar(d->path);
} else {
kWarning() << "Could not open package file, unsupported archive format:" << d->path << mimetype->name();
}
if (archive && archive->open(QIODevice::ReadOnly)) {
const KArchiveDirectory *source = archive->directory();
KTempDir tempdir;
source->copyTo(tempdir.name());
// This is to help with the theme packages, which include an extra folder in their package archive.
// Question: Would it be better to search just the first level dirs for metadata.desktop file?
// As in /path/Theme Name/Dir1/metadata.desktop and not /path/Theme Name/Dir1/Dir2/metadata.desktop
// This fixes bug https://bugs.kde.org/show_bug.cgi?id=149479 properly.
// 2nd rev. Search an extra first directory only.
QDir dir(tempdir.name());
QString filename = "metadata.desktop";
QFileInfo metadataFileInfo(dir, filename);
if (metadataFileInfo.exists()) {
d->createPackageMetadata(metadataFileInfo.absolutePath());
} else {
dir.setFilter(QDir::NoDotAndDotDot|QDir::Dirs);
dir.setSorting(QDir::DirsFirst);
QFileInfo firstDir(dir.entryInfoList().first());
metadataFileInfo = QFileInfo(firstDir.filePath(), filename);
if (metadataFileInfo.exists()) {
kWarning() << "Found in: " << metadataFileInfo.absolutePath();
d->createPackageMetadata(metadataFileInfo.absolutePath());
}
}
} else {
kWarning() << "Could not open package file:" << d->path;
}
delete archive;
}
}

View file

@ -250,26 +250,6 @@ public:
*/
void write(KConfigBase *config) const;
/**
* Installs a package matching this package structure. By default installs a
* native Plasma::Package.
*
* @param archivePath path to the package archive file
* @param packageRoot path to the directory where the package should be
* installed to
* @return true on successful installation, false otherwise
**/
virtual bool installPackage(const QString &archivePath, const QString &packageRoot);
/**
* Uninstalls a package matching this package structure.
*
* @param packageName the name of the package to remove
* @param packageRoot path to the directory where the package should be installed to
* @return true on successful removal of the package, false otherwise
*/
virtual bool uninstallPackage(const QString &packageName, const QString &packageRoot);
/**
* @return the prefix inserted between the base path and content entries
* @deprecated use contentsPrefixPaths() instead.

View file

@ -36,34 +36,19 @@
namespace Plasma
{
PlasmoidServiceJob::PlasmoidServiceJob(const QString &plasmoidLocation,
const QString &destination,
PlasmoidServiceJob::PlasmoidServiceJob(const QString &destination,
const QString &operation,
QMap<QString,QVariant>& parameters,
PlasmoidService *service)
: Plasma::ServiceJob(destination, operation, parameters,
static_cast<Plasma::Service*>(service)),
m_service(service),
m_packagePath(plasmoidLocation)
m_service(service)
{
}
void PlasmoidServiceJob::start()
{
if (operationName() == "GetPackage") {
kDebug() << "sending " << m_service->m_packagePath;
QFileInfo fileInfo(m_service->m_packagePath);
if (fileInfo.exists() && fileInfo.isAbsolute()) {
kDebug() << "file exists, let's try and read it";
QFile file(m_service->m_packagePath);
file.open(QIODevice::ReadOnly);
setResult(file.readAll());
} else {
kDebug() << "file doesn't exists, we're sending the plugin name";
setResult(m_packagePath);
}
} else if (operationName() == "GetMetaData") {
if (operationName() == "GetMetaData") {
KTemporaryFile tempFile;
m_service->m_metadata.write(tempFile.fileName());
QFile file(tempFile.fileName());
@ -90,25 +75,6 @@ PlasmoidService::PlasmoidService(const QString &packageLocation)
if (!m_metadata.isValid()) {
kDebug() << "not a valid package";
}
if (!m_tempFile.open()) {
kDebug() << "could not create tempfile";
}
QString packagePath = m_tempFile.fileName();
m_tempFile.close();
// put everything into a zip archive
KZip creation(packagePath);
creation.setCompression(KZip::NoCompression);
if (!creation.open(QIODevice::WriteOnly)) {
kDebug() << "could not open archive";
}
creation.addLocalFile(location + "metadata.desktop", "metadata.desktop");
location.append("contents/");
creation.addLocalDirectory(location, "contents");
creation.close();
m_packagePath = packagePath;
}
PlasmoidService::PlasmoidService(Applet *applet)
@ -116,7 +82,6 @@ PlasmoidService::PlasmoidService(Applet *applet)
setName("plasmoidservice");
if (!applet->package() || !applet->package()->isValid()) {
kDebug() << "not a valid package";
m_packagePath = applet->pluginName();
}
}
@ -128,7 +93,7 @@ PackageMetadata PlasmoidService::metadata() const
Plasma::ServiceJob* PlasmoidService::createJob(const QString& operation,
QMap<QString,QVariant>& parameters)
{
return new PlasmoidServiceJob(m_packagePath, destination(), operation, parameters, this);
return new PlasmoidServiceJob(destination(), operation, parameters, this);
}
}

View file

@ -26,8 +26,6 @@
#include "../service.h"
#include "../servicejob.h"
#include <ktemporaryfile.h>
namespace Plasma
{
@ -41,8 +39,7 @@ class PlasmoidServiceJob : public ServiceJob
Q_OBJECT
public:
PlasmoidServiceJob(const QString &plasmoidLocation,
const QString &destination,
PlasmoidServiceJob(const QString &destination,
const QString &operation,
QMap<QString,QVariant>& parameters,
PlasmoidService *parent = 0);
@ -51,7 +48,6 @@ class PlasmoidServiceJob : public ServiceJob
private:
PlasmoidService *m_service;
QString m_packagePath;
QString m_pluginName;
};
@ -72,7 +68,6 @@ class PlasmoidService : public Service, DataEngineConsumer
private:
QString m_packagePath;
PackageMetadata m_metadata;
KTemporaryFile m_tempFile;
friend class PlasmoidServiceJob;
};

View file

@ -292,39 +292,4 @@ void PlasmoidPackageTest::metadata()
QCOMPARE(metadata.name(), plasmoid);
}
void PlasmoidPackageTest::createAndInstallPackage()
{
QString plasmoid("plasmoid_to_package");
createTestPackage(plasmoid);
QString packagePath = mPackageRoot + '/' + "package.zip";
Plasma::PackageMetadata metadata(
QString(KDESRCDIR) + "/packagemetadatatest.desktop");
QVERIFY(Plasma::Package::createPackage(metadata,
mPackageRoot + '/' + plasmoid + "/contents",
packagePath));
QVERIFY(QFile::exists(packagePath));
KZip package(packagePath);
QVERIFY(package.open(QIODevice::ReadOnly));
const KArchiveDirectory *dir = package.directory();
QVERIFY(dir);
QVERIFY(dir->entry("metadata.desktop"));
const KArchiveEntry *contentsEntry = dir->entry("contents");
QVERIFY(contentsEntry);
QVERIFY(contentsEntry->isDirectory());
const KArchiveDirectory *contents =
static_cast<const KArchiveDirectory *>(contentsEntry);
QVERIFY(contents->entry("code"));
QVERIFY(contents->entry("images"));
QVERIFY(Plasma::Package::installPackage(packagePath, mPackageRoot, "plasma-applet-"));
QString installedPackage = mPackageRoot + "/test";
QVERIFY(QDir(installedPackage).exists());
p = new Plasma::Package(installedPackage, ps);
QVERIFY(p->isValid());
}
QTEST_KDEMAIN(PlasmoidPackageTest, NoGUI)

View file

@ -37,7 +37,6 @@ private Q_SLOTS:
void entryList();
void knownPackages();
void metadata();
void createAndInstallPackage();
private:
void removeDir(const QString &subdir);