Skip to content

Commit 9204ed1

Browse files
author
stoeckerb
committed
removed support for Qt4, added signal fileStored for callbacks after upload, more refactorings
1 parent 794a686 commit 9204ed1

12 files changed

+46
-90
lines changed

QFtpServer/debuglogdialog.cpp

+1-33
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,10 @@
33

44
static DebugLogDialog *theDialog = 0;
55

6-
// The message handler's signature has changed in Qt5.
7-
#if QT_VERSION >= 0x050000
86
static void myMessageOutput(QtMsgType /*type*/, const QMessageLogContext &/*context*/, const QString &msg)
97
{
108
theDialog->appendText(msg);
119
}
12-
#else
13-
static void myMessageOutput(QtMsgType /*type*/, const char *msg)
14-
{
15-
theDialog->appendText(msg);
16-
}
17-
#endif
1810

1911
DebugLogDialog::DebugLogDialog(QWidget *parent) :
2012
QDialog(parent),
@@ -23,22 +15,12 @@ DebugLogDialog::DebugLogDialog(QWidget *parent) :
2315
ui->setupUi(this);
2416
theDialog = this;
2517

26-
// The message handler's signature has changed in Qt5.
27-
#if QT_VERSION >= 0x050000
2818
qInstallMessageHandler(myMessageOutput);
29-
#else
30-
qInstallMsgHandler(myMessageOutput);
31-
#endif
3219
}
3320

3421
DebugLogDialog::~DebugLogDialog()
3522
{
36-
// The message handler's signature has changed in Qt5.
37-
#if QT_VERSION >= 0x050000
38-
qInstallMessageHandler(0);
39-
#else
40-
qInstallMsgHandler(0);
41-
#endif
23+
qInstallMessageHandler(nullptr);
4224
delete ui;
4325
}
4426

@@ -57,7 +39,6 @@ void DebugLogDialog::setOrientation(ScreenOrientation orientation)
5739

5840
Qt::WidgetAttribute attribute;
5941
switch (orientation) {
60-
#if QT_VERSION < 0x040702 || QT_VERSION >= 0x050000
6142
// Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
6243
// Qt 5 has removed them.
6344
case ScreenOrientationLockPortrait:
@@ -66,22 +47,9 @@ void DebugLogDialog::setOrientation(ScreenOrientation orientation)
6647
case ScreenOrientationLockLandscape:
6748
attribute = static_cast<Qt::WidgetAttribute>(129);
6849
break;
69-
default:
7050
case ScreenOrientationAuto:
7151
attribute = static_cast<Qt::WidgetAttribute>(130);
7252
break;
73-
#else // QT_VERSION < 0x040702
74-
case ScreenOrientationLockPortrait:
75-
attribute = Qt::WA_LockPortraitOrientation;
76-
break;
77-
case ScreenOrientationLockLandscape:
78-
attribute = Qt::WA_LockLandscapeOrientation;
79-
break;
80-
default:
81-
case ScreenOrientationAuto:
82-
attribute = Qt::WA_AutoOrientation;
83-
break;
84-
#endif // QT_VERSION < 0x040702
8553
};
8654
setAttribute(attribute, true);
8755
}

QFtpServer/mainwindow.cpp

+2-18
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ MainWindow::MainWindow(QWidget *parent)
4040
setWindowIcon(QIcon(":/icons/appicon"));
4141

4242
loadSettings();
43-
server = 0;
43+
server = nullptr;
4444
startServer();
4545
}
4646

@@ -65,31 +65,15 @@ void MainWindow::setOrientation(ScreenOrientation orientation)
6565

