1 | /* smplayer, GUI front-end for mplayer.
|
---|
2 | Copyright (C) 2006-2017 Ricardo Villalba <rvm@users.sourceforge.net>
|
---|
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 "loadpage.h"
|
---|
20 | #include <QNetworkAccessManager>
|
---|
21 | #include <QNetworkReply>
|
---|
22 | #include <QNetworkRequest>
|
---|
23 | #include <QDebug>
|
---|
24 |
|
---|
25 | QString LoadPage::default_user_agent;
|
---|
26 |
|
---|
27 | LoadPage::LoadPage(QNetworkAccessManager * man, QObject* parent)
|
---|
28 | : QObject(parent)
|
---|
29 | {
|
---|
30 | manager = man;
|
---|
31 | }
|
---|
32 |
|
---|
33 | LoadPage::~LoadPage() {
|
---|
34 | }
|
---|
35 |
|
---|
36 | void LoadPage::fetchPage(const QString & url) {
|
---|
37 | qDebug() << "LoadPage::fetchPage:" << url;
|
---|
38 | qDebug() << "LoadPage::fetchPage: user agent:" << userAgent();
|
---|
39 |
|
---|
40 | QNetworkRequest req(url);
|
---|
41 | req.setRawHeader("User-Agent", userAgent().toLatin1());
|
---|
42 | req.setRawHeader("Accept-Language", "en-us,en;q=0.5");
|
---|
43 | reply = manager->get(req);
|
---|
44 | connect(reply, SIGNAL(finished()), this, SLOT(gotResponse()));
|
---|
45 | }
|
---|
46 |
|
---|
47 | void LoadPage::gotResponse() {
|
---|
48 | qDebug() << "LoadPage::gotResponse";
|
---|
49 |
|
---|
50 | QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
|
---|
51 |
|
---|
52 | if (reply->error() == QNetworkReply::NoError) {
|
---|
53 | int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
---|
54 | qDebug() << "LoadPage::gotResponse: status:" << status;
|
---|
55 | QString r_url;
|
---|
56 | switch (status) {
|
---|
57 | case 301:
|
---|
58 | case 302:
|
---|
59 | case 307:
|
---|
60 | r_url = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl().toString();
|
---|
61 | qDebug() << "LoadPage::gotResponse: redirected:" << r_url;
|
---|
62 | fetchPage(r_url);
|
---|
63 | break;
|
---|
64 | default:
|
---|
65 | qDebug() << "LoadPage::gotResponse: emit pageLoaded";
|
---|
66 | emit pageLoaded(reply->readAll());
|
---|
67 | }
|
---|
68 | } else {
|
---|
69 | qDebug() << "LoadPage::gotResponse: error:" << reply->error() << ":" << reply->errorString();
|
---|
70 | emit errorOcurred((int)reply->error(), reply->errorString());
|
---|
71 | }
|
---|
72 | reply->deleteLater();
|
---|
73 | }
|
---|
74 |
|
---|
75 | #include "moc_loadpage.cpp"
|
---|
76 |
|
---|