#include "ubl-settings-environment.h"

config main_config;

//signal emmit handlers - no header initialization

//functions



void on_save_done(main_window *, config_str output, int size){
	char *final_output = yon_char_parsed_to_string(output,size,"");
	if (final_output){
		yon_debug_output("%s\n",final_output);
		free(final_output);
	}
	yon_char_parsed_free(output,size);
	// yon_interface_update(widgets);

	textdomain(template_ui_LocaleName);
	switch (main_config.save_config){
		case 0:
			yon_ubl_status_box_render(GLOBAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
		case 1:
			yon_ubl_status_box_render(LOCAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
		case 2:
			yon_ubl_status_box_render(GLOBAL_LOCAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
		case 3:
			yon_ubl_status_box_render(SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			
	}
	textdomain(LocaleName);
	
}

void yon_load_proceed(YON_CONFIG_TYPE type, main_window *widgets){
    gtk_list_store_clear(widgets->list);
	if (type!=YON_CONFIG_CUSTOM){
		yon_config_clean();
	}
    switch (type){
        case YON_CONFIG_GLOBAL:
            yon_config_load_config(type,config_get_command("global"),NULL);
        yon_ubl_status_box_render(yon_char_get_localised_from_lib(GLOBAL_LOAD_SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
            break;
        case YON_CONFIG_LOCAL:
            yon_config_load_config(type,config_get_command("system"),NULL);
        yon_ubl_status_box_render(yon_char_get_localised_from_lib(LOCAL_LOAD_SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
            break;
        case YON_CONFIG_CUSTOM:
            char *path = NULL;
            path=yon_custom_config_init(GTK_FILE_CHOOSER_ACTION_OPEN);
            if (!yon_char_is_empty(path)){
                yon_config_clean();
                yon_config_load_config(type,config_get_command(path),NULL);
                yon_ubl_status_box_render(yon_char_get_localised_from_lib(LOCAL_LOAD_SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
            }
            break;

    }
    {
        int size;
        GtkTreeIter iter;
        config_str envs = yon_config_get_all(&size);
        if (envs){
            for (int i=0;i<size;i++){
                char *env_type = yon_char_divide_search(envs[i],"=",-1);
                char *env_name = yon_char_divide_search(env_type,":",-1);
                yon_char_divide_search(env_name,"[",-1);
                env_type = yon_char_divide_search(env_type,"]",-1);
                char *desc=NULL;
                if (!yon_ubl_check_root()){
                    if (!strcmp(env_name,"system")||!strcmp(env_name,"profile")||!strcmp(env_name,yon_ubl_root_user_get())){
                        yon_window_config_get_parameter("description",env_type,&desc,YON_TYPE_STRING);
                        gtk_list_store_append(widgets->list,&iter);
                        int source = !strcmp(env_name,"system")? 0 : !strcmp(env_name,"profile") ? 1 : 2;
                        gtk_list_store_set(widgets->list,&iter,0,env_type,1,envs[i],2,_(env_name),3,desc ? desc : "",4,source,-1);
                    }
                }
                else {
                        yon_window_config_get_parameter("description",env_type,&desc,YON_TYPE_STRING);
                        gtk_list_store_append(widgets->list,&iter);
                        int source = !strcmp(env_name,"system")? 0 : !strcmp(env_name,"profile") ? 1 : 2;
                        gtk_list_store_set(widgets->list,&iter,0,env_type,1,envs[i],2,_(env_name),3,desc ? desc : "",4,source,-1);
                }
                free(env_type);
                free(env_name);
                free(envs[i]);
            }
        }
    }
    
    
}

void yon_config_global_load(GtkWidget *self, main_window *widgets){
    yon_load_proceed(YON_CONFIG_GLOBAL,widgets);
    main_config.load_mode=YON_CONFIG_GLOBAL;
}

void yon_config_local_load(GtkWidget *self, main_window *widgets){
    yon_load_proceed(YON_CONFIG_LOCAL,widgets);
    main_config.load_mode=YON_CONFIG_LOCAL;

}

void on_config_custom_load(GtkWidget *self,main_window *widgets){
	yon_load_proceed(YON_CONFIG_CUSTOM,widgets);
    main_config.load_mode=YON_CONFIG_CUSTOM;
}

void yon_config_save_global_local(){
    yon_save_proceed(NULL,YON_CONFIG_BOTH,config_get_command("global"),NULL);
    main_config.save_config = 2;
}

void yon_config_save_local(){
    yon_save_proceed("system",YON_CONFIG_LOCAL,config_get_command("system"),NULL);
    main_config.save_config = 1;
}

void yon_config_save_global(){
    yon_save_proceed("global",YON_CONFIG_GLOBAL,config_get_command("global"),NULL);
    main_config.save_config = 0;
}

void on_config_custom_save(GtkWidget *self, main_window *widgets){
	template_saving_window *window = yon_save_proceed(NULL,YON_CONFIG_CUSTOM,config_get_command("system"),NULL);
    main_config.save_config = 3;
}

// standard functions
void on_edit_accept(GtkWidget *self, editor_window *window){
    
    GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(window->EditingEntry));
    GtkTextIter iter, iterend;
    gtk_text_buffer_get_bounds(buffer,&iter,&iterend);
    char *text = (char*)gtk_text_buffer_get_text(buffer,&iter, &iterend,0);
    FILE *file = fopen(window->path,"w");
    fputs(text,file);
    fclose(file);
    on_subwindow_close(self);
}

void on_edit_destroy(GtkWidget *,GtkWidget *target){
    gtk_widget_set_sensitive(target,1);

}

void on_edit_open(GtkWidget *self, main_window *widgets){
    editor_window *window = malloc(sizeof(editor_window));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_editor);
    window->Window=yon_gtk_builder_get_widget(builder,"Window");
    window->CancelButton=yon_gtk_builder_get_widget(builder,"CancelButton");
    window->AcceptButton=yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->EditingEntry=yon_gtk_builder_get_widget(builder,"EditingEntry");
    window->HeaderLabel=yon_gtk_builder_get_widget(builder,"headerTopic");
    window->path = yon_char_new((char*)gtk_menu_item_get_label(GTK_MENU_ITEM(self)));
    char *topic = yon_char_unite(EDIT_ENV_LABEL," - ", window->path,NULL);
    gtk_label_set_text(GTK_LABEL(window->HeaderLabel),topic);
    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),topic,icon_path,"EnvWindow");
    if (window->path[0]=='~'){
        free(yon_char_divide(window->path,0));
        char *path = yon_char_unite(yon_ubl_user_get_home_directory(),window->path,NULL);
        free(window->path);
        window->path=path;
    } else if (window->path[0]=='/'&&getuid()!=0){
        gtk_widget_set_sensitive(window->AcceptButton,0);
    } 
    FILE *file = fopen(yon_debug_output("Path opens: %s\n",window->path),"r");
    if (file){
        char *final_text = "";
        char text[100];
        while(fgets(text,100,file)){
            final_text = yon_char_append(final_text,text);
        }
        gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(window->EditingEntry)),final_text,strlen(final_text));
        fclose(file);
    }
    g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_edit_accept),window);
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->Window),"destroy",G_CALLBACK(on_edit_destroy),self);


    gtk_widget_set_sensitive(self,0);
    gtk_widget_show(window->Window);

}

