1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 1999 Waldo Bastian <bastian@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-only
6*/
7
8#ifndef KSYCOCAFACTORY_H
9#define KSYCOCAFACTORY_H
10
11#include "ksycocaresourcelist_p.h"
12#include <QStandardPaths>
13#include <ksycocaentry.h>
14
15#include <ksycoca.h> // for KSycoca::self()
16
17#include <memory>
18
19class QString;
20class KSycoca;
21class KSycocaDict;
22template<typename T>
23class QList;
24template<typename KT, typename VT>
25class QHash;
26
27typedef QHash<QString, KSycocaEntry::Ptr> KSycocaEntryDict;
28class KSycocaFactoryPrivate;
29/**
30 * @internal
31 * Base class for sycoca factories
32 * Exported for unit tests
33 */
34class KSERVICE_EXPORT KSycocaFactory
35{
36public:
37 virtual KSycocaFactoryId factoryId() const = 0;
38
39protected: // virtual class
40 /**
41 * Create a factory which can be used to lookup from/create a database
42 * (depending on KSycoca::isBuilding())
43 */
44 explicit KSycocaFactory(KSycocaFactoryId factory_id, KSycoca *sycoca);
45
46public:
47 virtual ~KSycocaFactory();
48
49 /**
50 * @return the position of the factory in the sycoca file
51 */
52 int offset() const;
53
54 /**
55 * @return the dict, for special use by KBuildSycoca
56 */
57 KSycocaEntryDict *entryDict()
58 {
59 return m_entryDict;
60 }
61
62 /**
63 * Construct an entry from a config file.
64 * To be implemented in the real factories.
65 */
66 virtual KSycocaEntry *createEntry(const QString &file) const = 0;
67
68 /**
69 * Add an entry
70 */
71 virtual void addEntry(const KSycocaEntry::Ptr &newEntry);
72
73 /**
74 * Remove all entries with the given name.
75 * Not very fast (O(N)), use with care.
76 */
77 void removeEntry(const QString &entryName);
78
79 /**
80 * Read an entry from the database
81 */
82 virtual KSycocaEntry *createEntry(int offset) const = 0;
83
84 /**
85 * Get a list of all entries from the database.
86 */
87 virtual KSycocaEntry::List allEntries() const;
88
89 /**
90 * Saves all entries it maintains as well as index files
91 * for these entries to the stream 'str'.
92 *
93 * Also sets mOffset to the starting position.
94 *
95 * The stream is positioned at the end of the last index.
96 *
97 * Don't forget to call the parent first when you override
98 * this function.
99 */
100 virtual void save(QDataStream &str);
101
102 /**
103 * Writes out a header to the stream 'str'.
104 * The baseclass positions the stream correctly.
105 *
106 * Don't forget to call the parent first when you override
107 * this function.
108 */
109 virtual void saveHeader(QDataStream &str);
110
111 /**
112 * @return the resources for which this factory is responsible.
113 * @internal to kbuildsycoca
114 */
115 const KSycocaResourceList &resourceList() const;
116
117 /**
118 * @return the sycoca dict, for factories to find entries by name.
119 */
120 const KSycocaDict *sycocaDict() const;
121
122 /**
123 * @return true if the factory is completely empty - no entries defined
124 */
125 bool isEmpty() const;
126
127 KSycoca *sycoca() const
128 {
129 return m_sycoca;
130 }
131
132protected:
133 QDataStream *stream() const;
134
135 KSycocaResourceList m_resourceList;
136 KSycocaEntryDict *m_entryDict = nullptr;
137
138 /**
139 * Returns all directories for the given @p subdir of GenericDataLocation.
140 * Helper function for AnyFactory::resourceDirs().
141 */
142 static QStringList allDirectories(const QString &subdir);
143
144private:
145 QDataStream *m_str = nullptr;
146 KSycoca *m_sycoca = nullptr;
147 std::unique_ptr<KSycocaFactoryPrivate> const d;
148
149protected:
150 /** Virtual hook, used to add new "virtual" functions while maintaining
151 binary compatibility. Unused in this class.
152 */
153 virtual void virtual_hook(int id, void *data);
154};
155
156/**
157 * This, instead of a typedef, allows to declare "class ..." in header files.
158 * @internal
159 */
160class KSycocaFactoryList : public QList<KSycocaFactory *> // krazy:exclude=dpointer (acts as a typedef)
161{
162public:
163 KSycocaFactoryList()
164 {
165 }
166};
167
168#endif
169

source code of kservice/src/sycoca/ksycocafactory_p.h