File indexing completed on 2024-12-01 06:46:31

0001 /*
0002     This file is part of the KDE project
0003     SPDX-FileCopyrightText: 1998, 1999 Torben Weis <weis@kde.org>
0004     SPDX-FileCopyrightText: 2006 David Faure <faure@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #ifndef KSERVICEFACTORY_P_H
0010 #define KSERVICEFACTORY_P_H
0011 
0012 #include <QStringList>
0013 
0014 #include "kserviceoffer.h"
0015 #include "ksycocafactory_p.h"
0016 #include <assert.h>
0017 
0018 class KSycoca;
0019 class KSycocaDict;
0020 
0021 /**
0022  * @internal
0023  * A sycoca factory for services (e.g. applications)
0024  * It loads the services from parsing directories (e.g. prefix/share/applications/)
0025  * but can also create service from data streams or single config files
0026  *
0027  * Exported for unit tests
0028  */
0029 class KSERVICE_EXPORT KServiceFactory : public KSycocaFactory
0030 {
0031     K_SYCOCAFACTORY(KST_KServiceFactory)
0032 public:
0033     /**
0034      * Create factory
0035      */
0036     explicit KServiceFactory(KSycoca *sycoca);
0037     ~KServiceFactory() override;
0038 
0039     /**
0040      * Construct a KService from a config file.
0041      */
0042     KSycocaEntry *createEntry(const QString &) const override
0043     {
0044         assert(0);
0045         return nullptr;
0046     }
0047 
0048     /**
0049      * Find a service (by translated name, e.g. "Terminal")
0050      * (Not virtual because not used inside kbuildsycoca4, only an external service for some KDE apps)
0051      */
0052     KService::Ptr findServiceByName(const QString &_name);
0053 
0054     /**
0055      * Find a service (by desktop file name, e.g. "konsole")
0056      */
0057     virtual KService::Ptr findServiceByDesktopName(const QString &_name);
0058 
0059     /**
0060      * Find a service ( by desktop path, e.g. "System/konsole.desktop")
0061      */
0062     virtual KService::Ptr findServiceByDesktopPath(const QString &_name);
0063 
0064     /**
0065      * Find a service ( by menu id, e.g. "kde-konsole.desktop")
0066      */
0067     virtual KService::Ptr findServiceByMenuId(const QString &_menuId);
0068 
0069     KService::Ptr findServiceByStorageId(const QString &_storageId);
0070 
0071     /**
0072      * @return the services supporting the given service type
0073      * The @p serviceOffersOffset allows to jump to the right entries directly.
0074      */
0075     KServiceOfferList offers(int serviceTypeOffset, int serviceOffersOffset);
0076 
0077     /**
0078      * @return the services supporting the given service type; without information about initialPreference
0079      * The @p serviceOffersOffset allows to jump to the right entries directly.
0080      */
0081     KService::List serviceOffers(int serviceTypeOffset, int serviceOffersOffset);
0082 
0083     /**
0084      * Test if a specific service is associated with a specific servicetype
0085      * @param serviceTypeOffset the offset of the service type being tested
0086      * @param serviceOffersOffset allows to jump to the right entries for the service type directly.
0087      * @param testedServiceOffset the offset of the service being tested
0088      */
0089     bool hasOffer(int serviceTypeOffset, int serviceOffersOffset, int testedServiceOffset);
0090 
0091     /**
0092      * @return all services. Very memory consuming, avoid using.
0093      */
0094     KService::List allServices();
0095 
0096     /**
0097      * Returns the directories to watch for this factory.
0098      */
0099     static QStringList resourceDirs();
0100 
0101     /**
0102      * @return the unique service factory, creating it if necessary
0103      */
0104     static KServiceFactory *self();
0105 
0106 protected:
0107     KService *createEntry(int offset) const override;
0108 
0109     // All those variables are used by KBuildServiceFactory too
0110     int m_offerListOffset;
0111     KSycocaDict *m_nameDict;
0112     int m_nameDictOffset;
0113     KSycocaDict *m_relNameDict;
0114     int m_relNameDictOffset;
0115     KSycocaDict *m_menuIdDict;
0116     int m_menuIdDictOffset;
0117 
0118 protected:
0119     void virtual_hook(int id, void *data) override;
0120 
0121 private:
0122     class KServiceFactoryPrivate *d;
0123 };
0124 
0125 #endif