File indexing completed on 2024-04-14 14:18:22

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 1999 Matthias Hoelzer-Kluepfel <hoelzer@kde.org>
0004     SPDX-FileCopyrightText: 2000 Matthias Elter <elter@kde.org>
0005     SPDX-FileCopyrightText: 2003 Daniel Molkentin <molkentin@kde.org>
0006     SPDX-FileCopyrightText: 2003, 2006 Matthias Kretz <kretz@kde.org>
0007 
0008     SPDX-License-Identifier: LGPL-2.0-only
0009 */
0010 
0011 #include "kcmoduleinfo.h"
0012 
0013 #if KCMUTILS_BUILD_DEPRECATED_SINCE(5, 88)
0014 
0015 #include <kcmutils_debug.h>
0016 
0017 #include <QVariant>
0018 
0019 #include <KDesktopFile>
0020 
0021 #include <KLocalizedString>
0022 #include <KPluginInfo>
0023 
0024 class Q_DECL_HIDDEN KCModuleInfo::Private
0025 {
0026 public:
0027     Private();
0028     Private(const KPluginInfo &);
0029     Private(const KService::Ptr &);
0030 
0031     QStringList keywords;
0032     QString name, icon, lib, handle, fileName, doc, comment;
0033     bool allLoaded = false;
0034     int weight = 100;
0035 
0036     // For real C++ plugins
0037     KPluginInfo pluginInfo;
0038 
0039     // Can be a C++ plugin, or just a desktop file launching an executable (see autotest)
0040     KService::Ptr service;
0041 
0042     /**
0043      * Reads the service entries specific for KCModule from the desktop file.
0044      * The usual desktop entries are read in the Private ctor.
0045      */
0046     void loadAll();
0047 };
0048 
0049 KCModuleInfo::Private::Private()
0050 {
0051 }
0052 
0053 KCModuleInfo::Private::Private(const KPluginInfo &pluginInfo)
0054     : allLoaded(false)
0055     , pluginInfo(pluginInfo)
0056 {
0057     if (!pluginInfo.isValid()) {
0058         qCWarning(KCMUTILS_LOG) << "Invalid plugin";
0059         return;
0060     }
0061 
0062     // set the modules simple attributes
0063     name = pluginInfo.name();
0064     comment = pluginInfo.comment();
0065     icon = pluginInfo.icon();
0066     fileName = pluginInfo.entryPath();
0067     lib = pluginInfo.libraryPath();
0068     keywords = pluginInfo.property(QStringLiteral("Keywords")).toStringList();
0069 }
0070 
0071 KCModuleInfo::Private::Private(const KService::Ptr &service)
0072     : allLoaded(false)
0073     , pluginInfo()
0074     , service(service)
0075 {
0076     if (!service) {
0077         return;
0078     }
0079 
0080     name = service->name();
0081     comment = service->comment();
0082     icon = service->icon();
0083     fileName = service->entryPath();
0084     lib = service->library();
0085     keywords = service->keywords();
0086 }
0087 
0088 KCModuleInfo::KCModuleInfo()
0089     : d(new Private)
0090 {
0091 }
0092 
0093 KCModuleInfo::KCModuleInfo(const QString &desktopFile)
0094     : d(new Private(KService::serviceByStorageId(desktopFile)))
0095 {
0096 }
0097 
0098 KCModuleInfo::KCModuleInfo(KService::Ptr service)
0099     : d(new Private(service))
0100 {
0101 }
0102 
0103 KCModuleInfo::KCModuleInfo(const KPluginInfo &pluginInfo)
0104     : d(new Private(pluginInfo))
0105 {
0106 }
0107 
0108 KCModuleInfo::KCModuleInfo(const KCModuleInfo &rhs)
0109     : d(new Private)
0110 {
0111     (*this) = rhs;
0112 }
0113 
0114 KCModuleInfo &KCModuleInfo::operator=(const KCModuleInfo &rhs)
0115 {
0116     *d = *(rhs.d);
0117     return *this;
0118 }
0119 
0120 bool KCModuleInfo::operator==(const KCModuleInfo &rhs) const
0121 {
0122     return ((d->name == rhs.d->name) && (d->lib == rhs.d->lib) && (d->fileName == rhs.d->fileName));
0123 }
0124 
0125 bool KCModuleInfo::operator!=(const KCModuleInfo &rhs) const
0126 {
0127     return !operator==(rhs);
0128 }
0129 
0130 KCModuleInfo::~KCModuleInfo()
0131 {
0132     delete d;
0133 }
0134 
0135 bool KCModuleInfo::isValid() const
0136 {
0137     return d->pluginInfo.isValid() || d->service;
0138 }
0139 
0140 void KCModuleInfo::Private::loadAll()
0141 {
0142     allLoaded = true;
0143 
0144     if (!pluginInfo.isValid() && !service) { /* We have a bogus service. All get functions will return empty/zero values */
0145         return;
0146     }
0147 
0148     if (service) {
0149         // get the documentation path
0150         doc = service->property(QStringLiteral("X-DocPath"), QVariant::String).toString();
0151         if (doc.isEmpty()) {
0152             doc = service->property(QStringLiteral("DocPath"), QVariant::String).toString();
0153         }
0154 
0155         // read weight
0156         QVariant tmp = service->property(QStringLiteral("X-KDE-Weight"), QVariant::Int);
0157         weight = tmp.isValid() ? tmp.toInt() : 100;
0158 
0159         // factory handle
0160         tmp = service->property(QStringLiteral("X-KDE-FactoryName"), QVariant::String);
0161         handle = tmp.isValid() ? tmp.toString() : lib;
0162     } else {
0163         // get the documentation path
0164         doc = pluginInfo.property(QStringLiteral("X-DocPath")).toString();
0165         if (doc.isEmpty()) {
0166             doc = pluginInfo.property(QStringLiteral("DocPath")).toString();
0167         }
0168 
0169         // read weight
0170         QVariant tmp = pluginInfo.property(QStringLiteral("X-KDE-Weight")).toInt();
0171         weight = tmp.isValid() ? tmp.toInt() : 100;
0172 
0173         // factory handle
0174         tmp = pluginInfo.property(QStringLiteral("X-KDE-FactoryName"));
0175         handle = tmp.isValid() ? tmp.toString() : lib;
0176     }
0177 }
0178 
0179 QString KCModuleInfo::fileName() const
0180 {
0181     return d->fileName;
0182 }
0183 
0184 QStringList KCModuleInfo::keywords() const
0185 {
0186     return d->keywords;
0187 }
0188 
0189 QString KCModuleInfo::moduleName() const
0190 {
0191     return d->name;
0192 }
0193 
0194 KService::Ptr KCModuleInfo::service() const
0195 {
0196     if (d->service) {
0197         return d->service;
0198     }
0199     if (!d->pluginInfo.isValid()) {
0200         return {};
0201     }
0202     QT_WARNING_PUSH
0203     QT_WARNING_DISABLE_CLANG("-Wdeprecated-declarations")
0204     QT_WARNING_DISABLE_GCC("-Wdeprecated-declarations")
0205     return d->pluginInfo.service();
0206     QT_WARNING_POP
0207 }
0208 
0209 KPluginInfo KCModuleInfo::pluginInfo() const
0210 {
0211     return d->pluginInfo;
0212 }
0213 
0214 QString KCModuleInfo::comment() const
0215 {
0216     return d->comment;
0217 }
0218 
0219 QString KCModuleInfo::icon() const
0220 {
0221     return d->icon;
0222 }
0223 
0224 QString KCModuleInfo::library() const
0225 {
0226     return d->lib;
0227 }
0228 
0229 QString KCModuleInfo::docPath() const
0230 {
0231     if (!d->allLoaded) {
0232         d->loadAll();
0233     }
0234 
0235     return d->doc;
0236 }
0237 
0238 #if KCMUTILS_BUILD_DEPRECATED_SINCE(5, 85)
0239 QString KCModuleInfo::handle() const
0240 {
0241     if (!d->allLoaded) {
0242         d->loadAll();
0243     }
0244 
0245     return d->handle;
0246 }
0247 #endif
0248 
0249 int KCModuleInfo::weight() const
0250 {
0251     if (!d->allLoaded) {
0252         d->loadAll();
0253     }
0254 
0255     return d->weight;
0256 }
0257 
0258 QVariant KCModuleInfo::property(const QString &key) const
0259 {
0260     if (d->service) {
0261         return d->service->property(key);
0262     } else {
0263         return d->pluginInfo.property(key);
0264     }
0265 }
0266 
0267 #endif