/***************************************************************************
**                        WEBCAMTV	   v0.1.1
**
**                      mai-juin 2001
**              par: VIDEON, Mourad BEN SALEM
**            E-mail:mouradbs@yahoo.com, mourad@webcamtv.org
**            Videon: http://www.videontv.org
**            QT:    http://www.trolltech.com
****************************************************************************/
#include "webcamtv.h"

#include <qurloperator.h> 
#include <qlabel.h>
#include <qpushbutton.h>
#include <qlayout.h>
#include <qvariant.h>
#include <qtooltip.h>
#include <qwhatsthis.h>
#include <qimage.h>
#include <qpixmap.h>
#include <visimsrc.h>
#include <qstring.h>
#include <qlineedit.h> 
#include <qsocket.h> 



/* 
 *  Constructs a Webcamtv which is a child of 'parent', with the 
 *  name 'name' and widget flags set to 'f' 
 */

Cam::Cam( QWidget* parent,  const char* name, WFlags fl )
    : QWidget( parent, name, fl )
{

}



Webcamtv::Webcamtv( QWidget* parent,  const char* name, WFlags fl )
    : QWidget( parent, name, fl )
{
	const QString u("ftp://webcamtv.videontv.org");

    if ( !name )
	setName( "Webcamtv" );
    resize( 508, 339 ); 
    setCaption( tr( "WebcamTV" ) );

	currentURL = u;

	QPixmap *Scr = new QPixmap("logo.jpg");
	QPixmap *Ona = new QPixmap("red.jpg");

	url1 = new QLineEdit(this, "URL");
	url1->setGeometry(QRect(10,10,300,22));
	url1->setReadOnly( TRUE );
	url1->setEchoMode ( QLineEdit::Normal );


	Sock = new QSocket(this, "Socket");
	Sock->connectToHost ( currentURL, 11111 ); 

    Send = new QPushButton( this, "Send" );
    Send->setGeometry( QRect( 410, 50, 80, 26 ) ); 
    Send->setText( tr( "Send" ) );
    Send->setAutoResize( TRUE );

	Camera = new Cam( this, "camera" );
     
	ImageCam = new TV("logo.jpg", this, "Camera" );   

    Toki = new TV("red.jpg", this, "Toki" );
    

    
	// signals and slots connections

    connect( Send, SIGNAL(clicked()), this, SLOT(send_clicked()) );
	connect( Camera, SIGNAL(recivedChanged(QPixmap *)), this, SLOT(image(QPixmap *)) );
	connect( url1,SIGNAL(textChanged( const QString & )), this, SLOT(setURL( const QString & )) );
	connect( Sock,SIGNAL(readyRead () ), this, SLOT(setTally()));

}

/*  
 *  Destroys the object and frees any allocated resources
 */
Webcamtv::~Webcamtv()
{
    // no need to delete child widgets, Qt does it all for us
}

Cam::~Cam()
{
    // no need to delete child widgets, Qt does it all for us
}




void Webcamtv::send_ftp()
{
QByteArray a;
QDataStream s(a,IO_WriteOnly);
s << Image;

/// We can insert a security module to send the images in anonymity.


QUrlOperator op( currentURL );
op.put( a );


}
void Webcamtv::send_clicked()
{
	Webcamtv::send_ftp();
}

void Webcamtv::setURL(const QString &newurl)
{

Sock->close();
currentURL = newurl;
Sock->connectToHost ( currentURL, 11111 );

}

void Webcamtv::setTally()
{
QString S=Sock->readLine ();

if(S.compare("green")) emit	tallyChanged( 1 );
else emit	tallyChanged( 0 );


}


void Webcamtv::image(QPixmap* a)
{
	ImageCam->setVideo(a);
	Image = *a;
}

void Cam::setRecived(CVisRGBAByteImage *v)
{
	  QDataStream s;
	  
	  QImage *pv;
	  int i=0;
	  while (i<20)
	  {
	   i++;
	  }

	  pv = (QImage *) v	;

	emit recivedChanged(pv);     
}



void Cam::initCam()
{

imagesource = VisFindImageSource("");

}

void Cam::play()
{
if (imagesource.IsValid())
	{
	CVisSequence<CVisRGBABytePixel> sequence;
	sequence.ConnectToSource(imagesource, true, false);

	CVisRGBAByteImage imag;
	if (sequence.PopFront(imag,500))
		{
		setRecived(&imag);
		}
	}
}

void Cam::stop()
{

	sequence.DisconnectFromSource();

}


