You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ubl-settings-bootloader/source/ubl-settings-bootloader.cc

1137 lines
42 KiB

#include "ubl-settings-bootloader.h"
using namespace std;
int socket_ext_id_I = 0;
int socket_trd_id_I = 0;
bool flag_save = false;
bool flag_save_local = false;
bool flag_save_global = false;
bool flag_load_global = false;
bool flag_lock_help = false;
string version_application = "1.0";
CmdArgParser::CmdArgParser(const string& p_name, const string& p_description, const string& p_help)
: Glib::OptionGroup{p_name, p_description, p_help} {
Glib::OptionEntry socketIDArg;
socketIDArg.set_long_name("socket-id");
socketIDArg.set_flags(Glib::OptionEntry::FLAG_IN_MAIN);
socketIDArg.set_description("Settings manager socket");
Glib::OptionEntry socketExtId;
socketExtId.set_long_name("socket-ext-id");
socketExtId.set_flags(Glib::OptionEntry::FLAG_IN_MAIN);
socketExtId.set_description("Settings manager secondary socket");
Glib::OptionEntry socketTrdId;
socketTrdId.set_long_name("socket-trd-id");
socketTrdId.set_flags(Glib::OptionEntry::FLAG_IN_MAIN);
socketTrdId.set_description("Settings manager secondary socket");
add_entry(socketIDArg, m_socketID);
add_entry(socketExtId, socket_ext_id_I);
add_entry(socketTrdId, socket_trd_id_I);
}
::Window CmdArgParser::GetSocketID() const{
return m_socketID;
}
SettingsPlug::SettingsPlug(::Window p_socketID, Glib::RefPtr<Gtk::Builder> builder)
: Gtk::Plug{p_socketID} {
MainWindow* wnd = nullptr;
builder->get_widget_derived("window", wnd);
builder->get_widget("plugBox", plugBox);
plugBox->get_parent()->remove(*plugBox);
add(*plugBox);
show_all_children();
}
int MainWindow::check_root() {
if (geteuid() != 0) {
return 1;
}
return 0;
}
MainWindow::MainWindow(BaseObjectType* obj, Glib::RefPtr<Gtk::Builder> const& builder)
: Gtk::ApplicationWindow(obj), builder{builder} {
this->builder = builder;
this->settings();
}
MainWindow::MainWindow(Glib::RefPtr<Gtk::Builder> const& builder) {
this->builder = builder;
this->settings();
}
void MainWindow::settings(){
map_cmd_error[""] = "";
vec_param_names.push_back("GRUB_TIMEOUT");
vec_param_names.push_back("GRUB_CMDLINE_LINUX");
vec_param_names.push_back("GRUB_TERMINAL_INPUT");
vec_param_names.push_back("GRUB_TERMINAL_OUTPUT");
vec_param_names.push_back("GRUB_BOOT_SILENT");
vec_param_names.push_back("GRUB_USER");
vec_param_names.push_back("GRUB_PASSWORD");
vec_param_names.push_back("GRUB_DEFAULT");
this->get_builder();
this->localization();
this->add_CSS();
Gtk::Widget *boxWidget;
builder->get_widget("boxColor", boxWidget);
overHead->add_overlay(*boxWidget);
btnBoxAboutDialog->set_visible(false);
ubl_make_plugs(boxSave,boxButton, socket_ext_id_I, socket_trd_id_I);
#ifdef WEBKIT_FOUND
one = WEBKIT_WEB_VIEW( webkit_web_view_new() );
three = Glib::wrap(GTK_WIDGET(one));
wndWeb->add(*three);
#endif
this->flag_block_gui();
if (this->check_root() == 0) {
spbSecond->set_range(1, 60);
spbSecond->set_increments(1.0, 1.0);
spbSecond->set_value(1);
this->fill_in_view();
this->change_password_protecc();
this->change_security_login();
this->set_active_boot_second();
this->str_last_launched_os = this->pars_last_launched_os();
vector<string> os_control_list;
this->pars_os(os_control_list);
this->download_local_cfg();
this->download_globl_cfg();
std::map<string, vector<string>> map_list_os = format_os_list(os_control_list);
obj_menu_os.set_builder(builder, path_glade);
obj_menu_os.set_map(map_list_os);
obj_menu_os.set_entry(entrListOS);
// obj_menu_os.set_last_launched_os(map_global_cmd_selection["GRUB_DEFAULT"]);
obj_menu_os.set_last_launched_os(this->str_last_launched_os);
obj_menu_os.init();
obj_menu_os.event();
}
else{
boxWidgetAll->set_sensitive(false);
btnLoad->set_sensitive(false);
btnSave->set_sensitive(false);
imgInfo->set_from_icon_name(icon_warning, Gtk::ICON_SIZE_MENU);
info_status_app(info_box_error_css);
lblWarning->set_text(program_as_root);
this->set_active_boot_second();
this->fill_in_view();
}
this->event();
}
map<string, vector<string>> MainWindow::format_os_list(vector<string> &os_control_list) {
std::map<string, vector<string>> map_list_os;
set<string> set_key;
for (string& name_os : os_control_list) {
if (name_os.find(">") != string::npos) {
set_key.insert(name_os.substr(0,name_os.find(">")+1));
}
else {
vector<string> vec_os;
//vec_os.push_back(" ");
map_list_os[name_os] = vec_os;
}
}
set<string>::iterator it = set_key.begin();
for (int index = 1; it != set_key.end(); index++, it++) {
bool flag = false;
string name_os_buff = "";
vector<string> vec_os_values;
for (string& name_os : os_control_list) {
if (name_os.find(*it) != string::npos) {
flag = true;
vec_os_values.push_back(name_os.substr(name_os.find(">")+1, name_os.length() - 1));
name_os_buff = *it;
}
}
if (flag) {
map_list_os[name_os_buff] = vec_os_values;
}
}
return map_list_os;
}
vector<string> MainWindow::pars_os(vector<string> &os_control_list) {
string str_list_os = obj_process_call.call_all_sections(cmd_pars_list_os);
os_control_list = Utils::split(str_list_os, '\n');
return os_control_list;
}
string MainWindow::pars_last_launched_os() {
string str_list_os = obj_process_call.call_all_sections(cmd_last_launched_os);
Utils::str_replace_all(str_list_os, "\n" , "");
return str_list_os;
}
void MainWindow::flag_block_gui() {
if (flag_load_global == true) {
btnLoadGlob->set_sensitive(false);
}
if (flag_save_local == true && flag_save_global == true) {
btnSave->set_sensitive(false);
}
if (flag_save == true) {
btnSave->set_sensitive(false);
}
if (flag_save_local == true) {
btnSaveLocal->set_sensitive(false);
btnSaveLocalGlob->set_sensitive(false);
}
if (flag_save_global == true) {
btnSaveGlob->set_sensitive(false);
btnSaveLocalGlob->set_sensitive(false);
}
if (flag_lock_help == true) {
btnSynopsis->set_sensitive(false);
}
}
void MainWindow::fill_in_view() {
list_store_kernel = Gtk::ListStore::create(m_columns);
list_store_IPT = Gtk::ListStore::create(m_columns);
list_store_OTT = Gtk::ListStore::create(m_columns);
treeViewKernel->set_model(list_store_kernel);
treeViewIPT->set_model(list_store_IPT);
treeViewOTT->set_model(list_store_OTT);
vec_Option_kernel = this->read_file_and_view(kernel_csv, row_kernel, list_store_kernel);
vec_Option_IPT = this->read_file_and_view(IPT_csv, row_IPT, list_store_IPT);
vec_Option_OTT = this->read_file_and_view(OTT_csv, row_OTT, list_store_OTT);
size_kernel = vec_Option_kernel.size();
size_IPT = vec_Option_IPT.size();
size_OTT = vec_Option_OTT.size();
this->view_add_colums(*treeViewKernel);
this->view_add_colums(*treeViewIPT);
this->view_add_colums(*treeViewOTT);
}
void MainWindow::item_selected_kernel(const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator&) {
auto selection_IPT = treeViewIPT->get_selection();
auto selection_OTT = treeViewOTT->get_selection();
selection_IPT->unselect_all();
selection_OTT->unselect_all();
if (flag_blocked_tree_view == false) {
string str_flags = template_item_selected(size_kernel, list_store_kernel, "GRUB_CMDLINE_LINUX");
Utils::str_replace_all(str_flags, " \"", "\"");
Utils::str_replace_all(str_flags, "\"", "");
map_cmd_selection["GRUB_CMDLINE_LINUX"] = str_flags;
Utils::str_replace_all(str_flags, " ", ", ");
entryKernel->set_text(str_flags);
}
}
void MainWindow::item_selected_OTT(const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator&) {
auto selection_kernel = treeViewKernel->get_selection();
auto selection_IPT = treeViewIPT->get_selection();
selection_kernel->unselect_all();
selection_IPT->unselect_all();
if (flag_blocked_tree_view == false) {
string str_flags = template_item_selected(size_OTT, list_store_OTT, "GRUB_TERMINAL_OUTPUT");
Utils::str_replace_all(str_flags, " \"", "\"");
Utils::str_replace_all(str_flags, "\"", "");
map_cmd_selection["GRUB_TERMINAL_OUTPUT"] = str_flags;
Utils::str_replace_all(str_flags, " ", ", ");
entryOTT->set_text(str_flags);
}
}
void MainWindow::item_selected_IPT(const Gtk::TreeModel::Path&, const Gtk::TreeModel::iterator&) {
auto selection_kernel = treeViewKernel->get_selection();
auto selection_OTT = treeViewOTT->get_selection();
selection_kernel->unselect_all();
selection_OTT->unselect_all();
if (flag_blocked_tree_view == false) {
string str_flags = template_item_selected(size_IPT, list_store_IPT, "GRUB_TERMINAL_INPUT");
Utils::str_replace_all(str_flags, " \"", "\"");
Utils::str_replace_all(str_flags, "\"", "");
map_cmd_selection["GRUB_TERMINAL_INPUT"] = str_flags;
Utils::str_replace_all(str_flags, " ", ", ");
entryIPT->set_text(str_flags);
}
}
void MainWindow::get_download_mode(std::map <string, string> &map_temp) {
std::map <string, string>:: iterator iter_map_data;
iter_map_data = map_temp.find("GRUB_BOOT_SILENT");
if (iter_map_data != map_temp.end()) {
string value = iter_map_data->second;
if (value.length() != 0) {
Utils::str_replace_all(value, "\"","");
if (value == "splash") {
cmbDownloadMode->set_active(0);
}
else if (value == "splash quiet ub.silent") {
cmbDownloadMode->set_active(1);
}
else if (value == "quiet ub.silent") {
cmbDownloadMode->set_active(2);
}
else if (value == "plymouth.enable=0") {
cmbDownloadMode->set_active(3);
}
else{
cmbDownloadMode->set_active(-1);
}
}
else {
cmbDownloadMode->set_active(-1);
}
}
else {
cmbDownloadMode->set_active(-1);
}
}
void MainWindow::set_download_mode() {
int index = cmbDownloadMode->get_active_row_number();
string key = "";
if (index == 0) {
key = "splash";
}
else if (index == 1) {
key = "splash quiet ub.silent";
}
else if (index == 2) {
key = "quiet ub.silent";
}
else if (index == 3) {
key = "plymouth.enable=0";
}
else{
return;
}
map_cmd_selection["GRUB_BOOT_SILENT"] = key;
}
void MainWindow::set_security_login() {
bool index = chbSecurityLogin->get_active();
string key = "";
if (index == false) {
key = "";
}
else if (index == true) {
key = entrySecurityLogin->get_text();
}
map_cmd_selection["GRUB_USER"] = key;
}
void MainWindow::get_security_login(std::map <string, string> &map_temp) {
// Todo;
std::map <string, string>:: iterator iter_map_data;
iter_map_data = map_temp.find("GRUB_USER");
if (iter_map_data != map_temp.end()) {
string value = iter_map_data->second;
if (value.length() != 0) {
Utils::str_replace_all(value, " ","");
Utils::str_replace_all(value, "\"","");
chbSecurityLogin->set_active(1);
entrySecurityLogin->set_sensitive(true);
entrySecurityLogin->set_text(value);
}
else {
chbSecurityLogin->set_active(0);
entrySecurityLogin->set_text("");
entrySecurityLogin->set_sensitive(false);
}
}
else {
chbSecurityLogin->set_active(0);
entrySecurityLogin->set_text("");
entrySecurityLogin->set_sensitive(false);
}
}
void MainWindow::set_password_protec() {
bool index = chbPasswordProtecc->get_active();
string key = "";
if (index == false){
key = "";
}
else if (index == true) {
key = entryPasswordProtecc->get_text();
}
if (key != "************") {
map_cmd_selection["GRUB_PASSWORD"] = key;
}
}
void MainWindow::get_password_protec(std::map <string, string> &map_temp) {
std::map <string, string>:: iterator iter_map_data;
iter_map_data = map_temp.find("GRUB_PASSWORD");
if (iter_map_data != map_temp.end()) {
string value = iter_map_data->second;
if (value.length() != 0) {
Utils::str_replace_all(value, "\"","");
chbPasswordProtecc->set_active(1);
entryPasswordProtecc->set_text("************");
entryPasswordProtecc->set_sensitive(true);
}
else{
chbPasswordProtecc->set_active(0);
entryPasswordProtecc->set_text("");
}
}
else {
chbPasswordProtecc->set_active(-1);
}
}
string MainWindow::template_item_selected(int size, Glib::RefPtr<Gtk::ListStore> &list_store, string key) {
string cmds = map_cmd_selection[key];
Utils::str_replace_all(cmds, "\"", "");
for (int index = 0; index < size; index++) {
Gtk::TreeModel::Row row = list_store->children()[index];
if(row) {
string param = row[m_columns.name] + "";
if (row[m_columns.check_button] == true && cmds.find(param) == string::npos) {
cmds += " " + param + " ";
}
if (row[m_columns.check_button] == false && cmds.find(param) != string::npos) {
string param_del = " " + param;
Utils::str_remove(cmds, param_del);
Utils::str_remove(cmds, param);
}
}
}
Utils::str_replace_all(cmds, " ", " ");
if (cmds.length() > 0) {
if (cmds[0] == ' ') {
cmds = cmds.substr(1, cmds.length() - 1);
}
}
cmds = "\"" + cmds + "\"";
return cmds;
}
vector<string> MainWindow::read_file_and_view(string file_name ,Gtk::TreeModel::Row &row, Glib::RefPtr<Gtk::ListStore> list_store_m) {
std::array<std::vector<string>, 5> arr_view = Utils::read_csv(string(path_resources) + "/" + file_name);
std::vector<string> vec_Option = arr_view[0];
for (size_t index = 0; index < vec_Option.size(); index++) {
this->view_add_cell(row, list_store_m , arr_view, index);
}
return vec_Option;
}
void MainWindow::view_add_colums(Gtk::TreeView &treeView) {
treeView.append_column_editable(str_active, m_columns.check_button);
treeView.append_column(str_option, m_columns.name);
treeView.append_column(str_description, m_columns.description);
}
void MainWindow::view_add_cell(Gtk::TreeModel::Row &row, Glib::RefPtr<Gtk::ListStore> list_store_m , std::array<std::vector<string>, 5> &arr_view , size_t index) {
row = *(list_store_m->append());
bool flag_chb = false;
string name = arr_view[0][index];
row[m_columns.check_button] = flag_chb;
row[m_columns.name] = _(name.c_str());
row[m_columns.description] = _(arr_view[1][index].c_str());
}
void MainWindow::get_builder() {
builder->get_widget("lblHeaderAboutTopic", lblHeaderAboutTopic);
builder->get_widget("chbLoadVariantSelectionTimer", chbLoadVariantSelectionTimer);
builder->get_widget("lblInfoCommand", lblInfoCommand);
builder->get_widget("lblInfoCommand1", lblInfoCommand1);
builder->get_widget("lblInfoCommand2", lblInfoCommand2);
builder->get_widget("lblInfoDefaultDonw", lblInfoDefaultDonw);
builder->get_widget("lblInfoSeconds", lblInfoSeconds);
builder->get_widget("lblLoadMenu", lblLoadMenu);
builder->get_widget("lblInfoInputTerminal", lblInfoInputTerminal);
builder->get_widget("lblInfoOutputTerminal", lblInfoOutputTerminal);
builder->get_widget("lblInfoSeconds", lblInfoSeconds);
builder->get_widget("lblHeaderName", lblHeaderName);
builder->get_widget("lblInfoHead", lblInfoHead);
builder->get_widget("spbSecond", spbSecond);
builder->get_widget("entryKernel", entryKernel);
builder->get_widget("entryIPT", entryIPT);
builder->get_widget("entryOTT", entryOTT);
builder->get_widget("boxColor", boxColor);
builder->get_widget("boxWidgetAll", boxWidgetAll);
builder->get_widget("aboutWindows", aboutWindows);
builder->get_widget("btnSynopsis", btnSynopsis);
builder->get_widget("btnAbout", btnAbout);
builder->get_widget("btnLoadLocal", btnLoadLocal);
builder->get_widget("btnLoadGlob", btnLoadGlob);
builder->get_widget("btnSaveLocalGlob", btnSaveLocalGlob);
builder->get_widget("btnSaveLocal", btnSaveLocal);
builder->get_widget("btnSaveGlob", btnSaveGlob);
builder->get_widget("btnSettings", btnSettings);
builder->get_widget("btnLoad", btnLoad);
builder->get_widget("btnSave", btnSave);
builder->get_widget("boxButton", boxButton);
builder->get_widget("boxSave", boxSave);
builder->get_widget("boxInfoError", boxInfoError);
builder->get_widget("lblWarning", lblWarning);
builder->get_widget("imgInfo", imgInfo);
builder->get_widget("btnBoxAboutDialog", btnBoxAboutDialog);
builder->get_widget("treeViewKernel", treeViewKernel);
builder->get_widget("treeViewOTT", treeViewOTT);
builder->get_widget("treeViewIPT", treeViewIPT);
builder->get_widget("imgBG", imgBG);
builder->get_widget("overHead", overHead);
builder->get_widget("lblDownloadMode", lblDownloadMode);
builder->get_widget("cmbDownloadMode", cmbDownloadMode);
builder->get_widget("chbSecurityLogin", chbSecurityLogin);
builder->get_widget("chbPasswordProtecc", chbPasswordProtecc);
builder->get_widget("entryPasswordProtecc", entryPasswordProtecc);
builder->get_widget("entrySecurityLogin", entrySecurityLogin);
#ifdef WEBKIT_FOUND
builder->get_widget("wndWeb", wndWeb);
#endif
builder->get_widget("lblHeadeWndWeb", lblHeadeWndWeb);
builder->get_widget("lblwebHeaderName", lblwebHeaderName);
builder->get_widget("lblhelpText", lblhelpText);
builder->get_widget("lblhelpHeader", lblhelpHeader);
builder->get_widget("chkAlwaysOpenHelp", chkAlwaysOpenHelp);
builder->get_widget("btnReadHelp", btnReadHelp);
builder->get_widget("btnCancelHelp", btnCancelHelp);
builder->get_widget("wndShowWeb", wndShowWeb);
builder->get_widget("btnOpenMenuOS", btnOpenMenuOS);
builder->get_widget("entrListOS", entrListOS);
}
void MainWindow::localization(){
time_t now = time(0);
tm *ltm = localtime(&now);
unsigned int year = 1900+ ltm->tm_year;
string str_authors = string(copyright) + to_string(year);
aboutWindows->set_program_name(name_app);
aboutWindows->set_copyright(str_authors);
aboutWindows->set_comments(name_app);
aboutWindows->set_website(web_url);
aboutWindows->set_version(_(version_application.c_str()));
aboutWindows->set_website_label(project_Home_page);
lblInfoHead->set_text(configuring_system_boot);
lblHeaderName->set_text(name_app);
aboutWindows->set_comments(system_boot);
lblHeaderAboutTopic->set_label(str_about);
aboutWindows->set_website_label(project_Home_page);
lblInfoCommand->set_text(command_line_parameters);
lblInfoCommand1->set_text(command_line_parameters);
lblInfoCommand2->set_text(command_line_parameters);
lblInfoDefaultDonw->set_text(load_default);
lblInfoSeconds->set_text(seconds);
lblLoadMenu->set_text(download_menu);
lblInfoInputTerminal->set_text(terminal_for_input);
lblInfoOutputTerminal->set_text(terminal_for_output);
chbLoadVariantSelectionTimer->set_label(boot_selection_menu_timer);
btnSynopsis->set_label(str_help);
btnAbout->set_label(str_about_1);
lblHeadeWndWeb->set_text(name_app);
btnSaveLocalGlob->set_label(save_all);
btnSaveLocal->set_label(save_local);
btnSaveGlob->set_label(save_global);
btnLoadGlob->set_label(load_global);
btnLoadLocal->set_label(load_local);
btnSave->set_label(str_save);
btnLoad->set_label(str_load);
this->set_title(name_app);
// Todo: check
entrySecurityLogin->set_tooltip_text(loading_animation_without);
entryPasswordProtecc->set_tooltip_text(user_password_configuration_edit_mode);
chbSecurityLogin->set_tooltip_text(loading_animation_without);
chbPasswordProtecc->set_tooltip_text(user_password_configuration_edit_mode);
lblDownloadMode->set_text(boot_screen_display_mode);
chbSecurityLogin->set_label(boot_menu_user_name);
chbPasswordProtecc->set_label(boot_menu_user_password);
cmbDownloadMode->append(loading_animation_with);
cmbDownloadMode->append(loading_animation_without);
cmbDownloadMode->append(no_loading_animation);
cmbDownloadMode->append(lack_loading_animation);
lblhelpHeader->set_text(read_documentation_web);
lblhelpText->set_text(_(redirected_documentation));
btnReadHelp->set_label(read_online);
btnCancelHelp->set_label(cancel);
chkAlwaysOpenHelp->set_label(always_redirect);
lblwebHeaderName->set_label(name_app);
}
vector<string> MainWindow::get_setting_entry_all(string key, std::map <string, string>* map_temp) {
vector<string> vec_params;
std::map <string, string>:: iterator iter_map_data;
iter_map_data = (*map_temp).find(key);
if (iter_map_data != (*map_temp).end()) {
string value = iter_map_data->second;
if (value.length() != 0) {
Utils::str_replace_all(value, "\"", "");
vec_params = Utils::split(value, ' ');
Utils::str_replace_all(value, " ", ", ");
Utils::str_remove(value, ",");
(*map_temp)[key] = value;
}
}
return vec_params;
}
bool MainWindow::focus_out_txt_password(GdkEventFocus*) {
if (entryPasswordProtecc->get_text().length() == 0) {
info_status_app(info_box_error_css);
imgInfo->set_from_icon_name(icon_warning, Gtk::ICON_SIZE_MENU);
lblWarning->set_text(enter_password);
boxSave->set_sensitive(false);
}
else {
info_status_app(info_box_ok_css);
imgInfo->set_from_icon_name(icon_checked, Gtk::ICON_SIZE_MENU);
lblWarning->set_text("");
boxSave->set_sensitive(true);
string key = entryPasswordProtecc->get_text();
if (key != "************") {
map_cmd_selection["GRUB_PASSWORD"] = key;
}
}
return true;
}
bool MainWindow::focus_out_txt_login(GdkEventFocus*) {
if (entrySecurityLogin->get_text().length() == 0) {
info_status_app(info_box_error_css);
imgInfo->set_from_icon_name(icon_warning, Gtk::ICON_SIZE_MENU);
lblWarning->set_text(enter_login);
boxSave->set_sensitive(false);
}
else {
info_status_app(info_box_ok_css);
imgInfo->set_from_icon_name(icon_checked, Gtk::ICON_SIZE_MENU);
lblWarning->set_text("");
map_cmd_selection["GRUB_USER"] = entrySecurityLogin->get_text();
boxSave->set_sensitive(true);
}
return true;
}
bool MainWindow::focus_out_txt_kernel(GdkEventFocus*) {
this->set_entry_to_tree_view(list_store_kernel, *entryKernel, vec_Option_kernel, size_kernel, "GRUB_CMDLINE_LINUX");
return true;
}
bool MainWindow::focus_out_txt_IPT(GdkEventFocus*) {
this->set_entry_to_tree_view(list_store_IPT, *entryIPT, vec_Option_IPT, size_IPT, "GRUB_TERMINAL_INPUT");
return true;
}
bool MainWindow::focus_out_txt_OTT(GdkEventFocus*) {
this->set_entry_to_tree_view(list_store_OTT, *entryOTT, vec_Option_OTT, size_OTT, "GRUB_TERMINAL_OUTPUT");
return true;
}
void MainWindow::set_entry_to_tree_view(Glib::RefPtr<Gtk::ListStore> &list_store, Gtk::Entry &entry, std::vector<std::string> &vec_Option, int size, string key) {
flag_blocked_tree_view = true;
vector<string> vec_params;
std::string text = entry.get_text();
Utils::str_replace_all(text, " ", "");
vec_params = Utils::split(text, ',');
for (auto &name: vec_Option) {
this->set_row(list_store, size, name, false);
}
for (auto &name: vec_params) {
this->set_row(list_store, size, name, true);
}
Utils::str_remove(text, ",");
if (text.length() > 0) {
if (text[0] == ' ') {
text = text.substr(1, text.length() - 1);
}
}
if (text.find("\"") == string::npos && text.length() != 0) {
text = "\"" + text + "\"";
}
map_cmd_selection[key] = text;
flag_blocked_tree_view = false;
}
void MainWindow::get_menu_boot(std::map <string, string> &map_temp) {
std::map <string, string>:: iterator iter_map_data;
iter_map_data = map_temp.find("GRUB_TIMEOUT");
if (iter_map_data != map_temp.end()) {
if (iter_map_data->second.length() > 0) {
spbSecond->set_value(atoi(iter_map_data->second.c_str()));
chbLoadVariantSelectionTimer->set_active(true);
spbSecond->set_sensitive(true);
lblInfoSeconds->set_sensitive(true);
}
else {
spbSecond->set_value(1);
chbLoadVariantSelectionTimer->set_active(false);
spbSecond->set_sensitive(false);
lblInfoSeconds->set_sensitive(true);
}
}
else {
spbSecond->set_value(1);
chbLoadVariantSelectionTimer->set_active(false);
spbSecond->set_sensitive(false);
lblInfoSeconds->set_sensitive(true);
}
}
void MainWindow::cmd_entry_all(Gtk::Entry &entry, string cmd_settings, string cmd_remove) {
string cmds = entry.get_text();
Utils::str_remove(cmds, ",");
if (cmds.length() != 0) {
cmd_settings += "\"" + cmds + "\"";
Utils::str_replace_all(cmd_settings, " \"", "\"");
obj_process_system.call(cmd_settings, "");
}
else {
obj_process_system.call(cmd_remove, "");
}
}
void MainWindow::event(){
btnOpenMenuOS->signal_clicked().connect([&]() {obj_menu_os.show();});
btnCancelHelp->signal_clicked().connect([&]() {wndShowWeb->hide();});
chkAlwaysOpenHelp->signal_toggled().connect([&]() {flag_open_browser = true;});
btnReadHelp->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::open_browser));
btnSynopsis->signal_activate().connect(sigc::mem_fun(*this, &MainWindow::synopsis_show));
btnAbout->signal_activate().connect([&]() {aboutWindows->show();});
btnLoadLocal->signal_activate().connect(sigc::mem_fun(*this, &MainWindow::download_local_cfg));
btnLoadGlob->signal_activate().connect(sigc::mem_fun(*this, &MainWindow::download_globl_cfg));
btnSaveLocalGlob->signal_activate().connect([&]() {wrapper_save_all_cfg();});
btnSaveLocal->signal_activate().connect([&]() {wrapper_save_local_cfg();});
btnSaveGlob->signal_activate().connect([&]() {wrapper_save_global_cfg();});
entryKernel->signal_focus_out_event().connect(sigc::mem_fun(*this, &MainWindow::focus_out_txt_kernel));
entryIPT->signal_focus_out_event().connect(sigc::mem_fun(*this, &MainWindow::focus_out_txt_IPT));
entryOTT->signal_focus_out_event().connect(sigc::mem_fun(*this, &MainWindow::focus_out_txt_OTT));
entrySecurityLogin->signal_focus_out_event().connect(sigc::mem_fun(*this, &MainWindow::focus_out_txt_login));
entryPasswordProtecc->signal_focus_out_event().connect(sigc::mem_fun(*this, &MainWindow::focus_out_txt_password));
chbPasswordProtecc->signal_toggled().connect([&]() {change_password_protecc();});
chbSecurityLogin->signal_toggled().connect([&]() {change_security_login();});
if (this->check_root() == 0) {
Glib::RefPtr<Gtk::TreeModel> treeViewKernelModel = treeViewKernel->get_model();
treeViewKernelModel->signal_row_changed().connect(sigc::mem_fun(*this, &MainWindow::item_selected_kernel));
Glib::RefPtr<Gtk::TreeModel> treeViewIPTModel = treeViewIPT->get_model();
treeViewIPTModel->signal_row_changed().connect(sigc::mem_fun(*this, &MainWindow::item_selected_IPT));
Glib::RefPtr<Gtk::TreeModel> treeViewOTTModel = treeViewOTT->get_model();
treeViewOTTModel->signal_row_changed().connect(sigc::mem_fun(*this, &MainWindow::item_selected_OTT));
entryKernel->signal_focus_out_event().connect(sigc::mem_fun(*this, &MainWindow::focus_out_txt_kernel));
entryIPT->signal_focus_out_event().connect(sigc::mem_fun(*this, &MainWindow::focus_out_txt_IPT));
entryOTT->signal_focus_out_event().connect(sigc::mem_fun(*this, &MainWindow::focus_out_txt_OTT));
entryKernel->signal_focus_in_event().connect(sigc::mem_fun(*this, &MainWindow::focus_tree_view));
entryIPT->signal_focus_in_event().connect(sigc::mem_fun(*this, &MainWindow::focus_tree_view));
entryOTT->signal_focus_in_event().connect(sigc::mem_fun(*this, &MainWindow::focus_tree_view));
}
this->signal_delete_event().connect(sigc::mem_fun(*this, &MainWindow::gui_exit));
chbLoadVariantSelectionTimer->signal_toggled().connect(sigc::mem_fun(*this, &MainWindow::set_active_boot_second));
}
bool MainWindow::focus_tree_view(GdkEventFocus*) {
treeViewKernel->set_can_focus(false);
treeViewIPT->set_can_focus(false);
treeViewOTT->set_can_focus(false);
treeViewKernel->collapse_all();
treeViewIPT->collapse_all();
treeViewOTT->collapse_all();
return true;
}
bool MainWindow::gui_exit(GdkEventAny*) {
exit(1);
return true;
}
void MainWindow::change_security_login() {
bool index = chbSecurityLogin->get_active();
if (index == false) {
entrySecurityLogin->set_sensitive(false);
entrySecurityLogin->set_text("");
boxSave->set_sensitive(true);
info_status_app(info_box_ok_css);
imgInfo->set_from_icon_name(icon_checked, Gtk::ICON_SIZE_MENU);
lblWarning->set_text("");
}
else if (index == true) {
entrySecurityLogin->set_sensitive(true);
entrySecurityLogin->set_text("");
boxSave->set_sensitive(true);
}
}
void MainWindow::get_default_load(std::map <string, string> &map_temp) {
std::map <string, string>:: iterator iter_map_data;
iter_map_data = map_temp.find("GRUB_DEFAULT");
if (iter_map_data != map_temp.end()) {
string value = iter_map_data->second;
if (value.length() != 0) {
entrListOS->set_text(value);
}
else {
entrListOS->set_text("");
}
}
else {
entrListOS->set_text("");
}
}
void MainWindow::change_password_protecc() {
// Todo;
bool index = chbPasswordProtecc->get_active();
if (index == false) {
entryPasswordProtecc->set_sensitive(false);
entryPasswordProtecc->set_text("");
boxSave->set_sensitive(true);
info_status_app(info_box_ok_css);
imgInfo->set_from_icon_name(icon_checked, Gtk::ICON_SIZE_MENU);
lblWarning->set_text("");
}
else if (index == true) {
entryPasswordProtecc->set_sensitive(true);
entryPasswordProtecc->set_text("");
boxSave->set_sensitive(true);
}
else {
boxSave->set_sensitive(true);
}
}
void MainWindow::set_active_boot_second() {
spbSecond->set_sensitive(chbLoadVariantSelectionTimer->get_active());
lblInfoSeconds->set_sensitive(chbLoadVariantSelectionTimer->get_active());
if (chbLoadVariantSelectionTimer->get_active()) {
int second = spbSecond->get_value();
map_cmd_selection["GRUB_TIMEOUT"] = to_string(second);
}
else {
spbSecond->set_value(1);
map_cmd_selection["GRUB_TIMEOUT"] = "";
}
}
void MainWindow::wrapper_save_all_cfg() {
this->set_data_cfg();
bool _flag_save_global1 = true;
bool _flag_save_local1 = true;
if (flag_save_global == false) {
_flag_save_global1 = this->save_template(global_save, global_remove , "global");
}
if (flag_save_local == false) {
_flag_save_local1 = this->save_template(system_save, system_remove , "system");
}
if (_flag_save_global1 && _flag_save_local1) {
this->info_warning_error(5);
}
else {
info_warning_error(4);
}
map_global_cmd_selection = map_cmd_selection;
map_local_cmd_selection = map_cmd_selection;
}
void MainWindow::wrapper_save_global_cfg() {
this->set_data_cfg();
if (this->save_template(global_save, global_remove , "global")) {
this->info_warning_error(5);
}
else {
info_warning_error(3);
}
map_global_cmd_selection = map_cmd_selection;
}
bool MainWindow::save_template(string cmd, string cmd_remove, string flag_save) {
bool flag_no_save = true;
obj_save.set_data_local(map_local_cmd_selection);
obj_save.set_data_global(map_global_cmd_selection);
obj_save.set_data_gui(map_cmd_selection);
obj_save.set_vec_params(vec_param_names);
obj_save.save_one_cmd(cmd, cmd_remove ,flag_save);
vector<string> obj_vec_error = obj_save.get_error();
flag_no_save = obj_save.get_state_save();
return flag_no_save;
}
void MainWindow::wrapper_save_local_cfg() {
this->set_data_cfg();
if (this->save_template(system_save, system_remove , "system")) {
this->info_warning_error(5);
}
else {
info_warning_error(2);
}
map_local_cmd_selection = map_cmd_selection;
}
void MainWindow::set_data_cfg() {
string str_flags = entryKernel->get_text();
Utils::str_remove(str_flags, ",");
Utils::str_replace_all(str_flags, " \"", "\"");
map_cmd_selection["GRUB_CMDLINE_LINUX"] = str_flags;
str_flags = entryIPT->get_text();
Utils::str_remove(str_flags, ",");
Utils::str_replace_all(str_flags, " \"", "\"");
map_cmd_selection["GRUB_TERMINAL_INPUT"] = str_flags;
str_flags = entryOTT->get_text();
Utils::str_remove(str_flags, ",");
Utils::str_replace_all(str_flags, " \"", "\"");
map_cmd_selection["GRUB_TERMINAL_OUTPUT"] = str_flags;
if (chbLoadVariantSelectionTimer->get_active()) {
int second = spbSecond->get_value();
map_cmd_selection["GRUB_TIMEOUT"] = to_string(second);
}
else {
map_cmd_selection["GRUB_TIMEOUT"] = "";
}
string name_os = entrListOS->get_text();
if (name_os.length() != 0) {
map_cmd_selection["GRUB_DEFAULT"] = "\"" + name_os + "\"";
}
else {
map_cmd_selection["GRUB_DEFAULT"] = "";
}
this->set_download_mode();
this->set_security_login();
this->set_password_protec();
}
void MainWindow::download_globl_cfg() {
str_flag_load = "global";
this->load_template(&map_global_cmd_selection, global_load);
info_warning_error(1);
map_cmd_selection = map_global_cmd_selection;
}
void MainWindow::download_local_cfg() {
str_flag_load = "system";
this->load_template(&map_local_cmd_selection, system_load);
info_warning_error(0);
map_cmd_selection = map_local_cmd_selection;
}
void MainWindow::load_template(std::map<string, string>* map_temp, string cmd) {
if (cmd.find("system") != string::npos) {
this->init_dict("system");
}
else {
this->init_dict("global");
}
entryKernel->set_text("");
entryOTT->set_text("");
entryIPT->set_text("");
*map_temp = obj_load.get_load_data(*map_temp, cmd);
this->get_menu_boot(*map_temp);
this->get_setting_entry_all("GRUB_CMDLINE_LINUX", map_temp);
this->get_setting_entry_all("GRUB_TERMINAL_INPUT", map_temp);
this->get_setting_entry_all("GRUB_TERMINAL_OUTPUT", map_temp);
this->set_row_all(*map_temp, list_store_kernel, vec_Option_kernel, size_kernel, "GRUB_CMDLINE_LINUX");
this->set_row_all(*map_temp, list_store_IPT, vec_Option_IPT, size_IPT, "GRUB_TERMINAL_INPUT");
this->set_row_all(*map_temp, list_store_OTT, vec_Option_OTT, size_OTT, "GRUB_TERMINAL_OUTPUT");
this->get_download_mode(*map_temp);
this->get_security_login(*map_temp);
this->get_password_protec(*map_temp);
this->get_default_load(*map_temp);
this->set_entry(entryKernel, *map_temp, "GRUB_CMDLINE_LINUX");
this->set_entry(entryIPT, *map_temp, "GRUB_TERMINAL_INPUT");
this->set_entry(entryOTT, *map_temp, "GRUB_TERMINAL_OUTPUT");
string str_last_launched_os_ubconfig = (*map_temp)["GRUB_DEFAULT"];
if (str_last_launched_os_ubconfig.length() != 0) {
entrListOS->set_text(str_last_launched_os_ubconfig);
}
else {
entrListOS->set_text(str_last_launched_os);
}
}
void MainWindow::set_entry(Gtk::Entry* entry , std::map<string, string> &map_temp, string key) {
if (map_temp.find(key) != map_temp.end()) {
string value = map_temp[key];
Utils::str_replace_all(value, "\"", "");
Utils::str_replace_all(value, " ", ", ");
entry->set_text(value);
}
else {
entry->set_text("");
}
}
void MainWindow::init_dict(string flag_load) {
std::map<string, string> map_cmd_selection_n;
map_cmd_selection_n["GRUB_TIMEOUT"] = "";
map_cmd_selection_n["GRUB_CMDLINE_LINUX"] = "";
map_cmd_selection_n["GRUB_TERMINAL_INPUT"] = "";
map_cmd_selection_n["GRUB_TERMINAL_OUTPUT"] = "";
map_cmd_selection_n["GRUB_BOOT_SILENT"] = "";
map_cmd_selection_n["GRUB_USER"] = "";
map_cmd_selection_n["GRUB_PASSWORD"] = "";
map_cmd_selection_n["GRUB_DEFAULT"] = "";
map_cmd_selection = map_cmd_selection_n;
if (flag_load == "global") {
map_global_cmd_selection = map_cmd_selection_n;
}
else if (flag_load == "system") {
map_local_cmd_selection = map_cmd_selection_n;
}
}
void MainWindow::set_row_all(std::map <string, string> &map_cmd, Glib::RefPtr<Gtk::ListStore> &list_store, vector<string> &list_params, size_t size,string key) {
for (auto &name: list_params) {
this->set_row(list_store, size, name, false);
}
string value = map_cmd[key];
Utils::str_replace_all(value, "\"", "");
vector<string> vec_params = Utils::split(value, ' ');
for (auto &name: vec_params) {
this->set_row(list_store, size, name, true);
}
}
void MainWindow::set_row(Glib::RefPtr<Gtk::ListStore> &list_store, int size, string name, bool flag_chbox) {
for (int index = 0; index < size; index++) {
Gtk::TreeModel::Row row = list_store->children()[index];
if(row) {
if (row[m_columns.name] == name) {
row[m_columns.check_button] = flag_chbox;
}
}
}
}
void MainWindow::synopsis_show() {
if (flag_open_browser == true) {
this->open_browser();
}
else {
wndShowWeb->show_all();
}
}
void MainWindow::open_browser() {
#ifdef WEBKIT_FOUND
webkit_web_view_load_uri(one, link_doc);
wndWeb->show_all();
#else
this->template_open_browser();
#endif
}
void MainWindow::template_open_browser() {
string cmd = cmd_xdg + string(_(link_doc)) + " &";
string buf = "";
if (geteuid() == 0) {
string response_user = getlogin();
int size_s = std::snprintf(nullptr, 0, cmd_execute, response_user.c_str(), cmd.c_str()) + 1;
auto size = static_cast<size_t>(size_s);
std::unique_ptr<char[]> buf(new char[ size ]);
std::snprintf( buf.get(), size, cmd_execute, response_user.c_str(), cmd.c_str() );
cmd = string( buf.get(), buf.get() + size - 1 );
}
index_error = system(cmd.c_str());
}
void MainWindow::info_status_app(string stule) {
boxInfo->remove_class(info_box_ok_css);
boxInfo->remove_class(info_box_error_css);
boxInfo->add_class(stule);
}
void MainWindow::info_warning_error(int mess) {
if (obj_load.get_count_error() > 0 || obj_save.get_count_error() > 0) {
info_status_app(info_box_error_css);
imgInfo->set_from_icon_name(icon_warning, Gtk::ICON_SIZE_MENU);
string mess_error = "";
if (mess == 0) {
mess_error = string(local_read_error) + str_cmd_error;
lblWarning->set_text(mess_error);
}
else if (mess == 1) {
mess_error = string(global_read_error) + str_cmd_error;
lblWarning->set_text(mess_error);
}
else if (mess == 2) {
mess_error = string(local_write_error) + str_cmd_error;
lblWarning->set_text(mess_error);
}
else if (mess == 3) {
mess_error = string(global_write_error) + str_cmd_error;
lblWarning->set_text(mess_error);
}
else if (mess == 4) {
mess_error = string(error_save_all) + str_cmd_error;
lblWarning->set_text(mess_error);
}
str_cmd_error = "";
obj_load.set_count_error(0);
obj_save.set_count_error(0);
}
else {
info_status_app(info_box_ok_css);
imgInfo->set_from_icon_name(icon_checked, Gtk::ICON_SIZE_MENU);
if (mess == 0) {
lblWarning->set_text(local_load_ok);
}
else if (mess == 1) {
lblWarning->set_text(global_load_ok);
}
else if (mess == 2) {
lblWarning->set_text(local_ok_written);
}
else if (mess == 3) {
lblWarning->set_text(global_ok_written);
}
else if (mess == 4) {
lblWarning->set_text(successfully_save_all);
}
else if (mess == 5) {
lblWarning->set_text(nothing_save);
}
}
}
MainWindow::~MainWindow() {
#ifdef WEBKIT_FOUND
free(one);
#endif
}
void MainWindow::add_CSS(){
Glib::RefPtr<Gtk::CssProvider> cssProvider = Gtk::CssProvider::create();
cssProvider->load_from_path(path_css);
Glib::RefPtr<Gtk::StyleContext> styleContext = Gtk::StyleContext::create();
Glib::RefPtr<Gdk::Screen> screen = Gdk::Screen::get_default();//get default screen
styleContext->add_provider_for_screen(screen, cssProvider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);//add provider for screen in all application
Glib::RefPtr<Gtk::StyleContext> lblInfoHead_css = lblInfoHead->get_style_context();
Glib::RefPtr<Gtk::StyleContext> boxButton_css = boxButton->get_style_context();
Glib::RefPtr<Gtk::StyleContext> boxSave_css = boxSave->get_style_context();
boxInfo = boxInfoError->get_style_context();
imgBG->set(path_img_head_background);
if (socket_trd_id_I == 0 && socket_ext_id_I == 0){
boxButton_css->add_class("bkim_no_plug");
boxSave_css->add_class("bkim_no_plug");
}
lblInfoHead_css->add_class("textHead");
}
void help() {
string version = string(str_version) + version_application + "\n";
cout << version;
string help;
cout << str_help_h;
}