-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.cpp
More file actions
210 lines (177 loc) · 7.71 KB
/
main.cpp
File metadata and controls
210 lines (177 loc) · 7.71 KB
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
#include "cefheaders.h"
#include "mainwindow.h"
#include "cefengine.h"
#include "cefhandler.h"
#include "signalbroker.h"
#include "completioncallback.h"
#include <QApplication>
#include <QTimer>
#include <QDir>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusInterface>
#include <QFontDatabase>
#include <QNetworkProxyFactory>
#include <nativeeventfilter.h>
#include <unistd.h>
#include <sys/types.h>
CefHandler* handler;
SignalBroker* signalBroker;
QTimer cefEventLoopTimer;
QVariantMap settingsData;
QVariantMap notificationsData;
QStringList certErrorUrls;
QTimer batteryCheckTimer;
QFile historyFile(QDir::homePath() + "/.theweb/history"); //File used for reading/writing
CefString historyData; //Used in settings accessor
CefBrowserSettings defaultBrowserSettings;
int main(int argc, char *argv[])
{
//Create space for extra arguments (if need be)
if (geteuid() == 0) {
char **newArgv = (char**) malloc((argc + 1) * sizeof(*newArgv));
memcpy(newArgv, argv, sizeof(*newArgv) * argc);
newArgv[argc] = "--no-sandbox";
argc++;
argv = newArgv;
}
QApplication a(argc, argv);
//Set Application Information
a.setOrganizationName("theSuite");
a.setOrganizationDomain("");
a.setApplicationName("theWeb");
QSettings appSettings;
NativeEventFilter* nativeFilter = new NativeEventFilter;
a.installNativeEventFilter(nativeFilter);
qRegisterMetaType<Browser>();
qRegisterMetaType<CefRefPtr<CefFrame>>();
qRegisterMetaType<CefString>();
qRegisterMetaType<CefRefPtr<CefAuthCallback>>();
qRegisterMetaType<CefLifeSpanHandler::WindowOpenDisposition>();
qRegisterMetaType<CefPopupFeatures>();
qRegisterMetaType<CefWindowInfo>();
qRegisterMetaType<CefBrowserSettings>();
//Use the system proxy settings in Qt
QNetworkProxyFactory::setUseSystemConfiguration(true);
signalBroker = new SignalBroker();
//Initialize CEF
CefSettings settings;
//cef_string_t newUserAgent;
//newUserAgent.str = (char16*) QString::fromLocal8Bit((char*) settings.user_agent.str, settings.user_agent.length).append(" theWeb/15.00").toStdString().data();
//settings.user_agent = newUserAgent;
/*QString userAgent = "Mozilla/5.0";
cef_string_t* userAgentString = new cef_string_t();
cef_string_utf16_set((const char16*) userAgent.toStdString().data(), userAgent.length(), userAgentString, 1);
settings.user_agent = *userAgentString;*/
CefString(&settings.user_agent) = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) theWeb/15.00 Chrome/55.0.2883.59 Safari/537.36";
/*QString userAgent = QString::fromStdString(CefString(&settings.user_agent).ToString());
userAgent.insert(userAgent.indexOf("Chrome/"), "theWeb/15.00 ");
CefString(&settings.user_agent) = userAgent.toStdString();*/
CefString(&settings.cache_path) = QDir::homePath().append("/.theweb/cache").toStdString();
CefString(&settings.locale) = "en-US";
//settings.remote_debugging_port = 26154;
//settings.single_process = true;
CefRefPtr<CefEngine> app(new CefEngine);
CefMainArgs cefArgs(argc, argv);
//Initialize CEF
CefInitialize(cefArgs, settings, app.get(), NULL);
//Set default browser settings
CefString(&defaultBrowserSettings.sans_serif_font_family) = appSettings.value("fonts/sansSerif", QFontDatabase::systemFont(QFontDatabase::GeneralFont).family()).toString().toStdString();
CefString(&defaultBrowserSettings.fixed_font_family) = appSettings.value("fonts/mono", QFontDatabase::systemFont(QFontDatabase::FixedFont).family()).toString().toStdString();
//Create a handler
handler = new CefHandler();
//Connect required signals
QObject::connect(nativeFilter, SIGNAL(PlayPause()), handler, SLOT(PlayPause()));
QObject::connect(nativeFilter, SIGNAL(Previous()), handler, SLOT(Previous()));
//Read command line arguments
QStringList urlsToOpen;
for (QString arg : a.arguments().first().split(" ")) {
if (arg != a.applicationFilePath() && arg != "" && !arg.startsWith("-")) {
urlsToOpen.append(arg);
}
}
//Check if theWeb is already running. We do this after CEF initializes because CEF can block things.
bool isRunning;
isRunning = QDBusConnection::sessionBus().interface()->registeredServiceNames().value().contains("org.thesuite.theweb");
if (isRunning) {
//theWeb is already running. Check the PID
QDBusInterface interface("org.thesuite.theweb", "/org/thesuite/theweb", "org.thesuite.theweb");
if (interface.property("processID") != a.applicationPid()) {
//PID is different. Open a new window in parent process and exit this one.
qDebug() << "theWeb is already running. Opening new window in existing browser process (via DBus).";
//Send message via DBus to other theWeb process
QDBusMessage message = QDBusMessage::createMethodCall("org.thesuite.theweb", "/org/thesuite/theweb", "org.thesuite.theweb", "newWindow");
if (urlsToOpen.count() > 0) {
QVariantList arguments;
arguments.append(urlsToOpen.first());
message.setArguments(arguments);
}
QDBusConnection::sessionBus().call(message, QDBus::NoBlock);
//Exit theWeb
CefShutdown();
return 0;
}
} else {
//We'll get here only if theWeb is not running.
new MainDBus(); //Initialize DBus service
}
//Initialize Windows
historyFile.open(QFile::ReadWrite | QFile::Append);
bool windowOpened = false;
for (QString url : urlsToOpen) {
windowOpened = true;
CefWindowInfo windowInfo;
CefBrowserSettings settings;
MainWindow* w = new MainWindow(CefBrowserHost::CreateBrowserSync(windowInfo, CefRefPtr<CefHandler>(handler), url.toStdString(), settings, CefRefPtr<CefRequestContext>()));
w->show();
}
if (!windowOpened) {
MainWindow* w = new MainWindow();
w->show();
}
cefEventLoopTimer.setInterval(0);
QObject::connect(&cefEventLoopTimer, &QTimer::timeout, [=]() {
CefDoMessageLoopWork();
});
cefEventLoopTimer.start();
batteryCheckTimer.setInterval(1000);
QObject::connect(&batteryCheckTimer, &QTimer::timeout, [=]() {
QDBusInterface upower("org.freedesktop.UPower", "/org/freedesktop/UPower", "org.freedesktop.UPower", QDBusConnection::systemBus());
if (upower.isValid()) {
if (upower.property("OnBattery").toBool()) {
if (cefEventLoopTimer.interval() == 0) {
cefEventLoopTimer.setInterval(1000 / 60);
emit signalBroker->ReloadSettings();
}
} else {
if (cefEventLoopTimer.interval() == (1000 / 60)) {
cefEventLoopTimer.setInterval(0);
emit signalBroker->ReloadSettings();
}
}
}
});
batteryCheckTimer.start();
int ret = a.exec();
CefCookieManager::GetGlobalManager(NULL).get()->FlushStore(NULL);
CefShutdown();
appSettings.remove("browser/lastPages");
return ret;
}
void QuitApp(int exitCode = 0) {
cefEventLoopTimer.stop();
QApplication::exit(exitCode);
}
QString calculateSize(quint64 size) {
QString ret;
if (size > 1073741824) {
ret = QString::number(((float) size / 1024 / 1024 / 1024), 'f', 2).append(" GiB");
} else if (size > 1048576) {
ret = QString::number(((float) size / 1024 / 1024), 'f', 2).append(" MiB");
} else if (size > 1024) {
ret = QString::number(((float) size / 1024), 'f', 2).append(" KiB");
} else {
ret = QString::number((float) size, 'f', 2).append(" B");
}
return ret;
}