Clean up mimetype handling

This commit is contained in:
Anton Thomasson 2021-03-04 21:49:27 +01:00
parent d6ef26b64e
commit b569aede6b
8 changed files with 98 additions and 58 deletions

View file

@ -1,6 +1,7 @@
import QtQuick 2.0
import Sailfish.Silica 1.0
import seaprint.convertchecker 1.0
import seaprint.mimer 1.0
import "../pages/utils.js" as Utils
Setting {

View file

@ -1,5 +1,6 @@
import QtQuick 2.0
import Sailfish.Silica 1.0
import seaprint.mimer 1.0
import "../pages/utils.js" as Utils
Dialog {

View file

@ -1,8 +1,6 @@
import QtQuick 2.0
import Sailfish.Silica 1.0
import seaprint.convertchecker 1.0
import seaprint.ippmsg 1.0
import "../pages/utils.js" as Utils
Setting {
property var printer

View file

@ -6,30 +6,30 @@ function supported_formats(printer, ConvertChecker, considerAdditionalFormats)
formats=formats+printer.additionalDocumentFormats;
}
var raster = (has(formats, "image/pwg-raster") || has(formats, "image/urf"));
var raster = (has(formats, Mimer.PWG) || has(formats, Mimer.URF));
var mimetypes = [];
var supported = [];
if(has(formats, "application/pdf") ||
(ConvertChecker.pdf && ( has(formats, "application/postscript") || raster )))
if(has(formats, Mimer.PDF) ||
(ConvertChecker.pdf && ( has(formats, Mimer.Postscript) || raster )))
{
mimetypes.push("application/pdf");
mimetypes.push(Mimer.PDF);
supported.push("PDF");
}
if(has(formats, "application/postscript"))
if(has(formats, Mimer.Postscript))
{
mimetypes.push("application/postscript");
mimetypes.push(Mimer.Postscript);
supported.push("Postscript");
}
if (raster || has(formats, "image/jpeg") || has(formats, "image/png") || has(formats, "image/gif"))
if (raster || has(formats, Mimer.JPEG) || has(formats, Mimer.PNG))
{
mimetypes.push("image/jpeg");
mimetypes.push(Mimer.JPEG);
supported.push("JPEG");
mimetypes.push("image/png");
mimetypes.push(Mimer.PNG);
supported.push("PNG");
mimetypes.push("image/tiff");
mimetypes.push("image/gif");
mimetypes.push(Mimer.TIFF);
mimetypes.push(Mimer.GIF);
}
@ -184,21 +184,21 @@ function ippName(name, value)
}
case "document-format":
switch(value) {
case "application/octet-stream":
case Mimer.OctetStream:
return qsTr("auto-sense");
case "application/pdf":
case Mimer.PDF:
return qsTr("PDF");
case "application/postscript":
case Mimer.Postscript:
return qsTr("Postscript");
case "image/pwg-raster":
case Mimer.PWG:
return qsTr("PWG-raster");
case "image/urf":
case Mimer.URF:
return qsTr("URF-raster");
case "image/png":
case Mimer.PNG:
return qsTr("PNG");
case "image/jpeg":
case Mimer.JPEG:
return qsTr("JPEG");
case "image/gif":
case Mimer.GIF:
return qsTr("GIF");
default:
return value;
@ -250,25 +250,25 @@ function endsWith(ending, string)
function canConvertPdfTo(type)
{
var targets = ["application/octet-stream", "application/pdf", "application/postscript", "image/pwg-raster", "image/urf"];
var targets = [Mimer.OctetStream, Mimer.PDF, Mimer.Postscript, Mimer.PWG, Mimer.URF];
return has(targets, type)
}
function canTransferPdfAs(type)
{
var targets = ["application/octet-stream", "application/pdf"];
var targets = [Mimer.OctetStream, Mimer.PDF];
return has(targets, type)
}
function canTransferPostscriptAs(type)
{
var targets = ["application/octet-stream", "application/postscript"];
var targets = [Mimer.OctetStream, Mimer.Postscript];
return has(targets, type)
}
function canConvertImageTo(type)
{
var targets = ["application/octet-stream", "image/jpeg", "image/png", "image/pwg-raster", "image/urf", "image/gif"];
var targets = [Mimer.OctetStream, Mimer.JPEG, Mimer.PNG, Mimer.PWG, Mimer.URF];
return has(targets, type)
}
@ -282,7 +282,7 @@ function limitChoices(name, choices, mimeType, ConvertChecker)
{
switch(name) {
case "document-format":
if(mimeType == "application/pdf")
if(mimeType == Mimer.PDF)
{
if(ConvertChecker.pdf)
{
@ -294,7 +294,7 @@ function limitChoices(name, choices, mimeType, ConvertChecker)
}
}
else if(mimeType == "application/postscript")
else if(mimeType == Mimer.Postscript)
{
return choices.filter(canTransferPostscriptAs)
}
@ -304,7 +304,7 @@ function limitChoices(name, choices, mimeType, ConvertChecker)
}
else
{
return ["application/octet-stream"];
return [Mimer.OctetStream];
}
case "printer-resolution":
return choices.filter(unitsIsDpi);

View file

@ -2,6 +2,7 @@
#include <sailfishapp.h>
#include "papersizes.h"
#include "convertchecker.h"
#include "mimer.h"
#include <QImage>
#include <QMatrix>
#include <QPainter>
@ -75,19 +76,19 @@ void ConvertWorker::convertPdf(QNetworkRequest request, QString filename, QTempo
bool ps = false;
bool pdf = false;
if(targetFormat == "image/urf")
if(targetFormat == Mimer::URF)
{
urf = true;
}
else if(targetFormat == "image/pwg-raster")
else if(targetFormat == Mimer::PWG)
{
//ok
}
else if(targetFormat == "application/postscript")
else if(targetFormat == Mimer::Postscript)
{
ps = true;
}
else if (targetFormat == "application/pdf")
else if (targetFormat == Mimer::PDF)
{
pdf = true;
}
@ -329,13 +330,13 @@ void ConvertWorker::convertImage(QNetworkRequest request, QString filename, QTem
{
bool urf = false;
QString imageFormat = "";
QStringList supportedImageFormats = {"image/jpeg", "image/png", "image/gif"};
QStringList supportedImageFormats = {Mimer::JPEG, Mimer::PNG};
if(targetFormat == "image/urf")
if(targetFormat == Mimer::URF)
{
urf = true;
}
else if(targetFormat == "image/pwg-raster")
else if(targetFormat == Mimer::PWG)
{
//ok
}

View file

@ -136,14 +136,14 @@ void IppPrinter::UpdateAdditionalDocumentFormats()
QStringList kv = it->split(":");
if(kv.length()==2 && (kv[0]=="CMD" || kv[0]=="COMMAND SET"))
{
if(!supportedMimeTypes.contains("application/pdf") && kv[1].contains("PDF"))
if(!supportedMimeTypes.contains(Mimer::PDF) && kv[1].contains("PDF"))
{
_additionalDocumentFormats.append("application/pdf");
_additionalDocumentFormats.append(Mimer::PDF);
}
if(!supportedMimeTypes.contains("application/postscript") &&
if(!supportedMimeTypes.contains(Mimer::Postscript) &&
kv[1].contains("Postscript", Qt::CaseInsensitive))
{
_additionalDocumentFormats.append("application/postscript");
_additionalDocumentFormats.append(Mimer::Postscript);
}
}
}
@ -301,22 +301,22 @@ QString targetFormatIfAuto(QString documentFormat, QString mimeType, QJsonArray
{
if(forceRaster)
{
return firstMatch(supportedMimeTypes, {"image/pwg-raster", "image/urf"});
return firstMatch(supportedMimeTypes, {Mimer::PWG, Mimer::URF});
}
else if(documentFormat == "application/octet-stream")
else if(documentFormat == Mimer::OctetStream)
{
if(mimeType == "application/pdf")
if(mimeType == Mimer::PDF)
{
return firstMatch(supportedMimeTypes, {"application/pdf", "application/postscript", "image/pwg-raster", "image/urf" });
return firstMatch(supportedMimeTypes, {Mimer::PDF, Mimer::Postscript, Mimer::PWG, Mimer::URF });
}
else if(mimeType == "application/postscript")
else if(mimeType == Mimer::Postscript)
{
return firstMatch(supportedMimeTypes, {"application/postscript"});
return firstMatch(supportedMimeTypes, {Mimer::Postscript});
}
else if (mimeType.contains("image"))
else if(Mimer::isImage(mimeType))
{
QStringList ImageFormatPrioList {"image/png", "image/gif", "image/pwg-raster", "image/urf", "image/jpeg"};
if(mimeType == "image/jpeg")
QStringList ImageFormatPrioList {Mimer::PNG, Mimer::PWG, Mimer::URF, Mimer::JPEG};
if(mimeType == Mimer::JPEG)
{
// Prioritize transferring JPEG as JPEG, as it will not be transcoded
// Normally, it is the last choice (as the others are lossless)
@ -402,7 +402,7 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert,
documentFormat = targetFormatIfAuto(documentFormat, mimeType, supportedMimeTypes, alwaysConvert);
qDebug() << "adjusted target format:" << documentFormat;
if(documentFormat == "" || documentFormat == "application/octet-string")
if(documentFormat == "" || documentFormat == Mimer::OctetStream)
{
emit convertFailed(tr("Unknown document format"));
return;
@ -416,7 +416,7 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert,
quint32 HwResX = PrinterResolutionRef.toObject()["x"].toInt();
quint32 HwResY = PrinterResolutionRef.toObject()["y"].toInt();
if(documentFormat == "image/urf")
if(documentFormat == Mimer::URF)
{ // Ensure symmetric resolution for URF
if(HwResX < HwResY)
{
@ -459,13 +459,11 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert,
}
QString Sides = getAttrOrDefault(attrs, "sides").toString();
if(documentFormat=="image/pwg-raster"
|| documentFormat=="image/urf"
|| documentFormat == "application/postscript")
if(documentFormat == Mimer::PWG || documentFormat == Mimer::URF || documentFormat == Mimer::Postscript)
{ // Effected locally
attrs.remove("page-ranges");
}
else if (documentFormat == "application/pdf")
else if (documentFormat == Mimer::PDF)
{ // Only effected locally if really needed
if(attrs.contains("page-ranges") && !_attrs.contains("page-ranges-supported"))
{
@ -484,8 +482,8 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert,
// Always convert non-jpeg images to get resizing
// TODO: make this sane
if((mimeType == documentFormat)
&& (documentFormat == "image/jpeg" || !mimeType.contains("image"))
&& !((documentFormat == "application/pdf") && pdfPageRangeAdjustNeeded))
&& (documentFormat == Mimer::JPEG || !Mimer::isImage(mimeType))
&& !((documentFormat == Mimer::PDF) && pdfPageRangeAdjustNeeded))
{
QByteArray filedata = file.readAll();
contents = contents.append(filedata);
@ -507,7 +505,7 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert,
setBusyMessage("Converting");
if(mimeType == "application/pdf")
if(mimeType == Mimer::PDF)
{
bool TwoSided = false;
bool Tumble = false;
@ -524,7 +522,7 @@ void IppPrinter::print(QJsonObject attrs, QString filename, bool alwaysConvert,
emit doConvertPdf(request, filename, tempfile, documentFormat, Colors, Quality,
PaperSize, HwResX, HwResY, TwoSided, Tumble, PageRangeLow, PageRangeHigh);
}
else if (mimeType.contains("image"))
else if (Mimer::isImage(mimeType))
{
emit doConvertImage(request, filename, tempfile, documentFormat, Colors, Quality,
PaperSize, HwResX, HwResY);

View file

@ -1,5 +1,18 @@
#include "mimer.h"
const QString Mimer::OctetStream = "application/octet-stream";
const QString Mimer::PDF = "application/pdf";
const QString Mimer::Postscript = "application/postscript";
const QString Mimer::PWG = "image/pwg-raster";
const QString Mimer::URF = "image/urf";
const QString Mimer::PNG = "image/png";
const QString Mimer::GIF = "image/gif";
const QString Mimer::JPEG = "image/jpeg";
const QString Mimer::TIFF = "image/tiff";
Mimer::Mimer()
{

View file

@ -12,6 +12,34 @@ class Mimer : public QObject
public:
static Mimer* instance();
Q_INVOKABLE QString get_type(QString filename);
Q_PROPERTY(const QString OctetStream MEMBER OctetStream CONSTANT);
Q_PROPERTY(const QString PDF MEMBER PDF CONSTANT);
Q_PROPERTY(const QString Postscript MEMBER Postscript CONSTANT);
Q_PROPERTY(const QString PWG MEMBER PWG CONSTANT);
Q_PROPERTY(const QString URF MEMBER URF CONSTANT);
Q_PROPERTY(const QString PNG MEMBER PNG CONSTANT);
Q_PROPERTY(const QString GIF MEMBER GIF CONSTANT);
Q_PROPERTY(const QString JPEG MEMBER JPEG CONSTANT);
Q_PROPERTY(const QString TIFF MEMBER TIFF CONSTANT);
static const QString OctetStream;
static const QString PDF;
static const QString Postscript;
static const QString PWG;
static const QString URF;
static const QString PNG;
static const QString GIF;
static const QString JPEG;
static const QString TIFF;
Q_INVOKABLE static bool isImage(QString mimeType) {return mimeType.startsWith("image/");}
private:
Mimer();
static Mimer* m_Instance;