#include "ubinstall-gtk.h" 
 
config main_config; 
 
//functions 
 
// void yon_load_proceed(YON_CONFIG_TYPE type){
//     if (yon_config_load_register(type,,,NULL)){
		
//     }
// }

double yon_size_long_convert_automatic(unsigned long bytes, char *size);
double yon_size_long_convert_automatic(unsigned long bytes, char *size){
    int repeats;
    double byte_float=bytes;
    for (repeats=-1;byte_float>1024;repeats++){
        byte_float=byte_float/1024;
    }
    if (repeats==-1) {
        repeats=0;
        byte_float=byte_float/1024;
    }
    switch(repeats){
        case 0: (*size)='K';
        break;
        case 1: (*size)='M';
        break;
        case 2: (*size)='G';
        break;
        case 3: (*size)='T';
        break; 
    }
    return byte_float;
}

// standard functions 
 
void on_keyboard_accept(GtkWidget *self,main_window *widgets);
void on_keyboard_accept(GtkWidget *self,main_window *widgets){
    gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->LayoutsFilter));
    on_subwindow_close(self);
}

void on_layout_toggle(GtkCellRendererToggle*,  gchar* path, ubinstall_keyboard_window *window);
void on_layout_toggle(GtkCellRendererToggle*,  gchar* path, ubinstall_keyboard_window *window){
    GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(window->LayoutsTree));
    GtkTreeIter iter;
    if (gtk_tree_model_get_iter_from_string(model,&iter,path)){
        int status=0;
        gtk_tree_model_get(model,&iter,3,&status,-1);
        gtk_tree_store_set(GTK_TREE_STORE(model),&iter,3,!status,-1);
    }
}

ubinstall_keyboard_window *yon_ubinstall_keyboard_new(){
	ubinstall_keyboard_window *window = malloc(sizeof(ubinstall_keyboard_window));
	GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_ubinstall_keyboard);
	window->MainWindow=yon_gtk_builder_get_widget(builder,"MainWindow");
	window->StatusBox=yon_gtk_builder_get_widget(builder,"StatusBox");
	window->headerBar=yon_gtk_builder_get_widget(builder,"headerBar");
	window->CancelButton=yon_gtk_builder_get_widget(builder,"CancelButton");
	window->SaveButton=yon_gtk_builder_get_widget(builder,"SaveButton");
	window->LayoutsTree=yon_gtk_builder_get_widget(builder,"LayoutsTree");
    window->ActiveToggle = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"ActiveToggle"));

    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);

return window;
}

void on_keyboard_clicked (GtkWidget *, main_window *widgets);
void on_keyboard_clicked (GtkWidget *, main_window *widgets){
    ubinstall_keyboard_window *window = yon_ubinstall_keyboard_new();
    yon_gtk_window_setup(GTK_WINDOW(window->MainWindow),GTK_WINDOW(widgets->MainWindow),KEYBOARD_TITLE_LABEL,icon_path,"keyboard-window");
    gtk_tree_view_set_model(GTK_TREE_VIEW(window->LayoutsTree),GTK_TREE_MODEL(widgets->LayoutList));

    g_signal_connect(G_OBJECT(window->ActiveToggle),"toggled",G_CALLBACK(on_layout_toggle),window);
    g_signal_connect(G_OBJECT(window->SaveButton),"clicked",G_CALLBACK(on_keyboard_accept),widgets);
    gtk_widget_show(window->MainWindow);
}

void yon_language_selection_changed(GtkCellRenderer *, char *path, ubinstall_language_window *window);
void yon_language_selection_changed(GtkCellRenderer *, char *path, ubinstall_language_window *window){
    GtkTreeIter iter;
    int state;

    if (gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(window->liststore1),&iter,path)){
        gtk_tree_model_get(GTK_TREE_MODEL(window->liststore1),&iter,0,&state,-1);
        gtk_list_store_set(window->liststore1,&iter,0,!state,-1);
    }
}

void on_language_window_accept(GtkWidget *,dictionary *dict);
void on_language_window_accept(GtkWidget *,dictionary *dict){
    main_window *widgets= yon_dictionary_get_data(dict->first,main_window*);
    ubinstall_language_window *window = yon_dictionary_get_data(dict->first->next,ubinstall_language_window*);
    yon_gtk_list_store_copy_full(widgets->LanguagesList,window->liststore1);
    gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->LanguagesFilter));
    config_str parsed = NULL;
    int size;
    GtkTreeIter iter;
    for_iter(GTK_TREE_MODEL(window->liststore1),&iter){
        char *current;
        int status;
        gtk_tree_model_get(GTK_TREE_MODEL(window->liststore1),&iter,0,&status,1,&current,-1);
        if (status)
            yon_char_parsed_add_or_create_if_exists(parsed,&size,current);
    }
    gtk_entry_set_text(GTK_ENTRY(widgets->AvailableLanguagesEntry),yon_char_parsed_to_string(parsed,size,", "));
    on_subwindow_close(window->MainWindow);
    free(window);
}

ubinstall_language_window *yon_ubinstall_language_new(){
	ubinstall_language_window *window = malloc(sizeof(ubinstall_language_window));
	GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_ubinstall_language);
	window->liststore1=GTK_LIST_STORE(gtk_builder_get_object(builder,"liststore1"));
	window->MainWindow=yon_gtk_builder_get_widget(builder,"MainWindow");
	window->StatusBox=yon_gtk_builder_get_widget(builder,"StatusBox");
	window->DefaultCheck=yon_gtk_builder_get_widget(builder,"DefaultCheck");
	window->LanguagesTree=yon_gtk_builder_get_widget(builder,"LanguagesTree");
	window->headerBar=yon_gtk_builder_get_widget(builder,"headerBar");
	window->CancelButton=yon_gtk_builder_get_widget(builder,"CancelButton");
	window->SaveButton=yon_gtk_builder_get_widget(builder,"SaveButton");
	window->ToggleRenderer=GTK_CELL_RENDERER(gtk_builder_get_object(builder,"ToggleRenderer"));

    g_signal_connect(G_OBJECT(window->DefaultCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->LanguagesTree);
    g_signal_connect(G_OBJECT(window->ToggleRenderer),"toggled",G_CALLBACK(yon_language_selection_changed),window);
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
return window;
}

void on_language_clicked(GtkWidget *, main_window *widgets);
void on_language_clicked(GtkWidget *, main_window *widgets){
    ubinstall_language_window *window = yon_ubinstall_language_new();
    g_object_ref(G_OBJECT(window->liststore1));
    gtk_tree_view_set_model(GTK_TREE_VIEW(window->LanguagesTree),NULL);
    yon_gtk_list_store_copy_full(window->liststore1,widgets->LanguagesList);
    gtk_tree_view_set_model(GTK_TREE_VIEW(window->LanguagesTree),GTK_TREE_MODEL(window->liststore1));
    yon_gtk_window_setup(GTK_WINDOW(window->MainWindow),GTK_WINDOW(widgets->MainWindow),TITLE_LABEL,icon_path,"language-chooser-window");

    dictionary *dict=NULL;
    yon_dictionary_add_or_create_if_exists_with_data(dict,"widgets",widgets);
    yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
    g_signal_connect(G_OBJECT(window->SaveButton),"clicked",G_CALLBACK(on_language_window_accept),dict);
    gtk_widget_show(window->MainWindow);
}
 
