#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);
    }
    char *OS = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->BootloadDefaultOSEntry));
    if (strcmp(OS,DEFAULT_BOOTLOAD_MENU_ITEM_LABEL)){
        yon_config_register(GRUB_DEFAULT_parameter,GRUB_DEFAULT_parameter_command,OS);
    } else {
        yon_config_remove_by_key(GRUB_DEFAULT_parameter);
    }
    if (gtk_switch_get_active(GTK_SWITCH(widgets->BootloadNoPasswordSwitch))){
        GtkTreeIter iter;
            yon_config_register(AUTOLOGINUSER_parameter,AUTOLOGINUSER_parameter_command,"yes");
        GtkTreeModel *model = GTK_TREE_MODEL(widgets->BootloadUsersList);
        int admin_size=0;
        config_str admin_list = NULL;
        for_iter(model,&iter){
            int is_admin;
            char *username, *password;
            gtk_tree_model_get(model,&iter,0,&is_admin,1,&username,2,&password,-1);
            if (is_admin){
                yon_char_parsed_add_or_create_if_exists(admin_list,&admin_size,username);
            }
            char *admin_string = yon_char_parsed_to_string(admin_list,admin_size,",");
            yon_config_register(GRUB_PASSWORD_parameter(username),GRUB_PASSWORD_parameter_command(username),password);
            yon_config_register(GRUB_SUPERUSERS_parameter,GRUB_SUPERUSERS_parameter_command,admin_string);
            free(admin_string);
        }
    } else {
        int size;
        config_str users = yon_config_get_all_by_key(GRUB_PASSWORD_parameter_search,&size);
        for (int i=0;i<size;i++){
            yon_config_remove_by_key(GRUB_PASSWORD_parameter(users[i]));
        }
        yon_config_register(AUTOLOGINUSER_parameter,AUTOLOGINUSER_parameter_command,"no");
        yon_config_remove_by_key(GRUB_PASSWORD_parameter_all);
        yon_config_remove_by_key(GRUB_SUPERUSERS_parameter);
    }
    
    if (!main_config.configure_mode){
        int size, users_size, final_size, save_size;
        config_str grub_users = yon_config_find_keys(GRUB_PASSWORD_parameter_search,&users_size);
        config_str save_parameters = yon_char_parsed_new(&size,GRUB_TIMEOUT_parameter,GRUB_DEFAULT_parameter,AUTOLOGINUSER_parameter,GRUB_SUPERUSERS_parameter,NULL);
        config_str final = yon_char_parsed_merge(save_parameters,size,grub_users,users_size,&final_size);
        config_str parameters = yon_config_get_save_parameters_by_list(&save_size,final,final_size);
        if (parameters){
            char *command_parameters = yon_char_parsed_to_string(parameters,save_size," -- ");
            char *command = yon_char_unite(ubconfig_dull_command,"--target system ",command_parameters,NULL);
            if (!system(command)){}
            free(command);
            yon_char_parsed_free(parameters,save_size);
        }
        if (grub_users) yon_char_parsed_free(grub_users,users_size);
        if (save_parameters) yon_char_parsed_free(save_parameters,size);
    }

    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);

}

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;
                }
            }
        }
    }
    const char *password = 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;
    }
    if (!yon_char_is_empty(window->prev_name)&&strcmp(username,window->prev_name)){
        yon_config_remove_by_key(GRUB_PASSWORD(window->prev_name));
        int size;
        char *superusers = config(GRUB_SUPERUSERS_parameter);
        config_str parsed = yon_char_parse(superusers,&size,",");
        int pos = yon_char_parsed_check_exist(parsed,size,(char*)username);
        if (pos>-1){
            parsed = yon_char_parsed_rip(parsed,&size,pos);
            char *superusers = yon_char_parsed_to_string(parsed,size,",");
            if (!yon_char_is_empty(superusers)){
                yon_config_register(GRUB_SUPERUSERS_parameter,GRUB_SUPERUSERS_parameter_command,superusers);
            } else {
                yon_config_remove_by_key(GRUB_SUPERUSERS_parameter);
            }
        }
    }
    yon_config_register(GRUB_PASSWORD(username),GRUB_SUPERUSERS_parameter_command,(char*)password);
    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,"******",-1);

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->AdminCheck))){
        int size;
        char *superusers = config(GRUB_SUPERUSERS_parameter);
        config_str parsed = yon_char_parse(superusers,&size,",");
        if (yon_char_parsed_check_exist(parsed,size,(char*)username)==-1){
            yon_char_parsed_add_or_create_if_exists(parsed,&size,(char*)username);
            char *superusers = yon_char_parsed_to_string(parsed,size,",");
            yon_config_register(GRUB_SUPERUSERS_parameter,GRUB_SUPERUSERS_parameter_command,superusers);
        }
    } else {
        int size;
        char *superusers = config(GRUB_SUPERUSERS_parameter);
        config_str parsed = yon_char_parse(superusers,&size,",");
        int pos = yon_char_parsed_check_exist(parsed,size,(char*)username);
        if (pos!=-1){
            parsed = yon_char_parsed_rip(parsed,&size,pos);

            char *superusers = yon_char_parsed_to_string(parsed,size,",");
            yon_config_register(GRUB_SUPERUSERS_parameter,GRUB_SUPERUSERS_parameter_command,superusers);
        }
    }
    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_gtk_entry_block_restricted_symbols(GTK_ENTRY(window->UsernameEntry));
    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_menu_chosen(GtkWidget *, yon_menu_item *item){
    if (strcmp(item->target,DEFAULT_MENU_ITEM_LABEL)){
        gtk_entry_set_text(GTK_ENTRY(item->widgets->BootloadDefaultOSEntry),item->target);
    } else {
        gtk_entry_set_text(GTK_ENTRY(item->widgets->BootloadDefaultOSEntry),DEFAULT_MENU_ITEM_LABEL);
    }
        GdkDisplay *display = gdk_display_get_default();
        GdkSeat *seat = gdk_display_get_default_seat(display);
        if (item->window->seat_grab)
            gdk_seat_ungrab(seat);
        else
            gtk_grab_remove(item->window->MenuWindow);
        gtk_widget_destroy(item->window->MenuWindow);

}

