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