/**config_init() 
 * [EN] 
 *  
 * [RU] 
 * Функция инициализации всех параметров конфигурации 
*/ 
void config_init(){ 
    main_config.always_open_documentation=0; 
    main_config.socket_id=-1; 
    main_config.save_socket_id=-1; 
    main_config.load_socket_id=-1; 
    main_config.lock_help=0; 
    main_config.lock_help=0; 
    main_config.lock_load_global=0; 
    main_config.lock_save_global=0; 
    main_config.lock_save_local=0; 
    main_config.debug_mode=0;
    main_config.slider_thread=0;
    main_config.config_save_thread=NULL;
    main_config.install_thread=NULL;
    main_config.progress_thread=NULL;
}

int yon_image_resize_from_container(GtkImage *target, GdkPixbuf *pixbuf_unscaled);
int yon_image_resize_from_container(GtkImage *target, GdkPixbuf *pixbuf_unscaled){

        int width = gtk_widget_get_allocated_width(gtk_widget_get_parent(gtk_widget_get_parent(GTK_WIDGET(target))));
        int height = gtk_widget_get_allocated_height(gtk_widget_get_parent(gtk_widget_get_parent(GTK_WIDGET(target))));
        int newImgWidthDef = (width <= 217) ? width : width - 217;
        int newImgHeightDef = (height <= 120) ? height : height - 217;
        GdkPixbuf *pixBuf = pixbuf_unscaled;
        int newImgHeight = (int)(gdk_pixbuf_get_height(pixBuf) / ((double) gdk_pixbuf_get_width(pixBuf) / newImgWidthDef));
        if (newImgHeight > newImgHeightDef) {
            newImgHeight = newImgHeightDef;
            newImgWidthDef = newImgWidthDef;
        }
        int newImageWidth = (int)(gdk_pixbuf_get_width(pixBuf) / ((double) gdk_pixbuf_get_height(pixBuf) / newImgHeight));
        GdkPixbuf *scaledPixBuf = gdk_pixbuf_scale_simple(pixBuf, abs(newImageWidth), abs(newImgHeight), GDK_INTERP_BILINEAR);
        gtk_image_set_from_pixbuf(target, scaledPixBuf);

    return 1;
}

void on_region_resized(GtkWidget *,main_window *widgets);
void on_region_resized(GtkWidget *,main_window *widgets){
    int page = gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->Notebook));
    switch(page){
        case YON_PAGE_INSTALLATION: {
            yon_image_resize_from_container(GTK_IMAGE(widgets->SlidesImage), widgets->slides_original);
        } break;
        case YON_PAGE_REGION: {
            yon_image_resize_from_container(GTK_IMAGE(widgets->RegionImage), widgets->regions_original);

        } break;
        case YON_PAGE_KEYBOARD: {
            yon_image_resize_from_container(GTK_IMAGE(widgets->KeyboardImage), widgets->keyboard_original);
        } break;
    }
}

void on_toggle_block(GtkToggleButton *self);
void on_toggle_block(GtkToggleButton *self){
    int state = gtk_toggle_button_get_active(self);
    g_signal_handlers_block_by_func(G_OBJECT(self),on_toggle_block,NULL);
    gtk_toggle_button_set_active(self,!state);
    g_signal_handlers_unblock_by_func(G_OBJECT(self),on_toggle_block,NULL);
}

void yon_switch_page_render(main_window *widgets, int page);
void yon_switch_page_render(main_window *widgets, int page){
    GtkContainer *parent = GTK_CONTAINER(gtk_widget_get_parent(widgets->UsersToggle));
    GList *list = gtk_container_get_children(parent);
    for (guint i=0;i<g_list_length(list);i++){
        if (i!=(guint)page){
    g_signal_handlers_block_by_func(G_OBJECT(g_list_nth_data(list,i)),on_toggle_block,NULL);
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(g_list_nth_data(list,i)),0);
    g_signal_handlers_unblock_by_func(G_OBJECT(g_list_nth_data(list,i)),on_toggle_block,NULL);
        } else { 
    g_signal_handlers_block_by_func(G_OBJECT(g_list_nth_data(list,i)),on_toggle_block,NULL);
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(g_list_nth_data(list,i)),1);
    g_signal_handlers_unblock_by_func(G_OBJECT(g_list_nth_data(list,i)),on_toggle_block,NULL);
        
        }
    }
}

int cur_slide=0;

gboolean on_image_slide(void *data);
gboolean on_image_slide(void *data){
    main_window *widgets = (main_window*)data;
    int size;
    config_str target = yon_char_parsed_new(&size,slide_repeat_path);
    g_object_unref(widgets->slides_original);
    widgets->slides_original = gdk_pixbuf_new_from_resource_at_scale(target[cur_slide],600,400,1,NULL);
    GtkWidget *parent = gtk_widget_get_parent(widgets->SlidesImage);
    gtk_widget_destroy(widgets->SlidesImage);
    widgets->SlidesImage = gtk_image_new();
    gtk_container_add(GTK_CONTAINER(parent),widgets->SlidesImage);
    gtk_widget_show(widgets->SlidesImage);
    gtk_image_set_from_pixbuf(GTK_IMAGE(widgets->SlidesImage),widgets->slides_original);
    gtk_widget_queue_draw(widgets->SlidesImage);
    if (cur_slide<size-1) cur_slide++; else cur_slide=0;
    return 1;
}

void *on_config_save(char *command);
void *on_config_save(char *command){
    if (system(command)){};
    main_config.config_save_thread=NULL;
    return NULL;
}

void *yon_installation_start();
void *yon_installation_start(){
    if (system(start_fast_install_command)){};
    main_config.install_thread=NULL;
    g_thread_exit(NULL);
    return NULL;
    
}

void *yon_installation_progress_update(void *data);
void *yon_installation_progress_update(void *data){
    sleep(1);
    main_window *widgets = (main_window*)data;
    FILE *file = fopen(progress_path,"r");
    char *current = g_malloc0(4096);
    if (file){
        while (main_config.install_thread){
            current = fgets(current,4096,file);
            if (current[0]!='#'){
                int size;
                config_str parsed = yon_char_parse(current,&size,"|");
                gtk_label_set_text(GTK_LABEL(widgets->InstallationLabel),parsed[1]);
                gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(widgets->InstallationProgress),atof(parsed[0]));
            }

        }
    }
    return NULL;
}

