Cleanup of Package classes as discussed on panel-devel.

* Add convenience constructor to the Package class.
* Make some methods const in PackageStructures.
* Use QByteArray to store keys in PackageStructures, so that they are compared
  by value, and not by address.
* Add a test checking that the path to the metadata file is retrieved correctly.


svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=741935
This commit is contained in:
Paolo Capriotti 2007-11-26 19:16:40 +00:00
parent 9603d8cf57
commit b88aea051e
7 changed files with 61 additions and 35 deletions

View File

@ -68,6 +68,12 @@ Package::Package(const QString& packageRoot, const QString& package,
{ {
} }
Package::Package(const QString &packagePath, const PackageStructure &structure)
: d(new Private(structure, packagePath))
{
}
Package::~Package() Package::~Package()
{ {
delete d; delete d;

View File

@ -47,6 +47,15 @@ class PLASMA_EXPORT Package
**/ **/
Package(const QString& packageRoot, const QString& package, Package(const QString& packageRoot, const QString& package,
const PackageStructure& structure); const PackageStructure& structure);
/**
* Construct a Package object.
*
* @arg packagePath full path to the package directory
* @arg structure the package structure describing this package
*/
Package(const QString &packagePath, const PackageStructure &structure);
~Package(); ~Package();
/** /**

View File

@ -89,7 +89,7 @@ void PackageMetadata::write(const QString& filename) const
config.writeEntry("Description", d->description); config.writeEntry("Description", d->description);
config.writeEntry("Icon", d->icon); config.writeEntry("Icon", d->icon);
config.writeEntry("X-KDE-Screenshot", d->screenshot); config.writeEntry("X-KDE-Screenshot", d->screenshot);
config.writeEntry("ServiceTypes", d->serviceType); config.writeEntry("X-KDE-ServiceTypes", d->serviceType);
config.writeEntry("X-KDE-PluginInfo-Name", d->name); config.writeEntry("X-KDE-PluginInfo-Name", d->name);
config.writeEntry("X-KDE-PluginInfo-Author", d->author); config.writeEntry("X-KDE-PluginInfo-Author", d->author);
config.writeEntry("X-KDE-PluginInfo-Email", d->email); config.writeEntry("X-KDE-PluginInfo-Email", d->email);
@ -112,7 +112,7 @@ void PackageMetadata::read(const QString& filename)
d->description = config.readEntry("Description", d->description); d->description = config.readEntry("Description", d->description);
d->icon = config.readEntry("Icon", d->icon); d->icon = config.readEntry("Icon", d->icon);
d->screenshot= config.readEntry("X-KDE-Screenshot", d->screenshot); d->screenshot= config.readEntry("X-KDE-Screenshot", d->screenshot);
d->serviceType = config.readEntry("ServiceTypes", d->serviceType); d->serviceType = config.readEntry("X-KDE-ServiceTypes", d->serviceType);
d->author = config.readEntry("X-KDE-PluginInfo-Author", d->author); d->author = config.readEntry("X-KDE-PluginInfo-Author", d->author);
d->email = config.readEntry("X-KDE-PluginInfo-Email", d->email); d->email = config.readEntry("X-KDE-PluginInfo-Email", d->email);
d->version = config.readEntry("X-KDE-PluginInfo-Version", d->version); d->version = config.readEntry("X-KDE-PluginInfo-Version", d->version);

View File

@ -19,7 +19,7 @@
#include "packagestructure.h" #include "packagestructure.h"
#include <QHash> #include <QMap>
namespace Plasma namespace Plasma
{ {
@ -53,7 +53,7 @@ class PackageStructure::Private
{ {
public: public:
QString type; QString type;
QHash<const char*, ContentStructure> contents; QMap<QByteArray, ContentStructure> contents;
QStringList mimetypes; QStringList mimetypes;
}; };
@ -84,28 +84,28 @@ PackageStructure& PackageStructure::operator=(const PackageStructure& rhs)
return *this; return *this;
} }
QString PackageStructure::type() QString PackageStructure::type() const
{ {
return d->type; return d->type;
} }
QList<const char*> PackageStructure::directories() QList<const char*> PackageStructure::directories() const
{ {
QList<const char*> dirs; QList<const char*> dirs;
QHash<const char*, ContentStructure>::const_iterator it = d->contents.constBegin(); QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
while (it != d->contents.constEnd()) { while (it != d->contents.constEnd()) {
if (it.value().directory) { if (it.value().directory) {
dirs << it.key(); dirs << it.key().constData();
} }
++it; ++it;
} }
return dirs; return dirs;
} }
QList<const char*> PackageStructure::requiredDirectories() QList<const char*> PackageStructure::requiredDirectories() const
{ {
QList<const char*> dirs; QList<const char*> dirs;
QHash<const char*, ContentStructure>::const_iterator it = d->contents.constBegin(); QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
while (it != d->contents.constEnd()) { while (it != d->contents.constEnd()) {
if (it.value().directory && if (it.value().directory &&
it.value().required) { it.value().required) {
@ -116,10 +116,10 @@ QList<const char*> PackageStructure::requiredDirectories()
return dirs; return dirs;
} }
QList<const char*> PackageStructure::files() QList<const char*> PackageStructure::files() const
{ {
QList<const char*> files; QList<const char*> files;
QHash<const char*, ContentStructure>::const_iterator it = d->contents.constBegin(); QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
while (it != d->contents.constEnd()) { while (it != d->contents.constEnd()) {
if (!it.value().directory) { if (!it.value().directory) {
files << it.key(); files << it.key();
@ -129,10 +129,10 @@ QList<const char*> PackageStructure::files()
return files; return files;
} }
QList<const char*> PackageStructure::requiredFiles() QList<const char*> PackageStructure::requiredFiles() const
{ {
QList<const char*> files; QList<const char*> files;
QHash<const char*, ContentStructure>::const_iterator it = d->contents.constBegin(); QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
while (it != d->contents.constEnd()) { while (it != d->contents.constEnd()) {
if (!it.value().directory && it.value().required) { if (!it.value().directory && it.value().required) {
files << it.key(); files << it.key();
@ -162,9 +162,9 @@ void PackageStructure::addFileDefinition(const char* key, const QString& path, c
d->contents[key] = s; d->contents[key] = s;
} }
QString PackageStructure::path(const char* key) QString PackageStructure::path(const char* key) const
{ {
QHash<const char*, ContentStructure>::const_iterator it = d->contents.find(key); QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.find(key);
if (it == d->contents.constEnd()) { if (it == d->contents.constEnd()) {
return QString(); return QString();
} }
@ -172,9 +172,9 @@ QString PackageStructure::path(const char* key)
return it.value().path; return it.value().path;
} }
QString PackageStructure::name(const char* key) QString PackageStructure::name(const char* key) const
{ {
QHash<const char*, ContentStructure>::const_iterator it = d->contents.find(key); QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.find(key);
if (it == d->contents.constEnd()) { if (it == d->contents.constEnd()) {
return QString(); return QString();
} }
@ -184,7 +184,7 @@ QString PackageStructure::name(const char* key)
void PackageStructure::setRequired(const char* key, bool required) void PackageStructure::setRequired(const char* key, bool required)
{ {
QHash<const char*, ContentStructure>::iterator it = d->contents.find(key); QMap<QByteArray, ContentStructure>::iterator it = d->contents.find(key);
if (it == d->contents.end()) { if (it == d->contents.end()) {
return; return;
} }
@ -192,9 +192,9 @@ void PackageStructure::setRequired(const char* key, bool required)
it.value().required = required; it.value().required = required;
} }
bool PackageStructure::required(const char* key) bool PackageStructure::required(const char* key) const
{ {
QHash<const char*, ContentStructure>::const_iterator it = d->contents.find(key); QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.find(key);
if (it == d->contents.constEnd()) { if (it == d->contents.constEnd()) {
return false; return false;
} }
@ -209,7 +209,7 @@ void PackageStructure::setDefaultMimetypes(QStringList mimetypes)
void PackageStructure::setMimetypes(const char* key, QStringList mimetypes) void PackageStructure::setMimetypes(const char* key, QStringList mimetypes)
{ {
QHash<const char*, ContentStructure>::iterator it = d->contents.find(key); QMap<QByteArray, ContentStructure>::iterator it = d->contents.find(key);
if (it == d->contents.end()) { if (it == d->contents.end()) {
return; return;
} }
@ -217,9 +217,9 @@ void PackageStructure::setMimetypes(const char* key, QStringList mimetypes)
it.value().mimetypes = mimetypes; it.value().mimetypes = mimetypes;
} }
QStringList PackageStructure::mimetypes(const char* key) QStringList PackageStructure::mimetypes(const char* key) const
{ {
QHash<const char*, ContentStructure>::const_iterator it = d->contents.find(key); QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.find(key);
if (it == d->contents.constEnd()) { if (it == d->contents.constEnd()) {
return QStringList(); return QStringList();
} }

View File

@ -70,27 +70,27 @@ public:
/** /**
* Type of package this structure describes * Type of package this structure describes
**/ **/
QString type(); QString type() const;
/** /**
* The directories defined for this package * The directories defined for this package
**/ **/
QList<const char*> directories(); QList<const char*> directories() const;
/** /**
* The required directories defined for this package * The required directories defined for this package
**/ **/
QList<const char*> requiredDirectories(); QList<const char*> requiredDirectories() const;
/** /**
* The individual files, by key, that are defined for this package * The individual files, by key, that are defined for this package
**/ **/
QList<const char*> files(); QList<const char*> files() const;
/** /**
* The individual required files, by key, that are defined for this package * The individual required files, by key, that are defined for this package
**/ **/
QList<const char*> requiredFiles(); QList<const char*> requiredFiles() const;
/** /**
* Adds a directory to the structure of the package. It is added as * Adds a directory to the structure of the package. It is added as
@ -115,12 +115,12 @@ public:
/** /**
* @return path relative to the package root for the given entry * @return path relative to the package root for the given entry
**/ **/
QString path(const char* key); QString path(const char* key) const;
/** /**
* @return user visible name for the given entry * @return user visible name for the given entry
**/ **/
QString name(const char* key); QString name(const char* key) const;
/** /**
* Sets whether or not a given part of the structure is required or not. * Sets whether or not a given part of the structure is required or not.
@ -135,7 +135,7 @@ public:
/** /**
* @return true if the item at path exists and is required * @return true if the item at path exists and is required
**/ **/
bool required(const char* key); bool required(const char* key) const;
/** /**
* Defines the default mimetypes for any definitions that do not have * Defines the default mimetypes for any definitions that do not have
@ -159,7 +159,7 @@ public:
/** /**
* @return the mimetypes associated with the path, if any * @return the mimetypes associated with the path, if any
**/ **/
QStringList mimetypes(const char* key); QStringList mimetypes(const char* key) const;
/** /**
* Copy constructor * Copy constructor

View File

@ -239,9 +239,9 @@ void PlasmoidPackageTest::entryList()
void PlasmoidPackageTest::knownPackages() void PlasmoidPackageTest::knownPackages()
{ {
// Don't do strange things when package root doesn't exists. // Don't do strange things when package root doesn't exists.
QDir pRoot = QDir(mPackageRoot); QDir pRoot = QDir(mPackageRoot + "blah");
QVERIFY(!pRoot.exists()); QVERIFY(!pRoot.exists());
p = new Plasma::Package(mPackageRoot, mPackage, *ps); p = new Plasma::Package(mPackageRoot + "blah", mPackage, *ps);
QCOMPARE(p->knownPackages(mPackageRoot), QStringList()); QCOMPARE(p->knownPackages(mPackageRoot), QStringList());
delete p; delete p;
@ -278,4 +278,14 @@ void PlasmoidPackageTest::knownPackages()
QVERIFY(packages.contains(plamoid2)); QVERIFY(packages.contains(plamoid2));
} }
void PlasmoidPackageTest::metadata()
{
QString plasmoid("plasmoid_with_metadata");
createTestPackage(plasmoid);
QString path = mPackageRoot + '/' + plasmoid + "/metadata.desktop";
p = new Plasma::Package(mPackageRoot, plasmoid, *ps);
QCOMPARE(p->filePath("metadata"), path);
}
QTEST_KDEMAIN(PlasmoidPackageTest, NoGUI) QTEST_KDEMAIN(PlasmoidPackageTest, NoGUI)

View File

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