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 if ( isDirectory() ) return QLatin1String("contents2");
73 else return "document2";
76 void DocEntry::setUrl( const QString
&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
)
95 QString
DocEntry::info() const
100 void DocEntry::setLang( const QString
&lang
)
105 QString
DocEntry::lang() const
110 void DocEntry::setIdentifier( const QString
&identifier
)
112 mIdentifier
= identifier
;
115 QString
DocEntry::identifier() const
117 if ( mIdentifier
.isEmpty() ) mIdentifier
= KRandom::randomString( 15 );
121 void DocEntry::setIndexer( const QString
&indexer
)
126 QString
DocEntry::indexer() const
131 void DocEntry::setIndexTestFile( const QString
&indexTestFile
)
133 mIndexTestFile
= indexTestFile
;
136 QString
DocEntry::indexTestFile() const
138 return mIndexTestFile
;
141 void DocEntry::setWeight( int weight
)
146 int DocEntry::weight() const
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
)
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
)
201 bool DocEntry::isDirectory() const
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",
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");
240 bool DocEntry::indexExists( const QString
&indexDir
)
243 if ( mIndexTestFile
.isEmpty() ) {
244 testFile
= identifier() + QLatin1String(".exists");
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() ) {
258 if ( docUrl
.isLocalFile() && !KStandardDirs::exists( docUrl
.path() ) ) {
259 // kDebug(1400) << "URL not found: " << docUrl.url();
267 void DocEntry::addChild( DocEntry
*entry
)
269 entry
->setParent( this );
272 for( i
= 0; i
< mChildren
.count(); ++i
) {
274 if ( entry
->weight() < mChildren
.first()->weight() ) {
275 entry
->setNextSibling( mChildren
.first() );
276 mChildren
.prepend( entry
);
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
);
290 if ( i
== mChildren
.count() ) {
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()
313 void DocEntry::setParent( DocEntry
*parent
)
318 DocEntry
*DocEntry::parent()
323 void DocEntry::setNextSibling( DocEntry
*next
)
328 DocEntry
*DocEntry::nextSibling()
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>";