Skip to content

Commit

Permalink
Introduce IppPrinter abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed Feb 3, 2024
1 parent baa33a9 commit 44df94e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions lib/ippprinter.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
42 changes: 42 additions & 0 deletions lib/ippprinter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef IPPPRINTER_H
#define IPPPRINTER_H

#include <string>
#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
53 changes: 12 additions & 41 deletions utils/ippclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
#include <poppler-document.h>

#include "argget.h"
#include "curlrequester.h"
#include "ippmsg.h"
#include "ippprintjob.h"
#include "ippprinter.h"
#include "minimime.h"
#include "pointer.h"

Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 44df94e

Please sign in to comment.