1#include "docentry.h"
2
3#include <QRegExp>
4#include <QFileInfo>
5
6#include <KDebug>
7#include <KDesktopFile>
8#include <KUrl>
9#include <KStandardDirs>
10#include <KApplication>
11#include <KRandom>
12
13#include "prefs.h"
14
15using namespace KHC;
16
17DocEntry::DocEntry()
18{
19 init();
20}
21
22DocEntry::DocEntry( const QString &name, const QString &url,
23 const QString &icon )
24{
25 init();
26
27 mName = name;
28 mUrl = url;
29 mIcon = icon;
30}
31
32void DocEntry::init()
33{
34 mWeight = 0;
35 mSearchEnabled = false;
36 mDirectory = false;
37 mParent = 0;
38 mNextSibling = 0;
39}
40
41void DocEntry::setName( const QString &name )
42{
43 mName = name;
44}
45
46QString DocEntry::name() const
47{
48 return mName;
49}
50
51void DocEntry::setSearch( const QString &search )
52{
53 mSearch = search;
54}
55
56QString DocEntry::search() const
57{
58 return mSearch;
59}
60
61void DocEntry::setIcon( const QString &icon )
62{
63 mIcon = icon;
64}
65
66QString DocEntry::icon() const
67{
68 if ( !mIcon.isEmpty() ) return mIcon;
69 if ( !docExists() ) return QLatin1String("unknown");
70 if ( isDirectory() ) return QLatin1String("help-contents");
71
72 else return "text-plain";
73}
74
75void DocEntry::setUrl( const QString &url )
76{
77 mUrl = url;
78}
79
80QString DocEntry::url() const
81{
82 if ( !mUrl.isEmpty() ) return mUrl;
83 if ( identifier().isEmpty() ) return QString();
84
85 return "khelpcenter:" + identifier();
86}
87
88void DocEntry::setInfo( const QString &info )
89{
90 mInfo = info;
91}
92
93QString DocEntry::info() const
94{
95 return mInfo;
96}
97
98void DocEntry::setLang( const QString &lang )
99{
100 mLang = lang;
101}
102
103QString DocEntry::lang() const
104{
105 return mLang;
106}
107
108void DocEntry::setIdentifier( const QString &identifier )
109{
110 mIdentifier = identifier;
111}
112
113QString DocEntry::identifier() const
114{
115 if ( mIdentifier.isEmpty() ) mIdentifier = KRandom::randomString( 15 );
116 return mIdentifier;
117}
118
119void DocEntry::setIndexer( const QString &indexer )
120{
121 mIndexer = indexer;
122}
123
124QString DocEntry::indexer() const
125{
126 return mIndexer;
127}
128
129void DocEntry::setIndexTestFile( const QString &indexTestFile )
130{
131 mIndexTestFile = indexTestFile;
132}
133
134QString DocEntry::indexTestFile() const
135{
136 return mIndexTestFile;
137}
138
139void DocEntry::setWeight( int weight )
140{
141 mWeight = weight;
142}
143
144int DocEntry::weight() const
145{
146 return mWeight;
147}
148
149void DocEntry::setSearchMethod( const QString &method )
150{
151 mSearchMethod = method;
152}
153
154QString DocEntry::searchMethod() const
155{
156 return mSearchMethod;
157}
158
159void DocEntry::setDocumentType( const QString &str )
160{
161 mDocumentType = str;
162}
163
164QString DocEntry::documentType() const
165{
166 return mDocumentType;
167}
168
169QString DocEntry::khelpcenterSpecial() const
170{
171 return mKhelpcenterSpecial;
172}
173
174void DocEntry::enableSearch( bool enabled )
175{
176 mSearchEnabled = enabled;
177}
178
179bool DocEntry::searchEnabled() const
180{
181 return mSearchEnabled;
182}
183
184void DocEntry::setSearchEnabledDefault( bool enabled )
185{
186 mSearchEnabledDefault = enabled;
187}
188
189bool DocEntry::searchEnabledDefault() const
190{
191 return mSearchEnabledDefault;
192}
193
194void DocEntry::setDirectory( bool dir )
195{
196 mDirectory = dir;
197}
198
199bool DocEntry::isDirectory() const
200{
201 return mDirectory;
202}
203
204bool DocEntry::readFromFile( const QString &fileName )
205{
206 KDesktopFile file( fileName );
207 KConfigGroup desktopGroup = file.desktopGroup();
208
209 mName = file.readName();
210 mSearch = desktopGroup.readEntry( "X-DOC-Search" );
211 mIcon = file.readIcon();
212 mUrl = file.readDocPath();
213 mInfo = desktopGroup.readEntry( "Info" );
214 if ( mInfo.isNull() )
215 {
216 mInfo = desktopGroup.readEntry( "Comment" );
217 }
218 mLang = desktopGroup.readEntry( "Lang", "en" );
219 mIdentifier = desktopGroup.readEntry( "X-DOC-Identifier" );
220 if ( mIdentifier.isEmpty() )
221 {
222 QFileInfo fi( fileName );
223 mIdentifier = fi.completeBaseName();
224 }
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" );
234
235 mKhelpcenterSpecial = desktopGroup.readEntry("X-KDE-KHelpcenter-Special");
236
237 return true;
238}
239
240bool DocEntry::indexExists( const QString &indexDir )
241{
242 QString testFile;
243 if ( mIndexTestFile.isEmpty() )
244 {
245 testFile = identifier() + QLatin1String(".exists");
246 } else {
247 testFile = mIndexTestFile;
248 }
249
250 if ( !testFile.startsWith( QLatin1Char('/') ) ) testFile = indexDir + QLatin1Char('/') + testFile;
251 return QFile::exists( testFile );
252}
253
254bool DocEntry::docExists() const
255{
256 if ( !mUrl.isEmpty() )
257 {
258 KUrl docUrl( mUrl );
259 if ( docUrl.isLocalFile() && !KStandardDirs::exists( docUrl.toLocalFile() ) )
260 {
261 return false;
262 }
263 }
264
265 return true;
266}
267
268void DocEntry::addChild( DocEntry *entry )
269{
270 entry->setParent( this );
271
272 int i;
273 for( i = 0; i < mChildren.count(); ++i )
274 {
275 if ( i == 0 ) {
276 if ( entry->weight() < mChildren.first()->weight() )
277 {
278 entry->setNextSibling( mChildren.first() );
279 mChildren.prepend( entry );
280 break;
281 }
282 }
283 if ( i + 1 < mChildren.count() )
284 {
285 if ( entry->weight() >= mChildren[ i ]->weight() &&
286 entry->weight() < mChildren[ i + 1 ]->weight() )
287 {
288 entry->setNextSibling( mChildren[ i + 1 ] );
289 mChildren[ i ]->setNextSibling( entry );
290 mChildren.insert( mChildren.indexOf(mChildren.at( i + 1 )), entry );
291 break;
292 }
293 }
294 }
295 if ( i == mChildren.count() )
296 {
297 if ( i > 0 )
298 {
299 mChildren.last()->setNextSibling( entry );
300 }
301 mChildren.append( entry );
302 }
303}
304
305bool DocEntry::hasChildren()
306{
307 return mChildren.count();
308}
309
310DocEntry *DocEntry::firstChild()
311{
312 return mChildren.first();
313}
314
315DocEntry::List DocEntry::children()
316{
317 return mChildren;
318}
319
320void DocEntry::setParent( DocEntry *parent )
321{
322 mParent = parent;
323}
324
325DocEntry *DocEntry::parent()
326{
327 return mParent;
328}
329
330void DocEntry::setNextSibling( DocEntry *next )
331{
332 mNextSibling = next;
333}
334
335DocEntry *DocEntry::nextSibling()
336{
337 return mNextSibling;
338}
339
340bool DocEntry::isSearchable()
341{
342 return !search().isEmpty() && docExists() &&
343 indexExists( Prefs::indexDirectory() );
344}
345
346void DocEntry::dump() const
347{
348 kDebug() << " <docentry>";
349 kDebug() << " <name>" << mName << "</name>";
350 kDebug() << " <searchmethod>" << mSearchMethod << "</searchmethod>";
351 kDebug() << " <search>" << mSearch << "</search>";
352 kDebug() << " <indexer>" << mIndexer << "</indexer>";
353 kDebug() << " <indextestfile>" << mIndexTestFile << "</indextestfile>";
354 kDebug() << " <icon>" << mIcon << "</icon>";
355 kDebug() << " <url>" << mUrl << "</url>";
356 kDebug() << " <documenttype>" << mDocumentType << "</documenttype>";
357 kDebug() << " </docentry>";
358}
359// vim:ts=2:sw=2:et
360