void on_variable_save(GtkWidget *self,dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    add_window *window = yon_dictionary_get_data(dict->first->next,add_window*);
    GtkTreeIter iter,search_iter;
    GtkTextIter textiter,*textiterend;
    GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(window->DescriptionEntry));
    gtk_text_buffer_get_iter_at_line_offset(buffer,&textiter,0,0);
    textiterend = gtk_text_iter_copy(&textiter);
    gtk_text_iter_forward_to_end(textiterend);
    char *var_name = (char*)gtk_entry_get_text(GTK_ENTRY(window->VariableEntry));
    char *var_value = (char*)gtk_entry_get_text(GTK_ENTRY(window->ValueEntry));
    char *var_description = (char*)gtk_text_buffer_get_text(buffer,&textiter,textiterend,0);
    if (yon_char_is_empty(var_name)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->VariableEntry);
        return;
    } else if (!(((var_name[0]>='a'&&var_name[0]<='z')||(var_name[0]>='A'&&var_name[0]<='Z'))||var_name[0]=='_')){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),INVALID_FIRST_LETTER_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->VariableEntry);
        return;
    } else if (strlen(var_name)<2){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),VARIABLE_TOO_SHORT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->VariableEntry);
        return;
    }
    if (yon_char_is_empty(var_value)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->ValueEntry);
        return;
    }
    int source = gtk_combo_box_get_active(GTK_COMBO_BOX(window->SourceCombo));
    char *var_source = source ==0 ? "system" : source==1 ? "profile" : (char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->SourceUserCombo));
    char *lookup_name;
    int lookup_type;
    if (var_name&&strcmp(var_name,"")&&var_value&&strcmp(var_value,"")){
        int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(widgets->list),&search_iter);
        for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(widgets->list),&search_iter)){
            gtk_tree_model_get(GTK_TREE_MODEL(widgets->list),&search_iter,0,&lookup_name,4,&lookup_type,-1);
            if (!strcmp(var_name,lookup_name)&&lookup_type==source){
                free(lookup_name);
                yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),ALREADY_EXISTS_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                return;
            }
            free(lookup_name);
        }
        gtk_list_store_append(widgets->list,&iter);
        gtk_list_store_set(widgets->list,&iter,0,var_name,1,var_value,2,_(var_source),3,var_description,4,source,-1);
        char *target = ENVIRONMENT(source ==0 ? "system" : source==1 ? "profile" : (char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->SourceUserCombo)),var_name);
        yon_config_register(target,ENVIRONMENT_get_command(target),yon_char_new(var_value));
        yon_window_config_add_instant_parameter(var_name,"description",var_description,YON_TYPE_STRING);
        on_subwindow_close(self);
    }
    on_tab_selection_changed(NULL,widgets);
}

void on_source_changed(GtkWidget *self, add_window *window){
    if (gtk_combo_box_get_active(GTK_COMBO_BOX(window->SourceCombo))==2){
        gtk_widget_set_sensitive(window->SourceUserCombo,1);
    } else {
        gtk_widget_set_sensitive(window->SourceUserCombo,0);
    }
}

