You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
203 lines
5.3 KiB
203 lines
5.3 KiB
15 years ago
|
/* 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"
|
||
14 years ago
|
#include <tqstringlist.h>
|
||
|
#include <tqdict.h>
|
||
15 years ago
|
#include <kdebug.h>
|
||
|
|
||
14 years ago
|
template class TQDict<KSycocaEntry>;
|
||
|
template class TQDict<KSharedPtr<KSycocaEntry> >;
|
||
15 years ago
|
|
||
|
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....
|
||
14 years ago
|
TQ_INT32 i;
|
||
15 years ago
|
(*m_str) >> i;
|
||
|
m_sycocaDictOffset = i;
|
||
|
(*m_str) >> i;
|
||
|
m_beginEntryOffset = i;
|
||
|
(*m_str) >> i;
|
||
|
m_endEntryOffset = i;
|
||
|
|
||
14 years ago
|
int saveOffset = m_str->tqdevice()->tqat();
|
||
15 years ago
|
// Init index tables
|
||
|
m_sycocaDict = new KSycocaDict(m_str, m_sycocaDictOffset);
|
||
14 years ago
|
saveOffset = m_str->tqdevice()->tqat(saveOffset);
|
||
15 years ago
|
}
|
||
|
}
|
||
|
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
|
||
14 years ago
|
KSycocaFactory::saveHeader(TQDataStream &str)
|
||
15 years ago
|
{
|
||
|
// Write header
|
||
14 years ago
|
str.tqdevice()->tqat(mOffset);
|
||
14 years ago
|
str << (TQ_INT32) m_sycocaDictOffset;
|
||
|
str << (TQ_INT32) m_beginEntryOffset;
|
||
|
str << (TQ_INT32) m_endEntryOffset;
|
||
15 years ago
|
}
|
||
|
|
||
|
void
|
||
14 years ago
|
KSycocaFactory::save(TQDataStream &str)
|
||
15 years ago
|
{
|
||
|
if (!m_entryDict) return; // Error! Function should only be called when
|
||
|
// building database
|
||
|
if (!m_sycocaDict) return; // Error!
|
||
|
|
||
14 years ago
|
mOffset = str.tqdevice()->tqat(); // store position in member variable
|
||
15 years ago
|
m_sycocaDictOffset = 0;
|
||
|
|
||
|
// Write header (pass #1)
|
||
|
saveHeader(str);
|
||
|
|
||
14 years ago
|
m_beginEntryOffset = str.tqdevice()->tqat();
|
||
15 years ago
|
|
||
|
// Write all entries.
|
||
|
int entryCount = 0;
|
||
14 years ago
|
for(TQDictIterator<KSycocaEntry::Ptr> it ( *m_entryDict );
|
||
15 years ago
|
it.current();
|
||
|
++it)
|
||
|
{
|
||
|
KSycocaEntry *entry = (*it.current());
|
||
|
entry->save(str);
|
||
|
entryCount++;
|
||
|
}
|
||
|
|
||
14 years ago
|
m_endEntryOffset = str.tqdevice()->tqat();
|
||
15 years ago
|
|
||
|
// Write indices...
|
||
|
// Linear index
|
||
14 years ago
|
str << (TQ_INT32) entryCount;
|
||
14 years ago
|
for(TQDictIterator<KSycocaEntry::Ptr> it ( *m_entryDict );
|
||
15 years ago
|
it.current();
|
||
|
++it)
|
||
|
{
|
||
|
KSycocaEntry *entry = (*it.current());
|
||
14 years ago
|
str << (TQ_INT32) entry->offset();
|
||
15 years ago
|
}
|
||
|
|
||
|
// Dictionary index
|
||
14 years ago
|
m_sycocaDictOffset = str.tqdevice()->tqat();
|
||
15 years ago
|
m_sycocaDict->save(str);
|
||
|
|
||
14 years ago
|
int endOfFactoryData = str.tqdevice()->tqat();
|
||
15 years ago
|
|
||
|
// Update header (pass #2)
|
||
|
saveHeader(str);
|
||
|
|
||
|
// Seek to end.
|
||
14 years ago
|
str.tqdevice()->tqat(endOfFactoryData);
|
||
15 years ago
|
}
|
||
|
|
||
|
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!
|
||
|
|
||
14 years ago
|
TQString name = newEntry->name();
|
||
15 years ago
|
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!
|
||
|
|
||
14 years ago
|
TQString name = newEntry->name();
|
||
15 years ago
|
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
|
||
|
|
||
14 years ago
|
m_str->tqdevice()->tqat(m_endEntryOffset);
|
||
14 years ago
|
TQ_INT32 entryCount;
|
||
15 years ago
|
(*m_str) >> entryCount;
|
||
|
|
||
|
if (entryCount > 8192)
|
||
|
{
|
||
|
KSycoca::flagError();
|
||
|
return list;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQ_INT32 *offsetList = new TQ_INT32[entryCount];
|
||
15 years ago
|
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 );*/ }
|
||
|
|