#include "ubl-settings-kernel.h"

config main_config;

//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){
		printf("%s\n",final_output);
		free(final_output);
	}
	yon_char_parsed_free(output,size);
	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;
	}
	textdomain(LocaleName);
	
}

void yon_load_proceed(YON_CONFIG_TYPE type){
	if (type!=YON_CONFIG_CUSTOM){
		yon_config_clean();
	}
    if (!yon_char_is_empty(config_get_default_command))
        yon_config_load_config(YON_CONFIG_DEFAULT,config_get_default_command,NULL);
    if (type==YON_CONFIG_GLOBAL){
		yon_config_load_config(type,config_get_global_command,NULL);
	} else if (type==YON_CONFIG_LOCAL){
		yon_config_load_config(type,config_get_local_command,NULL);
    } else if (type==YON_CONFIG_CUSTOM){
		char *path="";
        textdomain(template_ui_LocaleName);
		GtkWidget *dialog = gtk_file_chooser_dialog_new(template_app_information.app_title,NULL,GTK_FILE_CHOOSER_ACTION_SAVE,CANCEL_LABEL,GTK_RESPONSE_CANCEL,OPEN_LABEL,GTK_RESPONSE_ACCEPT,NULL);
        textdomain(LocaleName);
        gtk_window_set_icon_name(GTK_WINDOW(dialog),"com.ublinux.ubl-settings-kernel");
        gtk_window_set_title(GTK_WINDOW(dialog),TITLE_LABEL);
        GtkFileFilter *filter = gtk_file_filter_new();
		gtk_file_filter_add_pattern(filter,"*.ini");
        gtk_file_filter_set_name(filter, "*.ini");
		gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
		gtk_widget_show(dialog);
		int response = gtk_dialog_run(GTK_DIALOG(dialog));
		if (response == GTK_RESPONSE_ACCEPT){
			char *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
			if (!yon_char_is_empty(file)){
				path=file;
			}
            yon_config_clean();
            char *command = yon_config_get_custom_command(path);
            yon_config_load_config(type,command,NULL);
			gtk_widget_destroy(dialog);
		} else {
			gtk_widget_destroy(dialog);
		}
	}
}

void on_config_save(GtkWidget *, main_window *){
	main_config.save_config=2;
	template_saving_window *window = yon_save_proceed(NULL,YON_CONFIG_BOTH,yon_config_get_custom_command("global"),NULL);
    if (window){};

}

void on_config_global_save(GtkWidget *, main_window *){
	main_config.save_config=0;
	template_saving_window *window = yon_save_proceed("global",YON_CONFIG_GLOBAL,yon_config_get_custom_command("global"),NULL);
    if (window){};

}

void on_config_local_save(GtkWidget *, main_window *){
	main_config.save_config=1;
	template_saving_window *window = yon_save_proceed("system",YON_CONFIG_LOCAL,yon_config_get_custom_command("system"),NULL);
    if (window){};

}

void on_config_custom_save(GtkWidget *, main_window *){
    main_config.save_config=1;
	template_saving_window *window = yon_save_proceed(NULL,YON_CONFIG_CUSTOM,yon_config_get_custom_command("system"),NULL);
    if (window){};

}


void on_config_local_load(GtkWidget *, main_window *widgets){
	yon_load_proceed(YON_CONFIG_LOCAL);
	textdomain(template_ui_LocaleName);
	yon_ubl_status_box_render(LOCAL_LOAD_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE); 
	textdomain(LocaleName);
	main_config.load_mode=YON_CONFIG_LOCAL;
        on_update_clicked(NULL,widgets);

}

