aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/browser.cpp49
-rw-r--r--src/browser.h2
-rw-r--r--src/main.cpp3
-rw-r--r--src/mainwindow/mainwindow.cpp11
-rw-r--r--src/mainwindow/mainwindow.h2
-rw-r--r--src/mainwindow/subwindow.cpp15
-rw-r--r--src/mainwindow/subwindow.h3
-rw-r--r--src/session.cpp25
-rw-r--r--src/session.h1
-rw-r--r--src/singleapplication.cpp38
-rw-r--r--src/singleapplication.h6
-rw-r--r--src/webengine/urlinterceptor.cpp17
-rw-r--r--src/webengine/urlinterceptor.h4
13 files changed, 106 insertions, 70 deletions
diff --git a/src/browser.cpp b/src/browser.cpp
index 73b466f..fe103a8 100644
--- a/src/browser.cpp
+++ b/src/browser.cpp
@@ -24,6 +24,7 @@
#include <version.h>
#include <webprofile.h>
#include "profilemanager.h"
+#include <QJsonDocument>
Browser::Browser(int &argc, char *argv[])
: SingleApplication(argc, argv)
@@ -128,27 +129,39 @@ void Browser::setup(const QString &defaultProfile)
connect(WebProfile::defaultProfile(), &WebProfile::downloadRequested, m_downloads.get(), &DownloadsWidget::addDownload);
}
-void Browser::createSession(const QString &profileName, bool newWindow, const QStringList &urls)
+void Browser::createSession(const QJsonObject &object)
{
- if(m_windows.isEmpty()) {
- createWindow();
- }
-
- auto *mainwindow = m_windows.last();
- if(newWindow) {
- QString firstUrl; // = WebProfile::defaultProfile()->homepage();
- if(!urls.isEmpty())
- firstUrl = urls.at(0);
- auto *w = mainwindow->createSubWindow(firstUrl);
- for(int i = 1; i < urls.count() - 1; i++) {
- w->addTab(QUrl::fromUserInput(urls.at(i)));
+ MainWindow *mainwindow = nullptr;
+ if(m_windows.isEmpty())
+ mainwindow = createWindow();
+ else
+ mainwindow = m_windows.last();
+
+ const QJsonArray subwindows = object.value("subwindows").toArray();
+
+ for(const QJsonValue &s : subwindows) {
+ const QJsonObject subwindow = s.toObject();
+ const QString profileId = subwindow.value("profile").toString();
+ WebProfile *profile = ProfileManager::profile(profileId);
+ Q_CHECK_PTR(profile);
+
+ SubWindow *window = nullptr;
+ for(SubWindow *w : mainwindow->subWindows()) {
+ if(w->profile() == profile) {
+ window = w;
+ break;
+ }
}
- } else {
- if(urls.isEmpty())
- mainwindow->createTab(WebProfile::defaultProfile()->homepage());
+ if(window == nullptr)
+ window = mainwindow->createSubWindow(profile);
+
+ const QJsonArray tabs = subwindow.value("tabs").toArray();
+ if(tabs.isEmpty())
+ window->addTab(profile->newtab());
else {
- for(const QString &url : urls) {
- mainwindow->createTab(QUrl::fromUserInput(url));
+ for(const QJsonValue &t : subwindow.value("tabs").toArray()) {
+ const QJsonObject tab = t.toObject();
+ window->addTab(QUrl::fromUserInput(tab.value("url").toString()));
}
}
}
diff --git a/src/browser.h b/src/browser.h
index b511b45..0fda721 100644
--- a/src/browser.h
+++ b/src/browser.h
@@ -50,7 +50,7 @@ signals:
void registerProfile(WebProfile *profile);
public slots:
- void createSession(const QString &profileName, bool newWindow, const QStringList &urls);
+ void createSession(const QJsonObject &object);
MainWindow *createWindow();
private:
diff --git a/src/main.cpp b/src/main.cpp
index 3de6bfe..93f033b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -13,6 +13,7 @@
#include <memory>
#include <iostream>
#include "plugin.h"
+#include "session.h"
int main(int argc, char **argv)
{
@@ -120,7 +121,7 @@ int main(int argc, char **argv)
QObject::connect(&app, &Browser::messageAvailable, &app, &Browser::createSession);
}
- app.sendMessage("", false, urls);
+ app.sendMessage(Session::toJsonObject(config->value<QString>("profile.default").value(), urls));
if(isSingleInstance)
return app.exec();
else
diff --git a/src/mainwindow/mainwindow.cpp b/src/mainwindow/mainwindow.cpp
index 07ac8f0..9a9b8e8 100644
--- a/src/mainwindow/mainwindow.cpp
+++ b/src/mainwindow/mainwindow.cpp
@@ -28,6 +28,7 @@
#include <QToolBar>
#include <QUrl>
#include <configuration/configuration.h>
+#include <webprofile.h>
#ifdef QT_DEBUG
#include "session.h"
@@ -267,6 +268,16 @@ SubWindow *MainWindow::currentSubWindow() const
return qobject_cast<SubWindow *>(mdiArea->currentSubWindow());
}
+SubWindow *MainWindow::createSubWindow(WebProfile *profile)
+{
+ auto *w = new SubWindow(m_config->section("window"), this);
+ w->setProfile(profile);
+ mdiArea->addSubWindow(w);
+ w->showMaximized();
+ w->setFocus();
+ return w;
+}
+
SubWindow *MainWindow::createSubWindow(const QString &url)
{
auto *w = new SubWindow(m_config->section("window"), this);
diff --git a/src/mainwindow/mainwindow.h b/src/mainwindow/mainwindow.h
index 9cb65d6..287602a 100644
--- a/src/mainwindow/mainwindow.h
+++ b/src/mainwindow/mainwindow.h
@@ -21,6 +21,7 @@ class AddressBar;
class SearchForm;
class WebView;
class NavigationBar;
+class WebProfile;
class MainWindow : public QMainWindow
{
friend class Browser;
@@ -47,6 +48,7 @@ public:
public slots:
void createTab(const QUrl &url);
+ SubWindow *createSubWindow(WebProfile *profile);
SubWindow *createSubWindow(const QString &url = QString());
void setView(WebView *view);
diff --git a/src/mainwindow/subwindow.cpp b/src/mainwindow/subwindow.cpp
index 354e07e..bcddabf 100644
--- a/src/mainwindow/subwindow.cpp
+++ b/src/mainwindow/subwindow.cpp
@@ -31,14 +31,14 @@ SubWindow::SubWindow(const QHash<QString, QString> &config, QWidget *parent, Qt:
resize(800, 600);
setWidget(tabWidget);
- profile = WebProfile::defaultProfile();
+ m_profile = WebProfile::defaultProfile();
// system menu
{
auto *menu = systemMenu();
menu->addSeparator();
- auto *profileName_action = menu->addAction(tr("Profile: %1").arg(profile->name()));
+ auto *profileName_action = menu->addAction(tr("Profile: %1").arg(m_profile->name()));
profileName_action->setEnabled(false);
auto *loadProfile_menu = menu->addMenu(tr("Load profile"));
@@ -130,16 +130,21 @@ WebView *SubWindow::view(int index) const
void SubWindow::setProfile(WebProfile *profile)
{
Q_CHECK_PTR(profile);
- this->profile = profile;
+ this->m_profile = profile;
for(int i = 0; i < tabWidget->count(); ++i) {
auto *view = qobject_cast<WebView *>(tabWidget->widget(i));
view->setProfile(profile);
}
}
+WebProfile *SubWindow::profile() const
+{
+ return m_profile;
+}
+
int SubWindow::addTab(const QUrl &url)
{
- auto *view = new WebView(profile, this);
+ auto *view = new WebView(m_profile, this);
if(!url.isEmpty())
view->load(url);
return tabWidget->addTab(view);
@@ -153,7 +158,7 @@ void SubWindow::setCurrentTab(int index)
QJsonObject SubWindow::session() const
{
QJsonObject obj;
- obj.insert("profile", ProfileManager::id(profile));
+ obj.insert("profile", ProfileManager::id(m_profile));
QJsonArray tabs;
for(int i = 0; i < tabWidget->count(); ++i) {
diff --git a/src/mainwindow/subwindow.h b/src/mainwindow/subwindow.h
index d5f4f4c..e6d7177 100644
--- a/src/mainwindow/subwindow.h
+++ b/src/mainwindow/subwindow.h
@@ -28,6 +28,7 @@ public:
WebView *view(int index) const;
void setProfile(WebProfile *profile);
+ WebProfile *profile() const;
QJsonObject session() const;
@@ -40,7 +41,7 @@ public slots:
void setCurrentTab(int index);
private:
- WebProfile *profile;
+ WebProfile *m_profile;
TabWidget *tabWidget;
QMetaObject::Connection titleConnection;
diff --git a/src/session.cpp b/src/session.cpp
index 281e657..6d025dc 100644
--- a/src/session.cpp
+++ b/src/session.cpp
@@ -28,3 +28,28 @@ QJsonObject Session::toJsonObject(MainWindow *window)
return session;
}
+
+QJsonObject Session::toJsonObject(const QString &profile, const QStringList &urls)
+{
+ QJsonObject session;
+
+ QJsonArray subwindows;
+ {
+ QJsonObject window;
+ window.insert("profile", profile);
+
+ QJsonArray tabs;
+ for(const auto &url : urls) {
+ QJsonObject tab;
+ tab.insert("url", url);
+ tab.insert("profile", profile);
+ tabs.append(tab);
+ }
+ window.insert("tabs", tabs);
+
+ subwindows.append(window);
+ }
+ session.insert("subwindows", subwindows);
+
+ return session;
+}
diff --git a/src/session.h b/src/session.h
index 2926468..4d5d59b 100644
--- a/src/session.h
+++ b/src/session.h
@@ -20,6 +20,7 @@ public:
explicit Session(QObject *parent = nullptr);
static QJsonObject toJsonObject(MainWindow *window);
+ static QJsonObject toJsonObject(const QString &profile, const QStringList &urls);
signals:
diff --git a/src/singleapplication.cpp b/src/singleapplication.cpp
index 720690d..60eaa6d 100644
--- a/src/singleapplication.cpp
+++ b/src/singleapplication.cpp
@@ -10,6 +10,8 @@
#include <QDataStream>
#include <QLocalServer>
#include <QLocalSocket>
+#include <QJsonObject>
+#include <QJsonDocument>
SingleApplication::SingleApplication(int &argc, char **argv)
: QApplication(argc, argv)
@@ -46,7 +48,7 @@ bool SingleApplication::bindLocalSocket(const QString &name)
// there is either no such socket, or the socket wasn't cleaned up
else {
m_localServer = new QLocalServer(this);
- connect(m_localServer, &QLocalServer::newConnection, this, &SingleApplication::parseMessage);
+ connect(m_localServer, &QLocalServer::newConnection, this, &SingleApplication::receiveMessage);
// no other server
QLocalServer::removeServer(LOCALSERVER_KEY);
@@ -60,21 +62,12 @@ QString SingleApplication::serverName() const
return m_localServer->fullServerName();
}
-int SingleApplication::sendMessage(const QString &profileName, bool newWindow, const QStringList &urls)
+int SingleApplication::sendMessage(const QJsonObject &message)
{
QLocalSocket socket;
socket.connectToServer(LOCALSERVER_KEY);
if(socket.waitForConnected(LOCALSERVER_TIMEOUT)) {
- QHash<QString, QVariant> hashedParams;
- hashedParams.insert("profile", profileName);
- hashedParams.insert("newWindow", newWindow);
- hashedParams.insert("urls", urls);
-
- QByteArray argumentData;
- QDataStream ds(&argumentData, QIODevice::WriteOnly);
- ds << hashedParams;
-
- socket.write(argumentData);
+ socket.write(QJsonDocument(message).toJson());
socket.waitForBytesWritten(LOCALSERVER_TIMEOUT);
return EXIT_SUCCESS;
}
@@ -82,28 +75,23 @@ int SingleApplication::sendMessage(const QString &profileName, bool newWindow, c
return EXIT_FAILURE;
}
-void SingleApplication::parseMessage()
+void SingleApplication::receiveMessage()
{
QLocalSocket *socket = m_localServer->nextPendingConnection();
- // null socket --> return
if(socket == nullptr) {
return;
}
socket->waitForReadyRead();
+ auto message = socket->readAll();
- QByteArray argumentData = socket->readAll();
-
- // skip if we got no data
- if(argumentData.isEmpty()) {
+ if(message.isEmpty())
return;
- }
-
- QHash<QString, QVariant> params;
- QDataStream ds(argumentData);
- ds >> params;
- socket->deleteLater();
+#ifdef QT_DEBUG
+ qDebug("received message: %s", qUtf8Printable(message));
+#endif
- emit messageAvailable(params["profile"].toString(), params["newWindow"].toBool(), params["urls"].toStringList());
+ QJsonDocument doc = QJsonDocument::fromJson(message);
+ emit messageAvailable(doc.object());
}
diff --git a/src/singleapplication.h b/src/singleapplication.h
index 05b7b40..68af953 100644
--- a/src/singleapplication.h
+++ b/src/singleapplication.h
@@ -23,13 +23,13 @@ public:
bool bindLocalSocket(const QString &name);
QString serverName() const;
- int sendMessage(const QString &profileName, bool newWindow, const QStringList &urls);
+ int sendMessage(const QJsonObject &message);
signals:
- void messageAvailable(const QString &profileName, bool newWindow, const QStringList &urls);
+ void messageAvailable(const QJsonObject &object);
private slots:
- void parseMessage();
+ void receiveMessage();
private:
const int LOCALSERVER_TIMEOUT = 500;
diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp
index 815018b..4fc23e0 100644
--- a/src/webengine/urlinterceptor.cpp
+++ b/src/webengine/urlinterceptor.cpp
@@ -9,7 +9,6 @@
#include "urlinterceptor.h"
#include <QDir>
#include <QTextStream>
-#include <QtConcurrent>
UrlRequestInterceptor::UrlRequestInterceptor(const QString &path, QObject *parent)
: QWebEngineUrlRequestInterceptor(parent)
@@ -18,28 +17,20 @@ UrlRequestInterceptor::UrlRequestInterceptor(const QString &path, QObject *paren
const QStringList hostFiles = hostsD.entryList(QDir::Files);
for(const QString &file : hostFiles) {
const QString absPath = hostsD.absoluteFilePath(file);
- QtConcurrent::run([this, absPath]() {
- auto r = parse(absPath);
+ auto r = parse(absPath);
#ifdef QT_DEBUG
- qDebug("Parsed %i rules from %s", r.count(), qUtf8Printable(absPath));
+ qDebug("Parsed %i rules from %s", r.count(), qUtf8Printable(absPath));
#endif
- rulesLock.lock();
- rules.unite(r);
- rulesLock.unlock();
- });
+ rules.unite(r);
}
}
-UrlRequestInterceptor::~UrlRequestInterceptor() = default;
-
void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
- rulesLock.lock();
if(rules.contains(info.requestUrl().host())) {
info.block(rules.value(info.requestUrl().host()).isBlocking);
}
- rulesLock.unlock();
}
QHash<QString, UrlRequestInterceptor::HostRule> parse(const QString &filename)
@@ -88,4 +79,4 @@ QHash<QString, UrlRequestInterceptor::HostRule> parse(const QString &filename)
}
return rules;
-};
+}
diff --git a/src/webengine/urlinterceptor.h b/src/webengine/urlinterceptor.h
index 951eb52..2a5c50d 100644
--- a/src/webengine/urlinterceptor.h
+++ b/src/webengine/urlinterceptor.h
@@ -9,7 +9,6 @@
#ifndef URLREQUESTINTERCEPTOR_H
#define URLREQUESTINTERCEPTOR_H
-#include <QMutex>
#include <QWebEngineUrlRequestInterceptor>
class UrlRequestInterceptor : public QWebEngineUrlRequestInterceptor
@@ -21,13 +20,12 @@ public:
};
explicit UrlRequestInterceptor(const QString &path, QObject *parent = nullptr);
- ~UrlRequestInterceptor() override;
+ ~UrlRequestInterceptor() = default;
void interceptRequest(QWebEngineUrlRequestInfo &info) override;
private:
QHash<QString, HostRule> rules;
- QMutex rulesLock;
};
QHash<QString, UrlRequestInterceptor::HostRule> parse(const QString &filename);