delay a few things on startup, such as setting the visibility mode, which ensures...
[personal-kdebase.git] / runtime / khelpcenter / docentry.cpp
blob9f5048cf9dd0db1ef40a27ef7bf0285055cd7cf7
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 // TODO: was contents2 -> needs to be changed to help-contents-alternate or similar
73 if ( isDirectory() ) return QLatin1String("help-contents");
74 else return "text-plain";
77 void DocEntry::setUrl( const QString &url )
79 mUrl = url;
82 QString DocEntry::url() const
84 if ( !mUrl.isEmpty() ) return mUrl;
86 if ( identifier().isEmpty() ) return QString();
88 return "khelpcenter:" + identifier();
91 void DocEntry::setInfo( const QString &info )
93 mInfo = info;
96 QString DocEntry::info() const
98 return mInfo;
101 void DocEntry::setLang( const QString &lang )
103 mLang = lang;
106 QString DocEntry::lang() const
108 return mLang;
111 void DocEntry::setIdentifier( const QString &identifier )
113 mIdentifier = identifier;
116 QString DocEntry::identifier() const
118 if ( mIdentifier.isEmpty() ) mIdentifier = KRandom::randomString( 15 );
119 return mIdentifier;
122 void DocEntry::setIndexer( const QString &indexer )
124 mIndexer = indexer;
127 QString DocEntry::indexer() const
129 return mIndexer;
132 void DocEntry::setIndexTestFile( const QString &indexTestFile )
134 mIndexTestFile = indexTestFile;
137 QString DocEntry::indexTestFile() const
139 return mIndexTestFile;
142 void DocEntry::setWeight( int weight )
144 mWeight = weight;
147 int DocEntry::weight() const
149 return mWeight;
152 void DocEntry::setSearchMethod( const QString &method )
154 mSearchMethod = method;
157 QString DocEntry::searchMethod() const
159 return mSearchMethod;
162 void DocEntry::setDocumentType( const QString &str )
164 mDocumentType = str;
167 QString DocEntry::documentType() const
169 return mDocumentType;
172 QString DocEntry::khelpcenterSpecial() const
174 return mKhelpcenterSpecial;
177 void DocEntry::enableSearch( bool enabled )
179 mSearchEnabled = enabled;
182 bool DocEntry::searchEnabled() const
184 return mSearchEnabled;
187 void DocEntry::setSearchEnabledDefault( bool enabled )
189 mSearchEnabledDefault = enabled;
192 bool DocEntry::searchEnabledDefault() const
194 return mSearchEnabledDefault;
197 void DocEntry::setDirectory( bool dir )
199 mDirectory = dir;
202 bool DocEntry::isDirectory() const
204 return mDirectory;
207 bool DocEntry::readFromFile( const QString &fileName )
209 KDesktopFile file( fileName );
210 KConfigGroup desktopGroup = file.desktopGroup();
212 mName = file.readName();
213 mSearch = desktopGroup.readEntry( "X-DOC-Search" );
214 mIcon = file.readIcon();
215 mUrl = file.readDocPath();
216 mInfo = desktopGroup.readEntry( "Info" );
217 if ( mInfo.isNull() ) {
218 mInfo = desktopGroup.readEntry( "Comment" );
220 mLang = desktopGroup.readEntry( "Lang", "en" );
221 mIdentifier = desktopGroup.readEntry( "X-DOC-Identifier" );
222 if ( mIdentifier.isEmpty() ) {
223 QFileInfo fi( fileName );
224 mIdentifier = fi.completeBaseName();
226 mIndexer = desktopGroup.readEntry( "X-DOC-Indexer" );
227 mIndexer.replace( "%f", fileName );
228 mIndexTestFile = desktopGroup.readEntry( "X-DOC-IndexTestFile" );
229 mSearchEnabledDefault = desktopGroup.readEntry( "X-DOC-SearchEnabledDefault",
230 false );
231 mSearchEnabled = mSearchEnabledDefault;
232 mWeight = desktopGroup.readEntry( "X-DOC-Weight", 0 );
233 mSearchMethod = desktopGroup.readEntry( "X-DOC-SearchMethod" );
234 mDocumentType = desktopGroup.readEntry( "X-DOC-DocumentType" );
236 mKhelpcenterSpecial = desktopGroup.readEntry("X-KDE-KHelpcenter-Special");
238 return true;
241 bool DocEntry::indexExists( const QString &indexDir )
243 QString testFile;
244 if ( mIndexTestFile.isEmpty() ) {
245 testFile = identifier() + QLatin1String(".exists");
246 } else {
247 testFile = mIndexTestFile;
250 if ( !testFile.startsWith( QLatin1Char('/') ) ) testFile = indexDir + QLatin1Char('/') + testFile;
252 return QFile::exists( testFile );
255 bool DocEntry::docExists() const
257 if ( !mUrl.isEmpty() ) {
258 KUrl docUrl( mUrl );
259 if ( docUrl.isLocalFile() && !KStandardDirs::exists( docUrl.path() ) ) {
260 // kDebug(1400) << "URL not found: " << docUrl.url();
261 return false;
265 return true;
268 void DocEntry::addChild( DocEntry *entry )
270 entry->setParent( this );
272 int i;
273 for( i = 0; i < mChildren.count(); ++i ) {
274 if ( i == 0 ) {
275 if ( entry->weight() < mChildren.first()->weight() ) {
276 entry->setNextSibling( mChildren.first() );
277 mChildren.prepend( entry );
278 break;
281 if ( i + 1 < mChildren.count() ) {
282 if ( entry->weight() >= mChildren[ i ]->weight() &&
283 entry->weight() < mChildren[ i + 1 ]->weight() ) {
284 entry->setNextSibling( mChildren[ i + 1 ] );
285 mChildren[ i ]->setNextSibling( entry );
286 mChildren.insert( mChildren.indexOf(mChildren.at( i + 1 )), entry );
287 break;
291 if ( i == mChildren.count() ) {
292 if ( i > 0 ) {
293 mChildren.last()->setNextSibling( entry );
295 mChildren.append( entry );
299 bool DocEntry::hasChildren()
301 return mChildren.count();
304 DocEntry *DocEntry::firstChild()
306 return mChildren.first();
309 DocEntry::List DocEntry::children()
311 return mChildren;
314 void DocEntry::setParent( DocEntry *parent )
316 mParent = parent;
319 DocEntry *DocEntry::parent()
321 return mParent;
324 void DocEntry::setNextSibling( DocEntry *next )
326 mNextSibling = next;
329 DocEntry *DocEntry::nextSibling()
331 return mNextSibling;
334 bool DocEntry::isSearchable()
336 return !search().isEmpty() && docExists() &&
337 indexExists( Prefs::indexDirectory() );
340 void DocEntry::dump() const
342 kDebug() << " <docentry>";
343 kDebug() << " <name>" << mName << "</name>";
344 kDebug() << " <searchmethod>" << mSearchMethod << "</searchmethod>";
345 kDebug() << " <search>" << mSearch << "</search>";
346 kDebug() << " <indexer>" << mIndexer << "</indexer>";
347 kDebug() << " <indextestfile>" << mIndexTestFile << "</indextestfile>";
348 kDebug() << " <icon>" << mIcon << "</icon>";
349 kDebug() << " <url>" << mUrl << "</url>";
350 kDebug() << " <documenttype>" << mDocumentType << "</documenttype>";
351 kDebug() << " </docentry>";
353 // vim:ts=2:sw=2:et