/**
 * Copyright 2009  Michael Leupold <lemma@confuego.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3, or any
 * later version accepted by the membership of KDE e.V. (or its
 * successor approved by the membership of KDE e.V.), which shall
 * act as a proxy defined in Section 6 of version 3 of the license.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef XMLKIOENTITIESLOADER_H
#define XMLKIOENTITIESLOADER_H

#include <QtCore/QObject>
#include <QtCore/QEventLoop>
#include <QtCore/QStack>
#include <KUrl>
#include <KIO/Job>

extern "C" {
    #include <libxml/xmlIO.h>
    #include <libxslt/documents.h>
}

/**
 * External entity loader for libxml.
 */
xmlParserInputPtr xmlKioEntitiesLoader(const char *URL, const char *ID,
                                       xmlParserCtxtPtr ctxt);
/**
 * Doc loader for libxslt.
 */
xmlDocPtr xsltKioDocLoader(const xmlChar *URI, xmlDictPtr dict, int options,
                           void *ctxt, xsltLoadType type);


/**
 * Install the custom entity loader on construction and remove it
 * on destruction.
 */
class KioEntityLoaderInstaller
{
public:
    KioEntityLoaderInstaller();
    ~KioEntityLoaderInstaller();
};
                           
/**
 * Created by xmlKioEntitiesLoader to track loading of external
 * entities.
 *
 * @internal
 */
class KioEntityLoader : public QObject
{
    Q_OBJECT

public:
    /**
     * Create a new request for an entity.
     */
    KioEntityLoader(const KUrl &url);

    /**
     * Destructor
     */
    virtual ~KioEntityLoader();

    /**
     * Start the slot synchroneously (using a QEventLoop) and
     * return the data received when finished.
     */
    QByteArray run();

    /**
     * Determine if the entity loader is currently loaded
     */
    static bool isLoaded();
    
    /**
     * Install the external entity loader. This is meant to be
     * called on loading the plugin.
     */
    static void installEntityLoader();

    /**
     * Remove the external entity loader.
     *
     * @remarks Actually it isn't removed, it just calls the previous
     *          default loader instead.
     */
    static void uninstallEntityLoader();

private Q_SLOTS:
    /**
     * Received data from the job
     */
    void slotData(KIO::Job *, const QByteArray &data);

    /**
     * Slot finished with a result code
     */
    void slotResult(KJob *job);
    
    // TODO: handle error

private:
    const KUrl m_url;
    QEventLoop m_waitLoop;
    QByteArray m_data;
    int m_errorCode;
    static bool s_isLoaded;
};

#endif
