File indexing completed on 2024-10-06 12:24:01

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 #ifndef KSYCOCAFACTORY_H
0009 #define KSYCOCAFACTORY_H
0010 
0011 #include "ksycocaresourcelist_p.h"
0012 #include <QStandardPaths>
0013 #include <ksycocaentry.h>
0014 
0015 #include <ksycoca.h> // for KSycoca::self()
0016 
0017 #include <memory>
0018 
0019 class QString;
0020 class KSycoca;
0021 class KSycocaDict;
0022 template<typename T>
0023 class QList;
0024 template<typename KT, typename VT>
0025 class QHash;
0026 
0027 typedef QHash<QString, KSycocaEntry::Ptr> KSycocaEntryDict;
0028 class KSycocaFactoryPrivate;
0029 /**
0030  * @internal
0031  * Base class for sycoca factories
0032  * Exported for unit tests
0033  */
0034 class KSERVICE_EXPORT KSycocaFactory
0035 {
0036 public:
0037     virtual KSycocaFactoryId factoryId() const = 0;
0038 
0039 protected: // virtual class
0040     /**
0041      * Create a factory which can be used to lookup from/create a database
0042      * (depending on KSycoca::isBuilding())
0043      */
0044     explicit KSycocaFactory(KSycocaFactoryId factory_id, KSycoca *sycoca);
0045 
0046 public:
0047     virtual ~KSycocaFactory();
0048 
0049     /**
0050      * @return the position of the factory in the sycoca file
0051      */
0052     int offset() const;
0053 
0054     /**
0055      * @return the dict, for special use by KBuildSycoca
0056      */
0057     KSycocaEntryDict *entryDict()
0058     {
0059         return m_entryDict;
0060     }
0061 
0062     /**
0063      * Construct an entry from a config file.
0064      * To be implemented in the real factories.
0065      */
0066     virtual KSycocaEntry *createEntry(const QString &file) const = 0;
0067 
0068     /**
0069      * Add an entry
0070      */
0071     virtual void addEntry(const KSycocaEntry::Ptr &newEntry);
0072 
0073     /**
0074      * Remove all entries with the given name.
0075      * Not very fast (O(N)), use with care.
0076      */
0077     void removeEntry(const QString &entryName);
0078 
0079     /**
0080      * Read an entry from the database
0081      */
0082     virtual KSycocaEntry *createEntry(int offset) const = 0;
0083 
0084     /**
0085      * Get a list of all entries from the database.
0086      */
0087     virtual KSycocaEntry::List allEntries() const;
0088 
0089     /**
0090      * Saves all entries it maintains as well as index files
0091      * for these entries to the stream 'str'.
0092      *
0093      * Also sets mOffset to the starting position.
0094      *
0095      * The stream is positioned at the end of the last index.
0096      *
0097      * Don't forget to call the parent first when you override
0098      * this function.
0099      */
0100     virtual void save(QDataStream &str);
0101 
0102     /**
0103      * Writes out a header to the stream 'str'.
0104      * The baseclass positions the stream correctly.
0105      *
0106      * Don't forget to call the parent first when you override
0107      * this function.
0108      */
0109     virtual void saveHeader(QDataStream &str);
0110 
0111     /**
0112      * @return the resources for which this factory is responsible.
0113      * @internal to kbuildsycoca
0114      */
0115     const KSycocaResourceList &resourceList() const;
0116 
0117     /**
0118      * @return the sycoca dict, for factories to find entries by name.
0119      */
0120     const KSycocaDict *sycocaDict() const;
0121 
0122     /**
0123      * @return true if the factory is completely empty - no entries defined
0124      */
0125     bool isEmpty() const;
0126 
0127     KSycoca *sycoca() const
0128     {
0129         return m_sycoca;
0130     }
0131 
0132 protected:
0133     QDataStream *stream() const;
0134 
0135     KSycocaResourceList m_resourceList;
0136     KSycocaEntryDict *m_entryDict = nullptr;
0137 
0138     /**
0139      * Returns all directories for the given @p subdir of GenericDataLocation.
0140      * Helper function for AnyFactory::resourceDirs().
0141      */
0142     static QStringList allDirectories(const QString &subdir);
0143 
0144 private:
0145     QDataStream *m_str = nullptr;
0146     KSycoca *m_sycoca = nullptr;
0147     std::unique_ptr<KSycocaFactoryPrivate> const d;
0148 
0149 protected:
0150     /** Virtual hook, used to add new "virtual" functions while maintaining
0151         binary compatibility. Unused in this class.
0152     */
0153     virtual void virtual_hook(int id, void *data);
0154 };
0155 
0156 /**
0157  * This, instead of a typedef, allows to declare "class ..." in header files.
0158  * @internal
0159  */
0160 class KSycocaFactoryList : public QList<KSycocaFactory *> // krazy:exclude=dpointer (acts as a typedef)
0161 {
0162 public:
0163     KSycocaFactoryList()
0164     {
0165     }
0166 };
0167 
0168 #endif