harbour-seaprint/qml/pages/utils.js

546 lines
14 KiB
JavaScript
Raw Permalink Normal View History

2021-07-11 15:18:50 +03:00
.pragma library
.import seaprint.mimer 1.0 as Mimer
.import seaprint.convertchecker 1.0 as ConvertChecker
.import "strings.js" as Strings
2021-07-11 15:18:50 +03:00
// TODO move to IppPrinter?
function supported_formats(printer, considerAdditionalFormats)
2019-12-01 22:27:00 +03:00
{
var formats = printer.attrs["document-format-supported"].value;
if(considerAdditionalFormats)
{
formats=formats+printer.additionalDocumentFormats;
}
2021-07-11 15:18:50 +03:00
var raster = (has(formats, Mimer.Mimer.PWG) || has(formats, Mimer.Mimer.URF));
2020-08-02 12:35:18 +03:00
var mimetypes = [];
var pdf = false;
var postscript = false;
var office = false;
var images = false;
2021-06-12 13:45:58 +03:00
var plaintext = false;
2021-07-11 15:18:50 +03:00
if(has(formats, Mimer.Mimer.PDF) ||
2021-11-20 19:31:03 +03:00
(has(formats, Mimer.Mimer.Postscript) || raster ))
{
pdf = true;
2021-07-11 15:18:50 +03:00
mimetypes.push(Mimer.Mimer.PDF);
}
2021-07-11 15:18:50 +03:00
if(has(formats, Mimer.Mimer.Postscript))
{
postscript = true;
2021-07-11 15:18:50 +03:00
mimetypes.push(Mimer.Mimer.Postscript);
}
2021-03-06 14:59:47 +03:00
if(pdf || has(formats, Mimer.Mimer.Plaintext))
{
plaintext = true;
mimetypes.push(Mimer.Mimer.Plaintext);
}
if((ConvertChecker.ConvertChecker.calligra) && pdf)
{
office = true;
2021-07-11 15:18:50 +03:00
mimetypes = mimetypes.concat(Mimer.Mimer.OfficeFormats);
}
2022-05-29 16:54:11 +03:00
if(pdf || has(formats, Mimer.Mimer.JPEG) || has(formats, Mimer.Mimer.PNG) || has(formats, Mimer.Mimer.RBMP))
{
images = true;
2021-07-11 15:18:50 +03:00
mimetypes.push(Mimer.Mimer.JPEG);
mimetypes.push(Mimer.Mimer.PNG);
mimetypes.push(Mimer.Mimer.TIFF);
mimetypes.push(Mimer.Mimer.GIF);
}
2022-05-29 16:54:11 +03:00
if(pdf)
{
mimetypes.push(Mimer.Mimer.SVG);
}
2021-06-12 13:45:58 +03:00
return {pdf: pdf, postscript: postscript, plaintext: plaintext, office: office, images: images, mimetypes: mimetypes};
2019-12-01 22:27:00 +03:00
}
function has(arrayish, what)
{
2020-05-09 15:24:51 +03:00
return arrayish.indexOf(what) != -1;
2019-12-01 22:27:00 +03:00
}
2020-01-03 15:37:51 +03:00
function ippName(name, value, printerStrings)
2020-01-03 15:37:51 +03:00
{
2021-03-21 00:45:58 +03:00
if(value==undefined)
{
return ""
}
2020-01-03 15:37:51 +03:00
switch(name) {
case "job-state":
switch(value) {
case 3:
return qsTr("pending");
case 4:
return qsTr("pending-held");
case 5:
return qsTr("processing");
case 6:
return qsTr("processing-stopped");
case 7:
return qsTr("canceled");
case 8:
return qsTr("aborted");
case 9:
return qsTr("completed");
default:
return qsTr("unknown state ")+value
}
2020-09-29 22:47:37 +03:00
case "printer-state":
switch(value) {
case 3:
return qsTr("Idle");
case 4:
return qsTr("Processing");
case 5:
return qsTr("Stopped");
default:
return qsTr("Unknown State ")+value
}
2020-01-03 15:37:51 +03:00
case "print-quality":
switch(value) {
case 3:
return qsTr("draft");
case 4:
return qsTr("normal");
case 5:
return qsTr("high");
default:
return qsTr("unknown quality ")+value
}
case "orientation-requested":
switch(value) {
case 3:
return qsTr("portrait");
case 4:
return qsTr("landscape");
case 5:
return qsTr("reverse landscape");
case 6:
return qsTr("reverse portrait");
default:
return qsTr("unknown orientation ")+value
}
case "printer-resolution":
var units = "";
if(value.units==3) {
units=qsTr("dpi");
} else if (units==4){
units=qsTr("dots/cm")
}
return ""+value.x+"x"+value.y+units;
case "media":
if(Strings.media.hasOwnProperty(value))
{
return Strings.media[value];
}
else
{
return value;
}
case "media-type":
if(Strings.mediaType.hasOwnProperty(value))
{
return Strings.mediaType[value];
}
else if(printerStrings != undefined && printerStrings.hasOwnProperty("media-type."+value))
2022-01-15 00:14:06 +03:00
{
return printerStrings["media-type."+value];
2022-01-15 00:14:06 +03:00
}
else
{
return value;
}
2020-09-29 22:47:37 +03:00
case "printer-state-reasons":
if(Strings.printerStateReasons.hasOwnProperty(value))
2020-09-29 22:47:37 +03:00
{
return Strings.printerStateReasons[value];
2020-09-29 22:47:37 +03:00
}
2020-10-04 15:36:01 +03:00
else if (endsWith("-report", value))
{
return ippName(name, value.substr(0, value.length-"-report".length))+" (" + qsTr("Report") + ")"
2020-10-04 15:36:01 +03:00
}
else if (endsWith("-warning", value))
{
return ippName(name, value.substr(0, value.length-"-warning".length))+" (" + qsTr("Warning") + ")"
2020-10-04 15:36:01 +03:00
}
else if (endsWith("-error", value))
{
return ippName(name, value.substr(0, value.length-"-error".length))+" (" + qsTr("Error") + ")"
2020-10-04 15:36:01 +03:00
}
2020-09-29 22:47:37 +03:00
else
{
return value;
}
2020-06-07 18:58:47 +03:00
case "sides":
switch(value) {
case "one-sided":
return qsTr("one-sided");
case "two-sided-long-edge":
return qsTr("two-sided");
case "two-sided-short-edge":
return qsTr("two-sided flipped");
default:
return value
}
2020-10-04 19:16:21 +03:00
case "multiple-document-handling":
switch(value) {
case "single-document":
return qsTr("single document");
case "separate-documents-uncollated-copies":
return qsTr("uncollated");
case "separate-documents-collated-copies":
return qsTr("collated");
case "single-document-new-sheet":
return qsTr("single document (new sheet)");
default:
return value
}
2020-06-07 18:58:47 +03:00
case "print-color-mode":
switch(value) {
case "auto":
return qsTr("automatic");
case "auto-monochrome":
return qsTr("auto monochrome");
case "bi-level":
return qsTr("bi-level");
case "color":
return qsTr("color");
case "highlight":
return qsTr("highlight");
case "monochrome":
return qsTr("monochrome");
case "process-bi-level":
return qsTr("process bi-level");
case "process-monochrome":
return qsTr("process monochrome");
default:
return value
}
case "print-scaling":
switch(value) {
case "auto":
return qsTr("auto");
case "auto-fit":
return qsTr("auto (fit)");
case "fill":
return qsTr("fill");
case "fit":
return qsTr("fit");
case "none":
return qsTr("none");
default:
return value;
}
2020-06-04 22:31:46 +03:00
case "document-format":
switch(value) {
2021-07-11 15:18:50 +03:00
case Mimer.Mimer.OctetStream:
return qsTr("auto-sense");
2021-07-11 15:18:50 +03:00
case Mimer.Mimer.PDF:
2020-06-04 22:31:46 +03:00
return qsTr("PDF");
2021-07-11 15:18:50 +03:00
case Mimer.Mimer.Postscript:
2020-06-04 22:31:46 +03:00
return qsTr("Postscript");
case Mimer.Mimer.Plaintext:
return qsTr("Plaintext");
2021-07-11 15:18:50 +03:00
case Mimer.Mimer.PWG:
2020-06-04 22:31:46 +03:00
return qsTr("PWG-raster");
2021-07-11 15:18:50 +03:00
case Mimer.Mimer.URF:
2020-06-04 22:31:46 +03:00
return qsTr("URF-raster");
2021-07-11 15:18:50 +03:00
case Mimer.Mimer.PNG:
2020-06-04 22:31:46 +03:00
return qsTr("PNG");
2021-07-11 15:18:50 +03:00
case Mimer.Mimer.JPEG:
2020-06-04 22:31:46 +03:00
return qsTr("JPEG");
2021-07-11 15:18:50 +03:00
case Mimer.Mimer.GIF:
2020-06-04 22:31:46 +03:00
return qsTr("GIF");
2022-05-29 16:54:11 +03:00
case Mimer.Mimer.SVG:
return qsTr("SVG");
2022-05-26 12:57:16 +03:00
case Mimer.Mimer.RBMP:
return qsTr("Reverse BMP");
2020-06-04 22:31:46 +03:00
default:
return value;
}
2021-07-11 13:46:27 +03:00
case "media-top-margin":
case "media-bottom-margin":
case "media-left-margin":
case "media-right-margin":
return ""+(value/100)+"mm"
case "media-source":
if(value.indexOf("tray-")==0)
{
return qsTr("tray")+" "+value.split("-")[1];
}
else if(value.indexOf("roll-")==0)
{
return qsTr("roll")+" "+value.split("-")[1];
}
2020-06-07 18:58:47 +03:00
else
{
2020-06-07 18:58:47 +03:00
switch(value) {
case "alternate":
return qsTr("alternate");
case "alternate-roll":
return qsTr("alternate-roll");
2020-06-07 18:58:47 +03:00
case "auto":
return qsTr("automatic");
case "bottom":
return qsTr("bottom");
case "by-pass-tray":
return qsTr("by-pass tray");
2020-06-07 18:58:47 +03:00
case "center":
return qsTr("center");
case "disc":
return qsTr("disc");
case "envelope":
return qsTr("envelope");
case "hagaki":
return qsTr("hagaki");
case "large-capacity":
return qsTr("large capacity");
case "left":
return qsTr("left");
2020-06-07 18:58:47 +03:00
case "main":
return qsTr("main");
case "main-roll":
return qsTr("main roll");
2020-06-07 18:58:47 +03:00
case "manual":
return qsTr("manual");
case "middle":
return qsTr("middle");
case "photo":
return qsTr("photo");
case "rear":
return qsTr("rear");
case "right":
return qsTr("right");
case "side":
return qsTr("side");
case "top":
return qsTr("top");
default:
return value;
}
}
case "output-bin":
if(value.indexOf("tray-")==0)
{
return qsTr("tray")+" "+value.split("-")[1];
}
else if(value.indexOf("stacker-")==0)
{
return qsTr("stacker")+" "+value.split("-")[1];
}
else if(value.indexOf("mailbox-")==0)
{
return qsTr("mailbox")+" "+value.split("-")[1];
}
else
{
switch(value) {
case "auto":
return qsTr("automatic");
case "top":
return qsTr("top");
case "middle":
return qsTr("middle");
case "bottom":
return qsTr("bottom");
case "side":
return qsTr("side");
case "left":
return qsTr("left");
case "right":
return qsTr("right");
case "center":
return qsTr("center");
case "rear":
return qsTr("rear");
case "face-up":
return qsTr("face-up");
case "face-down":
return qsTr("face-down");
case "large-capacity":
return qsTr("large capacity");
case "my-mailbox":
return qsTr("my mailbox");
default:
return value;
}
}
2020-01-03 15:37:51 +03:00
}
return value;
}
function endsWith(ending, string)
{
return string.lastIndexOf(ending) == (string.length - ending.length);
}
var pdfTargets = [Mimer.Mimer.OctetStream, Mimer.Mimer.PDF, Mimer.Mimer.Postscript, Mimer.Mimer.PWG, Mimer.Mimer.URF];
2020-06-04 22:31:46 +03:00
function canConvertPdfTo(type)
{
return has(pdfTargets, type)
2020-06-04 22:31:46 +03:00
}
2021-02-13 23:59:46 +03:00
function canTransferPostscriptAs(type)
{
2021-07-11 15:18:50 +03:00
var targets = [Mimer.Mimer.OctetStream, Mimer.Mimer.Postscript];
2021-02-13 23:59:46 +03:00
return has(targets, type)
}
function canConvertPlaintextTo(type)
2021-03-06 14:59:47 +03:00
{
var targets = pdfTargets;
targets.push(Mimer.Mimer.Plaintext);
2021-03-06 14:59:47 +03:00
return has(targets, type)
}
function canConvertOfficeDocumentTo(type)
{
return has(pdfTargets, type)
}
2022-05-29 16:54:11 +03:00
function canConvertSvgTo(type)
{
return has(pdfTargets, type)
}
2020-06-04 22:31:46 +03:00
function canConvertImageTo(type)
{
2022-05-26 12:57:16 +03:00
var targets = [Mimer.Mimer.OctetStream, Mimer.Mimer.JPEG, Mimer.Mimer.PNG, Mimer.Mimer.RBMP,
Mimer.Mimer.PWG, Mimer.Mimer.URF, Mimer.Mimer.PDF, Mimer.Mimer.Postscript];
2020-06-04 22:31:46 +03:00
return has(targets, type)
}
function unitsIsDpi(resolution)
{
return resolution.units == 3;
}
2021-12-03 21:33:28 +03:00
function knownPaperSize(mediaName)
{
return Strings.media.hasOwnProperty(mediaName);
}
2021-07-31 13:18:41 +03:00
function fixupChoices(name, choices, mimeType)
2020-06-04 22:31:46 +03:00
{
switch(name) {
case "document-format":
2021-07-11 15:18:50 +03:00
if(mimeType == Mimer.Mimer.PDF)
2020-06-04 22:31:46 +03:00
{
2021-11-20 19:31:03 +03:00
return choices.filter(canConvertPdfTo)
2020-06-04 22:31:46 +03:00
}
2021-07-11 15:18:50 +03:00
else if(mimeType == Mimer.Mimer.Plaintext)
2021-06-12 13:45:58 +03:00
{
return choices.filter(canConvertPlaintextTo)
2021-06-12 13:45:58 +03:00
}
2021-07-11 15:18:50 +03:00
else if(mimeType == Mimer.Mimer.Postscript)
2021-02-13 23:59:46 +03:00
{
return choices.filter(canTransferPostscriptAs)
}
2021-07-11 15:18:50 +03:00
else if(Mimer.Mimer.isOffice(mimeType))
2021-03-06 14:59:47 +03:00
{
return choices.filter(canConvertOfficeDocumentTo)
}
2022-05-29 16:54:11 +03:00
else if(mimeType == Mimer.Mimer.SVG)
{
return choices.filter(canConvertSvgTo)
}
2021-07-11 15:18:50 +03:00
else if(Mimer.Mimer.isImage(mimeType))
2020-06-04 22:31:46 +03:00
{
return choices.filter(canConvertImageTo);
}
else
{
2021-07-11 15:18:50 +03:00
return [Mimer.Mimer.OctetStream];
2020-06-04 22:31:46 +03:00
}
case "printer-resolution":
return choices.filter(unitsIsDpi);
2020-10-04 19:16:21 +03:00
case "multiple-document-handling" :
// Only collation settings valid, multiple documents not used
return choices.filter(function(elem) {return elem.indexOf("collated") != -1});
case "number-up":
if(choices.constructor.name === "Object")
{
var choice_array = [];
for(var i=choices.low; i <= choices.high; i++)
{
choice_array.push(i);
}
return choice_array;
}
else
{
return choices;
}
case "media":
2021-12-03 21:33:28 +03:00
return choices.filter(knownPaperSize);
2020-06-04 22:31:46 +03:00
default:
return choices;
}
}
function isWaringState(printer)
{
if(printer.attrs["printer-state"].value > 4)
{
return true;
}
2020-10-11 17:13:14 +03:00
if(printer.attrs.hasOwnProperty("printer-state-reasons"))
{
for(var i in printer.attrs["printer-state-reasons"].value)
2020-10-11 17:13:14 +03:00
{
var value = printer.attrs["printer-state-reasons"].value[i];
if(value != "none" && !(endsWith("-report", value)))
{
return true;
}
2020-10-11 17:13:14 +03:00
}
}
return false;
}
2021-02-13 19:30:19 +03:00
function unknownForEmptyString(s)
{
if(s === "")
{
return qsTr("Unknown");
}
else
{
return s;
}
}
2021-06-21 21:20:09 +03:00
function existsAndNotEmpty(attrName, printer)
{
if(printer.attrs.hasOwnProperty(attrName))
{
return printer.attrs[attrName].value != ""
}
else
{
return false
}
}
function basename(filewithpath)
{
return filewithpath.substring(filewithpath.lastIndexOf("/")+1);
}