Remove more redundant logic

This commit is contained in:
Anton Thomasson 2021-11-14 21:01:18 +01:00
parent 0ab3d5486d
commit 32d3cc6587
11 changed files with 74 additions and 317 deletions

View file

@ -115,8 +115,7 @@ Page {
tag: IppMsg.IntegerRange tag: IppMsg.IntegerRange
name: "page-ranges" name: "page-ranges"
prettyName: qsTr("Page range") prettyName: qsTr("Page range")
valid: (_valid || ConvertChecker.pdf) && valid: (selectedFileType == "application/pdf" || Mimer.isOffice(selectedFileType))
(selectedFileType == "application/pdf" || Mimer.isOffice(selectedFileType))
property var pdfpages: ConvertChecker.pdfPages(selectedFile) property var pdfpages: ConvertChecker.pdfPages(selectedFile)
high: pdfpages == 0 ? 65535 : pdfpages high: pdfpages == 0 ? 65535 : pdfpages

View file

@ -115,6 +115,8 @@ try {
Format format; Format format;
qDebug() << "to pdf" << HwResX << HwResY; qDebug() << "to pdf" << HwResX << HwResY;
emit busyMessage(tr("Printing"));
if(targetFormat == Mimer::URF) if(targetFormat == Mimer::URF)
{ {
@ -149,9 +151,7 @@ try {
write_fun WriteFun([&cid](unsigned char const* buf, unsigned int len) -> bool write_fun WriteFun([&cid](unsigned char const* buf, unsigned int len) -> bool
{ {
qDebug() << "wf called " << len;
cid.write((const char*)buf, len); cid.write((const char*)buf, len);
qDebug() << "wf returns " << len;
return true; return true;
}); });
@ -174,7 +174,7 @@ try {
if(res != 0) if(res != 0)
{ {
throw ConvertFailedException("Conversion failed"); throw ConvertFailedException(tr("Conversion failed"));
} }
qDebug() << "Finished"; qDebug() << "Finished";
@ -277,17 +277,8 @@ try {
painter.drawImage(xOffset, yOffset, inImage); painter.drawImage(xOffset, yOffset, inImage);
painter.end(); painter.end();
if(targetFormat == Mimer::PDF) convertPdf(tmpPdfFile.fileName(), header, targetFormat, Colors, Quality, PaperSize,
{ HwResX, HwResY, false, false, 0, 0, false, false);
QFile tempfileAsFile(tempfile.fileName());
tempfileAsFile.open(QIODevice::Append);
tempfileAsFile.write(tmpPdfFile.readAll());
tempfileAsFile.close();
}
else if(targetFormat == Mimer::Postscript)
{
pdftoPs(PaperSize, false, 0, 0, tmpPdfFile.fileName(), &tempfile);
}
} }
else else
@ -342,9 +333,9 @@ try {
qDebug() << "Finished"; qDebug() << "Finished";
} }
justUpload(tempfile.fileName(), header);
} }
justUpload(tempfile.fileName(), header);
qDebug() << "posted"; qDebug() << "posted";
} }
@ -433,45 +424,11 @@ try {
qDebug() << "PageRangeLow" << PageRangeLow << "PageRangeHigh" << PageRangeHigh << "pages" << pages; qDebug() << "PageRangeLow" << PageRangeLow << "PageRangeHigh" << PageRangeHigh << "pages" << pages;
QTemporaryFile tempfile; convertPdf(tmpPdfFile.fileName(), header, targetFormat, Colors, Quality, PaperSize, HwResX, HwResY, TwoSided, Tumble,
tempfile.open(); PageRangeLow, PageRangeHigh, BackHFlip, BackVFlip);
tempfile.close();
if(targetFormat == Mimer::PDF)
{
if(PageRangeLow != 1 || PageRangeHigh != pages)
{
qDebug() << "adjusting pages in PDF" << PageRangeLow << PageRangeHigh;
adjustPageRange(PaperSize, PageRangeLow, PageRangeHigh, tmpPdfFile.fileName(), &tempfile);
}
else
{
QFile tempfileAsFile(tempfile.fileName());
tempfileAsFile.open(QIODevice::Append);
tempfileAsFile.write(tmpPdfFile.readAll());
tempfileAsFile.close();
}
}
else if(targetFormat == Mimer::Postscript)
{
pdftoPs(PaperSize, TwoSided, PageRangeLow, PageRangeHigh, tmpPdfFile.fileName(), &tempfile);
}
else
{
pdfToRaster(targetFormat, Colors, Quality, PaperSize,
HwResX, HwResY, TwoSided, Tumble,
PageRangeLow, PageRangeHigh, pages, BackHFlip, BackVFlip,
tmpPdfFile.fileName(), &tempfile, false);
}
qDebug() << "Finished"; qDebug() << "Finished";
justUpload(tempfile.fileName(), header);
qDebug() << "posted"; qDebug() << "posted";
} }
@ -622,32 +579,10 @@ try {
painter.end(); painter.end();
QTemporaryFile tempfile; convertPdf(tmpPdfFile.fileName(), header, targetFormat, Colors, Quality, PaperSize, HwResX, HwResY,
tempfile.open(); TwoSided, Tumble, 0, 0, BackHFlip, BackVFlip);
tempfile.close();
if(targetFormat == Mimer::PDF)
{
QFile tempfileAsFile(tempfile.fileName());
tempfileAsFile.open(QIODevice::Append);
tempfileAsFile.write(tmpPdfFile.readAll());
tempfileAsFile.close();
}
else if(targetFormat == Mimer::Postscript)
{
pdftoPs(PaperSize, TwoSided, 0, 0, tmpPdfFile.fileName(), &tempfile);
}
else
{
pdfToRaster(targetFormat, Colors, Quality, PaperSize,
HwResX, HwResY, TwoSided, Tumble,
0, 0, pageCount, BackHFlip, BackVFlip,
tmpPdfFile.fileName(), &tempfile, false);
}
qDebug() << "Finished"; qDebug() << "Finished";
justUpload(tempfile.fileName(), header);
qDebug() << "posted"; qDebug() << "posted";
} }
@ -683,209 +618,3 @@ QString ConvertWorker::getPopplerShortPaperSize(QString PaperSize)
} }
return ShortPaperSize; return ShortPaperSize;
} }
void ConvertWorker::adjustPageRange(QString PaperSize, quint32 PageRangeLow, quint32 PageRangeHigh,
QString pdfFileName, QTemporaryFile* tempfile)
{
QProcess pdftocairo(this);
pdftocairo.setProgram("pdftocairo");
QStringList PdfToCairoArgs = {"-pdf"};
QString ShortPaperSize = getPopplerShortPaperSize(PaperSize);
PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
PdfToCairoArgs << QStringList {"-paper", ShortPaperSize, pdfFileName, "-"};
qDebug() << "pdftocairo args is " << PdfToCairoArgs;
pdftocairo.setArguments(PdfToCairoArgs);
pdftocairo.setStandardOutputFile(tempfile->fileName(), QIODevice::WriteOnly);
pdftocairo.start();
qDebug() << "Starting";
if(!pdftocairo.waitForStarted())
{
qDebug() << "pdftocairo died";
throw ConvertFailedException();
}
qDebug() << "Started";
if(!pdftocairo.waitForFinished(-1))
{
qDebug() << "pdftocairo failed";
throw ConvertFailedException();
}
}
void ConvertWorker::pdftoPs(QString PaperSize, bool TwoSided, quint32 PageRangeLow, quint32 PageRangeHigh,
QString pdfFileName, QTemporaryFile* tempfile)
{
QProcess pdftops(this);
pdftops.setProgram("pdftops");
QStringList PdfToPsArgs;
if(TwoSided)
{
PdfToPsArgs.append("-duplex");
}
QString ShortPaperSize = getPopplerShortPaperSize(PaperSize);
if(PageRangeLow != 0 && PageRangeHigh != 0)
{
PdfToPsArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
}
PdfToPsArgs << QStringList {"-paper", ShortPaperSize, pdfFileName, "-"};
qDebug() << "pdftops args is " << PdfToPsArgs;
pdftops.setArguments(PdfToPsArgs);
pdftops.setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
pdftops.start();
qDebug() << "Starting";
if(!pdftops.waitForStarted())
{
qDebug() << "pdftops died";
throw ConvertFailedException();
}
qDebug() << "Started";
if(!pdftops.waitForFinished(-1))
{
qDebug() << "pdftops failed";
throw ConvertFailedException();
}
}
void ConvertWorker::pdfToRaster(QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
quint32 PageRangeLow, quint32 PageRangeHigh, quint32 pages,
bool BackHFlip, bool BackVFlip,
QString pdfFileName, QTemporaryFile* tempfile, bool resize)
{
if(PageRangeLow==0)
{
PageRangeLow=1;
}
if(PageRangeHigh==0)
{
PageRangeHigh=pages;
}
// Actual number of pages to print
pages = PageRangeHigh-PageRangeLow+1;
qDebug() << "PageRangeLow" << PageRangeLow << "PageRangeHigh" << PageRangeHigh << "pages" << pages;
QProcess pdftocairo(this);
pdftocairo.setProgram("pdftocairo");
QStringList PdfToCairoArgs;
QProcess pdftoppm(this);
pdftoppm.setProgram("pdftoppm");
QStringList Pdf2PpmArgs = {"-rx", QString::number(HwResX), "-ry", QString::number(HwResY)};
if(resize)
{
QString ShortPaperSize = getPopplerShortPaperSize(PaperSize);
PdfToCairoArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
PageRangeLow = PageRangeHigh = 0;
PdfToCairoArgs << QStringList {"-pdf", "-paper", ShortPaperSize, pdfFileName, "-"};
pdftocairo.setArguments(PdfToCairoArgs);
pdftocairo.setStandardOutputProcess(&pdftoppm);
}
else
{
Pdf2PpmArgs << QStringList {"-f", QString::number(PageRangeLow), "-l", QString::number(PageRangeHigh)};
pdftoppm.setStandardInputFile(pdfFileName);
}
if(Colors == 1)
{
Pdf2PpmArgs.append("-gray");
}
qDebug() << "pdf2ppm args is " << Pdf2PpmArgs;
pdftoppm.setArguments(Pdf2PpmArgs);
QProcess ppm2pwg(this);
// Yo dawg, I heard you like programs...
ppm2pwg.setProgram("harbour-seaprint");
ppm2pwg.setArguments({"ppm2pwg"});
bool urf = targetFormat == Mimer::URF;
QStringList env;
ppm2PwgEnv(env, urf, Quality, PaperSize, HwResX, HwResY, TwoSided, Tumble, true, pages, BackHFlip, BackVFlip);
qDebug() << "ppm2pwg env is " << env;
ppm2pwg.setEnvironment(env);
pdftoppm.setStandardOutputProcess(&ppm2pwg);
ppm2pwg.setStandardOutputFile(tempfile->fileName(), QIODevice::WriteOnly);
qDebug() << "All connected";
if(resize)
{
pdftocairo.start();
}
pdftoppm.start();
ppm2pwg.start();
qDebug() << "Starting";
if(!pdftoppm.waitForStarted())
{
qDebug() << "pdftoppm died";
throw ConvertFailedException();
}
if(!ppm2pwg.waitForStarted())
{
qDebug() << "ppm2pwg died";
throw ConvertFailedException();
}
qDebug() << "All started";
bool ppm2pwgSuccess = false;
for(;;)
{
if(ppm2pwg.waitForFinished(250))
{
ppm2pwgSuccess = true;
break;
}
else
{
QList<QByteArray> ppm2pwgOutput = ppm2pwg.readAllStandardError().split('\n');
for(QList<QByteArray>::iterator it = ppm2pwgOutput.begin(); it != ppm2pwgOutput.end(); it++)
{
if(it->startsWith("Page"))
{
QList<QByteArray> ppm2pwgTokens = it->split(' ');
emit progress(ppm2pwgTokens.last().toInt()-1, pages);
}
}
}
}
if(!ppm2pwgSuccess)
{
qDebug() << "ppm2pwg failed";
throw ConvertFailedException();
}
}