void on_submenu_open(GtkWidget *,yon_menu_item *item){
    yon_menu_window *window = item->window;

    dictionary *dact = NULL;
    for_dictionaries(dact,item->children){
        yon_menu_item *child =  yon_dictionary_get_data(dact,yon_menu_item*);
        gtk_widget_hide(child->NextIconButton);
        gtk_list_box_insert(GTK_LIST_BOX(window->ChildrenListBox),child->MenuItemBox,-1);
            g_signal_connect(G_OBJECT(child->MenuButton),"clicked",G_CALLBACK(on_menu_chosen),dact->data);
        
    }
    gtk_revealer_set_reveal_child(GTK_REVEALER(item->window->ItemsRevealer),0);
    gtk_revealer_set_reveal_child(GTK_REVEALER(item->window->ChildrenRevealer),1);
    gtk_box_set_child_packing(GTK_BOX(gtk_widget_get_parent(window->ChildrenRevealer)),window->ChildrenRevealer,1,1,0,GTK_PACK_START);
    gtk_box_set_child_packing(GTK_BOX(gtk_widget_get_parent(window->ItemsRevealer)),window->ItemsRevealer,0,0,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);

}

yon_menu_window *yon_menu_window_open(GtkWidget *, main_window *widgets){
    yon_menu_window *window = yon_menu_window_new();
    gtk_window_set_transient_for(GTK_WINDOW(window->MenuWindow),GTK_WINDOW(widgets->MainWindow));
        {
            yon_menu_item *item = yon_menu_item_new();
            item->widgets=widgets;
            item->window=window;
            item->children=NULL;
            gtk_label_set_text(GTK_LABEL(item->MenuTextLabel),DEFAULT_MENU_ITEM_LABEL);
            item->target=yon_char_new(DEFAULT_MENU_ITEM_LABEL);
            gtk_list_box_insert(GTK_LIST_BOX(window->ItemsListBox),item->MenuItemBox,-1);
            yon_dictionary_add_or_create_if_exists_with_data(window->menu_items,DEFAULT_MENU_ITEM_LABEL,item);
            gtk_widget_show(item->MenuItemBox);
            gtk_widget_hide(item->NextIconButton);
            g_signal_connect(G_OBJECT(item->MenuButton),"clicked",G_CALLBACK(on_menu_chosen),item);
        }
    int size;
    config_str parsed = yon_config_load(get_menus_entry_command,&size);
    for (int i=0;i<size;i++){
        yon_char_remove_last_symbol(parsed[i],'\n');
        int param_size;
        config_str parameter = yon_char_parse(parsed[i],&param_size,">");
        dictionary * cur_item = NULL;
        if (param_size==2&&(cur_item=yon_dictionary_get(&window->menu_items,parameter[0]))){
            yon_menu_item *item = (yon_menu_item*)cur_item->data;
            yon_menu_item *item_child = yon_menu_item_new();
            gtk_widget_hide(item_child->NextIconButton);
            gtk_label_set_text(GTK_LABEL(item_child->MenuTextLabel),parameter[1]);
            item_child->target=yon_char_new(parsed[i]);
            gtk_widget_show(item_child->MenuItemBox);
            yon_dictionary_add_or_create_if_exists_with_data(item->children,parameter[1],item_child);
            item_child->widgets=widgets;
            item_child->window=window;
            
        } else {
            yon_menu_item *item = yon_menu_item_new();
            item->widgets=widgets;
            item->window=window;
            gtk_label_set_text(GTK_LABEL(item->MenuTextLabel),parameter[0]);
            item->target=yon_char_new(parsed[i]);
            gtk_list_box_insert(GTK_LIST_BOX(window->ItemsListBox),item->MenuItemBox,-1);
            yon_dictionary_add_or_create_if_exists_with_data(window->menu_items,parameter[0],item);
            gtk_widget_show(item->MenuItemBox);
            if (param_size==2){
                yon_menu_item *item_child = yon_menu_item_new();
                gtk_widget_show(item->NextIconButton);
                gtk_widget_hide(item_child->NextIconButton);
                gtk_label_set_text(GTK_LABEL(item_child->MenuTextLabel),parameter[1]);
                item_child->target=yon_char_new(parsed[i]);
                gtk_widget_show(item_child->MenuItemBox);
                item_child->widgets=widgets;
                item_child->window=window;
                g_signal_connect(G_OBJECT(item->MenuButton),"clicked",G_CALLBACK(on_submenu_open),item);
                yon_dictionary_add_or_create_if_exists_with_data(item->children,parameter[1],item_child);
            } else {
                gtk_widget_hide(item->NextIconButton);
                g_signal_connect(G_OBJECT(item->MenuButton),"clicked",G_CALLBACK(on_menu_chosen),item);
            }
        }

    }
    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){

        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);
    }
    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->MenuItemBox = yon_gtk_builder_get_widget(builder,"MenuItemBox");
    item->MenuButton = yon_gtk_builder_get_widget(builder,"MenuButton");
    item->MenuTextLabel = yon_gtk_builder_get_widget(builder,"MenuTextLabel");
    item->NextIconButton = yon_gtk_builder_get_widget(builder,"NextIconButton");
    item->children=NULL;

    return item;
}

