looqs/gui/ipcpreviewclient.cpp

134 lignes
3.1 KiB
C++
Brut Vue normale Historique

2022-05-27 09:29:56 +02:00
#include <QLocalSocket>
#include <QApplication>
#include "ipc.h"
#include "ipcpreviewclient.h"
#include "previewresultpdf.h"
#include "previewresultplaintext.h"
bool IPCPreviewClient::connect()
{
if(socket->state() == QLocalSocket::ConnectedState)
{
socket->disconnectFromServer();
if(socket->state() == QLocalSocket::ConnectedState)
{
socket->waitForDisconnected(100);
}
}
socket->connectToServer(socketPath);
socket->waitForConnected(100);
return socket->state() == QLocalSocket::ConnectedState;
}
QSharedPointer<PreviewResult> IPCPreviewClient::deserialize(QByteArray &array)
{
QDataStream stream{&array, QIODevice::ReadOnly};
PreviewResultType type;
stream >> type;
if(type == PreviewResultType::PDF)
{
return PreviewResultPdf::deserialize(array);
}
if(type == PreviewResultType::PlainText)
{
return PreviewResultPlainText::deserialize(array);
}
return QSharedPointer<PreviewResult>(nullptr);
}
IPCPreviewClient::IPCPreviewClient()
{
this->socket = new QLocalSocket(this);
}
void IPCPreviewClient::setSocketPath(QString socketPath)
{
this->socketPath = socketPath;
}
void IPCPreviewClient::startGeneration(RenderConfig config, const QVector<RenderTarget> &targets)
{
this->start(config, targets);
}
void IPCPreviewClient::start(RenderConfig config, const QVector<RenderTarget> &targets)
{
if(targets.count() == 0)
{
return;
}
if(!connect() || !socket->isOpen())
{
2022-05-28 22:19:26 +02:00
emit error("Could not connect to IPC worker");
return;
2022-05-27 09:29:56 +02:00
}
if(socket->isOpen() && socket->isWritable())
{
QDataStream stream(socket);
2024-05-20 19:14:40 +02:00
stream.startTransaction();
2022-05-27 09:29:56 +02:00
stream << GeneratePreviews;
stream << config;
stream << targets;
2024-05-20 19:14:40 +02:00
stream.commitTransaction();
2022-05-27 09:29:56 +02:00
socket->flush();
2024-05-20 19:14:40 +02:00
qsizetype numTarget = 0;
2022-05-27 09:29:56 +02:00
if(socket->isOpen() && socket->isReadable() && socket->state() == QLocalSocket::ConnectedState)
{
do
{
2024-05-20 19:14:40 +02:00
socket->waitForReadyRead(5000);
2022-05-27 09:29:56 +02:00
stream.startTransaction();
stream >> numTarget;
} while(!stream.commitTransaction() && socket->state() == QLocalSocket::ConnectedState);
if(numTarget != targets.count())
{
2024-05-20 19:14:40 +02:00
qDebug() << "Target count mismatch";
emit error("IPC Error: Server reports less targets than it should");
return;
2022-05-27 09:29:56 +02:00
}
}
else
{
2022-05-28 22:19:26 +02:00
emit error("Error while trying to process previews: " + socket->errorString());
return;
2022-05-27 09:29:56 +02:00
}
int processed = 0;
++this->currentPreviewGeneration;
while(socket->isOpen() && socket->isReadable() && processed < targets.count())
{
QByteArray array;
do
{
socket->waitForReadyRead(100);
stream.startTransaction();
stream >> array;
} while(!stream.commitTransaction() && socket->state() == QLocalSocket::ConnectedState);
emit previewReceived(deserialize(array), this->currentPreviewGeneration);
++processed;
}
if(processed != targets.count())
{
2022-05-28 22:19:26 +02:00
emit error("IPC worker didn't send enough previews. This is a bug, please report");
2022-05-27 09:29:56 +02:00
}
}
socket->disconnectFromServer();
emit finished();
}
void IPCPreviewClient::stopGeneration()
{
if(!connect() || !socket->isOpen())
{
2022-05-28 22:19:26 +02:00
emit error("Could not connect to IPC worker");
return;
2022-05-27 09:29:56 +02:00
}
QDataStream stream(socket);
stream << StopGeneratePreviews;
socket->flush();
}