⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sendfiledlgimpl.cpp

📁 张勇的linQ学习P2P及IM软件的极佳素材代码
💻 CPP
字号:
/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   copyright            : (C) 2003 by Zhang Yong                         *
 *   email                : z-yong163@163.com                              *
 ***************************************************************************/

#include "sendfiledlgimpl.h"
#include <qlabel.h>
#include <qprogressbar.h>
#include <qfiledialog.h>
#include <qmessagebox.h>


#define WSTYLES		\
	(WStyle_Customize | WStyle_DialogBorder | WStyle_Title | WStyle_SysMenu | WStyle_Minimize)


SendFileDlg::SendFileDlg(TCPSessionBase *tcp)
	: SendFileDlgBase(0, 0, FALSE, WDestructiveClose | WSTYLES), FileSession(tcp)
{
	bytesSent = lastBytes = 0;

	accountLabel->setText(tcpSession->getContactName());
	statusLabel->setText(tr("Waiting for reply..."));
}

void SendFileDlg::show()
{
	SendFileDlgBase::show();

	if (!tcpSession->isSender())
		return;

	QString pathName = QFileDialog::getOpenFileName(QString::null, QString::null, this);
	if (pathName.isEmpty())
		return;

	QFileInfo info(pathName);
	setCaption(tr("Send File") + " -- " + info.fileName());
	statusLabel->setText(tr("Waiting for reply..."));
	sizeLabel->setNum((int) info.size());
	progressBar->setTotalSteps(info.size());

	sendFileInfo(pathName.local8Bit(), info.fileName().utf8());
}

bool SendFileDlg::promptQuit()
{
	int r = QMessageBox::warning(this, tr("Warning"),
		tr("Are you sure to terminate this session?"),
		QMessageBox::Yes, QMessageBox::No);
	return (r == QMessageBox::Yes);
}

void SendFileDlg::closeEvent(QCloseEvent *e)
{
	if (promptQuit())
		SendFileDlgBase::closeEvent(e);
}

void SendFileDlg::slotCancel()
{
	if (promptQuit())
		reject();
}

const char *SendFileDlg::getPathName(const char *name, uint32 size)
{
	QString fileName = QString::fromUtf8(name);

	QString pathName = QFileDialog::getSaveFileName(
		fileName, QString::null, this);
	if (pathName.isEmpty())
		return NULL;

	setCaption(tr("Receive File") + " -- " + fileName);
	sizeLabel->setNum((int) size);
	progressBar->setTotalSteps(size);

	return strdup(pathName.local8Bit());
}

void SendFileDlg::onFileProgress(int n)
{
	if (bytesSent == 0)
		spanTime.start();

	bytesSent += n;
	progressBar->setProgress(bytesSent);

	QString text;
	if (tcpSession->isSender())
		text = tr("%1 bytes sent").arg(bytesSent);
	else
		text = tr("%1 bytes received").arg(bytesSent);

	statusLabel->setText(text);

	if (spanTime.elapsed() >= 1000) {
		double speed = (bytesSent - lastBytes) / 1.024 / spanTime.elapsed();
		spanTime.restart();
		lastBytes = bytesSent;

		speedLabel->setText(QString("%1KB/S").arg(speed, 0, 'f', 1));
	}
}

void SendFileDlg::onFileComplete()
{
	if (bytesSent > 0 && bytesSent == fileSize) {
		QMessageBox::information(this, tr("Success"),
			tr("File has been successfully transferred."));
	} else {
		QMessageBox::critical(this, tr("Failed"),
			tr("An error occurred during the file transffer."
				" The remote may have canceled this session."));
	}
	reject();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -