From 44df94e5a5c2c8b48f91da673e29410d0443bcfd Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Sat, 3 Feb 2024 12:04:54 +0100 Subject: [PATCH] Introduce IppPrinter abstraction --- Makefile | 2 +- lib/ippprinter.cpp | 35 ++++++++++++++++++++++++++++++ lib/ippprinter.h | 42 +++++++++++++++++++++++++++++++++++ utils/ippclient.cpp | 53 ++++++++++----------------------------------- 4 files changed, 90 insertions(+), 42 deletions(-) create mode 100644 lib/ippprinter.cpp create mode 100644 lib/ippprinter.h diff --git a/Makefile b/Makefile index a5d0a54..6e1ff27 100644 --- a/Makefile +++ b/Makefile @@ -39,7 +39,7 @@ baselinify_mad: bytestream.o baselinify_mad.o baselinify_main.o ippdecode: bytestream.o ippmsg.o ippattr.o ippdecode.o $(CXX) $^ -o $@ -ippclient: ippmsg.o ippattr.o ippprintjob.o printparameters.o ippclient.o curlrequester.o minimime.o pdf2printable.o ppm2pwg.o baselinify.o bytestream.o +ippclient: ippmsg.o ippattr.o ippprinter.o ippprintjob.o printparameters.o ippclient.o curlrequester.o minimime.o pdf2printable.o ppm2pwg.o baselinify.o bytestream.o $(CXX) $^ $(shell pkg-config --libs poppler-glib) -ljpeg -lcurl -lz -o $@ minimime: minimime_main.o minimime.o bytestream.o diff --git a/lib/ippprinter.cpp b/lib/ippprinter.cpp new file mode 100644 index 0000000..4df8327 --- /dev/null +++ b/lib/ippprinter.cpp @@ -0,0 +1,35 @@ +#include "ippprinter.h" +#include "curlrequester.h" + +IppPrinter::IppPrinter(std::string addr) : _addr(addr) +{ + _error = refresh(); +} + +Error IppPrinter::refresh() +{ + Error error; + IppAttrs getPrinterAttrsJobAttrs = IppMsg::baseOpAttrs(_addr); + IppMsg getPrinterAttrsMsg(IppMsg::GetPrinterAttrs, getPrinterAttrsJobAttrs); + + CurlIppPoster getPrinterAttrsReq(_addr, getPrinterAttrsMsg.encode(), _ignoreSslErrors, _verbose); + Bytestream getPrinterAttrsResult; + CURLcode res0 = getPrinterAttrsReq.await(&getPrinterAttrsResult); + if(res0 == CURLE_OK) + { + try + { + IppMsg getPrinterAttrsResp(getPrinterAttrsResult); + _printerAttrs = getPrinterAttrsResp.getPrinterAttrs(); + } + catch(const std::exception& e) + { + error = e.what(); + } + } + else + { + error = curl_easy_strerror(res0); + } + return error; +} diff --git a/lib/ippprinter.h b/lib/ippprinter.h new file mode 100644 index 0000000..6916db4 --- /dev/null +++ b/lib/ippprinter.h @@ -0,0 +1,42 @@ +#ifndef IPPPRINTER_H +#define IPPPRINTER_H + +#include +#include "error.h" +#include "ippattr.h" +#include "ippmsg.h" +#include "ippprintjob.h" + +class IppPrinter +{ +public: + IppPrinter() = delete; + IppPrinter(std::string addr); + Error refresh(); + + IppAttrs attributes() + { + return _printerAttrs; + } + + Error error() + { + return _error; + } + + IppPrintJob createJob() + { + return IppPrintJob(_printerAttrs); + } + +private: + std::string _addr; + bool _verbose = false; + bool _ignoreSslErrors = true; + + Error _error; + IppAttrs _printerAttrs; + +}; + +#endif //IPPPRINTER_H diff --git a/utils/ippclient.cpp b/utils/ippclient.cpp index 6bbfa57..46345db 100644 --- a/utils/ippclient.cpp +++ b/utils/ippclient.cpp @@ -6,9 +6,7 @@ #include #include "argget.h" -#include "curlrequester.h" -#include "ippmsg.h" -#include "ippprintjob.h" +#include "ippprinter.h" #include "minimime.h" #include "pointer.h" @@ -192,48 +190,21 @@ int main(int argc, char** argv) return 1; } + IppPrinter printer(addr); + Error error = printer.error(); + if(error) + { + std::cerr << "Could not get printer attributes: " << error.value() << std::endl; + return 1; + } + if(args.subCommand() == "get-attrs") { - IppAttrs getPrinterAttrsJobAttrs = IppMsg::baseOpAttrs(addr); - IppMsg getPrinterAttrsMsg(IppMsg::GetPrinterAttrs, getPrinterAttrsJobAttrs); - - CurlIppPoster getPrinterAttrsReq(addr, getPrinterAttrsMsg.encode(), true, verbose); - Bytestream getPrinterAttrsResult; - CURLcode res0 = getPrinterAttrsReq.await(&getPrinterAttrsResult); - IppAttrs printerAttrs; - if(res0 == CURLE_OK) - { - IppMsg getPrinterAttrsResp(getPrinterAttrsResult); - printerAttrs = getPrinterAttrsResp.getPrinterAttrs(); - } - else - { - std::cerr << "Could not get printer attributes: " << curl_easy_strerror(res0) << std::endl; - return 1; - } - std::cout << printerAttrs; + std::cout << printer.attributes(); } else if(args.subCommand() == "print") { - IppAttrs getPrinterAttrsJobAttrs = IppMsg::baseOpAttrs(addr); - IppMsg getPrinterAttrsMsg(IppMsg::GetPrinterAttrs, getPrinterAttrsJobAttrs); - - CurlIppPoster getPrinterAttrsReq(addr, getPrinterAttrsMsg.encode(), true, verbose); - Bytestream getPrinterAttrsResult; - CURLcode res0 = getPrinterAttrsReq.await(&getPrinterAttrsResult); - IppAttrs printerAttrs; - if(res0 == CURLE_OK) - { - IppMsg getPrinterAttrsResp(getPrinterAttrsResult); - printerAttrs = getPrinterAttrsResp.getPrinterAttrs(); - } - else - { - std::cerr << "Could not get printer attributes: " << curl_easy_strerror(res0) << std::endl; - return 1; - } - - IppPrintJob ip(printerAttrs); + IppPrintJob ip = printer.createJob(); if(oneStageOpt.isSet()) { @@ -343,7 +314,7 @@ int main(int argc, char** argv) nPages = poppler_document_get_n_pages(doc); } - Error error = ip.run(addr, inFile, mimeType, nPages, verbose); + error = ip.run(addr, inFile, mimeType, nPages, verbose); if(error) { std::cerr << "Print failed: " << error.value() << std::endl;