PhysicalPrinter : Implemented synchronizations from user printer profiles with "Print Host upload" information to the new physical printers

This commit is contained in:
YuSanka 2020-07-21 16:21:18 +02:00
parent f138978fe7
commit 6d28d68e4a
6 changed files with 138 additions and 29 deletions

View file

@ -1370,6 +1370,37 @@ const std::vector<std::string>& PhysicalPrinter::printer_options()
return s_opts;
}
const std::vector<std::string>& PhysicalPrinter::print_host_options()
{
static std::vector<std::string> s_opts;
if (s_opts.empty()) {
s_opts = {
"print_host",
"printhost_apikey",
"printhost_cafile"
};
}
return s_opts;
}
bool PhysicalPrinter::has_print_host_information(const PrinterPresetCollection& printer_presets)
{
for (const Preset& preset : printer_presets)
if (has_print_host_information(preset.config))
return true;
return false;
}
bool PhysicalPrinter::has_print_host_information(const DynamicPrintConfig& config)
{
for (const std::string& opt : print_host_options())
if (!config.opt_string(opt).empty())
return true;
return false;
}
const std::set<std::string>& PhysicalPrinter::get_preset_names() const
{
return preset_names;
@ -1532,42 +1563,69 @@ void PhysicalPrinterCollection::load_printers(const std::string& dir_path, const
// if there is saved user presets, contains information about "Print Host upload",
// Create default printers with this presets
// Throws an exception on error.
void PhysicalPrinterCollection::load_printers(const PrinterPresetCollection& printer_presets, std::string def_printer_name/* = ""*/)
// Note! "Print Host upload" options will be cleared after physical printer creations
void PhysicalPrinterCollection::load_printers_from_presets(PrinterPresetCollection& printer_presets, std::string def_printer_name)
{
if (def_printer_name.empty())
def_printer_name = "Printer";
int cnt=0;
std::string errors_cummulative;
// Store the loaded printers into a new vector
std::deque<PhysicalPrinter> printers_loaded;
for (const Preset& preset: printer_presets) {
const DynamicPrintConfig& config = preset.config;
if (!config.opt_string("print_host").empty() ||
!config.opt_string("printhost_apikey").empty() ||
!config.opt_string("printhost_cafile").empty() ) {
PhysicalPrinter printer((boost::format("%1% %2%") % def_printer_name % ++cnt ).str(), preset);
printer.loaded = true;
printers_loaded.emplace_back(printer);
for (Preset& preset: printer_presets) {
DynamicPrintConfig& config = preset.config;
const std::vector<std::string>& options = PhysicalPrinter::print_host_options();
save_printer(printer);
for(const std::string& option : options) {
if (!config.opt_string(option).empty()) {
// check if printer with those "Print Host upload" options already exist
PhysicalPrinter* existed_printer = find_printer_with_same_config(config);
if (existed_printer)
// just add preset for this printer
existed_printer->add_preset(preset.name);
else {
// create new printer from this preset
PhysicalPrinter printer((boost::format("%1% %2%") % def_printer_name % ++cnt ).str(), preset);
printer.loaded = true;
save_printer(printer);
}
// erase "Print Host upload" information from the preset
for (const std::string& opt : options)
config.opt_string(opt).clear();
// save changes for preset
preset.save();
// update those changes for edited preset if it's equal to the preset
Preset& edited = printer_presets.get_edited_preset();
if (preset.name == edited.name) {
for (const std::string& opt : options)
edited.config.opt_string(opt).clear();
}
break;
}
}
}
if (!errors_cummulative.empty())
throw std::runtime_error(errors_cummulative);
}
PhysicalPrinter* PhysicalPrinterCollection::find_printer( const std::string& name, bool first_visible_if_not_found)
{
PhysicalPrinter key(name);
auto it = this->find_printer_internal(name);
// Ensure that a temporary copy is returned if the preset found is currently selected.
return (it != m_printers.end() && it->name == key.name) ? &this->printer(it - m_printers.begin()) :
return (it != m_printers.end() && it->name == name) ? &this->printer(it - m_printers.begin()) :
first_visible_if_not_found ? &this->printer(0) : nullptr;
}
PhysicalPrinter* PhysicalPrinterCollection::find_printer_with_same_config(const DynamicPrintConfig& config)
{
for (const PhysicalPrinter& printer :*this) {
bool is_equal = true;
for (const std::string& opt : PhysicalPrinter::print_host_options())
if (is_equal && printer.config.opt_string(opt) != config.opt_string(opt))
is_equal = false;
if (is_equal)
return find_printer(printer.name);
}
return nullptr;
}
// Generate a file path from a profile name. Add the ".ini" suffix if it is missing.
std::string PhysicalPrinterCollection::path_from_name(const std::string& new_name) const
{

View file

@ -549,12 +549,15 @@ public:
// set of presets used with this physical printer
std::set<std::string> preset_names;
static std::string separator();
// Has this profile been loaded?
bool loaded = false;
static std::string separator();
static const std::vector<std::string>& printer_options();
static const std::vector<std::string>& print_host_options();
static bool has_print_host_information(const PrinterPresetCollection& printer_presets);
static bool has_print_host_information(const DynamicPrintConfig& config);
const std::set<std::string>& get_preset_names() const;
bool has_empty_config() const;
@ -626,7 +629,7 @@ public:
// Load ini files of the particular type from the provided directory path.
void load_printers(const std::string& dir_path, const std::string& subdir);
void load_printers(const PrinterPresetCollection &printer_presets, std::string def_printer_name = "");
void load_printers_from_presets(PrinterPresetCollection &printer_presets, std::string def_printer_name);
// Save the printer under a new name. If the name is different from the old one,
// a new printer is stored into the list of printers.
@ -704,6 +707,8 @@ private:
return const_cast<PhysicalPrinterCollection*>(this)->find_printer_internal(name);
}
PhysicalPrinter* find_printer_with_same_config( const DynamicPrintConfig &config);
// List of printers
// Use deque to force the container to allocate an object per each entry,
// so that the addresses of the presets don't change during resizing of the container.

View file

@ -201,7 +201,6 @@ void PresetBundle::load_presets(AppConfig &config, const std::string &preferred_
}
try {
this->physical_printers.load_printers(dir_user_presets, "physical_printer");
this->physical_printers.load_printers(this->printers);
} catch (const std::runtime_error &err) {
errors_cummulative += err.what();
}
@ -436,8 +435,7 @@ void PresetBundle::load_selections(AppConfig &config, const std::string &preferr
std::string initial_physical_printer_name = remove_ini_suffix(config.get("extras", "physical_printer"));
// Activate physical printer from the config
const PhysicalPrinter* initial_physical_printer = physical_printers.find_printer(initial_physical_printer_name);
if (initial_physical_printer)
if (!initial_physical_printer_name.empty())
physical_printers.select_printer_by_name(initial_physical_printer_name);
}