add_window *yon_add_window_new(){
    add_window *window = malloc(sizeof(add_window));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_add);
    window->Window=yon_gtk_builder_get_widget(builder,"Window");
    window->CancelButton=yon_gtk_builder_get_widget(builder,"CancelButton");
    window->HeaderTopicLabel=yon_gtk_builder_get_widget(builder,"headerTopic");
    window->AcceptButton=yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->VariableEntry=yon_gtk_builder_get_widget(builder,"VariableEntry");
    window->ValueEntry=yon_gtk_builder_get_widget(builder,"ValueEntry");
    window->SourceCombo=yon_gtk_builder_get_widget(builder,"SourceCombo");
    window->DescriptionEntry=yon_gtk_builder_get_widget(builder,"DescriptionEntry");
    window->SourceUserCombo=yon_gtk_builder_get_widget(builder,"UserSourceCombo");
    window->StatusBox=yon_gtk_builder_get_widget(builder,"StatusBox");

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

        for (int i=0;i<main_config.user_size;i++){
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->SourceUserCombo),main_config.user_list[i]);
        }
        yon_gtk_entry_block_symbols(GTK_ENTRY(window->VariableEntry),"\\*?<>+|@&^%$#№;[]!'");
        yon_gtk_entry_block_symbols(GTK_ENTRY(window->VariableEntry),":- ");
        yon_gtk_entry_block_languages(GTK_ENTRY(window->VariableEntry));
        return window;
}

void on_variable_add(GtkWidget *self, main_window *widgets){
    add_window *window = yon_add_window_new();
    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->AcceptButton),"clicked",G_CALLBACK(on_variable_save),dict);
    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),TITLE_LABEL,icon_path,"AddWindow");
    gtk_widget_show(window->Window);
}

void on_variable_remove(GtkWidget *self, main_window *widgets){
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->list);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&model, &iter)){
        int id;
        char *var_name, *user_id;
        gtk_tree_model_get(GTK_TREE_MODEL(widgets->list),&iter,0,&var_name,2,&user_id,4,&id,-1);
        char *source_name;
        if (id==0) source_name="system";
        if (id==1) source_name="profile";
        if (id==2) source_name=user_id;
        yon_config_remove_by_key(ENVIRONMENT(source_name,var_name));
        gtk_list_store_remove(widgets->list,&iter);
        yon_window_config_erase_instant_parameter(var_name,"description");
    }

}

void on_variable_edit_save(GtkWidget *self,dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    add_window *window = yon_dictionary_get_data(dict->first->next,add_window*);
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->list);
    GtkTreeIter iter;
    GtkTextIter textiter,*textiterend;
    GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(window->DescriptionEntry));
    gtk_text_buffer_get_iter_at_line_offset(buffer,&textiter,0,0);
    textiterend = gtk_text_iter_copy(&textiter);
    gtk_text_iter_forward_to_end(textiterend);
    char *var_name = (char*)gtk_entry_get_text(GTK_ENTRY(window->VariableEntry));
    char *var_value = (char*)gtk_entry_get_text(GTK_ENTRY(window->ValueEntry));
    char *var_description = (char*)gtk_text_buffer_get_text(buffer,&textiter,textiterend,0);
    int source = gtk_combo_box_get_active(GTK_COMBO_BOX(window->SourceCombo));
    char *var_source = (char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->SourceCombo));
    if (var_name&&strcmp(var_name,"")&&var_value&&strcmp(var_value,"")){
        gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&model,&iter);
        gtk_list_store_set(widgets->list,&iter,0,var_name,1,var_value,2,_(source ==0 ? "system" : source==1 ? "profile" : (char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->SourceUserCombo))),3,var_description,4,source,-1);
        char *target = ENVIRONMENT(source ==0 ? "system" : source==1 ? "profile" : (char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->SourceUserCombo)),var_name);
        yon_config_set(target,yon_char_new(var_value));
        yon_window_config_erase_instant_parameter(var_name,"description");
        yon_window_config_add_instant_parameter(var_name,"description",var_description,YON_TYPE_STRING);
        on_subwindow_close(self);
    }
}

void on_variable_edit(GtkWidget *self, main_window *widgets){
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->list);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&model, &iter)){
        
        add_window *window = yon_add_window_new();
        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->AcceptButton),"clicked",G_CALLBACK(on_variable_save),dict);
        gtk_widget_show(window->Window);
        gtk_widget_set_can_focus(window->VariableEntry,0);
        gtk_entry_set_has_frame(GTK_ENTRY(window->VariableEntry),0);
        gtk_style_context_add_class(gtk_widget_get_style_context(window->VariableEntry),"nobackground");
        gtk_style_context_add_class(gtk_widget_get_style_context(window->SourceCombo),"nobackground");
        gtk_style_context_add_class(gtk_widget_get_style_context(window->SourceCombo),"noborder");
        gtk_widget_set_sensitive(window->SourceCombo,0);
        char *var_variable;
        char *var_value;
        char *var_user;
        int var_source;
        char *var_description;
        gtk_tree_model_get(model,&iter,0,&var_variable,1,&var_value,2,&var_user,3,&var_description,4,&var_source,-1);
        gtk_entry_set_text(GTK_ENTRY(window->VariableEntry),var_variable);
        gtk_entry_set_text(GTK_ENTRY(window->ValueEntry),var_value);
        gtk_combo_box_set_active(GTK_COMBO_BOX(window->SourceCombo),var_source);
        gtk_label_set_text(GTK_LABEL(window->HeaderTopicLabel),yon_char_unite(EDIT_ENV_LABEL," - ", var_variable,NULL));
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),yon_char_unite(EDIT_ENV_LABEL," - ", var_variable,NULL),icon_path,"AddWindow");
        gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(window->DescriptionEntry)),var_description,strlen(var_description));
        for (int i=0;i<main_config.user_size;i++){
            if (!strcmp(main_config.user_list[i],var_user))
                gtk_combo_box_set_active(GTK_COMBO_BOX(window->SourceUserCombo),i+1);
        }
        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->AcceptButton),"clicked",G_CALLBACK(on_variable_edit_save),dict);

        gtk_widget_show(window->Window);
    }
}

