I've written a class that will handle file downloads. I think my logic is ok but the program isn't working as expected. The program downloads the first file successfully but hangs with the second download. I've gone through my written code over and over again but can't find the logical error.
I've maintained one QHttp and one QFile object in the class and scheduled one GET request at a time. The idea has been taken from the book - C++ GUi programming with Qt.
Please help me in finding out the logical error.
myhttp.h
#ifndef MYHTTP_H
#define MYHTTP_H
#include <QObject>
{
Q_OBJECT
public:
void get(const QString& strFullFilePath);
signals:
void done();
private slots:
void myHttpRequestDone(bool error);
private:
void processNextRequest();
};
#endif
#ifndef MYHTTP_H
#define MYHTTP_H
#include <QObject>
class QHttp;
class QFile;
class MyHttp : public QObject
{
Q_OBJECT
public:
MyHttp(QObject* parent = 0);
void get(const QString& strFullFilePath);
signals:
void done();
private slots:
void myHttpRequestDone(bool error);
private:
void processNextRequest();
QHttp* http;
QFile* file;
QStringList* pendingRequestList;
};
#endif
To copy to clipboard, switch view to plain text mode
myhttp.cpp :
#include <QtNetwork>
#include "myhttp.h"
{
file = 0;
connect(http, SIGNAL(done(bool)), this, SLOT(myHttpRequestDone(bool)));
}
void MyHttp::get(const QString& strFullFilePath)
{
// Add this request to list
pendingRequestList->append(strFullFilePath);
if (pendingRequestList->size() == 1)
{
processNextRequest();
}
}
void MyHttp::processNextRequest()
{
if (pendingRequestList->isEmpty() == true)
{
emit done();
}
else
{
// Get the first request in the list
QString strFullFilePath
= pendingRequestList
->first
();
// Extract the file name
QUrl url
(strFullFilePath
);
QString fileName
= fileInfo.
fileName();
// Create an instance of a file with the extracted name and open it
file = new QFile(fileName
);
// Send the request
http->setHost(url.host(), url.port(80));
http->get(url.path(), file);
}
}
void MyHttp::myHttpRequestDone(bool error)
{
file->close();
if (error == true)
{
file->remove();
}
delete file;
file = 0;
pendingRequestList->removeFirst();
processNextRequest();
}
#include <QtNetwork>
#include "myhttp.h"
MyHttp::MyHttp(QObject* parent /* = 0 */) : QObject(parent)
{
http = new QHttp;
file = 0;
pendingRequestList = new QStringList;
connect(http, SIGNAL(done(bool)), this, SLOT(myHttpRequestDone(bool)));
}
void MyHttp::get(const QString& strFullFilePath)
{
// Add this request to list
pendingRequestList->append(strFullFilePath);
if (pendingRequestList->size() == 1)
{
processNextRequest();
}
}
void MyHttp::processNextRequest()
{
if (pendingRequestList->isEmpty() == true)
{
emit done();
}
else
{
// Get the first request in the list
QString strFullFilePath = pendingRequestList->first();
// Extract the file name
QUrl url(strFullFilePath);
QFileInfo fileInfo(url.path());
QString fileName = fileInfo.fileName();
// Create an instance of a file with the extracted name and open it
file = new QFile(fileName);
file->open(QIODevice::WriteOnly);
// Send the request
http->setHost(url.host(), url.port(80));
http->get(url.path(), file);
}
}
void MyHttp::myHttpRequestDone(bool error)
{
file->close();
if (error == true)
{
file->remove();
}
delete file;
file = 0;
pendingRequestList->removeFirst();
processNextRequest();
}
To copy to clipboard, switch view to plain text mode
main.cpp :
#include <QCoreApplication>
#include "myhttp.h"
int main(int argc, char *argv[])
{
MyHttp* myhttp = new MyHttp();
QObject::connect(myhttp,
SIGNAL(done
()),
&app,
SLOT(quit
()));
myhttp->get("http://thedailystar.net/images/tdsmainlogo.jpg");
myhttp->get("http://thedailystar.net/photo/2008/09/06/2008-09-06__front02.jpg");
myhttp->get("http://ahmadferdous.googlepages.com/Ferdous.JPG");
return app.exec();
}
#include <QCoreApplication>
#include "myhttp.h"
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
MyHttp* myhttp = new MyHttp();
QObject::connect(myhttp, SIGNAL(done()), &app, SLOT(quit()));
myhttp->get("http://thedailystar.net/images/tdsmainlogo.jpg");
myhttp->get("http://thedailystar.net/photo/2008/09/06/2008-09-06__front02.jpg");
myhttp->get("http://ahmadferdous.googlepages.com/Ferdous.JPG");
return app.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks