summaryrefslogtreecommitdiff
path: root/browser/browser.cpp
blob: 5a0834c079c9723e71dbaef09e50b64ae8d0959c (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
/**
 * Copyright (C) 2013, Pelagicore
 *
 * Author: Marcel Schuette <marcel.schuette@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 <QDBusConnection>
#include <QFile>
#include <QWebSettings>
#include <QNetworkProxy>

#include "browser.h"
#include "browserview.h"
#include "userinput.h"
#include "networkmanager.h"
#include "iwebpagewindow_adaptor.h"
#include "iuserinput_adaptor.h"
#include "browserconfig.h"

bool restoreSavedProxyConfig();

browser::browser(cachemanager *manager, networkmanager *nm, QObject *parent) :
    QObject(parent), m_cacheManager (manager), m_networkManager(nm)
{
    qDebug() << __PRETTY_FUNCTION__;

    QWebSettings::globalSettings()->
        setAttribute(QWebSettings::PluginsEnabled, true);

    restoreSavedProxyConfig();
}

conn::brw::ERROR_IDS browser::createPageWindow(int a_eDeviceId, const conn::brw::Rect & a_oGeometry, conn::brw::OBJECT_HANDLE &a_hPageWindowHandle) {
    qDebug() << __PRETTY_FUNCTION__;

    Q_UNUSED(a_eDeviceId);

    userinput *m_userInput = new userinput();
    new IUserInputAdaptor(m_userInput);

    webpagewindow *wpw = new webpagewindow();
    new IWebPageWindowAdaptor(wpw);

    BrowserView *bvi = new BrowserView(m_cacheManager, m_userInput);

    bvi->setGeometry(a_oGeometry.i32X, a_oGeometry.i32Y, a_oGeometry.i32Width,
                         a_oGeometry.i32Height);
    bvi->show();
    a_hPageWindowHandle = bvi->winId();

    windowhash.insert(a_hPageWindowHandle, bvi->window());

    wpw->webitem = bvi;
    connect(bvi, SIGNAL(pageLoadFinished(bool)),       wpw,  SLOT(getUrlTitle()));
    connect(bvi, SIGNAL(pageLoadStarted()),            wpw,  SLOT(browserStartLoading()));
    connect(bvi, SIGNAL(onVisibilityChanged(bool)),    wpw,  SIGNAL(onVisibilityChanged(bool)));
    connect(bvi, SIGNAL(pageLoadFinished(bool)),       wpw,  SIGNAL(onLoadFinished(bool)));
    connect(bvi, SIGNAL(onUrlChanged(QString)),        wpw,  SIGNAL(onUrlChanged(QString)));
    connect(bvi, SIGNAL(onTitleChanged(QString)),      wpw,  SIGNAL(onTitleChanged(QString)));
    connect(bvi, SIGNAL(onLinkClicked(QString)),       wpw,  SIGNAL(onLinkClicked(QString)));
    connect(bvi, SIGNAL(onSelectionChanged(void)),     wpw,  SIGNAL(onSelectionChanged(void)));
    connect(bvi, SIGNAL(onStatusTextChanged(QString)), wpw,  SIGNAL(onStatusTextChanged(QString)));
    connect(bvi, SIGNAL(onZoomFactorChanged(double)),  wpw,  SIGNAL(onZoomFactorChanged(double)));
    connect(bvi, SIGNAL(onLinkHovered(QString)),       wpw,  SIGNAL(onLinkHovered(QString)));
    connect(bvi, SIGNAL(onInputText(QString, QString, int, int, int, int, int)), m_userInput, SLOT(inputTextReceived(QString, QString, int, int, int, int, int)));
    connect(this,SIGNAL(onPageWindowDestroyed(qlonglong)), wpw, SIGNAL(onClose()));
    connect(bvi, SIGNAL(onScrollPositionChanged(uint,uint)), wpw, SIGNAL(onScrollPositionChanged(uint,uint)));
    connect(bvi, SIGNAL(onActionStateChanged(uint)),   wpw,  SIGNAL(onActionStateChanged(uint)));
    connect(bvi, SIGNAL(onContentSizeChanged(uint, uint)),wpw,SIGNAL(onContentSizeChanged(uint,uint)));
    connect(bvi, SIGNAL(onFaviconReceived()),           wpw,  SIGNAL(onFaviconReceived()));

    connect(m_userInput, SIGNAL(inputText(QString)),   bvi, SLOT(inputText(QString)));
    connect(bvi,         SIGNAL(onSelect(const QString &, const conn::brw::SelectableOptionList &, bool)),
            m_userInput, SIGNAL(onSelect(const QString &, const conn::brw::SelectableOptionList &, bool)));
    connect(m_userInput, SIGNAL(selectIndexes(QList<int>)), bvi, SLOT(onSelectIndexes(QList<int>)));

    QDBusConnection conn = connection();

    QString *webpagewindowservice = new QString("/Browser/IWebPageWindow" + QString::number(a_hPageWindowHandle));
    qDebug() << *webpagewindowservice;

    webviewhash.insert(*webpagewindowservice, bvi);

    if(!conn.registerObject(*webpagewindowservice, wpw)) {
        qDebug() << "failed register object IWebPageWindow";
        exit(1);
    }

    QString *userinputservice = new QString( *webpagewindowservice + "/IUserInput");
    qDebug() << *userinputservice;
    if(!conn.registerObject(*userinputservice, m_userInput)) {
        qDebug() << "failed register object IUserInput";
        exit(1);
    }

    emit onPageWindowCreated(a_hPageWindowHandle, conn::brw::EID_NO_ERROR);
    return conn::brw::EID_NO_ERROR;
}

conn::brw::ERROR_IDS browser::destroyPageWindow(conn::brw::OBJECT_HANDLE a_hPageWindowHandle) {
    qDebug() << __PRETTY_FUNCTION__ << a_hPageWindowHandle;

    if(!windowhash.isEmpty()) {
        QWidget *tempwidget = windowhash.value(a_hPageWindowHandle);
        if(tempwidget) {
            windowhash.remove(a_hPageWindowHandle);
            tempwidget->hide();
            emit onPageWindowDestroyed(a_hPageWindowHandle);
            return conn::brw::EID_NO_ERROR;
        }
        return conn::brw::EID_DB_ITEM_NOT_EXISTS;
    }
    return conn::brw::EID_NOT_EXISTS;
}

conn::brw::ERROR_IDS browser::getPageWindows(conn::brw::ObjectHandleList &a_oPageWindowIds) {
    qDebug() << __PRETTY_FUNCTION__;

    conn::brw::ERROR_IDS ret;

    if(windowhash.isEmpty()) {
        ret = conn::brw::EID_NOT_EXISTS;
    } else {
        a_oPageWindowIds = windowhash.keys();
        ret =  conn::brw::EID_NO_ERROR;
    }
    return ret;
}

bool restoreSavedProxyConfig() {
    QString proxyHost;
    int proxyPort;
    QString proxyUsername;
    QString proxyPassword;
    BrowserConfig *bc = BrowserConfig::instance();

    if (bc->contains(BrowserConfig::CONFIG_PROXY_HOST))
        proxyHost = bc->getValue<QString>(BrowserConfig::CONFIG_PROXY_HOST);
    else
        return false;

    if (bc->contains(BrowserConfig::CONFIG_PROXY_PORT)) {
        proxyPort = bc->getValue<int>(BrowserConfig::CONFIG_PROXY_PORT);
    }
    else {
        qDebug("Found partial proxy configuration; CONFIG_PROXY_PORT missing");
        return false;
    }

    if (bc->contains(BrowserConfig::CONFIG_PROXY_USERNAME)) {
        proxyUsername = bc->getValue<QString>(BrowserConfig::CONFIG_PROXY_USERNAME);
    }
    else {
        qDebug("Found partial proxy configuration; CONFIG_PROXY_USERNAME missing");
        return false;
    }

    if (bc->contains(BrowserConfig::CONFIG_PROXY_PASSWORD)) {
        proxyPassword = bc->getValue<QString>(BrowserConfig::CONFIG_PROXY_PASSWORD);
    }
    else {
        qDebug("Found partial proxy configuration; CONFIG_PROXY_PASSWORD missing");
        return false;
    }

    // All properties found, let's set up the proxy

    QNetworkProxy proxy;
    proxy.setType(QNetworkProxy::HttpProxy);
    proxy.setHostName(proxyHost);
    proxy.setPort(proxyPort);
    proxy.setUser(proxyUsername);
    proxy.setPassword(proxyPassword);
    QNetworkProxy::setApplicationProxy(proxy);

}