1 #include <cassert>
2 
3 #include "Exception.hpp"
4 #include "Preset.hpp"
5 #include "AppConfig.hpp"
6 
7 #ifdef _MSC_VER
8     #define WIN32_LEAN_AND_MEAN
9     #define NOMINMAX
10     #include <Windows.h>
11 #endif /* _MSC_VER */
12 
13 // instead of #include "slic3r/GUI/I18N.hpp" :
14 #ifndef L
15 // !!! If you needed to translate some string,
16 // !!! please use _L(string)
17 // !!! _() - is a standard wxWidgets macro to translate
18 // !!! L() is used only for marking localizable string
19 // !!! It will be used in "xgettext" to create a Locating Message Catalog.
20 #define L(s) s
21 #endif /* L */
22 
23 #include <algorithm>
24 #include <fstream>
25 #include <stdexcept>
26 #include <unordered_map>
27 #include <boost/format.hpp>
28 #include <boost/filesystem.hpp>
29 #include <boost/filesystem/fstream.hpp>
30 #include <boost/algorithm/string.hpp>
31 #include <boost/algorithm/string/predicate.hpp>
32 
33 #include <boost/nowide/cenv.hpp>
34 #include <boost/nowide/convert.hpp>
35 #include <boost/nowide/cstdio.hpp>
36 #include <boost/nowide/fstream.hpp>
37 #include <boost/property_tree/ini_parser.hpp>
38 #include <boost/property_tree/ptree.hpp>
39 #include <boost/locale.hpp>
40 #include <boost/log/trivial.hpp>
41 
42 #include "libslic3r.h"
43 #include "Utils.hpp"
44 #include "PlaceholderParser.hpp"
45 
46 using boost::property_tree::ptree;
47 
48 namespace Slic3r {
49 
guess_config_file_type(const ptree & tree)50 ConfigFileType guess_config_file_type(const ptree &tree)
51 {
52     size_t app_config   = 0;
53     size_t bundle       = 0;
54     size_t config       = 0;
55     for (const ptree::value_type &v : tree) {
56         if (v.second.empty()) {
57             if (v.first == "background_processing" ||
58                 v.first == "last_output_path" ||
59                 v.first == "no_controller" ||
60                 v.first == "no_defaults")
61                 ++ app_config;
62             else if (v.first == "nozzle_diameter" ||
63                 v.first == "filament_diameter")
64                 ++ config;
65         } else if (boost::algorithm::starts_with(v.first, "print:") ||
66             boost::algorithm::starts_with(v.first, "filament:") ||
67             boost::algorithm::starts_with(v.first, "printer:") ||
68             v.first == "settings")
69             ++ bundle;
70         else if (v.first == "presets") {
71             ++ app_config;
72             ++ bundle;
73         } else if (v.first == "recent") {
74             for (auto &kvp : v.second)
75                 if (kvp.first == "config_directory" || kvp.first == "skein_directory")
76                     ++ app_config;
77         }
78     }
79     return (app_config > bundle && app_config > config) ? CONFIG_FILE_TYPE_APP_CONFIG :
80            (bundle > config) ? CONFIG_FILE_TYPE_CONFIG_BUNDLE : CONFIG_FILE_TYPE_CONFIG;
81 }
82 
83 
from_ini(const boost::filesystem::path & path,bool load_all)84 VendorProfile VendorProfile::from_ini(const boost::filesystem::path &path, bool load_all)
85 {
86     ptree tree;
87     boost::filesystem::ifstream ifs(path);
88     boost::property_tree::read_ini(ifs, tree);
89     return VendorProfile::from_ini(tree, path, load_all);
90 }
91 
92 static const std::unordered_map<std::string, std::string> pre_family_model_map {{
93     { "MK3",        "MK3" },
94     { "MK3MMU2",    "MK3" },
95     { "MK2.5",      "MK2.5" },
96     { "MK2.5MMU2",  "MK2.5" },
97     { "MK2S",       "MK2" },
98     { "MK2SMM",     "MK2" },
99     { "SL1",        "SL1" },
100 }};
101 
from_ini(const ptree & tree,const boost::filesystem::path & path,bool load_all)102 VendorProfile VendorProfile::from_ini(const ptree &tree, const boost::filesystem::path &path, bool load_all)
103 {
104     static const std::string printer_model_key = "printer_model:";
105     static const std::string filaments_section = "default_filaments";
106     static const std::string materials_section = "default_sla_materials";
107 
108     const std::string id = path.stem().string();
109 
110     if (! boost::filesystem::exists(path)) {
111         throw Slic3r::RuntimeError((boost::format("Cannot load Vendor Config Bundle `%1%`: File not found: `%2%`.") % id % path).str());
112     }
113 
114     VendorProfile res(id);
115 
116     // Helper to get compulsory fields
117     auto get_or_throw = [&](const ptree &tree, const std::string &key) -> ptree::const_assoc_iterator
118     {
119         auto res = tree.find(key);
120         if (res == tree.not_found()) {
121             throw Slic3r::RuntimeError((boost::format("Vendor Config Bundle `%1%` is not valid: Missing secion or key: `%2%`.") % id % key).str());
122         }
123         return res;
124     };
125 
126     // Load the header
127     const auto &vendor_section = get_or_throw(tree, "vendor")->second;
128     res.name = get_or_throw(vendor_section, "name")->second.data();
129 
130     auto config_version_str = get_or_throw(vendor_section, "config_version")->second.data();
131     auto config_version = Semver::parse(config_version_str);
132     if (! config_version) {
133         throw Slic3r::RuntimeError((boost::format("Vendor Config Bundle `%1%` is not valid: Cannot parse config_version: `%2%`.") % id % config_version_str).str());
134     } else {
135         res.config_version = std::move(*config_version);
136     }
137 
138     // Load URLs
139     const auto config_update_url = vendor_section.find("config_update_url");
140     if (config_update_url != vendor_section.not_found()) {
141         res.config_update_url = config_update_url->second.data();
142     }
143 
144     const auto changelog_url = vendor_section.find("changelog_url");
145     if (changelog_url != vendor_section.not_found()) {
146         res.changelog_url = changelog_url->second.data();
147     }
148 
149     if (! load_all) {
150         return res;
151     }
152 
153     // Load printer models
154     for (auto &section : tree) {
155         if (boost::starts_with(section.first, printer_model_key)) {
156             VendorProfile::PrinterModel model;
157             model.id = section.first.substr(printer_model_key.size());
158             model.name = section.second.get<std::string>("name", model.id);
159 
160             const char *technology_fallback = boost::algorithm::starts_with(model.id, "SL") ? "SLA" : "FFF";
161 
162             auto technology_field = section.second.get<std::string>("technology", technology_fallback);
163             if (! ConfigOptionEnum<PrinterTechnology>::from_string(technology_field, model.technology)) {
164                 BOOST_LOG_TRIVIAL(error) << boost::format("Vendor bundle: `%1%`: Invalid printer technology field: `%2%`") % id % technology_field;
165                 model.technology = ptFFF;
166             }
167 
168             model.family = section.second.get<std::string>("family", std::string());
169             if (model.family.empty() && res.name == "Prusa Research") {
170                 // If no family is specified, it can be inferred for known printers
171                 const auto from_pre_map = pre_family_model_map.find(model.id);
172                 if (from_pre_map != pre_family_model_map.end()) { model.family = from_pre_map->second; }
173             }
174 #if 0
175             // Remove SLA printers from the initial alpha.
176             if (model.technology == ptSLA)
177                 continue;
178 #endif
179             section.second.get<std::string>("variants", "");
180             const auto variants_field = section.second.get<std::string>("variants", "");
181             std::vector<std::string> variants;
182             if (Slic3r::unescape_strings_cstyle(variants_field, variants)) {
183                 for (const std::string &variant_name : variants) {
184                     if (model.variant(variant_name) == nullptr)
185                         model.variants.emplace_back(VendorProfile::PrinterVariant(variant_name));
186                 }
187             } else {
188                 BOOST_LOG_TRIVIAL(error) << boost::format("Vendor bundle: `%1%`: Malformed variants field: `%2%`") % id % variants_field;
189             }
190             auto default_materials_field = section.second.get<std::string>("default_materials", "");
191             if (default_materials_field.empty())
192             	default_materials_field = section.second.get<std::string>("default_filaments", "");
193             if (Slic3r::unescape_strings_cstyle(default_materials_field, model.default_materials)) {
194             	Slic3r::sort_remove_duplicates(model.default_materials);
195             	if (! model.default_materials.empty() && model.default_materials.front().empty())
196             		// An empty material was inserted into the list of default materials. Remove it.
197             		model.default_materials.erase(model.default_materials.begin());
198             } else {
199                 BOOST_LOG_TRIVIAL(error) << boost::format("Vendor bundle: `%1%`: Malformed default_materials field: `%2%`") % id % default_materials_field;
200             }
201             model.bed_model   = section.second.get<std::string>("bed_model", "");
202             model.bed_texture = section.second.get<std::string>("bed_texture", "");
203             if (! model.id.empty() && ! model.variants.empty())
204                 res.models.push_back(std::move(model));
205         }
206     }
207 
208     // Load filaments and sla materials to be installed by default
209     const auto filaments = tree.find(filaments_section);
210     if (filaments != tree.not_found()) {
211         for (auto &pair : filaments->second) {
212             if (pair.second.data() == "1") {
213                 res.default_filaments.insert(pair.first);
214             }
215         }
216     }
217     const auto materials = tree.find(materials_section);
218     if (materials != tree.not_found()) {
219         for (auto &pair : materials->second) {
220             if (pair.second.data() == "1") {
221                 res.default_sla_materials.insert(pair.first);
222             }
223         }
224     }
225 
226     return res;
227 }
228 
families() const229 std::vector<std::string> VendorProfile::families() const
230 {
231     std::vector<std::string> res;
232     unsigned num_familiies = 0;
233 
234     for (auto &model : models) {
235         if (std::find(res.begin(), res.end(), model.family) == res.end()) {
236             res.push_back(model.family);
237             num_familiies++;
238         }
239     }
240 
241     return res;
242 }
243 
244 // Suffix to be added to a modified preset name in the combo box.
245 static std::string g_suffix_modified = " (modified)";
suffix_modified()246 const std::string& Preset::suffix_modified()
247 {
248     return g_suffix_modified;
249 }
250 
update_suffix_modified(const std::string & new_suffix_modified)251 void Preset::update_suffix_modified(const std::string& new_suffix_modified)
252 {
253     g_suffix_modified = new_suffix_modified;
254 }
255 // Remove an optional "(modified)" suffix from a name.
256 // This converts a UI name to a unique preset identifier.
remove_suffix_modified(const std::string & name)257 std::string Preset::remove_suffix_modified(const std::string &name)
258 {
259     return boost::algorithm::ends_with(name, g_suffix_modified) ?
260         name.substr(0, name.size() - g_suffix_modified.size()) :
261         name;
262 }
263 
264 // Update new extruder fields at the printer profile.
normalize(DynamicPrintConfig & config)265 void Preset::normalize(DynamicPrintConfig &config)
266 {
267     auto *nozzle_diameter = dynamic_cast<const ConfigOptionFloats*>(config.option("nozzle_diameter"));
268     if (nozzle_diameter != nullptr)
269         // Loaded the FFF Printer settings. Verify, that all extruder dependent values have enough values.
270         config.set_num_extruders((unsigned int)nozzle_diameter->values.size());
271     if (config.option("filament_diameter") != nullptr) {
272         // This config contains single or multiple filament presets.
273         // Ensure that the filament preset vector options contain the correct number of values.
274         size_t n = (nozzle_diameter == nullptr) ? 1 : nozzle_diameter->values.size();
275         const auto &defaults = FullPrintConfig::defaults();
276         for (const std::string &key : Preset::filament_options()) {
277             if (key == "compatible_prints" || key == "compatible_printers")
278                 continue;
279             auto *opt = config.option(key, false);
280             /*assert(opt != nullptr);
281             assert(opt->is_vector());*/
282             if (opt != nullptr && opt->is_vector())
283                 static_cast<ConfigOptionVectorBase*>(opt)->resize(n, defaults.option(key));
284         }
285         // The following keys are mandatory for the UI, but they are not part of FullPrintConfig, therefore they are handled separately.
286         for (const std::string &key : { "filament_settings_id" }) {
287             auto *opt = config.option(key, false);
288             assert(opt == nullptr || opt->type() == coStrings);
289             if (opt != nullptr && opt->type() == coStrings)
290                 static_cast<ConfigOptionStrings*>(opt)->values.resize(n, std::string());
291         }
292     }
293 }
294 
remove_invalid_keys(DynamicPrintConfig & config,const DynamicPrintConfig & default_config)295 std::string Preset::remove_invalid_keys(DynamicPrintConfig &config, const DynamicPrintConfig &default_config)
296 {
297     std::string incorrect_keys;
298     for (const std::string &key : config.keys())
299         if (! default_config.has(key)) {
300             if (incorrect_keys.empty())
301                 incorrect_keys = key;
302             else {
303                 incorrect_keys += ", ";
304                 incorrect_keys += key;
305             }
306             config.erase(key);
307         }
308     return incorrect_keys;
309 }
310 
save()311 void Preset::save()
312 {
313     this->config.save(this->file);
314 }
315 
316 // Return a label of this preset, consisting of a name and a "(modified)" suffix, if this preset is dirty.
label() const317 std::string Preset::label() const
318 {
319     return this->name + (this->is_dirty ? g_suffix_modified : "");
320 }
321 
is_compatible_with_print(const PresetWithVendorProfile & preset,const PresetWithVendorProfile & active_print,const PresetWithVendorProfile & active_printer)322 bool is_compatible_with_print(const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_print, const PresetWithVendorProfile &active_printer)
323 {
324 	if (preset.vendor != nullptr && preset.vendor != active_printer.vendor)
325 		// The current profile has a vendor assigned and it is different from the active print's vendor.
326 		return false;
327     auto &condition             = preset.preset.compatible_prints_condition();
328     auto *compatible_prints     = dynamic_cast<const ConfigOptionStrings*>(preset.preset.config.option("compatible_prints"));
329     bool  has_compatible_prints = compatible_prints != nullptr && ! compatible_prints->values.empty();
330     if (! has_compatible_prints && ! condition.empty()) {
331         try {
332             return PlaceholderParser::evaluate_boolean_expression(condition, active_print.preset.config);
333         } catch (const std::runtime_error &err) {
334             //FIXME in case of an error, return "compatible with everything".
335             printf("Preset::is_compatible_with_print - parsing error of compatible_prints_condition %s:\n%s\n", active_print.preset.name.c_str(), err.what());
336             return true;
337         }
338     }
339     return preset.preset.is_default || active_print.preset.name.empty() || ! has_compatible_prints ||
340         std::find(compatible_prints->values.begin(), compatible_prints->values.end(), active_print.preset.name) !=
341             compatible_prints->values.end();
342 }
343 
is_compatible_with_printer(const PresetWithVendorProfile & preset,const PresetWithVendorProfile & active_printer,const DynamicPrintConfig * extra_config)344 bool is_compatible_with_printer(const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_printer, const DynamicPrintConfig *extra_config)
345 {
346 	if (preset.vendor != nullptr && preset.vendor != active_printer.vendor)
347 		// The current profile has a vendor assigned and it is different from the active print's vendor.
348 		return false;
349     auto &condition               = preset.preset.compatible_printers_condition();
350     auto *compatible_printers     = dynamic_cast<const ConfigOptionStrings*>(preset.preset.config.option("compatible_printers"));
351     bool  has_compatible_printers = compatible_printers != nullptr && ! compatible_printers->values.empty();
352     if (! has_compatible_printers && ! condition.empty()) {
353         try {
354             return PlaceholderParser::evaluate_boolean_expression(condition, active_printer.preset.config, extra_config);
355         } catch (const std::runtime_error &err) {
356             //FIXME in case of an error, return "compatible with everything".
357             printf("Preset::is_compatible_with_printer - parsing error of compatible_printers_condition %s:\n%s\n", active_printer.preset.name.c_str(), err.what());
358             return true;
359         }
360     }
361     return preset.preset.is_default || active_printer.preset.name.empty() || ! has_compatible_printers ||
362         std::find(compatible_printers->values.begin(), compatible_printers->values.end(), active_printer.preset.name) !=
363             compatible_printers->values.end();
364 }
365 
is_compatible_with_printer(const PresetWithVendorProfile & preset,const PresetWithVendorProfile & active_printer)366 bool is_compatible_with_printer(const PresetWithVendorProfile &preset, const PresetWithVendorProfile &active_printer)
367 {
368     DynamicPrintConfig config;
369     config.set_key_value("printer_preset", new ConfigOptionString(active_printer.preset.name));
370     const ConfigOption *opt = active_printer.preset.config.option("nozzle_diameter");
371     if (opt)
372         config.set_key_value("num_extruders", new ConfigOptionInt((int)static_cast<const ConfigOptionFloats*>(opt)->values.size()));
373     return is_compatible_with_printer(preset, active_printer, &config);
374 }
375 
set_visible_from_appconfig(const AppConfig & app_config)376 void Preset::set_visible_from_appconfig(const AppConfig &app_config)
377 {
378     if (vendor == nullptr) { return; }
379 
380     if (type == TYPE_PRINTER) {
381         const std::string &model = config.opt_string("printer_model");
382         const std::string &variant = config.opt_string("printer_variant");
383         if (model.empty() || variant.empty())
384         	return;
385         is_visible = app_config.get_variant(vendor->id, model, variant);
386     } else if (type == TYPE_FILAMENT || type == TYPE_SLA_MATERIAL) {
387     	const std::string &section_name = (type == TYPE_FILAMENT) ? AppConfig::SECTION_FILAMENTS : AppConfig::SECTION_MATERIALS;
388     	if (app_config.has_section(section_name)) {
389     		// Check whether this profile is marked as "installed" in PrusaSlicer.ini,
390     		// or whether a profile is marked as "installed", which this profile may have been renamed from.
391 	    	const std::map<std::string, std::string> &installed = app_config.get_section(section_name);
392 	    	auto has = [&installed](const std::string &name) {
393 	    		auto it = installed.find(name);
394 				return it != installed.end() && ! it->second.empty();
395 	    	};
396 	    	is_visible = has(this->name);
397 	    	for (auto it = this->renamed_from.begin(); ! is_visible && it != this->renamed_from.end(); ++ it)
398 	    		is_visible = has(*it);
399 	    }
400     }
401 }
402 
print_options()403 const std::vector<std::string>& Preset::print_options()
404 {
405     static std::vector<std::string> s_opts {
406         "layer_height", "first_layer_height", "perimeters", "spiral_vase", "slice_closing_radius",
407         "top_solid_layers", "top_solid_min_thickness", "bottom_solid_layers", "bottom_solid_min_thickness",
408         "extra_perimeters", "ensure_vertical_shell_thickness", "avoid_crossing_perimeters", "thin_walls", "overhangs",
409         "seam_position", "external_perimeters_first", "fill_density", "fill_pattern", "top_fill_pattern", "bottom_fill_pattern",
410         "infill_every_layers", "infill_only_where_needed", "solid_infill_every_layers", "fill_angle", "bridge_angle",
411         "solid_infill_below_area", "only_retract_when_crossing_perimeters", "infill_first",
412     	"ironing", "ironing_type", "ironing_flowrate", "ironing_speed", "ironing_spacing",
413         "max_print_speed", "max_volumetric_speed", "avoid_crossing_perimeters_max_detour",
414 #ifdef HAS_PRESSURE_EQUALIZER
415         "max_volumetric_extrusion_rate_slope_positive", "max_volumetric_extrusion_rate_slope_negative",
416 #endif /* HAS_PRESSURE_EQUALIZER */
417         "perimeter_speed", "small_perimeter_speed", "external_perimeter_speed", "infill_speed", "solid_infill_speed",
418         "top_solid_infill_speed", "support_material_speed", "support_material_xy_spacing", "support_material_interface_speed",
419         "bridge_speed", "gap_fill_speed", "travel_speed", "first_layer_speed", "perimeter_acceleration", "infill_acceleration",
420         "bridge_acceleration", "first_layer_acceleration", "default_acceleration", "skirts", "skirt_distance", "skirt_height", "draft_shield",
421         "min_skirt_length", "brim_width", "support_material", "support_material_auto", "support_material_threshold", "support_material_enforce_layers",
422         "raft_layers", "support_material_pattern", "support_material_with_sheath", "support_material_spacing",
423         "support_material_synchronize_layers", "support_material_angle", "support_material_interface_layers",
424         "support_material_interface_spacing", "support_material_interface_contact_loops", "support_material_contact_distance",
425         "support_material_buildplate_only", "dont_support_bridges", "notes", "complete_objects", "extruder_clearance_radius",
426         "extruder_clearance_height", "gcode_comments", "gcode_label_objects", "output_filename_format", "post_process", "perimeter_extruder",
427         "infill_extruder", "solid_infill_extruder", "support_material_extruder", "support_material_interface_extruder",
428         "ooze_prevention", "standby_temperature_delta", "interface_shells", "extrusion_width", "first_layer_extrusion_width",
429         "perimeter_extrusion_width", "external_perimeter_extrusion_width", "infill_extrusion_width", "solid_infill_extrusion_width",
430         "top_infill_extrusion_width", "support_material_extrusion_width", "infill_overlap", "infill_anchor", "infill_anchor_max", "bridge_flow_ratio", "clip_multipart_objects",
431         "elefant_foot_compensation", "xy_size_compensation", "threads", "resolution", "wipe_tower", "wipe_tower_x", "wipe_tower_y",
432         "wipe_tower_width", "wipe_tower_rotation_angle", "wipe_tower_bridging", "single_extruder_multi_material_priming",
433         "wipe_tower_no_sparse_layers", "compatible_printers", "compatible_printers_condition", "inherits"
434     };
435     return s_opts;
436 }
437 
filament_options()438 const std::vector<std::string>& Preset::filament_options()
439 {
440     static std::vector<std::string> s_opts {
441         "filament_colour", "filament_diameter", "filament_type", "filament_soluble", "filament_notes", "filament_max_volumetric_speed",
442         "extrusion_multiplier", "filament_density", "filament_cost", "filament_spool_weight", "filament_loading_speed", "filament_loading_speed_start", "filament_load_time",
443         "filament_unloading_speed", "filament_unloading_speed_start", "filament_unload_time", "filament_toolchange_delay", "filament_cooling_moves",
444         "filament_cooling_initial_speed", "filament_cooling_final_speed", "filament_ramming_parameters", "filament_minimal_purge_on_wipe_tower",
445         "temperature", "first_layer_temperature", "bed_temperature", "first_layer_bed_temperature", "fan_always_on", "cooling", "min_fan_speed",
446         "max_fan_speed", "bridge_fan_speed", "disable_fan_first_layers", "full_fan_speed_layer", "fan_below_layer_time", "slowdown_below_layer_time", "min_print_speed",
447         "start_filament_gcode", "end_filament_gcode",
448         // Retract overrides
449         "filament_retract_length", "filament_retract_lift", "filament_retract_lift_above", "filament_retract_lift_below", "filament_retract_speed", "filament_deretract_speed", "filament_retract_restart_extra", "filament_retract_before_travel",
450         "filament_retract_layer_change", "filament_wipe", "filament_retract_before_wipe",
451         // Profile compatibility
452         "filament_vendor", "compatible_prints", "compatible_prints_condition", "compatible_printers", "compatible_printers_condition", "inherits"
453     };
454     return s_opts;
455 }
456 
machine_limits_options()457 const std::vector<std::string>& Preset::machine_limits_options()
458 {
459     static std::vector<std::string> s_opts;
460     if (s_opts.empty()) {
461         s_opts = {
462 			"machine_max_acceleration_extruding", "machine_max_acceleration_retracting",
463 		    "machine_max_acceleration_x", "machine_max_acceleration_y", "machine_max_acceleration_z", "machine_max_acceleration_e",
464 		    "machine_max_feedrate_x", "machine_max_feedrate_y", "machine_max_feedrate_z", "machine_max_feedrate_e",
465 		    "machine_min_extruding_rate", "machine_min_travel_rate",
466 		    "machine_max_jerk_x", "machine_max_jerk_y", "machine_max_jerk_z", "machine_max_jerk_e",
467 		};
468 	}
469 	return s_opts;
470 }
471 
printer_options()472 const std::vector<std::string>& Preset::printer_options()
473 {
474     static std::vector<std::string> s_opts;
475     if (s_opts.empty()) {
476         s_opts = {
477             "printer_technology",
478             "bed_shape", "bed_custom_texture", "bed_custom_model", "z_offset", "gcode_flavor", "use_relative_e_distances",
479             "use_firmware_retraction", "use_volumetric_e", "variable_layer_height",
480             //FIXME the print host keys are left here just for conversion from the Printer preset to Physical Printer preset.
481             "host_type", "print_host", "printhost_apikey", "printhost_cafile",
482             "single_extruder_multi_material", "start_gcode", "end_gcode", "before_layer_gcode", "layer_gcode", "toolchange_gcode",
483             "color_change_gcode", "pause_print_gcode", "template_custom_gcode",
484             "between_objects_gcode", "printer_vendor", "printer_model", "printer_variant", "printer_notes", "cooling_tube_retraction",
485             "cooling_tube_length", "high_current_on_filament_swap", "parking_pos_retraction", "extra_loading_move", "max_print_height",
486             "default_print_profile", "inherits",
487             "remaining_times", "silent_mode",
488             "machine_limits_usage", "thumbnails"
489         };
490         s_opts.insert(s_opts.end(), Preset::machine_limits_options().begin(), Preset::machine_limits_options().end());
491         s_opts.insert(s_opts.end(), Preset::nozzle_options().begin(), Preset::nozzle_options().end());
492     }
493     return s_opts;
494 }
495 
496 // The following nozzle options of a printer profile will be adjusted to match the size
497 // of the nozzle_diameter vector.
nozzle_options()498 const std::vector<std::string>& Preset::nozzle_options()
499 {
500 	return print_config_def.extruder_option_keys();
501 }
502 
sla_print_options()503 const std::vector<std::string>& Preset::sla_print_options()
504 {
505     static std::vector<std::string> s_opts;
506     if (s_opts.empty()) {
507         s_opts = {
508             "layer_height",
509             "faded_layers",
510             "supports_enable",
511             "support_head_front_diameter",
512             "support_head_penetration",
513             "support_head_width",
514             "support_pillar_diameter",
515             "support_small_pillar_diameter_percent",
516             "support_max_bridges_on_pillar",
517             "support_pillar_connection_mode",
518             "support_buildplate_only",
519             "support_pillar_widening_factor",
520             "support_base_diameter",
521             "support_base_height",
522             "support_base_safety_distance",
523             "support_critical_angle",
524             "support_max_bridge_length",
525             "support_max_pillar_link_distance",
526             "support_object_elevation",
527             "support_points_density_relative",
528             "support_points_minimal_distance",
529             "slice_closing_radius",
530             "pad_enable",
531             "pad_wall_thickness",
532             "pad_wall_height",
533             "pad_brim_size",
534             "pad_max_merge_distance",
535             // "pad_edge_radius",
536             "pad_wall_slope",
537             "pad_object_gap",
538             "pad_around_object",
539             "pad_around_object_everywhere",
540             "pad_object_connector_stride",
541             "pad_object_connector_width",
542             "pad_object_connector_penetration",
543             "hollowing_enable",
544             "hollowing_min_thickness",
545             "hollowing_quality",
546             "hollowing_closing_distance",
547             "output_filename_format",
548             "default_sla_print_profile",
549             "compatible_printers",
550             "compatible_printers_condition",
551             "inherits"
552         };
553     }
554     return s_opts;
555 }
556 
sla_material_options()557 const std::vector<std::string>& Preset::sla_material_options()
558 {
559     static std::vector<std::string> s_opts;
560     if (s_opts.empty()) {
561         s_opts = {
562             "material_type",
563             "initial_layer_height",
564             "bottle_cost",
565             "bottle_volume",
566             "bottle_weight",
567             "material_density",
568             "exposure_time",
569             "initial_exposure_time",
570             "material_correction",
571             "material_notes",
572             "material_vendor",
573             "default_sla_material_profile",
574             "compatible_prints", "compatible_prints_condition",
575             "compatible_printers", "compatible_printers_condition", "inherits"
576         };
577     }
578     return s_opts;
579 }
580 
sla_printer_options()581 const std::vector<std::string>& Preset::sla_printer_options()
582 {
583     static std::vector<std::string> s_opts;
584     if (s_opts.empty()) {
585         s_opts = {
586             "printer_technology",
587             "bed_shape", "bed_custom_texture", "bed_custom_model", "max_print_height",
588             "display_width", "display_height", "display_pixels_x", "display_pixels_y",
589             "display_mirror_x", "display_mirror_y",
590             "display_orientation",
591             "fast_tilt_time", "slow_tilt_time", "area_fill",
592             "relative_correction",
593             "absolute_correction",
594             "elefant_foot_compensation",
595             "elefant_foot_min_width",
596             "gamma_correction",
597             "min_exposure_time", "max_exposure_time",
598             "min_initial_exposure_time", "max_initial_exposure_time",
599             //FIXME the print host keys are left here just for conversion from the Printer preset to Physical Printer preset.
600             "print_host", "printhost_apikey", "printhost_cafile",
601             "printer_notes",
602             "inherits"
603         };
604     }
605     return s_opts;
606 }
607 
PresetCollection(Preset::Type type,const std::vector<std::string> & keys,const Slic3r::StaticPrintConfig & defaults,const std::string & default_name)608 PresetCollection::PresetCollection(Preset::Type type, const std::vector<std::string> &keys, const Slic3r::StaticPrintConfig &defaults, const std::string &default_name) :
609     m_type(type),
610     m_edited_preset(type, "", false),
611     m_idx_selected(0)
612 {
613     // Insert just the default preset.
614     this->add_default_preset(keys, defaults, default_name);
615     m_edited_preset.config.apply(m_presets.front().config);
616 }
617 
~PresetCollection()618 PresetCollection::~PresetCollection()
619 {
620 }
621 
reset(bool delete_files)622 void PresetCollection::reset(bool delete_files)
623 {
624     if (m_presets.size() > m_num_default_presets) {
625         if (delete_files) {
626             // Erase the preset files.
627             for (Preset &preset : m_presets)
628                 if (! preset.is_default && ! preset.is_external && ! preset.is_system)
629                     boost::nowide::remove(preset.file.c_str());
630         }
631         // Don't use m_presets.resize() here as it requires a default constructor for Preset.
632         m_presets.erase(m_presets.begin() + m_num_default_presets, m_presets.end());
633         this->select_preset(0);
634     }
635     m_map_alias_to_profile_name.clear();
636     m_map_system_profile_renamed.clear();
637 }
638 
add_default_preset(const std::vector<std::string> & keys,const Slic3r::StaticPrintConfig & defaults,const std::string & preset_name)639 void PresetCollection::add_default_preset(const std::vector<std::string> &keys, const Slic3r::StaticPrintConfig &defaults, const std::string &preset_name)
640 {
641     // Insert just the default preset.
642     m_presets.emplace_back(Preset(this->type(), preset_name, true));
643     m_presets.back().config.apply_only(defaults, keys.empty() ? defaults.keys() : keys);
644     m_presets.back().loaded = true;
645     ++ m_num_default_presets;
646 }
647 
648 // Load all presets found in dir_path.
649 // Throws an exception on error.
load_presets(const std::string & dir_path,const std::string & subdir,PresetsConfigSubstitutions & substitutions,ForwardCompatibilitySubstitutionRule substitution_rule)650 void PresetCollection::load_presets(
651     const std::string &dir_path, const std::string &subdir,
652     PresetsConfigSubstitutions& substitutions, ForwardCompatibilitySubstitutionRule substitution_rule)
653 {
654     // Don't use boost::filesystem::canonical() on Windows, it is broken in regard to reparse points,
655     // see https://github.com/prusa3d/PrusaSlicer/issues/732
656     boost::filesystem::path dir = boost::filesystem::absolute(boost::filesystem::path(dir_path) / subdir).make_preferred();
657     m_dir_path = dir.string();
658     std::string errors_cummulative;
659     // Store the loaded presets into a new vector, otherwise the binary search for already existing presets would be broken.
660     // (see the "Preset already present, not loading" message).
661     std::deque<Preset> presets_loaded;
662     for (auto &dir_entry : boost::filesystem::directory_iterator(dir))
663         if (Slic3r::is_ini_file(dir_entry)) {
664             std::string name = dir_entry.path().filename().string();
665             // Remove the .ini suffix.
666             name.erase(name.size() - 4);
667             if (this->find_preset(name, false)) {
668                 // This happens when there's is a preset (most likely legacy one) with the same name as a system preset
669                 // that's already been loaded from a bundle.
670                 BOOST_LOG_TRIVIAL(warning) << "Preset already present, not loading: " << name;
671                 continue;
672             }
673             try {
674                 Preset preset(m_type, name, false);
675                 preset.file = dir_entry.path().string();
676                 // Load the preset file, apply preset values on top of defaults.
677                 try {
678                     DynamicPrintConfig config;
679                     ConfigSubstitutions config_substitutions = config.load_from_ini(preset.file, substitution_rule);
680                     if (! config_substitutions.empty())
681                         substitutions.push_back({ preset.name, m_type, PresetConfigSubstitutions::Source::UserFile, preset.file, std::move(config_substitutions) });
682                     // Find a default preset for the config. The PrintPresetCollection provides different default preset based on the "printer_technology" field.
683                     const Preset &default_preset = this->default_preset_for(config);
684                     preset.config = default_preset.config;
685                     preset.config.apply(std::move(config));
686                     Preset::normalize(preset.config);
687                     // Report configuration fields, which are misplaced into a wrong group.
688                     std::string incorrect_keys = Preset::remove_invalid_keys(config, default_preset.config);
689                     if (! incorrect_keys.empty())
690                         BOOST_LOG_TRIVIAL(error) << "Error in a preset file: The preset \"" <<
691                             preset.file << "\" contains the following incorrect keys: " << incorrect_keys << ", which were removed";
692                     preset.loaded = true;
693                 } catch (const std::ifstream::failure &err) {
694                     throw Slic3r::RuntimeError(std::string("The selected preset cannot be loaded: ") + preset.file + "\n\tReason: " + err.what());
695                 } catch (const std::runtime_error &err) {
696                     throw Slic3r::RuntimeError(std::string("Failed loading the preset file: ") + preset.file + "\n\tReason: " + err.what());
697                 }
698                 presets_loaded.emplace_back(preset);
699             } catch (const std::runtime_error &err) {
700                 errors_cummulative += err.what();
701                 errors_cummulative += "\n";
702             }
703         }
704     m_presets.insert(m_presets.end(), std::make_move_iterator(presets_loaded.begin()), std::make_move_iterator(presets_loaded.end()));
705     std::sort(m_presets.begin() + m_num_default_presets, m_presets.end());
706     this->select_preset(first_visible_idx());
707     if (! errors_cummulative.empty())
708         throw Slic3r::RuntimeError(errors_cummulative);
709 }
710 
711 // Load a preset from an already parsed config file, insert it into the sorted sequence of presets
712 // and select it, losing previous modifications.
load_preset(const std::string & path,const std::string & name,const DynamicPrintConfig & config,bool select)713 Preset& PresetCollection::load_preset(const std::string &path, const std::string &name, const DynamicPrintConfig &config, bool select)
714 {
715     DynamicPrintConfig cfg(this->default_preset().config);
716     cfg.apply_only(config, cfg.keys(), true);
717     return this->load_preset(path, name, std::move(cfg), select);
718 }
719 
profile_print_params_same(const DynamicPrintConfig & cfg_old,const DynamicPrintConfig & cfg_new)720 static bool profile_print_params_same(const DynamicPrintConfig &cfg_old, const DynamicPrintConfig &cfg_new)
721 {
722     t_config_option_keys diff = cfg_old.diff(cfg_new);
723     // Following keys are used by the UI, not by the slicing core, therefore they are not important
724     // when comparing profiles for equality. Ignore them.
725     for (const char *key : { "compatible_prints", "compatible_prints_condition",
726                              "compatible_printers", "compatible_printers_condition", "inherits",
727                              "print_settings_id", "filament_settings_id", "sla_print_settings_id", "sla_material_settings_id", "printer_settings_id",
728                              "printer_model", "printer_variant", "default_print_profile", "default_filament_profile", "default_sla_print_profile", "default_sla_material_profile",
729                              //FIXME remove the print host keys?
730                              "print_host", "printhost_apikey", "printhost_cafile" })
731         diff.erase(std::remove(diff.begin(), diff.end(), key), diff.end());
732     // Preset with the same name as stored inside the config exists.
733     return diff.empty();
734 }
735 
736 // Load a preset from an already parsed config file, insert it into the sorted sequence of presets
737 // and select it, losing previous modifications.
738 // Only a single profile could be edited at at the same time, which introduces complexity when loading
739 // filament profiles for multi-extruder printers.
load_external_preset(const std::string & path,const std::string & name,const std::string & original_name,const DynamicPrintConfig & combined_config,LoadAndSelect select)740 std::pair<Preset*, bool> PresetCollection::load_external_preset(
741     // Path to the profile source file (a G-code, an AMF or 3MF file, a config file)
742     const std::string           &path,
743     // Name of the profile, derived from the source file name.
744     const std::string           &name,
745     // Original name of the profile, extracted from the loaded config. Empty, if the name has not been stored.
746     const std::string           &original_name,
747     // Config to initialize the preset from. It may contain configs of all presets merged in a single dictionary!
748     const DynamicPrintConfig    &combined_config,
749     // Select the preset after loading?
750     LoadAndSelect                select)
751 {
752     // Load the preset over a default preset, so that the missing fields are filled in from the default preset.
753     DynamicPrintConfig cfg(this->default_preset_for(combined_config).config);
754     const auto        &keys = cfg.keys();
755     cfg.apply_only(combined_config, keys, true);
756     std::string                 &inherits = Preset::inherits(cfg);
757     if (select == LoadAndSelect::Never) {
758         // Some filament profile has been selected and modified already.
759         // Check whether this profile is equal to the modified edited profile.
760         const Preset &edited = this->get_edited_preset();
761         if ((edited.name == original_name || edited.name == inherits) && profile_print_params_same(edited.config, cfg))
762             // Just point to that already selected and edited profile.
763             return std::make_pair(&(*this->find_preset_internal(edited.name)), false);
764     }
765     // Is there a preset already loaded with the name stored inside the config?
766     std::deque<Preset>::iterator it       = this->find_preset_internal(original_name);
767     bool                         found    = it != m_presets.end() && it->name == original_name;
768     if (! found) {
769     	// Try to match the original_name against the "renamed_from" profile names of loaded system profiles.
770 		it = this->find_preset_renamed(original_name);
771 		found = it != m_presets.end();
772     }
773     if (found && profile_print_params_same(it->config, cfg)) {
774         // The preset exists and it matches the values stored inside config.
775         if (select == LoadAndSelect::Always)
776             this->select_preset(it - m_presets.begin());
777         return std::make_pair(&(*it), false);
778     }
779     if (! found && select != LoadAndSelect::Never && ! inherits.empty()) {
780         // Try to use a system profile as a base to select the system profile
781         // and override its settings with the loaded ones.
782         assert(it == m_presets.end());
783         it    = this->find_preset_internal(inherits);
784         found = it != m_presets.end() && it->name == inherits;
785         if (found && profile_print_params_same(it->config, cfg)) {
786             // The system preset exists and it matches the values stored inside config.
787             if (select == LoadAndSelect::Always)
788                 this->select_preset(it - m_presets.begin());
789             return std::make_pair(&(*it), false);
790         }
791     }
792     if (found) {
793         if (select != LoadAndSelect::Never) {
794             // Select the existing preset and override it with new values, so that
795             // the differences will be shown in the preset editor against the referenced profile.
796             this->select_preset(it - m_presets.begin());
797             // The source config may contain keys from many possible preset types. Just copy those that relate to this preset.
798             this->get_edited_preset().config.apply_only(combined_config, keys, true);
799             this->update_dirty();
800             assert(this->get_edited_preset().is_dirty);
801             return std::make_pair(&(*it), this->get_edited_preset().is_dirty);
802         }
803         if (inherits.empty()) {
804             // Update the "inherits" field.
805             // There is a profile with the same name already loaded. Should we update the "inherits" field?
806             inherits = it->vendor ? it->name : it->inherits();
807         }
808     }
809 
810     // The external preset does not match an internal preset, load the external preset.
811     std::string new_name;
812     for (size_t idx = 0;; ++ idx) {
813         std::string suffix;
814         if (original_name.empty()) {
815             if (idx > 0)
816                 suffix = " (" + std::to_string(idx) + ")";
817         } else {
818             if (idx == 0)
819                 suffix = " (" + original_name + ")";
820             else
821                 suffix = " (" + original_name + "-" + std::to_string(idx) + ")";
822         }
823         new_name = name + suffix;
824         it = this->find_preset_internal(new_name);
825         if (it == m_presets.end() || it->name != new_name)
826             // Unique profile name. Insert a new profile.
827             break;
828         if (profile_print_params_same(it->config, cfg)) {
829             // The preset exists and it matches the values stored inside config.
830             if (select == LoadAndSelect::Always)
831                 this->select_preset(it - m_presets.begin());
832             return std::make_pair(&(*it), false);
833         }
834         // Form another profile name.
835     }
836     // Insert a new profile.
837     Preset &preset = this->load_preset(path, new_name, std::move(cfg), select == LoadAndSelect::Always);
838     preset.is_external = true;
839     if (&this->get_selected_preset() == &preset)
840         this->get_edited_preset().is_external = true;
841 
842     return std::make_pair(&preset, false);
843 }
844 
load_preset(const std::string & path,const std::string & name,DynamicPrintConfig && config,bool select)845 Preset& PresetCollection::load_preset(const std::string &path, const std::string &name, DynamicPrintConfig &&config, bool select)
846 {
847     auto it = this->find_preset_internal(name);
848     if (it == m_presets.end() || it->name != name) {
849         // The preset was not found. Create a new preset.
850         it = m_presets.emplace(it, Preset(m_type, name, false));
851     }
852     Preset &preset = *it;
853     preset.file = path;
854     preset.config = std::move(config);
855     preset.loaded = true;
856     preset.is_dirty = false;
857     if (select)
858         this->select_preset_by_name(name, true);
859     return preset;
860 }
861 
save_current_preset(const std::string & new_name,bool detach)862 void PresetCollection::save_current_preset(const std::string &new_name, bool detach)
863 {
864     // 1) Find the preset with a new_name or create a new one,
865     // initialize it with the edited config.
866     auto it = this->find_preset_internal(new_name);
867     if (it != m_presets.end() && it->name == new_name) {
868         // Preset with the same name found.
869         Preset &preset = *it;
870         if (preset.is_default || preset.is_external || preset.is_system)
871             // Cannot overwrite the default preset.
872             return;
873         // Overwriting an existing preset.
874         preset.config = std::move(m_edited_preset.config);
875         // The newly saved preset will be activated -> make it visible.
876         preset.is_visible = true;
877         if (detach) {
878             // Clear the link to the parent profile.
879             preset.vendor = nullptr;
880 			preset.inherits().clear();
881 			preset.alias.clear();
882 			preset.renamed_from.clear();
883         }
884     } else {
885         // Creating a new preset.
886         Preset       &preset   = *m_presets.insert(it, m_edited_preset);
887         std::string  &inherits = preset.inherits();
888         std::string   old_name = preset.name;
889         preset.name = new_name;
890         preset.file = this->path_from_name(new_name);
891         preset.vendor = nullptr;
892 		preset.alias.clear();
893         preset.renamed_from.clear();
894         if (detach) {
895         	// Clear the link to the parent profile.
896         	inherits.clear();
897         } else if (preset.is_system) {
898             // Inheriting from a system preset.
899             inherits = /* preset.vendor->name + "/" + */ old_name;
900         } else if (inherits.empty()) {
901             // Inheriting from a user preset. Link the new preset to the old preset.
902             // inherits = old_name;
903         } else {
904             // Inherited from a user preset. Just maintain the "inherited" flag,
905             // meaning it will inherit from either the system preset, or the inherited user preset.
906         }
907         preset.is_default  = false;
908         preset.is_system   = false;
909         preset.is_external = false;
910         // The newly saved preset will be activated -> make it visible.
911         preset.is_visible  = true;
912         // Just system presets have aliases
913         preset.alias.clear();
914     }
915     // 2) Activate the saved preset.
916     this->select_preset_by_name(new_name, true);
917     // 2) Store the active preset to disk.
918     this->get_selected_preset().save();
919 }
920 
delete_current_preset()921 bool PresetCollection::delete_current_preset()
922 {
923     const Preset &selected = this->get_selected_preset();
924     if (selected.is_default)
925         return false;
926     if (! selected.is_external && ! selected.is_system) {
927         // Erase the preset file.
928         boost::nowide::remove(selected.file.c_str());
929     }
930     // Remove the preset from the list.
931     m_presets.erase(m_presets.begin() + m_idx_selected);
932     // Find the next visible preset.
933     size_t new_selected_idx = m_idx_selected;
934     if (new_selected_idx < m_presets.size())
935         for (; new_selected_idx < m_presets.size() && ! m_presets[new_selected_idx].is_visible; ++ new_selected_idx) ;
936     if (new_selected_idx == m_presets.size())
937         for (--new_selected_idx; new_selected_idx > 0 && !m_presets[new_selected_idx].is_visible; --new_selected_idx);
938     this->select_preset(new_selected_idx);
939     return true;
940 }
941 
delete_preset(const std::string & name)942 bool PresetCollection::delete_preset(const std::string& name)
943 {
944     auto it = this->find_preset_internal(name);
945 
946     const Preset& preset = *it;
947     if (preset.is_default)
948         return false;
949     if (!preset.is_external && !preset.is_system) {
950         // Erase the preset file.
951         boost::nowide::remove(preset.file.c_str());
952     }
953     m_presets.erase(it);
954     return true;
955 }
956 
get_selected_preset_parent() const957 const Preset* PresetCollection::get_selected_preset_parent() const
958 {
959     if (this->get_selected_idx() == size_t(-1))
960         // This preset collection has no preset activated yet. Only the get_edited_preset() is valid.
961         return nullptr;
962 
963     const Preset 	  &selected_preset = this->get_selected_preset();
964     if (selected_preset.is_system || selected_preset.is_default)
965         return &selected_preset;
966 
967     const Preset 	  &edited_preset   = this->get_edited_preset();
968     const std::string &inherits        = edited_preset.inherits();
969     const Preset      *preset          = nullptr;
970     if (inherits.empty()) {
971         if (selected_preset.is_external)
972             return nullptr;
973         preset = &this->default_preset(m_type == Preset::Type::TYPE_PRINTER && edited_preset.printer_technology() == ptSLA ? 1 : 0);
974     } else
975         preset = this->find_preset(inherits, false);
976     if (preset == nullptr) {
977 	    // Resolve the "renamed_from" field.
978     	assert(! inherits.empty());
979     	auto it = this->find_preset_renamed(inherits);
980 		if (it != m_presets.end())
981 			preset = &(*it);
982     }
983     return (preset == nullptr/* || preset->is_default*/ || preset->is_external) ? nullptr : preset;
984 }
985 
get_preset_parent(const Preset & child) const986 const Preset* PresetCollection::get_preset_parent(const Preset& child) const
987 {
988     const std::string &inherits = child.inherits();
989     if (inherits.empty())
990 // 		return this->get_selected_preset().is_system ? &this->get_selected_preset() : nullptr;
991         return nullptr;
992     const Preset* preset = this->find_preset(inherits, false);
993     if (preset == nullptr) {
994     	auto it = this->find_preset_renamed(inherits);
995 		if (it != m_presets.end())
996 			preset = &(*it);
997     }
998     return
999          // not found
1000         (preset == nullptr/* || preset->is_default */||
1001          // this should not happen, user profile should not derive from an external profile
1002          preset->is_external ||
1003          // this should not happen, however people are creative, see GH #4996
1004          preset == &child) ?
1005             nullptr :
1006             preset;
1007 }
1008 
1009 // Return vendor of the first parent profile, for which the vendor is defined, or null if such profile does not exist.
get_preset_with_vendor_profile(const Preset & preset) const1010 PresetWithVendorProfile PresetCollection::get_preset_with_vendor_profile(const Preset &preset) const
1011 {
1012 	const Preset		*p = &preset;
1013 	const VendorProfile *v = nullptr;
1014 	do {
1015 		if (p->vendor != nullptr) {
1016 			v = p->vendor;
1017 			break;
1018 		}
1019 		p = this->get_preset_parent(*p);
1020 	} while (p != nullptr);
1021 	return PresetWithVendorProfile(preset, v);
1022 }
1023 
get_preset_name_by_alias(const std::string & alias) const1024 const std::string& PresetCollection::get_preset_name_by_alias(const std::string& alias) const
1025 {
1026 	for (
1027 		// Find the 1st profile name with the alias.
1028 		auto it = Slic3r::lower_bound_by_predicate(m_map_alias_to_profile_name.begin(), m_map_alias_to_profile_name.end(), [&alias](auto &l){ return l.first < alias; });
1029 		// Continue over all profile names with the same alias.
1030 		it != m_map_alias_to_profile_name.end() && it->first == alias; ++ it)
1031 		if (auto it_preset = this->find_preset_internal(it->second);
1032 			it_preset != m_presets.end() && it_preset->name == it->second &&
1033             it_preset->is_visible && (it_preset->is_compatible || size_t(it_preset - m_presets.begin()) == m_idx_selected))
1034 	        return it_preset->name;
1035     return alias;
1036 }
1037 
get_preset_name_renamed(const std::string & old_name) const1038 const std::string* PresetCollection::get_preset_name_renamed(const std::string &old_name) const
1039 {
1040 	auto it_renamed = m_map_system_profile_renamed.find(old_name);
1041 	if (it_renamed != m_map_system_profile_renamed.end())
1042 		return &it_renamed->second;
1043 	return nullptr;
1044 }
1045 
get_suffix_modified()1046 const std::string& PresetCollection::get_suffix_modified() {
1047     return g_suffix_modified;
1048 }
1049 
1050 // Return a preset by its name. If the preset is active, a temporary copy is returned.
1051 // If a preset is not found by its name, null is returned.
find_preset(const std::string & name,bool first_visible_if_not_found)1052 Preset* PresetCollection::find_preset(const std::string &name, bool first_visible_if_not_found)
1053 {
1054     Preset key(m_type, name, false);
1055     auto it = this->find_preset_internal(name);
1056     // Ensure that a temporary copy is returned if the preset found is currently selected.
1057     return (it != m_presets.end() && it->name == key.name) ? &this->preset(it - m_presets.begin()) :
1058         first_visible_if_not_found ? &this->first_visible() : nullptr;
1059 }
1060 
1061 // Return index of the first visible preset. Certainly at least the '- default -' preset shall be visible.
first_visible_idx() const1062 size_t PresetCollection::first_visible_idx() const
1063 {
1064     size_t idx = m_default_suppressed ? m_num_default_presets : 0;
1065     for (; idx < this->m_presets.size(); ++ idx)
1066         if (m_presets[idx].is_visible)
1067             break;
1068     if (idx == m_presets.size())
1069         idx = 0;
1070     return idx;
1071 }
1072 
set_default_suppressed(bool default_suppressed)1073 void PresetCollection::set_default_suppressed(bool default_suppressed)
1074 {
1075     if (m_default_suppressed != default_suppressed) {
1076         m_default_suppressed = default_suppressed;
1077         bool default_visible = ! default_suppressed || m_idx_selected < m_num_default_presets;
1078         for (size_t i = 0; i < m_num_default_presets; ++ i)
1079             m_presets[i].is_visible = default_visible;
1080     }
1081 }
1082 
update_compatible_internal(const PresetWithVendorProfile & active_printer,const PresetWithVendorProfile * active_print,PresetSelectCompatibleType unselect_if_incompatible)1083 size_t PresetCollection::update_compatible_internal(const PresetWithVendorProfile &active_printer, const PresetWithVendorProfile *active_print, PresetSelectCompatibleType unselect_if_incompatible)
1084 {
1085     DynamicPrintConfig config;
1086     config.set_key_value("printer_preset", new ConfigOptionString(active_printer.preset.name));
1087     const ConfigOption *opt = active_printer.preset.config.option("nozzle_diameter");
1088     if (opt)
1089         config.set_key_value("num_extruders", new ConfigOptionInt((int)static_cast<const ConfigOptionFloats*>(opt)->values.size()));
1090     bool some_compatible = false;
1091     for (size_t idx_preset = m_num_default_presets; idx_preset < m_presets.size(); ++ idx_preset) {
1092         bool    selected        = idx_preset == m_idx_selected;
1093         Preset &preset_selected = m_presets[idx_preset];
1094         Preset &preset_edited   = selected ? m_edited_preset : preset_selected;
1095 
1096         const PresetWithVendorProfile this_preset_with_vendor_profile = this->get_preset_with_vendor_profile(preset_edited);
1097         bool    was_compatible  = preset_edited.is_compatible;
1098         preset_edited.is_compatible = is_compatible_with_printer(this_preset_with_vendor_profile, active_printer, &config);
1099         some_compatible |= preset_edited.is_compatible;
1100 	    if (active_print != nullptr)
1101 	        preset_edited.is_compatible &= is_compatible_with_print(this_preset_with_vendor_profile, *active_print, active_printer);
1102         if (! preset_edited.is_compatible && selected &&
1103         	(unselect_if_incompatible == PresetSelectCompatibleType::Always || (unselect_if_incompatible == PresetSelectCompatibleType::OnlyIfWasCompatible && was_compatible)))
1104             m_idx_selected = size_t(-1);
1105         if (selected)
1106             preset_selected.is_compatible = preset_edited.is_compatible;
1107     }
1108     // Update visibility of the default profiles here if the defaults are suppressed, the current profile is not compatible and we don't want to select another compatible profile.
1109     if (m_idx_selected >= m_num_default_presets && m_default_suppressed)
1110 	    for (size_t i = 0; i < m_num_default_presets; ++ i)
1111 	        m_presets[i].is_visible = ! some_compatible;
1112     return m_idx_selected;
1113 }
1114 
1115 // Save the preset under a new name. If the name is different from the old one,
1116 // a new preset is stored into the list of presets.
1117 // All presets are marked as not modified and the new preset is activated.
1118 //void PresetCollection::save_current_preset(const std::string &new_name);
1119 
1120 // Delete the current preset, activate the first visible preset.
1121 //void PresetCollection::delete_current_preset();
1122 
1123 // Update a dirty flag of the current preset
1124 // Return true if the dirty flag changed.
update_dirty()1125 bool PresetCollection::update_dirty()
1126 {
1127     bool was_dirty = this->get_selected_preset().is_dirty;
1128     bool is_dirty  = current_is_dirty();
1129     this->get_selected_preset().is_dirty = is_dirty;
1130     this->get_edited_preset().is_dirty = is_dirty;
1131 
1132     return was_dirty != is_dirty;
1133 }
1134 
1135 template<class T>
add_correct_opts_to_diff(const std::string & opt_key,t_config_option_keys & vec,const ConfigBase & other,const ConfigBase & this_c)1136 void add_correct_opts_to_diff(const std::string &opt_key, t_config_option_keys& vec, const ConfigBase &other, const ConfigBase &this_c)
1137 {
1138     const T* opt_init = static_cast<const T*>(other.option(opt_key));
1139     const T* opt_cur = static_cast<const T*>(this_c.option(opt_key));
1140     int opt_init_max_id = opt_init->values.size() - 1;
1141     for (int i = 0; i < int(opt_cur->values.size()); i++)
1142     {
1143         int init_id = i <= opt_init_max_id ? i : 0;
1144         if (opt_cur->values[i] != opt_init->values[init_id])
1145             vec.emplace_back(opt_key + "#" + std::to_string(i));
1146     }
1147 }
1148 
1149 // Use deep_diff to correct return of changed options, considering individual options for each extruder.
deep_diff(const ConfigBase & config_this,const ConfigBase & config_other)1150 inline t_config_option_keys deep_diff(const ConfigBase &config_this, const ConfigBase &config_other)
1151 {
1152     t_config_option_keys diff;
1153     for (const t_config_option_key &opt_key : config_this.keys()) {
1154         const ConfigOption *this_opt  = config_this.option(opt_key);
1155         const ConfigOption *other_opt = config_other.option(opt_key);
1156         if (this_opt != nullptr && other_opt != nullptr && *this_opt != *other_opt)
1157         {
1158             if (opt_key == "bed_shape" || opt_key == "thumbnails" || opt_key == "compatible_prints" || opt_key == "compatible_printers") {
1159                 // Scalar variable, or a vector variable, which is independent from number of extruders,
1160                 // thus the vector is presented to the user as a single input.
1161                 diff.emplace_back(opt_key);
1162             } else if (opt_key == "default_filament_profile") {
1163                 // Ignore this field, it is not presented to the user, therefore showing a "modified" flag for this parameter does not help.
1164                 // Also the length of this field may differ, which may lead to a crash if the block below is used.
1165             } else {
1166                 switch (other_opt->type()) {
1167                 case coInts:    add_correct_opts_to_diff<ConfigOptionInts       >(opt_key, diff, config_other, config_this);  break;
1168                 case coBools:   add_correct_opts_to_diff<ConfigOptionBools      >(opt_key, diff, config_other, config_this);  break;
1169                 case coFloats:  add_correct_opts_to_diff<ConfigOptionFloats     >(opt_key, diff, config_other, config_this);  break;
1170                 case coStrings: add_correct_opts_to_diff<ConfigOptionStrings    >(opt_key, diff, config_other, config_this);  break;
1171                 case coPercents:add_correct_opts_to_diff<ConfigOptionPercents   >(opt_key, diff, config_other, config_this);  break;
1172                 case coPoints:  add_correct_opts_to_diff<ConfigOptionPoints     >(opt_key, diff, config_other, config_this);  break;
1173                 default:        diff.emplace_back(opt_key);     break;
1174                 }
1175             }
1176         }
1177     }
1178     return diff;
1179 }
1180 
dirty_options(const Preset * edited,const Preset * reference,const bool deep_compare)1181 std::vector<std::string> PresetCollection::dirty_options(const Preset *edited, const Preset *reference, const bool deep_compare /*= false*/)
1182 {
1183     std::vector<std::string> changed;
1184     if (edited != nullptr && reference != nullptr) {
1185         changed = deep_compare ?
1186                 deep_diff(edited->config, reference->config) :
1187                 reference->config.diff(edited->config);
1188         // The "compatible_printers" option key is handled differently from the others:
1189         // It is not mandatory. If the key is missing, it means it is compatible with any printer.
1190         // If the key exists and it is empty, it means it is compatible with no printer.
1191         std::initializer_list<const char*> optional_keys { "compatible_prints", "compatible_printers" };
1192         for (auto &opt_key : optional_keys) {
1193             if (reference->config.has(opt_key) != edited->config.has(opt_key))
1194                 changed.emplace_back(opt_key);
1195         }
1196     }
1197     return changed;
1198 }
1199 
1200 // Select a new preset. This resets all the edits done to the currently selected preset.
1201 // If the preset with index idx does not exist, a first visible preset is selected.
select_preset(size_t idx)1202 Preset& PresetCollection::select_preset(size_t idx)
1203 {
1204     for (Preset &preset : m_presets)
1205         preset.is_dirty = false;
1206     if (idx >= m_presets.size())
1207         idx = first_visible_idx();
1208     m_idx_selected = idx;
1209     m_edited_preset = m_presets[idx];
1210     bool default_visible = ! m_default_suppressed || m_idx_selected < m_num_default_presets;
1211     for (size_t i = 0; i < m_num_default_presets; ++i)
1212         m_presets[i].is_visible = default_visible;
1213     return m_presets[idx];
1214 }
1215 
select_preset_by_name(const std::string & name_w_suffix,bool force)1216 bool PresetCollection::select_preset_by_name(const std::string &name_w_suffix, bool force)
1217 {
1218     std::string name = Preset::remove_suffix_modified(name_w_suffix);
1219     // 1) Try to find the preset by its name.
1220     auto it = this->find_preset_internal(name);
1221     size_t idx = 0;
1222     if (it != m_presets.end() && it->name == name && it->is_visible)
1223         // Preset found by its name and it is visible.
1224         idx = it - m_presets.begin();
1225     else {
1226         // Find the first visible preset.
1227         for (size_t i = m_default_suppressed ? m_num_default_presets : 0; i < m_presets.size(); ++ i)
1228             if (m_presets[i].is_visible) {
1229                 idx = i;
1230                 break;
1231             }
1232         // If the first visible preset was not found, return the 0th element, which is the default preset.
1233     }
1234 
1235     // 2) Select the new preset.
1236     if (m_idx_selected != idx || force) {
1237         this->select_preset(idx);
1238         return true;
1239     }
1240 
1241     return false;
1242 }
1243 
select_preset_by_name_strict(const std::string & name)1244 bool PresetCollection::select_preset_by_name_strict(const std::string &name)
1245 {
1246     // 1) Try to find the preset by its name.
1247     auto it = this->find_preset_internal(name);
1248     size_t idx = (size_t)-1;
1249     if (it != m_presets.end() && it->name == name && it->is_visible)
1250         // Preset found by its name.
1251         idx = it - m_presets.begin();
1252     // 2) Select the new preset.
1253     if (idx != (size_t)-1) {
1254         this->select_preset(idx);
1255         return true;
1256     }
1257     m_idx_selected = idx;
1258     return false;
1259 }
1260 
1261 // Merge one vendor's presets with the other vendor's presets, report duplicates.
merge_presets(PresetCollection && other,const VendorMap & new_vendors)1262 std::vector<std::string> PresetCollection::merge_presets(PresetCollection &&other, const VendorMap &new_vendors)
1263 {
1264     std::vector<std::string> duplicates;
1265     for (Preset &preset : other.m_presets) {
1266         if (preset.is_default || preset.is_external)
1267             continue;
1268         Preset key(m_type, preset.name);
1269         auto it = std::lower_bound(m_presets.begin() + m_num_default_presets, m_presets.end(), key);
1270         if (it == m_presets.end() || it->name != preset.name) {
1271             if (preset.vendor != nullptr) {
1272                 // Re-assign a pointer to the vendor structure in the new PresetBundle.
1273                 auto it = new_vendors.find(preset.vendor->id);
1274                 assert(it != new_vendors.end());
1275                 preset.vendor = &it->second;
1276             }
1277             this->m_presets.emplace(it, std::move(preset));
1278         } else
1279             duplicates.emplace_back(std::move(preset.name));
1280     }
1281     return duplicates;
1282 }
1283 
update_map_alias_to_profile_name()1284 void PresetCollection::update_map_alias_to_profile_name()
1285 {
1286 	m_map_alias_to_profile_name.clear();
1287 	for (const Preset &preset : m_presets)
1288 		m_map_alias_to_profile_name.emplace_back(preset.alias, preset.name);
1289 	std::sort(m_map_alias_to_profile_name.begin(), m_map_alias_to_profile_name.end(), [](auto &l, auto &r) { return l.first < r.first; });
1290 }
1291 
update_map_system_profile_renamed()1292 void PresetCollection::update_map_system_profile_renamed()
1293 {
1294 	m_map_system_profile_renamed.clear();
1295 	for (Preset &preset : m_presets)
1296 		for (const std::string &renamed_from : preset.renamed_from) {
1297             const auto [it, success] = m_map_system_profile_renamed.insert(std::pair<std::string, std::string>(renamed_from, preset.name));
1298 			if (! success)
1299                 BOOST_LOG_TRIVIAL(error) << boost::format("Preset name \"%1%\" was marked as renamed from \"%2%\", though preset name \"%3%\" was marked as renamed from \"%2%\" as well.") % preset.name % renamed_from % it->second;
1300 		}
1301 }
1302 
name() const1303 std::string PresetCollection::name() const
1304 {
1305     switch (this->type()) {
1306     case Preset::TYPE_PRINT:        return L("print");
1307     case Preset::TYPE_FILAMENT:     return L("filament");
1308     case Preset::TYPE_SLA_PRINT:    return L("SLA print");
1309     case Preset::TYPE_SLA_MATERIAL: return L("SLA material");
1310     case Preset::TYPE_PRINTER:      return L("printer");
1311     default:                        return "invalid";
1312     }
1313 }
1314 
section_name() const1315 std::string PresetCollection::section_name() const
1316 {
1317     switch (this->type()) {
1318     case Preset::TYPE_PRINT:        return "print";
1319     case Preset::TYPE_FILAMENT:     return "filament";
1320     case Preset::TYPE_SLA_PRINT:    return "sla_print";
1321     case Preset::TYPE_SLA_MATERIAL: return "sla_material";
1322     case Preset::TYPE_PRINTER:      return "printer";
1323     default:                        return "invalid";
1324     }
1325 }
1326 
1327 // Used for validating the "inherits" flag when importing user's config bundles.
1328 // Returns names of all system presets including the former names of these presets.
system_preset_names() const1329 std::vector<std::string> PresetCollection::system_preset_names() const
1330 {
1331     size_t num = 0;
1332     for (const Preset &preset : m_presets)
1333         if (preset.is_system)
1334             ++ num;
1335     std::vector<std::string> out;
1336     out.reserve(num);
1337     for (const Preset &preset : m_presets)
1338         if (preset.is_system) {
1339             out.emplace_back(preset.name);
1340             out.insert(out.end(), preset.renamed_from.begin(), preset.renamed_from.end());
1341         }
1342     std::sort(out.begin(), out.end());
1343     return out;
1344 }
1345 
1346 // Generate a file path from a profile name. Add the ".ini" suffix if it is missing.
path_from_name(const std::string & new_name) const1347 std::string PresetCollection::path_from_name(const std::string &new_name) const
1348 {
1349     std::string file_name = boost::iends_with(new_name, ".ini") ? new_name : (new_name + ".ini");
1350     return (boost::filesystem::path(m_dir_path) / file_name).make_preferred().string();
1351 }
1352 
default_preset_for(const DynamicPrintConfig & config) const1353 const Preset& PrinterPresetCollection::default_preset_for(const DynamicPrintConfig &config) const
1354 {
1355     const ConfigOptionEnumGeneric *opt_printer_technology = config.opt<ConfigOptionEnumGeneric>("printer_technology");
1356     return this->default_preset((opt_printer_technology == nullptr || opt_printer_technology->value == ptFFF) ? 0 : 1);
1357 }
1358 
find_by_model_id(const std::string & model_id) const1359 const Preset* PrinterPresetCollection::find_by_model_id(const std::string &model_id) const
1360 {
1361     if (model_id.empty()) { return nullptr; }
1362 
1363     const auto it = std::find_if(cbegin(), cend(), [&](const Preset &preset) {
1364         return preset.config.opt_string("printer_model") == model_id;
1365     });
1366 
1367     return it != cend() ? &*it : nullptr;
1368 }
1369 
1370 // -------------------------
1371 // ***  PhysicalPrinter  ***
1372 // -------------------------
1373 
separator()1374 std::string PhysicalPrinter::separator()
1375 {
1376     return " * ";
1377 }
1378 
printer_options()1379 const std::vector<std::string>& PhysicalPrinter::printer_options()
1380 {
1381     static std::vector<std::string> s_opts;
1382     if (s_opts.empty()) {
1383         s_opts = {
1384             "preset_name",
1385             "printer_technology",
1386 //            "printer_model",
1387             "host_type",
1388             "print_host",
1389             "printhost_apikey",
1390             "printhost_cafile",
1391             "printhost_port",
1392             "printhost_authorization_type",
1393             // HTTP digest authentization (RFC 2617)
1394             "printhost_user",
1395             "printhost_password"
1396         };
1397     }
1398     return s_opts;
1399 }
1400 
1401 static constexpr auto legacy_print_host_options = {
1402     "print_host",
1403     "printhost_apikey",
1404     "printhost_cafile",
1405 };
1406 
presets_with_print_host_information(const PrinterPresetCollection & printer_presets)1407 std::vector<std::string> PhysicalPrinter::presets_with_print_host_information(const PrinterPresetCollection& printer_presets)
1408 {
1409     std::vector<std::string> presets;
1410     for (const Preset& preset : printer_presets)
1411         if (has_print_host_information(preset.config))
1412             presets.emplace_back(preset.name);
1413 
1414     return presets;
1415 }
1416 
has_print_host_information(const DynamicPrintConfig & config)1417 bool PhysicalPrinter::has_print_host_information(const DynamicPrintConfig& config)
1418 {
1419     for (const char *opt : legacy_print_host_options)
1420         if (!config.opt_string(opt).empty())
1421             return true;
1422 
1423     return false;
1424 }
1425 
get_preset_names() const1426 const std::set<std::string>& PhysicalPrinter::get_preset_names() const
1427 {
1428     return preset_names;
1429 }
1430 
has_empty_config() const1431 bool PhysicalPrinter::has_empty_config() const
1432 {
1433     return  config.opt_string("print_host"        ).empty() &&
1434             config.opt_string("printhost_apikey"  ).empty() &&
1435             config.opt_string("printhost_cafile"  ).empty() &&
1436             config.opt_string("printhost_port"    ).empty() &&
1437             config.opt_string("printhost_user"    ).empty() &&
1438             config.opt_string("printhost_password").empty();
1439 }
1440 
update_preset_names_in_config()1441 void PhysicalPrinter::update_preset_names_in_config()
1442 {
1443     if (!preset_names.empty()) {
1444         std::string name;
1445         for (auto el : preset_names)
1446             name += el + ";";
1447         name.pop_back();
1448         config.set_key_value("preset_name", new ConfigOptionString(name));
1449     }
1450 }
1451 
save(const std::string & file_name_from,const std::string & file_name_to)1452 void PhysicalPrinter::save(const std::string& file_name_from, const std::string& file_name_to)
1453 {
1454     // rename the file
1455     boost::nowide::rename(file_name_from.data(), file_name_to.data());
1456     this->file = file_name_to;
1457     // save configuration
1458     this->config.save(this->file);
1459 }
1460 
update_from_preset(const Preset & preset)1461 void PhysicalPrinter::update_from_preset(const Preset& preset)
1462 {
1463     config.apply_only(preset.config, printer_options(), true);
1464     // add preset names to the options list
1465     preset_names.emplace(preset.name);
1466     update_preset_names_in_config();
1467 }
1468 
update_from_config(const DynamicPrintConfig & new_config)1469 void PhysicalPrinter::update_from_config(const DynamicPrintConfig& new_config)
1470 {
1471     config.apply_only(new_config, printer_options(), false);
1472 
1473     std::string str = config.opt_string("preset_name");
1474     std::set<std::string> values{};
1475     if (!str.empty()) {
1476         boost::split(values, str, boost::is_any_of(";"));
1477         for (const std::string& val : values)
1478             preset_names.emplace(val);
1479     }
1480     preset_names = values;
1481 }
1482 
reset_presets()1483 void PhysicalPrinter::reset_presets()
1484 {
1485     return preset_names.clear();
1486 }
1487 
add_preset(const std::string & preset_name)1488 bool PhysicalPrinter::add_preset(const std::string& preset_name)
1489 {
1490     return preset_names.emplace(preset_name).second;
1491 }
1492 
delete_preset(const std::string & preset_name)1493 bool PhysicalPrinter::delete_preset(const std::string& preset_name)
1494 {
1495     return preset_names.erase(preset_name) > 0;
1496 }
1497 
PhysicalPrinter(const std::string & name,const DynamicPrintConfig & default_config)1498 PhysicalPrinter::PhysicalPrinter(const std::string& name, const DynamicPrintConfig& default_config) :
1499     name(name), config(default_config)
1500 {
1501     update_from_config(config);
1502 }
1503 
PhysicalPrinter(const std::string & name,const DynamicPrintConfig & default_config,const Preset & preset)1504 PhysicalPrinter::PhysicalPrinter(const std::string& name, const DynamicPrintConfig &default_config, const Preset& preset) :
1505     name(name), config(default_config)
1506 {
1507     update_from_preset(preset);
1508 }
1509 
set_name(const std::string & name)1510 void PhysicalPrinter::set_name(const std::string& name)
1511 {
1512     this->name = name;
1513 }
1514 
get_full_name(std::string preset_name) const1515 std::string PhysicalPrinter::get_full_name(std::string preset_name) const
1516 {
1517     return name + separator() + preset_name;
1518 }
1519 
get_short_name(std::string full_name)1520 std::string PhysicalPrinter::get_short_name(std::string full_name)
1521 {
1522     int pos = full_name.find(separator());
1523     if (pos > 0)
1524         boost::erase_tail(full_name, full_name.length() - pos);
1525     return full_name;
1526 }
1527 
get_preset_name(std::string name)1528 std::string PhysicalPrinter::get_preset_name(std::string name)
1529 {
1530     int pos = name.find(separator());
1531     boost::erase_head(name, pos + 3);
1532     return Preset::remove_suffix_modified(name);
1533 }
1534 
1535 
1536 // -----------------------------------
1537 // ***  PhysicalPrinterCollection  ***
1538 // -----------------------------------
1539 
PhysicalPrinterCollection(const std::vector<std::string> & keys)1540 PhysicalPrinterCollection::PhysicalPrinterCollection( const std::vector<std::string>& keys)
1541 {
1542     // Default config for a physical printer containing all key/value pairs of PhysicalPrinter::printer_options().
1543     for (const std::string &key : keys) {
1544         const ConfigOptionDef *opt = print_config_def.get(key);
1545         assert(opt);
1546         assert(opt->default_value);
1547         m_default_config.set_key_value(key, opt->default_value->clone());
1548     }
1549 }
1550 
1551 // Load all printers found in dir_path.
1552 // Throws an exception on error.
load_printers(const std::string & dir_path,const std::string & subdir,PresetsConfigSubstitutions & substitutions,ForwardCompatibilitySubstitutionRule substitution_rule)1553 void PhysicalPrinterCollection::load_printers(
1554     const std::string& dir_path, const std::string& subdir,
1555     PresetsConfigSubstitutions& substitutions, ForwardCompatibilitySubstitutionRule substitution_rule)
1556 {
1557     // Don't use boost::filesystem::canonical() on Windows, it is broken in regard to reparse points,
1558     // see https://github.com/prusa3d/PrusaSlicer/issues/732
1559     boost::filesystem::path dir = boost::filesystem::absolute(boost::filesystem::path(dir_path) / subdir).make_preferred();
1560     m_dir_path = dir.string();
1561     std::string errors_cummulative;
1562     // Store the loaded printers into a new vector, otherwise the binary search for already existing presets would be broken.
1563     std::deque<PhysicalPrinter> printers_loaded;
1564     for (auto& dir_entry : boost::filesystem::directory_iterator(dir))
1565         if (Slic3r::is_ini_file(dir_entry)) {
1566             std::string name = dir_entry.path().filename().string();
1567             // Remove the .ini suffix.
1568             name.erase(name.size() - 4);
1569             if (this->find_printer(name, false)) {
1570                 // This happens when there's is a preset (most likely legacy one) with the same name as a system preset
1571                 // that's already been loaded from a bundle.
1572                 BOOST_LOG_TRIVIAL(warning) << "Printer already present, not loading: " << name;
1573                 continue;
1574             }
1575             try {
1576                 PhysicalPrinter printer(name, this->default_config());
1577                 printer.file = dir_entry.path().string();
1578                 // Load the preset file, apply preset values on top of defaults.
1579                 try {
1580                     DynamicPrintConfig config;
1581                     ConfigSubstitutions config_substitutions = config.load_from_ini(printer.file, substitution_rule);
1582                     if (! config_substitutions.empty())
1583                         substitutions.push_back({ name, Preset::TYPE_PHYSICAL_PRINTER, PresetConfigSubstitutions::Source::UserFile, printer.file, std::move(config_substitutions) });
1584                     printer.update_from_config(config);
1585                     printer.loaded = true;
1586                 }
1587                 catch (const std::ifstream::failure& err) {
1588                     throw Slic3r::RuntimeError(std::string("The selected preset cannot be loaded: ") + printer.file + "\n\tReason: " + err.what());
1589                 }
1590                 catch (const std::runtime_error& err) {
1591                     throw Slic3r::RuntimeError(std::string("Failed loading the preset file: ") + printer.file + "\n\tReason: " + err.what());
1592                 }
1593                 printers_loaded.emplace_back(printer);
1594             }
1595             catch (const std::runtime_error& err) {
1596                 errors_cummulative += err.what();
1597                 errors_cummulative += "\n";
1598             }
1599         }
1600     m_printers.insert(m_printers.end(), std::make_move_iterator(printers_loaded.begin()), std::make_move_iterator(printers_loaded.end()));
1601     std::sort(m_printers.begin(), m_printers.end());
1602     if (!errors_cummulative.empty())
1603         throw Slic3r::RuntimeError(errors_cummulative);
1604 }
1605 
load_printer(const std::string & path,const std::string & name,DynamicPrintConfig && config,bool select,bool save)1606 void PhysicalPrinterCollection::load_printer(const std::string& path, const std::string& name, DynamicPrintConfig&& config, bool select, bool save/* = false*/)
1607 {
1608     auto it = this->find_printer_internal(name);
1609     if (it == m_printers.end() || it->name != name) {
1610         // The preset was not found. Create a new preset.
1611         it = m_printers.emplace(it, PhysicalPrinter(name, config));
1612     }
1613 
1614     it->file = path;
1615     it->config = std::move(config);
1616     it->loaded = true;
1617     if (select)
1618         this->select_printer(*it);
1619 
1620     if (save)
1621         it->save();
1622 }
1623 
1624 // if there is saved user presets, contains information about "Print Host upload",
1625 // Create default printers with this presets
1626 // Note! "Print Host upload" options will be cleared after physical printer creations
load_printers_from_presets(PrinterPresetCollection & printer_presets)1627 void PhysicalPrinterCollection::load_printers_from_presets(PrinterPresetCollection& printer_presets)
1628 {
1629     int cnt=0;
1630     for (Preset& preset: printer_presets) {
1631         DynamicPrintConfig& config = preset.config;
1632         for(const char* option : legacy_print_host_options) {
1633             if (!config.opt_string(option).empty()) {
1634                 // check if printer with those "Print Host upload" options already exist
1635                 PhysicalPrinter* existed_printer = find_printer_with_same_config(config);
1636                 if (existed_printer)
1637                     // just add preset for this printer
1638                     existed_printer->add_preset(preset.name);
1639                 else {
1640                     std::string new_printer_name = (boost::format("Printer %1%") % ++cnt ).str();
1641                     while (find_printer(new_printer_name))
1642                         new_printer_name = (boost::format("Printer %1%") % ++cnt).str();
1643 
1644                     // create new printer from this preset
1645                     PhysicalPrinter printer(new_printer_name, this->default_config(), preset);
1646                     printer.loaded = true;
1647                     save_printer(printer);
1648                 }
1649 
1650                 // erase "Print Host upload" information from the preset
1651                 for (const char *opt : legacy_print_host_options)
1652                     config.opt_string(opt).clear();
1653                 // save changes for preset
1654                 preset.save();
1655 
1656                 // update those changes for edited preset if it's equal to the preset
1657                 Preset& edited = printer_presets.get_edited_preset();
1658                 if (preset.name == edited.name) {
1659                     for (const char *opt : legacy_print_host_options)
1660                         edited.config.opt_string(opt).clear();
1661                 }
1662 
1663                 break;
1664             }
1665         }
1666     }
1667 }
1668 
find_printer(const std::string & name,bool case_sensitive_search)1669 PhysicalPrinter* PhysicalPrinterCollection::find_printer( const std::string& name, bool case_sensitive_search)
1670 {
1671     auto it = this->find_printer_internal(name, case_sensitive_search);
1672 
1673     // Ensure that a temporary copy is returned if the preset found is currently selected.
1674     auto is_equal_name = [name, case_sensitive_search](const std::string& in_name) {
1675         if (case_sensitive_search)
1676             return in_name == name;
1677         return boost::to_lower_copy<std::string>(in_name) == boost::to_lower_copy<std::string>(name);
1678     };
1679 
1680     if (it == m_printers.end() || !is_equal_name(it->name))
1681         return nullptr;
1682     return &this->printer(it - m_printers.begin());
1683 }
1684 
find_printer_internal(const std::string & name,bool case_sensitive_search)1685 std::deque<PhysicalPrinter>::iterator PhysicalPrinterCollection::find_printer_internal(const std::string& name, bool case_sensitive_search/* = true*/)
1686 {
1687     if (case_sensitive_search)
1688         return Slic3r::lower_bound_by_predicate(m_printers.begin(), m_printers.end(), [&name](const auto& l) { return l.name < name;  });
1689 
1690     std::string low_name = boost::to_lower_copy<std::string>(name);
1691 
1692     size_t i = 0;
1693     for (const PhysicalPrinter& printer : m_printers) {
1694         if (boost::to_lower_copy<std::string>(printer.name) == low_name)
1695             break;
1696         i++;
1697     }
1698     if (i == m_printers.size())
1699         return m_printers.end();
1700 
1701     return m_printers.begin() + i;
1702 }
1703 
find_printer_with_same_config(const DynamicPrintConfig & config)1704 PhysicalPrinter* PhysicalPrinterCollection::find_printer_with_same_config(const DynamicPrintConfig& config)
1705 {
1706     for (const PhysicalPrinter& printer :*this) {
1707         bool is_equal = true;
1708         for (const char *opt : legacy_print_host_options)
1709             if (is_equal && printer.config.opt_string(opt) != config.opt_string(opt))
1710                 is_equal = false;
1711 
1712         if (is_equal)
1713             return find_printer(printer.name);
1714     }
1715     return nullptr;
1716 }
1717 
1718 // Generate a file path from a profile name. Add the ".ini" suffix if it is missing.
path_from_name(const std::string & new_name) const1719 std::string PhysicalPrinterCollection::path_from_name(const std::string& new_name) const
1720 {
1721     std::string file_name = boost::iends_with(new_name, ".ini") ? new_name : (new_name + ".ini");
1722     return (boost::filesystem::path(m_dir_path) / file_name).make_preferred().string();
1723 }
1724 
save_printer(PhysicalPrinter & edited_printer,const std::string & renamed_from)1725 void PhysicalPrinterCollection::save_printer(PhysicalPrinter& edited_printer, const std::string& renamed_from/* = ""*/)
1726 {
1727     // controll and update preset_names in edited_printer config
1728     edited_printer.update_preset_names_in_config();
1729 
1730     std::string name = renamed_from.empty() ? edited_printer.name : renamed_from;
1731     // 1) Find the printer with a new_name or create a new one,
1732     // initialize it with the edited config.
1733     auto it = this->find_printer_internal(name);
1734     if (it != m_printers.end() && it->name == name) {
1735         // Printer with the same name found.
1736         // Overwriting an existing preset.
1737         it->config = std::move(edited_printer.config);
1738         it->name = edited_printer.name;
1739         it->preset_names = edited_printer.preset_names;
1740         // sort printers and get new it
1741         std::sort(m_printers.begin(), m_printers.end());
1742         it = this->find_printer_internal(edited_printer.name);
1743     }
1744     else {
1745         // Creating a new printer.
1746         it = m_printers.emplace(it, edited_printer);
1747     }
1748     assert(it != m_printers.end());
1749 
1750     // 2) Save printer
1751     PhysicalPrinter& printer = *it;
1752     if (printer.file.empty())
1753         printer.file = this->path_from_name(printer.name);
1754 
1755     if (printer.file == this->path_from_name(printer.name))
1756         printer.save();
1757     else
1758         // if printer was renamed, we should rename a file and than save the config
1759         printer.save(printer.file, this->path_from_name(printer.name));
1760 
1761     // update idx_selected
1762     m_idx_selected = it - m_printers.begin();
1763 }
1764 
delete_printer(const std::string & name)1765 bool PhysicalPrinterCollection::delete_printer(const std::string& name)
1766 {
1767     auto it = this->find_printer_internal(name);
1768     if (it == m_printers.end())
1769         return false;
1770 
1771     const PhysicalPrinter& printer = *it;
1772     // Erase the preset file.
1773     boost::nowide::remove(printer.file.c_str());
1774     m_printers.erase(it);
1775     return true;
1776 }
1777 
delete_selected_printer()1778 bool PhysicalPrinterCollection::delete_selected_printer()
1779 {
1780     if (!has_selection())
1781         return false;
1782     const PhysicalPrinter& printer = this->get_selected_printer();
1783 
1784     // Erase the preset file.
1785     boost::nowide::remove(printer.file.c_str());
1786     // Remove the preset from the list.
1787     m_printers.erase(m_printers.begin() + m_idx_selected);
1788     // unselect all printers
1789     unselect_printer();
1790 
1791     return true;
1792 }
1793 
delete_preset_from_printers(const std::string & preset_name)1794 bool PhysicalPrinterCollection::delete_preset_from_printers( const std::string& preset_name)
1795 {
1796     std::vector<std::string> printers_for_delete;
1797     for (PhysicalPrinter& printer : m_printers) {
1798         if (printer.preset_names.size() == 1 && *printer.preset_names.begin() == preset_name)
1799             printers_for_delete.emplace_back(printer.name);
1800         else if (printer.delete_preset(preset_name))
1801             save_printer(printer);
1802     }
1803 
1804     if (!printers_for_delete.empty())
1805         for (const std::string& printer_name : printers_for_delete)
1806             delete_printer(printer_name);
1807 
1808     unselect_printer();
1809     return true;
1810 }
1811 
1812 // Get list of printers which have more than one preset and "preset_name" preset is one of them
get_printers_with_preset(const std::string & preset_name)1813 std::vector<std::string> PhysicalPrinterCollection::get_printers_with_preset(const std::string& preset_name)
1814 {
1815     std::vector<std::string> printers;
1816 
1817     for (auto printer : m_printers) {
1818         if (printer.preset_names.size() == 1)
1819             continue;
1820         if (printer.preset_names.find(preset_name) != printer.preset_names.end())
1821             printers.emplace_back(printer.name);
1822     }
1823 
1824     return printers;
1825 }
1826 
1827 // Get list of printers which has only "preset_name" preset
get_printers_with_only_preset(const std::string & preset_name)1828 std::vector<std::string> PhysicalPrinterCollection::get_printers_with_only_preset(const std::string& preset_name)
1829 {
1830     std::vector<std::string> printers;
1831 
1832     for (auto printer : m_printers)
1833         if (printer.preset_names.size() == 1 && *printer.preset_names.begin() == preset_name)
1834             printers.emplace_back(printer.name);
1835 
1836     return printers;
1837 }
1838 
get_selected_full_printer_name() const1839 std::string PhysicalPrinterCollection::get_selected_full_printer_name() const
1840 {
1841     return (m_idx_selected == size_t(-1)) ? std::string() : this->get_selected_printer().get_full_name(m_selected_preset);
1842 }
1843 
select_printer(const std::string & full_name)1844 void PhysicalPrinterCollection::select_printer(const std::string& full_name)
1845 {
1846     std::string printer_name = PhysicalPrinter::get_short_name(full_name);
1847     auto it = this->find_printer_internal(printer_name);
1848     if (it == m_printers.end()) {
1849         unselect_printer();
1850         return;
1851     }
1852 
1853     // update idx_selected
1854     m_idx_selected = it - m_printers.begin();
1855 
1856     // update name of the currently selected preset
1857     if (printer_name == full_name)
1858         // use first preset in the list
1859         m_selected_preset = *it->preset_names.begin();
1860     else
1861         m_selected_preset = it->get_preset_name(full_name);
1862 }
1863 
select_printer(const std::string & printer_name,const std::string & preset_name)1864 void PhysicalPrinterCollection::select_printer(const std::string& printer_name, const std::string& preset_name)
1865 {
1866     if (preset_name.empty())
1867         return select_printer(printer_name);
1868     return select_printer(printer_name + PhysicalPrinter::separator() + preset_name);
1869 }
1870 
select_printer(const PhysicalPrinter & printer)1871 void PhysicalPrinterCollection::select_printer(const PhysicalPrinter& printer)
1872 {
1873     return select_printer(printer.name);
1874 }
1875 
has_selection() const1876 bool PhysicalPrinterCollection::has_selection() const
1877 {
1878     return m_idx_selected != size_t(-1);
1879 }
1880 
unselect_printer()1881 void PhysicalPrinterCollection::unselect_printer()
1882 {
1883     m_idx_selected = size_t(-1);
1884     m_selected_preset.clear();
1885 }
1886 
is_selected(PhysicalPrinterCollection::ConstIterator it,const std::string & preset_name) const1887 bool PhysicalPrinterCollection::is_selected(PhysicalPrinterCollection::ConstIterator it, const std::string& preset_name) const
1888 {
1889     return  m_idx_selected      == size_t(it - m_printers.begin()) &&
1890             m_selected_preset   == preset_name;
1891 }
1892 
1893 
1894 namespace PresetUtils {
system_printer_model(const Preset & preset)1895 	const VendorProfile::PrinterModel* system_printer_model(const Preset &preset)
1896 	{
1897 		const VendorProfile::PrinterModel *out = nullptr;
1898 		if (preset.vendor != nullptr) {
1899 			auto *printer_model = preset.config.opt<ConfigOptionString>("printer_model");
1900 			if (printer_model != nullptr && ! printer_model->value.empty()) {
1901 				auto it = std::find_if(preset.vendor->models.begin(), preset.vendor->models.end(), [printer_model](const VendorProfile::PrinterModel &pm) { return pm.id == printer_model->value; });
1902 				if (it != preset.vendor->models.end())
1903 					out = &(*it);
1904 			}
1905 		}
1906 		return out;
1907 	}
1908 
system_printer_bed_model(const Preset & preset)1909     std::string system_printer_bed_model(const Preset& preset)
1910     {
1911         std::string out;
1912         const VendorProfile::PrinterModel* pm = PresetUtils::system_printer_model(preset);
1913         if (pm != nullptr && !pm->bed_model.empty()) {
1914             out = Slic3r::data_dir() + "/vendor/" + preset.vendor->id + "/" + pm->bed_model;
1915             if (!boost::filesystem::exists(boost::filesystem::path(out)))
1916                 out = Slic3r::resources_dir() + "/profiles/" + preset.vendor->id + "/" + pm->bed_model;
1917         }
1918         return out;
1919     }
1920 
system_printer_bed_texture(const Preset & preset)1921     std::string system_printer_bed_texture(const Preset& preset)
1922     {
1923         std::string out;
1924         const VendorProfile::PrinterModel* pm = PresetUtils::system_printer_model(preset);
1925         if (pm != nullptr && !pm->bed_texture.empty()) {
1926             out = Slic3r::data_dir() + "/vendor/" + preset.vendor->id + "/" + pm->bed_texture;
1927             if (!boost::filesystem::exists(boost::filesystem::path(out)))
1928                 out = Slic3r::resources_dir() + "/profiles/" + preset.vendor->id + "/" + pm->bed_texture;
1929         }
1930         return out;
1931     }
1932 } // namespace PresetUtils
1933 
1934 } // namespace Slic3r
1935