forked from 44670/FBrowser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
168 lines (145 loc) · 5.19 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include <QApplication>
#include <QDir>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkProxy>
#include <QTimer>
#include <QProcess>
void MainWindow::configureWebView() {
// Disable anti-aliasing for better performance
webView->setRenderHint(QPainter::Antialiasing, false);
webView->setRenderHint(QPainter::TextAntialiasing, false);
webView->setRenderHint(QPainter::SmoothPixmapTransform, false);
// Enable basic JavaScript support
webView->settings()->setAttribute(QWebSettings::JavascriptEnabled, true);
webView->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
webView->settings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
webView->settings()->setAttribute(QWebSettings::OfflineStorageDatabaseEnabled,
true);
webView->settings()->setAttribute(
QWebSettings::OfflineWebApplicationCacheEnabled, true);
// Enable WebAudio
webView->settings()->setAttribute(QWebSettings::WebAudioEnabled, true);
// Don't allow JavaScript to open/close windows
webView->settings()->setAttribute(QWebSettings::JavascriptCanOpenWindows,
false);
webView->settings()->setAttribute(QWebSettings::JavascriptCanCloseWindows,
false);
// Allow JavaScript to access clipboard
webView->settings()->setAttribute(QWebSettings::JavascriptCanAccessClipboard,
true);
// Allow universal access from file URLs
webView->settings()->setAttribute(QWebSettings::LocalContentCanAccessFileUrls,
true);
webView->settings()->setAttribute(
QWebSettings::LocalContentCanAccessRemoteUrls, true);
// Show Web Inspector
webView->settings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
// Disallow cache
}
MainWindow::MainWindow()
: QMainWindow(),
jsBridge(this) ,
osBridge(this) {
loadConfig();
// Load proxy settings
if (optProxyHost != "") {
qDebug() << "Setting proxy to" << optProxyHost << ":" << optProxyPort;
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::HttpProxy);
proxy.setHostName(optProxyHost);
proxy.setPort(optProxyPort);
QNetworkProxy::setApplicationProxy(proxy);
}
// Set window size
if (optWidth) {
resize(optWidth, optHeight);
}
// Make our window full-screen
// setWindowState(Qt::WindowFullScreen);
// Create the web view
webView = new QWebView(this);
configureWebView();
// Add the web view to our window
setCentralWidget(webView);
// Handle page load events
connect(webView, SIGNAL(loadStarted()), this, SLOT(onLoadStarted()));
// After 1000ms, load the URL
QTimer::singleShot(1000, this, SLOT(loadStartupUrl()));
}
MainWindow::~MainWindow() {}
void MainWindow::onLoadStarted() {
qDebug() << "onLoadStarted";
bool enableJSBridge = false;
bool enableOSBridge = false;
enableJSBridge = optEnableJSBridge;
enableOSBridge = optEnableOSBridge;
if (enableJSBridge) {
webView->page()->mainFrame()->addToJavaScriptWindowObject("FB_JSBridge", &jsBridge);
}
if (enableOSBridge) {
webView->page()->mainFrame()->addToJavaScriptWindowObject("FB_OSBridge", &osBridge);
}
}
void MainWindow::loadUrl(QString url) {
if (url.startsWith("./")) {
url = "file:///" + QDir::currentPath() + QDir::separator() + url.mid(2);
}
qDebug() << "Loading URL:" << url;
webView->load(QUrl(url));
}
void MainWindow::loadStartupUrl() { loadUrl(optStartupUrl); }
void MainWindow::loadConfig() {
bool haveUrlInCmdLine = false;
// Check for url in argv
if (qApp->arguments().size() > 1) {
optStartupUrl = qApp->arguments()[1];
haveUrlInCmdLine = true;
}
// Load config.json
QFile configFile("config.json");
if (!configFile.open(QIODevice::ReadOnly)) {
qDebug() << "Could not open config.json";
return;
}
QByteArray configData = configFile.readAll();
configFile.close();
QJsonDocument configJson = QJsonDocument::fromJson(configData);
QJsonObject configObject = configJson.object();
// Load url
if (configObject.contains("url") && (!haveUrlInCmdLine)) {
optStartupUrl = configObject["url"].toString();
}
// Load window size
if (configObject.contains("width")) {
optWidth = configObject["width"].toInt();
}
if (configObject.contains("height")) {
optHeight = configObject["height"].toInt();
}
// Load proxy settings
if (configObject.contains("proxyHost")) {
optProxyHost = configObject["proxyHost"].toString();
}
if (configObject.contains("proxyPort")) {
optProxyPort = configObject["proxyPort"].toInt();
}
if (configObject.contains("enableJSBridge")) {
optEnableJSBridge = configObject["enableJSBridge"].toBool();
}
if (configObject.contains("enableOSBridge")) {
optEnableOSBridge = configObject["enableOSBridge"].toBool();
}
}
QString OSBridge::runCmd(QStringList args) {
QProcess proc;
QString program = args[0];
QStringList arguments = args.mid(1);
proc.start(program, arguments);
proc.waitForFinished();
QString output = proc.readAllStandardOutput();
return output;
}