2 #include "docmetainfo.h"
7 #include <kstandarddirs.h>
10 #include <kconfiggroup.h>
12 #include "htmlsearch.h"
14 #include "docentrytraverser.h"
18 bool DocMetaInfo::mLoaded
= false;
20 DocMetaInfo
*DocMetaInfo::mSelf
= 0;
22 DocMetaInfo
*DocMetaInfo::self()
24 if ( !mSelf
) mSelf
= new DocMetaInfo
;
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
) {
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( '.');
61 if ( extensions
.count() >= 2 ) {
62 lang
= extensions
[ extensions
.count() - 2 ];
65 if ( !lang
.isEmpty() && !mLanguages
.contains( lang
) ) {
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)",
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
);
92 void DocMetaInfo::addDocEntry( DocEntry
*entry
)
94 mDocEntries
.append( entry
);
95 if ( !entry
->search().isEmpty() ) mSearchEntries
.append( entry
);
98 DocEntry::List
DocMetaInfo::docEntries()
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
);
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
);
154 DocEntry
*DocMetaInfo::scanMetaInfoDir( const QString
&dirName
,
158 if ( !dir
.exists() ) return 0;
160 foreach( const QFileInfo
&fi
, dir
.entryInfoList(QDir::Dirs
| QDir::Files
| QDir::NoDotAndDotDot
) ) {
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
);
174 DocEntry
*DocMetaInfo::addDirEntry( const QDir
&dir
, DocEntry
*parent
)
176 DocEntry
*dirEntry
= addDocEntry( dir
.absolutePath() + QLatin1String("/.directory") );
179 dirEntry
= new DocEntry
;
180 dirEntry
->setName( dir
.dirName() );
181 addDocEntry( dirEntry
);
184 dirEntry
->setDirectory( true );
186 if ( parent
) parent
->addChild( 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
);
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();
228 kDebug() << "DocMetaInfo::startTraverseEntry(): ERROR. No Traverser."
234 kDebug() << "DocMetaInfo::startTraverseEntry(): no entry.";
235 endTraverseEntries( traverser
);
239 traverser
->startProcess( entry
);
242 void DocMetaInfo::endProcess( DocEntry
*entry
, DocEntryTraverser
*traverser
)
244 // kDebug() << "DocMetaInfo::endProcess() " << entry->name();
247 endTraverseEntries( traverser
);
251 if ( entry
->hasChildren() ) {
252 startTraverseEntry( entry
->firstChild(), traverser
->childTraverser( entry
) );
253 } else if ( entry
->nextSibling() ) {
254 startTraverseEntry( entry
->nextSibling(), traverser
);
256 DocEntry
*parent
= entry
->parent();
257 DocEntryTraverser
*parentTraverser
= 0;
259 parentTraverser
= traverser
->parentTraverser();
260 traverser
->deleteTraverser();
261 if ( parent
->nextSibling() ) {
262 startTraverseEntry( parent
->nextSibling(), parentTraverser
);
265 parent
= parent
->parent();
266 traverser
= parentTraverser
;
270 endTraverseEntries( traverser
);
275 void DocMetaInfo::endTraverseEntries( DocEntryTraverser
*traverser
)
277 kDebug() << "DocMetaInfo::endTraverseEntries()";
280 kDebug() << " no more traversers.";
284 traverser
->finishTraversal();