Skip to content

Commit 76ea78b

Browse files
committed
Add ability to save settings for future print jobs
1 parent 16cf192 commit 76ea78b

File tree

5 files changed

+78
-5
lines changed

5 files changed

+78
-5
lines changed

lib/configdir.h

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#ifndef CONFIG_DIR
2+
#define CONFIG_DIR std::string("/home/") + getenv("USER") + "/.ppm2pwg"
3+
#endif

lib/ippprinter.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "ippprinter.h"
22
#include "curlrequester.h"
33
#include "stringutils.h"
4+
#include "configdir.h"
45
#include <filesystem>
56

67
IppPrinter::IppPrinter(std::string addr) : _addr(addr)
@@ -506,10 +507,6 @@ IppMsg IppPrinter::_mkMsg(uint16_t opOrStatus, IppAttrs opAttrs, IppAttrs jobAtt
506507
return msg;
507508
}
508509

509-
#ifndef CONFIG_DIR
510-
#define CONFIG_DIR std::string("/home/") + getenv("USER") + "/.ppm2pwg"
511-
#endif
512-
513510
void IppPrinter::_applyOverrides()
514511
{
515512
try

lib/ippprintjob.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "curlrequester.h"
44
#include "converter.h"
55
#include "stringutils.h"
6+
#include "configdir.h"
7+
#include <filesystem>
68

79
Error IppPrintJob::finalize(std::string inputFormat, int pages)
810
{
@@ -313,3 +315,57 @@ void IppPrintJob::adjustRasterSettings(int pages)
313315
}
314316
}
315317
}
318+
319+
std::filesystem::path settings_dir()
320+
{
321+
return std::filesystem::path(CONFIG_DIR) / "saved_settings";
322+
}
323+
324+
void IppPrintJob::restoreSettings()
325+
{
326+
if(_printerAttrs.has("printer-uuid"))
327+
{
328+
std::string uuid = _printerAttrs.get<std::string>("printer-uuid");
329+
std::ifstream ifs = std::ifstream(settings_dir() / uuid, std::ios::in | std::ios::binary);
330+
if(ifs)
331+
{
332+
Bytestream bts(ifs);
333+
std::string errStr;
334+
Json::object jsonObj = Json::parse(bts.getString(bts.size()), errStr).object_items();
335+
Json::object opJson = jsonObj["op-attrs"].object_items();
336+
Json::object jobJson = jsonObj["job-attrs"].object_items();
337+
IppAttrs savedOpSettings = IppAttrs::fromJSON(opJson);
338+
IppAttrs savedJobSettings = IppAttrs::fromJSON(jobJson);
339+
for(const auto& [name, attr] : savedOpSettings)
340+
{
341+
opAttrs.insert_or_assign(name, attr);
342+
}
343+
for(const auto& [name, attr] : savedJobSettings)
344+
{
345+
jobAttrs.insert_or_assign(name, attr);
346+
}
347+
}
348+
}
349+
}
350+
351+
bool IppPrintJob::saveSettings()
352+
{
353+
if(_printerAttrs.has("printer-uuid"))
354+
{
355+
std::string uuid = _printerAttrs.get<std::string>("printer-uuid");
356+
std::filesystem::create_directories(settings_dir());
357+
std::ofstream ofs = std::ofstream(settings_dir() / uuid, std::ios::out | std::ios::binary);
358+
Json::object json;
359+
if(!opAttrs.empty())
360+
{
361+
json["op-attrs"] = opAttrs.toJSON();
362+
}
363+
if(!jobAttrs.empty())
364+
{
365+
json["job-attrs"] = jobAttrs.toJSON();
366+
}
367+
ofs << Json(json).dump();
368+
return true;
369+
}
370+
return false;
371+
}

lib/ippprintjob.h

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class IppPrintJob
6060

6161
Error finalize(std::string inputFormat, int pages=0);
6262

63+
void restoreSettings();
64+
bool saveSettings();
65+
6366
IppAttrs opAttrs;
6467
IppAttrs jobAttrs;
6568
PrintParameters printParams;

utils/ippclient.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ int main(int argc, char** argv)
180180
int rightMargin;
181181

182182
bool antiAlias;
183+
bool save;
183184

184185
int id;
185186

@@ -227,6 +228,7 @@ int main(int argc, char** argv)
227228
SwitchArg<int> rightMarginOpt(rightMargin, {"-rm", "--right-margin"}, "Right margin (as per IPP)");
228229

229230
SwitchArg<bool> antiAliasOpt(antiAlias, {"-aa", "--antaialias"}, "Enable antialiasing in rasterization");
231+
SwitchArg<bool> saveOpt(save, {"--save"}, "Save options as local defaults for future jobs");
230232

231233
SwitchArg<int> idOpt(id, {"--id"}, "Id of print job.");
232234

@@ -253,7 +255,7 @@ int main(int argc, char** argv)
253255
&formatOpt, &mimeTypeOpt,
254256
&mediaTypeOpt, &mediaSourceOpt, &outputBinOpt, &finishingsOpt,
255257
&marginOpt, &topMarginOpt, &bottomMarginOpt, &leftMarginOpt, &rightMarginOpt,
256-
&antiAliasOpt},
258+
&antiAliasOpt, &saveOpt},
257259
{&addrArg, &pdfArg}}}});
258260

259261
bool correctArgs = args.get_args(argc, argv);
@@ -360,6 +362,10 @@ int main(int argc, char** argv)
360362
else if(args.subCommand() == "print")
361363
{
362364
IppPrintJob job = printer.createJob();
365+
if(!save)
366+
{
367+
job.restoreSettings();
368+
}
363369

364370
if(oneStageOpt.isSet())
365371
{
@@ -470,6 +476,14 @@ int main(int argc, char** argv)
470476
nPages = poppler_document_get_n_pages(doc);
471477
}
472478

479+
if(save)
480+
{
481+
if(!job.saveSettings())
482+
{
483+
std::cerr << "Could not save settings. Aborting." << std::endl;
484+
return 1;
485+
}
486+
}
473487
error = printer.runJob(job, inFile, mimeType, nPages, verbose);
474488

475489
if(error)

0 commit comments

Comments
 (0)