summaryrefslogtreecommitdiffstats
path: root/parts/documentation/plugins/chm/docchmplugin.cpp
blob: 7293d9851fdce8022b892e23f3206a6cd5473cf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/***************************************************************************
 *   Copyright (C) 2004 by Alexander Dymo                                  *
 *   cloudtemple@mksat.net                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/
#include "docchmplugin.h"

#include <kurl.h>
#include <kaboutdata.h>
#include <kconfig.h>
#include <klocale.h>
#include <klistview.h>
#include <kio/netaccess.h>
#include <iostream>
#include <fstream>
#include <tqvaluevector.h>
#include <kdevgenericfactory.h>
#include <kdevplugininfo.h>
#include <tqregexp.h>

#include "../../../../config.h"

static const KDevPluginInfo data("docchmplugin");
typedef KDevGenericFactory<DocCHMPlugin> DocCHMPluginFactory;
K_EXPORT_COMPONENT_FACTORY( libdocchmplugin, DocCHMPluginFactory(data) )

DocCHMPlugin::DocCHMPlugin(TQObject* parent, const char* name, TQStringList // args
                           )
    :DocumentationPlugin(DocCHMPluginFactory::instance()->config(), parent, name)
{
    setCapabilities(CustomDocumentationTitles); // | Index | FullTextSearch | ProjectDocumentation
    autoSetup();
}

DocCHMPlugin::~DocCHMPlugin()
{
}


DocumentationCatalogItem* DocCHMPlugin::createCatalog(KListView* contents, const TQString& title, const TQString& url)
{
    DocumentationCatalogItem *item = new DocumentationCatalogItem(this, contents, title);
    item->setURL(KURL(url));
    
    return item;
}

TQPair<KFile::Mode, TQString> DocCHMPlugin::catalogLocatorProps()
{
    return TQPair<KFile::Mode, TQString>(KFile::File, "*.chm");
}

TQString DocCHMPlugin::catalogTitle(const TQString& // url
                                   )
{
    return TQString();
}

TQString DocCHMPlugin::pluginName() const
{
    return i18n("CHM Documentation Collection");
}


TQString DocCHMPlugin::getSpecialData(const TQString& name, KURL url) {
    TQString ret = "";
    url.setProtocol("ms-its");
    
    url.addPath("/:" + name);
    TQString tmpFile;
    if( TDEIO::NetAccess::download( url, tmpFile, 0 ) )
    {
        std::filebuf fb;
        fb.open (tmpFile.ascii(), std::ios::in);
        std::istream is(&fb);
        char buf[5000] = " ";
        while(is.good()) {
            is.getline(buf, 5000);
            ret += buf; ret += "\n";
        }
        fb.close();
        TDEIO::NetAccess::removeTempFile( tmpFile );
    } else {
        kdDebug(9002) << "DocCHMPlugin::getSpecialData could not download data from " << url.url() << endl;
    }
    return ret;
}


///this currently is useless, because htdig cannot use kioslaves
TQStringList DocCHMPlugin::fullTextSearchLocations()
{
    //return TQStringList::split( '\n', getSpecialData("contents", m_url));
    return TQStringList();
}

bool DocCHMPlugin::needRefreshIndex(DocumentationCatalogItem* // item
                                    )
{
    return false;
}

void DocCHMPlugin::autoSetupPlugin()
{
    return;
}

void DocCHMPlugin::createIndex(IndexBox* // index
                               , DocumentationCatalogItem* // item
                               )
{
    return;
}


static KListViewItem* chainEnd(KListViewItem *parent) {
    if(parent == 0) return 0;
    KListViewItem* ret = dynamic_cast<KListViewItem*>(parent->firstChild());
    if(ret == 0) return 0;
    while(ret->nextSibling() != 0) {
        ret = dynamic_cast<KListViewItem*>(ret->nextSibling());
    }
    return ret;
}

static TQString decodeHTML(const TQString& s) {
    TQRegExp rx(TQString::fromLatin1("&#(\\d+);|&nbsp;"));
    TQString out = s;
    int pos = rx.search(out);
    while(pos > -1) {
        TQString found = rx.cap(0);
        if(found != "&nbsp;") {
            out.replace(pos, found.length(), static_cast<char>(rx.cap(1).toInt()));
        }else{
            out.replace(pos, found.length(), " ");
        }
        pos = rx.search(out, pos+1);
    }
    return out;
}


void DocCHMPlugin::createTOC(DocumentationCatalogItem* item)
{
    TQStringList lines = TQStringList::split("\n", getSpecialData("catalog", item->url()) );
    if(lines.count() % 4 != 0) { kdDebug(9002) << "DocCHMPlugin::createTOC: wrong count of strings"; return;}
    
    TQValueVector<DocumentationItem*> items;
    items.push_back(item);
    for(TQStringList::Iterator it = lines.begin(); it != lines.end();) {
        bool ok1 = true, ok2 = true;
        int parent = (*it).toInt(&ok1);
        ++it;
        int current = (*it).toInt(&ok2);
        ++it;
        if(int(items.size()) != current || !ok1 || !ok2 || parent < 0 || parent >= int(items.size()) || current < 0 || current != int(items.size())) {
            kdDebug(9002) << "DocCHMPlugin::createTOC error while parsing output of ioslave" << endl;
            break;
        }
        
        TQString& name(*it);
        ++it;
        KURL url(*it);
        ++it;
        
        items.push_back(new DocumentationItem(
                DocumentationItem::Document, items[parent], chainEnd(items[parent]), decodeHTML(name)));
        items[current]->setURL(url);
        if(parent != 0) items[parent]->setType(DocumentationItem::Book);
    }
    

    return;
}

void DocCHMPlugin::setCatalogURL(DocumentationCatalogItem* item)
{
    KURL chmURL = item->url();
    chmURL.setProtocol("ms-its");
    chmURL.addPath("/");
    item->setURL(chmURL);
}

#include "docchmplugin.moc"