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
|
/*
* This file is part of smolbote. It's copyrighted by the contributors recorded
* in the version control history of the file, available from its original
* location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote
*
* SPDX-License-Identifier: GPL-3.0
*/
#include "browser.h"
#include "aboutdialog.h"
#include "aboutplugin.h"
#include "mainwindow/addressbar.h"
#include "bookmarkswidget.h"
#include "conf.hpp"
#include "configuration.h"
#include "downloadswidget.h"
#include "mainwindow/mainwindow.h"
#include "webprofilemanager.h"
#include "subwindow/subwindow.h"
#include "util.h"
#include "webengine/urlinterceptor.h"
#include "webprofile.h"
#include <QAction>
#include <QDir>
#include <QFileDialog>
#include <QFileInfo>
#include <QFileInfoList>
#include <QJsonArray>
#include <QJsonDocument>
#include <QPluginLoader>
#include <QTimer>
#include <QVersionNumber>
#include <plugininterface.h>
#include <version.h>
#include "mainwindow/menubar.h"
#include "webengine/webview.h"
#include "urlfilter.h"
#include "adblock/adblocklist.h"
#include "hostlist/hostlist.h"
#include <spdlog/spdlog.h>
#include <pluginloader.h>
#include <QLibraryInfo>
#include <QTranslator>
Browser::Browser(int &argc, char *argv[], bool allowSecondary)
: SingleApplication(argc, argv, allowSecondary, SingleApplication::User | SingleApplication::SecondaryNotification | SingleApplication::ExcludeAppVersion)
{
setApplicationName(CONFIG_POI_NAME);
setWindowIcon(QIcon(CONFIG_POI_ICON));
setApplicationVersion(QVersionNumber::fromString(QLatin1String(poi_Version)).toString());
Configuration conf;
if(const auto _translation = conf.value<QString>("browser.translation")) {
auto *translator = new QTranslator(this);
if(translator->load(_translation.value()))
installTranslator(translator);
else
delete translator;
}
if(const auto _locale = conf.value<QString>("browser.locale")) {
auto *locale = new QTranslator(this);
if(locale->load("qt_" + _locale.value(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
installTranslator(locale);
else
delete locale;
}
if(auto iconTheme = conf.value<QString>("browser.iconTheme")) {
QIcon::setThemeName(iconTheme.value());
}
}
Browser::~Browser()
{
if(m_bookmarks)
m_bookmarks->save();
for(auto *info : m_plugins)
delete info;
qDeleteAll(m_windows);
m_windows.clear();
}
void Browser::about()
{
auto *dlg = new AboutDialog;
dlg->exec();
}
const QList<QPair<QString, Profile *>> Browser::profileList() const
{
QList<QPair<QString, Profile *>> profiles;
for(const QString &id : m_profileManager->idList()) {
profiles.append(qMakePair(id, m_profileManager->profile(id)));
}
return profiles;
}
QPair<QString, Profile *> Browser::loadProfile(const QString &id, bool isOffTheRecord)
{
Configuration conf;
const QString _id = [id](){
// if id contains a separator, it should be a path
if(id.contains(QDir::separator())) {
return QFileInfo(id).baseName();
} else {
return id;
}
}();
auto *profile = m_profileManager->profile(/* id */ _id, /* path */ (_id == id) ? QString() : id, isOffTheRecord);
connect(profile, &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload);
auto *interceptor = new UrlRequestInterceptor(profile, profile);
for(UrlFilter *filter : m_filters) {
interceptor->addFilter(filter);
}
const auto headers = conf.value<QStringList>("filter.header").value_or(QStringList());
for(const QString &header : headers) {
const auto h = header.split(QLatin1Literal(":"));
if(h.length() == 2)
interceptor->addHttpHeader(h.at(0).toLatin1(), h.at(1).toLatin1());
}
profile->setUrlRequestInterceptor(interceptor);
spdlog::info("Added profile: {}{}", qUtf8Printable(_id), profile->isOffTheRecord() ? " (otr)" : "");
return QPair<QString, WebProfile *>(_id, profile);
}
void Browser::removeProfile(const QString &id)
{
m_profileManager->deleteProfile(id);
}
WebProfileManager *Browser::getProfileManager()
{
return m_profileManager;
}
QPluginLoader *Browser::addPlugin(const QString &path)
{
if(path.isEmpty())
return nullptr;
auto *loader = new PluginLoader(path, PluginLoader::SignatureMatchIfAvailable, this);
const bool loaded = loader->load();
spdlog::info("Loading plugin [{}]: {}", qUtf8Printable(path), loaded ? "passed" : "failed");
if(!loaded) {
delete loader;
return nullptr;
}
auto *info = new PluginInfo(loader);
m_plugins.append(info);
for(MainWindow *window : m_windows) {
addPluginTo(info, window);
}
return loader;
}
void Browser::setup(QVector<QPluginLoader *> plugins)
{
Configuration conf;
for(QPluginLoader *loader : plugins) {
m_plugins.append(new PluginInfo(loader));
}
if(auto stylesheet = conf.value<QString>("browser.stylesheet")) {
QFile f(stylesheet.value());
if(f.open(QIODevice::ReadOnly)) {
setStyleSheet(f.readAll());
f.close();
}
}
// downloads
m_downloads = std::make_unique<DownloadsWidget>(conf.value<QString>("downloads.path").value());
// url request filter
for(const QString &hostlist : Util::files(conf.value<QString>("filter.hosts").value_or(QString()))) {
QFile f(hostlist);
if(f.open(QIODevice::ReadOnly | QIODevice::Text)) {
m_filters.append(new HostList(&f));
f.close();
}
}
for(const QString &adblock : Util::files(conf.value<QString>("filter.adblock").value_or(QString()))) {
QFile f(adblock);
if(f.open(QIODevice::ReadOnly | QIODevice::Text)) {
m_filters.append(new AdBlockList(&f));
f.close();
}
}
// cookie request filter
// load profiles
ProfileDefault_t p;
p.search = conf.value<QString>("profile.search").value();
p.homepage = conf.value<QString>("profile.homepage").value();
p.newtab = conf.value<QString>("profile.newtab").value();
m_profileManager = new WebProfileManager(p, this);
for(const QString &profilePath : Util::files(conf.value<QString>("profile.path").value(), { "*.profile" })) {
this->loadProfile(profilePath);
}
// set default profile
{
const QString id = conf.value<QString>("profile.default").value();
auto *profile = m_profileManager->profile(id);
if(profile == nullptr) {
profile = qobject_cast<WebProfile *>(loadProfile(id).second);
}
WebProfile::setDefaultProfile(profile);
}
// bookmarks
m_bookmarks = std::make_shared<BookmarksWidget>(QString::fromStdString(conf.value<std::string>("bookmarks.path").value()));
connect(m_bookmarks.get(), &BookmarksWidget::showContextMenu, this, [this](const QUrl &url, const QPoint &pos) {
auto *subwindow = m_windows.last()->currentSubWindow();
if(subwindow == nullptr)
return;
auto *menu = new QMenu(m_bookmarks.get());
menu->addAction(tr("Open link in current tab"), subwindow, [url, subwindow]() {
subwindow->currentView()->load(url);
});
auto *openInCurrentTabWithProfile = menu->addMenu(tr("Open link in current tab with profile"));
m_profileManager->profileMenu(openInCurrentTabWithProfile, [url, subwindow](WebProfile *profile) {
subwindow->currentView()->setProfile(profile);
subwindow->currentView()->load(url);
});
menu->addAction(tr("Open link in new tab"), subwindow, [url, subwindow]() {
subwindow->addTab(url);
});
auto *openInNewTabWithProfile = menu->addMenu(tr("Open link in new tab with profile"));
m_profileManager->profileMenu(openInNewTabWithProfile, [url, subwindow](WebProfile *profile) {
subwindow->addTab(url, profile);
});
menu->exec(pos);
});
connect(m_bookmarks.get(), &BookmarksWidget::openUrl, this, [this](const QUrl &url) {
m_windows.last()->createTab(url);
});
auto *timer = new QTimer(this);
connect(timer, &QTimer::timeout, m_bookmarks.get(), &BookmarksWidget::save);
// 5min * 60sec * 1000ms
timer->start(5 * 60 * 1000);
}
void Browser::showWidget(QWidget *widget, MainWindow *where) const
{
bool wasVisible = widget->isVisible();
for(MainWindow *w : qAsConst(m_windows))
w->removeDockWidget(widget);
if(!wasVisible)
where->addDockWidget(Qt::RightDockWidgetArea, widget);
}
MainWindow *Browser::createWindow()
{
// the window will delete itself when it closes, so we don't need to delete it
auto *window = new MainWindow();
connect(window->addressBar, &AddressBar::complete, m_bookmarks.get(), &BookmarksWidget::search);
for(auto *info : m_plugins) {
addPluginTo(info, window);
}
m_windows.append(window);
connect(window, &MainWindow::destroyed, this, [this, window]() {
m_windows.removeOne(window);
});
return window;
}
void Browser::addPluginTo(PluginInfo *info, MainWindow *window)
{
QPluginLoader *loader = info->loader;
auto *pluginMenu = new QMenu(loader->metaData().value("MetaData").toObject().value("name").toString());
window->m_menuBar->insertPlugin(pluginMenu);
info->menus.append(pluginMenu);
auto *aboutAction = pluginMenu->addAction(tr("About"));
connect(aboutAction, &QAction::triggered, this, [loader, window]() {
auto *dlg = new AboutPluginDialog(loader, window);
dlg->exec();
});
auto *runAction = pluginMenu->addAction(tr("Run"));
runAction->setShortcut(QKeySequence::fromString(loader->metaData().value("MetaData").toObject().value("shortcut").toString()));
connect(runAction, &QAction::triggered, window, [loader, window]() {
if(loader->isLoaded()) {
const auto *interface = qobject_cast<PluginInterface *>(loader->instance());
Q_CHECK_PTR(interface);
interface->createWidget(window)->exec();
}
});
auto *removeAction = pluginMenu->addAction(tr("Remove"));
connect(removeAction, &QAction::triggered, this, [this, info]() {;
m_plugins.removeOne(info);
delete info;
});
}
|