void on_config_global_load(GtkWidget *, main_window *widgets){
	yon_load_proceed(YON_CONFIG_GLOBAL);
	textdomain(template_ui_LocaleName);
	yon_ubl_status_box_render(GLOBAL_LOAD_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
	textdomain(LocaleName);
	main_config.load_mode=YON_CONFIG_GLOBAL;
        on_update_clicked(NULL,widgets);

}

void on_config_custom_load(GtkWidget *,main_window *widgets){
	yon_load_proceed(YON_CONFIG_CUSTOM);
	textdomain(template_ui_LocaleName);
	yon_ubl_status_box_render(GLOBAL_LOAD_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
	textdomain(LocaleName);
	main_config.load_mode=YON_CONFIG_CUSTOM;
        on_update_clicked(NULL,widgets);

}

    void on_terminal_done(GtkWidget *, int state, GtkWidget *status_box){
        if (!state) yon_ubl_status_box_spawn(GTK_CONTAINER(status_box),BUILD_SUCCESS_STATUS_LABEL,0,BACKGROUND_IMAGE_SUCCESS_TYPE);
        else yon_ubl_status_box_spawn(GTK_CONTAINER(status_box),BUILD_FAILED_STATUS_LABEL,0,BACKGROUND_IMAGE_FAIL_TYPE);
    }

void on_pacman_db_unlock(void){
    yon_launch(pacman_unlock_database_command);
    yon_ubl_status_box_render(yon_char_get_localised_from_lib(SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
}

    void yon_terminal_window_start(GtkWindow *parent_window, char *command){
        GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        GtkWidget *header = gtk_header_bar_new();
        GtkWidget *terminal = vte_terminal_new();
        GtkWidget *status_box = gtk_box_new(GTK_ORIENTATION_VERTICAL,5);
        GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL,5);
        GtkWidget *terminal_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
        GtkWidget *scroll = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL,gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(terminal)));

        gtk_widget_set_size_request(window,800,500);
        gtk_window_set_titlebar(GTK_WINDOW(window),header);
        gtk_container_add(GTK_CONTAINER(window),box);
        gtk_box_pack_start(GTK_BOX(box),status_box,0,0,0);
        gtk_box_pack_start(GTK_BOX(box),terminal_box,1,1,0);
        gtk_box_pack_start(GTK_BOX(terminal_box),terminal,1,1,0);
        gtk_box_pack_start(GTK_BOX(terminal_box),scroll,0,0,0);
        gtk_widget_set_margin_bottom(terminal_box,5);
        gtk_widget_set_margin_start(terminal_box,5);
        gtk_widget_set_margin_end(terminal_box,5);
        gtk_widget_show(window);
        gtk_widget_show(header);
        gtk_widget_show(terminal);
        gtk_widget_show(status_box);
        gtk_widget_show(box);
        gtk_widget_show(terminal_box);
        gtk_widget_show(scroll);

        vte_terminal_set_scrollback_lines(VTE_TERMINAL(terminal),-1);
        
        gtk_window_set_modal(GTK_WINDOW(window),1);
        gtk_header_bar_set_show_close_button(GTK_HEADER_BAR(header),1);
        gtk_window_set_title(GTK_WINDOW(window),gtk_window_get_title(parent_window));
        gtk_window_set_icon_name(GTK_WINDOW(window),gtk_window_get_icon_name(parent_window));
            g_signal_connect(G_OBJECT(terminal), "child-exited", G_CALLBACK(on_terminal_done), status_box);
        yon_terminal_integrated_start_shell(terminal,command,NULL,NULL);
    }

char *yon_package_dependent_get(char *target){
    int command_check = 0;
    command_check = system(check_pactree_exist_command);
    if (command_check) return target;
    int size=0;
    config_str parsed = NULL;
    if (strstr(target,",")){
        int final_size=0;
            int targets_size=0;
            config_str targets = yon_char_parse(target,&targets_size,",");
        for (int i=0;i<targets_size;i++){
            int deps_size = 0;
            config_str deps = yon_config_load(get_dependent_command(targets[i]),&deps_size);
            for(int j=0;j<deps_size;j++){
                yon_char_remove_last_symbol(deps[j],'\n');
            }
            config_str tmp = yon_char_parsed_merge(parsed,size,deps,deps_size,&final_size);
            if (parsed) yon_char_parsed_free(parsed,size);
            parsed=tmp;
            size=final_size;
        }
    } else {
        parsed = yon_config_load(get_dependent_command(target),&size);
    }
    int recurs_size=0;
    config_str recurs = NULL;
    for (int i=0;i<size;i++){
        yon_char_remove_last_symbol(parsed[i],'\n');
        yon_char_parsed_add_or_create_if_exists(recurs,&recurs_size,parsed[i]);
    }
    if (recurs_size){
        yon_char_parsed_free(parsed,size);
        parsed = recurs;
        size = recurs_size;
    } 
    return yon_char_parsed_to_string(parsed,size," ");
}

gboolean yon_tab_create_tags(kernels_tab *tab){
    GList *list = gtk_container_get_children(GTK_CONTAINER(tab->install_tags_box));
    for (guint i=0;i<g_list_length(list);i++){
        gtk_widget_destroy(g_list_nth_data(list,i));
    }
    g_list_free(list);
    // if (!getuid()){
        switch(tab->module_installed){
            case 1:{
            yon_tag_add(GTK_BOX(tab->install_tags_box),MODULE_INSTALLED_LABEL,"tag_green",NULL);

            gtk_image_set_from_icon_name(GTK_IMAGE(tab->module_icon),module_icon_path,GTK_ICON_SIZE_BUTTON);
                gtk_widget_show(tab->remove_module_button);
                gtk_widget_show(tab->update_module_button);
                gtk_widget_hide(tab->install_module_button);
                gtk_widget_set_tooltip_text(tab->module_icon,MODULE_INSTALLED_LABEL);
            } break;
            case 0:{
                gtk_widget_hide(tab->remove_module_button);
                gtk_widget_hide(tab->update_module_button);
                gtk_widget_show(tab->install_module_button);
            } break;
            case -1: {
                gtk_widget_hide(tab->remove_module_button);
                gtk_widget_hide(tab->update_module_button);
                gtk_widget_hide(tab->install_module_button);
                gtk_widget_hide(tab->module_label);
            } break;
        }
        switch (tab->package_installed){
            case 1: {
                yon_tag_add(GTK_BOX(tab->install_tags_box),PACKAGE_INSTALLED_LABEL,"tag_green",NULL);
                gtk_image_set_from_icon_name(GTK_IMAGE(tab->package_icon),package_icon_path,GTK_ICON_SIZE_BUTTON);
                gtk_widget_hide(tab->install_package_button);
                gtk_widget_show(tab->remove_package_button);
                gtk_widget_show(tab->update_package_button);
                gtk_widget_set_tooltip_text(tab->package_icon,PACKAGE_INSTALLED_LABEL);
            } break;
            case 0:{
                gtk_widget_show(tab->install_package_button);
                gtk_widget_hide(tab->remove_package_button);
                gtk_widget_hide(tab->update_package_button);
                }break;
            case -1:{
                gtk_widget_hide(tab->remove_package_button);
                gtk_widget_hide(tab->update_package_button);
                gtk_widget_hide(tab->install_package_button);
                gtk_widget_hide(tab->package_label);
            }
        }
        if (tab->package_installed||tab->module_installed){
            if (tab->launch_button){
                char *launched = config(KERNEL_BOOT_parameter);
                int boot_size;
                config_str boot_run = yon_config_load(get_active_kernel_command,&boot_size);
                if (boot_size>0){
                    yon_char_remove_last_symbol(boot_run[0],'\n');
                }

                if ((!(launched&&!strcmp(tab->package,launched))&&(boot_run&&!yon_char_is_empty(boot_run[0])&&strcmp(tab->package,boot_run[0])))){
                    gtk_widget_show(tab->launch_button);
                }
                else if (boot_run&&boot_run[0]&&(!launched||strcmp(launched,boot_run[0]))&&(!strcmp(tab->package,boot_run[0]))){
                    gtk_widget_show(tab->launch_button);
                    yon_tag_add(GTK_BOX(tab->install_tags_box),RUNNING_LABEL,"tag_green",NULL);
                } else if (!launched){
                    gtk_widget_show(tab->launch_button);
                } else {
                    gtk_widget_hide(tab->launch_button);
                    if(!getuid()){
                        if (boot_run&&!strcmp(boot_run[0],tab->package)){
                            yon_tag_add(GTK_BOX(tab->install_tags_box),RUNNING_LABEL,"tag_green",NULL);
                            gtk_widget_hide(tab->launch_button);
                        } else if (!yon_char_is_empty(launched)&&!strcmp(launched,tab->package)){
                            yon_tag_add(GTK_BOX(tab->install_tags_box),BOOT_RUN_LABEL,"tag_orange",NULL);
                            gtk_widget_hide(tab->launch_button);
                        } 
                    }
                }
                yon_char_parsed_free(boot_run,boot_size);
            }
            if (tab->changelog_button){
                gtk_widget_show(tab->changelog_button);
            }
        }
            gtk_widget_set_sensitive(tab->update_module_button,!tab->module_updated);
            gtk_widget_set_sensitive(tab->update_package_button,!tab->package_updated);
    // } else {
    //     char *launched = config(KERNEL_BOOT_parameter);
    //     int boot_size;
    //     config_str boot_run = yon_config_load(get_active_kernel_command,&boot_size);
    //     if(boot_size){
    //         yon_char_remove_last_symbol(boot_run[0],'\n');
    //         if (boot_run&&!strcmp(boot_run[0],tab->package)){
    //             yon_tag_add(GTK_BOX(tab->install_tags_box),RUNNING_LABEL,"tag_green",NULL);
    //             gtk_widget_hide(tab->launch_button);
    //         }
    //         if (!yon_char_is_empty(launched)&&strcmp(launched,boot_run[0])&&!strcmp(launched,tab->package)){
    //             gtk_widget_show(tab->launch_button);
    //         }
    //         yon_char_parsed_free(boot_run,boot_size);
    //     }
    //     yon_tag_add(GTK_BOX(tab->install_tags_box),UNKNOWN_LABEL,"tag_grey",NULL);
    // }
    gtk_widget_show(tab->main_box);

        g_mutex_lock(&main_config.update_mutex);
        main_config.update_active--;
        g_mutex_unlock(&main_config.update_mutex);
    return G_SOURCE_REMOVE;
}

char *yon_package_string_set(char *target){
    char *final = NULL;
    if (strstr(target,",")){
        final = yon_char_replace(target,","," ");
    } else final = target;
    return final;
}

void yon_tab_set_installed(kernels_tab *tab){
    int uid = getuid();
        g_mutex_lock(&main_config.update_mutex);
        main_config.update_active++;
        g_mutex_unlock(&main_config.update_mutex);
    char *target_module = yon_package_string_set(tab->module);
    char *target_package = yon_package_string_set(tab->package);
    if (yon_char_is_empty(target_module)){
        tab->module_installed=-1;
    } else {
            tab->module_installed = yon_packages_check_installed(target_module);
            if(!uid)
            tab->module_updated = !yon_packages_check_updates(target_module);
    }
    if (yon_char_is_empty(target_package)){
        tab->package_installed=-1;
    } else {
            tab->package_installed=yon_packages_check_installed(target_package);
            if(!uid)
            tab->package_updated = !yon_packages_check_updates(target_package);
    }
    if (tab->package_installed==-1&&tab->module_installed==-1){
        gtk_widget_destroy(tab->main_box);
        tab->main_box=NULL;
    }
    gdk_threads_add_idle((GSourceFunc)yon_tab_create_tags,tab);
}

void *yon_interface_update(main_window *widgets){
    int lock = 0;
    g_mutex_lock(&main_config.update_mutex);
    lock=main_config.update_active;
    g_mutex_unlock(&main_config.update_mutex);
    if (!lock){
        gtk_widget_show(widgets->LoadingSpinner);
        gtk_spinner_start(GTK_SPINNER(widgets->LoadingSpinner));
        gtk_widget_hide(widgets->MainOverlayBox);
        yon_ubl_status_box_render(LOADING_LABEL,BACKGROUND_IMAGE_INFO_TYPE);
        {
            GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->KernelsPackageBox));
            if (g_list_length(list)>0){
                for (guint i=0;i<g_list_length(list);i++){
                    while (gtk_events_pending()) gtk_main_iteration();
                    gtk_widget_destroy(g_list_nth_data(list,i));
                }
                g_list_free(list);
            }
        }
        {
            GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->AdditionsPackageBox));
            if (g_list_length(list)>0){
                for (guint i=0;i<g_list_length(list);i++){
                    while (gtk_events_pending()) gtk_main_iteration();
                    gtk_widget_destroy(g_list_nth_data(list,i));
                }
                g_list_free(list);
            }
        }
        int kernels_size;
        config_str kernels = yon_resource_open_file(kernels_path, &kernels_size);

        for (int i=1;i<kernels_size;i++){
            while (gtk_events_pending()) gtk_main_iteration();
            int parsed_size;
            config_str parsed = yon_char_parse(kernels[i],&parsed_size,";");
            if (!yon_packages_check_exist(parsed[1])&&!yon_packages_check_exist(parsed[2])){
                yon_char_parsed_free(parsed,parsed_size); 
                continue;
            }
            kernels_tab *tab = yon_tab_row_new(GTK_LIST_BOX(widgets->KernelsPackageBox),kernels[i],widgets);
            yon_tab_set_installed(tab);
            {
            GList *list = gtk_container_get_children(GTK_CONTAINER(tab->run_tags_box));
            for (guint k=0;k<g_list_length(list);k++){
                gtk_widget_destroy(g_list_nth_data(list,k));
            }
            g_list_free(list);}
            gtk_widget_set_can_focus(gtk_widget_get_parent(tab->main_box),0);
        }
        yon_char_parsed_free(kernels,kernels_size);
        kernels = yon_resource_open_file(kernels_addon_path, &kernels_size);
        for (int i=1;i<kernels_size;i++){
            while (gtk_events_pending()) gtk_main_iteration();
            int parsed_size;
            config_str parsed = yon_char_parse(kernels[i],&parsed_size,";");
            if (!yon_packages_check_exist(parsed[1])&&!yon_packages_check_exist(parsed[2])){
                yon_char_parsed_free(parsed,parsed_size); 
                continue;
            }
            additions_kernels_tab *tab = yon_additions_tab_row_new(GTK_LIST_BOX(widgets->AdditionsPackageBox),kernels[i],widgets);
            yon_tab_set_installed((kernels_tab*)tab);
            gtk_widget_set_can_focus(gtk_widget_get_parent(tab->main_box),0);
        }
        char *status_text = NULL;
                switch (main_config.action){
                    case ACTION_LOAD:
                        status_text = main_config.load_mode==YON_CONFIG_LOCAL?main_config.local_load_success_localised:main_config.global_load_success_localised;
                    break;
                    case ACTION_SAVE:
                        status_text = "";
                    break;
                    case ACTION_TERMINAL_EXECUTE_SUCCESS:
                        status_text = OPERATION_SUCCESS_LABEL;
                    break;
                    case ACTION_TERMINAL_EXECUTE_FAIL:
                        status_text = OPERATION_ERROR_LABEL;
                    break;
                }

                yon_ubl_status_box_render(status_text,BACKGROUND_IMAGE_SUCCESS_TYPE);
        gtk_widget_hide(widgets->LoadingSpinner);
        gtk_widget_show(widgets->MainOverlayBox);
        gtk_spinner_stop(GTK_SPINNER(widgets->LoadingSpinner));
    }
    return NULL;
}

info_window *yon_info_window_new(){
    info_window *window = malloc(sizeof(info_window));

    GtkBuilder *builder = gtk_builder_new_from_resource(glade_info_path);
    
    window->window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->HeadLabel = yon_gtk_builder_get_widget(builder,"HeadLabel");
    window->InfoLabel = yon_gtk_builder_get_widget(builder,"InfoLabel");

    return window;
}

void on_basic_toggled(GtkSwitch *,int state, install_window *window){
    gtk_menu_button_set_direction(GTK_MENU_BUTTON(window->ArrowButton),state?GTK_ARROW_UP:GTK_ARROW_DOWN);
    gtk_revealer_set_transition_type(GTK_REVEALER(window->ShowRevealer),state?GTK_REVEALER_TRANSITION_TYPE_SLIDE_DOWN:GTK_REVEALER_TRANSITION_TYPE_SLIDE_UP);
    gtk_revealer_set_reveal_child(GTK_REVEALER(window->ShowRevealer), state);
}

install_window *yon_install_window_new(){
    install_window *window = malloc(sizeof(install_window));

    GtkBuilder *builder = gtk_builder_new_from_resource(glade_install_path);
    
    window->window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->HeadLabel = yon_gtk_builder_get_widget(builder,"HeadLabel");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");

    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->ExecuteButton = yon_gtk_builder_get_widget(builder,"ExecuteButton");

    window->KernelVersionCombo = yon_gtk_builder_get_widget(builder,"KernelVersionCombo");
    window->FilenameEntry = yon_gtk_builder_get_widget(builder,"FilenameEntry");
    window->BasicSwitch = yon_gtk_builder_get_widget(builder,"BasicSwitch");
    window->BasicCurrentSwitch = yon_gtk_builder_get_widget(builder,"BasicCurrentSwitch");
    window->DebugSwitch = yon_gtk_builder_get_widget(builder,"DebugSwitch");
    window->UniversalSwitch = yon_gtk_builder_get_widget(builder,"UniversalSwitch");
    window->BackgroundFrame = yon_gtk_builder_get_widget(builder,"BackgroundFrame");
    window->ArrowButton = yon_gtk_builder_get_widget(builder,"ArrowButton");
    window->ShowRevealer = yon_gtk_builder_get_widget(builder,"ShowRevealer");

    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->BasicSwitch),"state-set",G_CALLBACK(on_basic_toggled),window);

    g_signal_connect(G_OBJECT(window->BasicSwitch),"state-set",G_CALLBACK(on_build_selection_changed),window);
    g_signal_connect(G_OBJECT(window->BasicCurrentSwitch),"state-set",G_CALLBACK(on_build_selection_changed),window);
    g_signal_connect(G_OBJECT(window->DebugSwitch),"state-set",G_CALLBACK(on_build_selection_changed),window);
    g_signal_connect(G_OBJECT(window->UniversalSwitch),"state-set",G_CALLBACK(on_build_selection_changed),window);
    return window;
}

