玉兔远程控制 0.1.0-bate11
载入中...
搜索中...
未找到
BackendFileTransfer.cpp
1// Copyright Copyright (c) Kang Lin studio, All Rights Reserved
2// Author: Kang Lin <kl222@126.com>
3
4#include <QCoreApplication>
5#include <QLoggingCategory>
6#include "BackendFileTransfer.h"
7
8static Q_LOGGING_CATEGORY(log, "FileTransfer.Backend")
9
10class CFileTransferEvent : public QEvent
11{
12public:
13 enum class Command {
14 MakeDir,
15 RemoveDir,
16 RemoveFile,
17 Rename,
18 GetDir,
19 StartFileTransfer,
20 StopFileTransfer
21 };
22 Q_ENUM(Command)
23 CFileTransferEvent(Command cmd,
24 const QString& szSrcPath = QString(),
25 const QString& szDesPath = QString());
26
27 Command m_Command;
28 QString m_szSourcePath;
29 QString m_szDestination;
30 CRemoteFileSystem* m_pRemoteFileSystem;
31 QSharedPointer<CFileTransfer> m_FileTransfer;
32};
33
34CFileTransferEvent::CFileTransferEvent(Command cmd,
35 const QString &szSrcPath,
36 const QString &szDesPath)
37 : QEvent(QEvent::Type::User)
38 , m_Command(cmd)
39 , m_szSourcePath(szSrcPath)
40 , m_szDestination(szDesPath)
41{
42}
43
44CBackendFileTransfer::CBackendFileTransfer(COperateFileTransfer *pOperate)
45 : CBackend(pOperate)
46 , m_pOperate(pOperate)
47#if HAVE_LIBSSH
48 , m_pSFTP(nullptr)
49#endif
50 , m_pPara(nullptr)
51{
52 qDebug(log) << Q_FUNC_INFO;
53 if(m_pOperate)
54 m_pPara = m_pOperate->GetParameter();
55 SetConnect(pOperate);
56}
57
58CBackendFileTransfer::~CBackendFileTransfer()
59{
60 qDebug(log) << Q_FUNC_INFO;
61}
62
64{
65 return 0;
66}
67
68bool CBackendFileTransfer::event(QEvent *event)
69{
70 if(event->type() == QEvent::Type::User) {
71 CFileTransferEvent* pEvent = (CFileTransferEvent*)event;
72 switch(pEvent->m_Command) {
73 case CFileTransferEvent::Command::MakeDir:
74#if HAVE_LIBSSH
75 if(m_pSFTP)
76 m_pSFTP->MakeDir(pEvent->m_szSourcePath);
77#endif
78 break;
79 case CFileTransferEvent::Command::RemoveDir:
80#if HAVE_LIBSSH
81 if(m_pSFTP)
82 m_pSFTP->RemoveDir(pEvent->m_szSourcePath);
83#endif
84 break;
85 case CFileTransferEvent::Command::RemoveFile:
86#if HAVE_LIBSSH
87 if(m_pSFTP)
88 m_pSFTP->RemoveFile(pEvent->m_szSourcePath);
89#endif
90 break;
91 case CFileTransferEvent::Command::Rename:
92#if HAVE_LIBSSH
93 if(m_pSFTP)
94 m_pSFTP->Rename(pEvent->m_szSourcePath, pEvent->m_szDestination);
95#endif
96 break;
97 case CFileTransferEvent::Command::GetDir:
98 {
99#if HAVE_LIBSSH
100 if(m_pSFTP)
101 m_pSFTP->slotGetDir(pEvent->m_szSourcePath, pEvent->m_pRemoteFileSystem);
102#endif
103 break;
104 }
105 case CFileTransferEvent::Command::StartFileTransfer:
106 {
107#if HAVE_LIBSSH
108 if(m_pSFTP)
109 m_pSFTP->slotStartFileTransfer(pEvent->m_FileTransfer);
110#endif
111 break;
112 }
113 case CFileTransferEvent::Command::StopFileTransfer:
114 {
115#if HAVE_LIBSSH
116 if(m_pSFTP)
117 m_pSFTP->slotStopFileTransfer(pEvent->m_FileTransfer);
118#endif
119 break;
120 }
121 default:
122 break;
123 }
124 }
125 return CBackend::event(event);
126}
127
128CBackend::OnInitReturnValue CBackendFileTransfer::OnInit()
129{
130 OnInitReturnValue nRet = OnInitReturnValue::Fail;
131 if(m_pPara->GetProtocol() == CParameterFileTransfer::Protocol::SFTP)
132 nRet = InitSFTP();
133 return nRet;
134}
135
137{
138 int nRet = 0;
139#if HAVE_LIBSSH
140 if(m_pSFTP) {
141 m_pSFTP->close();
142 m_pSFTP->deleteLater();
143 m_pSFTP = nullptr;
144 }
145#endif
146 return nRet;
147}
148
150{
151 int nRet = 0;
152#if HAVE_LIBSSH
153 if(m_pSFTP)
154 nRet = m_pSFTP->Process();
155#endif
156 return nRet;
157}
158
159int CBackendFileTransfer::SetConnect(COperateFileTransfer *pOperate)
160{
161 int nRet = 0;
162 bool check = false;
163 CFrmFileTransfer* pForm = qobject_cast<CFrmFileTransfer*>(pOperate->GetViewer());
164 Q_ASSERT(pForm);
165 check = connect(pForm, SIGNAL(sigGetDir(CRemoteFileSystem*)),
166 this, SLOT(slotGetDir(CRemoteFileSystem*)),
167 Qt::DirectConnection);
168 Q_ASSERT(check);
169 check = connect(this, SIGNAL(sigGetDir(CRemoteFileSystem*, QVector<QSharedPointer<CRemoteFileSystem> >, bool)),
170 pForm, SIGNAL(sigGetDir(CRemoteFileSystem*, QVector<QSharedPointer<CRemoteFileSystem> >, bool)));
171 Q_ASSERT(check);
172 check = connect(pForm, SIGNAL(sigMakeDir(const QString&)),
173 this, SLOT(slotMakeDir(const QString&)));
174 Q_ASSERT(check);
175 check = connect(pForm, SIGNAL(sigRemoveDir(const QString&)),
176 this, SLOT(slotRemoveDir(const QString&)));
177 Q_ASSERT(check);
178 check = connect(pForm, SIGNAL(sigRemoveFile(const QString&)),
179 this, SLOT(slotRemoveFile(const QString&)));
180 Q_ASSERT(check);
181 check = connect(pForm, SIGNAL(sigRename(const QString&, const QString&)),
182 this, SLOT(slotRename(const QString&, const QString&)));
183 Q_ASSERT(check);
184 check = connect(pForm, SIGNAL(sigStartFileTransfer(QSharedPointer<CFileTransfer>)),
185 this, SLOT(slotStartFileTransfer(QSharedPointer<CFileTransfer>)));
186 Q_ASSERT(check);
187 check = connect(pForm, SIGNAL(sigStopFileTransfer(QSharedPointer<CFileTransfer>)),
188 this, SLOT(slotStopFileTransfer(QSharedPointer<CFileTransfer>)));
189 Q_ASSERT(check);
190 check = connect(this, SIGNAL(sigFileTransferUpdate(QSharedPointer<CFileTransfer>)),
191 pForm, SLOT(slotFileTransferUpdate(QSharedPointer<CFileTransfer>)));
192 Q_ASSERT(check);
193 return nRet;
194}
195
196CBackendFileTransfer::OnInitReturnValue CBackendFileTransfer::InitSFTP()
197{
198 CParameterSSH* ssh = &m_pOperate->GetParameter()->m_SSH;
199#if HAVE_LIBSSH
200 m_pSFTP = new CChannelSFTP(this, ssh);
201 if(!m_pSFTP) {
202 return OnInitReturnValue::Fail;
203 }
204 bool bRet = m_pSFTP->open(QIODevice::ReadWrite);
205 if(!bRet) {
206 QString szErr;
207 szErr = tr("Open SFTP fail.") + m_pSFTP->errorString();
208 qCritical(log) << szErr;
209 emit sigShowMessageBox(tr("Error"), szErr, QMessageBox::Critical);
210 return OnInitReturnValue::Fail;
211 }
212#endif
213 emit sigRunning();
214 return OnInitReturnValue::UseOnProcess;
215}
216
217void CBackendFileTransfer::slotMakeDir(const QString &szDir)
218{
219 if(szDir.isEmpty()) return;
220 CFileTransferEvent* pEvent = new CFileTransferEvent(
221 CFileTransferEvent::Command::MakeDir, szDir);
222 QCoreApplication::postEvent(this, pEvent);
223 WakeUp();
224}
225
226void CBackendFileTransfer::slotRemoveDir(const QString &szDir)
227{
228 if(szDir.isEmpty()) return;
229 CFileTransferEvent* pEvent = new CFileTransferEvent(
230 CFileTransferEvent::Command::RemoveDir, szDir);
231 QCoreApplication::postEvent(this, pEvent);
232 WakeUp();
233}
234
235void CBackendFileTransfer::slotRemoveFile(const QString &szFile)
236{
237 if(szFile.isEmpty()) return;
238 CFileTransferEvent* pEvent = new CFileTransferEvent(
239 CFileTransferEvent::Command::RemoveFile, szFile);
240 QCoreApplication::postEvent(this, pEvent);
241 WakeUp();
242}
243
244void CBackendFileTransfer::slotRename(const QString &oldName, const QString &newName)
245{
246 if(oldName.isEmpty() && newName.isEmpty()) return;
247 CFileTransferEvent* pEvent = new CFileTransferEvent(
248 CFileTransferEvent::Command::Rename, oldName, newName);
249 QCoreApplication::postEvent(this, pEvent);
250 WakeUp();
251}
252
253void CBackendFileTransfer::slotGetDir(CRemoteFileSystem *pRemoteFileSystem)
254{
255 if(!pRemoteFileSystem || pRemoteFileSystem->GetPath().isEmpty())
256 return;
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);
262 WakeUp();
263}
264
265void CBackendFileTransfer::slotStartFileTransfer(QSharedPointer<CFileTransfer> f)
266{
267 if(!f) return;
268 CFileTransferEvent* pEvent = new CFileTransferEvent(
269 CFileTransferEvent::Command::StartFileTransfer);
270 pEvent->m_FileTransfer = f;
271 QCoreApplication::postEvent(this, pEvent);
272 WakeUp();
273}
274
275void CBackendFileTransfer::slotStopFileTransfer(QSharedPointer<CFileTransfer> f)
276{
277 if(!f) return;
278 CFileTransferEvent* pEvent = new CFileTransferEvent(
279 CFileTransferEvent::Command::StopFileTransfer);
280 pEvent->m_FileTransfer = f;
281 QCoreApplication::postEvent(this, pEvent);
282 WakeUp();
283}
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
得到显示视图