6666
Qt::WidgetAttribute attribute;
6767
switch (orientation) {
68-
#if QT_VERSION < 0x040702 || QT_VERSION >= 0x050000
69-
// Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
70-
// Qt 5 has removed them.
7168
case ScreenOrientationLockPortrait:
7269
attribute = static_cast<Qt::WidgetAttribute>(128);
7370
break;
7471
case ScreenOrientationLockLandscape:
7572
attribute = static_cast<Qt::WidgetAttribute>(129);
7673
break;
77-
default:
7874
case ScreenOrientationAuto:
7975
attribute = static_cast<Qt::WidgetAttribute>(130);
8076
break;
81-
#else // QT_VERSION < 0x040702
82-
case ScreenOrientationLockPortrait:
83-
attribute = Qt::WA_LockPortraitOrientation;
84-
break;
85-
case ScreenOrientationLockLandscape:
86-
attribute = Qt::WA_LockLandscapeOrientation;
87-
break;
88-
default:
89-
case ScreenOrientationAuto:
90-
attribute = Qt::WA_AutoOrientation;
91-
break;
92-
#endif // QT_VERSION < 0x040702
9377
};
9478
setAttribute(attribute, true);
9579
}
@@ -150,7 +134,7 @@ void MainWindow::startServer()
150134
usersConfigsMappings.insert(userName, FtpConfig(ui->lineEditRootPath->text(), password));
151135
delete server;
152136
server = new FtpServer(this,usersConfigsMappings , ui->lineEditPort->text().toInt(), ui->checkBoxReadOnly->isChecked(), ui->checkBoxOnlyOneIpAllowed->isChecked());
153-
connect(server, SIGNAL(newPeerIp(QString)), SLOT(onPeerIpChanged(QString)));
137+
connect(server, &FtpServer::newPeerIp, this, &MainWindow::onPeerIpChanged);
154138
if (server->isListening()) {
155139
ui->statusBar->showMessage("Listening at " + FtpServer::lanIp());
156140
} else {

QFtpServerLib/ftpcommand.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ void FtpCommand::start(QSslSocket *socket)
1313
started = true;
1414
this->socket = socket;
1515
socket->setParent(this);
16-
connect(socket, SIGNAL(disconnected()), this, SLOT(deleteLater()));
16+
connect(socket, &QAbstractSocket::disconnected, this, &QObject::deleteLater);
1717
startImplementation();
1818
}

