File indexing completed on 2024-04-28 11:44:26

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