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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
/* ============================================================
*
* This file is a part of the rekonq project
*
* Copyright (C) 2008-2010 by Andrea Diamantini <adjam7 at gmail dot com>
* Copyright (C) 2009-2010 by Lionel Chauvin <megabigbug@yahoo.fr>
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* ============================================================ */
// Self Includes
#include "webview.h"
#include "webview.moc"
// Auto Includes
#include "rekonq.h"
// Local Includes
#include "mainwindow.h"
#include "mainview.h"
#include "webpage.h"
#include "bookmarksmanager.h"
#include "searchengine.h"
// KDE Includes
#include <KService>
#include <KUriFilterData>
#include <KStandardShortcut>
#include <KMenu>
#include <KActionMenu>
#include <ktoolinvocation.h>
// Qt Includes
#include <QtCore/QDir>
#include <QtGui/QAction>
#include <QtGui/QContextMenuEvent>
#include <QtGui/QWheelEvent>
#include <QtGui/QMouseEvent>
#include <QtGui/QClipboard>
#include <QtGui/QKeyEvent>
#include <QtGui/QLayout>
#include <QtDBus/QDBusConnectionInterface>
#include <QtDBus/QDBusInterface>
#include <QtDBus/QDBusReply>
WebView::WebView(QWidget* parent)
: KWebView(parent, false)
, _mousePos(QPoint(0, 0))
, _scrollTimer(new QTimer(this))
, _VScrollSpeed(0)
, _HScrollSpeed(0)
, _canEnableAutoScroll(true)
, _isAutoScrollEnabled(false)
{
WebPage *page = new WebPage(this);
setPage(page);
// download system
connect(this, SIGNAL(linkShiftClicked(const KUrl &)), page, SLOT(downloadUrl(const KUrl &)));
connect(page, SIGNAL(downloadRequested(const QNetworkRequest &)), page, SLOT(downloadRequest(const QNetworkRequest &)));
// middle click || ctrl + click signal
connect(this, SIGNAL(linkMiddleOrCtrlClicked(const KUrl &)), this, SLOT(loadUrlInNewTab(const KUrl &)));
// loadUrl signal
connect(this, SIGNAL(loadUrl(const KUrl &, const Rekonq::OpenType &)),
Application::instance(), SLOT(loadUrl(const KUrl &, const Rekonq::OpenType &)));
// scrolling timer
connect(_scrollTimer, SIGNAL(timeout()), this, SLOT(scrollFrameChanged()));
_scrollTimer->setInterval(100);
}
WebView::~WebView()
{
delete _scrollTimer;
disconnect();
}
WebPage *WebView::page()
{
WebPage *page = qobject_cast<WebPage *>(KWebView::page());
return page;
}
void WebView::contextMenuEvent(QContextMenuEvent *event)
{
QWebHitTestResult result = page()->mainFrame()->hitTestContent(event->pos());
MainWindow *mainwindow = Application::instance()->mainWindow();
KMenu menu(this);
QAction *a;
// is a link?
if (!result.linkUrl().isEmpty())
{
// link actions
a = new KAction(KIcon("tab-new"), i18n("Open in New &Tab"), this);
a->setData(result.linkUrl());
connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewTab()));
menu.addAction(a);
a = new KAction(KIcon("window-new"), i18n("Open in New &Window"), this);
a->setData(result.linkUrl());
connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewWindow()));
menu.addAction(a);
menu.addAction(pageAction(KWebPage::DownloadLinkToDisk));
menu.addAction(pageAction(KWebPage::CopyLinkToClipboard));
menu.addSeparator();
}
// is content editable && selected? Add CUT
if (result.isContentEditable() && result.isContentSelected())
{
// actions for text selected in field
menu.addAction(pageAction(KWebPage::Cut));
}
// is content selected) Add COPY
if (result.isContentSelected())
{
a = pageAction(KWebPage::Copy);
if (!result.linkUrl().isEmpty())
a->setText(i18n("Copy Text")); //for link
else
a->setText(i18n("Copy"));
menu.addAction(a);
}
// is content editable? Add PASTE
if (result.isContentEditable())
{
menu.addAction(pageAction(KWebPage::Paste));
}
// is content selected? Add SEARCH actions
if (result.isContentSelected())
{
KActionMenu *searchMenu = new KActionMenu(KIcon("edit-find"), i18n("Search with"), this);
foreach(KService::Ptr engine, SearchEngine::favorites())
{
a = new KAction(engine->name(), this);
a->setIcon(Application::icon(SearchEngine::buildQuery(engine, "")));
a->setData(engine->entryPath());
connect(a, SIGNAL(triggered(bool)), this, SLOT(search()));
searchMenu->addAction(a);
}
if (!searchMenu->menu()->isEmpty())
{
menu.addAction(searchMenu);
}
menu.addSeparator();
// TODO Add translate, show translation
}
// is an image?
if (!result.pixmap().isNull())
{
menu.addSeparator();
// TODO remove copy_this_image action
a = new KAction(KIcon("view-media-visualization"), i18n("&View Image"), this);
a->setData(result.imageUrl());
connect(a, SIGNAL(triggered(Qt::MouseButtons, Qt::KeyboardModifiers)), this, SLOT(viewImage(Qt::MouseButtons, Qt::KeyboardModifiers)));
menu.addAction(a);
menu.addAction(pageAction(KWebPage::DownloadImageToDisk));
menu.addAction(pageAction(KWebPage::CopyImageToClipboard));
menu.addSeparator();
}
// Open url text in new tab/window
if (result.linkUrl().isEmpty())
{
QString text = selectedText();
if (text.startsWith(QL1S("http://"))
|| text.startsWith(QL1S("https://"))
|| text.startsWith(QL1S("www."))
)
{
QString truncatedURL = text;
if (text.length() > 18)
{
truncatedURL.truncate(15);
truncatedURL += "...";
}
//open selected text url in a new tab
a = new KAction(KIcon("tab-new"), i18n("Open '%1' in New Tab", truncatedURL), this);
a->setData(QUrl(text));
connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewTab()));
menu.addAction(a);
//open selected text url in a new window
a = new KAction(KIcon("window-new"), i18n("Open '%1' in New Window", truncatedURL), this);
a->setData(QUrl(text));
connect(a, SIGNAL(triggered(bool)), this, SLOT(openLinkInNewWindow()));
menu.addAction(a);
menu.addSeparator();
}
}
// page actions
if (!result.isContentSelected() && result.linkUrl().isEmpty())
{
// navigation
QWebHistory *history = page()->history();
if (history->canGoBack())
{
menu.addAction(pageAction(KWebPage::Back));
}
if (history->canGoForward())
{
menu.addAction(pageAction(KWebPage::Forward));
}
menu.addAction(mainwindow->actionByName("view_redisplay"));
if (result.pixmap().isNull())
{
menu.addSeparator();
menu.addAction(mainwindow->actionByName("new_tab"));
menu.addAction(mainwindow->actionByName("new_window"));
menu.addSeparator();
//Frame
KActionMenu *frameMenu = new KActionMenu(i18n("Current Frame"), this);
frameMenu->addAction(pageAction(KWebPage::OpenFrameInNewWindow));
a = new KAction(KIcon("document-print-frame"), i18n("Print Frame"), this);
connect(a, SIGNAL(triggered()), this, SLOT(printFrame()));
frameMenu->addAction(a);
menu.addAction(frameMenu);
menu.addSeparator();
// Page Actions
menu.addAction(pageAction(KWebPage::SelectAll));
menu.addAction(mainwindow->actionByName(KStandardAction::name(KStandardAction::SaveAs)));
if (ReKonfig::kgetList())
{
a = new KAction(KIcon("kget"), i18n("List All Links"), this);
connect(a, SIGNAL(triggered(bool)), page(), SLOT(downloadAllContentsWithKGet()));
menu.addAction(a);
}
menu.addAction(mainwindow->actionByName("page_source"));
a = new KAction(KIcon("layer-visible-on"), i18n("Inspect Element"), this);
connect(a, SIGNAL(triggered(bool)), this, SLOT(inspect()));
menu.addAction(a);
a = Application::bookmarkProvider()->actionByName("rekonq_add_bookmark");
menu.addAction(a);
}
if (mainwindow->isFullScreen())
{
menu.addSeparator();
menu.addAction(mainwindow->actionByName("fullscreen"));
}
}
menu.exec(mapToGlobal(event->pos()));
}
void WebView::mousePressEvent(QMouseEvent *event)
{
if (_isAutoScrollEnabled)
{
setCursor(Qt::ArrowCursor);
_VScrollSpeed = 0;
_HScrollSpeed = 0;
_scrollTimer->stop();
_isAutoScrollEnabled = false;
return;
}
QWebHitTestResult result = page()->mainFrame()->hitTestContent(event->pos());
_canEnableAutoScroll = ReKonfig::autoScroll() && !result.isContentEditable() && result.linkUrl().isEmpty();
switch (event->button())
{
case Qt::XButton1:
triggerPageAction(KWebPage::Back);
break;
case Qt::XButton2:
triggerPageAction(KWebPage::Forward);
break;
case Qt::MidButton:
if (_canEnableAutoScroll && !_isAutoScrollEnabled)
{
setCursor(KIcon("transform-move").pixmap(32));
_clickPos = event->pos();
_isAutoScrollEnabled = true;
}
break;
default:
break;
};
KWebView::mousePressEvent(event);
}
void WebView::mouseMoveEvent(QMouseEvent *event)
{
_mousePos = event->pos();
if (_isAutoScrollEnabled)
{
QPoint r = _mousePos - _clickPos;
_HScrollSpeed = r.x() / 2; // you are too fast..
_VScrollSpeed = r.y() / 2;
if (!_scrollTimer->isActive())
_scrollTimer->start();
return;
}
if (Application::instance()->mainWindow()->isFullScreen())
{
if (event->pos().y() >= 0 && event->pos().y() <= 4)
{
Application::instance()->mainWindow()->setWidgetsVisible(true);
}
else
{
Application::instance()->mainWindow()->setWidgetsVisible(false);
}
}
KWebView::mouseMoveEvent(event);
}
void WebView::search()
{
KAction *a = qobject_cast<KAction*>(sender());
KService::Ptr engine = KService::serviceByDesktopPath(a->data().toString());
KUrl urlSearch = KUrl(SearchEngine::buildQuery(engine, selectedText()));
emit loadUrl(urlSearch, Rekonq::NewCurrentTab);
}
void WebView::printFrame()
{
Application::instance()->mainWindow()->printRequested(page()->currentFrame());
}
void WebView::viewImage(Qt::MouseButtons buttons, Qt::KeyboardModifiers modifiers)
{
KAction *a = qobject_cast<KAction*>(sender());
KUrl url(a->data().toUrl());
if (modifiers & Qt::ControlModifier || buttons == Qt::MidButton)
{
emit loadUrl(url, Rekonq::SettingOpenTab);
}
else
{
emit loadUrl(url, Rekonq::CurrentTab);
}
}
void WebView::openLinkInNewWindow()
{
KAction *a = qobject_cast<KAction*>(sender());
KUrl url(a->data().toUrl());
emit loadUrl(url, Rekonq::NewWindow);
}
void WebView::openLinkInNewTab()
{
KAction *a = qobject_cast<KAction*>(sender());
KUrl url(a->data().toUrl());
emit loadUrl(url, Rekonq::SettingOpenTab);
}
void WebView::keyPressEvent(QKeyEvent *event)
{
if (event->modifiers() == Qt::ControlModifier)
{
if (event->key() == Qt::Key_C)
{
triggerPageAction(KWebPage::Copy);
return;
}
if (event->key() == Qt::Key_A)
{
triggerPageAction(KWebPage::SelectAll);
return;
}
}
if (!_canEnableAutoScroll)
{
KWebView::keyPressEvent(event);
return;
}
// Auto Scrolling
if (event->modifiers() == Qt::ShiftModifier)
{
if (event->key() == Qt::Key_Up)
{
_VScrollSpeed--;
if (!_scrollTimer->isActive())
_scrollTimer->start();
return;
}
if (event->key() == Qt::Key_Down)
{
_VScrollSpeed++;
if (!_scrollTimer->isActive())
_scrollTimer->start();
return;
}
if (event->key() == Qt::Key_Right)
{
_HScrollSpeed++;
if (!_scrollTimer->isActive())
_scrollTimer->start();
return;
}
if (event->key() == Qt::Key_Left)
{
_HScrollSpeed--;
if (!_scrollTimer->isActive())
_scrollTimer->start();
return;
}
if (_scrollTimer->isActive())
{
_scrollTimer->stop();
}
else
{
if (_VScrollSpeed || _HScrollSpeed)
_scrollTimer->start();
}
}
KWebView::keyPressEvent(event);
}
void WebView::wheelEvent(QWheelEvent *event)
{
KWebView::wheelEvent(event);
// Sync with the zoom slider
if (event->modifiers() == Qt::ControlModifier)
{
// Limits of the slider
if (zoomFactor() > 1.9)
setZoomFactor(1.9);
else if (zoomFactor() < 0.1)
setZoomFactor(0.1);
// Round the factor (Fix slider's end value)
int newFactor = zoomFactor() * 10;
if ((zoomFactor() * 10 - newFactor) > 0.5)
newFactor++;
emit zoomChanged((qreal)newFactor / 10);
}
}
void WebView::inspect()
{
QAction *a = Application::instance()->mainWindow()->actionByName("web_inspector");
if (a && !a->isChecked())
a->trigger();
pageAction(QWebPage::InspectElement)->trigger();
}
void WebView::loadUrlInNewTab(const KUrl &url)
{
emit loadUrl(url, Rekonq::SettingOpenTab);
}
void WebView::scrollFrameChanged()
{
// do the scrolling
page()->currentFrame()->scroll(_HScrollSpeed, _VScrollSpeed);
// check if we reached the end
int y = page()->currentFrame()->scrollPosition().y();
if (y == 0 || y == page()->currentFrame()->scrollBarMaximum(Qt::Vertical))
_VScrollSpeed = 0;
int x = page()->currentFrame()->scrollPosition().x();
if (x == 0 || x == page()->currentFrame()->scrollBarMaximum(Qt::Horizontal))
_HScrollSpeed = 0;
}
|