/****************************************************************************
 **
 ** 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
