#include "ubinstall-gtk.h" 
 
config main_config; 
 
// //functions 
void on_vnc_toggled(GtkWidget *self, main_window *){
    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(self))){
        yon_launch(enable_vnc_command);
    } else {
        yon_launch(disable_vnc_command);
    }
}

void on_pacman_software_all_toggled(GtkWidget *, char *path, main_window *widgets){
    GtkTreeIter iter;
    char *name, *type, *description;
    gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(widgets->PacmanSoftwareAllList),&iter,path);
    gtk_tree_model_get(GTK_TREE_MODEL(widgets->PacmanSoftwareAllList),&iter,1,&name,2,&type,3,&description,-1);
    gtk_list_store_remove(widgets->PacmanSoftwareAllList,&iter);
    gtk_list_store_append(widgets->PacmanSoftwareChosenList,&iter);
    gtk_list_store_set(widgets->PacmanSoftwareChosenList,&iter,0,1,1,name,2,type,3,description,-1);
    g_hash_table_add(widgets->pacmanchosen,yon_char_new(name));

}

void on_pacman_software_chosen_toggled(GtkWidget *, char *path, main_window *widgets){
    GtkTreeIter iter;
    char *name, *type, *description;
    gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(widgets->PacmanSoftwareChosenList),&iter,path);
    gtk_tree_model_get(GTK_TREE_MODEL(widgets->PacmanSoftwareChosenList),&iter,1,&name,2,&type,3,&description,-1);
    gtk_list_store_remove(widgets->PacmanSoftwareChosenList,&iter);
    gtk_list_store_append(widgets->PacmanSoftwareAllList,&iter);
    gtk_list_store_set(widgets->PacmanSoftwareAllList,&iter,0,0,1,name,2,type,3,description,-1);
    g_hash_table_remove(widgets->pacmanchosen,name);
}

gboolean yon_pacman_fill(struct pacman_struct *pacman){
    main_window *widgets = pacman->widgets;
    config_str loaded = pacman->packages;
    int size = pacman->packages_size;


    for (int i=0;i<size;i++){
        if (g_hash_table_contains(widgets->pacmanchosen,loaded[i])) continue;
        char *version = yon_packages_get_version(YON_PACKAGES_SYNC,loaded[i]);
        char *description = yon_packages_get_description(YON_PACKAGES_SYNC,loaded[i]);
        GtkTreeIter iter;
        gtk_list_store_append(widgets->PacmanSoftwareAllList,&iter);
        gtk_list_store_set(widgets->PacmanSoftwareAllList,&iter,0,0,1,loaded[i],2,version,3,description,-1);
        if (!yon_char_is_empty(version))free(version);
        if (!yon_char_is_empty(description)) free(description);
    }
    yon_char_parsed_free(loaded,size);
    gtk_widget_set_sensitive(widgets->PacmanSoftwareSearchEntry,1);
    gtk_widget_set_sensitive(widgets->PacmanSoftwareChosenPackagesTree,1);
    gtk_widget_set_sensitive(widgets->PacmanSoftwareStatusButton,1);
    g_object_ref(G_OBJECT(widgets->MainSpinner));
    gtk_container_remove(GTK_CONTAINER(widgets->PacmanLoadingOverlay),widgets->MainSpinner);
    return G_SOURCE_REMOVE;
}

void *yon_pacman_load(struct pacman_struct *pacman){
    const char *find_package = pacman->find_package;
        // char *command = get_local_module_info_command(find_package);
        pacman->packages = yon_packages_find(YON_PACKAGES_SYNC,find_package,&pacman->packages_size);
            g_idle_add((GSourceFunc)yon_pacman_fill,pacman);
        return NULL;
}

void on_pacman_icon_press(GtkEntry *self,GtkEntryIconPosition icon_pos,GdkEvent* ,main_window *widgets){
    if(icon_pos != GTK_ENTRY_ICON_SECONDARY) return;
    
    gtk_list_store_clear(widgets->PacmanSoftwareAllList);

    const char *find_package = gtk_entry_get_text(self);
    struct pacman_struct *pacman = new (struct pacman_struct);
    pacman->widgets=widgets;
    pacman->find_package=find_package;
    gtk_widget_set_sensitive(widgets->PacmanSoftwareSearchEntry,0);
    gtk_widget_set_sensitive(widgets->PacmanSoftwareChosenPackagesTree,0);
    gtk_widget_set_sensitive(widgets->PacmanSoftwareStatusButton,0);
    gtk_overlay_add_overlay(GTK_OVERLAY(widgets->PacmanLoadingOverlay),widgets->MainSpinner);
    g_thread_new("pacman_modules_get_thread",(GThreadFunc)yon_pacman_load,pacman);
}

void on_toggle_button_switch_on(GtkWidget *, GtkToggleButton *toggle){
    gtk_toggle_button_set_active(toggle,1);
}

double yon_size_long_convert_to_mod(double size, char mod){
    int sizemod = yon_get_size_get_from_letter(mod);
    double final_size = size;
    for (int i=-1;i<sizemod;i++){
        final_size/=1024;
    }
    return final_size;
}

source_element *yon_source_element_new(){
    source_element *element = new(source_element);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_source_element);

    element->MainBox = yon_gtk_builder_get_widget(builder,"MainBox");
    element->TypeCombo = yon_gtk_builder_get_widget(builder,"TypeCombo");
    element->PathEntry = yon_gtk_builder_get_widget(builder,"PathEntry");
    element->PathButton = yon_gtk_builder_get_widget(builder,"PathButton");
    element->RemoveButton = yon_gtk_builder_get_widget(builder,"RemoveButton");

    g_signal_connect(G_OBJECT(element->RemoveButton),"clicked",G_CALLBACK(on_source_remove),element);
    g_signal_connect(G_OBJECT(element->PathButton),"clicked",G_CALLBACK(on_source_choose),element);
    g_object_set_data(G_OBJECT(element->PathButton),"combo",element->TypeCombo);

    return element;
}

void yon_source_element_add(char *key,void*,source_window *window){
    source_element *element = yon_source_element_new();
    gtk_entry_set_text(GTK_ENTRY(element->PathEntry),key);
    gtk_box_pack_start(GTK_BOX(window->AddBox),element->MainBox,0,0,0);
    g_object_set_data(G_OBJECT(element->MainBox),"window",window);
    g_object_set_data(G_OBJECT(element->PathButton),"target",window->PathEntry);
}

void yon_source_update(source_window *window){
    GList *list = gtk_container_get_children(GTK_CONTAINER(window->AddBox));
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        gtk_widget_destroy(GTK_WIDGET(iter->data));
    }
    g_hash_table_foreach(window->sources,(GHFunc)yon_source_element_add,window);
}

void on_source_add(GtkWidget *,source_window *window){
    const char *path = gtk_entry_get_text(GTK_ENTRY(window->PathEntry));
    if (!g_hash_table_contains(window->sources,path)){
        g_hash_table_add(window->sources,yon_char_new(path));
        yon_source_update(window);
    }
}

void on_source_remove(GtkWidget *,source_element *element){
    source_window *window = g_object_get_data(G_OBJECT(element->MainBox),"window");
    const char *path = gtk_entry_get_text(GTK_ENTRY(window->PathEntry));
    if (g_hash_table_contains(window->sources,path)){
        g_hash_table_remove(window->sources,path);
        yon_source_update(window);
    }
}

