玉兔远程控制 0.1.0-bate4
载入中...
搜索中...
未找到
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
63bool CBackendFileTransfer::event(QEvent *event)
64{
65 if(event->type() == QEvent::Type::User) {
66 CFileTransferEvent* pEvent = (CFileTransferEvent*)event;
67 switch(pEvent->m_Command) {
68 case CFileTransferEvent::Command::MakeDir:
69#if HAVE_LIBSSH
70 if(m_pSFTP)
71 m_pSFTP->MakeDir(pEvent->m_szSourcePath);
72#endif
73 break;
74 case CFileTransferEvent::Command::RemoveDir:
75#if HAVE_LIBSSH
76 if(m_pSFTP)
77 m_pSFTP->RemoveDir(pEvent->m_szSourcePath);
78#endif
79 break;
80 case CFileTransferEvent::Command::RemoveFile:
81#if HAVE_LIBSSH
82 if(m_pSFTP)
83 m_pSFTP->RemoveFile(pEvent->m_szSourcePath);
84#endif
85 break;
86 case CFileTransferEvent::Command::Rename:
87#if HAVE_LIBSSH
88 if(m_pSFTP)
89 m_pSFTP->Rename(pEvent->m_szSourcePath, pEvent->m_szDestination);
90#endif
91 break;
92 case CFileTransferEvent::Command::GetDir:
93 {
94#if HAVE_LIBSSH
95 if(m_pSFTP)
96 m_pSFTP->slotGetDir(pEvent->m_szSourcePath, pEvent->m_pRemoteFileSystem);
97#endif
98 break;
99 }
100 case CFileTransferEvent::Command::StartFileTransfer:
101 {
102#if HAVE_LIBSSH
103 if(m_pSFTP)
104 m_pSFTP->slotStartFileTransfer(pEvent->m_FileTransfer);
105#endif
106 break;
107 }
108 case CFileTransferEvent::Command::StopFileTransfer:
109 {
110#if HAVE_LIBSSH
111 if(m_pSFTP)
112 m_pSFTP->slotStopFileTransfer(pEvent->m_FileTransfer);
113#endif
114 break;
115 }
116 default:
117 break;
118 }
119 }
120 return CBackend::event(event);
121}
122
123CBackend::OnInitReturnValue CBackendFileTransfer::OnInit()
124{
125 OnInitReturnValue nRet = OnInitReturnValue::Fail;
126 if(m_pPara->GetProtocol() == CParameterFileTransfer::Protocol::SFTP)
127 nRet = InitSFTP();
128 return nRet;
129}
130
132{
133 int nRet = 0;
134#if HAVE_LIBSSH
135 if(m_pSFTP) {
136 m_pSFTP->close();
137 m_pSFTP->deleteLater();
138 m_pSFTP = nullptr;
139 }
140#endif
141 return nRet;
142}
143
145{
146 int nRet = 0;
147#if HAVE_LIBSSH
148 if(m_pSFTP)
149 nRet = m_pSFTP->Process();
150#endif
151 return nRet;
152}
153
154int CBackendFileTransfer::SetConnect(COperateFileTransfer *pOperate)
155{
156 int nRet = 0;
157 bool check = false;
158 CFrmFileTransfer* pForm = qobject_cast<CFrmFileTransfer*>(pOperate->GetViewer());
159 Q_ASSERT(pForm);
160 check = connect(pForm, SIGNAL(sigGetDir(CRemoteFileSystem*)),
161 this, SLOT(slotGetDir(CRemoteFileSystem*)),
162 Qt::DirectConnection);
163 Q_ASSERT(check);
164 check = connect(this, SIGNAL(sigGetDir(CRemoteFileSystem*, QVector<QSharedPointer<CRemoteFileSystem> >, bool)),
165 pForm, SIGNAL(sigGetDir(CRemoteFileSystem*, QVector<QSharedPointer<CRemoteFileSystem> >, bool)));
166 Q_ASSERT(check);
167 check = connect(pForm, SIGNAL(sigMakeDir(const QString&)),
168 this, SLOT(slotMakeDir(const QString&)));
169 Q_ASSERT(check);
170 check = connect(pForm, SIGNAL(sigRemoveDir(const QString&)),
171 this, SLOT(slotRemoveDir(const QString&)));
172 Q_ASSERT(check);
173 check = connect(pForm, SIGNAL(sigRemoveFile(const QString&)),
174 this, SLOT(slotRemoveFile(const QString&)));
175 Q_ASSERT(check);
176 check = connect(pForm, SIGNAL(sigRename(const QString&, const QString&)),
177 this, SLOT(slotRename(const QString&, const QString&)));
178 Q_ASSERT(check);
179 check = connect(pForm, SIGNAL(sigStartFileTransfer(QSharedPointer<CFileTransfer>)),
180 this, SLOT(slotStartFileTransfer(QSharedPointer<CFileTransfer>)));
181 Q_ASSERT(check);
182 check = connect(pForm, SIGNAL(sigStopFileTransfer(QSharedPointer<CFileTransfer>)),
183 this, SLOT(slotStopFileTransfer(QSharedPointer<CFileTransfer>)));
184 Q_ASSERT(check);
185 check = connect(this, SIGNAL(sigFileTransferUpdate(QSharedPointer<CFileTransfer>)),
186 pForm, SLOT(slotFileTransferUpdate(QSharedPointer<CFileTransfer>)));
187 Q_ASSERT(check);
188 return nRet;
189}
190
191CBackendFileTransfer::OnInitReturnValue CBackendFileTransfer::InitSFTP()
192{
193 CParameterSSH* ssh = &m_pOperate->GetParameter()->m_SSH;
194#if HAVE_LIBSSH
195 m_pSFTP = new CChannelSFTP(this, ssh);
196 if(!m_pSFTP) {
197 return OnInitReturnValue::Fail;
198 }
199 bool bRet = m_pSFTP->open(QIODevice::ReadWrite);
200 if(!bRet) {
201 QString szErr;
202 szErr = tr("Open SFTP fail.") + m_pSFTP->errorString();
203 qCritical(log) << szErr;
204 emit sigShowMessageBox(tr("Error"), szErr, QMessageBox::Critical);
205 return OnInitReturnValue::Fail;
206 }
207#endif
208 emit sigRunning();
209 return OnInitReturnValue::UseOnProcess;
210}
211
212void CBackendFileTransfer::slotMakeDir(const QString &szDir)
213{
214 if(szDir.isEmpty()) return;
215 CFileTransferEvent* pEvent = new CFileTransferEvent(
216 CFileTransferEvent::Command::MakeDir, szDir);
217 QCoreApplication::postEvent(this, pEvent);
218 WakeUp();
219}
220
221void CBackendFileTransfer::slotRemoveDir(const QString &szDir)
222{
223 if(szDir.isEmpty()) return;
224 CFileTransferEvent* pEvent = new CFileTransferEvent(
225 CFileTransferEvent::Command::RemoveDir, szDir);
226 QCoreApplication::postEvent(this, pEvent);
227 WakeUp();
228}
229
230void CBackendFileTransfer::slotRemoveFile(const QString &szFile)
231{
232 if(szFile.isEmpty()) return;
233 CFileTransferEvent* pEvent = new CFileTransferEvent(
234 CFileTransferEvent::Command::RemoveFile, szFile);
235 QCoreApplication::postEvent(this, pEvent);
236 WakeUp();
237}
238
239void CBackendFileTransfer::slotRename(const QString &oldName, const QString &newName)
240{
241 if(oldName.isEmpty() && newName.isEmpty()) return;
242 CFileTransferEvent* pEvent = new CFileTransferEvent(
243 CFileTransferEvent::Command::Rename, oldName, newName);
244 QCoreApplication::postEvent(this, pEvent);
245 WakeUp();
246}
247
248void CBackendFileTransfer::slotGetDir(CRemoteFileSystem *p)
249{
250 if(!p || p->GetPath().isEmpty())
251 return;
252 QString szPath = p->GetPath();
253 CFileTransferEvent* pEvent = new CFileTransferEvent(
254 CFileTransferEvent::Command::GetDir, szPath);
255 pEvent->m_pRemoteFileSystem = (CRemoteFileSystem*) p;
256 QCoreApplication::postEvent(this, pEvent);
257 WakeUp();
258}
259
260void CBackendFileTransfer::slotStartFileTransfer(QSharedPointer<CFileTransfer> f)
261{
262 if(!f) return;
263 CFileTransferEvent* pEvent = new CFileTransferEvent(
264 CFileTransferEvent::Command::StartFileTransfer);
265 pEvent->m_FileTransfer = f;
266 QCoreApplication::postEvent(this, pEvent);
267 WakeUp();
268}
269
270void CBackendFileTransfer::slotStopFileTransfer(QSharedPointer<CFileTransfer> f)
271{
272 if(!f) return;
273 CFileTransferEvent* pEvent = new CFileTransferEvent(
274 CFileTransferEvent::Command::StopFileTransfer);
275 pEvent->m_FileTransfer = f;
276 QCoreApplication::postEvent(this, pEvent);
277 WakeUp();
278}
virtual OnInitReturnValue OnInit() override
初始化
virtual int OnClean() override
清理
virtual int OnProcess() override
具体操作处理
后端接口。它由协议插件实现。 它默认启动一个定时器来开启一个非 Qt 事件循环(就是普通的循环处理)。 详见: Start()、 slotTimeOut()、 OnProcess() 。 当然,它仍然支...
Definition Backend.h:42
virtual int WakeUp()
Wake up.
Definition Backend.cpp:100
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
得到显示视图