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

Plasma

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 #include <QFileInfo>
00024 
00025 #include <kconfiggroup.h>
00026 #include <kstandarddirs.h>
00027 #include <kservicetypetrader.h>
00028 #include <kurl.h>
00029 #include <ktemporaryfile.h>
00030 #include <ktempdir.h>
00031 #include <kzip.h>
00032 #include <kio/netaccess.h>
00033 #include <kio/job.h>
00034 
00035 #include "package.h"
00036 
00037 namespace Plasma
00038 {
00039 
00040 class ContentStructure
00041 {
00042     public:
00043         ContentStructure()
00044             : directory(false),
00045               required(false)
00046         {
00047         }
00048 
00049         ContentStructure(const ContentStructure &other)
00050         {
00051             path = other.path;
00052             name = other.name;
00053             mimetypes = other.mimetypes;
00054             directory = other.directory;
00055             required = other.required;
00056         }
00057 
00058         QString path;
00059         QString name;
00060         QStringList mimetypes;
00061         bool directory : 1;
00062         bool required : 1;
00063 };
00064 
00065 class PackageStructurePrivate
00066 {
00067 public:
00068     PackageStructurePrivate()
00069         : metadata(0),
00070           externalPaths(false)
00071     {
00072     }
00073 
00074     ~PackageStructurePrivate()
00075     {
00076         delete metadata;
00077     }
00078 
00079     void createPackageMetadata(const QString &path);
00080 
00081     static QHash<QString, PackageStructure::Ptr> structures;
00082 
00083     QString type;
00084     QString path;
00085     QString contentsPrefix;
00086     QString packageRoot;
00087     QString servicePrefix;
00088     QMap<QByteArray, ContentStructure> contents;
00089     QStringList mimetypes;
00090     PackageMetadata *metadata;
00091     bool externalPaths;
00092  };
00093 
00094 QHash<QString, PackageStructure::Ptr> PackageStructurePrivate::structures;
00095 
00096 PackageStructure::PackageStructure(QObject *parent, const QString &type)
00097     : QObject(parent),
00098       d(new PackageStructurePrivate)
00099 {
00100     d->type = type;
00101     d->contentsPrefix = "contents/";
00102     d->packageRoot = "plasma/plasmoids/";
00103     d->servicePrefix = "plasma-applet-";
00104 }
00105 
00106 PackageStructure::~PackageStructure()
00107 {
00108     delete d;
00109 }
00110 
00111 PackageStructure::Ptr PackageStructure::load(const QString &packageFormat)
00112 {
00113     if (packageFormat.isEmpty()) {
00114         return Ptr(new PackageStructure());
00115     }
00116 
00117     PackageStructure::Ptr structure = PackageStructurePrivate::structures[packageFormat];
00118 
00119     if (structure) {
00120         return structure;
00121     }
00122 
00123     // first we check for plugins in sycoca
00124     QString constraint = QString("[X-KDE-PluginInfo-Name] == '%1'").arg(packageFormat);
00125     KService::List offers =
00126         KServiceTypeTrader::self()->query("Plasma/PackageStructure", constraint);
00127 
00128     QVariantList args;
00129     QString error;
00130     foreach (const KService::Ptr &offer, offers) {
00131         PackageStructure::Ptr structure(
00132             offer->createInstance<Plasma::PackageStructure>(0, args, &error));
00133 
00134         if (structure) {
00135             return structure;
00136         }
00137 
00138         kDebug() << "Couldn't load PackageStructure for" << packageFormat
00139                  << "! reason given: " << error;
00140     }
00141 
00142     // if that didn't give us any love, then we try to load from a config file
00143     structure = new PackageStructure();
00144     QString configPath("plasma/packageformats/%1rc");
00145     configPath = KStandardDirs::locate("data", configPath.arg(packageFormat));
00146 
00147     if (!configPath.isEmpty()) {
00148         KConfig config(configPath);
00149         structure->read(&config);
00150         PackageStructurePrivate::structures[packageFormat] = structure;
00151         return structure;
00152     }
00153 
00154     // try to load from absolute file path
00155     KUrl url(packageFormat);
00156     if (url.isLocalFile()) {
00157         KConfig config(KIO::NetAccess::mostLocalUrl(url, NULL).path(), KConfig::SimpleConfig);
00158         structure->read(&config);
00159         PackageStructurePrivate::structures[structure->type()] = structure;
00160     } else {
00161         KTemporaryFile tmp;
00162         if (tmp.open()) {
00163             KIO::Job *job = KIO::file_copy(url, KUrl(tmp.fileName()),
00164                                            -1, KIO::Overwrite | KIO::HideProgressInfo);
00165             if (job->exec()) {
00166                 KConfig config(tmp.fileName(), KConfig::SimpleConfig);
00167                 structure->read(&config);
00168                 PackageStructurePrivate::structures[structure->type()] = structure;
00169             }
00170         }
00171     }
00172 
00173     return structure;
00174 }
00175 
00176 PackageStructure &PackageStructure::operator=(const PackageStructure &rhs)
00177 {
00178     if (this == &rhs) {
00179         return *this;
00180     }
00181 
00182     *d = *rhs.d;
00183     return *this;
00184 }
00185 
00186 QString PackageStructure::type() const
00187 {
00188     return d->type;
00189 }
00190 
00191 QList<const char*> PackageStructure::directories() const
00192 {
00193     QList<const char*> dirs;
00194     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
00195     while (it != d->contents.constEnd()) {
00196         if (it.value().directory) {
00197             dirs << it.key();
00198         }
00199         ++it;
00200     }
00201     return dirs;
00202 }
00203 
00204 QList<const char*> PackageStructure::requiredDirectories() const
00205 {
00206     QList<const char*> dirs;
00207     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
00208     while (it != d->contents.constEnd()) {
00209         if (it.value().directory &&
00210             it.value().required) {
00211             dirs << it.key();
00212         }
00213         ++it;
00214     }
00215     return dirs;
00216 }
00217 
00218 QList<const char*> PackageStructure::files() const
00219 {
00220     QList<const char*> files;
00221     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
00222     while (it != d->contents.constEnd()) {
00223         if (!it.value().directory) {
00224             files << it.key();
00225         }
00226         ++it;
00227     }
00228     return files;
00229 }
00230 
00231 QList<const char*> PackageStructure::requiredFiles() const
00232 {
00233     QList<const char*> files;
00234     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
00235     while (it != d->contents.constEnd()) {
00236         if (!it.value().directory && it.value().required) {
00237             files << it.key();
00238         }
00239         ++it;
00240     }
00241     return files;
00242 }
00243 
00244 void PackageStructure::addDirectoryDefinition(const char *key,
00245                                               const QString &path, const QString &name)
00246 {
00247     ContentStructure s;
00248     s.name = name;
00249     s.path = path;
00250     s.directory = true;
00251 
00252     d->contents[key] = s;
00253 }
00254 
00255 void PackageStructure::addFileDefinition(const char *key, const QString &path, const QString &name)
00256 {
00257     ContentStructure s;
00258     s.name = name;
00259     s.path = path;
00260     s.directory = false;
00261 
00262     d->contents[key] = s;
00263 }
00264 
00265 QString PackageStructure::path(const char *key) const
00266 {
00267     //kDebug() << "looking for" << key;
00268     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
00269     if (it == d->contents.constEnd()) {
00270         return QString();
00271     }
00272 
00273     //kDebug() << "found" << key << "and the value is" << it.value().path;
00274     return it.value().path;
00275 }
00276 
00277 QString PackageStructure::name(const char *key) const
00278 {
00279     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
00280     if (it == d->contents.constEnd()) {
00281         return QString();
00282     }
00283 
00284     return it.value().name;
00285 }
00286 
00287 void PackageStructure::setRequired(const char *key, bool required)
00288 {
00289     QMap<QByteArray, ContentStructure>::iterator it = d->contents.find(key);
00290     if (it == d->contents.end()) {
00291         return;
00292     }
00293 
00294     it.value().required = required;
00295 }
00296 
00297 bool PackageStructure::isRequired(const char *key) const
00298 {
00299     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
00300     if (it == d->contents.constEnd()) {
00301         return false;
00302     }
00303 
00304     return it.value().required;
00305 }
00306 
00307 void PackageStructure::setDefaultMimetypes(QStringList mimetypes)
00308 {
00309     d->mimetypes = mimetypes;
00310 }
00311 
00312 void PackageStructure::setMimetypes(const char *key, QStringList mimetypes)
00313 {
00314     QMap<QByteArray, ContentStructure>::iterator it = d->contents.find(key);
00315     if (it == d->contents.end()) {
00316         return;
00317     }
00318 
00319     it.value().mimetypes = mimetypes;
00320 }
00321 
00322 QStringList PackageStructure::mimetypes(const char *key) const
00323 {
00324     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
00325     if (it == d->contents.constEnd()) {
00326         return QStringList();
00327     }
00328 
00329     if (it.value().mimetypes.isEmpty()) {
00330         return d->mimetypes;
00331     }
00332 
00333     return it.value().mimetypes;
00334 }
00335 
00336 void PackageStructure::setPath(const QString &path)
00337 {
00338     if (d->path == path) {
00339         return;
00340     }
00341 
00342     d->path = path;
00343     delete d->metadata;
00344     d->metadata = 0;
00345     pathChanged();
00346 }
00347 
00348 QString PackageStructure::path() const
00349 {
00350     return d->path;
00351 }
00352 
00353 void PackageStructure::pathChanged()
00354 {
00355     // default impl does nothing, this is a hook for subclasses.
00356 }
00357 
00358 void PackageStructure::read(const KConfigBase *config)
00359 {
00360     d->contents.clear();
00361     d->mimetypes.clear();
00362     d->type = config->group("").readEntry("Type", QString());
00363 
00364     QStringList groups = config->groupList();
00365     foreach (const QString &group, groups) {
00366         QByteArray key = group.toAscii();
00367         KConfigGroup entry = config->group(group);
00368 
00369         QString path = entry.readEntry("Path", QString());
00370         QString name = entry.readEntry("Name", QString());
00371         QStringList mimetypes = entry.readEntry("Mimetypes", QStringList());
00372         bool directory = entry.readEntry("Directory", false);
00373         bool required = entry.readEntry("Required", false);
00374 
00375         if (directory) {
00376             addDirectoryDefinition(key, path, name);
00377         } else {
00378             addFileDefinition(key, path, name);
00379         }
00380 
00381         setMimetypes(key, mimetypes);
00382         setRequired(key, required);
00383     }
00384 }
00385 
00386 void PackageStructure::write(KConfigBase *config) const
00387 {
00388     config->group("").writeEntry("Type", type());
00389 
00390     QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
00391     while (it != d->contents.constEnd()) {
00392         KConfigGroup group = config->group(it.key());
00393         group.writeEntry("Path", it.value().path);
00394         group.writeEntry("Name", it.value().name);
00395         if (!it.value().mimetypes.isEmpty()) {
00396             group.writeEntry("Mimetypes", it.value().mimetypes);
00397         }
00398         if (it.value().directory) {
00399             group.writeEntry("Directory", true);
00400         }
00401         if (it.value().required) {
00402             group.writeEntry("Required", true);
00403         }
00404 
00405         ++it;
00406     }
00407 }
00408 
00409 QString PackageStructure::contentsPrefix() const
00410 {
00411     return d->contentsPrefix;
00412 }
00413 
00414 void PackageStructure::setContentsPrefix(const QString &prefix)
00415 {
00416     d->contentsPrefix = prefix;
00417 }
00418 
00419 bool PackageStructure::installPackage(const QString &package, const QString &packageRoot)
00420 {
00421     return Package::installPackage(package, packageRoot, d->servicePrefix);
00422 }
00423 
00424 bool PackageStructure::uninstallPackage(const QString &packageName, const QString &packageRoot)
00425 {
00426     return Package::uninstallPackage(packageName, packageRoot, d->servicePrefix);
00427 }
00428 
00429 void PackageStructure::createNewWidgetBrowser(QWidget *parent)
00430 {
00431     Q_UNUSED(parent)
00432     emit newWidgetBrowserFinished();
00433 }
00434 
00435 QString PackageStructure::defaultPackageRoot() const
00436 {
00437     return d->packageRoot;
00438 }
00439 
00440 QString PackageStructure::servicePrefix() const
00441 {
00442     return d->servicePrefix;
00443 }
00444 
00445 void PackageStructure::setDefaultPackageRoot(const QString &packageRoot)
00446 {
00447     d->packageRoot = packageRoot;
00448 }
00449 
00450 void PackageStructure::setServicePrefix(const QString &servicePrefix)
00451 {
00452     d->servicePrefix = servicePrefix;
00453 }
00454 
00455 void PackageStructurePrivate::createPackageMetadata(const QString &path)
00456 {
00457     if (metadata) {
00458         delete metadata;
00459         metadata = 0;
00460     }
00461 
00462     QString metadataPath(path + "/metadata.desktop");
00463     if (!QFile::exists(metadataPath)) {
00464         metadataPath.clear();
00465         kWarning() << "No metadata file in the package, expected it at:" << metadataPath;
00466     }
00467 
00468     metadata = new PackageMetadata(metadataPath);
00469 }
00470 
00471 PackageMetadata PackageStructure::metadata()
00472 {
00473     if (!d->metadata && !d->path.isEmpty()) {
00474         QFileInfo fileInfo(d->path);
00475 
00476         if (fileInfo.isDir()) {
00477             d->createPackageMetadata(d->path);
00478         } else if (fileInfo.exists()) {
00479             KZip archive(d->path);
00480             if (archive.open(QIODevice::ReadOnly)) {
00481                 const KArchiveDirectory *source = archive.directory();
00482                 KTempDir tempdir;
00483                 source->copyTo(tempdir.name());
00484                 d->createPackageMetadata(tempdir.name());
00485             } else {
00486                 kWarning() << "Could not open package file:" << d->path;
00487             }
00488         }
00489     }
00490 
00491     if (!d->metadata) {
00492         d->metadata = new PackageMetadata();
00493     }
00494 
00495     return *d->metadata;
00496 }
00497 
00498 bool PackageStructure::allowExternalPaths() const
00499 {
00500     return d->externalPaths;
00501 }
00502 
00503 void PackageStructure::setAllowExternalPaths(bool allow)
00504 {
00505     d->externalPaths = allow;
00506 }
00507 
00508 } // Plasma namespace
00509 
00510 #include "packagestructure.moc"
00511 

Plasma

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
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