void on_inspecor_edit_activate(GtkWidget *, GtkTreePath *,GtkTreeViewColumn *,dictionary *dict){
    on_inspecor_edit(NULL,dict);
}

void on_inspecor_edit(GtkWidget *, dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    inspector_window *window = yon_dictionary_get_data(dict->first->next,inspector_window*);
    GtkTreeIter iter,inspector_iter;
    GtkTreeModel *model = GTK_TREE_MODEL(main_config.env_variables);
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->Tree)),&model,&inspector_iter)){
        char *var_name;
        char *var_val;
        char *var_descr;
        char *lookup_name;
        int lookup_type;
        gtk_tree_model_get(model,&inspector_iter,0,&var_name,1,&var_val,2,&var_descr,-1);
        int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(widgets->list),&iter);
        for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(widgets->list),&iter)){
            gtk_tree_model_get(GTK_TREE_MODEL(widgets->list),&iter,0,&lookup_name,4,&lookup_type,-1);
            if (!strcmp(var_name,lookup_name)&&lookup_type==0){
                free(lookup_name);
                gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&iter);
                on_variable_edit(widgets->EditButton,widgets);
                return;
            }
            free(lookup_name);
        }
        gtk_list_store_append(widgets->list,&iter);
        gtk_list_store_set(widgets->list,&iter,0,var_name,1,var_val,2,_("system"),3,var_descr,4,0,-1);
        gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&iter);
        yon_config_register(ENVIRONMENT("system",var_name),ENVIRONMENT_get_command(ENVIRONMENT("system",var_name)),yon_char_new(var_val));
        {

            GtkTreeModel *model = GTK_TREE_MODEL(widgets->list);
            GtkTreeIter iter;
            if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&model, &iter)){
                
                add_window *dialog = yon_add_window_new();
                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",dialog);
                g_signal_connect(G_OBJECT(dialog->AcceptButton),"clicked",G_CALLBACK(on_variable_save),dict);
                yon_gtk_window_setup(GTK_WINDOW(dialog->Window),GTK_WINDOW(window->Window),TITLE_LABEL,icon_path,"AddWindow");
                gtk_widget_show(dialog->Window);
                gtk_widget_set_can_focus(dialog->VariableEntry,0);
                gtk_entry_set_has_frame(GTK_ENTRY(dialog->VariableEntry),0);
                gtk_style_context_add_class(gtk_widget_get_style_context(dialog->VariableEntry),"nobackground");
                gtk_style_context_add_class(gtk_widget_get_style_context(dialog->SourceCombo),"nobackground");
                gtk_style_context_add_class(gtk_widget_get_style_context(dialog->SourceCombo),"noborder");
                gtk_widget_set_sensitive(dialog->SourceCombo,0);
                char *var_variable;
                char *var_value;
                char *var_user;
                int var_source;
                char *var_description;
                gtk_tree_model_get(model,&iter,0,&var_variable,1,&var_value,2,&var_user,3,&var_description,4,&var_source,-1);
                gtk_entry_set_text(GTK_ENTRY(dialog->VariableEntry),var_variable);
                gtk_entry_set_text(GTK_ENTRY(dialog->ValueEntry),var_value);
                gtk_combo_box_set_active(GTK_COMBO_BOX(dialog->SourceCombo),var_source);
                gtk_label_set_text(GTK_LABEL(dialog->HeaderTopicLabel),yon_char_unite(TITLE_LABEL," - ", var_variable,NULL));
                gtk_text_buffer_set_text(gtk_text_view_get_buffer(GTK_TEXT_VIEW(dialog->DescriptionEntry)),var_description,strlen(var_description));
                for (int i=0;i<main_config.user_size;i++){
                    if (!strcmp(main_config.user_list[i],var_user))
                        gtk_combo_box_set_active(GTK_COMBO_BOX(dialog->SourceUserCombo),i+1);
                }
                yon_dictionary_add_or_create_if_exists_with_data(dict,"widgets",widgets);
                yon_dictionary_add_or_create_if_exists_with_data(dict,"window",dialog);
                g_signal_connect(G_OBJECT(dialog->AcceptButton),"clicked",G_CALLBACK(on_variable_edit_save),dict);

                gtk_widget_show(dialog->Window);
            }
        }
    }
    on_tab_selection_changed(NULL,widgets);
}

void on_inspector_destroy(GtkWidget *,main_window *widgets){
    gtk_widget_set_sensitive(widgets->InspectButton,1);

}

void on_inspector_destroyed(GtkWidget *, GdkEvent *,inspector_window *window){
    GList *list = gtk_tree_view_get_columns(GTK_TREE_VIEW(window->Tree));
    for (guint i=0;i<g_list_length(list);i++){
        GValue val = G_VALUE_INIT;
        g_object_get_property(G_OBJECT(g_list_nth_data(list,i)),"fixed-width",&val);

        char *column_n = yon_char_from_int((int)i);
        char *column_name = yon_char_append("InspectorColumn",column_n);
        free(column_n);
        int value = g_value_get_int(&val);
        yon_window_config_add_instant_parameter(column_name,"window",&value,YON_TYPE_INT);
        free(column_name);
    }
    g_list_free(list);
    

}

