• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libplasma

packagestructure.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *   Copyright 2007 by Aaron Seigo <aseigo@kde.org>                        *
00003 *                                                                             *
00004 *   This library is free software; you can redistribute it and/or             *
00005 *   modify it under the terms of the GNU Library General Public               *
00006 *   License as published by the Free Software Foundation; either              *
00007 *   version 2 of the License, or (at your option) any later version.          *
00008 *                                                                             *
00009 *   This library is distributed in the hope that it will be useful,           *
00010 *   but WITHOUT ANY WARRANTY; without even the implied warranty of            *
00011 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU          *
00012 *   Library General Public License for more details.                          *
00013 *                                                                             *
00014 *   You should have received a copy of the GNU Library General Public License *
00015 *   along with this library; see the file COPYING.LIB.  If not, write to      *
00016 *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,      *
00017 *   Boston, MA 02110-1301, USA.                                               *
00018 *******************************************************************************/
00019 
00020 #include "packagestructure.h"
00021 
00022 #include <QMap>
00023 
00024 #include <KConfigGroup>
00025 #include <KStandardDirs>
00026 #include <KServiceTypeTrader>
00027 
00028 #include "package.h"
00029 
00030 namespace Plasma
00031 {
00032 
00033 class ContentStructure
00034 {
00035     public:
00036         ContentStructure()
00037             : directory(false),
00038               required(false)
00039         {
00040         }
00041 
00042         ContentStructure(const ContentStructure& other)
00043         {
00044             path = other.path;
00045             name = other.name;
00046             mimetypes = other.mimetypes;
00047             directory = other.directory;
00048             required = other.required;
00049         }
00050 
00051         QString path;
00052         QString name;
00053         QStringList mimetypes;
00054         bool directory;
00055         bool required;
00056 };
00057 
00058 
00059 class PackageStructurePrivate
00060 {
00061 public:
00062     QString type;
00063     QString path;
00064     QString contentsPrefix;
00065     QMap<QByteArray, ContentStructure> contents;
00066     QStringList mimetypes;
00067     static QHash<QString, PackageStructure::Ptr> structures;
00068  };
00069 
00070 QHash<QString, PackageStructure::Ptr> PackageStructurePrivate::structures;
00071 
00072 PackageStructure::PackageStructure(QObject *parent, const QString &type)
00073     : QObject(parent),
00074       d(new PackageStructurePrivate)
00075 {
00076     d->type = type;
00077     d->contentsPrefix = "contents/";
00078 }
00079 
00080 PackageStructure::~PackageStructure()
00081 {
00082     delete d;
00083 }
00084 
00085 PackageStructure::Ptr PackageStructure::load(const QString &packageFormat)
00086 {
00087     if (packageFormat.isEmpty()) {
00088         return Ptr(new PackageStructure());
00089     }
00090 
00091     PackageStructure::Ptr structure = PackageStructurePrivate::structures[packageFormat];
00092 
00093     if (structure) {
00094         return structure;
00095     }
00096 
00097     // first we check for plugins in sycoca
00098     QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg(packageFormat);
00099     KService::List offers = KServiceTypeTrader::self()->query("Plasma/PackageStructure", constraint);
00100 
00101     QVariantList args;
00102     QString error;
00103     foreach (const KService::Ptr &offer, offers) {
00104         PackageStructure::Ptr structure(offer->createInstance<Plasma::PackageStructure>(0, args, &error));
00105 
00106         if (structure) {
00107             return structure;
00108         }
00109 
00110         kDebug() << "Couldn't load PackageStructure for" << packageFormat << "! reason given: " << error;
00111     }
00112 
00113     // if that didn't give us any love, then we try to load from a config file
00114     structure = new PackageStructure();
00115     QString configPath("plasma/packageformats/%1rc");
00116     configPath = KStandardDirs::locate("data", configPath.arg(packageFormat));
00117 
00118     if (!configPath.isEmpty()) {
00119         KConfig config(configPath);
00120         structure->read(&config);
00121         PackageStructurePrivate::structures[packageFormat] = structure;
00122     }
00123 
00124     return structure;
00125 }
00126 
00127 PackageStructure& PackageStructure::operator=(const PackageStructure& rhs)
00128 {
00129     if (this == &rhs) {
00130         return *this;
00131     }
00132 
00133     *d = *rhs.d;
00134     return *this;
00135 }
00136 
00137 QString PackageStructure::type() const
00138 {
00139     return d->type;
00140 }
00141 
00142 QList<const char*> PackageStructure::directories() const
00143 {
00144     QList<const char*> dirs;
00145     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
00146     while (it != d->contents.constEnd()) {
00147         if (it.value().directory) {
00148             dirs << it.key();
00149         }
00150         ++it;
00151     }
00152     return dirs;
00153 }
00154 
00155 QList<const char*> PackageStructure::requiredDirectories() const
00156 {
00157     QList<const char*> dirs;
00158     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
00159     while (it != d->contents.constEnd()) {
00160         if (it.value().directory &&
00161             it.value().required) {
00162             dirs << it.key();
00163         }
00164         ++it;
00165     }
00166     return dirs;
00167 }
00168 
00169 QList<const char*> PackageStructure::files() const
00170 {
00171     QList<const char*> files;
00172     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
00173     while (it != d->contents.constEnd()) {
00174         if (!it.value().directory) {
00175             files << it.key();
00176         }
00177         ++it;
00178     }
00179     return files;
00180 }
00181 
00182 QList<const char*> PackageStructure::requiredFiles() const
00183 {
00184     QList<const char*> files;
00185     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
00186     while (it != d->contents.constEnd()) {
00187         if (!it.value().directory && it.value().required) {
00188             files << it.key();
00189         }
00190         ++it;
00191     }
00192     return files;
00193 }
00194 
00195 void PackageStructure::addDirectoryDefinition(const char* key, const QString& path, const QString& name)
00196 {
00197     ContentStructure s;
00198     s.name = name;
00199     s.path = path;
00200     s.directory = true;
00201 
00202     d->contents[key] = s;
00203 }
00204 
00205 void PackageStructure::addFileDefinition(const char* key, const QString& path, const QString& name)
00206 {
00207     ContentStructure s;
00208     s.name = name;
00209     s.path = path;
00210     s.directory = false;
00211 
00212     d->contents[key] = s;
00213 }
00214 
00215 QString PackageStructure::path(const char* key) const
00216 {
00217     //kDebug() << "looking for" << key;
00218     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.find(key);
00219     if (it == d->contents.constEnd()) {
00220         return QString();
00221     }
00222 
00223     //kDebug() << "found" << key << "and the value is" << it.value().path;
00224     return it.value().path;
00225 }
00226 
00227 QString PackageStructure::name(const char* key) const
00228 {
00229     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.find(key);
00230     if (it == d->contents.constEnd()) {
00231         return QString();
00232     }
00233 
00234     return it.value().name;
00235 }
00236 
00237 void PackageStructure::setRequired(const char* key, bool required)
00238 {
00239     QMap<QByteArray, ContentStructure>::iterator it = d->contents.find(key);
00240     if (it == d->contents.end()) {
00241         return;
00242     }
00243 
00244     it.value().required = required;
00245 }
00246 
00247 bool PackageStructure::isRequired(const char* key) const
00248 {
00249     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.find(key);
00250     if (it == d->contents.constEnd()) {
00251         return false;
00252     }
00253 
00254     return it.value().required;
00255 }
00256 
00257 void PackageStructure::setDefaultMimetypes(QStringList mimetypes)
00258 {
00259     d->mimetypes = mimetypes;
00260 }
00261 
00262 void PackageStructure::setMimetypes(const char* key, QStringList mimetypes)
00263 {
00264     QMap<QByteArray, ContentStructure>::iterator it = d->contents.find(key);
00265     if (it == d->contents.end()) {
00266         return;
00267     }
00268 
00269     it.value().mimetypes = mimetypes;
00270 }
00271 
00272 QStringList PackageStructure::mimetypes(const char* key) const
00273 {
00274     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.find(key);
00275     if (it == d->contents.constEnd()) {
00276         return QStringList();
00277     }
00278 
00279     if (it.value().mimetypes.isEmpty()) {
00280         return d->mimetypes;
00281     }
00282 
00283     return it.value().mimetypes;
00284 }
00285 
00286 void PackageStructure::setPath(const QString &path)
00287 {
00288     d->path = path;
00289     pathChanged();
00290 }
00291 
00292 QString PackageStructure::path() const
00293 {
00294     return d->path;
00295 }
00296 
00297 void PackageStructure::pathChanged()
00298 {
00299     // default impl does nothing, this is a hook for subclasses.
00300 }
00301 
00302 void PackageStructure::read(const KConfigBase *config)
00303 {
00304     d->contents.clear();
00305     d->mimetypes.clear();
00306     d->type = config->group("").readEntry("Type", QString()); 
00307 
00308     QStringList groups = config->groupList();
00309     foreach (const QString &group, groups) {
00310         QByteArray key = group.toAscii();
00311         KConfigGroup entry = config->group(group);
00312 
00313         QString path = entry.readEntry("Path", QString());
00314         QString name = entry.readEntry("Name", QString());
00315         QStringList mimetypes = entry.readEntry("Mimetypes", QStringList());
00316         bool directory = entry.readEntry("Directory", false);
00317         bool required = entry.readEntry("Required", false);
00318 
00319         if (directory) {
00320             addDirectoryDefinition(key, path, name);
00321         } else {
00322             addFileDefinition(key, path, name);
00323         }
00324 
00325         setMimetypes(key, mimetypes);
00326         setRequired(key, required);
00327     }
00328 }
00329 
00330 void PackageStructure::write(KConfigBase *config) const
00331 {
00332     config->group("").writeEntry("Type", type());
00333 
00334     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
00335     while (it != d->contents.constEnd()) {
00336         KConfigGroup group = config->group(it.key());
00337         group.writeEntry("Path", it.value().path);
00338         group.writeEntry("Name", it.value().name);
00339         if (!it.value().mimetypes.isEmpty()) {
00340             group.writeEntry("Mimetypes", it.value().mimetypes);
00341         }
00342         if (it.value().directory) {
00343             group.writeEntry("Directory", true);
00344         }
00345         if (it.value().required) {
00346             group.writeEntry("Required", true);
00347         }
00348 
00349         ++it;
00350     }
00351 }
00352 
00353 QString PackageStructure::contentsPrefix() const
00354 {
00355     return d->contentsPrefix;
00356 }
00357 
00358 void PackageStructure::setContentsPrefix(const QString &prefix)
00359 {
00360     d->contentsPrefix = prefix;
00361 }
00362 
00363 bool PackageStructure::installPackage(const QString &package, const QString &packageRoot)
00364 {
00365     return Package::installPackage(package, packageRoot);
00366 }
00367 
00368 bool PackageStructure::uninstallPackage(const QString &packageName, const QString &packageRoot)
00369 {
00370     //TODO: implement
00371     return false;
00372 }
00373 
00374 } // Plasma namespace
00375 
00376 #include "packagestructure.moc"
00377 

libplasma

Skip menu "libplasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal