ENH: [STUDIO-1871] merge changes of user presets from other sessions

Change-Id: I2841ff4e23b18ebe7cdf374b69a494a4c05a49ee
This commit is contained in:
chunmao.guo 2023-01-09 14:13:14 +08:00 committed by Lane.Wei
parent e71b5a88b7
commit ae959b7400
4 changed files with 28 additions and 15 deletions

View File

@ -1543,6 +1543,10 @@ bool PresetCollection::load_user_preset(std::string name, std::map<std::string,
BOOST_LOG_TRIVIAL(error) << "Error in a preset file: The preset \"" <<
name << "\" contains the following incorrect keys: " << incorrect_keys << ", which were removed";
if (need_update) {
if (iter->name == m_edited_preset.name && iter->is_dirty) {
// Keep modifies when update from remote
new_config.apply_only(m_edited_preset.config, m_edited_preset.config.diff(iter->config));
}
iter->config = new_config;
iter->updated_time = cloud_update_time;
iter->version = cloud_version.value();

View File

@ -553,6 +553,10 @@ PresetsConfigSubstitutions PresetBundle::load_user_presets(AppConfig &
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(" print's selected_idx %1%, selected_name %2%") %prints.get_selected_idx() %prints.get_selected_preset_name();
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(" filament's selected_idx %1%, selected_name %2%") %filaments.get_selected_idx() %filaments.get_selected_preset_name();
BOOST_LOG_TRIVIAL(info) << __FUNCTION__ << boost::format(" printers's selected_idx %1%, selected_name %2%") %printers.get_selected_idx() %printers.get_selected_preset_name();
// Sync removing
remove_users_preset(config, &my_presets);
std::map<std::string, std::map<std::string, std::string>>::iterator it;
for (it = my_presets.begin(); it != my_presets.end(); it++) {
std::string name = it->first;
@ -838,13 +842,26 @@ bool PresetBundle::validate_printers(const std::string &name, DynamicPrintConfig
#endif
}
void PresetBundle::remove_users_preset(AppConfig& config)
void PresetBundle::remove_users_preset(AppConfig &config, std::map<std::string, std::map<std::string, std::string>> *my_presets)
{
auto check_removed = [my_presets, this](Preset &preset) -> bool {
if (my_presets == nullptr) return true;
if (my_presets->find(preset.name) != my_presets->end()) return false;
if (!preset.sync_info.empty()) return false; // syncing, not remove
if (preset.setting_id.empty()) return false; // no id, not remove
// Saved preset is removed by another session
if (preset.is_dirty) {
preset.setting_id.clear();
return false;
}
preset.remove_files();
return true;
};
std::string preset_folder_user_id = config.get("preset_folder");
std::string printer_selected_preset_name = printers.get_selected_preset().name;
bool need_reset_printer_preset = false;
for (auto it = printers.begin(); it != printers.end();) {
if (it->is_user() && !it->user_id.empty() && it->user_id.compare(preset_folder_user_id) == 0) {
if (it->is_user() && !it->user_id.empty() && it->user_id.compare(preset_folder_user_id) == 0 && check_removed(*it)) {
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << boost::format(":printers erase %1%, type %2% user_id %3%") % it->name % Preset::get_type_string(it->type) % it->user_id;
if (it->name == printer_selected_preset_name)
need_reset_printer_preset = true;
@ -875,7 +892,7 @@ void PresetBundle::remove_users_preset(AppConfig& config)
bool need_reset_print_preset = false;
// remove preset if user_id is not current user
for (auto it = prints.begin(); it != prints.end();) {
if (it->is_user() && !it->user_id.empty() && it->user_id.compare(preset_folder_user_id) == 0) {
if (it->is_user() && !it->user_id.empty() && it->user_id.compare(preset_folder_user_id) == 0 && check_removed(*it)) {
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << boost::format(":prints erase %1%, type %2% user_id %3%")%it->name %Preset::get_type_string(it->type) %it->user_id;
if (it->name == selected_print_name)
need_reset_print_preset = true;
@ -895,7 +912,7 @@ void PresetBundle::remove_users_preset(AppConfig& config)
std::string selected_filament_name = filaments.get_selected_preset().name;
bool need_reset_filament_preset = false;
for (auto it = filaments.begin(); it != filaments.end();) {
if (it->is_user() && !it->user_id.empty() && it->user_id.compare(preset_folder_user_id) == 0) {
if (it->is_user() && !it->user_id.empty() && it->user_id.compare(preset_folder_user_id) == 0 && check_removed(*it)) {
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << boost::format(":filaments erase %1%, type %2% user_id %3%")%it->name %Preset::get_type_string(it->type) %it->user_id;
if (it->name == selected_filament_name)
need_reset_filament_preset = true;
@ -1348,7 +1365,7 @@ void PresetBundle::load_selections(AppConfig &config, const PresetPreferences& p
void PresetBundle::export_selections(AppConfig &config)
{
assert(this->printers.get_edited_preset().printer_technology() != ptFFF || filament_presets.size() >= 1);
assert(this->printers.get_edited_preset().printer_technology() != ptFFF || filament_presets.size() > 1 || filaments.get_selected_preset_name() == filament_presets.front());
//assert(this->printers.get_edited_preset().printer_technology() != ptFFF || filament_presets.size() > 1 || filaments.get_selected_preset_name() == filament_presets.front());
config.clear_section("presets");
config.set("presets", PRESET_PRINT_NAME, prints.get_selected_preset_name());
config.set("presets", PRESET_FILAMENT_NAME, filament_presets.front());

View File

@ -51,7 +51,7 @@ public:
PresetsConfigSubstitutions load_user_presets(AppConfig &config, std::map<std::string, std::map<std::string, std::string>>& my_presets, ForwardCompatibilitySubstitutionRule rule);
PresetsConfigSubstitutions import_presets(std::vector<std::string> &files, std::function<int(std::string const &)> override_confirm, ForwardCompatibilitySubstitutionRule rule);
void save_user_presets(AppConfig& config, std::vector<std::string>& need_to_delete_list);
void remove_users_preset(AppConfig &config);
void remove_users_preset(AppConfig &config, std::map<std::string, std::map<std::string, std::string>> * my_presets = nullptr);
void update_user_presets_directory(const std::string preset_folder);
void remove_user_presets_directory(const std::string preset_folder);
void update_system_preset_setting_ids(std::map<std::string, std::map<std::string, std::string>>& system_presets);

View File

@ -3939,9 +3939,9 @@ bool Tab::select_preset(std::string preset_name, bool delete_current /*=false*/,
try {
//BBS delete preset
Preset &current_preset = m_presets->get_selected_preset();
current_preset.sync_info = "delete";
if (!current_preset.setting_id.empty()) {
BOOST_LOG_TRIVIAL(info) << "delete preset = " << current_preset.name << ", setting_id = " << current_preset.setting_id;
m_presets->set_sync_info_and_save(current_preset.name, current_preset.setting_id, "delete");
wxGetApp().delete_preset_from_cloud(current_preset.setting_id);
}
BOOST_LOG_TRIVIAL(info) << boost::format("will delete current preset...");
@ -4559,14 +4559,6 @@ void Tab::delete_preset()
if (m_type == Preset::TYPE_PRINTER && !physical_printers.empty())
physical_printers.delete_preset_from_printers(current_preset.name);
//BBS delete preset
//will delete in select_preset
current_preset.sync_info = "delete";
if (!current_preset.setting_id.empty()) {
BOOST_LOG_TRIVIAL(info) << "delete preset = " << current_preset.name << ", setting_id = " << current_preset.setting_id;
wxGetApp().delete_preset_from_cloud(current_preset.setting_id);
}
// Select will handle of the preset dependencies, of saving & closing the depending profiles, and
// finally of deleting the preset.
this->select_preset("", true);