/* **************************************************************************

    Copyright 2008 Frederik Gladhorn <frederik.gladhorn@kdemail.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) any later version.                                   *
 *                                                                         *
 ************************************************************************* */


#ifndef PARLEYPLASMA_H
#define PARLEYPLASMA_H

#include <Plasma/Applet>
#include <Plasma/Svg>
#include <plasma/dataengine.h>
#include <plasma/plasma.h>

#include <QGraphicsTextItem>

#include "ui_config.h"

#include <kio/job.h>
#include <Plasma/PushButton>
#include <KJob>

#include "../libs/soundprovider/soundproviderfactory.h"
#include "../libs/soundprovider/wiktionaryprovider.h"

class KDialog;
class QFont;
class QSizeF;
class KConfigDialog;

/// DOC TODO
/**
 * DOC TODO
 */
class ParleyPlasma : public Plasma::Applet
{
    Q_OBJECT

    public:
        ParleyPlasma(QObject *parent, const QVariantList &args);
        ~ParleyPlasma();
        void init();
        void setContentSize(const QSizeF& size);
        QSizeF contentSizeHint() const;
        void paintInterface(QPainter *painter,
                            const QStyleOptionGraphicsItem *option,
                            const QRect& contentsRect);
        void constraintsEvent(Plasma::Constraints constraints);

    public slots:
        void createConfigurationInterface(KConfigDialog *parent);
        void configAccepted();
        void showFontSelectDlg();
        void dataUpdated(const QString &name, const Plasma::DataEngine::Data &data);
        // in the config dialog a file was selected
        void urlSelected (const KUrl &file);

        void soundFileLoaded(QString /* thread ID key */item, KUrl localSoundFile);
	void sayWord();

    protected:
        virtual void hoverEnterEvent ( QGraphicsSceneHoverEvent * event );
        virtual void hoverLeaveEvent ( QGraphicsSceneHoverEvent * event );
	
        void resizeItems();

        void lookupVoiceFile(QString word, QString lang, bool);
        void lookupWiktionaryFor(QString word, QString lang, bool);

    private:
        enum ShowSolution {
            Hover = 0,
            Always
        };
        Plasma::Svg *m_theme;
        QGraphicsTextItem *m_label1;
        QGraphicsTextItem *m_label2;

        Ui::config ui;

        QFont m_font;
        QSizeF m_size;

        Plasma::DataEngine* m_engine;

	/// URL of the vocabulary file selected from the user in the konfig dialogue.
        KUrl m_sourceFile;

        /// List of languages available in loaded vocabulary file.
        QStringList m_languages;

        /// Enum of ShowSolution, so either "Hover" or "Always".
        int m_solutionType;

        int m_lang1;
        int m_lang2;
        int m_updateInterval;

	/// index which sound provider shall be used or 
        /// 0 if no sound output shall be supported (default)
	int m_soundProvider;

        /// the URL the sound file can be loaded from or empty if none is avail.
        QUrl m_soundurl;

	/** button to trigger download and playing of the sound file. 
         * If green a sound file is available, if red/orange none found, 
         * if grey still searching.
         */
        Plasma::PushButton *m_play;
};

K_EXPORT_PLASMA_APPLET(parley, ParleyPlasma)

#endif