int yon_tag_add(GtkBox *target,char *tag_label, char *tag_style, char *icon_name){
    if (GTK_IS_BOX(target)&&!yon_char_is_empty(tag_label)&&!yon_char_is_empty(tag_style)){
        GtkWidget *box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,2);
        GtkWidget *label = gtk_label_new(tag_label);
        PangoAttrList *attr = pango_attr_list_new();
        PangoAttribute *scale_attr = pango_attr_size_new_absolute(12 * PANGO_SCALE);
        pango_attr_list_insert(attr,scale_attr);
        
        gtk_label_set_attributes(GTK_LABEL(label),attr);
        gtk_style_context_add_class(gtk_widget_get_style_context(label),tag_style);
        GtkWidget *icon = NULL;
        if (icon_name){
            icon = gtk_image_new_from_icon_name(icon_name,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_widget_show(label);
        gtk_widget_show(box);
        gtk_box_pack_start(target,box,0,0,0);
        return 1;
    }
    return 0;
}

gboolean on_command_execute_success(GtkWidget *,gint status,main_window *widgets){
    // gtk_revealer_set_reveal_child(GTK_REVEALER(widgets->MainRevealer),0);
    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(widgets->InstallationProgress),0);
    g_mutex_lock(&main_config.progress_mutex);
    main_config.progress_active=0;
    g_mutex_unlock(&main_config.progress_mutex);
    gtk_widget_set_sensitive(widgets->CancelButton,0);
    if (!status){
        on_update_clicked(NULL,widgets);
        main_config.action=ACTION_TERMINAL_EXECUTE_SUCCESS;
    } else {
        on_update_clicked(NULL,widgets);
        main_config.action=ACTION_TERMINAL_EXECUTE_FAIL;
        yon_ubl_status_box_render(yon_char_get_localised_from_lib(FAIL_LABEL),BACKGROUND_IMAGE_FAIL_TYPE);
        return 0;
    }
    gtk_widget_set_sensitive(widgets->UpdateButton,1);
    dictionary *dict = NULL;
    for_dictionaries(dict,main_config.kernel_tabs){
        if (!getuid()){
            kernels_tab *tab = (kernels_tab*)dict->data;
            if (tab->update_module_button) gtk_widget_set_sensitive(tab->update_module_button,!tab->module_updated);
            if (tab->update_package_button) gtk_widget_set_sensitive(tab->update_package_button,!tab->package_updated);
            if (tab->install_module_button) gtk_widget_set_sensitive(tab->install_module_button,1);
            if (tab->install_package_button) gtk_widget_set_sensitive(tab->install_package_button,1);
            if (tab->remove_module_button) gtk_widget_set_sensitive(tab->remove_module_button,1);
            if (tab->remove_package_button) gtk_widget_set_sensitive(tab->remove_package_button,1);
        }
    }
    for_dictionaries(dict,main_config.addition_tabs){
        if (!getuid()){
            kernels_tab *tab = (kernels_tab*)dict->data;
            if (tab->update_module_button) gtk_widget_set_sensitive(tab->update_module_button,!tab->module_updated);
            if (tab->update_package_button) gtk_widget_set_sensitive(tab->update_package_button,!tab->package_updated);
            if (tab->install_module_button) gtk_widget_set_sensitive(tab->install_module_button,1);
            if (tab->install_package_button) gtk_widget_set_sensitive(tab->install_package_button,1);
            if (tab->remove_module_button) gtk_widget_set_sensitive(tab->remove_module_button,1);
            if (tab->remove_package_button) gtk_widget_set_sensitive(tab->remove_package_button,1);
        }
    }
        {
            if (!status)
                gtk_button_clicked(GTK_BUTTON(widgets->MoreButton));
        }
    yon_packages_update();
    return G_SOURCE_REMOVE;
}

void *yon_command_execute_async(char *command,main_window *widgets){
    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(widgets->InstallationProgress),0);
    vte_terminal_reset(VTE_TERMINAL(widgets->InstallTerminal),0,1);
    yon_terminal_integrated_start_shell(widgets->InstallTerminal,yon_debug_output("%s\n",command),NULL,NULL);
    gtk_widget_set_sensitive(widgets->CancelButton,1);
    glong columns = vte_terminal_get_column_count(VTE_TERMINAL(widgets->InstallTerminal));
    vte_terminal_set_size(VTE_TERMINAL(widgets->InstallTerminal),columns,20);
    yon_ubl_status_box_render(OPERATION_IN_PROGRESS_LABEL,BACKGROUND_IMAGE_INFO_TYPE);

    g_mutex_lock(&main_config.progress_mutex);
    main_config.progress_active=1;
    g_mutex_unlock(&main_config.progress_mutex);
    g_timeout_add(500,(GSourceFunc)on_terminal_check_progress,widgets);
    gtk_widget_set_sensitive(widgets->UpdateButton,0);
    dictionary *dict = NULL;
    if (main_config.kernel_tabs){
        for_dictionaries(dict,main_config.kernel_tabs){
            if (((kernels_tab*)dict->data)->update_module_button) gtk_widget_set_sensitive(((kernels_tab*)dict->data)->update_module_button,0);
            if (((kernels_tab*)dict->data)->update_package_button) gtk_widget_set_sensitive(((kernels_tab*)dict->data)->update_package_button,0);
            if (((kernels_tab*)dict->data)->install_module_button) gtk_widget_set_sensitive(((kernels_tab*)dict->data)->install_module_button,0);
            if (((kernels_tab*)dict->data)->install_package_button) gtk_widget_set_sensitive(((kernels_tab*)dict->data)->install_package_button,0);
            if (((kernels_tab*)dict->data)->remove_module_button) gtk_widget_set_sensitive(((kernels_tab*)dict->data)->remove_module_button,0);
            if (((kernels_tab*)dict->data)->remove_package_button) gtk_widget_set_sensitive(((kernels_tab*)dict->data)->remove_package_button,0);
        }
    }
    if (main_config.addition_tabs){
        for_dictionaries(dict,main_config.addition_tabs){
            if (((kernels_tab*)dict->data)->update_module_button) gtk_widget_set_sensitive(((kernels_tab*)dict->data)->update_module_button,0);
            if (((kernels_tab*)dict->data)->update_package_button) gtk_widget_set_sensitive(((kernels_tab*)dict->data)->update_package_button,0);
            if (((kernels_tab*)dict->data)->install_module_button) gtk_widget_set_sensitive(((kernels_tab*)dict->data)->install_module_button,0);
            if (((kernels_tab*)dict->data)->install_package_button) gtk_widget_set_sensitive(((kernels_tab*)dict->data)->install_package_button,0);
            if (((kernels_tab*)dict->data)->remove_module_button) gtk_widget_set_sensitive(((kernels_tab*)dict->data)->remove_module_button,0);
            if (((kernels_tab*)dict->data)->remove_package_button) gtk_widget_set_sensitive(((kernels_tab*)dict->data)->remove_package_button,0);
        }
    }
    gtk_button_clicked(GTK_BUTTON(widgets->MoreButton));
    return NULL;
}

void on_module_install_accept(GtkWidget *, dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    kernels_tab *tab = yon_dictionary_get_data(dict->first->next,kernels_tab*);
    dialog_window *window = yon_dictionary_get_data(dict->first->next->next,dialog_window*);
    free(dict->first->next->next);
    dict->first->next->next = NULL;
    char *module_formated = yon_package_string_set(tab->module);
    char *modules_to_install = NULL;
    if (!tab->module_updated) modules_to_install = module_formated;
    else modules_to_install = module_formated;

    GtkTreeIter iter;
    for_iter (GTK_TREE_MODEL(window->liststore1),&iter){
        int status;
        char *package;
        gtk_tree_model_get(GTK_TREE_MODEL(window->liststore1),&iter,0,&status,1,&package,-1);
        if (status){
            if (strstr(package,",")){
                int parsed_size;
                config_str parsed = yon_char_parse(package,&parsed_size,",");
                package = "";
                for (int i=0;i<parsed_size;i++){
                    char *temp = yon_char_unite(package," ",parsed[i],NULL);
                    if (!yon_char_is_empty(package)) free(package);
                    package = temp;
                }
            }
            char *temp = yon_char_unite(modules_to_install," ", package, NULL);
            if (strcmp(modules_to_install,module_formated)) free(modules_to_install);
            modules_to_install = temp;
        }
    }
        char *command = install_command(modules_to_install);
        yon_command_execute_async(command,widgets);
        tab->module_installed=1;
        on_subwindow_close(window->Window);
}

void on_package_install_accept(GtkWidget *, dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    kernels_tab *tab = yon_dictionary_get_data(dict->first->next,kernels_tab*);
    dialog_window *window = yon_dictionary_get_data(dict->first->next->next,dialog_window*);
    char *package_formated = yon_package_string_set(tab->package);
    char *modules_to_install = package_formated;

    GtkTreeIter iter;
    for_iter (GTK_TREE_MODEL(window->liststore1),&iter){
        int status;
        char *package;
        gtk_tree_model_get(GTK_TREE_MODEL(window->liststore1),&iter,0,&status,1,&package,-1);
        if (status){
            char *temp = yon_char_unite(modules_to_install," ", package, NULL);
            if (strcmp(modules_to_install,package_formated)) free(modules_to_install);
            modules_to_install = temp;
        }
    }
        char *command = install_command(modules_to_install);
        yon_command_execute_async(command,widgets);
        tab->package_installed=1;
        on_subwindow_close(window->Window);
}

