(hopefully) fix some broken download stuff I introduced yesterday.
[Rockbox.git] / rbutil / rbutilqt / httpget.cpp
blobe54df73c3f2af99d6b2d9e4bb3e3c2b162e0a562
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2007 by Dominik Riebeling
10 * $Id$
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <QtCore>
21 #include <QtNetwork>
22 #include <QtDebug>
24 #include "httpget.h"
27 HttpGet::HttpGet(QObject *parent)
28 : QObject(parent)
30 qDebug() << "--> HttpGet::HttpGet()";
31 outputToBuffer = true;
32 getRequest = -1;
33 connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
34 connect(&http, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
35 connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
36 connect(&http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
37 connect(&http, SIGNAL(stateChanged(int)), this, SLOT(httpState(int)));
38 connect(&http, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
40 connect(&http, SIGNAL(readyRead(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
45 QByteArray HttpGet::readAll()
47 return dataBuffer;
51 QHttp::Error HttpGet::error()
53 return http.error();
57 void HttpGet::httpProgress(int read, int total)
59 emit dataReadProgress(read, total);
63 void HttpGet::setProxy(const QUrl &proxy)
65 qDebug() << "HttpGet::setProxy" << proxy.toString();
66 http.setProxy(proxy.host(), proxy.port(), proxy.userName(), proxy.password());
70 void HttpGet::setFile(QFile *file)
72 outputFile = file;
73 outputToBuffer = false;
74 qDebug() << "HttpGet::setFile" << outputFile->fileName();
78 void HttpGet::abort()
80 http.abort();
81 if(!outputToBuffer)
82 outputFile->close();
86 bool HttpGet::getFile(const QUrl &url)
88 if (!url.isValid()) {
89 qDebug() << "Error: Invalid URL" << endl;
90 return false;
93 if (url.scheme() != "http") {
94 qDebug() << "Error: URL must start with 'http:'" << endl;
95 return false;
98 if (url.path().isEmpty()) {
99 qDebug() << "Error: URL has no path" << endl;
100 return false;
102 // if no output file was set write to buffer
103 if(!outputToBuffer) {
104 if (!outputFile->open(QIODevice::ReadWrite)) {
105 qDebug() << "Error: Cannot open " << qPrintable(outputFile->fileName())
106 << " for writing: " << qPrintable(outputFile->errorString())
107 << endl;
108 return false;
111 http.setHost(url.host(), url.port(80));
112 if(outputToBuffer) {
113 qDebug() << "downloading to buffer:" << QString(url.toEncoded());
114 getRequest = http.get(QString(url.toEncoded()));
116 else {
117 qDebug() << "downloading to file:" << QString(url.toEncoded()) << qPrintable(outputFile->fileName());
118 getRequest = http.get(QString(url.toEncoded()), outputFile);
120 qDebug() << "request scheduled: GET" << getRequest;
122 http.close();
123 return true;
127 void HttpGet::httpDone(bool error)
129 if (error) {
130 qDebug() << "Error: " << qPrintable(http.errorString()) << endl;
132 if(!outputToBuffer) {
133 outputFile->close();
134 qDebug() << "File downloaded as" << qPrintable(outputFile->fileName());
136 else qDebug() << "file downloaded to buffer";
138 emit done(error);
142 void HttpGet::httpFinished(int id, bool error)
144 qDebug() << "HttpGet::httpFinished(int, bool) =" << id << error;
145 if(id == getRequest) dataBuffer = http.readAll();
146 qDebug() << "pending:" << http.hasPendingRequests();
147 //if(!http.hasPendingRequests()) httpDone(error);
148 emit requestFinished(id, error);
152 void HttpGet::httpStarted(int id)
154 qDebug() << "HttpGet::httpStarted(int) =" << id;
158 QString HttpGet::errorString()
160 return http.errorString();
164 void HttpGet::httpResponseHeader(const QHttpResponseHeader &resp)
166 // if there is a network error abort all scheduled requests for
167 // this download
168 response = resp.statusCode();
169 if(response != 200) http.abort();
173 int HttpGet::httpResponse()
175 return response;
179 void HttpGet::httpState(int state)
181 QString s[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
182 "Reading", "Connected", "Closing"};
183 if(state <= 6)
184 qDebug() << "HttpGet::httpState() = " << s[state];
185 else qDebug() << "HttpGet::httpState() = " << state;