KCMUtils

kcmoduleinfo.cpp
1 /*
2  This file is part of the KDE project
3  SPDX-FileCopyrightText: 1999 Matthias Hoelzer-Kluepfel <[email protected]>
4  SPDX-FileCopyrightText: 2000 Matthias Elter <[email protected]>
5  SPDX-FileCopyrightText: 2003 Daniel Molkentin <[email protected]>
6  SPDX-FileCopyrightText: 2003, 2006 Matthias Kretz <[email protected]>
7 
8  SPDX-License-Identifier: LGPL-2.0-only
9 */
10 
11 #include "kcmoduleinfo.h"
12 
13 #if KCMUTILS_BUILD_DEPRECATED_SINCE(5, 88)
14 
15 #include <kcmutils_debug.h>
16 
17 #include <QVariant>
18 
19 #include <KDesktopFile>
20 
21 #include <KLocalizedString>
22 #include <KPluginInfo>
23 
24 class Q_DECL_HIDDEN KCModuleInfo::Private
25 {
26 public:
27  Private();
28  Private(const KPluginInfo &);
29  Private(const KService::Ptr &);
30 
31  QStringList keywords;
32  QString name, icon, lib, handle, fileName, doc, comment;
33  bool allLoaded = false;
34  int weight = 100;
35 
36  // For real C++ plugins
37  KPluginInfo pluginInfo;
38 
39  // Can be a C++ plugin, or just a desktop file launching an executable (see autotest)
40  KService::Ptr service;
41 
42  /**
43  * Reads the service entries specific for KCModule from the desktop file.
44  * The usual desktop entries are read in the Private ctor.
45  */
46  void loadAll();
47 };
48 
49 KCModuleInfo::Private::Private()
50 {
51 }
52 
53 KCModuleInfo::Private::Private(const KPluginInfo &pluginInfo)
54  : allLoaded(false)
55  , pluginInfo(pluginInfo)
56 {
57  if (!pluginInfo.isValid()) {
58  qCWarning(KCMUTILS_LOG) << "Invalid plugin";
59  return;
60  }
61 
62  // set the modules simple attributes
63  name = pluginInfo.name();
64  comment = pluginInfo.comment();
65  icon = pluginInfo.icon();
66  fileName = pluginInfo.entryPath();
67  lib = pluginInfo.libraryPath();
68  keywords = pluginInfo.property(QStringLiteral("Keywords")).toStringList();
69 }
70 
71 KCModuleInfo::Private::Private(const KService::Ptr &service)
72  : allLoaded(false)
73  , pluginInfo()
74  , service(service)
75 {
76  if (!service) {
77  return;
78  }
79 
80  name = service->name();
81  comment = service->comment();
82  icon = service->icon();
83  fileName = service->entryPath();
84  lib = service->library();
85  keywords = service->keywords();
86 }
87 
89  : d(new Private)
90 {
91 }
92 
94  : d(new Private(KService::serviceByStorageId(desktopFile)))
95 {
96 }
97 
99  : d(new Private(service))
100 {
101 }
102 
104  : d(new Private(pluginInfo))
105 {
106 }
107 
109  : d(new Private)
110 {
111  (*this) = rhs;
112 }
113 
115 {
116  *d = *(rhs.d);
117  return *this;
118 }
119 
121 {
122  return ((d->name == rhs.d->name) && (d->lib == rhs.d->lib) && (d->fileName == rhs.d->fileName));
123 }
124 
126 {
127  return !operator==(rhs);
128 }
129 
131 {
132  delete d;
133 }
134 
136 {
137  return d->pluginInfo.isValid() || d->service;
138 }
139 
140 void KCModuleInfo::Private::loadAll()
141 {
142  allLoaded = true;
143 
144  if (!pluginInfo.isValid() && !service) { /* We have a bogus service. All get functions will return empty/zero values */
145  return;
146  }
147 
148  if (service) {
149  // get the documentation path
150  doc = service->property(QStringLiteral("X-DocPath"), QVariant::String).toString();
151  if (doc.isEmpty()) {
152  doc = service->property(QStringLiteral("DocPath"), QVariant::String).toString();
153  }
154 
155  // read weight
156  QVariant tmp = service->property(QStringLiteral("X-KDE-Weight"), QVariant::Int);
157  weight = tmp.isValid() ? tmp.toInt() : 100;
158 
159  // factory handle
160  tmp = service->property(QStringLiteral("X-KDE-FactoryName"), QVariant::String);
161  handle = tmp.isValid() ? tmp.toString() : lib;
162  } else {
163  // get the documentation path
164  doc = pluginInfo.property(QStringLiteral("X-DocPath")).toString();
165  if (doc.isEmpty()) {
166  doc = pluginInfo.property(QStringLiteral("DocPath")).toString();
167  }
168 
169  // read weight
170  QVariant tmp = pluginInfo.property(QStringLiteral("X-KDE-Weight")).toInt();
171  weight = tmp.isValid() ? tmp.toInt() : 100;
172 
173  // factory handle
174  tmp = pluginInfo.property(QStringLiteral("X-KDE-FactoryName"));
175  handle = tmp.isValid() ? tmp.toString() : lib;
176  }
177 }
178 
180 {
181  return d->fileName;
182 }
183 
185 {
186  return d->keywords;
187 }
188 
190 {
191  return d->name;
192 }
193 
195 {
196  if (d->service) {
197  return d->service;
198  }
199  if (!d->pluginInfo.isValid()) {
200  return {};
201  }
202  QT_WARNING_PUSH
203  QT_WARNING_DISABLE_CLANG("-Wdeprecated-declarations")
204  QT_WARNING_DISABLE_GCC("-Wdeprecated-declarations")
205  return d->pluginInfo.service();
206  QT_WARNING_POP
207 }
208 
210 {
211  return d->pluginInfo;
212 }
213 
215 {
216  return d->comment;
217 }
218 
220 {
221  return d->icon;
222 }
223 
225 {
226  return d->lib;
227 }
228 
230 {
231  if (!d->allLoaded) {
232  d->loadAll();
233  }
234 
235  return d->doc;
236 }
237 
238 #if KCMUTILS_BUILD_DEPRECATED_SINCE(5, 85)
240 {
241  if (!d->allLoaded) {
242  d->loadAll();
243  }
244 
245  return d->handle;
246 }
247 #endif
248 
250 {
251  if (!d->allLoaded) {
252  d->loadAll();
253  }
254 
255  return d->weight;
256 }
257 
259 {
260  if (d->service) {
261  return d->service->property(key);
262  } else {
263  return d->pluginInfo.property(key);
264  }
265 }
266 
267 #endif
bool isValid() const const
QString libraryPath() const
bool operator!=(const KCModuleInfo &rhs) const
KCModuleInfo & operator=(const KCModuleInfo &rhs)
Assignment operator.
QString icon() const
QString comment() const
bool operator==(const KCModuleInfo &rhs) const
Returns true if rhs describes the same KCModule as this object.
KService::Ptr service() const
int weight() const
QString icon() const
KPluginInfo pluginInfo() const
QString entryPath() const
int toInt(bool *ok) const const
QString moduleName() const
QVariant property(const QString &key) const
QVariant property(const QString &key) const
bool isValid() const
QString docPath() const
QString name() const
KCModuleInfo()
Same as above but creates an empty KCModuleInfo.
const char * name(StandardAction id)
QString fileName() const
bool isValid() const
Returns true if the KCM was found.
QString comment() const
QStringList toStringList() const const
QString library() const
A class that provides information about a KCModule.
Definition: kcmoduleinfo.h:36
~KCModuleInfo()
Default destructor.
QString handle() const
QString toString() const const
QStringList keywords() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sun Jun 26 2022 03:51:23 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.