File indexing completed on 2024-04-28 15:29:52

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999 Waldo Bastian <bastian@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #include "kservicetypefactory_p.h"
0009 #include "kservicetypeprofile.h"
0010 #include "ksycoca.h"
0011 #include "ksycocadict_p.h"
0012 #include "ksycocatype.h"
0013 #include "ksycocautils_p.h"
0014 #include "servicesdebug.h"
0015 
0016 #include <assert.h>
0017 
0018 KServiceTypeFactory::KServiceTypeFactory(KSycoca *db)
0019     : KSycocaFactory(KST_KServiceTypeFactory, db)
0020 {
0021     if (!sycoca()->isBuilding()) {
0022         QDataStream *str = stream();
0023         if (str) {
0024             // Read Header
0025             qint32 n;
0026             (*str) >> n;
0027             if (n > 1024) {
0028                 KSycoca::flagError();
0029             } else {
0030                 QString string;
0031                 qint32 i;
0032                 for (; n; --n) {
0033                     *str >> string >> i;
0034                     m_propertyTypeDict.insert(string, i);
0035                 }
0036             }
0037         } else {
0038             qWarning() << "Could not open sycoca database, you must run kbuildsycoca first!";
0039         }
0040     }
0041 }
0042 
0043 KServiceTypeFactory::~KServiceTypeFactory()
0044 {
0045     if (!sycoca()->isBuilding()) {
0046         KServiceTypeProfile::clearCache();
0047     }
0048 }
0049 
0050 KServiceType::Ptr KServiceTypeFactory::findServiceTypeByName(const QString &_name)
0051 {
0052     if (!sycocaDict()) {
0053         return KServiceType::Ptr(); // Error!
0054     }
0055     assert(!sycoca()->isBuilding());
0056     int offset = sycocaDict()->find_string(_name);
0057     if (!offset) {
0058         return KServiceType::Ptr(); // Not found
0059     }
0060     KServiceType::Ptr newServiceType(createEntry(offset));
0061 
0062     // Check whether the dictionary was right.
0063     if (newServiceType && (newServiceType->name() != _name)) {
0064         // No it wasn't...
0065         newServiceType = nullptr; // Not found
0066     }
0067     return newServiceType;
0068 }
0069 
0070 QMetaType::Type KServiceTypeFactory::findPropertyTypeByName(const QString &_name)
0071 {
0072     if (!sycocaDict()) {
0073         return QMetaType::UnknownType; // Error!
0074     }
0075 
0076     assert(!sycoca()->isBuilding());
0077 
0078     return static_cast<QMetaType::Type>(m_propertyTypeDict.value(_name, QMetaType::UnknownType));
0079 }
0080 
0081 KServiceType::List KServiceTypeFactory::allServiceTypes()
0082 {
0083     KServiceType::List result;
0084     const KSycocaEntry::List list = allEntries();
0085     for (KSycocaEntry::List::ConstIterator it = list.begin(); it != list.end(); ++it) {
0086         if ((*it)->isType(KST_KServiceType)) {
0087             KServiceType::Ptr newServiceType(static_cast<KServiceType *>((*it).data()));
0088             result.append(newServiceType);
0089         }
0090     }
0091     return result;
0092 }
0093 
0094 QStringList KServiceTypeFactory::resourceDirs()
0095 {
0096     return KSycocaFactory::allDirectories(QStringLiteral("kservicetypes5"));
0097 }
0098 
0099 KServiceType *KServiceTypeFactory::createEntry(int offset) const
0100 {
0101     KSycocaType type;
0102     QDataStream *str = sycoca()->findEntry(offset, type);
0103     if (!str) {
0104         return nullptr;
0105     }
0106 
0107     if (type != KST_KServiceType) {
0108         qCWarning(SERVICES) << "KServiceTypeFactory: unexpected object entry in KSycoca database (type=" << int(type) << ")";
0109         return nullptr;
0110     }
0111 
0112     KServiceType *newEntry = new KServiceType(*str, offset);
0113     if (newEntry && !newEntry->isValid()) {
0114         qCWarning(SERVICES) << "KServiceTypeFactory: corrupt object in KSycoca database!";
0115         delete newEntry;
0116         newEntry = nullptr;
0117     }
0118     return newEntry;
0119 }
0120 
0121 void KServiceTypeFactory::virtual_hook(int id, void *data)
0122 {
0123     KSycocaFactory::virtual_hook(id, data);
0124 }