#include "ubinstall-gtk.h"

int yon_bootloader_save(main_window *widgets){
    if (gtk_switch_get_active(GTK_SWITCH(widgets->BootloadTimerSwitch))){
        long time = gtk_spin_button_get_value(GTK_SPIN_BUTTON(widgets->BootloadTimerSpin));
        yon_config_register(GRUB_TIMEOUT_parameter,GRUB_TIMEOUT_parameter_command,yon_char_from_long(time));
    } else {
        yon_config_remove_by_key(GRUB_TIMEOUT_parameter);
    }
    {
        int size;
        config_str users = yon_config_get_all_by_key(GRUB_PASSWORD_parameter_search,&size);
        for (int i=0;i<size;i++){
            char *parameter = yon_char_divide_search(users[i],"=",-1);
            yon_config_remove_by_key(parameter);
            free(parameter);
        }
        char *old_packages = config(GRUB_SUPERUSERS_parameter);
        yon_config_register(GRUB_SUPERUSERS_parameter,GRUB_SUPERUSERS_parameter_command,NULL);
        int status = yon_char_is_empty(old_packages);
        yon_config_set_status(GRUB_SUPERUSERS_parameter,status-1);
    }

    if (!gtk_switch_get_active(GTK_SWITCH(widgets->BootloadNoPasswordSwitch))){
        yon_config_remove_by_key(GRUB_SUPERUSERS_parameter);

    } else {
        GtkTreeIter iter;
        GtkTreeModel *model = GTK_TREE_MODEL(widgets->BootloadUsersList);
        for_iter(model,&iter){
            int is_admin;
            char *username, *password;
            gtk_tree_model_get(model,&iter,0,&is_admin,1,&username,2,&password,-1);
            yon_config_register(GRUB_PASSWORD_parameter(username),GRUB_PASSWORD_parameter_command(username),password);
            if (is_admin){
                yon_config_append_element(GRUB_SUPERUSERS_parameter,username,",");
            } else {
                yon_config_remove_by_key(GRUB_SUPERUSERS_parameter);
            }
        }
    }
    

    if (!main_config.configure_mode){
        int user_size=0; 
        config_str users = yon_config_get_all_keys_by_key(GRUB_PASSWORD_parameter_search,&user_size);
        yon_char_parsed_add_or_create_if_exists(users,&user_size,GRUB_SUPERUSERS_parameter);
        yon_char_parsed_add_or_create_if_exists(users,&user_size,GRUB_TIMEOUT_parameter);
        yon_char_parsed_add_or_create_if_exists(users,&user_size,GRUB_DEFAULT_parameter);
        yon_debug_output("%s\n",yon_config_save_list_simple(users, user_size,main_config.config_load_path));
    }

    yon_bootloader_interface_update(widgets);
    return 1;
}

void yon_password_change(GtkWidget *, bootloader_user_window *window){
	GtkEntry *output_target = GTK_ENTRY(window->PasswordEntry);
	yon_password_window *dialog = yon_password_open(output_target);
    yon_gtk_window_setup(GTK_WINDOW(dialog->Window),GTK_WINDOW(window->Window),NULL,icon_path,"password_window");
    gtk_widget_show(window->Window);
    yon_password_hash_list_set(dialog,NULL,NULL,0);

}

void on_bootloader_user_accept(GtkWidget *, bootloader_user_window *window){
    main_window *widgets = g_object_get_data(G_OBJECT(window->AcceptButton),"widgets");
    const char *username = gtk_entry_get_text(GTK_ENTRY(window->UsernameEntry));

    if (yon_char_is_empty(window->prev_name)){
        GtkTreeIter iter;
        GtkTreeModel *model = GTK_TREE_MODEL(widgets->BootloadUsersList);
        for_iter(model,&iter){
            char *name;
            gtk_tree_model_get(model,&iter,1,&name,-1);
            if (!strcmp(name,username)){
                dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
                data->action_text = BOOTLOADER_USER_EXIST_LABEL(username);
                if (yon_confirmation_dialog_call(window->Window,data)!=GTK_RESPONSE_ACCEPT){
                    return;
                }
            }
        }
    }
    char *password = yon_char_new(gtk_entry_get_text(GTK_ENTRY(window->PasswordEntry)));
    if (yon_char_is_empty(username)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),_EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->UsernameEntry);
        return;
    }
    if (yon_char_is_empty(password)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),_EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->PasswordEntry);
        return;
    } else {
        if (strstr(password,"|")){
            int pos = yon_char_find_last(password,'|');
            free(yon_char_divide(password,pos));
        }
    }
    GtkTreeIter iter;
    gtk_list_store_append(widgets->BootloadUsersList,&iter);
    gtk_list_store_set(widgets->BootloadUsersList,&iter,0,gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->AdminCheck)),1,username,2,password,3,"******",-1);

    on_subwindow_close(window->Window);
}

