Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лабы / 3 / 2.tar / 2 / 2 / net_copy / ftransfer

.cpp
Скачиваний:
16
Добавлен:
17.04.2013
Размер:
5.3 Кб
Скачать
/*
  netFileCopyer
  Copyright (C) 2004 Dmitry S. Vasilchenko.

  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

  The GNU GPL is contained in /usr/doc/copyright/GPL on a Debian
  system and in the file COPYING in the Linux kernel source.
*/

#include <qapplication.h>

#include <qpushbutton.h>
#include <qlabel.h>
#include <qprogressbar.h> 
#include <qtextedit.h>
#include <qlayout.h>
#include <qtimer.h>

#include <time.h>
#include <math.h>

#include "ftransfer.h"
#include "uthreads.h"


static const char *stateStrings[] = {
/*00*/ "Preparing for transfer",
/*01*/ "",
/*02*/ "Connecting",
/*03*/ "Accepting connection",
/*04*/ "",
/*05*/ "Send file",
/*06*/ "",
/*07*/ "",
/*08*/ "",
/*09*/ "",
/*10*/ "",
/*11*/ "",
/*12*/ "Receive file",
/*13*/ "",
/*14*/ "",
/*15*/ "",
/*16*/ "",
/*17*/ "",
/*18*/ "",
/*19*/ "",
/*20*/ "Transfer finished", 
/*21*/ "Socket error",
/*22*/ "Connection error",
/*23*/ "Error while sending info",
/*24*/ "Error getting ACK",
/*25*/ "",
/*26*/ "File open error",
/*27*/ "File read error",
/*28*/ "File send error",
/*29*/ "",
/*30*/ "",

/*31*/ "Error accepting connection",
/*32*/ "Error getting info",
/*33*/ "Error sending ACK",
/*34*/ "File write error",
/*35*/ "File receive error",
/*36*/ "Common error!"
};

FTransferDialog::FTransferDialog(QWidget *parent, const char *name,
	bool modal, WFlags f, 
	QString ip, int port, QString file, int state)
    	: QDialog(parent, name, modal, f)
{
	if (state == S_SEND)
		setCaption(tr("Send file"));
	else
		setCaption(tr("Receive file"));
		
	progress = new QProgressBar(this);
	progress->setTotalSteps(100);
	
	stateLabel = new QLabel(tr("Preparing..."), this);
	fnameLabel = new QLabel(tr("Transfer file: unknown"), this);
	timeLabel = new QLabel(tr("time: 0sec"), this);
	speedLabel = new QLabel(tr("speed: 0b/sec"), this);
	
	haltButton = new QPushButton(tr("Stop!"),this);
	
	statEdit = new QTextEdit(this);
	statEdit->setTextFormat(Qt::LogText);

	statEdit->append( "Remote IP: "+ip );
	if (state == S_SEND){
		statEdit->append( QString( "Remote port: %1" ).arg( port ) );
		statEdit->append( "Send local file: "+file );
	}
	else{
		statEdit->append( QString( "Local port: %1" ).arg( port ) );
		statEdit->append( "Receive into folder: "+file );
	}
    
	connect(haltButton, SIGNAL(clicked()), 
			this, SLOT(close()));
	
	QVBoxLayout *topLayout = new QVBoxLayout;
	topLayout->addWidget(stateLabel);
	topLayout->addWidget(fnameLabel);
	topLayout->addWidget(timeLabel);
	topLayout->addWidget(speedLabel);
	topLayout->addWidget(progress);
	topLayout->addWidget(statEdit);
	
	QHBoxLayout *bottomLayout = new QHBoxLayout;
	bottomLayout->addStretch(1);
	bottomLayout->addWidget(haltButton);
	
	QVBoxLayout *mainLayout = new QVBoxLayout(this);
	mainLayout->setMargin(11);
	mainLayout->setSpacing(6);
	mainLayout->addLayout(topLayout);
	mainLayout->addLayout(bottomLayout);

	file_size = 0;
	file_progress = 0;
	start_time = 0;
	file_name = "";
	stage = STAGE_PRE;
	
	start_transfer = false;
	 
	/* starting connection thread */
	tmain = new ThMain();
	tmain->setParams(ip, port, file, state);
	tmain->pdialog = this;
	
	tmain->start();
	
	upTimer = new QTimer( this );
	connect( upTimer, SIGNAL(timeout()), this, SLOT(viewUpdate()) );
	upTimer->start( 500, false );
}

void FTransferDialog::closeEvent( QCloseEvent *event)
{
	if (upTimer->isActive()) upTimer->stop();
	if (tmain->running()) tmain->stop();
	while (tmain->running()) usleep(100);
	
	event->accept();
}

void FTransferDialog::stateUpdate(int pstage,
		size_t pfile_size, size_t pfile_progress,
		time_t pstart_time, QString pfile_name)
{
	stage = pstage;
	file_size = pfile_size;
	file_progress = pfile_progress;
	start_time = pstart_time;
	file_name = pfile_name;
	if ( stage == STAGE_ERROR && tmain->oi_estate() )
		last_error = tmain->oi_last_error();
}

void FTransferDialog::viewUpdate()
{
	
	/* current stage */
	if (!last_error.isNull()){
		stateLabel->setText(last_error);
	}
	else
		stateLabel->setText(tr(stateStrings[stage]));
	
	if (start_transfer){
		/* file name */
		fnameLabel->setText(tr("Transfer file: %1").arg(file_name));
		
		time_t el_time = time(NULL) - start_time;

		/* execution time */
		timeLabel->setText(tr("time: %1sec").arg((unsigned int)el_time,0,10));
	
		/* transfer speed */
		size_t speed = (el_time==0) ? 0 : (file_progress/el_time);
		speedLabel->setText(tr("speed: %1kb/sec").arg(speed/1024,0,10));

		/* progress indicator */
		if (file_size > 0){
			progress->setProgress( (int)floor(  100* 
					((float)(file_progress) / (float)file_size ) 
				) );
		}
	}
	
	if (stage >= STAGE_FINISH){
		upTimer->stop();
		haltButton->setText(tr("Close"));
	}
}


Соседние файлы в папке net_copy