2008-11-04 00:08:39 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright 2007 by Aaron Seigo <aseigo@kde.org> *
|
2011-07-08 19:36:29 +02:00
|
|
|
* Copyright 2010 by Marco Martin <notmart@gmail.com> *
|
|
|
|
* Copyright 2010 by Kevin Ottens <ervin@kde.org> *
|
|
|
|
* Copyright 2009 by Rob Scheepmaker *
|
2008-11-04 00:08:39 +01:00
|
|
|
* *
|
|
|
|
* 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 "package.h"
|
|
|
|
|
2011-07-30 18:31:22 +02:00
|
|
|
#include <QDBusInterface>
|
|
|
|
#include <QDBusPendingCall>
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QFile>
|
2011-07-19 21:37:39 +02:00
|
|
|
#include <QIODevice>
|
2008-11-04 00:08:39 +01:00
|
|
|
#include <QRegExp>
|
2009-10-20 01:04:42 +02:00
|
|
|
#include <QtNetwork/QHostInfo>
|
2011-12-06 20:08:22 +01:00
|
|
|
#include <qtemporarydir.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2008-11-04 03:20:46 +01:00
|
|
|
#include <karchive.h>
|
2011-07-15 12:57:46 +02:00
|
|
|
#include <kdebug.h>
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kdesktopfile.h>
|
2009-07-25 11:34:19 +02:00
|
|
|
#include <kmimetype.h>
|
2011-07-15 12:57:46 +02:00
|
|
|
#include <kservicetypetrader.h>
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2011-09-02 10:50:04 +02:00
|
|
|
#include "config-plasma.h"
|
|
|
|
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <kplugininfo.h>
|
|
|
|
#include <kstandarddirs.h>
|
2009-07-25 11:34:19 +02:00
|
|
|
#include <ktar.h>
|
2008-11-04 03:04:34 +01:00
|
|
|
#include <ktempdir.h>
|
|
|
|
#include <ktemporaryfile.h>
|
|
|
|
#include <kzip.h>
|
|
|
|
#include <kdebug.h>
|
2011-07-15 12:57:46 +02:00
|
|
|
|
2011-07-19 21:37:39 +02:00
|
|
|
#include "packagestructure.h"
|
2011-07-15 13:34:10 +02:00
|
|
|
#include "pluginloader.h"
|
2009-09-02 04:27:16 +02:00
|
|
|
#include "private/package_p.h"
|
2011-07-15 12:57:46 +02:00
|
|
|
#include "private/packages_p.h"
|
2008-11-04 00:08:39 +01:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2010-10-14 15:23:56 +02:00
|
|
|
bool copyFolder(QString sourcePath, QString targetPath)
|
|
|
|
{
|
|
|
|
QDir source(sourcePath);
|
|
|
|
if(!source.exists())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QDir target(targetPath);
|
|
|
|
if(!target.exists()) {
|
|
|
|
QString targetName = target.dirName();
|
|
|
|
target.cdUp();
|
|
|
|
target.mkdir(targetName);
|
|
|
|
target = QDir(targetPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const QString &fileName, source.entryList(QDir::Files)) {
|
|
|
|
QString sourceFilePath = sourcePath + QDir::separator() + fileName;
|
|
|
|
QString targetFilePath = targetPath + QDir::separator() + fileName;
|
|
|
|
|
|
|
|
if (!QFile::copy(sourceFilePath, targetFilePath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const QString &subFolderName, source.entryList(QDir::AllDirs | QDir::NoDotAndDotDot)) {
|
|
|
|
QString sourceSubFolderPath = sourcePath + QDir::separator() + subFolderName;
|
|
|
|
QString targetSubFolderPath = targetPath + QDir::separator() + subFolderName;
|
|
|
|
|
|
|
|
if (!copyFolder(sourceSubFolderPath, targetSubFolderPath)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool removeFolder(QString folderPath)
|
|
|
|
{
|
|
|
|
QDir folder(folderPath);
|
|
|
|
if(!folder.exists())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
foreach (const QString &fileName, folder.entryList(QDir::Files)) {
|
|
|
|
if (!QFile::remove(folderPath + QDir::separator() + fileName)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const QString &subFolderName, folder.entryList(QDir::AllDirs | QDir::NoDotAndDotDot)) {
|
|
|
|
if (!removeFolder(folderPath + QDir::separator() + subFolderName)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString folderName = folder.dirName();
|
|
|
|
folder.cdUp();
|
|
|
|
return folder.rmdir(folderName);
|
|
|
|
}
|
|
|
|
|
2011-07-19 21:37:39 +02:00
|
|
|
Package::Package(PackageStructure *structure)
|
2011-07-15 12:57:46 +02:00
|
|
|
: d(new PackagePrivate())
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2011-07-19 21:37:39 +02:00
|
|
|
d->structure = structure;
|
2011-07-21 15:04:56 +02:00
|
|
|
if (d->structure) {
|
|
|
|
d->structure.data()->initPackage(this);
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2010-09-03 21:44:28 +02:00
|
|
|
Package::Package()
|
|
|
|
: d(new PackagePrivate(PackageStructure::Ptr(0), QString()))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
Package::Package(const QString &packageRoot, const QString &package,
|
|
|
|
PackageStructure::Ptr structure)
|
2012-06-16 13:11:07 +02:00
|
|
|
: d(new PackagePrivate(structure, packageRoot + '/' + package))
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Package::Package(const QString &packagePath, PackageStructure::Ptr structure)
|
|
|
|
: d(new PackagePrivate(structure, packagePath))
|
|
|
|
{
|
2012-06-16 13:11:07 +02:00
|
|
|
d->structure = structure;
|
|
|
|
if (d->structure) {
|
|
|
|
d->structure.data()->initPackage(this);
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2010-09-03 21:44:28 +02:00
|
|
|
Package::Package(const Package &other)
|
2011-07-22 17:24:40 +02:00
|
|
|
: d(other.d)
|
2010-09-03 21:44:28 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
Package::~Package()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-09-03 21:44:28 +02:00
|
|
|
Package &Package::operator=(const Package &rhs)
|
|
|
|
{
|
|
|
|
if (&rhs != this) {
|
2011-07-22 17:24:40 +02:00
|
|
|
d = rhs.d;
|
2010-09-03 21:44:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
bool Package::isValid() const
|
|
|
|
{
|
2010-12-15 07:29:36 +01:00
|
|
|
if (!d->valid) {
|
2008-11-04 00:08:39 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-04 20:18:32 +01:00
|
|
|
//search for the file in all prefixes and in all possible paths for each prefix
|
|
|
|
//even if it's a big nested loop, usually there is one prefix and one location
|
|
|
|
//so shouldn't cause too much disk access
|
2011-07-22 17:24:40 +02:00
|
|
|
QHashIterator<QByteArray, ContentStructure> it(d->contents);
|
2011-07-15 12:57:46 +02:00
|
|
|
while (it.hasNext()) {
|
|
|
|
it.next();
|
|
|
|
if (!it.value().required) {
|
|
|
|
continue;
|
|
|
|
}
|
2010-12-15 07:29:36 +01:00
|
|
|
|
2010-11-03 23:12:30 +01:00
|
|
|
bool failed = true;
|
2011-07-15 12:57:46 +02:00
|
|
|
foreach (const QString &path, it.value().paths) {
|
|
|
|
foreach (const QString &prefix, d->contentsPrefixPaths) {
|
|
|
|
if (QFile::exists(d->path + prefix + path)) {
|
2010-11-04 20:18:32 +01:00
|
|
|
failed = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!failed) {
|
2010-11-03 23:12:30 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (failed) {
|
2011-07-15 12:57:46 +02:00
|
|
|
kWarning() << "Could not find required" << (it.value().directory ? "directory" : "file") << it.key();
|
2008-11-04 00:08:39 +01:00
|
|
|
d->valid = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 12:57:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Package::name(const char *key) const
|
|
|
|
{
|
2011-07-21 14:41:01 +02:00
|
|
|
#ifndef PLASMA_NO_PACKAGE_EXTRADATA
|
2011-07-22 17:24:40 +02:00
|
|
|
QHash<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
|
2011-07-15 12:57:46 +02:00
|
|
|
if (it == d->contents.constEnd()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return it.value().name;
|
2011-07-21 14:41:01 +02:00
|
|
|
#else
|
|
|
|
Q_UNUSED(key);
|
|
|
|
return QString();
|
|
|
|
#endif
|
2011-07-15 12:57:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Package::isRequired(const char *key) const
|
|
|
|
{
|
2011-07-22 17:24:40 +02:00
|
|
|
QHash<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
|
2011-07-15 12:57:46 +02:00
|
|
|
if (it == d->contents.constEnd()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return it.value().required;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList Package::mimeTypes(const char *key) const
|
|
|
|
{
|
2011-07-21 14:41:01 +02:00
|
|
|
#ifndef PLASMA_NO_PACKAGE_EXTRADATA
|
2011-07-22 17:24:40 +02:00
|
|
|
QHash<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
|
2011-07-15 12:57:46 +02:00
|
|
|
if (it == d->contents.constEnd()) {
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (it.value().mimeTypes.isEmpty()) {
|
|
|
|
return d->mimeTypes;
|
|
|
|
}
|
|
|
|
|
|
|
|
return it.value().mimeTypes;
|
2011-07-21 14:41:01 +02:00
|
|
|
#else
|
|
|
|
return QStringList();
|
|
|
|
#endif
|
2011-07-15 12:57:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Package::defaultPackageRoot() const
|
|
|
|
{
|
|
|
|
return d->defaultPackageRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Package::setDefaultPackageRoot(const QString &packageRoot)
|
|
|
|
{
|
2011-07-22 17:24:40 +02:00
|
|
|
d.detach();
|
2011-07-15 12:57:46 +02:00
|
|
|
d->defaultPackageRoot = packageRoot;
|
|
|
|
if (!d->defaultPackageRoot.isEmpty() && !d->defaultPackageRoot.endsWith('/')) {
|
|
|
|
d->defaultPackageRoot.append('/');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Package::servicePrefix() const
|
|
|
|
{
|
|
|
|
return d->servicePrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Package::setServicePrefix(const QString &servicePrefix)
|
|
|
|
{
|
2011-07-22 17:24:40 +02:00
|
|
|
d.detach();
|
2011-07-15 12:57:46 +02:00
|
|
|
d->servicePrefix = servicePrefix;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Package::allowExternalPaths() const
|
|
|
|
{
|
|
|
|
return d->externalPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Package::setAllowExternalPaths(bool allow)
|
|
|
|
{
|
2011-07-22 17:24:40 +02:00
|
|
|
d.detach();
|
2011-07-15 12:57:46 +02:00
|
|
|
d->externalPaths = allow;
|
|
|
|
}
|
|
|
|
|
|
|
|
KPluginInfo Package::metadata() const
|
|
|
|
{
|
|
|
|
if (!d->metadata && !d->path.isEmpty()) {
|
2011-07-19 21:37:39 +02:00
|
|
|
const QString metadataPath = filePath("metadata");
|
|
|
|
if (!metadataPath.isEmpty()) {
|
|
|
|
d->createPackageMetadata(metadataPath);
|
|
|
|
} else {
|
|
|
|
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);
|
2011-12-04 14:37:10 +01:00
|
|
|
} else if (mimeType->is("application/x-compressed-tar") || mimeType->is("application/x-gzip") ||
|
|
|
|
mimeType->is("application/x-tar") || mimeType->is("application/x-bzip-compressed-tar")) {
|
2011-07-19 21:37:39 +02:00
|
|
|
archive = new KTar(d->path);
|
|
|
|
} else {
|
|
|
|
kWarning() << "Could not open package file, unsupported archive format:" << d->path << mimeType->name();
|
|
|
|
}
|
2011-07-15 12:57:46 +02:00
|
|
|
|
2011-07-19 21:37:39 +02:00
|
|
|
if (archive && archive->open(QIODevice::ReadOnly)) {
|
|
|
|
const KArchiveDirectory *source = archive->directory();
|
2011-12-06 20:08:22 +01:00
|
|
|
QTemporaryDir tempdir;
|
|
|
|
source->copyTo(tempdir.path() + '/');
|
|
|
|
d->createPackageMetadata(tempdir.path() + '/');
|
2011-07-19 21:37:39 +02:00
|
|
|
} else {
|
|
|
|
kWarning() << "Could not open package file:" << d->path;
|
|
|
|
}
|
2010-11-03 23:12:30 +01:00
|
|
|
|
2011-07-19 21:37:39 +02:00
|
|
|
delete archive;
|
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 12:57:46 +02:00
|
|
|
if (!d->metadata) {
|
|
|
|
d->metadata = new KPluginInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
return *d->metadata;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Package::filePath(const char *fileType, const QString &filename) const
|
|
|
|
{
|
|
|
|
if (!d->valid) {
|
2010-09-05 00:50:50 +02:00
|
|
|
//kDebug() << "package is not valid";
|
2008-11-04 00:08:39 +01:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2011-07-22 17:24:40 +02:00
|
|
|
const QString discoveryKey(fileType + filename);
|
|
|
|
if (d->discoveries.contains(discoveryKey)) {
|
|
|
|
//qDebug() << "looking for" << discoveryKey << d->discoveries.value(discoveryKey);
|
|
|
|
return d->discoveries[discoveryKey];
|
|
|
|
}
|
|
|
|
|
2010-11-03 23:12:30 +01:00
|
|
|
QStringList paths;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-03-09 06:57:57 +01:00
|
|
|
if (qstrlen(fileType) != 0) {
|
2011-07-15 12:57:46 +02:00
|
|
|
if (!d->contents.contains(fileType)) {
|
|
|
|
//kDebug() << "package does not contain" << fileType << filename;
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
paths = d->contents[fileType].paths;
|
2010-03-09 06:57:57 +01:00
|
|
|
|
2010-11-03 23:12:30 +01:00
|
|
|
if (paths.isEmpty()) {
|
2010-09-05 00:50:50 +02:00
|
|
|
//kDebug() << "no matching path came of it, while looking for" << fileType << filename;
|
2011-07-22 17:24:40 +02:00
|
|
|
d->discoveries.insert(discoveryKey, QString());
|
2010-03-09 06:57:57 +01:00
|
|
|
return QString();
|
|
|
|
}
|
2010-11-04 20:18:32 +01:00
|
|
|
} else {
|
2010-12-15 08:13:20 +01:00
|
|
|
//when filetype is empty paths is always empty, so try with an empty string
|
2010-11-04 20:18:32 +01:00
|
|
|
paths << QString();
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2010-11-04 20:18:32 +01:00
|
|
|
//Nested loop, but in the medium case resolves to just one iteration
|
2010-12-15 08:13:20 +01:00
|
|
|
//kDebug() << "prefixes:" << prefixes.count() << prefixes;
|
2011-07-15 12:57:46 +02:00
|
|
|
foreach (const QString &contentsPrefix, d->contentsPrefixPaths) {
|
|
|
|
const QString prefix(d->path + contentsPrefix);
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-11-04 20:18:32 +01:00
|
|
|
foreach (const QString &path, paths) {
|
|
|
|
QString file = prefix + path;
|
2008-11-04 00:08:39 +01:00
|
|
|
|
2010-11-04 20:18:32 +01:00
|
|
|
if (!filename.isEmpty()) {
|
|
|
|
file.append("/").append(filename);
|
2010-11-03 23:12:30 +01:00
|
|
|
}
|
|
|
|
|
2010-12-15 08:13:20 +01:00
|
|
|
//kDebug() << "testing" << file << QFile::exists("/bin/ls") << QFile::exists(file);
|
2010-11-04 20:18:32 +01:00
|
|
|
if (QFile::exists(file)) {
|
2011-07-15 12:57:46 +02:00
|
|
|
if (d->externalPaths) {
|
2010-12-15 08:13:20 +01:00
|
|
|
//kDebug() << "found" << file;
|
2011-07-22 17:24:40 +02:00
|
|
|
d->discoveries.insert(discoveryKey, file);
|
2010-11-04 20:18:32 +01:00
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure that we don't return files outside of our base path
|
|
|
|
// due to symlink or ../ games
|
|
|
|
QDir dir(file);
|
|
|
|
QString canonicalized = dir.canonicalPath() + QDir::separator();
|
|
|
|
|
2011-07-15 12:57:46 +02:00
|
|
|
//kDebug() << "testing that" << canonicalized << "is in" << d->path;
|
|
|
|
if (canonicalized.startsWith(d->path)) {
|
2010-12-15 08:13:20 +01:00
|
|
|
//kDebug() << "found" << file;
|
2011-07-22 17:24:40 +02:00
|
|
|
d->discoveries.insert(discoveryKey, file);
|
2010-11-04 20:18:32 +01:00
|
|
|
return file;
|
|
|
|
}
|
2010-11-03 23:12:30 +01:00
|
|
|
}
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 12:57:46 +02:00
|
|
|
//kDebug() << fileType << filename << "does not exist in" << prefixes << "at root" << d->path;
|
2008-11-04 00:08:39 +01:00
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2011-07-15 12:57:46 +02:00
|
|
|
QStringList Package::entryList(const char *key) const
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
|
|
|
if (!d->valid) {
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
2011-07-22 17:24:40 +02:00
|
|
|
QHash<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
|
2011-07-15 12:57:46 +02:00
|
|
|
if (it == d->contents.constEnd()) {
|
2011-07-15 14:17:42 +02:00
|
|
|
//kDebug() << "couldn't find" << key;
|
2011-07-15 12:57:46 +02:00
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
2011-07-15 14:17:42 +02:00
|
|
|
//kDebug() << "going to list" << key;
|
2011-07-15 12:57:46 +02:00
|
|
|
QStringList list;
|
|
|
|
foreach (const QString &prefix, d->contentsPrefixPaths) {
|
2011-07-15 14:17:42 +02:00
|
|
|
//kDebug() << " looking in" << prefix;
|
2011-07-15 12:57:46 +02:00
|
|
|
foreach (const QString &path, it.value().paths) {
|
2011-07-15 14:17:42 +02:00
|
|
|
//kDebug() << " looking in" << path;
|
2011-07-15 12:57:46 +02:00
|
|
|
if (it.value().directory) {
|
2011-07-15 14:17:42 +02:00
|
|
|
//kDebug() << "it's a directory, so trying out" << d->path + prefix + path;
|
2011-07-15 12:57:46 +02:00
|
|
|
QDir dir(d->path + prefix + path);
|
|
|
|
|
|
|
|
if (d->externalPaths) {
|
|
|
|
list += dir.entryList(QDir::Files | QDir::Readable);
|
|
|
|
} else {
|
|
|
|
// ensure that we don't return files outside of our base path
|
|
|
|
// due to symlink or ../ games
|
|
|
|
QString canonicalized = dir.canonicalPath();
|
2011-07-15 14:17:42 +02:00
|
|
|
if (canonicalized.startsWith(d->path)) {
|
2011-07-15 12:57:46 +02:00
|
|
|
list += dir.entryList(QDir::Files | QDir::Readable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const QString fullPath = d->path + prefix + path;
|
2011-07-15 14:17:42 +02:00
|
|
|
//kDebug() << "it's a file at" << fullPath << QFile::exists(fullPath);
|
2011-07-15 12:57:46 +02:00
|
|
|
if (!QFile::exists(fullPath)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->externalPaths) {
|
|
|
|
list += fullPath;
|
|
|
|
} else {
|
|
|
|
QDir dir(fullPath);
|
|
|
|
QString canonicalized = dir.canonicalPath() + QDir::separator();
|
|
|
|
|
|
|
|
//kDebug() << "testing that" << canonicalized << "is in" << d->path;
|
|
|
|
if (canonicalized.startsWith(d->path)) {
|
|
|
|
list += fullPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2009-05-13 01:05:40 +02:00
|
|
|
void Package::setPath(const QString &path)
|
|
|
|
{
|
2011-07-22 17:24:40 +02:00
|
|
|
if (path == d->path) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d.detach();
|
|
|
|
d->discoveries.clear();
|
2011-07-15 12:57:46 +02:00
|
|
|
if (path.isEmpty()) {
|
|
|
|
d->path.clear();
|
|
|
|
d->valid = false;
|
2011-07-22 17:24:40 +02:00
|
|
|
|
|
|
|
if (d->structure) {
|
|
|
|
d->structure.data()->pathChanged(this);
|
|
|
|
}
|
|
|
|
|
2011-07-15 12:57:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDir dir(path);
|
|
|
|
if (dir.isRelative()) {
|
|
|
|
QString location;
|
|
|
|
if (!d->defaultPackageRoot.isEmpty()) {
|
|
|
|
dir.setPath(d->defaultPackageRoot);
|
|
|
|
if (dir.isRelative()) {
|
2012-05-30 20:25:25 +02:00
|
|
|
location = QStandardPaths::locate(QStandardPaths::GenericDataLocation, d->defaultPackageRoot + path);
|
2011-07-15 12:57:46 +02:00
|
|
|
} else {
|
|
|
|
location = d->defaultPackageRoot + path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (location.isEmpty()) {
|
2012-05-30 20:25:25 +02:00
|
|
|
location = QStandardPaths::locate(QStandardPaths::GenericDataLocation, path);
|
2011-07-15 12:57:46 +02:00
|
|
|
|
|
|
|
if (location.isEmpty()) {
|
|
|
|
d->path.clear();
|
|
|
|
d->valid = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dir.setPath(location);
|
2010-09-03 21:44:28 +02:00
|
|
|
}
|
2011-07-15 12:57:46 +02:00
|
|
|
|
|
|
|
QString basePath = dir.canonicalPath();
|
|
|
|
bool valid = QFile::exists(basePath);
|
|
|
|
|
|
|
|
if (valid) {
|
|
|
|
QFileInfo info(basePath);
|
|
|
|
if (info.isDir() && !basePath.endsWith('/')) {
|
|
|
|
basePath.append('/');
|
|
|
|
}
|
|
|
|
//kDebug() << "basePath is" << basePath;
|
|
|
|
} else {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2011-07-15 12:57:46 +02:00
|
|
|
kDebug() << path << "invalid, basePath is" << basePath;
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2011-07-15 12:57:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d->path == basePath) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
d->path = basePath;
|
|
|
|
delete d->metadata;
|
|
|
|
d->metadata = 0;
|
|
|
|
d->valid = !d->path.isEmpty();
|
2011-07-22 17:24:40 +02:00
|
|
|
|
|
|
|
if (d->structure) {
|
|
|
|
d->structure.data()->pathChanged(this);
|
|
|
|
}
|
2009-05-13 01:05:40 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
const QString Package::path() const
|
|
|
|
{
|
2011-07-15 12:57:46 +02:00
|
|
|
return d->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList Package::contentsPrefixPaths() const
|
|
|
|
{
|
|
|
|
return d->contentsPrefixPaths;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Package::setContentsPrefixPaths(const QStringList &prefixPaths)
|
|
|
|
{
|
2011-07-22 17:24:40 +02:00
|
|
|
d.detach();
|
2011-07-15 12:57:46 +02:00
|
|
|
d->contentsPrefixPaths = prefixPaths;
|
|
|
|
if (d->contentsPrefixPaths.isEmpty()) {
|
|
|
|
d->contentsPrefixPaths << QString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Package::contentsHash() const
|
|
|
|
{
|
|
|
|
if (!d->valid) {
|
|
|
|
kWarning() << "can not create hash due to Package being invalid";
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QCryptographicHash hash(QCryptographicHash::Sha1);
|
|
|
|
QString metadataPath = d->path + "metadata.desktop";
|
|
|
|
if (QFile::exists(metadataPath)) {
|
|
|
|
QFile f(metadataPath);
|
|
|
|
if (f.open(QIODevice::ReadOnly)) {
|
|
|
|
while (!f.atEnd()) {
|
|
|
|
hash.addData(f.read(1024));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
kWarning() << "could not add" << f.fileName() << "to the hash; file could not be opened for reading.";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
kWarning() << "no metadata at" << metadataPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (QString prefix, d->contentsPrefixPaths) {
|
|
|
|
const QString basePath = d->path + prefix;
|
|
|
|
QDir dir(basePath);
|
|
|
|
|
|
|
|
if (!dir.exists()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
d->updateHash(basePath, QString(), dir, hash);
|
|
|
|
}
|
|
|
|
|
2011-07-15 14:24:35 +02:00
|
|
|
return hash.result().toHex();
|
2011-07-15 12:57:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Package::addDirectoryDefinition(const char *key, const QString &path, const QString &name)
|
|
|
|
{
|
|
|
|
ContentStructure s;
|
|
|
|
|
|
|
|
if (d->contents.contains(key)) {
|
|
|
|
s = d->contents[key];
|
|
|
|
}
|
|
|
|
|
2011-07-21 14:41:01 +02:00
|
|
|
#ifndef PLASMA_NO_PACKAGE_EXTRADATA
|
2011-07-15 12:57:46 +02:00
|
|
|
if (!name.isEmpty()) {
|
|
|
|
s.name = name;
|
|
|
|
}
|
2011-07-21 14:41:01 +02:00
|
|
|
#else
|
|
|
|
Q_UNUSED(name)
|
|
|
|
#endif
|
2011-07-15 12:57:46 +02:00
|
|
|
|
|
|
|
s.paths.append(path);
|
|
|
|
s.directory = true;
|
|
|
|
|
|
|
|
d->contents[key] = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Package::addFileDefinition(const char *key, const QString &path, const QString &name)
|
|
|
|
{
|
|
|
|
ContentStructure s;
|
|
|
|
|
|
|
|
if (d->contents.contains(key)) {
|
|
|
|
s = d->contents[key];
|
|
|
|
}
|
|
|
|
|
2011-07-21 14:41:01 +02:00
|
|
|
#ifndef PLASMA_NO_PACKAGE_EXTRADATA
|
2011-07-15 12:57:46 +02:00
|
|
|
if (!name.isEmpty()) {
|
|
|
|
s.name = name;
|
|
|
|
}
|
2011-07-21 14:41:01 +02:00
|
|
|
#else
|
|
|
|
Q_UNUSED(name)
|
|
|
|
#endif
|
2011-07-15 12:57:46 +02:00
|
|
|
|
|
|
|
s.paths.append(path);
|
|
|
|
s.directory = false;
|
|
|
|
|
|
|
|
d->contents[key] = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Package::removeDefinition(const char *key)
|
|
|
|
{
|
|
|
|
d->contents.remove(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Package::setRequired(const char *key, bool required)
|
|
|
|
{
|
2011-07-22 17:24:40 +02:00
|
|
|
QHash<QByteArray, ContentStructure>::iterator it = d->contents.find(key);
|
2011-07-15 12:57:46 +02:00
|
|
|
if (it == d->contents.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-22 17:24:40 +02:00
|
|
|
d.detach();
|
2011-07-15 12:57:46 +02:00
|
|
|
it.value().required = required;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2011-07-15 12:57:46 +02:00
|
|
|
void Package::setDefaultMimeTypes(QStringList mimeTypes)
|
2008-11-04 00:08:39 +01:00
|
|
|
{
|
2011-07-21 14:41:01 +02:00
|
|
|
#ifndef PLASMA_NO_PACKAGE_EXTRADATA
|
2011-07-22 17:24:40 +02:00
|
|
|
d.detach();
|
2011-07-15 12:57:46 +02:00
|
|
|
d->mimeTypes = mimeTypes;
|
2011-07-21 14:41:01 +02:00
|
|
|
#endif
|
2011-07-15 12:57:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void Package::setMimeTypes(const char *key, QStringList mimeTypes)
|
|
|
|
{
|
2011-07-21 14:41:01 +02:00
|
|
|
#ifndef PLASMA_NO_PACKAGE_EXTRADATA
|
2011-07-22 17:24:40 +02:00
|
|
|
QHash<QByteArray, ContentStructure>::iterator it = d->contents.find(key);
|
2011-07-15 12:57:46 +02:00
|
|
|
if (it == d->contents.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-22 17:24:40 +02:00
|
|
|
d.detach();
|
2011-07-15 12:57:46 +02:00
|
|
|
it.value().mimeTypes = mimeTypes;
|
2011-07-21 14:41:01 +02:00
|
|
|
#endif
|
2011-07-15 12:57:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<const char*> Package::directories() const
|
|
|
|
{
|
|
|
|
QList<const char*> dirs;
|
2011-07-22 17:24:40 +02:00
|
|
|
QHash<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
|
2011-07-15 12:57:46 +02:00
|
|
|
while (it != d->contents.constEnd()) {
|
|
|
|
if (it.value().directory) {
|
|
|
|
dirs << it.key();
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return dirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<const char*> Package::requiredDirectories() const
|
|
|
|
{
|
|
|
|
QList<const char*> dirs;
|
2011-07-22 17:24:40 +02:00
|
|
|
QHash<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
|
2011-07-15 12:57:46 +02:00
|
|
|
while (it != d->contents.constEnd()) {
|
|
|
|
if (it.value().directory &&
|
|
|
|
it.value().required) {
|
|
|
|
dirs << it.key();
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return dirs;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<const char*> Package::files() const
|
|
|
|
{
|
|
|
|
QList<const char*> files;
|
2011-07-22 17:24:40 +02:00
|
|
|
QHash<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
|
2011-07-15 12:57:46 +02:00
|
|
|
while (it != d->contents.constEnd()) {
|
|
|
|
if (!it.value().directory) {
|
|
|
|
files << it.key();
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<const char*> Package::requiredFiles() const
|
|
|
|
{
|
|
|
|
QList<const char*> files;
|
2011-07-22 17:24:40 +02:00
|
|
|
QHash<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
|
2011-07-15 12:57:46 +02:00
|
|
|
while (it != d->contents.constEnd()) {
|
|
|
|
if (!it.value().directory && it.value().required) {
|
|
|
|
files << it.key();
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2011-07-19 21:37:39 +02:00
|
|
|
bool Package::installPackage(const QString &package, const QString &packageRoot)
|
2011-07-15 12:57:46 +02:00
|
|
|
{
|
2011-07-19 21:37:39 +02:00
|
|
|
if (d->structure) {
|
|
|
|
return d->structure.data()->installPackage(this, package, packageRoot);
|
2011-07-15 12:57:46 +02:00
|
|
|
}
|
|
|
|
|
2011-07-19 21:37:39 +02:00
|
|
|
return PackagePrivate::installPackage(package, packageRoot, d->servicePrefix);
|
2011-07-15 12:57:46 +02:00
|
|
|
}
|
|
|
|
|
2011-07-19 21:37:39 +02:00
|
|
|
bool PackagePrivate::installPackage(const QString &package, const QString &packageRoot, const QString &servicePrefix)
|
2011-07-15 12:57:46 +02:00
|
|
|
{
|
|
|
|
//TODO: report *what* failed if something does fail
|
|
|
|
QDir root(packageRoot);
|
|
|
|
|
|
|
|
if (!root.exists()) {
|
2012-06-12 12:59:34 +02:00
|
|
|
QDir().mkpath(packageRoot);
|
2011-07-15 12:57:46 +02:00
|
|
|
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;
|
2011-12-06 20:08:22 +01:00
|
|
|
QTemporaryDir tempdir;
|
2011-07-15 12:57:46 +02:00
|
|
|
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") ||
|
2011-12-01 23:50:44 +01:00
|
|
|
mimetype->is("application/x-tar")|| mimetype->is("application/x-bzip-compressed-tar") ||
|
|
|
|
mimetype->is("application/x-xz") || mimetype->is("application/x-lzma")) {
|
2011-07-15 12:57:46 +02:00
|
|
|
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;
|
2011-12-06 20:08:22 +01:00
|
|
|
path = tempdir.path() + '/';
|
2011-07-15 12:57:46 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
KPluginInfo 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) {
|
2012-05-31 14:58:18 +02:00
|
|
|
kWarning() << "Could not move package to destination:" << targetName;
|
2011-07-15 12:57:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
2012-05-31 14:58:18 +02:00
|
|
|
kDebug() << "************************** 12";
|
2011-07-15 12:57:46 +02:00
|
|
|
// it's a directory containing the stuff, so copy the contents rather
|
|
|
|
// than move them
|
|
|
|
const bool ok = copyFolder(path, targetName);
|
2012-05-31 14:58:18 +02:00
|
|
|
kDebug() << "************************** 13";
|
2011-07-15 12:57:46 +02:00
|
|
|
if (!ok) {
|
2012-05-31 14:58:18 +02:00
|
|
|
kWarning() << "Could not copy package to destination:" << targetName;
|
2011-07-15 12:57:46 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (archivedPackage) {
|
|
|
|
// no need to remove the temp dir (which has been successfully moved if it's an archive)
|
|
|
|
tempdir.setAutoRemove(false);
|
|
|
|
}
|
|
|
|
|
2011-07-19 21:37:39 +02:00
|
|
|
if (!servicePrefix.isEmpty()) {
|
2011-07-15 12:57:46 +02:00
|
|
|
// and now we register it as a service =)
|
2012-05-31 14:58:18 +02:00
|
|
|
kDebug() << "************************** 1";
|
2011-07-15 12:57:46 +02:00
|
|
|
QString metaPath = targetName + "/metadata.desktop";
|
2012-05-31 14:58:18 +02:00
|
|
|
kDebug() << "************************** 2";
|
2011-07-15 12:57:46 +02:00
|
|
|
KDesktopFile df(metaPath);
|
|
|
|
KConfigGroup cg = df.desktopGroup();
|
2012-05-31 14:58:18 +02:00
|
|
|
kDebug() << "************************** 3";
|
2011-07-15 12:57:46 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2011-07-19 21:37:39 +02:00
|
|
|
const QString serviceName = servicePrefix + meta.pluginName() + ".desktop";
|
2011-07-15 12:57:46 +02:00
|
|
|
|
2012-05-30 20:25:25 +02:00
|
|
|
QString service = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kde5/services/") + serviceName;
|
2012-05-31 14:58:18 +02:00
|
|
|
kDebug() << "************************** 4";
|
2011-07-15 12:57:46 +02:00
|
|
|
const bool ok = QFile::copy(metaPath, service);
|
2012-05-31 14:58:18 +02:00
|
|
|
kDebug() << "************************** 5";
|
2011-07-15 12:57:46 +02:00
|
|
|
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 {
|
2012-05-31 14:58:18 +02:00
|
|
|
kWarning() << "Could not register package as service (this is not necessarily fatal):" << serviceName;
|
2011-07-15 12:57:46 +02:00
|
|
|
}
|
2012-05-31 14:58:18 +02:00
|
|
|
kDebug() << "************************** 7";
|
2011-07-15 12:57:46 +02:00
|
|
|
}
|
|
|
|
|
2012-05-14 01:02:07 +02:00
|
|
|
QDBusInterface sycoca("org.kde.kded5", "/kbuildsycoca");
|
2011-07-30 18:31:22 +02:00
|
|
|
sycoca.asyncCall("recreate");
|
2011-07-15 12:57:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Package::uninstallPackage(const QString &packageName, const QString &packageRoot)
|
2011-07-19 21:37:39 +02:00
|
|
|
{
|
|
|
|
if (d->structure) {
|
|
|
|
return d->structure.data()->uninstallPackage(this, packageName, packageRoot);
|
|
|
|
}
|
|
|
|
|
|
|
|
return PackagePrivate::uninstallPackage(packageName, packageRoot, d->servicePrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PackagePrivate::uninstallPackage(const QString &packageName, const QString &packageRoot, const QString &servicePrefix)
|
2011-07-15 12:57:46 +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;
|
|
|
|
}
|
|
|
|
|
2011-07-19 21:37:39 +02:00
|
|
|
const QString serviceName = servicePrefix + packageName + ".desktop";
|
2011-07-15 12:57:46 +02:00
|
|
|
|
2012-05-30 20:25:25 +02:00
|
|
|
QString service = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/kde5/services/") + serviceName;
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2011-07-15 12:57:46 +02:00
|
|
|
kDebug() << "Removing service file " << service;
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2011-07-15 12:57:46 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-05-14 01:02:07 +02:00
|
|
|
QDBusInterface sycoca("org.kde.kded5", "/kbuildsycoca");
|
2011-07-30 18:31:22 +02:00
|
|
|
sycoca.asyncCall("recreate");
|
2011-07-15 12:57:46 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PackagePrivate::PackagePrivate()
|
2011-07-22 17:24:40 +02:00
|
|
|
: QSharedData(),
|
|
|
|
servicePrefix("plasma-applet-"),
|
2011-07-15 12:57:46 +02:00
|
|
|
metadata(0),
|
|
|
|
externalPaths(false),
|
|
|
|
valid(false)
|
|
|
|
{
|
|
|
|
contentsPrefixPaths << "contents/";
|
2012-06-18 13:52:02 +02:00
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
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()) {
|
|
|
|
//FIXME: the '/' search will break on non-UNIX. do we care?
|
|
|
|
QString installedIcon("plasma_applet_" + data.pluginName() +
|
|
|
|
iconPath.right(iconPath.length() - iconPath.lastIndexOf("/")));
|
|
|
|
cg.writeEntry("Icon", installedIcon);
|
|
|
|
installedIcon = KStandardDirs::locateLocal("icon", installedIcon);
|
2010-10-14 14:27:15 +02:00
|
|
|
QFile::copy(iconPath, installedIcon);
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
PackagePrivate::PackagePrivate(const PackageStructure::Ptr st, const QString &p)
|
|
|
|
: structure(st),
|
|
|
|
service(0)
|
|
|
|
{
|
2010-09-03 21:44:28 +02:00
|
|
|
if (structure) {
|
2011-07-13 20:32:25 +02:00
|
|
|
if (p.isEmpty()) {
|
|
|
|
structure->setPath(structure->defaultPackageRoot());
|
|
|
|
} else {
|
|
|
|
structure->setPath(p);
|
|
|
|
}
|
2010-09-03 21:44:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
valid = structure && !structure->path().isEmpty();
|
|
|
|
}
|
|
|
|
|
2011-07-14 14:53:57 +02:00
|
|
|
PackagePrivate::PackagePrivate(const PackageStructure::Ptr st, const QString &packageRoot, const QString &path)
|
|
|
|
: structure(st),
|
|
|
|
service(0)
|
|
|
|
{
|
|
|
|
if (structure) {
|
|
|
|
if (packageRoot.isEmpty()) {
|
|
|
|
structure->setPath(structure->defaultPackageRoot()%"/"%path);
|
|
|
|
} else {
|
2011-07-17 12:46:17 +02:00
|
|
|
structure->setPath(packageRoot%"/"%path);
|
2011-07-14 14:53:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
valid = structure && !structure->path().isEmpty();
|
2011-07-15 12:57:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PackagePrivate::PackagePrivate(const PackagePrivate &other)
|
2011-07-22 17:24:40 +02:00
|
|
|
: QSharedData()
|
2011-07-15 12:57:46 +02:00
|
|
|
{
|
|
|
|
*this = other;
|
2011-07-21 14:57:43 +02:00
|
|
|
metadata = 0;
|
2011-07-15 12:57:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PackagePrivate::~PackagePrivate()
|
|
|
|
{
|
|
|
|
delete metadata;
|
|
|
|
}
|
|
|
|
|
|
|
|
PackagePrivate &PackagePrivate::operator=(const PackagePrivate &rhs)
|
|
|
|
{
|
2011-07-22 17:24:40 +02:00
|
|
|
if (&rhs == this) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-07-21 14:57:43 +02:00
|
|
|
structure = rhs.structure;
|
2011-07-15 12:57:46 +02:00
|
|
|
path = rhs.path;
|
|
|
|
contentsPrefixPaths = rhs.contentsPrefixPaths;
|
|
|
|
servicePrefix = rhs.servicePrefix;
|
|
|
|
contents = rhs.contents;
|
2011-07-21 14:41:01 +02:00
|
|
|
#ifndef PLASMA_NO_PACKAGE_EXTRADATA
|
2011-07-15 12:57:46 +02:00
|
|
|
mimeTypes = rhs.mimeTypes;
|
2011-07-21 14:41:01 +02:00
|
|
|
#endif
|
2011-07-15 12:57:46 +02:00
|
|
|
defaultPackageRoot = rhs.defaultPackageRoot;
|
|
|
|
servicePrefix = rhs.servicePrefix;
|
|
|
|
metadata = 0;
|
|
|
|
externalPaths = rhs.externalPaths;
|
|
|
|
valid = rhs.valid;
|
|
|
|
return *this;
|
2008-11-04 00:08:39 +01:00
|
|
|
}
|
|
|
|
|
2011-05-27 12:26:59 +02:00
|
|
|
void PackagePrivate::updateHash(const QString &basePath, const QString &subPath, const QDir &dir, QCryptographicHash &hash)
|
2009-09-21 23:37:44 +02:00
|
|
|
{
|
|
|
|
// hash is calculated as a function of:
|
|
|
|
// * files ordered alphabetically by name, with each file's:
|
|
|
|
// * path relative to the content root
|
|
|
|
// * file data
|
|
|
|
// * directories ordered alphabetically by name, with each dir's:
|
|
|
|
// * path relative to the content root
|
|
|
|
// * file listing (recursing)
|
|
|
|
// symlinks (in both the file and dir case) are handled by adding
|
|
|
|
// the name of the symlink itself and the abs path of what it points to
|
|
|
|
|
|
|
|
const QDir::SortFlags sorting = QDir::Name | QDir::IgnoreCase;
|
|
|
|
const QDir::Filters filters = QDir::Hidden | QDir::System | QDir::NoDotAndDotDot;
|
|
|
|
foreach (const QString &file, dir.entryList(QDir::Files | filters, sorting)) {
|
|
|
|
if (!subPath.isEmpty()) {
|
2011-05-27 12:26:59 +02:00
|
|
|
hash.addData(subPath.toUtf8());
|
2009-09-21 23:37:44 +02:00
|
|
|
}
|
|
|
|
|
2011-05-27 12:26:59 +02:00
|
|
|
hash.addData(file.toUtf8());
|
2009-09-21 23:37:44 +02:00
|
|
|
|
|
|
|
QFileInfo info(dir.path() + '/' + file);
|
|
|
|
if (info.isSymLink()) {
|
2011-05-27 12:26:59 +02:00
|
|
|
hash.addData(info.symLinkTarget().toUtf8());
|
2009-09-21 23:37:44 +02:00
|
|
|
} else {
|
|
|
|
QFile f(info.filePath());
|
|
|
|
if (f.open(QIODevice::ReadOnly)) {
|
|
|
|
while (!f.atEnd()) {
|
2011-05-27 12:26:59 +02:00
|
|
|
hash.addData(f.read(1024));
|
2009-09-21 23:37:44 +02:00
|
|
|
}
|
|
|
|
} else {
|
2009-09-21 23:48:19 +02:00
|
|
|
kWarning() << "could not add" << f.fileName() << "to the hash; file could not be opened for reading. "
|
|
|
|
<< "permissions fail?" << info.permissions() << info.isFile();
|
2009-09-21 23:37:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (const QString &subDirPath, dir.entryList(QDir::Dirs | filters, sorting)) {
|
|
|
|
const QString relativePath = subPath + subDirPath + '/';
|
2011-05-27 12:26:59 +02:00
|
|
|
hash.addData(relativePath.toUtf8());
|
2009-09-21 23:37:44 +02:00
|
|
|
|
|
|
|
QDir subDir(dir.path());
|
|
|
|
subDir.cd(subDirPath);
|
|
|
|
|
|
|
|
if (subDir.path() != subDir.canonicalPath()) {
|
2011-05-27 12:26:59 +02:00
|
|
|
hash.addData(subDir.canonicalPath().toUtf8());
|
2009-09-21 23:37:44 +02:00
|
|
|
} else {
|
|
|
|
updateHash(basePath, relativePath, subDir, hash);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-15 12:57:46 +02:00
|
|
|
void PackagePrivate::createPackageMetadata(const QString &path)
|
2009-09-21 23:37:44 +02:00
|
|
|
{
|
2011-07-15 12:57:46 +02:00
|
|
|
delete metadata;
|
2009-09-21 23:48:19 +02:00
|
|
|
|
2011-07-15 12:57:46 +02:00
|
|
|
QString metadataPath(path + "/metadata.desktop");
|
|
|
|
if (!QFile::exists(metadataPath)) {
|
|
|
|
kWarning() << "No metadata file in the package, expected it at:" << metadataPath;
|
|
|
|
metadataPath.clear();
|
2010-12-15 07:29:36 +01:00
|
|
|
}
|
|
|
|
|
2011-07-15 12:57:46 +02:00
|
|
|
metadata = new KPluginInfo(metadataPath);
|
2009-09-21 23:37:44 +02:00
|
|
|
}
|
|
|
|
|
2008-11-04 00:08:39 +01:00
|
|
|
} // Namespace
|