From 48685325e6e7605e7abd143eb9adae704c35ea5c Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Sat, 21 Oct 2023 05:43:48 +0200 Subject: [PATCH] Use KDSingleApplication as a submodule --- .github/workflows/build.yml | 10 + .gitmodules | 4 + 3rdparty/kdsingleapplication/CMakeLists.txt | 4 +- .../kdsingleapplication/KDSingleApplication | 1 + 3rdparty/kdsingleapplication/LICENSE | 6 - .../LICENSES/BSD-3-Clause.txt | 11 - 3rdparty/kdsingleapplication/LICENSES/MIT.txt | 9 - 3rdparty/kdsingleapplication/README.md | 53 --- .../kdsingleapplication.cpp | 106 ------ .../kdsingleapplication/kdsingleapplication.h | 48 --- .../kdsingleapplication_lib.h | 23 -- .../kdsingleapplication_localsocket.cpp | 324 ------------------ .../kdsingleapplication_localsocket_p.h | 126 ------- CMakeLists.txt | 15 +- src/config.h.in | 2 + src/main.cpp | 8 + 16 files changed, 40 insertions(+), 710 deletions(-) create mode 100644 .gitmodules create mode 160000 3rdparty/kdsingleapplication/KDSingleApplication delete mode 100644 3rdparty/kdsingleapplication/LICENSE delete mode 100644 3rdparty/kdsingleapplication/LICENSES/BSD-3-Clause.txt delete mode 100644 3rdparty/kdsingleapplication/LICENSES/MIT.txt delete mode 100644 3rdparty/kdsingleapplication/README.md delete mode 100644 3rdparty/kdsingleapplication/kdsingleapplication.cpp delete mode 100644 3rdparty/kdsingleapplication/kdsingleapplication.h delete mode 100644 3rdparty/kdsingleapplication/kdsingleapplication_lib.h delete mode 100644 3rdparty/kdsingleapplication/kdsingleapplication_localsocket.cpp delete mode 100644 3rdparty/kdsingleapplication/kdsingleapplication_localsocket_p.h diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1f0b875b..9585086f 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -103,6 +103,7 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 + submodules: recursive - name: Add safe git directory run: git config --global --add safe.directory ${GITHUB_WORKSPACE} - name: Create Build Environment @@ -222,6 +223,7 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 + submodules: recursive - name: Add safe git directory run: git config --global --add safe.directory ${GITHUB_WORKSPACE} - name: Create Build Environment @@ -329,6 +331,7 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 + submodules: recursive - name: Add safe git directory run: git config --global --add safe.directory ${GITHUB_WORKSPACE} - name: Create Build Environment @@ -429,6 +432,7 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 + submodules: recursive - name: Add safe git directory run: git config --global --add safe.directory ${GITHUB_WORKSPACE} - name: Create Build Environment @@ -535,6 +539,7 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 + submodules: recursive - name: Add safe git directory run: git config --global --add safe.directory ${GITHUB_WORKSPACE} - name: Create Build Environment @@ -632,6 +637,7 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 + submodules: recursive - name: Add safe git directory run: git config --global --add safe.directory ${GITHUB_WORKSPACE} - name: Create Build Environment @@ -731,6 +737,7 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 + submodules: recursive - name: Add safe git directory run: git config --global --add safe.directory ${GITHUB_WORKSPACE} - name: Create Build Environment @@ -792,6 +799,7 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 + submodules: recursive - name: Import certificate file if: github.repository == 'strawberrymusicplayer/strawberry' && matrix.runner == 'macos-11' @@ -924,6 +932,7 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 + submodules: recursive - name: Add safe git directory run: git config --global --add safe.directory ${GITHUB_WORKSPACE} @@ -1192,6 +1201,7 @@ jobs: uses: actions/checkout@v4 with: fetch-depth: 0 + submodules: recursive - name: Add safe git directory shell: bash diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 00000000..f8b8f6cd --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "3rdparty/kdsingleapplication/KDSingleApplication"] + path = 3rdparty/kdsingleapplication/KDSingleApplication + url = git@github.com:KDAB/KDSingleApplication.git + branch = master diff --git a/3rdparty/kdsingleapplication/CMakeLists.txt b/3rdparty/kdsingleapplication/CMakeLists.txt index 257a52d2..547ba1a1 100644 --- a/3rdparty/kdsingleapplication/CMakeLists.txt +++ b/3rdparty/kdsingleapplication/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.7) -set(SOURCES kdsingleapplication.cpp kdsingleapplication_localsocket.cpp) -set(HEADERS kdsingleapplication.h kdsingleapplication_localsocket_p.h) +set(SOURCES KDSingleApplication/src/kdsingleapplication.cpp KDSingleApplication/src/kdsingleapplication_localsocket.cpp) +set(HEADERS KDSingleApplication/src/kdsingleapplication.h KDSingleApplication/src/kdsingleapplication_localsocket_p.h) qt_wrap_cpp(MOC ${HEADERS}) add_library(kdsingleapplication STATIC ${SOURCES} ${MOC}) target_compile_definitions(kdsingleapplication PRIVATE -DKDSINGLEAPPLICATION_STATIC_BUILD) diff --git a/3rdparty/kdsingleapplication/KDSingleApplication b/3rdparty/kdsingleapplication/KDSingleApplication new file mode 160000 index 00000000..ffce501f --- /dev/null +++ b/3rdparty/kdsingleapplication/KDSingleApplication @@ -0,0 +1 @@ +Subproject commit ffce501f4533bb8c19e9a89d712077acecd9c78d diff --git a/3rdparty/kdsingleapplication/LICENSE b/3rdparty/kdsingleapplication/LICENSE deleted file mode 100644 index 12ee5866..00000000 --- a/3rdparty/kdsingleapplication/LICENSE +++ /dev/null @@ -1,6 +0,0 @@ -KDSingleApplication is (C) 2019-2023, Klarälvdalens Datakonsult AB, -and is available under the terms of the MIT license. - -See the full license text in the LICENSES folder. - -Contact KDAB at to inquire about commercial licensing. diff --git a/3rdparty/kdsingleapplication/LICENSES/BSD-3-Clause.txt b/3rdparty/kdsingleapplication/LICENSES/BSD-3-Clause.txt deleted file mode 100644 index 086d3992..00000000 --- a/3rdparty/kdsingleapplication/LICENSES/BSD-3-Clause.txt +++ /dev/null @@ -1,11 +0,0 @@ -Copyright (c) . - -Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: - -1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. - -2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - -3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/3rdparty/kdsingleapplication/LICENSES/MIT.txt b/3rdparty/kdsingleapplication/LICENSES/MIT.txt deleted file mode 100644 index 2071b23b..00000000 --- a/3rdparty/kdsingleapplication/LICENSES/MIT.txt +++ /dev/null @@ -1,9 +0,0 @@ -MIT License - -Copyright (c) - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/3rdparty/kdsingleapplication/README.md b/3rdparty/kdsingleapplication/README.md deleted file mode 100644 index d18e788d..00000000 --- a/3rdparty/kdsingleapplication/README.md +++ /dev/null @@ -1,53 +0,0 @@ -# KDSingleApplication - -`KDSingleApplication` is a helper class for single-instance policy applications -written by [KDAB](https://www.kdab.com). - -## Usage - -Currently the documentation is woefully lacking, but see the examples or tests -for inspiration. Basically it involves: - -1. Create a `Q(Core|Gui)Application` object. -2. Create a `KDSingleApplication` object. -3. Check if the current instance is *primary* (or "master") or - *secondary* (or "slave") by calling `isPrimaryInstance`: - * the *primary* instance needs to listen from messages coming from the - secondary instances, by connecting a slot to the `messageReceived` signal; - * the *secondary* instances can send messages to the primary instance - by calling `sendMessage`. - -## Licensing - -KDSingleApplication is (C) 2019-2023, Klarälvdalens Datakonsult AB, and is available -under the terms of the [MIT license](LICENSES/MIT.txt). - -Contact KDAB at if you need different licensing options. - -## Get Involved - -KDAB will happily accept external contributions. - -Please submit your contributions or issue reports from our GitHub space at -. - -## About KDAB - -KDSingleApplication is supported and maintained by Klarälvdalens Datakonsult AB (KDAB). - -The KDAB Group is the global No.1 software consultancy for Qt, C++ and -OpenGL applications across desktop, embedded and mobile platforms. - -The KDAB Group provides consulting and mentoring for developing Qt applications -from scratch and in porting from all popular and legacy frameworks to Qt. -We continue to help develop parts of Qt and are one of the major contributors -to the Qt Project. We can give advanced or standard trainings anywhere -around the globe on Qt as well as C++, OpenGL, 3D and more. - -Please visit to meet the people who write code like this. - -Stay up-to-date with KDAB product announcements: - -* [KDAB Newsletter](https://news.kdab.com) -* [KDAB Blogs](https://www.kdab.com/category/blogs) -* [KDAB on Twitter](https://twitter.com/KDABQt) diff --git a/3rdparty/kdsingleapplication/kdsingleapplication.cpp b/3rdparty/kdsingleapplication/kdsingleapplication.cpp deleted file mode 100644 index 0fbdb1d9..00000000 --- a/3rdparty/kdsingleapplication/kdsingleapplication.cpp +++ /dev/null @@ -1,106 +0,0 @@ -/* - This file is part of KDSingleApplication. - - SPDX-FileCopyrightText: 2019-2023 Klarälvdalens Datakonsult AB, a KDAB Group company - - SPDX-License-Identifier: MIT - - Contact KDAB at for commercial licensing options. -*/ - -#include "kdsingleapplication.h" - -#include -#include - -// TODO: make this pluggable. -#include "kdsingleapplication_localsocket_p.h" - -// Avoiding dragging in Qt private APIs for now, so this does not inherit -// from QObjectPrivate. -class KDSingleApplicationPrivate -{ -public: - explicit KDSingleApplicationPrivate(const QString &name, KDSingleApplication *q); - - QString name() const - { - return m_name; - } - - bool isPrimaryInstance() const - { - return m_impl.isPrimaryInstance(); - } - - bool sendMessage(const QByteArray &message, int timeout) - { - return m_impl.sendMessage(message, timeout); - } - -private: - Q_DECLARE_PUBLIC(KDSingleApplication) - - KDSingleApplication *q_ptr; - QString m_name; - - KDSingleApplicationLocalSocket m_impl; -}; - -KDSingleApplicationPrivate::KDSingleApplicationPrivate(const QString &name, KDSingleApplication *q) - : q_ptr(q) - , m_name(name) - , m_impl(name) -{ - if (Q_UNLIKELY(name.isEmpty())) - qFatal("KDSingleApplication requires a non-empty application name"); - - if (isPrimaryInstance()) { - QObject::connect(&m_impl, &KDSingleApplicationLocalSocket::messageReceived, - q, &KDSingleApplication::messageReceived); - } -} - -static QString extractExecutableName(const QString &applicationFilePath) -{ - return QFileInfo(applicationFilePath).fileName(); -} - -KDSingleApplication::KDSingleApplication(QObject *parent) - : KDSingleApplication(extractExecutableName(QCoreApplication::applicationFilePath()), parent) -{ -} - -KDSingleApplication::KDSingleApplication(const QString &name, QObject *parent) - : QObject(parent) - , d_ptr(new KDSingleApplicationPrivate(name, this)) -{ -} - -QString KDSingleApplication::name() const -{ - Q_D(const KDSingleApplication); - return d->name(); -} - -bool KDSingleApplication::isPrimaryInstance() const -{ - Q_D(const KDSingleApplication); - return d->isPrimaryInstance(); -} - -bool KDSingleApplication::sendMessage(const QByteArray &message) -{ - return sendMessageWithTimeout(message, 5000); -} - -bool KDSingleApplication::sendMessageWithTimeout(const QByteArray &message, int timeout) -{ - Q_ASSERT(!isPrimaryInstance()); - - Q_D(KDSingleApplication); - return d->sendMessage(message, timeout); -} - - -KDSingleApplication::~KDSingleApplication() = default; diff --git a/3rdparty/kdsingleapplication/kdsingleapplication.h b/3rdparty/kdsingleapplication/kdsingleapplication.h deleted file mode 100644 index 2d9ab6c0..00000000 --- a/3rdparty/kdsingleapplication/kdsingleapplication.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - This file is part of KDSingleApplication. - - SPDX-FileCopyrightText: 2019-2023 Klarälvdalens Datakonsult AB, a KDAB Group company - - SPDX-License-Identifier: MIT - - Contact KDAB at for commercial licensing options. -*/ -#ifndef KDSINGLEAPPLICATION_H -#define KDSINGLEAPPLICATION_H - -#include - -#include - -#include "kdsingleapplication_lib.h" - -class KDSingleApplicationPrivate; - -class KDSINGLEAPPLICATION_EXPORT KDSingleApplication : public QObject -{ - Q_OBJECT - Q_PROPERTY(QString name READ name CONSTANT) - Q_PROPERTY(bool isPrimaryInstance READ isPrimaryInstance CONSTANT) - -public: - explicit KDSingleApplication(QObject *parent = nullptr); - explicit KDSingleApplication(const QString &name, QObject *parent = nullptr); - ~KDSingleApplication(); - - QString name() const; - bool isPrimaryInstance() const; - -public Q_SLOTS: - // avoid default arguments and overloads, as they don't mix with connections - bool sendMessage(const QByteArray &message); - bool sendMessageWithTimeout(const QByteArray &message, int timeout); - -Q_SIGNALS: - void messageReceived(const QByteArray &message); - -private: - Q_DECLARE_PRIVATE(KDSingleApplication) - std::unique_ptr d_ptr; -}; - -#endif // KDSINGLEAPPLICATION_H diff --git a/3rdparty/kdsingleapplication/kdsingleapplication_lib.h b/3rdparty/kdsingleapplication/kdsingleapplication_lib.h deleted file mode 100644 index 060dd673..00000000 --- a/3rdparty/kdsingleapplication/kdsingleapplication_lib.h +++ /dev/null @@ -1,23 +0,0 @@ -/* - This file is part of KDSingleApplication. - - SPDX-FileCopyrightText: 2019-2023 Klarälvdalens Datakonsult AB, a KDAB Group company - - SPDX-License-Identifier: MIT - - Contact KDAB at for commercial licensing options. -*/ -#ifndef KDSINGLEAPPLICATION_LIB_H -#define KDSINGLEAPPLICATION_LIB_H - -#include - -#if defined(KDSINGLEAPPLICATION_STATIC_BUILD) -#define KDSINGLEAPPLICATION_EXPORT -#elif defined(KDSINGLEAPPLICATION_SHARED_BUILD) -#define KDSINGLEAPPLICATION_EXPORT Q_DECL_EXPORT -#else -#define KDSINGLEAPPLICATION_EXPORT Q_DECL_IMPORT -#endif - -#endif // KDSINGLEAPPLICATION_LIB_H diff --git a/3rdparty/kdsingleapplication/kdsingleapplication_localsocket.cpp b/3rdparty/kdsingleapplication/kdsingleapplication_localsocket.cpp deleted file mode 100644 index 04e9812e..00000000 --- a/3rdparty/kdsingleapplication/kdsingleapplication_localsocket.cpp +++ /dev/null @@ -1,324 +0,0 @@ -/* - This file is part of KDSingleApplication. - - SPDX-FileCopyrightText: 2019-2023 Klarälvdalens Datakonsult AB, a KDAB Group company - - SPDX-License-Identifier: MIT - - Contact KDAB at for commercial licensing options. -*/ - -#include "kdsingleapplication_localsocket_p.h" - -#include -#include -#include -#include -#include - -#include -#include - -#include -#include - -#include -#include - -#ifdef Q_OS_UNIX -// for ::getuid() -# include -# include -#endif - -#ifdef Q_OS_WIN -# include -#endif - -namespace { -static constexpr auto LOCALSOCKET_CONNECTION_TIMEOUT = std::chrono::seconds(5); -static constexpr char LOCALSOCKET_PROTOCOL_VERSION = 2; -} // namespace - -#ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wmissing-declarations" -#endif -Q_LOGGING_CATEGORY(kdsaLocalSocket, "kdsingleapplication.localsocket", QtWarningMsg) -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif - -#ifdef __clang__ -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wgnu-zero-variadic-macro-arguments" -#endif - -KDSingleApplicationLocalSocket::KDSingleApplicationLocalSocket(const QString &name, QObject *parent) - : QObject(parent) -{ -#if defined(Q_OS_UNIX) - /* cppcheck-suppress useInitializationList */ - m_socketName = QStringLiteral("kdsingleapp-%1-%2") - .arg(::getuid()) - .arg(name); -#elif defined(Q_OS_WIN) - // I'm not sure of a "global session identifier" on Windows; are - // multiple logins from the same user a possibility? For now, following this: - // https://docs.microsoft.com/en-us/windows/desktop/devnotes/getting-the-session-id-of-the-current-process - - DWORD sessionId; - BOOL haveSessionId = ProcessIdToSessionId(GetCurrentProcessId(), &sessionId); - - m_socketName = QString::fromUtf8("kdsingleapp-%1-%2") - .arg(haveSessionId ? sessionId : 0) - .arg(name); -#else -#error "KDSingleApplication has not been ported to this platform" -#endif - - const QString lockFilePath = - QDir::tempPath() + QLatin1Char('/') + m_socketName + QLatin1String(".lock"); - - qCDebug(kdsaLocalSocket) << "Socket name is" << m_socketName; - qCDebug(kdsaLocalSocket) << "Lock file path is" << lockFilePath; - - std::unique_ptr lockFile(new QLockFile(lockFilePath)); - lockFile->setStaleLockTime(0); - - if (!lockFile->tryLock()) { - // someone else has the lock => we're secondary - qCDebug(kdsaLocalSocket) << "Secondary instance"; - return; - } - - qCDebug(kdsaLocalSocket) << "Primary instance"; - - std::unique_ptr server = std::make_unique(); - if (!server->listen(m_socketName)) { - // maybe the primary crashed, leaving a stale socket; delete it and try again - QLocalServer::removeServer(m_socketName); - if (!server->listen(m_socketName)) { - // TODO: better error handling. - qWarning("KDSingleApplication: unable to make the primary instance listen on %ls: %ls", - qUtf16Printable(m_socketName), - qUtf16Printable(server->errorString())); - - return; - } - } - - connect(server.get(), &QLocalServer::newConnection, - this, &KDSingleApplicationLocalSocket::handleNewConnection); - - m_lockFile = std::move(lockFile); - m_localServer = std::move(server); -} - -KDSingleApplicationLocalSocket::~KDSingleApplicationLocalSocket() = default; - -bool KDSingleApplicationLocalSocket::isPrimaryInstance() const -{ - return m_localServer != nullptr; -} - -bool KDSingleApplicationLocalSocket::sendMessage(const QByteArray &message, int timeout) -{ - Q_ASSERT(!isPrimaryInstance()); - QLocalSocket socket; - - qCDebug(kdsaLocalSocket) << "Preparing to send message" << message << "with timeout" << timeout; - - QDeadlineTimer deadline(timeout); - - // There is an inherent race here with the setup of the server side. - // Even if the socket lock is held by the server, the server may not - // be listening yet. So this connection may fail; keep retrying - // until we hit the timeout. - do { - socket.connectToServer(m_socketName); - if (socket.waitForConnected(static_cast(deadline.remainingTime()))) - break; - } while (!deadline.hasExpired()); - - qCDebug(kdsaLocalSocket) << "Socket state:" << socket.state() << "Timer remaining" << deadline.remainingTime() << "Expired?" << deadline.hasExpired(); - - if (deadline.hasExpired()) { - qCWarning(kdsaLocalSocket) << "Connection timed out"; - return false; - } - - socket.write(&LOCALSOCKET_PROTOCOL_VERSION, 1); - - { - QByteArray encodedMessage; - QDataStream ds(&encodedMessage, QIODevice::WriteOnly); - ds << message; - socket.write(encodedMessage); - } - - qCDebug(kdsaLocalSocket) << "Wrote message in the socket" - << "Timer remaining" << deadline.remainingTime() << "Expired?" << deadline.hasExpired(); - - // There is no acknowledgement mechanism here. - // Should there be one? - - while (socket.bytesToWrite() > 0) { - if (!socket.waitForBytesWritten(static_cast(deadline.remainingTime()))) { - qCWarning(kdsaLocalSocket) << "Message to primary timed out"; - return false; - } - } - - qCDebug(kdsaLocalSocket) << "Bytes written, now disconnecting" - << "Timer remaining" << deadline.remainingTime() << "Expired?" << deadline.hasExpired(); - - socket.disconnectFromServer(); - - if (socket.state() == QLocalSocket::UnconnectedState) { - qCDebug(kdsaLocalSocket) << "Disconnected -- success!"; - return true; - } - - if (!socket.waitForDisconnected(static_cast(deadline.remainingTime()))) { - qCWarning(kdsaLocalSocket) << "Disconnection from primary timed out"; - return false; - } - - qCDebug(kdsaLocalSocket) << "Disconnected -- success!"; - - return true; -} - -void KDSingleApplicationLocalSocket::handleNewConnection() -{ - Q_ASSERT(m_localServer); - - QLocalSocket *socket = nullptr; - while ((socket = m_localServer->nextPendingConnection())) { - qCDebug(kdsaLocalSocket) << "Got new connection on" << m_socketName << "state" << socket->state(); - - Connection c(socket); - socket = c.socket.get(); - - c.readDataConnection = QObjectConnectionHolder( - connect(socket, &QLocalSocket::readyRead, - this, &KDSingleApplicationLocalSocket::readDataFromSecondary)); - - c.secondaryDisconnectedConnection = QObjectConnectionHolder( - connect(socket, &QLocalSocket::disconnected, - this, &KDSingleApplicationLocalSocket::secondaryDisconnected)); - - c.abortConnection = QObjectConnectionHolder( - connect(c.timeoutTimer.get(), &QTimer::timeout, - this, &KDSingleApplicationLocalSocket::abortConnectionToSecondary)); - - m_clients.push_back(std::move(c)); - - // Note that by the time we get here, the socket could've already been closed, - // and no signals emitted (hello, Windows!). Read what's already in the socket. - if (readDataFromSecondarySocket(socket)) - return; - - if (socket->state() == QLocalSocket::UnconnectedState) - secondarySocketDisconnected(socket); - } -} - -template -static auto findConnectionBySocket(Container &container, QLocalSocket *socket) -{ - auto i = std::find_if(container.begin(), - container.end(), - [socket](const auto &c) { return c.socket.get() == socket; }); - Q_ASSERT(i != container.end()); - return i; -} - -template -static auto findConnectionByTimer(Container &container, QTimer *timer) -{ - auto i = std::find_if(container.begin(), - container.end(), - [timer](const auto &c) { return c.timeoutTimer.get() == timer; }); - Q_ASSERT(i != container.end()); - return i; -} - -void KDSingleApplicationLocalSocket::readDataFromSecondary() -{ - QLocalSocket *socket = static_cast(sender()); - readDataFromSecondarySocket(socket); -} - -bool KDSingleApplicationLocalSocket::readDataFromSecondarySocket(QLocalSocket *socket) -{ - auto i = findConnectionBySocket(m_clients, socket); - Connection &c = *i; - c.readData.append(socket->readAll()); - - qCDebug(kdsaLocalSocket) << "Got more data from a secondary. Data read so far:" << c.readData; - - const QByteArray &data = c.readData; - - if (data.size() >= 1) { - if (data[0] != LOCALSOCKET_PROTOCOL_VERSION) { - qCDebug(kdsaLocalSocket) << "Got an invalid protocol version"; - m_clients.erase(i); - return true; - } - } - - QDataStream ds(data); - ds.skipRawData(1); - - ds.startTransaction(); - QByteArray message; - ds >> message; - - if (ds.commitTransaction()) { - qCDebug(kdsaLocalSocket) << "Got a complete message:" << message; - Q_EMIT messageReceived(message); - m_clients.erase(i); - return true; - } - - return false; -} - -void KDSingleApplicationLocalSocket::secondaryDisconnected() -{ - QLocalSocket *socket = static_cast(sender()); - secondarySocketDisconnected(socket); -} - -void KDSingleApplicationLocalSocket::secondarySocketDisconnected(QLocalSocket *socket) -{ - auto i = findConnectionBySocket(m_clients, socket); - Connection c = std::move(*i); - m_clients.erase(i); - - qCDebug(kdsaLocalSocket) << "Secondary disconnected. Data read:" << c.readData; -} - -void KDSingleApplicationLocalSocket::abortConnectionToSecondary() -{ - QTimer *timer = static_cast(sender()); - - auto i = findConnectionByTimer(m_clients, timer); - Connection c = std::move(*i); - m_clients.erase(i); - - qCDebug(kdsaLocalSocket) << "Secondary timed out. Data read:" << c.readData; -} - -KDSingleApplicationLocalSocket::Connection::Connection(QLocalSocket *_socket) - : socket(_socket) - , timeoutTimer(new QTimer) -{ - timeoutTimer->start(LOCALSOCKET_CONNECTION_TIMEOUT); -} - -#ifdef __clang__ -#pragma clang diagnostic pop -#endif diff --git a/3rdparty/kdsingleapplication/kdsingleapplication_localsocket_p.h b/3rdparty/kdsingleapplication/kdsingleapplication_localsocket_p.h deleted file mode 100644 index ec9c60ee..00000000 --- a/3rdparty/kdsingleapplication/kdsingleapplication_localsocket_p.h +++ /dev/null @@ -1,126 +0,0 @@ -/* - This file is part of KDSingleApplication. - - SPDX-FileCopyrightText: 2019-2023 Klarälvdalens Datakonsult AB, a KDAB Group company - - SPDX-License-Identifier: MIT - - Contact KDAB at for commercial licensing options. -*/ - -#ifndef KDSINGLEAPPLICATION_LOCALSOCKET_P_H -#define KDSINGLEAPPLICATION_LOCALSOCKET_P_H - -#include -#include -#include - -QT_BEGIN_NAMESPACE -class QLockFile; -class QLocalServer; -class QLocalSocket; -class QTimer; -QT_END_NAMESPACE - -#include -#include - -struct QObjectDeleteLater -{ - void operator()(QObject *o) - { - o->deleteLater(); - } -}; - -class QObjectConnectionHolder -{ - Q_DISABLE_COPY(QObjectConnectionHolder) - QMetaObject::Connection c; - -public: - QObjectConnectionHolder() - { - } - - explicit QObjectConnectionHolder(QMetaObject::Connection _c) - : c(std::move(_c)) - { - } - - ~QObjectConnectionHolder() - { - QObject::disconnect(c); - } - - QObjectConnectionHolder(QObjectConnectionHolder &&other) noexcept - : c(std::exchange(other.c, {})) - { - } - - QObjectConnectionHolder &operator=(QObjectConnectionHolder &&other) noexcept - { - QObjectConnectionHolder moved(std::move(other)); - swap(moved); - return *this; - } - - void swap(QObjectConnectionHolder &other) noexcept - { - using std::swap; - swap(c, other.c); - } -}; - -class KDSingleApplicationLocalSocket : public QObject -{ - Q_OBJECT - -public: - explicit KDSingleApplicationLocalSocket(const QString &name, - QObject *parent = nullptr); - ~KDSingleApplicationLocalSocket(); - - bool isPrimaryInstance() const; - -public Q_SLOTS: - bool sendMessage(const QByteArray &message, int timeout); - -Q_SIGNALS: - void messageReceived(const QByteArray &message); - -private: - void handleNewConnection(); - void readDataFromSecondary(); - bool readDataFromSecondarySocket(QLocalSocket *socket); - void secondaryDisconnected(); - void secondarySocketDisconnected(QLocalSocket *socket); - void abortConnectionToSecondary(); - - QString m_socketName; - - std::unique_ptr m_lockFile; // protects m_localServer - std::unique_ptr m_localServer; - - struct Connection - { - explicit Connection(QLocalSocket *s); - - std::unique_ptr socket; - std::unique_ptr timeoutTimer; - QByteArray readData; - - // socket/timeoutTimer are deleted via deleteLater (as we delete them - // in slots connected to their signals). Before the deleteLater is acted upon, - // they may emit further signals, triggering logic that it's not supposed - // to be triggered (as the Connection has already been destroyed). - // Use this Holder to break the connections. - QObjectConnectionHolder readDataConnection; - QObjectConnectionHolder secondaryDisconnectedConnection; - QObjectConnectionHolder abortConnection; - }; - - std::vector m_clients; -}; - -#endif // KDSINGLEAPPLICATION_LOCALSOCKET_P_H diff --git a/CMakeLists.txt b/CMakeLists.txt index e2609a98..5579e4b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -307,12 +307,23 @@ else() endif() find_package(${KDSINGLEAPPLICATION_NAME}) if(TARGET KDAB::kdsingleapplication) - message(STATUS "Using system KDSingleApplication") + if(QT_VERSION_MAJOR EQUAL 5) + set(KDSINGLEAPPLICATION_VERSION "${KDSingleApplication_VERSION}") + elseif(QT_VERSION_MAJOR EQUAL 6) + set(KDSINGLEAPPLICATION_VERSION "${KDSingleApplication-qt6_VERSION}") + endif() + if(KDSINGLEAPPLICATION_VERSION GREATER_EQUAL 1.0.95) + set(HAVE_KDSINGLEAPPLICATION_OPTIONS ON) + else() + set(HAVE_KDSINGLEAPPLICATION_OPTIONS OFF) + endif() + message(STATUS "Using system KDSingleApplication (Version ${KDSINGLEAPPLICATION_VERSION})") set(SINGLEAPPLICATION_LIBRARIES KDAB::kdsingleapplication) else() message(STATUS "Using 3rdparty KDSingleApplication") + set(HAVE_KDSINGLEAPPLICATION_OPTIONS ON) add_subdirectory(3rdparty/kdsingleapplication) - set(SINGLEAPPLICATION_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/kdsingleapplication) + set(SINGLEAPPLICATION_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/kdsingleapplication/KDSingleApplication/src) set(SINGLEAPPLICATION_LIBRARIES kdsingleapplication) add_definitions(-DKDSINGLEAPPLICATION_STATIC_BUILD) endif() diff --git a/src/config.h.in b/src/config.h.in index 3be0c0f2..ef8ac505 100644 --- a/src/config.h.in +++ b/src/config.h.in @@ -58,4 +58,6 @@ #cmakedefine HAVE_EBUR128 +#cmakedefine HAVE_KDSINGLEAPPLICATION_OPTIONS + #endif // CONFIG_H_IN diff --git a/src/main.cpp b/src/main.cpp index bb8009f1..a0e78cca 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -156,7 +156,11 @@ int main(int argc, char *argv[]) { // Only start a core application now, so we can check if there's another instance without requiring an X server. // This MUST be done before parsing the commandline options so QTextCodec gets the right system locale for filenames. QCoreApplication core_app(argc, argv); +#ifdef HAVE_KDSINGLEAPPLICATION_OPTIONS + KDSingleApplication single_app(QCoreApplication::applicationName(), KDSingleApplication::Option::IncludeUsernameInSocketName); +#else KDSingleApplication single_app(QCoreApplication::applicationName()); +#endif // Parse commandline options - need to do this before starting the full QApplication, so it works without an X server if (!options.Parse()) return 1; logging::SetLevels(options.log_levels()); @@ -193,7 +197,11 @@ int main(int argc, char *argv[]) { QGuiApplication::setQuitOnLastWindowClosed(false); QApplication a(argc, argv); +#ifdef HAVE_KDSINGLEAPPLICATION_OPTIONS + KDSingleApplication single_app(QCoreApplication::applicationName(), KDSingleApplication::Option::IncludeUsernameInSocketName); +#else KDSingleApplication single_app(QCoreApplication::applicationName()); +#endif if (!single_app.isPrimaryInstance()) { if (options.is_empty()) { qLog(Info) << "Strawberry is already running - activating existing window (2)";