1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
9 * Copyright (C) 2007 by Dominik Riebeling
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 ****************************************************************************/
27 HttpGet::HttpGet(QObject
*parent
)
30 qDebug() << "--> HttpGet::HttpGet()";
31 outputToBuffer
= true;
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()
51 QHttp::Error
HttpGet::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
)
73 outputToBuffer
= false;
74 qDebug() << "HttpGet::setFile" << outputFile
->fileName();
86 bool HttpGet::getFile(const QUrl
&url
)
89 qDebug() << "Error: Invalid URL" << endl
;
93 if (url
.scheme() != "http") {
94 qDebug() << "Error: URL must start with 'http:'" << endl
;
98 if (url
.path().isEmpty()) {
99 qDebug() << "Error: URL has no path" << endl
;
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())
111 http
.setHost(url
.host(), url
.port(80));
113 qDebug() << "downloading to buffer:" << QString(url
.toEncoded());
114 getRequest
= http
.get(QString(url
.toEncoded()));
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
;
127 void HttpGet::httpDone(bool error
)
130 qDebug() << "Error: " << qPrintable(http
.errorString()) << endl
;
132 if(!outputToBuffer
) {
134 qDebug() << "File downloaded as" << qPrintable(outputFile
->fileName());
136 else qDebug() << "file downloaded to buffer";
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
168 response
= resp
.statusCode();
169 if(response
!= 200) http
.abort();
173 int HttpGet::httpResponse()
179 void HttpGet::httpState(int state
)
181 QString s
[] = {"Unconnected", "HostLookup", "Connecting", "Sending",
182 "Reading", "Connected", "Closing"};
184 qDebug() << "HttpGet::httpState() = " << s
[state
];
185 else qDebug() << "HttpGet::httpState() = " << state
;