/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtNetwork module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
//#define QFTPPI_DEBUG
//#define QFTPDTP_DEBUG
#include "qftp.h"
#include "qabstractsocket.h"
#ifndef QT_NO_FTP
QT_BEGIN_NAMESPACE
QBasicAtomicInt QFtpCommand::idCounter = Q_BASIC_ATOMIC_INITIALIZER(1);
QFtpCommand::QFtpCommand(QFtp::Command cmd, QStringList raw, const QByteArray &ba)
: command(cmd), rawCmds(raw), is_ba(true)
{
id = idCounter.fetchAndAddRelaxed(1);
data.ba = new QByteArray(ba);
}
QFtpCommand::QFtpCommand(QFtp::Command cmd, QStringList raw, QIODevice *dev)
: command(cmd), rawCmds(raw), is_ba(false)
{
id = idCounter.fetchAndAddRelaxed(1);
data.dev = dev;
}
QFtpCommand::~QFtpCommand()
{
if (is_ba)
delete data.ba;
}
/**********************************************************************
*
* QFtpDTP implemenatation
*
*********************************************************************/
QFtpDTP::QFtpDTP(QFtpPI *p, QObject *parent) :
QObject(parent),
socket(0),
listener(this),
pi(p),
callWriteData(false)
{
clearData();
listener.setObjectName(QLatin1String("QFtpDTP active state server"));
connect(&listener, SIGNAL(newConnection()), SLOT(setupSocket()));
}
void QFtpDTP::setData(QByteArray *ba)
{
is_ba = true;
data.ba = ba;
}
void QFtpDTP::setDevice(QIODevice *dev)
{
is_ba = false;
data.dev = dev;
}
void QFtpDTP::setBytesTotal(qint64 bytes)
{
bytesTotal = bytes;
bytesDone = 0;
emit dataTransferProgress(bytesDone, bytesTotal);
}
void QFtpDTP::connectToHost(const QString & host, quint16 port)
{
bytesFromSocket.clear();
if (socket) {
delete socket;
socket = 0;
}
socket = new QTcpSocket(this);
#ifndef QT_NO_BEARERMANAGEMENT
//copy network session down to the socket
socket->setProperty("_q_networksession", property("_q_networksession"));
#endif
socket->setObjectName(QLatin1String("QFtpDTP Passive state socket"));
connect(socket, SIGNAL(connected()), SLOT(socketConnected()));
connect(socket, SIGNAL(readyRead()), SLOT(socketReadyRead()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)), SLOT(socketError(QAbstractSocket::SocketError)));
connect(socket, SIGNAL(disconnected()), SLOT(socketConnectionClosed()));
connect(socket, SIGNAL(bytesWritten(qint64)), SLOT(socketBytesWritten(qint64)));
socket->connectToHost(host, port);
}
int QFtpDTP::setupListener(const QHostAddress &address)
{
#ifndef QT_NO_BEARERMANAGEMENT
//copy network session down to the socket
listener.setProperty("_q_networksession", property("_q_networksession"));
#endif
if (!listener.isListening() && !listener.listen(address, 0))
return -1;
return listener.serverPort();
}
void QFtpDTP::waitForConnection()
{
// This function is only interesting in Active transfer mode; it works
// around a limitation in QFtp's design by blocking, waiting for an
// incoming connection. For the default Passive mode, it does nothing.
if (listener.isListening())
listener.waitForNewConnection();
}
QTcpSocket::SocketState QFtpDTP::state() const
{
return socket ? socket->state() : QTcpSocket::UnconnectedState;
}
qint64 QFtpDTP::bytesAvailable() const
{
if (!socket || socket->state() != QTcpSocket::ConnectedState)
return (qint64) bytesFromSocket.size();
return socket->bytesAvailable();
}
qint64 QFtpDTP::read(char *data, qint64 maxlen)
{
qint64 read;
if (socket && socket->state() == QTcpSocket::ConnectedState) {
read = socket->read(data, maxlen);
} else {
read = qMin(maxlen, qint64(bytesFromSocket.size()));
memcpy(data, bytesFromSocket.data(), read);
bytesFromSocket.remove(0, read);
}
bytesDone += read;
return read;
}
QByteArray QFtpDTP::readAll()
{
QByteArray tmp;
if (socket && socket->state() == QTcpSocket::ConnectedState) {
tmp = socket->readAll();
bytesDone += tmp.size();
} else {
tmp = bytesFromSocket;
bytesFromSocket.clear();
}
return tmp;
}
void QFtpDTP::writeData()
{
if (!socket)
return;
if (is_ba) {
#if defined(QFTPDTP_DEBUG)
qDebug("QFtpDTP::writeData: write %d bytes", data.ba->size());
#endif
if (data.ba->size() == 0)
emit dataTransferProgress(0, bytesTotal);
else
socket->write(data.ba->data(), data.ba->size());
socket->close();
clearData();
} else if (data.dev) {
callWriteData = false;
const qint64 blockSize = 16*1024;
char buf[16*1024];
qint64 read = data.dev->read(buf, blockSize);
#if defined(QFTPDTP_DEBUG)
qDebug("QFtpDTP::writeData: write() of size %lli bytes", read);
#endif
if (read > 0) {
socket->write(buf, read);
} else if (read == -1 || (!data.dev->isSequential() && data.dev->atEnd())) {
// error or EOF
if (bytesDone == 0 && socket->bytesToWrite() == 0)
emit dataTransferProgress(0, bytesTotal);
socket->close();
clearData();
}
// do we continue uploading?
callWriteData = data.dev != 0;
}
}
void QFtpDTP::dataReadyRead()
{
writeData();
}
inline bool QFtpDTP::hasError() const
{
return !err.isNull();
}
inline QString QFtpDTP::errorMessage() const
{
return err;
}
inline void QFtpDTP::clearError()
{
err.clear();
}
void QFtpDTP::abortConnection()
{
#if defined(QFTPDTP_DEBUG)
qDebug("QFtpDTP::abortConnection, bytesAvailable == %lli",
socket ? socket->bytesAvailable() : (qint64) 0);
#endif
callWriteData = false;
clearData();
if (socket)
socket->abort();
}
static void _q_fixupDateTime(QDateTime *dateTime)
{
// Adjust for future tolerance.
const int futureTolerance = 86400;
if (dateTime->secsTo(QDateTime::currentDateTime()) < -futureTolerance) {
QDate d = dateTime->date();
d.setDate(d.year() - 1, d.month(), d.day());
dateTime->setDate(d);
}
}
static void
QFtp 编译报错 使用了未定义类型“QFtpPrivate”的方法
10 浏览量
2024-05-10
11:16:45
上传
评论
收藏 24KB ZIP 举报
zgjxhgh
- 粉丝: 5
- 资源: 4
最新资源
- SVID_20240523_141155_1.mp4
- Code for the complete guide to tkinter tutorial
- 关于百货中心供应链管理系统.zip
- SimpleFolderIcon-master 修改Unity的Project下的文件夹图标
- A python Tkinter widget to display tile based maps
- A pure Python library for adding tables to a Tkinter application
- Vector资源文件.zip
- MobaXterm-Installer
- MicroMsg.xlsx
- 88-520告白(520气球).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