void on_inspector_open(GtkWidget *self, main_window *widgets){
    gtk_widget_set_sensitive(widgets->InspectButton,0);
    inspector_window *window = malloc(sizeof(inspector_window));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_inspector);
    window->Tree=yon_gtk_builder_get_widget(builder,"InspectorTree");
    window->Window=yon_gtk_builder_get_widget(builder,"Window");
    window->EditButton=yon_gtk_builder_get_widget(builder,"EditButton");

    yon_gtk_window_setup(GTK_WINDOW(window->Window),NULL,TITLE_LABEL,icon_path,"InspectorWindow");
    gtk_tree_view_set_model(GTK_TREE_VIEW(window->Tree),GTK_TREE_MODEL(main_config.env_variables));

    g_signal_connect(G_OBJECT(window->Window),"destroy",G_CALLBACK(on_inspector_destroy),widgets);
    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->EditButton),"clicked",G_CALLBACK(on_inspecor_edit),dict);
    g_signal_connect(G_OBJECT(window->Window),"delete-event",G_CALLBACK(on_inspector_destroyed),window);
    g_signal_connect(G_OBJECT(window->Tree),"row-activated",G_CALLBACK(on_inspecor_edit_activate),dict);

    GList *list = gtk_tree_view_get_columns(GTK_TREE_VIEW(window->Tree));

    for (guint i=0;i<g_list_length(list)-1;i++){
        char *column_n = yon_char_from_int((int)i);
        char *column_name = yon_char_append("InspectorColumn",column_n);
        free(column_n);

        int width;
        yon_window_config_get_parameter("window",column_name,&width,YON_TYPE_INT);
        
        if (width>0){
        gtk_tree_view_column_set_fixed_width(GTK_TREE_VIEW_COLUMN(g_list_nth_data(list,i)),width);
        } else {
            yon_gtk_tree_view_column_minimal_fixed_size_set(GTK_TREE_VIEW_COLUMN(g_list_nth_data(list,i)));
        }
        free(column_name);
        
    }
    g_list_free(list);

    window->menu = yon_rmb_menu_setup(window->Tree,NULL,NULL,
    INSPECTOR_EDIT_LABEL,edit_icon_name,G_CALLBACK(on_inspecor_edit),dict,
    NULL);

    
    gtk_widget_show(window->Window);

}

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

void on_root_switched(GtkWidget *self, main_window *widgets){

    char *argline = LocaleName;
    // gtk_widget_hide(widgets->Window);
    for (int i=1;i<main_config.launch_size;i++){
        char *temp = yon_char_unite(argline," -a ",main_config.launch_arguments[i],NULL);
        if(strcmp(argline,""))
            free(argline);
        argline=temp;
    }
    if (getuid()!=0){
        argline = yon_char_unite("setsid /usr/bin/bash -c 'pkexec ",argline,"'; exit",NULL);
        pthread_t thread_id;
        pthread_create(&thread_id, NULL, (void *)on_root_get_root,argline);
        gtk_widget_destroy(widgets->Window);
    }
}

void on_tab_selection_changed(GtkWidget *self,main_window *widgets) {
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->list);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&model,&iter)){
        gtk_widget_set_sensitive(widgets->EditButton,1);
        gtk_widget_set_sensitive(widgets->RemoveButton,1);
    } else {
        gtk_widget_set_sensitive(widgets->EditButton,0);
        gtk_widget_set_sensitive(widgets->RemoveButton,0);
    }
}

void on_env_files_add(GtkWidget *self, env_files_window *window){
    GtkTreeModel *model = GTK_TREE_MODEL(window->liststore1);
    GtkTreeIter iter;
    GtkWidget *dialog = gtk_file_chooser_dialog_new(TITLE_LABEL,NULL,GTK_FILE_CHOOSER_ACTION_OPEN,CANCEL_LABEL,GTK_RESPONSE_CANCEL, ACCEPT_LABEL, GTK_RESPONSE_ACCEPT,NULL);
    yon_gtk_window_setup(GTK_WINDOW(dialog),GTK_WINDOW(window->Window),TITLE_LABEL,icon_path,"FileChooser");
    if (getuid()==0){
        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),"/");
        gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog),yon_ubl_user_get_home_directory(),NULL);
    }
    int responce = gtk_dialog_run(GTK_DIALOG(dialog));
    if (responce==GTK_RESPONSE_ACCEPT){
        char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
        if (strstr(filename,yon_ubl_user_get_home_directory())){
            yon_char_divide(filename,strlen(yon_ubl_user_get_home_directory())-1);
            filename = yon_char_append("~",filename);
        }
        gtk_list_store_append(window->liststore1,&iter);
        gtk_list_store_set(window->liststore1,&iter,0,filename,-1);
    }
        gtk_widget_destroy(dialog);
}

