1 /*
2 SPDX-FileCopyrightText: 2001 Waldo Bastian <bastian@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only
5 */
6
7 #include "navigatorappgroupitem.h"
8 #include "navigatorappitem.h"
9 #include "docentry.h"
10 #include "khc_debug.h"
11
12 #include <KService>
13 #include <KServiceGroup>
14
15 using namespace KHC;
16
NavigatorAppGroupItem(DocEntry * entry,QTreeWidget * parent,const QString & relPath)17 NavigatorAppGroupItem::NavigatorAppGroupItem( DocEntry *entry, QTreeWidget *parent,
18 const QString &relPath )
19 : NavigatorItem( entry, parent ),
20 mRelpath( relPath )
21 {
22 populate();
23 }
24
NavigatorAppGroupItem(DocEntry * entry,QTreeWidgetItem * parent,const QString & relPath)25 NavigatorAppGroupItem::NavigatorAppGroupItem( DocEntry *entry, QTreeWidgetItem *parent,
26 const QString &relPath )
27 : NavigatorItem( entry, parent ),
28 mRelpath( relPath )
29 {
30 populate();
31 }
32
NavigatorAppGroupItem(DocEntry * entry,QTreeWidget * parent,QTreeWidgetItem * after)33 NavigatorAppGroupItem::NavigatorAppGroupItem( DocEntry *entry, QTreeWidget *parent,
34 QTreeWidgetItem *after )
35 : NavigatorItem( entry, parent, after )
36 {
37 populate();
38 }
39
NavigatorAppGroupItem(DocEntry * entry,QTreeWidgetItem * parent,QTreeWidgetItem * after)40 NavigatorAppGroupItem::NavigatorAppGroupItem( DocEntry *entry, QTreeWidgetItem *parent,
41 QTreeWidgetItem *after )
42 : NavigatorItem( entry, parent, after )
43 {
44 populate();
45 }
46
setRelpath(const QString & relpath)47 void NavigatorAppGroupItem::setRelpath( const QString &relpath )
48 {
49 mRelpath = relpath;
50 }
51
itemExpanded(bool open)52 void NavigatorAppGroupItem::itemExpanded(bool open)
53 {
54 qCDebug(KHC_LOG) << "NavigatorAppGroupItem::itemExpanded()";
55
56 if ( open && (childCount() == 0) && !mPopulated )
57 {
58 qCDebug(KHC_LOG) << " -> populate:" << this << "-" << mRelpath;
59 populate();
60 }
61 NavigatorItem::itemExpanded(open);
62 }
63
populate(bool recursive)64 void NavigatorAppGroupItem::populate( bool recursive )
65 {
66 if ( mPopulated ) return;
67
68 KServiceGroup::Ptr root = KServiceGroup::group(mRelpath);
69 if ( !root ) {
70 qCWarning(KHC_LOG) << "No Service groups for" << mRelpath;
71 return;
72 }
73 KServiceGroup::List list = root->entries();
74 bool no_children_present = true;
75
76
77 for ( KServiceGroup::List::ConstIterator it = list.constBegin();
78 it != list.constEnd(); ++it )
79 {
80 const KSycocaEntry::Ptr e = *it;
81 QString url;
82
83 switch ( e->sycocaType() ) {
84 case KST_KService:
85 {
86 const KService::Ptr s(static_cast<KService*>(e.data()));
87 url = documentationURL( s.data() );
88 if ( !url.isEmpty() ) {
89 DocEntry *entry = new DocEntry( s->name(), url, s->icon() );
90 NavigatorAppItem *item = new NavigatorAppItem( entry, this );
91 item->setAutoDeleteDocEntry( true );
92 no_children_present = false;
93 }
94 break;
95 }
96 case KST_KServiceGroup:
97 {
98 const KServiceGroup::Ptr g(static_cast<KServiceGroup*>(e.data()));
99 if ( ( g->childCount() == 0 ) || g->name().startsWith( QLatin1Char('.') ) )
100 continue;
101 DocEntry *entry = new DocEntry( g->caption(), QString(), g->icon() );
102 NavigatorAppGroupItem *appItem;
103 appItem = new NavigatorAppGroupItem( entry, this, g->relPath() );
104 appItem->setAutoDeleteDocEntry( true );
105 if ( recursive ) appItem->populate( recursive );
106 no_children_present = false;
107 break;
108 }
109 default:
110 break;
111 }
112 }
113 sortChildren( 0, Qt::AscendingOrder /* ascending */ );
114 mPopulated = true;
115 setHidden(no_children_present);
116 }
117
documentationURL(const KService * s)118 QString NavigatorAppGroupItem::documentationURL( const KService *s )
119 {
120 QString docPath = s->property( QStringLiteral("DocPath") ).toString();
121 if ( docPath.isEmpty() ) {
122 docPath = s->property( QStringLiteral("X-DocPath") ).toString();
123 if ( docPath.isEmpty() ) {
124 return QString();
125 }
126 }
127
128 if ( docPath.startsWith(QLatin1String("file:")) || docPath.startsWith(QLatin1String("http") ) )
129 return docPath;
130
131 return QStringLiteral( "help:/" ) + docPath;
132 }
133
134 // vim:ts=2:sw=2:et
135