void on_module_remove_accept(GtkWidget *, dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    kernels_tab *tab = yon_dictionary_get_data(dict->first->next,kernels_tab*);
    dialog_window *window = yon_dictionary_get_data(dict->first->next->next,dialog_window*);
    dict=dict->first;
    free(dict->first->next->next);
    dict->first->next->next = NULL;
    char *module_formated = yon_package_string_set(tab->module);
    char *modules_to_install = module_formated;

    if (gtk_widget_get_visible(window->DependencesFrame)){
        GtkTreeIter iter;
        for_iter (GTK_TREE_MODEL(window->liststore1),&iter){
            int status;
            char *package;
            gtk_tree_model_get(GTK_TREE_MODEL(window->liststore1),&iter,0,&status,1,&package,-1);
            if (status){
                if (strstr(package,",")){
                    int parsed_size;
                    config_str parsed = yon_char_parse(package,&parsed_size,",");
                    package = "";
                    for (int i=0;i<parsed_size;i++){
                        char *temp = yon_char_unite(package," ",parsed[i],NULL);
                        if (!yon_char_is_empty(package)) free(package);
                        package = temp;
                    }
                }
                char *temp = yon_char_unite(modules_to_install," ", package, NULL);
                if (strcmp(modules_to_install,module_formated)) free(modules_to_install);
                modules_to_install = temp;
            }
        }
    }else {
        int size;
        config_str tmp = yon_config_load(get_dependent_command(modules_to_install),&size);
        for (int i=0;i<size;i++){
            yon_char_remove_last_symbol(tmp[i],'\n');
        }
        modules_to_install = yon_char_parsed_to_string(tmp,size," ");

    }
        char *command = remove_command(modules_to_install);
        yon_command_execute_async(command,widgets);
        tab->module_installed=0;
        on_subwindow_close(window->Window);
}

void on_package_remove_accept(GtkWidget *, dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    kernels_tab *tab = yon_dictionary_get_data(dict->first->next,kernels_tab*);
    dialog_window *window = yon_dictionary_get_data(dict->first->next->next,dialog_window*);
    dict=dict->first;
    free(dict->first->next->next);
    dict->first->next->next = NULL;
    char *module_formated = yon_package_string_set(tab->package);
    char *modules_to_install = module_formated;

    if (gtk_widget_get_visible(window->DependencesFrame)){
        GtkTreeIter iter;
        for_iter (GTK_TREE_MODEL(window->liststore1),&iter){
            int status;
            char *package;
            gtk_tree_model_get(GTK_TREE_MODEL(window->liststore1),&iter,0,&status,1,&package,-1);
            if (status){
                
                if (strstr(package,"\n")){
                    int parsed_size;
                    config_str parsed = yon_char_parse(package,&parsed_size,"\n");
                    package = "";
                    for (int i=0;i<parsed_size;i++){
                        char *temp = yon_char_unite(package," ",parsed[i],NULL);
                        if (!yon_char_is_empty(package)) free(package);
                        package = temp;
                    }
                }
                char *temp = yon_char_unite(modules_to_install," ", package, NULL);
                if (strcmp(modules_to_install,module_formated)) free(modules_to_install);
                modules_to_install = temp;
            }
        }
    }else {
        int size;
        config_str tmp = yon_config_load(get_dependent_command(modules_to_install),&size);
        for (int i=0;i<size;i++){
            yon_char_remove_last_symbol(tmp[i],'\n');
        }
        modules_to_install = yon_char_parsed_to_string(tmp,size," ");

    }
        char *command = remove_command(modules_to_install);
        yon_command_execute_async(command,widgets);
        tab->package_installed=0;
        on_subwindow_close(window->Window);
}

dialog_window *yon_dialog_window_new(){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_dialog_path);
    dialog_window *window = malloc(sizeof(dialog_window));
    window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->HeadLabel = yon_gtk_builder_get_widget(builder,"HeadLabel");
    window->TopicLabel = yon_gtk_builder_get_widget(builder,"TopicLabel");
    window->DependencesTree = yon_gtk_builder_get_widget(builder,"DependencesTree");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->DependencesFrame = yon_gtk_builder_get_widget(builder,"DependencesFrame");
    window->ChooseCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"ChooseCell"));
    window->liststore1 = GTK_LIST_STORE(gtk_builder_get_object(builder,"liststore1"));
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    yon_cell_renderer_toggle_set_toggle(GTK_CELL_RENDERER_TOGGLE(window->ChooseCell),window->DependencesTree);
    return window;
}

void on_module_install_clicked(GtkWidget *self,dictionary *dict){
    main_config.action=ACTION_TERMINAL_EXECUTE_SUCCESS;
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    kernels_tab *tab = yon_dictionary_get_data(dict->first->next,kernels_tab*);
    char *action_text = NULL;
        dialog_window *window = yon_dialog_window_new();
        gtk_window_set_transient_for(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window));
        g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_module_install_accept),dict);

    if (self == tab->update_module_button){
            gtk_box_set_spacing(GTK_BOX(gtk_widget_get_parent(window->TopicLabel)),25);
            gtk_widget_set_margin_start(gtk_widget_get_parent(window->TopicLabel),10);
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),UPDATE_MODULE_TITLE_LABEL,icon_path,"update_dialog");
        if (!gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook))){
            action_text = KERNEL_MODULE_UPDATE_CONFIRMATION_LABEL(tab->name);
            gtk_label_set_markup(GTK_LABEL(window->HeadLabel),UPDATE_MODULE_TITLE_LABEL);
        } else {
            action_text = ADDITION_MODULE_UPDATE_CONFIRMATION_LABEL(tab->name);
            gtk_label_set_markup(GTK_LABEL(window->HeadLabel),UPDATE_ADDITION_MODULE_TITLE_LABEL);

        }
    } else {
        if (!gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook))){
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),INSTALL_MODULE_TITLE_LABEL,icon_path,"dialog");
            action_text = KERNEL_MODULE_INSTALL_CONFIRMATION_LABEL(tab->name);
            gtk_label_set_markup(GTK_LABEL(window->HeadLabel),INSTALL_MODULE_TITLE_LABEL);
        } else {
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),INSTALL_MODULE_TITLE_LABEL,icon_path,"additions_dialog");
            action_text = ADDITION_MODULE_INSTALL_CONFIRMATION_LABEL(tab->name);
            gtk_label_set_markup(GTK_LABEL(window->HeadLabel),INSTALL_ADDITION_MODULE_TITLE_LABEL);
        }
    }

        gtk_label_set_markup(GTK_LABEL(window->TopicLabel),action_text);
        gtk_widget_show(window->Window);
        if (dict->first->next->next) {
            yon_dictionary_free(dict->first->next->next);
            dict->first->next->next=NULL;
        }
        yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
    if (gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook))||self==tab->update_module_button){
        
        gtk_widget_hide(window->DependencesFrame);
        gtk_box_set_child_packing(GTK_BOX(gtk_widget_get_parent(window->DependencesFrame)),gtk_widget_get_parent(gtk_widget_get_parent(window->TopicLabel)),1,1,0,GTK_PACK_START);
    } else {
        int size;
        config_str additions = yon_resource_open_file(kernels_addon_path,&size);
        GtkTreeIter iter;
        for (int i=0;i<tab->module_requirements_size;i++){
            gtk_list_store_append(window->liststore1,&iter);
            for (int j=1;j<size;j++){
                int cur_size;
                config_str parsed = yon_char_parse(additions[j],&cur_size,";");
                if (!strcmp(parsed[1],tab->module_requirements[i]))
                    gtk_list_store_set(window->liststore1,&iter,1,parsed[1],2,parsed[3],4,1,-1);
                yon_char_parsed_free(parsed,cur_size);
            }
        }
        yon_char_parsed_free(additions,size);
    }
}

void on_package_install_clicked(GtkWidget *self,dictionary *dict){
    main_config.action=ACTION_TERMINAL_EXECUTE_SUCCESS;
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    kernels_tab *tab = yon_dictionary_get_data(dict->first->next,kernels_tab*);
    char *action_text = NULL;
        dialog_window *window = yon_dialog_window_new();
        gtk_window_set_transient_for(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window));
        g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_package_install_accept),dict);
        gtk_tree_view_column_set_title(GTK_TREE_VIEW_COLUMN(gtk_tree_view_get_column(GTK_TREE_VIEW(window->DependencesTree),1)),PACKAGE_TAB_LABEL);
    if (self == tab->update_package_button){
        if (!gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook))){
        action_text = KERNEL_PACKAGE_UPDATE_CONFIRMATION_LABEL(tab->name);
        gtk_label_set_markup(GTK_LABEL(window->HeadLabel),UPDATE_PACKAGE_TITLE_LABEL);
        } else {
        action_text = ADDITION_PACKAGE_UPDATE_CONFIRMATION_LABEL(tab->name);
        gtk_label_set_markup(GTK_LABEL(window->HeadLabel),UPDATE_ADDITION_PACKAGE_TITLE_LABEL);
        }
    }else{
        if (!gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook))){
        action_text = KERNEL_PACKAGE_INSTALL_CONFIRMATION_LABEL(tab->name);
        gtk_label_set_markup(GTK_LABEL(window->HeadLabel),INSTALL_PACKAGE_TITLE_LABEL);
        } else {
            action_text = ADDITION_PACKAGE_INSTALL_CONFIRMATION_LABEL(tab->name);
            gtk_label_set_markup(GTK_LABEL(window->HeadLabel),INSTALL_ADDITION_PACKAGE_TITLE_LABEL);
        }
    }

        gtk_label_set_markup(GTK_LABEL(window->TopicLabel),action_text);
        gtk_widget_show(window->Window);
        if (dict->first->next->next) {
            yon_dictionary_free(dict->first->next->next);
            dict->first->next->next=NULL;
        }
        yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);

    if (gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook))){
        gtk_label_set_text(GTK_LABEL(window->HeadLabel),self==tab->update_package_button?UPDATE_ADDITION_PACKAGE_TITLE_LABEL:INSTALL_ADDITION_PACKAGE_TITLE_LABEL);
        gtk_label_set_markup(GTK_LABEL(window->TopicLabel),ADDITION_PACKAGE_UPDATE_CONFIRMATION_LABEL(tab->name));
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),self==tab->update_package_button?UPDATE_PACKAGE_TITLE_LABEL:INSTALL_PACKAGE_TITLE_LABEL,icon_path,"additions_dialog");
        gtk_widget_hide(window->DependencesFrame);
        gtk_box_set_child_packing(GTK_BOX(gtk_widget_get_parent(window->DependencesFrame)),gtk_widget_get_parent(gtk_widget_get_parent(window->TopicLabel)),1,1,0,GTK_PACK_START);
    } else {
        gtk_label_set_text(GTK_LABEL(window->HeadLabel),self==tab->update_package_button?UPDATE_PACKAGE_TITLE_LABEL:INSTALL_PACKAGE_TITLE_LABEL);
        gtk_label_set_markup(GTK_LABEL(window->TopicLabel),self==tab->update_package_button?KERNEL_PACKAGE_UPDATE_CONFIRMATION_LABEL(tab->name):KERNEL_PACKAGE_INSTALL_CONFIRMATION_LABEL(tab->name));
        if (self!=tab->update_package_button){
            yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),INSTALL_PACKAGE_TITLE_LABEL,icon_path,"dialog");
            int size;
            config_str additions = yon_resource_open_file(kernels_addon_path,&size);
            GtkTreeIter iter;
            for (int i=0;i<tab->package_requirements_size;i++){
                gtk_list_store_append(window->liststore1,&iter);
                for (int j=1;j<size;j++){
                    int cur_size;
                    config_str parsed = yon_char_parse(additions[j],&cur_size,";");
                    if (!strcmp(parsed[2],tab->package_requirements[i]))
                        gtk_list_store_set(window->liststore1,&iter,2,parsed[3],-1);
                    yon_char_parsed_free(parsed,cur_size);
                }
                gtk_list_store_set(window->liststore1,&iter,0,1,1,tab->package_requirements[i],4,1,-1);
            }
            yon_char_parsed_free(additions,size);
        } else {
            gtk_widget_set_margin_start(gtk_widget_get_parent(window->TopicLabel),10);
            gtk_box_set_spacing(GTK_BOX(gtk_widget_get_parent(window->TopicLabel)),25);
            gtk_box_set_child_packing(GTK_BOX(gtk_widget_get_parent(window->DependencesFrame)),gtk_widget_get_parent(gtk_widget_get_parent(window->TopicLabel)),1,1,0,GTK_PACK_START);
            yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),UPDATE_PACKAGE_TITLE_LABEL,icon_path,"update_dialog");
            gtk_widget_hide(window->DependencesFrame);
        }

    }
}

