move the entryList and path setting logic into PackageStructure so that packages can have a bit more control over their life

svn path=/trunk/KDE/kdelibs/; revision=950408
This commit is contained in:
Aaron J. Seigo 2009-04-07 05:42:30 +00:00
parent ca2b1249dc
commit 55feec87c0
3 changed files with 63 additions and 47 deletions

View File

@ -47,22 +47,10 @@ class PackagePrivate
{
public:
PackagePrivate(const PackageStructure::Ptr st, const QString &p)
: structure(st),
basePath(p)
: structure(st)
{
QDir dir(basePath);
basePath = dir.canonicalPath();
valid = QFile::exists(basePath);
if (valid) {
QFileInfo info(basePath);
if (info.isDir()) {
basePath.append(QDir::separator());
}
kDebug() << "basePath is" << basePath;
} else {
kDebug() << p << "invalid, basePath is" << basePath;
}
structure->setPath(p);
valid = !structure->path().isEmpty();
}
~PackagePrivate()
@ -70,7 +58,6 @@ public:
}
PackageStructure::Ptr structure;
QString basePath;
bool valid;
};
@ -78,13 +65,11 @@ Package::Package(const QString &packageRoot, const QString &package,
PackageStructure::Ptr structure)
: d(new PackagePrivate(structure, packageRoot + '/' + package))
{
structure->setPath(d->basePath);
}
Package::Package(const QString &packagePath, PackageStructure::Ptr structure)
: d(new PackagePrivate(structure, packagePath))
{
structure->setPath(d->basePath);
}
Package::~Package()
@ -99,7 +84,7 @@ bool Package::isValid() const
}
foreach (const char *dir, d->structure->requiredDirectories()) {
if (!QFile::exists(d->basePath + d->structure->contentsPrefix() + d->structure->path(dir))) {
if (!QFile::exists(d->structure->path() + d->structure->contentsPrefix() + d->structure->path(dir))) {
kWarning() << "Could not find required directory" << dir;
d->valid = false;
return false;
@ -107,9 +92,9 @@ bool Package::isValid() const
}
foreach (const char *file, d->structure->requiredFiles()) {
if (!QFile::exists(d->basePath + d->structure->contentsPrefix() + d->structure->path(file))) {
if (!QFile::exists(d->structure->path() + d->structure->contentsPrefix() + d->structure->path(file))) {
kWarning() << "Could not find required file" << file << ", look in"
<< d->basePath + d->structure->contentsPrefix() + d->structure->path(file) << endl;
<< d->structure->path() + d->structure->contentsPrefix() + d->structure->path(file) << endl;
d->valid = false;
return false;
}
@ -132,7 +117,7 @@ QString Package::filePath(const char *fileType, const QString &filename) const
return QString();
}
path.prepend(d->basePath + d->structure->contentsPrefix());
path.prepend(d->structure->path() + d->structure->contentsPrefix());
if (!filename.isEmpty()) {
path.append("/").append(filename);
@ -147,7 +132,7 @@ QString Package::filePath(const char *fileType, const QString &filename) const
// due to symlink or ../ games
QDir dir(path);
QString canonicalized = dir.canonicalPath() + QDir::separator();
if (canonicalized.startsWith(d->basePath)) {
if (canonicalized.startsWith(d->structure->path())) {
return path;
}
}
@ -167,27 +152,7 @@ QStringList Package::entryList(const char *fileType) const
return QStringList();
}
QString path = d->structure->path(fileType);
if (path.isEmpty()) {
return QStringList();
}
QDir dir(d->basePath + d->structure->contentsPrefix() + path);
if (dir.exists()) {
if (d->structure->allowExternalPaths()) {
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(d->basePath)) {
return dir.entryList(QDir::Files | QDir::Readable);
}
}
return QStringList();
return d->structure->entryList(fileType);
}
PackageMetadata Package::metadata() const
@ -197,7 +162,7 @@ PackageMetadata Package::metadata() const
const QString Package::path() const
{
return d->basePath;
return d->structure->path();
}
const PackageStructure::Ptr Package::structure() const

View File

@ -19,6 +19,7 @@
#include "packagestructure.h"
#include <QDir>
#include <QMap>
#include <QFileInfo>
@ -262,6 +263,32 @@ QList<const char*> PackageStructure::requiredFiles() const
return files;
}
QStringList PackageStructure::entryList(const char *key)
{
QString p = path(key);
if (p.isEmpty()) {
return QStringList();
}
QDir dir(d->path + d->contentsPrefix + p);
if (dir.exists()) {
if (d->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(d->path)) {
return dir.entryList(QDir::Files | QDir::Readable);
}
}
return QStringList();
}
void PackageStructure::addDirectoryDefinition(const char *key,
const QString &path, const QString &name)
{
@ -356,11 +383,26 @@ QStringList PackageStructure::mimetypes(const char *key) const
void PackageStructure::setPath(const QString &path)
{
if (d->path == path) {
QDir dir(path);
QString basePath = dir.canonicalPath();
bool valid = QFile::exists(basePath);
if (valid) {
QFileInfo info(basePath);
if (info.isDir()) {
basePath.append(QDir::separator());
}
kDebug() << "basePath is" << basePath;
} else {
kDebug() << path << "invalid, basePath is" << basePath;
return;
}
d->path = path;
if (d->path == basePath) {
return;
}
d->path = basePath;
delete d->metadata;
d->metadata = 0;
pathChanged();

View File

@ -154,6 +154,15 @@ public:
**/
QString path(const char *key) const;
/**
* Get the list of files of a given type.
*
* @arg key the type of file to look for
* @return list of files by name
* @since 4.3
*/
QStringList entryList(const char *key);
/**
* @return user visible name for the given entry
**/