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

991 lines
38 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "ubl-settings-bootloader.h"
using namespace std;
string path_app = "/usr/bin/";
string path_glade= "/usr/share/ubl-settings-bootloader/ui/ubl-settings-bootloader.glade";
string path_css = "/usr/share/ubl-settings-bootloader/css/ubl-settings-bootloader.css";
string app_name = "ubl-settings-bootloader";
string path_resources = "/usr/share/ubl-settings-bootloader";
const string path_img_head_background = "/usr/share/ubl-settings-bootloader/images/logo-background.png";
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 std::string& p_name, const std::string& p_description, const std::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) {
//lblMessageSudo->set_text(_("The program must be run with \nsuperuser privileges!"));
//mess_sudo->show();
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);
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->download_local_cfg();
this->download_globl_cfg();
}
else{
boxWidgetAll->set_sensitive(false);
btnLoad->set_sensitive(false);
btnSave->set_sensitive(false);
imgInfo->set_from_icon_name("com.ublinux.ubl-settings-bootloader.warning", Gtk::ICON_SIZE_MENU);
info_status_app("boxInfoMessError");
lblWarning->set_text(_("The program must be run as root"));
this->set_active_boot_second();
this->fill_in_view();
}
this->event();
}
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("cmdline-linux.csv", row_kernel, list_store_kernel);
vec_Option_IPT = this->read_file_and_view("grub-terminal-input.csv", row_IPT, list_store_IPT);
vec_Option_OTT = this->read_file_and_view("grub-terminal-output.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);
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);
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);
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 cmds = "";
for (int index = 0; index < size; index++) {
Gtk::TreeModel::Row row = list_store->children()[index];
if(row) {
if (row[m_columns.check_button] == true) {
cmds += row[m_columns.name] + " ";
}
}
}
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<std::string>, 5> arr_view = Utils::read_csv(path_resources + "/" + file_name);
std::vector<std::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(_("Active"), m_columns.check_button);
treeView.append_column(_("Option"), m_columns.name);
treeView.append_column(_("Description"), m_columns.description);
}
void MainWindow::view_add_cell(Gtk::TreeModel::Row &row, Glib::RefPtr<Gtk::ListStore> list_store_m , std::array<std::vector<std::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("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);
builder->get_widget("cmbDefaultDonw", cmbDefaultDonw);
}
void MainWindow::localization(){
time_t now = time(0);
tm *ltm = localtime(&now);
unsigned int year = 1900+ ltm->tm_year;
string str_authors = string(_("Copyright © UBSoft LLC, 2022 - ")) + to_string(year);
aboutWindows->set_copyright(str_authors);
aboutWindows->set_comments(_("ubl-settings-bootloader"));
aboutWindows->set_website(_("https://wiki.ublinux.com"));
aboutWindows->set_version(_(version_application.c_str()));
aboutWindows->set_website_label(_("Project Home Page"));
lblInfoHead->set_text(_("Configuring system boot parameters"));
lblHeaderName->set_text(_("ubl-settings-bootloader"));
aboutWindows->set_comments(_("System boot"));
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(_("Help"));
btnAbout->set_label(_("About"));
btnSaveLocalGlob->set_label(_("Save to global and local configuration"));
btnSaveLocal->set_label(_("Save local configuration"));
btnSaveGlob->set_label(_("Save global configuration"));
btnLoadGlob->set_label(_("Load global configuration"));
btnLoadLocal->set_label(_("Load local configuration"));
btnSave->set_label(_("Save"));
btnLoad->set_label(_("Load"));
this->set_title(_("ubl-settings-bootloader"));
// Todo: check
entrySecurityLogin->set_tooltip_text(gettext("User name to enter boot configuration editing mode"));
entryPasswordProtecc->set_tooltip_text(gettext("User password to enter boot configuration edit mode"));
chbSecurityLogin->set_tooltip_text(gettext("User name to enter boot configuration editing mode"));
chbPasswordProtecc->set_tooltip_text(gettext("User password to enter boot 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 the magazine"));
cmbDownloadMode->append(_("Loading animation without a magazine"));
cmbDownloadMode->append(_("Нет анимации загрузки, полный журнал"));
cmbDownloadMode->append(_("Lack of loading animation with the output of the abbreviated log"));
cmbDefaultDonw->append(_("Last successful upload"));
}
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("boxInfoMessError");
imgInfo->set_from_icon_name("com.ublinux.ubl-settings-bootloader.warning", Gtk::ICON_SIZE_MENU);
lblWarning->set_text(_("Enter your password"));
boxSave->set_sensitive(false);
}
else {
info_status_app("boxInfoMessOK");
imgInfo->set_from_icon_name("com.ublinux.ubl-settings-bootloader.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("boxInfoMessError");
imgInfo->set_from_icon_name("com.ublinux.ubl-settings-bootloader.warning", Gtk::ICON_SIZE_MENU);
lblWarning->set_text(_("Enter login"));
boxSave->set_sensitive(false);
}
else {
info_status_app("boxInfoMessOK");
imgInfo->set_from_icon_name("com.ublinux.ubl-settings-bootloader.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);
return true;
}
bool MainWindow::focus_out_txt_IPT(GdkEventFocus*) {
this->set_entry_to_tree_view(list_store_IPT, *entryIPT, vec_Option_IPT, size_IPT);
return true;
}
bool MainWindow::focus_out_txt_OTT(GdkEventFocus*) {
this->set_entry_to_tree_view(list_store_OTT, *entryOTT, vec_Option_OTT, size_OTT);
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) {
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);
}
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(){
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));
}
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::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("boxInfoMessOK");
imgInfo->set_from_icon_name("com.ublinux.ubl-settings-bootloader.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) {
cmbDefaultDonw->set_active_text(value);
}
else {
cmbDefaultDonw->set_active(0);
}
}
else {
cmbDefaultDonw->set_active(-1);
}
}
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("boxInfoMessOK");
imgInfo->set_from_icon_name("com.ublinux.ubl-settings-bootloader.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"] = "";
}
int index = cmbDefaultDonw->get_active_row_number();
if (index != 0 && index != -1) {
map_cmd_selection["GRUB_DEFAULT"] = cmbDefaultDonw->get_active_text();
}
else {
map_cmd_selection["GRUB_DEFAULT"] = "";
}
this->set_download_mode();
this->set_security_login();
this->set_password_protec();
}
void MainWindow::download_globl_cfg() {
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() {
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");
}
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<std::string, std::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, std::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() {
string cmd = open_link + string(_("https://wiki.ublinux.com/ru/Программное_обеспечениерограммы_и_утилиты/Все/")) + app_name;
if (geteuid() == 0) {
string response_user = getlogin();
cmd = "su -l " + response_user + " -c \" DISPLAY=$DISPLAY " + cmd + " \"";
}
obj_process_system.call(cmd, "&");
}
void MainWindow::info_status_app(string stule) {
boxInfo->remove_class("boxInfoMessOK");
boxInfo->remove_class("boxInfoMessError");
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("boxInfoMessError");
imgInfo->set_from_icon_name("com.ublinux.ubl-settings-bootloader.warning", Gtk::ICON_SIZE_MENU);
string mess_error = "";
if (mess == 0) {
mess_error = string(_("Local configuration reading error")) + str_cmd_error;
lblWarning->set_text(mess_error);
}
else if (mess == 1) {
mess_error = string(_("Global configuration read error")) + str_cmd_error;
lblWarning->set_text(mess_error);
}
else if (mess == 2) {
mess_error = string(_("Local configuration write error")) + str_cmd_error;
lblWarning->set_text(mess_error);
}
else if (mess == 3) {
mess_error = string(_("Global configuration write error")) + str_cmd_error;
lblWarning->set_text(mess_error);
}
else if (mess == 4) {
mess_error = string(_("Error saved local and global configuration")) + 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("boxInfoMessOK");
imgInfo->set_from_icon_name("com.ublinux.ubl-settings-bootloader.checked", Gtk::ICON_SIZE_MENU);
if (mess == 0) {
lblWarning->set_text(_("Local configuration downloaded successfully"));
}
else if (mess == 1) {
lblWarning->set_text(_("Global configuration downloaded successfully"));
}
else if (mess == 2) {
lblWarning->set_text(_("Local configuration successfully written"));
}
else if (mess == 3) {
lblWarning->set_text(_("Global configuration successfully written"));
}
else if (mess == 4) {
lblWarning->set_text(_("Successfully saved local and global configuration"));
}
else if (mess == 5) {
lblWarning->set_text(_("Nothing to save!"));
}
}
}
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(_("ubl-settings-bootloader version: ")) + version_application + "\n";
cout << version.c_str();
string help;
help = "GTK settings bootloader for UBLinux\n"
"\n"
"Usage: ubl-settings-bootloader [OPTIONS...]\n"
"Options:\n"
" -h, --help\t Show this help\n"
" -V, --version\t Show package version\n"
" --lock-help Lock utility help\n"
" --lock-save Lock saving local and global configuration\n"
" --lock-save-local Lock save global configuration\n"
" --lock-save-global Lock load global configuration\n"
" --lock-load-global Lock load global configuration\n";
cout << _(help.c_str());
}