From 76ea78b9454d28fdd55f561b3444af17970fa0e9 Mon Sep 17 00:00:00 2001 From: Anton Thomasson Date: Sun, 25 Feb 2024 22:36:59 +0100 Subject: [PATCH] Add ability to save settings for future print jobs --- lib/configdir.h | 3 +++ lib/ippprinter.cpp | 5 +--- lib/ippprintjob.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++ lib/ippprintjob.h | 3 +++ utils/ippclient.cpp | 16 ++++++++++++- 5 files changed, 78 insertions(+), 5 deletions(-) create mode 100644 lib/configdir.h diff --git a/lib/configdir.h b/lib/configdir.h new file mode 100644 index 0000000..c252233 --- /dev/null +++ b/lib/configdir.h @@ -0,0 +1,3 @@ +#ifndef CONFIG_DIR +#define CONFIG_DIR std::string("/home/") + getenv("USER") + "/.ppm2pwg" +#endif diff --git a/lib/ippprinter.cpp b/lib/ippprinter.cpp index 70aebb5..1b0a6d8 100644 --- a/lib/ippprinter.cpp +++ b/lib/ippprinter.cpp @@ -1,6 +1,7 @@ #include "ippprinter.h" #include "curlrequester.h" #include "stringutils.h" +#include "configdir.h" #include IppPrinter::IppPrinter(std::string addr) : _addr(addr) @@ -506,10 +507,6 @@ IppMsg IppPrinter::_mkMsg(uint16_t opOrStatus, IppAttrs opAttrs, IppAttrs jobAtt return msg; } -#ifndef CONFIG_DIR -#define CONFIG_DIR std::string("/home/") + getenv("USER") + "/.ppm2pwg" -#endif - void IppPrinter::_applyOverrides() { try diff --git a/lib/ippprintjob.cpp b/lib/ippprintjob.cpp index ef7ed51..bf8f785 100644 --- a/lib/ippprintjob.cpp +++ b/lib/ippprintjob.cpp @@ -3,6 +3,8 @@ #include "curlrequester.h" #include "converter.h" #include "stringutils.h" +#include "configdir.h" +#include Error IppPrintJob::finalize(std::string inputFormat, int pages) { @@ -313,3 +315,57 @@ void IppPrintJob::adjustRasterSettings(int pages) } } } + +std::filesystem::path settings_dir() +{ + return std::filesystem::path(CONFIG_DIR) / "saved_settings"; +} + +void IppPrintJob::restoreSettings() +{ + if(_printerAttrs.has("printer-uuid")) + { + std::string uuid = _printerAttrs.get("printer-uuid"); + std::ifstream ifs = std::ifstream(settings_dir() / uuid, std::ios::in | std::ios::binary); + if(ifs) + { + Bytestream bts(ifs); + std::string errStr; + Json::object jsonObj = Json::parse(bts.getString(bts.size()), errStr).object_items(); + Json::object opJson = jsonObj["op-attrs"].object_items(); + Json::object jobJson = jsonObj["job-attrs"].object_items(); + IppAttrs savedOpSettings = IppAttrs::fromJSON(opJson); + IppAttrs savedJobSettings = IppAttrs::fromJSON(jobJson); + for(const auto& [name, attr] : savedOpSettings) + { + opAttrs.insert_or_assign(name, attr); + } + for(const auto& [name, attr] : savedJobSettings) + { + jobAttrs.insert_or_assign(name, attr); + } + } + } +} + +bool IppPrintJob::saveSettings() +{ + if(_printerAttrs.has("printer-uuid")) + { + std::string uuid = _printerAttrs.get("printer-uuid"); + std::filesystem::create_directories(settings_dir()); + std::ofstream ofs = std::ofstream(settings_dir() / uuid, std::ios::out | std::ios::binary); + Json::object json; + if(!opAttrs.empty()) + { + json["op-attrs"] = opAttrs.toJSON(); + } + if(!jobAttrs.empty()) + { + json["job-attrs"] = jobAttrs.toJSON(); + } + ofs << Json(json).dump(); + return true; + } + return false; +} diff --git a/lib/ippprintjob.h b/lib/ippprintjob.h index 6412289..34fb9db 100644 --- a/lib/ippprintjob.h +++ b/lib/ippprintjob.h @@ -60,6 +60,9 @@ class IppPrintJob Error finalize(std::string inputFormat, int pages=0); + void restoreSettings(); + bool saveSettings(); + IppAttrs opAttrs; IppAttrs jobAttrs; PrintParameters printParams; diff --git a/utils/ippclient.cpp b/utils/ippclient.cpp index ec5cfa2..00d46aa 100644 --- a/utils/ippclient.cpp +++ b/utils/ippclient.cpp @@ -180,6 +180,7 @@ int main(int argc, char** argv) int rightMargin; bool antiAlias; + bool save; int id; @@ -227,6 +228,7 @@ int main(int argc, char** argv) SwitchArg rightMarginOpt(rightMargin, {"-rm", "--right-margin"}, "Right margin (as per IPP)"); SwitchArg antiAliasOpt(antiAlias, {"-aa", "--antaialias"}, "Enable antialiasing in rasterization"); + SwitchArg saveOpt(save, {"--save"}, "Save options as local defaults for future jobs"); SwitchArg idOpt(id, {"--id"}, "Id of print job."); @@ -253,7 +255,7 @@ int main(int argc, char** argv) &formatOpt, &mimeTypeOpt, &mediaTypeOpt, &mediaSourceOpt, &outputBinOpt, &finishingsOpt, &marginOpt, &topMarginOpt, &bottomMarginOpt, &leftMarginOpt, &rightMarginOpt, - &antiAliasOpt}, + &antiAliasOpt, &saveOpt}, {&addrArg, &pdfArg}}}}); bool correctArgs = args.get_args(argc, argv); @@ -360,6 +362,10 @@ int main(int argc, char** argv) else if(args.subCommand() == "print") { IppPrintJob job = printer.createJob(); + if(!save) + { + job.restoreSettings(); + } if(oneStageOpt.isSet()) { @@ -470,6 +476,14 @@ int main(int argc, char** argv) nPages = poppler_document_get_n_pages(doc); } + if(save) + { + if(!job.saveSettings()) + { + std::cerr << "Could not save settings. Aborting." << std::endl; + return 1; + } + } error = printer.runJob(job, inFile, mimeType, nPages, verbose); if(error)