From c5560db690dd2346c098d9a0476695fb0780a906 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Thu, 22 Dec 2022 14:37:30 +0100 Subject: [PATCH] Make the socket manager just manage the server, leave the socket controller in charge of controlling the socket Signed-off-by: Claudio Cambra --- src/gui/CMakeLists.txt | 6 +- .../macOS/fileprovidersocketcontroller.cpp | 27 +++++- src/gui/macOS/fileprovidersocketcontroller.h | 13 ++- src/gui/macOS/fileprovidersocketmanager.cpp | 90 ------------------- src/gui/macOS/fileprovidersocketserver.cpp | 78 ++++++++++++++++ ...etmanager.h => fileprovidersocketserver.h} | 12 ++- ...mac.mm => fileprovidersocketserver_mac.mm} | 0 7 files changed, 118 insertions(+), 108 deletions(-) delete mode 100644 src/gui/macOS/fileprovidersocketmanager.cpp create mode 100644 src/gui/macOS/fileprovidersocketserver.cpp rename src/gui/macOS/{fileprovidersocketmanager.h => fileprovidersocketserver.h} (78%) rename src/gui/macOS/{fileprovidersocketmanager_mac.mm => fileprovidersocketserver_mac.mm} (100%) diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt index a7816d401..645076050 100644 --- a/src/gui/CMakeLists.txt +++ b/src/gui/CMakeLists.txt @@ -287,9 +287,9 @@ IF( APPLE ) macOS/fileproviderdomainmanager_mac.mm macOS/fileprovidersocketcontroller.h macOS/fileprovidersocketcontroller.cpp - macOS/fileprovidersocketmanager.h - macOS/fileprovidersocketmanager.cpp - macOS/fileprovidersocketmanager_mac.mm) + macOS/fileprovidersocketserver.h + macOS/fileprovidersocketserver.cpp + macOS/fileprovidersocketserver_mac.mm) if(SPARKLE_FOUND AND BUILD_UPDATER) # Define this, we need to check in updater.cpp diff --git a/src/gui/macOS/fileprovidersocketcontroller.cpp b/src/gui/macOS/fileprovidersocketcontroller.cpp index ceaa65936..fd1ff5360 100644 --- a/src/gui/macOS/fileprovidersocketcontroller.cpp +++ b/src/gui/macOS/fileprovidersocketcontroller.cpp @@ -14,17 +14,36 @@ #include "fileprovidersocketcontroller.h" -namespace OCC -{ +#include -namespace Mac -{ +namespace OCC { + +namespace Mac { + +Q_LOGGING_CATEGORY(lcFileProviderSocketController, "nextcloud.gui.macos.fileprovider.socketcontroller", QtInfoMsg) FileProviderSocketController::FileProviderSocketController(QLocalSocket *socket, QObject *parent) : QObject{parent} , _socket(socket) { + connect(socket, &QLocalSocket::disconnected, + this, &FileProviderSocketController::slotOnDisconnected); + connect(socket, &QLocalSocket::destroyed, + this, &FileProviderSocketController::slotSocketDestroyed); +} +void FileProviderSocketController::slotOnDisconnected() +{ + qCInfo(lcFileProviderSocketController) << "File provider socket disconnected"; + _socket->deleteLater(); + Q_EMIT socketDestroyed(_socket); +} + +void FileProviderSocketController::slotSocketDestroyed(QObject *object) +{ + Q_UNUSED(object) + qCInfo(lcFileProviderSocketController) << "File provider socket object has been destroyed, destroying controller"; + Q_EMIT socketDestroyed(_socket); } } diff --git a/src/gui/macOS/fileprovidersocketcontroller.h b/src/gui/macOS/fileprovidersocketcontroller.h index 169a622c2..f660a38e6 100644 --- a/src/gui/macOS/fileprovidersocketcontroller.h +++ b/src/gui/macOS/fileprovidersocketcontroller.h @@ -18,11 +18,9 @@ #include #include -namespace OCC -{ +namespace OCC { -namespace Mac -{ +namespace Mac { class FileProviderSocketController : public QObject { @@ -31,6 +29,13 @@ class FileProviderSocketController : public QObject public: explicit FileProviderSocketController(QLocalSocket *socket, QObject *parent = nullptr); +signals: + void socketDestroyed(const QLocalSocket * const socket); + +private slots: + void slotOnDisconnected(); + void slotSocketDestroyed(QObject *object); + private: QPointer _socket; }; diff --git a/src/gui/macOS/fileprovidersocketmanager.cpp b/src/gui/macOS/fileprovidersocketmanager.cpp deleted file mode 100644 index 03257a467..000000000 --- a/src/gui/macOS/fileprovidersocketmanager.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2022 by Claudio Cambra - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * for more details. - */ - -#include "fileprovidersocketmanager.h" - -#include - -#include "fileprovidersocketcontroller.h" - -namespace OCC -{ - -namespace Mac -{ - -Q_LOGGING_CATEGORY(lcFileProviderSocketManager, "nextcloud.gui.macos.fileprovider.socketmanager", QtInfoMsg) - -FileProviderSocketManager::FileProviderSocketManager(QObject *parent) - : QObject{parent} -{ -#ifdef Q_OS_MACOS - _socketPath = fileProviderSocketPath(); -#endif - startListening(); -} - -void FileProviderSocketManager::startListening() -{ - QLocalServer::removeServer(_socketPath); - - const auto serverStarted = _socketServer.listen(_socketPath); - if (!serverStarted) { - qCWarning(lcFileProviderSocketManager) << "Could not start file provider socket server" - << _socketPath; - } else { - qCInfo(lcFileProviderSocketManager) << "File provider socket server started, listening" - << _socketPath; - } - - connect(&_socketServer, &QLocalServer::newConnection, - this, &FileProviderSocketManager::slotNewConnection); -} - -void FileProviderSocketManager::slotNewConnection() -{ - if (!_socketServer.hasPendingConnections()) { - return; - } - - const auto socket = _socketServer.nextPendingConnection(); - if (!socket) { - return; - } - - connect(socket, &QLocalSocket::disconnected, - this, &FileProviderSocketManager::slotOnDisconnected); - connect(socket, &QLocalSocket::destroyed, - this, &FileProviderSocketManager::slotSocketDestroyed); - - const FileProviderSocketControllerPtr controller(new FileProviderSocketController(socket)); - _socketControllers.insert(socket, controller); -} - -void FileProviderSocketManager::slotOnDisconnected() -{ - const auto socket = qobject_cast(sender()); - Q_ASSERT(socket); - socket->deleteLater(); -} - -void FileProviderSocketManager::slotSocketDestroyed(QObject *object) -{ - const auto socket = qobject_cast(object); - _socketControllers.remove(socket); -} - -} // namespace Mac - -} // namespace OCC diff --git a/src/gui/macOS/fileprovidersocketserver.cpp b/src/gui/macOS/fileprovidersocketserver.cpp new file mode 100644 index 000000000..7ebf55ea5 --- /dev/null +++ b/src/gui/macOS/fileprovidersocketserver.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2022 by Claudio Cambra + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +#include "fileprovidersocketserver.h" + +#include + +#include "fileprovidersocketcontroller.h" + +namespace OCC { + +namespace Mac { + +Q_LOGGING_CATEGORY(lcFileProviderSocketServer, "nextcloud.gui.macos.fileprovider.socketserver", QtInfoMsg) + +FileProviderSocketServer::FileProviderSocketServer(QObject *parent) + : QObject{parent} +{ +#ifdef Q_OS_MACOS + _socketPath = fileProviderSocketPath(); +#endif + startListening(); +} + +void FileProviderSocketServer::startListening() +{ + QLocalServer::removeServer(_socketPath); + + const auto serverStarted = _socketServer.listen(_socketPath); + if (!serverStarted) { + qCWarning(lcFileProviderSocketServer) << "Could not start file provider socket server" + << _socketPath; + } else { + qCInfo(lcFileProviderSocketServer) << "File provider socket server started, listening" + << _socketPath; + } + + connect(&_socketServer, &QLocalServer::newConnection, + this, &FileProviderSocketServer::slotNewConnection); +} + +void FileProviderSocketServer::slotNewConnection() +{ + if (!_socketServer.hasPendingConnections()) { + return; + } + + qCInfo(lcFileProviderSocketServer) << "New connection in file provider socket server"; + const auto socket = _socketServer.nextPendingConnection(); + if (!socket) { + return; + } + + const FileProviderSocketControllerPtr socketController(new FileProviderSocketController(socket, this)); + connect(socketController.data(), &FileProviderSocketController::socketDestroyed, + this, &FileProviderSocketServer::slotSocketDestroyed); + _socketControllers.insert(socket, socketController); +} + +void FileProviderSocketServer::slotSocketDestroyed(const QLocalSocket * const socket) +{ + _socketControllers.remove(socket); +} + +} // namespace Mac + +} // namespace OCC diff --git a/src/gui/macOS/fileprovidersocketmanager.h b/src/gui/macOS/fileprovidersocketserver.h similarity index 78% rename from src/gui/macOS/fileprovidersocketmanager.h rename to src/gui/macOS/fileprovidersocketserver.h index 12f3f2d4d..8ad298228 100644 --- a/src/gui/macOS/fileprovidersocketmanager.h +++ b/src/gui/macOS/fileprovidersocketserver.h @@ -18,8 +18,7 @@ #include #include -namespace OCC -{ +namespace OCC { namespace Mac { @@ -30,23 +29,22 @@ using FileProviderSocketControllerPtr = QSharedPointer _socketControllers; + QHash _socketControllers; }; } // namespace Mac diff --git a/src/gui/macOS/fileprovidersocketmanager_mac.mm b/src/gui/macOS/fileprovidersocketserver_mac.mm similarity index 100% rename from src/gui/macOS/fileprovidersocketmanager_mac.mm rename to src/gui/macOS/fileprovidersocketserver_mac.mm