delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / khelpcenter / docmetainfo.cpp
blobc4d2d67484cd11c2b38152b8416778cc69b144ca
2 #include "docmetainfo.h"
4 #include <QRegExp>
6 #include <kdebug.h>
7 #include <kstandarddirs.h>
8 #include <klocale.h>
9 #include <kconfig.h>
10 #include <kconfiggroup.h>
12 #include "htmlsearch.h"
14 #include "docentrytraverser.h"
16 using namespace KHC;
18 bool DocMetaInfo::mLoaded = false;
20 DocMetaInfo *DocMetaInfo::mSelf = 0;
22 DocMetaInfo *DocMetaInfo::self()
24 if ( !mSelf ) mSelf = new DocMetaInfo;
25 return mSelf;
28 DocMetaInfo::DocMetaInfo()
30 kDebug() << "DocMetaInfo()";
32 mHtmlSearch = new HTMLSearch;
34 mRootEntry.setName( i18n("Top-Level Documentation") );
37 DocMetaInfo::~DocMetaInfo()
39 kDebug() << "~DocMetaInfo()";
41 DocEntry::List::ConstIterator it;
42 for( it = mDocEntries.constBegin(); it != mDocEntries.constEnd(); ++it ) {
43 delete *it;
46 delete mHtmlSearch;
48 mLoaded = false;
50 mSelf = 0;
53 DocEntry *DocMetaInfo::addDocEntry( const QString &fileName )
55 QFileInfo fi( fileName );
56 if ( !fi.exists() ) return 0;
58 QString extension = fi.completeSuffix();
59 QStringList extensions = extension.split( '.');
60 QString lang;
61 if ( extensions.count() >= 2 ) {
62 lang = extensions[ extensions.count() - 2 ];
65 if ( !lang.isEmpty() && !mLanguages.contains( lang ) ) {
66 return 0;
69 DocEntry *entry = new DocEntry();
71 if ( entry->readFromFile( fileName ) ) {
72 if ( !lang.isEmpty() && lang != mLanguages.first() ) {
73 entry->setLang( lang );
74 entry->setName( i18nc("doctitle (language)","%1 (%2)",
75 entry->name() ,
76 mLanguageNames[ lang ] ) );
78 if ( entry->searchMethod().toLower() == "htdig" ) {
79 mHtmlSearch->setupDocEntry( entry );
81 QString indexer = entry->indexer();
82 indexer.replace( "%f", fileName );
83 entry->setIndexer( indexer );
84 addDocEntry( entry );
85 return entry;
86 } else {
87 delete entry;
88 return 0;
92 void DocMetaInfo::addDocEntry( DocEntry *entry )
94 mDocEntries.append( entry );
95 if ( !entry->search().isEmpty() ) mSearchEntries.append( entry );
98 DocEntry::List DocMetaInfo::docEntries()
100 return mDocEntries;
103 DocEntry::List DocMetaInfo::searchEntries()
105 return mSearchEntries;
108 QString DocMetaInfo::languageName( const QString &langcode )
110 if ( langcode == "en" ) return i18nc("Describes documentation entries that are in English","English");
112 QString cfgfile = KStandardDirs::locate( "locale",
113 QString::fromLatin1( "%1/entry.desktop" ).arg( langcode ) );
115 kDebug() << "-- langcode: " << langcode << " cfgfile: " << cfgfile;
117 KConfig _cfg( cfgfile, KConfig::SimpleConfig );
118 KConfigGroup cfg(&_cfg, "KCM Locale" );
119 QString name = cfg.readEntry( "Name", langcode );
121 return name;
124 void DocMetaInfo::scanMetaInfo( bool force )
126 if ( mLoaded && !force ) return;
128 mLanguages = KGlobal::locale()->languageList();
130 kDebug( 1400 ) << "LANGS: " << mLanguages.join( QLatin1String(" ") );
132 QStringList::ConstIterator it;
133 for( it = mLanguages.constBegin(); it != mLanguages.constEnd(); ++it ) {
134 mLanguageNames.insert( *it, languageName( *it ) );
137 KConfig config( QLatin1String("khelpcenterrc") );
138 KConfigGroup cg(&config, "General");
139 QStringList metaInfos = cg.readEntry( "MetaInfoDirs" , QStringList() );
141 if ( metaInfos.isEmpty() ) {
142 KStandardDirs* kstd = KGlobal::dirs();
143 //kstd->addResourceType( "data", 0, QLatin1String("share/apps/khelpcenter") );
144 metaInfos = kstd->findDirs( "appdata", "plugins" );
146 for( it = metaInfos.constBegin(); it != metaInfos.constEnd(); ++it) {
147 kDebug() << "DocMetaInfo::scanMetaInfo(): scanning " << *it;
148 scanMetaInfoDir( *it, &mRootEntry );
151 mLoaded = true;
154 DocEntry *DocMetaInfo::scanMetaInfoDir( const QString &dirName,
155 DocEntry *parent )
157 QDir dir( dirName );
158 if ( !dir.exists() ) return 0;
160 foreach( const QFileInfo &fi, dir.entryInfoList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot) ) {
161 DocEntry *entry = 0;
162 if ( fi.isDir() ) {
163 DocEntry *dirEntry = addDirEntry( QDir( fi.absoluteFilePath() ), parent );
164 entry = scanMetaInfoDir( fi.absoluteFilePath(), dirEntry );
165 } else if ( fi.suffix() == QLatin1String("desktop") ) {
166 entry = addDocEntry( fi.absoluteFilePath() );
167 if ( parent && entry ) parent->addChild( entry );
171 return 0;
174 DocEntry *DocMetaInfo::addDirEntry( const QDir &dir, DocEntry *parent )
176 DocEntry *dirEntry = addDocEntry( dir.absolutePath() + QLatin1String("/.directory") );
178 if ( !dirEntry ) {
179 dirEntry = new DocEntry;
180 dirEntry->setName( dir.dirName() );
181 addDocEntry( dirEntry );
184 dirEntry->setDirectory( true );
186 if ( parent ) parent->addChild( dirEntry );
188 return dirEntry;
192 void DocMetaInfo::traverseEntries( DocEntryTraverser *traverser )
194 traverseEntry( &mRootEntry, traverser );
197 void DocMetaInfo::traverseEntry( DocEntry *entry, DocEntryTraverser *traverser )
199 DocEntry::List children = entry->children();
200 DocEntry::List::ConstIterator it;
201 for( it = children.constBegin(); it != children.constEnd(); ++it ) {
202 if ( (*it)->isDirectory() && !(*it)->hasChildren() &&
203 (*it)->khelpcenterSpecial().isEmpty() ) continue;
204 traverser->process( *it );
205 if ( (*it)->hasChildren() ) {
206 DocEntryTraverser *t = traverser->childTraverser( *it );
207 if (t) {
208 traverseEntry( *it, t );
209 t->deleteTraverser();
215 void DocMetaInfo::startTraverseEntries( DocEntryTraverser *traverser )
217 kDebug() << "DocMetaInfo::startTraverseEntries()";
218 traverser->setNotifyee( this );
219 startTraverseEntry( &mRootEntry, traverser );
222 void DocMetaInfo::startTraverseEntry( DocEntry *entry,
223 DocEntryTraverser *traverser )
225 // kDebug() << "DocMetaInfo::startTraverseEntry() " << entry->name();
227 if ( !traverser ) {
228 kDebug() << "DocMetaInfo::startTraverseEntry(): ERROR. No Traverser."
229 << endl;
230 return;
233 if ( !entry ) {
234 kDebug() << "DocMetaInfo::startTraverseEntry(): no entry.";
235 endTraverseEntries( traverser );
236 return;
239 traverser->startProcess( entry );
242 void DocMetaInfo::endProcess( DocEntry *entry, DocEntryTraverser *traverser )
244 // kDebug() << "DocMetaInfo::endProcess() " << entry->name();
246 if ( !entry ) {
247 endTraverseEntries( traverser );
248 return;
251 if ( entry->hasChildren() ) {
252 startTraverseEntry( entry->firstChild(), traverser->childTraverser( entry ) );
253 } else if ( entry->nextSibling() ) {
254 startTraverseEntry( entry->nextSibling(), traverser );
255 } else {
256 DocEntry *parent = entry->parent();
257 DocEntryTraverser *parentTraverser = 0;
258 while ( parent ) {
259 parentTraverser = traverser->parentTraverser();
260 traverser->deleteTraverser();
261 if ( parent->nextSibling() ) {
262 startTraverseEntry( parent->nextSibling(), parentTraverser );
263 break;
264 } else {
265 parent = parent->parent();
266 traverser = parentTraverser;
269 if ( !parent ) {
270 endTraverseEntries( traverser );
275 void DocMetaInfo::endTraverseEntries( DocEntryTraverser *traverser )
277 kDebug() << "DocMetaInfo::endTraverseEntries()";
279 if ( !traverser ) {
280 kDebug() << " no more traversers.";
281 return;
284 traverser->finishTraversal();
286 // vim:ts=2:sw=2:et