summaryrefslogtreecommitdiffstats
path: root/parts/documentation/plugins/qt/docqtplugin.cpp
blob: 257a8cc2035f8e2afc709e07715cd835bd14b585 (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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/***************************************************************************
 *   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 "docqtplugin.h"

#include <unistd.h>

#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqdialog.h>

#include <kurl.h>
#include <kaboutdata.h>
#include <kconfig.h>
#include <klocale.h>
#include <klistview.h>

#include <urlutil.h>
#include <kdevgenericfactory.h>
#include <kdevplugininfo.h>

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

class TQtDocumentationCatalogItem: public DocumentationCatalogItem
{
public:
    TQtDocumentationCatalogItem(const TQString &dcfFile, DocumentationPlugin* plugin,
        KListView *parent, const TQString &name)
        :DocumentationCatalogItem(plugin, parent, name), m_dcfFile(dcfFile)
    {
    }
    TQtDocumentationCatalogItem(const TQString &dcfFile, DocumentationPlugin* plugin,
        DocumentationItem *parent, const TQString &name)
        :DocumentationCatalogItem(plugin, parent, name), m_dcfFile(dcfFile)
    {
    }
    TQString dcfFile() const { return m_dcfFile; }

private:
    TQString m_dcfFile;
};

static const KDevPluginInfo data("docqtplugin");
typedef KDevGenericFactory<DocQtPlugin> DocQtPluginFactory;
K_EXPORT_COMPONENT_FACTORY( libdocqtplugin, DocQtPluginFactory(data) )

DocQtPlugin::DocQtPlugin(TQObject* parent, const char* name, const TQStringList)
    :DocumentationPlugin(DocQtPluginFactory::instance()->config(), parent, name)
{
    setCapabilities(Index | FullTextSearch | CustomDocumentationTitles);
    autoSetup();
}

DocQtPlugin::~DocQtPlugin()
{
}

void DocQtPlugin::createTOC(DocumentationCatalogItem *item)
{
    TQtDocumentationCatalogItem *qtItem = dynamic_cast<TQtDocumentationCatalogItem *>(item);
    if (!qtItem)
        return;

    TQFileInfo fi(qtItem->dcfFile());

    TQFile f(qtItem->dcfFile());
    if (!f.open(IO_ReadOnly))
    {
        kdDebug(9002) << "Could not read" << qtItem->dcfFile() << endl;
        return;
    }
    TQDomDocument doc;
    if (!doc.setContent(&f) || doc.doctype().name() != "DCF")
    {
        kdDebug(9002) << "Not a valid DCF file: " << qtItem->dcfFile() << endl;
        return;
    }
    f.close();

    TQDomElement docEl = doc.documentElement();
    TQDomElement titleEl = docEl.namedItem("DCF").toElement();

    TQDomElement childEl = docEl.lastChild().toElement();
    while (!childEl.isNull())
    {
        if (childEl.tagName() == "section")
        {
            TQString ref = childEl.attribute("ref");
            TQString title = childEl.attribute("title");

            DocumentationItem *sectionItem = new DocumentationItem(DocumentationItem::Book, item, title);
            KURL sectionUrl(fi.dirPath(true) + "/" + ref);
            sectionItem->setURL(sectionUrl);

            TQDomElement grandChild = childEl.lastChild().toElement();
            while(!grandChild.isNull())
            {
                if (grandChild.tagName() == "keyword")
                {
                    TQString keyRef = grandChild.attribute("ref");
                    TQString keyTitle = grandChild.text();

                    DocumentationItem *keyItem = new DocumentationItem(DocumentationItem::Document, sectionItem, keyTitle);
                    KURL keyUrl(fi.dirPath(true) + "/" + keyRef);
                    keyItem->setURL(keyUrl);
                }
                if (grandChild.tagName() == "section")
                {
                    TQString keyRef = grandChild.attribute("ref");
                    TQString keyTitle = grandChild.attribute("title");
                    DocumentationItem *keyItem = new DocumentationItem(DocumentationItem::Book, sectionItem, keyTitle);
                    KURL keyUrl(fi.dirPath(true) + "/" + keyRef);
                    keyItem->setURL(keyUrl);
                }
                grandChild = grandChild.previousSibling().toElement();
            }
        }
        childEl = childEl.previousSibling().toElement();
    }
}

void DocQtPlugin::autoSetupPlugin()
{
    TQString qtDocDir(QT_DOCDIR);
    qtDocDir = URLUtil::envExpand(qtDocDir);
    if (qtDocDir.isEmpty())
    {
        qtDocDir = getenv("TQTDIR");
    }
    if (!qtDocDir.isEmpty())
    {
        config->setGroup("Search Settings");
        config->writeEntry("TQt Reference Documentation", true);
        config->setGroup("Index Settings");
        config->writeEntry("TQt Reference Documentation", true);
        config->setGroup("Locations");
        config->writePathEntry("TQt Reference Documentation", qtDocDir + TQString("/qt.dcf"));
        config->writePathEntry("TQt Assistant Manual", qtDocDir + TQString("/assistant.dcf"));
        config->writePathEntry("TQt Designer Manual", qtDocDir + TQString("/designer.dcf"));
        config->writePathEntry("Guide to the TQt Translation Tools", qtDocDir + TQString("/linguist.dcf"));
        config->writePathEntry("qmake User Guide", qtDocDir + TQString("/qmake.dcf"));
    }
}

void DocQtPlugin::setCatalogURL(DocumentationCatalogItem *item)
{
    TQtDocumentationCatalogItem *qtItem = dynamic_cast<TQtDocumentationCatalogItem *>(item);
    if (!qtItem)
        return;

    TQFileInfo fi(qtItem->dcfFile());

    TQFile f(qtItem->dcfFile());
    if (!f.open(IO_ReadOnly))
    {
        kdDebug(9002) << "Could not read" << qtItem->dcfFile() << endl;
        return;
    }
    TQDomDocument doc;
    if (!doc.setContent(&f) || doc.doctype().name() != "DCF")
    {
        kdDebug(9002) << "Not a valid DCF file: " << qtItem->dcfFile() << endl;
        return;
    }
    f.close();

    TQDomElement docEl = doc.documentElement();
    TQDomElement titleEl = docEl.namedItem("DCF").toElement();

    if (item->url().isEmpty())
    {
        KURL url(fi.dirPath(true) + "/" + docEl.attribute("ref", TQString()));
        item->setURL(url);
    }
}

TQString DocQtPlugin::pluginName() const
{
    return i18n("TQt Documentation Collection");
}

bool DocQtPlugin::needRefreshIndex(DocumentationCatalogItem *item)
{
    TQtDocumentationCatalogItem *qtItem = dynamic_cast<TQtDocumentationCatalogItem *>(item);
    if (!qtItem)
        return false;

    TQFileInfo fi(qtItem->dcfFile());
    config->setGroup("Index");
    if (fi.lastModified() > config->readDateTimeEntry(qtItem->text(0), new TQDateTime()))
    {
        kdDebug() << "need rebuild index for " << qtItem->text(0) << endl;
        config->writeEntry(item->text(0), fi.lastModified());
        return true;
    }
    else
        return false;
}

void DocQtPlugin::createIndex(IndexBox *index, DocumentationCatalogItem *item)
{
    TQtDocumentationCatalogItem *qtItem = dynamic_cast<TQtDocumentationCatalogItem *>(item);
    if (!qtItem)
        return;

    TQFileInfo fi(qtItem->dcfFile());

    TQFile f(qtItem->dcfFile());
    if (!f.open(IO_ReadOnly))
    {
        kdDebug(9002) << "Could not read" << qtItem->dcfFile() << endl;
        return;
    }
    TQDomDocument doc;
    if (!doc.setContent(&f) || doc.doctype().name() != "DCF")
    {
        kdDebug(9002) << "Not a valid DCF file: " << qtItem->dcfFile() << endl;
        return;
    }
    f.close();

    TQDomElement docEl = doc.documentElement();
    TQDomElement titleEl = docEl.namedItem("DCF").toElement();

    TQDomElement childEl = docEl.firstChild().toElement();
    while (!childEl.isNull())
    {
        if (childEl.tagName() == "section")
        {
            createSectionIndex(fi, index, item, childEl);
        }
        childEl = childEl.nextSibling().toElement();
    }
}

void DocQtPlugin::createSectionIndex(TQFileInfo &fi, IndexBox *index, DocumentationCatalogItem *item,
                                     TQDomElement section)
{
    //adymo: do not load section to index for TQt reference documentation
    TQString title = section.attribute("title");
    if (fi.fileName() != "qt.dcf")
    {
        TQString ref = section.attribute("ref");

        IndexItemProto *ii = new IndexItemProto(this, item, index, title, item->text(0));
        ii->addURL(KURL(fi.dirPath(true) + "/" + ref));
    }

    TQDomElement grandChild = section.firstChild().toElement();
    while(!grandChild.isNull())
    {
        if (grandChild.tagName() == "keyword")
        {
            TQString keyRef = grandChild.attribute("ref");
            TQString keyTitle = grandChild.text();

                    //adymo: a little hack to avoid unwanted elements
            if (keyRef != "qdir-example.html")
            {
                IndexItemProto *ii = new IndexItemProto(this, item, index, keyTitle, title);
                ii->addURL(KURL(fi.dirPath(true) + "/" + keyRef));
            }
        }
        if (grandChild.tagName() == "section")
        {
            createSectionIndex(fi, index, item, grandChild);
        }
        grandChild = grandChild.nextSibling().toElement();
    }
}

TQStringList DocQtPlugin::fullTextSearchLocations()
{
    TQStringList locs;

    TQMap<TQString, TQString> entryMap = config->entryMap("Locations");

    for (TQMap<TQString, TQString>::const_iterator it = entryMap.begin();
        it != entryMap.end(); ++it)
    {
        config->setGroup("Search Settings");
        if (config->readBoolEntry(it.key(), false))
        {
            config->setGroup("Locations");
            TQFileInfo fi(config->readPathEntry(it.key()));
            locs << fi.dirPath(true);
        }
    }

    return locs;
}

TQPair<KFile::Mode, TQString> DocQtPlugin::catalogLocatorProps()
{
    return TQPair<KFile::Mode, TQString>(KFile::File, "*.xml *.dcf");
}

TQString DocQtPlugin::catalogTitle(const TQString &url)
{
    TQFileInfo fi(url);
    if (!fi.exists())
        return TQString();

    TQFile f(url);
    if (!f.open(IO_ReadOnly))
        return TQString();

    TQDomDocument doc;
    if (!doc.setContent(&f) || (doc.doctype().name() != "DCF"))
        return TQString();
    f.close();

    TQDomElement docEl = doc.documentElement();

    return docEl.attribute("title", TQString());
}

DocumentationCatalogItem *DocQtPlugin::createCatalog(KListView *contents, const TQString &title, const TQString &url)
{
    return new TQtDocumentationCatalogItem(url, this, contents, title);
}

#include "docqtplugin.moc"