/***************************************************************************
 *   Copyright (C) 2005 by Armando Navarro-Vazquez                         *
 *   armando@linux                                                         *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include <kparts/genericfactory.h>

#include "kryomolchimeextension.h"
#include "khtml_part.h"
#include <qlayout.h>
#include <qobject.h>





#include <qpushbutton.h>
#include <qmessagebox.h>
//Added by qt3to4:
#include <Q3HBoxLayout>
typedef KParts::GenericFactory<KryoMolChimeExtension> KryoMolChimeExtensionFactory;
K_EXPORT_COMPONENT_FACTORY ( libkryomolchime /*library name*/, KryoMolChimeExtensionFactory );
KryoMolChimeExtension::KryoMolChimeExtension ( QWidget *parentWidget, const char *widgetName,                           QObject *parent, const char *name, const QStringList &args )
    : KParts::ReadOnlyPart ( parent, name )
{
  kDebug() << "Chime Extension";
  setInstance ( KryoMolChimeExtensionFactory::instance() );
  QWidget* w= new QWidget();
  Q3HBoxLayout* l = new Q3HBoxLayout ( w );
  QPushButton* button = new QPushButton ( w );
  l->addWidget ( button );
  connect ( button,SIGNAL ( clicked() ),this,SLOT ( OnButtonClicked() ) );
  setWidget ( w );
  m_htmlparent= dynamic_cast<KHTMLPart*> ( parent );
  if ( m_htmlparent )
  {
    connect ( m_htmlparent,SIGNAL ( completed() ),this,SLOT ( OnParentCompleted() ) );
  }
  ParseScript ( args );

}



KryoMolChimeExtension::~KryoMolChimeExtension()
{
}

KAboutData* KryoMolChimeExtension::createAboutData()
{
  KAboutData* aboutData =
      new KAboutData ( "kryomol", I18N_NOOP ( "KryoMol" ),
                       "0.6.5", I18N_NOOP ( "Molecular Visor" ),
                       KAboutData::License_GPL,
                       "(c) 2003, Armando Navarro-Vazquez  <qoajnv@usc.es>" );
  return aboutData;
}

bool KryoMolChimeExtension::openFile()
{

  return true;
}


void KryoMolChimeExtension::OnParentCompleted()
{

  KParts::BrowserHostExtension* bt= m_htmlparent->browserHostExtension();
  QObjectList* lt=m_htmlparent->queryList ( "KryoMolPart" );
  QObject* it;
  for ( it=lt->first();it;it=lt->next() )
  {
    connect ( this,SIGNAL ( script ( const QString& ) ),it,SLOT ( ParseChimeScript ( const QString& ) ) );
  }

}

void KryoMolChimeExtension::ParseScript ( const QStringList& args )
{
  QStringList::const_iterator it;
  for ( it=args.begin();it!=args.end();++it )
  {
    int found= ( *it ).find ( "TARGET",0,false );
    if ( found >= 0 )
    {
      m_target= ( *it ).section ( '=',-1 );
      m_target.remove ( '"' );
    }

    found= ( *it ).find ( "SCRIPT",0,false );
    if ( found >= 0 )
    {
      m_script= ( *it ).section ( '=',-1 );
      m_script.remove ( '"' );
    }


  }
}

void KryoMolChimeExtension::OnButtonClicked()
{
  emit script ( m_script );
}
