PDA

View Full Version : QFTP connection problem



c3po4
12th January 2011, 20:54
Hi guys,

I am working on an FTP Client that is searching in the current directory for other directories.
When he founds some, he will try to download 2 special files inside these directories.

My problem is, the client is doing well as far as hes choosing the right folders, creating them and also trying to get all the files.

But somewhere, I dont know where he stops downloading the files.
So some Files that were queued wont download and the rest of the files were almost loaded but only almost (~5 kbyte are still missing).

I have no Idea where my mistake could be.

Sorry for my english, i am from Germany.
Can you pls have a look at my source Code? Its just an fragment of my whole program.

header

#ifndef FTPCONNECT_H
#define FTPCONNECT_H

#include <QtGui>
#include <QtNetwork>
#include <QByteArray>
#include <QtGui/QMainWindow>

#include "deviceroutine.h"

using namespace std;

class FTPconnect : DeviceRoutine
{
Q_OBJECT
public:
FTPconnect(Ui::DeviceRoutineClass *,QWidget *parent = 0, Qt::WFlags flags = 0);
~FTPconnect();

QProgressDialog *progress;
QFtp *ftpzeiger;
QFile *file;
bool abgebrochen;

private:
//Zeiger auf die UI der DeviceRoutine
Ui::DeviceRoutineClass* ui;
//Array für alle Verzeichnisse des FTP
vector <QString> directorylist;
int dircount;

public slots:
void ftp_start();
void ftp_ende();
void ftp_progress(qint64 done,qint64 total);
void ftp_CommandFinished(int, bool error);
void ftp_CommandStarted(int id);
void ftp_abort();

void getfiles(const QUrlInfo &);
void getdirectories(const QUrlInfo &);
void getdirectories_ende();
};

#endif

... cpp


#include "FTPconnect.h"
#include "deviceroutine.h"
#include <QMessageBox>

FTPconnect::FTPconnect(Ui::DeviceRoutineClass *ui,QWidget *parent, Qt::WFlags flags) : DeviceRoutine(parent, flags){

//Zeiger auf UI setzen
this->ui=ui;
file=NULL;
dircount=0;
}

FTPconnect::~FTPconnect(){}

void FTPconnect::ftp_start(){

ui->textEdit_ausgabe_FTP->setText("");

abgebrochen=false;

if((is_directory(ui->lineEdit_FTP_pfad->text().toStdString())==0) && (ui->lineEdit_FTP_pfad->isEnabled()==1)){
//Dann breche die Aktion ab und wähle neuen Pfad
ui->textEdit_ausgabe_FTP->append("------------------------------------------------------------------------------------\n") ;
ui->textEdit_ausgabe_FTP->append("Kein gueltiger FTP Ausgabepfad angegeben! Bitte aendern...\n");
ui->textEdit_ausgabe_FTP->append("------------------------------------------------------------------------------------\n");
//wähle neuen Pfad
return;
}

progress= new QProgressDialog("Analysiere Verzeichnisstruktur...", "Abbrechen", 0, 100, this);
progress->setAutoReset(0);

//Ftp bereistellen
ftpzeiger = new QFtp();
ftpzeiger->setTransferMode(QFtp::Active);

connect(ftpzeiger, SIGNAL(done(bool)), this, SLOT(getdirectories_ende()));
connect(ftpzeiger, SIGNAL(dataTransferProgress(qint64, qint64)), this, SLOT(ftp_progress(qint64,qint64)));
connect(ftpzeiger, SIGNAL(commandStarted(int)),this, SLOT(ftp_CommandStarted(int)));
connect(ftpzeiger, SIGNAL(commandFinished(int,bool)),this, SLOT(ftp_CommandFinished(int,bool)));

//Hole nur Verzeichnisse
connect(ftpzeiger, SIGNAL(listInfo(QUrlInfo)),this, SLOT(getdirectories(QUrlInfo)));

//Host aus dem Lineedit holen
this->ftpzeiger->connectToHost(ui->lineEdit_host->text());

//Logindaten - Wenn beide Felder leer --> anonymous Login
if ((ui->lineEdit_login->text())=="" && (ui->lineEdit_passwort->text()==""))
ftpzeiger->login();
else
ftpzeiger->login(ui->lineEdit_login->text(),ui->lineEdit_passwort->text());

if (ui->lineEdit_verzeichnis->text()==0){
ui->textEdit_ausgabe_FTP->append("Bitte ein Verzeichnis angeben...");
this->~FTPconnect();
return;
}
//Verzeichnis auswählen
ftpzeiger->cd(ui->lineEdit_verzeichnis->text());

//Directory Listing
ftpzeiger->list();
//Event Schleife
progress->exec();

if(directorylist.size()==0)
ui->textEdit_ausgabe_FTP->append("Keine gueltigen Dateien gefunden...\n");

//Events für Ende und Datenübertragung
connect(ftpzeiger, SIGNAL(done(bool)), this, SLOT(ftp_ende()));


//Trenne nicht mehr benötigte Signale
disconnect(ftpzeiger, SIGNAL(done(bool)), this, SLOT(getdirectories_ende()));
disconnect(ftpzeiger, SIGNAL(listInfo(QUrlInfo)),this, SLOT(getdirectories(QUrlInfo)));

//Hole nur noch Dateien
connect(ftpzeiger, SIGNAL(listInfo(QUrlInfo)),this, SLOT(getfiles(QUrlInfo)));

//Für jede gefundenen Ordner
for(dircount=0;dircount<directorylist.size();dircount++){
progress= new QProgressDialog("Kopiere Dateien...", "Abbrechen", 0, 100, this);
//connect(progress, SIGNAL(canceled()), this, SLOT(ftp_abort()));

//Verzeichnis und File auswählen
ftpzeiger->cd(ui->lineEdit_verzeichnis->text() + "/" + directorylist[dircount]);

//Dateien werden gesucht - intern
ftpzeiger->list();

ui->textEdit_ausgabe_FTP->append("Verzeichnis:" + directorylist[dircount]);
progress->exec();
}
}

