nextcloud-desktop/src/gui/macOS/fileprovidersocketserver.cpp
Claudio Cambra 034fbb8245
Add file provider socket state provider method in socket server
Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
2024-07-23 14:08:24 +08:00

105 lines
3.3 KiB
C++

/*
* Copyright (C) 2022 by Claudio Cambra <claudio.cambra@nextcloud.com>
*
* 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 <QLocalSocket>
#include <QLoggingCategory>
#include "libsync/account.h"
#include "fileprovidersocketcontroller.h"
namespace OCC {
namespace Mac {
Q_LOGGING_CATEGORY(lcFileProviderSocketServer, "nextcloud.gui.macos.fileprovider.socketserver", QtInfoMsg)
FileProviderSocketServer::FileProviderSocketServer(QObject *parent)
: QObject{parent}
{
_socketPath = fileProviderSocketPath();
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::syncStateChanged,
this, &FileProviderSocketServer::syncStateChanged);
connect(socketController.data(), &FileProviderSocketController::socketDestroyed,
this, &FileProviderSocketServer::slotSocketDestroyed);
_socketControllers.insert(socket, socketController);
socketController->start();
}
void FileProviderSocketServer::slotSocketDestroyed(const QLocalSocket * const socket)
{
const auto socketController = _socketControllers.take(socket);
if (socketController) {
const auto rawSocketControllerPtr = socketController.data();
delete rawSocketControllerPtr;
}
}
FileProviderSocketState FileProviderSocketServer::socketStateForAccount(const QString &userIdAtHost) const
{
auto connected = false;
auto latestStatus = SyncResult::Undefined;
for (const auto &controller : _socketControllers) {
if (controller->accountState()->account()->userIdAtHostWithPort() == userIdAtHost) {
connected = true;
latestStatus = controller->latestStatus();
break;
}
}
return { .connected = connected, .latestStatus = latestStatus };
}
} // namespace Mac
} // namespace OCC