when a new activity is added, don't move more than necessary to show it
[kdebase.git] / runtime / khelpcenter / infotree.cpp
blob2e7d60dc4e5b162361da25b21f5755123145ae66
1 /*
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.
21 #include "infotree.h"
23 #include "navigatoritem.h"
24 #include "docentry.h"
26 #include <kapplication.h>
27 #include <kconfig.h>
28 #include <kconfiggroup.h>
29 #include <kdebug.h>
30 #include <kiconloader.h>
31 #include <k3listview.h>
32 #include <klocale.h>
33 #include <kstandarddirs.h>
34 #include <kurl.h>
36 #include <QFile>
37 #include <QTextStream>
38 #include <QPixmap>
39 #include <stdlib.h> // for getenv()
40 #include <kglobal.h>
42 using namespace KHC;
44 class InfoCategoryItem : public NavigatorItem
46 public:
47 InfoCategoryItem( NavigatorItem *parent, const QString &text );
49 virtual void setOpen( bool open );
52 class InfoNodeItem : public NavigatorItem
54 public:
55 InfoNodeItem( InfoCategoryItem *parent, const QString &text );
58 InfoCategoryItem::InfoCategoryItem( NavigatorItem *parent, const QString &text )
59 : NavigatorItem( new DocEntry( text ), parent )
61 setAutoDeleteDocEntry( true );
62 setOpen( false );
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 else setPixmap( 0, SmallIcon( "contents2" ) );
74 InfoNodeItem::InfoNodeItem( InfoCategoryItem *parent, const QString &text )
75 : NavigatorItem( new DocEntry( text ), parent )
77 setAutoDeleteDocEntry( true );
78 // kDebug( 1400 ) << "Created info node item: " << text;
81 InfoTree::InfoTree( QObject *parent )
82 : TreeBuilder( parent ),
83 m_parentItem( 0 )
87 void InfoTree::build( NavigatorItem *parent )
89 kDebug( 1400 ) << "Populating info tree.";
91 m_parentItem = parent;
93 DocEntry *entry = new DocEntry( i18n( "Alphabetically" ) );
94 m_alphabItem = new NavigatorItem( entry, parent );
95 m_alphabItem->setAutoDeleteDocEntry( true );
96 entry = new DocEntry( i18n( "By Category" ) );
97 m_categoryItem = new NavigatorItem( entry, parent );
98 m_categoryItem->setAutoDeleteDocEntry( true );
100 KConfigGroup cfg(KGlobal::config(), "Info pages");
101 QStringList infoDirFiles = cfg.readEntry( "Search paths" , QStringList() );
102 // Default paths taken fron kdebase/kioslave/info/kde-info2html.conf
103 if ( infoDirFiles.isEmpty() ) {
104 infoDirFiles << "/usr/share/info";
105 infoDirFiles << "/usr/info";
106 infoDirFiles << "/usr/lib/info";
107 infoDirFiles << "/usr/local/info";
108 infoDirFiles << "/usr/local/lib/info";
109 infoDirFiles << "/usr/X11R6/info";
110 infoDirFiles << "/usr/X11R6/lib/info";
111 infoDirFiles << "/usr/X11R6/lib/xemacs/info";
114 QString infoPath = ::getenv( "INFOPATH" );
115 if ( !infoPath.isEmpty() )
116 infoDirFiles += infoPath.split( ':');
118 QStringList::ConstIterator it = infoDirFiles.begin();
119 QStringList::ConstIterator end = infoDirFiles.end();
120 for ( ; it != end; ++it ) {
121 QString infoDirFileName = *it + "/dir";
122 if ( QFile::exists( infoDirFileName ) )
123 parseInfoDirFile( infoDirFileName );
126 m_alphabItem->sortChildItems( 0, true /* ascending */ );
129 void InfoTree::parseInfoDirFile( const QString &infoDirFileName )
131 kDebug( 1400 ) << "Parsing info dir file " << infoDirFileName;
133 QFile infoDirFile( infoDirFileName );
134 if ( !infoDirFile.open( QIODevice::ReadOnly ) )
135 return;
137 QTextStream stream( &infoDirFile );
138 // Skip introduction blurb.
139 while ( !stream.atEnd() && !stream.readLine().startsWith( "* Menu:" ) ) {
143 while ( !stream.atEnd() ) {
144 QString s = stream.readLine();
145 if ( s.trimmed().isEmpty() )
146 continue;
148 InfoCategoryItem *catItem = new InfoCategoryItem( m_categoryItem, s );
149 while ( !stream.atEnd() && !s.trimmed().isEmpty() ) {
150 s = stream.readLine();
151 if ( s[ 0 ] == '*' ) {
152 const int colon = s.indexOf( ":" );
153 const int openBrace = s.indexOf( "(", colon );
154 const int closeBrace = s.indexOf( ")", openBrace );
155 const int dot = s.indexOf( ".", closeBrace );
157 QString appName = s.mid( 2, colon - 2 );
158 QString url = "info:/" + s.mid( openBrace + 1, closeBrace - openBrace - 1 );
159 if ( dot - closeBrace > 1 )
160 url += QLatin1Char('/') + s.mid( closeBrace + 1, dot - closeBrace - 1 );
161 else
162 url += QLatin1String("/Top");
164 InfoNodeItem *item = new InfoNodeItem( catItem, appName );
165 item->entry()->setUrl( url );
167 InfoCategoryItem *alphabSection = 0;
168 for ( Q3ListViewItem* it=m_alphabItem->firstChild(); it; it=it->nextSibling() ) {
169 if ( it->text( 0 ) == QString( appName[ 0 ].toUpper() ) ) {
170 alphabSection = static_cast<InfoCategoryItem *>( it );
171 break;
175 if ( alphabSection == 0 )
176 alphabSection = new InfoCategoryItem( m_alphabItem, QString( appName[ 0 ].toUpper() ) );
178 item = new InfoNodeItem( alphabSection, appName );
179 item->entry()->setUrl( url );
183 infoDirFile.close();
186 #include "infotree.moc"
187 // vim:ts=2:sw=2:et