Skip to content

Commit

Permalink
Add ability to save settings for future print jobs
Browse files Browse the repository at this point in the history
  • Loading branch information
attah committed Feb 25, 2024
1 parent 16cf192 commit 76ea78b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 5 deletions.
3 changes: 3 additions & 0 deletions lib/configdir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#ifndef CONFIG_DIR
#define CONFIG_DIR std::string("/home/") + getenv("USER") + "/.ppm2pwg"
#endif
5 changes: 1 addition & 4 deletions lib/ippprinter.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "ippprinter.h"
#include "curlrequester.h"
#include "stringutils.h"
#include "configdir.h"
#include <filesystem>

IppPrinter::IppPrinter(std::string addr) : _addr(addr)
Expand Down Expand Up @@ -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
Expand Down
56 changes: 56 additions & 0 deletions lib/ippprintjob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include "curlrequester.h"
#include "converter.h"
#include "stringutils.h"
#include "configdir.h"
#include <filesystem>

Error IppPrintJob::finalize(std::string inputFormat, int pages)
{
Expand Down Expand Up @@ -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<std::string>("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<std::string>("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;
}
3 changes: 3 additions & 0 deletions lib/ippprintjob.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class IppPrintJob

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

void restoreSettings();
bool saveSettings();

IppAttrs opAttrs;
IppAttrs jobAttrs;
PrintParameters printParams;
Expand Down
16 changes: 15 additions & 1 deletion utils/ippclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ int main(int argc, char** argv)
int rightMargin;

bool antiAlias;
bool save;

int id;

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

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

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

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

0 comments on commit 76ea78b

Please sign in to comment.