From 5825451aef1a762bfaeff2d37c09b3790deee7b1 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 26 Jun 2018 19:51:52 +0200 Subject: Socket messages are json formatted --- lib/about/aboutdialog.cpp | 3 ++- src/browser.cpp | 49 +++++++++++++++++++++++++--------------- src/browser.h | 2 +- src/main.cpp | 3 ++- src/mainwindow/mainwindow.cpp | 11 +++++++++ src/mainwindow/mainwindow.h | 2 ++ src/mainwindow/subwindow.cpp | 15 ++++++++---- src/mainwindow/subwindow.h | 3 ++- src/session.cpp | 25 ++++++++++++++++++++ src/session.h | 1 + src/singleapplication.cpp | 38 +++++++++++-------------------- src/singleapplication.h | 6 ++--- src/webengine/urlinterceptor.cpp | 17 ++++---------- src/webengine/urlinterceptor.h | 4 +--- 14 files changed, 108 insertions(+), 71 deletions(-) diff --git a/lib/about/aboutdialog.cpp b/lib/about/aboutdialog.cpp index 4fb7a27..9136d4a 100644 --- a/lib/about/aboutdialog.cpp +++ b/lib/about/aboutdialog.cpp @@ -50,7 +50,8 @@ AboutDialog::AboutDialog(QWidget *parent) "" "

This program is distributed in the hope that it will be useful, but without any warranty.

")); - ui->detailsLabel->setText(tr("

Build " SMOLBOTE_BRANCH ":" SMOLBOTE_COMMIT "

" + ui->detailsLabel->setText(tr("

Version " SMOLBOTE_DESCRIBE "

" + "

Build " SMOLBOTE_BRANCH ":" SMOLBOTE_COMMIT "

" "

Compiled with " compiler "

")); } 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 #include #include "profilemanager.h" +#include 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 #include #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("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 #include #include +#include #ifdef QT_DEBUG #include "session.h" @@ -267,6 +268,16 @@ SubWindow *MainWindow::currentSubWindow() const return qobject_cast(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 &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(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 #include #include +#include +#include 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 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 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 #include -#include 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 parse(const QString &filename) @@ -88,4 +79,4 @@ QHash 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 #include 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 rules; - QMutex rulesLock; }; QHash parse(const QString &filename); -- cgit v1.2.1