summaryrefslogtreecommitdiff
path: root/browser/browserview.h
blob: 0ec7765b44b263bf871e40a70400c4feb34e5ab8 (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
/**
 * 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/.
 */

#ifndef BROWSERVIEW_H
#define BROWSERVIEW_H

#include <QGraphicsWebView>
#include <QGraphicsView>
#include <QResizeEvent>
#include <QWebElement>
#include <QDebug>
#include <QSemaphore>
#include <QNetworkAccessManager>

#include "cachemanager.h"
#include "../common/browserdefs.h"
#include "browserpage.h"
#include "userinput.h"

class InputHandler : public QObject {
Q_OBJECT
public:
    InputHandler () : m_textElements (new QStringList()) {
        *m_textElements << "date" << "datetime" << "datetime-local" << "email"
        << "month" << "number" << "password" << "range" << "tel" << "text"
        << "time" << "url" << "week";
    }
public slots:
    void setScrollPosition (const int &x, const int &y) {
        emit onScroll ((uint)x,(uint)y);
    }
    void setCurrentFocus (const QWebElement &elem) {
        QString attrType = elem.attribute("type", "text");
        if ((elem.tagName().compare("INPUT", Qt::CaseInsensitive) == 0 &&
                m_textElements->contains(attrType))
            ||
            elem.tagName().compare("TEXTAREA", Qt::CaseInsensitive) == 0)
        {
            emit onInputText (elem.attribute("name"), elem.attribute("value"),
                              elem.attribute("type", "0").toInt(),
                              elem.attribute("maxlength", "0").toInt(),
                              elem.attribute("max", "0").toInt(),
                              elem.attribute("min","0").toInt(),
                              elem.attribute("step","0").toInt());
        } else if (elem.tagName().compare("OPTION", Qt::CaseInsensitive) == 0){
            handleSelectElement(elem.parent());
        } else if (elem.tagName().compare("SELECT", Qt::CaseInsensitive) == 0){
            if (elem.hasAttribute("multiple")) {
                return;
            }
            handleSelectElement(elem);
        }
        m_elem = elem;
    }

    void handleSelectElement(const QWebElement &elem) {
        conn::brw::SelectableOptionList options;
        QList<QWebElement> elems;
        QWebElement first = elem.firstChild();
        QWebElement last  = elem.lastChild();

        conn::brw::SelectableOption o;
        o.strValue = first.toPlainText();
        options.append(o);
        elems.append(first);
        while (elems.last() != last) {
            conn::brw::SelectableOption o;
            o.strValue = elems.last().toPlainText();
            elems.append(elems.last().nextSibling());
            options.append(o);
        }

        qDebug() << "Options:" << options.size();
        emit onSelect(elem.attribute("name", ""), options, true);

    }
    const QWebElement currentFocus () { return m_elem; }

signals:
    void onInputText(QString name, QString value, int type, int maxlength,
                     int max, int min, int step);
    void onScroll(uint x, uint y);
    void onSelect(const QString &, const conn::brw::SelectableOptionList &, bool);

private:
    QWebElement m_elem;
    QStringList *m_textElements;
};

class BrowserView : public QGraphicsView
{
     Q_OBJECT
public:
    BrowserView(cachemanager *, userinput *);
    bool load(const QString &a_Url);
    int getProgress() { return m_currentProgress; }
    QString getUrl() { return m_webview.url().toString(); }
    const QString getTitle() { return m_webview.title(); }
    void goBack() { m_webview.back(); }
    void goForward() { m_webview.forward(); }
    void pageReload() { m_webview.reload(); }
    void pageStop() { m_webview.stop(); }
    void scroll (conn::brw::SCROLL_DIRECTION dir, conn::brw::SCROLL_TYPE type);
    QSize contentSize();
    void setZoomFactor(double);
    double getZoomFactor();
    void getScrollPosition(uint&, uint&);
    void setScrollPosition(uint, uint);
    QString createScreenshot(QString url);
    QString getFaviconFilePath(QString url);
    void activate();
    void select();

signals:
    void pageLoadStarted();
    void pageLoadFinished(bool);
    void pageLoadProgress(int);
    void onInputText(QString name, QString value, int type, int maxlength,
                     int max, int min, int step);
    void onUrlChanged(QString url);
    void onTitleChanged(QString title);
    void onLinkClicked(QString);
    void onSelectionChanged();
    void onStatusTextChanged(QString);
    void onVisibilityChanged(bool);
    void onScrollPositionChanged(uint,uint);
    void onZoomFactorChanged(double);
    void onLinkHovered(QString);
    void onActionStateChanged(uint);
    void onContentSizeChanged(uint, uint);
    void onFaviconReceived();
    void onSelect(const QString &, const conn::brw::SelectableOptionList &, bool);

protected:
    virtual void resizeEvent (QResizeEvent *event);
    virtual bool eventFilter (QObject *obj, QEvent *evt);

protected slots:
    void loadProgress(int);
    void loadFinished(bool);
    void urlChanged(QUrl);
    void titleChanged(QString);
    void linkClicked(QUrl);
    void scrollPositionChanged(uint x, uint y);
    void contentSizeChanged(const QSize&);
    void onSelectIndexes(QList<int>);
    void inputText (QString input);

private:
    QGraphicsWebView m_webview;
    InputHandler m_inputHandler;
    int m_currentProgress;
    uint m_scrollPositionX;
    uint m_scrollPositionY;
    cachemanager *m_cacheManager;
};


#endif /* BROWSERVIEW_H */