2007-06-17 02:25:16 +02:00
|
|
|
/******************************************************************************
|
2007-08-06 13:20:02 +02:00
|
|
|
* Copyright 2007 by Aaron Seigo <aseigo@kde.org> *
|
2007-06-17 02:25:16 +02: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 "packagestructure.h"
|
|
|
|
|
2007-11-26 20:16:40 +01:00
|
|
|
#include <QMap>
|
2008-02-26 02:46:17 +01:00
|
|
|
|
2007-12-02 14:16:39 +01:00
|
|
|
#include <KConfigGroup>
|
2008-02-26 02:46:17 +01:00
|
|
|
#include <KStandardDirs>
|
2008-02-26 05:07:07 +01:00
|
|
|
#include <KServiceTypeTrader>
|
2008-02-26 02:46:17 +01:00
|
|
|
|
2008-02-27 01:51:04 +01:00
|
|
|
#include "package.h"
|
2007-06-17 02:25:16 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class ContentStructure
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ContentStructure()
|
|
|
|
: directory(false),
|
|
|
|
required(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-07-20 05:21:40 +02:00
|
|
|
ContentStructure(const ContentStructure& other)
|
|
|
|
{
|
|
|
|
path = other.path;
|
|
|
|
name = other.name;
|
|
|
|
mimetypes = other.mimetypes;
|
|
|
|
directory = other.directory;
|
|
|
|
required = other.required;
|
|
|
|
}
|
|
|
|
|
2007-06-17 02:25:16 +02:00
|
|
|
QString path;
|
|
|
|
QString name;
|
|
|
|
QStringList mimetypes;
|
|
|
|
bool directory;
|
|
|
|
bool required;
|
|
|
|
};
|
|
|
|
|
2008-02-26 05:07:07 +01:00
|
|
|
|
2007-06-17 02:25:16 +02:00
|
|
|
class PackageStructure::Private
|
|
|
|
{
|
2008-02-26 05:07:07 +01:00
|
|
|
public:
|
|
|
|
QString type;
|
2008-02-28 04:53:26 +01:00
|
|
|
QString path;
|
2008-02-26 05:07:07 +01:00
|
|
|
QMap<QByteArray, ContentStructure> contents;
|
|
|
|
QStringList mimetypes;
|
|
|
|
static QHash<QString, PackageStructure::Ptr> structures;
|
2008-02-28 04:53:26 +01:00
|
|
|
};
|
2007-06-17 02:25:16 +02:00
|
|
|
|
2008-02-26 05:07:07 +01:00
|
|
|
QHash<QString, PackageStructure::Ptr> PackageStructure::Private::structures;
|
2007-06-17 02:25:16 +02:00
|
|
|
|
2008-02-26 05:07:07 +01:00
|
|
|
PackageStructure::PackageStructure(QObject *parent, const QString &type)
|
|
|
|
: QObject(parent),
|
|
|
|
d(new Private)
|
2007-06-17 02:25:16 +02:00
|
|
|
{
|
2008-02-26 05:07:07 +01:00
|
|
|
d->type = type;
|
2007-06-17 02:25:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PackageStructure::~PackageStructure()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2008-02-26 05:07:07 +01:00
|
|
|
PackageStructure::Ptr PackageStructure::load(const QString &packageFormat)
|
2008-02-26 02:46:17 +01:00
|
|
|
{
|
|
|
|
if (packageFormat.isEmpty()) {
|
2008-02-26 05:07:07 +01:00
|
|
|
return Ptr(new PackageStructure());
|
2008-02-26 02:46:17 +01:00
|
|
|
}
|
|
|
|
|
2008-02-26 05:07:07 +01:00
|
|
|
PackageStructure::Ptr structure = Private::structures[packageFormat];
|
|
|
|
|
|
|
|
if (structure) {
|
|
|
|
return structure;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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();
|
2008-02-26 02:46:17 +01:00
|
|
|
QString configPath("plasma/packageformats/%1rc");
|
|
|
|
configPath = KStandardDirs::locate("data", configPath.arg(packageFormat));
|
|
|
|
|
|
|
|
if (!configPath.isEmpty()) {
|
|
|
|
KConfig config(configPath);
|
2008-02-26 05:07:07 +01:00
|
|
|
structure->read(&config);
|
2008-02-28 04:53:26 +01:00
|
|
|
Private::structures[packageFormat] = structure;
|
2008-02-26 02:46:17 +01:00
|
|
|
}
|
|
|
|
|
2008-02-26 05:07:07 +01:00
|
|
|
return structure;
|
2008-02-26 02:46:17 +01:00
|
|
|
}
|
|
|
|
|
2007-06-17 02:25:16 +02:00
|
|
|
PackageStructure& PackageStructure::operator=(const PackageStructure& rhs)
|
|
|
|
{
|
|
|
|
if (this == &rhs) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
*d = *rhs.d;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2007-11-26 20:16:40 +01:00
|
|
|
QString PackageStructure::type() const
|
2007-06-17 02:25:16 +02:00
|
|
|
{
|
|
|
|
return d->type;
|
|
|
|
}
|
|
|
|
|
2007-11-26 20:16:40 +01:00
|
|
|
QList<const char*> PackageStructure::directories() const
|
2007-06-17 02:25:16 +02:00
|
|
|
{
|
2007-10-22 03:28:48 +02:00
|
|
|
QList<const char*> dirs;
|
2007-11-26 20:16:40 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
|
2007-06-17 02:25:16 +02:00
|
|
|
while (it != d->contents.constEnd()) {
|
|
|
|
if (it.value().directory) {
|
2007-11-26 20:16:40 +01:00
|
|
|
dirs << it.key().constData();
|
2007-06-17 02:25:16 +02:00
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return dirs;
|
|
|
|
}
|
|
|
|
|
2007-11-26 20:16:40 +01:00
|
|
|
QList<const char*> PackageStructure::requiredDirectories() const
|
2007-07-20 05:21:40 +02:00
|
|
|
{
|
2007-10-22 03:28:48 +02:00
|
|
|
QList<const char*> dirs;
|
2007-11-26 20:16:40 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
|
2007-07-20 05:21:40 +02:00
|
|
|
while (it != d->contents.constEnd()) {
|
|
|
|
if (it.value().directory &&
|
|
|
|
it.value().required) {
|
2007-10-22 03:28:48 +02:00
|
|
|
dirs << it.key();
|
2007-07-20 05:21:40 +02:00
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return dirs;
|
|
|
|
}
|
|
|
|
|
2007-11-26 20:16:40 +01:00
|
|
|
QList<const char*> PackageStructure::files() const
|
2007-07-20 05:21:40 +02:00
|
|
|
{
|
2007-10-22 03:28:48 +02:00
|
|
|
QList<const char*> files;
|
2007-11-26 20:16:40 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
|
2007-07-20 05:21:40 +02:00
|
|
|
while (it != d->contents.constEnd()) {
|
2007-10-22 03:28:48 +02:00
|
|
|
if (!it.value().directory) {
|
|
|
|
files << it.key();
|
2007-07-20 05:21:40 +02:00
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
2007-11-26 20:16:40 +01:00
|
|
|
QList<const char*> PackageStructure::requiredFiles() const
|
2007-06-17 02:25:16 +02:00
|
|
|
{
|
2007-10-22 03:28:48 +02:00
|
|
|
QList<const char*> files;
|
2007-11-26 20:16:40 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
|
2007-06-17 02:25:16 +02:00
|
|
|
while (it != d->contents.constEnd()) {
|
2007-10-22 03:28:48 +02:00
|
|
|
if (!it.value().directory && it.value().required) {
|
|
|
|
files << it.key();
|
2007-06-17 02:25:16 +02:00
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
return files;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::addDirectoryDefinition(const char* key, const QString& path, const QString& name)
|
|
|
|
{
|
|
|
|
ContentStructure s;
|
|
|
|
s.name = name;
|
|
|
|
s.path = path;
|
2007-10-09 19:22:52 +02:00
|
|
|
s.directory = true;
|
2007-06-17 02:25:16 +02:00
|
|
|
|
|
|
|
d->contents[key] = s;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::addFileDefinition(const char* key, const QString& path, const QString& name)
|
|
|
|
{
|
|
|
|
ContentStructure s;
|
|
|
|
s.name = name;
|
|
|
|
s.path = path;
|
|
|
|
s.directory = false;
|
|
|
|
|
|
|
|
d->contents[key] = s;
|
|
|
|
}
|
|
|
|
|
2007-11-26 20:16:40 +01:00
|
|
|
QString PackageStructure::path(const char* key) const
|
2007-06-17 02:25:16 +02:00
|
|
|
{
|
2007-11-26 20:16:40 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.find(key);
|
2007-06-17 02:25:16 +02:00
|
|
|
if (it == d->contents.constEnd()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return it.value().path;
|
|
|
|
}
|
|
|
|
|
2007-11-26 20:16:40 +01:00
|
|
|
QString PackageStructure::name(const char* key) const
|
2007-06-17 02:25:16 +02:00
|
|
|
{
|
2007-11-26 20:16:40 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.find(key);
|
2007-06-17 02:25:16 +02:00
|
|
|
if (it == d->contents.constEnd()) {
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
return it.value().name;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::setRequired(const char* key, bool required)
|
|
|
|
{
|
2007-11-26 20:16:40 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::iterator it = d->contents.find(key);
|
2007-06-17 02:25:16 +02:00
|
|
|
if (it == d->contents.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
it.value().required = required;
|
|
|
|
}
|
|
|
|
|
2007-11-26 20:16:40 +01:00
|
|
|
bool PackageStructure::required(const char* key) const
|
2007-06-17 02:25:16 +02:00
|
|
|
{
|
2007-11-26 20:16:40 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.find(key);
|
2007-06-17 02:25:16 +02:00
|
|
|
if (it == d->contents.constEnd()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return it.value().required;
|
|
|
|
}
|
|
|
|
|
2007-06-17 03:04:59 +02:00
|
|
|
void PackageStructure::setDefaultMimetypes(QStringList mimetypes)
|
|
|
|
{
|
|
|
|
d->mimetypes = mimetypes;
|
|
|
|
}
|
|
|
|
|
2007-06-17 02:25:16 +02:00
|
|
|
void PackageStructure::setMimetypes(const char* key, QStringList mimetypes)
|
|
|
|
{
|
2007-11-26 20:16:40 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::iterator it = d->contents.find(key);
|
2007-06-17 02:25:16 +02:00
|
|
|
if (it == d->contents.end()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
it.value().mimetypes = mimetypes;
|
|
|
|
}
|
|
|
|
|
2007-11-26 20:16:40 +01:00
|
|
|
QStringList PackageStructure::mimetypes(const char* key) const
|
2007-06-17 02:25:16 +02:00
|
|
|
{
|
2007-11-26 20:16:40 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.find(key);
|
2007-06-17 02:25:16 +02:00
|
|
|
if (it == d->contents.constEnd()) {
|
|
|
|
return QStringList();
|
|
|
|
}
|
|
|
|
|
2007-06-17 03:04:59 +02:00
|
|
|
if (it.value().mimetypes.isEmpty()) {
|
|
|
|
return d->mimetypes;
|
|
|
|
}
|
|
|
|
|
2007-06-17 02:25:16 +02:00
|
|
|
return it.value().mimetypes;
|
|
|
|
}
|
|
|
|
|
2008-02-28 04:53:26 +01:00
|
|
|
void PackageStructure::setPath(const QString &path)
|
|
|
|
{
|
|
|
|
d->path = path;
|
|
|
|
pathChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString PackageStructure::path() const
|
|
|
|
{
|
|
|
|
return d->path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::pathChanged()
|
|
|
|
{
|
|
|
|
// Do nothing ... subclasses might, however.
|
|
|
|
}
|
|
|
|
|
2008-02-26 02:46:17 +01:00
|
|
|
void PackageStructure::read(const KConfigBase *config)
|
2007-12-02 14:16:39 +01:00
|
|
|
{
|
2008-02-26 02:46:17 +01:00
|
|
|
d->contents.clear();
|
|
|
|
d->mimetypes.clear();
|
|
|
|
d->type = config->group("").readEntry("Type", QString());
|
|
|
|
|
2007-12-02 14:16:39 +01:00
|
|
|
QStringList groups = config->groupList();
|
2008-02-26 02:46:17 +01:00
|
|
|
foreach (QString group, groups) {
|
|
|
|
QByteArray key = group.toAscii();
|
|
|
|
KConfigGroup entry = config->group(group);
|
|
|
|
|
2007-12-02 14:16:39 +01:00
|
|
|
QString path = entry.readEntry("Path", QString());
|
|
|
|
QString name = entry.readEntry("Name", QString());
|
|
|
|
QStringList mimetypes = entry.readEntry("Mimetypes", QStringList());
|
|
|
|
bool directory = entry.readEntry("Directory", false);
|
|
|
|
bool required = entry.readEntry("Required", false);
|
2008-02-26 02:46:17 +01:00
|
|
|
|
2007-12-02 14:16:39 +01:00
|
|
|
if (directory) {
|
2008-02-26 02:46:17 +01:00
|
|
|
addDirectoryDefinition(key, path, name);
|
|
|
|
} else {
|
|
|
|
addFileDefinition(key, path, name);
|
2007-12-02 14:16:39 +01:00
|
|
|
}
|
2008-02-26 02:46:17 +01:00
|
|
|
|
|
|
|
setMimetypes(key, mimetypes);
|
|
|
|
setRequired(key, required);
|
2007-12-02 14:16:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PackageStructure::write(KConfigBase *config) const
|
|
|
|
{
|
|
|
|
config->group("").writeEntry("Type", type());
|
2008-02-26 02:46:17 +01:00
|
|
|
|
2007-12-02 14:16:39 +01:00
|
|
|
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
|
|
|
|
while (it != d->contents.constEnd()) {
|
|
|
|
KConfigGroup group = config->group(it.key());
|
|
|
|
group.writeEntry("Path", it.value().path);
|
|
|
|
group.writeEntry("Name", it.value().name);
|
|
|
|
if (!it.value().mimetypes.isEmpty()) {
|
|
|
|
group.writeEntry("Mimetypes", it.value().mimetypes);
|
|
|
|
}
|
|
|
|
if (it.value().directory) {
|
|
|
|
group.writeEntry("Directory", true);
|
|
|
|
}
|
|
|
|
if (it.value().required) {
|
|
|
|
group.writeEntry("Required", true);
|
|
|
|
}
|
2008-02-26 02:46:17 +01:00
|
|
|
|
2007-12-02 14:16:39 +01:00
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-27 01:51:04 +01:00
|
|
|
bool PackageStructure::installPackage(const QString &package, const QString &packageRoot)
|
|
|
|
{
|
|
|
|
return Package::installPackage(package, packageRoot);
|
|
|
|
}
|
|
|
|
|
2007-06-17 02:25:16 +02:00
|
|
|
} // Plasma namespace
|
|
|
|
|
2008-02-26 05:07:07 +01:00
|
|
|
#include "packagestructure.moc"
|
|
|
|
|