Use Bytestream instead of QByteArray for IPP messages

This commit is contained in:
Anton Thomasson 2021-12-14 20:01:52 +01:00
parent 79e7175f87
commit 70206916e4
9 changed files with 59 additions and 100 deletions

View file

@ -21,34 +21,34 @@ ConvertWorker::ConvertWorker(IppPrinter* parent) // : QObject((QObject*)parent)
_printer = parent; _printer = parent;
} }
void ConvertWorker::command(QByteArray msg) void ConvertWorker::command(Bytestream msg)
{ {
CurlRequester cr(_printer->httpUrl()); CurlRequester cr(_printer->httpUrl());
cr.setFinishedCallback(_printer, &IppPrinter::getPrinterAttributesFinished); cr.setFinishedCallback(_printer, &IppPrinter::getPrinterAttributesFinished);
qDebug() << "command..."; qDebug() << "command...";
cr.write(msg.data(), msg.length()); cr.write((char*)msg.raw(), msg.size());
} }
// TODO: de-duplicate // TODO: de-duplicate
void ConvertWorker::getJobs(QByteArray msg) void ConvertWorker::getJobs(Bytestream msg)
{ {
CurlRequester cr(_printer->httpUrl()); CurlRequester cr(_printer->httpUrl());
cr.setFinishedCallback(_printer, &IppPrinter::getJobsRequestFinished); cr.setFinishedCallback(_printer, &IppPrinter::getJobsRequestFinished);
cr.write(msg.data(), msg.length()); cr.write((char*)msg.raw(), msg.size());
} }
void ConvertWorker::cancelJob(QByteArray msg) void ConvertWorker::cancelJob(Bytestream msg)
{ {
CurlRequester cr(_printer->httpUrl()); CurlRequester cr(_printer->httpUrl());
cr.setFinishedCallback(_printer, &IppPrinter::cancelJobFinished); cr.setFinishedCallback(_printer, &IppPrinter::cancelJobFinished);
cr.write(msg.data(), msg.length()); cr.write((char*)msg.raw(), msg.size());
} }
void ConvertWorker::justUpload(QString filename, QByteArray header) void ConvertWorker::justUpload(QString filename, Bytestream header)
{ {
try { try {
emit busyMessage(tr("Printing")); emit busyMessage(tr("Printing"));
@ -59,7 +59,7 @@ try {
QFile file(filename); QFile file(filename);
file.open(QFile::ReadOnly); file.open(QFile::ReadOnly);
OK(cr.write(header.data(), header.length())); OK(cr.write((char*)header.raw(), header.size()));
QByteArray tmp = file.readAll(); QByteArray tmp = file.readAll();
OK(cr.write(tmp.data(), tmp.length())); OK(cr.write(tmp.data(), tmp.length()));
file.close(); file.close();
@ -70,7 +70,7 @@ catch(const ConvertFailedException& e)
} }
} }
void ConvertWorker::convertPdf(QString filename, QByteArray header, void ConvertWorker::convertPdf(QString filename, Bytestream header,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
quint32 PageRangeLow, quint32 PageRangeHigh, bool BackHFlip, bool BackVFlip) quint32 PageRangeLow, quint32 PageRangeHigh, bool BackHFlip, bool BackVFlip)
@ -109,7 +109,7 @@ try {
CurlRequester cr(_printer->httpUrl()); CurlRequester cr(_printer->httpUrl());
cr.setFinishedCallback(_printer, &IppPrinter::printRequestFinished); cr.setFinishedCallback(_printer, &IppPrinter::printRequestFinished);
OK(cr.write(header.data(), header.length())); OK(cr.write((char*)header.raw(), header.size()));
write_fun WriteFun([&cr](unsigned char const* buf, unsigned int len) -> bool write_fun WriteFun([&cr](unsigned char const* buf, unsigned int len) -> bool
{ {
@ -148,7 +148,7 @@ catch(const ConvertFailedException& e)
} }
} }
void ConvertWorker::convertImage(QString filename, QByteArray header, void ConvertWorker::convertImage(QString filename, Bytestream header,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, QMargins margins) quint32 HwResX, quint32 HwResY, QMargins margins)
{ {
@ -297,7 +297,7 @@ try {
CurlRequester cr(_printer->httpUrl()); CurlRequester cr(_printer->httpUrl());
cr.setFinishedCallback(_printer, &IppPrinter::printRequestFinished); cr.setFinishedCallback(_printer, &IppPrinter::printRequestFinished);
OK(cr.write(header.data(), header.length())); OK(cr.write((char*)header.raw(), header.size()));
OK(cr.write((char*)(outBts.raw()), outBts.size())); OK(cr.write((char*)(outBts.raw()), outBts.size()));
} }
@ -310,7 +310,7 @@ catch(const ConvertFailedException& e)
} }
} }
void ConvertWorker::convertOfficeDocument(QString filename, QByteArray header, void ConvertWorker::convertOfficeDocument(QString filename, Bytestream header,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
quint32 PageRangeLow, quint32 PageRangeHigh, bool BackHFlip, bool BackVFlip) quint32 PageRangeLow, quint32 PageRangeHigh, bool BackHFlip, bool BackVFlip)
@ -403,7 +403,7 @@ catch(const ConvertFailedException& e)
} }
} }
void ConvertWorker::convertPlaintext(QString filename, QByteArray header, void ConvertWorker::convertPlaintext(QString filename, Bytestream header,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
bool BackHFlip, bool BackVFlip) bool BackHFlip, bool BackVFlip)

