ksycocafactory.cpp
00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 #include "ksycoca.h"
00020 #include "ksycocatype.h"
00021 #include "ksycocafactory.h"
00022 #include "ksycocaentry.h"
00023 #include "ksycocadict.h"
00024 #include <qstringlist.h>
00025 #include <qdict.h>
00026 #include <kdebug.h>
00027 
00028 template class QDict<KSycocaEntry>;
00029 template class QDict<KSharedPtr<KSycocaEntry> >;
00030 
00031 KSycocaFactory::KSycocaFactory(KSycocaFactoryId factory_id)
00032  : m_resourceList(0), m_entryDict(0), m_sycocaDict(0)
00033 {
00034   if (!KSycoca::self()->isBuilding()) 
00035   {
00036       m_str = KSycoca::self()->findFactory( factory_id );
00037       
00038       if (m_str) 
00039       {
00040           
00041           Q_INT32 i;
00042           (*m_str) >> i;
00043           m_sycocaDictOffset = i;
00044           (*m_str) >> i;
00045           m_beginEntryOffset = i;
00046           (*m_str) >> i;
00047           m_endEntryOffset = i;
00048 
00049           int saveOffset = m_str->device()->at();
00050           
00051           m_sycocaDict = new KSycocaDict(m_str, m_sycocaDictOffset);   
00052           saveOffset = m_str->device()->at(saveOffset);
00053       }
00054    }
00055    else
00056    {
00057       
00058       m_str = 0;
00059       m_resourceList = 0;
00060       m_entryDict = new KSycocaEntryDict(977);
00061       m_entryDict->setAutoDelete(true);
00062       m_sycocaDict = new KSycocaDict();
00063       m_beginEntryOffset = 0;
00064       m_endEntryOffset = 0;
00065 
00066       
00067    }
00068    KSycoca::self()->addFactory(this);
00069 }
00070 
00071 KSycocaFactory::~KSycocaFactory()
00072 {
00073    delete m_entryDict;
00074    delete m_sycocaDict;
00075 }
00076 
00077 void
00078 KSycocaFactory::saveHeader(QDataStream &str)
00079 {
00080    
00081    str.device()->at(mOffset);
00082    str << (Q_INT32) m_sycocaDictOffset;
00083    str << (Q_INT32) m_beginEntryOffset;
00084    str << (Q_INT32) m_endEntryOffset;
00085 }
00086 
00087 void
00088 KSycocaFactory::save(QDataStream &str)
00089 {
00090    if (!m_entryDict) return; 
00091                              
00092    if (!m_sycocaDict) return; 
00093 
00094    mOffset = str.device()->at(); 
00095    m_sycocaDictOffset = 0;
00096 
00097    
00098    saveHeader(str);
00099 
00100    m_beginEntryOffset = str.device()->at();
00101 
00102    
00103    int entryCount = 0;
00104    for(QDictIterator<KSycocaEntry::Ptr> it ( *m_entryDict ); 
00105        it.current(); 
00106        ++it)
00107    {
00108       KSycocaEntry *entry = (*it.current());
00109       entry->save(str);
00110       entryCount++;
00111    }
00112 
00113    m_endEntryOffset = str.device()->at();
00114 
00115    
00116    
00117    str << (Q_INT32) entryCount;
00118    for(QDictIterator<KSycocaEntry::Ptr> it ( *m_entryDict ); 
00119        it.current(); 
00120        ++it)
00121    {
00122       KSycocaEntry *entry = (*it.current());
00123       str << (Q_INT32) entry->offset(); 
00124    }
00125 
00126    
00127    m_sycocaDictOffset = str.device()->at();      
00128    m_sycocaDict->save(str);
00129 
00130    int endOfFactoryData = str.device()->at();
00131 
00132    
00133    saveHeader(str);
00134 
00135    
00136    str.device()->at(endOfFactoryData);
00137 }
00138 
00139 void 
00140 KSycocaFactory::addEntry(KSycocaEntry *newEntry, const char *)
00141 {
00142    if (!m_entryDict) return; 
00143                              
00144 
00145    if (!m_sycocaDict) return; 
00146 
00147    QString name = newEntry->name();
00148    m_entryDict->insert( name, new KSycocaEntry::Ptr(newEntry) );
00149    m_sycocaDict->add( name, newEntry );
00150 }
00151 
00152 void 
00153 KSycocaFactory::removeEntry(KSycocaEntry *newEntry)
00154 {
00155    if (!m_entryDict) return; 
00156                              
00157 
00158    if (!m_sycocaDict) return; 
00159 
00160    QString name = newEntry->name();
00161    m_entryDict->remove( name );
00162    m_sycocaDict->remove( name );
00163 }
00164 
00165 KSycocaEntry::List KSycocaFactory::allEntries()
00166 {
00167    KSycocaEntry::List list;
00168    if (!m_str) return list;
00169 
00170    
00171 
00172    m_str->device()->at(m_endEntryOffset);
00173    Q_INT32 entryCount;
00174    (*m_str) >> entryCount;
00175    
00176    if (entryCount > 8192)
00177    {
00178       KSycoca::flagError();
00179       return list;
00180    }
00181 
00182    Q_INT32 *offsetList = new Q_INT32[entryCount];
00183    for(int i = 0; i < entryCount; i++)
00184    {
00185       (*m_str) >> offsetList[i];
00186    }
00187 
00188    for(int i = 0; i < entryCount; i++)
00189    {
00190       KSycocaEntry *newEntry = createEntry(offsetList[i]);
00191       if (newEntry)
00192       {
00193          list.append( KSycocaEntry::Ptr( newEntry ) );
00194       }
00195    }
00196    delete [] offsetList;
00197    return list;
00198 }
00199 
00200 void KSycocaFactory::virtual_hook( int , void* )
00201 {  }
00202 
 
This file is part of the documentation for kdecore Library Version 3.2.0.