summaryrefslogtreecommitdiff
path: root/browser/browserview.cpp
blob: 697361a61daf559940c659162fe1e529914922c2 (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
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
/**
 * Copyright (C) 2014, Pelagicore
 *
 * Author: Jonatan PĂ„lsson <jonatan.palsson@pelagicore.com>
 *
 * This file is part of the GENIVI project Browser Proof-Of-Concept
 * For further information, see http://genivi.org/
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#include <QDebug>
#include <QWebFrame>
#include <QWebPage>
#include <QCoreApplication>
#include <QTemporaryFile>
#include <QSemaphore>
#include <QWebSettings>

#include "browserview.h"
#include "cachemanager.h"
#include "../common/browserdefs.h"
#include "browserpage.h"
#include "userinput.h"
#include "browserconfig.h"
#include "errorlogger.h"

BrowserView::BrowserView(cachemanager *cm, userinput *uip)
    : m_scrollPositionX(0), m_scrollPositionY (0), m_cacheManager (cm)
{
    QString startPage;
    m_cacheManager = cm;
    if (!this->scene()) {
        this->setScene(new QGraphicsScene());
    }
    this->scene()->addItem (&m_webview);

    QWebSettings::setIconDatabasePath(".");

    m_webview.setPage(new BrowserPage(this, uip));

    m_webview.page()->setNetworkAccessManager(cm->getNetworkAccessManager());

    startPage = BrowserConfig::instance()->getValue<QString>(BrowserConfig::CONFIG_STARTPAGE);
    if (startPage.compare("") == 0) {
        startPage = "http://www.bmw.com";
        errorlogger::logError("Using default start page");
    } else {
        errorlogger::logError("Using stored start page");
    }

    this->load(startPage);

    this->installEventFilter(this);

    setWindowFlags(Qt::FramelessWindowHint);
    m_webview.page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);

    connect(&m_webview, SIGNAL (loadStarted()),             this, SIGNAL (pageLoadStarted ()));
    connect(&m_webview, SIGNAL (statusBarMessage(QString)), this, SIGNAL (onStatusTextChanged(QString)));
    connect(&m_webview, SIGNAL (loadFinished(bool)),        this, SLOT   (loadFinished (bool)));
    connect(&m_webview, SIGNAL (loadProgress(int)),         this, SLOT   (loadProgress(int)));
    connect(&m_webview, SIGNAL (urlChanged(QUrl)),          this, SLOT   (urlChanged(QUrl)));
    connect(&m_webview, SIGNAL (titleChanged(QString)),     this, SLOT   (titleChanged(QString)));
    connect(&m_webview, SIGNAL (linkClicked(QUrl)),         this, SLOT   (linkClicked(QUrl)));

    connect(m_webview.page(), SIGNAL (selectionChanged(void)), this, SIGNAL(onSelectionChanged(void)));
    connect(m_webview.page(), SIGNAL (linkHovered(const QString&,const QString&,const QString&)), this, SIGNAL(onLinkHovered(QString)));
    connect(m_webview.page()->mainFrame(), SIGNAL (contentsSizeChanged(const QSize &)), this, SLOT (contentSizeChanged(const QSize&)));
    connect(&m_webview, SIGNAL (iconChanged()),             this, SIGNAL (onFaviconReceived()));

    connect(&m_inputHandler, SIGNAL (onInputText(QString, QString, int, int, int, int, int)),
        this, SIGNAL (onInputText(QString, QString, int, int, int, int, int)));
    connect(&m_inputHandler, SIGNAL(onScroll(uint,uint)), this, SLOT(scrollPositionChanged(uint,uint)));
    connect(&m_inputHandler, SIGNAL(onSelect(const QString &, const conn::brw::SelectableOptionList &, bool)), this, SIGNAL(onSelect(const QString &, const conn::brw::SelectableOptionList &, bool)));
}

bool BrowserView::load(const QString &a_Url)
{
    if (m_cacheManager) {
        QNetworkRequest req = QNetworkRequest(QUrl(a_Url));
        req.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
                         m_cacheManager->getCacheLoadControl());
        m_webview.load(req);
    } else {
        qDebug() << "No cacheManager present, defaulting to load(url)";
        m_webview.load(a_Url);
    }

    BrowserConfig::instance()->setValue<QString>(BrowserConfig::CONFIG_STARTPAGE, a_Url);
    return true;
}

void BrowserView::loadProgress(int progress)
{
    m_currentProgress = progress;
    emit pageLoadProgress (progress);
}

void BrowserView::loadFinished(bool ok)
{
    // Inject some JS into the page, and hook it up to an object of ours, this
    // allows us to detect when the user toggles an input field

    m_webview.page()->mainFrame()->addToJavaScriptWindowObject("inputHandler", &m_inputHandler);

    m_webview.page()->mainFrame()->evaluateJavaScript(
    "(function() {"
        "var pocCurrentElement = null;"
        "document.addEventListener('focus', function(e){"
        "    if (pocCurrentElement != e.target) {"
        "       e.target.setAttribute('value', e.target.value);"
        "       window.inputHandler.setCurrentFocus(e.target);"
        "       pocCurrentElement = e.target;"
        "    }"
        "}, true);"
        "document.addEventListener('click', function(e){"
        "   e.target.setAttribute('value', e.target.value);"
        "   window.inputHandler.setCurrentFocus(e.target);"
        "   pocCurrentElement = e.target;"
        "}, true);"
        "document.addEventListener('focusout', function(e){"
        "    pocCurrentElement = e.relatedTarget;"
        "}, true);"
        "var elems = document.querySelectorAll('select:not([multiple])');"
        "for (var i = 0; i < elems.length; i++) {"
        "   elem = elems[i];"
        "   elem.addEventListener('mousedown', function(e) {"
        "       e.target.setAttribute('value', e.target.value);"
        "       window.inputHandler.setCurrentFocus(e.target);"
        "       pocCurrentElement = e.target;"
        "   }, true);"
        "}"
    "})()"
    );

    m_webview.page()->mainFrame()->evaluateJavaScript(
    "document.addEventListener('scroll', function(){"
    "    window.inputHandler.setScrollPosition(window.pageXOffset, window.pageYOffset);"
    "}, true);");

    emit pageLoadFinished (ok);
}

void BrowserView::scroll (conn::brw::SCROLL_DIRECTION dir, conn::brw::SCROLL_TYPE type)
{
    int stepSize = 50;
    int xMultiplier = 0;
    int yMultiplier = 0;
    Qt::Key key(Qt::Key_Tab);

    switch (dir) {
    case conn::brw::SD_TOP:
        yMultiplier = -1;
        key = Qt::Key_Backtab;
        break;
    case conn::brw::SD_BOTTOM:
        yMultiplier = 1;
        key = Qt::Key_Tab;
        break;
    case conn::brw::SD_RIGHT:
        xMultiplier = 1;
        key = Qt::Key_Tab;
        break;
    case conn::brw::SD_LEFT:
        xMultiplier = -1;
        key = Qt::Key_Backtab;
        break;
    default:
        qDebug() << "Invalid direction";
        break;
    }

    if (type == conn::brw::ST_PAGE)
        stepSize= this->height();

    if (type == conn::brw::ST_LINK) {
        QKeyEvent event(QEvent::KeyPress, key, Qt::NoModifier);
        QCoreApplication::sendEvent(m_webview.page(), &event);
    } else {
        if (m_webview.page() && m_webview.page()->mainFrame())
            m_webview.page()->mainFrame()->scroll(stepSize*xMultiplier,
                                                  stepSize*yMultiplier);
    }
}

void BrowserView::inputText (QString input)
{
    QString entryStr = "window.document.activeElement.value = '" + input + "';";
    qDebug() << "About to input:" << input;
    m_webview.page()->mainFrame()->evaluateJavaScript(entryStr);
}

void BrowserView::resizeEvent (QResizeEvent *event) {
    int w = event->size().width();
    int h = event->size().height();
    m_webview.setGeometry (QRect(0,0,w,h));
}

QSize BrowserView::contentSize()
{
    return m_webview.page()->mainFrame()->contentsSize();
}

void BrowserView::urlChanged (QUrl url)
{
    QString strUrl = url.toString();
    if (strUrl.compare("") != 0)
        emit onUrlChanged (strUrl);
}

void BrowserView::titleChanged (QString title)
{
    if (title.compare("") != 0)
        emit onTitleChanged (title);
}

void BrowserView::linkClicked(QUrl url) {
    QString strUrl = url.toString();
    this->load(strUrl);
    emit onLinkClicked(strUrl);
}

bool BrowserView::eventFilter(QObject *obj, QEvent *event)
{
    if (event->type()      == QEvent::Show)
        emit onVisibilityChanged(true);
    else if (event->type() == QEvent::Hide)
        emit onVisibilityChanged(false);

    return QGraphicsView::eventFilter(obj, event);
}

void BrowserView::setZoomFactor(double factor)
{
    m_webview.setZoomFactor(factor);
    emit onZoomFactorChanged (factor);
}

double BrowserView::getZoomFactor()
{
    return m_webview.zoomFactor();
}

void BrowserView::scrollPositionChanged(uint x, uint y)
{
    m_scrollPositionX = x;
    m_scrollPositionY = y;
    emit onScrollPositionChanged(x,y);
}

void BrowserView::setScrollPosition(uint x, uint y)
{
    QString cmd = QString("window.scrollTo(%1,%2);").arg(QString::number(x), QString::number(y));
    m_webview.page()->mainFrame()->evaluateJavaScript(cmd);
    m_scrollPositionX = x;
    m_scrollPositionY = y;
}

void BrowserView::getScrollPosition(uint &x, uint &y)
{
    x = m_scrollPositionX;
    y = m_scrollPositionY;
}

QString BrowserView::createScreenshot(QString directory) {
    m_webview.page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
    m_webview.page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);

    QImage *image = new QImage(m_webview.page()->viewportSize(),
                               QImage::Format_ARGB32);
    QPainter *painter = new QPainter(image);
    QTemporaryFile outFile(directory + "/XXXXXX.png");
    outFile.setAutoRemove(false);
    outFile.open();
    m_webview.page()->mainFrame()->render(painter);

    painter->end();

    m_webview.page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAsNeeded);
    m_webview.page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAsNeeded);

    image->save(&outFile, "PNG");
    outFile.close();
    return outFile.fileName();
}

QString BrowserView::getFaviconFilePath(QString url) {

    QIcon icon = QWebSettings::globalSettings()->iconForUrl(QUrl(url));
    if (icon.isNull())
        return "";

    QImage image = icon.pixmap(15).toImage();

    QTemporaryFile outFile("XXXXXX.png");
    outFile.setAutoRemove(false);
    outFile.open();

    image.save(&outFile, "PNG");
    outFile.close();

    return outFile.fileName();
}

void BrowserView::contentSizeChanged(const QSize &size) {
    emit onContentSizeChanged(size.width(), size.height());
}

void BrowserView::select() {
    QKeyEvent event(QEvent::KeyPress, Qt::Key_Return, Qt::NoModifier);
    QCoreApplication::sendEvent(m_webview.page(), &event);
}
void BrowserView::activate() {
    this->setVisible(true);
}

void BrowserView::onSelectIndexes(QList<int> indexes) {
    m_webview.page()->mainFrame()->evaluateJavaScript(
        "for (var i = 0; i < document.activeElement.options.length; i++){"
        "    document.activeElement.options[i].selected = false;"
        "}"
    );
    for (int i = 0; i < indexes.size(); i++) {
        QString cmd = QString(
            "document.activeElement.options[%1].selected = true;").arg(
                                QString::number(indexes.at(i)));
        m_webview.page()->mainFrame()->evaluateJavaScript(cmd);
    }
}