void on_module_remove_done(GtkWidget *self, int state, kernels_tab *tab){
    char *command = get_package_is_installed_command(tab->module);
    if (!state&&!system(command)){
        yon_ubl_status_box_render(PACKAGE_REMOVED_BUT_NOT_DISABLED_LABEL,BACKGROUND_IMAGE_INFO_TYPE);
    }
    g_signal_handlers_disconnect_by_func(G_OBJECT(self),on_module_remove_done,tab);
}

void on_module_remove_clicked(GtkWidget *self,dictionary *dict){
    main_config.action=ACTION_TERMINAL_EXECUTE_SUCCESS;
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    kernels_tab *tab = yon_dictionary_get_data(dict->first->next,kernels_tab*);
    int size;
    config_str cnf = yon_config_load(SAVE_MODE_GET_COMMAND,&size);
    if (!cnf){
        cnf = yon_char_parsed_new(&size,"changes",NULL);
    }
    yon_char_remove_last_symbol(cnf[0],'\n');
    SAVE_MODE_TYPE type = yon_ubl_save_mode_get_type(cnf[0]);
    int installed_kernels=0;
    dictionary *cur_ker=NULL;
    for_dictionaries(cur_ker,main_config.kernel_tabs){
        switch(type){
            case SAVE_MODE_RAM_HOME:
            case SAVE_MODE_SANDBOX:{
                if (((kernels_tab*)cur_ker->data)->module_installed){
                    installed_kernels++;
                } break;
            default:
                if (((kernels_tab*)cur_ker->data)->package_installed||((kernels_tab*)cur_ker->data)->module_installed){
                    installed_kernels++;
                }

            } break;
        }
    }
    char *action_text = NULL;
        dialog_window *window = yon_dialog_window_new();
        gtk_window_set_transient_for(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window));
        if (dict->first->next->next){
            dict=dict->first;
            free(dict->first->next->next);
            dict->first->next->next=NULL;
        }
        yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
        dict=dict->first;
        g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_module_remove_accept),dict);

    gtk_widget_show(window->Window);
    if (gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook))){
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),self==tab->update_package_button?UPDATE_MODULE_TITLE_LABEL:INSTALL_MODULE_TITLE_LABEL,icon_path,"additions_dialog");
        action_text = ADDITION_MODULE_REMOVE_CONFIRMATION_LABEL(tab->name);
        gtk_widget_hide(window->DependencesFrame);
        gtk_box_set_child_packing(GTK_BOX(gtk_widget_get_parent(window->DependencesFrame)),gtk_widget_get_parent(gtk_widget_get_parent(window->TopicLabel)),1,1,0,GTK_PACK_START);
        gtk_label_set_markup(GTK_LABEL(window->HeadLabel),REMOVE_ADDITION_MODULE_TITLE_LABEL);
    } else {
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),self==tab->update_package_button?UPDATE_MODULE_TITLE_LABEL:INSTALL_MODULE_TITLE_LABEL,icon_path,"dialog");
        gtk_label_set_text(GTK_LABEL(window->HeadLabel),REMOVE_MODULE_TITLE_LABEL);
        action_text = ADDITION_MODULE_REMOVE_CONFIRMATION_LABEL(tab->name);
    if (installed_kernels>1){
        action_text = KERNEL_MODULE_REMOVE_CONFIRMATION_LABEL(tab->name);
    } else if (installed_kernels==1){
        action_text = KERNEL_REMOVE_LAST_CONFIRMATION_LABEL(tab->name);
    }
        int size;
        config_str additions = yon_resource_open_file(kernels_addon_path,&size);
        GtkTreeIter itar;
        char *target_depends = yon_package_dependent_get(tab->module);
        if (target_depends&&strcmp(target_depends,tab->package)&&strstr(target_depends," ")){
            free(yon_char_divide_search(target_depends," ",-1));
            char *final = yon_char_replace(target_depends," ","\n");
            gtk_list_store_append(window->liststore1,&itar);
            gtk_list_store_set(window->liststore1,&itar,0,1,1,target_depends,-1);
            free(final);
        }
        for (int i=0;i<tab->module_requirements_size;i++){
            if (yon_packages_check_installed(tab->module_requirements[i])){
                for (int j=1;j<size;j++){
                    int cur_size;
                    config_str parsed = yon_char_parse(additions[j],&cur_size,";");
                    if (!strcmp(parsed[1],tab->module_requirements[i])){
                        char *target = parsed[1];
                        char *depends = yon_package_dependent_get(target);
                        if (strcmp(depends,target)){
                            gtk_list_store_append(window->liststore1,&itar);
                            gtk_list_store_set(window->liststore1,&itar,0,1,1,depends,2, parsed[3],-1);

                        } else {
                            gtk_list_store_append(window->liststore1,&itar);
                            gtk_list_store_set(window->liststore1,&itar,0,1,1,target,2, parsed[3],-1);

                        }
                    }
                    yon_char_parsed_free(parsed,cur_size);
                }
            }
                
        }
        yon_char_parsed_free(additions,size);
    }
    gtk_label_set_markup(GTK_LABEL(window->TopicLabel),action_text);

}

void on_package_remove_clicked(GtkWidget *self,dictionary *dict){
    main_config.action=ACTION_TERMINAL_EXECUTE_SUCCESS;
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    kernels_tab *tab = yon_dictionary_get_data(dict->first->next,kernels_tab*);
    int size;
    config_str cnf = yon_config_load(SAVE_MODE_GET_COMMAND,&size);
    yon_char_remove_last_symbol(cnf[0],'\n');
    SAVE_MODE_TYPE type = yon_ubl_save_mode_get_type(cnf[0]);
    int installed_kernels=0;
    dictionary *cur_ker=NULL;
    for_dictionaries(cur_ker,main_config.kernel_tabs){
        switch(type){
            case SAVE_MODE_RAM_HOME:
            case SAVE_MODE_SANDBOX:{
                if (((kernels_tab*)cur_ker->data)->module_installed){
                    installed_kernels++;
                } break;
            default:
                if (((kernels_tab*)cur_ker->data)->package_installed||((kernels_tab*)cur_ker->data)->module_installed){
                    installed_kernels++;
                }

            } break;
        }
    }
    
        dialog_window *window = yon_dialog_window_new();
        gtk_window_set_transient_for(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window));
        gtk_tree_view_column_set_title(GTK_TREE_VIEW_COLUMN(gtk_tree_view_get_column(GTK_TREE_VIEW(window->DependencesTree),1)),PACKAGE_TAB_LABEL);
        if (dict->first->next->next){
            dict=dict->first;
            free(dict->first->next->next);
            dict->first->next->next=NULL;
        }
        yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
        dict=dict->first;
        g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_package_remove_accept),dict);
        char *action_text = "";

    gtk_widget_show(window->Window);
    if (gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook))){
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),self==tab->update_package_button?UPDATE_MODULE_TITLE_LABEL:INSTALL_MODULE_TITLE_LABEL,icon_path,"additions_dialog");
        action_text = ADDITION_PACKAGE_REMOVE_CONFIRMATION_LABEL(tab->name);
        gtk_label_set_text(GTK_LABEL(window->HeadLabel),REMOVE_ADDITION_PACKAGE_TITLE_LABEL);
        gtk_widget_hide(window->DependencesFrame);
        gtk_box_set_child_packing(GTK_BOX(gtk_widget_get_parent(window->DependencesFrame)),gtk_widget_get_parent(gtk_widget_get_parent(window->TopicLabel)),1,1,0,GTK_PACK_START);
    } else {
        if (installed_kernels>1){
            action_text = KERNEL_PACKAGE_REMOVE_CONFIRMATION_LABEL(tab->name);
        } else if (installed_kernels==1){
            action_text = KERNEL_REMOVE_LAST_CONFIRMATION_LABEL(tab->name);
        }
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),self==tab->update_package_button?UPDATE_MODULE_TITLE_LABEL:INSTALL_MODULE_TITLE_LABEL,icon_path,"dialog");
        
        gtk_label_set_text(GTK_LABEL(window->HeadLabel),REMOVE_PACKAGE_TITLE_LABEL);
        int size;
        GtkTreeIter itar;
        char *target_depends = yon_package_dependent_get(tab->package);
        if (target_depends&&strcmp(target_depends,tab->package)){
            // free(yon_char_divide_search(target_depends," ",-1));
            char *final = yon_char_replace(target_depends," ","\n");
            gtk_list_store_append(window->liststore1,&itar);
            gtk_list_store_set(window->liststore1,&itar,0,1,1,final,-1);
            free(final);
        }
        config_str additions = yon_resource_open_file(kernels_addon_path,&size);
        for (int i=0;i<tab->module_requirements_size;i++){
            if (yon_packages_check_installed(tab->module_requirements[i])){
                for (int j=1;j<size;j++){
                    int cur_size;
                    config_str parsed = yon_char_parse(additions[j],&cur_size,";");
                    if (!strcmp(parsed[1],tab->module_requirements[i])){
                        char *target = parsed[2];
                        char *depends = yon_package_dependent_get(target);
                        if (yon_char_is_empty(depends)) continue;
                        if (strcmp(depends,target)){
                            gtk_list_store_append(window->liststore1,&itar);
                            gtk_list_store_set(window->liststore1,&itar,0,1,1,depends,2, parsed[3],-1);

                        } else {
                            gtk_list_store_append(window->liststore1,&itar);
                            gtk_list_store_set(window->liststore1,&itar,0,1,1,target,2, parsed[3],-1);

                        }
                    }
                    yon_char_parsed_free(parsed,cur_size);
                }
            }
        }
        yon_char_parsed_free(additions,size);
    }
    gtk_label_set_markup(GTK_LABEL(window->TopicLabel),action_text);

}

