summaryrefslogtreecommitdiffstats
path: root/qthelpprojectwriter.cpp
blob: 22ea7389cc1c41a7e65828cd846a7d7da266864a (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
/****************************************************************************
 **
 ** Copyright (C) 2008-2010 Nokia Corporation and/or its subsidiary(-ies).
 ** Contact: Nokia Corporation (qt-info@nokia.com)
 **
 ** This file is part of the doxygen2qthelp project on Trolltech Labs.
 **
 ** This file may be used under the terms of the GNU General Public
 ** License version 2.0 or 3.0 as published by the Free Software Foundation
 ** and appearing in the file LICENSE.GPL included in the packaging of
 ** this file.  Please review the following information to ensure GNU
 ** General Public Licensing requirements will be met:
 ** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
 ** http://www.gnu.org/copyleft/gpl.html.
 **
 ** If you are unsure which license is appropriate for your use, please
 ** contact the sales department at qt-sales@nokia.com.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/

#include "qthelpprojectwriter_p.h"
#include <qhelpdatainterface_p.h>

#include <QtCore/QList>
#include <QtCore/QStringList>
#include <QtCore/QFile>

QT_BEGIN_NAMESPACE

QtHelpProjectWriter::QtHelpProjectWriter()
        : m_helpData(NULL) {
    setAutoFormatting(true);
}

void QtHelpProjectWriter::writeHead()
{
    writeStartDocument();
    writeStartElement(QLatin1String("QtHelpProject"));
    writeAttribute(QLatin1String("version"), QLatin1String("1.0"));

    Q_ASSERT(m_helpData != NULL);
    writeTextElement(QLatin1String("namespace"),
            m_helpData->namespaceName());
    writeTextElement(QLatin1String("virtualFolder"),
            m_helpData->virtualFolder());
}

void QtHelpProjectWriter::writeCustomFilters()
{
    Q_ASSERT(m_helpData != NULL);
    const QList<QHelpDataCustomFilter> filters = m_helpData->customFilters();
    foreach (const QHelpDataCustomFilter & filter, filters) {
        writeStartElement(QLatin1String("customFilter"));
        writeAttribute(QLatin1String("name"), filter.name);
        foreach (const QString & att, filter.filterAttributes) {
            writeTextElement(QLatin1String("filterAttribute"), att);
        }
        writeEndElement();
    }
}

void QtHelpProjectWriter::writeTocItems(
        const QList<QHelpDataContentItem*> &items)
{
    if (items.isEmpty()) {
        return;
    }

    foreach (const QHelpDataContentItem *item, items) {
        const QList<QHelpDataContentItem*> children = item->children();
        if (children.isEmpty()) {
            writeEmptyElement(QLatin1String("section"));
        } else {
            writeStartElement(QLatin1String("section"));
        }
        writeAttribute(QLatin1String("title"), item->title());
        writeAttribute(QLatin1String("ref"), item->reference());
        if (!children.isEmpty()) {
            writeTocItems(children);
            writeEndElement();
        }
    }
}

void QtHelpProjectWriter::writeFilterSections()
{
    Q_ASSERT(m_helpData != NULL);
    const QList<QHelpDataFilterSection> sections = m_helpData->filterSections();
    foreach (const QHelpDataFilterSection & section, sections) {
        // Filter section
        writeStartElement(QLatin1String("filterSection"));

        // Filter attributes
        const QStringList atts = section.filterAttributes();
        foreach (const QString & att, atts) {
            writeTextElement(QLatin1String("filterAttribute"), att);
        }

        // TOC
        writeStartElement(QLatin1String("toc"));
        writeTocItems(section.contents());
        writeEndElement();

        // Keywords
        const QList<QHelpDataIndexItem> indices = section.indices();
        writeStartElement(QLatin1String("keywords"));
        foreach (const QHelpDataIndexItem & indexItem, indices) {
            const bool nameEmpty = indexItem.name.isEmpty();
            const bool idEmpty = indexItem.identifier.isEmpty();
            if (nameEmpty && idEmpty) {
                continue;
            }

            writeEmptyElement(QLatin1String("keyword"));
            if (!nameEmpty) {
                writeAttribute(QLatin1String("name"), indexItem.name);
            }
            if (!idEmpty) {
                writeAttribute(QLatin1String("id"), indexItem.identifier);
            }
            writeAttribute(QLatin1String("ref"), indexItem.reference);
        }
        writeEndElement();

        // Files
        const QStringList files = section.files();
        writeStartElement(QLatin1String("files"));
        foreach (const QString & file, files) {
            writeTextElement(QLatin1String("file"), file);
        }
        writeEndElement();

        // Filter section
        writeEndElement();
    }
}

void QtHelpProjectWriter::writeBody()
{
    writeCustomFilters();
    writeFilterSections();
}

void QtHelpProjectWriter::writeFoot()
{
    writeEndElement();
    writeEndDocument();
}

bool QtHelpProjectWriter::writeFile(const QHelpDataInterface *helpData,
        const QString &fileName)
{
    Q_ASSERT(helpData != NULL);
    m_helpData = helpData;

    QFile out(fileName);
    if (!out.open(QIODevice::WriteOnly))
        return false;
    setDevice(&out);

    writeHead();
    writeBody();
    writeFoot();

    out.close();
    return true;
}

QT_END_NAMESPACE