1/*
2 * This file is part of the KDE Help Center
3 *
4 * Copyright (C) 2001 Waldo Bastian <bastian@kde.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public
8 * License version 2 or at your option version 3 as published
9 * by the Free Software Foundation.
10 *
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.
15 *
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.
19 */
20
21#include "navigatorappitem.h"
22#include "docentry.h"
23
24#include <KDebug>
25#include <KService>
26#include <KServiceGroup>
27
28using namespace KHC;
29
30NavigatorAppItem::NavigatorAppItem( DocEntry *entry, QTreeWidget *parent,
31 const QString &relPath )
32 : NavigatorItem( entry, parent ),
33 mRelpath( relPath ),
34 mPopulated( false )
35{
36 populate();
37}
38
39NavigatorAppItem::NavigatorAppItem( DocEntry *entry, QTreeWidgetItem *parent,
40 const QString &relPath )
41 : NavigatorItem( entry, parent ),
42 mRelpath( relPath ),
43 mPopulated( false )
44{
45 populate();
46}
47
48NavigatorAppItem::NavigatorAppItem( DocEntry *entry, QTreeWidget *parent,
49 QTreeWidgetItem *after )
50 : NavigatorItem( entry, parent, after ),
51 mPopulated( false )
52{
53 populate();
54}
55
56NavigatorAppItem::NavigatorAppItem( DocEntry *entry, QTreeWidgetItem *parent,
57 QTreeWidgetItem *after )
58 : NavigatorItem( entry, parent, after ),
59 mPopulated( false )
60{
61 populate();
62}
63
64void NavigatorAppItem::setRelpath( const QString &relpath )
65{
66 mRelpath = relpath;
67}
68
69void NavigatorAppItem::setExpanded(bool open)
70{
71 kDebug() << "NavigatorAppItem::setOpen()";
72
73 if ( open && (childCount() == 0) && !mPopulated )
74 {
75 kDebug() << "NavigatorAppItem::setOpen(" << this << ", "
76 << mRelpath << ")" << endl;
77 populate();
78 }
79 QTreeWidgetItem::setExpanded(open);
80}
81
82void NavigatorAppItem::populate( bool recursive )
83{
84 if ( mPopulated ) return;
85
86 KServiceGroup::Ptr root = KServiceGroup::group(mRelpath);
87 if ( !root ) {
88 kWarning() << "No Service groups\n";
89 return;
90 }
91 KServiceGroup::List list = root->entries();
92
93
94 for ( KServiceGroup::List::ConstIterator it = list.constBegin();
95 it != list.constEnd(); ++it )
96 {
97 const KSycocaEntry::Ptr e = *it;
98 NavigatorItem *item;
99 QString url;
100
101 switch ( e->sycocaType() ) {
102 case KST_KService:
103 {
104 const KService::Ptr s = KService::Ptr::staticCast(e);
105 url = documentationURL( s.data() );
106 if ( !url.isEmpty() ) {
107 DocEntry *entry = new DocEntry( s->name(), url, s->icon() );
108 item = new NavigatorItem( entry, this );
109 item->setAutoDeleteDocEntry( true );
110 }
111 break;
112 }
113 case KST_KServiceGroup:
114 {
115 KServiceGroup::Ptr g = KServiceGroup::Ptr::staticCast(e);
116 if ( ( g->childCount() == 0 ) || g->name().startsWith( '.' ) )
117 continue;
118 DocEntry *entry = new DocEntry( g->caption(), "", g->icon() );
119 NavigatorAppItem *appItem;
120 appItem = new NavigatorAppItem( entry, this, g->relPath() );
121 appItem->setAutoDeleteDocEntry( true );
122 if ( recursive ) appItem->populate( recursive );
123 break;
124 }
125 default:
126 break;
127 }
128 }
129 sortChildren( 0, Qt::AscendingOrder /* ascending */ );
130 mPopulated = true;
131}
132
133QString NavigatorAppItem::documentationURL( const KService *s )
134{
135 QString docPath = s->property( QLatin1String("DocPath") ).toString();
136 if ( docPath.isEmpty() ) {
137 docPath = s->property( QLatin1String("X-DocPath") ).toString();
138 if ( docPath.isEmpty() ) {
139 return QString();
140 }
141 }
142
143 if ( docPath.startsWith( QLatin1String("file:")) || docPath.startsWith( QLatin1String("http:") ) )
144 return docPath;
145
146 return QLatin1String( "help:/" ) + docPath;
147}
148
149// vim:ts=2:sw=2:et
150