#include "save.h" namespace Lib_save { vector Save::get_error() { return this->vec_errors; } bool Save::get_state_save() { return this->flag_no_save; } void Save::set_data_global(std::map &map_global) { this->map_global = &map_global; } void Save::set_data_local(std::map &map_local) { this->map_local = &map_local; } void Save::set_data_gui(std::map &map_gui) { this->map_gui = &map_gui; } bool Save::check_save(string flag_save, string key_name) { std::map :: iterator iter_map_data; std::map :: iterator iter_map_data_old; std::map *map_data_old; if (flag_save == "system") { map_data_old = map_local; } else if (flag_save == "global") { map_data_old = map_global; } iter_map_data = (*map_gui).find(key_name); iter_map_data_old = (*map_data_old).find(key_name); if ((*map_local).find(key_name) != (*map_local).end() && (*map_global).find(key_name) != (*map_global).end()) { if ((*map_local)[key_name] != (*map_global)[key_name]) { return true; } } if (iter_map_data_old == (*map_data_old).end() && iter_map_data != (*map_gui).end()) { return true; } else if (iter_map_data->second != iter_map_data_old->second) { return true; } else if (iter_map_data->second.length() == 0 && iter_map_data_old->second.length() == 0) { return false; } else if (iter_map_data->second == iter_map_data_old->second) { return false; } else { return true; } return true; } int Save::get_count_error() { return process.get_count_error(); } string Save::get_cmd_error() { return process.get_cmd_error(); } void Save::set_count_error(int count_error) { process.set_count_error(count_error); } void Save::set_vec_params(vector& vec_param_names) { this->vec_param_names = &vec_param_names; } void Save::save(string sections, string str_flag_save) { string key = ""; string value = ""; string cmd = ""; string str_error = ""; this->flag_no_save = true; for (const auto &key: *vec_param_names) { if (map_gui->find(key) != map_gui->end()) { value = (*map_gui)[key]; if (this->check_save(str_flag_save, key)) { if (value.length() != 0) { cmd = "ubconfig --target " + str_flag_save + " set " + sections + " " + key + "=\"" + value + "\""; } else if (value.length() == 0) { cmd = "ubconfig --target " + str_flag_save + " remove " + sections + " " + key; } else { cmd = ""; } if (cmd.length() != 0) { process.call(cmd, ""); this->flag_no_save = false; str_error = process.get_cmd_error(); if (str_error.length() != 0) { this->vec_errors.push_back(str_error); str_error = ""; } } } } } } void Save::save_one_cmd(string cmd1, string cmd_remove, string str_flag_save) { string key = ""; string value = ""; string cmd = ""; this->flag_no_save = true; cmd = cmd1; string remove = cmd_remove; for (const auto &key: *vec_param_names) { if (map_gui->find(key) != map_gui->end()) { value = (*map_gui)[key]; if (this->check_save(str_flag_save, key)) { if (value.length() != 0) { cmd = cmd + key + "=\"" + value + "\" "; } else if (value.length() == 0) { remove = remove + key + " "; } } } } if (cmd.length() != cmd1.length()) { this->template_save(cmd); } if (cmd_remove.length() != remove.length()) { this->template_save(remove); } } void Save::template_save(string cmd) { string str_error = ""; process.call(cmd, ""); this->flag_no_save = false; str_error = process.get_cmd_error(); if (str_error.length() != 0) { this->vec_errors.push_back(str_error); str_error = ""; } } }