void on_launch_cliked(GtkWidget *self,dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    kernels_tab *tab = yon_dictionary_get_data(dict->first->next,kernels_tab*);
    dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
    data->action_text = LAUNCH_CONFIRMATION_LABEL(tab->name);
    if (yon_confirmation_dialog_call(self,data) == GTK_RESPONSE_ACCEPT){
        yon_config_register(KERNEL_BOOT_parameter,KERNEL_BOOT_command,tab->package);
        on_update_clicked(NULL,widgets);

    }
}

kernels_tab *yon_tab_row_new(GtkListBox *target, char *target_string, main_window *widgets){
    int size;
    config_str parsed = yon_char_parse(target_string,&size,";");
    if (size){
        kernels_tab *tab = NULL;
        int types_size;

            tab = malloc(sizeof(kernels_tab));

            GtkBuilder *builder = gtk_builder_new_from_resource(glade_table_row_path);
            tab->main_box = yon_gtk_builder_get_widget(builder,"TableRowLabel");
            tab->name_label = yon_gtk_builder_get_widget(builder,"NameLabel");
            tab->module_label = yon_gtk_builder_get_widget(builder,"ModuleLabel");
            tab->package_label = yon_gtk_builder_get_widget(builder,"PackageLabel");
            tab->module_icon = yon_gtk_builder_get_widget(builder,"ModuleIcon");
            tab->package_icon = yon_gtk_builder_get_widget(builder,"PackageIcon");
            tab->module_label = yon_gtk_builder_get_widget(builder,"ModuleLabel");
            tab->package_label = yon_gtk_builder_get_widget(builder,"PackageLabel");
            tab->install_tags_box = yon_gtk_builder_get_widget(builder,"InstallTagsBox");
            tab->run_tags_box = yon_gtk_builder_get_widget(builder,"RunBox");
            tab->tags_box = yon_gtk_builder_get_widget(builder,"TagsBox");
            tab->description_label = yon_gtk_builder_get_widget(builder,"DescriptionLabel");
            tab->remove_module_button = yon_gtk_builder_get_widget(builder,"RemoveModuleButton");
            tab->remove_package_button = yon_gtk_builder_get_widget(builder,"RemovePackageButton");
            tab->update_module_button = yon_gtk_builder_get_widget(builder,"ModuleUpdateButton");
            tab->update_package_button = yon_gtk_builder_get_widget(builder,"PackageUpdateButton");
            tab->install_module_button = yon_gtk_builder_get_widget(builder,"InstallModuleButton");
            tab->install_package_button = yon_gtk_builder_get_widget(builder,"InstallPackageButton");
            tab->changelog_button = yon_gtk_builder_get_widget(builder,"ChangelogButton");
            tab->launch_button = yon_gtk_builder_get_widget(builder,"LaunchButton");
            tab->module_updated=1;
            tab->package_updated=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,"tab",tab);
            g_signal_connect(G_OBJECT(tab->update_module_button),"clicked",G_CALLBACK(on_module_install_clicked),dict);
            g_signal_connect(G_OBJECT(tab->update_package_button),"clicked",G_CALLBACK(on_package_install_clicked),dict);
            g_signal_connect(G_OBJECT(tab->install_module_button),"clicked",G_CALLBACK(on_module_install_clicked),dict);
            g_signal_connect(G_OBJECT(tab->install_package_button),"clicked",G_CALLBACK(on_package_install_clicked),dict);
            g_signal_connect(G_OBJECT(tab->remove_module_button),"clicked",G_CALLBACK(on_module_remove_clicked),dict);
            g_signal_connect(G_OBJECT(tab->remove_package_button),"clicked",G_CALLBACK(on_package_remove_clicked),dict);
            g_signal_connect(G_OBJECT(tab->changelog_button),"clicked",G_CALLBACK(on_changelog_clicked),tab);
            g_signal_connect(G_OBJECT(tab->launch_button),"clicked",G_CALLBACK(on_launch_cliked),dict);
            g_signal_connect(G_OBJECT(tab->module_label),"activate-link",G_CALLBACK(on_info_clicked),tab);
            g_signal_connect(G_OBJECT(tab->package_label),"activate-link",G_CALLBACK(on_info_clicked),tab);

            
            if(getuid()){
                if (tab->launch_button) gtk_widget_set_sensitive(tab->launch_button,0);
                if (tab->remove_module_button) gtk_widget_set_sensitive(tab->remove_module_button,0);
                if (tab->install_module_button) gtk_widget_set_sensitive(tab->install_module_button,0);
                if (tab->remove_package_button) gtk_widget_set_sensitive(tab->remove_package_button,0);
                if (tab->install_package_button) gtk_widget_set_sensitive(tab->install_package_button,0);
            }
            tab->module_installed=-1;
            tab->package_installed=-1;
            tab->name = yon_char_new(parsed[0]);
            tab->module = yon_char_new(parsed[1]);
            tab->package = yon_char_new(parsed[2]);
            char *name = yon_char_unite("<b>",tab->name,"</b>",NULL);
            char *module = yon_char_unite("<i>",MODULE_LABEL," <a href=\"\">",tab->module,"</a></i>",NULL);
            char *package = yon_char_unite("<i>",PACKAGE_LABEL," <a href=\"\">",tab->package,"</a></i>",NULL);

            yon_dictionary_add_or_create_if_exists_with_data(main_config.kernel_tabs,tab->package,tab);

            gtk_list_box_insert(target,tab->main_box,-1);
            config_str types_parsed = yon_char_parse(parsed[5],&types_size,", ");
            for (int k=0;k<types_size;k++){
                char *passw = types_parsed[k][0]==' '?yon_char_divide(types_parsed[k],0):NULL;
                if (passw) free(passw);
                char *tag_type = NULL;
                if (!strcmp(_(types_parsed[k]),LTS_TAG)){
                    tag_type = "tag_blue";
                } else if (!strcmp(_(types_parsed[k]),STABLE_TAG)){
                    tag_type = "tag_green";
                } else if (!strcmp(_(types_parsed[k]),REALTIME_TAG)){
                    tag_type = "tag_orange";
                } else if (!strcmp(_(types_parsed[k]),HARDENED_TAG)){
                    tag_type = "tag_yellow";
                } else if (!strcmp(_(types_parsed[k]),RECOMENDED_TAG)){
                    tag_type = "tag_blue";
                } else if (!strcmp(_(types_parsed[k]),REACTIVE_TAG)){
                    tag_type = "tag_purple";
                } else continue;

                yon_tag_add(GTK_BOX(tab->tags_box),_(types_parsed[k]),tag_type,NULL);
            }
            tab->module_requirements = yon_char_parse(parsed[3],&tab->module_requirements_size,",");
            tab->package_requirements = yon_char_parse(parsed[4],&tab->package_requirements_size,",");

            unsigned int desc_size;
            config_str desc = yon_char_wrap_to_lines(_(parsed[6]),3,&desc_size);
            char *description = yon_char_parsed_to_string(desc,desc_size,"\n");
            
            yon_char_parsed_free(types_parsed,types_size);
            gtk_label_set_markup(GTK_LABEL(tab->name_label),name);
            gtk_label_set_markup(GTK_LABEL(tab->module_label),module);
            gtk_label_set_markup(GTK_LABEL(tab->package_label),package);
            gtk_label_set_text(GTK_LABEL(tab->description_label),description);
            gtk_size_group_add_widget(GTK_SIZE_GROUP(widgets->KernelSizeGroup),gtk_widget_get_parent(gtk_widget_get_parent(tab->name_label)));
            gtk_size_group_add_widget(GTK_SIZE_GROUP(widgets->TagsSizeGroup),tab->tags_box);
            gtk_size_group_add_widget(GTK_SIZE_GROUP(widgets->InstallSizeGroup),tab->install_tags_box);
            gtk_size_group_add_widget(GTK_SIZE_GROUP(widgets->RunSizeGroup),tab->run_tags_box);
            gtk_size_group_add_widget(GTK_SIZE_GROUP(widgets->DescriptionSizeGroup),tab->description_label);
            gtk_size_group_add_widget(GTK_SIZE_GROUP(widgets->ButtonsSizeGroup),gtk_widget_get_parent(tab->launch_button));



        return tab;
    }
    return NULL;
}

