2 * This file is part of the KDE Help Center
4 * Copyright (C) 2002 Frerich Raabe (raabe@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.
23 #include "navigatoritem.h"
26 #include <kapplication.h>
28 #include <kconfiggroup.h>
30 #include <kiconloader.h>
31 #include <k3listview.h>
33 #include <kstandarddirs.h>
37 #include <QTextStream>
39 #include <stdlib.h> // for getenv()
44 class InfoCategoryItem
: public NavigatorItem
47 InfoCategoryItem( NavigatorItem
*parent
, const QString
&text
);
49 virtual void setOpen( bool open
);
52 class InfoNodeItem
: public NavigatorItem
55 InfoNodeItem( InfoCategoryItem
*parent
, const QString
&text
);
58 InfoCategoryItem::InfoCategoryItem( NavigatorItem
*parent
, const QString
&text
)
59 : NavigatorItem( new DocEntry( text
), parent
)
61 setAutoDeleteDocEntry( true );
63 // kDebug(1400) << "Got category: " << text;
66 void InfoCategoryItem::setOpen( bool open
)
68 NavigatorItem::setOpen( open
);
70 if ( open
&& childCount() > 0 ) setPixmap( 0, SmallIcon( "help-contents" ) );
71 // TODO: was contents2 -> needs to be changed to help-contents-alternate or similar
72 else setPixmap( 0, SmallIcon( "help-contents" ) );
75 InfoNodeItem::InfoNodeItem( InfoCategoryItem
*parent
, const QString
&text
)
76 : NavigatorItem( new DocEntry( text
), parent
)
78 setAutoDeleteDocEntry( true );
79 // kDebug( 1400 ) << "Created info node item: " << text;
82 InfoTree::InfoTree( QObject
*parent
)
83 : TreeBuilder( parent
),
88 void InfoTree::build( NavigatorItem
*parent
)
90 kDebug( 1400 ) << "Populating info tree.";
92 m_parentItem
= parent
;
94 DocEntry
*entry
= new DocEntry( i18n( "Alphabetically" ) );
95 m_alphabItem
= new NavigatorItem( entry
, parent
);
96 m_alphabItem
->setAutoDeleteDocEntry( true );
97 entry
= new DocEntry( i18n( "By Category" ) );
98 m_categoryItem
= new NavigatorItem( entry
, parent
);
99 m_categoryItem
->setAutoDeleteDocEntry( true );
101 KConfigGroup
cfg(KGlobal::config(), "Info pages");
102 QStringList infoDirFiles
= cfg
.readEntry( "Search paths" , QStringList() );
103 // Default paths taken fron kdebase/kioslave/info/kde-info2html.conf
104 if ( infoDirFiles
.isEmpty() ) {
105 infoDirFiles
<< "/usr/share/info";
106 infoDirFiles
<< "/usr/info";
107 infoDirFiles
<< "/usr/lib/info";
108 infoDirFiles
<< "/usr/local/info";
109 infoDirFiles
<< "/usr/local/lib/info";
110 infoDirFiles
<< "/usr/X11R6/info";
111 infoDirFiles
<< "/usr/X11R6/lib/info";
112 infoDirFiles
<< "/usr/X11R6/lib/xemacs/info";
115 QString infoPath
= ::getenv( "INFOPATH" );
116 if ( !infoPath
.isEmpty() )
117 infoDirFiles
+= infoPath
.split( ':');
119 QStringList::ConstIterator it
= infoDirFiles
.constBegin();
120 QStringList::ConstIterator end
= infoDirFiles
.constEnd();
121 for ( ; it
!= end
; ++it
) {
122 QString infoDirFileName
= *it
+ "/dir";
123 if ( QFile::exists( infoDirFileName
) )
124 parseInfoDirFile( infoDirFileName
);
127 m_alphabItem
->sortChildItems( 0, true /* ascending */ );
130 void InfoTree::parseInfoDirFile( const QString
&infoDirFileName
)
132 kDebug( 1400 ) << "Parsing info dir file " << infoDirFileName
;
134 QFile
infoDirFile( infoDirFileName
);
135 if ( !infoDirFile
.open( QIODevice::ReadOnly
) )
138 QTextStream
stream( &infoDirFile
);
139 // Skip introduction blurb.
140 while ( !stream
.atEnd() && !stream
.readLine().startsWith( "* Menu:" ) ) {
144 while ( !stream
.atEnd() ) {
145 QString s
= stream
.readLine();
146 if ( s
.trimmed().isEmpty() )
149 InfoCategoryItem
*catItem
= new InfoCategoryItem( m_categoryItem
, s
);
150 while ( !stream
.atEnd() && !s
.trimmed().isEmpty() ) {
151 s
= stream
.readLine();
152 if ( s
[ 0 ] == '*' ) {
153 const int colon
= s
.indexOf( ":" );
154 const int openBrace
= s
.indexOf( "(", colon
);
155 const int closeBrace
= s
.indexOf( ")", openBrace
);
156 const int dot
= s
.indexOf( ".", closeBrace
);
158 QString appName
= s
.mid( 2, colon
- 2 );
159 QString url
= "info:/" + s
.mid( openBrace
+ 1, closeBrace
- openBrace
- 1 );
160 if ( dot
- closeBrace
> 1 )
161 url
+= QLatin1Char('/') + s
.mid( closeBrace
+ 1, dot
- closeBrace
- 1 );
163 url
+= QLatin1String("/Top");
165 InfoNodeItem
*item
= new InfoNodeItem( catItem
, appName
);
166 item
->entry()->setUrl( url
);
168 InfoCategoryItem
*alphabSection
= 0;
169 for ( Q3ListViewItem
* it
=m_alphabItem
->firstChild(); it
; it
=it
->nextSibling() ) {
170 if ( it
->text( 0 ) == QString( appName
[ 0 ].toUpper() ) ) {
171 alphabSection
= static_cast<InfoCategoryItem
*>( it
);
176 if ( alphabSection
== 0 )
177 alphabSection
= new InfoCategoryItem( m_alphabItem
, QString( appName
[ 0 ].toUpper() ) );
179 item
= new InfoNodeItem( alphabSection
, appName
);
180 item
->entry()->setUrl( url
);
187 #include "infotree.moc"