1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2010 Ricardo Villalba <rvm@escomposlinux.org>
|
---|
3 |
|
---|
4 | This program is free software; you can redistribute it and/or modify
|
---|
5 | it under the terms of the GNU General Public License as published by
|
---|
6 | the Free Software Foundation; either version 2 of the License, or
|
---|
7 | (at your option) any later version.
|
---|
8 |
|
---|
9 | This program is distributed in the hope that it will be useful,
|
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
12 | GNU General Public License for more details.
|
---|
13 |
|
---|
14 | You should have received a copy of the GNU General Public License
|
---|
15 | along with this program; if not, write to the Free Software
|
---|
16 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
17 | */
|
---|
18 |
|
---|
19 | #include "simplehttp.h"
|
---|
20 | #include <QUrl>
|
---|
21 |
|
---|
22 | SimpleHttp::SimpleHttp( QObject * parent ) : QHttp(parent)
|
---|
23 | {
|
---|
24 | http_get_id = -1;
|
---|
25 |
|
---|
26 | connect( this, SIGNAL(requestFinished(int, bool)),
|
---|
27 | this, SLOT(httpRequestFinished(int, bool)) );
|
---|
28 |
|
---|
29 | connect( this, SIGNAL(responseHeaderReceived(const QHttpResponseHeader &)),
|
---|
30 | this, SLOT(readResponseHeader(const QHttpResponseHeader &)) );
|
---|
31 | }
|
---|
32 |
|
---|
33 | SimpleHttp::~SimpleHttp() {
|
---|
34 | }
|
---|
35 |
|
---|
36 | void SimpleHttp::download(const QString & url) {
|
---|
37 | downloaded_text.clear();
|
---|
38 |
|
---|
39 | QUrl u(url);
|
---|
40 | setHost( u.host() );
|
---|
41 | http_get_id = get( u.path() );
|
---|
42 |
|
---|
43 | emit connecting(u.host());
|
---|
44 | }
|
---|
45 |
|
---|
46 | void SimpleHttp::readResponseHeader(const QHttpResponseHeader &responseHeader) {
|
---|
47 | qDebug("SimpleHttp::readResponseHeader: statusCode: %d", responseHeader.statusCode());
|
---|
48 |
|
---|
49 | if (responseHeader.statusCode() == 301) {
|
---|
50 | QString new_url = responseHeader.value("Location");
|
---|
51 | qDebug("SimpleHttp::readResponseHeader: Location: '%s'", new_url.toLatin1().constData());
|
---|
52 | download(new_url);
|
---|
53 | }
|
---|
54 | else
|
---|
55 | if (responseHeader.statusCode() == 302) {
|
---|
56 | QString location = responseHeader.value("Location");
|
---|
57 | qDebug("SimpleHttp::readResponseHeader: Location: '%s'", location.toLatin1().constData());
|
---|
58 | http_get_id = get( location );
|
---|
59 | }
|
---|
60 | else
|
---|
61 | if (responseHeader.statusCode() != 200) {
|
---|
62 | qDebug("SimpleHttp::readResponseHeader: error: '%s'", responseHeader.reasonPhrase().toLatin1().constData());
|
---|
63 | emit downloadFailed(responseHeader.reasonPhrase());
|
---|
64 | abort();
|
---|
65 | }
|
---|
66 | }
|
---|
67 |
|
---|
68 | void SimpleHttp::httpRequestFinished(int request_id, bool error) {
|
---|
69 | qDebug("SimpleHttp::httpRequestFinished: http_get_id: %d request_id: %d, error: %d", http_get_id, request_id, error);
|
---|
70 |
|
---|
71 | if (request_id != http_get_id) return;
|
---|
72 |
|
---|
73 | downloaded_text += readAll();
|
---|
74 |
|
---|
75 | if (!downloaded_text.isEmpty()) {
|
---|
76 | emit downloadFinished(downloaded_text);
|
---|
77 | }
|
---|
78 | }
|
---|
79 |
|
---|
80 | #include "moc_simplehttp.cpp"
|
---|
81 |
|
---|