1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
|
/******************************************************************************
* Copyright 2007 by Aaron Seigo <aseigo@kde.org> *
* *
* 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"
#include "config-plasma.h"
#include <QDirIterator>
#include <QDir>
#include <QMap>
#include <QMutableListIterator>
#include <QFileInfo>
#include <kconfiggroup.h>
#include <kdebug.h>
#ifndef PLASMA_NO_KIO
#include <kio/job.h>
#endif
#include <kmimetype.h>
#include <kstandarddirs.h>
#include <kservicetypetrader.h>
#include <ktar.h>
#include <ktemporaryfile.h>
#include <ktempdir.h>
#include <kurl.h>
#include <kzip.h>
#include "package.h"
#include "private/packages_p.h"
#include "theme.h"
namespace Plasma
{
class ContentStructure
{
public:
ContentStructure()
: directory(false),
required(false)
{
}
ContentStructure(const ContentStructure &other)
{
paths = other.paths;
name = other.name;
mimetypes = other.mimetypes;
directory = other.directory;
required = other.required;
}
QStringList paths;
QString name;
QStringList mimetypes;
bool directory : 1;
bool required : 1;
};
class PackageStructurePrivate
{
public:
PackageStructurePrivate(const QString &t)
: type(t),
packageRoot("plasma/plasmoids"),
servicePrefix("plasma-applet-"),
metadata(0),
externalPaths(false)
{
contentsPrefixPaths << "contents/";
}
~PackageStructurePrivate()
{
delete metadata;
}
void createPackageMetadata(const QString &path);
QStringList entryList(const QString &prefix, const QString &requestedPath);
QString type;
QString path;
QStringList contentsPrefixPaths;
QString packageRoot;
QString servicePrefix;
QMap<QByteArray, ContentStructure> contents;
QStringList mimetypes;
PackageMetadata *metadata;
bool externalPaths;
};
PackageStructure::PackageStructure(QObject *parent, const QString &type)
: QObject(parent),
d(new PackageStructurePrivate(type))
{
}
PackageStructure::~PackageStructure()
{
delete d;
}
PackageStructure::Ptr PackageStructure::load(const QString &packageFormat)
{
if (packageFormat.isEmpty()) {
return Ptr(new PackageStructure());
}
PackageStructure::Ptr structure;
if (packageFormat == "Plasma/Applet") {
structure = defaultPackageStructure(AppletComponent);
structure->d->type = "Plasma/Applet";
} else if (packageFormat == "Plasma/DataEngine") {
structure = defaultPackageStructure(DataEngineComponent);
structure->d->type = "Plasma/DataEngine";
} else if (packageFormat == "Plasma/Runner") {
structure = defaultPackageStructure(RunnerComponent);
structure->d->type = "Plasma/Runner";
} else if (packageFormat == "Plasma/Wallpaper") {
structure = defaultPackageStructure(WallpaperComponent);
structure->d->type = "Plasma/Wallpaper";
} else if (packageFormat == "Plasma/Theme") {
structure = Theme::packageStructure();
structure->d->type = "Plasma/Theme";
} else if (packageFormat == "Plasma/Generic") {
structure = defaultPackageStructure(GenericComponent);
structure->d->type = "Plasma/Generic";
structure->setDefaultPackageRoot("plasma/packages/");
}
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();
QString configPath("plasma/packageformats/%1rc");
configPath = KStandardDirs::locate("data", configPath.arg(packageFormat));
if (!configPath.isEmpty()) {
KConfig config(configPath);
structure->read(&config);
return structure;
}
// try to load from absolute file path
KUrl url(packageFormat);
if (url.isLocalFile()) {
KConfig config(url.toLocalFile(), KConfig::SimpleConfig);
structure->read(&config);
}
#ifndef PLASMA_NO_KIO
else {
KTemporaryFile tmp;
if (tmp.open()) {
KIO::Job *job = KIO::file_copy(url, KUrl(tmp.fileName()),
-1, KIO::Overwrite | KIO::HideProgressInfo);
if (job->exec()) {
KConfig config(tmp.fileName(), KConfig::SimpleConfig);
structure->read(&config);
}
}
}
#endif
return structure;
}
PackageStructure &PackageStructure::operator=(const PackageStructure &rhs)
{
if (this == &rhs) {
return *this;
}
*d = *rhs.d;
return *this;
}
QString PackageStructure::type() const
{
return d->type;
}
QList<const char*> PackageStructure::directories() const
{
QList<const char*> dirs;
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
while (it != d->contents.constEnd()) {
if (it.value().directory) {
dirs << it.key();
}
++it;
}
return dirs;
}
QList<const char*> PackageStructure::requiredDirectories() const
{
QList<const char*> dirs;
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
while (it != d->contents.constEnd()) {
if (it.value().directory &&
it.value().required) {
dirs << it.key();
}
++it;
}
return dirs;
}
QList<const char*> PackageStructure::files() const
{
QList<const char*> files;
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
while (it != d->contents.constEnd()) {
if (!it.value().directory) {
files << it.key();
}
++it;
}
return files;
}
QList<const char*> PackageStructure::requiredFiles() const
{
QList<const char*> files;
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constBegin();
while (it != d->contents.constEnd()) {
if (!it.value().directory && it.value().required) {
files << it.key();
}
++it;
}
return files;
}
QStringList PackageStructure::entryList(const char *key)
{
QString p = path(key);
if (p.isEmpty()) {
return QStringList();
}
QStringList list;
if (d->contentsPrefixPaths.isEmpty()) {
// no prefixes is the same as d->contentsPrefixPths with QStringList() << QString()
list << d->entryList(QString(), p);
} else {
foreach (QString prefix, d->contentsPrefixPaths) {
list << d->entryList(prefix, p);
}
}
return list;
}
QStringList PackageStructurePrivate::entryList(const QString &prefix, const QString &requestedPath)
{
QDir dir(path + prefix + requestedPath);
if (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(path)) {
return dir.entryList(QDir::Files | QDir::Readable);
}
return QStringList();
}
void PackageStructure::addDirectoryDefinition(const char *key,
const QString &path, const QString &name)
{
ContentStructure s;
if (d->contents.contains(key)) {
s = d->contents[key];
}
if (!name.isEmpty()) {
s.name = name;
}
s.paths.append(path);
s.directory = true;
d->contents[key] = s;
}
void PackageStructure::addFileDefinition(const char *key, const QString &path, const QString &name)
{
ContentStructure s;
if (d->contents.contains(key)) {
s = d->contents[key];
}
if (!name.isEmpty()) {
s.name = name;
}
s.paths.append(path);
s.directory = false;
d->contents[key] = s;
}
void PackageStructure::removeDefinition(const char *key)
{
d->contents.remove(key);
}
QString PackageStructure::path(const char *key) const
{
//kDebug() << "looking for" << key;
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
if (it == d->contents.constEnd()) {
return QString();
}
//kDebug() << "found" << key << "and the value is" << it.value().paths.first();
return it.value().paths.first();
}
QStringList PackageStructure::searchPath(const char *key) const
{
//kDebug() << "looking for" << key;
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
if (it == d->contents.constEnd()) {
return QStringList();
}
//kDebug() << "found" << key << "and the value is" << it.value().paths;
return it.value().paths;
}
QString PackageStructure::name(const char *key) const
{
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
if (it == d->contents.constEnd()) {
return QString();
}
return it.value().name;
}
void PackageStructure::setRequired(const char *key, bool required)
{
QMap<QByteArray, ContentStructure>::iterator it = d->contents.find(key);
if (it == d->contents.end()) {
return;
}
it.value().required = required;
}
bool PackageStructure::isRequired(const char *key) const
{
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
if (it == d->contents.constEnd()) {
return false;
}
return it.value().required;
}
void PackageStructure::setDefaultMimetypes(QStringList mimetypes)
{
d->mimetypes = mimetypes;
}
void PackageStructure::setMimetypes(const char *key, QStringList mimetypes)
{
QMap<QByteArray, ContentStructure>::iterator it = d->contents.find(key);
if (it == d->contents.end()) {
return;
}
it.value().mimetypes = mimetypes;
}
QStringList PackageStructure::mimetypes(const char *key) const
{
QMap<QByteArray, ContentStructure>::const_iterator it = d->contents.constFind(key);
if (it == d->contents.constEnd()) {
return QStringList();
}
if (it.value().mimetypes.isEmpty()) {
return d->mimetypes;
}
return it.value().mimetypes;
}
void PackageStructure::setPath(const QString &path)
{
KUrl url(path);
QDir dir(url.toLocalFile());
QString basePath = dir.canonicalPath();
bool valid = QFile::exists(basePath);
if (valid) {
QFileInfo info(basePath);
if (info.isDir() && !basePath.endsWith('/')) {
basePath.append('/');
}
//kDebug() << "basePath is" << basePath;
} else {
kDebug() << path << "invalid, basePath is" << basePath;
return;
}
if (d->path == basePath) {
return;
}
d->path = basePath;
delete d->metadata;
d->metadata = 0;
pathChanged();
}
QString PackageStructure::path() const
{
return d->path;
}
void PackageStructure::pathChanged()
{
// default impl does nothing, this is a hook for subclasses.
}
void PackageStructure::read(const KConfigBase *config)
{
d->contents.clear();
d->mimetypes.clear();
KConfigGroup general(config, QString());
d->type = general.readEntry("Type", QString());
d->contentsPrefixPaths = general.readEntry("ContentsPrefixPaths", d->contentsPrefixPaths);
d->packageRoot = general.readEntry("DefaultPackageRoot", d->packageRoot);
d->externalPaths = general.readEntry("AllowExternalPaths", d->externalPaths);
QStringList groups = config->groupList();
foreach (const QString &group, groups) {
KConfigGroup entry(config, group);
QByteArray key = group.toLatin1();
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);
if (directory) {
addDirectoryDefinition(key, path, name);
} else {
addFileDefinition(key, path, name);
}
setMimetypes(key, mimetypes);
setRequired(key, required);
}
}
void PackageStructure::write(KConfigBase *config) const
{
KConfigGroup general = KConfigGroup(config, "");
general.writeEntry("Type", type());
general.writeEntry("ContentsPrefixPaths", d->contentsPrefixPaths);
general.writeEntry("DefaultPackageRoot", d->packageRoot);
general.writeEntry("AllowExternalPaths", d->externalPaths);
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().paths);
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);
}
++it;
}
}
QString PackageStructure::contentsPrefix() const
{
return d->contentsPrefixPaths.isEmpty() ? QString() : d->contentsPrefixPaths.first();
}
void PackageStructure::setContentsPrefix(const QString &prefix)
{
d->contentsPrefixPaths.clear();
d->contentsPrefixPaths << prefix;
}
QStringList PackageStructure::contentsPrefixPaths() const
{
return d->contentsPrefixPaths;
}
void PackageStructure::setContentsPrefixPaths(const QStringList &prefixPaths)
{
d->contentsPrefixPaths = prefixPaths;
// the code assumes that the prefixes have a trailing slash
// so let's make that true here
QMutableStringListIterator it(d->contentsPrefixPaths);
while (it.hasNext()) {
it.next();
if (!it.value().endsWith('/')) {
it.setValue(it.value() % '/');
}
}
}
bool PackageStructure::installPackage(const QString &package, const QString &packageRoot)
{
return Package::installPackage(package, packageRoot, d->servicePrefix);
}
bool PackageStructure::uninstallPackage(const QString &packageName, const QString &packageRoot)
{
return Package::uninstallPackage(packageName, packageRoot, d->servicePrefix);
}
void PackageStructure::createNewWidgetBrowser(QWidget *parent)
{
Q_UNUSED(parent)
emit newWidgetBrowserFinished();
}
QString PackageStructure::defaultPackageRoot() const
{
return d->packageRoot;
}
QString PackageStructure::servicePrefix() const
{
return d->servicePrefix;
}
void PackageStructure::setDefaultPackageRoot(const QString &packageRoot)
{
d->packageRoot = packageRoot;
}
void PackageStructure::setServicePrefix(const QString &servicePrefix)
{
d->servicePrefix = servicePrefix;
}
void PackageStructurePrivate::createPackageMetadata(const QString &path)
{
delete metadata;
metadata = 0;
QString metadataPath(path + "/metadata.desktop");
if (!QFile::exists(metadataPath)) {
kWarning() << "No metadata file in the package, expected it at:" << metadataPath;
metadataPath.clear();
}
metadata = new PackageMetadata(metadataPath);
}
//FIXME KDE5: should be const
PackageMetadata PackageStructure::metadata()
{
if (!d->metadata && !d->path.isEmpty()) {
QFileInfo fileInfo(d->path);
if (fileInfo.isDir()) {
d->createPackageMetadata(d->path);
} else if (fileInfo.exists()) {
KArchive *archive = 0;
KMimeType::Ptr mimetype = KMimeType::findByPath(d->path);
if (mimetype->is("application/zip")) {
archive = new KZip(d->path);
} else if (mimetype->is("application/x-compressed-tar") || mimetype->is("application/x-gzip") ||
mimetype->is("application/x-xz-compressed-tar") || mimetype->is("application/x-lzma-compressed-tar") ||
mimetype->is("application/x-tar")|| mimetype->is("application/x-bzip-compressed-tar")) {
archive = new KTar(d->path);
} else {
kWarning() << "Could not open package file, unsupported archive format:" << d->path << mimetype->name();
}
if (archive && archive->open(QIODevice::ReadOnly)) {
const KArchiveDirectory *source = archive->directory();
KTempDir tempdir;
source->copyTo(tempdir.name());
// This is to help with the theme packages, which include an extra folder in their package archive.
// Question: Would it be better to search just the first level dirs for metadata.desktop file?
// As in /path/Theme Name/Dir1/metadata.desktop and not /path/Theme Name/Dir1/Dir2/metadata.desktop
// This fixes bug https://bugs.kde.org/show_bug.cgi?id=149479 properly.
// 2nd rev. Search an extra first directory only.
QDir dir(tempdir.name());
QString filename = "metadata.desktop";
QFileInfo metadataFileInfo(dir, filename);
if (metadataFileInfo.exists()) {
d->createPackageMetadata(metadataFileInfo.absolutePath());
} else {
dir.setFilter(QDir::NoDotAndDotDot|QDir::Dirs);
dir.setSorting(QDir::DirsFirst);
QFileInfo firstDir(dir.entryInfoList().first());
metadataFileInfo = QFileInfo(firstDir.filePath(), filename);
if (metadataFileInfo.exists()) {
kWarning() << "Found in: " << metadataFileInfo.absolutePath();
d->createPackageMetadata(metadataFileInfo.absolutePath());
}
}
} else {
kWarning() << "Could not open package file:" << d->path;
}
delete archive;
}
}
if (!d->metadata) {
d->metadata = new PackageMetadata();
}
return *d->metadata;
}
bool PackageStructure::allowExternalPaths() const
{
return d->externalPaths;
}
void PackageStructure::setAllowExternalPaths(bool allow)
{
d->externalPaths = allow;
}
} // Plasma namespace
#include "packagestructure.moc"
|