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

Лабы / 3 / 3.tar / 3 / 3 / net_copy / prepare

.cpp
Скачиваний:
17
Добавлен:
17.04.2013
Размер:
6.47 Кб
Скачать
/*
  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 <qlineedit.h>
#include <qpushbutton.h>
#include <qspinbox.h>
#include <qlabel.h>
#include <qradiobutton.h>
#include <qlayout.h>
#include <qbuttongroup.h>
#include <qfiledialog.h>
#include <qmessagebox.h>


#include "prepare.h"
#include "ftransfer.h"


PrepareDialog::PrepareDialog(QWidget *parent, const char *name)
    : QDialog(parent, name)
{
	setCaption(tr("Transfer prepare"));
	
	/* Send */
	s_ipLabel = new QLabel(tr("Remote ip: "), this);
	s_ipEdit = new QLineEdit(this);
	s_ipEdit->setText("127.0.0.1");
	
	s_portLabel = new QLabel(tr("Remote port: "), this);
	s_portBox = new QSpinBox(this);
	s_portBox->setRange(1024,65535);

	s_fileLabel = new QLabel(tr("Select file: "), this);
	s_fileEdit = new QLineEdit(this);
	s_fileEdit->setEnabled(false);
	s_fileButton = new QPushButton(tr(">>"), this);

	/* Receive */
	r_portLabel = new QLabel(tr("Local port: "), this);
	r_portBox = new QSpinBox(this);
	r_portBox->setRange(1024,65535);

	r_folderLabel = new QLabel(tr("Select folder: "), this);
	r_folderEdit = new QLineEdit(this);
	r_folderEdit->setEnabled(false);
	r_folderButton = new QPushButton(tr(">>"), this);
	
	
	startButton = new QPushButton(tr("&Start"), this);
	startButton->setDefault(true);
	
	quitButton = new QPushButton(tr("&Quit"), this);

	s_Button = new QRadioButton(tr("Send file"), this);
	s_Button->setChecked(false);

	r_Button = new QRadioButton(tr("Receive file"), this);
	r_Button->setChecked(true);
	
	QButtonGroup *buttonGroup = new QButtonGroup(0);
	buttonGroup->insert(s_Button);
	buttonGroup->insert(r_Button);
		
	connect(s_fileButton, SIGNAL(clicked()),
		this, SLOT(selectFile()));
	connect(r_folderButton, SIGNAL(clicked()),
		this, SLOT(selectFile()));

	connect(s_Button, SIGNAL(stateChanged( int )),
		this, SLOT(checkboxChanged( int )));
	connect(r_Button, SIGNAL(stateChanged( int )),
		this, SLOT(checkboxChanged( int )));
		
	connect(startButton, SIGNAL(clicked()),
		this, SLOT(startClicked()));
	
	connect(quitButton, SIGNAL(clicked()),
		this, SLOT(close()));     
	
	/* Send */
	QHBoxLayout *l_ip = new QHBoxLayout;
	l_ip->addWidget(s_ipLabel);
	l_ip->addWidget(s_ipEdit);

	QHBoxLayout *l_sport = new QHBoxLayout;
	l_sport->addWidget(s_portLabel);
	l_sport->addWidget(s_portBox);
	
	QHBoxLayout *l_sfile = new QHBoxLayout;
	l_sfile->addWidget(s_fileLabel);
	l_sfile->addWidget(s_fileEdit);
	l_sfile->addWidget(s_fileButton);
	
	QVBoxLayout *l_s = new QVBoxLayout;
	l_s->addWidget(s_Button);
	l_s->addLayout(l_ip);
	l_s->addLayout(l_sport);
	l_s->addLayout(l_sfile);
	
	/* Receive */
	QHBoxLayout *l_rport = new QHBoxLayout;
	l_rport->addWidget(r_portLabel);
	l_rport->addWidget(r_portBox);
	
	QHBoxLayout *l_rfolder = new QHBoxLayout;
	l_rfolder->addWidget(r_folderLabel);
	l_rfolder->addWidget(r_folderEdit);
	l_rfolder->addWidget(r_folderButton);
	
	QVBoxLayout *l_r = new QVBoxLayout;
	l_r->addWidget(r_Button);
	l_r->addLayout(l_rport);
	l_r->addLayout(l_rfolder);
	
	QHBoxLayout *l_buttons = new QHBoxLayout;
	l_buttons->addStretch(1);
	l_buttons->addWidget(startButton);
	l_buttons->addWidget(quitButton);
	
	
	QVBoxLayout *l_main = new QVBoxLayout(this);
	l_main->setMargin(11);
	l_main->setSpacing(6);
	l_main->addLayout(l_r);
	l_main->addLayout(l_s);
	l_main->addLayout(l_buttons);
	
	checkboxChanged(0);
}

void PrepareDialog::startClicked()
{
	if (s_Button->isChecked()){ /* send */
		if (s_ipEdit->text().isEmpty()){
			/* show message here */
			QMessageBox::information( this, caption(),
			"You should type the remote IP address.");
			return;
		}
		QString ip = s_ipEdit->text();
	
		int port = s_portBox->value();
	
		if (s_fileEdit->text().isEmpty()){
			/* show message here */
			QMessageBox::information( this, caption(),
			"You should select the file.");
			return;
		}
		QString file = s_fileEdit->text();
	
		startSend(ip, port, file);
	}
	else{ /* receive */
		int port = r_portBox->value();
	
		if (r_folderEdit->text().isEmpty()){
			/* show message here */
			QMessageBox::information( this, caption(),
			"You should select the folder.");
			return;
		}
		QString folder = r_folderEdit->text();
	
		startReceive(port, folder);
	}
	
}


void PrepareDialog::selectFile()
{
	QFileDialog* fd;
	if (s_Button->isChecked()){ /* select file */
		fd = new QFileDialog( this, "file select dialog", TRUE );
		fd->setMode( QFileDialog::ExistingFile );
		fd->setFilter( "Any file (*)" );
		if ( fd->exec() == QDialog::Accepted ){
			s_fileEdit->setText(fd->selectedFile());
		}
	}
	else{ /* select directory */
		fd = new QFileDialog( this, "directory open dialog", TRUE );
		fd->setMode( QFileDialog::Directory );
		if ( fd->exec() == QDialog::Accepted ){
			r_folderEdit->setText(fd->selectedFile());
		}
	}
}


void PrepareDialog::checkboxChanged(int state)
{
	if (s_Button->isChecked()){ /* send */
		s_ipEdit->setEnabled(true);
		s_portBox->setEnabled(true);
		s_fileButton->setEnabled(true);

		r_portBox->setEnabled(false);
		r_folderButton->setEnabled(false);
	}
	else{ /* receive */
		s_ipEdit->setEnabled(false);
		s_portBox->setEnabled(false);
		s_fileButton->setEnabled(false);

		r_portBox->setEnabled(true);
		r_folderButton->setEnabled(true);
	}
}


void PrepareDialog::startReceive(int port, const QString &folder)
{
	FTransferDialog *d = new FTransferDialog(0, NULL, false, Qt::WDestructiveClose ,
			QString(""),port,folder, S_RECEIVE);
	d->show();
}

void PrepareDialog::startSend(const QString &ip, int port, const QString &file)
{
	FTransferDialog *d = new FTransferDialog(0, NULL, false, Qt::WDestructiveClose,
			ip,port,file, S_SEND);
	d->show();
}
Соседние файлы в папке net_copy