/****************************************************************************
 **
 ** 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 "htmlhelpdatainterface_p.h"
#include "htmlhelpparser_p.h"

#include <QtCore/QFileInfo>
#include <QtCore/QDir>
#include <QtCore/QMap>
#include <QtCore/QString>
#include <QtCore/QVariant>
#include <QtCore/QSettings>

QT_BEGIN_NAMESPACE

HtmlHelpDataInterface::HtmlHelpDataInterface(const QString &projectFileName,
        const QString &namespaceName, const QString &virtualFolder)
        : m_nameSpace(namespaceName), m_virtualFolder(virtualFolder)
{
    initFromDoxygenHtmlHelpProject(projectFileName);
}

void HtmlHelpDataInterface::initFromDoxygenHtmlHelpProject(
        const QString &projectFileName)
{
    // Parse hhc/hhk/hhp
    QSettings iniReader(projectFileName, QSettings::IniFormat);
    m_projectTitle = iniReader.value("OPTIONS/Title").toString();
    m_filterIdent = m_projectTitle;
    m_entryPageHref = iniReader.value("OPTIONS/Default topic",
            "index.html").toString();
    parseToc(getTocFileName(projectFileName));
    parseIndex(getIndexFileName(projectFileName));

    // File list
    m_rootPath = getRootPath(projectFileName);
    scanRootPath();

    // Custom filters
    QHelpDataCustomFilter everything;
    everything.filterAttributes << m_filterIdent;
    everything.name = m_projectTitle;
    m_custumFilters << everything;

    // Sections
    QHelpDataFilterSection section;
    section.addFilterAttribute(m_filterIdent);
    section.setIndices(m_index);
    section.setContents(m_toc);
    section.setFiles(m_files);
    m_filterSections.append(section);
}

const QStringList HtmlHelpDataInterface::applyPrefix(const QStringList &list,
        const QString &prefix) const
{
    if (prefix.isNull()) {
        return list;
    }

    QStringList processed;
    foreach (const QString &entry, list) {
        processed << prefix + "/" + entry;
    }
    return processed;
}

QStringList HtmlHelpDataInterface::findSubDirs(const QString &dir) const
{
    return QDir(dir).entryList(QDir::Dirs | QDir::NoDotAndDotDot);
}

QStringList HtmlHelpDataInterface::findDoxygenFiles(const QString &dir) const
{
    const QStringList filter = QStringList()
            << "*.css"
            << "*.gif"
            << "*.htm"
            << "*.html"
            << "*.jpeg"
            << "*.jpg"
            << "*.png"
            ;
    return QDir(dir).entryList(filter);
}

QStringList HtmlHelpDataInterface::findDoxygenFilesRecursively(const QString &dir,
        const QString &prefix) const
{
    QStringList files;
    foreach (const QString &subDir, findSubDirs(dir)) {
        files += findDoxygenFilesRecursively(dir + "/" + subDir,
                prefix.isNull() ? subDir : prefix + "/" + subDir);
    }
    return files + applyPrefix(findDoxygenFiles(dir), prefix);
}

void HtmlHelpDataInterface::scanRootPath()
{
    m_files = findDoxygenFilesRecursively(m_rootPath);
}

void HtmlHelpDataInterface::parseToc(const QString &fileName)
{
    HtmlHelpParser parser;
    parser.parseFile(fileName);
    m_toc = parser.stealToc(m_projectTitle, m_entryPageHref);
}

void HtmlHelpDataInterface::parseIndex(const QString &fileName)
{
    HtmlHelpParser parser;
    parser.parseFile(fileName);
    m_index = parser.stealIndex();
}

QString HtmlHelpDataInterface::replaceFileExtension(const QString &fileName,
        const QString &extension)
{
    QFileInfo fileInfo(fileName);
    const int suffixLen = fileInfo.suffix().count();
    return fileName.left(fileName.count() - suffixLen) + extension;
}

QString HtmlHelpDataInterface::getTocFileName(const QString &projectFileName)
{
    return replaceFileExtension(projectFileName, QLatin1String("hhc"));
}

QString HtmlHelpDataInterface::getIndexFileName(const QString &projectFileName)
{
    return replaceFileExtension(projectFileName, QLatin1String("hhk"));
}

QString HtmlHelpDataInterface::getRootPath(const QString &projectFileName)
{
    QFileInfo fileInfo(projectFileName);
    return fileInfo.dir().absolutePath();
}

QString HtmlHelpDataInterface::namespaceName() const
{
    return m_nameSpace;
}

QString HtmlHelpDataInterface::virtualFolder() const
{
    return m_virtualFolder;
}

QList<QHelpDataCustomFilter> HtmlHelpDataInterface::customFilters() const
{
    return m_custumFilters;
}

QList<QHelpDataFilterSection> HtmlHelpDataInterface::filterSections() const
{
    return m_filterSections;
}

QMap<QString, QVariant> HtmlHelpDataInterface::metaData() const
{
    return QMap<QString, QVariant>();
}

QString HtmlHelpDataInterface::rootPath() const
{
    return m_rootPath;
}

QT_END_NAMESPACE
