WIP: make an IODevice shim

This commit is contained in:
Anton Thomasson 2020-01-26 12:08:57 +01:00
parent e0b10c83a1
commit 5ea76a4433
5 changed files with 202 additions and 10 deletions

View file

@ -26,10 +26,12 @@ SOURCES += src/harbour-seaprint.cpp \
src/bytestream.cpp \
src/ippmsg.cpp \
src/ippprinter.cpp \
src/mimer.cpp
src/mimer.cpp \
src/ippraster.cpp
DISTFILES += qml/harbour-seaprint.qml \
../harbour-splay/qml/pages/ChannelItem.qml \
qml/cover/CoverPage.qml \
qml/components/*.qml \
qml/pages/*.qml \
@ -63,4 +65,5 @@ HEADERS += \
src/bytestream.h \
src/ippmsg.h \
src/ippprinter.h \
src/mimer.h
src/mimer.h \
src/ippraster.h

View file

@ -1,6 +1,7 @@
#include "ippprinter.h"
#include "ioprepender.h"
#include "ippraster.h"
#include <seaprint_version.h>
#include <QtConcurrent/QtConcurrent>
IppPrinter::IppPrinter()
{
@ -196,6 +197,71 @@ void IppPrinter::ignoreKnownSslErrors(QNetworkReply *reply, const QList<QSslErro
reply->ignoreSslErrors(errors);
}
void IppPrinter::doWork(QString filename, QTemporaryFile* tempfile)
{
QNetworkRequest request;
request.setUrl(httpUrl());
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
request.setHeader(QNetworkRequest::UserAgentHeader, "SeaPrint "SEAPRINT_VERSION);
QProcess* muraster = new QProcess(this);
muraster->setProgram("/home/nemo/stuff/bin/muraster");
muraster->setArguments({"-F", "pgm", filename});
QProcess* ppm2pwg = new QProcess(this);
ppm2pwg->setProgram("/home/nemo/repos/pwg/ppm2pwg");
QStringList env; // {"PREPEND_FILE="+tempfile->fileName()};
bool apple = false;
if(apple)
{
env.append("URF=true");
}
qDebug() << "Prepend file env done";
ppm2pwg->setEnvironment(env);
muraster->setStandardOutputProcess(ppm2pwg);
ppm2pwg->setStandardOutputFile(tempfile->fileName(), QIODevice::Append);
connect(muraster, SIGNAL(finished(int, QProcess::ExitStatus)), muraster, SLOT(deleteLater()));
connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), ppm2pwg, SLOT(deleteLater()));
connect(ppm2pwg, SIGNAL(finished(int, QProcess::ExitStatus)), tempfile, SLOT(deleteLater()));
qDebug() << "All connected";
muraster->start();
ppm2pwg->start();
qDebug() << "Starting";
if(!muraster->waitForStarted())
{
qDebug() << "muraster died";
return;
}
if(!ppm2pwg->waitForStarted())
{
qDebug() << "ppm2pwg died";
return;
}
qDebug() << "All started";
ppm2pwg->waitForFinished();
qDebug() << "Finished";
// IppRaster* raster = new IppRaster(ppm2pwg);
// raster->open(QIODevice::ReadOnly);
// request.setAttribute(QNetworkRequest::DoNotBufferUploadDataAttribute, true);
_print_nam->post(request, tempfile);
qDebug() << "posted";
}
void IppPrinter::print(QJsonObject attrs, QString filename){
@ -210,11 +276,6 @@ void IppPrinter::print(QJsonObject attrs, QString filename){
}
QFileInfo fileinfo(file);
QNetworkRequest request;
request.setUrl(httpUrl());
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
request.setHeader(QNetworkRequest::UserAgentHeader, "SeaPrint "SEAPRINT_VERSION);
QJsonObject o = opAttrs();
o.insert("job-name", QJsonObject {{"tag", IppMsg::NameWithoutLanguage}, {"value", fileinfo.fileName()}});
@ -236,8 +297,37 @@ void IppPrinter::print(QJsonObject attrs, QString filename){
QByteArray filedata = file.readAll();
contents = contents.append(filedata);
_print_nam->post(request, contents);
file.close();
// TODO: do this only conditionally, and to the raster suppoerted/preferred
bool transcode = true;
if(transcode)
{
file.close();
QTemporaryFile* tempfile = new QTemporaryFile();
tempfile->open();
tempfile->write(contents);
qDebug() << tempfile->fileName();
tempfile->close();
QtConcurrent::run(this, &IppPrinter::doWork, filename, tempfile);
}
else {
QNetworkRequest request;
request.setUrl(httpUrl());
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/ipp");
request.setHeader(QNetworkRequest::UserAgentHeader, "SeaPrint "SEAPRINT_VERSION);
QByteArray filedata = file.readAll();
contents = contents.append(filedata);
file.close();
_print_nam->post(request, contents);
}
return;
}
bool IppPrinter::getJobs() {

View file

@ -48,6 +48,9 @@ public slots:
void ignoreKnownSslErrors(QNetworkReply *reply, const QList<QSslError> &errors);
void doWork(QString filename, QTemporaryFile* tempfile);
private:
QUrl _url;
QUrl httpUrl();

57
src/ippraster.cpp Normal file
View file

@ -0,0 +1,57 @@
#include "ippraster.h"
IppRaster::IppRaster(QIODevice *underlyingDevice)
: underlyingDevice(underlyingDevice)
{
connect(underlyingDevice, SIGNAL(readyRead()), this, SLOT(doDataAvail()));
connect(underlyingDevice, SIGNAL(readChannelFinished()), this, SLOT(doReadFinished()));
}
bool IppRaster::open(QIODevice::OpenMode mode)
{
qDebug() << "open";
bool underlyingOk;
if (underlyingDevice->isOpen())
underlyingOk = (underlyingDevice->openMode() == mode);
else
underlyingOk = underlyingDevice->open(mode);
if (underlyingOk) {
setOpenMode(mode);
qDebug() << "open true";
emit readyRead();
return true;
}
qDebug() << underlyingOk << underlyingDevice->openMode() << underlyingDevice->isOpen() << mode << "borked";
return false;
}
void IppRaster::close()
{
qDebug() << "close";
underlyingDevice->close();
}
bool IppRaster::seek(qint64 pos)
{
qDebug() << "seek";
return false;
}
bool IppRaster::atEnd() const
{
qDebug() << "atend";
return underlyingDevice->atEnd();
}
qint64 IppRaster::readData(char *data, qint64 maxlen)
{
qDebug() << "read" << maxlen;
return underlyingDevice->read(data, maxlen);
}
qint64 IppRaster::writeData(const char *data, qint64 len)
{
return underlyingDevice->write(data, len);
}

39
src/ippraster.h Normal file
View file

@ -0,0 +1,39 @@
#ifndef IPPRASTER_H
#define IPPRASTER_H
#include <qiodevice.h>
#include <QDebug>
class IppRaster : public QIODevice
{
Q_OBJECT
public:
IppRaster(QIODevice *underlyingDevice);
~IppRaster() {qDebug() << "deleted";}
bool open(QIODevice::OpenMode mode) override;
void close() override;
bool seek(qint64) override;
bool atEnd() const override;
qint64 readData(char *data, qint64 maxlen) override;
qint64 writeData(const char *data, qint64 len) override;
bool isSequential() const override {return false;}
qint64 bytesAvailable() const override {return underlyingDevice->bytesAvailable();}
qint64 size() const override {return underlyingDevice->size();}
bool waitForReadyRead(int msecs) {return true;}
public slots:
void doDataAvail() {emit readyRead();}
void doReadFinished() {emit readChannelFinished();}
private:
QIODevice* underlyingDevice;
};
#endif