void on_page_changed(GtkWidget *,GtkWidget *,int page, main_window *widgets);
void on_page_changed(GtkWidget *,GtkWidget *,int page, main_window *widgets){
    yon_switch_page_render(widgets,page>3?page-2:page);
    switch(page){
        case YON_PAGE_WELCOME: {
            gtk_widget_hide(widgets->CancelInstallButton);
            gtk_widget_set_sensitive(widgets->BackButton,0);
            gtk_widget_set_sensitive(widgets->NextButton,1);
            gtk_widget_set_sensitive(widgets->BackButton,0);
            gtk_button_set_label(GTK_BUTTON(widgets->NextButton),NEXT_LABEL);
            gtk_button_set_label(GTK_BUTTON(widgets->CancelInstallButton),CANCEL_LABEL);
            gtk_image_set_from_icon_name(GTK_IMAGE(gtk_button_get_image(GTK_BUTTON(widgets->NextButton))),"com.ublinux.ubinstall-gtk.arrow-right-symbolic",GTK_ICON_SIZE_BUTTON);
            gtk_image_set_from_icon_name(GTK_IMAGE(gtk_button_get_image(GTK_BUTTON(widgets->CancelInstallButton))),"com.ublinux.ubinstall-gtk.circle-exit-symbolic",GTK_ICON_SIZE_BUTTON);
        } break;
        case YON_PAGE_LICENCE:{
            gtk_widget_show(widgets->CancelInstallButton);
            gtk_widget_set_sensitive(widgets->BackButton,1);
        } break;
        case YON_PAGE_REGION: {
                yon_image_resize_from_container(GTK_IMAGE(widgets->RegionImage), widgets->regions_original);

        } break;
        case YON_PAGE_KEYBOARD: {

            yon_image_resize_from_container(GTK_IMAGE(widgets->KeyboardImage), widgets->keyboard_original);
        } break;
        case YON_PAGE_INSTALLATION:{
            gtk_widget_set_sensitive(widgets->BackButton,0);
            g_thread_join(main_config.config_save_thread);
            if (!main_config.install_thread)
            main_config.install_thread = g_thread_new("InstallThread",(GThreadFunc)yon_installation_start,NULL);
            if (!main_config.install_thread)
            main_config.progress_thread = g_thread_new("ProgressThread",(GThreadFunc)yon_installation_progress_update,widgets);
            
            if (!main_config.slider_thread)
            main_config.slider_thread = g_timeout_add(5000,(GSourceFunc)on_image_slide,widgets);
            gtk_widget_show(gtk_widget_get_parent(widgets->InstallationProgress));
        } break;
        case YON_PAGE_OS_COMPONENTS:
        case YON_PAGE_SOFTWARE:
                yon_switch_page_render(widgets,3);
            break;
        case YON_PAGE_USERS:
            gtk_widget_set_sensitive(widgets->NextButton,1);
            break;
        case YON_PAGE_COMPLETION:{
            gtk_widget_set_sensitive(widgets->BackButton,0);
            gtk_button_set_label(GTK_BUTTON(widgets->NextButton),RESTART_LABEL);
            gtk_button_set_label(GTK_BUTTON(widgets->CancelInstallButton),EXIT_LABEL);
            gtk_image_set_from_icon_name(GTK_IMAGE(gtk_button_get_image(GTK_BUTTON(widgets->NextButton))),"com.ublinux.libublsettingsui-gtk3.sync-symbolic",GTK_ICON_SIZE_BUTTON);
            gtk_image_set_from_icon_name(GTK_IMAGE(gtk_button_get_image(GTK_BUTTON(widgets->CancelInstallButton))),"com.ublinux.libublsettingsui-gtk3.reset-symbolic",GTK_ICON_SIZE_BUTTON);
            
                int size=0;
                config_str parameters = yon_config_get_all(&size);
                main_config.config_save_thread = g_thread_new("savethread",(GThreadFunc)on_config_save,save_config_command(yon_char_parsed_to_string(parameters,size," ")));
            }
            break;
        case YON_PAGE_INSTALL_COMMON:
        case YON_PAGE_INSTALL_SEPARATE:
        case YON_PAGE_INSTALL_SAME_PARTITION: {
            yon_switch_page_render(widgets,2);
        }
    }
}

