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
)
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()
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
)
60 qDebug() << "HttpGet::setFile" << outputFile
->fileName();
71 bool HttpGet::getFile(const QUrl
&url
)
74 qDebug() << "Error: Invalid URL" << endl
;
78 if (url
.scheme() != "http") {
79 qDebug() << "Error: URL must start with 'http:'" << endl
;
83 if (url
.path().isEmpty()) {
84 qDebug() << "Error: URL has no path" << endl
;
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())
99 http
.setHost(url
.host(), url
.port(80));
100 http
.get(url
.path(), outputFile
);
105 void HttpGet::httpDone(bool error
)
108 qDebug() << "Error: " << qPrintable(http
.errorString()) << endl
;
110 qDebug() << "File downloaded as " << qPrintable(outputFile
->fileName())
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()