This repository has been archived by the owner on Mar 3, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 428
/
WebPageManager.cpp
141 lines (119 loc) · 3.47 KB
/
WebPageManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "WebPageManager.h"
#include "WebPage.h"
#include "NetworkCookieJar.h"
#include "NetworkAccessManager.h"
WebPageManager::WebPageManager(QObject *parent) : QObject(parent) {
m_ignoreSslErrors = false;
m_cookieJar = new NetworkCookieJar(this);
m_success = true;
m_loggingEnabled = false;
m_ignoredOutput = new QFile(this);
m_timeout = -1;
m_networkAccessManager = new NetworkAccessManager(this);
m_networkAccessManager->setCookieJar(m_cookieJar);
createPage(this)->setFocus();
}
NetworkAccessManager *WebPageManager::networkAccessManager() {
return m_networkAccessManager;
}
void WebPageManager::append(WebPage *value) {
m_pages.append(value);
}
QList<WebPage *> WebPageManager::pages() const {
return m_pages;
}
void WebPageManager::setCurrentPage(WebPage *page) {
m_currentPage = page;
}
WebPage *WebPageManager::currentPage() const {
return m_currentPage;
}
WebPage *WebPageManager::createPage(QObject *parent) {
WebPage *page = new WebPage(this, parent);
connect(page, SIGNAL(loadStarted()),
this, SLOT(emitLoadStarted()));
connect(page, SIGNAL(pageFinished(bool)),
this, SLOT(setPageStatus(bool)));
connect(page, SIGNAL(requestCreated(QByteArray &, QNetworkReply *)),
this, SLOT(requestCreated(QByteArray &, QNetworkReply *)));
append(page);
return page;
}
void WebPageManager::emitLoadStarted() {
if (m_started.empty()) {
logger() << "Load started";
emit loadStarted();
}
m_started += qobject_cast<WebPage *>(sender());
}
void WebPageManager::requestCreated(QByteArray &url, QNetworkReply *reply) {
logger() << "Started request to" << url;
if (reply->isFinished())
replyFinished(reply);
else {
connect(reply, SIGNAL(finished()), SLOT(handleReplyFinished()));
}
}
void WebPageManager::handleReplyFinished() {
QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
disconnect(reply, SIGNAL(finished()), this, SLOT(handleReplyFinished()));
replyFinished(reply);
}
void WebPageManager::replyFinished(QNetworkReply *reply) {
int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
logger() << "Received" << status << "from" << reply->url().toString();
}
void WebPageManager::setPageStatus(bool success) {
logger() << "Page finished with" << success;
m_started.remove(qobject_cast<WebPage *>(sender()));
m_success = success && m_success;
if (m_started.empty()) {
emitPageFinished();
}
}
void WebPageManager::emitPageFinished() {
logger() << "Load finished";
emit pageFinished(m_success);
m_success = true;
}
void WebPageManager::setIgnoreSslErrors(bool value) {
m_ignoreSslErrors = value;
}
bool WebPageManager::ignoreSslErrors() {
return m_ignoreSslErrors;
}
int WebPageManager::getTimeout() {
return m_timeout;
}
void WebPageManager::setTimeout(int timeout) {
m_timeout = timeout;
}
void WebPageManager::reset() {
m_timeout = -1;
m_cookieJar->clearCookies();
m_networkAccessManager->reset();
m_pages.first()->resetLocalStorage();
m_pages.first()->deleteLater();
m_pages.clear();
createPage(this)->setFocus();
}
NetworkCookieJar *WebPageManager::cookieJar() {
return m_cookieJar;
}
bool WebPageManager::isLoading() const {
foreach(WebPage *page, pages()) {
if (page->isLoading()) {
return true;
}
}
return false;
}
QDebug WebPageManager::logger() const {
if (m_loggingEnabled)
return qDebug();
else
return QDebug(m_ignoredOutput);
}
void WebPageManager::enableLogging() {
m_loggingEnabled = true;
}