bootloader_user_window *yon_bootloader_user_window_new(){
    bootloader_user_window *window = new(bootloader_user_window);

    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_bootloader_user);

    window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->TitleLabel = yon_gtk_builder_get_widget(builder,"TitleLabel");
    window->AdminCheck = yon_gtk_builder_get_widget(builder,"AdminCheck");
    window->UsernameEntry = yon_gtk_builder_get_widget(builder,"UsernameEntry");
    window->PasswordEntry = yon_gtk_builder_get_widget(builder,"PasswordEntry");
    window->PasswordButton = yon_gtk_builder_get_widget(builder,"PasswordButton");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->prev_name = NULL;

    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->PasswordButton),"clicked",G_CALLBACK(yon_password_change),window);
    yon_gtk_entry_set_password_visibility_icon(GTK_ENTRY(window->PasswordEntry));
    yon_on_entry_set_allowed_symbols(GTK_ENTRY(window->UsernameEntry),"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-*");
    return window;
}

void on_bootloader_user_add(GtkWidget *, main_window *widgets){
    bootloader_user_window *window = yon_bootloader_user_window_new();
    g_object_set_data(G_OBJECT(window->AcceptButton),"widgets",widgets);
    g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_bootloader_user_accept),window);
    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->MainWindow),NULL,icon_path,"bootloader_window");
    gtk_widget_show(window->Window);
}

void on_bootloader_user_edit(GtkWidget *, main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->BootloadUserTree)),&model,&iter)){
        int admin;
        char *username;
        char *password;
        gtk_tree_model_get(model,&iter,0,&admin,1,&username,2,&password,-1);
        bootloader_user_window *window = yon_bootloader_user_window_new();
        g_object_set_data(G_OBJECT(window->AcceptButton),"widgets",widgets);
        g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_bootloader_user_accept),window);
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->MainWindow),EDIT_USER_LABEL,icon_path,"bootloader_window");
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->AdminCheck),admin);
        gtk_entry_set_text(GTK_ENTRY(window->UsernameEntry),username);
        gtk_entry_set_text(GTK_ENTRY(window->PasswordEntry),password);
        gtk_widget_show(window->Window);
    }
}

void on_bootloader_admin_toggled(GtkWidget *, char *path, main_window *widgets){
    GtkTreeIter iter;
    int active;
    gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(widgets->BootloadUsersList),&iter,path);
    gtk_tree_model_get(GTK_TREE_MODEL(widgets->BootloadUsersList),&iter,0,&active,-1);
    gtk_list_store_set(widgets->BootloadUsersList,&iter,0,!active,-1);
}

void on_bootloader_user_remove(GtkWidget *, main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->BootloadUserTree)),&model,&iter)){
        char *target;
        gtk_tree_model_get(model,&iter,1,&target,-1);
        dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
        data->title = WARNING_TITLE_LABEL;
        data->action_text = BOOTLOADER_USER_REMOVE_LABEL(target);
        if (yon_confirmation_dialog_call(widgets->MainWindow,data)){
            gtk_list_store_remove(widgets->BootloadUsersList,&iter);
        }
    }
}

