-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.cpp
363 lines (333 loc) · 13.6 KB
/
browser.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "browser.h"
#include "lineedit.h"
#include "webenginepage.h"
#include "webengineview.h"
#include <QInputDialog>
#include <QFileInfo>
#include <QFileDialog>
#include <QDebug>
#include <QStyle>
Browser::Browser(QWidget* parent) : QWidget(parent) {
try {
tabs = new QTabWidget(this);
tabs->setDocumentMode(true);
connect(tabs, &QTabWidget::tabBarDoubleClicked, this, static_cast<void (Browser::*)()>(&Browser::add_tab));
connect(tabs, &QTabWidget::currentChanged, this, &Browser::tab_change);
tabs->setTabsClosable(true);
connect(tabs, &QTabWidget::tabCloseRequested, this, &Browser::close_tab);
layout = new QVBoxLayout(this);
navbar = new QToolBar("Navigation", this);
backbtn = new QPushButton(this);
backbtn->setIcon(style()->standardIcon(QStyle::SP_ArrowBack));
fwdbtn = new QPushButton(this);
fwdbtn->setIcon(style()->standardIcon(QStyle::SP_ArrowForward));
reloadbtn = new QPushButton(this);
reloadbtn->setIcon(style()->standardIcon(QStyle::SP_BrowserReload));
shortcut_reload = new QShortcut(QKeySequence("F5"), this);
shortcut_new_tab = new QShortcut(QKeySequence("Ctrl+T"), this);
shortcut_find_text = new QShortcut(QKeySequence("Ctrl+F"), this);
connect(shortcut_find_text, &QShortcut::activated, this, &Browser::find_text);
homebtn = new QPushButton(this);
homebtn->setIcon(style()->standardIcon(QStyle::SP_ComputerIcon));
stopbtn = new QPushButton(this);
stopbtn->setIcon(style()->standardIcon(QStyle::SP_BrowserStop));
connect(backbtn, &QPushButton::clicked, this, [this]() {
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
cw->back();
});
connect(fwdbtn, &QPushButton::clicked, this, [this]() {
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
cw->forward();
});
connect(reloadbtn, &QPushButton::clicked, this, [this]() {
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
cw->reload();
});
connect(shortcut_reload, &QShortcut::activated, this, [this]() {
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
cw->reload();
});
connect(shortcut_new_tab, &QShortcut::activated, this, static_cast<void (Browser::*)()>(&Browser::add_tab));
connect(stopbtn, &QPushButton::clicked, this, [this]() {
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
cw->stop();
});
connect(homebtn, &QPushButton::clicked, this, &Browser::go_home);
urlbar = new LineEdit(this);
shortcut_url = new QShortcut(QKeySequence("Ctrl+G"), this);
connect(shortcut_url, &QShortcut::activated, this, &Browser::focus_urlbar);
connect(urlbar, &QLineEdit::returnPressed, this, &Browser::goto_url);
shortcut_print_to_pdf = new QShortcut(QKeySequence("Ctrl+P"), this);
connect(shortcut_print_to_pdf, &QShortcut::activated, this, &Browser::printToPdf);
navbar->addWidget(backbtn);
navbar->addWidget(fwdbtn);
navbar->addWidget(homebtn);
navbar->addWidget(urlbar);
navbar->addWidget(reloadbtn);
navbar->addWidget(stopbtn);
layout->addWidget(navbar);
layout->addWidget(tabs);
favbtn = new QToolButton(this);
favbtn->setIcon(style()->standardIcon(QStyle::SP_DialogYesButton));
favbtn->setPopupMode(QToolButton::InstantPopup);
favmenu = new QMenu(this);
connect(favmenu, &QMenu::triggered, this, &Browser::goto_favorite);
favbtn->setMenu(favmenu);
navbar->addWidget(favbtn);
load_favorites();
setLayout(layout);
add_tab();
search_text = "";
shortcut_find_next = new QShortcut(QKeySequence("N"), this);
shortcut_find_prev = new QShortcut(QKeySequence("P"), this);
shortcut_find_exit = new QShortcut(QKeySequence("ESC"), this);
connect(shortcut_find_next, &QShortcut::activated, this, &Browser::find_next);
connect(shortcut_find_prev, &QShortcut::activated, this, &Browser::find_prev);
connect(shortcut_find_exit, &QShortcut::activated, this, &Browser::find_exit);
} catch (const std::exception& e) {
qDebug() << "Browser.__init__: " << e.what();
}
}
void Browser::add_favorite() {
try {
int tab_index = tabs->currentIndex();
QString name = tabs->tabText(tab_index);
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
QUrl url = cw->url();
QString favorite = name + "=" + url.toString() + "\n";
QFile fav_file(QDir::homePath() + "/.favorites");
if (fav_file.open(QIODevice::Append)) {
QTextStream out(&fav_file);
out << favorite;
fav_file.close();
load_favorites();
}
} catch (const std::exception& e) {
qDebug() << "Browser.add_favorite: " << e.what();
}
}
void Browser::load_favorites() {
try {
favmenu->clear();
QAction* add_favorite_action = favmenu->addAction("Add Favorite");
connect(add_favorite_action, &QAction::triggered, this, &Browser::add_favorite);
QFile fav_file(QDir::homePath() + "/.favorites");
if (fav_file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream in(&fav_file);
while (!in.atEnd()) {
QString fav = in.readLine();
if (!fav.isEmpty()) {
QStringList aryfav = fav.split('=');
QAction* fav_act = new QAction(aryfav[0], this);
fav_act->setData(aryfav[1].trimmed());
favmenu->addAction(fav_act);
}
}
fav_file.close();
}
} catch (const std::exception& e) {
qDebug() << "Browser.load_favorites: " << e.what();
}
}
void Browser::goto_favorite(QAction* favorite) {
qDebug() << "Browser.goto_favorite: ";
if (favorite->text() == "Add Favorite") {
add_favorite();
} else {
qDebug() << favorite->data();
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
cw->setUrl(QUrl(favorite->data().toString()));
}
}
void Browser::handle_auth(const QUrl& url, QAuthenticator* auth) {
qDebug() << "Browser.handle_auth";
qDebug() << url;
qDebug() << auth;
try {
QString user = QInputDialog::getText(this, url.toString() + " has requested authentication", "User name:");
if (!user.isEmpty()) {
auth->setUser(user);
qDebug() << "Browser.handle_auth set user";
}
QString passwd = QInputDialog::getText(this, url.toString() + " has requested authentication", "Password:", QLineEdit::Password);
if (!passwd.isEmpty()) {
auth->setPassword(passwd);
qDebug() << "Browser.handle_auth set password";
}
} catch (const std::exception& e) {
qDebug() << "Browser.handle_auth: " << e.what();
}
}
void Browser::find_text() {
try {
QString text = QInputDialog::getText(this, "Search", "Find Text:");
if (!text.isEmpty()) {
qDebug() << "Browser.find_text";
qDebug() << text;
search_text = text;
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
cw->page()->findText(search_text, QWebEnginePage::FindFlags(0), [this](bool found) { find_text_callback(found); });
}
} catch (const std::exception& e) {
qDebug() << "Browser.find_text: " << e.what();
}
}
void Browser::find_text_callback(bool b_found) {
qDebug() << b_found;
if (!b_found && !search_text.isEmpty()) {
find_text();
}
}
void Browser::find_next() {
try {
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
cw->page()->findText(search_text, QWebEnginePage::FindFlags(0), [this](bool found) { find_text_callback(found); });
} catch (const std::exception& e) {
qDebug() << "Browser.find_next: " << e.what();
}
}
void Browser::find_prev() {
try {
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
cw->page()->findText(search_text, QWebEnginePage::FindFlags(QWebEnginePage::FindBackward), [this](bool found) { find_text_callback(found); });
} catch (const std::exception& e) {
qDebug() << "Browser.find_prev: " << e.what();
}
}
void Browser::find_exit() {
try {
search_text = "";
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
cw->page()->findText(search_text, QWebEnginePage::FindFlags(0), [this](bool found) { find_text_callback(found); });
} catch (const std::exception& e) {
qDebug() << "Browser.find_exit: " << e.what();
}
}
void Browser::close_tab(int i) {
try {
if (tabs->count() > 1) {
delete tabs->widget(i);
}
} catch (const std::exception& e) {
qDebug() << "Browser.close_tab: " << e.what();
}
}
void Browser::add_tab() {
WebEnginePage* page = add_tab(QUrl("https://google.com"));
}
WebEnginePage* Browser::add_tab(const QUrl& qurl) {
try {
WebEngineView* webView = new WebEngineView(tabs);
WebEnginePage* page = new WebEnginePage(webView);
page->setBrowser(this);
connect(page->profile(), &QWebEngineProfile::downloadRequested, this, &Browser::save);
connect(page, &WebEnginePage::authenticationRequired, this, &Browser::handle_auth);
webView->setPage(page);
webView->settings()->setAttribute(QWebEngineSettings::PluginsEnabled, true);
webView->settings()->setAttribute(QWebEngineSettings::PdfViewerEnabled, true);
qDebug() << "Browser.add_tab: url: " << qurl;
webView->setUrl(qurl);
connect(webView, &QWebEngineView::urlChanged, this, &Browser::update_url);
connect(webView, &QWebEngineView::titleChanged, this, &Browser::update_tab_label);
int i = tabs->addTab(webView, webView->title());
tabs->setCurrentIndex(i);
return page;
} catch (const std::exception& e) {
qDebug() << "Browser.add_tab: " << e.what();
}
}
void Browser::printToPdf() {
try {
int tab_index = tabs->currentIndex();
QString tab_text = tabs->tabText(tab_index);
qDebug() << "Browser.printToPdf.tab_text: " << tab_text;
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
QWebEnginePage* page = cw->page();
QString path = QFileDialog::getSaveFileName(this, "Print to PDF", "", "*.pdf");
qDebug() << "Browser.printToPdf.path: " << path;
page->printToPdf(path);
} catch (const std::exception& e) {
qDebug() << "Browser.printToPdf: " << e.what();
}
}
void Browser::save(QWebEngineDownloadItem *request) {
try {
qDebug() << "save requested:";
qDebug() << request->state();
qDebug() << request->path();
if (request->state() == QWebEngineDownloadItem::DownloadRequested) {
QString fileName = request->downloadFileName();
QString suffix = QFileInfo(fileName).completeSuffix();
QString path = QFileDialog::getSaveFileName(this->tabs->currentWidget(), "Save File", fileName, "*." + suffix);
if (!path.isEmpty()) {
request->setPath(path);
request->accept();
} else {
request->cancel();
}
}
} catch (const std::exception &e) {
qDebug() << "Browser.save: " << e.what();
}
}
void Browser::focus_urlbar() {
try {
urlbar->setText("");
urlbar->setCursorPosition(0);
urlbar->setFocus();
} catch (const std::exception& e) {
qDebug() << "Browser.focus_urlbar: " << e.what();
}
}
void Browser::go_home() {
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
cw->setUrl(QUrl("http://retro.adrenlinerush.net"));
}
void Browser::goto_url() {
QUrl url(urlbar->text());
if (url.toString().contains(" ") || (!url.toString().contains(".") && url.scheme().isEmpty())) {
url = QUrl("https://www.google.com/search?q=" + url.toString().split(" ").join("+"));
qDebug() << "Browser::gotoUrl search: " << url.toString();
} else if (url.scheme().isEmpty()) {
url.setScheme("http");
}
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
cw->setUrl(url);
}
void Browser::update_url(const QUrl& url) {
qDebug() << "Browser::updateUrl: url: " << url;
if (!url.isEmpty()) {
update_url_bar(url.toString());
}
}
void Browser::update_url_bar(const QString& url) {
try {
qDebug() << "Browser::updateUrlBar: url: " << url;
urlbar->setText(url);
urlbar->setCursorPosition(0);
} catch (const std::exception& e) {
qDebug() << "Browser::updateUrlBar Error: " << e.what();
}
}
void Browser::tab_change(int i) {
try {
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
update_url_bar(cw->page()->url().toString());
} catch (const std::exception& e) {
qDebug() << "Browser::tab_change Error: " << e.what();
}
}
void Browser::update_tab_label() {
try {
QWebEngineView* cw = qobject_cast<QWebEngineView*>(tabs->currentWidget());
qDebug() << "Browser::updateTabLabel: title: " << cw->page()->title();
int i = tabs->currentIndex();
QString title = cw->page()->title();
if (title.length() > 15) {
title = title.left(15);
}
tabs->setTabText(i, title);
} catch (const std::exception& e) {
qDebug() << "Browser::updateTabLabel Error: " << e.what();
}
}