QFtpServerLib/ftpcommand.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class FtpCommand : public QObject
1111
{
1212
Q_OBJECT
1313
public:
14-
explicit FtpCommand(QObject *parent = 0);
14+
explicit FtpCommand(QObject *parent = nullptr);
1515

1616
signals:
1717
void reply(const QString &details);

QFtpServerLib/ftpcontrolconnection.cpp

+22-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "ftpstorcommand.h"
55
#include "sslserver.h"
66
#include "dataconnection.h"
7+
#include "ftpserver.h"
78

89
#include <QFileInfo>
910
#include <QDateTime>
@@ -24,8 +25,8 @@ FtpControlConnection::FtpControlConnection(QObject *parent, QSslSocket *socket,
2425
isLoggedIn = false;
2526
encryptDataConnection = false;
2627
socket->setParent(this);
27-
connect(socket, SIGNAL(readyRead()), this, SLOT(acceptNewData()));
28-
connect(socket, SIGNAL(disconnected()), this, SLOT(deleteLater()));
28+
connect(socket, &QIODevice::readyRead, this, &FtpControlConnection::acceptNewData);
29+
connect(socket, &QAbstractSocket::disconnected, this, &QObject::deleteLater);
2930
currentDirectory = "/";
3031
dataConnection = new DataConnection(certData, this);
3132
this->certData = certData;
@@ -36,6 +37,11 @@ FtpControlConnection::~FtpControlConnection()
3637
{
3738
}
3839

40+
void FtpControlConnection::onFileStored(const QString &filepath) const
41+
{
42+
qobject_cast<FtpServer*>(parent())->onFileStored(filepath);
43+
}
44+
3945
void FtpControlConnection::acceptNewData()
4046
{
4147
if (!socket->canReadLine()) {
@@ -240,7 +246,7 @@ void FtpControlConnection::processCommand(const QString &entireCommand)
240246

241247
void FtpControlConnection::startOrScheduleCommand(FtpCommand *ftpCommand)
242248
{
243-
connect(ftpCommand, SIGNAL(reply(QString)), this, SLOT(reply(QString)));
249+
connect(ftpCommand, &FtpCommand::reply, this, &FtpControlConnection::reply);
244250

245251
if (!dataConnection->setFtpCommand(ftpCommand)) {
246252
delete ftpCommand;
@@ -258,7 +264,7 @@ void FtpControlConnection::port(const QString &addressAndPort)
258264
QRegExp exp("\\s*(\\d+,\\d+,\\d+,\\d+),(\\d+),(\\d+)");
259265
exp.indexIn(addressAndPort);
260266
QString hostName = exp.cap(1).replace(',', '.');
261-
int port = exp.cap(2).toInt() * 256 + exp.cap(3).toInt();
267+
quint16 port = exp.cap(2).toUShort() * 256 + exp.cap(3).toUShort();
262268
dataConnection->scheduleConnectToHost(hostName, port, encryptDataConnection);
263269
reply("200 Command okay.");
264270
}
@@ -281,7 +287,15 @@ void FtpControlConnection::retr(const QString &fileName)
281287

282288
void FtpControlConnection::stor(const QString &fileName, bool appendMode)
283289
{
284-
startOrScheduleCommand(new FtpStorCommand(this, fileName, appendMode, seekTo()));
290+
auto storCommand = new FtpStorCommand(this, fileName, appendMode, seekTo());
291+
connect(storCommand,&FtpStorCommand::reply,[this, fileName](const QString &details){
292+
if(details.startsWith("226"))
293+
{
294+
qobject_cast<FtpServer*>(parent())->onFileStored(toLocalPath(fileName));
295+
}
296+
297+
});
298+
startOrScheduleCommand(storCommand);
285299
}
286300

287301
void FtpControlConnection::cwd(const QString &dir)
@@ -345,7 +359,7 @@ void FtpControlConnection::quit()
345359
// If we have a running download or upload, we will wait until it's
346360
// finished before closing the control connection.
347361
if (dataConnection->ftpCommand()) {
348-
connect(dataConnection->ftpCommand(), SIGNAL(destroyed()), this, SLOT(disconnectFromHost()));
362+
connect(dataConnection->ftpCommand(), &QObject::destroyed, this, &FtpControlConnection::disconnectFromHost);
349363
} else {
350364
disconnectFromHost();
351365
}
@@ -416,9 +430,10 @@ void FtpControlConnection::feat()
416430
"211-Features:\r\n"
417431
" UTF8\r\n"
418432
"211 End\r\n"
419-
);
433+
);
420434
}
421435

436+
422437
qint64 FtpControlConnection::seekTo()
423438
{
424439
qint64 seekTo = 0;

QFtpServerLib/ftpcontrolconnection.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <QObject>
88
#include <QPointer>
9-
9+
#include <functional>
1010
class QSslSocket;
1111
class FtpCommand;
1212
class DataConnection;
@@ -23,7 +23,7 @@ class FtpControlConnection : public QObject
2323
explicit FtpControlConnection(QObject *parent, QSslSocket *socket, const SslCertData & certData, const QHash<QString, FtpConfig> & usersConfigsMapping, bool readOnly = false);
2424
~FtpControlConnection();
2525

26-
signals:
26+
void onFileStored(const QString & filepath) const;
2727

2828
public slots:
2929
// Slot used by the data connection handlers to send messages to the FTP
@@ -94,6 +94,7 @@ private slots:
9494
void cdup();
9595
// Server reports which features it supports.
9696
void feat();
97+
9798
// If the previous command was REST, returns its value. The REST command
9899
// allows to the client to continue partially downloaded/uploaded files.
99100
qint64 seekTo();

QFtpServerLib/ftplistcommand.cpp

-12
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,6 @@ void FtpListCommand::startImplementation()
3232

3333
emit reply("150 File status okay; about to open data connection.");
3434

35-
// index = 0;
36-
/* list = new QFileInfoList;
37-
if (!info.isDir()) {
38-
*list = (QFileInfoList() << info);
39-
} else {
40-
*list = QDir(listDirectory).entryInfoList();
41-
}
42-
43-
// Start the timer.
44-
timer = new QTimer(this);
45-
connect(timer, SIGNAL(timeout()), this, SLOT(listNextBatch()));
46-
timer->start(0);*/
4735
auto list = !info.isDir() ? (QFileInfoList() << info) : QDir(listDirectory).entryInfoList();
4836
for(const auto & l : list)
4937
socket->write(fileListingString(l).toUtf8());

QFtpServerLib/ftpretrcommand.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ FtpRetrCommand::FtpRetrCommand(QObject *parent, const QString &fileName, qint64
77
{
88
this->fileName = fileName;
99
this->seekTo = seekTo;
10-
file = 0;
10+
file = nullptr;
1111
}
1212

1313
FtpRetrCommand::~FtpRetrCommand()
@@ -36,9 +36,9 @@ void FtpRetrCommand::startImplementation()
3636
// For encryted SSL sockets, we need to use the encryptedBytesWritten()
3737
// signal, see the QSslSocket documentation to for reasons why.
3838
if (socket->isEncrypted()) {
39-
connect(socket, SIGNAL(encryptedBytesWritten(qint64)), this, SLOT(refillSocketBuffer(qint64)));
39+
connect(socket, &QSslSocket::encryptedBytesWritten, this, &FtpRetrCommand::refillSocketBuffer);
4040
} else {
41-
connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(refillSocketBuffer(qint64)));
41+
connect(socket, &QIODevice::bytesWritten, this, &FtpRetrCommand::refillSocketBuffer);
4242
}
4343

4444
refillSocketBuffer(128*1024);

QFtpServerLib/ftpserver.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,8 @@ FtpServer::FtpServer(QObject *parent, const QHash<QString, FtpConfig> & usersCon
1414
// In Qt4, QHostAddress::Any listens for IPv4 connections only, but as of
1515
// Qt5, it now listens on all available interfaces, and
1616
// QHostAddress::AnyIPv4 needs to be used if we want only IPv4 connections.
17-
#if QT_VERSION >= 0x050000
1817
server->listen(QHostAddress::AnyIPv4, port);
19-
#else
20-
server->listen(QHostAddress::Any, port);
21-
#endif
22-
connect(server, SIGNAL(newConnection()), this, SLOT(startNewControlConnection()));
18+
connect(server, &QTcpServer::newConnection, this, &FtpServer::startNewControlConnection);
2319
this->usersConfigMapping = usersConfigMapping;
2420
this->readOnly = readOnly;
2521
this->onlyOneIpAllowed = onlyOneIpAllowed;
@@ -46,6 +42,11 @@ QString FtpServer::lanIp()
4642
return "";
4743
}
4844

45+
void FtpServer::onFileStored(const QString &filepath) const
46+
{
47+
emit fileStored(filepath);
48+
}
49+
4950
void FtpServer::startNewControlConnection()
5051
{
5152
QSslSocket *socket = static_cast<QSslSocket *>(server->nextPendingConnection());

QFtpServerLib/ftpserver.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ class QFTPSERVERLIBSHARED_EXPORT FtpServer : public QObject
3333

3434
// Get the LAN IP of the host, e.g. "192.168.1.10".
3535
static QString lanIp();
36-
36+
void onFileStored(const QString & filepath) const;
3737
signals:
3838
// A connection from a new IP has been established. This signal is emitted
3939
// when the FTP server is connected by a new IP. The new IP will then be
4040
// stored and will not cause this FTP server instance to emit this signal
4141
// any more.
4242
void newPeerIp(const QString &ip);
43+
void fileStored(const QString & filepath) const;
4344

4445
private slots:
4546
// Called by the SSL server when we have received a new connection.

QFtpServerLib/ftpstorcommand.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "ftpcontrolconnection.h"
12
#include "ftpstorcommand.h"
23
#include <QFile>
34
#include <QSslSocket>
@@ -7,7 +8,7 @@ FtpStorCommand::FtpStorCommand(QObject *parent, const QString &fileName, bool ap
78
{
89
this->fileName = fileName;
910
this->appendMode = appendMode;
10-
file = 0;
11+
file = nullptr;
1112
this->seekTo = seekTo;
1213
success = false;
1314
}
@@ -17,6 +18,7 @@ FtpStorCommand::~FtpStorCommand()
1718
if (started) {
1819
if (success) {
1920
emit reply("226 Closing data connection.");
21+
file->deleteLater();
2022
} else {
2123
emit reply("451 Requested action aborted: local error in processing.");
2224
}
@@ -35,13 +37,13 @@ void FtpStorCommand::startImplementation()
3537
if (seekTo) {
3638
file->seek(seekTo);
3739
}
38-
connect(socket, SIGNAL(readyRead()), this, SLOT(acceptNextBlock()));
40+
connect(socket, &QIODevice::readyRead, this, &FtpStorCommand::acceptNextBlock);
3941
}
4042

4143
void FtpStorCommand::acceptNextBlock()
4244
{
4345
const QByteArray &bytes = socket->readAll();
44-
int bytesWritten = file->write(bytes);
46+
qint64 bytesWritten = file->write(bytes);
4547
if (bytesWritten != bytes.size()) {
4648
emit reply("451 Requested action aborted. Could not write data to file.");
4749
deleteLater();

QFtpServerLib/sslserver.h

-4
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,7 @@ class SslServer : public QTcpServer
3232
// QTcpServer::incomingConnection(qintptr handle) this 2nd one is sneaky as it
3333
// compiles properly but no connections appear to arrive since the compiler
3434
// doesn’t consider int and qintptr the same.
35-
#if QT_VERSION >= 0x050000
3635
typedef qintptr PortableSocketDescriptorType;
37-
#else
38-
typedef int PortableSocketDescriptorType;
39-
#endif
4036

4137
void incomingConnection(PortableSocketDescriptorType socketDescriptor);
4238
};

0 commit comments

Comments
 (0)