void yon_menu_open_submenu(yon_menu_window *window, yon_menu_item *item){
    int size;
    config_str options = yon_config_load(get_menus_entry_command,&size);
            bind_textdomain_codeset("ublinux-init-grub-functions","UTF-8");
    textdomain("ublinux-init-grub-functions");

    for (int i=1;i<size;i++){
        yon_char_remove_last_symbol(options[i],'\n');
        int parsed_size;
        config_str parsed = yon_char_parse(_(options[i]),&parsed_size,";");
        int id_size;
        config_str id_parsed = yon_char_parse(parsed[0],&id_size,">");
        int name_size;
        config_str name_parsed = yon_char_parse(_(parsed[1]),&name_size,">");
        if (yon_char_parsed_check_exist(id_parsed,id_size,item->id)==0){
            yon_menu_item *subitem = yon_menu_add_item(window,parsed[0],name_parsed[1],0);
            gtk_list_box_insert(GTK_LIST_BOX(window->ChildrenListBox),subitem->MenuItemRow,-1);
            gtk_widget_hide(subitem->ArrowImage);
            
        }
        yon_char_parsed_free(id_parsed,id_size);
        yon_char_parsed_free(name_parsed,name_size);
        yon_char_parsed_free(parsed,parsed_size);
    }
    textdomain(LocaleName);

}

void yon_menu_ungrab(yon_menu_window *window){
        GdkDisplay *display = gdk_display_get_default();
        GdkSeat *seat = gdk_display_get_default_seat(display);
        if (window->seat_grab)
            gdk_seat_ungrab(seat);
        else
            gtk_grab_remove(window->MenuWindow);
        gtk_widget_destroy(window->MenuWindow);
}

void on_menu_chosen(GtkWidget *, GtkListBoxRow* row, yon_menu_window *window){
    yon_menu_item *item = g_object_get_data(G_OBJECT(row),"yon_menu_item");
    if (item->children){
        yon_menu_open_submenu(window,item);
        gtk_widget_show(window->ChildrenRevealer);
        gtk_widget_hide(window->ItemsRevealer);
    gtk_revealer_set_reveal_child(GTK_REVEALER(window->ItemsRevealer),0);
    gtk_revealer_set_reveal_child(GTK_REVEALER(window->ChildrenRevealer),1);
    } else {
        gtk_entry_set_text(GTK_ENTRY(window->target),item->name);
        yon_config_register(GRUB_DEFAULT_parameter,GRUB_DEFAULT_parameter_command,item->id);
        yon_menu_ungrab(window);
    }

}

yon_menu_item *yon_menu_add_item(yon_menu_window *target_menu,const char *id, const char *name, int children){
    yon_menu_item *item = yon_menu_item_new();
    item->window=target_menu;
    item->children = children;
    item->id = yon_char_new(id);
    item->name = yon_char_new(name);
    gtk_label_set_text(GTK_LABEL(item->MenuTextLabel),item->name);


    g_object_set_data(G_OBJECT(item->MenuItemRow),"yon_menu_item",item);
    gtk_widget_show(item->MenuItemRow);
    return item;

}

