Oops, part 2: I shouldn't remove an icon that is still in use. Also convert some...
[Rockbox.git] / rbutil / rbutilqt / httpget.cpp
blob7680cb2d53277daac746bbb3e751d388732dae59
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 m_usecache = false;
31 qDebug() << "--> HttpGet::HttpGet()";
32 outputToBuffer = true;
33 cached = false;
34 getRequest = -1;
35 connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
36 connect(&http, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
37 connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
38 connect(&http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
39 connect(&http, SIGNAL(stateChanged(int)), this, SLOT(httpState(int)));
40 connect(&http, SIGNAL(requestStarted(int)), this, SLOT(httpStarted(int)));
42 connect(&http, SIGNAL(readyRead(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
47 void HttpGet::setCache(QDir d)
49 m_cachedir = d;
50 bool result = true;
52 QString p = m_cachedir.absolutePath() + "/rbutil-cache";
53 if(QFileInfo(m_cachedir.absolutePath()).isDir())
54 if(!QFileInfo(p).isDir())
55 result = m_cachedir.mkdir("rbutil-cache");
56 else result = false;
57 qDebug() << "HttpGet::setCache(QDir)" << result;
58 m_usecache = !result;
62 void HttpGet::setCache(bool c)
64 m_usecache = c;
68 QByteArray HttpGet::readAll()
70 return dataBuffer;
74 QHttp::Error HttpGet::error()
76 return http.error();
80 void HttpGet::httpProgress(int read, int total)
82 emit dataReadProgress(read, total);
86 void HttpGet::setProxy(const QUrl &proxy)
88 qDebug() << "HttpGet::setProxy" << proxy.toString();
89 http.setProxy(proxy.host(), proxy.port(), proxy.userName(), proxy.password());
93 void HttpGet::setFile(QFile *file)
95 outputFile = file;
96 outputToBuffer = false;
97 qDebug() << "HttpGet::setFile" << outputFile->fileName();
101 void HttpGet::abort()
103 http.abort();
104 if(!outputToBuffer)
105 outputFile->close();
109 bool HttpGet::getFile(const QUrl &url)
111 if (!url.isValid()) {
112 qDebug() << "Error: Invalid URL" << endl;
113 return false;
116 if (url.scheme() != "http") {
117 qDebug() << "Error: URL must start with 'http:'" << endl;
118 return false;
121 if (url.path().isEmpty()) {
122 qDebug() << "Error: URL has no path" << endl;
123 return false;
125 // if no output file was set write to buffer
126 if(!outputToBuffer) {
127 if (!outputFile->open(QIODevice::ReadWrite)) {
128 qDebug() << "Error: Cannot open " << qPrintable(outputFile->fileName())
129 << " for writing: " << qPrintable(outputFile->errorString())
130 << endl;
131 return false;
134 // put hash generation here so it can get reused later
135 QString hash = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex();
136 cachefile = m_cachedir.absolutePath() + "/rbutil-cache/" + hash;
137 if(m_usecache) {
138 // check if the file is present in cache
139 qDebug() << "[HTTP] cache ENABLED for" << url.toEncoded();
140 if(QFileInfo(cachefile).isReadable() && QFileInfo(cachefile).size() > 0) {
141 qDebug() << "[HTTP] cached file found!" << cachefile;
142 getRequest = -1;
143 QFile c(cachefile);
144 if(!outputToBuffer) {
145 qDebug() << outputFile->fileName();
146 c.open(QIODevice::ReadOnly);
147 outputFile->open(QIODevice::ReadWrite);
148 outputFile->write(c.readAll());
149 outputFile->close();
150 c.close();
152 else {
153 c.open(QIODevice::ReadOnly);
154 dataBuffer = c.readAll();
155 c.close();
157 response = 200; // fake "200 OK" HTTP response
158 cached = true;
159 httpDone(false); // we're done now. This will emit the correct signal too.
160 return true;
162 else qDebug() << "[HTTP] file not cached, downloading to" << cachefile;
165 else {
166 qDebug() << "[HTTP] cache DISABLED";
168 http.setHost(url.host(), url.port(80));
169 // construct query (if any)
170 QList<QPair<QString, QString> > qitems = url.queryItems();
171 if(url.hasQuery()) {
172 query = "?";
173 for(int i = 0; i < qitems.size(); i++)
174 query += qitems.at(i).first + "=" + qitems.at(i).second + "&";
175 qDebug() << query;
178 if(outputToBuffer) {
179 qDebug() << "[HTTP] downloading to buffer:" << url.toString();
180 getRequest = http.get(url.path() + query);
182 else {
183 qDebug() << "[HTTP] downloading to file:" << url.toString() << qPrintable(outputFile->fileName());
184 getRequest = http.get(url.path() + query, outputFile);
186 qDebug() << "[HTTP] request scheduled: GET" << getRequest;
188 return true;
192 void HttpGet::httpDone(bool error)
194 if (error) {
195 qDebug() << "[HTTP] Error: " << qPrintable(http.errorString()) << httpResponse();
197 if(!outputToBuffer)
198 outputFile->close();
200 if(m_usecache && !cached) {
201 qDebug() << "[HTTP] creating cache file" << cachefile;
202 QFile c(cachefile);
203 c.open(QIODevice::ReadWrite);
204 if(!outputToBuffer) {
205 outputFile->open(QIODevice::ReadOnly | QIODevice::Truncate);
206 c.write(outputFile->readAll());
207 outputFile->close();
209 else
210 c.write(dataBuffer);
212 c.close();
214 emit done(error);
218 void HttpGet::httpFinished(int id, bool error)
220 qDebug() << "HttpGet::httpFinished(int, bool) =" << id << error;
221 if(id == getRequest) dataBuffer = http.readAll();
222 qDebug() << "pending:" << http.hasPendingRequests();
223 //if(!http.hasPendingRequests()) httpDone(error);
224 emit requestFinished(id, error);
228 void HttpGet::httpStarted(int id)
230 qDebug() << "HttpGet::httpStarted(int) =" << id;
234 QString HttpGet::errorString()
236 return http.errorString();
240 void HttpGet::httpResponseHeader(const QHttpResponseHeader &resp)
242 // if there is a network error abort all scheduled requests for
243 // this download
244 response = resp.statusCode();
245 if(response != 200) {
246 qDebug() << "http response error:" << response << resp.reasonPhrase();
247 http.abort();
249 // 301 -- moved permanently
250 // 302 -- found
251 // 303 -- see other
252 // 307 -- moved temporarily
253 // in all cases, header: location has the correct address so we can follow.
254 if(response == 301 || response == 302 || response == 303 || response == 307) {
255 // start new request with new url
256 qDebug() << "http response" << response << "- following";
257 getFile(resp.value("location") + query);
262 int HttpGet::httpResponse()
264 return response;
268 void HttpGet::httpState(int state)
270 QString s[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
271 "Reading", "Connected", "Closing"};
272 if(state <= 6)
273 qDebug() << "HttpGet::httpState() = " << s[state];
274 else qDebug() << "HttpGet::httpState() = " << state;