7 #include <KDesktopFile>
9 #include <KStandardDirs>
10 #include <KApplication>
22 DocEntry::DocEntry( const QString
&name
, const QString
&url
,
35 mSearchEnabled
= false;
41 void DocEntry::setName( const QString
&name
)
46 QString
DocEntry::name() const
51 void DocEntry::setSearch( const QString
&search
)
56 QString
DocEntry::search() const
61 void DocEntry::setIcon( const QString
&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
)
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
)
96 QString
DocEntry::info() const
101 void DocEntry::setLang( const QString
&lang
)
106 QString
DocEntry::lang() const
111 void DocEntry::setIdentifier( const QString
&identifier
)
113 mIdentifier
= identifier
;
116 QString
DocEntry::identifier() const
118 if ( mIdentifier
.isEmpty() ) mIdentifier
= KRandom::randomString( 15 );
122 void DocEntry::setIndexer( const QString
&indexer
)
127 QString
DocEntry::indexer() const
132 void DocEntry::setIndexTestFile( const QString
&indexTestFile
)
134 mIndexTestFile
= indexTestFile
;
137 QString
DocEntry::indexTestFile() const
139 return mIndexTestFile
;
142 void DocEntry::setWeight( int weight
)
147 int DocEntry::weight() const
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
)
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
)
202 bool DocEntry::isDirectory() const
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",
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");
241 bool DocEntry::indexExists( const QString
&indexDir
)
244 if ( mIndexTestFile
.isEmpty() ) {
245 testFile
= identifier() + QLatin1String(".exists");
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() ) {
259 if ( docUrl
.isLocalFile() && !KStandardDirs::exists( docUrl
.path() ) ) {
260 // kDebug(1400) << "URL not found: " << docUrl.url();
268 void DocEntry::addChild( DocEntry
*entry
)
270 entry
->setParent( this );
273 for( i
= 0; i
< mChildren
.count(); ++i
) {
275 if ( entry
->weight() < mChildren
.first()->weight() ) {
276 entry
->setNextSibling( mChildren
.first() );
277 mChildren
.prepend( entry
);
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
);
291 if ( i
== mChildren
.count() ) {
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()
314 void DocEntry::setParent( DocEntry
*parent
)
319 DocEntry
*DocEntry::parent()
324 void DocEntry::setNextSibling( DocEntry
*next
)
329 DocEntry
*DocEntry::nextSibling()
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>";