additions_kernels_tab *yon_additions_tab_row_new(GtkListBox *target, char *target_string, main_window *widgets){
    int size=0;
    config_str parsed = yon_char_parse(target_string,&size,";");
    if (size){
        additions_kernels_tab *tab = NULL;
        
            GtkBuilder *builder = gtk_builder_new_from_resource(glade_additions_table_row_path);

            tab = malloc(sizeof(additions_kernels_tab));
            tab->main_box = yon_gtk_builder_get_widget(builder,"TableRowLabel");
            tab->name_label = yon_gtk_builder_get_widget(builder,"NameLabel");
            tab->module_label = yon_gtk_builder_get_widget(builder,"ModuleLabel");
            tab->package_label = yon_gtk_builder_get_widget(builder,"PackageLabel");
            tab->module_icon = yon_gtk_builder_get_widget(builder,"ModuleIcon");
            tab->package_icon = yon_gtk_builder_get_widget(builder,"PackageIcon");
            tab->module_label = yon_gtk_builder_get_widget(builder,"ModuleLabel");
            tab->package_label = yon_gtk_builder_get_widget(builder,"PackageLabel");
            tab->install_tags_box = yon_gtk_builder_get_widget(builder,"InstallTagsBox");
            tab->description_label = yon_gtk_builder_get_widget(builder,"DescriptionLabel");
            tab->remove_module_button = yon_gtk_builder_get_widget(builder,"RemoveModuleButton");
            tab->remove_package_button = yon_gtk_builder_get_widget(builder,"RemovePackageButton");
            tab->install_module_button = yon_gtk_builder_get_widget(builder,"InstallModuleButton");
            tab->install_package_button = yon_gtk_builder_get_widget(builder,"InstallPackageButton");
            tab->update_module_button = yon_gtk_builder_get_widget(builder,"ModuleUpdateButton");
            tab->update_package_button = yon_gtk_builder_get_widget(builder,"PackageUpdateButton");
            tab->changelog_button = NULL;
            tab->launch_button = NULL;
            tab->run_tags_box = NULL;
            tab->tags_box = NULL;
            tab->module_updated=1;
            tab->package_updated=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,"tab",tab);
            g_signal_connect(G_OBJECT(tab->update_module_button),"clicked",G_CALLBACK(on_module_install_clicked),dict);
            g_signal_connect(G_OBJECT(tab->update_package_button),"clicked",G_CALLBACK(on_package_install_clicked),dict);
            g_signal_connect(G_OBJECT(tab->install_module_button),"clicked",G_CALLBACK(on_module_install_clicked),dict);
            g_signal_connect(G_OBJECT(tab->install_package_button),"clicked",G_CALLBACK(on_package_install_clicked),dict);
            g_signal_connect(G_OBJECT(tab->remove_module_button),"clicked",G_CALLBACK(on_module_remove_clicked),dict);
            g_signal_connect(G_OBJECT(tab->remove_package_button),"clicked",G_CALLBACK(on_package_remove_clicked),dict);
            g_signal_connect(G_OBJECT(tab->module_label),"activate-link",G_CALLBACK(on_info_clicked),tab);
            g_signal_connect(G_OBJECT(tab->package_label),"activate-link",G_CALLBACK(on_info_clicked),tab);

            if(getuid()){
                if (tab->launch_button) gtk_widget_set_sensitive(tab->launch_button,0);
                if (tab->remove_module_button) gtk_widget_set_sensitive(tab->remove_module_button,0);
                if (tab->install_module_button) gtk_widget_set_sensitive(tab->install_module_button,0);
                if (tab->remove_package_button) gtk_widget_set_sensitive(tab->remove_package_button,0);
                if (tab->install_package_button) gtk_widget_set_sensitive(tab->install_package_button,0);
            }
            
            gtk_list_box_insert(target,tab->main_box,-1);

            tab->name = yon_char_new(parsed[0]);
            tab->module = yon_char_new(parsed[1]);
            tab->package = yon_char_new(parsed[2]);
            char *name = yon_char_unite("<b>",tab->name,"</b>",NULL);
            char *module = yon_char_unite("<i>",MODULE_LABEL," <a href=\"\">",tab->module,"</a></i>",NULL);
            char *package = yon_char_unite("<i>",PACKAGE_LABEL," <a href=\"\">",tab->package,"</a></i>",NULL);

            yon_dictionary_add_or_create_if_exists_with_data(main_config.addition_tabs,tab->package,tab);

            gtk_label_set_markup(GTK_LABEL(tab->name_label),name);
            gtk_label_set_markup(GTK_LABEL(tab->module_label),module);
            gtk_label_set_markup(GTK_LABEL(tab->package_label),package);
            gtk_label_set_text(GTK_LABEL(tab->description_label),_(parsed[3]));
            gtk_size_group_add_widget(GTK_SIZE_GROUP(widgets->KernelSizeGroup),gtk_widget_get_parent(gtk_widget_get_parent(tab->name_label)));
            gtk_size_group_add_widget(GTK_SIZE_GROUP(widgets->InstallSizeGroup),tab->install_tags_box);
            gtk_size_group_add_widget(GTK_SIZE_GROUP(widgets->DescriptionSizeGroup),tab->description_label);
            gtk_size_group_add_widget(GTK_SIZE_GROUP(widgets->ButtonsSizeGroup),gtk_widget_get_parent(tab->remove_module_button));
        return tab;
    }
    return NULL;
}

gboolean on_terminal_check_progress(main_window *widgets){
    g_mutex_lock(&main_config.progress_mutex);
    if (!main_config.progress_active){
        g_mutex_unlock(&main_config.progress_mutex);
        return G_SOURCE_REMOVE;
    }
    gtk_progress_bar_pulse(GTK_PROGRESS_BAR(widgets->InstallationProgress));
    g_mutex_unlock(&main_config.progress_mutex);
    return G_SOURCE_CONTINUE;
}

// 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.load_mode=YON_CONFIG_LOCAL;
    main_config.save_config=1;
    main_config.kernel_tabs=NULL;
    main_config.addition_tabs=NULL;
    main_config.progress_active=0;
    main_config.update_active=0;
    main_config.action = ACTION_LOAD;
        textdomain(template_ui_LocaleName);
    main_config.local_load_success_localised = LOCAL_LOAD_SUCCESS_LABEL;
    main_config.global_load_success_localised = GLOBAL_LOAD_SUCCESS_LABEL;
    textdomain(LocaleName);

}

void on_update_clicked(GtkWidget *self, main_window *widgets){
    yon_packages_update();
    if (self)
        main_config.action=ACTION_LOAD;
    int lock=0;
    g_mutex_lock(&main_config.update_mutex);
    lock = main_config.update_active;
    g_mutex_unlock(&main_config.update_mutex);
    if (!lock){
        yon_dictionary_free_all(main_config.kernel_tabs,free);
        yon_dictionary_free_all(main_config.addition_tabs,free);
        main_config.kernel_tabs=NULL;
        main_config.addition_tabs=NULL;
        yon_interface_update(widgets);

    }
}

void on_info_clicked(GtkWidget *self, char *, kernels_tab *tab){
    info_window *window = yon_info_window_new();
    yon_gtk_window_setup(GTK_WINDOW(window->window),NULL,INFO_TITLE_LABEL,icon_path,"info_window");
    char *package=NULL;
    if (self == tab->module_label){
        package = yon_char_new(tab->module);
    } else {
        package = yon_char_new(tab->package);
    }
    int size;  

    if (strstr(package,",")){
        
        char *temp = yon_char_replace(package,","," ");
        free(package);
        package = temp;
    }
    
    config_str info = yon_config_load(get_package_info_command(package),&size);
    if (size!=-1){
        char *final_text = yon_char_parsed_to_string(info,size,"");
        gtk_label_set_text(GTK_LABEL(window->InfoLabel),final_text);
        free(final_text);
    } else {
        gtk_label_set_text(GTK_LABEL(window->InfoLabel),PACKAGE_NOT_FOUND_LABEL);
    }
    gtk_widget_show(window->window);
}

void on_changelog_clicked(GtkWidget *, kernels_tab *tab){
    info_window *window = yon_info_window_new();
    yon_gtk_window_setup(GTK_WINDOW(window->window),GTK_WINDOW(gtk_widget_get_toplevel(tab->main_box)),INFO_TITLE_LABEL,icon_path,"changelog_window");
    int size;
    config_str info = yon_config_load(yon_debug_output("%s\n",get_kernel_changelog_command(tab->package)),&size);
    if (size!=-1){
        char *full_text = yon_char_parsed_to_string(info,size,"");
        gtk_label_set_text(GTK_LABEL(window->InfoLabel),full_text);
        free(full_text);
        yon_char_parsed_free(info,size);
    } else {
        gtk_label_set_text(GTK_LABEL(window->InfoLabel),PACKAGE_NOT_FOUND_LABEL);
    }
    gtk_widget_show(window->window);
}

void on_build_selection_changed(GtkWidget *,int,install_window *window){
    int base = gtk_switch_get_active(GTK_SWITCH(window->BasicSwitch));
    int base_current = gtk_switch_get_active(GTK_SWITCH(window->BasicCurrentSwitch));
    int debug = gtk_switch_get_active(GTK_SWITCH(window->DebugSwitch));
    int universal = gtk_switch_get_active(GTK_SWITCH(window->UniversalSwitch));
    if (base||base_current||debug||universal) gtk_widget_set_sensitive(window->ExecuteButton,1);
    else gtk_widget_set_sensitive(window->ExecuteButton,0);
}

void on_build_execute(GtkWidget *, dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    install_window *window = yon_dictionary_get_data(dict->first->next,install_window*);
    int basic = gtk_switch_get_active(GTK_SWITCH(window->BasicSwitch));
    int drivers = gtk_switch_get_active(GTK_SWITCH(window->BasicCurrentSwitch));
    int debug = gtk_switch_get_active(GTK_SWITCH(window->DebugSwitch));
    int universal = gtk_switch_get_active(GTK_SWITCH(window->UniversalSwitch));
    char *target = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->KernelVersionCombo));
    char *filename = (char*)gtk_entry_get_text(GTK_ENTRY(window->FilenameEntry));
    if (yon_char_is_empty(target)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),NOTHING_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->KernelVersionCombo);
    }
    int size=0;
    config_str commands = NULL;
    if (basic) yon_char_parsed_add_or_create_if_exists(commands,&size,build_command_basic(filename,target,drivers));
    if (debug)  yon_char_parsed_add_or_create_if_exists(commands,&size,build_command_debug(filename,target));
    if (universal) yon_char_parsed_add_or_create_if_exists(commands,&size, build_command_universal(filename,target));
    if (size){
        char *command = yon_char_parsed_to_string(commands,size,"; ");
        yon_terminal_window_start(GTK_WINDOW(widgets->Window),yon_debug_output("%s\n",command));
        gtk_widget_destroy(window->window);
    } else {
        textdomain(template_ui_LocaleName);
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),NOTHING_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        textdomain(LocaleName);
        return;
    }
}

void on_building_clicked(GtkWidget *, main_window *widgets){
    if (widgets){};
    install_window *window = yon_install_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);
    yon_gtk_window_setup(GTK_WINDOW(window->window),GTK_WINDOW(widgets->Window),INFO_TITLE_LABEL,icon_path,"install_window");
    g_signal_connect(G_OBJECT(window->ExecuteButton),"clicked",G_CALLBACK(on_build_execute),dict);

    int size;
    config_str kernels = yon_resource_open_file(kernels_path,&size);
    for (int i=1;i<size;i++){
        int cur_size;
        config_str parsed = yon_char_parse(kernels[i],&cur_size,";");
        if (cur_size&&!system(get_package_is_installed_command(parsed[2]))){
            gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->KernelVersionCombo),parsed[2],parsed[0]);
        }
        gtk_combo_box_set_active(GTK_COMBO_BOX(window->KernelVersionCombo),0);
    }

    gtk_widget_show(window->window);
}

void on_boot_toggled(GtkWidget *, char *path, main_window *widgets){
    if (widgets||path){};

}

void on_terminal_cancel(GtkWidget *,main_window *widgets){
    const char cancel_signal = 3;
    char *command = yon_char_new(" ");
    command[0]=cancel_signal;
    char *tmp = yon_char_unite(command,"\n exit 0\n",NULL);
    free(command);
    vte_terminal_feed_child(VTE_TERMINAL(widgets->InstallTerminal),tmp,strlen(tmp));
    free(tmp);
    yon_launch(pacman_unlock_database_command);
}

