-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cpp
183 lines (144 loc) · 4.66 KB
/
mainwindow.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFileDialog>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
manager = new QNetworkAccessManager(this);
diskCache = new QNetworkDiskCache(this);
diskCache->setCacheDirectory("testqtnetworkmanager");
manager->setCache(diskCache);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this, SLOT(replyFinished(QNetworkReply*)));
this->webPage = new MyQWebPage(this);
this->webPage->setNetworkAccessManager(manager);
this->webPage->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
connect(this->webPage,SIGNAL(formSubmitted(const QNetworkRequest&)),this,SLOT(OnFormSubmitted(const QNetworkRequest&)));
this->ui->webView->setPage(this->webPage);
this->on_pushButton_recommend_clicked();
this->ui->urlText->setFocus();
}
MainWindow::~MainWindow()
{
delete ui;
delete this->diskCache;
delete this->manager;
delete this->webPage;
}
void MainWindow::on_pushButton_open_clicked()
{
this->ui->webView->load(QUrl(this->ui->urlText->text()));
}
void MainWindow::on_webView_loadProgress(int progress)
{
this->setWindowTitle(QString("加载进度 %1%").arg(progress));
}
void MainWindow::on_webView_loadFinished(bool ok)
{
if (ok)
{
this->setWindowTitle(this->ui->webView->title());
this->ui->pushButton_open->setDisabled(false);
QString url = this->ui->webView->url().toString();
// 关闭妨碍阅读的广告窗口
// epzw.com: ED_CloseIt()
QWebFrame *frame = this->webPage->mainFrame();
if (url.contains("epzw.com"))
{
frame->evaluateJavaScript(QString("ED_CloseIt();"));
}
else if (url.contains("to59.com"))
{
// TODO
}
else if (url.contains("gdsanlian.com"))
{
QWebElement element = frame->findFirstElement("div");
if (!element.isNull())
{
element.setStyleProperty("display", "none");
}
}
}
else
{
this->setWindowTitle("加载页面失败,请检查!");
}
}
void MainWindow::replyFinished(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError)
{
}
else
{
qDebug() << reply->errorString();
}
}
void MainWindow::OnFormSubmitted(const QNetworkRequest &request)
{
this->on_webView_linkClicked(request.url());
}
void MainWindow::on_epzwSite_clicked()
{
this->ui->urlText->setText(QString("http://www.epzw.com/"));
this->on_pushButton_open_clicked();
}
void MainWindow::on__59toSite_clicked()
{
this->ui->urlText->setText(QString("http://www.59to.com/"));
this->on_pushButton_open_clicked();
}
void MainWindow::on_webView_linkClicked(const QUrl &url)
{
QString oldUrl = this->ui->webView->url().toString();
bool isOpen = true;
if (!(oldUrl.contains(QString("yixinit.com")) || oldUrl.compare(QString("")) == 0))
{
if (!oldUrl.isEmpty() && !this->isSameDomain(oldUrl, url.host()))
{
// 咨询是否打开外链
isOpen = QMessageBox::Yes == QMessageBox::question(this, "外链咨询", QString("是否打开外链: %1 ?").arg(url.toString()), QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
}
}
if (isOpen)
{
this->ui->urlText->setText(url.toString());
this->on_pushButton_open_clicked();
}
}
bool MainWindow::isSameDomain(QString url1, QString url2)
{
QUrl curUrl = QUrl(url1);
QStringList list1 = curUrl.host().replace(QString("http://"), "").replace(QString("https://"), "").replace(QString("/"), "").split(QString("."));
QStringList list2 = url2.replace(QString("http://"), "").replace(QString("https://"), "").replace(QString("/"), "").split(QString("."));
QVector<QString> v1;
QVector<QString> v2;
for (int i = list1.size() - 1; i > 0; i--)
{
v1.append(list1.at(i));
}
for (int i = list2.size() - 1; i > 0; i--)
{
v2.append((list2.at(i)));
}
return (v1.at(0).compare(v2.at(0)) == 0) && (v1.at(1).compare(v2.at(1)) == 0);
}
void MainWindow::on_pushButton_qidian_clicked()
{
this->ui->urlText->setText(QString("http://www.qidian.com/"));
this->on_pushButton_open_clicked();
}
void MainWindow::on_pushButton_motie_clicked()
{
this->ui->urlText->setText(QString("http://www.motie.com/"));
this->on_pushButton_open_clicked();
}
void MainWindow::on_pushButton_recommend_clicked()
{
this->ui->urlText->setText(QString("http://"));
this->ui->webView->load(QUrl(QString("http://www.yixinit.com/xiaoshuo.html")));
}