void FTPconnect::ftp_ende(){
if (abgebrochen==1){
ui->textEdit_ausgabe_FTP->append("\nDownload abgebrochen!");
}
if(ftpzeiger->error()==0)
ui->textEdit_ausgabe_FTP->append("Datei erfolgreich heruntergeladen!");
else
ui->textEdit_ausgabe_FTP->append(this->ftpzeiger->errorString());

//Aufräumen
this->progress->close();
this->progress->deleteLater();
this->progress=NULL;
}

//setze DL Fortschritt in Progressbar um
void FTPconnect::ftp_progress(qint64 done,qint64 total){
this->progress->setMaximum(total);
this->progress->setValue(done);
}

void FTPconnect::ftp_CommandFinished(int, bool error){

if (ftpzeiger->currentCommand() == QFtp::ConnectToHost) {
ui->textEdit_ausgabe_FTP->append("Verbindung zum Host aufgebaut...\n");
if (error){
ui->textEdit_ausgabe_FTP->append(ftpzeiger->errorString ());
this->progress->close();
}
}

if (ftpzeiger->currentCommand() == QFtp::Login){
ui->textEdit_ausgabe_FTP->append("Logindaten ueberprüft...\n");
if (error){
ui->textEdit_ausgabe_FTP->append(ftpzeiger->errorString ());
this->progress->close();
}
}

if (ftpzeiger->currentCommand() == QFtp::Get) {

ui->textEdit_ausgabe_FTP->append("Datenuebertragung beendet...\n");
if (error) {
ui->textEdit_ausgabe_FTP->append(ftpzeiger->errorString ());
ftpzeiger->abort();
this->progress->close();
file->close();
file->remove();
}
}

if (ftpzeiger->currentCommand() == QFtp::List) {
ui->textEdit_ausgabe_FTP->append("Verzeichnisse eingelesen...\n");
if (error) {
ui->textEdit_ausgabe_FTP->append(ftpzeiger->errorString ());
ftpzeiger->abort();
this->progress->close();
}
}
}

void FTPconnect::ftp_CommandStarted(int id){

if (ftpzeiger->currentCommand() == QFtp::ConnectToHost)
ui->textEdit_ausgabe_FTP->append("Verbindung zum Host wird aufgebaut...");

if (ftpzeiger->currentCommand() == QFtp::Login)
ui->textEdit_ausgabe_FTP->append("Logindaten werden ueberprüft...");

if (ftpzeiger->currentCommand() == QFtp::Get)
ui->textEdit_ausgabe_FTP->append("Datenuebertragung gestartet...");

if (ftpzeiger->currentCommand() == QFtp::List)
ui->textEdit_ausgabe_FTP->append("Lese Verzeichniss ein...");
}

void FTPconnect::ftp_abort(){
this->ftpzeiger->abort();
this->progress->close();
abgebrochen=1;

}

void FTPconnect::getfiles(const QUrlInfo &urlInfo){
//Wenn devlist oder devparm ist
if ((urlInfo.isDir()==false) && ((urlInfo.name()=="devList.dat") || (urlInfo.name()=="devParm.dat"))){

if (ui->radioButton_FTP_eigen->isChecked()){
//Wenn der Rezeptordner nicht existiert,lege ihn an
if(is_directory(ui->lineEdit_FTP_pfad->text().toStdString() + "\\" + directorylist[dircount].toStdString())==false)
create_directory(ui->lineEdit_FTP_pfad->text().toStdString() + "\\" + directorylist[dircount].toStdString());

progress->setLabelText("Downloade " + directorylist[dircount] + "...");
ui->textEdit_ausgabe_FTP->append("Downloade " + urlInfo.name() + "...");

//File zum schreiben/lesen öffnen
file = new QFile(ui->lineEdit_FTP_pfad->text() + "\\" + directorylist[dircount] + "\\" + urlInfo.name() );
file->open(QIODevice::WriteOnly);

ftpzeiger->get(urlInfo.name(),file);
}
//Standartausgabe nach FTP
else{
//Wenn FTP noch nicht existiert, lege es an
if(is_directory("FTP")==false)
create_directory("FTP");

//Wenn der Rezeptordner nicht existiert,lege ihn an
if(is_directory("FTP\\" + directorylist[dircount].toStdString())==false)
create_directory("FTP\\" + directorylist[dircount].toStdString());

progress->setLabelText("Downloade " + directorylist[dircount] + "...");
ui->textEdit_ausgabe_FTP->append("Downloade " + urlInfo.name() + "...");
/* Hier später auf DevList und DevParm prüfen */

//File zum schreiben/lesen öffnen
file = new QFile("FTP\\" + directorylist[dircount] + "\\" + urlInfo.name() );
file->open(QIODevice::ReadWrite);

ftpzeiger->get(urlInfo.name(),file);

}
}
}

void FTPconnect::getdirectories(const QUrlInfo &urlInfo){

//Wenn Verzeichnis ist (aber kein . oder ..)
if ((urlInfo.isDir()==true) && (urlInfo.name()!=".") && (urlInfo.name()!=".."))
directorylist.push_back(urlInfo.name());
}

void FTPconnect::getdirectories_ende(){

//Aufräumen
this->progress->close();
this->progress=NULL;

}


Just ignore my German comments ... ;)

I am happy if you could give me some other advices, still learning.


thx a lot,
c3po4