summaryrefslogtreecommitdiffstats
path: root/kdecore/ksycocafactory.cpp
blob: c224ebe7957e12305b1ab509f15ace2fcff9dcf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*  This file is part of the KDE libraries
 *  Copyright (C) 1999 David Faure <faure@kde.org>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation;
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#include "ksycoca.h"
#include "ksycocatype.h"
#include "ksycocafactory.h"
#include "ksycocaentry.h"
#include "ksycocadict.h"
#include <tqstringlist.h>
#include <tqdict.h>
#include <kdebug.h>

template class TQDict<KSycocaEntry>;
template class TQDict<KSharedPtr<KSycocaEntry> >;

KSycocaFactory::KSycocaFactory(KSycocaFactoryId factory_id)
 : m_resourceList(0), m_entryDict(0), m_sycocaDict(0)
{
  if (!KSycoca::self()->isBuilding()) // read-only database ?
  {
      m_str = KSycoca::self()->findFactory( factory_id );
      // can't call factoryId() here since the constructor can't call inherited methods
      if (m_str) // Can be 0 in case of errors
      {
          // Read position of index tables....
          TQ_INT32 i;
          (*m_str) >> i;
          m_sycocaDictOffset = i;
          (*m_str) >> i;
          m_beginEntryOffset = i;
          (*m_str) >> i;
          m_endEntryOffset = i;

          int saveOffset = m_str->device()->at();
          // Init index tables
          m_sycocaDict = new KSycocaDict(m_str, m_sycocaDictOffset);   
          saveOffset = m_str->device()->at(saveOffset);
      }
   }
   else
   {
      // Build new database!
      m_str = 0;
      m_resourceList = 0;
      m_entryDict = new KSycocaEntryDict(977);
      m_entryDict->setAutoDelete(true);
      m_sycocaDict = new KSycocaDict();
      m_beginEntryOffset = 0;
      m_endEntryOffset = 0;

      // m_resourceList will be filled in by inherited constructors
   }
   KSycoca::self()->addFactory(this);
}

KSycocaFactory::~KSycocaFactory()
{
   delete m_entryDict;
   delete m_sycocaDict;
}

void
KSycocaFactory::saveHeader(TQDataStream &str)
{
   // Write header 
   str.device()->at(mOffset);
   str << (TQ_INT32) m_sycocaDictOffset;
   str << (TQ_INT32) m_beginEntryOffset;
   str << (TQ_INT32) m_endEntryOffset;
}

void
KSycocaFactory::save(TQDataStream &str)
{
   if (!m_entryDict) return; // Error! Function should only be called when
                             // building database
   if (!m_sycocaDict) return; // Error!

   mOffset = str.device()->at(); // store position in member variable
   m_sycocaDictOffset = 0;

   // Write header (pass #1)
   saveHeader(str);

   m_beginEntryOffset = str.device()->at();

   // Write all entries.
   int entryCount = 0;
   for(TQDictIterator<KSycocaEntry::Ptr> it ( *m_entryDict ); 
       it.current(); 
       ++it)
   {
      KSycocaEntry *entry = (*it.current());
      entry->save(str);
      entryCount++;
   }

   m_endEntryOffset = str.device()->at();

   // Write indices...
   // Linear index
   str << (TQ_INT32) entryCount;
   for(TQDictIterator<KSycocaEntry::Ptr> it ( *m_entryDict ); 
       it.current(); 
       ++it)
   {
      KSycocaEntry *entry = (*it.current());
      str << (TQ_INT32) entry->offset(); 
   }

   // Dictionary index
   m_sycocaDictOffset = str.device()->at();      
   m_sycocaDict->save(str);

   int endOfFactoryData = str.device()->at();

   // Update header (pass #2)
   saveHeader(str);

   // Seek to end.
   str.device()->at(endOfFactoryData);
}

void 
KSycocaFactory::addEntry(KSycocaEntry *newEntry, const char *)
{
   if (!m_entryDict) return; // Error! Function should only be called when
                             // building database

   if (!m_sycocaDict) return; // Error!

   TQString name = newEntry->name();
   m_entryDict->insert( name, new KSycocaEntry::Ptr(newEntry) );
   m_sycocaDict->add( name, newEntry );
}

void 
KSycocaFactory::removeEntry(KSycocaEntry *newEntry)
{
   if (!m_entryDict) return; // Error! Function should only be called when
                             // building database

   if (!m_sycocaDict) return; // Error!

   TQString name = newEntry->name();
   m_entryDict->remove( name );
   m_sycocaDict->remove( name );
}

KSycocaEntry::List KSycocaFactory::allEntries()
{
   KSycocaEntry::List list;
   if (!m_str) return list;

   // Assume we're NOT building a database

   m_str->device()->at(m_endEntryOffset);
   TQ_INT32 entryCount;
   (*m_str) >> entryCount;
   
   if (entryCount > 8192)
   {
      KSycoca::flagError();
      return list;
   }

   TQ_INT32 *offsetList = new TQ_INT32[entryCount];
   for(int i = 0; i < entryCount; i++)
   {
      (*m_str) >> offsetList[i];
   }

   for(int i = 0; i < entryCount; i++)
   {
      KSycocaEntry *newEntry = createEntry(offsetList[i]);
      if (newEntry)
      {
         list.append( KSycocaEntry::Ptr( newEntry ) );
      }
   }
   delete [] offsetList;
   return list;
}

void KSycocaFactory::virtual_hook( int /*id*/, void* /*data*/)
{ /*BASE::virtual_hook( id, data );*/ }