/***************************************************************************
                          dcgeturl.cpp  -  description
                             -------------------
    begin                : Thu Oct 4 2001
    copyright            : (C) 2001 by Mathias Küster
    email                : mathen@users.berlios.de
 ***************************************************************************/

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

#include <qlabel.h>
#include <qpushbutton.h>
#include <QHttp>
#include <QUrl>
#include <QProgressBar>

#include <dclib/core/cbytearray.h>

#include "dcgeturl.h"

#include <dclib/dcos.h>

DCGetURL::DCGetURL( QString sURL )
	: QDialog( 0, "", TRUE )
{
	setupUi(this);
	
	m_pHttp  = new QHttp();
	
	m_pUrl   = new QUrl(sURL);
	m_pHttp->setHost( m_pUrl->host() );
	
	m_baData = new CByteArray();
	m_baData->SetSize(0);

	m_bRun = FALSE;

	InitDocument();
}

DCGetURL::~DCGetURL()
{
	if ( m_pHttp )
		delete m_pHttp;

	if ( m_pUrl )
		delete m_pUrl;

	if ( m_baData )
		delete m_baData;
}

/** */
void DCGetURL::InitDocument()
{
	connect( m_pHttp, SIGNAL(stateChanged( int )), this, SLOT(slotStateChanged( int )) );
	connect( m_pHttp, SIGNAL(dataReadProgress( int, int )), this, SLOT(slotDataReadProgress( int, int )) );
	connect( m_pHttp, SIGNAL(done( bool )), this, SLOT(slotFinished( bool )) );
	connect( PushButton_CANCEL, SIGNAL(clicked()), this, SLOT(reject()));
	
	ProgressBar_Download->setMinimum(0);
}

/** */
void DCGetURL::show()
{
	if ( !m_bRun )
	{
		m_pHttp->get( m_pUrl->path() );
		m_bRun = TRUE;
	}

	QDialog::show();
}

/** */
void DCGetURL::GetData( CByteArray * data )
{
	if(!data)
		return;

	data->SetSize(0);
	data->Append( m_baData->Data(), m_baData->Size() );
}

/** */
void DCGetURL::slotStateChanged( int state )
{
	switch ( state )
	{
		case QHttp::Unconnected:
			TextLabel_Status->setText(tr("Unconnected."));
			break;
		case QHttp::HostLookup:
			TextLabel_Status->setText(tr("Looking up ") + m_pUrl->host() + " ...");
			break;
		case QHttp::Connecting:
			TextLabel_Status->setText(tr("Connecting..."));
			break;
		/* case QHttp::Sending:
			TextLabel_Status->setText(tr("Sending data..."));
			break;
		case QHttp::Reading:
			TextLabel_Status->setText(tr("Reading data..."));
			break; */
		case QHttp::Connected:
			TextLabel_Status->setText(tr("Connected."));
			break;
		case QHttp::Closing:
			TextLabel_Status->setText(tr("Closing..."));
			break;
		default:
			break;
	}
}

/** */
void DCGetURL::slotDataReadProgress( int bytesDone, int bytesTotal )
{
	QString s;

	ProgressBar_Download->setMaximum(bytesTotal);
	ProgressBar_Download->setValue(bytesDone);

	s.sprintf("%d/%d",bytesDone,bytesTotal);

	TextLabel_Status->setText(s);
}

/** */
void DCGetURL::slotFinished( bool error )
{
	if ( error == TRUE )
	{
		TextLabel_Status->setText(m_pHttp->errorString());
		
		reject();
	}
	else
	{
		TextLabel_Status->setText(tr("Finished."));
		
		m_baData->Append( m_pHttp->readAll().data(), m_pHttp->bytesAvailable() );
		
		accept();
	}
}