View file

@ -31,27 +31,27 @@ private:
ConvertWorker(); ConvertWorker();
public slots: public slots:
void command(QByteArray msg); void command(Bytestream msg);
void getJobs(QByteArray msg); void getJobs(Bytestream msg);
void cancelJob(QByteArray msg); void cancelJob(Bytestream msg);
void justUpload(QString filename, QByteArray header); void justUpload(QString filename, Bytestream header);
void convertPdf(QString filename, QByteArray header, void convertPdf(QString filename, Bytestream header,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
quint32 PageRangeLow, quint32 PageRangeHigh, bool BackHFlip, bool BackVFlip); quint32 PageRangeLow, quint32 PageRangeHigh, bool BackHFlip, bool BackVFlip);
void convertImage(QString filename, QByteArray header, void convertImage(QString filename, Bytestream header,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, QMargins margins); quint32 HwResX, quint32 HwResY, QMargins margins);
void convertOfficeDocument(QString filename, QByteArray header, void convertOfficeDocument(QString filename, Bytestream header,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
quint32 PageRangeLow, quint32 PageRangeHigh, bool BackHFlip, bool BackVFlip); quint32 PageRangeLow, quint32 PageRangeHigh, bool BackHFlip, bool BackVFlip);
void convertPlaintext(QString filename, QByteArray header, void convertPlaintext(QString filename, Bytestream header,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, bool BackHFlip, bool BackVFlip); quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, bool BackHFlip, bool BackVFlip);

View file

