Stop using new for processes

This commit is contained in:
Anton Thomasson 2021-06-12 11:57:39 +02:00
parent 22b6ec1c63
commit 76fdbb987f

View file

@ -211,37 +211,35 @@ try {
} }
else else
{ // We are converting to a raster format { // We are converting to a raster format
QProcess* ppm2pwg = new QProcess(this); QProcess ppm2pwg(this);
// Yo dawg, I heard you like programs... // Yo dawg, I heard you like programs...
ppm2pwg->setProgram("harbour-seaprint"); ppm2pwg.setProgram("harbour-seaprint");
ppm2pwg->setArguments({"ppm2pwg"}); ppm2pwg.setArguments({"ppm2pwg"});
QStringList env; QStringList env;
ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, false, false, false, 0); ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, false, false, false, 0);
qDebug() << "ppm2pwg env is " << env; qDebug() << "ppm2pwg env is " << env;
ppm2pwg->setEnvironment(env); ppm2pwg.setEnvironment(env);
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append); ppm2pwg.setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater()));
qDebug() << "All connected"; qDebug() << "All connected";
ppm2pwg->start(); ppm2pwg.start();
bool gray = Colors == 0 ? inImage.allGray() : Colors == 1; bool gray = Colors == 0 ? inImage.allGray() : Colors == 1;
outImage.save(ppm2pwg, gray ? "pgm" : "ppm"); outImage.save(&ppm2pwg, gray ? "pgm" : "ppm");
qDebug() << "Starting"; qDebug() << "Starting";
if(!ppm2pwg->waitForStarted()) if(!ppm2pwg.waitForStarted())
{ {
qDebug() << "ppm2pwg died"; qDebug() << "ppm2pwg died";
throw ConvertFailedException(); throw ConvertFailedException();
} }
qDebug() << "All started"; qDebug() << "All started";
ppm2pwg->waitForFinished(); ppm2pwg.waitForFinished();
qDebug() << "Finished"; qDebug() << "Finished";
} }
@ -281,8 +279,8 @@ try {
throw ConvertFailedException(tr("Unsupported PDF paper size")); throw ConvertFailedException(tr("Unsupported PDF paper size"));
} }
QProcess* CalligraConverter = new QProcess(this); QProcess CalligraConverter(this);
CalligraConverter->setProgram("calligraconverter"); CalligraConverter.setProgram("calligraconverter");
QStringList CalligraConverterArgs = {"--batch", "--mimetype", Mimer::PDF, "--print-orientation", "Portrait", "--print-papersize", ShortPaperSize}; QStringList CalligraConverterArgs = {"--batch", "--mimetype", Mimer::PDF, "--print-orientation", "Portrait", "--print-papersize", ShortPaperSize};
CalligraConverterArgs << filename; CalligraConverterArgs << filename;
@ -292,15 +290,13 @@ try {
CalligraConverterArgs << tmpPdfFile.fileName(); CalligraConverterArgs << tmpPdfFile.fileName();
qDebug() << "CalligraConverteArgs is" << CalligraConverterArgs; qDebug() << "CalligraConverteArgs is" << CalligraConverterArgs;
CalligraConverter->setArguments(CalligraConverterArgs); CalligraConverter.setArguments(CalligraConverterArgs);
connect(CalligraConverter, SIGNAL(finished(int, QProcess::ExitStatus)), CalligraConverter, SLOT(deleteLater())); CalligraConverter.start();
CalligraConverter->start();
qDebug() << "CalligraConverter Starting"; qDebug() << "CalligraConverter Starting";
if(!CalligraConverter->waitForStarted()) if(!CalligraConverter.waitForStarted())
{ {
qDebug() << "CalligraConverter died"; qDebug() << "CalligraConverter died";
throw ConvertFailedException(); throw ConvertFailedException();
@ -308,7 +304,7 @@ try {
qDebug() << "CalligraConverter Started"; qDebug() << "CalligraConverter Started";
if(!CalligraConverter->waitForFinished(-1)) if(!CalligraConverter.waitForFinished(-1))
{ {
qDebug() << "CalligraConverter failed"; qDebug() << "CalligraConverter failed";
throw ConvertFailedException(); throw ConvertFailedException();
@ -413,8 +409,8 @@ QString ConvertWorker::getPopplerShortPaperSize(QString PaperSize)
void ConvertWorker::adjustPageRange(QString PaperSize, quint32 PageRangeLow, quint32 PageRangeHigh, void ConvertWorker::adjustPageRange(QString PaperSize, quint32 PageRangeLow, quint32 PageRangeHigh,
QString pdfFileName, QTemporaryFile* tempfile) QString pdfFileName, QTemporaryFile* tempfile)
{ {
QProcess* pdftocairo = new QProcess(this); QProcess pdftocairo(this);
pdftocairo->setProgram("pdftocairo"); pdftocairo.setProgram("pdftocairo");
QStringList PdfToCairoArgs = {"-pdf"}; QStringList PdfToCairoArgs = {"-pdf"};
QString ShortPaperSize = getPopplerShortPaperSize(PaperSize); QString ShortPaperSize = getPopplerShortPaperSize(PaperSize);
@ -424,16 +420,15 @@ void ConvertWorker::adjustPageRange(QString PaperSize, quint32 PageRangeLow, qui
PdfToCairoArgs << QStringList {"-paper", ShortPaperSize, pdfFileName, "-"}; PdfToCairoArgs << QStringList {"-paper", ShortPaperSize, pdfFileName, "-"};
qDebug() << "pdftocairo args is " << PdfToCairoArgs; qDebug() << "pdftocairo args is " << PdfToCairoArgs;
pdftocairo->setArguments(PdfToCairoArgs); pdftocairo.setArguments(PdfToCairoArgs);
pdftocairo->setStandardOutputFile(tempfile->fileName(), QIODevice::Append); pdftocairo.setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
connect(pdftocairo, SIGNAL(finished(int, QProcess::ExitStatus)), pdftocairo, SLOT(deleteLater()));
pdftocairo->start(); pdftocairo.start();
qDebug() << "Starting"; qDebug() << "Starting";
if(!pdftocairo->waitForStarted()) if(!pdftocairo.waitForStarted())
{ {
qDebug() << "pdftocairo died"; qDebug() << "pdftocairo died";
throw ConvertFailedException(); throw ConvertFailedException();
@ -441,7 +436,7 @@ void ConvertWorker::adjustPageRange(QString PaperSize, quint32 PageRangeLow, qui
qDebug() << "Started"; qDebug() << "Started";
if(!pdftocairo->waitForFinished(-1)) if(!pdftocairo.waitForFinished(-1))
{ {
qDebug() << "pdftocairo failed"; qDebug() << "pdftocairo failed";
throw ConvertFailedException(); throw ConvertFailedException();
@ -451,8 +446,8 @@ void ConvertWorker::adjustPageRange(QString PaperSize, quint32 PageRangeLow, qui
void ConvertWorker::pdftoPs(QString PaperSize, bool TwoSided, quint32 PageRangeLow, quint32 PageRangeHigh, void ConvertWorker::pdftoPs(QString PaperSize, bool TwoSided, quint32 PageRangeLow, quint32 PageRangeHigh,
QString pdfFileName, QTemporaryFile* tempfile) QString pdfFileName, QTemporaryFile* tempfile)
{ {
QProcess* pdftops = new QProcess(this); QProcess pdftops(this);
pdftops->setProgram("pdftops"); pdftops.setProgram("pdftops");
QStringList PdfToPsArgs; QStringList PdfToPsArgs;
if(TwoSided) if(TwoSided)
{ {
@ -466,16 +461,15 @@ void ConvertWorker::pdftoPs(QString PaperSize, bool TwoSided, quint32 PageRangeL
PdfToPsArgs << QStringList {"-paper", ShortPaperSize, pdfFileName, "-"}; PdfToPsArgs << QStringList {"-paper", ShortPaperSize, pdfFileName, "-"};
qDebug() << "pdftops args is " << PdfToPsArgs; qDebug() << "pdftops args is " << PdfToPsArgs;
pdftops->setArguments(PdfToPsArgs); pdftops.setArguments(PdfToPsArgs);
pdftops->setStandardOutputFile(tempfile->fileName(), QIODevice::Append); pdftops.setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
connect(pdftops, SIGNAL(finished(int, QProcess::ExitStatus)), pdftops, SLOT(deleteLater()));
pdftops->start(); pdftops.start();
qDebug() << "Starting"; qDebug() << "Starting";
if(!pdftops->waitForStarted()) if(!pdftops.waitForStarted())
{ {
qDebug() << "pdftops died"; qDebug() << "pdftops died";
throw ConvertFailedException(); throw ConvertFailedException();
@ -483,7 +477,7 @@ void ConvertWorker::pdftoPs(QString PaperSize, bool TwoSided, quint32 PageRangeL
qDebug() << "Started"; qDebug() << "Started";
if(!pdftops->waitForFinished(-1)) if(!pdftops.waitForFinished(-1))
{ {
qDebug() << "pdftops failed"; qDebug() << "pdftops failed";
throw ConvertFailedException(); throw ConvertFailedException();
@ -510,12 +504,12 @@ void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Qu
pages = PageRangeHigh-PageRangeLow+1; pages = PageRangeHigh-PageRangeLow+1;
qDebug() << "PageRangeLow" << PageRangeLow << "PageRangeHigh" << PageRangeHigh << "pages" << pages; qDebug() << "PageRangeLow" << PageRangeLow << "PageRangeHigh" << PageRangeHigh << "pages" << pages;
QProcess* pdftocairo = new QProcess(this); QProcess pdftocairo(this);
pdftocairo->setProgram("pdftocairo"); pdftocairo.setProgram("pdftocairo");
QStringList PdfToCairoArgs; QStringList PdfToCairoArgs;
QProcess* pdftoppm = new QProcess(this); QProcess pdftoppm(this);
pdftoppm->setProgram("pdftoppm"); pdftoppm.setProgram("pdftoppm");
QStringList Pdf2PpmArgs = {"-rx", QString::number(HwResX), "-ry", QString::number(HwResY)}; QStringList Pdf2PpmArgs = {"-rx", QString::number(HwResX), "-ry", QString::number(HwResY)};
if(resize) if(resize)
@ -526,16 +520,14 @@ void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Qu
PageRangeLow = PageRangeHigh = 0; PageRangeLow = PageRangeHigh = 0;
PdfToCairoArgs << QStringList {"-pdf", "-paper", ShortPaperSize, pdfFileName, "-"}; PdfToCairoArgs << QStringList {"-pdf", "-paper", ShortPaperSize, pdfFileName, "-"};
pdftocairo->setArguments(PdfToCairoArgs); pdftocairo.setArguments(PdfToCairoArgs);
connect(pdftocairo, SIGNAL(finished(int, QProcess::ExitStatus)), pdftocairo, SLOT(deleteLater())); pdftocairo.setStandardOutputProcess(&pdftoppm);
pdftocairo->setStandardOutputProcess(pdftoppm);
} }
else else
{ {
Pdf2PpmArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)}; Pdf2PpmArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
pdftoppm->setStandardInputFile(pdfFileName); pdftoppm.setStandardInputFile(pdfFileName);
} }
@ -544,13 +536,13 @@ void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Qu
Pdf2PpmArgs.append("-gray"); Pdf2PpmArgs.append("-gray");
} }
qDebug() << "pdf2ppm args is " << Pdf2PpmArgs; qDebug() << "pdf2ppm args is " << Pdf2PpmArgs;
pdftoppm->setArguments(Pdf2PpmArgs); pdftoppm.setArguments(Pdf2PpmArgs);
QProcess* ppm2pwg = new QProcess(this); QProcess ppm2pwg(this);
// Yo dawg, I heard you like programs... // Yo dawg, I heard you like programs...
ppm2pwg->setProgram("harbour-seaprint"); ppm2pwg.setProgram("harbour-seaprint");
ppm2pwg->setArguments({"ppm2pwg"}); ppm2pwg.setArguments({"ppm2pwg"});
bool urf = targetFormat == Mimer::URF; bool urf = targetFormat == Mimer::URF;
@ -558,36 +550,29 @@ void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Qu
ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, TwoSided, Tumble, true, pages); ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, TwoSided, Tumble, true, pages);
qDebug() << "ppm2pwg env is " << env; qDebug() << "ppm2pwg env is " << env;
ppm2pwg->setEnvironment(env); ppm2pwg.setEnvironment(env);
pdftoppm->setStandardOutputProcess(ppm2pwg); pdftoppm.setStandardOutputProcess(&ppm2pwg);
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append); ppm2pwg.setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
connect(pdftoppm, SIGNAL(finished(int, QProcess::ExitStatus)), pdftoppm, SLOT(deleteLater()));
connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater()));
qDebug() << "All connected"; qDebug() << "All connected";
if(resize) if(resize)
{ {
pdftocairo->start(); pdftocairo.start();
}
else
{
delete pdftocairo;
} }
pdftoppm->start(); pdftoppm.start();
ppm2pwg->start(); ppm2pwg.start();
qDebug() << "Starting"; qDebug() << "Starting";
if(!pdftoppm->waitForStarted()) if(!pdftoppm.waitForStarted())
{ {
qDebug() << "pdftoppm died"; qDebug() << "pdftoppm died";
throw ConvertFailedException(); throw ConvertFailedException();
} }
if(!ppm2pwg->waitForStarted()) if(!ppm2pwg.waitForStarted())
{ {
qDebug() << "ppm2pwg died"; qDebug() << "ppm2pwg died";
throw ConvertFailedException(); throw ConvertFailedException();
@ -598,14 +583,14 @@ void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Qu
for(;;) for(;;)
{ {
if(ppm2pwg->waitForFinished(250)) if(ppm2pwg.waitForFinished(250))
{ {
ppm2pwgSuccess = true; ppm2pwgSuccess = true;
break; break;
} }
else else
{ {
QList<QByteArray> ppm2pwgOutput = ppm2pwg->readAllStandardError().split('\n'); QList<QByteArray> ppm2pwgOutput = ppm2pwg.readAllStandardError().split('\n');
for(QList<QByteArray>::iterator it = ppm2pwgOutput.begin(); it != ppm2pwgOutput.end(); it++) for(QList<QByteArray>::iterator it = ppm2pwgOutput.begin(); it != ppm2pwgOutput.end(); it++)
{ {
if(it->startsWith("Page")) if(it->startsWith("Page"))