void on_source_choose(GtkWidget *self){
    GtkWidget *target_entry = g_object_get_data(G_OBJECT(self),"target");
    GtkWidget *target_mode = g_object_get_data(G_OBJECT(self),"combo");
    switch (gtk_combo_box_get_active(GTK_COMBO_BOX(target_mode))){
        case 0: {
            
        } break;
        case 1:{
            filechooser_window *dialog = yon_file_chooser_window_new(GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
        yon_gtk_window_setup(GTK_WINDOW(dialog->Window),GTK_WINDOW(dialog->Window),OPEN_LABEL,icon_path,"filechooser_window");
            if (yon_file_chooser_start(dialog)==GTK_RESPONSE_ACCEPT){
                gtk_entry_set_text(GTK_ENTRY(target_entry),dialog->last_success_selection);
                free(dialog);
            }

        } break;
        case 2: {
            filechooser_window *dialog = yon_file_chooser_window_new(GTK_FILE_CHOOSER_ACTION_OPEN);
        yon_gtk_window_setup(GTK_WINDOW(dialog->Window),GTK_WINDOW(dialog->Window),OPEN_LABEL,icon_path,"filechooser_window");
            GtkFileFilter *filter = gtk_file_filter_new();
            gtk_file_filter_add_pattern(filter,"*.iso");
            gtk_file_filter_set_name(filter,".iso");
            gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog->MainFileChooser),filter);

            if (yon_file_chooser_start(dialog)==GTK_RESPONSE_ACCEPT){
                gtk_entry_set_text(GTK_ENTRY(target_entry),dialog->last_success_selection);
                free(dialog);
            }

        } break;
    }
}

void on_source_accept(GtkWidget *,source_window *window){
    if (gtk_switch_get_active(GTK_SWITCH(window->AutoSwitch))){
        yon_config_register(source_parameter,source_parameter_command,"auto");
    }
    guint size;
    config_str paths = (config_str)g_hash_table_get_keys_as_array(window->sources,&size);
    if (!size){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->PathEntry);
        return;
    }
    char *parameter_string = yon_char_parsed_to_string(paths,(int)size,",");
    yon_config_register(source_parameter,source_parameter_command,parameter_string);
    free(parameter_string);
    on_subwindow_close(window->Window);

    if (!main_config.configure_mode){
        int size;
        config_str parameters = yon_config_get_save_parameters_by_key(&size,locale_parameter,lang_parameter,zone_parameter,NULL);
        if (parameters){
            char *command_parameters = yon_char_parsed_to_string(parameters,size," -- ");
            char *command = yon_char_unite(ubconfig_dull_command,"--target system ",command_parameters,NULL);
            if (!system(command)){}
            free(command);
            yon_char_parsed_free(parameters,size);
        }
    }
}

source_window *yon_source_window_new(){
    source_window *window = new(source_window);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_source);

    window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->AutoSwitch = yon_gtk_builder_get_widget(builder,"AutoSwitch");
    window->TypeCombo = yon_gtk_builder_get_widget(builder,"TypeCombo");
    window->PathEntry = yon_gtk_builder_get_widget(builder,"PathEntry");
    window->PathButton = yon_gtk_builder_get_widget(builder,"PathButton");
    window->AddButton = yon_gtk_builder_get_widget(builder,"AddButton");
    window->AddBox = yon_gtk_builder_get_widget(builder,"AddBox");
    window->sources = g_hash_table_new(g_str_hash,g_str_equal);

    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_source_accept),window);
    g_signal_connect(G_OBJECT(window->AddButton),"clicked",G_CALLBACK(on_source_add),window);
    g_signal_connect(G_OBJECT(window->PathButton),"clicked",G_CALLBACK(on_source_choose),window);
    g_signal_connect(G_OBJECT(window->AutoSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),window->AddBox);
    g_signal_connect(G_OBJECT(window->AutoSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),window->TypeCombo);
    g_signal_connect(G_OBJECT(window->AutoSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),window->AddButton);
    g_signal_connect(G_OBJECT(window->AutoSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),window->PathButton);
    g_signal_connect(G_OBJECT(window->AutoSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),window->PathEntry);
    g_object_set_data(G_OBJECT(window->PathButton),"combo",window->TypeCombo);
    g_object_set_data(G_OBJECT(window->PathButton),"target",window->PathEntry);
    return window;
}

void on_source_clicked(GtkWidget *,main_window *widgets){
    source_window *window = yon_source_window_new();
    g_object_set_data(G_OBJECT(window->Window),"widgets",widgets);
    gtk_widget_show(window->Window);
}

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 
// /**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.install_thread=0;
    main_config.progress_thread=0;
    main_config.install_complete=0;
    main_config.save_done=0;
    main_config.save_configured=0;
    main_config.configure_mode=0;
    main_config.load_mode=YON_CONFIG_LOCAL;
    main_config.log_progress_buzy=0;
    main_config.log_end=0;
    main_config.exit_accepted=0;
    main_config.config_load_path = NULL;
    main_config.config_save_path = NULL;
    yon_packages_init();
    main_config.network_types = g_hash_table_new(g_str_hash,g_str_equal);
    main_config.cur_slide=0;
}

// void on_root_get_root(char *argline){
//     yon_launch(argline);
// }

// void *on_setup_system_configuration(void *data);

void on_locale_changed(GtkWidget *,main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model;
    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->LanguagesTree)),&model,&iter)) return;
    gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->InstallerCountryFilter));
    char *config_code = config(installer_lang_parameter);
    if (yon_char_is_empty(config_code)){
        config_code = yon_char_new(setlocale(LC_ALL,NULL));
        if (strstr(config_code,".")){
            yon_char_divide_search_self(config_code,".",-1);
        }
    }
    GtkTreeIter itar;
    for_iter(widgets->InstallerCountryFilter,&iter){
        gtk_tree_model_filter_convert_iter_to_child_iter(GTK_TREE_MODEL_FILTER(widgets->InstallerCountryFilter),&itar,&iter);
        char *target;
        gtk_tree_model_get(GTK_TREE_MODEL(widgets->InstallerCountryList),&itar,2,&target,-1);
        if (!strcmp(config_code,target)){
            gtk_tree_model_filter_convert_child_iter_to_iter(GTK_TREE_MODEL_FILTER(widgets->InstallerCountryFilter),&iter,&itar);
            gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->CountryLanguagesTree)),&iter);
            break;
        }
    }
    
}

void on_installer_language_changed(GtkWidget *self, main_window *widgets){
    char *window_language;
    GtkTreeIter iter;
    GtkTreeModel *model;
    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->CountryLanguagesTree)),&model,&iter)) return;
        gtk_tree_model_get(model,&iter,2,&window_language,-1);
    if (!yon_char_is_empty(window_language)){
        yon_config_register(installer_lang_parameter,installer_lang_parameter_command,(char*)window_language);
    }
    char *active_lang_id = gtk_tree_model_get_string_from_iter(model,&iter);
    int size;
    config_str parameters = yon_config_get_save_parameters_by_key(&size,installer_lang_parameter,NULL);
    char *command_parameters = yon_char_parsed_to_string(parameters,size," ");
    char *command = yon_char_unite(ubconfig_dull_command,"--target system ",command_parameters,NULL);
    g_signal_handlers_block_by_func(G_OBJECT(widgets->MainWindow),G_CALLBACK(on_yon_exit),widgets);
    if (!system(command)){};
    gtk_widget_destroy(widgets->MainWindow);
    setlocale(LC_ALL, window_language);
    textdomain(LocaleName);
    g_setenv("LANGUAGE",window_language,1);

    yon_main_window_create(widgets);
    char *path = yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL);
    yon_window_config_setup(GTK_WINDOW(widgets->MainWindow));
    yon_window_config_load(path);
    g_signal_handlers_block_by_func(G_OBJECT(self),G_CALLBACK(on_locale_changed),widgets);
    model = GTK_TREE_MODEL(widgets->InstallerLanguageList);
    gtk_tree_model_get_iter_from_string(model,&iter,active_lang_id);
    g_signal_handlers_unblock_by_func(G_OBJECT(self),G_CALLBACK(on_locale_changed),widgets);

}

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

