summaryrefslogtreecommitdiffstats
path: root/src/webview/qwebview_ios.mm
blob: b9fdff9fa583e5698e79389ecf8e5636b890254c (plain)
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
/****************************************************************************
**
** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtWebView module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "qwebview_ios_p.h"
#include "qwebview_p.h"

#include <QtQuick/qquickitem.h>
#include <QtCore/qmap.h>

#include <CoreFoundation/CoreFoundation.h>
#include <UIKit/UIKit.h>

#import <UIKit/UIView.h>
#import <UIKit/UIWindow.h>
#import <UIKit/UIViewController.h>
#import <UIKit/UITapGestureRecognizer.h>
#import <UIKit/UIGestureRecognizerSubclass.h>

QT_BEGIN_NAMESPACE

QWebViewPrivate *QWebViewPrivate::create(QWebView *q)
{
    return new QIosWebViewPrivate(q);
}

static inline CGRect toCGRect(const QRectF &rect)
{
    return CGRectMake(rect.x(), rect.y(), rect.width(), rect.height());
}

// -------------------------------------------------------------------------

@interface QIOSNativeViewSelectedRecognizer : UIGestureRecognizer <UIGestureRecognizerDelegate>
{
@public
    QNativeViewController *m_item;
}
@end

@implementation QIOSNativeViewSelectedRecognizer

- (id)initWithQWindowControllerItem:(QNativeViewController *)item
{
    self = [super initWithTarget:self action:@selector(nativeViewSelected:)];
    if (self) {
        self.cancelsTouchesInView = NO;
        self.delaysTouchesEnded = NO;
        m_item = item;
    }
    return self;
}

- (BOOL)canPreventGestureRecognizer:(UIGestureRecognizer *)other
{
    Q_UNUSED(other);
    return NO;
}

- (BOOL)canBePreventedByGestureRecognizer:(UIGestureRecognizer *)other
{
    Q_UNUSED(other);
    return NO;
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    Q_UNUSED(touches);
    Q_UNUSED(event);
    self.state = UIGestureRecognizerStateRecognized;
}

- (void)nativeViewSelected:(UIGestureRecognizer *)gestureRecognizer
{
    Q_UNUSED(gestureRecognizer);
    m_item->setFocus(true);
}

@end

// -------------------------------------------------------------------------

class QWebViewInterface;

@interface QtWebViewDelegate : NSObject<UIWebViewDelegate> {
    QIosWebViewPrivate *qIosWebViewPrivate;
}
- (QtWebViewDelegate *)initWithQAbstractWebView:(QIosWebViewPrivate *)webViewPrivate;
- (void)pageDone;

// protocol:
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
@end

@implementation QtWebViewDelegate
- (QtWebViewDelegate *)initWithQAbstractWebView:(QIosWebViewPrivate *)webViewPrivate
{
    qIosWebViewPrivate = webViewPrivate;
    return self;
}

- (void)pageDone
{
    Q_EMIT qIosWebViewPrivate->loadProgressChanged();
    Q_EMIT qIosWebViewPrivate->loadingChanged();
    Q_EMIT qIosWebViewPrivate->titleChanged();
    Q_EMIT qIosWebViewPrivate->urlChanged();
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    Q_UNUSED(webView);
    // UIWebViewDelegate gives us per-frame notifications while the QWebView API
    // should provide per-page notifications. Keep track of started frame loads
    // and emit notifications when the final frame completes.
    ++qIosWebViewPrivate->requestFrameCount;
    Q_EMIT qIosWebViewPrivate->loadingChanged();
    Q_EMIT qIosWebViewPrivate->loadProgressChanged();
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    Q_UNUSED(webView);
    if (--qIosWebViewPrivate->requestFrameCount == 0)
        [self pageDone];
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    Q_UNUSED(webView);
    Q_UNUSED(error);
    if (--qIosWebViewPrivate->requestFrameCount == 0)
        [self pageDone];
}
@end

QIosWebViewPrivate::QIosWebViewPrivate(QObject *p)
    : QWebViewPrivate(p)
    , uiWebView(0)
    , m_recognizer(0)
{
    CGRect frame = CGRectMake(0.0, 0.0, 400, 400);
    uiWebView = [[UIWebView alloc] initWithFrame:frame];
    uiWebView.delegate = [[QtWebViewDelegate alloc] initWithQAbstractWebView:this];

    m_recognizer = [[QIOSNativeViewSelectedRecognizer alloc] initWithQWindowControllerItem:this];
    [uiWebView addGestureRecognizer:m_recognizer];

}

QIosWebViewPrivate::~QIosWebViewPrivate()
{
    [uiWebView.delegate release];
    uiWebView.delegate = nil; // reset as per UIWebViewDelegate documentation
    [uiWebView release];
    [m_recognizer release];
}

QUrl QIosWebViewPrivate::url() const
{
    NSString *currentURL = [uiWebView stringByEvaluatingJavaScriptFromString:@"window.location.href"];
    return QString::fromNSString(currentURL);
}

void QIosWebViewPrivate::setUrl(const QUrl &url)
{
    requestFrameCount = 0;
    [uiWebView loadRequest:[NSURLRequest requestWithURL:url.toNSURL()]];
}

bool QIosWebViewPrivate::canGoBack() const
{
    return uiWebView.canGoBack;
}

bool QIosWebViewPrivate::canGoForward() const
{
    return uiWebView.canGoForward;
}

QString QIosWebViewPrivate::title() const
{
    NSString *title = [uiWebView stringByEvaluatingJavaScriptFromString:@"document.title"];
    return QString::fromNSString(title);
}

int QIosWebViewPrivate::loadProgress() const
{
    // TODO:
    return isLoading() ? 100 : 0;
}

bool QIosWebViewPrivate::isLoading() const
{
    return uiWebView.loading;
}

void QIosWebViewPrivate::setParentView(QObject *view)
{
    if (!uiWebView)
        return;

    QWindow *window = qobject_cast<QWindow *>(view);
    if (window != 0) {
        UIView *parentView = reinterpret_cast<UIView *>(window->winId());
        [parentView addSubview:uiWebView];
    } else {
        [uiWebView removeFromSuperview];
    }
}

void QIosWebViewPrivate::setGeometry(const QRect &geometry)
{
    [uiWebView setFrame:toCGRect(geometry)];
}

void QIosWebViewPrivate::setVisibility(QWindow::Visibility visibility)
{
    Q_UNUSED(visibility);
}

void QIosWebViewPrivate::setVisible(bool visible)
{
    [uiWebView setHidden:visible];
}

void QIosWebViewPrivate::setFocus(bool focus)
{
    Q_EMIT requestFocus(focus);
}

void QIosWebViewPrivate::goBack()
{
    [uiWebView goBack];
}

void QIosWebViewPrivate::goForward()
{
    [uiWebView goForward];
}

void QIosWebViewPrivate::stop()
{
    [uiWebView stopLoading];
}

void QIosWebViewPrivate::reload()
{
    [uiWebView reload];
}

void QIosWebViewPrivate::runJavaScriptPrivate(const QString &script, int callbackId)
{
    // ### TODO needs more async
    NSString *result = [uiWebView stringByEvaluatingJavaScriptFromString:script.toNSString()];
    if (callbackId != -1)
        Q_EMIT javaScriptResult(callbackId, QString::fromNSString(result));
}

QT_END_NAMESPACE