PDA

View Full Version : QFtp...doesnt seems to work ....!!!



salmanmanekia
23rd February 2008, 13:45
hey guys....i have written this piece of code for a QFtp application,but this doesnt seem to work....i dont know whr it goes wrong,but it makes a file with 0 byte.......pls,pls any help wud be appreciated....


/* main method */

#include <QtGui/QApplication>
#include <QApplication>
#include "exftp.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
exFtp w;

w.show();
//a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
return a.exec();
}


/* .h File */

#ifndef EXFTP_H
#define EXFTP_H

#include <QtGui/QMainWindow>
#include <QFtp>
#include <QUrl>
#include <QFile>
#include <QDialog>
#include <QString>
class QLineEdit;
class QLabel;
class QPushButton;
class QDialogButtonBox;
class QProgressDialog;
//#include "ui_exftp.h"

class exFtp : public QDialog
{
Q_OBJECT

public:
exFtp();
~exFtp();
private:
QFtp *ftp;
QFile file;
QUrl *urlAdd;
QString localFileName;
QLineEdit *ftpServerLineEdit;
QLabel *ftpServerLabel;
QLabel *statusLabel;
QPushButton *downloadButton;
QPushButton *quitButton;
QDialogButtonBox *buttonBox;
QProgressDialog *progressDialog;
//Ui::exFtpClass ui;
private slots:
bool downloadStarted();
void EnabledButton(const QString&);
void ftpDone(bool);
void On_CommandFinished();
};

#endif // EXFTP_H


/* .cpp file */

#include <QtGui>
#include "exftp.h"

exFtp::exFtp()
{
ftpServerLabel = new QLabel("FTP");
ftpServerLineEdit = new QLineEdit();
statusLabel = new QLabel();
downloadButton = new QPushButton("Download");
downloadButton->setEnabled(false);
quitButton = new QPushButton("Quit");
buttonBox = new QDialogButtonBox;
buttonBox->addButton(downloadButton,QDialogButtonBox::ActionR ole);
buttonBox->addButton(quitButton,QDialogButtonBox::RejectRole) ;
progressDialog = new QProgressDialog(this);
QHBoxLayout *topLayout = new QHBoxLayout;
topLayout->addWidget(ftpServerLabel);
topLayout->addWidget(ftpServerLineEdit);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(topLayout);
mainLayout->addWidget(statusLabel);
mainLayout->addWidget(buttonBox);
setLayout(mainLayout);
ftp = new QFtp(this);
connect(ftpServerLineEdit,SIGNAL(textEdited(const QString&)),this,SLOT(EnabledButton(const QString&)));
// connect(ftpServerLineEdit,SIGNAL(textEdited(const QString&)),this,SLOT(QUrl(const QString&)))
connect(downloadButton,SIGNAL(clicked()),this,SLOT (downloadStarted()));
connect(ftp,SIGNAL(done(bool)),this,SLOT(ftpDone(b ool)));
connect(ftp,SIGNAL(commandFinished(int,bool)),this ,SLOT(On_CommandFinished()));
connect(quitButton,SIGNAL(clicked()),this,SLOT(acc ept()));
//ui.setupUi(this);
}
void exFtp::EnabledButton(const QString& Url)
{
downloadButton->setEnabled(!(Url.isEmpty()));
urlAdd = new QUrl();
urlAdd->setUrl(Url);
urlAdd->setPath(Url);
QFileInfo fi(Url);
localFileName = fi.fileName();
if(localFileName.isEmpty())
localFileName = "exFtp.out";

}
void exFtp::On_CommandFinished()
{
file.close();
}
bool exFtp::downloadStarted()
{
file.setFileName(localFileName);
//file.open(QIODevice::WriteOnly);
if(!file.open(QIODevice::WriteOnly))
{
statusLabel->setEnabled(true);
statusLabel->setText(tr("File at %1 cannot be Open/Accesed").arg(localFileName));
return false;
}
ftp->connectToHost(urlAdd->host(),urlAdd->port(21));
ftp->login();
ftp->get(urlAdd->path(),&file);
//ftp->close();
return true;
}
void exFtp::ftpDone(bool fileDone)
{
if(fileDone)
statusLabel->setText("DownLoaded");
else
statusLabel->setText("no,..!!");
}

exFtp::~exFtp()
{

}

codeslicer
23rd February 2008, 15:02
Well, I'm relatively new to Qt, but did you check the FTP example (http://doc.trolltech.com/latest/network-ftp.html)? :confused:

salmanmanekia
24th February 2008, 04:10
yup...i have seen the example....nd taken all possible clues from it....but no use....still the prob. is there......

wysota
24th February 2008, 13:28
Either monitor the network traffic or the state of the QFtp object and see at which point the execution stops - see if the component connects to the host, logins, etc. At some point it probably fails to do its task but you don't notice it.

salmanmanekia
25th February 2008, 00:03
yup,u r rite,the execution is not proper ....but i think thr is problem in code,m nt sure if QFtp is used properly in the code....
i cannt undestand what u mean by monitoring traffic..?...nd can u tell me a reliable site....which if provided downloads a file from it...

wysota
25th February 2008, 00:06
yup,u r rite,the execution is not proper ....but i think thr is problem in code,m nt sure if QFtp is used properly in the code....

Please use proper sentences instead of that awful sms slang.


i cannt undestand what u mean by monitoring traffic..?

I mean see what goes through your network interface card. There are many networking sniffer tools that do this - just search the net.

salmanmanekia
25th February 2008, 00:17
Thanks for the advice...
I am not sure about network sniffer,is it realy necessary and dont you see any problem with the code....what i understand is that if code is fine then it should work and vice versa....aint it...!!

wysota
25th February 2008, 00:21
I am not sure about network sniffer,is it realy necessary
No, it's not necessary - you can go for the other approach I suggested. But the sniffer will show you what exactly happens during the connection (and if there is a connection at all). To monitor the state of the object you'll need to write some debugging code and deduce the problem from received output.


dont you see any problem with the code...
I don't see anything obvious.


what i understand is that if code is fine then it should work and vice versa
Then you understand incorrectly. The code might be fine but for example your personal firewall might be blocking the connection.

salmanmanekia
25th February 2008, 01:01
Worked the Program with Network sniffer,it doesnt detect any Incoming /Outgoing FTP packets,but the firewall i have installed asks me for the connection to be established when i run the program.....the firewall is of panda.....just for the sake of confirmation the address which i provide in FTP address bar is "ftp.trolltech.com"....is this right...??

wysota
25th February 2008, 08:58
Worked the Program with Network sniffer,it doesnt detect any Incoming /Outgoing FTP packets
How about any packets going to ftp.trolltech.com? Can you connect to ftp.trolltech.com using your regular ftp client? Does the sniffer report the traffic on ftp ports then?

salmanmanekia
25th February 2008, 09:30
Can you connect to ftp.trolltech.com using your regular ftp client?

Yup,the ftp address works fine.