/***************************************************************************
 *   Copyright 2007-2008 Wouter Haffmans <cheetah@simply-life.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) 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 14 of version 3 of the license.                    *
 *                                                                         *
 *   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, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#ifndef PALETTEMANAGERXML_H
#define PALETTEMANAGERXML_H

#include "palettemanager.h"

class QDomDocument;
class QDomElement;

/**
 * This implementation of a PaletteManager supports reading the data from and
 * writing it to XML files.
 */
class PaletteManagerXML : public PaletteManager
{
Q_OBJECT
public:
    PaletteManagerXML(QObject *parent = 0);

    explicit PaletteManagerXML(QString fileName, QObject *parent = 0);

    ~PaletteManagerXML();

    virtual bool    loadFromFile(QString file);
    virtual bool    saveToFile(QString file) const;
    virtual void    addColor(QString name, Color color);
    virtual void    addPalette(QString name, Palette* palette);
    virtual void    clearColors();
    virtual void    clearPalettes();
    virtual void    removeColor(Color color);
    virtual void    removeColor(QString name);
    virtual void    removePalette(QString name);

    static QDomElement createColorDomElement(QDomDocument document, Color color, QString name = QString());
    static Color    elementToColor(QDomElement element);


public slots:
    virtual void    setColors(QMap<QString, Color> colors);
    virtual void    setPalettes(QMap<QString, Palette *> palettes);

private:
    bool            checkKidDrawElement() const;
    QDomElement     kidDrawElement();
    QDomElement     colorsElement();
    QDomElement     palettesElement();

    void            domClearColors();
    void            domClearPalettes();
    void            domSetColors(QMap<QString, Color> colors);
    void            domSetPalettes(QMap<QString, Palette *> palettes);
    bool            readDom();
    void            readDomColors();
    void            readDomPalettes();

    QDomElement     createPaletteElement(Palette *palette, QString name = QString());
    QDomElement     createColorElement(Color color, QString name = QString());
    QDomElement     colorToElement(QColor color, QString name,
                            QColor::Spec spec = QColor::Invalid);
    static QDomElement colorToDomElement(QDomDocument document, QColor color,
                            QString name = QString(), QColor::Spec spec = QColor::Invalid);

    static QColor   elementToQColor(QDomElement element);

    static int      readElementInt(QDomElement p, QString name,
                        int defValue = 0);

    QDomElement     createTextElement(QString name, QString contents);
    static QDomElement createTextElement(QDomDocument document, QString name, QString contents);

    QDomDocument*   m_document;

};

#endif