void on_env_files_edit(GtkWidget *self, env_files_window *window){
    GtkTreeModel *model = GTK_TREE_MODEL(window->liststore1);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->MainTree)),&model,&iter)){
        char *path;
        gtk_tree_model_get(model,&iter,0,&path,-1);
        path = yon_char_divide(path,yon_char_find_last(path,'/'));
        GtkWidget *dialog = gtk_file_chooser_dialog_new(TITLE_LABEL,NULL,GTK_FILE_CHOOSER_ACTION_OPEN,CANCEL_LABEL,GTK_RESPONSE_CANCEL, ACCEPT_LABEL, GTK_RESPONSE_ACCEPT,NULL);
        gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog),path);
        if (getuid()==0){
            gtk_file_chooser_add_shortcut_folder(GTK_FILE_CHOOSER(dialog),yon_ubl_user_get_home_directory(),NULL);
        }
        int responce = gtk_dialog_run(GTK_DIALOG(dialog));
        if (responce==GTK_RESPONSE_ACCEPT){
            char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
            if (strstr(filename,yon_ubl_user_get_home_directory())){
                yon_char_divide(filename,strlen(yon_ubl_user_get_home_directory())-1);
                filename = yon_char_append("~",filename);
            }
            gtk_list_store_set(window->liststore1,&iter,0,filename,-1);
        }
        gtk_widget_destroy(dialog);
    }
    
}

void on_env_files_remove(GtkWidget *self, env_files_window *window){
    GtkTreeModel *model = GTK_TREE_MODEL(window->liststore1);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->MainTree)),&model,&iter)){
        gtk_list_store_remove(window->liststore1,&iter);
    }
}

void on_cell_editing_done(GtkCellRendererText *self, char *path, char *new_text, env_files_window *window){
    GtkTreeIter iter;
    gtk_tree_model_get_iter(GTK_TREE_MODEL(window->liststore1),&iter,gtk_tree_path_new_from_string(path));
    if (new_text&&strcmp(new_text,""))
        gtk_list_store_set(window->liststore1,&iter,0,new_text,-1);
    GValue *val = g_malloc0(sizeof(GValue));
    g_object_get_property(G_OBJECT(window->TextCell),"editable",val);
    g_value_set_boolean(val,0);
    g_object_set_property(G_OBJECT(window->TextCell),"editable",val);
    g_free(val);
}

void on_cell_editing_canceled(GtkCellRenderer *self, env_files_window *window){
    GtkTreeModel *model = GTK_TREE_MODEL(window->liststore1);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->MainTree)),&model,&iter)){
        char *prev_name;
        gtk_tree_model_get(model,&iter,0,&prev_name,-1);
        if (!prev_name||!strcmp(prev_name,"")) gtk_list_store_remove(window->liststore1,&iter);
        GValue *val = g_malloc0(sizeof(GValue));
        g_object_get_property(G_OBJECT(window->TextCell),"editable",val);
        g_value_set_boolean(val,0);
        g_object_set_property(G_OBJECT(window->TextCell),"editable",val);
        g_free(val);
    }
}

void on_env_files_open(GtkWidget *self, main_window *widgets);

void yon_menu_update(main_window *widgets){
    GtkWidget *submenu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(widgets->EditMenuItem));
    GList *list = gtk_container_get_children(GTK_CONTAINER(submenu));
    for (int i=0;i<g_list_length(list);i++){
        gtk_container_remove(GTK_CONTAINER(submenu),g_list_nth_data(list,i));
    }
    int size;
    config_str vars = yon_resource_open_file(environment_paths_path,&size);
    for (int i=0; i<size;i++){
        vars[i]=yon_char_divide_search(vars[i],"\n",-1);
        GtkWidget *menuitem = gtk_menu_item_new_with_label(vars[i]);
        gtk_container_add(GTK_CONTAINER(submenu),menuitem);
        gtk_style_context_add_class(gtk_widget_get_style_context(menuitem),"menuitemmiddle");
        gtk_widget_show(menuitem);
        g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_edit_open),widgets);
    }
    int envs_size;
    GtkTreeIter iter;
    config_str custom_envs = yon_file_open(config_envs_path,&envs_size);
    if (custom_envs){
        for (int i=0;i<envs_size;i++){
            custom_envs[i]=yon_char_divide_search(custom_envs[i],"\n",-1);
            GtkWidget *menuitem = gtk_menu_item_new_with_label(custom_envs[i]);
            gtk_container_add(GTK_CONTAINER(submenu),menuitem);
            gtk_style_context_add_class(gtk_widget_get_style_context(menuitem),"menuitemmiddle");
            gtk_widget_show(menuitem);
            g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_edit_open),widgets);
            free(custom_envs[i]);
        }
    }
    // GtkWidget *menuitem = gtk_menu_item_new_with_label(EDIT_LIST_LABEL);
    GtkWidget *menuitem = gtk_menu_item_new();
    {
        GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
        GtkWidget *label = gtk_label_new(EDIT_LIST_LABEL);
        GtkWidget *icon = gtk_image_new_from_icon_name("com.ublinux.libublsettingsui-gtk3.settings-symbolic",GTK_ICON_SIZE_BUTTON);
        gtk_box_pack_start(GTK_BOX(box),icon,0,0,0);
        gtk_box_pack_start(GTK_BOX(box),label,1,1,0);
        gtk_container_add(GTK_CONTAINER(menuitem),box);
    }
    gtk_container_add(GTK_CONTAINER(submenu),menuitem);
    gtk_style_context_add_class(gtk_widget_get_style_context(menuitem),"menuitembottom");
    gtk_widget_show_all(menuitem);
    g_signal_connect(G_OBJECT(menuitem),"activate",G_CALLBACK(on_env_files_open),widgets);
}