void on_gparted_open(){
    yon_launch_app_with_arguments(open_gparted_command,NULL);
}


void on_reboot_accepted(main_window *widgets){
    g_mutex_lock(&main_config.install_mutex);
    if (main_config.install_thread){
        pthread_cancel((pthread_t)main_config.install_thread);
    g_mutex_unlock(&main_config.install_mutex);
    }
    main_config.exit_accepted=1;
    g_signal_emit_by_name(G_OBJECT(widgets->MainWindow),"delete-event");
    while(gtk_events_pending()) gtk_main_iteration();
    if (system("reboot")){};
}

void on_exit_accepted(main_window *widgets){
    g_mutex_lock(&main_config.install_mutex);
    if (main_config.install_thread){
        pthread_cancel((pthread_t)main_config.install_thread);
    g_mutex_unlock(&main_config.install_mutex);
    }
    main_config.exit_accepted=1;
    // g_signal_emit_by_name(G_OBJECT(widgets->MainWindow),"delete-event");
    while(gtk_events_pending()) gtk_main_iteration();
    gtk_widget_destroy(widgets->MainWindow);
}

gboolean on_yon_exit(GtkWidget *,GdkEvent*, main_window *widgets){
    if (!main_config.exit_accepted||(main_config.save_done&&main_config.install_complete)){
        if (widgets){};
        dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
        data->action_text=WARNING_TEXT_LABEL;
        data->title=WARNING_TITLE_LABEL;
        if (yon_confirmation_dialog_call(widgets->MainWindow,data)==GTK_RESPONSE_ACCEPT ){
            on_exit_accepted(widgets);
            gtk_main_quit();
            return 1;
        }
    }

    return 1;
}

typedef struct{
    char *locale_name;
    char *code;
    char *localised_name;
    char *code_full;
} locale_struct;

void set_locales_list(main_window *widgets){
    gtk_list_store_clear(widgets->InstallerLanguageList);
    gtk_list_store_clear(widgets->InstallerCountryList);

    GHashTable *localisations = g_hash_table_new(g_str_hash,g_str_equal);
    int locales_size;
    config_str locales = yon_dir_get_contents(locales_list_command,&locales_size);
    dictionary *locale_dict = NULL;
    for (int i=0;i<locales_size;i++){
        if (!strcmp(locales[i],".")) continue;
        if (!strcmp(locales[i],"..")) continue;
        char *path = yon_char_unite(locales_list_command,locales[i],NULL);
        int info_size;
        config_str code_info = yon_file_open(path,&info_size);
        char *code = yon_char_parsed_check_exist_begins_with(code_info,info_size,"lang_ab");
        if (yon_char_is_empty(code)) continue;
        yon_char_remove_last_symbol(code,'\n');
        yon_char_remove_last_symbol(code,'"');
        free(yon_char_divide_search(code,"\"",-1));
        
        {
            char *region = yon_char_parsed_check_exist_begins_with(code_info,info_size,"territory");
            if (!yon_char_is_empty(region)){
                yon_char_remove_last_symbol(region,'\n');
                yon_char_remove_last_symbol(region,'"');
            free(yon_char_divide_search(region,"\"",-1));
            }
            char *lang_name = yon_char_parsed_check_exist_begins_with(code_info,info_size,"language");
            if (!yon_char_is_empty(lang_name)){
                yon_char_remove_last_symbol(lang_name,'\n');
                yon_char_remove_last_symbol(lang_name,'"');
            free(yon_char_divide_search(lang_name,"\"",-1));
            }
            char *tooltip = yon_char_parsed_check_exist_begins_with(code_info,info_size,"title");
            if (!yon_char_is_empty(tooltip)){
                yon_char_remove_last_symbol(tooltip,'\n');
                yon_char_remove_last_symbol(tooltip,'"');
            free(yon_char_divide_search(tooltip,"\"",-1));
            }
            GtkTreeIter iter;
            char *country = yon_char_unite(lang_name," (",region,")",NULL);
            gtk_list_store_append(widgets->InstallerCountryList,&iter);
            gtk_list_store_set(widgets->InstallerCountryList,&iter,0,code,1,_(country),2,locales[i],3,_(tooltip),-1);
        }
        if (!g_hash_table_contains(localisations,code)) {
            locale_struct *locale = malloc(sizeof(locale_struct));
            memset(locale,0,sizeof(locale_struct));

            locale->locale_name = yon_char_parsed_check_exist_begins_with(code_info,info_size,"language");
            locale->code = code;
            locale->localised_name = yon_char_parsed_check_exist_begins_with(code_info,info_size,"lang_name");
            locale->code_full = yon_char_new(locales[i]);

            if (!yon_char_is_empty(locale->locale_name)){
                yon_char_remove_last_symbol(locale->locale_name,'\n');
                yon_char_remove_last_symbol(locale->locale_name,'"');
            }
            if (!yon_char_is_empty(locale->localised_name)){
                yon_char_remove_last_symbol(locale->localised_name,'\n');
                yon_char_remove_last_symbol(locale->localised_name,'"');
            }
            yon_dictionary_add_or_create_if_exists_with_data(locale_dict,locales[i],locale);
            free(yon_char_divide_search(locale->locale_name,"\"",-1));
            free(yon_char_divide_search(locale->localised_name,"\"",-1));
            g_hash_table_add(localisations,code);
        }
        yon_char_parsed_free(code_info,info_size);
    }
    yon_char_parsed_free(locales,locales_size);
    
    GtkTreeIter iter;
    dictionary *dict;
    char *config_val = yon_char_new(config(installer_lang_parameter));
    char *config_code = NULL;
    if (yon_char_is_empty(config_val)) config_val = yon_char_new(setlocale(LC_ALL,NULL));
    if(strstr(config_val,"_")){
        config_code = yon_char_divide_search(config_val,"_",-1);
        free(config_val);

    }
    for_dictionaries(dict,locale_dict){
        locale_struct *locale = yon_dictionary_get_data(dict,locale_struct*);
        gtk_list_store_append(widgets->InstallerLanguageList,&iter);
        gtk_list_store_set(widgets->InstallerLanguageList,&iter,0,locale->code,1,locale->locale_name,2,_(locale->localised_name),3,locale->code_full,-1);
        if (!yon_char_is_empty(config_code)){
            if (!strcmp(config_code,locale->code)){
                g_signal_handlers_block_by_func(G_OBJECT(widgets->CountryLanguagesTree),G_CALLBACK(on_installer_language_changed),widgets);
                gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->LanguagesTree)),&iter);
                on_locale_changed(NULL,widgets);
                g_signal_handlers_unblock_by_func(G_OBJECT(widgets->CountryLanguagesTree),G_CALLBACK(on_installer_language_changed),widgets);
            }
        }
        
    }

}

gboolean on_country_filter(GtkTreeModel *self, GtkTreeIter *iter, main_window *widgets){
    GtkTreeIter itar;
    GtkTreeModel *model;
    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->LanguagesTree)),&model,&itar)) return 0;
    char *cur_code,*country;
    gtk_tree_model_get(self,iter,0,&cur_code,1,&country,-1);
    char *code;

    gtk_tree_model_get(model,&itar,0,&code,-1);
    if (!strcmp(code,cur_code)){
        char *config_lang = config(installer_lang_parameter);
        if (!yon_char_is_empty(config_lang)&&!strcmp(cur_code,config_lang)){
            if (gtk_tree_model_filter_convert_child_iter_to_iter(GTK_TREE_MODEL_FILTER(widgets->InstallerCountryFilter),iter,&itar)){
                gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->CountryLanguagesTree)),&itar);
            }
        }
        return 1;
    }
    return 0;

}

