1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-23 02:32:10 +02:00
falkonOfficial/src/webview/webview.cpp

582 lines
15 KiB
C++
Raw Normal View History

2011-03-03 18:29:20 +01:00
/* ============================================================
* QupZilla - WebKit based browser
* Copyright (C) 2010-2012 David Rosca <nowrep@gmail.com>
2011-03-03 18:29:20 +01:00
*
* 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 3 of the License, or
* (at your option) any later version.
*
* 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/>.
* ============================================================ */
2011-03-02 16:57:41 +01:00
#include "webview.h"
#include "webpage.h"
#include "webhistorywrapper.h"
2011-03-02 16:57:41 +01:00
#include "mainapplication.h"
#include "globalfunctions.h"
#include "iconprovider.h"
#include "historymodel.h"
#include "autofillmodel.h"
#include "downloadmanager.h"
#include "sourceviewer.h"
#include "siteinfo.h"
#include "searchenginesmanager.h"
2011-03-02 16:57:41 +01:00
WebView::WebView(QWidget* parent)
: QWebView(parent)
, m_currentZoom(100)
, m_isLoading(false)
, m_progress(0)
2011-03-02 16:57:41 +01:00
{
connect(this, SIGNAL(loadStarted()), this, SLOT(slotLoadStarted()));
connect(this, SIGNAL(loadProgress(int)), this, SLOT(slotLoadProgress(int)));
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished()));
2011-03-02 16:57:41 +01:00
connect(this, SIGNAL(iconChanged()), this, SLOT(slotIconChanged()));
// Zoom levels same as in firefox
2011-03-02 16:57:41 +01:00
m_zoomLevels << 30 << 50 << 67 << 80 << 90 << 100 << 110 << 120 << 133 << 150 << 170 << 200 << 240 << 300;
qApp->installEventFilter(this);
2011-03-02 16:57:41 +01:00
}
QIcon WebView::icon() const
2011-03-02 16:57:41 +01:00
{
if (url().scheme() == "qupzilla") {
return QIcon(":icons/qupzilla.png");
}
if (!QWebView::icon().isNull()) {
return QWebView::icon();
}
2011-03-02 16:57:41 +01:00
if (!m_siteIcon.isNull() && m_siteIconUrl.host() == url().host()) {
return m_siteIcon;
}
2011-03-02 16:57:41 +01:00
return _iconForUrl(url());
2011-03-02 16:57:41 +01:00
}
QString WebView::title() const
{
QString title = QWebView::title();
if (title.isEmpty()) {
title = url().toString(QUrl::RemoveFragment);
}
if (title.isEmpty() || title == "about:blank") {
return tr("No Named Page");
}
return title;
}
QUrl WebView::url() const
2011-03-02 16:57:41 +01:00
{
QUrl returnUrl = QWebView::url();
2011-03-02 16:57:41 +01:00
if (returnUrl.isEmpty()) {
returnUrl = m_aboutToLoadUrl;
2011-03-02 16:57:41 +01:00
}
return returnUrl;
2011-03-02 16:57:41 +01:00
}
void WebView::load(const QUrl &url)
2011-03-02 16:57:41 +01:00
{
if (url.scheme() == "javascript") {
page()->mainFrame()->evaluateJavaScript(url.toString());
2011-03-02 16:57:41 +01:00
return;
}
2011-03-02 16:57:41 +01:00
if (isUrlValid(url)) {
QWebView::load(url);
emit urlChanged(url);
m_aboutToLoadUrl = url;
return;
}
QUrl searchUrl = mApp->searchEnginesManager()->searchUrl(url.toString());
QWebView::load(searchUrl);
emit urlChanged(searchUrl);
m_aboutToLoadUrl = searchUrl;
2011-03-02 16:57:41 +01:00
}
bool WebView::isLoading() const
{
return m_isLoading;
}
int WebView::loadProgress() const
2011-03-02 16:57:41 +01:00
{
return m_progress;
2011-03-02 16:57:41 +01:00
}
bool WebView::isUrlValid(const QUrl &url)
2011-03-02 16:57:41 +01:00
{
if (url.scheme() == "data" || url.scheme() == "qrc" || url.scheme() == "mailto") {
return true;
2011-03-02 16:57:41 +01:00
}
if (url.scheme() == "qupzilla" || url.scheme() == "file") {
return !url.path().isEmpty();
}
2011-03-02 16:57:41 +01:00
if (url.isValid() && !url.host().isEmpty() && !url.scheme().isEmpty()) {
return true;
2011-03-02 16:57:41 +01:00
}
return false;
2011-03-02 16:57:41 +01:00
}
QUrl WebView::guessUrlFromString(const QString &string)
{
QString trimmedString = string.trimmed();
// Check the most common case of a valid url with scheme and host first
QUrl url = QUrl::fromEncoded(trimmedString.toUtf8(), QUrl::TolerantMode);
if (url.isValid() && !url.scheme().isEmpty() && !url.host().isEmpty()) {
2011-03-02 16:57:41 +01:00
return url;
}
2011-03-02 16:57:41 +01:00
// Absolute files that exists
if (QDir::isAbsolutePath(trimmedString) && QFile::exists(trimmedString)) {
2011-03-02 16:57:41 +01:00
return QUrl::fromLocalFile(trimmedString);
}
2011-03-02 16:57:41 +01:00
// If the string is missing the scheme or the scheme is not valid prepend a scheme
QString scheme = url.scheme();
if (scheme.isEmpty() || scheme.contains(QLatin1Char('.')) || scheme == QLatin1String("localhost")) {
// Do not do anything for strings such as "foo", only "foo.com"
int dotIndex = trimmedString.indexOf(QLatin1Char('.'));
if (dotIndex != -1 || trimmedString.startsWith(QLatin1String("localhost"))) {
const QString hostscheme = trimmedString.left(dotIndex).toLower();
QByteArray scheme = (hostscheme == QLatin1String("ftp")) ? "ftp" : "http";
trimmedString = QLatin1String(scheme) + QLatin1String("://") + trimmedString;
}
url = QUrl::fromEncoded(trimmedString.toUtf8(), QUrl::TolerantMode);
}
if (url.isValid()) {
2011-03-02 16:57:41 +01:00
return url;
}
2011-03-02 16:57:41 +01:00
return QUrl();
}
void WebView::addNotification(QWidget* notif)
{
emit showNotification(notif);
}
void WebView::applyZoom()
2011-03-02 16:57:41 +01:00
{
setZoomFactor(qreal(m_currentZoom) / 100.0);
}
2011-03-02 16:57:41 +01:00
void WebView::zoomIn()
{
int i = m_zoomLevels.indexOf(m_currentZoom);
2011-03-02 16:57:41 +01:00
if (i < m_zoomLevels.count() - 1) {
m_currentZoom = m_zoomLevels[i + 1];
2011-03-02 16:57:41 +01:00
}
applyZoom();
}
2011-03-02 16:57:41 +01:00
void WebView::zoomOut()
{
int i = m_zoomLevels.indexOf(m_currentZoom);
if (i > 0) {
m_currentZoom = m_zoomLevels[i - 1];
}
2011-03-02 16:57:41 +01:00
applyZoom();
}
2011-03-02 16:57:41 +01:00
void WebView::zoomReset()
{
m_currentZoom = 100;
applyZoom();
}
2011-03-02 16:57:41 +01:00
void WebView::reload()
{
if (QWebView::url().isEmpty() && !m_aboutToLoadUrl.isEmpty()) {
load(m_aboutToLoadUrl);
return;
2011-03-02 16:57:41 +01:00
}
QWebView::reload();
}
void WebView::back()
{
QWebHistory* history = page()->history();
if (WebHistoryWrapper::canGoBack(history)) {
WebHistoryWrapper::goBack(history);
emit urlChanged(url());
emit iconChanged();
2011-03-02 16:57:41 +01:00
}
}
2011-03-02 16:57:41 +01:00
void WebView::forward()
{
QWebHistory* history = page()->history();
if (WebHistoryWrapper::canGoForward(history)) {
WebHistoryWrapper::goForward(history);
emit urlChanged(url());
emit iconChanged();
2011-03-02 16:57:41 +01:00
}
}
2011-03-02 16:57:41 +01:00
void WebView::selectAll()
{
triggerPageAction(QWebPage::SelectAll);
2011-03-02 16:57:41 +01:00
}
void WebView::slotLoadStarted()
{
m_isLoading = true;
m_progress = 0;
}
void WebView::slotLoadProgress(int progress)
2011-03-02 16:57:41 +01:00
{
m_progress = progress;
2011-03-02 16:57:41 +01:00
}
void WebView::slotLoadFinished()
2011-03-02 16:57:41 +01:00
{
m_isLoading = false;
m_progress = 100;
if (m_lastUrl != url()) {
mApp->history()->addHistoryEntry(this);
2011-03-02 16:57:41 +01:00
}
mApp->autoFill()->completePage(qobject_cast<WebPage*>(page()));
m_lastUrl = url();
}
void WebView::slotIconChanged()
{
m_siteIcon = icon();
m_siteIconUrl = url();
2011-03-02 16:57:41 +01:00
}
void WebView::openUrlInNewWindow()
{
2011-03-17 17:03:04 +01:00
if (QAction* action = qobject_cast<QAction*>(sender())) {
mApp->makeNewWindow(Qz::BW_NewWindow, action->data().toUrl());
2011-03-02 16:57:41 +01:00
}
}
void WebView::sendLinkByMail()
{
2011-03-17 17:03:04 +01:00
if (QAction* action = qobject_cast<QAction*>(sender())) {
QDesktopServices::openUrl(QUrl("mailto:?body=" + action->data().toString()));
2011-03-02 16:57:41 +01:00
}
}
void WebView::copyLinkToClipboard()
{
2011-03-17 17:03:04 +01:00
if (QAction* action = qobject_cast<QAction*>(sender())) {
2011-03-02 16:57:41 +01:00
QApplication::clipboard()->setText(action->data().toString());
}
}
void WebView::downloadLinkToDisk()
2011-03-02 16:57:41 +01:00
{
if (QAction* action = qobject_cast<QAction*>(sender())) {
DownloadManager* dManager = mApp->downManager();
QNetworkRequest request(action->data().toUrl());
dManager->download(request, qobject_cast<WebPage*>(page()), false);
}
2011-03-02 16:57:41 +01:00
}
void WebView::copyImageToClipboard()
{
triggerPageAction(QWebPage::CopyImageToClipboard);
}
void WebView::openActionUrl()
2011-03-02 16:57:41 +01:00
{
2011-03-17 17:03:04 +01:00
if (QAction* action = qobject_cast<QAction*>(sender())) {
load(action->data().toUrl());
2011-03-02 16:57:41 +01:00
}
}
void WebView::showSource(QWebFrame* frame, const QString &selectedHtml)
{
if (!frame) {
frame = page()->mainFrame();
2011-03-02 16:57:41 +01:00
}
SourceViewer* source = new SourceViewer(frame, selectedHtml);
qz_centerWidgetToParent(source, this);
source->show();
2011-03-02 16:57:41 +01:00
}
void WebView::showSiteInfo()
{
SiteInfo* s = new SiteInfo(this, this);
s->show();
2011-03-02 16:57:41 +01:00
}
void WebView::printPage(QWebFrame* frame)
2011-03-02 16:57:41 +01:00
{
QPrintPreviewDialog* dialog = new QPrintPreviewDialog(this);
dialog->resize(800, 750);
2011-03-02 16:57:41 +01:00
if (!frame) {
connect(dialog, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print(QPrinter*)));
}
else {
connect(dialog, SIGNAL(paintRequested(QPrinter*)), frame, SLOT(print(QPrinter*)));
}
dialog->exec();
dialog->deleteLater();
2011-03-02 16:57:41 +01:00
}
void WebView::copyText()
2011-03-02 16:57:41 +01:00
{
if (!selectedText().isEmpty()) {
QApplication::clipboard()->setText(selectedText());
}
2011-03-02 16:57:41 +01:00
}
QUrl WebView::lastUrl()
2011-03-02 16:57:41 +01:00
{
return m_lastUrl;
2011-03-02 16:57:41 +01:00
}
bool WebView::isMediaElement(const QWebElement &element)
{
return (element.tagName().toLower() == "video" || element.tagName().toLower() == "audio");
}
QMenu *WebView::createMediaContextMenu(const QWebHitTestResult &hitTest)
{
QMenu* menu = new QMenu(this);
m_mediaElement = hitTest.element();
bool paused = m_mediaElement.evaluateJavaScript("this.paused").toBool();
bool muted = m_mediaElement.evaluateJavaScript("this.muted").toBool();
QUrl videoUrl = m_mediaElement.evaluateJavaScript("this.currentSrc").toUrl();
menu->addAction(paused ? tr("&Play") : tr("&Pause"), this, SLOT(pauseMedia()))->setIcon(QIcon::fromTheme(paused ? "media-playback-start" : "media-playback-pause"));
menu->addAction(muted ? tr("Un&mute") : tr("&Mute"), this, SLOT(muteMedia()))->setIcon(QIcon::fromTheme(muted ? "audio-volume-high" : "audio-volume-high"));
menu->addSeparator();
menu->addAction(QIcon::fromTheme("edit-copy"), tr("&Copy Media Address"), this, SLOT(copyLinkToClipboard()))->setData(videoUrl);
menu->addAction(QIcon::fromTheme("mail-message-new"), tr("&Send Media Address"), this, SLOT(sendLinkByMail()))->setData(videoUrl);
menu->addAction(QIcon::fromTheme("download"), tr("&Download Media To Disk"), this, SLOT(downloadLinkToDisk()))->setData(videoUrl);
return menu;
}
void WebView::pauseMedia()
{
bool paused = m_mediaElement.evaluateJavaScript("this.paused").toBool();
if (paused) {
m_mediaElement.evaluateJavaScript("this.play()");
}
else {
m_mediaElement.evaluateJavaScript("this.pause()");
}
}
void WebView::muteMedia()
{
bool muted = m_mediaElement.evaluateJavaScript("this.muted").toBool();
if (muted) {
m_mediaElement.evaluateJavaScript("this.muted = false");
}
else {
m_mediaElement.evaluateJavaScript("this.muted = true");
}
}
void WebView::controlsMedia()
{
bool controls= m_mediaElement.evaluateJavaScript("this.controls").toBool();
if (controls) {
m_mediaElement.evaluateJavaScript("this.controls = false");
}
else {
m_mediaElement.evaluateJavaScript("this.controls = true");
}
}
2011-03-17 17:03:04 +01:00
void WebView::wheelEvent(QWheelEvent* event)
2011-03-02 16:57:41 +01:00
{
if (event->modifiers() & Qt::ControlModifier) {
int numDegrees = event->delta() / 8;
int numSteps = numDegrees / 15;
if (numSteps == 1) {
2011-03-02 16:57:41 +01:00
zoomIn();
}
else {
2011-03-02 16:57:41 +01:00
zoomOut();
}
2011-03-02 16:57:41 +01:00
event->accept();
return;
}
QWebView::wheelEvent(event);
}
void WebView::mousePressEvent(QMouseEvent* event)
{
switch (event->button()) {
case Qt::XButton1:
back();
event->accept();
break;
case Qt::XButton2:
forward();
event->accept();
break;
default:
QWebView::mousePressEvent(event);
break;
}
}
void WebView::keyPressEvent(QKeyEvent* event)
{
switch (event->key()) {
case Qt::Key_C:
if (event->modifiers() == Qt::ControlModifier) {
copyText();
event->accept();
return;
}
break;
case Qt::Key_A:
if (event->modifiers() == Qt::ControlModifier) {
selectAll();
event->accept();
return;
}
break;
default:
break;
}
QWebView::keyPressEvent(event);
}
void WebView::resizeEvent(QResizeEvent* event)
{
QWebView::resizeEvent(event);
emit viewportResized(page()->viewportSize());
}
void WebView::setZoom(int zoom)
{
m_currentZoom = zoom;
applyZoom();
}
///
// This function was taken and modified from QTestBrowser to fix bug #33 with flightradar24.com
// You can find original source and copyright here:
// http://gitorious.org/+qtwebkit-developers/webkit/qtwebkit/blobs/qtwebkit-2.2/Tools/QtTestBrowser/launcherwindow.cpp
///
bool WebView::eventFilter(QObject* obj, QEvent* event)
{
if (obj != this) {
return false;
}
if (event->type() == QEvent::MouseButtonPress ||
event->type() == QEvent::MouseButtonRelease ||
event->type() == QEvent::MouseButtonDblClick ||
event->type() == QEvent::MouseMove) {
QMouseEvent* ev = static_cast<QMouseEvent*>(event);
if (ev->type() == QEvent::MouseMove
&& !(ev->buttons() & Qt::LeftButton)) {
return false;
}
QTouchEvent::TouchPoint touchPoint;
touchPoint.setState(Qt::TouchPointMoved);
if ((ev->type() == QEvent::MouseButtonPress
|| ev->type() == QEvent::MouseButtonDblClick)) {
touchPoint.setState(Qt::TouchPointPressed);
}
else if (ev->type() == QEvent::MouseButtonRelease) {
touchPoint.setState(Qt::TouchPointReleased);
}
touchPoint.setId(0);
touchPoint.setScreenPos(ev->globalPos());
touchPoint.setPos(ev->pos());
touchPoint.setPressure(1);
// If the point already exists, update it. Otherwise create it.
if (m_touchPoints.size() > 0 && !m_touchPoints[0].id()) {
m_touchPoints[0] = touchPoint;
}
else if (m_touchPoints.size() > 1 && !m_touchPoints[1].id()) {
m_touchPoints[1] = touchPoint;
}
else {
m_touchPoints.append(touchPoint);
}
if (!m_touchPoints.isEmpty()) {
QEvent::Type type = QEvent::TouchUpdate;
if (m_touchPoints.size() == 1) {
if (m_touchPoints[0].state() == Qt::TouchPointReleased) {
type = QEvent::TouchEnd;
}
else if (m_touchPoints[0].state() == Qt::TouchPointPressed) {
type = QEvent::TouchBegin;
}
}
QTouchEvent touchEv(type);
touchEv.setTouchPoints(m_touchPoints);
QCoreApplication::sendEvent(page(), &touchEv);
// After sending the event, remove all touchpoints that were released
if (m_touchPoints[0].state() == Qt::TouchPointReleased) {
m_touchPoints.removeAt(0);
}
if (m_touchPoints.size() > 1 && m_touchPoints[1].state() == Qt::TouchPointReleased) {
m_touchPoints.removeAt(1);
}
}
return false;
}
return QWebView::eventFilter(obj, event);
}