2021-09-28 21:44:09 +02:00
|
|
|
#include <QFile>
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QProcess>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QLocalSocket>
|
|
|
|
#include <QDataStream>
|
|
|
|
#include "ipcserver.h"
|
2022-04-24 12:34:34 +02:00
|
|
|
#include "common.h"
|
|
|
|
#include "databasefactory.h"
|
|
|
|
#include "../shared/logger.h"
|
2022-05-27 09:26:37 +02:00
|
|
|
#include "renderconfig.h"
|
|
|
|
#include "rendertarget.h"
|
|
|
|
#include "ipcpreviewworker.h"
|
2021-09-28 21:44:09 +02:00
|
|
|
|
|
|
|
IpcServer::IpcServer()
|
|
|
|
{
|
2022-05-28 17:24:42 +02:00
|
|
|
/* Only 1, we are doing work for the GUI, not a service for general availability */
|
|
|
|
this->spawningServer.setMaxPendingConnections(1);
|
2021-09-28 21:44:09 +02:00
|
|
|
connect(&this->spawningServer, &QLocalServer::newConnection, this, &IpcServer::spawnerNewConnection);
|
2022-05-28 17:24:42 +02:00
|
|
|
connect(&this->previewWorker, &IPCPreviewWorker::previewGenerated, this, &IpcServer::handlePreviewGenerated);
|
|
|
|
connect(&this->previewWorker, &IPCPreviewWorker::finished, this, [this] { this->currentSocket->flush(); });
|
2021-09-28 21:44:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IpcServer::startSpawner(QString socketPath)
|
|
|
|
{
|
|
|
|
QFile::remove(socketPath);
|
|
|
|
return this->spawningServer.listen(socketPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IpcServer::spawnerNewConnection()
|
|
|
|
{
|
2022-05-27 09:26:37 +02:00
|
|
|
QLocalSocket *socket = this->spawningServer.nextPendingConnection();
|
|
|
|
connect(socket, &QLocalSocket::disconnected, socket, &QLocalSocket::deleteLater);
|
2022-05-28 17:24:42 +02:00
|
|
|
this->currentSocket = socket;
|
2022-05-27 09:26:37 +02:00
|
|
|
if(socket != nullptr)
|
2021-09-28 21:44:09 +02:00
|
|
|
{
|
|
|
|
if(!socket->waitForReadyRead())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-05-27 09:26:37 +02:00
|
|
|
QDataStream stream(socket);
|
2021-09-28 21:44:09 +02:00
|
|
|
IPCCommand command;
|
|
|
|
stream >> command;
|
2022-05-17 19:22:59 +02:00
|
|
|
if(command == GeneratePreviews)
|
2021-09-28 21:44:09 +02:00
|
|
|
{
|
2022-05-17 19:22:59 +02:00
|
|
|
RenderConfig renderConfig;
|
|
|
|
QVector<RenderTarget> targets;
|
2022-05-27 09:26:37 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
/* TODO: this is not entirely robust */
|
|
|
|
socket->waitForReadyRead(100);
|
|
|
|
stream.startTransaction();
|
|
|
|
stream >> renderConfig >> targets;
|
|
|
|
} while(!stream.commitTransaction() && socket->state() == QLocalSocket::ConnectedState);
|
|
|
|
|
|
|
|
stream << targets.count();
|
|
|
|
socket->flush();
|
2022-05-28 17:24:42 +02:00
|
|
|
previewWorker.start(renderConfig, targets, socket);
|
|
|
|
}
|
|
|
|
if(command == StopGeneratePreviews)
|
|
|
|
{
|
|
|
|
previewWorker.stop();
|
2021-09-28 21:44:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-28 17:24:42 +02:00
|
|
|
|
|
|
|
void IpcServer::handlePreviewGenerated(QByteArray ba)
|
|
|
|
{
|
|
|
|
QDataStream stream{this->currentSocket};
|
|
|
|
stream << ba;
|
|
|
|
this->currentSocket->flush();
|
|
|
|
}
|