-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
372 lines (333 loc) · 11.5 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
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
364
365
366
367
368
369
370
371
372
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QWebView>
#include <QToolBar>
#include <QProgressBar>
#include <QLabel>
#include <QLineEdit>
#include <QMenu>
#include <QAction>
#include <QVBoxLayout>
#include <QWidget>
#include <QSizePolicy>
#include <QUrl>
#include <QDebug>
#include <QFontMetrics>
#include <QListIterator>
#include <QWebHistoryItem>
#include <QWebHistory>
#include <QWebPage>
#include <QMessageBox>
#include <QTimer>
#include <QNetworkInterface>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QTableView>
#include <QStandardItemModel>
#include "bookmarkdb.h"
#include "webhistory.h"
MainWindow::MainWindow(const QUrl &url) : ui(new Ui::MainWindow), progress(0)
{
ui->setupUi(this);
bookmarkDB = std::unique_ptr<BookmarkDB>{ new BookmarkDB };
createWidgets(url);
createActions();
createMenus();
createConnections();
createLayout();
view->load(url);
startRequest(url);
pageTimer->start(1000 * 120);
loadBookmarks();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createWidgets(const QUrl &url)
{
view = new QWebView(this);
view->load(url);
locationEdit = new QLineEdit(this);
locationEdit->setSizePolicy(QSizePolicy::Expanding,
locationEdit->sizePolicy().verticalPolicy());
toolBar = addToolBar(tr("Navigation"));
progressBar = new QProgressBar(this);
progressBar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
pageTimer = new QTimer(this);
}
void MainWindow::createActions()
{
exitAction = new QAction(tr("Exit"), this);
exitAction->setShortcut(tr("Ctrl+Q"));
exitAction->setStatusTip(tr("Exit"));
showHistoryAction = new QAction(tr("Show web history"), this);
showHistoryAction->setShortcut(tr("Ctrl+H"));
showHistoryAction->setStatusTip(tr("Show history"));
clearHistoryAction = new QAction(tr("Clear history"), this);
clearHistoryAction->setShortcut(tr("Ctrl+D"));
clearHistoryAction->setStatusTip(tr("Clear history"));
addNewBookmarkAction = new QAction(tr("Add new bookmark"), this);
addNewBookmarkAction->setShortcut(tr("Ctrl+N"));
addNewBookmarkAction->setStatusTip(tr("Add new bookmark"));
hostInfoAction = new QAction(tr("Show host info"), this);
hostInfoAction->setShortcut(tr("Ctrl+I"));
hostInfoAction->setStatusTip(tr("Show host info"));
toolBar->addAction(view->pageAction(QWebPage::Back));
toolBar->addAction(view->pageAction(QWebPage::Forward));
}
void MainWindow::createMenus()
{
fileMenu = menuBar()->addMenu(tr("File"));
fileMenu->addAction(hostInfoAction);
fileMenu->addSeparator();
fileMenu->addAction(exitAction);
pageHistoryMenu = menuBar()->addMenu(tr("Page history"));
pageHistoryMenu->addAction(showHistoryAction);
pageHistoryMenu->addAction(clearHistoryAction);
pageHistoryMenu->addSeparator();
bookmarkMenu = menuBar()->addMenu(tr("Bookmarks"));
bookmarkMenu->addAction(addNewBookmarkAction);
bookmarkMenu->addSeparator();
showLastAddedBookMarks = bookmarkMenu->addMenu("Lastly added");
}
void MainWindow::createConnections()
{
connect(view, SIGNAL(loadProgress(int)), progressBar, SLOT(setValue(int)));
connect(view, SIGNAL(titleChanged(QString)), SLOT(setBrowserTitle()));
connect(view, SIGNAL(loadProgress(int)), SLOT(setProgress(int)));
connect(view, SIGNAL(loadFinished(bool)), SLOT(finishedLoading(bool)));
connect(locationEdit, SIGNAL(returnPressed()), SLOT(changePage()));
connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));
connect(pageHistoryMenu, SIGNAL(aboutToShow()), SLOT(setWebHistory()));
connect(pageTimer, SIGNAL(timeout()), SLOT(networkTimeOut()));
connect(hostInfoAction, SIGNAL(triggered()), this, SLOT(showHostInfo()));
connect(showHistoryAction, SIGNAL(triggered()), this,
SLOT(showWebHistory()));
connect(clearHistoryAction, SIGNAL(triggered()), this,
SLOT(clearWebHistory()));
/*
connect(view->pageAction(QWebPage::Back), SIGNAL(triggered()), this,
SLOT(getBackPageUrl()));
connect(view->pageAction(QWebPage::Forward), SIGNAL(triggered()), this,
SLOT(getForwardPageUrl()));
*/
connect(addNewBookmarkAction, SIGNAL(triggered()), SLOT(addNewBookmark()));
}
void MainWindow::createLayout()
{
toolBar->addWidget(locationEdit);
toolBar->addWidget(progressBar);
setCentralWidget(view);
setUnifiedTitleAndToolBarOnMac(true);
view->setAttribute(Qt::WA_DeleteOnClose);
resize(1000, 800);
}
void MainWindow::changePage()
{
// every time a user enters a url to the address field
// this function is called. It starts the request and timer.
// The timer is set to trigger after 120 s and stops
// the loading
QUrl url = QUrl::fromUserInput(locationEdit->text());
if (url.isValid() && !url.isEmpty()) {
view->load(url);
startRequest(url);
view->setFocus();
pageTimer->start(1000 * 120);
} else {
view->stop();
view->setHtml("<h3><font color=red>Given url (" + url.toString() +
") is not valid."
"</font></h3><br>" +
url.errorString() + "<br>" +
"Please check that you have given a right url.");
}
}
void MainWindow::setBrowserTitle()
{
if (progress <= 0 || progress >= 100)
setWindowTitle(view->title());
else
setWindowTitle(QString("%1 (%2%)").arg(view->title()).arg(progress));
}
void MainWindow::setProgress(int value)
{
// update progress bar
progress = value;
setBrowserTitle();
}
void MainWindow::finishedLoading(bool ok)
{
// Each valid page is being stored to a container
// for later use so that the last visited page is
// the first item on the stack.
if (ok) {
progress = 100;
setBrowserTitle();
locationEdit->setText(view->url().toString());
if (!view->url().isEmpty() && !view->title().isEmpty()) {
if (visitedUrls.contains(qMakePair(view->url(), view->title()))) {
visitedUrls.prepend(visitedUrls.takeAt(visitedUrls.indexOf(
qMakePair(view->url(), view->title()))));
} else {
visitedUrls.prepend(qMakePair(view->url(), view->title()));
}
updatePageHistoryActionList();
}
}
pageTimer->stop();
}
void MainWindow::updatePageHistoryActionList()
{
// Update web history menu.
// Restrict the content to 10 items and set
// a shortcut to each item.
if (!pageHistoryItems.isEmpty())
for (const auto &item : pageHistoryItems) {
pageHistoryMenu->removeAction(item);
}
pageHistoryItems.clear();
QFontMetrics fontMetrics(font());
int k = 0;
while (k < visitedUrls.count() && k < MaxWebHistoryItems()) {
QString title =
fontMetrics.elidedText(visitedUrls.at(k).second, Qt::ElideRight,
pageHistoryMenu->maximumWidth());
QAction *action = new QAction(title, pageHistoryMenu);
action->setData(visitedUrls.at(k).first);
action->setShortcut(tr("Ctrl+%1").arg(k));
connect(action, SIGNAL(triggered()), SLOT(openPageHistoryUrl()));
pageHistoryItems.append(action);
k++;
}
}
void MainWindow::setWebHistory()
{
for (const auto &item : pageHistoryItems) {
pageHistoryMenu->addAction(item);
}
}
void MainWindow::openPageHistoryUrl()
{
QAction *action = qobject_cast<QAction *>(QObject::sender());
view->load(action->data().toUrl());
}
void MainWindow::networkTimeOut()
{
view->stop();
view->setHtml(
"<h1><font color=red>Network timeout (> 120 s)</h1>"
"<h2>Failed to connect to the network</font></h2>"
"Perhaps the proxy settings are wrong, or maybe a proxy is needed.");
}
void MainWindow::showHostInfo()
{
// Provide info of the host IP-addresses
QList<QNetworkInterface> iList = QNetworkInterface::allInterfaces();
QString info;
for (const auto &item : iList) {
info += "<p><strong>Interface " + item.humanReadableName() +
" </strong></p>";
QList<QNetworkAddressEntry> addressList = item.addressEntries();
for (const auto &address : addressList) {
info += "   IP-Address: " + address.ip().toString() +
"<br>";
}
info += "<br>";
}
view->stop();
view->setHtml(info);
}
void MainWindow::showWebHistory()
{
webHistoryView = std::unique_ptr<WebHistory>{ new WebHistory(visitedUrls) };
connect(webHistoryView->getView(), SIGNAL(doubleClicked(QModelIndex)),
SLOT(openClicked(QModelIndex)));
}
void MainWindow::clearWebHistory()
{
QWebHistory *history = view->history();
history->clear();
for (const auto &item : pageHistoryItems) {
pageHistoryMenu->removeAction(item);
}
pageHistoryItems.clear();
visitedUrls.clear();
}
void MainWindow::addNewBookmark()
{
bookmarkDB->addBookmark(view->title(), view->url());
QAction *action = new QAction(view->title(), showLastAddedBookMarks);
action->setData(view->url());
connect(action, SIGNAL(triggered()), SLOT(openPageHistoryUrl()));
showLastAddedBookMarks->addAction(action);
bookmarks.append(action);
}
void MainWindow::loadBookmarks()
{
auto bm = bookmarkDB->getBookmarks();
if (!bm.isEmpty()) {
auto iter = bm.constBegin();
while (iter != bm.constEnd()) {
QAction *action = new QAction(iter.key(), showLastAddedBookMarks);
action->setData(iter.value());
connect(action, SIGNAL(triggered()), SLOT(openPageHistoryUrl()));
showLastAddedBookMarks->addAction(action);
bookmarks.append(action);
++iter;
}
}
}
void MainWindow::httpFinished()
{
// If a web page loading fails, give an error.
if (reply->error()) {
view->stop();
QString title =
tr("Error loading page: %1").arg(reply->url().toString());
QString html = QString(
"<html>"
" <head>"
" <title>%1</title>"
" </head>"
" <body>"
" <h1><font color=red>%2</font></h1>"
" <h2>When connecting to: %3.</h2>"
" <ul>"
" <li>Check the address for errors such as "
"<b>ww</b>.example.com"
" instead of <b>www</b>.example.com</li>"
" <li>If the address is correct, try checking the network"
" connection.</li>"
" <li>If your computer or network is protected by a firewall "
"or"
" proxy, make sure that the browser demo is permitted to "
"access"
" the network.</li>"
" </ul>"
" <br/><br/>"
" </div>"
" </body>"
"</html>")
.arg(title)
.arg(reply->errorString())
.arg(reply->url().toString());
view->setHtml(html, reply->url());
if (!visitedUrls.isEmpty()) {
visitedUrls.removeFirst();
}
}
reply->deleteLater();
}
void MainWindow::startRequest(const QUrl &url)
{
QNetworkRequest request(url);
reply = view->page()->networkAccessManager()->get(request);
connect(reply, SIGNAL(finished()), this, SLOT(httpFinished()));
}
void MainWindow::openClicked(QModelIndex index)
{
view->load(webHistoryView->getUrlForItem(index));
}