void on_terminal_revealer_open(GtkWidget *, main_window *widgets){
    int opened = gtk_widget_get_visible(widgets->TerminalPaned);
    switch (opened){
        case 0: { // opens
            gtk_button_set_label(GTK_BUTTON(widgets->MoreButton),LESS_LABEL);

            g_object_ref(widgets->MainNotebook);
            gtk_container_remove(GTK_CONTAINER(gtk_widget_get_parent(widgets->MainNotebook)),widgets->MainNotebook);
            gtk_paned_add1(GTK_PANED(widgets->TerminalPaned),widgets->MainNotebook);

            int pos;
            if (yon_window_config_get_parameter("settings","paned_pos",&pos,YON_TYPE_INT)) {
                gtk_paned_set_position(GTK_PANED(widgets->TerminalPaned),pos);
            }
            
            gtk_widget_show(widgets->TerminalPaned);
            gtk_widget_show(widgets->MainNotebook);

        } break;
        case 1: { //closes
            gtk_button_set_label(GTK_BUTTON(widgets->MoreButton),MORE_LABEL);

            int pos = gtk_paned_get_position(GTK_PANED(widgets->TerminalPaned));
            yon_window_config_add_instant_parameter("paned_pos","settings",&pos,YON_TYPE_INT);

            GValue val = G_VALUE_INIT;
            g_object_get_property(G_OBJECT(widgets->TerminalPaned),"max-position",&val);
            gtk_paned_set_position(GTK_PANED(widgets->TerminalPaned),g_value_get_int(&val));
            g_value_unset(&val);

            g_object_ref(widgets->MainNotebook);
            gtk_container_remove(GTK_CONTAINER(widgets->TerminalPaned),widgets->MainNotebook);
            gtk_box_pack_start(GTK_BOX(gtk_widget_get_parent(widgets->TerminalPaned)),widgets->MainNotebook,1,1,0);

            gtk_box_reorder_child(GTK_BOX(gtk_widget_get_parent(widgets->MainNotebook)),widgets->MainNotebook,0);
            gtk_widget_hide(widgets->TerminalPaned);
        } break;
    }
}

void on_paned_position_changed(GtkWidget *, GdkEvent*,main_window *widgets){
    int size = (int)(gtk_widget_get_allocated_height(widgets->InterfaceBox)*0.7);
    if (gtk_paned_get_position(GTK_PANED(widgets->TerminalPaned))>size-40)
        gtk_paned_set_position(GTK_PANED(widgets->TerminalPaned),size-40);
}

void on_database_update(void *,main_window *widgets){
    yon_terminal_integrated_start(widgets->InstallTerminal,pacman_update_command);
    gtk_button_clicked(GTK_BUTTON(widgets->MoreButton));
    }


/**yon_main_window_complete(main_window *widgets) 
 * [EN] 
 *  
 * [RU] 
 * Функция настройки основного окна приложения. [widgets] - структура со стандартным интерфейсом. 
*/ 
void yon_main_window_complete(main_window *widgets){ 
    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); 
    // Custom widgets configuration 
    {
        widgets->MainNotebook = yon_gtk_builder_get_widget(builder, "MainNotebook");
        widgets->UpdateButton = yon_gtk_builder_get_widget(builder, "UpdateButton");
        widgets->BuildingButton = yon_gtk_builder_get_widget(builder, "BuildingButton");
        widgets->UnlockPacmanButton = yon_gtk_builder_get_widget(builder, "UnlockPacmanButton");
        widgets->KernelsUpperTree = yon_gtk_builder_get_widget(builder, "KernelsUpperTree");
        widgets->KernelsTree = yon_gtk_builder_get_widget(builder, "KernelsTree");
        widgets->AdditionsUpperTree = yon_gtk_builder_get_widget(builder, "AdditionsUpperTree");
        widgets->AdditionsTree = yon_gtk_builder_get_widget(builder, "TestListBox");
        widgets->MainRevealer = yon_gtk_builder_get_widget(builder, "MainRevealer");
        widgets->CancelButton = yon_gtk_builder_get_widget(builder, "CancelButton");
        widgets->MoreButton = yon_gtk_builder_get_widget(builder, "MoreButton");
        widgets->InstallationProgress = yon_gtk_builder_get_widget(builder, "InstallationProgress");
        widgets->InstallTerminal = yon_gtk_builder_get_widget(builder, "InstallTerminal");
        widgets->TerminalRevealer = yon_gtk_builder_get_widget(builder, "TerminalRevealer");
        widgets->TerminalPaned = yon_gtk_builder_get_widget(builder, "TerminalPaned");
        widgets->TerminalScroll = yon_gtk_builder_get_widget(builder, "TerminalScroll");
        widgets->MainOverlay = yon_gtk_builder_get_widget(builder, "MainOverlay");
        widgets->MainOverlayBox = yon_gtk_builder_get_widget(builder, "MainOverlayBox");
        widgets->LoadingSpinner = yon_gtk_builder_get_widget(builder, "LoadingSpinner");
        widgets->KernelsList = GTK_LIST_STORE(gtk_builder_get_object(builder,"KernelsList"));
        widgets->AdditionsList = GTK_LIST_STORE(gtk_builder_get_object(builder,"AdditionsList"));
        widgets->KernelsPackageBox = yon_gtk_builder_get_widget(builder,"KernelsPackageBox");
        widgets->AdditionsPackageBox = yon_gtk_builder_get_widget(builder,"AdditionsPackageBox");
        widgets->StatusSizeGroup = GTK_SIZE_GROUP(gtk_builder_get_object(builder,"StatusSizeGroup"));
        widgets->KernelSizeGroup = GTK_SIZE_GROUP(gtk_builder_get_object(builder,"KernelSizeGroup"));
        widgets->TagsSizeGroup = GTK_SIZE_GROUP(gtk_builder_get_object(builder,"TagsSizeGroup"));
        widgets->InstallSizeGroup = GTK_SIZE_GROUP(gtk_builder_get_object(builder,"InstallSizeGroup"));
        widgets->RunSizeGroup = GTK_SIZE_GROUP(gtk_builder_get_object(builder,"RunSizeGroup"));
        widgets->DescriptionSizeGroup = GTK_SIZE_GROUP(gtk_builder_get_object(builder,"DescriptionSizeGroup"));
        widgets->ButtonsSizeGroup = GTK_SIZE_GROUP(gtk_builder_get_object(builder,"ButtonsSizeGroup"));

        g_signal_connect(G_OBJECT(widgets->TerminalPaned),"notify::position",G_CALLBACK(on_paned_position_changed),widgets);
        g_signal_connect(G_OBJECT(widgets->TerminalPaned),"notify::max-position",G_CALLBACK(on_paned_position_changed),widgets);
        g_signal_connect(G_OBJECT(widgets->UpdateButton),"clicked",G_CALLBACK(on_update_clicked),widgets);
        g_signal_connect(G_OBJECT(widgets->BuildingButton),"clicked",G_CALLBACK(on_building_clicked),widgets);
        g_signal_connect(G_OBJECT(widgets->UnlockPacmanButton),"clicked",G_CALLBACK(on_pacman_db_unlock),NULL);
        g_signal_connect(G_OBJECT(widgets->MoreButton),"clicked",G_CALLBACK(on_terminal_revealer_open),widgets);
        g_signal_connect(G_OBJECT(widgets->CancelButton),"clicked",G_CALLBACK(on_terminal_cancel),widgets);
        g_signal_connect(G_OBJECT(widgets->InstallTerminal),"child-exited",G_CALLBACK(on_command_execute_success),widgets);

        g_signal_connect(G_OBJECT(widgets->SaveMenuItem),"activate",G_CALLBACK(on_config_save),widgets);
        g_signal_connect(G_OBJECT(widgets->SaveGlobalMenuItem),"activate",G_CALLBACK(on_config_global_save),widgets);
        g_signal_connect(G_OBJECT(widgets->SaveLocalMenuItem),"activate",G_CALLBACK(on_config_local_save),widgets);
        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(on_config_global_load),widgets);
        g_signal_connect(G_OBJECT(widgets->LoadLocalMenuItem),"activate",G_CALLBACK(on_config_local_load),widgets);
        g_signal_connect(G_OBJECT(widgets->LoadCustomMenuItem),"activate",G_CALLBACK(on_config_custom_load),widgets);
        gtk_overlay_add_overlay(GTK_OVERLAY(widgets->MainOverlay),widgets->LoadingSpinner);

        char *path = yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL);
        yon_window_config_load(path);
        int paned_pos=0;
        if (yon_window_config_get_parameter("settings","paned_pos",&paned_pos,YON_TYPE_INT)) {
            gtk_paned_set_position(GTK_PANED(widgets->TerminalPaned),paned_pos);
        }

        yon_root_button_setup((template_main_window*)widgets,main_config.launch_arguments,main_config.launch_size);
        gtk_progress_bar_set_pulse_step(GTK_PROGRESS_BAR(widgets->InstallationProgress),0.1);
        gtk_paned_add2(GTK_PANED(widgets->TerminalPaned),widgets->TerminalRevealer);
        GdkRGBA rgba;
        vte_terminal_get_color_background_for_draw(VTE_TERMINAL(widgets->InstallTerminal),&rgba);
        vte_terminal_set_color_cursor(VTE_TERMINAL(widgets->InstallTerminal),&rgba);

        glong columns = vte_terminal_get_column_count(VTE_TERMINAL(widgets->InstallTerminal));
        vte_terminal_set_size(VTE_TERMINAL(widgets->InstallTerminal),columns,20);

        yon_load_proceed(YON_CONFIG_LOCAL);
        yon_packages_init();
        if (system(yon_check_database_command)){
            dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
            data->action_text=TITLE_LABEL;
            data->action_text=DATABASE_UNACCESSIBLE_LABEL;
            if (yon_confirmation_dialog_call(widgets->HeadOverlay,data)){
                on_database_update(NULL,widgets);
            } else {
                on_update_clicked(NULL,widgets);

            }
        } else {
            on_update_clicked(NULL,widgets);

        }

        if (getuid()){
            gtk_widget_set_sensitive(widgets->BuildingButton,0);
        } 
        // gtk_button_clicked(GTK_BUTTON(widgets->MoreButton));
        // yon_terminal_integrated_start(widgets->InstallTerminal,pacman_update_command);
        
        yon_save_window_set_postsave_function(on_save_done,widgets);
        yon_gtk_widget_set_scroll_window_for_scroll(gtk_widget_get_parent(widgets->InstallTerminal),GTK_SCROLLBAR(widgets->TerminalScroll));
    }
}

int main(int argc, char *argv[]){ 
    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.launch_arguments=yon_char_parsed_copy(argv,argc);
    main_config.launch_size=argc;
    template_main_window *widgets = yon_ubl_window_setup();
    yon_main_window_complete((main_window*)widgets);
    if (getuid()!=0){
        textdomain(template_ui_LocaleName);
        yon_ubl_status_box_render(ROOT_WARNING_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
        textdomain(LocaleName);
    }
    gtk_main();
    return 0;
}
