summaryrefslogtreecommitdiff
path: root/rbutil/rbutilqt/httpget.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'rbutil/rbutilqt/httpget.cpp')
-rw-r--r--rbutil/rbutilqt/httpget.cpp144
1 files changed, 144 insertions, 0 deletions
diff --git a/rbutil/rbutilqt/httpget.cpp b/rbutil/rbutilqt/httpget.cpp
new file mode 100644
index 0000000000..b567a7df80
--- /dev/null
+++ b/rbutil/rbutilqt/httpget.cpp
@@ -0,0 +1,144 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 *
9 * Copyright (C) 2007 by Dominik Riebeling
10 * $Id:$
11 *
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.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#include <QtCore>
21#include <QtNetwork>
22#include <QtDebug>
23
24#include "httpget.h"
25
26
27HttpGet::HttpGet(QObject *parent)
28 : QObject(parent)
29{
30
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&)));
36}
37
38
39QHttp::Error HttpGet::error()
40{
41 return http.error();
42}
43
44void HttpGet::httpProgress(int read, int total)
45{
46 emit dataReadProgress(read, total);
47}
48
49
50void HttpGet::setProxy(const QUrl &proxy)
51{
52 qDebug() << "HttpGet::setProxy" << proxy.toString();
53 http.setProxy(proxy.host(), proxy.port(), proxy.userName(), proxy.password());
54}
55
56
57void HttpGet::setFile(QFile *file)
58{
59 outputFile = file;
60 qDebug() << "HttpGet::setFile" << outputFile->fileName();
61}
62
63
64void HttpGet::abort()
65{
66 http.abort();
67 outputFile->close();
68}
69
70
71bool HttpGet::getFile(const QUrl &url)
72{
73 if (!url.isValid()) {
74 qDebug() << "Error: Invalid URL" << endl;
75 return false;
76 }
77
78 if (url.scheme() != "http") {
79 qDebug() << "Error: URL must start with 'http:'" << endl;
80 return false;
81 }
82
83 if (url.path().isEmpty()) {
84 qDebug() << "Error: URL has no path" << endl;
85 return false;
86 }
87
88 QString localFileName = outputFile->fileName();
89 if (localFileName.isEmpty())
90 outputFile->setFileName(QFileInfo(url.path()).fileName());
91
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;
97 }
98
99 http.setHost(url.host(), url.port(80));
100 http.get(url.path(), outputFile);
101 http.close();
102 return true;
103}
104
105void HttpGet::httpDone(bool error)
106{
107 if (error) {
108 qDebug() << "Error: " << qPrintable(http.errorString()) << endl;
109 } else {
110 qDebug() << "File downloaded as " << qPrintable(outputFile->fileName())
111 << endl;
112 }
113 outputFile->close();
114 emit done(error);
115}
116
117
118void HttpGet::httpFinished(int id, bool error)
119{
120 qDebug() << "HttpGet::httpFinished";
121 qDebug() << "id:" << id << "error:" << error;
122 emit requestFinished(id, error);
123
124}
125
126
127QString HttpGet::errorString()
128{
129 return http.errorString();
130}
131
132
133void HttpGet::httpResponseHeader(const QHttpResponseHeader &resp)
134{
135 qDebug() << "HttpGet::httpResponseHeader()" << resp.statusCode();
136 response = resp.statusCode();
137 if(response != 200) http.abort();
138}
139
140
141int HttpGet::httpResponse()
142{
143 return response;
144}