void on_page_navigation_clicked(GtkWidget *self, main_window *widgets);
void on_page_navigation_clicked(GtkWidget *self, main_window *widgets){
    int mode = self==widgets->NextButton ? 1 : self == widgets->BackButton ? -1 : self==widgets->CancelInstallButton?-2:0;
    int page = gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->Notebook));
    switch (mode){
        case 1: { // Next
            switch (page){
                case YON_PAGE_SECTIONS:{ //sections
                    int active_id=-1;
                    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->InstallationRadio)))
                        active_id=0;
                    else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->InstallationNearRadio)))
                        active_id=1;
                    else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->InstallationLinuxRadio)))
                        active_id=2;
                    // else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->InstallationRadio)))
                    //     active_id=3;
                    // else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->InstallationRadio)))
                    //     active_id=4;
                    main_config.install_mode=active_id;
                    switch (active_id){
                        case 0:{ // normal installation
                            gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_INSTALL_COMMON);
                        } break;
                        case 1:
                            gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_INSTALL_SEPARATE);
                            break;
                        case 2:
                            gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_INSTALL_SAME_PARTITION);
                    }
                }break;
                case YON_PAGE_REGION:{ //region
                    gtk_widget_set_sensitive(widgets->BackButton,1);
                    if (gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->RegionCombo))==-1){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),REGION_EMPTY_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(widgets->RegionCombo);
                        return;
                    } else if (gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->ZoneCombo))==-1){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),ZONE_EMPTY_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(widgets->ZoneCombo);
                        return;
                    } else if (yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(widgets->AvailableLanguagesEntry)))){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),LANGUAGES_EMPTY_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(widgets->AvailableLanguagesEntry);
                        return;
                    } else if (gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->LanguagesCombo))==-1){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),LOCALE_EMPTY_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(widgets->LanguagesCombo);
                        return;
                    }
                    char *region = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->RegionCombo));
                    char *zone = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->ZoneCombo));
                    GtkTreeIter iter;
                    GtkTreeModel *model = GTK_TREE_MODEL(widgets->LanguagesFilter);
                    char *languages = "";
                    char *lang_code=NULL;
                    for_iter(model,&iter){
                        gtk_tree_model_get(model,&iter,1,&lang_code,-1);
                        yon_char_unite(languages,",",lang_code,NULL);
                    }
                    char *language = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->LanguagesCombo));
                    yon_config_register(locale_parameter,locale_parameter_command,languages);
                    yon_config_register(lang_parameter,lang_parameter_command,language);
                    yon_config_register(zone_parameter,zone_parameter_command,yon_char_unite(region,"/",zone,NULL));

                    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),page+1);
                }break;
                case YON_PAGE_KEYBOARD: { //keyboard
                    gtk_widget_set_sensitive(widgets->BackButton,1);
                    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->ManualLayoutRadio))){
                        GtkTreeIter iter;
                        if (!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(widgets->LayoutList),&iter)){
                            yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),LAYOUTS_CHOSEN_BUT_EMPTY_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                            yon_ubl_status_highlight_incorrect(widgets->ManualLayoutRadio);
                            yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(widgets->LayoutTree));
                        }
                    }
                    char *layouts_list="";
                    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->DefaultLayoutRadio))){
                        GtkTreeModel *layouts_model=NULL;
                        GtkTreeIter iter;
                        char *cur_layout=NULL;
                        for_iter(layouts_model,&iter){
                            gtk_tree_model_get(layouts_model,&iter,0,&cur_layout,-1);
                            layouts_list = yon_char_unite(layouts_list,",",cur_layout,NULL);
                        }
                    } else {
                        yon_config_remove_by_key(xkblayout_parameter);
                    }
                    char *model = gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->KeyboardModelCombo))?(char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->KeyboardModelCombo)):"";
                    // char *layout_switch;
                    yon_config_register(xkbmodel_parameter,xkbmodel_parameter_command,model);
                    yon_config_register(locale_parameter,locale_parameter_command,layouts_list);

                    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),page+1);
                } break;
                default:{ // all other pages
                    gtk_widget_set_sensitive(widgets->BackButton,1);
                    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),page+1);
                }break;
                case YON_PAGE_INSTALL_COMMON: {
                    GtkTreeModel *model;
                    GtkTreeIter iter;
                    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->CommonInstallationDevicesTree)),&model,&iter)){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),NO_DEVICE_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(widgets->CommonInstallationDevicesTree));
                        return;
                    }
                    char *file_system_type = (char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widgets->CommonInstallationFilesystemTypeCombo));
                    char *device_name = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->CommonInstallationSectionNameEntry));
                    char *device;
                    gtk_tree_model_get(model,&iter,0,&device,-1);
                    yon_config_register(AUTOINSTALL_TYPE_INSTALL,AUTOINSTALL_TYPE_INSTALL_command,"fast");
                    yon_config_register(AUTOINSTALL_TYPE_INSTALL,AUTOINSTALL_DEVICE_command,device);
                    yon_config_register(device_label_parameter,device_label_parameter_command,device_name);
                    yon_config_register(part_type_parameter,part_type_parameter_command,file_system_type);
                    
                    int size=0;
                    config_str parameters = yon_config_get_all(&size);
                    main_config.config_save_thread = g_thread_new("savethread",(GThreadFunc)on_config_save,save_config_command(yon_char_parsed_to_string(parameters,size," ")));
                    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_INSTALLATION);
                }break;
                case YON_PAGE_INSTALL_SEPARATE:
                    {
                    GtkTreeModel *model;
                    GtkTreeIter iter;
                    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->InstallationNearSysDevicesTree)),&model,&iter)){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),NO_DEVICE_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(widgets->CommonInstallationDevicesTree));
                        return;
                    }
                    char *device;
                    gtk_tree_model_get(model,&iter,0,&device,-1);
                    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->InstallationNearSysSectionTree)),&model,&iter)){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),NO_DEVICE_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(widgets->CommonInstallationDevicesTree));
                        return;
                    }
                    double part_size = gtk_spin_button_get_value(GTK_SPIN_BUTTON(widgets->InstallationNearSizeSpin));
                    if (part_size){
                        char *size_letter = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->InstallationNearSizeTypeSpin));
                        char *size_final = yon_char_append(yon_char_from_long((long)part_size),size_letter);
                        yon_config_register(part_size_parameter,part_size_parameter_command,size_final);
                    } else yon_config_remove_by_key(part_size_parameter);
                    char *file_system_type = (char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widgets->NextInstallationFilesystemTypeCombo));
                    char *device_name = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->NextInstallationSectionNameEntry));
                    char *part;
                    gtk_tree_model_get(model,&iter,0,&part,-1);
                        yon_config_register(AUTOINSTALL_TYPE_INSTALL,AUTOINSTALL_TYPE_INSTALL_command,"near");
                        yon_config_register(AUTOINSTALL_TYPE_INSTALL,AUTOINSTALL_DEVICE_command,device);
                        yon_config_register(AUTOINSTALL_TYPE_INSTALL,AUTOSTART_PARTS_command,part);
                    yon_config_register(device_label_parameter,device_label_parameter_command,device_name);
                    yon_config_register(part_type_parameter,part_type_parameter_command,file_system_type);
                    if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->NextInstallationFormatCheck)))
                        yon_config_register(device_format_parameter,device_format_parameter_command,"no");
                    
                    int size=0;
                    config_str parameters = yon_config_get_all(&size);
                    main_config.config_save_thread = g_thread_new("savethread",(GThreadFunc)on_config_save,save_config_command(yon_char_parsed_to_string(parameters,size," ")));
                    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_INSTALLATION);
                    }
                    break;
                case YON_PAGE_INSTALL_SAME_PARTITION:
                {
                    GtkTreeModel *model;
                    GtkTreeIter iter;
                    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SamePlaceDeviceTree)),&model,&iter)){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),NO_DEVICE_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(widgets->CommonInstallationDevicesTree));
                        return;
                    }
                    char *device;
                    gtk_tree_model_get(model,&iter,0,&device,-1);
                    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SamePlacePartTree)),&model,&iter)){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),NO_DEVICE_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(widgets->CommonInstallationDevicesTree));
                        return;
                    }
                    double part_size = gtk_spin_button_get_value(GTK_SPIN_BUTTON(widgets->SamePlaceSizeSpin));
                    if (part_size){
                        char *size_letter = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(widgets->SamePlaceSizeTypeSpin));
                        char *size_final = yon_char_append(yon_char_from_long((long)part_size),size_letter);
                        yon_config_register(part_size_parameter,part_size_parameter_command,size_final);
                    } else yon_config_remove_by_key(part_size_parameter);
                    char *file_system_type = (char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(widgets->SameInstallationFilesystemTypeCombo));
                    char *device_name = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->SameInstallationSectionNameEntry));
                    char *part;
                    gtk_tree_model_get(model,&iter,0,&part,-1);
                        yon_config_register(AUTOINSTALL_TYPE_INSTALL,AUTOINSTALL_TYPE_INSTALL_command,"near");
                        yon_config_register(AUTOINSTALL_TYPE_INSTALL,AUTOINSTALL_DEVICE_command,device);
                        yon_config_register(AUTOINSTALL_TYPE_INSTALL,AUTOSTART_PARTS_command,part);
                    yon_config_register(device_label_parameter,device_label_parameter_command,device_name);
                    yon_config_register(part_type_parameter,part_type_parameter_command,file_system_type);
                    if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->NextInstallationFormatCheck)))
                        yon_config_register(device_format_parameter,device_format_parameter_command,"no");
                    
                    int size=0;
                    config_str parameters = yon_config_get_all(&size);
                    main_config.config_save_thread = g_thread_new("savethread",(GThreadFunc)on_config_save,save_config_command(yon_char_parsed_to_string(parameters,size," ")));
                    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_INSTALLATION);
                }break;
                case YON_PAGE_USERS:{ //users
                    if (yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(widgets->UserNameEntry)))){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(widgets->UserNameEntry);
                        return;
                    } else if (yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(widgets->LoginEntry)))){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(widgets->LoginEntry);
                        return;

                    } else if (gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->PasswordCombo))==1&&yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(widgets->PasswordEntry)))){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(widgets->PasswordEntry);
                        return;

                    } else if (gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->AdminPasswordCombo))==1&&yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(widgets->AdminPasswordEntry)))){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(widgets->AdminPasswordEntry);
                        return;

                    } else if (yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(widgets->HotnameEntry)))){

                        yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(widgets->HotnameEntry);
                        return;
                    }
                    char *username = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->UserNameEntry));
                    char *login = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->UserNameEntry));
                    char *password = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->UserNameEntry));
                    char *root_password = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->UserNameEntry));
                    char *hostname = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->UserNameEntry));
                    char *autologin = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->AutologinCheck))?"yes":"no";
                    yon_config_register(user_name_parameter,user_name_parameter_command,login);
                    yon_config_register(user_gecos_parameter,user_gecos_parameter_command,username);
                    yon_config_register(user_password_parameter,user_password_parameter_command,password);
                    yon_config_register(root_password_parameter,root_password_parameter_command,root_password);
                    yon_config_register(autologin_parameter,autologin_parameter_command,autologin);
                    yon_config_register(xkbmodel_parameter,xkbmodel_parameter_command,hostname);
                    yon_config_register(hostname_parameter,hostname_parameter_command,root_password);
                    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_COMPLETION);
                }break;
                case YON_PAGE_COMPLETION:
                    if (!system("reboot"))
                    return;
                break;
            }
        } break;
        case -1: { // Previous
            switch (page){
                case YON_PAGE_REGION: {

                    gtk_widget_set_sensitive(widgets->BackButton,0);
                    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),page-1);
                }break;
                case YON_PAGE_INSTALL_COMMON:
                case YON_PAGE_INSTALL_SEPARATE:
                case YON_PAGE_INSTALL_SAME_PARTITION:
                    {
                        gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),2);
                    } break;
                default:{
                    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),page-1);
                }
            }

        } break;
        case -2: { // Cancel/repeat
            switch (page){
                default:{
                    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),0);
                }
            }
        }break;
    }
}

