KService

kservicetype.cpp
1 /*
2  This file is part of the KDE libraries
3  SPDX-FileCopyrightText: 1999 Waldo Bastian <[email protected]>
4  SPDX-FileCopyrightText: David Faure <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.0-only
7 */
8 
9 #include "kservicetype.h"
10 #include "kservice.h"
11 #include "kservicefactory_p.h"
12 #include "kservicetype_p.h"
13 #include "kservicetypefactory_p.h"
14 #include "kservicetypeprofile.h"
15 #include "ksycoca.h"
16 #include "ksycoca_p.h"
17 #include "servicesdebug.h"
18 #include <KConfigGroup>
19 #include <KDesktopFile>
20 #include <assert.h>
21 
22 extern int servicesDebugArea();
23 
25 template QDataStream &operator<<<QString, QVariant>(QDataStream &, const QMap<QString, QVariant> &);
26 
28  : KSycocaEntry(*new KServiceTypePrivate(config->fileName()))
29 {
31  d->init(config);
32 }
33 
34 void KServiceTypePrivate::init(KDesktopFile *config)
35 {
36  // Q_Q(KServiceType);
37 
38  KConfigGroup desktopGroup = config->desktopGroup();
39  m_strName = desktopGroup.readEntry("X-KDE-ServiceType");
40  m_strComment = desktopGroup.readEntry("Comment");
41  deleted = desktopGroup.readEntry("Hidden", false);
42 
43  // We store this as property to preserve BC, we can't change that
44  // because KSycoca needs to remain BC between KDE 2.x and KDE 3.x
45  QString sDerived = desktopGroup.readEntry("X-KDE-Derived");
46  m_bDerived = !sDerived.isEmpty();
47  if (m_bDerived) {
48  m_mapProps.insert(QStringLiteral("X-KDE-Derived"), sDerived);
49  }
50 
51  const QStringList tmpList = config->groupList();
52  QStringList::const_iterator gIt = tmpList.begin();
53 
54  for (; gIt != tmpList.end(); ++gIt) {
55  if ((*gIt).startsWith(QLatin1String("Property::"))) {
56  KConfigGroup cg(config, *gIt);
57  QVariant v = QVariant::nameToType(cg.readEntry("Type").toLatin1().constData());
58  v = cg.readEntry("Value", v);
59 
60  if (v.isValid()) {
61  m_mapProps.insert((*gIt).mid(10), v);
62  }
63  }
64  }
65 
66  gIt = tmpList.begin();
67  for (; gIt != tmpList.end(); ++gIt) {
68  if ((*gIt).startsWith(QLatin1String("PropertyDef::"))) {
69  KConfigGroup cg(config, *gIt);
70  m_mapPropDefs.insert((*gIt).mid(13), QVariant::nameToType(cg.readEntry("Type").toLatin1().constData()));
71  }
72  }
73 }
74 
75 KServiceType::KServiceType(QDataStream &_str, int offset)
76  : KSycocaEntry(*new KServiceTypePrivate(_str, offset))
77 {
79  d->load(_str);
80 }
81 
82 void KServiceTypePrivate::load(QDataStream &_str)
83 {
84  qint8 b;
85  QString dummy;
86  _str >> m_strName >> dummy >> m_strComment >> m_mapProps >> m_mapPropDefs >> b >> m_serviceOffersOffset;
87  m_bDerived = m_mapProps.contains(QLatin1String("X-KDE-Derived"));
88 }
89 
90 void KServiceTypePrivate::save(QDataStream &_str)
91 {
92  KSycocaEntryPrivate::save(_str);
93  // !! This data structure should remain binary compatible at all times !!
94  // You may add new fields at the end. Make sure to update the version
95  // number in ksycoca.h
96  _str << m_strName << QString() /*was icon*/ << m_strComment << m_mapProps << m_mapPropDefs << qint8(1) << m_serviceOffersOffset;
97 }
98 
99 KServiceType::~KServiceType()
100 {
101 }
102 
104 {
105  const QVariant v = property(QStringLiteral("X-KDE-Derived"));
106  return v.toString();
107 }
108 
109 bool KServiceType::inherits(const QString &servTypeName) const
110 {
111  if (name() == servTypeName) {
112  return true;
113  }
114  QString st = parentServiceType();
115  while (!st.isEmpty()) {
117  if (!ptr) {
118  return false; // error
119  }
120  if (ptr->name() == servTypeName) {
121  return true;
122  }
123  st = ptr->parentServiceType();
124  }
125  return false;
126 }
127 
128 QVariant KServiceTypePrivate::property(const QString &_name) const
129 {
130  QVariant v;
131 
132  if (_name == QLatin1String("Name")) {
133  v = QVariant(m_strName);
134  } else if (_name == QLatin1String("Comment")) {
135  v = QVariant(m_strComment);
136  } else {
137  v = m_mapProps.value(_name);
138  }
139 
140  return v;
141 }
142 
143 QStringList KServiceTypePrivate::propertyNames() const
144 {
145  QStringList res = m_mapProps.keys();
146  res.append(QStringLiteral("Name"));
147  res.append(QStringLiteral("Comment"));
148  return res;
149 }
150 
151 QVariant::Type KServiceType::propertyDef(const QString &_name) const
152 {
153  Q_D(const KServiceType);
154  return static_cast<QVariant::Type>(d->m_mapPropDefs.value(_name, QVariant::Invalid));
155 }
156 
158 {
159  Q_D(const KServiceType);
160  return d->m_mapPropDefs.keys();
161 }
162 
164 {
166  return KSycocaPrivate::self()->serviceTypeFactory()->findServiceTypeByName(_name);
167 }
168 
170 {
172  return KSycocaPrivate::self()->serviceTypeFactory()->allServiceTypes();
173 }
174 
176 {
177  Q_D(KServiceType);
178  if (d->m_parentTypeLoaded) {
179  return d->parentType;
180  }
181 
182  d->m_parentTypeLoaded = true;
183 
184  const QString parentSt = parentServiceType();
185  if (parentSt.isEmpty()) {
186  return KServiceType::Ptr();
187  }
188 
190  d->parentType = KSycocaPrivate::self()->serviceTypeFactory()->findServiceTypeByName(parentSt);
191  if (!d->parentType) {
192  qCWarning(SERVICES) << entryPath() << "specifies undefined MIME type/servicetype" << parentSt;
193  }
194  return d->parentType;
195 }
196 
198 {
199  Q_D(KServiceType);
200  Q_ASSERT(offset != -1);
201  d->m_serviceOffersOffset = offset;
202 }
203 
205 {
206  Q_D(const KServiceType);
207  return d->serviceOffersOffset();
208 }
209 
210 QString KServiceType::comment() const
211 {
212  Q_D(const KServiceType);
213  return d->comment();
214 }
215 
217 {
218  Q_D(const KServiceType);
219  return d->m_bDerived;
220 }
221 
223 {
224  Q_D(const KServiceType);
225  return d->m_mapPropDefs;
226 }
static Ptr serviceType(const QString &_name)
Returns a pointer to the servicetype &#39;_name&#39; or nullptr if the service type is unknown.
static List allServiceTypes()
Returns a list of all the supported servicetypes.
T value() const const
A service type is, well, a type of service, where a service is an application or plugin.
Definition: kservicetype.h:35
KServiceType(KDesktopFile *config)
Construct a service type and take all information from a desktop file.
QVariant::Type propertyDef(const QString &_name) const
Returns the type of the property definition with the given _name.
bool isDerived() const
Checks whether this service type inherits another one.
void append(const T &value)
QString entryPath() const
Base class for all Sycoca entries.
Definition: ksycocaentry.h:32
QVariant::Type nameToType(const char *name)
bool isEmpty() const const
void setServiceOffersOffset(int offset)
QList::iterator end()
int serviceOffersOffset() const
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QExplicitlySharedDataPointer< KServiceType > Ptr
A shared data pointer for KServiceType.
Definition: kservicetype.h:41
QStringList propertyDefNames() const
Returns the list of all property definitions for this servicetype.
QString parentServiceType() const
If this service type inherits from another service type, return the name of the parent.
bool inherits(const QString &servTypeName) const
Checks whether this service type is or inherits from servTypeName.
QStringList groupList() const override
Q_D(Todo)
QString comment() const
Returns the descriptive comment associated, if any.
QVariant property(const QString &name) const
Returns the requested property.
QString toString() const const
T readEntry(const QString &key, const T &aDefault) const
QString name() const
static KSycoca * self()
Get or create the only instance of KSycoca (read-only)
Definition: ksycoca.cpp:378
QMap< QString, QVariant::Type > propertyDefs() const
QList::iterator begin()
void ensureCacheValid()
Ensures the ksycoca database is up to date.
Definition: ksycoca.cpp:825
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Tue Jan 25 2022 22:43:43 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.