Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / khelpcenter / docentry.cpp
blobe464e345cd641e87ba6d0b94ca0fba36fce25ee0
1 #include "docentry.h"
3 #include <QRegExp>
4 #include <QFileInfo>
6 #include <KDebug>
7 #include <KDesktopFile>
8 #include <KUrl>
9 #include <KStandardDirs>
10 #include <KApplication>
11 #include <KRandom>
13 #include "prefs.h"
15 using namespace KHC;
17 DocEntry::DocEntry()
19 init();
22 DocEntry::DocEntry( const QString &name, const QString &url,
23 const QString &icon )
25 init();
27 mName = name;
28 mUrl = url;
29 mIcon = icon;
32 void DocEntry::init()
34 mWeight = 0;
35 mSearchEnabled = false;
36 mDirectory = false;
37 mParent = 0;
38 mNextSibling = 0;
41 void DocEntry::setName( const QString &name )
43 mName = name;
46 QString DocEntry::name() const
48 return mName;
51 void DocEntry::setSearch( const QString &search )
53 mSearch = search;
56 QString DocEntry::search() const
58 return mSearch;
61 void DocEntry::setIcon( const QString &icon )
63 mIcon = icon;
66 QString DocEntry::icon() const
68 if ( !mIcon.isEmpty() ) return mIcon;
70 if ( !docExists() ) return QLatin1String("unknown");
72 if ( isDirectory() ) return QLatin1String("contents2");
73 else return "document2";
76 void DocEntry::setUrl( const QString &url )
78 mUrl = url;
81 QString DocEntry::url() const
83 if ( !mUrl.isEmpty() ) return mUrl;
85 if ( identifier().isEmpty() ) return QString();
87 return "khelpcenter:" + identifier();
90 void DocEntry::setInfo( const QString &info )
92 mInfo = info;
95 QString DocEntry::info() const
97 return mInfo;
100 void DocEntry::setLang( const QString &lang )
102 mLang = lang;
105 QString DocEntry::lang() const
107 return mLang;
110 void DocEntry::setIdentifier( const QString &identifier )
112 mIdentifier = identifier;
115 QString DocEntry::identifier() const
117 if ( mIdentifier.isEmpty() ) mIdentifier = KRandom::randomString( 15 );
118 return mIdentifier;
121 void DocEntry::setIndexer( const QString &indexer )
123 mIndexer = indexer;
126 QString DocEntry::indexer() const
128 return mIndexer;
131 void DocEntry::setIndexTestFile( const QString &indexTestFile )
133 mIndexTestFile = indexTestFile;
136 QString DocEntry::indexTestFile() const
138 return mIndexTestFile;
141 void DocEntry::setWeight( int weight )
143 mWeight = weight;
146 int DocEntry::weight() const
148 return mWeight;
151 void DocEntry::setSearchMethod( const QString &method )
153 mSearchMethod = method;
156 QString DocEntry::searchMethod() const
158 return mSearchMethod;
161 void DocEntry::setDocumentType( const QString &str )
163 mDocumentType = str;
166 QString DocEntry::documentType() const
168 return mDocumentType;
171 QString DocEntry::khelpcenterSpecial() const
173 return mKhelpcenterSpecial;
176 void DocEntry::enableSearch( bool enabled )
178 mSearchEnabled = enabled;
181 bool DocEntry::searchEnabled() const
183 return mSearchEnabled;
186 void DocEntry::setSearchEnabledDefault( bool enabled )
188 mSearchEnabledDefault = enabled;
191 bool DocEntry::searchEnabledDefault() const
193 return mSearchEnabledDefault;
196 void DocEntry::setDirectory( bool dir )
198 mDirectory = dir;
201 bool DocEntry::isDirectory() const
203 return mDirectory;
206 bool DocEntry::readFromFile( const QString &fileName )
208 KDesktopFile file( fileName );
209 KConfigGroup desktopGroup = file.desktopGroup();
211 mName = file.readName();
212 mSearch = desktopGroup.readEntry( "X-DOC-Search" );
213 mIcon = file.readIcon();
214 mUrl = file.readDocPath();
215 mInfo = desktopGroup.readEntry( "Info" );
216 if ( mInfo.isNull() ) {
217 mInfo = desktopGroup.readEntry( "Comment" );
219 mLang = desktopGroup.readEntry( "Lang", "en" );
220 mIdentifier = desktopGroup.readEntry( "X-DOC-Identifier" );
221 if ( mIdentifier.isEmpty() ) {
222 QFileInfo fi( fileName );
223 mIdentifier = fi.completeBaseName();
225 mIndexer = desktopGroup.readEntry( "X-DOC-Indexer" );
226 mIndexer.replace( "%f", fileName );
227 mIndexTestFile = desktopGroup.readEntry( "X-DOC-IndexTestFile" );
228 mSearchEnabledDefault = desktopGroup.readEntry( "X-DOC-SearchEnabledDefault",
229 false );
230 mSearchEnabled = mSearchEnabledDefault;
231 mWeight = desktopGroup.readEntry( "X-DOC-Weight", 0 );
232 mSearchMethod = desktopGroup.readEntry( "X-DOC-SearchMethod" );
233 mDocumentType = desktopGroup.readEntry( "X-DOC-DocumentType" );
235 mKhelpcenterSpecial = desktopGroup.readEntry("X-KDE-KHelpcenter-Special");
237 return true;
240 bool DocEntry::indexExists( const QString &indexDir )
242 QString testFile;
243 if ( mIndexTestFile.isEmpty() ) {
244 testFile = identifier() + QLatin1String(".exists");
245 } else {
246 testFile = mIndexTestFile;
249 if ( !testFile.startsWith( QLatin1Char('/') ) ) testFile = indexDir + QLatin1Char('/') + testFile;
251 return QFile::exists( testFile );
254 bool DocEntry::docExists() const
256 if ( !mUrl.isEmpty() ) {
257 KUrl docUrl( mUrl );
258 if ( docUrl.isLocalFile() && !KStandardDirs::exists( docUrl.path() ) ) {
259 // kDebug(1400) << "URL not found: " << docUrl.url();
260 return false;
264 return true;
267 void DocEntry::addChild( DocEntry *entry )
269 entry->setParent( this );
271 int i;
272 for( i = 0; i < mChildren.count(); ++i ) {
273 if ( i == 0 ) {
274 if ( entry->weight() < mChildren.first()->weight() ) {
275 entry->setNextSibling( mChildren.first() );
276 mChildren.prepend( entry );
277 break;
280 if ( i + 1 < mChildren.count() ) {
281 if ( entry->weight() >= mChildren[ i ]->weight() &&
282 entry->weight() < mChildren[ i + 1 ]->weight() ) {
283 entry->setNextSibling( mChildren[ i + 1 ] );
284 mChildren[ i ]->setNextSibling( entry );
285 mChildren.insert( mChildren.indexOf(mChildren.at( i + 1 )), entry );
286 break;
290 if ( i == mChildren.count() ) {
291 if ( i > 0 ) {
292 mChildren.last()->setNextSibling( entry );
294 mChildren.append( entry );
298 bool DocEntry::hasChildren()
300 return mChildren.count();
303 DocEntry *DocEntry::firstChild()
305 return mChildren.first();
308 DocEntry::List DocEntry::children()
310 return mChildren;
313 void DocEntry::setParent( DocEntry *parent )
315 mParent = parent;
318 DocEntry *DocEntry::parent()
320 return mParent;
323 void DocEntry::setNextSibling( DocEntry *next )
325 mNextSibling = next;
328 DocEntry *DocEntry::nextSibling()
330 return mNextSibling;
333 bool DocEntry::isSearchable()
335 return !search().isEmpty() && docExists() &&
336 indexExists( Prefs::indexDirectory() );
339 void DocEntry::dump() const
341 kDebug() << " <docentry>";
342 kDebug() << " <name>" << mName << "</name>";
343 kDebug() << " <searchmethod>" << mSearchMethod << "</searchmethod>";
344 kDebug() << " <search>" << mSearch << "</search>";
345 kDebug() << " <indexer>" << mIndexer << "</indexer>";
346 kDebug() << " <indextestfile>" << mIndexTestFile << "</indextestfile>";
347 kDebug() << " <icon>" << mIcon << "</icon>";
348 kDebug() << " <url>" << mUrl << "</url>";
349 kDebug() << " <documenttype>" << mDocumentType << "</documenttype>";
350 kDebug() << " </docentry>";
352 // vim:ts=2:sw=2:et