harbour-seaprint/src/ippprinter.cpp

844 lines
26 KiB
C++
Raw Normal View History

2019-12-01 22:27:00 +03:00
#include "ippprinter.h"
2020-01-20 22:11:41 +03:00
#include <seaprint_version.h>
2020-05-03 15:13:44 +03:00
#include "mimer.h"
2020-05-16 16:17:42 +03:00
#include "papersizes.h"
#include "overrider.h"
#include "settings.h"
2019-12-01 22:27:00 +03:00
2021-07-14 16:07:55 +03:00
Q_DECLARE_METATYPE(QMargins)
2019-12-01 22:27:00 +03:00
IppPrinter::IppPrinter()
{
_nam = new QNetworkAccessManager(this);
2019-12-08 15:55:56 +03:00
_print_nam = new QNetworkAccessManager(this);
_jobs_nam = new QNetworkAccessManager(this);
2019-12-12 22:53:46 +03:00
_job_cancel_nam = new QNetworkAccessManager(this);
2020-06-06 12:27:08 +03:00
connect(_nam, &QNetworkAccessManager::finished, this, &IppPrinter::getPrinterAttributesFinished);
connect(_nam, &QNetworkAccessManager::sslErrors, this, &IppPrinter::onSslErrors);
2020-01-04 13:55:22 +03:00
2020-06-06 12:27:08 +03:00
connect(_print_nam, &QNetworkAccessManager::finished, this, &IppPrinter::printRequestFinished);
connect(_print_nam, &QNetworkAccessManager::sslErrors, this, &IppPrinter::onSslErrors);
2020-01-04 13:55:22 +03:00
2020-06-06 12:27:08 +03:00
connect(_jobs_nam, &QNetworkAccessManager::finished,this, &IppPrinter::getJobsRequestFinished);
connect(_jobs_nam, &QNetworkAccessManager::sslErrors, this, &IppPrinter::onSslErrors);
2020-01-04 13:55:22 +03:00
2020-06-06 12:27:08 +03:00
connect(_job_cancel_nam, &QNetworkAccessManager::finished,this, &IppPrinter::cancelJobFinished);
connect(_job_cancel_nam, &QNetworkAccessManager::sslErrors, this, &IppPrinter::onSslErrors);
2020-01-04 13:55:22 +03:00
2019-12-01 22:27:00 +03:00
QObject::connect(this, &IppPrinter::urlChanged, this, &IppPrinter::onUrlChanged);
2020-05-01 14:49:17 +03:00
qRegisterMetaType<QTemporaryFile*>("QTemporaryFile*");
_worker = new ConvertWorker;
_worker->moveToThread(&_workerThread);
connect(&_workerThread, &QThread::finished, _worker, &QObject::deleteLater);
connect(this, &IppPrinter::doConvertPdf, _worker, &ConvertWorker::convertPdf);
2020-05-13 20:53:44 +03:00
connect(this, &IppPrinter::doConvertImage, _worker, &ConvertWorker::convertImage);
2021-03-06 14:59:47 +03:00
connect(this, &IppPrinter::doConvertOfficeDocument, _worker, &ConvertWorker::convertOfficeDocument);
2021-06-12 13:45:58 +03:00
connect(this, &IppPrinter::doConvertPlaintext, _worker, &ConvertWorker::convertPlaintext);
connect(_worker, &ConvertWorker::done, this, &IppPrinter::convertDone);
2020-07-30 23:21:14 +03:00
connect(_worker, &ConvertWorker::progress, this, &IppPrinter::setProgress);
connect(_worker, &ConvertWorker::failed, this, &IppPrinter::convertFailed);
2021-07-14 16:07:55 +03:00
qRegisterMetaType<QMargins>();
_workerThread.start();
_tainted = false;
2019-12-01 22:27:00 +03:00
}
IppPrinter::~IppPrinter() {
delete _nam;
2019-12-08 15:55:56 +03:00
delete _print_nam;
delete _jobs_nam;
2019-12-12 22:53:46 +03:00
delete _job_cancel_nam;
}
QJsonObject IppPrinter::opAttrs() {
QString name = qgetenv("USER");
2019-12-12 22:53:46 +03:00
QJsonObject o
{
{"attributes-charset", QJsonObject {{"tag", IppMsg::Charset}, {"value", "utf-8"}}},
{"attributes-natural-language", QJsonObject {{"tag", IppMsg::NaturalLanguage}, {"value", "en-us"}}},
{"printer-uri", QJsonObject {{"tag", IppMsg::Uri}, {"value", _url.toString()}}},
{"requesting-user-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", name}}},
2019-12-12 22:53:46 +03:00
};
return o;
2019-12-01 22:27:00 +03:00
}
void IppPrinter::setUrl(QString url_s)
2019-12-01 22:27:00 +03:00
{
QUrl url = QUrl(url_s);
qDebug() << url.scheme();
2021-06-19 19:16:42 +03:00
// If not already a good scheme, try to fixup, or give an empty url
if(url.scheme() != "ipp" && url.scheme() != "ipps" && url.scheme() != "file")
{
if(url.scheme() == "")
{
url = QUrl("ipp://"+url_s); // Why isn't setScheme working?
}
else if (url.scheme() == "http") {
url.setScheme("ipp");
}
2021-06-19 19:16:42 +03:00
else if (url.scheme() == "https") {
url.setScheme("ipps");
}
else {
url = QUrl();
}
}
qDebug() << url_s << url;
2019-12-01 22:27:00 +03:00
if(url != _url)
{
_url = url;
emit urlChanged();
}
}
void IppPrinter::onUrlChanged()
{
2019-12-17 22:20:09 +03:00
refresh();
}
void IppPrinter::refresh() {
// _attrs = QJsonObject();
// emit attrsChanged();
2019-12-01 22:27:00 +03:00
// _additionalDocumentFormats = QStringList();
// emit additionalDocumentFormatsChanged();
2021-06-19 19:16:42 +03:00
// FFFFUUUU
_nam->clearAccessCache();
_jobs_nam->clearAccessCache();
_job_cancel_nam->clearAccessCache();
_print_nam->clearAccessCache();
2021-02-13 16:59:38 +03:00
if(_url.scheme() == "file")
{
_attrs = QJsonObject();
2021-02-13 16:59:38 +03:00
QFile file(_url.toLocalFile());
if(file.open(QIODevice::ReadOnly))
{
QJsonDocument JsonDocument = QJsonDocument::fromJson(file.readAll());
2021-02-13 16:59:38 +03:00
_attrs = JsonDocument.object();
2021-03-21 14:49:54 +03:00
// These won't load anyway...r
_attrs.remove("printer-icons");
2021-02-13 16:59:38 +03:00
file.close();
Overrider::instance()->apply(_attrs);
2021-02-13 16:59:38 +03:00
}
emit attrsChanged();
UpdateAdditionalDocumentFormats();
}
else
{
QNetworkRequest request = mkReq();
QJsonObject o = opAttrs();
2019-12-01 22:27:00 +03:00
2021-02-13 16:59:38 +03:00
IppMsg msg = IppMsg(o);
_nam->post(request, msg.encode(IppMsg::GetPrinterAttrs));
}
2019-12-01 22:27:00 +03:00
}
2021-02-13 16:59:38 +03:00
void IppPrinter::UpdateAdditionalDocumentFormats()
2019-12-01 22:27:00 +03:00
{
_additionalDocumentFormats = QStringList();
if(_attrs.contains("printer-device-id"))
{
QJsonArray supportedMimeTypes = _attrs["document-format-supported"].toObject()["value"].toArray();
QStringList printerDeviceId = _attrs["printer-device-id"].toObject()["value"].toString().split(";");
for (QStringList::iterator it = printerDeviceId.begin(); it != printerDeviceId.end(); it++)
{
QStringList kv = it->split(":");
2020-06-13 14:05:33 +03:00
if(kv.length()==2 && (kv[0]=="CMD" || kv[0]=="COMMAND SET"))
{
2021-03-04 23:49:27 +03:00
if(!supportedMimeTypes.contains(Mimer::PDF) && kv[1].contains("PDF"))
{
2021-03-04 23:49:27 +03:00
_additionalDocumentFormats.append(Mimer::PDF);
}
2021-03-04 23:49:27 +03:00
if(!supportedMimeTypes.contains(Mimer::Postscript) &&
2020-06-13 14:05:33 +03:00
kv[1].contains("Postscript", Qt::CaseInsensitive))
{
2021-03-04 23:49:27 +03:00
_additionalDocumentFormats.append(Mimer::Postscript);
}
}
}
qDebug() << "additionalDocumentFormats" << _additionalDocumentFormats;
}
2021-02-13 16:59:38 +03:00
emit additionalDocumentFormatsChanged();
}
void IppPrinter::getPrinterAttributesFinished(QNetworkReply *reply)
{
2021-06-19 19:16:42 +03:00
qDebug() << reply->request().url() << reply->error() << reply->errorString() << reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString();
2021-02-13 16:59:38 +03:00
_attrs = QJsonObject();
if(reply->error() == QNetworkReply::NoError)
{
try {
IppMsg resp(reply);
qDebug() << resp.getStatus() << resp.getOpAttrs() << resp.getPrinterAttrs();
_attrs = resp.getPrinterAttrs();
Overrider::instance()->apply(_attrs);
2021-02-13 16:59:38 +03:00
}
catch(const std::exception& e)
{
qDebug() << e.what();
}
}
emit attrsChanged();
2021-02-13 16:59:38 +03:00
UpdateAdditionalDocumentFormats();
2019-12-01 22:27:00 +03:00
}
2019-12-08 15:55:56 +03:00
void IppPrinter::printRequestFinished(QNetworkReply *reply)
2019-12-01 22:27:00 +03:00
{
2019-12-17 22:18:57 +03:00
_jobAttrs = QJsonObject();
bool status = false;
2019-12-01 22:27:00 +03:00
if(reply->error() == QNetworkReply::NoError)
{
try {
IppMsg resp(reply);
2019-12-06 22:18:48 +03:00
qDebug() << resp.getStatus() << resp.getOpAttrs() << resp.getJobAttrs();
2019-12-08 15:55:56 +03:00
_jobAttrs = resp.getJobAttrs()[0].toObject();
if(resp.getOpAttrs().keys().contains("status-message"))
{ // Sometimes there are no response attributes at all,
// maybe status-message from the operation attributes is somewhat useful
_jobAttrs["status-message"] = resp.getOpAttrs()["status-message"];
}
2019-12-17 22:18:57 +03:00
status = resp.getStatus() <= 0xff;
2019-12-01 22:27:00 +03:00
}
2020-08-29 18:32:11 +03:00
catch(const std::exception& e)
2019-12-01 22:27:00 +03:00
{
qDebug() << e.what();
}
}
2019-12-17 22:18:57 +03:00
else {
2020-06-13 14:05:33 +03:00
_jobAttrs.insert("job-state-message", QJsonObject {{"tag", IppMsg::TextWithoutLanguage},
{"value", "Network error"}});
2019-12-17 22:18:57 +03:00
}
emit jobAttrsChanged();
2020-04-04 14:56:36 +03:00
emit jobFinished(status);
2019-12-01 22:27:00 +03:00
}
2019-12-08 15:55:56 +03:00
void IppPrinter::getJobsRequestFinished(QNetworkReply *reply)
{
if(reply->error() == QNetworkReply::NoError)
{
try {
IppMsg resp(reply);
qDebug() << resp.getStatus() << resp.getOpAttrs() << resp.getJobAttrs();
_jobs = resp.getJobAttrs();
emit jobsChanged();
}
2020-08-29 18:32:11 +03:00
catch(const std::exception& e)
2019-12-08 15:55:56 +03:00
{
qDebug() << e.what();
}
}
}
2019-12-01 22:27:00 +03:00
2019-12-12 22:53:46 +03:00
void IppPrinter::cancelJobFinished(QNetworkReply *reply)
{
2020-02-23 15:11:28 +03:00
bool status = false;
2019-12-12 22:53:46 +03:00
if(reply->error() == QNetworkReply::NoError)
{
try {
IppMsg resp(reply);
qDebug() << resp.getStatus() << resp.getOpAttrs() << resp.getJobAttrs();
2020-02-23 15:11:28 +03:00
status = resp.getStatus() <= 0xff;
2019-12-12 22:53:46 +03:00
}
2020-08-29 18:32:11 +03:00
catch(const std::exception& e)
2019-12-12 22:53:46 +03:00
{
qDebug() << e.what();
}
}
2020-02-23 15:11:28 +03:00
emit cancelStatus(status);
2019-12-12 22:53:46 +03:00
getJobs();
}
2021-06-19 19:16:42 +03:00
void IppPrinter::onSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
{
_tainted = true;
emit taintedChanged();
return ignoreSslErrors(reply, errors);
}
void IppPrinter::ignoreSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
2020-01-04 13:55:22 +03:00
{
2021-06-19 19:16:42 +03:00
bool ignore = Settings::instance()->ignoreSslErrors();
qDebug() << reply->request().url() << "SSL handshake failed" << errors << ignore;
if(ignore)
{
reply->ignoreSslErrors(errors);
2020-01-04 13:55:22 +03:00
}
}
void IppPrinter::convertDone(QNetworkRequest request, QTemporaryFile* data)
2020-05-01 14:49:17 +03:00
{
connect(_print_nam, SIGNAL(finished(QNetworkReply*)), data, SLOT(deleteLater()));
2020-05-01 14:49:17 +03:00
data->open();
2020-05-06 21:46:41 +03:00
2021-04-18 10:34:48 +03:00
setBusyMessage(tr("Transferring"));
2020-05-06 21:46:41 +03:00
2020-06-03 21:42:18 +03:00
QNetworkReply* reply = _print_nam->post(request, data);
connect(reply, &QNetworkReply::uploadProgress, this, &IppPrinter::setProgress);
2020-05-01 14:49:17 +03:00
}
void IppPrinter::convertFailed(QString message)
{
_jobAttrs = QJsonObject();
_jobAttrs.insert("job-state-message", QJsonObject {{"tag", IppMsg::TextWithoutLanguage}, {"value", message}});
emit jobAttrsChanged();
emit jobFinished(false);
}
2020-01-04 13:55:22 +03:00
QString firstMatch(QJsonArray supported, QStringList wanted)
{
for(QStringList::iterator it = wanted.begin(); it != wanted.end(); it++)
{
if(supported.contains(*it))
{
return *it;
}
}
return "";
}
2021-03-21 19:10:40 +03:00
QString targetFormatIfAuto(QString documentFormat, QString mimeType, QJsonArray supportedMimeTypes)
{
2021-03-21 19:10:40 +03:00
if(documentFormat == Mimer::OctetStream)
{
2021-06-12 13:45:58 +03:00
if(mimeType == Mimer::PDF || mimeType == Mimer::Plaintext)
{
2021-03-04 23:49:27 +03:00
return firstMatch(supportedMimeTypes, {Mimer::PDF, Mimer::Postscript, Mimer::PWG, Mimer::URF });
}
2021-03-04 23:49:27 +03:00
else if(mimeType == Mimer::Postscript)
2021-02-13 23:59:46 +03:00
{
2021-03-04 23:49:27 +03:00
return firstMatch(supportedMimeTypes, {Mimer::Postscript});
2021-02-13 23:59:46 +03:00
}
else if(Mimer::isOffice(mimeType))
{
return firstMatch(supportedMimeTypes, {Mimer::PDF, Mimer::Postscript, Mimer::PWG, Mimer::URF });
}
2021-03-04 23:49:27 +03:00
else if(Mimer::isImage(mimeType))
{
2021-06-13 14:56:44 +03:00
QStringList ImageFormatPrioList {Mimer::PNG, Mimer::PWG, Mimer::URF, Mimer::PDF, Mimer::Postscript, Mimer::JPEG};
2021-03-04 23:49:27 +03:00
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)
ImageFormatPrioList.prepend(mimeType);
}
return firstMatch(supportedMimeTypes, ImageFormatPrioList);
}
return documentFormat;
}
return documentFormat;
}
void IppPrinter::adjustRasterSettings(QString documentFormat, QJsonObject& jobAttrs, quint32& HwResX, quint32& HwResY,
bool& BackHFlip, bool& BackVFlip)
2020-06-04 22:31:46 +03:00
{
if(documentFormat != Mimer::PWG && documentFormat != Mimer::URF)
{
return;
}
//TODO? jobAttrs.remove("printer-resolution");
if(documentFormat == Mimer::PWG)
{
quint32 diff = std::numeric_limits<quint32>::max();
quint32 AdjustedHwResX = HwResX;
quint32 AdjustedHwResY = HwResY;
foreach(QJsonValue res, _attrs["pwg-raster-document-resolution-supported"].toObject()["value"].toArray())
{
QJsonObject resObj = res.toObject();
if(resObj["units"] != 3)
{
continue;
}
quint32 tmpDiff = std::abs(int(HwResX-resObj["x"].toInt())) + std::abs(int(HwResY-resObj["y"].toInt()));
if(tmpDiff < diff)
{
diff = tmpDiff;
AdjustedHwResX = resObj["x"].toInt();
AdjustedHwResY = resObj["y"].toInt();
}
}
HwResX = AdjustedHwResX;
HwResY = AdjustedHwResY;
}
if(documentFormat == Mimer::URF)
{ // Ensure symmetric resolution for URF
HwResX = HwResY = std::min(HwResX, HwResY);
quint32 diff = std::numeric_limits<quint32>::max();
quint32 AdjustedHwRes = HwResX;
QJsonArray urfSupported = _attrs["urf-supported"].toObject()["value"].toArray();
foreach(QJsonValue us, urfSupported)
{
if(us.toString().startsWith("RS"))
{ //RS300[-600]
QStringList resolutions = us.toString().mid(2).split("-");
foreach(QString res, resolutions)
{
int intRes = res.toInt();
quint32 tmpDiff = std::abs(int(HwResX - intRes));
if(tmpDiff < diff)
{
diff = tmpDiff;
AdjustedHwRes = intRes;
}
}
HwResX = HwResY = AdjustedHwRes;
break;
}
}
}
QString Sides = getAttrOrDefault(jobAttrs, "sides").toString();
if(Sides != "" && Sides != "one-sided")
{
if(documentFormat == Mimer::PWG)
{
QString DocumentSheetBack = _attrs["pwg-raster-document-sheet-back"].toObject()["value"].toString();
if(Sides=="two-sided-long-edge")
{
if(DocumentSheetBack=="flipped")
{
BackVFlip=true;
}
else if(DocumentSheetBack=="rotated")
{
BackHFlip=true;
BackVFlip=true;
}
}
else if(Sides=="two-sided-short-edge")
{
if(DocumentSheetBack=="flipped")
{
BackHFlip=true;
}
else if(DocumentSheetBack=="manual-tumble")
{
BackHFlip=true;
BackVFlip=true;
}
}
}
else if(documentFormat == Mimer::URF)
{
QJsonArray URfSupported = _attrs["urf-supported"].toObject()["value"].toArray();
if(Sides=="two-sided-long-edge")
{
if(URfSupported.contains("DM2"))
{
BackVFlip=true;
}
else if(URfSupported.contains("DM3"))
{
BackHFlip=true;
BackVFlip=true;
}
}
else if(Sides=="two-sided-short-edge")
{
if(URfSupported.contains("DM2"))
{
BackHFlip=true;
}
else if(URfSupported.contains("DM4"))
{
BackHFlip=true;
BackVFlip=true;
}
}
}
}
}
void IppPrinter::print(QJsonObject jobAttrs, QString filename)
{
qDebug() << "printing" << filename << jobAttrs;
2019-12-01 22:27:00 +03:00
2020-06-03 21:42:18 +03:00
_progress = "";
emit progressChanged();
2019-12-01 22:27:00 +03:00
QFile file(filename);
bool file_ok = file.open(QIODevice::ReadOnly);
if(!file_ok)
2020-04-04 14:56:36 +03:00
{
emit convertFailed(tr("Failed to open file"));
2020-04-04 14:56:36 +03:00
return;
}
2019-12-01 22:27:00 +03:00
Mimer* mimer = Mimer::instance();
QString mimeType = mimer->get_type(filename);
QJsonArray supportedMimeTypes = _attrs["document-format-supported"].toObject()["value"].toArray();
for(QStringList::iterator it = _additionalDocumentFormats.begin(); it != _additionalDocumentFormats.end(); it++)
{
supportedMimeTypes.append(*it);
}
qDebug() << supportedMimeTypes << supportedMimeTypes.contains(mimeType);
2019-12-01 22:27:00 +03:00
QFileInfo fileinfo(file);
2019-12-12 22:53:46 +03:00
QJsonObject o = opAttrs();
o.insert("job-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", fileinfo.fileName()}});
QString PaperSize = getAttrOrDefault(jobAttrs, "media").toString();
2021-07-31 13:59:51 +03:00
if(jobAttrs.contains("media-col") && jobAttrs.contains("media"))
{
qDebug() << "moving media to media-col" << PaperSize;
if(!PaperSizes.contains(PaperSize))
{
emit convertFailed(tr("Unknown document format dimensions"));
return;
}
2021-07-31 13:52:33 +03:00
int x = PaperSizes[PaperSize].width()*100;
int y = PaperSizes[PaperSize].height()*100;
QJsonObject Dimensions =
{{"tag", IppMsg::BeginCollection},
{"value", QJsonObject { {"x-dimension", QJsonObject{{"tag", IppMsg::Integer}, {"value", x}}},
{"y-dimension", QJsonObject{{"tag", IppMsg::Integer}, {"value", y}}} }
}};
// TODO: make a setter function
QJsonObject MediaCol = jobAttrs["media-col"].toObject();
QJsonObject MediaColValue = MediaCol["value"].toObject();
MediaColValue["media-size"] = Dimensions;
MediaCol["value"] = MediaColValue;
2020-06-21 16:49:50 +03:00
MediaCol["tag"] = IppMsg::BeginCollection;
jobAttrs["media-col"] = MediaCol;
jobAttrs.remove("media");
}
2020-06-04 22:31:46 +03:00
QString documentFormat = getAttrOrDefault(jobAttrs, "document-format").toString();
2021-03-21 19:10:40 +03:00
qDebug() << "target format:" << documentFormat;
2020-06-04 22:31:46 +03:00
// document-format goes in the op-attrs and not the job-attrs
o.insert("document-format", QJsonObject {{"tag", IppMsg::MimeMediaType}, {"value", documentFormat}});
jobAttrs.remove("document-format");
2021-03-21 19:10:40 +03:00
documentFormat = targetFormatIfAuto(documentFormat, mimeType, supportedMimeTypes);
qDebug() << "adjusted target format:" << documentFormat;
2021-03-04 23:49:27 +03:00
if(documentFormat == "" || documentFormat == Mimer::OctetStream)
2020-06-04 22:31:46 +03:00
{
emit convertFailed(tr("Unknown document format"));
return;
}
2019-12-01 22:27:00 +03:00
qDebug() << "Printing job" << o << jobAttrs;
2020-01-26 14:08:57 +03:00
2020-12-09 21:59:58 +03:00
QNetworkRequest request = mkReq();
QJsonValue PrinterResolutionRef = getAttrOrDefault(jobAttrs, "printer-resolution");
2021-07-19 23:03:08 +03:00
quint32 HwResX = PrinterResolutionRef.toObject()["x"].toInt(300);
quint32 HwResY = PrinterResolutionRef.toObject()["y"].toInt(300);
bool BackHFlip = false;
bool BackVFlip = false;
adjustRasterSettings(documentFormat, jobAttrs, HwResX, HwResY, BackHFlip, BackVFlip);
quint32 Quality = getAttrOrDefault(jobAttrs, "print-quality").toInt();
QString PrintColorMode = getAttrOrDefault(jobAttrs, "print-color-mode").toString();
2020-06-05 22:46:17 +03:00
quint32 Colors = PrintColorMode.contains("color") ? 3 : PrintColorMode.contains("monochrome") ? 1 : 0;
bool pdfPageRangeAdjustNeeded = false;
quint32 PageRangeLow = 0;
quint32 PageRangeHigh = 0;
if(jobAttrs.contains("page-ranges"))
{
QJsonObject PageRanges = getAttrOrDefault(jobAttrs, "page-ranges").toObject();
PageRangeLow = PageRanges["low"].toInt();
PageRangeHigh = PageRanges["high"].toInt();
}
QString Sides = getAttrOrDefault(jobAttrs, "sides").toString();
2021-03-06 19:20:51 +03:00
if(documentFormat == Mimer::PWG || documentFormat == Mimer::URF || documentFormat == Mimer::Postscript || Mimer::isOffice(mimeType))
2020-10-17 13:08:51 +03:00
{ // Effected locally
jobAttrs.remove("page-ranges");
}
2021-03-04 23:49:27 +03:00
else if (documentFormat == Mimer::PDF)
2020-10-17 13:08:51 +03:00
{ // Only effected locally if really needed
if(jobAttrs.contains("page-ranges") && !_attrs.contains("page-ranges-supported"))
2020-08-01 21:18:47 +03:00
{
pdfPageRangeAdjustNeeded = true;
jobAttrs.remove("page-ranges");
2020-08-01 21:18:47 +03:00
}
}
qDebug() << "Final op attributes:" << o;
qDebug() << "Final job attributes:" << jobAttrs;
IppMsg job = mk_msg(o, jobAttrs);
QByteArray contents = job.encode(IppMsg::PrintJob);
// Non-jpeg images, Postscript and PDF (when not adjusting pages locally)
// Always convert non-jpeg images to get resizing
// TODO: make this sane
if((mimeType == documentFormat)
2021-03-04 23:49:27 +03:00
&& (documentFormat == Mimer::JPEG || !Mimer::isImage(mimeType))
&& !((documentFormat == Mimer::PDF) && pdfPageRangeAdjustNeeded))
{
QByteArray filedata = file.readAll();
contents = contents.append(filedata);
file.close();
2021-04-18 10:34:48 +03:00
setBusyMessage(tr("Transferring"));
QNetworkReply* reply = _print_nam->post(request, contents);
connect(reply, &QNetworkReply::uploadProgress, this, &IppPrinter::setProgress);
}
else
2020-01-26 14:08:57 +03:00
{
file.close();
if(PaperSize == "")
{
PaperSize = "iso_a4_210x297mm";
}
else if(!PaperSizes.contains(PaperSize))
{
emit convertFailed(tr("Unsupported print media"));
return;
}
2020-01-26 14:08:57 +03:00
QTemporaryFile* tempfile = new QTemporaryFile();
tempfile->open();
tempfile->write(contents);
qDebug() << tempfile->fileName();
tempfile->close();
2021-03-06 14:59:47 +03:00
bool TwoSided = false;
bool Tumble = false;
if(Sides=="two-sided-long-edge")
{
TwoSided = true;
}
else if(Sides=="two-sided-short-edge")
{
TwoSided = true;
Tumble = true;
}
2021-04-18 10:34:48 +03:00
setBusyMessage(tr("Converting"));
2020-05-06 21:46:41 +03:00
2021-03-04 23:49:27 +03:00
if(mimeType == Mimer::PDF)
2020-05-13 20:53:44 +03:00
{
2020-06-04 22:31:46 +03:00
emit doConvertPdf(request, filename, tempfile, documentFormat, Colors, Quality,
PaperSize, HwResX, HwResY, TwoSided, Tumble, PageRangeLow, PageRangeHigh,
BackHFlip, BackVFlip);
2020-05-13 20:53:44 +03:00
}
2021-06-12 13:45:58 +03:00
else if(mimeType == Mimer::Plaintext)
{
emit doConvertPlaintext(request, filename, tempfile, documentFormat, Colors, Quality,
PaperSize, HwResX, HwResY, TwoSided, Tumble, BackHFlip, BackVFlip);
2021-06-12 13:45:58 +03:00
}
2021-03-04 23:49:27 +03:00
else if (Mimer::isImage(mimeType))
2020-05-13 20:53:44 +03:00
{
2021-07-14 16:07:55 +03:00
QMargins margins(getAttrOrDefault(jobAttrs, "media-left-margin", "media-col").toInt(),
getAttrOrDefault(jobAttrs, "media-top-margin", "media-col").toInt(),
getAttrOrDefault(jobAttrs, "media-right-margin", "media-col").toInt(),
getAttrOrDefault(jobAttrs, "media-bottom-margin", "media-col").toInt());
2020-06-04 22:31:46 +03:00
emit doConvertImage(request, filename, tempfile, documentFormat, Colors, Quality,
2021-07-14 16:07:55 +03:00
PaperSize, HwResX, HwResY, margins);
2020-05-13 20:53:44 +03:00
}
2021-03-06 14:59:47 +03:00
else if(Mimer::isOffice(mimeType))
{
emit doConvertOfficeDocument(request, filename, tempfile, documentFormat, Colors, Quality,
PaperSize, HwResX, HwResY, TwoSided, Tumble, PageRangeLow, PageRangeHigh,
BackHFlip, BackVFlip);
2021-03-06 14:59:47 +03:00
}
else
{
emit convertFailed(tr("Cannot convert this file format"));
return;
}
2020-01-26 14:08:57 +03:00
}
return;
2019-12-01 22:27:00 +03:00
}
2019-12-08 15:55:56 +03:00
bool IppPrinter::getJobs() {
qDebug() << "getting jobs";
2019-12-12 22:53:46 +03:00
QJsonObject o = opAttrs();
2020-01-03 15:37:51 +03:00
o.insert("requested-attributes", QJsonObject {{"tag", IppMsg::Keyword}, {"value", "all"}});
2019-12-08 15:55:56 +03:00
IppMsg job = IppMsg(o, QJsonObject());
2020-12-09 21:59:58 +03:00
QNetworkRequest request = mkReq();
2019-12-08 15:55:56 +03:00
QByteArray contents = job.encode(IppMsg::GetJobs);
_jobs_nam->post(request, contents);
return true;
}
2019-12-12 22:53:46 +03:00
bool IppPrinter::cancelJob(qint32 jobId) {
2020-02-23 15:11:28 +03:00
qDebug() << "cancelling jobs";
2019-12-08 15:55:56 +03:00
2019-12-12 22:53:46 +03:00
QJsonObject o = opAttrs();
o.insert("job-id", QJsonObject {{"tag", IppMsg::Integer}, {"value", jobId}});
IppMsg job = IppMsg(o, QJsonObject());
2020-12-09 21:59:58 +03:00
QNetworkRequest request = mkReq();
2019-12-12 22:53:46 +03:00
QByteArray contents = job.encode(IppMsg::CancelJob);
2020-01-20 22:11:41 +03:00
2019-12-12 22:53:46 +03:00
_job_cancel_nam->post(request, contents);
return true;
}
bool IppPrinter::isIpps()
{
return _url.scheme() == "ipps";
}
2021-06-28 21:15:21 +03:00
bool IppPrinter::correctSuffix()
{
foreach(QJsonValue u, _attrs["printer-uri-supported"].toObject()["value"].toArray())
{
QUrl url(u.toString());
if(url.path() == _url.path())
{
return true;
}
}
return false;
}
QStringList IppPrinter::suffixes()
{
QStringList res;
foreach(QJsonValue u, _attrs["printer-uri-supported"].toObject()["value"].toArray())
{
QUrl url(u.toString());
if(!res.contains(url.path()))
{
res.append(url.path());
}
}
res.sort();
return res;
}
QUrl IppPrinter::httpUrl() {
2021-06-19 19:16:42 +03:00
qDebug() << _url;
QUrl url = _url;
2021-06-19 19:16:42 +03:00
if(url.scheme() == "ipps")
{
url.setScheme("https");
if(url.port() == -1) {
url.setPort(443);
}
}
else
{
url.setScheme("http");
if(url.port() == -1) {
url.setPort(631);
}
}
return url;
}
2020-12-09 21:59:58 +03:00
QNetworkRequest IppPrinter::mkReq() {
QNetworkRequest request;
request.setUrl(httpUrl());
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
request.setHeader(QNetworkRequest::UserAgentHeader, "SeaPrint " SEAPRINT_VERSION);
request.setRawHeader("Accept-Encoding", "identity");
2021-06-19 19:16:42 +03:00
request.setSslConfiguration(QSslConfiguration());
2020-12-09 21:59:58 +03:00
return request;
}
2020-05-06 21:46:41 +03:00
void IppPrinter::setBusyMessage(QString msg)
{
_busyMessage = msg;
emit busyMessageChanged();
}
2020-06-03 21:42:18 +03:00
void IppPrinter::setProgress(qint64 sent, qint64 total)
{
if(total == 0)
return;
_progress = QString::number(100*sent/total);
_progress += "%";
emit progressChanged();
}
2021-07-14 16:07:55 +03:00
QJsonValue IppPrinter::getAttrOrDefault(QJsonObject jobAttrs, QString name, QString subkey)
{
2021-07-14 16:07:55 +03:00
if(subkey == "")
{
2021-07-14 16:07:55 +03:00
if(jobAttrs.contains(name))
{
return jobAttrs[name].toObject()["value"];
}
else
{
return _attrs[name+"-default"].toObject()["value"];
}
}
2021-07-14 16:07:55 +03:00
else
{
QJsonObject subObj = jobAttrs[subkey].toObject()["value"].toObject();
if(subObj.contains(name))
{
return subObj[name].toObject()["value"];
}
else
{
return _attrs[name+"-default"].toObject()["value"];
}
}
}
IppMsg IppPrinter::mk_msg(QJsonObject opAttrs, QJsonObject jobAttrs)
{
if(_attrs.contains("ipp-versions-supported") &&
_attrs["ipp-versions-supported"].toObject()["value"].toArray().contains("2.0"))
{
qDebug() << "TWO-POINT-ZERO";
return IppMsg(opAttrs, jobAttrs, 2, 0);
}
return IppMsg(opAttrs, jobAttrs);
}