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

KDECore

  • sources
  • kde-4.12
  • kdelibs
  • 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 
31 class KSycocaFactory::Private
32 {
33 public:
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 
50 KSycocaFactory::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 
80 KSycocaFactory::~KSycocaFactory()
81 {
82  delete m_entryDict;
83  delete d;
84 }
85 
86 void
87 KSycocaFactory::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 
96 void
97 KSycocaFactory::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 
145 void
146 KSycocaFactory::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 
171 void
172 KSycocaFactory::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 
183 KSycocaEntry::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 
221 int KSycocaFactory::offset() const
222 {
223  return d->mOffset;
224 }
225 
226 const KSycocaResourceList * KSycocaFactory::resourceList() const
227 {
228  return m_resourceList;
229 }
230 
231 const KSycocaDict * KSycocaFactory::sycocaDict() const
232 {
233  return d->m_sycocaDict;
234 }
235 
236 bool KSycocaFactory::isEmpty() const
237 {
238  return d->m_beginEntryOffset == d->m_endEntryOffset;
239 }
240 
241 QDataStream* KSycocaFactory::stream() const
242 {
243  return m_str;
244 }
245 
246 void KSycocaFactory::virtual_hook( int /*id*/, void* /*data*/)
247 { /*BASE::virtual_hook( id, data );*/ }
248 
KSycocaDict
Definition: ksycocadict_p.h:36
KSycocaFactory::stream
QDataStream * stream() const
Definition: ksycocafactory.cpp:241
KSharedPtr
Can be used to control the lifetime of an object that has derived QSharedData.
Definition: kconfiggroup.h:38
KSycocaFactory::resourceList
const KSycocaResourceList * resourceList() const
Definition: ksycocafactory.cpp:226
KSycocaFactory::m_entryDict
KSycocaEntryDict * m_entryDict
Definition: ksycocafactory.h:130
kdebug.h
KSycocaFactory::isEmpty
bool isEmpty() const
Definition: ksycocafactory.cpp:236
KSycocaFactory::m_resourceList
KSycocaResourceList * m_resourceList
Definition: ksycocafactory.h:129
KSycocaFactory::createEntry
virtual KSycocaEntry * createEntry(const QString &file, const char *resource) const =0
Construct an entry from a config file.
QString
KSycocaFactory::removeEntry
void removeEntry(const QString &entryName)
Remove all entries with the given name.
Definition: ksycocafactory.cpp:172
ksycocatype.h
KSycocaEntryDict
QHash< QString, KSycocaEntry::Ptr > KSycocaEntryDict
Definition: ksycocafactory.h:28
ksycocadict_p.h
KSycocaEntry
Base class for all Sycoca entries.
Definition: ksycocaentry.h:41
KSycocaFactory::KSycocaFactoryId
KSycocaFactoryId
Definition: ksycocatype.h:44
KSycocaFactory::addEntry
virtual void addEntry(const KSycocaEntry::Ptr &newEntry)
Add an entry.
Definition: ksycocafactory.cpp:146
KSycocaFactory::~KSycocaFactory
virtual ~KSycocaFactory()
Definition: ksycocafactory.cpp:80
KSycocaFactory::allEntries
virtual KSycocaEntry::List allEntries() const
Get a list of all entries from the database.
Definition: ksycocafactory.cpp:183
KSycocaFactory::sycocaDict
const KSycocaDict * sycocaDict() const
Definition: ksycocafactory.cpp:231
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
KSycoca::flagError
static void flagError()
A read error occurs.
Definition: ksycoca.cpp:559
ksycocaentry.h
KSycoca::addFactory
void addFactory(KSycocaFactory *)
Definition: ksycoca.cpp:341
ksycoca.h
KSycocaFactory::offset
int offset() const
Definition: ksycocafactory.cpp:221
ksycocafactory.h
qint32
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
kDebug
#define kDebug
Definition: kdebug.h:316
KSycocaFactory::saveHeader
virtual void saveHeader(QDataStream &str)
Writes out a header to the stream 'str'.
Definition: ksycocafactory.cpp:87
KSycoca::self
static KSycoca * self()
Get or create the only instance of KSycoca (read-only)
Definition: ksycoca.cpp:293
QList< Ptr >
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
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:47:09 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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