Implemented merging of system profiles from various vendors.

This commit is contained in:
bubnikv 2018-04-18 13:35:51 +02:00
parent 0711f84ea0
commit fa97a86751
4 changed files with 61 additions and 2 deletions

View File

@ -790,6 +790,29 @@ bool PresetCollection::select_preset_by_name_strict(const std::string &name)
return false; return false;
} }
// Merge one vendor's presets with the other vendor's presets, report duplicates.
std::vector<std::string> PresetCollection::merge_presets(PresetCollection &&other, const std::set<VendorProfile> &new_vendors)
{
std::vector<std::string> duplicates;
for (Preset &preset : other.m_presets) {
if (preset.is_default || preset.is_external)
continue;
Preset key(m_type, preset.name);
auto it = std::lower_bound(m_presets.begin() + 1, m_presets.end(), key);
if (it == m_presets.end() || it->name != preset.name) {
if (preset.vendor != nullptr) {
// Re-assign a pointer to the vendor structure in the new PresetBundle.
auto it = new_vendors.find(*preset.vendor);
assert(it != new_vendors.end());
preset.vendor = &(*it);
}
this->m_presets.emplace(it, std::move(preset));
} else
duplicates.emplace_back(std::move(preset.name));
}
return duplicates;
}
std::string PresetCollection::name() const std::string PresetCollection::name() const
{ {
switch (this->type()) { switch (this->type()) {

View File

@ -336,6 +336,9 @@ protected:
// This is a temporary state, which shall be fixed immediately by the following step. // This is a temporary state, which shall be fixed immediately by the following step.
bool select_preset_by_name_strict(const std::string &name); bool select_preset_by_name_strict(const std::string &name);
// Merge one vendor's presets with the other vendor's presets, report duplicates.
std::vector<std::string> merge_presets(PresetCollection &&other, const std::set<VendorProfile> &new_vendors);
private: private:
PresetCollection(); PresetCollection();
PresetCollection(const PresetCollection &other); PresetCollection(const PresetCollection &other);

View File

@ -178,6 +178,7 @@ std::string PresetBundle::load_system_presets()
// Here the vendor specific read only Config Bundles are stored. // Here the vendor specific read only Config Bundles are stored.
boost::filesystem::path dir = (boost::filesystem::path(data_dir()) / "vendor").make_preferred(); boost::filesystem::path dir = (boost::filesystem::path(data_dir()) / "vendor").make_preferred();
std::string errors_cummulative; std::string errors_cummulative;
bool first = true;
for (auto &dir_entry : boost::filesystem::directory_iterator(dir)) for (auto &dir_entry : boost::filesystem::directory_iterator(dir))
if (boost::filesystem::is_regular_file(dir_entry.status()) && boost::algorithm::iends_with(dir_entry.path().filename().string(), ".ini")) { if (boost::filesystem::is_regular_file(dir_entry.status()) && boost::algorithm::iends_with(dir_entry.path().filename().string(), ".ini")) {
std::string name = dir_entry.path().filename().string(); std::string name = dir_entry.path().filename().string();
@ -185,7 +186,25 @@ std::string PresetBundle::load_system_presets()
name.erase(name.size() - 4); name.erase(name.size() - 4);
try { try {
// Load the config bundle, flatten it. // Load the config bundle, flatten it.
if (first) {
// Reset this PresetBundle and load the first vendor config.
this->load_configbundle(dir_entry.path().string(), LOAD_CFGBNDLE_SYSTEM); this->load_configbundle(dir_entry.path().string(), LOAD_CFGBNDLE_SYSTEM);
first = false;
} else {
// Load the other vendor configs, merge them with this PresetBundle.
// Report duplicate profiles.
PresetBundle other;
other.load_configbundle(dir_entry.path().string(), LOAD_CFGBNDLE_SYSTEM);
std::vector<std::string> duplicates = this->merge_presets(std::move(other));
if (! duplicates.empty()) {
errors_cummulative += "Vendor configuration file " + name + " contains the following presets with names used by other vendors: ";
for (size_t i = 0; i < duplicates.size(); ++ i) {
if (i > 0)
errors_cummulative += ", ";
errors_cummulative += duplicates[i];
}
}
}
} catch (const std::runtime_error &err) { } catch (const std::runtime_error &err) {
errors_cummulative += err.what(); errors_cummulative += err.what();
errors_cummulative += "\n"; errors_cummulative += "\n";
@ -194,6 +213,18 @@ std::string PresetBundle::load_system_presets()
return errors_cummulative; return errors_cummulative;
} }
// Merge one vendor's presets with the other vendor's presets, report duplicates.
std::vector<std::string> PresetBundle::merge_presets(PresetBundle &&other)
{
this->vendors.insert(other.vendors.begin(), other.vendors.end());
std::vector<std::string> duplicate_prints = this->prints .merge_presets(std::move(other.prints), this->vendors);
std::vector<std::string> duplicate_filaments = this->filaments.merge_presets(std::move(other.filaments), this->vendors);
std::vector<std::string> duplicate_printers = this->printers .merge_presets(std::move(other.printers), this->vendors);
append(duplicate_prints, std::move(duplicate_filaments));
append(duplicate_prints, std::move(duplicate_printers));
return duplicate_prints;
}
static inline std::string remove_ini_suffix(const std::string &name) static inline std::string remove_ini_suffix(const std::string &name)
{ {
std::string out = name; std::string out = name;

View File

@ -120,6 +120,8 @@ public:
private: private:
std::string load_system_presets(); std::string load_system_presets();
// Merge one vendor's presets with the other vendor's presets, report duplicates.
std::vector<std::string> merge_presets(PresetBundle &&other);
// Set the "enabled" flag for printer vendors, printer models and printer variants // Set the "enabled" flag for printer vendors, printer models and printer variants
// based on the user configuration. // based on the user configuration.