2 * This file is part of the KDE Help Center
4 * Copyright (C) 1999 Matthias Elter (me@kde.org)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "scrollkeepertreebuilder.h"
23 #include "navigatoritem.h"
26 #include <kapplication.h>
33 #include <QtXml/QtXml>
36 #include <kconfiggroup.h>
40 ScrollKeeperTreeBuilder::ScrollKeeperTreeBuilder( QObject
*parent
, const char *name
)
43 setObjectName( name
);
48 void ScrollKeeperTreeBuilder::loadConfig()
50 KConfigGroup
configGroup( KGlobal::config(), "ScrollKeeper" );
51 mShowEmptyDirs
= configGroup
.readEntry( "ShowEmptyDirs", false);
54 NavigatorItem
*ScrollKeeperTreeBuilder::build( NavigatorItem
*parent
,
55 NavigatorItem
*after
)
57 QString lang
= KGlobal::locale()->language();
59 kDebug(1400) << "ScrollKeeper language: " << lang
;
62 proc
<< "scrollkeeper-get-content-list";
65 proc
.setOutputChannelMode(KProcess::OnlyStdoutChannel
);
67 if ( !proc
.waitForFinished() ) {
68 kDebug(1400) << "Could not execute scrollkeeper-get-content-list";
71 mContentsList
= proc
.readAllStandardOutput().trimmed();
73 if (!QFile::exists(mContentsList
)) {
74 kDebug(1400) << "Scrollkeeper contents file '" << mContentsList
75 << "' does not exist." << endl
;
79 QDomDocument
doc("ScrollKeeperContentsList");
80 QFile
f(mContentsList
);
81 if ( !f
.open( QIODevice::ReadOnly
) )
83 if ( !doc
.setContent( &f
) ) {
89 // Create top-level item
90 mItems
.append(parent
);
92 QDomElement docElem
= doc
.documentElement();
94 NavigatorItem
*result
= 0;
96 QDomNode n
= docElem
.firstChild();
97 while( !n
.isNull() ) {
98 QDomElement e
= n
.toElement();
100 if (e
.tagName() == "sect") {
101 NavigatorItem
*createdItem
;
102 insertSection( parent
, after
, e
, createdItem
);
103 if ( createdItem
) result
= createdItem
;
112 int ScrollKeeperTreeBuilder::insertSection( NavigatorItem
*parent
,
113 NavigatorItem
*after
,
114 const QDomNode
§Node
,
115 NavigatorItem
*§Item
)
117 // TODO: was contents2 -> needs to be changed to help-contents-alternate or similar
118 DocEntry
*entry
= new DocEntry( "", "", "help-contents" );
119 sectItem
= new NavigatorItem( entry
, parent
, after
);
120 sectItem
->setAutoDeleteDocEntry( true );
121 mItems
.append( sectItem
);
123 int numDocs
= 0; // Number of docs created in this section
125 QDomNode n
= sectNode
.firstChild();
126 while( !n
.isNull() ) {
127 QDomElement e
= n
.toElement();
129 if ( e
.tagName() == "title" ) {
130 entry
->setName( e
.text() );
131 sectItem
->updateItem();
132 } else if (e
.tagName() == "sect") {
133 NavigatorItem
*created
;
134 numDocs
+= insertSection( sectItem
, 0, e
, created
);
135 } else if (e
.tagName() == "doc") {
136 insertDoc(sectItem
,e
);
143 // Remove empty sections
144 if (!mShowEmptyDirs
&& numDocs
== 0) {
152 void ScrollKeeperTreeBuilder::insertDoc( NavigatorItem
*parent
,
153 const QDomNode
&docNode
)
155 DocEntry
*entry
= new DocEntry( "", "", "text-plain" );
156 NavigatorItem
*docItem
= new NavigatorItem( entry
, parent
);
157 docItem
->setAutoDeleteDocEntry( true );
158 mItems
.append( docItem
);
162 QDomNode n
= docNode
.firstChild();
163 while( !n
.isNull() ) {
164 QDomElement e
= n
.toElement();
166 if ( e
.tagName() == "doctitle" ) {
167 entry
->setName( e
.text() );
168 docItem
->updateItem();
169 } else if ( e
.tagName() == "docsource" ) {
170 url
.append( e
.text() );
171 } else if ( e
.tagName() == "docformat" ) {
172 QString mimeType
= e
.text();
173 if ( mimeType
== "text/html") {
174 // Let the HTML part figure out how to get the doc
175 } else if ( mimeType
== "application/xml"
176 || mimeType
== "text/xml" /*deprecated name*/ ) {
177 if ( url
.left( 5 ) == "file:" ) url
= url
.mid( 5 );
178 url
.prepend( "ghelp:" );
180 url
.replace( QRegExp( ".xml$" ), ".html" );
182 } else if ( mimeType
== "text/sgml" ) {
183 // GNOME docs use this type. We don't have a real viewer for this.
184 url
.prepend( "file:" );
185 } else if ( mimeType
.left(5) == "text/" ) {
186 url
.prepend( "file:" );
193 entry
->setUrl( url
);
196 #include "scrollkeepertreebuilder.moc"