@ -3,8 +3,6 @@
#include "settings.h" #include "settings.h"
#include <seaprint_version.h> #include <seaprint_version.h>
Q_DECLARE_METATYPE(CURLcode)
static size_t trampoline(char* dest, size_t size, size_t nmemb, void* userp) static size_t trampoline(char* dest, size_t size, size_t nmemb, void* userp)
{ {
CurlRequester* cid = (CurlRequester*)userp; CurlRequester* cid = (CurlRequester*)userp;
@ -13,8 +11,6 @@ static size_t trampoline(char* dest, size_t size, size_t nmemb, void* userp)
CurlWorker::CurlWorker(QUrl addr, void* parent) CurlWorker::CurlWorker(QUrl addr, void* parent)
{ {
qRegisterMetaType<CURLcode>();
_curl = curl_easy_init(); _curl = curl_easy_init();
// if(!curl) // if(!curl)
// return false; // return false;
@ -50,7 +46,7 @@ CurlWorker::~CurlWorker()
} }
void CurlWorker::run(){ void CurlWorker::run(){
QByteArray buf; Bytestream buf;
curl_easy_setopt(_curl, CURLOPT_WRITEDATA, &buf); curl_easy_setopt(_curl, CURLOPT_WRITEDATA, &buf);
curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, write_callback);

View file

@ -5,6 +5,7 @@
#include <QUrl> #include <QUrl>
#include <curl/curl.h> #include <curl/curl.h>
#include <QtDebug> #include <QtDebug>
#include <bytestream.h>
class CurlWorker : public QThread class CurlWorker : public QThread
{ {
@ -18,11 +19,11 @@ public:
static size_t write_callback(char *ptr, size_t size, size_t nmemb, void* userdata) static size_t write_callback(char *ptr, size_t size, size_t nmemb, void* userdata)
{ {
size_t bytes_to_write = size*nmemb; size_t bytes_to_write = size*nmemb;
((QByteArray*)userdata)->append(ptr, bytes_to_write); ((Bytestream*)userdata)->putBytes(ptr, bytes_to_write);
return bytes_to_write; return bytes_to_write;
} }
signals: signals:
void done(CURLcode, QByteArray); void done(CURLcode, Bytestream);
private: private:
// Container for the cURL global init and cleanup // Container for the cURL global init and cleanup

View file

@ -9,6 +9,9 @@
#include <src/svgprovider.h> #include <src/svgprovider.h>
#include <src/settings.h> #include <src/settings.h>
Q_DECLARE_METATYPE(CURLcode)
Q_DECLARE_METATYPE(Bytestream)
template <class T> template <class T>
static QObject* singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine) static QObject* singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngine)
{ {
@ -22,6 +25,9 @@ static QObject* singletontype_provider(QQmlEngine *engine, QJSEngine *scriptEngi
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
qRegisterMetaType<CURLcode>();
qRegisterMetaType<Bytestream>();
QGuiApplication* app = SailfishApp::application(argc, argv); QGuiApplication* app = SailfishApp::application(argc, argv);
app->setOrganizationName(QStringLiteral("net.attah")); app->setOrganizationName(QStringLiteral("net.attah"));

View file

@ -19,52 +19,10 @@ IppMsg::~IppMsg()
{ {
} }
IppMsg::IppMsg(QNetworkReply* resp)
IppMsg::IppMsg(Bytestream& bts)
{ {
QByteArray tmp = resp->readAll(); // Bytestream bts(resp.constData(), resp.length());
Bytestream bts(tmp.constData(), tmp.length());
quint32 reqId;
bts >> _majVsn >> _minVsn >> _status >> reqId;
QJsonObject attrs;
IppMsg::IppTag currentAttrType = IppTag::EndAttrs;
while(!bts.atEnd())
{
if(bts.peekU8() <= IppTag::UnsupportedAttrs) {
if(currentAttrType == IppTag::OpAttrs) {
_opAttrs = attrs;
}
else if (currentAttrType == IppTag::JobAttrs) {
_jobAttrs.append(attrs);
}
else if (currentAttrType == IppTag::PrinterAttrs) {
_printerAttrs = attrs;
}
else if (currentAttrType == IppTag::UnsupportedAttrs) {
qDebug() << "WARNING: unsupported attrs reported:" << attrs;
}
if(bts >>= (uint8_t)IppTag::EndAttrs) {
break;
}
currentAttrType = (IppTag)bts.getU8();
attrs = QJsonObject();
}
else {
consume_attribute(attrs, bts);
}
}
}
IppMsg::IppMsg(QByteArray resp)
{
Bytestream bts(resp.constData(), resp.length());
quint32 reqId; quint32 reqId;
@ -327,7 +285,7 @@ QString IppMsg::consume_attribute(QJsonObject& attrs, Bytestream& data)
return name; return name;
} }
QByteArray IppMsg::encode(Operation op) Bytestream IppMsg::encode(Operation op)
{ {
Bytestream ipp; Bytestream ipp;
@ -369,7 +327,7 @@ QByteArray IppMsg::encode(Operation op)
ipp << quint8(EndAttrs); ipp << quint8(EndAttrs);
return QByteArray((char*)(ipp.raw()), ipp.size()); return ipp;
} }
void IppMsg::encode_attr(Bytestream& msg, quint8 tag, QString name, QJsonValue value, bool inCollection) void IppMsg::encode_attr(Bytestream& msg, quint8 tag, QString name, QJsonValue value, bool inCollection)

View file

@ -1,9 +1,8 @@
#ifndef IPP_PROTO_H #ifndef IPP_MSG_H
#define IPP_PROTO_H #define IPP_MSG_H
#include "bytestream.h" #include "bytestream.h"
#include <QByteArray>
#include <QObject> #include <QObject>
#include <QDebug> #include <QDebug>
#include <QUrl> #include <QUrl>
@ -66,13 +65,12 @@ public:
}; };
explicit IppMsg(); explicit IppMsg();
explicit IppMsg(QNetworkReply* resp); explicit IppMsg(Bytestream& resp);
explicit IppMsg(QByteArray resp);
IppMsg(QJsonObject opAttrs, QJsonObject jobAttrs=QJsonObject(), quint8 majVsn=1, quint8 minVsn=1); IppMsg(QJsonObject opAttrs, QJsonObject jobAttrs=QJsonObject(), quint8 majVsn=1, quint8 minVsn=1);
IppMsg(const IppMsg& other) = default; IppMsg(const IppMsg& other) = default;
~IppMsg(); ~IppMsg();
QByteArray encode(Operation op); Bytestream encode(Operation op);
QJsonObject getPrinterAttrs() {return _printerAttrs;} QJsonObject getPrinterAttrs() {return _printerAttrs;}
QJsonArray getJobAttrs() {return _jobAttrs;} QJsonArray getJobAttrs() {return _jobAttrs;}
QJsonObject getOpAttrs() {return _opAttrs;} QJsonObject getOpAttrs() {return _opAttrs;}
@ -100,4 +98,4 @@ private:
static quint32 _reqid; static quint32 _reqid;
}; };
#endif // IPP_PROTO_H #endif // IPP_MSG_H

View file

@ -154,7 +154,7 @@ void IppPrinter::UpdateAdditionalDocumentFormats()
emit additionalDocumentFormatsChanged(); emit additionalDocumentFormatsChanged();
} }
void IppPrinter::getPrinterAttributesFinished(CURLcode res, QByteArray data) void IppPrinter::getPrinterAttributesFinished(CURLcode res, Bytestream data)
{ {
qDebug() << res; qDebug() << res;
_attrs = QJsonObject(); _attrs = QJsonObject();
@ -179,7 +179,7 @@ void IppPrinter::getPrinterAttributesFinished(CURLcode res, QByteArray data)
UpdateAdditionalDocumentFormats(); UpdateAdditionalDocumentFormats();
} }
void IppPrinter::printRequestFinished(CURLcode res, QByteArray data) void IppPrinter::printRequestFinished(CURLcode res, Bytestream data)
{ {
_jobAttrs = QJsonObject(); _jobAttrs = QJsonObject();
bool status = false; bool status = false;
@ -211,7 +211,7 @@ void IppPrinter::printRequestFinished(CURLcode res, QByteArray data)
emit jobFinished(status); emit jobFinished(status);
} }
void IppPrinter::getJobsRequestFinished(CURLcode res, QByteArray data) void IppPrinter::getJobsRequestFinished(CURLcode res, Bytestream data)
{ {
if(res == CURLE_OK) if(res == CURLE_OK)
{ {
@ -229,7 +229,7 @@ void IppPrinter::getJobsRequestFinished(CURLcode res, QByteArray data)
} }
void IppPrinter::cancelJobFinished(CURLcode res, QByteArray data) void IppPrinter::cancelJobFinished(CURLcode res, Bytestream data)
{ {
bool status = false; bool status = false;
if(res == CURLE_OK) if(res == CURLE_OK)
@ -549,7 +549,7 @@ void IppPrinter::print(QJsonObject jobAttrs, QString filename)
qDebug() << "Final job attributes:" << jobAttrs; qDebug() << "Final job attributes:" << jobAttrs;
IppMsg job = mk_msg(o, jobAttrs); IppMsg job = mk_msg(o, jobAttrs);
QByteArray contents = job.encode(IppMsg::PrintJob); Bytestream contents = job.encode(IppMsg::PrintJob);
// Shouldn't and can't process these formats respectively // Shouldn't and can't process these formats respectively
if((mimeType == documentFormat) && (documentFormat == Mimer::JPEG || documentFormat == Mimer::Postscript)) if((mimeType == documentFormat) && (documentFormat == Mimer::JPEG || documentFormat == Mimer::Postscript))

View file

@ -50,27 +50,27 @@ signals:
void jobFinished(bool status); void jobFinished(bool status);
void cancelStatus(bool status); void cancelStatus(bool status);
void doCommand(QByteArray msg); void doCommand(Bytestream msg);
void doGetJobs(QByteArray msg); void doGetJobs(Bytestream msg);
void doCancelJob(QByteArray msg); void doCancelJob(Bytestream msg);
void doJustUpload(QString filename, QByteArray header); void doJustUpload(QString filename, Bytestream header);
void doConvertPdf(QString filename, QByteArray header, void doConvertPdf(QString filename, Bytestream header,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
quint32 PageRangeLow, quint32 PageRangeHigh, bool BackHFlip, bool BackVFlip); quint32 PageRangeLow, quint32 PageRangeHigh, bool BackHFlip, bool BackVFlip);
void doConvertImage(QString filename, QByteArray header, void doConvertImage(QString filename, Bytestream header,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, QMargins margins); quint32 HwResX, quint32 HwResY, QMargins margins);
void doConvertOfficeDocument(QString filename, QByteArray header, void doConvertOfficeDocument(QString filename, Bytestream header,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble,
quint32 PageRangeLow, quint32 PageRangeHigh, bool BackHFlip, bool BackVFlip); quint32 PageRangeLow, quint32 PageRangeHigh, bool BackHFlip, bool BackVFlip);
void doConvertPlaintext(QString filename, QByteArray header, void doConvertPlaintext(QString filename, Bytestream header,
QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize, QString targetFormat, quint32 Colors, quint32 Quality, QString PaperSize,
quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, bool BackHFlip, bool BackVFlip); quint32 HwResX, quint32 HwResY, bool TwoSided, bool Tumble, bool BackHFlip, bool BackVFlip);
@ -84,10 +84,10 @@ public slots:
void onUrlChanged(); void onUrlChanged();
void UpdateAdditionalDocumentFormats(); void UpdateAdditionalDocumentFormats();
void getPrinterAttributesFinished(CURLcode res, QByteArray data); void getPrinterAttributesFinished(CURLcode res, Bytestream data);
void printRequestFinished(CURLcode res, QByteArray data); void printRequestFinished(CURLcode res, Bytestream data);
void getJobsRequestFinished(CURLcode res, QByteArray data); void getJobsRequestFinished(CURLcode res, Bytestream data);
void cancelJobFinished(CURLcode res, QByteArray data); void cancelJobFinished(CURLcode res, Bytestream data);
static void ignoreSslErrors(QNetworkReply *reply, const QList<QSslError> &errors); static void ignoreSslErrors(QNetworkReply *reply, const QList<QSslError> &errors);