yon_menu_window *yon_menu_window_open(GtkWidget *, main_window *widgets){
    yon_menu_window *window = yon_menu_window_new();
    window->target = widgets->BootloadDefaultOSEntry;
    g_signal_connect(G_OBJECT(window->ItemsListBox),"row-activated",G_CALLBACK(on_menu_chosen),window);
    g_signal_connect(G_OBJECT(window->ChildrenListBox),"row-activated",G_CALLBACK(on_menu_chosen),window);
    gtk_window_set_transient_for(GTK_WINDOW(window->MenuWindow),GTK_WINDOW(widgets->MainWindow));
        {
            yon_menu_item *item = yon_menu_add_item(window,"",DEFAULT_MENU_ITEM_LABEL,0);
            gtk_list_box_insert(GTK_LIST_BOX(window->ItemsListBox),item->MenuItemRow,-1);
            gtk_widget_hide(item->ArrowImage);
        }
    int size;
    config_str options = yon_config_load(get_menus_entry_command,&size);
    GHashTable *created = g_hash_table_new(g_str_hash,g_str_equal);
    bind_textdomain_codeset("ublinux-init-grub-functions","UTF-8");
    textdomain("ublinux-init-grub-functions");
    for (int i=1;i<size;i++){
        yon_char_remove_last_symbol(options[i],'\n');
        int parsed_size;
        config_str parsed = yon_char_parse(options[i],&parsed_size,";");
        {
            char *temp = _(parsed[1]);
            parsed[1] = yon_char_new(temp);
        }
        int have_children=0;
        char *children_id;
        if ((children_id=strstr(parsed[0],">"))){
            children_id[0]='\0';
            have_children=1;
        }
        char *children_name;
        if ((children_name=strstr(parsed[1],">"))){
            children_name[0]='\0';
            have_children=1;
        }

        if (!g_hash_table_contains(created,parsed[0])){
            g_hash_table_add(created,parsed[0]);
            yon_menu_item *item = yon_menu_add_item(window,parsed[0],_(parsed[1]),have_children);
            gtk_list_box_insert(GTK_LIST_BOX(window->ItemsListBox),item->MenuItemRow,-1);
            if (!have_children){
                gtk_widget_hide(item->ArrowImage);
            }
        }
    }
    textdomain(LocaleName);
    g_hash_table_unref(created);
    yon_char_parsed_free(options,size);
    gtk_window_set_transient_for(GTK_WINDOW(window->MenuWindow), GTK_WINDOW(widgets->MainWindow));

    int x,y,width;
    gtk_widget_realize(window->MenuWindow);
    gdk_window_get_position(gtk_widget_get_window(widgets->BootloadDefaulOSButton),&x,&y);
    gtk_widget_translate_coordinates(widgets->BootloadDefaulOSButton,widgets->MainWindow,x,y,&window->button_pos_x,&window->button_pos_y);
    gtk_window_get_size(GTK_WINDOW(window->MenuWindow),&width,NULL);
    GtkAllocation alloc;
    gtk_widget_get_allocation(widgets->BootloadDefaulOSButton, &alloc);
    gtk_window_get_position(GTK_WINDOW(widgets->MainWindow),&x,&y);
    gtk_window_move(GTK_WINDOW(window->MenuWindow),window->button_pos_x-width,window->button_pos_y);
    gtk_widget_show(window->MenuWindow);
    GdkGrabStatus status = gdk_seat_grab(gdk_display_get_default_seat(gdk_display_get_default()),gtk_widget_get_window(window->MenuWindow),GDK_SEAT_CAPABILITY_POINTER,TRUE,NULL,NULL,NULL,NULL);
    if (status != GDK_GRAB_SUCCESS) {
        window->seat_grab=0;
        gtk_grab_add(window->MenuWindow);
    } else {
        window->seat_grab=1;
    }
    return window;
}

gboolean on_menu_clicked(GtkWidget *, GdkEventButton *event, yon_menu_window *window){
    int x,y,width,height;
    gtk_window_get_size(GTK_WINDOW(window->MenuWindow),&width,&height);
    gtk_window_get_position(GTK_WINDOW(window->MenuWindow),&x,&y);
    if (event->x_root<x||event->y_root<y||event->x_root>x+width||event->y_root>y+height){
        yon_menu_ungrab(window);
    }
    return 1;

}

gboolean yon_on_revealer_switched (yon_menu_window *window){
    if (gtk_revealer_get_reveal_child(GTK_REVEALER(window->ItemsRevealer))){
        gtk_widget_hide(window->ChildrenRevealer);
        gtk_widget_show(window->ItemsRevealer);
    } else {
        gtk_widget_hide(window->ItemsRevealer);
        gtk_widget_show(window->ChildrenRevealer);

    }
    return G_SOURCE_REMOVE;
}

void on_children_clean(GtkWidget*, yon_menu_window *window){
    GList *list = gtk_container_get_children(GTK_CONTAINER(window->ChildrenListBox));
    list = g_list_last(list);
    for (guint i=0;i<g_list_length(list);i++){
        GList *item = gtk_container_get_children(GTK_CONTAINER(list->data));
        g_object_ref(G_OBJECT(item->data));
        gtk_container_remove(GTK_CONTAINER(list->data),GTK_WIDGET(item->data));
        gtk_widget_destroy(GTK_WIDGET(list->data));
        g_list_free(item);
        list=list->prev;
    }

    gtk_revealer_set_reveal_child(GTK_REVEALER(window->ItemsRevealer),1);
    gtk_revealer_set_reveal_child(GTK_REVEALER(window->ChildrenRevealer),0);
    gtk_box_set_child_packing(GTK_BOX(gtk_widget_get_parent(window->ItemsRevealer)),window->ItemsRevealer,1,1,0,GTK_PACK_START);
    yon_on_revealer_switched(window);
    g_signal_connect(G_OBJECT(window->MenuWindow),"size-allocate",G_CALLBACK(on_menu_window_size_reallocated),window);
    gtk_box_set_child_packing(GTK_BOX(gtk_widget_get_parent(window->ChildrenRevealer)),window->ChildrenRevealer,0,0,0,GTK_PACK_START);

}

