/**
 * 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.
 **/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>

#include <tqapplication.h>
#include <tqcstring.h>
#include <tqtimer.h>
#include <tqmultilineedit.h>
#include <tqpushbutton.h>
#include <tqpopupmenu.h>
#include <tqslider.h>
#include <tqvaluelist.h>

#include <kprocess.h>
#include <tdemessagebox.h>
#include <tdeaboutdata.h>
#include <kdebug.h>
#include <tdeconfig.h>
#include <tdeaction.h>
#include <kstandarddirs.h>
#include <tdeparts/factory.h>

#include "kmplayerpartbase.h"
#include "kmplayer_koffice_part.h"
#include "kmplayerview.h"
#include "kmplayerconfig.h"

#ifdef HAVE_KOFFICE

#include <tqdom.h>
//#include <tqmetaobject.h>
#include <tqlayout.h>
#include <tqptrlist.h>
#include <tqpainter.h>
#include <KoFrame.h>

class KMPlayerFactory : public KParts::Factory {
public:
    KMPlayerFactory ();
    virtual ~KMPlayerFactory ();
    virtual KParts::Part *createPartObject
        (TQWidget *wparent, const char *wname, TQObject *parent, const char *name,
         const char *className, const TQStringList &args);
    static TDEInstance * instance () { return s_instance; }
private:
    static TDEInstance * s_instance;
};

K_EXPORT_COMPONENT_FACTORY (libkmplayerkofficepart, KMPlayerFactory)

TDEInstance *KMPlayerFactory::s_instance = 0;

KMPlayerFactory::KMPlayerFactory () {
    s_instance = new TDEInstance ("KMPlayerKofficePart");
}

KMPlayerFactory::~KMPlayerFactory () {
    delete s_instance;
}

KParts::Part *KMPlayerFactory::createPartObject
  (TQWidget *wparent, const char *wname,
   TQObject *parent, const char * name,
   const char * cls, const TQStringList & args) {
    if (strstr (cls, "KoDocument"))
        return new KOfficeMPlayer (wparent, wname, parent, name);
    return 0L;
}

//-----------------------------------------------------------------------------


KOfficeMPlayer::KOfficeMPlayer (TQWidget *parentWidget, const char *widgetName, TQObject* parent, const char* name, bool singleViewMode) 
  : KoDocument (parentWidget, widgetName, parent, name, singleViewMode),
    m_config (new TDEConfig ("kmplayerrc")),
    m_player (new KMPlayer::PartBase (parentWidget, 0L, 0L, 0L, m_config))
{
    setInstance (KMPlayerFactory::instance (), false);
    setReadWrite (false);
    m_player->init();
    m_player->setSource (m_player->sources () ["urlsource"]);
    //setWidget (view);
}

KOfficeMPlayer::~KOfficeMPlayer () {
    kdDebug() << "KOfficeMPlayer::~KOfficeMPlayer" << /*kdBacktrace() <<*/ endl;
}

void KOfficeMPlayer::paintContent (TQPainter& p, const TQRect& r, bool, double, double) {
    p.fillRect (r, TQBrush (TQColor (0, 0, 0)));
}

bool KOfficeMPlayer::initDoc(InitDocFlags flags, TQWidget* parentWidget) {
    kdDebug() << "KOfficeMPlayer::initDoc" << endl;
    return true;
}

bool KOfficeMPlayer::loadXML (TQIODevice *, const TQDomDocument & doc) {
    TQDomNode node = doc.firstChild ();
    if (node.isNull ()) return true;
    kdDebug() << "KOfficeMPlayer::loadXML " << node.nodeName () << endl; 
    node = node.firstChild ();
    if (node.isNull ()) return true;
    kdDebug() << "KOfficeMPlayer::loadXML " << node.nodeName () << endl; 
    node = node.firstChild ();
    if (node.isNull () || !node.isText ()) return true;
    m_player->setURL (KURL (node.toText ().data ()));
    return true;
}

bool KOfficeMPlayer::loadOasis (const TQDomDocument &, KoOasisStyles &, const TQDomDocument &, KoStore *) {
    return true;
}

bool KOfficeMPlayer::saveOasis( KoStore* store, KoXmlWriter* manifestWriter )
{
    return true;
}

TQDomDocument KOfficeMPlayer::saveXML() {
    TQDomDocument doc = createDomDocument ("kmplayer", TQString::number(1.0));
    TQDomElement docelm = doc.documentElement();
    docelm.setAttribute ("editor", "KMPlayer");
    docelm.setAttribute ("mime", "application/x-kmplayer");
    TQDomElement url = doc.createElement ("url");
    url.appendChild (doc.createTextNode (m_player->url ().url ()));
    doc.appendChild (url);
    return doc;
}

KoView* KOfficeMPlayer::createViewInstance (TQWidget* parent, const char* name) {
    kdDebug() << "KOfficeMPlayer::createViewInstance" << endl;
    return new KOfficeMPlayerView (this, parent);
}

KOfficeMPlayerView::KOfficeMPlayerView (KOfficeMPlayer* part, TQWidget* parent, const char* name)
    : KoView (part, parent, name),
      m_view (static_cast <KMPlayer::View*> (part->player ()->view ())) {
    kdDebug() << "KOfficeMPlayerView::KOfficeMPlayerView this:" << this << " parent:" << parent << endl;
    m_oldparent = static_cast <TQWidget*> (m_view->parent());
    m_view->reparent (this, TQPoint (0, 0));
    TQVBoxLayout * box = new TQVBoxLayout (this, 0, 0);
    box->addWidget (m_view);
}

KOfficeMPlayerView::~KOfficeMPlayerView () {
    kdDebug() << "KOfficeMPlayerView::~KOfficeMPlayerView this:" << this << endl;
    m_view->reparent (m_oldparent, TQPoint (0, 0));
}

#include "kmplayer_koffice_part.moc"
#endif