oops, forgot to commit the icon files.
[Rockbox.git] / rbutil / rbutilqt / httpget.cpp
blobb567a7df80634ddd83e7a307edb047bda6c2f333
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)
31 outputFile = new QFile(this);
32 connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
33 connect(&http, SIGNAL(dataReadProgress(int, int)), this, SLOT(httpProgress(int, int)));
34 connect(&http, SIGNAL(requestFinished(int, bool)), this, SLOT(httpFinished(int, bool)));
35 connect(&http, SIGNAL(responseHeaderReceived(const QHttpResponseHeader&)), this, SLOT(httpResponseHeader(const QHttpResponseHeader&)));
39 QHttp::Error HttpGet::error()
41 return http.error();
44 void HttpGet::httpProgress(int read, int total)
46 emit dataReadProgress(read, total);
50 void HttpGet::setProxy(const QUrl &proxy)
52 qDebug() << "HttpGet::setProxy" << proxy.toString();
53 http.setProxy(proxy.host(), proxy.port(), proxy.userName(), proxy.password());
57 void HttpGet::setFile(QFile *file)
59 outputFile = file;
60 qDebug() << "HttpGet::setFile" << outputFile->fileName();
64 void HttpGet::abort()
66 http.abort();
67 outputFile->close();
71 bool HttpGet::getFile(const QUrl &url)
73 if (!url.isValid()) {
74 qDebug() << "Error: Invalid URL" << endl;
75 return false;
78 if (url.scheme() != "http") {
79 qDebug() << "Error: URL must start with 'http:'" << endl;
80 return false;
83 if (url.path().isEmpty()) {
84 qDebug() << "Error: URL has no path" << endl;
85 return false;
88 QString localFileName = outputFile->fileName();
89 if (localFileName.isEmpty())
90 outputFile->setFileName(QFileInfo(url.path()).fileName());
92 if (!outputFile->open(QIODevice::ReadWrite)) {
93 qDebug() << "Error: Cannot open " << qPrintable(outputFile->fileName())
94 << " for writing: " << qPrintable(outputFile->errorString())
95 << endl;
96 return false;
99 http.setHost(url.host(), url.port(80));
100 http.get(url.path(), outputFile);
101 http.close();
102 return true;
105 void HttpGet::httpDone(bool error)
107 if (error) {
108 qDebug() << "Error: " << qPrintable(http.errorString()) << endl;
109 } else {
110 qDebug() << "File downloaded as " << qPrintable(outputFile->fileName())
111 << endl;
113 outputFile->close();
114 emit done(error);
118 void HttpGet::httpFinished(int id, bool error)
120 qDebug() << "HttpGet::httpFinished";
121 qDebug() << "id:" << id << "error:" << error;
122 emit requestFinished(id, error);
127 QString HttpGet::errorString()
129 return http.errorString();
133 void HttpGet::httpResponseHeader(const QHttpResponseHeader &resp)
135 qDebug() << "HttpGet::httpResponseHeader()" << resp.statusCode();
136 response = resp.statusCode();
137 if(response != 200) http.abort();
141 int HttpGet::httpResponse()
143 return response;