void on_menu_window_size_reallocated(GtkWidget *self,GtkAllocation *,yon_menu_window *window){
    int x,y,width;
    gtk_window_get_size(GTK_WINDOW(window->MenuWindow),&width,NULL);
    gtk_window_get_position(GTK_WINDOW(gtk_window_get_transient_for(GTK_WINDOW(window->MenuWindow))),&x,&y);
    gtk_window_move(GTK_WINDOW(window->MenuWindow),window->button_pos_x-width,window->button_pos_y);
    g_signal_handlers_disconnect_by_func(self,on_menu_window_size_reallocated,window);
}

 yon_menu_window *yon_menu_window_new(){
    yon_menu_window *window = new(yon_menu_window);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_menu_window);
    window->MenuWindow = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->ItemsRevealer = yon_gtk_builder_get_widget(builder,"ItemsRevealer");
    window->BackButton = yon_gtk_builder_get_widget(builder,"BackButton");
    window->ItemsListBox = yon_gtk_builder_get_widget(builder,"ItemsListBox");
    window->ChildrenRevealer = yon_gtk_builder_get_widget(builder,"ChildrenRevealer");
    window->ChildrenListBox = yon_gtk_builder_get_widget(builder,"ChildrenListBox");

    window->menu_items=NULL;

    g_signal_connect(G_OBJECT(window->BackButton),"clicked",G_CALLBACK(on_children_clean),window);
    g_signal_connect(G_OBJECT(window->MenuWindow),"button-press-event",G_CALLBACK(on_menu_clicked),window);
    return window;
}

yon_menu_item *yon_menu_item_new(){
    yon_menu_item *item = new(yon_menu_item);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_menu_item);
    item->MenuItemRow = yon_gtk_builder_get_widget(builder,"MenuItemRow");
    item->MenuTextLabel = yon_gtk_builder_get_widget(builder,"MenuTextLabel");
    item->ArrowImage = yon_gtk_builder_get_widget(builder,"ArrowImage");

    return item;
}

char *yon_bootloader_get_os_name(char *parameter){
    bind_textdomain_codeset("ublinux-init-grub-functions","UTF-8");
    textdomain("ublinux-init-grub-functions");
    char *final_os_name = NULL;
    int size;
    config_str menu_entries = yon_config_load(get_menus_entry_command,&size);
    for (int i=0;i<size;i++){
        yon_char_remove_last_symbol(menu_entries[i],'\n');
        int parsed_size;
        config_str parsed = yon_char_parse(menu_entries[i],&parsed_size,";");
        if (!strcmp(parsed[0],parameter)){
            char *name = strstr(_(parsed[1]),">");
            if (!yon_char_is_empty(name)){
                name++;
                final_os_name = yon_char_new(name);
            } else {
                final_os_name = yon_char_new(_(parsed[1]));
            }
        }
        yon_char_parsed_free(parsed,parsed_size);
    }
    yon_char_parsed_free(menu_entries,size);
    textdomain(LocaleName);
    return final_os_name;
}

void yon_bootloader_interface_update(main_window *widgets){
    char *timeout = config(GRUB_TIMEOUT_parameter);
    char *admins = config(GRUB_SUPERUSERS_parameter);
    char *os = config(GRUB_DEFAULT_parameter);
    int size;
    config_str users = yon_config_get_all_by_key(GRUB_PASSWORD_parameter_search,&size);
    if (!yon_char_is_empty(timeout)){
        long timeout_l = atol(timeout);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(widgets->BootloadTimerSpin),timeout_l);
    }
    if (!yon_char_is_empty(os)){
        gtk_entry_set_text(GTK_ENTRY(widgets->BootloadDefaultOSEntry),os);
    } else {
        gtk_entry_set_text(GTK_ENTRY(widgets->BootloadDefaultOSEntry),DEFAULT_MENU_ITEM_LABEL);
    }
        int admins_size;
        config_str admins_parsed = yon_char_parse(admins,&admins_size,",");
        for (int i=0;i<size;i++){
            char *value = yon_char_new(users[i]);
            char *parameter = yon_char_divide_search(value,"=",-1);
            if (yon_config_get_status(parameter)<-1) continue;

            char *key = yon_config_parameter_get_key(parameter);

            GtkTreeIter iter;
            gtk_list_store_append(widgets->BootloadUsersList,&iter);
            gtk_list_store_set(widgets->BootloadUsersList,&iter,0,yon_char_parsed_check_exist(admins_parsed,admins_size,key),1,key,2,value,-1);
        }

}