void on_region_changed(GtkComboBox *self, main_window *widgets);
void on_region_changed(GtkComboBox *self, main_window *widgets){
    char *active = (char*)gtk_combo_box_get_active_id(self);
    active = yon_char_append("/usr/share/zoneinfo/",active);
    int size;
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->ZoneCombo));
    if (yon_file_is_directory(active)){
        config_str parsed = yon_file_ls(active,&size);
        for (int i=0;i<size;i++){
            gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->ZoneCombo),parsed[i],_(parsed[i]));
        }
    }
}

void on_locale_changed(GtkWidget *,main_window *);
void on_locale_changed(GtkWidget *,main_window *){

}

void on_additional_software_toggled();
void on_additional_software_toggled(){

}

void on_near_installation_device_changed(GtkWidget *self, main_window *widgets);
void on_near_installation_device_changed(GtkWidget *self, main_window *widgets){
    gtk_list_store_clear(widgets->PartitionsList);
    GtkTreeIter iter;
    GtkTreeModel *model;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(self)),&model,&iter)){
        char *disk_path="";
        gtk_tree_model_get(model,&iter,0,&disk_path,-1);
        int size;
        config_str parsed;
        parsed = yon_config_load(get_parts_and_devices_command,&size);
        char *string = yon_char_parsed_to_string(parsed,size,"");
        struct json_object *root;
        struct json_object *blockdevices;
        root = json_tokener_parse(string);
        json_object_object_get_ex(root, "blockdevices", &blockdevices);
        for (long unsigned int i = 0; i < json_object_array_length(blockdevices); i++) {
            struct json_object *device = json_object_array_get_idx(blockdevices, i);
            struct json_object *type, *path, *size, *model, *vendor, *serial;

            json_object_object_get_ex(device, "type", &type);
            if (strcmp("part",json_object_get_string(type)))
                continue;
            json_object_object_get_ex(device, "path", &path);
            if (!strstr(json_object_get_string(path),disk_path)){
                continue;
            }
            json_object_object_get_ex(device, "size", &size);
            json_object_object_get_ex(device, "model", &model);
            json_object_object_get_ex(device, "vendor", &vendor);
            json_object_object_get_ex(device, "serial", &serial);

            gtk_list_store_append(widgets->PartitionsList,&iter);
            gtk_list_store_set(widgets->PartitionsList,&iter,0,json_object_get_string(path),1,json_object_get_string(model),2,json_object_get_string(serial),3,json_object_get_string(size),4,json_object_get_string(vendor),-1);
        }
        yon_char_parsed_free(parsed,size);
    }
}

void on_same_installation_device_changed(GtkWidget *, main_window *widgets);
void on_same_installation_device_changed(GtkWidget *, main_window *widgets){
    gtk_list_store_clear(widgets->PartitionsList);
    GtkTreeIter iter;
    GtkTreeModel *model;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SamePlaceDeviceTree)),&model,&iter)){
        char *disk_path="";
        gtk_tree_model_get(model,&iter,0,&disk_path,-1);
        int size;
        config_str parsed;
        parsed = yon_config_load(get_parts_and_devices_command,&size);
        char *string = yon_char_parsed_to_string(parsed,size,"");
        struct json_object *root;
        struct json_object *blockdevices;
        root = json_tokener_parse(string);
        json_object_object_get_ex(root, "blockdevices", &blockdevices);
        for (long unsigned int i = 0; i < json_object_array_length(blockdevices); i++) {
            struct json_object *device = json_object_array_get_idx(blockdevices, i);
            struct json_object *type, *path, *size, *model, *vendor, *serial;

            json_object_object_get_ex(device, "type", &type);
            if (strcmp("part",json_object_get_string(type)))
                continue;
            json_object_object_get_ex(device, "path", &path);
            if (!strstr(json_object_get_string(path),disk_path)){
                continue;
            }
            json_object_object_get_ex(device, "size", &size);
            json_object_object_get_ex(device, "model", &model);
            json_object_object_get_ex(device, "vendor", &vendor);
            json_object_object_get_ex(device, "serial", &serial);

            gtk_list_store_append(widgets->PartitionsList,&iter);
            gtk_list_store_set(widgets->PartitionsList,&iter,0,json_object_get_string(path),1,json_object_get_string(model),2,json_object_get_string(serial),3,json_object_get_string(size),4,json_object_get_string(vendor),-1);
        }
        yon_char_parsed_free(parsed,size);
    }
}