View file

@ -58,6 +58,7 @@ public slots:
signals: signals:
void done(QNetworkRequest request, QTemporaryFile* data); void done(QNetworkRequest request, QTemporaryFile* data);
void progress(qint64 done, qint64 pages); void progress(qint64 done, qint64 pages);
void busyMessage(QString msg);
void failed(QString message); void failed(QString message);
private: private:
@ -65,18 +66,6 @@ private:
IppPrinter* _printer; IppPrinter* _printer;
QString getPopplerShortPaperSize(QString PaperSize); QString getPopplerShortPaperSize(QString PaperSize);
void adjustPageRange(QString PaperSize, quint32 PageRangeLow, quint32 PageRangeHigh,
QString pdfFileName, QTemporaryFile* tempfile);
void pdftoPs(QString PaperSize, bool TwoSided, quint32 PageRangeLow, quint32 PageRangeHigh,
QString pdfFileName, QTemporaryFile* tempfile);
void pdfToRaster(QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
quint32 PageRangeLow, quint32 PageRangeHigh, quint32 pages,
bool BackHFlip, bool BackVFlip,
QString pdfFileName, QTemporaryFile* tempfile, bool resize);
}; };
#endif // CONVERTWORKER_H #endif // CONVERTWORKER_H

View file

@ -27,6 +27,7 @@ IppPrinter::IppPrinter()
connect(this, &IppPrinter::doConvertOfficeDocument, _worker, &ConvertWorker::convertOfficeDocument); connect(this, &IppPrinter::doConvertOfficeDocument, _worker, &ConvertWorker::convertOfficeDocument);
connect(this, &IppPrinter::doConvertPlaintext, _worker, &ConvertWorker::convertPlaintext); connect(this, &IppPrinter::doConvertPlaintext, _worker, &ConvertWorker::convertPlaintext);
connect(_worker, &ConvertWorker::progress, this, &IppPrinter::setProgress); connect(_worker, &ConvertWorker::progress, this, &IppPrinter::setProgress);
connect(_worker, &ConvertWorker::busyMessage, this, &IppPrinter::setBusyMessage);
connect(_worker, &ConvertWorker::failed, this, &IppPrinter::convertFailed); connect(_worker, &ConvertWorker::failed, this, &IppPrinter::convertFailed);
qRegisterMetaType<QMargins>(); qRegisterMetaType<QMargins>();
@ -536,7 +537,6 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename)
QString PrintColorMode = getAttrOrDefault(jobAttrs, "print-color-mode").toString(); QString PrintColorMode = getAttrOrDefault(jobAttrs, "print-color-mode").toString();
quint32 Colors = PrintColorMode.contains("color") ? 3 : PrintColorMode.contains("monochrome") ? 1 : 0; quint32 Colors = PrintColorMode.contains("color") ? 3 : PrintColorMode.contains("monochrome") ? 1 : 0;
bool pdfPageRangeAdjustNeeded = false;
quint32 PageRangeLow = 0; quint32 PageRangeLow = 0;
quint32 PageRangeHigh = 0; quint32 PageRangeHigh = 0;
@ -545,21 +545,9 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename)
QJsonObject PageRanges = getAttrOrDefault(jobAttrs, "page-ranges").toObject(); QJsonObject PageRanges = getAttrOrDefault(jobAttrs, "page-ranges").toObject();
PageRangeLow = PageRanges["low"].toInt(); PageRangeLow = PageRanges["low"].toInt();
PageRangeHigh = PageRanges["high"].toInt(); PageRangeHigh = PageRanges["high"].toInt();
} // Always effected locally
QString Sides = getAttrOrDefault(jobAttrs, "sides").toString();
if(documentFormat == Mimer::PWG || documentFormat == Mimer::URF || documentFormat == Mimer::Postscript || Mimer::isOffice(mimeType))
{ // Effected locally
jobAttrs.remove("page-ranges"); jobAttrs.remove("page-ranges");
} }
else if (documentFormat == Mimer::PDF)
{ // Only effected locally if really needed
if(jobAttrs.contains("page-ranges") && !_attrs.contains("page-ranges-supported"))
{
pdfPageRangeAdjustNeeded = true;
jobAttrs.remove("page-ranges");
}
}
qDebug() << "Final op attributes:" << o; qDebug() << "Final op attributes:" << o;
qDebug() << "Final job attributes:" << jobAttrs; qDebug() << "Final job attributes:" << jobAttrs;
@ -567,16 +555,10 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename)
IppMsg job = mk_msg(o, jobAttrs); IppMsg job = mk_msg(o, jobAttrs);
QByteArray contents = job.encode(IppMsg::PrintJob); QByteArray contents = job.encode(IppMsg::PrintJob);
// Non-jpeg images, Postscript and PDF (when not adjusting pages locally) // Shouldn't and can't process these formats respectively
// Always convert non-jpeg images to get resizing if(documentFormat == Mimer::JPEG || documentFormat == Mimer::Postscript)
// TODO: make this sane
if((mimeType == documentFormat)
&& (documentFormat == Mimer::JPEG || !Mimer::isImage(mimeType))
&& !((documentFormat == Mimer::PDF) && pdfPageRangeAdjustNeeded))
{ {
emit doJustUpload(filename, contents); emit doJustUpload(filename, contents);
} }
else else
{ {
@ -592,6 +574,8 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename)
bool TwoSided = false; bool TwoSided = false;
bool Tumble = false; bool Tumble = false;
QString Sides = getAttrOrDefault(jobAttrs, "sides").toString();
if(Sides=="two-sided-long-edge") if(Sides=="two-sided-long-edge")
{ {
TwoSided = true; TwoSided = true;

View file

@ -191,6 +191,14 @@
<source>Failed to open file</source> <source>Failed to open file</source>
<translation>Öffnen der Datei fehlgeschlagen</translation> <translation>Öffnen der Datei fehlgeschlagen</translation>
</message> </message>
<message>
<source>Printing</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Conversion failed</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>CoverPage</name> <name>CoverPage</name>

View file

@ -191,6 +191,14 @@
<source>Failed to open file</source> <source>Failed to open file</source>
<translation>Error al abrir archivo</translation> <translation>Error al abrir archivo</translation>
</message> </message>
<message>
<source>Printing</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Conversion failed</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>CoverPage</name> <name>CoverPage</name>

View file

@ -191,6 +191,14 @@
<source>Failed to open file</source> <source>Failed to open file</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Printing</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Conversion failed</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>CoverPage</name> <name>CoverPage</name>

View file

@ -191,6 +191,14 @@
<source>Failed to open file</source> <source>Failed to open file</source>
<translation>Bestand openen mislukt</translation> <translation>Bestand openen mislukt</translation>
</message> </message>
<message>
<source>Printing</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Conversion failed</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>CoverPage</name> <name>CoverPage</name>

View file

@ -191,6 +191,14 @@
<source>Failed to open file</source> <source>Failed to open file</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Printing</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Conversion failed</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>CoverPage</name> <name>CoverPage</name>

View file

@ -191,6 +191,14 @@
<source>Failed to open file</source> <source>Failed to open file</source>
<translation></translation> <translation></translation>
</message> </message>
<message>
<source>Printing</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Conversion failed</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>CoverPage</name> <name>CoverPage</name>

View file

@ -191,6 +191,14 @@
<source>Failed to open file</source> <source>Failed to open file</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>
<message>
<source>Printing</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Conversion failed</source>
<translation type="unfinished"></translation>
</message>
</context> </context>
<context> <context>
<name>CoverPage</name> <name>CoverPage</name>