void on_env_files_save(GtkWidget *self, dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    env_files_window *window = yon_dictionary_get_data(dict->first->next,env_files_window*);
    GtkTreeIter iter;
    int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(window->liststore1),&iter);
    char *final_string = "";
    for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(window->liststore1),&iter)){
        char *cur_string;
        gtk_tree_model_get(GTK_TREE_MODEL(window->liststore1),&iter,0,&cur_string,-1);
        final_string=yon_char_unite(final_string,strcmp(final_string,"")?"\n":"",cur_string,NULL);
        free(cur_string);
    }
    FILE *file = fopen(config_envs_path,"w");
    fputs(final_string,file);
    fclose(file);
    yon_menu_update(widgets);
    on_subwindow_close(self);
}

void on_env_files_open(GtkWidget *self, main_window *widgets){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_env_files);
    env_files_window *window = malloc(sizeof(env_files_window));     
    window->liststore1=GTK_LIST_STORE(gtk_builder_get_object(builder,"liststore1"));
    window->Window=yon_gtk_builder_get_widget(builder,"Window");
    window->AddButton=yon_gtk_builder_get_widget(builder,"AddButton");
    window->RemoveButton=yon_gtk_builder_get_widget(builder,"RemoveButton");
    window->ConfigureButton=yon_gtk_builder_get_widget(builder,"ConfigureButton");
    window->MainTree=yon_gtk_builder_get_widget(builder,"MainTree");
    window->CancelButton=yon_gtk_builder_get_widget(builder,"CancelButton");
    window->AcceptButton=yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->TextCell=GTK_CELL_RENDERER(gtk_builder_get_object(builder,"TextCell"));
    int envs_size;
    GtkTreeIter iter;
    config_str custom_envs = yon_file_open(config_envs_path,&envs_size);
    if (custom_envs&&envs_size){
        for (int i=0;i<envs_size;i++){
            custom_envs[i]=yon_char_divide_search(custom_envs[i],"\n",-1);
            gtk_list_store_append(window->liststore1,&iter);
            gtk_list_store_set(window->liststore1,&iter,0,custom_envs[i],-1);
            free(custom_envs[i]);
        }
    } else {
        config_str envs = yon_resource_open_file(environment_paths_path,&envs_size);
        if (envs&&envs_size){
            for (int i=0;i<envs_size;i++){
                yon_char_remove_last_symbol(envs[i],'\n');
                gtk_list_store_append(window->liststore1,&iter);
                gtk_list_store_set(window->liststore1,&iter,0,envs[i],-1);
            }
        }
    }
    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->AddButton),"clicked",G_CALLBACK(on_env_files_add),window);
    g_signal_connect(G_OBJECT(window->ConfigureButton),"clicked",G_CALLBACK(on_env_files_edit),window);
    g_signal_connect(G_OBJECT(window->RemoveButton),"clicked",G_CALLBACK(on_env_files_remove),window);
    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_env_files_save),dict);

    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),TITLE_LABEL,icon_path,"env_files_window");
    
    gtk_widget_show(window->Window);
}

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.load_mode=YON_CONFIG_LOCAL;
    main_config.save_config = 1;
    main_config.env_variables=gtk_list_store_new(3,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING);
    // main_config.env_strings=yon_config_load(get_vars_command,&main_config.env_size);
}

void on_variable_activate(GtkWidget *self, GtkTreePath *, GtkTreeViewColumn*, main_window *widgets){
    on_variable_edit(self,widgets);
}

