-
Notifications
You must be signed in to change notification settings - Fork 0
/
stab.cpp
279 lines (205 loc) · 7.48 KB
/
stab.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "stab.h"
#include "ui_stab.h"
STab::STab(QWidget *parent) : QWidget(parent), ui(new Ui::STab)
{
ui->setupUi(this);
ui->progressBar->hide();
connect(ui->loadButton, SIGNAL(clicked()), this, SLOT(setDownloadStreamPage()));
connect(ui->urlEdit, SIGNAL(returnPressed()), this, SLOT(setDownloadStreamPage()));
ui->verticalLayout->setAlignment(Qt::AlignTop);
}
void STab::resizeEvent(QResizeEvent * event)
{
ui->verticalLayoutWidget->setGeometry(QRect(0, 0, this->width(), this->height()));
ui->frame->setGeometry(0, 0, this->width(), 37);
ui->horizontalLayoutWidget->setGeometry(ui->frame->x(), ui->frame->y(), ui->frame->width(), ui->frame->height());
}
void STab::setDownloadStreamPage()
{
qDebug("STab::setDownloadStreamPage");
prepareUiToLoad();
ui->progressBar->setValue(5);
connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(handleStreamPage(bool)));
ui->webView->load(QUrl(ui->urlEdit->text()));
}
void STab::handleStreamPage(bool isLoad)
{
qDebug("STab::handleStreamPage");
disconnect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(handleStreamPage(bool)));
if (!isLoad) {
emit titleChanged(trUtf8("Stream not found"));
showMessage(trUtf8("Error: Stream not found"));
return;
}
ui->progressBar->setValue(45);
QNetworkAccessManager* accessManager = ui->webView->page()->networkAccessManager();
QNetworkRequest request(ui->webView->url());
QNetworkReply* reply = accessManager->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(handleStreamSourcePage()));
}
void STab::handleStreamSourcePage()
{
qDebug("STab::handleStreamSourcePage");
QNetworkReply* reply = qobject_cast<QNetworkReply*>(const_cast<QObject*>(sender()));
ui->progressBar->setValue(80);
disconnect(reply, SIGNAL(finished()), this, SLOT(handleStreamSourcePage()));
if (reply->error() != 0) {
emit titleChanged(trUtf8("Stream not found"));
qDebug() << QString("Response error: ") << reply->error();
showMessage(trUtf8("Error: Stream not found"));
return;
}
QUrl url = reply->url();
QRegExp wwwRegExp(QString("^www."), Qt::CaseInsensitive);
QString host = url.host().replace(wwwRegExp, QString(""));
qDebug() << QString("Host") << host;
QSettings* const settings = Settings::getHostsInstance();
settings->beginGroup(host);
QString type = settings->value("type").toString();
settings->endGroup();
if (type.isEmpty()) {
this->currentGroup = QString("default");
} else {
this->currentGroup = host;
}
QString content = QString::fromUtf8(reply->readAll());
if (!exeStream(this->currentGroup, content)) {
showMessage(trUtf8("Stream not found on cerrent page"));
return;
}
// Получаем title для таба
QString titleRegExp = QString("<title>([^<]+)</title>");
QRegExp titleRx(titleRegExp, Qt::CaseInsensitive);
titleRx.indexIn(content);
this->title = titleRx.cap(1);
emit titleChanged(this->title);
}
bool STab::setStreamByLink(const QString& regExp, const QString& source)
{
QRegExp streamRx(regExp, Qt::CaseInsensitive);
streamRx.indexIn(source);
QString url = streamRx.cap(1);
if (url.isEmpty()) {
return false;
}
connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(handleFinishStreamLoading(bool)));
ui->webView->load(QUrl(url));
return true;
}
bool STab::setStreamByEmbed(const QString& regExp, const QString& source)
{
qDebug("STab::setStreamByEmbed");
QRegExp streamRx(regExp, Qt::CaseInsensitive, QRegExp::RegExp2);
streamRx.setMinimal(true);
streamRx.indexIn(source);
QString code = streamRx.cap(1);
if (code.isEmpty()) {
return false;
}
connect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(handleFinishStreamLoading(bool)));
QString content("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /><style>body{padding:0px; margin:0px;}</style></head><body>");
content.append(code);
content.append("</body></html>");
ui->webView->setContent(content.toUtf8());
return true;
}
void STab::handleFinishStreamLoading(bool)
{
qDebug("STab::handleFinishStreamLoading");
disconnect(ui->webView, SIGNAL(loadFinished(bool)), this, SLOT(handleFinishStreamLoading(bool)));
ui->progressBar->setValue(99);
//
QSettings* const settings = Settings::getHostsInstance();
settings->beginGroup(this->currentGroup);
QString mode = settings->value("mode").toString();
if (mode == QString("regexps")) {
QWebFrame *frame = ui->webView->page()->mainFrame();
QString content = frame->toHtml();
int size = settings->beginReadArray("rule");
for (int i = 0; i < size; ++i) {
settings->setArrayIndex(i);
QRegExp rx(settings->value("from").toString());
content.replace(rx, settings->value("to").toString());
}
settings->endArray();
qDebug() << content;
ui->webView->setContent(content.toUtf8());
}
settings->endGroup();
prepareUi();
}
void STab::showMessage(const QString& message)
{
prepareUi();
QString content = QString("<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /></head><body>");
content.append(message);
content.append(QString("</body></html>"));
ui->webView->setContent(content.toUtf8());
}
void STab::prepareUiToLoad()
{
ui->loadButton->setEnabled(false);
ui->urlEdit->setEnabled(false);
ui->webView->hide();
ui->progressBar->show();
}
void STab::prepareUi()
{
ui->loadButton->setEnabled(true);
ui->urlEdit->setEnabled(true);
ui->progressBar->hide();
ui->webView->show();
}
void STab::hideNavigationPanel()
{
ui->frame->hide();
}
void STab::showNavigationPanel()
{
ui->frame->show();
}
QString STab::getTitle()
{
return this->title;
}
bool STab::exeStream(const QString& groupName, const QString& source)
{
qDebug("STab::exeStream");
this->currentGroup = groupName;
QSettings* const settings = Settings::getHostsInstance();
settings->beginGroup(groupName);
QString type = settings->value("type").toString();
settings->endGroup();
qDebug() << QString("Type") << type;
if (type == QString("link")) {
settings->beginGroup(groupName);
QString regExp = settings->value("reg_exp").toString();
settings->endGroup();
qDebug() << QString("Stream RegExp: ") << regExp;
return setStreamByLink(regExp, source);
} else if (type == QString("multi")) {
settings->beginGroup(groupName);
QList<QString> lists;
int size = settings->beginReadArray("rule");
for (int i = 0; i < size; ++i) {
settings->setArrayIndex(i);
QString nextGroupName = settings->value("name").toString();
lists.append(nextGroupName);
}
settings->endArray();
settings->endGroup();
for (int j = 0; j < lists.size(); j++) {
qDebug() << QString("Group name") << j << lists[j];
if(exeStream(lists[j], source)) {
return true;
}
}
} else if (type == QString("embed")) {
settings->beginGroup(groupName);
QString regExp = settings->value("reg_exp").toString();
settings->endGroup();
qDebug() << QString("Stream RegExp") << regExp;
return setStreamByEmbed(regExp, source);
}
return false;
}