-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpage.cpp
200 lines (172 loc) · 6.63 KB
/
webpage.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
#include <QAuthenticator>
#include <QIcon>
#include <QLineEdit>
#include <QTimer>
#include <QVBoxLayout>
#include <QWebEngineHistory>
#include "file_dialog.h"
#include "webpage.h"
#include "webview.h"
WebPage::WebPage(QWebEngineProfile* profile, QWidget* parent): QWebEnginePage(profile, parent)
{
old_url = url();
setProperty("icon", QIcon(QStringLiteral(":/icons/gizmoduck")));
connect(this, &WebPage::authenticationRequired, this, &WebPage::handleAuthenticationRequired);
lifecycle = new QTimer(this);
connect(this, &WebPage::recommendedStateChanged, [this]()
{
// qDebug() << "State change advised:" << recommendedState();
if (recommendedState()==QWebEnginePage::LifecycleState::Active)
{
//lifecycle->start (1);
setLifecycleState (QWebEnginePage::LifecycleState::Active);
}
else if (!isVisible()&& // TODO: a setting for permamently awake pages.
(url().host()!="discord.com")&&
// (url().host()!="twitter.com")&&
// (url().host()!="x.com")&&
(url().host()!="tripwire.eve-apps.com")&&
(url().host()!="colab.research.google.com")&&
(url().host()!="localhost")
)
{
if (recommendedState()==QWebEnginePage::LifecycleState::Frozen)
lifecycle->start(15*60*1000);
else if (recommendedState()==QWebEnginePage::LifecycleState::Discarded)
lifecycle->start(30*60*1000);
}
});
connect (this, &WebPage::lifecycleStateChanged, [this, profile](QWebEnginePage::LifecycleState state)
{
if (state == QWebEnginePage::LifecycleState::Discarded)
{
emit iconChanged(QIcon(QStringLiteral (":/icons/sleep")));
}
else if (state == QWebEnginePage::LifecycleState::Frozen)
emit iconChanged(QIcon(QStringLiteral (":/icons/freeze")));
else
{
profile->requestIconForPageURL(url(), 16, [this](const QIcon& icon, const QUrl&, const QUrl&)
{
emit iconChanged(icon.isNull() ? QIcon(QStringLiteral(":/icons/gizmoduck")) : icon);
});
}
});
connect(lifecycle, &QTimer::timeout, [this]()
{
if ((lifecycleState() != recommendedState()) && (recommendedState() != QWebEnginePage::LifecycleState::Discarded))
{
setLifecycleState (recommendedState());
// qDebug() << "State for" << url() << "changed to" << lifecycleState();
}
});
connect(this, &WebPage::urlChanged, [this](const QUrl& new_url)
{
if (!(old_url == new_url))
{
emit url_changed(old_url, new_url);
}
});
}
WebPage::~WebPage()
{
lifecycle->stop();
}
void WebPage::confirm_url_change(const QUrl& new_url)
// Probably excessive, just to ensure thread safety.
{
old_url = new_url;
}
//TODO: args
QStringList WebPage::chooseFiles (QWebEnginePage::FileSelectionMode mode, const QStringList&/*oldFiles*/, const QStringList&/*acceptedMimeTypes*/)
{
QStringList list;
QScopedPointer<FileDialog> dialog;
dialog.reset (new FileDialog(QWebEngineView::forPage(this), tr("Select File"), "", tr("All Files (*.*)")));
dialog->setAcceptMode (QFileDialog::AcceptOpen);
if (mode == QWebEnginePage::FileSelectOpen)
dialog->setFileMode (QFileDialog::ExistingFile);
else if (mode == QWebEnginePage::FileSelectOpenMultiple)
dialog->setFileMode (QFileDialog::ExistingFiles);
if (dialog->exec())
list = dialog->selectedFiles();
else list << "";
//dialog->deleteLater();
return list;
}
void WebPage::handleAuthenticationRequired(const QUrl& url, QAuthenticator* auth)
{
QWidget* mainWindow = QWebEngineView::forPage(this)->window();
QDialog dialog(mainWindow);
dialog.setModal(true);
dialog.setWindowFlags(dialog.windowFlags() & ~Qt::WindowContextHelpButtonHint);
QLabel* auth_label(new QLabel(tr("Enter username and password for \"%1\" at %2").arg (auth->realm()).arg(url.toString().toHtmlEscaped()), &dialog));
auth_label->setWordWrap(true);
QLineEdit* username(new QLineEdit(&dialog));
QLineEdit* password(new QLineEdit(&dialog));
QVBoxLayout* layout = new QVBoxLayout(&dialog);
layout->addWidget(auth_label);
layout->addWidget(username);
layout->addWidget(password);
dialog.setLayout(layout);
connect(password, &QLineEdit::returnPressed, &dialog, &QDialog::accept);
if (dialog.exec() == QDialog::Accepted)
{
auth->setUser(username->text());
auth->setPassword(password->text());
}
else
{
*auth = QAuthenticator();
}
}
// We currently do fine without it, but the example code is left intact in case it's needed again.
/*
bool WebPage::acceptNavigationRequest(const QUrl& url, NavigationType type, bool isMainFrame)
{
if (isMainFrame)
qDebug() << "Going to url" << url << "by navitype" << type;
if ((type == QWebEnginePage::NavigationTypeLinkClicked) && isMainFrame)
{
if (WebView* view = qobject_cast<WebView*>(QWebEngineView::forPage(this)))
{
//qDebug() << "Clicked the link" << url.toString();
emit view->link_requested(url.toString(), false);
return false;
}
}
else if (((type == QWebEnginePage::NavigationTypeRedirect) && isMainFrame) && (this->url().host() != url.host()))
{
if (WebView* view = qobject_cast<WebView*>(QWebEngineView::forPage(this)))
{
qDebug() << "Intercepting" << url << "request by" << this->url().host();
emit view->link_requested(url.toString(), false);
return false;
}
}
else if ((type == QWebEnginePage::NavigationTypeFormSubmitted) && isMainFrame)// && (this->url().host() != url.host()) && isMainFrame)
{
WebView* v = qobject_cast<WebView*>(view());
if (v)
{
qDebug() << "Intercepting" << url << "request by" << this->url().host();
emit v->link_requested (url.toString());
return false;
}
else if ((type == QWebEnginePage::NavigationTypeTyped) && (this->url().host() != url.host()) && isMainFrame)
{
if (WebView* view = qobject_cast<WebView*>(QWebEngineView::forPage(this)))
{
qDebug() << "Intercepting pseudo-typed" << url << "request by" << this->url().host();
emit view->link_requested(url.toString(), false);
return false;
}
}
else if (!isMainFrame)
{
qDebug() << "NON_MAINFRAME request of url" << url << "by navitype" << type << "passed through.";
return true;
}
return true;
}
*/