KService

kservicetype.cpp
1 /* This file is part of the KDE libraries
2  * Copyright (C) 1999 Waldo Bastian <[email protected]>
3  * David Faure <[email protected]>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License version 2 as published by the Free Software Foundation;
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  **/
19 
20 #include "kservicetype.h"
21 #include "kservicetype_p.h"
22 #include "ksycoca.h"
23 #include "ksycoca_p.h"
24 #include "kservice.h"
25 #include "kservicetypefactory_p.h"
26 #include "kservicefactory_p.h"
27 #include "kservicetypeprofile.h"
28 #include <assert.h>
29 #include <KDesktopFile>
30 #include <KConfigGroup>
31 #include "servicesdebug.h"
32 
33 extern int servicesDebugArea();
34 
35 template QDataStream &operator>> <QString, QVariant>(QDataStream &, QMap<QString, QVariant> &);
36 template QDataStream &operator<< <QString, QVariant>(QDataStream &, const QMap<QString, QVariant> &);
37 
39  : KSycocaEntry(*new KServiceTypePrivate(config->fileName()))
40 {
41  Q_D(KServiceType);
42  d->init(config);
43 }
44 
45 void
46 KServiceTypePrivate::init(KDesktopFile *config)
47 {
48 // Q_Q(KServiceType);
49 
50  KConfigGroup desktopGroup = config->desktopGroup();
51  m_strName = desktopGroup.readEntry("X-KDE-ServiceType");
52  m_strComment = desktopGroup.readEntry("Comment");
53  deleted = desktopGroup.readEntry("Hidden", false);
54 
55  // We store this as property to preserve BC, we can't change that
56  // because KSycoca needs to remain BC between KDE 2.x and KDE 3.x
57  QString sDerived = desktopGroup.readEntry("X-KDE-Derived");
58  m_bDerived = !sDerived.isEmpty();
59  if (m_bDerived) {
60  m_mapProps.insert(QStringLiteral("X-KDE-Derived"), sDerived);
61  }
62 
63  const QStringList tmpList = config->groupList();
64  QStringList::const_iterator gIt = tmpList.begin();
65 
66  for (; gIt != tmpList.end(); ++gIt) {
67  if ((*gIt).startsWith(QLatin1String("Property::"))) {
68  KConfigGroup cg(config, *gIt);
69  QVariant v = QVariant::nameToType(cg.readEntry("Type").toLatin1().constData());
70  v = cg.readEntry("Value", v);
71 
72  if (v.isValid()) {
73  m_mapProps.insert((*gIt).mid(10), v);
74  }
75  }
76  }
77 
78  gIt = tmpList.begin();
79  for (; gIt != tmpList.end(); ++gIt) {
80  if ((*gIt).startsWith(QLatin1String("PropertyDef::"))) {
81  KConfigGroup cg(config, *gIt);
82  m_mapPropDefs.insert((*gIt).mid(13),
83  QVariant::nameToType(cg.readEntry("Type").toLatin1().constData()));
84  }
85  }
86 }
87 
88 KServiceType::KServiceType(QDataStream &_str, int offset)
89  : KSycocaEntry(*new KServiceTypePrivate(_str, offset))
90 {
91  Q_D(KServiceType);
92  d->load(_str);
93 }
94 
95 void
96 KServiceTypePrivate::load(QDataStream &_str)
97 {
98  qint8 b;
99  QString dummy;
100  _str >> m_strName >> dummy >> m_strComment >> m_mapProps >> m_mapPropDefs
101  >> b >> m_serviceOffersOffset;
102  m_bDerived = m_mapProps.contains(QLatin1String("X-KDE-Derived"));
103 }
104 
105 void
106 KServiceTypePrivate::save(QDataStream &_str)
107 {
108  KSycocaEntryPrivate::save(_str);
109  // !! This data structure should remain binary compatible at all times !!
110  // You may add new fields at the end. Make sure to update the version
111  // number in ksycoca.h
112  _str << m_strName << QString() /*was icon*/ << m_strComment << m_mapProps << m_mapPropDefs
113  << qint8(1) << m_serviceOffersOffset;
114 }
115 
116 KServiceType::~KServiceType()
117 {
118 }
119 
121 {
122  const QVariant v = property(QStringLiteral("X-KDE-Derived"));
123  return v.toString();
124 }
125 
126 bool KServiceType::inherits(const QString &servTypeName) const
127 {
128  if (name() == servTypeName) {
129  return true;
130  }
131  QString st = parentServiceType();
132  while (!st.isEmpty()) {
134  if (!ptr) {
135  return false; //error
136  }
137  if (ptr->name() == servTypeName) {
138  return true;
139  }
140  st = ptr->parentServiceType();
141  }
142  return false;
143 }
144 
145 QVariant
146 KServiceTypePrivate::property(const QString &_name) const
147 {
148  QVariant v;
149 
150  if (_name == QLatin1String("Name")) {
151  v = QVariant(m_strName);
152  } else if (_name == QLatin1String("Comment")) {
153  v = QVariant(m_strComment);
154  } else {
155  v = m_mapProps.value(_name);
156  }
157 
158  return v;
159 }
160 
162 KServiceTypePrivate::propertyNames() const
163 {
164  QStringList res = m_mapProps.keys();
165  res.append(QStringLiteral("Name"));
166  res.append(QStringLiteral("Comment"));
167  return res;
168 }
169 
171 KServiceType::propertyDef(const QString &_name) const
172 {
173  Q_D(const KServiceType);
174  return static_cast<QVariant::Type>(d->m_mapPropDefs.value(_name, QVariant::Invalid));
175 }
176 
179 {
180  Q_D(const KServiceType);
181  return d->m_mapPropDefs.keys();
182 }
183 
185 {
187  return KSycocaPrivate::self()->serviceTypeFactory()->findServiceTypeByName(_name);
188 }
189 
191 {
193  return KSycocaPrivate::self()->serviceTypeFactory()->allServiceTypes();
194 }
195 
197 {
198  Q_D(KServiceType);
199  if (d->m_parentTypeLoaded) {
200  return d->parentType;
201  }
202 
203  d->m_parentTypeLoaded = true;
204 
205  const QString parentSt = parentServiceType();
206  if (parentSt.isEmpty()) {
207  return KServiceType::Ptr();
208  }
209 
211  d->parentType = KSycocaPrivate::self()->serviceTypeFactory()->findServiceTypeByName(parentSt);
212  if (!d->parentType) {
213  qCWarning(SERVICES) << entryPath() << "specifies undefined mimetype/servicetype" << parentSt;
214  }
215  return d->parentType;
216 }
217 
219 {
220  Q_D(KServiceType);
221  Q_ASSERT(offset != -1);
222  d->m_serviceOffersOffset = offset;
223 }
224 
226 {
227  Q_D(const KServiceType);
228  return d->serviceOffersOffset();
229 }
230 
231 QString KServiceType::comment() const
232 {
233  Q_D(const KServiceType);
234  return d->comment();
235 }
236 
238 {
239  Q_D(const KServiceType);
240  return d->m_bDerived;
241 }
242 
244 {
245  Q_D(const KServiceType);
246  return d->m_mapPropDefs;
247 }
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:45
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:41
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:51
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
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:360
QMap< QString, QVariant::Type > propertyDefs() const
QList::iterator begin()
void ensureCacheValid()
Ensures the ksycoca database is up to date.
Definition: ksycoca.cpp:770
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Jul 12 2020 22:47:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.