From 9fbab94798455c79bfadee9041a78bd47ff642e4 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Mon, 2 Jul 2018 09:24:39 +0200 Subject: Replace SingleApplication with itay-grudev/SingleApplication --- src/CMakeLists.txt | 5 ++- src/browser.cpp | 4 +- src/browser.h | 2 +- src/main.cpp | 38 +++++++++------- src/profilemanager.cpp | 2 +- src/singleapplication.cpp | 109 ---------------------------------------------- src/singleapplication.h | 42 ------------------ 7 files changed, 31 insertions(+), 171 deletions(-) delete mode 100644 src/singleapplication.cpp delete mode 100644 src/singleapplication.h (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 8cd03e6..2f80ad1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -9,8 +9,6 @@ set(CMAKE_AUTORCC ON) set(poi_SRC # main main.cpp - singleapplication.cpp - singleapplication.h browser.cpp browser.h session.cpp @@ -59,6 +57,7 @@ endif() target_include_directories(poi PRIVATE ${Boost_INCLUDE_DIRS} + PRIVATE ../3rd-party PRIVATE ../lib ../plugins PRIVATE ../lib/configuration PRIVATE ../lib/web) @@ -66,6 +65,7 @@ target_include_directories(poi target_link_libraries(poi Qt5::Core Qt5::Widgets Qt5::Concurrent Qt5::WebEngineWidgets ${Boost_LIBRARIES} + SingleApplication about addressbar configuration @@ -80,6 +80,7 @@ if(Plasma) endif(Plasma) target_compile_definitions(poi + PRIVATE QAPPLICATION_CLASS=QApplication PRIVATE QTBUG_65223_WORKAROUND #PRIVATE QTBUG_68224_WORKAROUND ) diff --git a/src/browser.cpp b/src/browser.cpp index 6ffcaaa..f6b3805 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -27,11 +27,13 @@ #include Browser::Browser(int &argc, char *argv[]) - : SingleApplication(argc, argv) + : SingleApplication(argc, argv, true, SingleApplication::User | SingleApplication::SecondaryNotification | SingleApplication::ExcludeAppVersion) { setApplicationName("smolbote"); setWindowIcon(QIcon(":/icon.svg")); setApplicationVersion(SMOLBOTE_VERSION); + + //qDebug(QAPPLICATION_CLASS); } Browser::~Browser() diff --git a/src/browser.h b/src/browser.h index 3405e85..b03243f 100644 --- a/src/browser.h +++ b/src/browser.h @@ -9,7 +9,7 @@ #ifndef SMOLBOTE_BROWSER_H #define SMOLBOTE_BROWSER_H -#include "singleapplication.h" +#include "SingleApplication/singleapplication.h" #include #include #include diff --git a/src/main.cpp b/src/main.cpp index 00a57d5..870120e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -84,6 +84,13 @@ int main(int argc, char **argv) // set this, otherwise the webview becomes black when using a stylesheet app.setAttribute(Qt::AA_DontCreateNativeWidgetSiblings, true); + if(app.isPrimary()) + qDebug("app is primary"); + else if(app.isSecondary()) + qDebug("app is secondary"); + else + qDebug("app is something?"); + // translator if(config->exists("browser.locale")) { auto *translator = new QTranslator(&app); @@ -127,28 +134,29 @@ int main(int argc, char **argv) } // set up socket - bool isSingleInstance = app.bindLocalSocket(socket.value()); - if(isSingleInstance) { -#ifdef QT_DEBUG - qDebug("Local socket bound"); -#endif - - QObject::connect(&app, &Browser::messageAvailable, &app, &Browser::createSession); - } + QObject::connect(&app, &Browser::receivedMessage, &app, [&app](quint32 instanceId, QByteArray message) { + auto doc = QJsonDocument::fromJson(message); + app.createSession(doc.object()); + }); - if(session) { + if(app.isPrimary()) { + app.createSession(Session::toJsonObject(profile.value(), urls)); + } else if(session) { QFile sessionJson(session.value()); if(sessionJson.open(QIODevice::ReadOnly | QIODevice::Text)) { app.sendMessage(sessionJson.readAll()); sessionJson.close(); + qDebug("session data sent"); + return EXIT_SUCCESS; } else { qWarning("Could not open session [%s].", qUtf8Printable(sessionJson.fileName())); } - } else - app.sendMessage(Session::toJsonObject(profile.value(), urls)); - - if(isSingleInstance) - return app.exec(); - else + } else { + auto message = Session::toJsonObject(profile.value(), urls); + qDebug("sending message: %s", app.sendMessage(QJsonDocument(message).toJson()) ? "okay" : "failed"); + qDebug("message>>>\n%s", qUtf8Printable(QJsonDocument(message).toJson())); return EXIT_SUCCESS; + } + + return app.exec(); } diff --git a/src/profilemanager.cpp b/src/profilemanager.cpp index 1038598..bb967b9 100644 --- a/src/profilemanager.cpp +++ b/src/profilemanager.cpp @@ -80,7 +80,7 @@ WebProfile *ProfileManager::profile(const QString &id) if(profiles.contains(id)) return profiles.value(id); else - return nullptr; + return WebProfile::defaultProfile(); } const QMap &ProfileManager::profileList() diff --git a/src/singleapplication.cpp b/src/singleapplication.cpp deleted file mode 100644 index 3c442b4..0000000 --- a/src/singleapplication.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* - * 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/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "singleapplication.h" -#include -#include -#include -#include -#include - -SingleApplication::SingleApplication(int &argc, char **argv) - : QApplication(argc, argv) -{ -} - -SingleApplication::~SingleApplication() -{ - if(m_localServer != nullptr) { - if(m_localServer->isListening()) { - m_localServer->close(); - QLocalServer::removeServer(LOCALSERVER_KEY); - } - } -} - -/** - * @brief SingleApplication::bindLocalSocket check for running local server by connecting to it - * @return true if no other instance, false otherwise - */ -bool SingleApplication::bindLocalSocket(const QString &name) -{ - LOCALSERVER_KEY = name; - - QLocalSocket socket; - socket.connectToServer(LOCALSERVER_KEY); - - if(socket.waitForConnected(LOCALSERVER_TIMEOUT)) { - // another server is running - socket.close(); - return false; - } - - // 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::receiveMessage); - - // no other server - QLocalServer::removeServer(LOCALSERVER_KEY); - return m_localServer->listen(LOCALSERVER_KEY); - } -} - -QString SingleApplication::serverName() const -{ - Q_CHECK_PTR(m_localServer); - return m_localServer->fullServerName(); -} - -int SingleApplication::sendMessage(const QByteArray &data) -{ - QLocalSocket socket; - socket.connectToServer(LOCALSERVER_KEY); - if(socket.waitForConnected(LOCALSERVER_TIMEOUT)) { - socket.write(data); - socket.waitForBytesWritten(LOCALSERVER_TIMEOUT); - return EXIT_SUCCESS; - } - return EXIT_FAILURE; -} - -int SingleApplication::sendMessage(const QJsonObject &message) -{ - QLocalSocket socket; - socket.connectToServer(LOCALSERVER_KEY); - if(socket.waitForConnected(LOCALSERVER_TIMEOUT)) { - socket.write(QJsonDocument(message).toJson()); - socket.waitForBytesWritten(LOCALSERVER_TIMEOUT); - return EXIT_SUCCESS; - } - - return EXIT_FAILURE; -} - -void SingleApplication::receiveMessage() -{ - QLocalSocket *socket = m_localServer->nextPendingConnection(); - if(socket == nullptr) { - return; - } - - socket->waitForReadyRead(); - auto message = socket->readAll(); - - if(message.isEmpty()) - return; - -#ifdef QT_DEBUG - qDebug("received message: %s", qUtf8Printable(message)); -#endif - - QJsonDocument doc = QJsonDocument::fromJson(message); - emit messageAvailable(doc.object()); -} diff --git a/src/singleapplication.h b/src/singleapplication.h deleted file mode 100644 index 84c0fda..0000000 --- a/src/singleapplication.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * 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/smolbote.hg - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#ifndef SMOLBOTE_SINGLEAPPLICATION_H -#define SMOLBOTE_SINGLEAPPLICATION_H - -#include - -class QLocalServer; -class SingleApplication : public QApplication -{ - Q_OBJECT - -public: - explicit SingleApplication(int &argc, char **argv); - ~SingleApplication() override; - - bool bindLocalSocket(const QString &name); - QString serverName() const; - - int sendMessage(const QByteArray &data); - int sendMessage(const QJsonObject &message); - -signals: - void messageAvailable(const QJsonObject &object); - -private slots: - void receiveMessage(); - -private: - const int LOCALSERVER_TIMEOUT = 500; - QString LOCALSERVER_KEY; - - QLocalServer *m_localServer = nullptr; -}; - -#endif // SMOLBOTE_SINGLEAPPLICATION_H -- cgit v1.2.1