forked from len0rd/rockbox
Extend HttpGet class to allow downloading a file to a buffer instead of a file.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@14347 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
cacd382963
commit
07e4ddb79d
2 changed files with 68 additions and 20 deletions
|
@ -7,7 +7,7 @@
|
||||||
* \/ \/ \/ \/ \/
|
* \/ \/ \/ \/ \/
|
||||||
*
|
*
|
||||||
* Copyright (C) 2007 by Dominik Riebeling
|
* Copyright (C) 2007 by Dominik Riebeling
|
||||||
* $Id:$
|
* $Id$
|
||||||
*
|
*
|
||||||
* All files in this archive are subject to the GNU General Public License.
|
* All files in this archive are subject to the GNU General Public License.
|
||||||
* See the file COPYING in the source tree root for full license agreement.
|
* See the file COPYING in the source tree root for full license agreement.
|
||||||
|
@ -27,12 +27,25 @@
|
||||||
HttpGet::HttpGet(QObject *parent)
|
HttpGet::HttpGet(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
{
|
{
|
||||||
|
qDebug() << "--> HttpGet::HttpGet()";
|
||||||
outputFile = new QFile(this);
|
outputFile = new QFile(this);
|
||||||
|
outputFile->setFileName("");
|
||||||
|
getRequest = -1;
|
||||||
connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
|
connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
|
||||||
connect(&http, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
|
connect(&http, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
|
||||||
connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
|
connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
|
||||||
connect(&http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
|
connect(&http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
|
||||||
|
connect(&http, SIGNAL(stateChanged(int)), this, SLOT(httpState(int)));
|
||||||
|
//connect(&http, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
|
||||||
|
|
||||||
|
connect(&http, SIGNAL(readyRead(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QByteArray HttpGet::readAll()
|
||||||
|
{
|
||||||
|
return dataBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,6 +54,7 @@ QHttp::Error HttpGet::error()
|
||||||
return http.error();
|
return http.error();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void HttpGet::httpProgress(int read, int total)
|
void HttpGet::httpProgress(int read, int total)
|
||||||
{
|
{
|
||||||
emit dataReadProgress(read, total);
|
emit dataReadProgress(read, total);
|
||||||
|
@ -64,7 +78,8 @@ void HttpGet::setFile(QFile *file)
|
||||||
void HttpGet::abort()
|
void HttpGet::abort()
|
||||||
{
|
{
|
||||||
http.abort();
|
http.abort();
|
||||||
outputFile->close();
|
if(!outputFile->fileName().isEmpty());
|
||||||
|
outputFile->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -84,45 +99,61 @@ bool HttpGet::getFile(const QUrl &url)
|
||||||
qDebug() << "Error: URL has no path" << endl;
|
qDebug() << "Error: URL has no path" << endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
// if no output file was set write to buffer
|
||||||
QString localFileName = outputFile->fileName();
|
if(!outputFile->fileName().isEmpty()) {
|
||||||
if (localFileName.isEmpty())
|
if (!outputFile->open(QIODevice::ReadWrite)) {
|
||||||
outputFile->setFileName(QFileInfo(url.path()).fileName());
|
qDebug() << "Error: Cannot open " << qPrintable(outputFile->fileName())
|
||||||
|
<< " for writing: " << qPrintable(outputFile->errorString())
|
||||||
if (!outputFile->open(QIODevice::ReadWrite)) {
|
<< endl;
|
||||||
qDebug() << "Error: Cannot open " << qPrintable(outputFile->fileName())
|
return false;
|
||||||
<< " for writing: " << qPrintable(outputFile->errorString())
|
}
|
||||||
<< endl;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
qDebug() << "starting download to " << qPrintable(outputFile->fileName());
|
||||||
http.setHost(url.host(), url.port(80));
|
http.setHost(url.host(), url.port(80));
|
||||||
http.get(url.path(), outputFile);
|
if(outputFile->fileName().isEmpty()) {
|
||||||
|
qDebug() << "downloading to buffer";
|
||||||
|
getRequest = http.get(QString(url.toEncoded()));
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
qDebug() << "downloading to file";
|
||||||
|
getRequest = http.get(QString(url.toEncoded()), outputFile);
|
||||||
|
}
|
||||||
|
qDebug() << "request scheduled: GET" << getRequest;
|
||||||
|
|
||||||
http.close();
|
http.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void HttpGet::httpDone(bool error)
|
void HttpGet::httpDone(bool error)
|
||||||
{
|
{
|
||||||
|
qDebug() << "bytesAvailable =" << http.bytesAvailable();
|
||||||
if (error) {
|
if (error) {
|
||||||
qDebug() << "Error: " << qPrintable(http.errorString()) << endl;
|
qDebug() << "Error: " << qPrintable(http.errorString()) << endl;
|
||||||
} else {
|
} else {
|
||||||
qDebug() << "File downloaded as " << qPrintable(outputFile->fileName())
|
qDebug() << "File downloaded as " << qPrintable(outputFile->fileName())
|
||||||
<< endl;
|
<< endl;
|
||||||
}
|
}
|
||||||
outputFile->close();
|
if(!outputFile->fileName().isEmpty())
|
||||||
|
outputFile->close();
|
||||||
|
|
||||||
emit done(error);
|
emit done(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void HttpGet::httpFinished(int id, bool error)
|
void HttpGet::httpFinished(int id, bool error)
|
||||||
{
|
{
|
||||||
qDebug() << "HttpGet::httpFinished";
|
qDebug() << "HttpGet::httpFinished(int, bool) =" << id << error;
|
||||||
qDebug() << "id:" << id << "error:" << error;
|
if(id == getRequest) dataBuffer = http.readAll();
|
||||||
emit requestFinished(id, error);
|
emit requestFinished(id, error);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HttpGet::httpStarted(int id)
|
||||||
|
{
|
||||||
|
qDebug() << "HttpGet::httpStarted(int) =" << id;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
QString HttpGet::errorString()
|
QString HttpGet::errorString()
|
||||||
{
|
{
|
||||||
|
@ -132,7 +163,8 @@ QString HttpGet::errorString()
|
||||||
|
|
||||||
void HttpGet::httpResponseHeader(const QHttpResponseHeader &resp)
|
void HttpGet::httpResponseHeader(const QHttpResponseHeader &resp)
|
||||||
{
|
{
|
||||||
qDebug() << "HttpGet::httpResponseHeader()" << resp.statusCode();
|
// if there is a network error abort all scheduled requests for
|
||||||
|
// this download
|
||||||
response = resp.statusCode();
|
response = resp.statusCode();
|
||||||
if(response != 200) http.abort();
|
if(response != 200) http.abort();
|
||||||
}
|
}
|
||||||
|
@ -142,3 +174,14 @@ int HttpGet::httpResponse()
|
||||||
{
|
{
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void HttpGet::httpState(int state)
|
||||||
|
{
|
||||||
|
QString s[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
|
||||||
|
"Reading", "Connected", "Closing"};
|
||||||
|
if(state <= 6)
|
||||||
|
qDebug() << "HttpGet::httpState() = " << s[state];
|
||||||
|
else qDebug() << "HttpGet::httpState() = " << state;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
* \/ \/ \/ \/ \/
|
* \/ \/ \/ \/ \/
|
||||||
*
|
*
|
||||||
* Copyright (C) 2007 by Dominik Riebeling
|
* Copyright (C) 2007 by Dominik Riebeling
|
||||||
* $Id:$
|
* $Id$
|
||||||
*
|
*
|
||||||
* All files in this archive are subject to the GNU General Public License.
|
* All files in this archive are subject to the GNU General Public License.
|
||||||
* See the file COPYING in the source tree root for full license agreement.
|
* See the file COPYING in the source tree root for full license agreement.
|
||||||
|
@ -39,6 +39,7 @@ class HttpGet : public QObject
|
||||||
QString errorString(void);
|
QString errorString(void);
|
||||||
void setFile(QFile*);
|
void setFile(QFile*);
|
||||||
int httpResponse(void);
|
int httpResponse(void);
|
||||||
|
QByteArray readAll(void);
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void abort(void);
|
void abort(void);
|
||||||
|
@ -53,11 +54,15 @@ class HttpGet : public QObject
|
||||||
void httpProgress(int, int);
|
void httpProgress(int, int);
|
||||||
void httpFinished(int, bool);
|
void httpFinished(int, bool);
|
||||||
void httpResponseHeader(const QHttpResponseHeader&);
|
void httpResponseHeader(const QHttpResponseHeader&);
|
||||||
|
void httpState(int);
|
||||||
|
void httpStarted(int);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QHttp http;
|
QHttp http;
|
||||||
QFile *outputFile;
|
QFile *outputFile;
|
||||||
int response;
|
int response;
|
||||||
|
int getRequest;
|
||||||
|
QByteArray dataBuffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue