/**
 * Copyright (C) 2002-2003 by Koos Vriezen <koos.vriezen@gmail.com>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License version 2 as published by the Free Software Foundation.
 *
 *  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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 **/

#ifndef KMPLAYER_PART_H
#define KMPLAYER_PART_H

#include <tdeparts/browserextension.h>
#include "kmplayerpartbase.h"
#include "kmplayersource.h"


class TDEAboutData;
class KMPlayerPart;
class TDEInstance;
class JSCommandEntry;

/*
 * Wrapper source for URLSource that has a HREF attribute
 */
class KMPLAYER_NO_EXPORT KMPlayerHRefSource : public KMPlayer::Source {
    TQ_OBJECT
  
public:
    KMPlayerHRefSource (KMPlayer::PartBase * player);
    virtual ~KMPlayerHRefSource ();
    virtual bool processOutput (const TQString & line);
    virtual bool hasLength ();

    void setURL (const KURL &);
    void clear ();
    virtual TQString prettyName ();
public slots:
    virtual void init ();
    virtual void activate ();
    virtual void deactivate ();
    void finished ();
private slots:
    void grabReady (const TQString & path);
    void play ();
private:
    TQString m_grabfile;
    bool m_finished;
};


/*
 * Part notifications to hosting application
 */
class KMPLAYER_NO_EXPORT KMPlayerBrowserExtension : public KParts::BrowserExtension {
    TQ_OBJECT
  
public:
    KMPlayerBrowserExtension(KMPlayerPart *parent);
    KDE_NO_CDTOR_EXPORT ~KMPlayerBrowserExtension () {}
    void urlChanged (const TQString & url);
    void setLoadingProgress (int percentage);

    void saveState (TQDataStream & stream);
    void restoreState (TQDataStream & stream);
    void requestOpenURL (const KURL & url, const TQString & target, const TQString & service);
public slots:
    void slotRequestOpenURL (const KURL & url, const TQString & target);
};

/*
 * Part javascript support
 */
class KMPLAYER_NO_EXPORT KMPlayerLiveConnectExtension : public KParts::LiveConnectExtension {
    TQ_OBJECT
  
public:
    KMPlayerLiveConnectExtension (KMPlayerPart * parent);
    ~KMPlayerLiveConnectExtension ();

    // LiveConnect interface
    bool get (const unsigned long, const TQString &,
            KParts::LiveConnectExtension::Type &, unsigned long &, TQString &);
    bool put (const unsigned long, const TQString &, const TQString &);
    bool call (const unsigned long, const TQString &,
            const TQStringList &, KParts::LiveConnectExtension::Type &, 
            unsigned long &, TQString &);
    void unregister (const unsigned long);
    void sendEvent(const unsigned long objid, const TQString & event, const KParts::LiveConnectExtension::ArgList & args ) {
        emit partEvent(objid, event, args);
    }

    void enableFinishEvent (bool b = true) { m_enablefinish = b; }
signals:
    void partEvent (const unsigned long, const TQString &,
                    const KParts::LiveConnectExtension::ArgList &);
public slots:
    void setSize (int w, int h);
    void started ();
    void finished ();
    void evaluate (const TQString & script, TQString & result);
private:
    KMPlayerPart * player;
    TQString script_result;
    const JSCommandEntry * lastJSCommandEntry;
    bool m_started;
    bool m_enablefinish;
    bool m_evaluating;
};


/*
 * Part that gets created when used a KPart
 */
class KMPLAYER_NO_EXPORT KMPlayerPart : public KMPlayer::PartBase {
    TQ_OBJECT
  
    friend struct GroupPredicate;
public:
    enum Features {
        Feat_Unknown = 0,
        Feat_Viewer = 0x01, Feat_Controls = 0x02,
        Feat_Label = 0x04, Feat_StatusBar = 0x08,
        Feat_InfoPanel = 0x10, Feat_VolumeSlider = 0x20, Feat_PlayList = 0x40,
        Feat_ImageWindow = 0x80, Feat_All = 0xff
    };
    KMPlayerPart (TQWidget * wparent, const char * wname,
              TQObject * parent, const char * name, const TQStringList &args);
    ~KMPlayerPart ();

    KDE_NO_EXPORT KMPlayerBrowserExtension * browserextension() const
        { return m_browserextension; }
    KMPlayerLiveConnectExtension * liveconnectextension () const
        { return m_liveconnectextension; }
    KDE_NO_EXPORT bool hasFeature (int f) { return m_features & f; }
    bool allowRedir (const KURL & url) const;
    void connectToPart (KMPlayerPart *);
    KMPlayerPart * master () const { return m_master; }
    void setMaster (KMPlayerPart * m) { m_master = m; }
    virtual void setLoaded (int percentage);
    bool openNewURL (const KURL & url); // for JS interface
public slots:
    virtual bool openURL (const KURL & url);
    virtual bool closeURL ();
    void setMenuZoom (int id);
protected slots:
    virtual void playingStarted ();
    virtual void playingStopped ();
    void viewerPartDestroyed (TQObject *);
    void viewerPartProcessChanged (const char *);
    void viewerPartSourceChanged (KMPlayer::Source *, KMPlayer::Source *);
    void waitForImageWindowTimeOut ();
    void statusPosition (int pos, int length);
private:
    void setAutoControls (bool);
    KMPlayerPart * m_master;
    KMPlayerBrowserExtension * m_browserextension;
    KMPlayerLiveConnectExtension * m_liveconnectextension;
    TQString m_group;
    KURL m_docbase;
    TQString m_src_url;
    TQString m_file_name;
    int m_features;
    int last_time_left;
    bool m_started_emited : 1;
    //bool m_noresize : 1;
    bool m_havehref : 1;
};


#endif