void yon_bootloader_interface_update(main_window *widgets){
    char *timeout = config(GRUB_TIMEOUT_parameter);
    char *admins = config(GRUB_SUPERUSERS_parameter);
    char *autologin = config(AUTOLOGINUSER_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);
    }
    if (!yon_char_is_empty(autologin)&&(!strcmp(autologin,"yes")||!strcmp(autologin,"enable"))){
        gtk_switch_set_active(GTK_SWITCH(widgets->BootloadNoPasswordSwitch),1);
    } else {
        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);
            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){
    char *timeout = config(GRUB_TIMEOUT_parameter);
    char *os = config(GRUB_DEFAULT_parameter);
    char *autologin = config(AUTOLOGINUSER_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)){
        gtk_entry_set_text(GTK_ENTRY(widgets->BootloadDefaultOSEntry),os);
    } else {
        gtk_entry_set_text(GTK_ENTRY(widgets->BootloadDefaultOSEntry),DEFAULT_BOOTLOAD_MENU_ITEM_LABEL);
    }
    if (!yon_char_is_empty(autologin)&&(!strcmp(autologin,"yes")||!strcmp(autologin,"enable"))){
        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);
            char *key = yon_config_parameter_get_key(parameter_name);

            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,parameter,3,"******",-1);
            free(parameter);
            free(parameter_name);
            free(key);
        }
        yon_char_parsed_free(admins_parsed,admins_size);
    }
}