Rabbit Remote Control 0.1.0-bate1
Loading...
Searching...
No Matches
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(m_pSFTP)
95 m_pSFTP->slotGetDir(pEvent->m_szSourcePath, pEvent->m_pRemoteFileSystem);
96 break;
97 }
98 case CFileTransferEvent::Command::StartFileTransfer:
99 {
100#if HAVE_LIBSSH
101 if(m_pSFTP)
102 m_pSFTP->slotStartFileTransfer(pEvent->m_FileTransfer);
103#endif
104 break;
105 }
106 case CFileTransferEvent::Command::StopFileTransfer:
107 {
108#if HAVE_LIBSSH
109 if(m_pSFTP)
110 m_pSFTP->slotStopFileTransfer(pEvent->m_FileTransfer);
111#endif
112 break;
113 }
114 default:
115 break;
116 }
117 }
118 return CBackend::event(event);
119}
120
121CBackend::OnInitReturnValue CBackendFileTransfer::OnInit()
122{
123 OnInitReturnValue nRet = OnInitReturnValue::Fail;
124 if(m_pPara->GetProtocol() == CParameterFileTransfer::Protocol::SFTP)
125 nRet = InitSFTP();
126 return nRet;
127}
128
130{
131 int nRet = 0;
132#if HAVE_LIBSSH
133 if(m_pSFTP) {
134 m_pSFTP->close();
135 m_pSFTP->deleteLater();
136 m_pSFTP = nullptr;
137 }
138#endif
139 return nRet;
140}
141
143{
144 int nRet = 0;
145#if HAVE_LIBSSH
146 if(m_pSFTP)
147 nRet = m_pSFTP->Process();
148#endif
149 return nRet;
150}
151
152int CBackendFileTransfer::SetConnect(COperateFileTransfer *pOperate)
153{
154 int nRet = 0;
155 bool check = false;
156 CFrmFileTransfer* pForm = qobject_cast<CFrmFileTransfer*>(pOperate->GetViewer());
157 Q_ASSERT(pForm);
158 check = connect(pForm, SIGNAL(sigGetDir(CRemoteFileSystem*)),
159 this, SLOT(slotGetDir(CRemoteFileSystem*)),
160 Qt::DirectConnection);
161 Q_ASSERT(check);
162 check = connect(this, SIGNAL(sigGetDir(CRemoteFileSystem*, QVector<QSharedPointer<CRemoteFileSystem> >, bool)),
163 pForm, SIGNAL(sigGetDir(CRemoteFileSystem*, QVector<QSharedPointer<CRemoteFileSystem> >, bool)));
164 Q_ASSERT(check);
165 check = connect(pForm, SIGNAL(sigMakeDir(const QString&)),
166 this, SLOT(slotMakeDir(const QString&)));
167 Q_ASSERT(check);
168 check = connect(pForm, SIGNAL(sigRemoveDir(const QString&)),
169 this, SLOT(slotRemoveDir(const QString&)));
170 Q_ASSERT(check);
171 check = connect(pForm, SIGNAL(sigRemoveFile(const QString&)),
172 this, SLOT(slotRemoveFile(const QString&)));
173 Q_ASSERT(check);
174 check = connect(pForm, SIGNAL(sigRename(const QString&, const QString&)),
175 this, SLOT(slotRename(const QString&, const QString&)));
176 Q_ASSERT(check);
177 check = connect(pForm, SIGNAL(sigStartFileTransfer(QSharedPointer<CFileTransfer>)),
178 this, SLOT(slotStartFileTransfer(QSharedPointer<CFileTransfer>)));
179 Q_ASSERT(check);
180 check = connect(pForm, SIGNAL(sigStopFileTransfer(QSharedPointer<CFileTransfer>)),
181 this, SLOT(slotStopFileTransfer(QSharedPointer<CFileTransfer>)));
182 Q_ASSERT(check);
183 check = connect(this, SIGNAL(sigFileTransferUpdate(QSharedPointer<CFileTransfer>)),
184 pForm, SLOT(slotFileTransferUpdate(QSharedPointer<CFileTransfer>)));
185 Q_ASSERT(check);
186 return nRet;
187}
188
189CBackendFileTransfer::OnInitReturnValue CBackendFileTransfer::InitSFTP()
190{
191 CParameterSSH* ssh = &m_pOperate->GetParameter()->m_SSH;
192#if HAVE_LIBSSH
193 m_pSFTP = new CChannelSFTP(this, ssh);
194 if(!m_pSFTP) {
195 return OnInitReturnValue::Fail;
196 }
197 bool bRet = m_pSFTP->open(QIODevice::ReadWrite);
198 if(!bRet) {
199 QString szErr;
200 szErr = tr("Open SFTP fail.") + m_pSFTP->errorString();
201 qCritical(log) << szErr;
202 emit sigShowMessageBox(tr("Error"), szErr, QMessageBox::Critical);
203 return OnInitReturnValue::Fail;
204 }
205#endif
206 emit sigRunning();
207 return OnInitReturnValue::UseOnProcess;
208}
209
210void CBackendFileTransfer::slotMakeDir(const QString &szDir)
211{
212 if(szDir.isEmpty()) return;
213 CFileTransferEvent* pEvent = new CFileTransferEvent(
214 CFileTransferEvent::Command::MakeDir, szDir);
215 QCoreApplication::postEvent(this, pEvent);
216 WakeUp();
217}
218
219void CBackendFileTransfer::slotRemoveDir(const QString &szDir)
220{
221 if(szDir.isEmpty()) return;
222 CFileTransferEvent* pEvent = new CFileTransferEvent(
223 CFileTransferEvent::Command::RemoveDir, szDir);
224 QCoreApplication::postEvent(this, pEvent);
225 WakeUp();
226}
227
228void CBackendFileTransfer::slotRemoveFile(const QString &szFile)
229{
230 if(szFile.isEmpty()) return;
231 CFileTransferEvent* pEvent = new CFileTransferEvent(
232 CFileTransferEvent::Command::RemoveFile, szFile);
233 QCoreApplication::postEvent(this, pEvent);
234 WakeUp();
235}
236
237void CBackendFileTransfer::slotRename(const QString &oldName, const QString &newName)
238{
239 if(oldName.isEmpty() && newName.isEmpty()) return;
240 CFileTransferEvent* pEvent = new CFileTransferEvent(
241 CFileTransferEvent::Command::Rename, oldName, newName);
242 QCoreApplication::postEvent(this, pEvent);
243 WakeUp();
244}
245
246void CBackendFileTransfer::slotGetDir(CRemoteFileSystem *p)
247{
248 if(!p || p->GetPath().isEmpty())
249 return;
250 QString szPath = p->GetPath();
251 CFileTransferEvent* pEvent = new CFileTransferEvent(
252 CFileTransferEvent::Command::GetDir, szPath);
253 pEvent->m_pRemoteFileSystem = (CRemoteFileSystem*) p;
254 QCoreApplication::postEvent(this, pEvent);
255 WakeUp();
256}
257
258void CBackendFileTransfer::slotStartFileTransfer(QSharedPointer<CFileTransfer> f)
259{
260 if(!f) return;
261 CFileTransferEvent* pEvent = new CFileTransferEvent(
262 CFileTransferEvent::Command::StartFileTransfer);
263 pEvent->m_FileTransfer = f;
264 QCoreApplication::postEvent(this, pEvent);
265 WakeUp();
266}
267
268void CBackendFileTransfer::slotStopFileTransfer(QSharedPointer<CFileTransfer> f)
269{
270 if(!f) return;
271 CFileTransferEvent* pEvent = new CFileTransferEvent(
272 CFileTransferEvent::Command::StopFileTransfer);
273 pEvent->m_FileTransfer = f;
274 QCoreApplication::postEvent(this, pEvent);
275 WakeUp();
276}
virtual OnInitReturnValue OnInit() override
Initialization.
virtual int OnClean() override
Clean.
virtual int OnProcess() override
Specific operation processing of plug-in.
Backend interface.
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)
Trigger the display of a message dialog (QMessageBox) in the main thread from a background thread wit...
void sigRunning()
Emitted when the plugin is successfully started.
File transfer operate interface.
virtual QWidget * GetViewer() override
Get Viewer.