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;
63bool CBackendFileTransfer::event(QEvent *event)
65 if(event->type() == QEvent::Type::User) {
66 CFileTransferEvent* pEvent = (CFileTransferEvent*)event;
67 switch(pEvent->m_Command) {
68 case CFileTransferEvent::Command::MakeDir:
71 m_pSFTP->MakeDir(pEvent->m_szSourcePath);
74 case CFileTransferEvent::Command::RemoveDir:
77 m_pSFTP->RemoveDir(pEvent->m_szSourcePath);
80 case CFileTransferEvent::Command::RemoveFile:
83 m_pSFTP->RemoveFile(pEvent->m_szSourcePath);
86 case CFileTransferEvent::Command::Rename:
89 m_pSFTP->Rename(pEvent->m_szSourcePath, pEvent->m_szDestination);
92 case CFileTransferEvent::Command::GetDir:
95 m_pSFTP->slotGetDir(pEvent->m_szSourcePath, pEvent->m_pRemoteFileSystem);
98 case CFileTransferEvent::Command::StartFileTransfer:
102 m_pSFTP->slotStartFileTransfer(pEvent->m_FileTransfer);
106 case CFileTransferEvent::Command::StopFileTransfer:
110 m_pSFTP->slotStopFileTransfer(pEvent->m_FileTransfer);
118 return CBackend::event(event);
123 OnInitReturnValue nRet = OnInitReturnValue::Fail;
124 if(m_pPara->GetProtocol() == CParameterFileTransfer::Protocol::SFTP)
135 m_pSFTP->deleteLater();
147 nRet = m_pSFTP->Process();
160 Qt::DirectConnection);
162 check = connect(
this, SIGNAL(sigGetDir(
CRemoteFileSystem*, QVector<QSharedPointer<CRemoteFileSystem> >,
bool)),
163 pForm, SIGNAL(sigGetDir(
CRemoteFileSystem*, QVector<QSharedPointer<CRemoteFileSystem> >,
bool)));
165 check = connect(pForm, SIGNAL(sigMakeDir(
const QString&)),
166 this, SLOT(slotMakeDir(
const QString&)));
168 check = connect(pForm, SIGNAL(sigRemoveDir(
const QString&)),
169 this, SLOT(slotRemoveDir(
const QString&)));
171 check = connect(pForm, SIGNAL(sigRemoveFile(
const QString&)),
172 this, SLOT(slotRemoveFile(
const QString&)));
174 check = connect(pForm, SIGNAL(sigRename(
const QString&,
const QString&)),
175 this, SLOT(slotRename(
const QString&,
const QString&)));
177 check = connect(pForm, SIGNAL(sigStartFileTransfer(QSharedPointer<CFileTransfer>)),
178 this, SLOT(slotStartFileTransfer(QSharedPointer<CFileTransfer>)));
180 check = connect(pForm, SIGNAL(sigStopFileTransfer(QSharedPointer<CFileTransfer>)),
181 this, SLOT(slotStopFileTransfer(QSharedPointer<CFileTransfer>)));
183 check = connect(
this, SIGNAL(sigFileTransferUpdate(QSharedPointer<CFileTransfer>)),
184 pForm, SLOT(slotFileTransferUpdate(QSharedPointer<CFileTransfer>)));
189CBackendFileTransfer::OnInitReturnValue CBackendFileTransfer::InitSFTP()
195 return OnInitReturnValue::Fail;
197 bool bRet = m_pSFTP->open(QIODevice::ReadWrite);
200 szErr = tr(
"Open SFTP fail.") + m_pSFTP->errorString();
201 qCritical(log) << szErr;
203 return OnInitReturnValue::Fail;
207 return OnInitReturnValue::UseOnProcess;
210void CBackendFileTransfer::slotMakeDir(
const QString &szDir)
212 if(szDir.isEmpty())
return;
213 CFileTransferEvent* pEvent =
new CFileTransferEvent(
214 CFileTransferEvent::Command::MakeDir, szDir);
215 QCoreApplication::postEvent(
this, pEvent);
219void CBackendFileTransfer::slotRemoveDir(
const QString &szDir)
221 if(szDir.isEmpty())
return;
222 CFileTransferEvent* pEvent =
new CFileTransferEvent(
223 CFileTransferEvent::Command::RemoveDir, szDir);
224 QCoreApplication::postEvent(
this, pEvent);
228void CBackendFileTransfer::slotRemoveFile(
const QString &szFile)
230 if(szFile.isEmpty())
return;
231 CFileTransferEvent* pEvent =
new CFileTransferEvent(
232 CFileTransferEvent::Command::RemoveFile, szFile);
233 QCoreApplication::postEvent(
this, pEvent);
237void CBackendFileTransfer::slotRename(
const QString &oldName,
const QString &newName)
239 if(oldName.isEmpty() && newName.isEmpty())
return;
240 CFileTransferEvent* pEvent =
new CFileTransferEvent(
241 CFileTransferEvent::Command::Rename, oldName, newName);
242 QCoreApplication::postEvent(
this, pEvent);
248 if(!p || p->GetPath().isEmpty())
250 QString szPath = p->GetPath();
251 CFileTransferEvent* pEvent =
new CFileTransferEvent(
252 CFileTransferEvent::Command::GetDir, szPath);
254 QCoreApplication::postEvent(
this, pEvent);
258void CBackendFileTransfer::slotStartFileTransfer(QSharedPointer<CFileTransfer> f)
261 CFileTransferEvent* pEvent =
new CFileTransferEvent(
262 CFileTransferEvent::Command::StartFileTransfer);
263 pEvent->m_FileTransfer = f;
264 QCoreApplication::postEvent(
this, pEvent);
268void CBackendFileTransfer::slotStopFileTransfer(QSharedPointer<CFileTransfer> f)
271 CFileTransferEvent* pEvent =
new CFileTransferEvent(
272 CFileTransferEvent::Command::StopFileTransfer);
273 pEvent->m_FileTransfer = f;
274 QCoreApplication::postEvent(
this, pEvent);
virtual OnInitReturnValue OnInit() override
初始化
virtual int OnClean() override
清理
virtual int OnProcess() override
具体操作处理
后端接口。它由协议插件实现。 它默认启动一个定时器来开启一个非 Qt 事件循环(就是普通的循环处理)。 详见: Start()、 slotTimeOut()、 OnProcess() 。 当然,它仍然支...
virtual int WakeUp()
Wake up.
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
得到显示视图