2008-11-04 00:08:39 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright 2007 by Aaron Seigo <aseigo@kde.org> *
|
|
|
|
* *
|
|
|
|
* This library is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU Library General Public *
|
|
|
|
* License as published by the Free Software Foundation; either *
|
|
|
|
* version 2 of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This library is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
|
|
|
|
* Library General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Library General Public License *
|
|
|
|
* along with this library; see the file COPYING.LIB. If not, write to *
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
|
|
|
|
* Boston, MA 02110-1301, USA. *
|
|
|
|
*******************************************************************************/
|
|
|
|
|
|
|
|
#include "packagestructure.h"
|
|
|
|
|
2010-10-14 14:27:15 +02:00
|
|
|
#include "config-plasma.h"
|
|
|
|
|
2009-04-07 07:42:30 +02:00
|
|
|
#include <QDir>
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QMap>
|
|
|
|
#include <QFileInfo>
|
|
|
|
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kconfiggroup.h>
|
2010-11-10 16:25:56 +01:00
|
|
|
#include <kdebug.h>
|
2011-05-20 10:43:40 +02:00
|
|
|
#include <kdesktopfile.h>
|
2010-10-14 14:27:15 +02:00
|
|
|
#ifndef PLASMA_NO_KIO
|
2011-05-20 10:43:40 +02:00
|
|
|
#include <kio/copyjob.h>
|
|
|
|
#include <kio/deletejob.h>
|
|
|
|
#include <kio/jobclasses.h>
|
2010-10-01 23:33:16 +02:00
|
|
|
#include <kio/job.h>
|
2010-10-14 14:27:15 +02:00
|
|
|
#endif
|
2010-10-01 23:33:16 +02:00
|
|
|
#include <kmimetype.h>
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kstandarddirs.h>
|
|
|
|
#include <kservicetypetrader.h>
|
2010-10-01 23:33:16 +02:00
|
|
|
#include <ktar.h>
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <ktemporaryfile.h>
|
|
|
|
#include <ktempdir.h>
|
2010-10-01 23:33:16 +02:00
|
|
|
#include <kurl.h>
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kzip.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
#include "package.h"
|
2009-02-07 19:48:11 +01:00
|
|
|
#include "private/packages_p.h"
|
|
|
|
#include "theme.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class ContentStructure
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ContentStructure()
|
|
|
|
: directory(false),
|
|
|
|
required(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ContentStructure(const ContentStructure &other)
|
|
|
|
{
|
2010-11-03 23:11:28 +01:00
|
|
|
paths = other.paths;
|
2008-11-04 00:08:39 +01:00
|
|
|
name = other.name;
|
2011-05-06 12:20:00 +02:00
|
|
|
mimeTypes = other.mimeTypes;
|
2008-11-04 00:08:39 +01:00
|
|
|
directory = other.directory;
|
|
|
|
required = other.required;
|
|
|
|
}
|
|
|
|
|
2010-11-03 23:11:28 +01:00
|
|
|
QStringList paths;
|
2008-11-04 00:08:39 +01:00
|
|
|
QString name;
|
2011-05-06 12:20:00 +02:00
|
|
|
QStringList mimeTypes;
|
2009-01-16 02:02:35 +01:00
|
|
|
bool directory : 1;
|
|
|
|
bool required : 1;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class PackageStructurePrivate
|
|
|
|
{
|
|
|
|
public:
|
2010-04-09 01:47:21 +02:00
|
|
|
PackageStructurePrivate(const QString &t)
|
|
|
|
: type(t),
|
|
|
|
packageRoot("plasma/plasmoids"),
|
|
|
|
servicePrefix("plasma-applet-"),
|
|
|
|
metadata(0),
|
2009-01-16 02:02:35 +01:00
|
|
|
externalPaths(false)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2010-11-04 20:18:32 +01:00
|
|
|
contentsPrefixPaths << "contents/";
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
2009-01-16 02:02:35 +01:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
~PackageStructurePrivate()
|
|
|
|
{
|
|
|
|
delete metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
void createPackageMetadata(const QString &path);
|
2010-12-15 07:28:33 +01:00
|
|
|
QStringList entryList(const QString &prefix, const QString &requestedPath);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2009-01-16 02:02:35 +01:00
|
|
|
static QHash<QString, PackageStructure::Ptr> structures;
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
QString type;
|
|
|
|
QString path;
|
2010-11-04 20:18:32 +01:00
|
|
|
QStringList contentsPrefixPaths;
|
2008-11-04 00:08:39 +01:00
|
|
|
QString packageRoot;
|
|
|
|
QString servicePrefix;
|
|
|
|
QMap<QByteArray, ContentStructure> contents;
|
2011-05-06 12:20:00 +02:00
|
|
|
QStringList mimeTypes;
|
2008-11-04 00:08:39 +01:00
|
|
|
PackageMetadata *metadata;
|
2009-01-16 02:02:35 +01:00
|
|
|
bool externalPaths;
|
2008-11-04 00:08:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
QHash<QString, PackageStructure::Ptr> PackageStructurePrivate::structures;
|
|
|
|
|
|
|
|
PackageStructure::PackageStructure(QObject *parent, const QString &type)
|
|
|
|
: QObject(parent),
|
2010-04-09 01:47:21 +02:00
|
|
|
d(new PackageStructurePrivate(type))
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
PackageStructure::~PackageStructure()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
PackageStructure::Ptr PackageStructure::load(const QString &packageFormat)
|
|
|
|
{
|
|
|
|
if (packageFormat.isEmpty()) {
|
|
|
|
return Ptr(new PackageStructure());
|
|
|
|
}
|
|
|
|
|
|
|
|
PackageStructure::Ptr structure = PackageStructurePrivate::structures[packageFormat];
|
|
|
|
|
|
|
|
if (structure) {
|
|
|
|
return structure;
|
|
|
|
}
|
|
|
|
|
2009-02-08 17:53:54 +01:00
|
|
|
if (packageFormat == "Plasma/Applet") {
|
2009-02-07 19:48:11 +01:00
|
|
|
structure = defaultPackageStructure(AppletComponent);
|
2009-02-09 19:02:03 +01:00
|
|
|
structure->d->type = "Plasma/Applet";
|
2009-02-08 17:53:54 +01:00
|
|
|
} else if (packageFormat == "Plasma/DataEngine") {
|
2009-02-07 19:48:11 +01:00
|
|
|
structure = defaultPackageStructure(DataEngineComponent);
|
2009-02-09 19:02:03 +01:00
|
|
|
structure->d->type = "Plasma/DataEngine";
|
2009-02-08 17:53:54 +01:00
|
|
|
} else if (packageFormat == "Plasma/Runner") {
|
2009-02-07 19:48:11 +01:00
|
|
|
structure = defaultPackageStructure(RunnerComponent);
|
2009-02-09 19:02:03 +01:00
|
|
|
structure->d->type = "Plasma/Runner";
|
2009-10-29 06:54:11 +01:00
|
|
|
} else if (packageFormat == "Plasma/Wallpaper") {
|
|
|
|
structure = defaultPackageStructure(WallpaperComponent);
|
|
|
|
structure->d->type = "Plasma/Wallpaper";
|
2009-02-08 17:53:54 +01:00
|
|
|
} else if (packageFormat == "Plasma/Theme") {
|
2009-02-07 19:48:11 +01:00
|
|
|
structure = Theme::packageStructure();
|
2009-02-09 19:02:03 +01:00
|
|
|
structure->d->type = "Plasma/Theme";
|
2009-02-07 19:48:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (structure) {
|
|
|
|
PackageStructurePrivate::structures[packageFormat] = structure;
|
|
|
|
return structure;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
// first we check for plugins in sycoca
|
|
|
|
QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg(packageFormat);
|
|
|
|
KService::List offers =
|
|
|
|
KServiceTypeTrader::self()->query("Plasma/PackageStructure", constraint);
|
|
|
|
|
|
|
|
QVariantList args;
|
|
|
|
QString error;
|
|
|
|
foreach (const KService::Ptr &offer, offers) {
|
|
|
|
PackageStructure::Ptr structure(
|
|
|
|
offer->createInstance<Plasma::PackageStructure>(0, args, &error));
|
|
|
|
|
|
|
|
if (structure) {
|
|
|
|
return structure;
|
|
|
|
}
|
|
|
|
|
|
|
|
kDebug() << "Couldn't load PackageStructure for" << packageFormat
|
|
|
|
<< "! reason given: " << error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if that didn't give us any love, then we try to load from a config file
|
|
|
|
structure = new PackageStructure();
|
|
|
|
QString configPath("plasma/packageformats/%1rc");
|
|
|
|
configPath = KStandardDirs::locate("data", configPath.arg(packageFormat));
|
|
|
|
|
|
|
|
if (!configPath.isEmpty()) {
|
|
|
|
KConfig config(configPath);
|
|
|
|
structure->read(&config);
|
|
|
|
PackageStructurePrivate::structures[packageFormat] = structure;
|
|
|
|
return structure;
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to load from absolute file path
|
|
|
|
KUrl url(packageFormat);
|
|
|
|
if (url.isLocalFile()) {
|
2010-05-10 16:39:54 +02:00
|
|
|
KConfig config(url.toLocalFile(), KConfig::SimpleConfig);
|
2008-11-04 00:08:39 +01:00
|
|
|
structure->read(&config);
|
|
|
|
PackageStructurePrivate::structures[structure->type()] = structure;
|
2010-10-14 14:27:15 +02:00
|
|
|
}
|
|
|
|
#ifndef PLASMA_NO_KIO
|
|
|
|
else {
|
2008-11-04 00:08:39 +01:00
|
|
|
KTemporaryFile tmp;
|
|
|
|
if (tmp.open()) {
|
|
|
|
KIO::Job *job = KIO::file_copy(url, KUrl(tmp.fileName()),
|
|
|
|
-1, KIO::Overwrite | KIO::HideProgressInfo);
|
|
|
|
if (job->exec()) {
|
|
|
|
KConfig config(tmp.fileName(), KConfig::SimpleConfig);
|
|
|
|
structure->read(&config);
|
|
|
|
PackageStructurePrivate::structures[structure->type()] = structure;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-14 14:27:15 +02:00
|
|
|
#endif
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
return structure;
|
|
|
|
}
|
|
|
|
|
|
|
|
PackageStructure &PackageStructure::operator=(const PackageStructure &rhs)
|
|
|
|
{
|
|
|
|
if (this == &rhs) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
*d = *rhs.d;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PackageStructure::type() const
|
|
|
|
{
|
|
|
|
return d->type;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<const char*> PackageStructure::directories() const
|
|
|
|
{
|
|
|
|
QList<const char*> dirs;
|
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
|
|
|
|
while (it != d->contents.constEnd()) {
|
|
|
|
if (it.value().directory) {
|
|
|
|
dirs << it.key();
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return dirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<const char*> PackageStructure::requiredDirectories() const
|
|
|
|
{
|
|
|
|
QList<const char*> dirs;
|
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
|
|
|
|
while (it != d->contents.constEnd()) {
|
|
|
|
if (it.value().directory &&
|
|
|
|
it.value().required) {
|
|
|
|
dirs << it.key();
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return dirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<const char*> PackageStructure::files() const
|
|
|
|
{
|
|
|
|
QList<const char*> files;
|
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
|
|
|
|
while (it != d->contents.constEnd()) {
|
|
|
|
if (!it.value().directory) {
|
|
|
|
files << it.key();
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<const char*> PackageStructure::requiredFiles() const
|
|
|
|
{
|
|
|
|
QList<const char*> files;
|
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
|
|
|
|
while (it != d->contents.constEnd()) {
|
|
|
|
if (!it.value().directory && it.value().required) {
|
|
|
|
files << it.key();
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2009-04-07 07:42:30 +02:00
|
|
|
QStringList PackageStructure::entryList(const char *key)
|
|
|
|
{
|
|
|
|
QString p = path(key);
|
|
|
|
|
|
|
|
if (p.isEmpty()) {
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
2010-11-04 20:18:32 +01:00
|
|
|
QStringList list;
|
2010-12-15 07:28:33 +01:00
|
|
|
if (d->contentsPrefixPaths.isEmpty()) {
|
|
|
|
// no prefixes is the same as d->contentsPrefixPths with QStringList() << QString()
|
|
|
|
list << d->entryList(QString(), p);
|
|
|
|
} else {
|
|
|
|
foreach (QString prefix, d->contentsPrefixPaths) {
|
|
|
|
list << d->entryList(prefix, p);
|
2009-04-07 07:42:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-04 20:18:32 +01:00
|
|
|
return list;
|
2009-04-07 07:42:30 +02:00
|
|
|
}
|
|
|
|
|
2010-12-15 07:28:33 +01:00
|
|
|
QStringList PackageStructurePrivate::entryList(const QString &prefix, const QString &requestedPath)
|
|
|
|
{
|
|
|
|
QDir dir(path + prefix + requestedPath);
|
|
|
|
|
|
|
|
if (externalPaths) {
|
|
|
|
return dir.entryList(QDir::Files | QDir::Readable);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that we don't return files outside of our base path
|
|
|
|
// due to symlink or ../ games
|
|
|
|
QString canonicalized = dir.canonicalPath();
|
|
|
|
if (canonicalized.startsWith(path)) {
|
|
|
|
return dir.entryList(QDir::Files | QDir::Readable);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
void PackageStructure::addDirectoryDefinition(const char *key,
|
|
|
|
const QString &path, const QString &name)
|
|
|
|
{
|
|
|
|
ContentStructure s;
|
2010-11-03 23:11:28 +01:00
|
|
|
|
|
|
|
if (d->contents.contains(key)) {
|
|
|
|
s = d->contents[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
s.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
s.paths.append(path);
|
2008-11-04 00:08:39 +01:00
|
|
|
s.directory = true;
|
|
|
|
|
|
|
|
d->contents[key] = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::addFileDefinition(const char *key, const QString &path, const QString &name)
|
|
|
|
{
|
|
|
|
ContentStructure s;
|
2010-11-03 23:11:28 +01:00
|
|
|
|
|
|
|
if (d->contents.contains(key)) {
|
|
|
|
s = d->contents[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
s.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
s.paths.append(path);
|
2008-11-04 00:08:39 +01:00
|
|
|
s.directory = false;
|
|
|
|
|
|
|
|
d->contents[key] = s;
|
|
|
|
}
|
|
|
|
|
2010-11-03 23:11:28 +01:00
|
|
|
void PackageStructure::removeDefinition(const char *key)
|
|
|
|
{
|
|
|
|
d->contents.remove(key);
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
QString PackageStructure::path(const char *key) const
|
|
|
|
{
|
|
|
|
//kDebug() << "looking for" << key;
|
2009-01-06 14:09:05 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
|
2008-11-04 00:08:39 +01:00
|
|
|
if (it == d->contents.constEnd()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2010-11-03 23:11:28 +01:00
|
|
|
//kDebug() << "found" << key << "and the value is" << it.value().paths.first();
|
|
|
|
return it.value().paths.first();
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList PackageStructure::searchPath(const char *key) const
|
|
|
|
{
|
|
|
|
//kDebug() << "looking for" << key;
|
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
|
|
|
|
if (it == d->contents.constEnd()) {
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
|
|
|
//kDebug() << "found" << key << "and the value is" << it.value().paths;
|
|
|
|
return it.value().paths;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString PackageStructure::name(const char *key) const
|
|
|
|
{
|
2009-01-06 14:09:05 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
|
2008-11-04 00:08:39 +01:00
|
|
|
if (it == d->contents.constEnd()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return it.value().name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::setRequired(const char *key, bool required)
|
|
|
|
{
|
|
|
|
QMap<QByteArray, ContentStructure>::iterator it = d->contents.find(key);
|
|
|
|
if (it == d->contents.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
it.value().required = required;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PackageStructure::isRequired(const char *key) const
|
|
|
|
{
|
2009-01-06 14:09:05 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
|
2008-11-04 00:08:39 +01:00
|
|
|
if (it == d->contents.constEnd()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return it.value().required;
|
|
|
|
}
|
|
|
|
|
2011-05-06 12:31:03 +02:00
|
|
|
void PackageStructure::setDefaultMimeTypes(QStringList mimeTypes)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2011-05-06 12:20:00 +02:00
|
|
|
d->mimeTypes = mimeTypes;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2011-05-06 12:31:03 +02:00
|
|
|
void PackageStructure::setMimeTypes(const char *key, QStringList mimeTypes)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
QMap<QByteArray, ContentStructure>::iterator it = d->contents.find(key);
|
|
|
|
if (it == d->contents.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-05-06 12:20:00 +02:00
|
|
|
it.value().mimeTypes = mimeTypes;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2011-05-06 12:20:00 +02:00
|
|
|
QStringList PackageStructure::mimeTypes(const char *key) const
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2009-01-06 14:09:05 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
|
2008-11-04 00:08:39 +01:00
|
|
|
if (it == d->contents.constEnd()) {
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
2011-05-06 12:20:00 +02:00
|
|
|
if (it.value().mimeTypes.isEmpty()) {
|
|
|
|
return d->mimeTypes;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2011-05-06 12:20:00 +02:00
|
|
|
return it.value().mimeTypes;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::setPath(const QString &path)
|
|
|
|
{
|
2009-09-11 00:53:41 +02:00
|
|
|
KUrl url(path);
|
2009-10-12 17:47:38 +02:00
|
|
|
QDir dir(url.toLocalFile());
|
2009-04-07 07:42:30 +02:00
|
|
|
QString basePath = dir.canonicalPath();
|
|
|
|
bool valid = QFile::exists(basePath);
|
|
|
|
|
|
|
|
if (valid) {
|
|
|
|
QFileInfo info(basePath);
|
2010-12-15 08:11:46 +01:00
|
|
|
if (info.isDir() && !basePath.endsWith('/')) {
|
|
|
|
basePath.append('/');
|
2009-04-07 07:42:30 +02:00
|
|
|
}
|
2009-04-10 22:05:37 +02:00
|
|
|
//kDebug() << "basePath is" << basePath;
|
2009-04-07 07:42:30 +02:00
|
|
|
} else {
|
|
|
|
kDebug() << path << "invalid, basePath is" << basePath;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->path == basePath) {
|
2009-01-13 20:50:01 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-07 07:42:30 +02:00
|
|
|
d->path = basePath;
|
2008-11-07 18:26:28 +01:00
|
|
|
delete d->metadata;
|
|
|
|
d->metadata = 0;
|
2008-11-04 00:08:39 +01:00
|
|
|
pathChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PackageStructure::path() const
|
|
|
|
{
|
|
|
|
return d->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::pathChanged()
|
|
|
|
{
|
|
|
|
// default impl does nothing, this is a hook for subclasses.
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::read(const KConfigBase *config)
|
|
|
|
{
|
|
|
|
d->contents.clear();
|
2011-05-06 12:20:00 +02:00
|
|
|
d->mimeTypes.clear();
|
2010-10-14 14:38:48 +02:00
|
|
|
KConfigGroup general(config, QString());
|
2010-04-09 01:13:45 +02:00
|
|
|
d->type = general.readEntry("Type", QString());
|
2010-11-04 20:18:32 +01:00
|
|
|
d->contentsPrefixPaths = general.readEntry("ContentsPrefixPaths", d->contentsPrefixPaths);
|
2010-04-09 01:13:45 +02:00
|
|
|
d->packageRoot = general.readEntry("DefaultPackageRoot", d->packageRoot);
|
|
|
|
d->externalPaths = general.readEntry("AllowExternalPaths", d->externalPaths);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QStringList groups = config->groupList();
|
|
|
|
foreach (const QString &group, groups) {
|
2010-10-14 14:38:48 +02:00
|
|
|
KConfigGroup entry(config, group);
|
2008-11-04 00:08:39 +01:00
|
|
|
QByteArray key = group.toAscii();
|
|
|
|
|
|
|
|
QString path = entry.readEntry("Path", QString());
|
|
|
|
QString name = entry.readEntry("Name", QString());
|
2011-05-06 12:20:00 +02:00
|
|
|
QStringList mimeTypes = entry.readEntry("Mimetypes", QStringList());
|
2008-11-04 00:08:39 +01:00
|
|
|
bool directory = entry.readEntry("Directory", false);
|
|
|
|
bool required = entry.readEntry("Required", false);
|
|
|
|
|
|
|
|
if (directory) {
|
|
|
|
addDirectoryDefinition(key, path, name);
|
|
|
|
} else {
|
|
|
|
addFileDefinition(key, path, name);
|
|
|
|
}
|
|
|
|
|
2011-05-06 12:31:03 +02:00
|
|
|
setMimeTypes(key, mimeTypes);
|
2008-11-04 00:08:39 +01:00
|
|
|
setRequired(key, required);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::write(KConfigBase *config) const
|
|
|
|
{
|
2010-04-09 01:13:45 +02:00
|
|
|
KConfigGroup general = KConfigGroup(config, "");
|
|
|
|
general.writeEntry("Type", type());
|
2010-11-04 20:18:32 +01:00
|
|
|
general.writeEntry("ContentsPrefixPaths", d->contentsPrefixPaths);
|
2010-04-09 01:13:45 +02:00
|
|
|
general.writeEntry("DefaultPackageRoot", d->packageRoot);
|
|
|
|
general.writeEntry("AllowExternalPaths", d->externalPaths);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
|
|
|
|
while (it != d->contents.constEnd()) {
|
|
|
|
KConfigGroup group = config->group(it.key());
|
2010-11-03 23:11:28 +01:00
|
|
|
group.writeEntry("Path", it.value().paths);
|
2008-11-04 00:08:39 +01:00
|
|
|
group.writeEntry("Name", it.value().name);
|
2011-05-06 12:20:00 +02:00
|
|
|
if (!it.value().mimeTypes.isEmpty()) {
|
2011-05-06 14:17:49 +02:00
|
|
|
group.writeEntry("Mimetypes", it.value().mimeTypes);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
if (it.value().directory) {
|
|
|
|
group.writeEntry("Directory", true);
|
|
|
|
}
|
|
|
|
if (it.value().required) {
|
|
|
|
group.writeEntry("Required", true);
|
|
|
|
}
|
|
|
|
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PackageStructure::contentsPrefix() const
|
|
|
|
{
|
2010-12-15 07:28:33 +01:00
|
|
|
return d->contentsPrefixPaths.isEmpty() ? QString() : d->contentsPrefixPaths.first();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::setContentsPrefix(const QString &prefix)
|
|
|
|
{
|
2010-11-04 20:18:32 +01:00
|
|
|
d->contentsPrefixPaths.clear();
|
|
|
|
d->contentsPrefixPaths << prefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList PackageStructure::contentsPrefixPaths() const
|
|
|
|
{
|
|
|
|
return d->contentsPrefixPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::setContentsPrefixPaths(const QStringList &prefixPaths)
|
|
|
|
{
|
|
|
|
d->contentsPrefixPaths = prefixPaths;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PackageStructure::installPackage(const QString &package, const QString &packageRoot)
|
|
|
|
{
|
2011-05-20 10:43:40 +02:00
|
|
|
//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")) {
|
|
|
|
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.
|
|
|
|
#ifndef PLASMA_NO_KIO
|
|
|
|
KIO::CopyJob *job = KIO::move(KUrl(path), KUrl(targetName), KIO::HideProgressInfo);
|
|
|
|
const bool ok = job->exec();
|
|
|
|
const QString errorString = job->errorString();
|
|
|
|
#else
|
|
|
|
const bool ok = copyFolder(path, targetName);
|
|
|
|
removeFolder(path);
|
|
|
|
const QString errorString("unknown");
|
|
|
|
#endif
|
|
|
|
if (!ok) {
|
|
|
|
kWarning() << "Could not move package to destination:" << targetName << " : " << errorString;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// it's a directory containing the stuff, so copy the contents rather
|
|
|
|
// than move them
|
|
|
|
#ifndef PLASMA_NO_KIO
|
|
|
|
KIO::CopyJob *job = KIO::copy(KUrl(path), KUrl(targetName), KIO::HideProgressInfo);
|
|
|
|
const bool ok = job->exec();
|
|
|
|
const QString errorString = job->errorString();
|
|
|
|
#else
|
|
|
|
const bool ok = copyFolder(path, targetName);
|
|
|
|
const QString errorString("unknown");
|
|
|
|
#endif
|
|
|
|
if (!ok) {
|
|
|
|
kWarning() << "Could not copy package to destination:" << targetName << " : " << errorString;
|
|
|
|
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 (!d->servicePrefix.isEmpty()) {
|
|
|
|
// and now we register it as a service =)
|
|
|
|
QString metaPath = targetName + "/metadata.desktop";
|
|
|
|
KDesktopFile df(metaPath);
|
|
|
|
KConfigGroup cg = df.desktopGroup();
|
|
|
|
|
|
|
|
// 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 = d->servicePrefix + meta.pluginName();
|
|
|
|
|
|
|
|
QString service = KStandardDirs::locateLocal("services", serviceName + ".desktop");
|
|
|
|
#ifndef PLASMA_NO_KIO
|
|
|
|
KIO::FileCopyJob *job = KIO::file_copy(metaPath, service, -1, KIO::HideProgressInfo);
|
|
|
|
const bool ok = job->exec();
|
|
|
|
const QString errorString = job->errorString();
|
|
|
|
#else
|
|
|
|
const bool ok = QFile::copy(metaPath, service);
|
|
|
|
const QString errorString("unknown");
|
|
|
|
#endif
|
|
|
|
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 << " : " << errorString;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PackageStructure::uninstallPackage(const QString &packageName, const QString &packageRoot)
|
|
|
|
{
|
2011-05-20 10:43:40 +02:00
|
|
|
// We need to remove the package directory and its metadata file.
|
|
|
|
const QString targetName = packageRoot + '/' + packageName;
|
|
|
|
|
|
|
|
if (!QFile::exists(targetName)) {
|
|
|
|
kWarning() << targetName << "does not exist";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString serviceName = d->servicePrefix + packageName;
|
|
|
|
|
|
|
|
QString service = KStandardDirs::locateLocal("services", serviceName + ".desktop");
|
|
|
|
kDebug() << "Removing service file " << service;
|
|
|
|
bool ok = QFile::remove(service);
|
|
|
|
|
|
|
|
if (!ok) {
|
|
|
|
kWarning() << "Unable to remove " << service;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef PLASMA_NO_KIO
|
|
|
|
KIO::DeleteJob *job = KIO::del(KUrl(targetName));
|
|
|
|
ok = job->exec();
|
|
|
|
const QString errorString = job->errorString();
|
|
|
|
#else
|
|
|
|
ok = removeFolder(targetName);
|
|
|
|
const QString errorString("unknown");
|
|
|
|
#endif
|
|
|
|
if (!ok) {
|
|
|
|
kWarning() << "Could not delete package from:" << targetName << " : " << errorString;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::createNewWidgetBrowser(QWidget *parent)
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent)
|
|
|
|
emit newWidgetBrowserFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PackageStructure::defaultPackageRoot() const
|
|
|
|
{
|
|
|
|
return d->packageRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PackageStructure::servicePrefix() const
|
|
|
|
{
|
|
|
|
return d->servicePrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::setDefaultPackageRoot(const QString &packageRoot)
|
|
|
|
{
|
|
|
|
d->packageRoot = packageRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::setServicePrefix(const QString &servicePrefix)
|
|
|
|
{
|
|
|
|
d->servicePrefix = servicePrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructurePrivate::createPackageMetadata(const QString &path)
|
|
|
|
{
|
2010-08-25 16:48:41 +02:00
|
|
|
delete metadata;
|
|
|
|
metadata = 0;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
QString metadataPath(path + "/metadata.desktop");
|
|
|
|
if (!QFile::exists(metadataPath)) {
|
|
|
|
kWarning() << "No metadata file in the package, expected it at:" << metadataPath;
|
2010-12-22 21:55:59 +01:00
|
|
|
metadataPath.clear();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
metadata = new PackageMetadata(metadataPath);
|
|
|
|
}
|
|
|
|
|
2011-05-06 12:21:58 +02:00
|
|
|
PackageMetadata PackageStructure::metadata() const
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
if (!d->metadata && !d->path.isEmpty()) {
|
|
|
|
QFileInfo fileInfo(d->path);
|
|
|
|
|
|
|
|
if (fileInfo.isDir()) {
|
|
|
|
d->createPackageMetadata(d->path);
|
|
|
|
} else if (fileInfo.exists()) {
|
2010-10-01 23:33:16 +02:00
|
|
|
KArchive *archive = 0;
|
2011-05-06 12:20:00 +02:00
|
|
|
KMimeType::Ptr mimeType = KMimeType::findByPath(d->path);
|
2010-10-01 23:33:16 +02:00
|
|
|
|
2011-05-06 12:20:00 +02:00
|
|
|
if (mimeType->is("application/zip")) {
|
2010-10-01 23:33:16 +02:00
|
|
|
archive = new KZip(d->path);
|
2011-05-06 12:20:00 +02:00
|
|
|
} else if (mimeType->is("application/x-compressed-tar") ||
|
|
|
|
mimeType->is("application/x-tar")|| mimeType->is("application/x-bzip-compressed-tar")) {
|
2010-10-01 23:33:16 +02:00
|
|
|
archive = new KTar(d->path);
|
|
|
|
} else {
|
2011-05-06 12:20:00 +02:00
|
|
|
kWarning() << "Could not open package file, unsupported archive format:" << d->path << mimeType->name();
|
2010-10-01 23:33:16 +02:00
|
|
|
}
|
|
|
|
|
2010-12-22 21:59:33 +01:00
|
|
|
if (archive && archive->open(QIODevice::ReadOnly)) {
|
2010-10-01 23:33:16 +02:00
|
|
|
const KArchiveDirectory *source = archive->directory();
|
2008-11-04 00:08:39 +01:00
|
|
|
KTempDir tempdir;
|
|
|
|
source->copyTo(tempdir.name());
|
|
|
|
d->createPackageMetadata(tempdir.name());
|
|
|
|
} else {
|
|
|
|
kWarning() << "Could not open package file:" << d->path;
|
|
|
|
}
|
2010-10-01 23:33:16 +02:00
|
|
|
|
|
|
|
delete archive;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!d->metadata) {
|
|
|
|
d->metadata = new PackageMetadata();
|
|
|
|
}
|
|
|
|
|
|
|
|
return *d->metadata;
|
|
|
|
}
|
|
|
|
|
2009-01-16 02:02:35 +01:00
|
|
|
bool PackageStructure::allowExternalPaths() const
|
|
|
|
{
|
|
|
|
return d->externalPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::setAllowExternalPaths(bool allow)
|
|
|
|
{
|
|
|
|
d->externalPaths = allow;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#include "packagestructure.moc"
|
|
|
|
|