Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions QFtpServer/QFtpServer.pro
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions QFtpServer/ftpthread.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
36 changes: 36 additions & 0 deletions QFtpServer/ftpthread.h
Original file line number Diff line number Diff line change
@@ -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 <QThread>

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
42 changes: 33 additions & 9 deletions QFtpServer/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
#include <QCoreApplication>
#include <QSettings>
#include <QFileDialog>
#include <QIntValidator>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
: QMainWindow(parent),
ui(new Ui::MainWindow),
server(nullptr)
{
ui->setupUi(this);

Expand Down Expand Up @@ -45,6 +48,10 @@ MainWindow::MainWindow(QWidget *parent)

MainWindow::~MainWindow()
{
if(server)
{
server->quit();
}
saveSettings();
delete ui;
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 5 additions & 2 deletions QFtpServer/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define MAINWINDOW_H

#include <QMainWindow>
#include "ftpthread.h"

namespace Ui {
class MainWindow;
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down