• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDECore

  • kdecore
  • sycoca
ksycocafactory.cpp
Go to the documentation of this file.
1/* This file is part of the KDE libraries
2 * Copyright (C) 1999 David Faure <faure@kde.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License version 2 as published by the Free Software Foundation;
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Library General Public License for more details.
12 *
13 * You should have received a copy of the GNU Library General Public License
14 * along with this library; see the file COPYING.LIB. If not, write to
15 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301, USA.
17 */
18
19#include "ksycocafactory.h"
20#include "ksycoca.h"
21#include "ksycocatype.h"
22#include "ksycocaentry.h"
23#include "ksycocadict_p.h"
24
25#include <config.h>
26#include <kdebug.h>
27
28#include <QThread>
29#include <QtCore/QHash>
30
31class KSycocaFactory::Private
32{
33public:
34 Private() : mOffset(0),
35 m_sycocaDictOffset(0),
36 m_beginEntryOffset(0),
37 m_endEntryOffset(0) {}
38 ~Private()
39 {
40 delete m_sycocaDict;
41 }
42
43 int mOffset;
44 int m_sycocaDictOffset;
45 int m_beginEntryOffset;
46 int m_endEntryOffset;
47 KSycocaDict *m_sycocaDict;
48};
49
50KSycocaFactory::KSycocaFactory(KSycocaFactoryId factory_id)
51 : m_resourceList(0), m_entryDict(0), m_str(0), d(new Private)
52{
53 if (!KSycoca::self()->isBuilding() && (m_str = KSycoca::self()->findFactory(factory_id))) {
54 // Read position of index tables....
55 qint32 i;
56 (*m_str) >> i;
57 d->m_sycocaDictOffset = i;
58 (*m_str) >> i;
59 d->m_beginEntryOffset = i;
60 (*m_str) >> i;
61 d->m_endEntryOffset = i;
62
63 QDataStream* str = stream();
64 int saveOffset = str->device()->pos();
65 // Init index tables
66 d->m_sycocaDict = new KSycocaDict(str, d->m_sycocaDictOffset);
67 saveOffset = str->device()->seek(saveOffset);
68 } else {
69 // We are in kbuildsycoca4 -- build new database!
70 m_entryDict = new KSycocaEntryDict;
71 d->m_sycocaDict = new KSycocaDict;
72 d->m_beginEntryOffset = 0;
73 d->m_endEntryOffset = 0;
74
75 // m_resourceList will be filled in by inherited constructors
76 }
77 KSycoca::self()->addFactory(this);
78}
79
80KSycocaFactory::~KSycocaFactory()
81{
82 delete m_entryDict;
83 delete d;
84}
85
86void
87KSycocaFactory::saveHeader(QDataStream &str)
88{
89 // Write header
90 str.device()->seek(d->mOffset);
91 str << (qint32) d->m_sycocaDictOffset;
92 str << (qint32) d->m_beginEntryOffset;
93 str << (qint32) d->m_endEntryOffset;
94}
95
96void
97KSycocaFactory::save(QDataStream &str)
98{
99 if (!m_entryDict) return; // Error! Function should only be called when
100 // building database
101 if (!d->m_sycocaDict) return; // Error!
102
103 d->mOffset = str.device()->pos(); // store position in member variable
104 d->m_sycocaDictOffset = 0;
105
106 // Write header (pass #1)
107 saveHeader(str);
108
109 d->m_beginEntryOffset = str.device()->pos();
110
111 // Write all entries.
112 int entryCount = 0;
113 for(KSycocaEntryDict::Iterator it = m_entryDict->begin();
114 it != m_entryDict->end(); ++it)
115 {
116 KSycocaEntry::Ptr entry = *it;
117 entry->save(str);
118 entryCount++;
119 }
120
121 d->m_endEntryOffset = str.device()->pos();
122
123 // Write indices...
124 // Linear index
125 str << (qint32) entryCount;
126 for(KSycocaEntryDict::Iterator it = m_entryDict->begin();
127 it != m_entryDict->end(); ++it)
128 {
129 str << qint32(it->data()->offset());
130 }
131
132 // Dictionary index
133 d->m_sycocaDictOffset = str.device()->pos();
134 d->m_sycocaDict->save(str);
135
136 int endOfFactoryData = str.device()->pos();
137
138 // Update header (pass #2)
139 saveHeader(str);
140
141 // Seek to end.
142 str.device()->seek(endOfFactoryData);
143}
144
145void
146KSycocaFactory::addEntry(const KSycocaEntry::Ptr& newEntry)
147{
148 if (!m_entryDict) return; // Error! Function should only be called when
149 // building database
150
151 if (!d->m_sycocaDict) return; // Error!
152
153 KSycocaEntry::Ptr oldEntry = m_entryDict->value(newEntry->storageId());
154 if (oldEntry) {
155 // Already exists -> replace
156 // We found a more-local override, e.g. ~/.local/share/applications/kde4/foo.desktop
157 // So forget about the more global file.
158 //
159 // This can also happen with two .protocol files using the same protocol= entry.
160 // If we didn't remove one here, we would end up asserting because save()
161 // wasn't called on one of the entries.
162 //kDebug(7021) << "removing" << oldEntry.data() << oldEntry->entryPath() << "because of" << newEntry->entryPath() << "they have the same storageId" << newEntry->storageId();
163 removeEntry(newEntry->storageId());
164 }
165
166 const QString name = newEntry->storageId();
167 m_entryDict->insert( name, newEntry );
168 d->m_sycocaDict->add( name, newEntry );
169}
170
171void
172KSycocaFactory::removeEntry(const QString& entryName)
173{
174 if (!m_entryDict) return; // Error! Function should only be called when
175 // building database
176
177 if (!d->m_sycocaDict) return; // Error!
178
179 m_entryDict->remove( entryName );
180 d->m_sycocaDict->remove( entryName ); // O(N)
181}
182
183KSycocaEntry::List KSycocaFactory::allEntries() const
184{
185 KSycocaEntry::List list;
186
187 // Assume we're NOT building a database
188
189 QDataStream* str = stream();
190 if (!str) return list;
191 str->device()->seek(d->m_endEntryOffset);
192 qint32 entryCount;
193 (*str) >> entryCount;
194
195 if (entryCount > 8192)
196 {
197 kDebug(7021) << QThread::currentThread() << "error detected in factory" << this;
198 KSycoca::flagError();
199 return list;
200 }
201
202 // offsetList is needed because createEntry() modifies the stream position
203 qint32 *offsetList = new qint32[entryCount];
204 for(int i = 0; i < entryCount; i++)
205 {
206 (*str) >> offsetList[i];
207 }
208
209 for(int i = 0; i < entryCount; i++)
210 {
211 KSycocaEntry *newEntry = createEntry(offsetList[i]);
212 if (newEntry)
213 {
214 list.append( KSycocaEntry::Ptr( newEntry ) );
215 }
216 }
217 delete [] offsetList;
218 return list;
219}
220
221int KSycocaFactory::offset() const
222{
223 return d->mOffset;
224}
225
226const KSycocaResourceList * KSycocaFactory::resourceList() const
227{
228 return m_resourceList;
229}
230
231const KSycocaDict * KSycocaFactory::sycocaDict() const
232{
233 return d->m_sycocaDict;
234}
235
236bool KSycocaFactory::isEmpty() const
237{
238 return d->m_beginEntryOffset == d->m_endEntryOffset;
239}
240
241QDataStream* KSycocaFactory::stream() const
242{
243 return m_str;
244}
245
246void KSycocaFactory::virtual_hook( int /*id*/, void* /*data*/)
247{ /*BASE::virtual_hook( id, data );*/ }
248
KSharedPtr
Can be used to control the lifetime of an object that has derived QSharedData.
Definition: ksharedptr.h:64
KSycocaDict
Definition: ksycocadict_p.h:37
KSycocaEntry
Base class for all Sycoca entries.
Definition: ksycocaentry.h:42
KSycocaFactory::m_entryDict
KSycocaEntryDict * m_entryDict
Definition: ksycocafactory.h:130
KSycocaFactory::resourceList
const KSycocaResourceList * resourceList() const
Definition: ksycocafactory.cpp:226
KSycocaFactory::allEntries
virtual KSycocaEntry::List allEntries() const
Get a list of all entries from the database.
Definition: ksycocafactory.cpp:183
KSycocaFactory::~KSycocaFactory
virtual ~KSycocaFactory()
Definition: ksycocafactory.cpp:80
KSycocaFactory::virtual_hook
virtual void virtual_hook(int id, void *data)
Virtual hook, used to add new "virtual" functions while maintaining binary compatibility.
Definition: ksycocafactory.cpp:246
KSycocaFactory::removeEntry
void removeEntry(const QString &entryName)
Remove all entries with the given name.
Definition: ksycocafactory.cpp:172
KSycocaFactory::isEmpty
bool isEmpty() const
Definition: ksycocafactory.cpp:236
KSycocaFactory::saveHeader
virtual void saveHeader(QDataStream &str)
Writes out a header to the stream 'str'.
Definition: ksycocafactory.cpp:87
KSycocaFactory::offset
int offset() const
Definition: ksycocafactory.cpp:221
KSycocaFactory::save
virtual void save(QDataStream &str)
Saves all entries it maintains as well as index files for these entries to the stream 'str'.
Definition: ksycocafactory.cpp:97
KSycocaFactory::KSycocaFactory
KSycocaFactory(KSycocaFactoryId factory_id)
Create a factory which can be used to lookup from/create a database (depending on KSycoca::isBuilding...
Definition: ksycocafactory.cpp:50
KSycocaFactory::m_resourceList
KSycocaResourceList * m_resourceList
Definition: ksycocafactory.h:129
KSycocaFactory::stream
QDataStream * stream() const
Definition: ksycocafactory.cpp:241
KSycocaFactory::addEntry
virtual void addEntry(const KSycocaEntry::Ptr &newEntry)
Add an entry.
Definition: ksycocafactory.cpp:146
KSycocaFactory::createEntry
virtual KSycocaEntry * createEntry(const QString &file, const char *resource) const =0
Construct an entry from a config file.
KSycocaFactory::KSycocaFactoryId
KSycocaFactoryId
A KSycocaFactoryId is a code (out of the KSycocaFactoryId enum) assigned to each class type derived f...
Definition: ksycocatype.h:44
KSycocaFactory::sycocaDict
const KSycocaDict * sycocaDict() const
Definition: ksycocafactory.cpp:231
KSycoca::self
static KSycoca * self()
Get or create the only instance of KSycoca (read-only)
Definition: ksycoca.cpp:293
KSycoca::addFactory
void addFactory(KSycocaFactory *)
Definition: ksycoca.cpp:341
KSycoca::flagError
static void flagError()
A read error occurs.
Definition: ksycoca.cpp:559
QList< Ptr >
QString
qint32
kDebug
#define kDebug
Definition: kdebug.h:316
kdebug.h
ksycoca.h
ksycocadict_p.h
ksycocaentry.h
ksycocafactory.h
KSycocaEntryDict
QHash< QString, KSycocaEntry::Ptr > KSycocaEntryDict
Definition: ksycocafactory.h:30
ksycocatype.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

KDECore

Skip menu "KDECore"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal