1 #include "ftpfunctions.h"
2 #include "appglobal.h"
3 #include "ftpevents.h"
4 #include "logging.h"
5 #include "configparams.h"
6 #include "dispatcher.h"
7 
8 #include <QApplication>
9 #include <QDebug>
10 #include <QTemporaryFile>
11 #include <QMessageBox>
12 #include <errno.h>
13 
14 
ftpFunctions()15 ftpFunctions::ftpFunctions()
16 {
17   thread=NULL;
18   ftpThr=NULL;
19   busy=false;
20 }
21 
~ftpFunctions()22 ftpFunctions::~ftpFunctions()
23 {
24 
25 }
26 
test(QString name,QString tHost,int tPort,QString tUser,QString tPasswd,QString tDirectory,bool doSetup)27 bool ftpFunctions::test(QString name,QString tHost,int tPort,QString tUser,QString tPasswd,QString tDirectory, bool doSetup)
28 {
29   bool result=false;
30   QString fn;
31   QString rfn;
32   QTemporaryFile tst;
33   host=tHost;
34   port=tPort;
35   user=tUser;
36   passwd=tPasswd;
37   directory=tDirectory;
38   idName=name;
39   endOfCommands=false;
40   addToLog(QString("%1 execFTPTest").arg(idName), LOGFTPFUNC);
41   if (!tst.open())
42     {
43       lastError=FTPERROR;
44       lastErrorStr=QString("Error writing temp file: %1, %2").arg(tst.fileName()).arg(strerror(errno));
45     }
46   else
47     {
48       if(doSetup) setupFtp();
49       fn=tst.fileName();
50       tst.write("connection test\n");
51       tst.close();
52       rfn = QString("test_%1.txt").arg(myCallsign);
53       if((result=checkUpload(fn,rfn)))
54         {
55           result=checkRemove(rfn);
56         }
57     }
58 
59   if(result)
60     {
61       lastErrorStr=QString("Connection to %1/%2 OK").arg(host).arg(directory);
62     }
63 //  if(lastTest)
64 //    {
65 //     thread->quit();
66 //    while(thread->isRunning())
67 //      {
68 //          QApplication::processEvents();
69 //       }
70 //    }
71   return result;
72 }
73 
checkUpload(QString fn,QString rfn)74 bool ftpFunctions::checkUpload(QString fn, QString rfn)
75 {
76   endOfCommands=false;
77   ftp_uploadEvent *ftpUpl=new ftp_uploadEvent(fn,rfn,false);
78   QApplication::postEvent(ftpThr,ftpUpl);
79   checkWait(true);
80   return lastError==FTPOK;
81 }
82 
83 
checkRemove(QString rfn)84 bool ftpFunctions::checkRemove(QString rfn)
85 {
86   endOfCommands=false;
87   ftp_removeEvent *ftpRemove=new ftp_removeEvent(rfn,true);
88   QApplication::postEvent(ftpThr,ftpRemove);
89   checkWait(true);
90   return lastError==FTPOK;
91 }
92 
checkStart()93 bool ftpFunctions::checkStart()
94 {
95   endOfCommands=false;
96   if(ftpThr!=NULL)
97     {
98       return true;
99     }
100   return false;
101 }
102 
uploadFile(QString source,QString destination,bool wait,bool closeWhenDone)103 void ftpFunctions::uploadFile(QString source,QString destination,bool wait,bool closeWhenDone)
104 {
105   if (!checkStart()) return;
106   ftp_uploadEvent *ftpUpl=new ftp_uploadEvent(source,destination,closeWhenDone);
107   QApplication::postEvent(ftpThr,ftpUpl);
108   checkWait(wait);
109 }
110 
111 
downloadFile(QString source,QString destination,bool wait,bool closeWhenDone)112 void ftpFunctions::downloadFile(QString source, QString destination,bool wait,bool closeWhenDone)
113 {
114   if (!checkStart()) return;
115   ftp_downloadEvent *ftpDwnl=new ftp_downloadEvent(source,destination,closeWhenDone);
116   QApplication::postEvent(ftpThr,ftpDwnl);
117   checkWait(wait);
118 }
119 
120 
uploadData(QByteArray ba,QString destination,bool wait,bool closeWhenDone)121 void ftpFunctions::uploadData(QByteArray ba,QString destination,bool wait,bool closeWhenDone)
122 {
123   QString fileName;
124   if(!ftmp.open()) return ;
125   ftmp.write(ba);
126   ftmp.close();
127   fileName=ftmp.fileName();
128   uploadFile(fileName,destination,wait,closeWhenDone);
129 
130 }
131 
remove(QString source,bool wait,bool closeWhenDone)132 void ftpFunctions::remove(QString source,bool wait,bool closeWhenDone)
133 {
134   if (!checkStart()) return;
135   ftp_removeEvent *ftpRemove=new ftp_removeEvent(source,closeWhenDone);
136   QApplication::postEvent(ftpThr,ftpRemove);
137   checkWait(wait);
138 }
139 
mremove(QString mask,bool wait,bool closeWhenDone)140 void ftpFunctions::mremove(QString mask,bool wait,bool closeWhenDone)
141 {
142   if (!checkStart()) return;
143   mremoveCmd=true;
144   addToLog("mremove cmd",LOGFTPFUNC);
145   ftp_listEvent *ftpList=new ftp_listEvent(mask,closeWhenDone);
146   QApplication::postEvent(ftpThr,ftpList);
147   checkWait(wait);
148 }
149 
150 
rename(QString source,QString destination,bool wait,bool closeWhenDone)151 void ftpFunctions::rename(QString source, QString destination,bool wait,bool closeWhenDone)
152 {
153   if (!checkStart()) return;
154   ftp_renameEvent *ftpRename=new ftp_renameEvent(source,destination,closeWhenDone);
155   QApplication::postEvent(ftpThr,ftpRename);
156   checkWait(wait);
157 }
158 
changePath(QString source,bool wait)159 void ftpFunctions::changePath(QString source,bool wait)
160 {
161   if (!checkStart()) return;
162   addToLog(QString("changePath %1").arg(source),LOGFTPFUNC);
163   ftp_cdEvent *ftpCd=new ftp_cdEvent(source);
164   QApplication::postEvent(ftpThr,ftpCd);
165   checkWait(wait);
166 }
167 
168 
listFiles(QString mask,bool closeWhenDone)169 void ftpFunctions::listFiles(QString mask,bool closeWhenDone)
170 {
171   if (!checkStart()) return;
172   ftp_listEvent *ftpList=new ftp_listEvent(mask,closeWhenDone);
173   QApplication::postEvent(ftpThr,ftpList);
174 }
175 
setupFtp(QString name,QString tHost,int tPort,QString tUser,QString tPasswd,QString tDirectory)176 void ftpFunctions::setupFtp(QString name,QString tHost,int tPort,QString tUser,QString tPasswd,QString tDirectory)
177 {
178   // create an ftp thread;
179   host=tHost;
180   port=tPort;
181   user=tUser;
182   passwd=tPasswd;
183   directory=tDirectory;
184   idName=name;
185   setupFtp();
186 }
187 
changeThreadName(QString tidName)188 void ftpFunctions::changeThreadName(QString tidName)
189 {
190   idName=tidName;
191 }
192 
setupFtp()193 void ftpFunctions::setupFtp()
194 {
195   thread = new QThread;
196 //  qDebug() << "creating thread" << idName;
197   thread->setObjectName(idName);
198   ftpThr=new ftpThread(idName);
199   ftpThr->setHostParams(host,port,user,passwd,directory);
200   ftpThr->moveToThread(thread);
201   connect(thread, SIGNAL(started()), ftpThr, SLOT(slotInit()));
202   connect(ftpThr, SIGNAL(commandsDone(int ,QString)), this,   SLOT(slotCommandsDone(int ,QString)));
203   connect(ftpThr, SIGNAL(downloadFinished(bool,QString)), this,   SLOT(slotDownloadFinished(bool,QString)));
204   connect(ftpThr, SIGNAL(listingComplete(bool)), this,   SLOT(slotListingFinished(bool)));
205   //automatically delete thread and task object when work is done:
206   connect( ftpThr, SIGNAL(ftpQuit()), thread, SLOT (quit()), Qt::DirectConnection);
207   connect( thread, SIGNAL(finished()),  SLOT(slotThreadFinished()));
208   thread->start();
209   while(!thread->isRunning())
210     {
211       QApplication::processEvents();
212     }
213   busy=true;
214   mremoveCmd=false;
215 }
216 
217 
disconnectFtp()218 void ftpFunctions::disconnectFtp()
219 {
220   if(ftpThr)
221     {
222       ftp_disconnectEvent *ftpDisconnect=new ftp_disconnectEvent();
223       QApplication::postEvent(ftpThr,ftpDisconnect);
224       busy=false;
225     }
226 }
227 
228 
slotThreadFinished()229 void ftpFunctions::slotThreadFinished()
230 {
231 //  qDebug() << "slotThreadFished" << thread->objectName();
232   thread->deleteLater();
233 //  ftpThr=NULL;
234   busy=false;
235 }
236 
slotDownloadFinished(bool err,QString filename)237 void ftpFunctions::slotDownloadFinished(bool err,QString filename)
238 {
239   emit downloadDone(err,filename);
240 }
241 
slotListingFinished(bool err)242 void ftpFunctions::slotListingFinished(bool err)
243 {
244   int i;
245   QList <QUrlInfo> users;
246   if(mremoveCmd)
247     {
248       users=getListing();
249       for(i=0;i<users.count();i++)
250         {
251           remove(users.at(i).name(),false,false);
252         }
253       //     disconnectFtp();
254     }
255   emit listingDone(err);
256 }
257 
258 
259 
260 
261 
getListing()262 QList<QUrlInfo> ftpFunctions::getListing()
263 {
264   return ftpThr->getList();
265 }
266 
267 
slotCommandsDone(int err,QString errStr)268 void ftpFunctions::slotCommandsDone(int err,QString errStr)
269 {
270   displayMBoxEvent *stmb;
271   lastError=(eftpError) err;
272   lastErrorStr=errStr;
273   endOfCommands=true;
274   addToLog("slotDone",LOGFTPFUNC);
275   if(err)
276     {
277       stmb= new displayMBoxEvent("FTP Error", QString("Host %1\n%2").arg(host).arg(errStr));
278       QApplication::postEvent( dispatcherPtr, stmb );
279     }
280 }
281 
checkWait(bool wait)282 void ftpFunctions::checkWait(bool wait)
283 {
284   if(wait)
285     {
286       addToLog("start ftp wait",LOGFTPFUNC);
287       while(!endOfCommands && busy)
288         {
289           qApp->processEvents();
290         }
291       addToLog(QString("ftp wait done %1 %2").arg(endOfCommands).arg(busy),LOGFTPFUNC);
292     }
293 }
294