void yon_bootloader_init(main_window *widgets){
    if (!main_config.configure_mode){
        int size = 0;
        config_str users = yon_config_get_all_by_key(GRUB_PASSWORD_parameter_search,&size);
        yon_config_remove_by_list(users,size);
        yon_config_update_by_args(main_config.config_load_path, GRUB_TIMEOUT_parameter,GRUB_SUPERUSERS_parameter,NULL);
        yon_config_load_config(YON_CONFIG_CUSTOM,ubconfig_load_command(main_config.config_load_path),GRUB_PASSWORD_parameter_command_all,NULL);
    }
        gtk_list_store_clear(widgets->BootloadUsersList);
    char *timeout = config(GRUB_TIMEOUT_parameter);
    char *os = config(GRUB_DEFAULT_parameter);
    char *admins = config(GRUB_SUPERUSERS_parameter);
    int size;
    config_str users = yon_config_get_all_by_key(GRUB_PASSWORD_parameter_search,&size);
    if (!yon_char_is_empty(timeout)){
        long timeout_long = atol(timeout);
        gtk_switch_set_active(GTK_SWITCH(widgets->BootloadTimerSwitch),1);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(widgets->BootloadTimerSpin),timeout_long);
    } else{
        gtk_switch_set_active(GTK_SWITCH(widgets->BootloadTimerSwitch),0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(widgets->BootloadTimerSpin),0);
    }
    if (!yon_char_is_empty(os)){
        char *os_name = yon_bootloader_get_os_name(os);
        if (!yon_char_is_empty(os_name)){
            gtk_entry_set_text(GTK_ENTRY(widgets->BootloadDefaultOSEntry),os_name);
            free(os_name);
        }
    } else {
        gtk_entry_set_text(GTK_ENTRY(widgets->BootloadDefaultOSEntry),DEFAULT_BOOTLOAD_MENU_ITEM_LABEL);
    }
    if (!admins&&!size){
        gtk_switch_set_active(GTK_SWITCH(widgets->BootloadNoPasswordSwitch),0);
    } else {
        gtk_switch_set_active(GTK_SWITCH(widgets->BootloadNoPasswordSwitch),1);
        int admins_size;
        config_str admins_parsed = yon_char_parse(admins,&admins_size,",");
        GtkTreeIter iter;
        for (int i=0;i<size;i++){
            char *parameter = yon_char_new(users[i]);
            char *parameter_name = yon_char_divide_search(parameter,"=",-1);
            if (yon_config_get_status(parameter_name)<-1) continue;
            char *key = yon_config_parameter_get_key(parameter_name);
            char *key_sub = key+yon_char_find_last(key,'[');
            char *subkey = yon_config_parameter_get_key(key_sub);

            gtk_list_store_append(widgets->BootloadUsersList,&iter);
            gtk_list_store_set(widgets->BootloadUsersList,&iter,0,yon_char_parsed_check_exist(admins_parsed,admins_size,subkey)>-1,1,subkey,2,parameter,3,"******",-1);
            free(parameter);
            free(parameter_name);
            free(subkey);
        }
        yon_char_parsed_free(admins_parsed,admins_size);
    }
}

void on_bootloader_selected(GtkTreeView *,main_window *widgets){
    GtkTreeModel *model = NULL;
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->BootloadUserTree)),&model,&iter)){
        gtk_widget_set_sensitive(widgets->BootloadUserRemoveButton,1);
        gtk_widget_set_sensitive(widgets->BootloadUserEditButton,1);
    } else {
        gtk_widget_set_sensitive(widgets->BootloadUserRemoveButton,0);
        gtk_widget_set_sensitive(widgets->BootloadUserEditButton,0);
    }
}