/***************************************************************************
                          dcfiletransferinfo.cpp  -  description
                             -------------------
    begin                : Sam Jul 13 2002
    copyright            : (C) 2002-2004 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 "dcfiletransferinfo.h"

#include <QLabel>
#include <QPixmap>
#include <QPainter>
#include <QLineEdit>
#include <QProgressBar>
#include <QFileInfo>

#include <dclib/dcos.h>
#include <dclib/cdownloadqueue.h>

#include "dcconfig.h"
#include "dcconnectionmanager.h"
#include "dctransferview.h"
#include "dcguiutils.h"
#include "dciconloader.h"

#include <limits.h>

/** */
DCFileTransferInfo::DCFileTransferInfo( CString filename, QWidget * parent ) : QWidget( parent )
{
	setupUi(this);
	
	setWindowIcon( g_pIconLoader->GetPixmap(eiDOWNLOAD) );
	setAttribute(Qt::WA_DeleteOnClose);
	
	QFileInfo fi(QString::fromAscii(filename.Data()));
	setWindowTitle( fi.fileName() + " - " + tr("File Transfer Info") );
	
	m_sFileName = filename;

	InitDocument();

	connect( &m_Timer, SIGNAL(timeout()), SLOT(timerDone()) );

	m_Timer.setSingleShot( true );
	m_Timer.start( 0 );
}

/** */
DCFileTransferInfo::~DCFileTransferInfo()
{
	m_Timer.stop();
}

/** */
void DCFileTransferInfo::InitDocument()
{
	DCFileChunkObject * pFileChunk;

	if ( g_pTransferView )
	{
		if ( (pFileChunk=g_pTransferView->DLM_QueueGetFileChunk(m_sFileName)) != 0 )
		{
			LineEdit_FILENAME->setText( pFileChunk->m_sLocalFile.Data() );
			LineEdit_HASH->setText( pFileChunk->m_sHash.Data() );
			
			delete pFileChunk;
		}
	}
	
	progressBar1->setMinimum(0);
}

/** */
void DCFileTransferInfo::timerDone()
{
	if ( g_pTransferView )
	{
		DCFileChunkObject * pFileChunk;
		if ( (pFileChunk=g_pTransferView->DLM_QueueGetFileChunk(m_sFileName)) != 0 )
		{
			LineEdit_FILESIZE->setText( DCGuiUtils::GetSizeString(pFileChunk->m_nSizeDone) +
					" / " + DCGuiUtils::GetSizeString(pFileChunk->m_nSize) );
			LineEdit_SOURCES->setText( QString().setNum(pFileChunk->m_nReferenceCount) );
			
			if ( pFileChunk->m_nSize > (ulonglong) INT_MAX )
			{
				progressBar1->setMaximum((int) (pFileChunk->m_nSize/1048576));
				progressBar1->setValue((int) (pFileChunk->m_nSizeDone/1048576));
			}
			else
			{
				progressBar1->setMaximum((int) pFileChunk->m_nSize);
				progressBar1->setValue((int) pFileChunk->m_nSizeDone);
			}

			int x1,x2;
			DCChunkObject * ChunkObject;
			QPainter pa;
			QPixmap p(PixmapLabel_CHUNKINFO->size());
			p.fill(Qt::white);
			int width = p.width();
			int area = width * p.height();
			
			pa.begin(&p);

			if ( pFileChunk->m_nSize != 0 )
			{
				ChunkObject = 0;

				while( (ChunkObject=pFileChunk->m_Chunks.Next(ChunkObject)) != 0 )
				{
					if ( ChunkObject->m_eChunkState == ecsFREE )
						pa.setPen( Qt::red );
					else
						pa.setPen( Qt::green );

					x1 = (ChunkObject->m_nStart*area/pFileChunk->m_nSize);
        				x2 = (ChunkObject->m_nEnd*area/pFileChunk->m_nSize);

					int ys,xs,ye,xe,y;

					xs = x1%width;
					ys = (x1-xs)/width;
					xe = x2%width;
					ye = (x2-xe)/width;

					// printf("%d %d %d %d\n",xs,xe,ys,ye);

					for(y=ys;y<=ye;y++)
					{
						if( (y==ys) && (y==ye) )
							pa.drawLine( xs, y, xe, y );
						else if ( y==ys )
							pa.drawLine( xs, y, width, y );
						else if ( y==ye )
							pa.drawLine( 0, y, xe, y );
						else
							pa.drawLine( 0, y, width, y );
					}
				}
			}
			
			delete pFileChunk;

			pa.end();

			PixmapLabel_CHUNKINFO->setPixmap(p);
		}
	}

	m_Timer.setSingleShot( true );
	m_Timer.start( 5000 );
}

void DCFileTransferInfo::closeEvent( QCloseEvent *e)
{
        emit onDie((QWidget*)this->parent());//this widget is always shows in a parent window

        e->accept();
}
