Rabbit Remote Control 0.1.0-alpha
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_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 return OnInitReturnValue::Fail;
200#endif
201 emit sigRunning();
202 return OnInitReturnValue::UseOnProcess;
203}
204
205void CBackendFileTransfer::slotMakeDir(const QString &szDir)
206{
207 if(szDir.isEmpty()) return;
208 CFileTransferEvent* pEvent = new CFileTransferEvent(
209 CFileTransferEvent::Command::MakeDir, szDir);
210 QCoreApplication::postEvent(this, pEvent);
211 WakeUp();
212}
213
214void CBackendFileTransfer::slotRemoveDir(const QString &szDir)
215{
216 if(szDir.isEmpty()) return;
217 CFileTransferEvent* pEvent = new CFileTransferEvent(
218 CFileTransferEvent::Command::RemoveDir, szDir);
219 QCoreApplication::postEvent(this, pEvent);
220 WakeUp();
221}
222
223void CBackendFileTransfer::slotRemoveFile(const QString &szFile)
224{
225 if(szFile.isEmpty()) return;
226 CFileTransferEvent* pEvent = new CFileTransferEvent(
227 CFileTransferEvent::Command::RemoveFile, szFile);
228 QCoreApplication::postEvent(this, pEvent);
229 WakeUp();
230}
231
232void CBackendFileTransfer::slotRename(const QString &oldName, const QString &newName)
233{
234 if(oldName.isEmpty() && newName.isEmpty()) return;
235 CFileTransferEvent* pEvent = new CFileTransferEvent(
236 CFileTransferEvent::Command::Rename, oldName, newName);
237 QCoreApplication::postEvent(this, pEvent);
238 WakeUp();
239}
240
241void CBackendFileTransfer::slotGetDir(CRemoteFileSystem *p)
242{
243 if(!p || p->GetPath().isEmpty())
244 return;
245 QString szPath = p->GetPath();
246 CFileTransferEvent* pEvent = new CFileTransferEvent(
247 CFileTransferEvent::Command::GetDir, szPath);
248 pEvent->m_pRemoteFileSystem = (CRemoteFileSystem*) p;
249 QCoreApplication::postEvent(this, pEvent);
250 WakeUp();
251}
252
253void CBackendFileTransfer::slotStartFileTransfer(QSharedPointer<CFileTransfer> f)
254{
255 if(!f) return;
256 CFileTransferEvent* pEvent = new CFileTransferEvent(
257 CFileTransferEvent::Command::StartFileTransfer);
258 pEvent->m_FileTransfer = f;
259 QCoreApplication::postEvent(this, pEvent);
260 WakeUp();
261}
262
263void CBackendFileTransfer::slotStopFileTransfer(QSharedPointer<CFileTransfer> f)
264{
265 if(!f) return;
266 CFileTransferEvent* pEvent = new CFileTransferEvent(
267 CFileTransferEvent::Command::StopFileTransfer);
268 pEvent->m_FileTransfer = f;
269 QCoreApplication::postEvent(this, pEvent);
270 WakeUp();
271}
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 sigRunning()
Emitted when the plugin is successfully started.
File transfer operate interface.
virtual QWidget * GetViewer() override
Get Viewer.