Add some accelerator keys to the Actions menu.
[Rockbox.git] / rbutil / rbutilqt / httpget.cpp
blob0bf5d966eb2fe192e7302ae4e6da2b858d98b36d
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())
55 if(!QFileInfo(p).isDir())
56 result = m_cachedir.mkdir("rbutil-cache");
58 else result = false;
59 qDebug() << "HttpGet::setCache(QDir)" << result;
60 m_usecache = result;
64 void HttpGet::setCache(bool c)
66 m_usecache = c;
70 QByteArray HttpGet::readAll()
72 return dataBuffer;
76 QHttp::Error HttpGet::error()
78 return http.error();
82 void HttpGet::httpProgress(int read, int total)
84 emit dataReadProgress(read, total);
88 void HttpGet::setProxy(const QUrl &proxy)
90 qDebug() << "HttpGet::setProxy" << proxy.toString();
91 http.setProxy(proxy.host(), proxy.port(), proxy.userName(), proxy.password());
95 void HttpGet::setFile(QFile *file)
97 outputFile = file;
98 outputToBuffer = false;
99 qDebug() << "HttpGet::setFile" << outputFile->fileName();
103 void HttpGet::abort()
105 http.abort();
106 if(!outputToBuffer)
107 outputFile->close();
111 bool HttpGet::getFile(const QUrl &url)
113 if (!url.isValid()) {
114 qDebug() << "Error: Invalid URL" << endl;
115 return false;
118 if (url.scheme() != "http") {
119 qDebug() << "Error: URL must start with 'http:'" << endl;
120 return false;
123 if (url.path().isEmpty()) {
124 qDebug() << "Error: URL has no path" << endl;
125 return false;
127 // if no output file was set write to buffer
128 if(!outputToBuffer) {
129 if (!outputFile->open(QIODevice::ReadWrite)) {
130 qDebug() << "Error: Cannot open " << qPrintable(outputFile->fileName())
131 << " for writing: " << qPrintable(outputFile->errorString())
132 << endl;
133 return false;
136 // put hash generation here so it can get reused later
137 QString hash = QCryptographicHash::hash(url.toEncoded(), QCryptographicHash::Md5).toHex();
138 cachefile = m_cachedir.absolutePath() + "/rbutil-cache/" + hash;
139 if(m_usecache) {
140 // check if the file is present in cache
141 qDebug() << "[HTTP] cache ENABLED for" << url.toEncoded();
142 if(QFileInfo(cachefile).isReadable() && QFileInfo(cachefile).size() > 0) {
143 qDebug() << "[HTTP] cached file found!" << cachefile;
144 getRequest = -1;
145 QFile c(cachefile);
146 if(!outputToBuffer) {
147 qDebug() << outputFile->fileName();
148 c.open(QIODevice::ReadOnly);
149 outputFile->open(QIODevice::ReadWrite);
150 outputFile->write(c.readAll());
151 outputFile->close();
152 c.close();
154 else {
155 c.open(QIODevice::ReadOnly);
156 dataBuffer = c.readAll();
157 c.close();
159 response = 200; // fake "200 OK" HTTP response
160 cached = true;
161 httpDone(false); // we're done now. This will emit the correct signal too.
162 return true;
164 else qDebug() << "[HTTP] file not cached, downloading to" << cachefile;
167 else {
168 qDebug() << "[HTTP] cache DISABLED";
170 http.setHost(url.host(), url.port(80));
171 // construct query (if any)
172 QList<QPair<QString, QString> > qitems = url.queryItems();
173 if(url.hasQuery()) {
174 query = "?";
175 for(int i = 0; i < qitems.size(); i++)
176 query += qitems.at(i).first + "=" + qitems.at(i).second + "&";
177 qDebug() << query;
180 if(outputToBuffer) {
181 qDebug() << "[HTTP] downloading to buffer:" << url.toString();
182 getRequest = http.get(url.path() + query);
184 else {
185 qDebug() << "[HTTP] downloading to file:" << url.toString() << qPrintable(outputFile->fileName());
186 getRequest = http.get(url.path() + query, outputFile);
188 qDebug() << "[HTTP] request scheduled: GET" << getRequest;
190 return true;
194 void HttpGet::httpDone(bool error)
196 if (error) {
197 qDebug() << "[HTTP] Error: " << qPrintable(http.errorString()) << httpResponse();
199 if(!outputToBuffer)
200 outputFile->close();
202 if(m_usecache && !cached) {
203 qDebug() << "[HTTP] creating cache file" << cachefile;
204 QFile c(cachefile);
205 c.open(QIODevice::ReadWrite);
206 if(!outputToBuffer) {
207 outputFile->open(QIODevice::ReadOnly | QIODevice::Truncate);
208 c.write(outputFile->readAll());
209 outputFile->close();
211 else
212 c.write(dataBuffer);
214 c.close();
216 emit done(error);
220 void HttpGet::httpFinished(int id, bool error)
222 qDebug() << "HttpGet::httpFinished(int, bool) =" << id << error;
223 if(id == getRequest) dataBuffer = http.readAll();
224 qDebug() << "pending:" << http.hasPendingRequests();
225 //if(!http.hasPendingRequests()) httpDone(error);
226 emit requestFinished(id, error);
230 void HttpGet::httpStarted(int id)
232 qDebug() << "HttpGet::httpStarted(int) =" << id;
236 QString HttpGet::errorString()
238 return http.errorString();
242 void HttpGet::httpResponseHeader(const QHttpResponseHeader &resp)
244 // if there is a network error abort all scheduled requests for
245 // this download
246 response = resp.statusCode();
247 if(response != 200) {
248 qDebug() << "http response error:" << response << resp.reasonPhrase();
249 http.abort();
251 // 301 -- moved permanently
252 // 302 -- found
253 // 303 -- see other
254 // 307 -- moved temporarily
255 // in all cases, header: location has the correct address so we can follow.
256 if(response == 301 || response == 302 || response == 303 || response == 307) {
257 // start new request with new url
258 qDebug() << "http response" << response << "- following";
259 getFile(resp.value("location") + query);
264 int HttpGet::httpResponse()
266 return response;
270 void HttpGet::httpState(int state)
272 QString s[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
273 "Reading", "Connected", "Closing"};
274 if(state <= 6)
275 qDebug() << "HttpGet::httpState() = " << s[state];
276 else qDebug() << "HttpGet::httpState() = " << state;