4#include <QCoreApplication>
5#include <QLoggingCategory>
6#include "BackendFileTransfer.h"
8static Q_LOGGING_CATEGORY(log,
"FileTransfer.Backend")
10class CFileTransferEvent : public QEvent
23 CFileTransferEvent(Command cmd,
24 const QString& szSrcPath = QString(),
25 const QString& szDesPath = QString());
28 QString m_szSourcePath;
29 QString m_szDestination;
31 QSharedPointer<CFileTransfer> m_FileTransfer;
34CFileTransferEvent::CFileTransferEvent(Command cmd,
35 const QString &szSrcPath,
36 const QString &szDesPath)
37 : QEvent(QEvent::Type::User)
39 , m_szSourcePath(szSrcPath)
40 , m_szDestination(szDesPath)
46 , m_pOperate(pOperate)
52 qDebug(log) << Q_FUNC_INFO;
54 m_pPara = m_pOperate->GetParameter();
58CBackendFileTransfer::~CBackendFileTransfer()
60 qDebug(log) << Q_FUNC_INFO;
68bool CBackendFileTransfer::event(QEvent *event)
70 if(event->type() == QEvent::Type::User) {
71 CFileTransferEvent* pEvent = (CFileTransferEvent*)event;
72 switch(pEvent->m_Command) {
73 case CFileTransferEvent::Command::MakeDir:
76 m_pSFTP->MakeDir(pEvent->m_szSourcePath);
79 case CFileTransferEvent::Command::RemoveDir:
82 m_pSFTP->RemoveDir(pEvent->m_szSourcePath);
85 case CFileTransferEvent::Command::RemoveFile:
88 m_pSFTP->RemoveFile(pEvent->m_szSourcePath);
91 case CFileTransferEvent::Command::Rename:
94 m_pSFTP->Rename(pEvent->m_szSourcePath, pEvent->m_szDestination);
97 case CFileTransferEvent::Command::GetDir:
101 m_pSFTP->slotGetDir(pEvent->m_szSourcePath, pEvent->m_pRemoteFileSystem);
105 case CFileTransferEvent::Command::StartFileTransfer:
109 m_pSFTP->slotStartFileTransfer(pEvent->m_FileTransfer);
113 case CFileTransferEvent::Command::StopFileTransfer:
117 m_pSFTP->slotStopFileTransfer(pEvent->m_FileTransfer);
125 return CBackend::event(event);
130 OnInitReturnValue nRet = OnInitReturnValue::Fail;
131 if(m_pPara->GetProtocol() == CParameterFileTransfer::Protocol::SFTP)
142 m_pSFTP->deleteLater();
154 nRet = m_pSFTP->Process();
167 Qt::DirectConnection);
169 check = connect(
this, SIGNAL(sigGetDir(
CRemoteFileSystem*, QVector<QSharedPointer<CRemoteFileSystem> >,
bool)),
170 pForm, SIGNAL(sigGetDir(
CRemoteFileSystem*, QVector<QSharedPointer<CRemoteFileSystem> >,
bool)));
172 check = connect(pForm, SIGNAL(sigMakeDir(
const QString&)),
173 this, SLOT(slotMakeDir(
const QString&)));
175 check = connect(pForm, SIGNAL(sigRemoveDir(
const QString&)),
176 this, SLOT(slotRemoveDir(
const QString&)));
178 check = connect(pForm, SIGNAL(sigRemoveFile(
const QString&)),
179 this, SLOT(slotRemoveFile(
const QString&)));
181 check = connect(pForm, SIGNAL(sigRename(
const QString&,
const QString&)),
182 this, SLOT(slotRename(
const QString&,
const QString&)));
184 check = connect(pForm, SIGNAL(sigStartFileTransfer(QSharedPointer<CFileTransfer>)),
185 this, SLOT(slotStartFileTransfer(QSharedPointer<CFileTransfer>)));
187 check = connect(pForm, SIGNAL(sigStopFileTransfer(QSharedPointer<CFileTransfer>)),
188 this, SLOT(slotStopFileTransfer(QSharedPointer<CFileTransfer>)));
190 check = connect(
this, SIGNAL(sigFileTransferUpdate(QSharedPointer<CFileTransfer>)),
191 pForm, SLOT(slotFileTransferUpdate(QSharedPointer<CFileTransfer>)));
196CBackendFileTransfer::OnInitReturnValue CBackendFileTransfer::InitSFTP()
198 CParameterSSH* ssh = &m_pOperate->GetParameter()->m_SSH;
200 m_pSFTP =
new CChannelSFTP(
this, ssh);
202 return OnInitReturnValue::Fail;
204 bool bRet = m_pSFTP->open(QIODevice::ReadWrite);
207 szErr = tr(
"Open SFTP fail.") + m_pSFTP->errorString();
208 qCritical(log) << szErr;
210 return OnInitReturnValue::Fail;
214 return OnInitReturnValue::UseOnProcess;
217void CBackendFileTransfer::slotMakeDir(
const QString &szDir)
219 if(szDir.isEmpty())
return;
220 CFileTransferEvent* pEvent =
new CFileTransferEvent(
221 CFileTransferEvent::Command::MakeDir, szDir);
222 QCoreApplication::postEvent(
this, pEvent);
226void CBackendFileTransfer::slotRemoveDir(
const QString &szDir)
228 if(szDir.isEmpty())
return;
229 CFileTransferEvent* pEvent =
new CFileTransferEvent(
230 CFileTransferEvent::Command::RemoveDir, szDir);
231 QCoreApplication::postEvent(
this, pEvent);
235void CBackendFileTransfer::slotRemoveFile(
const QString &szFile)
237 if(szFile.isEmpty())
return;
238 CFileTransferEvent* pEvent =
new CFileTransferEvent(
239 CFileTransferEvent::Command::RemoveFile, szFile);
240 QCoreApplication::postEvent(
this, pEvent);
244void CBackendFileTransfer::slotRename(
const QString &oldName,
const QString &newName)
246 if(oldName.isEmpty() && newName.isEmpty())
return;
247 CFileTransferEvent* pEvent =
new CFileTransferEvent(
248 CFileTransferEvent::Command::Rename, oldName, newName);
249 QCoreApplication::postEvent(
this, pEvent);
255 if(!pRemoteFileSystem || pRemoteFileSystem->GetPath().isEmpty())
257 QString szPath = pRemoteFileSystem->GetPath();
258 CFileTransferEvent* pEvent =
new CFileTransferEvent(
259 CFileTransferEvent::Command::GetDir, szPath);
260 pEvent->m_pRemoteFileSystem = (CRemoteFileSystem*) pRemoteFileSystem;
261 QCoreApplication::postEvent(
this, pEvent);
265void CBackendFileTransfer::slotStartFileTransfer(QSharedPointer<CFileTransfer> f)
268 CFileTransferEvent* pEvent =
new CFileTransferEvent(
269 CFileTransferEvent::Command::StartFileTransfer);
270 pEvent->m_FileTransfer = f;
271 QCoreApplication::postEvent(
this, pEvent);
275void CBackendFileTransfer::slotStopFileTransfer(QSharedPointer<CFileTransfer> f)
278 CFileTransferEvent* pEvent =
new CFileTransferEvent(
279 CFileTransferEvent::Command::StopFileTransfer);
280 pEvent->m_FileTransfer = f;
281 QCoreApplication::postEvent(
this, pEvent);
virtual OnInitReturnValue OnInit() override
初始化
virtual int WakeUp() override
唤醒后台线程。
virtual int OnClean() override
清理
virtual int OnProcess() override
具体操作处理
后端接口。它由协议插件实现。 它默认启动一个定时器来开启一个非 Qt 事件循环(就是普通的循环处理)。 详见: Start()、 slotTimeOut()、 OnProcess() 。 当然,它仍然支...
void sigShowMessageBox(const QString &szTitle, const QString &szMessage, const QMessageBox::Icon &icon=QMessageBox::Information)
从后台线程中触发在主线程中显示消息对话框(QMessageBox),不阻塞后台线程
void sigRunning()
当插件开始成功后触发。仅由插件触发
File transfer operate interface
virtual QWidget * GetViewer() override
得到显示视图