main_window *yon_main_window_complete(main_window *widgets){
    /* Widgets getting | Получение виджетов */
    widgets = yon_remalloc(widgets,sizeof(main_window)); 
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path);
    gtk_box_pack_start(GTK_BOX(widgets->InterfaceBox),yon_gtk_builder_get_widget(builder,"BoxMain"),1,1,0); 

    widgets->LeftBox = yon_gtk_builder_get_widget(builder,"HeaderLeftBox");

    widgets->MainTree = yon_gtk_builder_get_widget(builder,"EnvTree");
    widgets->list = GTK_LIST_STORE(gtk_builder_get_object(builder,"liststore1"));
    widgets->InspectButton = yon_gtk_builder_get_widget(builder,"InspectButton");
    widgets->AddButton = yon_gtk_builder_get_widget(builder,"AddButton");
    widgets->RemoveButton = yon_gtk_builder_get_widget(builder,"RemoveButton");
    widgets->EditButton = yon_gtk_builder_get_widget(builder,"EditButton");

    widgets->EditMenuItem = yon_gtk_builder_get_widget(builder,"EditMenuItem");
    gtk_window_set_icon_name(GTK_WINDOW(widgets->Window),icon_path);
    gtk_window_set_title(GTK_WINDOW(widgets->Window),TITLE_LABEL);
    widgets->menu = yon_rmb_menu_setup(widgets->MainTree,NULL,NULL,
    MAIN_ADD_LABEL,add_icon_name,G_CALLBACK(on_variable_add),widgets,
    MAIN_REMOVE_LABEL,remove_icon_name,G_CALLBACK(on_variable_remove),widgets,
    MAIN_EDIT_LABEL,edit_icon_name,G_CALLBACK(on_variable_edit),widgets,
    NULL);
    {
        gtk_style_context_add_class(gtk_widget_get_style_context(widgets->EditMenuItem),"menuitempointer");
        yon_menu_update(widgets);
    }
    {
        GtkWidget *menu = gtk_widget_get_parent(widgets->DocumentationMenuItem);
        gtk_menu_shell_prepend(GTK_MENU_SHELL(menu),widgets->EditMenuItem);
    }
    int size;
    GtkTreeIter iter;
    {
        config_str descriptions = yon_resource_open_file(get_descriptions_command,&size);
        dictionary *dict=NULL;
        for (int i=0;i<size;i++){
            char *name = yon_char_divide_search(descriptions[i],";",-1);
            descriptions[i]=yon_char_divide_search(descriptions[i],"\n",-1);
            yon_dictionary_add_or_create_if_exists_with_data(dict,name,descriptions[i]);
        }
        main_config.env_descriptions = dict;
        for (int i=0;main_config.env_strings[i];i++){
            char *env_value = yon_char_new(main_config.env_strings[i]);
            char *env_name = yon_char_divide_search(env_value,"=",-1);
            env_value = yon_char_divide_search(env_value,"\n",-1);
            char *descr = NULL;
            if (main_config.env_descriptions){
                if (yon_dictionary_get(&main_config.env_descriptions,env_name))
                    descr = yon_dictionary_get_data(main_config.env_descriptions,char*);
            }
            gtk_list_store_append(main_config.env_variables,&iter);
            gtk_list_store_set(main_config.env_variables,&iter,0,env_name,1,env_value,2,descr ? _(descr) : "",-1);
            yon_window_config_add_instant_parameter(env_name,"description",descr ? _(descr) : "",YON_TYPE_STRING);
            free(env_name);
            free(env_value);
        }
    }
    {
        config_str uids = yon_config_load(get_min_max_uid_command,&size);
        int uid_min = atoi(uids[0]);
        int uid_max = atoi(uids[1]);
        struct passwd *user = NULL;
        main_config.user_list = NULL;
        while ((user = getpwent())) {
            if (user->pw_uid>=uid_min&&user->pw_uid<=uid_max){
                if (!main_config.user_list)
                    main_config.user_list = yon_char_parsed_new(&main_config.user_size,user->pw_name,NULL);
                else {
                    main_config.user_list = yon_char_parsed_append(main_config.user_list,&main_config.user_size,user->pw_name);
                }
            }
        }
        endpwent();
    }

    /* Widget registration for config monitoring | Регистрация виджетов для мониторинга конфига */
    // yon_window_config_add_custom_parameter(widgets->HeadInfoLabel,"head-text","label",YON_TYPE_STRING); 

    /* Signal connection | Присоединение сигналов */
    g_signal_connect(G_OBJECT(widgets->InspectButton),"clicked",G_CALLBACK(on_inspector_open),widgets);
    g_signal_connect(G_OBJECT(widgets->AddButton),"clicked",G_CALLBACK(on_variable_add),widgets);
    g_signal_connect(G_OBJECT(widgets->RemoveButton),"clicked",G_CALLBACK(on_variable_remove),widgets);
    g_signal_connect(G_OBJECT(widgets->EditButton),"clicked",G_CALLBACK(on_variable_edit),widgets);
    g_signal_connect(G_OBJECT(widgets->MainTree),"cursor-changed",G_CALLBACK(on_tab_selection_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->SaveMenuItem),"activate",G_CALLBACK(yon_config_save_global_local),NULL);
    g_signal_connect(G_OBJECT(widgets->SaveGlobalMenuItem),"activate",G_CALLBACK(yon_config_save_global),NULL);
    g_signal_connect(G_OBJECT(widgets->SaveLocalMenuItem),"activate",G_CALLBACK(yon_config_save_local),NULL);
    g_signal_connect(G_OBJECT(widgets->SaveCustomMenuItem),"activate",G_CALLBACK(on_config_custom_save),widgets);
    g_signal_connect(G_OBJECT(widgets->LoadGlobalMenuItem),"activate",G_CALLBACK(yon_config_global_load),widgets);
    g_signal_connect(G_OBJECT(widgets->LoadLocalMenuItem),"activate",G_CALLBACK(yon_config_local_load),widgets);
    g_signal_connect(G_OBJECT(widgets->LoadCustomMenuItem),"activate",G_CALLBACK(on_config_custom_load),widgets);
    g_signal_connect(G_OBJECT(widgets->MainTree),"row-activated",G_CALLBACK(on_variable_activate),widgets);
    
    yon_window_config_setup(GTK_WINDOW(widgets->Window));
	yon_window_config_load(config_path);
    yon_load_proceed(YON_CONFIG_LOCAL,widgets);
    yon_save_window_set_postsave_function(on_save_done,widgets);
    return widgets;
}

int main(int argc, char **argv, char **envp){
    local=setlocale(LC_ALL, "");
    textdomain (LocaleName);
    config_init();
    yon_ubl_connect_config((_template_config*)&main_config); 
    yon_ubl_window_init(TITLE_LABEL,TITLE_INFO_LABEL,LocaleName,CssPath,LocaleName,version_application,WIKI_LINK);
	config_str unfound = NULL;
	int size=0;
	yon_ubl_setup_arguments(argc,argv,&unfound,&size,NULL);
    gtk_init(&argc,&argv);
    main_config.env_strings = envp;
    main_config.launch_arguments=argv;
    main_config.launch_size=argc;
    template_main_window *widgets = yon_ubl_window_setup(); 
    yon_root_button_setup(widgets,argv,argc);
    yon_main_window_complete((main_window*)widgets);
    if (getuid()!=0){
        yon_ubl_status_box_render(yon_char_get_localised_from_lib(ROOT_WARNING_LABEL),BACKGROUND_IMAGE_FAIL_TYPE);
    }
    gtk_main();
}