/***************************************************************************
 *   Copyright (C) 2004 by Judd Baileys                                    *
 *   jbaileys@bigpond.net.au                                               *
 *                                                                         *
 *   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 <khtmlview.h>
#include <khtml_part.h>
#include "docbrowser.h"
#include <qlayout.h>
#include <qvariant.h>
#include <qgroupbox.h>
#include <klocale.h>
#include <kstandarddirs.h>
#include <klineedit.h>
#include <kstatusbar.h>
#include <ktoolbar.h>
#include <kmessagebox.h>
#include <kiconloader.h>
#include <kstandarddirs.h>
#include <kglobal.h>
#include <kio/job.h>
#include <kio/netaccess.h>
//#include <kio/progressbase.h> 
#include "progresswidget.h"
#include <kparts/part.h>
//#include <kio/statusbarprogress.h>

const int URLLined = 1;

DocBrowser::DocBrowser (QWidget *parent, const char *name, WFlags fl) : QWidget(parent, name, fl)
{
    DocBrowserLayout = new QGridLayout( this, 1, 1, 11, 6, "DocBrowserLayout"); 
     toolbar = new KToolBar(this, "toolbar");   
      toolbar->insertButton(KGlobal::iconLoader()->loadIcon("gohome.png",KIcon::Panel,0,false),0, SIGNAL (clicked()), this, SLOT (homeURL ()));    
    

      toolbar->insertButton(UserIcon("kpppon.png"),0, SIGNAL (clicked()), this, SLOT (webURL ()));
      toolbar->insertSeparator(20,20);        
//      toolbar->setText("Location:");
      toolbar->insertLined ( "http://", URLLined, SIGNAL (returnPressed ()),this, SLOT (slotNewURL ()) );
      toolbar->setItemAutoSized (URLLined);   
      layout10 = new QVBoxLayout( 0, 0, 6, "layout10"); 
      loadingProgress = new ProgressWidget(this, "loadingProcess");     

//     statusbar = new KStatusBar( this, "statusBar");
     
//     statusbar->insertItem ("Ready", 1,100, TRUE);
//     statusbar->hide();
//      statusProgress = new KIO::StatusbarProgress(statusbar, 1 );     


//    statusbar->addWidget( statusProgress, 0 , 0 );  
//     statusbar->addWidget( statusProgress, 0 , 0 );
//     statusbar->addWidget(loadingProgress);
//     statusProgress->hide();
     khtmlpart = new KHTMLPart (this, "khtmlpart");
    
    KStandardDirs *dirs = KGlobal::dirs();
    QString docPath;
    docPath = dirs->findResource ("data", "kdsl/html/");  

    KURL url = docPath + "index.html";
    khtmlpart->openURL(url);
    khtmlpart->setMetaRefreshEnabled(true);
    khtmlpart->enablePlugins(true);
    khtmlpart->autoloadImages(true);
    khtmlpart->enableJava(true);
    khtmlpart->enableJScript(true);
    
    layout10->addWidget( toolbar);
    layout10->addWidget( khtmlpart->view());  
 //   layout10->addWidget(  statusbar); 
    layout10->addWidget(loadingProgress);
    DocBrowserLayout->addLayout( layout10, 0, 0 );

    connect( khtmlpart->browserExtension(),SIGNAL( openURLRequest( const KURL &, const KParts::URLArgs & ) ),
        this, SLOT( openURLRequest(const KURL &, const KParts::URLArgs & ) ) );	   
    connect( khtmlpart, SIGNAL( onURL(const QString & ) ), this, SLOT(statusBarText(const QString &) ) );
    khtmlpart->setProgressInfoEnabled(FALSE);
//      loadingProgress->kProgressWidget->delete();
//      loadingProgress->layout1->remove(loadingProgress->kProgressWidget );
 //     loadingProgress->kProgressWidget->hide();

}

void DocBrowser::openURLRequest(const KURL &url, const KParts::URLArgs & )
{
	QString tempURL;
 //     KIO::Job *job =  KIO::NetAccess::download(url, tempURL);
//      KIO::Job *job = KIO::get( url,0,0 );
//       KIO::Job *job =  KHTMLPart::openURL(url);
//      statusProgress->setJob( job );   
      khtmlpart->openURL(url);
 //      statusProgress->setJob(KIO::get (url,0,0));
   
 //       statusProgress->setJob(url);    
//	khtmlpart->openURL(url);
       toolbar->setLinedText(1, url.url());
}

//void DocBrowser::formSubmitNotification (const char *action, const QString &url, const QByteArray &formData, const QString &target, const QString &contentType, const QString &boundary)

//{

//}
void DocBrowser::statusBarText(const QString &url)
{
//  statusbar->changeItem(url, 1);
   loadingProgress->progressTextLabel->setText(url);
}

void DocBrowser::slotNewURL  ()
 {

  khtmlpart->openURL (toolbar->getLinedText (URLLined));
//   statusProgress->setProgressInfoEnabled(FALSE);
//     loadingProgress->progressTextLabel->setText("Ready");
//       statusProgress->setJob(KIO::get (toolbar->getLinedText (URLLined),0, 0));
//   statusbar->changeItem((toolbar->getLinedText (URLLined)), 1);
   return;
 }
 
 void DocBrowser::homeURL ()
 {
       KStandardDirs *dirs = KGlobal::dirs();
       QString docPath;
       docPath = dirs->findResource ("data", "kdsl/html/");
       KURL url = docPath + "index.html";
       khtmlpart->openURL(url);  

  
//TransferJob (const KURL &url, int command, const QByteArray &packedArgs, const QByteArray &_staticData, bool showProgressInfo)  
 //      KIO::TransferJob *job = KIO::get( url,FALSE, FALSE );
//       statusProgress->setJob(KIO::get (url,0,0));
       toolbar->setLinedText(1, docPath + "index.html" );    
}

void DocBrowser::webURL ()
{
      KURL url = "http://kdslbroadband.sourceforge.net";
 //       KIO::TransferJob *job = KIO::get( url );
 //      statusProgress->setJob( job );       
 //     khtmlpart->openURL(url); 
//      statusProgress->setJob(KIO::get (url, 1, 0));
      khtmlpart->openURL(url);
      toolbar->setLinedText(1, "http://kdslbroadband.sourceforge.net" );
}

 void DocBrowser::closeURL()
{
      khtmlpart->closeURL();
}

DocBrowser::~DocBrowser()
{
    // no need to delete child widgets, Qt does it all for us
}

