salmanmanekia
23rd February 2008, 12: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()
{
}
/* 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()
{
}