/**yon_main_window_complete(main_window *widgets) 
 * [EN] 
 *  
 * [RU] 
 * Функция настройки основного окна приложения. [widgets] - структура со стандартным интерфейсом. 
*/ 
main_window *yon_main_window_complete(){ 
    main_window *widgets=NULL;
    widgets = yon_remalloc(widgets,sizeof(main_window)); 
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path); 
    // Custom widgets configuration 
    widgets->builder = builder;

    widgets->DevicesList=GTK_LIST_STORE(gtk_builder_get_object(builder,"DevicesList"));
	widgets->LanguagesList=GTK_LIST_STORE(gtk_builder_get_object(builder,"LanguagesList"));
	widgets->MainWindow=yon_gtk_builder_get_widget(builder,"MainWindow");
	widgets->WelcomeToggle=yon_gtk_builder_get_widget(builder,"WelcomeToggle");
	widgets->LicenceToggle=yon_gtk_builder_get_widget(builder,"LicenceToggle");
	widgets->LocationToggle=yon_gtk_builder_get_widget(builder,"LocationToggle");
	widgets->KeyboardToggle=yon_gtk_builder_get_widget(builder,"KeyboardToggle");
	widgets->SectionsToggle=yon_gtk_builder_get_widget(builder,"SectionsToggle");
	widgets->UsersToggle=yon_gtk_builder_get_widget(builder,"USersToggle");
	widgets->SummaryToggle=yon_gtk_builder_get_widget(builder,"SummaryToggle");
	widgets->InstallationToggle=yon_gtk_builder_get_widget(builder,"InstallationToggle");
	widgets->LanguageCombo=yon_gtk_builder_get_widget(builder,"LanguageCombo");
	widgets->RegionCombo=yon_gtk_builder_get_widget(builder,"RegionCombo");
	widgets->ZoneCombo=yon_gtk_builder_get_widget(builder,"ZoneCombo");
	widgets->AvailableLanguagesEntry=yon_gtk_builder_get_widget(builder,"AvailableLanguagesEntry");
	widgets->AvailableLanguagesButton=yon_gtk_builder_get_widget(builder,"AvailableLanguagesButton");
	widgets->LanguagesCombo=yon_gtk_builder_get_widget(builder,"LanguagesCombo");
	widgets->KeyboardModelCombo=yon_gtk_builder_get_widget(builder,"KeyboardModelCombo");
	widgets->LayoutBindingCombo=yon_gtk_builder_get_widget(builder,"LayoutBindingCombo");
	widgets->DefaultLayoutRadio=yon_gtk_builder_get_widget(builder,"DefaultLayoutRadio");
	widgets->ManualLayoutRadio=yon_gtk_builder_get_widget(builder,"ManualLayoutRadio");
	widgets->LayoutTree=yon_gtk_builder_get_widget(builder,"LayoutTree");
	widgets->AddButton=yon_gtk_builder_get_widget(builder,"AddButton");
	widgets->RemoveButton=yon_gtk_builder_get_widget(builder,"RemoveButton");
	widgets->InstallationRadio=yon_gtk_builder_get_widget(builder,"InstallationRadio");
	widgets->InstallationNearRadio=yon_gtk_builder_get_widget(builder,"InstallationNearRadio");
	widgets->InstallationLinuxRadio=yon_gtk_builder_get_widget(builder,"InstallationLinuxRadio");
	widgets->InstallationWindowsRadio=yon_gtk_builder_get_widget(builder,"InstallationWindowsRadio");
	widgets->InstallationOptionsRadio=yon_gtk_builder_get_widget(builder,"InstallationOptionsRadio");
	widgets->UserNameEntry=yon_gtk_builder_get_widget(builder,"UserNameEntry");
	widgets->LoginEntry=yon_gtk_builder_get_widget(builder,"LoginEntry");
	widgets->PasswordCombo=yon_gtk_builder_get_widget(builder,"PasswordCombo");
	widgets->PasswordEntry=yon_gtk_builder_get_widget(builder,"PasswordEntry");
	widgets->PasswordButton=yon_gtk_builder_get_widget(builder,"PasswordButton");
	widgets->AutologinCheck=yon_gtk_builder_get_widget(builder,"AutologinCheck");
	widgets->AdminPasswordCombo=yon_gtk_builder_get_widget(builder,"AdminPasswordCombo");
	widgets->AdminPasswordEntry=yon_gtk_builder_get_widget(builder,"AdminPasswordEntry");
	widgets->AdminPasswordButton=yon_gtk_builder_get_widget(builder,"AdminPasswordButton");
	widgets->HotnameEntry=yon_gtk_builder_get_widget(builder,"HotnameEntry");
	widgets->CancelInstallButton=yon_gtk_builder_get_widget(builder,"CancelInstallButton");
	widgets->BackButton=yon_gtk_builder_get_widget(builder,"BackButton");
	widgets->NextButton=yon_gtk_builder_get_widget(builder,"NextButton");
	widgets->mainSettingsButton=yon_gtk_builder_get_widget(builder,"mainSettingsButton");
	widgets->menu1=yon_gtk_builder_get_widget(builder,"menu1");
	widgets->menu2=yon_gtk_builder_get_widget(builder,"menu2");
    widgets->RegionImage = yon_gtk_builder_get_widget(builder,"RegionImage");
    widgets->RegionBox = yon_gtk_builder_get_widget(builder,"RegionBox");
    widgets->RegionAspect = yon_gtk_builder_get_widget(builder,"RegionAspect");
    widgets->KeyboardImage = yon_gtk_builder_get_widget(builder,"KeyboardImage");
    widgets->KeyboardBox = yon_gtk_builder_get_widget(builder,"KeyboardBox");
    widgets->Notebook = yon_gtk_builder_get_widget(builder,"Notebook");
    widgets->LicenceLabel = yon_gtk_builder_get_widget(builder,"LicenceLabel");
    widgets->SlidesImage = yon_gtk_builder_get_widget(builder,"SlidesImage");
    widgets->CommonInstallationDevicesTree = yon_gtk_builder_get_widget(builder,"CommonInstallationDevicesTree");
    widgets->AdditionalSoftwareTree = yon_gtk_builder_get_widget(builder,"AdditionalSoftwareTree");

    widgets->SamePlaceDeviceTree = yon_gtk_builder_get_widget(builder,"SamePlaceDeviceTree");
    widgets->SamePlacePartTree = yon_gtk_builder_get_widget(builder,"SamePlacePartTree");
    widgets->SamePlaceSizeSpin = yon_gtk_builder_get_widget(builder,"SamePlaceSizeSpin");
    widgets->SamePlaceSizeTypeSpin = yon_gtk_builder_get_widget(builder,"SamePlaceSizeTypeSpin");

    widgets->InstallationNearSysDevicesTree = yon_gtk_builder_get_widget(builder,"InstallationNearSysDevicesTree");
    widgets->InstallationNearSysSectionTree = yon_gtk_builder_get_widget(builder,"InstallationNearSysSectionTree");
    widgets->InstallationNearSizeSpin = yon_gtk_builder_get_widget(builder,"InstallationNearSizeSpin");
    widgets->InstallationNearSizeTypeSpin = yon_gtk_builder_get_widget(builder,"InstallationNearSizeTypeSpin");

    widgets->InstallationProgress = yon_gtk_builder_get_widget(builder,"InstallationProgress");
    widgets->InstallationLabel = yon_gtk_builder_get_widget(builder,"InstallationLabel");
    widgets->PackageInstallationProgress = yon_gtk_builder_get_widget(builder,"PackageInstallationProgress");
    widgets->PackageInstallationLabel = yon_gtk_builder_get_widget(builder,"PackageInstallationLabel");

    widgets->SameInstallationFilesystemTypeCombo = yon_gtk_builder_get_widget(builder,"SameInstallationFilesystemTypeCombo");
    widgets->SameInstallationFormatCheck = yon_gtk_builder_get_widget(builder,"SameInstallationFormatCheck");
    widgets->SameInstallationSectionNameEntry = yon_gtk_builder_get_widget(builder,"SameInstallationSectionNameEntry");
    widgets->NextInstallationFilesystemTypeCombo = yon_gtk_builder_get_widget(builder,"NextInstallationFilesystemTypeCombo");
    widgets->NextInstallationFormatCheck = yon_gtk_builder_get_widget(builder,"NextInstallationFormatCheck");
    widgets->NextInstallationSectionNameEntry = yon_gtk_builder_get_widget(builder,"NextInstallationSectionNameEntry");
    widgets->CommonInstallationFilesystemTypeCombo = yon_gtk_builder_get_widget(builder,"CommonInstallationFilesystemTypeCombo");
    widgets->CommonInstallationSectionNameEntry = yon_gtk_builder_get_widget(builder,"CommonInstallationSectionNameEntry");

    widgets->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    widgets->AdditionalSoftwareCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"AdditionalSoftwareCell"));

    widgets->LanguagesList = GTK_LIST_STORE(gtk_builder_get_object(builder,"LanguagesList"));
    widgets->LanguagesFilter = GTK_TREE_MODEL(gtk_builder_get_object(builder,"LanguagesFilter"));
    widgets->LayoutsFilter = GTK_TREE_MODEL(gtk_builder_get_object(builder,"LayoutsFilter"));
    widgets->LayoutList = GTK_TREE_STORE(gtk_builder_get_object(builder,"LayoutList"));
    widgets->AdditionalSoftwareList = GTK_LIST_STORE(gtk_builder_get_object(builder,"AdditionalSoftwareList"));
    widgets->PartitionsList = GTK_LIST_STORE(gtk_builder_get_object(builder,"PartitionsList"));

    g_signal_connect(G_OBJECT(widgets->Notebook),"switch-page",G_CALLBACK(on_page_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->MainWindow),"check-resize",G_CALLBACK(on_region_resized),widgets);
    g_signal_connect(G_OBJECT(widgets->BackButton),"clicked",G_CALLBACK(on_page_navigation_clicked),widgets);
    g_signal_connect(G_OBJECT(widgets->NextButton),"clicked",G_CALLBACK(on_page_navigation_clicked),widgets);
    g_signal_connect(G_OBJECT(widgets->CancelInstallButton),"clicked",G_CALLBACK(on_page_navigation_clicked),widgets);
    g_signal_connect(G_OBJECT(widgets->AvailableLanguagesButton),"clicked",G_CALLBACK(on_language_clicked),widgets);
    g_signal_connect(G_OBJECT(widgets->RegionCombo),"changed",G_CALLBACK(on_region_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->AddButton),"clicked",G_CALLBACK(on_keyboard_clicked),widgets);

    g_signal_connect(G_OBJECT(widgets->UsersToggle),"toggled",G_CALLBACK(on_toggle_block),NULL);
    g_signal_connect(G_OBJECT(widgets->LicenceToggle),"toggled",G_CALLBACK(on_toggle_block),NULL);
    g_signal_connect(G_OBJECT(widgets->WelcomeToggle),"toggled",G_CALLBACK(on_toggle_block),NULL);
    g_signal_connect(G_OBJECT(widgets->KeyboardToggle),"toggled",G_CALLBACK(on_toggle_block),NULL);
    g_signal_connect(G_OBJECT(widgets->LocationToggle),"toggled",G_CALLBACK(on_toggle_block),NULL);
    g_signal_connect(G_OBJECT(widgets->SectionsToggle),"toggled",G_CALLBACK(on_toggle_block),NULL);
    g_signal_connect(G_OBJECT(widgets->InstallationToggle),"toggled",G_CALLBACK(on_toggle_block),NULL);
    g_signal_connect(G_OBJECT(widgets->InstallationNearSysDevicesTree),"cursor-changed",G_CALLBACK(on_near_installation_device_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->SamePlaceDeviceTree),"cursor-changed",G_CALLBACK(on_near_installation_device_changed),widgets);
    gtk_tree_model_filter_set_visible_column(GTK_TREE_MODEL_FILTER(widgets->LayoutsFilter),3);

    g_signal_connect(G_OBJECT(widgets->LanguageCombo),"changed",G_CALLBACK(on_locale_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->AdditionalSoftwareCell),"toggled",G_CALLBACK(on_additional_software_toggled),widgets);

    g_signal_connect(G_OBJECT(widgets->ManualLayoutRadio),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),gtk_widget_get_parent(gtk_widget_get_parent(widgets->AddButton)));
    // g_signal_connect(G_OBJECT(widgets->InstallationToggle),"toggled",G_CALLBACK(on_toggle_block),widgets);

    gtk_tree_model_filter_set_visible_column(GTK_TREE_MODEL_FILTER(widgets->LanguagesFilter),0);
    gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->LanguagesFilter));

    gtk_window_set_title(GTK_WINDOW(widgets->MainWindow),TITLE_LABEL);
    gtk_window_set_icon_name(GTK_WINDOW(widgets->MainWindow),icon_path);

    int size;
    config_str parsed = yon_file_open(licence_path,&size);
    if (size){
        char *licence = yon_char_parsed_to_string(parsed,size,"");
        gtk_label_set_text(GTK_LABEL(widgets->LicenceLabel),licence);
        free(licence);
        yon_char_parsed_free(parsed,size);
    }

    widgets->regions_original = gdk_pixbuf_new_from_resource(regions_path,NULL);
    widgets->keyboard_original = gdk_pixbuf_new_from_resource(keyboard_path,NULL);
    widgets->slides_original = gdk_pixbuf_new_from_resource(slide_0_path,NULL);
    int width = gdk_pixbuf_get_width(widgets->regions_original);
    int height = gdk_pixbuf_get_height(widgets->regions_original);
    widgets->region_height_mult = (float)height/width; 
    gtk_image_set_from_pixbuf(GTK_IMAGE(widgets->RegionImage),gdk_pixbuf_scale_simple(widgets->regions_original,600,400,GDK_INTERP_BILINEAR));
    gtk_image_set_from_pixbuf(GTK_IMAGE(widgets->KeyboardImage),gdk_pixbuf_scale_simple(widgets->keyboard_original,600,400,GDK_INTERP_BILINEAR));
    gtk_image_set_from_pixbuf(GTK_IMAGE(widgets->SlidesImage),gdk_pixbuf_scale_simple(widgets->slides_original,600,400,GDK_INTERP_BILINEAR));

    GtkTreeIter iter;
    gtk_list_store_clear(widgets->LanguagesList);
    parsed = yon_file_open(languages_path,&size);
    for (int i=0;i<size;i++){
        int cur_size;
        yon_char_remove_last_symbol(parsed[i],'\n')
        config_str cur = yon_char_parse(parsed[i],&cur_size,";");
        if (cur_size){
            gtk_list_store_append(widgets->LanguagesList,&iter);
            gtk_list_store_set(widgets->LanguagesList,&iter,0,0,1,_(cur[1]),2,cur[0],-1);
        }
        yon_char_parsed_free(cur,cur_size);
    }
    yon_char_parsed_free(parsed,size);

    parsed = yon_dir_get_contents(zone_path,&size);
    for (int i=0;i<size;i++){
        if (!strstr(parsed[i],".")&&strcmp(parsed[i],"posix")&&strcmp(parsed[i],"right")){
            char *path = yon_char_append(zone_path,parsed[i]);
            if (yon_file_is_directory(path)){
                gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->RegionCombo),parsed[i],_(parsed[i]));
            }
            free(path);
        }
    }
    yon_char_parsed_free(parsed,size);
    parsed = yon_config_load(get_layouts_command,&size);
    GtkTreeIter itar;
    for (int i=0;i<size;i++){
        yon_char_remove_last_symbol(parsed[i],'\n');
        int layout_size;
        config_str layout = yon_char_parse(parsed[i],&layout_size,"|");
        if (layout_size){
            char *layout_id = yon_char_new(layout[0]);
            gtk_tree_store_append(widgets->LayoutList,&iter,NULL);
            gtk_tree_store_set(widgets->LayoutList,&iter,0,layout[0],1,_(layout[1]),2,1,-1);
            yon_char_parsed_free(layout,layout_size);
            layout = yon_config_load(get_layouts_local_command(layout_id),&layout_size);
            for (int j=0;j<layout_size;j++){
                yon_char_remove_last_symbol(layout[j],'\n');
                int parsed_size;
                config_str layouts_parsed = yon_char_parse(layout[j],&parsed_size,"|");
                if (parsed_size){
                    gtk_tree_store_append(widgets->LayoutList,&itar,&iter);
                    gtk_tree_store_set(widgets->LayoutList,&itar,0,layouts_parsed[0],1,_(layouts_parsed[1]),2,1,3,0,-1);
                    yon_char_parsed_free(layouts_parsed,parsed_size);
                }
            }
            if (layout_size==-1) {
                gtk_tree_store_set(widgets->LayoutList,&iter,2,1,-1);
            }
            yon_char_parsed_free(layout,layout_size);
        }
    }
    yon_char_parsed_free(parsed,size);
    {
        parsed = yon_config_load(get_devices_command,&size);
        char *string = yon_char_parsed_to_string(parsed,size,"");
        struct json_object *root;
        struct json_object *blockdevices;
        root = json_tokener_parse(string);
        json_object_object_get_ex(root, "blockdevices", &blockdevices);
        for (long unsigned int i = 0; i < json_object_array_length(blockdevices); i++) {
            struct json_object *device = json_object_array_get_idx(blockdevices, i);
            struct json_object *path, *size, *model, *vendor, *serial;

            json_object_object_get_ex(device, "path", &path);
            json_object_object_get_ex(device, "size", &size);
            json_object_object_get_ex(device, "model", &model);
            json_object_object_get_ex(device, "vendor", &vendor);
            json_object_object_get_ex(device, "serial", &serial);

            gtk_list_store_append(widgets->DevicesList,&iter);
            gtk_list_store_set(widgets->DevicesList,&iter,0,json_object_get_string(path),1,json_object_get_string(model),2,json_object_get_string(serial),3,json_object_get_string(size),4,json_object_get_string(vendor),-1);
        }
        yon_char_parsed_free(parsed,size);
    }
    
    parsed = yon_resource_open_file(additional_software_path,&size);
    for (int i=1;i<size;i++){
        int module_size;
        config_str module_parsed = yon_char_parse(parsed[i],&module_size,"|");
        if (module_size){
            gtk_list_store_append(widgets->AdditionalSoftwareList,&iter);
            gtk_list_store_set(widgets->AdditionalSoftwareList,&iter,0,1,1,module_parsed[0],3,module_parsed[1],-1); //2,module_parsed[2]
        }
    }

    config_str models = yon_config_load(get_models_command,&size);
    for (int i=0;i<size;i+=2){
        models[i] = yon_char_divide_search(models[i],"\n",-1);
        models[i+1] = yon_char_divide_search(models[i+1],"\n",-1);
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->KeyboardModelCombo),models[i],_(models[i+1]));
    }

    return widgets;
}
 