void yon_main_window_create(main_window *widgets){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path); 
    gtk_builder_add_callback_symbol(builder,"yon_gtk_widget_set_sensitive_from_toggle_button",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button));
    gtk_builder_add_callback_symbol(builder,"on_toggle_button_switch_on",G_CALLBACK(on_toggle_button_switch_on));
    // Custom widgets configuration 
    widgets->builder = builder;
    gtk_builder_set_translation_domain(builder,template_ui_LocaleName);

    {
        widgets->DevicesList=GTK_LIST_STORE(gtk_builder_get_object(builder,"DevicesList"));
        widgets->LanguagesList=GTK_LIST_STORE(gtk_builder_get_object(builder,"LanguagesList"));

        widgets->PartitionsList = GTK_LIST_STORE(gtk_builder_get_object(builder,"PartitionsList"));

        main_config.kernel_unchosen_radio = yon_gtk_builder_get_widget(builder,"KernelUnchosenRadio");

        widgets->MainWindow=yon_gtk_builder_get_widget(builder,"MainWindow");
        widgets->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
        widgets->StatusBox2 = yon_gtk_builder_get_widget(builder,"StatusBox2");
        widgets->Notebook = yon_gtk_builder_get_widget(builder,"Notebook");
        widgets->MainSpinner=yon_gtk_builder_get_widget(builder,"MainSpinner");

        widgets->EnableVNCMenuItem = yon_gtk_builder_get_widget(builder,"EnableVNCMenuItem");
        widgets->DocumentationMenuItem = yon_ubl_menu_item_documentation_new(DOCUMENTATION_LABEL);
        widgets->AboutMenuItem = yon_ubl_menu_item_about_new(ABOUT_LABEL);

        widgets->LoadGlobalConfigurationMenuItem = yon_gtk_builder_get_widget(builder,"LoadGlobalConfigurationMenuItem");
        widgets->LoadLocalConfigurationMenuItem = yon_gtk_builder_get_widget(builder,"LoadLocalConfigurationMenuItem");
        widgets->LoadExternalConfigurationMenuItem = yon_gtk_builder_get_widget(builder,"LoadExternalConfigurationMenuItem");

        widgets->SaveGlobalLocalConfigurationMenuItem = yon_gtk_builder_get_widget(builder,"SaveGlobalLocalConfigurationMenuItem");
        widgets->SaveGlobalConfigurationMenuItem = yon_gtk_builder_get_widget(builder,"SaveGlobalConfigurationMenuItem");
        widgets->SaveLocalConfigurationMenuItem = yon_gtk_builder_get_widget(builder,"SaveLocalConfigurationMenuItem");
        widgets->SaveExternalConfigurationMenuItem = yon_gtk_builder_get_widget(builder,"SaveExternalConfigurationMenuItem");

        widgets->SlidesImage = yon_gtk_builder_get_widget(builder,"SlidesImage");

        widgets->LicenceLabel = yon_gtk_builder_get_widget(builder,"LicenceLabel");

        widgets->menu1=yon_gtk_builder_get_widget(builder,"menu1");
        widgets->menu2=yon_gtk_builder_get_widget(builder,"menu2");
        widgets->ConfigurationModeMenuItem = yon_gtk_builder_get_widget(builder,"ConfigurationModeMenuItem");

        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->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->CompletionToggle=yon_gtk_builder_get_widget(builder,"CompletionToggle");
        widgets->InstallationToggle=yon_gtk_builder_get_widget(builder,"InstallationToggle");

        widgets->StartScenarioButton=yon_gtk_builder_get_widget(builder,"StartScenarioButton");

        widgets->SourceButton=yon_gtk_builder_get_widget(builder,"SourceButton");
        widgets->SkipInstallationButton=yon_gtk_builder_get_widget(builder,"SkipInstallationButton");

        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->GrubInstallRadio = yon_gtk_builder_get_widget(builder,"GrubInstallRadio");
        widgets->GrubUpdateRadio = yon_gtk_builder_get_widget(builder,"GrubUpdateRadio");
        widgets->SeparateRadio = yon_gtk_builder_get_widget(builder,"SeparateRadio");
        widgets->OSRadio = yon_gtk_builder_get_widget(builder,"OSRadio");
        widgets->UserDataOnlyRadio = yon_gtk_builder_get_widget(builder,"UserDataOnlyRadio");

        widgets->CommonInstallationDevicesTree = yon_gtk_builder_get_widget(builder,"CommonInstallationDevicesTree");
        widgets->CommonInstallationFilesystemTypeCombo = yon_gtk_builder_get_widget(builder,"CommonInstallationFilesystemTypeCombo");
        widgets->CommonInstallationSectionNameEntry = yon_gtk_builder_get_widget(builder,"CommonInstallationSectionNameEntry");
        widgets->CommonFormatSwitch = yon_gtk_builder_get_widget(builder,"CommonFormatSwitch");
        widgets->GpartedCommonButton = yon_gtk_builder_get_widget(builder,"GpartedCommonButton");

        widgets->SamePlaceDeviceTree = yon_gtk_builder_get_widget(builder,"SamePlaceDeviceTree");
        widgets->SamePlacePartTree = yon_gtk_builder_get_widget(builder,"SamePlacePartTree");
        widgets->SameInstallationFormatSwitch = yon_gtk_builder_get_widget(builder,"SameInstallationFormatSwitch");
        widgets->SameInstallationFormatRevealer = yon_gtk_builder_get_widget(builder,"SameInstallationFormatRevealer");
        widgets->SameInstallationFilesystemTypeCombo = yon_gtk_builder_get_widget(builder,"SameInstallationFilesystemTypeCombo");
        widgets->SameInstallationSectionNameEntry = yon_gtk_builder_get_widget(builder,"SameInstallationSectionNameEntry");
        widgets->GpartedSameButton = yon_gtk_builder_get_widget(builder,"GpartedSameButton");

        widgets->NextInstallationFilesystemTypeCombo = yon_gtk_builder_get_widget(builder,"NextInstallationFilesystemTypeCombo");
        widgets->NextInstallationFormatSwitch = yon_gtk_builder_get_widget(builder,"NextInstallationFormatSwitch");
        widgets->NextInstallationFormatRevealer = yon_gtk_builder_get_widget(builder,"NextInstallationFormatRevealer");
        widgets->NextInstallationSectionNameEntry = yon_gtk_builder_get_widget(builder,"NextInstallationSectionNameEntry");
        widgets->NextInstallationSizeSpin = yon_gtk_builder_get_widget(builder,"NextInstallationFormatSizeSpin");
        widgets->NextInstallationSizeTypeSpin = yon_gtk_builder_get_widget(builder,"NextInstallationFormatSizeCombo");
        widgets->NextInstallationSysDevicesTree = yon_gtk_builder_get_widget(builder,"NextInstallationSysDevicesTree");
        widgets->NextInstallationSysSectionTree = yon_gtk_builder_get_widget(builder,"NextInstallationSysSectionTree");
        widgets->GpartedNextInstallationButton = yon_gtk_builder_get_widget(builder,"GpartedNextInstallationButton");
        
        widgets->AdvancedDeviceChosenCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"AdvancedDeviceChosenCell"));
        widgets->AdvancedPartChosenCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"AdvancedPartChosenCell"));
        widgets->AdvancedDeviceTree = yon_gtk_builder_get_widget(builder,"AdvancedDeviceTree");
        widgets->AdvancedVirtualDeviceCombo = yon_gtk_builder_get_widget(builder,"AdvancedVirtualDeviceCombo");
        widgets->AdvancedPartitionTree = yon_gtk_builder_get_widget(builder,"AdvancedPartitionTree");
        widgets->AdvancedPartitionAddBox = yon_gtk_builder_get_widget(builder,"AdvancedPartitionAddBox");
        widgets->AdvancedAddButton = yon_gtk_builder_get_widget(builder,"AdvancedAddButton");
        widgets->AdvancedLoadTypeSwitch = yon_gtk_builder_get_widget(builder,"AdvancedLoadTypeSwitch");
        widgets->AdvancedBiosSectorSwitch = yon_gtk_builder_get_widget(builder,"AdvancedBiosSectorSwitch");
        widgets->AdvancedEFISwitch = yon_gtk_builder_get_widget(builder,"AdvancedEFISwitch");
        widgets->AdvancedSwapSwitch = yon_gtk_builder_get_widget(builder,"AdvancedSwapSwitch");
        widgets->AdvancedSwapAutoSwitch = yon_gtk_builder_get_widget(builder,"AdvancedSwapAutoSwitch");
        widgets->AdvancedSwapRamSwitch = yon_gtk_builder_get_widget(builder,"AdvancedSwapRamSwitch");
        widgets->AdvancedSwapFixedSwitch = yon_gtk_builder_get_widget(builder,"AdvancedSwapFixedSwitch");
        widgets->AdvancedSwapFixedSizeSwitch = yon_gtk_builder_get_widget(builder,"AdvancedSwapFixedSizeSwitch");
        widgets->AdvancedSwapFixedSpin = yon_gtk_builder_get_widget(builder,"AdvancedSwapFixedSpin");
        widgets->AdvancedSwapRevealer = yon_gtk_builder_get_widget(builder,"AdvancedSwapRevealer");
        widgets->AdvancedLoadTypeRevealer = yon_gtk_builder_get_widget(builder,"AdvancedLoadTypeRevealer");
        widgets->advanced_partition_order = g_sequence_new(NULL);

        widgets->GrubInstallDevicesTree = yon_gtk_builder_get_widget(builder,"GrubInstallDevicesTree");
        widgets->GrubInstallPartitionTree = yon_gtk_builder_get_widget(builder,"GrubInstallPartitionTree");
        widgets->GpartedGrubInstallButton = yon_gtk_builder_get_widget(builder,"GpartedGrubInstallButton");

        widgets->GrubUpdateDevicesTree = yon_gtk_builder_get_widget(builder,"GrubUpdateDevicesTree");
        widgets->GrubUpdatePartitionTree = yon_gtk_builder_get_widget(builder,"GrubUpdatePartitionTree");
        widgets->GpartedGrubUpdateButton = yon_gtk_builder_get_widget(builder,"GpartedGrubUpdateButton");

        widgets->OSDevicesTree = yon_gtk_builder_get_widget(builder,"OSDevicesTree");
        widgets->OSSysSectionTree = yon_gtk_builder_get_widget(builder,"OSSysSectionTree");
        widgets->GpartedOSButton = yon_gtk_builder_get_widget(builder,"GpartedOSButton");
        widgets->OSFormatSwitch = yon_gtk_builder_get_widget(builder,"OSFormatSwitch");
        widgets->OSFormatSizeSpin = yon_gtk_builder_get_widget(builder,"OSFormatSizeSpin");
        widgets->OSFormatSizeCombo = yon_gtk_builder_get_widget(builder,"OSFormatSizeCombo");
        widgets->OSFormatPartitionEntry = yon_gtk_builder_get_widget(builder,"OSFormatPartitionEntry");
        widgets->OSFilesystemTypeCombo = yon_gtk_builder_get_widget(builder,"OSFilesystemTypeCombo");
        widgets->OSFormatFSMarkEntry = yon_gtk_builder_get_widget(builder,"OSFormatFSMarkEntry");
        widgets->OSFormatEncryptionCombo = yon_gtk_builder_get_widget(builder,"OSFormatEncryptionCombo");
        widgets->OSFormatEncryptionEntry = yon_gtk_builder_get_widget(builder,"OSFormatEncryptionEntry");

        widgets->UserdataDevicesTree = yon_gtk_builder_get_widget(builder,"UserdataDevicesTree");
        widgets->UserdataSysSectionTree = yon_gtk_builder_get_widget(builder,"UserdataSysSectionTree");
        widgets->GpartedUserdataButton = yon_gtk_builder_get_widget(builder,"UserdataGpartedButton");
        widgets->UserdataFormatSwitch = yon_gtk_builder_get_widget(builder,"UserdataFormatSwitch");
        widgets->UserdataFormatSizeSpin = yon_gtk_builder_get_widget(builder,"UserdataFormatSizeSpin");
        widgets->UserdataFormatSizeCombo = yon_gtk_builder_get_widget(builder,"UserdataFormatSizeCombo");
        widgets->UserdataFormatPartitionEntry = yon_gtk_builder_get_widget(builder,"UserdataFormatPartitionEntry");
        widgets->UserdataFilesystemTypeCombo = yon_gtk_builder_get_widget(builder,"UserdataFilesystemTypeCombo");
        widgets->UserdataFormatFSMarkEntry = yon_gtk_builder_get_widget(builder,"UserdataFormatFSMarkEntry");
        widgets->UserdataFormatEncryptionCombo = yon_gtk_builder_get_widget(builder,"UserdataFormatEncryptionCombo");
        widgets->UserdataFormatEncryptionEntry = yon_gtk_builder_get_widget(builder,"UserdataFormatEncryptionEntry");

        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->LanguagesTree=yon_gtk_builder_get_widget(builder,"LanguagesTree");
        widgets->CountryLanguagesTree=yon_gtk_builder_get_widget(builder,"CountryLanguagesTree");
        widgets->InstallerCountryList=GTK_LIST_STORE(gtk_builder_get_object(builder,"InstallerCountryList"));
        widgets->InstallerCountryFilter=GTK_TREE_MODEL(gtk_builder_get_object(builder,"InstallerCountryFilter"));
        widgets->InstallerLanguageList=GTK_LIST_STORE(gtk_builder_get_object(builder,"InstallerLanguageList"));
        // 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->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->RegionSensitiveSwitch = yon_gtk_builder_get_widget(builder,"RegionSensitiveSwitch");

        widgets->UserImage=yon_gtk_builder_get_widget(builder,"UserImage");
        widgets->UserRootNameEntry=yon_gtk_builder_get_widget(builder,"UserRootNameEntry");
        widgets->UserRootLoginEntry=yon_gtk_builder_get_widget(builder,"UserRootLoginEntry");
        widgets->UserRootPasswordCombo=yon_gtk_builder_get_widget(builder,"UserRootPasswordCombo");
        widgets->UserRootPasswordEntry=yon_gtk_builder_get_widget(builder,"UserRootPasswordEntry");
        widgets->UserRootPasswordButton=yon_gtk_builder_get_widget(builder,"UserRootPasswordButton");
        widgets->UserAutologinSwitch=yon_gtk_builder_get_widget(builder,"UserAutologinSwitch");
        widgets->UserAddBox=yon_gtk_builder_get_widget(builder,"UserAddBox");
        widgets->UserAddButton=yon_gtk_builder_get_widget(builder,"UserAddButton");

        widgets->HotnameEntry=yon_gtk_builder_get_widget(builder,"HotnameEntry");
        widgets->mainSettingsButton=yon_gtk_builder_get_widget(builder,"mainSettingsButton");
        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->KeyboardLayoutChosenCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"KeyboardLayoutChosenCell"));

        widgets->AdditionalSoftwareList = GTK_LIST_STORE(gtk_builder_get_object(builder,"AdditionalSoftwareList"));
        widgets->AdditionalSoftwareTree = yon_gtk_builder_get_widget(builder,"AdditionalSoftwareTree");
        widgets->AdditionalSoftwareCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"AdditionalSoftwareCell"));


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

        widgets->KernelListBox = yon_gtk_builder_get_widget(builder,"KernelListBox");
        widgets->KernelInstallLabel = yon_gtk_builder_get_widget(builder,"KernelInstallLabel");
        widgets->KernelEnableLabel = yon_gtk_builder_get_widget(builder,"KernelEnableLabel");
        widgets->KernelNameLabel = yon_gtk_builder_get_widget(builder,"KernelNameLabel");
        widgets->KernelTagsLabel = yon_gtk_builder_get_widget(builder,"KernelTagsLabel");
        widgets->KernelModulesLabel = yon_gtk_builder_get_widget(builder,"KernelModulesLabel");
        widgets->KernelDescriptionLabel = yon_gtk_builder_get_widget(builder,"KernelDescriptionLabel");
        widgets->KernelSizeGroup = GTK_SIZE_GROUP(gtk_builder_get_object(builder,"TagsSizeGroup"));

        widgets->KernelAddonListBox = yon_gtk_builder_get_widget(builder,"KernelAddonListBox");
        widgets->KernelAddonInstallLabel = yon_gtk_builder_get_widget(builder,"KernelAddonInstallLabel");
        widgets->KernelAddonNameLabel = yon_gtk_builder_get_widget(builder,"KernelAddonNameLabel");
        widgets->KernelAddonModulesLabel = yon_gtk_builder_get_widget(builder,"KernelAddonModulesLabel");
        widgets->KernelAddonDescriptionLabel = yon_gtk_builder_get_widget(builder,"KernelAddonDescriptionLabel");

        widgets->OSSoftwareListBox = yon_gtk_builder_get_widget(builder,"OSSoftwareListBox");
        widgets->OSSoftwareInstallLabel = yon_gtk_builder_get_widget(builder,"OSSoftwareInstallLabel");
        widgets->OSSoftwareNameLabel = yon_gtk_builder_get_widget(builder,"OSSoftwareNameLabel");
        widgets->OSSoftwareTagsLabel = yon_gtk_builder_get_widget(builder,"OSSoftwareTagsLabel");
        widgets->OSSoftwareDescriptionLabel = yon_gtk_builder_get_widget(builder,"OSSoftwareDescriptionLabel");

        widgets->PacmanSoftwareSearchEntry = yon_gtk_builder_get_widget(builder,"PacmanSoftwareSearchEntry");
        widgets->PacmanSoftwareStatusImage = yon_gtk_builder_get_widget(builder,"PacmanSoftwareStatusImage");
        widgets->PacmanSoftwareStatusLabel = yon_gtk_builder_get_widget(builder,"PacmanSoftwareStatusLabel");
        widgets->PacmanSoftwareStatusButton = yon_gtk_builder_get_widget(builder,"PacmanSoftwareStatusButton");

        widgets->PacmanLoadingOverlay = yon_gtk_builder_get_widget(builder,"PacmanLoadingOverlay");
        widgets->PacmanSoftwareAllPackagesTree = yon_gtk_builder_get_widget(builder,"PacmanSoftwareAllPackagesTree");
        widgets->PacmanSoftwareAllList = GTK_LIST_STORE(gtk_builder_get_object(builder,"PacmanSoftwareAllList"));
        widgets->PacmanSoftwareAllCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"PacmanSoftwareAllCell"));

        widgets->PacmanSoftwareChosenPackagesTree = yon_gtk_builder_get_widget(builder,"PacmanSoftwareChosenPackagesTree");
        widgets->PacmanSoftwareChosenList = GTK_LIST_STORE(gtk_builder_get_object(builder,"PacmanSoftwareChosenList"));
        widgets->PacmanSoftwareChosenCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"PacmanSoftwareChosenCell"));

        widgets->StartupImage = yon_gtk_builder_get_widget(builder,"StartupImage");
        widgets->StartupServicesTree = yon_gtk_builder_get_widget(builder,"StartupServicesTree");
        widgets->StartupServicesAddButton = yon_gtk_builder_get_widget(builder,"StartupServicesAddButton");
        widgets->StartupServicesEditButton = yon_gtk_builder_get_widget(builder,"StartupServicesEditButton");
        widgets->StartupServicesRemoveButton = yon_gtk_builder_get_widget(builder,"StartupServicesRemoveButton");
        widgets->StartupChosenCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"StartupChosenCell"));
        widgets->StartupList = GTK_LIST_STORE(gtk_builder_get_object(builder,"StartupList"));
        
        widgets->BootloaderImage = yon_gtk_builder_get_widget(builder,"BootloaderImage");
        widgets->BootloadTimerSwitch = yon_gtk_builder_get_widget(builder,"BootloadTimerSwitch");
        widgets->BootloadTimerSpin = yon_gtk_builder_get_widget(builder,"BootloadTimerSpin");
        widgets->BootloadDefaultOSEntry = yon_gtk_builder_get_widget(builder,"BootloadDefaultOSEntry");
        widgets->BootloadDefaulOSButton = yon_gtk_builder_get_widget(builder,"BootloadDefaulOSButton");
        widgets->BootloadNoPasswordSwitch = yon_gtk_builder_get_widget(builder,"BootloadNoPasswordSwitch");
        widgets->BootloadUserTree = yon_gtk_builder_get_widget(builder,"BootloadUserTree");
        widgets->BootloadUserAddButton = yon_gtk_builder_get_widget(builder,"BootloadUserAddButton");
        widgets->BootloadUserRemoveButton = yon_gtk_builder_get_widget(builder,"BootloadUserRemoveButton");
        widgets->BootloadUsersList = GTK_LIST_STORE(gtk_builder_get_object(builder,"BootloadUsersList"));

        widgets->NetworkImage = yon_gtk_builder_get_widget(builder,"NetworkImage");
        widgets->NetworkDomainSwitch = yon_gtk_builder_get_widget(builder,"NetworkDomainSwitch");
        widgets->NetworkDomainNameEntry = yon_gtk_builder_get_widget(builder,"NetworkDomainNameEntry");
        widgets->NetworkDomainAdminEntry = yon_gtk_builder_get_widget(builder,"NetworkDomainAdminEntry");
        widgets->NetworkDomainPasswordEntry = yon_gtk_builder_get_widget(builder,"NetworkDomainPasswordEntry");
        widgets->NetworkNTPServerSwitch = yon_gtk_builder_get_widget(builder,"NetworkNTPServerSwitch");
        widgets->NetworkNTPCombo = yon_gtk_builder_get_widget(builder,"NetworkNTPCombo");
        widgets->NetworkNTPEntry = yon_gtk_builder_get_widget(builder,"NetworkNTPEntry");
        widgets->NetworkConnectionsBox = yon_gtk_builder_get_widget(builder,"NetworkConnectionsBox");
        widgets->NetworkConnectionsAddButton = yon_gtk_builder_get_widget(builder,"NetworkConnectionsAddButton");
        widgets->HostnameSensitiveSwitch = yon_gtk_builder_get_widget(builder,"HostnameSensitiveSwitch");
        widgets->AutoHostnameCheck=yon_gtk_builder_get_widget(builder,"AutoHostnameCheck");
        widgets->network_connections = NULL;
        widgets->pacmanchosen = g_hash_table_new_full(g_str_hash,g_str_equal,free,NULL);

        main_config.status_box = widgets->StatusBox;
    }
    g_signal_connect(G_OBJECT(widgets->MainWindow),"delete-event",G_CALLBACK(on_yon_exit),widgets);
    GtkWidget *menu = yon_gtk_builder_get_widget(builder,"menu2");
    gtk_style_context_add_class(gtk_widget_get_style_context(widgets->DocumentationMenuItem),"menuitemmiddle");
    gtk_style_context_remove_class(gtk_widget_get_style_context(widgets->DocumentationMenuItem),"menuitemtop");
    gtk_menu_shell_append(GTK_MENU_SHELL(menu),widgets->DocumentationMenuItem);
    gtk_menu_shell_append(GTK_MENU_SHELL(menu),widgets->AboutMenuItem);
    
    g_signal_connect(G_OBJECT(widgets->LoadGlobalConfigurationMenuItem),"activate",G_CALLBACK(on_config_global_load),widgets);
    g_signal_connect(G_OBJECT(widgets->LoadLocalConfigurationMenuItem),"activate",G_CALLBACK(on_config_local_load),widgets);
    g_signal_connect(G_OBJECT(widgets->LoadExternalConfigurationMenuItem),"activate",G_CALLBACK(on_config_custom_load),widgets);

    g_signal_connect(G_OBJECT(widgets->EnableVNCMenuItem),"activate",G_CALLBACK(on_vnc_toggled),widgets);

    g_signal_connect(G_OBJECT(widgets->ReadFullLogButton),"clicked",G_CALLBACK(on_process_log_view),widgets);
    g_signal_connect(G_OBJECT(widgets->ReadShortLogButton),"clicked",G_CALLBACK(on_summary_log_view),widgets);

    g_signal_connect(G_OBJECT(widgets->GpartedCommonButton),"clicked",G_CALLBACK(on_gparted_open),NULL);
    g_signal_connect(G_OBJECT(widgets->GpartedSameButton),"clicked",G_CALLBACK(on_gparted_open),NULL);
    g_signal_connect(G_OBJECT(widgets->GpartedNextInstallationButton),"clicked",G_CALLBACK(on_gparted_open),NULL);

    g_signal_connect(G_OBJECT(widgets->MainWindow),"check-resize",G_CALLBACK(on_region_resized),widgets);
    g_signal_connect(G_OBJECT(widgets->NextButton),"clicked",G_CALLBACK(on_page_next_clicked),widgets);
    g_signal_connect(G_OBJECT(widgets->BackButton),"clicked",G_CALLBACK(on_page_prev_clicked),widgets);
    g_signal_connect(G_OBJECT(widgets->CancelInstallButton),"clicked",G_CALLBACK(on_page_cancel_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->RegionSensitiveSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch),widgets->RegionCombo);
    g_signal_connect(G_OBJECT(widgets->RegionSensitiveSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch),widgets->ZoneCombo);
    g_signal_connect(G_OBJECT(widgets->AddButton),"clicked",G_CALLBACK(on_keyboard_clicked),widgets);
    g_signal_connect(G_OBJECT(widgets->RemoveButton),"clicked",G_CALLBACK(on_keyboard_removed),widgets);

    g_signal_connect(G_OBJECT(widgets->GrubInstallDevicesTree),"cursor-changed",G_CALLBACK(on_device_selection_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->GrubUpdateDevicesTree),"cursor-changed",G_CALLBACK(on_device_selection_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->NextInstallationSysDevicesTree),"cursor-changed",G_CALLBACK(on_device_selection_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->SamePlaceDeviceTree),"cursor-changed",G_CALLBACK(on_device_selection_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->UserdataDevicesTree),"cursor-changed",G_CALLBACK(on_device_selection_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->OSDevicesTree),"cursor-changed",G_CALLBACK(on_device_selection_changed),widgets);

    g_signal_connect(G_OBJECT(widgets->ConfigurationModeMenuItem),"toggled",G_CALLBACK(on_configuration_mode_switch),widgets);

    g_signal_connect(G_OBJECT(widgets->SamePlacePartTree),"cursor-changed",G_CALLBACK(on_partition_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->NextInstallationSysSectionTree),"cursor-changed",G_CALLBACK(on_partition_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->NextInstallationSizeTypeSpin),"changed",G_CALLBACK(on_partition_changed),widgets);

    g_signal_connect(G_OBJECT(widgets->UserRootPasswordCombo),"changed",G_CALLBACK(yon_gtk_widget_set_sensitive_from_combo_box),widgets->UserRootPasswordEntry);
    g_signal_connect(G_OBJECT(widgets->UserRootPasswordCombo),"changed",G_CALLBACK(yon_gtk_widget_set_sensitive_from_combo_box),widgets->UserRootPasswordButton);
    g_signal_connect(G_OBJECT(widgets->UserAddButton),"clicked",G_CALLBACK(on_user_add),widgets);
    g_signal_connect(G_OBJECT(widgets->UserRootPasswordButton),"clicked",G_CALLBACK(yon_password_root_new),widgets);
    g_signal_connect(G_OBJECT(widgets->BootloadDefaulOSButton),"clicked",G_CALLBACK(yon_menu_window_open),widgets);
    gtk_tree_model_filter_set_visible_column(GTK_TREE_MODEL_FILTER(widgets->LayoutsFilter),3);

    g_signal_connect(G_OBJECT(widgets->LanguagesTree),"cursor-changed",G_CALLBACK(on_locale_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->CountryLanguagesTree),"cursor-changed",G_CALLBACK(on_installer_language_changed),widgets);
    // 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->StartupChosenCell),"toggled",G_CALLBACK(on_srartup_services_toggled),widgets);
    
    g_signal_connect(G_OBJECT(widgets->StartupServicesTree),"cursor-changed",G_CALLBACK(on_startup_services_selection_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->StartupServicesAddButton),"clicked",G_CALLBACK(on_startup_service_add),widgets);
    g_signal_connect(G_OBJECT(widgets->StartupServicesEditButton),"clicked",G_CALLBACK(on_startup_service_edit),widgets);
    g_signal_connect(G_OBJECT(widgets->StartupServicesRemoveButton),"clicked",G_CALLBACK(on_startup_services_remove),widgets);

    
    g_signal_connect(G_OBJECT(widgets->AutoHostnameCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),widgets->HotnameEntry);

    yon_gtk_revealer_set_from_switch(GTK_REVEALER(widgets->NextInstallationFormatRevealer),GTK_SWITCH(widgets->NextInstallationFormatSwitch));
    yon_gtk_revealer_set_from_switch(GTK_REVEALER(widgets->SameInstallationFormatRevealer),GTK_SWITCH(widgets->SameInstallationFormatSwitch));
    
    g_signal_connect(G_OBJECT(widgets->PacmanSoftwareAllCell),"toggled",G_CALLBACK(on_pacman_software_all_toggled),widgets);
    g_signal_connect(G_OBJECT(widgets->PacmanSoftwareChosenCell),"toggled",G_CALLBACK(on_pacman_software_chosen_toggled),widgets);
    g_signal_connect(G_OBJECT(widgets->PacmanSoftwareSearchEntry),"icon-press",G_CALLBACK(on_pacman_icon_press),widgets);
    g_signal_connect(G_OBJECT(widgets->BootloadUserAddButton),"clicked",G_CALLBACK(on_bootloader_user_add),widgets);

    g_signal_connect(G_OBJECT(widgets->BootloadTimerSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch),widgets->BootloadTimerSpin);
    g_signal_connect(G_OBJECT(widgets->BootloadNoPasswordSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),widgets->BootloadUserRemoveButton);
    g_signal_connect(G_OBJECT(widgets->BootloadNoPasswordSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),widgets->BootloadUserAddButton);
    g_signal_connect(G_OBJECT(widgets->BootloadNoPasswordSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),widgets->BootloadUserTree);

    g_signal_connect(G_OBJECT(widgets->NetworkDomainSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch),widgets->NetworkDomainNameEntry);
    g_signal_connect(G_OBJECT(widgets->NetworkDomainSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch),widgets->NetworkDomainAdminEntry);
    g_signal_connect(G_OBJECT(widgets->NetworkDomainSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch),widgets->NetworkDomainPasswordEntry);
    g_signal_connect(G_OBJECT(widgets->NetworkNTPServerSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch),widgets->NetworkNTPCombo);
    g_signal_connect(G_OBJECT(widgets->NetworkNTPServerSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch),widgets->NetworkNTPEntry);
    g_signal_connect(G_OBJECT(widgets->HostnameSensitiveSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch),widgets->HotnameEntry);
    g_signal_connect(G_OBJECT(widgets->HostnameSensitiveSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch),widgets->AutoHostnameCheck);
    g_signal_connect(G_OBJECT(widgets->HostnameSensitiveSwitch),"state-set",G_CALLBACK(on_autohostname_sensitiveness_check),widgets);
    g_signal_connect(G_OBJECT(widgets->AutoHostnameCheck),"toggled",G_CALLBACK(on_autohostname_check),widgets);
    g_signal_connect(G_OBJECT(widgets->HotnameEntry),"changed",G_CALLBACK(on_hostname_entry_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->NetworkConnectionsAddButton),"clicked",G_CALLBACK(on_connection_add),widgets);
    g_signal_connect(G_OBJECT(widgets->NetworkNTPCombo),"changed",G_CALLBACK(on_ntp_sync),widgets);
    yon_gtk_revealer_set_from_switch(GTK_REVEALER(widgets->AdvancedLoadTypeRevealer),GTK_SWITCH(widgets->AdvancedLoadTypeSwitch));
    yon_gtk_revealer_set_from_switch(GTK_REVEALER(widgets->AdvancedSwapRevealer),GTK_SWITCH(widgets->AdvancedSwapSwitch));

    g_signal_connect(G_OBJECT(widgets->AdvancedSwapFixedSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch),widgets->AdvancedSwapFixedSpin);
    g_signal_connect(G_OBJECT(widgets->AdvancedSwapFixedSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch),widgets->AdvancedSwapFixedSizeSwitch);

    g_signal_connect(G_OBJECT(widgets->AdvancedAddButton),"clicked",G_CALLBACK(on_install_advanced_add_new),widgets);
    g_signal_connect(G_OBJECT(widgets->AdvancedDeviceChosenCell),"toggled",G_CALLBACK(on_install_advanced_device_chosen),widgets);
    g_signal_connect(G_OBJECT(widgets->AdvancedPartChosenCell),"toggled",G_CALLBACK(on_install_advanced_partition_chosen),widgets);
    g_signal_connect(G_OBJECT(widgets->KeyboardLayoutChosenCell),"toggled",G_CALLBACK(on_keyboard_layout_chosen),widgets);
    // g_signal_connect(G_OBJECT(widgets->AdvancedPartitionAddBox),"add",G_CALLBACK(on_advanced_parts_added),widgets);
    // g_signal_connect(G_OBJECT(widgets->AdvancedPartitionAddBox),"remove",G_CALLBACK(on_advanced_parts_removed),widgets);
    g_signal_connect(G_OBJECT(widgets->StartScenarioButton),"clicked",G_CALLBACK(yon_quick_install),widgets);
    g_signal_connect(G_OBJECT(widgets->SourceButton),"clicked",G_CALLBACK(on_source_clicked),widgets);
    g_signal_connect(G_OBJECT(widgets->SkipInstallationButton),"clicked",G_CALLBACK(on_system_setup_pass),widgets);

    gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(widgets->InstallerCountryFilter),(GtkTreeModelFilterVisibleFunc)on_country_filter,widgets,NULL);
    {

        if (main_config.lock_load_global == 1){
            gtk_widget_set_sensitive(widgets->LoadGlobalConfigurationMenuItem,0);
        }
        if (main_config.lock_save_global == 1){
            gtk_widget_set_sensitive(widgets->SaveGlobalConfigurationMenuItem,0);
            gtk_widget_set_sensitive(widgets->SaveGlobalLocalConfigurationMenuItem,0);
        }
        if (main_config.lock_save_local == 1){
            gtk_widget_set_sensitive(widgets->SaveLocalConfigurationMenuItem,0);
            gtk_widget_set_sensitive(widgets->SaveGlobalLocalConfigurationMenuItem,0);
        }
        if (getuid()){
            gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widgets->ConfigurationModeMenuItem),1);
        }


        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 = NULL;
        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);
        }
    }
    {
        int size;
        config_str slides = yon_char_parsed_new(&size,slide_repeat_path);
        widgets->region_original = gdk_pixbuf_new_from_resource(regions_path,NULL);
        widgets->keyboard_original = gdk_pixbuf_new_from_resource(keyboard_path,NULL);
        widgets->user_original = gdk_pixbuf_new_from_resource(users_path,NULL);
        widgets->startup_original = gdk_pixbuf_new_from_resource(startup_path,NULL);
        widgets->bootloader_original = gdk_pixbuf_new_from_resource(bootloader_path,NULL);
        widgets->network_original = gdk_pixbuf_new_from_resource(network_icon_path,NULL);
        widgets->slides_original = NULL;
        widgets->slides_original = g_list_prepend(widgets->slides_original,gdk_pixbuf_new_from_resource(slide_0_path,NULL));
        for (int i=1;i<size;i++){
            widgets->slides_original = g_list_prepend(widgets->slides_original,gdk_pixbuf_new_from_resource(slides[i-1],NULL));
        }
        widgets->slides_original = g_list_reverse(widgets->slides_original);
        yon_char_parsed_free(slides,size);
    }
    gtk_builder_connect_signals(builder,NULL);
    // yon_load_proceed(YON_CONFIG_LOCAL);
    // yon_interface_update(widgets);
    if (!yon_char_is_empty(config(AUTOINSTALL_TYPE_INSTALL))){
        gtk_widget_show(widgets->StartScenarioButton);
    } else {
        gtk_widget_hide(widgets->StartScenarioButton);
    }
    // char *locale = config(installer_lang_parameter);
    // if (!yon_char_is_empty(locale)){
    //     locale = setlocale(LC_ALL,NULL);
    // } 
    // yon_ubl_window_init(TITLE_LABEL,DESCRIPTION_LABEL,locale,CssPath,LocaleName,version_application,WIKI_LINK);
    set_locales_list(widgets);
    // {
    //     char *parameter = config(installer_lang_parameter);
    //     if (yon_char_is_empty(parameter)){
    //         parameter=setlocale(LC_ALL,NULL);
    //     }
    //     GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->LanguagesBox));
    //     GList *iter;
    //     for (iter = list;iter;iter=iter->next){
    //         char *lang = g_object_get_data(G_OBJECT(iter->data),"language");
    //         if (!strcmp(lang,parameter)){
    //             gtk_flow_box_select_child(GTK_FLOW_BOX(widgets->LanguagesBox),GTK_FLOW_BOX_CHILD(iter->data));
    //         }
    //     }
    // }
}

/**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)); 
    yon_main_window_create(widgets);
    return widgets;
}

int main(int argc, char *argv[]){ 
    setlocale(LC_ALL, "");
    textdomain (LocaleName);
    config_init();
	config_str unfound = NULL;
	int size=0;
    yon_ubl_connect_config((_template_config*)&main_config); 
	yon_ubl_setup_arguments(argc,argv,&unfound,&size,NULL);
    gtk_init(&argc,&argv);
    main_window *widgets = NULL;
    widgets = yon_main_window_complete();
    on_config_global_load(NULL,widgets);
    if (widgets){}; 
    char *path = yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL);
    yon_window_config_setup(GTK_WINDOW(widgets->MainWindow));
    yon_window_config_load(path);
    main_config.launch_arguments=yon_char_parsed_copy(argv,argc);
    main_config.launch_size=argc;
    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);
    if (getuid()!=0){
        yon_ubl_status_box_render(yon_char_get_localised_from_lib(ROOT_WARNING_LABEL),BACKGROUND_IMAGE_FAIL_TYPE);
    }
    gtk_main();
    return 0;
}