diff --git a/QFtpServer/QFtpServer.pro b/QFtpServer/QFtpServer.pro index ef8d8b3..f62b286 100644 --- a/QFtpServer/QFtpServer.pro +++ b/QFtpServer/QFtpServer.pro @@ -22,10 +22,13 @@ symbian:TARGET.CAPABILITY += NetworkServices # CONFIG += mobility # MOBILITY += -SOURCES += main.cpp mainwindow.cpp \ - debuglogdialog.cpp +SOURCES += main.cpp \ + mainwindow.cpp \ + debuglogdialog.cpp \ + ftpthread.cpp HEADERS += mainwindow.h \ - debuglogdialog.h + ftpthread.h \ + debuglogdialog.h FORMS += mainwindow.ui \ debuglogdialog.ui QT += network diff --git a/QFtpServer/ftpthread.cpp b/QFtpServer/ftpthread.cpp new file mode 100644 index 0000000..892268a --- /dev/null +++ b/QFtpServer/ftpthread.cpp @@ -0,0 +1,38 @@ +// Author: Kang Lin (kl222@126.com) + +#include "ftpthread.h" +#include "ftpserver.h" + +CFtpThread::CFtpThread(QString szPath, + int nPort, + QString szUser, + QString szPassword, + bool bReadOnly, + bool bOnlyOneIpAllowed, + QObject *parent) + : QThread(parent), + m_szPath(szPath), + m_nPort(nPort), + m_szUser(szUser), + m_szPassword(szPassword), + m_bReadOnly(bReadOnly), + m_bOnlyOneIpAllowed(bOnlyOneIpAllowed) +{ +} + +void CFtpThread::run() +{ + FtpServer server(nullptr, m_szPath, m_nPort, m_szUser, + m_szPassword, m_bReadOnly, m_bOnlyOneIpAllowed); + bool check = connect(&server, SIGNAL(newPeerIp(const QString&)), + this, SIGNAL(sigNewPeerIp(const QString&))); + Q_ASSERT(check); + + if (server.isListening()) { + emit sigMessage("Listening at " + FtpServer::lanIp()); + } else { + emit sigMessage("Not listening"); + } + + exec(); +} diff --git a/QFtpServer/ftpthread.h b/QFtpServer/ftpthread.h new file mode 100644 index 0000000..a91a55d --- /dev/null +++ b/QFtpServer/ftpthread.h @@ -0,0 +1,36 @@ +// Author: Kang Lin (kl222@126.com) + +#ifndef CBACKGROUNDTHREAD_H_KL_2021_4_4 +#define CBACKGROUNDTHREAD_H_KL_2021_4_4 + +#include + +class CFtpThread : public QThread +{ + Q_OBJECT + +public: + explicit CFtpThread(QString szPath, + int nPort, + QString szUser = QString(), + QString szPassword = QString(), + bool bReadOnly = false, + bool bOnlyOneIpAllowed = false, + QObject *parent = nullptr); +signals: + void sigMessage(const QString &msg); + void sigNewPeerIp(const QString &ip); + +protected: + virtual void run() override; + +private: + QString m_szPath; + int m_nPort; + QString m_szUser; + QString m_szPassword; + bool m_bReadOnly; + bool m_bOnlyOneIpAllowed; +}; + +#endif // CBACKGROUNDTHREAD_H_KL_2021_4_4 diff --git a/QFtpServer/mainwindow.cpp b/QFtpServer/mainwindow.cpp index ab544a8..b888646 100644 --- a/QFtpServer/mainwindow.cpp +++ b/QFtpServer/mainwindow.cpp @@ -6,9 +6,12 @@ #include #include #include +#include MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent), ui(new Ui::MainWindow) + : QMainWindow(parent), + ui(new Ui::MainWindow), + server(nullptr) { ui->setupUi(this); @@ -45,6 +48,10 @@ MainWindow::MainWindow(QWidget *parent) MainWindow::~MainWindow() { + if(server) + { + server->quit(); + } saveSettings(); delete ui; } @@ -145,15 +152,27 @@ void MainWindow::startServer() userName = ui->lineEditUserName->text(); password = ui->lineEditPassword->text(); } - delete server; - server = new FtpServer(this, ui->lineEditRootPath->text(), ui->lineEditPort->text().toInt(), userName, - password, ui->checkBoxReadOnly->isChecked(), ui->checkBoxOnlyOneIpAllowed->isChecked()); - connect(server, SIGNAL(newPeerIp(QString)), SLOT(onPeerIpChanged(QString))); - if (server->isListening()) { - ui->statusBar->showMessage("Listening at " + FtpServer::lanIp()); - } else { - ui->statusBar->showMessage("Not listening"); + if(server) + { + server->quit(); } + //delete server; + server = new CFtpThread(ui->lineEditRootPath->text(), + ui->lineEditPort->text().toInt(), + userName, + password, + ui->checkBoxReadOnly->isChecked(), + ui->checkBoxOnlyOneIpAllowed->isChecked()); + bool check = connect(server, SIGNAL(sigNewPeerIp(const QString&)), + this, SLOT(onPeerIpChanged(const QString&))); + Q_ASSERT(check); + check = connect(server, SIGNAL(sigMessage(const QString&)), + this, SLOT(onMessage(const QString&))); + Q_ASSERT(check); + check = connect(server, SIGNAL(finished()), + server, SLOT(deleteLater())); + Q_ASSERT(check); + server->start(); } void MainWindow::on_pushButtonRestartServer_clicked() @@ -191,6 +210,11 @@ void MainWindow::onPeerIpChanged(const QString &peerIp) ui->statusBar->showMessage("Connected to " + peerIp); } +void MainWindow::onMessage(const QString& msg) +{ + ui->statusBar->showMessage(msg); +} + void MainWindow::on_pushButtonShowDebugLog_clicked() { DebugLogDialog *dlg = new DebugLogDialog; diff --git a/QFtpServer/mainwindow.h b/QFtpServer/mainwindow.h index e2d56fc..32fb706 100644 --- a/QFtpServer/mainwindow.h +++ b/QFtpServer/mainwindow.h @@ -2,6 +2,7 @@ #define MAINWINDOW_H #include +#include "ftpthread.h" namespace Ui { class MainWindow; @@ -33,7 +34,9 @@ private slots: void on_toolButtonBrowse_clicked(); void onPeerIpChanged(const QString &peerIp); - + + void onMessage(const QString &msg); + void on_pushButtonShowDebugLog_clicked(); void on_pushButtonExit_clicked(); @@ -42,7 +45,7 @@ private slots: Ui::MainWindow *ui; // This is the FTP server object. - FtpServer *server; + CFtpThread *server; // Load the stored settings. void loadSettings();