class UploadWorker : public QThread{ Q_OBJECTpublic: UploadWorker(QWidget *parent, QStringList *FileNameList, QFtp *ftp);protected: void run();private: FtpDialog *parent; QStringList *FileNameList; QFtp *ftp;};void UploadWorker::run(){ int i; QFile *file; QFileInfo fi; for (i=0;i<FileNameList->count();i++){// Ждем окончание предыдущей закачки, если вдруг не закончилась while (parent->upl_started) msleep(100); file = new QFile(FileNameList->at(i)); fi.setFile(FileNameList->at(i)); if (file->open(QIODevice::ReadOnly)) ftp->put(file,fi.fileName());// Ждем окончание закачки и удаляем объект на который сслается file while (parent->upl_started) msleep(100); file->close(); delete file; }}
ftpCommandStarted(int commandId){ if (ftp->currentCommand() == QFtp::Put) { printf("!!!ftpCommandStarted\n"); parent->upl_started=true; }}ftpCommandFinished(int commandId, bool error){ if (ftp->currentCommand() == QFtp::Put) { printf("Downloaded file to current directory \n"); parent->upl_started=false; }}
class FtpDialog : public QDialog, private Ui::FtpFrame{ Q_OBJECTpublic: FtpDialog(QWidget *parent = 0); bool upl_started; QFtp *ftp;private slots: void connectOrDisconnect(); void ftpCommandFinished(int commandId, bool error); void ftpCommandStarted(int commandId); void updateDataTransferProgress(qint64 readBytes, qint64 totalBytes); void upload();private: class UploadWorker *up;};class UploadWorker : public QThread{ Q_OBJECTpublic: UploadWorker(FtpDialog *parent, QStringList *FileNameList, QFtp *ftp);protected: void run();private: FtpDialog *parent; QStringList *FileNameList; QFtp *ftp;};
void FtpDialog::ftpCommandStarted(int commandId){ if (ftp->currentCommand() == QFtp::Put) { printf("ftpCommandStarted\n"); upl_started=true; }}void FtpDialog::ftpCommandFinished(int commandId, bool error){ if (ftp->currentCommand() == QFtp::Put) { if (error) QMessageBox::information(this, tr("FTP"),"error upload"); else { QMessageBox::information(this, tr("FTP"),"Downloaded file to the current ftp directory"); upl_started=false; } }}void FtpDialog::connectOrDisconnect(){ if (ftp) { ftp->abort(); ftp->deleteLater(); ftp = 0; return; } ftp = new QFtp(this); connect(ftp, SIGNAL(commandFinished(int, bool)), this, SLOT(ftpCommandFinished(int, bool))); connect(ftp, SIGNAL(commandStarted(int)), this, SLOT(ftpCommandStarted(int))); ftp->login();}void FtpDialog::upload(){ QStringList *fl = new QStringList();// заполняем fl строками - пути к файлам которые надо загрузить по ftp for (int i=0; i< files_list->count(); i++) fl->append(files_list->item(i)->text());// создаем тред up = new UploadWorker(this,fl,ftp);// и запускаем его up->start();}
UploadWorker::UploadWorker(FtpDialog *parent, QStringList *FileNameList, QFtp *ftp){ this->parent=parent; this->FileNameList= new QStringList(*FileNameList); this->ftp=ftp;}void UploadWorker::run(){ int i; QFile *file; QFileInfo fi; ftp->setTransferMode(QFtp::Passive); for (i=0;i<FileNameList->count();i++){// если закачка еще не завершилась то ждем while (parent->upl_started){ msleep(100); printf("while1"); } file = new QFile(FileNameList->at(i)); fi.setFile(FileNameList->at(i)); if (file->open(QIODevice::ReadOnly)){ printf("file opened. Transfer started\nFile is: %s\n",file->fileName().toLocal8Bit().constData()); printf("ftp state is: %d\n", ftp->state()); printf("ftp filename is: %s\n", fi.fileName().toLocal8Bit().constData()); ftp->put(file,fi.fileName()); } else { printf("file open error.\n"); }// ждем завершения закачки while (parent->upl_started){ msleep(100); printf("while2"); } file->close(); delete file; }}
file opened. Transfer startedFile is: E:/110tsd.jpgftp state is: 4ftp filename is: 110tsd.jpgfile opened. Transfer startedFile is: E:/110tsd_.jpgftp state is: 4ftp filename is: 110tsd_.jpgfile opened. Transfer startedFile is: E:/110tsd_retro.jpgftp state is: 4ftp filename is: 110tsd_retro.jpgfile opened. Transfer startedFile is: E:/ava.jpgftp state is: 4ftp filename is: ava.jpg