int main(int argc, char *argv[]){ 
    local=setlocale(LC_ALL, ""); 
    textdomain (LocaleName); 
    config_init(); 
    int option_index=0; 
    int show_help=0; 
    { 
        struct option long_options[] = { 
                {"help", 0, 0, 'h'}, 
                {"version", 0, 0, 'V'}, 
                {"lock-help", 0,0, 1}, 
                {"lock-save", 0,0, 2}, 
                {"lock-save-local", 0,0, 3}, 
                {"lock-save-global", 0,0, 4}, 
                {"lock-load-global", 0,0, 5}, 
                {"socket-id", 1, 0, 's'}, 
                {"socket-ext-id", 1,0, 'e'}, 
                {"socket-trd-id", 1,0, 't'}, 
                {"clear-config", 0,0, 'c'}, 
                {"debug", 0,0, 'd'}, 
                { NULL, 0, NULL, 0 } 
        }; 
        for (int i=0;i<argc;i++){ 
            int argument=getopt_long(argc,argv,"hVvs:e:t:cd",long_options,&option_index); 
            switch(argument){ 
                case 'h': 
                    show_help=1; 
                    break; 
                case 'v': 
                case 'V': 
                    printf("%s\n",VERSION_LABEL); 
                    exit(0); 
                    break; 
                case 's': 
                    if(optarg) 
                        main_config.socket_id=atoi(optarg); 
                    break; 
                case 'e': 
                    if(optarg) 
                        main_config.save_socket_id=atoi(optarg); 
                    break; 
                case 't': 
                    if(optarg) 
                        main_config.load_socket_id=atoi(optarg); 
                    break; 
                case 'c': if (system(yon_char_unite("rm -rfd ", yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",NULL))){}; 
                    break; 
                case 'd': main_config.debug_mode=1; 
                    break;
                case 1: 
                    main_config.lock_help=1; 
                    break; 
                case 2: 
                    main_config.lock_save_local=1; 
                    main_config.lock_save_global=1; 
                    break; 
                case 3: 
                    main_config.lock_save_local=1; 
                    break; 
                case 4: 
                    main_config.lock_save_global=1; 
                    break; 
                case 5: 
                    main_config.lock_load_global=1; 
                    break;
            } 
        } 
        if (show_help==1&&main_config.lock_help!=1){ 
            printf("%s\n",HELP_LABEL(NULL)); 
            exit(0); 
            } 
    } 
 
    if (getuid()!=0){ 
        main_config.lock_load_global=1; 
        main_config.lock_save_global=1; 
        main_config.lock_save_local=1; 
    } 
    gtk_init(&argc,&argv); 
    main_window *widgets = NULL;
    widgets = yon_main_window_complete((main_window*)widgets); 
    yon_window_config_setup(GTK_WINDOW(widgets->MainWindow));

    yon_window_config_load(config_path);
    GtkCssProvider *css=gtk_css_provider_new();
    gtk_css_provider_load_from_resource(css,CssPath);
    gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
                            GTK_STYLE_PROVIDER(css),
                            -1);
    gtk_main(); 
}