1 | /* |
2 | * This file is part of the KDE project. |
3 | * |
4 | * Copyright (C) 2007 Trolltech ASA |
5 | * Copyright (C) 2008 Urs Wolfer <uwolfer @ kde.org> |
6 | * Copyright (C) 2008 Laurent Montel <montel@kde.org> |
7 | * Copyright (C) 2008 Michael Howell <mhowell123@gmail.com> |
8 | * Copyright (C) 2009 Dawit Alemayehu <adawit @ kde.org> |
9 | * |
10 | * This library is free software; you can redistribute it and/or |
11 | * modify it under the terms of the GNU Library General Public |
12 | * License as published by the Free Software Foundation; either |
13 | * version 2 of the License, or (at your option) any later version. |
14 | * |
15 | * This library is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 | * Library General Public License for more details. |
19 | * |
20 | * You should have received a copy of the GNU Library General Public License |
21 | * along with this library; see the file COPYING.LIB. If not, write to |
22 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
23 | * Boston, MA 02110-1301, USA. |
24 | * |
25 | */ |
26 | |
27 | #ifndef KWEBVIEW_P_H |
28 | #define KWEBVIEW_P_H |
29 | |
30 | #include <QtCore/QEvent> |
31 | #include <QtGui/QClipboard> |
32 | #include <QtGui/QApplication> |
33 | #include <QtWebKit/QWebFrame> |
34 | #include <QtWebKit/QWebElement> |
35 | |
36 | #include <kurl.h> |
37 | #include <kurifilter.h> |
38 | |
39 | #include "kwebpage.h" |
40 | |
41 | #define QL1S(x) QLatin1String(x) |
42 | |
43 | template <class T> |
44 | class KWebViewPrivate |
45 | { |
46 | public: |
47 | KWebViewPrivate(T *parent) |
48 | : q(parent), |
49 | keyboardModifiers(Qt::NoModifier) , |
50 | pressedButtons(Qt::NoButton) |
51 | { |
52 | } |
53 | |
54 | bool isExternalContentAllowed() |
55 | { |
56 | KWebPage *webPage = qobject_cast<KWebPage*>(q->page()); |
57 | if (webPage) { |
58 | return webPage->isExternalContentAllowed(); |
59 | } |
60 | |
61 | return false; |
62 | } |
63 | |
64 | void setAllowExternalContent(bool allow) |
65 | { |
66 | KWebPage *webPage = qobject_cast<KWebPage*>(q->page()); |
67 | if (webPage) { |
68 | webPage->setAllowExternalContent(allow); |
69 | } |
70 | } |
71 | |
72 | bool wheelEvent(int delta) |
73 | { |
74 | if (QApplication::keyboardModifiers() & Qt::ControlModifier) { |
75 | const int numDegrees = delta / 8; |
76 | const int numSteps = numDegrees / 15; |
77 | q->setZoomFactor(q->zoomFactor() + numSteps * 0.1); |
78 | return true; |
79 | } |
80 | |
81 | return false; |
82 | } |
83 | |
84 | bool mouseReleased(const QPoint &pos) |
85 | { |
86 | hitTest = q->page()->mainFrame()->hitTestContent(pos); |
87 | const QUrl url = hitTest.linkUrl(); |
88 | |
89 | if (!url.isEmpty()) { |
90 | if ((pressedButtons & Qt::MidButton) || |
91 | ((pressedButtons & Qt::LeftButton) && (keyboardModifiers & Qt::ControlModifier))) { |
92 | emit q->linkMiddleOrCtrlClicked(url); |
93 | return true; |
94 | } |
95 | |
96 | if ((pressedButtons & Qt::LeftButton) && (keyboardModifiers & Qt::ShiftModifier)) { |
97 | emit q->linkShiftClicked(url); |
98 | return true; |
99 | } |
100 | } |
101 | |
102 | return false; |
103 | } |
104 | |
105 | bool handleUrlPasteFromClipboard(QEvent* event) |
106 | { |
107 | QWebPage *page = q->page(); |
108 | if ((pressedButtons & Qt::MidButton) && page) { |
109 | |
110 | // WORKAROUND: Let the page handle the event first so that middle clicking |
111 | // on scroll bars does not cause navigation to a url that might have been |
112 | // copied into the selection clipboard. |
113 | page->event(event); |
114 | if (event->isAccepted()) |
115 | return true; |
116 | |
117 | if (!hitTest.linkUrl().isValid() && !hitTest.isContentEditable() && !page->isModified()) { |
118 | QString subType (QL1S("plain" )); |
119 | const QString clipboardText = QApplication::clipboard()->text(subType, QClipboard::Selection); |
120 | if (!clipboardText.isEmpty()) { |
121 | KUriFilterData data (clipboardText.left(250).trimmed()); |
122 | data.setCheckForExecutables(false); // don't allow executables... |
123 | if (KUriFilter::self()->filterUri(data, QStringList(QL1S("kshorturifilter" )))) { |
124 | switch (data.uriType()) { |
125 | case KUriFilterData::LocalFile: |
126 | case KUriFilterData::LocalDir: |
127 | case KUriFilterData::NetProtocol: |
128 | emit q->selectionClipboardUrlPasted(data.uri(), QString()); |
129 | #ifndef KDE_NO_DEPRECATED |
130 | emit q->selectionClipboardUrlPasted(data.uri()); |
131 | #endif |
132 | return true; |
133 | default: |
134 | break; |
135 | } |
136 | } else if (KUriFilter::self()->filterSearchUri(data, KUriFilter::NormalTextFilter)) { |
137 | emit q->selectionClipboardUrlPasted(data.uri(), clipboardText); |
138 | #ifndef KDE_NO_DEPRECATED |
139 | emit q->selectionClipboardUrlPasted(data.uri()); |
140 | #endif |
141 | return true; |
142 | } |
143 | } |
144 | } |
145 | } |
146 | |
147 | return false; |
148 | } |
149 | |
150 | T *q; |
151 | Qt::KeyboardModifiers keyboardModifiers; |
152 | Qt::MouseButtons pressedButtons; |
153 | QWebHitTestResult hitTest; |
154 | }; |
155 | |
156 | #endif // KWEBVIEW_P_H |
157 | |