#include "ubl-settings-logging.h"

config main_config;

//signal emmit handlers - no header initialization


void on_save_window_parameter_switched(GtkCellRendererToggle *, gchar *path, saving_window *window){
    GtkTreeIter iter;
    if (gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(window->list),&iter,path)){
        gboolean is_active;
        gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,0,&is_active,-1);
        gtk_list_store_set(window->list,&iter,0,!is_active,-1);
    }

}

char *yon_size_get_letter_from_int(int size){
    switch (size){
        case 0: return "K";
            break;
        case 1: return "M";
            break;
        case 2: return "G";
            break;
    }
    return 0;
}

int yon_size_get_int_from_letter(char size){
    switch (size){
        case 'K': return 0;
            break;
        case 'M': return 1;
            break;
        case 'G': return 2;
            break;
    }
    return 0;
}

//functions

// void yon_polkit_authorized_action(GCallback *func){

// }

void yon_logs_custom_save(main_window *widgets){
    GtkTreeIter iter;
    char *name;
    char *description;
    char *paths;
    int standard;
    int standard_paths;
    int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(widgets->list),&iter);
    char *final_strings=NULL;
    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,&name,1,&paths,2,&description,3,&standard,4,&standard_paths,-1);
        if (paths){
            if (standard!=1){
                paths = yon_char_replace(paths,"\n",",");
                final_strings = yon_char_unite(final_strings?yon_char_append(final_strings,"\n"):"",name?name:"",";",paths?paths:"",";",description?description:"",NULL);
            }
        }  
        free(paths);
    }
    FILE *fl = fopen(custom_logs_saving_file_path,"w");
    if (final_strings)
        fputs(final_strings,fl);
    fclose(fl);
}

void update_loaded_logrotate(){
    int size=0;
    gtk_list_store_clear(main_config.logrotate_list);
    GtkTreeIter iter;
    config_str loaded = yon_config_get_all_by_key("LOGROTATE[",&size);
    char *desc;
    char *name;
    for (int i=0;i<size;i++){
        char *paths = yon_char_divide_search(loaded[i],"=",-1);
        yon_char_divide(paths,strlen("LOGROTATE"));
        yon_char_divide_search_self(paths,"]",-1);
        yon_window_config_get_parameter("description",paths,&desc,YON_TYPE_STRING);
        yon_window_config_get_parameter("render_name",paths,&name,YON_TYPE_STRING);
        char *parameters = strstr(loaded[i],":");
        if(!parameters) continue;
        char *paths_found=yon_char_divide(loaded[i],strlen(loaded[i])-strlen(parameters));
        paths_found = yon_char_replace(paths_found,",","\n");
        if(paths_found[0]=='\"') yon_char_divide(paths_found,0);
        if(paths_found[strlen(paths_found)-1]=='\"') paths_found[strlen(paths_found)-1]='\"';
        gtk_list_store_append(main_config.logrotate_list,&iter);
        gtk_list_store_set(main_config.logrotate_list,&iter,0,paths,1,paths_found,2,desc ? desc : "",-1);
    }
}

void yon_journald_size_update(journald_window *window){
    int sz;
    config_str logsize = yon_config_load(journald_size_command,&sz);
    if (logsize){
        if (logsize[0][strlen(logsize[0])-1]=='\n')
            logsize[0][strlen(logsize[0])-1]='\0';
        gtk_label_set_text(GTK_LABEL(window->LogSizeLabel),logsize[0]);
    }
}

int 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);
    }
    switch (type){
        case YON_CONFIG_GLOBAL:
            yon_debug_output("%s\n",yon_char_unite(config_get_command("global"),NULL));
            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);
            update_loaded_logrotate();
            return 1;
            break;
        case YON_CONFIG_LOCAL:
            yon_config_load_config(type,config_get_command("system"),NULL);
            yon_config_load_config(YON_CONFIG_GLOBAL,config_get_global_only_parameters,NULL);
            yon_ubl_status_box_render(yon_char_get_localised_from_lib(LOCAL_LOAD_SUCCESS_LABEL),BACKGROUND_IMAGE_SUCCESS_TYPE);
            update_loaded_logrotate();
            return 1;
            break;
        case YON_CONFIG_CUSTOM:
            char *path = NULL;
            path=yon_custom_config_init();
            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;
        default:
	}
        update_loaded_logrotate();
    return 1;
}

void on_load_global(){
    yon_load_proceed(YON_CONFIG_GLOBAL);
    main_config.load_mode=YON_CONFIG_GLOBAL;
        update_loaded_logrotate();
}

void on_load_local(){
    yon_load_proceed(YON_CONFIG_LOCAL);
    main_config.load_mode=YON_CONFIG_LOCAL;
        update_loaded_logrotate();

}

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

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

}

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

}

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

}

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

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;
		case 3:
			yon_ubl_status_box_render(SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			
	}
	textdomain(LocaleName);
	
}
void on_save_failed(main_window *, config_str , int ){
}
// Inspector functions

void on_inspector_jornald_read_async(inspector_window *window){
    FILE *fl;
    switch(window->last_mode){
        case 0:
            fl = popen(get_journal_output_command,"r");
            break;
        case 1:
            fl = popen(get_journal_output_since_boot_command,"r");
            break;
        case 2:
            fl = popen(get_journal_output_followed_command,"r");
            break;
        case 3:
            fl = popen(get_journal_output_kernel_command,"r");
            break;
        case 4:
            fl = popen(get_journal_output_prioritied_command,"r");
            break;
        case 5:
            fl = popen(get_journal_all_boots_command,"r");
            break;
        case 6:
            fl = popen(get_journal_all_units_command,"r");
            break;
        case 7:
            fl = popen(get_journal_init_tree_command,"r");
            break;
        default: fl = popen(get_journal_output_command,"r");
        break;
    }
    char *output = g_malloc0(sizeof(char)*4096);
    GtkTreeIter iter;
    main_config.remove_thread=0;
    fd_set fds;
    struct timeval tv;
    int fd = fileno(fl);
    while (main_config.remove_thread==0){
        FD_ZERO(&fds);
        FD_SET(fd, &fds);
        tv.tv_sec = 1;
        tv.tv_usec = 0;
        if(select(fd + 1, &fds, NULL, NULL, &tv)){
            output = fgets(output,4092,fl);
            if((output)){
                if(strcmp(output,"")){
                    gtk_list_store_append(window->list, &iter);
                    
                    char *identifier="";
                    char *temp = yon_char_new(output);
                    char *log=temp;
                    if (strstr(log,"\033[H\033[2J\033[3"))
                        yon_char_divide(log,strlen("\033[H\033[2J\033[3"));
                    log = yon_char_divide_search(log,"\n",-1);
                    if (window->last_mode<5){
                        gtk_tree_view_column_set_visible(gtk_tree_view_get_column(GTK_TREE_VIEW(window->JournaldTree),0),1);
                        gtk_tree_view_column_set_visible(gtk_tree_view_get_column(GTK_TREE_VIEW(window->JournaldTree),1),1);
                        gtk_tree_view_column_set_visible(gtk_tree_view_get_column(GTK_TREE_VIEW(window->JournaldTree),2),1);
                        identifier = log;
                        log=strstr(log,":");
                        if (log){
                            log++;
                            log=strstr(log,":");
                        }
                        if (log){
                            log++;
                            log=strstr(log,":");
                        }
                        if (log)
                            log++;

                        if (log&&strcmp(log," ")){
                            char *iden = yon_char_new(identifier);
                            identifier=yon_char_divide(iden,strlen(identifier)-(strlen(log)-1));
                            free(iden);
                        } else continue;
                        
                    } else if (window->last_mode>=5){
                        gtk_tree_view_column_set_visible(gtk_tree_view_get_column(GTK_TREE_VIEW(window->JournaldTree),0),0);
                        gtk_tree_view_column_set_visible(gtk_tree_view_get_column(GTK_TREE_VIEW(window->JournaldTree),1),0);
                        gtk_tree_view_column_set_visible(gtk_tree_view_get_column(GTK_TREE_VIEW(window->JournaldTree),2),0);
                        gtk_list_store_set(window->list,&iter,3,log,-1);
                        continue;
                    }
                    if (!yon_char_is_empty(identifier)){
                        int size;
                        config_str meta_data = yon_char_parse(identifier,&size," ");
                        if (meta_data){
                            char *user = meta_data[3];
                            char *process = meta_data[4];
                            char *data = yon_char_unite(meta_data[0]," ",meta_data[1]," ",meta_data[2],NULL);
                            if(process[strlen(process)-1]==':') process[strlen(process)-1]='\0';

                            gtk_list_store_set(window->list,&iter,0,data,1,user, 2, process, 3,log,4,identifier,-1);
                            yon_char_parsed_free(meta_data,size);
                        }
                    }
                    free(temp);
                }
            }
        }
    }
    main_config.inspector_thread=0;
    pclose(fl);
}

void on_inspector_update(GtkWidget *self, inspector_window *window){
    if (!strcmp(window->journal_name,"journald")){
        if(self!=window->UpdateButton){
            gtk_style_context_remove_class(gtk_widget_get_style_context(window->ShowKernelButton),"chosenOutline");
            gtk_style_context_remove_class(gtk_widget_get_style_context(window->NewMessagesButton),"chosenOutline");
            gtk_style_context_remove_class(gtk_widget_get_style_context(window->ShowAllBootsButton),"chosenOutline");
            gtk_style_context_remove_class(gtk_widget_get_style_context(window->ShowAllUnitsButton),"chosenOutline");
            gtk_style_context_remove_class(gtk_widget_get_style_context(window->ShowInitTreeButton),"chosenOutline");
            gtk_style_context_remove_class(gtk_widget_get_style_context(window->ShowPriorityButton),"chosenOutline");
            gtk_style_context_remove_class(gtk_widget_get_style_context(window->ShowSinceBootButton),"chosenOutline");
            gtk_widget_set_sensitive(window->ShowKernelButton,1);
            gtk_widget_set_sensitive(window->NewMessagesButton,1);
            gtk_widget_set_sensitive(window->ShowAllBootsButton,1);
            gtk_widget_set_sensitive(window->ShowAllUnitsButton,1);
            gtk_widget_set_sensitive(window->ShowInitTreeButton,1);
            gtk_widget_set_sensitive(window->ShowPriorityButton,1);
            gtk_widget_set_sensitive(window->ShowSinceBootButton,1);
        }
        gtk_widget_hide(window->LoggingNotebook);
        gtk_widget_show(gtk_widget_get_parent(window->JournaldTree));
        if (self==window->ShowSinceBootButton){
            window->last_mode=1;
            gtk_style_context_add_class(gtk_widget_get_style_context(window->ShowSinceBootButton),"chosenOutline");
            gtk_widget_set_sensitive(window->ShowSinceBootButton,0);
        }
        else if (self==window->NewMessagesButton){
            window->last_mode=2;
            gtk_style_context_add_class(gtk_widget_get_style_context(window->NewMessagesButton),"chosenOutline");
            gtk_widget_set_sensitive(window->NewMessagesButton,0);
        }
        else if (self==window->ShowKernelButton){
            window->last_mode=3;
            gtk_style_context_add_class(gtk_widget_get_style_context(window->ShowKernelButton),"chosenOutline");
            gtk_widget_set_sensitive(window->ShowKernelButton,0);
        }
        else if (self==window->ShowPriorityButton){
            window->last_mode=4;
            gtk_style_context_add_class(gtk_widget_get_style_context(window->ShowPriorityButton),"chosenOutline");
            gtk_widget_set_sensitive(window->ShowPriorityButton,0);
        }
        else if (self==window->ShowAllBootsButton){
            window->last_mode=5;
            gtk_style_context_add_class(gtk_widget_get_style_context(window->ShowAllBootsButton),"chosenOutline");
            gtk_widget_set_sensitive(window->ShowAllBootsButton,0);
        }
        else if (self==window->ShowAllUnitsButton){
            window->last_mode=6;
            gtk_style_context_add_class(gtk_widget_get_style_context(window->ShowAllUnitsButton),"chosenOutline");
            gtk_widget_set_sensitive(window->ShowAllUnitsButton,0);
        }
        else if (self==window->ShowInitTreeButton){
            window->last_mode=7;
            gtk_style_context_add_class(gtk_widget_get_style_context(window->ShowInitTreeButton),"chosenOutline");
            gtk_widget_set_sensitive(window->ShowInitTreeButton,0);
        }
        else if (window->last_mode==0){
            gtk_style_context_add_class(gtk_widget_get_style_context(window->ShowSinceBootButton),"chosenOutline");
            gtk_widget_set_sensitive(window->ShowSinceBootButton,0);
        }
        main_config.remove_thread=0;
        gtk_tree_view_set_model(GTK_TREE_VIEW(window->JournaldTree),GTK_TREE_MODEL(window->list));
        gtk_list_store_clear(window->list);
        gtk_tree_view_set_model(GTK_TREE_VIEW(window->JournaldTree),GTK_TREE_MODEL(window->list));
        if (main_config.inspector_thread){
            main_config.remove_thread=1;
            pthread_join(main_config.inspector_thread,NULL);
        }
        pthread_create(&main_config.inspector_thread, NULL, (void *)on_inspector_jornald_read_async, window);
    } else {
        if (window->terminals){
        gtk_widget_hide(gtk_widget_get_parent(window->JournaldTree));
        gtk_widget_show(window->LoggingNotebook);
            dictionary *dict=NULL;
            for_dictionaries (dict,window->terminals){
                GtkWidget *terminal = (GtkWidget*)dict->data;
                yon_terminal_integrated_start(terminal,read_log_command(dict->key));
            }
        }
    }
    
}

GtkWidget *yon_inspector_add_terminal(inspector_window *window,char *page_name,int mode){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_terminal_path);
    GtkWidget *box = yon_gtk_builder_get_widget(builder,"TerminalBox");
    GtkWidget *terminal = yon_gtk_builder_get_widget(builder,"LoggingTerminal");
    GtkWidget *unfound_label = yon_gtk_builder_get_widget(builder,"UnfoundLabel");
    GtkWidget *label = gtk_label_new(page_name);
    gtk_notebook_append_page(GTK_NOTEBOOK(window->LoggingNotebook),box,label);
    if (mode){
        GdkRGBA rgba;
        rgba.alpha=1;
        rgba.red=0.95; 
        rgba.green=0.95;
        rgba.blue=0.95;
        vte_terminal_set_color_cursor(VTE_TERMINAL(terminal),&rgba);
        vte_terminal_set_color_background(VTE_TERMINAL(terminal),&rgba);
        rgba.red=0;
        rgba.green=0;
        rgba.blue=0;
        vte_terminal_set_color_foreground(VTE_TERMINAL(terminal),&rgba);
        return terminal;
    } else {
        gtk_widget_destroy(terminal);
        gtk_widget_show(unfound_label);
        char *label_text = yon_char_append(page_name,PATH_NOT_FOUND_LABEL);
        gtk_label_set_text(GTK_LABEL(unfound_label),label_text);
        return NULL;
    }
    
}

void on_inspector_close(GtkWidget *, inspector_window *window){
    main_config.remove_thread++;
    GList *list = gtk_tree_view_get_columns(GTK_TREE_VIEW(window->JournaldTree));
    for (guint i=0; i< g_list_length(list);i++){
        int value=gtk_tree_view_column_get_fixed_width((GtkTreeViewColumn*)g_list_nth_data(list,i));
        yon_window_config_add_instant_parameter(yon_char_append("inspectorcolumn",yon_char_from_int(i)),"window",&value,YON_TYPE_INT);
    }

}

void on_inspector_open(GtkWidget *, main_window *widgets){
    inspector_window *window = malloc(sizeof(inspector_window));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_inspector_path);
    window->MainWindow=yon_gtk_builder_get_widget(builder,"MainWindow");
    window->TitleLabel=yon_gtk_builder_get_widget(builder,"headerTopic");
    window->ShowSinceBootButton=yon_gtk_builder_get_widget(builder,"ShowSinceBootButton");
    window->NewMessagesButton=yon_gtk_builder_get_widget(builder,"NewMessagesButton");
    window->ShowKernelButton=yon_gtk_builder_get_widget(builder,"ShowKernelButton");
    window->ShowPriorityButton=yon_gtk_builder_get_widget(builder,"ShowPriorityButton");
    window->ShowAllBootsButton=yon_gtk_builder_get_widget(builder,"ShowAllBootsButton");
    window->ShowAllUnitsButton=yon_gtk_builder_get_widget(builder,"ShowAllUnitsButton");
    window->ShowInitTreeButton=yon_gtk_builder_get_widget(builder,"ShowInitTreeButton");
    window->LoggingNotebook=yon_gtk_builder_get_widget(builder,"MainNotebook");
    window->UpdateButton=yon_gtk_builder_get_widget(builder,"UpdateButton");
    window->JournalActionsBox=yon_gtk_builder_get_widget(builder,"JournalActionsBox");
    window->JournaldTree=yon_gtk_builder_get_widget(builder,"JournaldTree");
    window->list=GTK_LIST_STORE(gtk_builder_get_object(builder,"liststore1"));
    yon_window_config_custom_window_setup(GTK_WINDOW(window->MainWindow),"InspectorWindow");
    gtk_window_set_transient_for(GTK_WINDOW(window->MainWindow),GTK_WINDOW(widgets->Window));
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->list);
    GList *list = gtk_tree_view_get_columns(GTK_TREE_VIEW(window->JournaldTree));
    for (guint i=0; i< g_list_length(list);i++){
        int value;
        yon_window_config_get_parameter("window",yon_char_append("inspectorcolumn",yon_char_from_int(i)),&value,YON_TYPE_INT);
        if (value)
            gtk_tree_view_column_set_fixed_width((GtkTreeViewColumn*)g_list_nth_data(list,i),value);
    }
    g_list_free(list);
    GtkTreeIter iter;
    char *name;
    char *paths;
    g_signal_connect(G_OBJECT(window->MainWindow),"destroy", G_CALLBACK(on_inspector_close),window);
    g_signal_connect(G_OBJECT(window->UpdateButton),"clicked", G_CALLBACK(on_inspector_update),window);
    g_signal_connect(G_OBJECT(window->ShowKernelButton),"clicked", G_CALLBACK(on_inspector_update),window);
    g_signal_connect(G_OBJECT(window->NewMessagesButton),"clicked", G_CALLBACK(on_inspector_update),window);
    g_signal_connect(G_OBJECT(window->ShowPriorityButton),"clicked", G_CALLBACK(on_inspector_update),window);
    g_signal_connect(G_OBJECT(window->ShowSinceBootButton),"clicked", G_CALLBACK(on_inspector_update),window);
    g_signal_connect(G_OBJECT(window->ShowAllBootsButton),"clicked", G_CALLBACK(on_inspector_update),window);
    g_signal_connect(G_OBJECT(window->ShowAllUnitsButton),"clicked", G_CALLBACK(on_inspector_update),window);
    g_signal_connect(G_OBJECT(window->ShowInitTreeButton),"clicked", G_CALLBACK(on_inspector_update),window);
    
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&model,&iter)){
        gtk_tree_model_get(model,&iter,0,&name,1,&paths,-1);
        window->journal_name=name;
        window->journal_paths=yon_char_parse(paths,&window->journal_size,"\n");
        yon_gtk_window_setup(GTK_WINDOW(window->MainWindow),NULL,INSPECTOR_TITLE_LABEL(name),icon_path,"inspector_window");
        gtk_label_set_text(GTK_LABEL(window->TitleLabel),INSPECTOR_TITLE_LABEL(name));
        config_str final=NULL;
        int final_size=0;
        for (int i=0;i<window->journal_size;i++){
            if (strstr(window->journal_paths[i],"*")){
                int cur_size=0;
                char *cur_folder = yon_char_divide(window->journal_paths[i],yon_char_find_last(window->journal_paths[i],'/'));
                config_str files = yon_file_ls(cur_folder,&cur_size);
                char *prearg = yon_char_divide_search(window->journal_paths[i],"*",-1);
                for (int j=0;j<cur_size;j++){
                    if(strstr(files[j],window->journal_paths[i])&&strstr(files[j],prearg)){
                        if(!final) final = yon_char_parsed_new(&final_size,yon_char_unite(cur_folder,"/",files[j],NULL),NULL);
                        else final = yon_char_parsed_append(final,&final_size,yon_char_unite(cur_folder,"/",files[j],NULL));
                    }
                }
                yon_char_parsed_free(files,cur_size);
            } else {
                if(!final) final = yon_char_parsed_new(&final_size,window->journal_paths[i],NULL);
                else final = yon_char_parsed_append(final,&final_size,window->journal_paths[i]);
            }
        }
        yon_char_parsed_free(window->journal_paths,window->journal_size);
        window->journal_paths=final;
        window->journal_size=final_size;
        window->terminals = NULL;
        if (strcmp(name,"journald")){
            gtk_widget_set_margin_top(gtk_widget_get_parent(window->UpdateButton),25);
            window->last_mode=-1;
            if (window->journal_size==0){
                GtkWidget *terminal = yon_inspector_add_terminal(window,window->journal_name,0);
                        if (terminal){
                            yon_dictionary_add_or_create_if_exists_with_data(window->terminals,window->journal_name,terminal);
                        }
            } else {
                for (int i=0;i<window->journal_size;i++){
                    if (!access(window->journal_paths[i],F_OK)){
                        GFile *file = g_file_new_for_path(window->journal_paths[i]);
                        GFileType type = g_file_query_file_type(file,G_FILE_QUERY_INFO_NONE,NULL);
                        if(type==G_FILE_TYPE_REGULAR)
                            yon_dictionary_add_or_create_if_exists_with_data(window->terminals,window->journal_paths[i],yon_inspector_add_terminal(window,window->journal_paths[i],1));
                    }
                    else{
                        GtkWidget *terminal = yon_inspector_add_terminal(window,window->journal_paths[i],0);
                        if (terminal)
                            yon_dictionary_add_or_create_if_exists_with_data(window->terminals,window->journal_paths[i],terminal);
                    }
                }
            }
            gtk_widget_hide(window->JournalActionsBox);
        } else {
            window->last_mode=0;
                yon_dictionary_add_or_create_if_exists_with_data(window->terminals,"Journald",yon_inspector_add_terminal(window,"Journald",1));
        }

        on_inspector_update(window->UpdateButton,window);
        gtk_widget_show(window->MainWindow);
    }
}

// Logrotate window

void on_logrotate_configuration_path_filechooser_selection_changed(GtkWidget *, logrotate_filechooser_window *dialog){
    GFile *file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(dialog->FileChooser));
    GFileType type = g_file_query_file_type(file,G_FILE_QUERY_INFO_NONE, NULL);
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->FileOrDirCheck))){
        if (type==G_FILE_TYPE_REGULAR){
            gtk_widget_set_sensitive(dialog->AcceptButton,0);
        } else {
            gtk_widget_set_sensitive(dialog->AcceptButton,1);
        }
    } else {
        if (type==G_FILE_TYPE_REGULAR){
            gtk_widget_set_sensitive(dialog->AcceptButton,1);
        } else {
            gtk_widget_set_sensitive(dialog->AcceptButton,0);
        }
    }
}

void on_logrotate_filechooser_file_dir_toggled(GtkWidget *, logrotate_filechooser_window *dialog){
    on_logrotate_configuration_path_filechooser_selection_changed(NULL,dialog);
}

void on_logrotate_configuration_path_filechooser_save_edit(GtkWidget *, dictionary *dict){
    logrotate_configure_window *window = yon_dictionary_get_data(dict->first,logrotate_configure_window*);
    logrotate_filechooser_window *dialog = yon_dictionary_get_data(dict->first->next,logrotate_filechooser_window*);
    GtkTreeIter iter, itar;
    GtkTreeModel *model = GTK_TREE_MODEL(window->list);
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->LogPathsTree)),&model,&iter)){
        char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog->FileChooser));
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->FileOrDirCheck))){
            if (filename[strlen(filename)-1]!='/') filename = yon_char_append(filename,"/*");
            else filename = yon_char_append(filename,"*");
        }
        int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(window->list),&itar);
        int deleted=0;
        for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&itar)){
            char *path;
            gtk_tree_model_get(GTK_TREE_MODEL(window->list),&itar,0,&path,-1); 
            if (path&&filename)
                if (!strcmp(path,filename)&&gtk_tree_path_compare(gtk_tree_model_get_path(GTK_TREE_MODEL(window->list),&itar),gtk_tree_model_get_path(GTK_TREE_MODEL(window->list),&iter))){
                    yon_ubl_status_list_store_highlight_incorrect(window->list,gtk_tree_iter_copy(&itar));
                    yon_ubl_status_box_spawn(GTK_CONTAINER(dialog->StatusBox),REPEAT_PATHS_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                    deleted=1;
                }
        }
        if(!deleted){
            gtk_list_store_set(window->list,&iter,0,filename,-1);
            on_subwindow_close(dialog->FileChooser);
        }
    }
}

void on_logrotate_configuration_path_filechooser_save(GtkWidget *, dictionary *dict){
    logrotate_configure_window *window = yon_dictionary_get_data(dict->first,logrotate_configure_window*);
    logrotate_filechooser_window *dialog = yon_dictionary_get_data(dict->first->next,logrotate_filechooser_window*);
    char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog->FileChooser));
        if (yon_char_is_empty(filename)){
            
            yon_ubl_status_box_spawn(GTK_CONTAINER(dialog->StatusBox),NOTHING_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            return;
        }
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->FileOrDirCheck))){
        GFile *file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(dialog->FileChooser));
        GFileType type = G_FILE_TYPE_UNKNOWN;
        if(file)
                type = g_file_query_file_type(file,G_FILE_QUERY_INFO_NONE, NULL);
        if (!filename||(type == G_FILE_TYPE_REGULAR)){
            filename = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog->FileChooser));
        }
        if (filename[strlen(filename)-1]!='/')
            filename=yon_char_append(filename,"/*");
        else 
            filename=yon_char_append(filename,"*");
    }
    GtkTreeIter iter, itar;
    int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(window->list),&itar);
    int deleted=0;
        for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&itar)){
            char *path;
            gtk_tree_model_get(GTK_TREE_MODEL(window->list),&itar,0,&path,-1);
            if (path&&filename)
                if (!strcmp(path,filename)){
                    yon_ubl_status_list_store_highlight_incorrect(window->list,gtk_tree_iter_copy(&itar));
                    yon_ubl_status_box_spawn(GTK_CONTAINER(dialog->StatusBox),REPEAT_PATHS_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                    deleted=1;
                }
        }
        if(!deleted){
            gtk_list_store_append(window->list,&iter);
            gtk_list_store_set(window->list,&iter,0,filename,-1);
            on_subwindow_close(dialog->FileChooser);
        }
}

void on_logrotate_configuration_path_filechooser_open(GtkWidget *self, logrotate_configure_window *window){
    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(window->list);
    if (self==window->LogPathEditButton){
        if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->LogPathsTree)),&model,&iter))
        return;
    }
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_filechooser_path);
    logrotate_filechooser_window *dialog = malloc(sizeof(logrotate_filechooser_window));
    dialog->FileChooser = yon_gtk_builder_get_widget(builder,"FileChooser");
    dialog->FileOrDirCheck = yon_gtk_builder_get_widget(builder,"FileOrDirCheck");
    dialog->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    dialog->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    dialog->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    yon_window_config_custom_window_set(GTK_WINDOW(dialog->FileChooser),"FileDialog");
    yon_gtk_window_setup(GTK_WINDOW(dialog->FileChooser),GTK_WINDOW(window->MainWindow),TITLE_LABEL,icon_path,"FileChooser");
    GtkFileFilter *filter = gtk_file_filter_new();
    gtk_file_filter_set_name(filter,"*log*");
    gtk_file_filter_add_pattern(filter,"*log*");
    gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog->FileChooser),filter);

    dictionary *dict = NULL;
    yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
    yon_dictionary_add_or_create_if_exists_with_data(dict,"dialog",dialog);
    g_signal_connect(G_OBJECT(dialog->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    if (self==window->LogPathAddButton)
        g_signal_connect(G_OBJECT(dialog->AcceptButton),"clicked",G_CALLBACK(on_logrotate_configuration_path_filechooser_save),dict);
    else if (self==window->LogPathEditButton){
        g_signal_connect(G_OBJECT(dialog->AcceptButton),"clicked",G_CALLBACK(on_logrotate_configuration_path_filechooser_save_edit),dict);
        GtkTreeModel *model=NULL;
        GtkTreeIter iter;
        if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->LogPathsTree)),&model,&iter)){
            char *path;
            gtk_tree_model_get(model,&iter,0,&path,-1);
            gtk_file_chooser_set_current_folder(GTK_FILE_CHOOSER(dialog->FileChooser),path);
        }
    }
    g_signal_connect(G_OBJECT(dialog->FileChooser),"selection-changed",G_CALLBACK(on_logrotate_configuration_path_filechooser_selection_changed),dialog);
    g_signal_connect(G_OBJECT(dialog->FileOrDirCheck),"toggled",G_CALLBACK(on_logrotate_filechooser_file_dir_toggled),dialog);
    gtk_widget_show(dialog->FileChooser);
}

void on_logrotate_configuration_path_add(GtkWidget *, logrotate_configure_window *window){
    GtkTreeModel *model = GTK_TREE_MODEL(window->list);
    GtkTreeIter iter;
        gtk_list_store_append(window->list, &iter);
        GtkTreeViewColumn *column = gtk_tree_view_get_column(GTK_TREE_VIEW(window->LogPathsTree),0);
        gtk_tree_view_set_cursor(GTK_TREE_VIEW(window->LogPathsTree),gtk_tree_model_get_path(model,&iter),column,1);
}

void on_path_editing_done(GtkCellRendererText* ,gchar* gtkpath,gchar* new_text,logrotate_configure_window *window){
    GtkTreeIter iter, itar;
    char *cur_path;
    gtk_tree_model_get_iter(GTK_TREE_MODEL(window->list),&iter,gtk_tree_path_new_from_string(gtkpath));
    gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,0,&cur_path,-1);

    if (!strcmp(new_text,"")||(cur_path&&!strcmp(cur_path,""))) 
        gtk_list_store_remove(window->list,&iter);
    else if (new_text&&strcmp(new_text,"")){
        int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(window->list),&itar);
        for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&itar)){
            char *path;
            gtk_tree_model_get(GTK_TREE_MODEL(window->list),&itar,0,&path,-1);
            int comp = gtk_tree_path_compare(gtk_tree_path_new_from_string(gtkpath),gtk_tree_model_get_path(GTK_TREE_MODEL(window->list),&itar));
            if (path&&new_text&& comp)
                if (!strcmp(path,new_text)){
                    yon_ubl_status_list_store_highlight_incorrect(window->list,gtk_tree_iter_copy(&itar));
                    yon_ubl_status_list_store_highlight_incorrect(window->list,gtk_tree_iter_copy(&iter));
                    yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),REPEAT_PATHS_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                }
        }
            gtk_list_store_set(window->list,&iter,0,new_text,-1);

    }
}

void on_path_editing_cancel(GtkCellRendererText* ,logrotate_configure_window *window){
    GtkTreeModel *model = GTK_TREE_MODEL(window->list);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->LogPathsTree)),&model,&iter)){
        char *path;
        gtk_tree_model_get(model,&iter,0,&path,-1);
        if (!path||!strcmp(path,""))
            gtk_list_store_remove(window->list,&iter);
    }
}

void on_logrotate_configuration_path_edit(GtkWidget *, logrotate_configure_window *window){
    GtkTreeModel *model = GTK_TREE_MODEL(window->list);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->LogPathsTree)),&model,&iter)){
        GtkTreeViewColumn *column = gtk_tree_view_get_column(GTK_TREE_VIEW(window->LogPathsTree),0);
        gtk_tree_view_set_cursor(GTK_TREE_VIEW(window->LogPathsTree),gtk_tree_model_get_path(model,&iter),column,1);
    }
}

void on_logrotate_configuration_path_remove(GtkWidget *, logrotate_configure_window *window){
    GtkTreeModel *model = GTK_TREE_MODEL(window->list);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->LogPathsTree)),&model,&iter)){
        gtk_list_store_remove(window->list,&iter);
    }
}

void on_combo_activate_first(GtkComboBox *self, GtkWidget *target){
    if (gtk_combo_box_get_active(self)==1) gtk_widget_set_sensitive(target,1);
    else gtk_widget_set_sensitive(target,0);

}

void on_combo_activate_second(GtkComboBox *self, GtkWidget *target){
    if (gtk_combo_box_get_active(self)==2) gtk_widget_set_sensitive(target,1);
    else gtk_widget_set_sensitive(target,0);

}

void on_file_amount_checked(GtkToggleButton *self, logrotate_configure_window *window){
    if (gtk_toggle_button_get_active(self)==1){
        gtk_widget_set_sensitive(window->FileAmountCombo,1);
        gtk_widget_set_sensitive(window->FileAmountSpin,1);
        if (gtk_combo_box_get_active(GTK_COMBO_BOX(window->FileAmountCombo))==2){
            gtk_widget_set_sensitive(window->FileAmountButton,1);
            gtk_widget_set_sensitive(window->FileAmountEntry,1);
        }
    } else {
            gtk_widget_set_sensitive(window->FileAmountButton,0);
            gtk_widget_set_sensitive(window->FileAmountEntry,0);
            gtk_widget_set_sensitive(window->FileAmountSpin,0);
            gtk_widget_set_sensitive(window->FileAmountCombo,0);

    }
}

void on_rules_accept(GtkWidget *, dictionary *dict){
    GtkEntry *entry = yon_dictionary_get_data(dict->first,GtkEntry*);
    logrotate_rules_window *window = yon_dictionary_get_data(dict->first->next,logrotate_rules_window*);
    int owner_read = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UserReadCheck));
    int owner_write = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UserWriteCheck));
    int owner_exec = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UserExecuteCheck));

    int group_read = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->GroupReadCheck));
    int group_write = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->GroupWriteCheck));
    int group_exec = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->GroupExecuteCheck));

    int public_read = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->PublicReadCheck));
    int public_write = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->PublicWriteCheck));
    int public_exec = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->PublicExecuteCheck));
    int owner = owner_read&&owner_write&&owner_exec? 7 : \
                owner_read&&!owner_write&&!owner_exec ? 4 : \
                !owner_read&&owner_write&&!owner_exec ? 2 : \
                !owner_read&&!owner_write&&owner_exec ? 1 : \
                owner_read&&owner_write&&!owner_exec ? 6 : \
                owner_read&&!owner_write&&owner_exec ? 5 : \
                !owner_read&&owner_write&&owner_exec ? 3 : \
                0;
    int group = group_read&&group_write&&group_exec? 7 : \
                group_read&&!group_write&&!group_exec ? 4 : \
                !group_read&&group_write&&!group_exec ? 2 : \
                !group_read&&!group_write&&group_exec ? 1 : \
                group_read&&group_write&&!group_exec ? 6 : \
                group_read&&!group_write&&group_exec ? 5 : \
                !group_read&&group_write&&group_exec ? 3 : \
                0;
    int public = public_read&&public_write&&public_exec? 7 : \
                public_read&&!public_write&&!public_exec ? 4 : \
                !public_read&&public_write&&!public_exec ? 2 : \
                !public_read&&!public_write&&public_exec ? 1 : \
                public_read&&public_write&&!public_exec ? 6 : \
                public_read&&!public_write&&public_exec ? 5 : \
                !public_read&&public_write&&public_exec ? 3 : \
                0;
    gtk_entry_set_text(entry,yon_char_unite(yon_char_from_int(owner),yon_char_from_int(group),yon_char_from_int(public),NULL));
    on_subwindow_close(window->Window);
}

void on_rules_update(GtkWidget *, logrotate_rules_window *window){
    int owner_read = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UserReadCheck));
    int owner_write = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UserWriteCheck));
    int owner_exec = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UserExecuteCheck));

    int group_read = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->GroupReadCheck));
    int group_write = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->GroupWriteCheck));
    int group_exec = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->GroupExecuteCheck));

    int public_read = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->PublicReadCheck));
    int public_write = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->PublicWriteCheck));
    int public_exec = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->PublicExecuteCheck));
    int owner = owner_read&&owner_write&&owner_exec? 7 : \
                owner_read&&!owner_write&&!owner_exec ? 4 : \
                !owner_read&&owner_write&&!owner_exec ? 2 : \
                !owner_read&&!owner_write&&owner_exec ? 1 : \
                owner_read&&owner_write&&!owner_exec ? 6 : \
                owner_read&&!owner_write&&owner_exec ? 5 : \
                !owner_read&&owner_write&&owner_exec ? 3 : \
                0;
    int group = group_read&&group_write&&group_exec? 7 : \
                group_read&&!group_write&&!group_exec ? 4 : \
                !group_read&&group_write&&!group_exec ? 2 : \
                !group_read&&!group_write&&group_exec ? 1 : \
                group_read&&group_write&&!group_exec ? 6 : \
                group_read&&!group_write&&group_exec ? 5 : \
                !group_read&&group_write&&group_exec ? 3 : \
                0;
    int public = public_read&&public_write&&public_exec? 7 : \
                public_read&&!public_write&&!public_exec ? 4 : \
                !public_read&&public_write&&!public_exec ? 2 : \
                !public_read&&!public_write&&public_exec ? 1 : \
                public_read&&public_write&&!public_exec ? 6 : \
                public_read&&!public_write&&public_exec ? 5 : \
                !public_read&&public_write&&public_exec ? 3 : \
                0;
    gtk_entry_set_text(GTK_ENTRY(window->FinalEntry),yon_char_unite(yon_char_from_int(owner),yon_char_from_int(group),yon_char_from_int(public),NULL));
}

void on_rules_open(GtkWidget *,GtkEntry *entry){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_rules_path);
    logrotate_rules_window *window = malloc(sizeof(logrotate_rules_window));
    window->Window=yon_gtk_builder_get_widget(builder,"MainWindow");
    window->UserReadCheck=yon_gtk_builder_get_widget(builder,"UserReadCheck");
    window->UserWriteCheck=yon_gtk_builder_get_widget(builder,"UserWriteCheck");
    window->UserExecuteCheck=yon_gtk_builder_get_widget(builder,"UserExecuteCheck");
    window->GroupReadCheck=yon_gtk_builder_get_widget(builder,"GroupReadCheck");
    window->GroupWriteCheck=yon_gtk_builder_get_widget(builder,"GroupWriteCheck");
    window->GroupExecuteCheck=yon_gtk_builder_get_widget(builder,"GroupExecuteCheck");
    window->PublicReadCheck=yon_gtk_builder_get_widget(builder,"PublicReadCheck");
    window->PublicWriteCheck=yon_gtk_builder_get_widget(builder,"PublicWriteCheck");
    window->PublicExecuteCheck=yon_gtk_builder_get_widget(builder,"PublicExecuteCheck");
    window->FinalEntry=yon_gtk_builder_get_widget(builder,"FinalEntry");
    window->CancelButton=yon_gtk_builder_get_widget(builder,"CancelButton");
    window->SaveButton=yon_gtk_builder_get_widget(builder,"SaveButton");
    yon_window_config_custom_window_set(GTK_WINDOW(window->Window),"RulesWindow");
    gtk_window_set_title(GTK_WINDOW(window->Window),RULES_TITLE_LABEL);
    dictionary *dict = NULL;
    yon_dictionary_add_or_create_if_exists_with_data(dict,"entry",entry);
    yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->SaveButton),"clicked",G_CALLBACK(on_rules_accept),dict);

    g_signal_connect(G_OBJECT(window->UserReadCheck),"toggled",G_CALLBACK(on_rules_update),window);
    g_signal_connect(G_OBJECT(window->UserWriteCheck),"toggled",G_CALLBACK(on_rules_update),window);
    g_signal_connect(G_OBJECT(window->UserExecuteCheck),"toggled",G_CALLBACK(on_rules_update),window);

    g_signal_connect(G_OBJECT(window->GroupReadCheck),"toggled",G_CALLBACK(on_rules_update),window);
    g_signal_connect(G_OBJECT(window->GroupWriteCheck),"toggled",G_CALLBACK(on_rules_update),window);
    g_signal_connect(G_OBJECT(window->GroupExecuteCheck),"toggled",G_CALLBACK(on_rules_update),window);

    g_signal_connect(G_OBJECT(window->PublicReadCheck),"toggled",G_CALLBACK(on_rules_update),window);
    g_signal_connect(G_OBJECT(window->PublicWriteCheck),"toggled",G_CALLBACK(on_rules_update),window);
    g_signal_connect(G_OBJECT(window->PublicExecuteCheck),"toggled",G_CALLBACK(on_rules_update),window);
    gtk_widget_show(window->Window);
}

void on_filechooser_mode_switch(GtkWidget *, logrotate_filechooser_window *window){
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->FileOrDirCheck))){
        gtk_file_chooser_set_action(GTK_FILE_CHOOSER(window->FileChooser),GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
    }
        gtk_file_chooser_set_action(GTK_FILE_CHOOSER(window->FileChooser),GTK_FILE_CHOOSER_ACTION_OPEN);
}

void on_filechooser_save(GtkWidget *, dictionary *dict){
    logrotate_configure_window *window = yon_dictionary_get_data(dict->first,logrotate_configure_window*);
    logrotate_filechooser_window *dialog = yon_dictionary_get_data(dict->first->next,logrotate_filechooser_window*);
    char *filename = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->FileOrDirCheck))?gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog->FileChooser)):gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog->FileChooser));
    gtk_entry_set_text(GTK_ENTRY(window->FileAmountEntry),filename);
    on_subwindow_close(dialog->FileChooser);
}

void on_filechooser_open(GtkWidget *, logrotate_configure_window *window){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_filechooser_path);
    logrotate_filechooser_window *dialog = malloc(sizeof(logrotate_filechooser_window));
    dialog->FileChooser = yon_gtk_builder_get_widget(builder,"FileChooser");
    dialog->FileOrDirCheck = yon_gtk_builder_get_widget(builder,"FileOrDirCheck");
    dialog->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    dialog->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    yon_window_config_custom_window_set(GTK_WINDOW(dialog->FileChooser),"FileDialog");
    yon_gtk_window_setup(GTK_WINDOW(dialog->FileChooser),GTK_WINDOW(window->MainWindow),TITLE_LABEL,icon_path,"FileChooser");

    dictionary *dict = NULL;
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->FileOrDirCheck),1);
    gtk_widget_hide(dialog->FileOrDirCheck);
    yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
    yon_dictionary_add_or_create_if_exists_with_data(dict,"dialog",dialog);
    g_signal_connect(G_OBJECT(dialog->FileOrDirCheck),"toggled",G_CALLBACK(on_filechooser_mode_switch),dialog);
    g_signal_connect(G_OBJECT(dialog->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(dialog->AcceptButton),"clicked",G_CALLBACK(on_filechooser_save),dict);
    gtk_widget_show(dialog->FileChooser);
}

void on_logrotate_period_check_toggled(GtkToggleButton *self,logrotate_configure_window *window){
    if (gtk_toggle_button_get_active(self)){
        if (gtk_combo_box_get_active(GTK_COMBO_BOX(window->RotationPeriodMainCombo))==2) gtk_widget_set_sensitive(window->RotationPeriodAdditionalCombo,1);
        else gtk_widget_set_sensitive(window->RotationPeriodAdditionalCombo,0);
        gtk_widget_set_sensitive(window->RotationPeriodMainCombo,1);
    } else {
        gtk_widget_set_sensitive(window->RotationPeriodMainCombo,0);
        gtk_widget_set_sensitive(window->RotationPeriodAdditionalCombo,0);
    }
}

void on_logrotate_paths_cursor_changed(GtkWidget *, logrotate_configure_window *window){
    GtkTreeModel *model = GTK_TREE_MODEL(window->list);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->LogPathsTree)),&model,&iter)){
        gtk_widget_set_sensitive(window->LogPathEditButton,1);
        gtk_widget_set_sensitive(window->LogPathRemoveButton,1);
    } else {
        gtk_widget_set_sensitive(window->LogPathEditButton,0);
        gtk_widget_set_sensitive(window->LogPathRemoveButton,0);

    }
}

int on_logrotate_check_repeats(GtkWidget *, logrotate_configure_window *window){
    GtkTreeIter iter,*itar;
    GtkTreeModel *model = GTK_TREE_MODEL(window->list);
    char *chpath=NULL;
    char *path=NULL;
    int valid = gtk_tree_model_get_iter_first(model,&iter);
    itar=gtk_tree_iter_copy(&iter);
    for (;valid;valid = gtk_tree_model_iter_next(model,&iter)){
        gtk_tree_model_get(model,&iter,0,&chpath,-1);
        int valid1 = gtk_tree_model_get_iter_first(model,itar);
        for (;valid1;valid1=gtk_tree_model_iter_next(model,itar)){
            GtkTreePath *path_iter = gtk_tree_model_get_path(model,&iter);
            GtkTreePath *path_itar = gtk_tree_model_get_path(model,itar);
            if (gtk_tree_path_compare(path_iter,path_itar)){
                gtk_tree_model_get(model,itar,0,&path,-1);
                if (!strcmp(path,chpath)){
                    yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),REPEAT_PATHS_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                    yon_ubl_status_list_store_highlight_incorrect(window->list,itar);
                    GtkTreeIter *ittr = gtk_tree_iter_copy(&iter);
                    yon_ubl_status_list_store_highlight_incorrect(window->list,ittr);
                    
                    return 1;
                }
                free(path);
            }
            gtk_tree_path_free(path_iter);
            gtk_tree_path_free(path_itar);
        
        }
    
    }
    return 0;
}

void on_logrotate_save(GtkWidget *, dictionary *dict){
    logrotate_window *dialog = yon_dictionary_get_data(dict->first,logrotate_window*);
    logrotate_configure_window *window = yon_dictionary_get_data(dict->first->next,logrotate_configure_window*);
    char *log_name="";
    char *paths="";
    char *period="";
    char *size="";
    char *rotate_count="";
    char *olddir="";
    char *su="";
    char *missingok="";
    char *ifempty="";
    char *notifempty="";
    char *minage="";
    char *maxage="";
    char *minsize="";
    char *maxsize="";
    char *create="";
    char *copytruncate="";
    char *compress="";
    char *delaycompress="";
    char *extension="";
    char *dateext="";
    char *start="";
    char *mail="";
    char *mailfirst="";
    char *sharedscripts="";
    char *firstaction="";
    char *lastaction="";
    char *prerotate="";
    char *postrotate="";
    char *preremove="";
    char *endscript="";

    GtkTreeIter iter;
    int matched=0;
    int path_amount=0;
    config_str found_paths_list=NULL;
    int found_paths=0;
    char *cur_check_paths;
    log_name = (char*)gtk_entry_get_text(GTK_ENTRY(window->LogNameEntry));
    if (!strcmp(log_name,"")){
        yon_ubl_status_highlight_incorrect(window->LogNameEntry);
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        return;
    }

    int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(main_config.logrotate_list),&iter);
    for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(main_config.logrotate_list),&iter)){
        gtk_tree_model_get(GTK_TREE_MODEL(main_config.logrotate_list),&iter,1,&cur_check_paths,-1);
        if (!found_paths_list) found_paths_list = yon_char_parsed_new(&found_paths,cur_check_paths,NULL);
        else found_paths_list = yon_char_parsed_append(found_paths_list,&found_paths,cur_check_paths);
    }
    valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(window->list),&iter);
    if (!valid) {
        yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(window->LogPathsTree));
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        return;
    }
    if (on_logrotate_check_repeats(NULL,window)==1) return;
    for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&iter)){
        char *cur_path;
        gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,0,&cur_path,-1);
        for (int i=0;i<found_paths;i++){
            if (!strcmp(cur_path,found_paths_list[i])) matched++;
        }
        path_amount++;
        paths = yon_char_unite(paths,strcmp(paths,"") ? "," : "", cur_path,NULL);
    }
    if(!window->paths) {
        if (matched==path_amount) {
            yon_ubl_status_highlight_incorrect(gtk_widget_get_parent(window->LogPathsTree));
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),REPEATED_PATHS_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            return;
        }
    }
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->RotationPeriodCheck))){
        int active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->RotationPeriodMainCombo));
        switch (active){
            case 0: period = "hourly";
                break;
            case 1: period = "daily";
                break;
            case 2: period = "weekly";
                int weekday = gtk_combo_box_get_active(GTK_COMBO_BOX(window->RotationPeriodAdditionalCombo));
                weekday++;
                if (weekday==7) weekday=0;
                period = yon_char_unite(period," ",yon_char_from_int(weekday),NULL);
                break;
            case 3: period = "monthly";
                break;
            case 4: period = "yearly";
                break;
        }
    }

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->JournalMaxSizeCheck))){
        int last = gtk_combo_box_get_active(GTK_COMBO_BOX(window->JournalMaxSizeCombo));
        long maxsize = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->JournalMaxSizeSpin));
        size = yon_char_unite("size ",yon_char_from_long(maxsize),yon_size_get_letter_from_int(last),NULL);
    }

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->FileAmountCheck))){
        long fileamount = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->FileAmountSpin));
        int active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->FileAmountCombo));
        rotate_count = yon_char_append("rotate ",yon_char_from_long(fileamount));
        if (active==2){
            const char *text = gtk_entry_get_text(GTK_ENTRY(window->FileAmountEntry));
            if (yon_char_is_empty(text)){
                yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                yon_ubl_status_highlight_incorrect(window->FileAmountEntry);
                return;
            }
            olddir=yon_char_unite("olddir ",text,NULL);
        }
    }

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->RotationCheck))){
        char *user_rotate = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->RotationAtUserCombo));
        char *group_rotate = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->RotationAtGroupCombo));
        if (!yon_char_is_empty(user_rotate)&&!yon_char_is_empty(group_rotate)){
            if (gtk_combo_box_get_active(GTK_COMBO_BOX(window->RotationAtUserCombo))==0) user_rotate = "root";
            if (gtk_combo_box_get_active(GTK_COMBO_BOX(window->RotationAtGroupCombo))==0) group_rotate = "root";  
            su = yon_char_unite("su ",user_rotate," ",group_rotate,NULL);
        }
    } else su ="";

    int error_processing = gtk_combo_box_get_active(GTK_COMBO_BOX(window->ErrorProcessingCombo));
    if (error_processing==0|| error_processing==1) { 
        error_processing=1;
        missingok = "missingok";
    } else {
        missingok = "nomissingok";
    }

    int empty_journal = gtk_combo_box_get_active(GTK_COMBO_BOX(window->JournalEmptyCombo));
    if (empty_journal==0||empty_journal==1) {
        empty_journal = 1;
        ifempty = "ifempty";
    }

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->DoNotRotateYoungerCheck))){
        long younger = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->DoNotRotateYoungerSpin));
        minage = yon_char_append("minage ",yon_char_from_long(younger));
    }

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->DeleteOlderCheck))){
        long older = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->DeleteOlderSpin));
        maxage = yon_char_append("maxage ", yon_char_from_long(older));
    }

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->RotateSizeMaxAfterTimeCheck))){
        long value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->RotateSizeMaxAfterTimeSpin));
        int last = gtk_combo_box_get_active(GTK_COMBO_BOX(window->RotateSizeMaxAfterTimeCombo));
        minsize = yon_char_unite("minsize ",yon_char_from_long(value),yon_size_get_letter_from_int(last),NULL);
    }

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->RotateSizeMaxBeforeTimeCheck))){
        long value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->RotateSizeMaxBeforeTimeSpin));
        int last = gtk_combo_box_get_active(GTK_COMBO_BOX(window->RotateSizeMaxBeforeTimeCombo));
        maxsize = yon_char_unite("maxsize ", yon_char_from_long(value),yon_size_get_letter_from_int(last),NULL);
    }

    copytruncate = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->CutCheck)) ? "copytruncate" : "";

    int compression_active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->CompressionCombo));
    if (compression_active){
        switch (compression_active){
            case 1: compress = "compress";
                break;
            case 2: compress = "nocompress";
                break;
        }
    }
    
    int compression_delay_active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->QueueCombo));
    if (compression_delay_active){
        switch (compression_delay_active){
            case 1: delaycompress = "delaycompress";
                break;
            case 2: delaycompress = "nodelaycompress";
                break;
        }
    }

    int original_active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->SaveOriginalCombo));
    if (original_active==1){
                char *ext_file = (char*)gtk_entry_get_text(GTK_ENTRY(window->SaveOriginalEntry));
                if (yon_char_is_empty(ext_file)){
                    yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                    yon_ubl_status_highlight_incorrect(window->SaveOriginalEntry);
                    return;
                }
                extension = yon_char_append("extension ",ext_file);
    }

    if (gtk_combo_box_get_active(GTK_COMBO_BOX(window->AddDateCombo))==1){
        dateext = "dateext";
    }
    
    if (gtk_combo_box_get_active(GTK_COMBO_BOX(window->OldNumberCombo))==1){
        long value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->OldNumberEntry));
        if (value>0)
            start = yon_char_append("start ",yon_char_from_long(value));
    }

    int email_active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->EmailCombo));
    if (email_active){
        switch(email_active){
            case 1:
                char *adress = (char*)gtk_entry_get_text(GTK_ENTRY(window->EmailEntry));
                char *error = yon_gtk_entry_check_restricted(GTK_ENTRY(window->EmailEntry));
                if (!yon_char_is_empty(error)){
                    yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),error,5,BACKGROUND_IMAGE_FAIL_TYPE);
                    yon_ubl_status_highlight_incorrect(window->EmailEntry);
                    return;
                }
                if (!yon_char_is_empty(adress)){
                    mail=yon_char_append("mail ", adress);
                }
                    int contents_active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->EmailContentsCombo));
                    if (contents_active==1)
                        mailfirst = "mailfirst";
                    else if (contents_active==2)
                        mailfirst = "maillast";
                break;
            case 2: mail = "nomail";
                break;
        }
    }

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->SharedScenariosCheck))){
        char *before_once = (char*)gtk_entry_get_text(GTK_ENTRY(window->CommandBeforeOnceEntry));
        char *after_once = (char*)gtk_entry_get_text(GTK_ENTRY(window->CommandAfterOnceEntry));
        char *before = (char*)gtk_entry_get_text(GTK_ENTRY(window->CommandBeforeEntry));
        char *after = (char*)gtk_entry_get_text(GTK_ENTRY(window->CommandAfterEntry));
        char *endwork = (char*)gtk_entry_get_text(GTK_ENTRY(window->EndworkCommandEntry));

        if (!check_command_is_legit(before_once)){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),COMMAND_INVALID_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(window->CommandBeforeOnceEntry);
            return;
        }
        if (!check_command_is_legit(after_once)){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),COMMAND_INVALID_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(window->CommandAfterOnceEntry);
            return;
        }
        if (!check_command_is_legit(before)){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),COMMAND_INVALID_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(window->CommandBeforeEntry);
            return;
        }
        if (!check_command_is_legit(after)){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),COMMAND_INVALID_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(window->CommandAfterEntry);
            return;
        }
        if (!check_command_is_legit(endwork)){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),COMMAND_INVALID_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(window->EndworkCommandEntry);
            return;
        }
        sharedscripts = "sharedscripts";
        if (!yon_char_is_empty(before_once)) {
            firstaction = yon_char_append("firstaction,",before_once);
        }
        if (!yon_char_is_empty(after_once)) {
            lastaction = yon_char_append("lastaction,",after_once);
        }
        if (!yon_char_is_empty(before)) {
            prerotate = yon_char_append("prerotate,",before);
        }
        if (!yon_char_is_empty(after)) {
            postrotate = yon_char_append("postrotate,",after);
        }
        if (!yon_char_is_empty(endwork)){
            preremove = yon_char_append("preremove,",endwork);
        }
        if (!yon_char_is_empty(firstaction)||!yon_char_is_empty(after_once)||!yon_char_is_empty(before)||!yon_char_is_empty(after)) 
            endscript = "endscript";
    }
    int create_active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->CreateLogCombo));
    if (create_active){
        if (create_active==1){
            create = "nocreate";
        } else if (create_active==2){
            char *rule = (char*)gtk_entry_get_text(GTK_ENTRY(window->CreateLogEntry));
            char *rule_user = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->CreateLogUserCombo));
            char *rule_group = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->CreateGroupCombo));
            if (yon_char_is_empty(rule)){
                yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                yon_ubl_status_highlight_incorrect(window->CreateLogEntry);
                return;
            }
            if (yon_char_is_empty(rule_user)){
                yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                yon_ubl_status_highlight_incorrect(window->CreateLogUserCombo);
                return;
            }
            if (yon_char_is_empty(rule_group)){
                yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                yon_ubl_status_highlight_incorrect(window->CreateGroupCombo);
                return;
            }
            create = yon_char_unite("create ", rule," ",rule_user," ",rule_group,NULL);
        } else if (create_active==3){
            create = "nocopytruncate";
        }
    }

    char *commandline = yon_char_unite(period,
    yon_add_space_if_exists(size),
    yon_add_space_if_exists(rotate_count),
    yon_add_space_if_exists(olddir),
    yon_add_space_if_exists(su),
    yon_add_space_if_exists(missingok),
    yon_add_space_if_exists(ifempty),
    yon_add_space_if_exists(notifempty),
    yon_add_space_if_exists(minage),
    yon_add_space_if_exists(maxage),
    yon_add_space_if_exists(minsize),
    yon_add_space_if_exists(maxsize),
    yon_add_space_if_exists(create),
    yon_add_space_if_exists(copytruncate),
    yon_add_space_if_exists(compress),
    yon_add_space_if_exists(delaycompress),
    yon_add_space_if_exists(extension),
    yon_add_space_if_exists(dateext),
    yon_add_space_if_exists(start),
    yon_add_space_if_exists(mail),
    yon_add_space_if_exists(mailfirst),
    yon_add_space_if_exists(sharedscripts),
    yon_add_space_if_exists(firstaction),
    yon_add_space_if_exists(lastaction),
    yon_add_space_if_exists(prerotate),
    yon_add_space_if_exists(postrotate),
    yon_add_space_if_exists(preremove),
    yon_add_space_if_exists(endscript),
    NULL);
    if (commandline[0]==','){
        free(yon_char_divide_search(commandline,",",-1));
    }
    if(!yon_char_is_empty(paths)){
        if(window->paths){
            yon_config_remove_by_key(window->paths);
            yon_window_config_erase_custom_parameter(window->paths,"description");
        }
        GtkTreeIter iter;
        paths = yon_char_replace(paths,"\n",",");
        yon_config_register(LOGROTATE(log_name),LOGROTATE_parameter_command(log_name),yon_char_unite(paths,":",commandline,NULL));
        yon_window_config_add_instant_parameter(paths,"description",yon_char_new((char*)gtk_entry_get_text(GTK_ENTRY(window->LogDescriptionEntry))),YON_TYPE_STRING);
        yon_window_config_add_instant_parameter(paths,"render_name",yon_char_new((char*)gtk_entry_get_text(GTK_ENTRY(window->LogNameEntry))),YON_TYPE_STRING);
        GtkTreeModel *model = GTK_TREE_MODEL(main_config.logrotate_list);
        if (!dialog||!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->MainTree)),&model,&iter)){
            gtk_list_store_append(main_config.logrotate_list,&iter);
        }
        paths=yon_char_replace(paths,",","\n");
        gtk_list_store_set(main_config.logrotate_list,&iter,0,log_name,1,paths,2,gtk_entry_get_text(GTK_ENTRY(window->LogDescriptionEntry)),-1);
        on_subwindow_close(window->MainWindow);
    }
}

void on_rules_changed(GtkEditable *editable, const gchar *text, gint length, gint *position){
gchar *new_text = g_new(gchar, length + 1);
    gint i, j = 0;

    for (i = 0; i < length; i++) {
        if (text[i]>'0'&&text[i]<'7') {
            new_text[j] = text[i];
            j++;
        }
    }
    if (j < length) {
        g_signal_handlers_block_by_func(editable, G_CALLBACK(on_rules_changed), NULL);
        gtk_editable_insert_text(editable, new_text, j, position);
        g_signal_handlers_unblock_by_func(editable, G_CALLBACK(on_rules_changed), NULL);
        g_signal_stop_emission_by_name(editable, "insert-text");
    }

    g_free(new_text);
}

logrotate_configure_window *yon_logrotate_window_new(char *paths, logrotate_window *dialog, char *log_name){
    logrotate_configure_window *window = malloc(sizeof(logrotate_configure_window));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_logrotate_path);
    window->MainWindow=yon_gtk_builder_get_widget(builder,"MainWindow");
    window->CancelButton=yon_gtk_builder_get_widget(builder,"CancelButton");
    window->SaveButton=yon_gtk_builder_get_widget(builder,"SaveButton");
    window->LogNameEntry=yon_gtk_builder_get_widget(builder,"LogNameEntry");
    window->LogDescriptionEntry=yon_gtk_builder_get_widget(builder,"LogDescriptionEntry");
    window->RotationPeriodCheck=yon_gtk_builder_get_widget(builder,"RotationPeriodCheck");
    window->RotationPeriodMainCombo=yon_gtk_builder_get_widget(builder,"RotationPeriodMainCombo");
    window->RotationPeriodAdditionalCombo=yon_gtk_builder_get_widget(builder,"RotationPeriodAdditionalCombo");
    window->JournalMaxSizeCheck=yon_gtk_builder_get_widget(builder,"JournalMaxSizeCheck");
    window->JournalMaxSizeSpin=yon_gtk_builder_get_widget(builder,"JournalMaxSizeSpin");
    window->JournalMaxSizeCombo=yon_gtk_builder_get_widget(builder,"JournalMaxSizeCombo");
    window->FileAmountCheck=yon_gtk_builder_get_widget(builder,"FileAmountCheck");
    window->FileAmountSpin=yon_gtk_builder_get_widget(builder,"FileAmountSpin");
    window->FileAmountCombo=yon_gtk_builder_get_widget(builder,"FileAmountCombo");
    window->FileAmountEntry=yon_gtk_builder_get_widget(builder,"FileAmountEntry");
    window->FileAmountButton=yon_gtk_builder_get_widget(builder,"FileAmountButton");
    window->RotationCheck=yon_gtk_builder_get_widget(builder,"RotationCheck");
    window->RotationAtUserCombo=yon_gtk_builder_get_widget(builder,"RotationAtUserCombo");
    window->RotationAtGroupCombo=yon_gtk_builder_get_widget(builder,"RotationAtGroupCombo");
    window->ErrorProcessingCombo=yon_gtk_builder_get_widget(builder,"ErrorProcessingCombo");
    window->JournalEmptyCombo=yon_gtk_builder_get_widget(builder,"JournalEmptyCombo");
    window->DoNotRotateYoungerCheck=yon_gtk_builder_get_widget(builder,"DoNotRotateYoungerCheck");
    window->DoNotRotateYoungerSpin=yon_gtk_builder_get_widget(builder,"DoNotRotateYoungerSpin");
    window->DeleteOlderCheck=yon_gtk_builder_get_widget(builder,"DeleteOlderCheck");
    window->DeleteOlderSpin=yon_gtk_builder_get_widget(builder,"DeleteOlderSpin");
    window->RotateSizeMaxAfterTimeCheck=yon_gtk_builder_get_widget(builder,"RotateSizeMaxAfterTimeCheck");
    window->RotateSizeMaxAfterTimeSpin=yon_gtk_builder_get_widget(builder,"RotateSizeMaxAfterTimeSpin");
    window->RotateSizeMaxAfterTimeCombo=yon_gtk_builder_get_widget(builder,"RotateSizeMaxAfterTimeCombo");
    window->RotateSizeMaxBeforeTimeCheck=yon_gtk_builder_get_widget(builder,"RotateSizeMaxBeforeTimeCheck");
    window->RotateSizeMaxBeforeTimeSpin=yon_gtk_builder_get_widget(builder,"RotateSizeMaxBeforeTimeSpin");
    window->RotateSizeMaxBeforeTimeCombo=yon_gtk_builder_get_widget(builder,"RotateSizeMaxBeforeTimeCombo");
    window->CreateLogCombo=yon_gtk_builder_get_widget(builder,"CreateLogCombo");
    window->CreateLogUserCombo=yon_gtk_builder_get_widget(builder,"CreateLogUserCombo");
    window->CreateGroupCombo=yon_gtk_builder_get_widget(builder,"CreateGroupCombo");
    window->CreateLogEntry=yon_gtk_builder_get_widget(builder,"CreateLogEntry");
    window->CreateLogButton=yon_gtk_builder_get_widget(builder,"CreateLogButton");
    window->CutCheck=yon_gtk_builder_get_widget(builder,"CutCheck");
    window->CompressionCombo=yon_gtk_builder_get_widget(builder,"CompressionCombo");
    window->QueueCombo=yon_gtk_builder_get_widget(builder,"QueueCombo");
    window->SaveOriginalCombo=yon_gtk_builder_get_widget(builder,"SaveOriginalCombo");
    window->SaveOriginalEntry=yon_gtk_builder_get_widget(builder,"SaveOriginalEntry");
    window->AddDateCombo=yon_gtk_builder_get_widget(builder,"AddDateCombo");
    window->OldNumberCombo=yon_gtk_builder_get_widget(builder,"OldNumberCombo");
    window->OldNumberEntry=yon_gtk_builder_get_widget(builder,"OldNumberEntry");
    window->EmailCombo=yon_gtk_builder_get_widget(builder,"EmailCombo");
    window->EmailEntry=yon_gtk_builder_get_widget(builder,"EmailEntry");
    window->EmailContentsCombo=yon_gtk_builder_get_widget(builder,"EmailContentsCombo");
    window->ManualInputEntry=yon_gtk_builder_get_widget(builder,"ManualInputEntry");
    window->LogSizeLabel=yon_gtk_builder_get_widget(builder,"LogSizeLabel");
    window->headerTopic=yon_gtk_builder_get_widget(builder,"headerTopic");
    window->LogPathsTree=yon_gtk_builder_get_widget(builder,"LogPathsTree");
    window->LogPathEditButton=yon_gtk_builder_get_widget(builder,"LogPathEditButton");
    window->LogPathAddButton=yon_gtk_builder_get_widget(builder,"LogPathAddButton");
    window->LogPathRemoveButton=yon_gtk_builder_get_widget(builder,"LogPathRemoveButton");
    window->list=GTK_LIST_STORE(gtk_builder_get_object(builder,"liststore1"));
    window->PathCell=GTK_CELL_RENDERER(gtk_builder_get_object(builder,"PathCell"));
    window->SharedScenariosCheck=yon_gtk_builder_get_widget(builder,"SharedScenariosCheck");
    window->CommandBeforeOnceEntry=yon_gtk_builder_get_widget(builder,"CommandBeforeOnceEntry");
    window->CommandAfterOnceEntry=yon_gtk_builder_get_widget(builder,"CommandAfterOnceEntry");
    window->CommandBeforeEntry=yon_gtk_builder_get_widget(builder,"CommandBeforeEntry");
    window->CommandAfterEntry=yon_gtk_builder_get_widget(builder,"CommandAfterEntry");
    window->EndworkCommandEntry=yon_gtk_builder_get_widget(builder,"EndworkCommandEntry");
    window->StatusBox=yon_gtk_builder_get_widget(builder,"StatusBox");
            g_signal_connect(G_OBJECT(window->CreateLogEntry),"insert-text",G_CALLBACK(on_rules_changed),NULL);

    gtk_window_set_title(GTK_WINDOW(window->MainWindow),LOGROTATE_TITLE_LABEL);
    if (dialog)
        gtk_window_set_transient_for(GTK_WINDOW(window->MainWindow),GTK_WINDOW(dialog->Window));
    window->paths=paths;

    if (paths){
        int sz;
        float finalsize=0.0;
        char *temp_paths = yon_char_new(paths);
        config_str parsed_paths = yon_char_parse(temp_paths,&sz,"\n");
        free(temp_paths);
        temp_paths=NULL;
        for (int i=0;i<sz;i++){
            if (!access(parsed_paths[i],F_OK)) yon_char_unite(temp_paths ? yon_char_append(temp_paths," ") : "",parsed_paths[i],NULL); 
        }
        config_str logsize = yon_config_load(logrotate_size_command(temp_paths),&sz);
        for (int i=0;i<sz;i++){
            if (logsize[i][strlen(logsize[i])-1]=='\n') logsize[i][strlen(logsize[i])-1]='\0';
            finalsize+=atof(logsize[i]);
        }
        int sizenumber=0;
        finalsize = yon_size_convert_automatic(finalsize,&sizenumber);
        char *fin = yon_char_from_float(finalsize);
        gtk_label_set_text(GTK_LABEL(window->LogSizeLabel),yon_char_append(fin,yon_size_get_letter_from_int(sizenumber)));

    }
    yon_entry_set_entry_type(GTK_ENTRY(window->EmailEntry),GTK_BOX(window->StatusBox));
    yon_entry_set_entry_type(GTK_ENTRY(window->FileAmountEntry),GTK_BOX(window->StatusBox));
    yon_entry_set_entry_type(GTK_ENTRY(window->CommandBeforeOnceEntry),GTK_BOX(window->StatusBox));
    yon_entry_set_entry_type(GTK_ENTRY(window->CommandAfterOnceEntry),GTK_BOX(window->StatusBox));
    yon_entry_set_entry_type(GTK_ENTRY(window->CommandBeforeEntry),GTK_BOX(window->StatusBox));
    yon_entry_set_entry_type(GTK_ENTRY(window->CommandAfterEntry),GTK_BOX(window->StatusBox));
    yon_entry_set_entry_type(GTK_ENTRY(window->EndworkCommandEntry),GTK_BOX(window->StatusBox));
    dictionary *dict=NULL;
    yon_dictionary_add_or_create_if_exists_with_data(dict,"dialog",dialog);
    yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->SaveButton),"clicked",G_CALLBACK(on_logrotate_save),dict);
    g_signal_connect(G_OBJECT(window->PathCell),"edited",G_CALLBACK(on_path_editing_done),window);
    g_signal_connect(G_OBJECT(window->PathCell),"editing-canceled",G_CALLBACK(on_path_editing_cancel),window);
    g_signal_connect(G_OBJECT(window->LogPathAddButton),"clicked",G_CALLBACK(on_logrotate_configuration_path_filechooser_open),window);
    g_signal_connect(G_OBJECT(window->LogPathEditButton),"clicked",G_CALLBACK(on_logrotate_configuration_path_filechooser_open),window);
    g_signal_connect(G_OBJECT(window->LogPathRemoveButton),"clicked",G_CALLBACK(on_logrotate_configuration_path_remove),window);
    
    g_signal_connect(G_OBJECT(window->RotationPeriodCheck),"toggled",G_CALLBACK(on_logrotate_period_check_toggled),window);

    g_signal_connect(G_OBJECT(window->JournalMaxSizeCheck),"clicked",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->JournalMaxSizeCombo);
    g_signal_connect(G_OBJECT(window->JournalMaxSizeCheck),"clicked",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->JournalMaxSizeSpin);

    g_signal_connect(G_OBJECT(window->FileAmountCheck),"toggled",G_CALLBACK(on_file_amount_checked),window);
    
    g_signal_connect(G_OBJECT(window->DoNotRotateYoungerCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->DoNotRotateYoungerSpin);

    g_signal_connect(G_OBJECT(window->DeleteOlderCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->DeleteOlderSpin);

    g_signal_connect(G_OBJECT(window->RotateSizeMaxAfterTimeCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->RotateSizeMaxAfterTimeSpin);
    g_signal_connect(G_OBJECT(window->RotateSizeMaxAfterTimeCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->RotateSizeMaxAfterTimeCombo);

    g_signal_connect(G_OBJECT(window->RotateSizeMaxBeforeTimeCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->RotateSizeMaxBeforeTimeSpin);
    g_signal_connect(G_OBJECT(window->RotateSizeMaxBeforeTimeCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->RotateSizeMaxBeforeTimeCombo);
    g_signal_connect(G_OBJECT(window->RotationCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->RotationAtUserCombo);
    g_signal_connect(G_OBJECT(window->RotationCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->RotationAtGroupCombo);
    
    g_signal_connect(G_OBJECT(window->CreateLogCombo),"changed",G_CALLBACK(on_combo_activate_second),window->CreateLogUserCombo);
    g_signal_connect(G_OBJECT(window->CreateLogCombo),"changed",G_CALLBACK(on_combo_activate_second),window->CreateGroupCombo);
    g_signal_connect(G_OBJECT(window->CreateLogCombo),"changed",G_CALLBACK(on_combo_activate_second),window->CreateLogEntry);
    g_signal_connect(G_OBJECT(window->CreateLogCombo),"changed",G_CALLBACK(on_combo_activate_second),window->CreateLogButton);
    
    g_signal_connect(G_OBJECT(window->FileAmountCombo),"changed",G_CALLBACK(on_combo_activate_second),window->FileAmountButton);
    g_signal_connect(G_OBJECT(window->FileAmountCombo),"changed",G_CALLBACK(on_combo_activate_second),window->FileAmountEntry);
    g_signal_connect(G_OBJECT(window->CompressionCombo),"changed",G_CALLBACK(on_combo_activate_first),window->QueueCombo);
    g_signal_connect(G_OBJECT(window->SaveOriginalCombo),"changed",G_CALLBACK(on_combo_activate_first),window->SaveOriginalEntry);
    g_signal_connect(G_OBJECT(window->OldNumberCombo),"changed",G_CALLBACK(on_combo_activate_first),window->OldNumberEntry);

    g_signal_connect(G_OBJECT(window->EmailCombo),"changed",G_CALLBACK(on_combo_activate_first),window->EmailEntry);
    g_signal_connect(G_OBJECT(window->EmailCombo),"changed",G_CALLBACK(on_combo_activate_first),window->EmailContentsCombo);

    g_signal_connect(G_OBJECT(window->RotationPeriodMainCombo),"changed",G_CALLBACK(on_combo_activate_second),window->RotationPeriodAdditionalCombo);

    g_signal_connect(G_OBJECT(window->FileAmountButton),"clicked",G_CALLBACK(on_filechooser_open),window);

    g_signal_connect(G_OBJECT(window->CreateLogButton),"clicked",G_CALLBACK(on_rules_open),window->CreateLogEntry);

    g_signal_connect(G_OBJECT(window->LogPathsTree),"cursor-changed",G_CALLBACK(on_logrotate_paths_cursor_changed),window);

    int usize=0;
    config_str users = yon_ubl_get_all_users(&usize);
    for (int i=0;i<usize;i++){
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->CreateLogUserCombo),users[i],users[i]);
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->RotationAtUserCombo),users[i],users[i]);
    }

    for (int i=0;i<main_config.groups_size;i++){
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->RotationAtGroupCombo),main_config.groups[i],main_config.groups[i]);
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->CreateGroupCombo),main_config.groups[i],main_config.groups[i]);
    }

    if (paths){
        int size;
        GtkTreeIter iter;
        config_str parsed = yon_char_parse(paths,&size,"\n");
        for (int i=0; i<size;i++){
            gtk_list_store_append(window->list,&iter);
            gtk_list_store_set(window->list,&iter,0,parsed[i],-1);
        }
        yon_char_parsed_free(parsed,size);
        if (log_name){
            char *parameters = yon_config_get_by_key(LOGROTATE(log_name));
            if (parameters){
                char *firstaction = "";
                char *lastaction = "";
                char *prerotate = "";
                char *postrotate = "";
                char *preremove = "";
                char *current_script = NULL;
                if(parameters[strlen(parameters)-1]=='\"') parameters[strlen(parameters)-1]='\0';
                parameters=strstr(parameters,":");
                if (parameters) parameters++;
                parsed = yon_char_parse(parameters,&size,",");
                for (int i=0;i<size;i++){
                    if (!strcmp(parsed[i],"hourly")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationPeriodMainCombo),0);
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->RotationPeriodCheck),1);
                    } else if (!strcmp(parsed[i],"daily")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationPeriodMainCombo),1);
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->RotationPeriodCheck),1);
                    }else if (strstr(parsed[i],"weekly")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationPeriodMainCombo),2);
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->RotationPeriodCheck),1);
                        int dayofweek = atoi(strstr(parsed[i]," ")+1);
                        if (dayofweek==0) dayofweek=7;
                        dayofweek--;
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationPeriodAdditionalCombo),dayofweek);
                    }else if (!strcmp(parsed[i],"monthly")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationPeriodMainCombo),3);
                    }else if (!strcmp(parsed[i],"yearly")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationPeriodMainCombo),4);
                    }else if (strstr(parsed[i],"olddir")){
                        char *value = strstr(parsed[i]," ");
                        if (value){ value++;
                            gtk_combo_box_set_active(GTK_COMBO_BOX(window->FileAmountCombo),2);
                        } else {
                            gtk_combo_box_set_active(GTK_COMBO_BOX(window->FileAmountCombo),1);
                        }
                        gtk_entry_set_text(GTK_ENTRY(window->FileAmountEntry),value);
                    }else if (!strcmp(parsed[i],"noolddir")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->FileAmountCombo),0);
                    }else if (strstr(parsed[i],"su")){
                        char *value = strstr(parsed[i]," ");
                        if (value) value++;
                        char *nvalue = yon_char_divide_search(value," ",-1);
                        int us = yon_gtk_combo_box_text_find(window->RotationAtUserCombo,nvalue);
                        int gr = yon_gtk_combo_box_text_find(window->RotationAtGroupCombo,value);
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationAtUserCombo),us);
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationAtGroupCombo),gr);
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->RotationCheck),1);
                    }else if (!strcmp(parsed[i],"missingok")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->ErrorProcessingCombo),1);
                    }else if (!strcmp(parsed[i],"nomissingok")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->ErrorProcessingCombo),2);
                    }else if (!strcmp(parsed[i],"ifempty")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->JournalEmptyCombo),1);
                    }else if (!strcmp(parsed[i],"notifempty")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->JournalEmptyCombo),2);
                    } else if (!strcmp(parsed[i],"nocreate")) {
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->CreateLogCombo),1);
                    } else if (!strcmp(parsed[i],"copytruncate")) {
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->CutCheck),1);
                    } else if (!strcmp(parsed[i],"nocopytruncate")) {
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->CreateLogCombo),3);
                    } else if (strstr(parsed[i],"create")){
                        int parameter_size;
                        config_str parameter = yon_char_parse(parsed[i],&parameter_size," ");
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->CreateLogCombo),2);
                        gtk_combo_box_set_active_id(GTK_COMBO_BOX(window->CreateLogUserCombo),parameter[2]);
                        gtk_combo_box_set_active_id(GTK_COMBO_BOX(window->CreateGroupCombo),parameter[3]);
                        gtk_entry_set_text(GTK_ENTRY(window->CreateLogEntry),parameter[1]);
                        yon_char_parsed_free(parameter,parameter_size);

                    }else if (strstr(parsed[i],"minage")){
                        char *value = strstr(parsed[i]," ");
                        if (value) value++;
                        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->DoNotRotateYoungerSpin),atol(value));
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->DoNotRotateYoungerCheck),1);
                    }else if (strstr(parsed[i],"maxage")){
                        char *value = strstr(parsed[i]," ");
                        if (value) value++;
                        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->DeleteOlderSpin),atol(value));
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->DeleteOlderCheck),1);
                    }else if (strstr(parsed[i],"minsize")){
                        char *value = strstr(parsed[i]," ");
                        if (value) value++;
                        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->RotateSizeMaxAfterTimeSpin),atol(value));
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->RotateSizeMaxAfterTimeCheck),1);
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotateSizeMaxAfterTimeCombo),yon_size_get_int_from_letter(value[strlen(value)-1]));
                    }else if (strstr(parsed[i],"maxsize")){
                        char *value = strstr(parsed[i]," ");
                        if (value) value++;
                        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->RotateSizeMaxBeforeTimeSpin),atol(value));
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->RotateSizeMaxBeforeTimeCheck),1);
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotateSizeMaxBeforeTimeCombo),yon_size_get_int_from_letter(value[strlen(value)-1]));
                    }else if (strstr(parsed[i],"size")){
                        int sizemod = yon_size_get_int_from_letter(parsed[i][strlen(parsed[i])-1]);
                        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->JournalMaxSizeSpin),atol(strstr(parsed[i], " ")+1));
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->JournalMaxSizeCombo),sizemod);
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->JournalMaxSizeCheck),1);
                    }else if (!strcmp(parsed[i],"copytruncate")){
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->CutCheck),1);
                    }else if (!strcmp(parsed[i],"compress")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->CompressionCombo),1);
                    }else if (!strcmp(parsed[i],"nocompress")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->CompressionCombo),2);
                    }else if (!strcmp(parsed[i],"delaycompress")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->QueueCombo),1);
                        
                    }else if (!strcmp(parsed[i],"nodelaycompress")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->QueueCombo),2);
                    }else if (strstr(parsed[i],"extension")){
                        char *value = strstr(parsed[i]," ");
                        if (value) value++;
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->SaveOriginalCombo),1);
                        gtk_entry_set_text(GTK_ENTRY(window->SaveOriginalEntry),value);
                    }else if (!strcmp(parsed[i],"dateext")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->AddDateCombo),1);
                    }else if (strstr(parsed[i],"start")){
                        char *value = strstr(parsed[i]," ");
                        if (value) value++;
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->OldNumberCombo),1);
                        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->OldNumberEntry),atol(value));
                    }else if (!strcmp(parsed[i],"mailfirst")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->EmailContentsCombo),1);
                    }else if (!strcmp(parsed[i],"maillast")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->EmailContentsCombo),2);
                    }else if (!strcmp(parsed[i],"nomail")){
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->EmailCombo),2);
                    }else if (strstr(parsed[i],"mail")){
                        char *value = strstr(parsed[i]," ");
                        if (value) value++;
                        gtk_entry_set_text(GTK_ENTRY(window->EmailEntry),value);
                        gtk_combo_box_set_active(GTK_COMBO_BOX(window->EmailCombo),1);
                    }else if (!strcmp(parsed[i],"sharedscripts")){
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->SharedScenariosCheck),1);
                    }else if (strstr(parsed[i],"firstaction")){
                        current_script = "firstaction";
                    }else if (strstr(parsed[i],"lastaction")){
                        current_script = "lastaction";
                    }else if (strstr(parsed[i],"prerotate")){
                        current_script = "prerotate";
                    }else if (strstr(parsed[i],"postrotate")){
                        current_script = "postrotate";
                    }else if (!strcmp(parsed[i],"preremove")){
                        current_script = "preremove";
                    }else if (!strcmp(parsed[i],"endscript")){
                        current_script = NULL;
                    }else if (strstr(parsed[i],"rotate")){
                        char *value = strstr(parsed[i]," ")+1;
                        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->FileAmountSpin),atol(value));
                        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->FileAmountCheck),1);
                    } else {
                        if (!yon_char_is_empty(current_script)){
                            if (!strcmp(current_script,"firstaction")){
                                char *temp = yon_char_unite(firstaction, !yon_char_is_empty(firstaction)?",":"", parsed[i],NULL);
                                if(!yon_char_is_empty(firstaction)) free(firstaction);
                                firstaction = temp;
                            } else if (!strcmp(current_script,"lastaction")){
                                char *temp = yon_char_unite(lastaction, !yon_char_is_empty(lastaction)?",":"", parsed[i],NULL);
                                if(!yon_char_is_empty(lastaction)) free(lastaction);
                                lastaction = temp;

                            } else if (!strcmp(current_script,"prerotate")){
                                char *temp = yon_char_unite(prerotate, !yon_char_is_empty(prerotate)?",":"", parsed[i],NULL);
                                if(!yon_char_is_empty(prerotate)) free(prerotate);
                                prerotate = temp;

                            } else if (!strcmp(current_script,"postrotate")){
                                char *temp = yon_char_unite(postrotate, !yon_char_is_empty(postrotate)?",":"", parsed[i],NULL);
                                if(!yon_char_is_empty(postrotate)) free(postrotate);
                                postrotate = temp;

                            } else if (!strcmp(current_script,"preremove")){
                                char *temp = yon_char_unite(preremove, !yon_char_is_empty(preremove)?",":"", parsed[i],NULL);
                                if(!yon_char_is_empty(preremove)) free(preremove);
                                preremove = temp;

                            }
                        }
                    }
                }
                if (!yon_char_is_empty(firstaction)) gtk_entry_set_text(GTK_ENTRY(window->CommandBeforeOnceEntry),firstaction);
                if (!yon_char_is_empty(lastaction)) gtk_entry_set_text(GTK_ENTRY(window->CommandAfterOnceEntry),lastaction);
                if (!yon_char_is_empty(prerotate)) gtk_entry_set_text(GTK_ENTRY(window->CommandBeforeEntry),prerotate);
                if (!yon_char_is_empty(postrotate)) gtk_entry_set_text(GTK_ENTRY(window->CommandAfterEntry),postrotate);
                if (!yon_char_is_empty(preremove)) gtk_entry_set_text(GTK_ENTRY(window->EndworkCommandEntry),preremove);
            }
        }
    }
    
    return window;
}

void on_logrotate_add(GtkWidget *, logrotate_window *dialog){
    logrotate_configure_window *window = yon_logrotate_window_new(NULL,NULL,NULL);
        gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(GTK_TREE_VIEW(dialog->MainTree)));
    yon_gtk_window_setup(GTK_WINDOW(window->MainWindow),GTK_WINDOW(dialog->Window),TITLE_LABEL,icon_path,"logrotate_add_window");
    gtk_widget_show(window->MainWindow);
    gtk_widget_set_sensitive(window->LogNameEntry,1);
    gtk_widget_set_sensitive(window->LogDescriptionEntry,1);
    gtk_label_set_text(GTK_LABEL(window->LogSizeLabel),"");
}

void on_logrotate_edit_activate(GtkWidget*,GtkTreePath*,GtkTreeViewColumn*, logrotate_window *window){
    on_logrotate_edit(NULL,window);
}

void on_logrotate_edit(GtkWidget *, logrotate_window *window){
    GtkTreeModel *model = GTK_TREE_MODEL(main_config.logrotate_list);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->MainTree)),&model, &iter)){
        char *name;
        int size=0;
        char *paths;
        char *description;
        gtk_tree_model_get(gtk_tree_view_get_model(GTK_TREE_VIEW(window->MainTree)),&iter,1,&paths,-1);
        config_str path_parsed = yon_char_parse(paths,&size,"\n");
        paths=NULL;
        for (int i=0;i<size;i++){
            paths=yon_char_unite(paths ? yon_char_append(paths,","):"",path_parsed[i],NULL);
        }
        gtk_tree_model_get(model,&iter,0,&name,1,&paths,2,&description,-1);
        logrotate_configure_window *dialog = yon_logrotate_window_new(paths,window,name);
    yon_gtk_window_setup(GTK_WINDOW(dialog->MainWindow),GTK_WINDOW(window->Window),TITLE_LABEL,icon_path,"logrotate_edit_window");
        gtk_widget_show(dialog->MainWindow);
        gtk_entry_set_text(GTK_ENTRY(dialog->LogNameEntry),name ? name : "");
        gtk_entry_set_text(GTK_ENTRY(dialog->LogDescriptionEntry),description ? description : "");
        gtk_widget_set_can_focus(dialog->LogNameEntry,0);
        gtk_entry_set_has_frame(GTK_ENTRY(dialog->LogNameEntry),0);
        gtk_style_context_add_class(gtk_widget_get_style_context(dialog->LogNameEntry),"nobackground");
        gtk_style_context_add_class(gtk_widget_get_style_context(dialog->LogDescriptionEntry),"nobackground");
    }

}

void on_logrotate_remove(GtkWidget *, logrotate_window *window){
    GtkTreeModel *model = GTK_TREE_MODEL(main_config.logrotate_list);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->MainTree)),&model, &iter)){
        char *paths;
        gtk_tree_model_get(model,&iter,0,&paths,-1);
        gtk_list_store_remove(main_config.logrotate_list,&iter);
        yon_config_remove_by_key(LOGROTATE(paths));
    }
}

void on_logrotate_apps_configure_activate(GtkWidget *, GtkTreePath*, GtkTreeViewColumn*,logrotate_window *window){
    on_logrotate_apps_configure(NULL,window);
}


void on_logrotate_apps_configure(GtkWidget *, logrotate_window *window){
    GtkTreeModel *model = GTK_TREE_MODEL(main_config.logrotate_apps_list);
    GtkTreeIter iter,itar,itor;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->AppsMainTree)),&model,&iter)){
        char *paths;
        char *name;
        char *settings;
        int cur_size=0;
        int found=0;
        gtk_tree_model_get(model,&iter,0,&name,1,&paths,2,&settings,-1);
        config_str cur = yon_char_parse(paths,&cur_size,"\n");
        int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(main_config.logrotate_list),&itor);
        for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(main_config.logrotate_list),&itor)){
            char *check_paths;
            gtk_tree_model_get(GTK_TREE_MODEL(main_config.logrotate_list),&itor,1,&check_paths,-1);
            int check_size=0;
            config_str parsed_check = yon_char_parse(check_paths,&check_size,"\n");
            for (int i=0;i<cur_size;i++){
                for (int j=0;j<check_size;j++){
                    if (!strcmp(cur[i],parsed_check[j])) found ++;
                } 
            }
            if (cur_size==found) {
                yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),REPEATED_PATHS_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                return;
            }
                
        }
        gtk_list_store_append(main_config.logrotate_list,&itar);
        settings = yon_char_replace(settings,",\t",",");
        if (settings[0]==',') yon_char_divide_search(settings,",",-1);
        gtk_list_store_set(main_config.logrotate_list,&itar,0,name,1,paths,-1);
        paths = yon_char_replace(paths,"\n",",");
        yon_config_register(LOGROTATE(name),LOGROTATE_parameter_command(name),yon_char_unite(paths,":",settings,NULL));
        textdomain(template_ui_LocaleName);
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),SUCCESS_LABEL,5,BACKGROUND_IMAGE_SUCCESS_TYPE);
        textdomain(LocaleName);
    }
}

void on_log_edit_logrotate_settings(GtkWidget *, dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    add_log_window *window = yon_dictionary_get_data(dict->first->next,add_log_window*);
    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)){
        char *name;
        char *description;
        gtk_tree_model_get(model,&iter,0,&name,2,&description,-1);
        GList *list = gtk_container_get_children(GTK_CONTAINER(window->LogPathsBox));
        char *pathse=NULL;
        for (guint i=0;i<g_list_length(list);i++){
            GList *paths = gtk_container_get_children((GtkContainer*)g_list_nth_data(list,i));
           pathse=yon_char_unite(pathse ? yon_char_append(pathse,","):"",gtk_entry_get_text((GtkEntry*)g_list_nth_data(paths,1)),NULL);
        }
        logrotate_configure_window *dialog = yon_logrotate_window_new(pathse,NULL,name);
        yon_gtk_window_setup(GTK_WINDOW(dialog->MainWindow),GTK_WINDOW(window->MainWindow),LOGROTATE_TITLE_LABEL,icon_path,"LogrotateWindow");
        gtk_widget_show(dialog->MainWindow);
        gtk_entry_set_text(GTK_ENTRY(dialog->LogNameEntry),name);
        gtk_entry_set_text(GTK_ENTRY(dialog->LogDescriptionEntry),description);
        gtk_widget_set_can_focus(dialog->LogNameEntry,0);
        gtk_widget_set_can_focus(dialog->LogDescriptionEntry,0);
        gtk_entry_set_has_frame(GTK_ENTRY(dialog->LogNameEntry),0);
        gtk_entry_set_has_frame(GTK_ENTRY(dialog->LogDescriptionEntry),0);
        gtk_widget_grab_focus(dialog->LogPathAddButton);
        gtk_style_context_add_class(gtk_widget_get_style_context(dialog->LogNameEntry),"nobackground");
        gtk_style_context_add_class(gtk_widget_get_style_context(dialog->LogDescriptionEntry),"nobackground");
        
    }
}

// Logrotate tab window

void on_logrotate_tab_selection_changed(GtkWidget *, logrotate_window *window){
    GtkTreeModel *model = GTK_TREE_MODEL(main_config.logrotate_list);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->MainTree)),&model,&iter)){
        gtk_widget_set_sensitive(window->AddButton,1);
        gtk_widget_set_sensitive(window->RemoveButton,1);
        gtk_widget_set_sensitive(window->ConfigureButton,1);
    } else {
        gtk_widget_set_sensitive(window->AddButton,1);
        gtk_widget_set_sensitive(window->RemoveButton,0);
        gtk_widget_set_sensitive(window->ConfigureButton,0);
    }
}

void on_logrotate_applications_tab_selection_changed(GtkWidget *, logrotate_window *window){
    GtkTreeModel *model = GTK_TREE_MODEL(main_config.logrotate_apps_list);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->AppsMainTree)),&model,&iter)){
        gtk_widget_set_sensitive(window->AppsConfigureButton,1);
    } else {
        gtk_widget_set_sensitive(window->AppsConfigureButton,0);
    }
}

void on_logrotate_closed(GtkWidget *self,logrotate_window *window){
    gtk_widget_destroy(window->Menu->menu);
    gtk_widget_destroy(window->AppsMenu->menu);
    free(window->Menu);
    free(window->AppsMenu);
    on_subwindow_close(self);
}

void on_logrotate_tab_open(GtkWidget *, main_window *widgets){
    logrotate_window *window = malloc(sizeof(logrotate_window));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_logrotate_table_path);
    window->Window=yon_gtk_builder_get_widget(builder,"MainWindow");
    window->AppsMainTree=yon_gtk_builder_get_widget(builder,"AppsMainTree");
    window->MainTree=yon_gtk_builder_get_widget(builder,"MainTree");
    window->AppsConfigureButton=yon_gtk_builder_get_widget(builder,"AppsConfigureButton");
    window->CancelButton=yon_gtk_builder_get_widget(builder,"CancelButton");
    window->SaveButton=yon_gtk_builder_get_widget(builder,"SaveButton");
    window->ConfigureButton = yon_gtk_builder_get_widget(builder,"ConfigureButton");
    window->AddButton = yon_gtk_builder_get_widget(builder,"AddButton");
    window->RemoveButton = yon_gtk_builder_get_widget(builder,"RemoveButton");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    yon_window_config_custom_window_get(GTK_WINDOW(window->Window),"LogrotateTabWindow");
    gtk_window_set_title(GTK_WINDOW(window->Window),TABLE_TITLE_LABEL);

    gtk_window_set_transient_for(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window));

    gtk_tree_view_set_model(GTK_TREE_VIEW(window->MainTree),GTK_TREE_MODEL(main_config.logrotate_list));
    gtk_tree_view_set_model(GTK_TREE_VIEW(window->AppsMainTree),GTK_TREE_MODEL(main_config.logrotate_apps_list));

    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_logrotate_closed),window);
    g_signal_connect(G_OBJECT(window->SaveButton),"clicked",G_CALLBACK(on_logrotate_closed),window);
    g_signal_connect(G_OBJECT(window->AddButton),"clicked",G_CALLBACK(on_logrotate_add),window);
    g_signal_connect(G_OBJECT(window->RemoveButton),"clicked",G_CALLBACK(on_logrotate_remove),window);
    g_signal_connect(G_OBJECT(window->ConfigureButton),"clicked",G_CALLBACK(on_logrotate_edit),window);
    g_signal_connect(G_OBJECT(window->AppsConfigureButton),"clicked",G_CALLBACK(on_logrotate_apps_configure),window);

    g_signal_connect(G_OBJECT(window->MainTree),"cursor-changed",G_CALLBACK(on_logrotate_tab_selection_changed),window);
    g_signal_connect(G_OBJECT(window->AppsMainTree),"cursor-changed",G_CALLBACK(on_logrotate_applications_tab_selection_changed),window);
    g_signal_connect(G_OBJECT(window->MainTree),"row-activated",G_CALLBACK(on_logrotate_edit_activate),window);
    g_signal_connect(G_OBJECT(window->AppsMainTree),"row-activated",G_CALLBACK(on_logrotate_apps_configure_activate),window);

    window->Menu = yon_rmb_menu_setup(window->MainTree,NULL,NULL,
    ADD_CONFIGURATION_LABEL,add_icon_name,G_CALLBACK(on_logrotate_add),window,
    REMOVE_CONFIGURATION_LABEL,minus_icon_name,G_CALLBACK(on_logrotate_remove),window,
    CONFIGURE_CONFIGURATION_LABEL,settings_icon_name,G_CALLBACK(on_logrotate_edit),window,
    NULL);
    window->AppsMenu = yon_rmb_menu_setup(window->AppsMainTree,NULL,NULL,
    IMPORT_CONFIGURATION_LABEL,import_icon_name,G_CALLBACK(on_logrotate_apps_configure),window,
    NULL);
    gtk_widget_show(window->Window);
}

// Journald window

void yon_combo_compress_check(GtkComboBox *self, journald_window *window){
    if (gtk_combo_box_get_active(self)==3){
        gtk_widget_set_sensitive(window->LogCompressionSizeCombo,1);
        gtk_widget_set_sensitive(window->LogCompressionEntry,1);
    } else {
        gtk_widget_set_sensitive(window->LogCompressionSizeCombo,0);
        gtk_widget_set_sensitive(window->LogCompressionEntry,0);
    }
}

void on_journald_reset(GtkWidget *, journald_window *window){
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogStorageCombo),0);
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogCompressionCombo),0);
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->RedirectCombo),0);
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->LowerSizeCombo),0);
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->LowerTimeCombo),0);
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogStorageCombo),0);
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogFreeSpaceCombo),0);
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->ForwardedTypeCombo),0);
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationSizeMaxCombo),0);
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogCompressionSizeCombo),0);
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->DivideJournalFilesCombo),0);
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogSizeMaxCombo),0);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->LogSizeMaxCheck),0);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->LogIntervalCheck),0);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->LogFreeSpaceCheck),0);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->RotationSizeMaxCheck),0);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->LogIntervalAmountCheck),0);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->LogSizeMaxSpin),0);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->LogIntervalSpin),0);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->LogFreeSpaceSpin),0);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->RotationSizeMaxSpin),0);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->LogIntervalAmountSpin),0);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->LogCompressionEntry),0);
    gtk_entry_set_text(GTK_ENTRY(window->RedirectTTYEntry),"");

    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->LowerSizeEntry),0);
    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->LowerTimeEntry),0);
}

void on_journald_save(GtkWidget *, journald_window *window){
    char *storage = "";
    int storage_active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->LogStorageCombo));
    switch (storage_active){
        case 1: storage="volatile";
            break;
        case 2: storage="persistent";
            break;
        case 3: storage="auto";
            break;
        case 4: storage="none";
            break;
        
    }
    char *compress = "";
    int compress_active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->LogCompressionCombo));
    if (compress_active==1) compress = "yes";
    else if (compress_active==2) compress = "no";
    else if (compress_active==3) {
        int compress_size_active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->LogCompressionSizeCombo));
        int compress_size = atoi(gtk_entry_get_text(GTK_ENTRY(window->LogCompressionEntry)));
        compress=yon_char_from_int(compress_size);
        if (compress_size==0)
            compress_active=0;
        switch (compress_size_active){
            case 0: compress = yon_char_append(compress,"K");
                break;
            case 1: compress = yon_char_append(compress,"M");
                break;
            case 2: compress = yon_char_append(compress,"G");
                break;
        }
    }
    char *splitmode = "";
    int splitmode_active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->DivideJournalFilesCombo));
    if (splitmode_active == 1) splitmode = "uid";
    else if (splitmode_active == 2 ) splitmode = "none";

    long interval=-1;
    long amount=-1;
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->LogIntervalCheck))){
        interval = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->LogIntervalSpin)); 
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->LogIntervalAmountCheck))){
            amount = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->LogIntervalAmountSpin));
            if (amount>0)
                yon_config_register(JOURNALD("RateLimitBurst"),JOURNALD_parameter_command("RateLimitBurst"),yon_char_from_long(amount));
        }
        if (interval>0)
            yon_config_register(JOURNALD("RateLimitIntervalSec"),JOURNALD_parameter_command("RateLimitIntervalSec"),yon_char_from_long(interval));
    } else {
        yon_config_remove_by_key(JOURNALD("RateLimitBurst"));
        yon_config_remove_by_key(JOURNALD("RateLimitIntervalSec"));

    }
    
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->LogSizeMaxCheck))){
        long logssize=0;
        logssize = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->LogSizeMaxSpin));
        int log_size_mod = gtk_combo_box_get_active(GTK_COMBO_BOX(window->LogSizeMaxCombo));
        yon_config_register(JOURNALD("SystemMaxUse"),JOURNALD_parameter_command("SystemMaxUse"),yon_char_append(yon_char_from_long(logssize),yon_size_get_letter_from_int(log_size_mod)));
    } else 
        yon_config_remove_by_key(JOURNALD("SystemMaxUse"));

    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->RotationSizeMaxCheck))){
        char * rotationMaxSize;
        long maxsize = 0;
        maxsize=gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->RotationSizeMaxSpin));
        char *modif = yon_size_get_letter_from_int(gtk_combo_box_get_active(GTK_COMBO_BOX(window->RotationSizeMaxCombo)));
        rotationMaxSize = yon_char_append(yon_char_from_long(maxsize),modif);
        if (maxsize>0)
            yon_config_register(JOURNALD("SystemMaxFileSize"),JOURNALD_parameter_command("SystemMaxFileSize"),rotationMaxSize);
    } else 
        yon_config_remove_by_key(JOURNALD("SystemMaxFileSize"));

    long storagefree;
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->LogFreeSpaceCheck))){
        char *storagespace;
        storagefree = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->LogFreeSpaceSpin));
        char *modif = yon_size_get_letter_from_int(gtk_combo_box_get_active(GTK_COMBO_BOX(window->LogFreeSpaceCombo)));
        storagespace = yon_char_append(yon_char_from_long(storagefree),modif);

        if (storagefree>0)
            yon_config_register(JOURNALD("SystemKeepFree"),JOURNALD_parameter_command("SystemKeepFree"),storagespace);
    } else
        yon_config_remove_by_key(JOURNALD("SystemKeepFree"));

    int consoleforwardactive = gtk_combo_box_get_active(GTK_COMBO_BOX(window->RedirectCombo));
    char *confor=NULL;
    char *TTY=NULL;
    if (consoleforwardactive == 1){
        confor = "yes";
        TTY = yon_char_new((char*)gtk_entry_get_text(GTK_ENTRY(window->RedirectTTYEntry)));
        if (yon_char_is_empty(TTY))
            consoleforwardactive=0;
    }
    else if (consoleforwardactive == 2){
        confor = "no";
    }

    int forwardempty = gtk_combo_box_get_active(GTK_COMBO_BOX(window->ForwardedTypeCombo));
    
    if (storage_active!=0)
        yon_config_register(JOURNALD("Storage"),JOURNALD_parameter_command("Storage"),storage);
    else
        yon_config_remove_by_key(JOURNALD("Storage"));
    if (compress_active!=0)
        yon_config_register(JOURNALD("Compress"),JOURNALD_parameter_command("Compress"),compress);
    else 
        yon_config_remove_by_key(JOURNALD("Compress"));
    if (splitmode_active!=0)
        yon_config_register(JOURNALD("SplitMode"),JOURNALD_parameter_command("SplitMode"),splitmode);
    else
        yon_config_remove_by_key(JOURNALD("SplitMode"));
    if (consoleforwardactive != 0){
        yon_config_register(JOURNALD("ForwardToConsole"),JOURNALD_parameter_command("ForwardToConsole"),confor);
        yon_config_register(JOURNALD("TTYPath"),JOURNALD_parameter_command("TTYPath"),TTY);
    } else {
        yon_config_remove_by_key(JOURNALD("ForwardToConsole"));
        yon_config_remove_by_key(JOURNALD("TTYPath"));
    }
    if (forwardempty!=0)
        yon_config_register(JOURNALD("MaxLevelConsole"),JOURNALD_parameter_command("MaxLevelConsole"),yon_char_from_int(forwardempty-1));
    else 
        yon_config_remove_by_key(JOURNALD("MaxLevelConsole"));
    on_subwindow_close(window->MainWindow);
}

void on_journald_size_lower_size(GtkWidget *, journald_window *window){
    long time_long = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->LowerSizeEntry));
    if (time_long){
        char *time = yon_char_from_long(time_long);
        char *time_mod = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->LowerSizeCombo));
        char *time_full = yon_char_append(time,time_mod);
        if (!system(journald_lower_size_command(time_full))){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),yon_char_get_localised_from_lib(SUCCESS_LABEL),5,BACKGROUND_IMAGE_SUCCESS_TYPE);
        } else {
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),yon_char_get_localised_from_lib(FAIL_LABEL),5,BACKGROUND_IMAGE_SUCCESS_TYPE);
        }
    } else {
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_SUCCESS_TYPE);
        yon_ubl_status_highlight_incorrect(window->LowerSizeEntry);
    }
    yon_journald_size_update(window);
}

void on_journald_size_lower_time(GtkWidget *, journald_window *window){
    long time_long = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->LowerTimeEntry));
    if (time_long){
        char *time = yon_char_from_long(time_long);
        char *time_mod = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->LowerTimeCombo));
        char *time_full = yon_char_append(time,time_mod);
        if (!system(journald_lower_time_command(time_full))){
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),yon_char_get_localised_from_lib(SUCCESS_LABEL),5,BACKGROUND_IMAGE_SUCCESS_TYPE);
        } else {
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),yon_char_get_localised_from_lib(FAIL_LABEL),5,BACKGROUND_IMAGE_SUCCESS_TYPE);
        }
    } else {
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_SUCCESS_TYPE);
        yon_ubl_status_highlight_incorrect(window->LowerTimeEntry);
    }
    yon_journald_size_update(window);
}

journald_window *yon_journald_open(GtkWidget *, main_window *widgets){
 journald_window *window = malloc(sizeof(journald_window));
 GtkBuilder *builder = gtk_builder_new_from_resource(glade_journald_path);
    window->MainWindow=yon_gtk_builder_get_widget(builder,"MainWindow");
    window->StatusBox=yon_gtk_builder_get_widget(builder,"StatusBox");
    window->CancelButton=yon_gtk_builder_get_widget(builder,"CancelButton");
    window->SaveButton=yon_gtk_builder_get_widget(builder,"SaveButton");
    window->ResetButton=yon_gtk_builder_get_widget(builder,"ResetButton");
    window->LogNameLabel=yon_gtk_builder_get_widget(builder,"LogNameEntry");
    window->LogDescriptionLabel=yon_gtk_builder_get_widget(builder,"LogDescriptionEntry");
    window->LogStorageCombo=yon_gtk_builder_get_widget(builder,"LogStorageCombo");
    window->LogCompressionCombo=yon_gtk_builder_get_widget(builder,"LogCompressionCombo");
    window->LogCompressionEntry=yon_gtk_builder_get_widget(builder,"LogCompressionSpin");
    window->LogCompressionSizeCombo=yon_gtk_builder_get_widget(builder,"LogCompressionSizeCombo");
    window->DivideJournalFilesCombo=yon_gtk_builder_get_widget(builder,"DivideJournalFilesCombo");
    window->LogIntervalCheck=yon_gtk_builder_get_widget(builder,"LogIntervalCheck");
    window->LogIntervalSpin=yon_gtk_builder_get_widget(builder,"LogIntervalSpin");
    window->LogIntervalAmountCheck=yon_gtk_builder_get_widget(builder,"LogIntervalAmountCheck");
    window->LogIntervalAmountSpin=yon_gtk_builder_get_widget(builder,"LogIntervalAmountSpin");
    window->LogSizeMaxCheck=yon_gtk_builder_get_widget(builder,"LogSizeMaxCheck");
    window->LogSizeMaxSpin=yon_gtk_builder_get_widget(builder,"LogSizeMaxSpin");
    window->LogSizeMaxCombo=yon_gtk_builder_get_widget(builder,"LogSizeMaxCombo");
    window->RotationSizeMaxCheck=yon_gtk_builder_get_widget(builder,"RotationSizeMaxCheck");
    window->RotationSizeMaxSpin=yon_gtk_builder_get_widget(builder,"RotationSizeMaxSpin");
    window->RotationSizeMaxCombo=yon_gtk_builder_get_widget(builder,"RotationSizeMaxCombo");
    window->LogFreeSpaceCheck=yon_gtk_builder_get_widget(builder,"LogFreeSpaceCheck");
    window->LogFreeSpaceSpin=yon_gtk_builder_get_widget(builder,"LogFreeSpaceSpin");
    window->LogFreeSpaceCombo=yon_gtk_builder_get_widget(builder,"LogFreeSpaceCombo");
    window->RedirectCombo=yon_gtk_builder_get_widget(builder,"RedirectCombo");
    window->RedirectTTYEntry=yon_gtk_builder_get_widget(builder,"RedirectTTYEntry");
    window->ForwardedTypeCombo=yon_gtk_builder_get_widget(builder,"ForwardedTypeCombo");
    window->LogSizeLabel=yon_gtk_builder_get_widget(builder,"LogSizeLabel");
    window->LowerSizeEntry=yon_gtk_builder_get_widget(builder,"LowerSizeSpin");
    window->LowerSizeCombo=yon_gtk_builder_get_widget(builder,"LowerSizeCombo");
    window->LowerSizeButton=yon_gtk_builder_get_widget(builder,"LowerSizeButton");
    window->LowerTimeEntry=yon_gtk_builder_get_widget(builder,"LowerTimeSpin");
    window->LowerTimeCombo=yon_gtk_builder_get_widget(builder,"LowerTimeCombo");
    window->LowerTimeButton=yon_gtk_builder_get_widget(builder,"LowerTimeButton");
    window->headerTopic=yon_gtk_builder_get_widget(builder,"headerTopic");

    yon_window_config_custom_window_setup(GTK_WINDOW(window->MainWindow),"JournaldWindow");

    gtk_window_set_transient_for(GTK_WINDOW(window->MainWindow),GTK_WINDOW(widgets->Window));
    
    yon_journald_size_update(window);

    gtk_window_set_title(GTK_WINDOW(window->MainWindow),JOURNALD_TITLE_LABEL);

    g_signal_connect(G_OBJECT(window->LowerSizeButton),"clicked",G_CALLBACK(on_journald_size_lower_size),window);
    g_signal_connect(G_OBJECT(window->LowerTimeButton),"clicked",G_CALLBACK(on_journald_size_lower_time),window);

    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->SaveButton),"clicked",G_CALLBACK(on_journald_save),window);
    g_signal_connect(G_OBJECT(window->ResetButton),"clicked",G_CALLBACK(on_journald_reset),window);

    g_signal_connect(G_OBJECT(window->LogIntervalCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->LogIntervalSpin);
    g_signal_connect(G_OBJECT(window->LogIntervalCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->LogIntervalAmountCheck);
    g_signal_connect(G_OBJECT(window->LogIntervalAmountCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->LogIntervalAmountSpin);

    g_signal_connect(G_OBJECT(window->LogSizeMaxCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->LogSizeMaxSpin);
    g_signal_connect(G_OBJECT(window->LogSizeMaxCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->LogSizeMaxCombo);

    g_signal_connect(G_OBJECT(window->RotationSizeMaxCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->RotationSizeMaxSpin);
    g_signal_connect(G_OBJECT(window->RotationSizeMaxCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->RotationSizeMaxCombo);

    g_signal_connect(G_OBJECT(window->LogFreeSpaceCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->LogFreeSpaceSpin);
    g_signal_connect(G_OBJECT(window->LogFreeSpaceCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->LogFreeSpaceCombo);

    g_signal_connect(G_OBJECT(window->LogCompressionCombo),"changed",G_CALLBACK(yon_combo_compress_check),window);
    g_signal_connect(G_OBJECT(window->RedirectCombo),"changed",G_CALLBACK(yon_gtk_widget_set_sensitive_from_combo_box_inversed),window->RedirectTTYEntry);
    int size=0;
    config_str settings = yon_config_get_all_by_key("JOURNALD",&size);
    for (int i=0;i<size;i++){
        char *name = yon_char_divide_search(settings[i],"=",-1);
        if (!strcmp(JOURNALD("Storage"),name)){
            if (!strcmp(settings[i],"volatile")) gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogStorageCombo),1);
            else if (!strcmp(settings[i],"persistent")) gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogStorageCombo),2);
            else if (!strcmp(settings[i],"auto")) gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogStorageCombo),3);
            else if (!strcmp(settings[i],"none")) gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogStorageCombo),4);

        }else if (!strcmp(JOURNALD("Compress"),name)){
            if (!strcmp(settings[i],"yes")) gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogCompressionCombo),1);
            else if (!strcmp(settings[i],"no")) gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogCompressionCombo),2);
            else {
                gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogCompressionCombo),3);
                char last = settings[i][strlen(settings[i])-1];
                gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->LogCompressionEntry),atoi(settings[i]));
                gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogCompressionSizeCombo),yon_size_get_int_from_letter(last));
            }

        }else if (!strcmp(JOURNALD("SplitMode"),name)){
            if (!strcmp(settings[i],"uid")) gtk_combo_box_set_active(GTK_COMBO_BOX(window->DivideJournalFilesCombo),1);
            else if (!strcmp(settings[i],"none")) gtk_combo_box_set_active(GTK_COMBO_BOX(window->DivideJournalFilesCombo),2);
        }else if (!strcmp(JOURNALD("RateLimitIntervalSec"),name)){
            gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->LogIntervalSpin),atol(settings[i]));
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->LogIntervalCheck),1);
        }else if (!strcmp(JOURNALD("RateLimitBurst"),name)){
            gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->LogIntervalAmountSpin),atol(settings[i]));
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->LogIntervalAmountCheck),1);
        }else if (!strcmp(JOURNALD("SystemMaxUse"),name)){
            char last = settings[i][strlen(settings[i])-1];
            gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->LogSizeMaxSpin),atol(settings[i]));
            gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogSizeMaxCombo),yon_size_get_int_from_letter(last));
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->LogSizeMaxCheck),1);
        }else if (!strcmp(JOURNALD("SystemKeepFree"),name)){
            char last = settings[i][strlen(settings[i])-1];
            gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->LogFreeSpaceSpin),atol(settings[i]));
            gtk_combo_box_set_active(GTK_COMBO_BOX(window->LogFreeSpaceCombo),yon_size_get_int_from_letter(last));
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->LogFreeSpaceCheck),1);
        }else if (!strcmp(JOURNALD("SystemMaxFileSize"),name)){
            char last = settings[i][strlen(settings[i])-1];
            gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->RotationSizeMaxSpin),atol(settings[i]));
            gtk_combo_box_set_active(GTK_COMBO_BOX(window->RotationSizeMaxCombo),yon_size_get_int_from_letter(last));
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->RotationSizeMaxCheck),1);
        }else if (!strcmp(JOURNALD("ForwardToConsole"),name)){
            if (!strcmp(settings[i],"yes")) gtk_combo_box_set_active(GTK_COMBO_BOX(window->RedirectCombo),1);
            else if (!strcmp(settings[i],"no")) gtk_combo_box_set_active(GTK_COMBO_BOX(window->RedirectCombo),2);
        }else if (!strcmp(JOURNALD("TTYPath"),name)){
            gtk_entry_set_text(GTK_ENTRY(window->RedirectTTYEntry), settings[i]);
        }else if (!strcmp(JOURNALD("MaxLevelConsole"),name)){
            gtk_combo_box_set_active(GTK_COMBO_BOX(window->ForwardedTypeCombo),atoi(settings[i])+1);
        }
    }

    return window;
}

void on_journald_open(GtkWidget *self, main_window *widgets){
    journald_window *window = yon_journald_open(self,widgets);
    GtkTreeIter iter;
    GtkTreeModel *model;
    GtkWidget *tree_view;
    char *name, *description;
    int name_col, desc_col;
    if (self == widgets->ServicesEditButton){
        model = GTK_TREE_MODEL(widgets->ServicesList);
        tree_view = widgets->ServicesTree;
        name_col = 2;
        desc_col = 3;
    } else {
        model = GTK_TREE_MODEL(widgets->list);
        tree_view = widgets->MainTree;
        name_col = 0;
        desc_col = 2;
    }
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view)),&model,&iter)){
        gtk_tree_model_get(model,&iter,name_col,&name,desc_col,&description,-1);
        gtk_entry_set_text(GTK_ENTRY(window->LogDescriptionLabel),description);
        gtk_entry_set_text(GTK_ENTRY(window->LogNameLabel),name);
    }
    gtk_widget_show(window->MainWindow);
}

// Log adding menu

void on_log_save(GtkWidget *self, dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    add_log_window *window = yon_dictionary_get_data(dict->first->next,add_log_window*);
        GList *list = gtk_container_get_children(GTK_CONTAINER(window->LogPathsBox));
        char *pathse=NULL;
        for (guint i=0;i<g_list_length(list);i++){
            GList *paths = gtk_container_get_children((GtkContainer*)g_list_nth_data(list,i));
            char *cur = (char*)gtk_entry_get_text((GtkEntry*)g_list_nth_data(paths,1));
            char *error = yon_gtk_entry_check_restricted((GtkEntry*)g_list_nth_data(paths,1));
            if (!yon_char_is_empty(error)){
                yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),error,5,BACKGROUND_IMAGE_FAIL_TYPE);
                yon_ubl_status_highlight_incorrect((GtkWidget*)g_list_nth_data(paths,1));
                return;
            }
            if (strcmp(cur,""))
                pathse=yon_char_unite(pathse ? yon_char_append(pathse,"\n"):"",cur,NULL);
        }

        int fo=0;
        int so=0;
        int paths_size;
        if (!pathse){
            yon_ubl_status_highlight_incorrect(window->LogPathsBox);
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            return;
        }
        config_str paths_parsed = yon_char_parse(pathse,&paths_size,"\n");
        if (yon_char_parsed_check_repeats(paths_parsed,paths_size,&fo,&so)){
            GList *list = gtk_container_get_children(GTK_CONTAINER(window->LogPathsBox));
            yon_ubl_status_highlight_incorrect((GtkWidget*)g_list_nth_data(list,so));
            yon_ubl_status_highlight_incorrect((GtkWidget*)g_list_nth_data(list,fo));
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),REPEAT_PATHS_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            return;
        }
        char *name = (char*)gtk_entry_get_text(GTK_ENTRY(window->NameEntry));
        char *desc = (char*)gtk_entry_get_text(GTK_ENTRY(window->LogDescriptionEntry));
        if (!yon_char_is_empty(name)){
            GtkTreeModel *model = GTK_TREE_MODEL(widgets->list);
            GtkTreeIter iter;
            int stat=0;
            if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&model,&iter)){
                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)){
                    char *paths=NULL;
                    char *name;
                    gtk_tree_model_get(GTK_TREE_MODEL(widgets->list),&iter,0,&name,1,&paths,-1);
                    if (!strcmp(gtk_entry_get_text(GTK_ENTRY(window->NameEntry)),name)){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),REPEATED_PATHS_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(window->NameEntry);
                        return;
                    }
                    int paths_size=0;
                    config_str paths_parsed = yon_char_parse(paths,&paths_size,"\n");
                    int pathse_size=0;
                    config_str pathse_parsed = yon_char_parse(pathse,&pathse_size,"\n");
                    if (yon_char_parsed_includes_char_parsed(pathse_parsed,paths_parsed,pathse_size,paths_size)){
                        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),REPEATED_PATHS_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        return;
                    }
                }
                gtk_list_store_append(widgets->list,&iter);
                gtk_list_store_set(widgets->list,&iter,3,0,-1);
                gtk_list_store_set(widgets->list,&iter,0,name,1,pathse,2,!yon_char_is_empty(desc) ? desc : "",3,0,-1);
                yon_window_config_custom_window_set(GTK_WINDOW(window->MainWindow),"AddWindow");
            } else {
                yon_window_config_custom_window_set(GTK_WINDOW(window->MainWindow),"EditWindow");
                char *nm;
                char *paths_old;
                gtk_tree_model_get(GTK_TREE_MODEL(widgets->list),&iter,0,&nm,1,&paths_old,3,&stat,-1);
                if (strcmp(paths_old,pathse))
                    stat = stat==0?0:stat==1?2:2;
                gtk_list_store_set(widgets->list,&iter,3,stat,-1);
            }
            gtk_list_store_set(widgets->list,&iter,0,name,1,pathse,2,!yon_char_is_empty(desc) ? desc : "",3,stat,-1);
            if (stat==2) gtk_widget_set_sensitive(widgets->RemoveButton,1);
        } else {
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),WRITE_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(window->NameEntry);
            return;
        }
    yon_logs_custom_save(widgets);
    on_subwindow_close(self);
}

add_log_window *on_add_open(main_window *widgets){
    add_log_window *window = malloc(sizeof(add_log_window));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_add_path);
    window->MainWindow=yon_gtk_builder_get_widget(builder,"MainWindow");
    window->CancelButton=yon_gtk_builder_get_widget(builder,"CancelButton");
    window->SaveButton=yon_gtk_builder_get_widget(builder,"SaveButton");
    window->LogTypeCombo=yon_gtk_builder_get_widget(builder,"LogTypeCombo");
    window->LogDescriptionEntry=yon_gtk_builder_get_widget(builder,"LogDescriptionEntry");
    window->LogrotateButton=yon_gtk_builder_get_widget(builder,"LogrotateButton");
    window->MetalogButton=yon_gtk_builder_get_widget(builder,"MetalogButton");
    window->SyslogButton=yon_gtk_builder_get_widget(builder,"SyslogButton");
    window->JournaldButton=yon_gtk_builder_get_widget(builder,"JournaldButton");
    window->LogSizeLabel=yon_gtk_builder_get_widget(builder,"LogSizeLabel");
    window->StatisticsFrame=yon_gtk_builder_get_widget(builder,"StatisticsFrame");
    window->ControlSectionBox=yon_gtk_builder_get_widget(builder,"ControlSectionBox");
    window->LogPathsBox=yon_gtk_builder_get_widget(builder,"LogPathsBox");
    window->NameEntry=yon_gtk_builder_get_widget(builder,"NameEntry");
    window->StatusBox=yon_gtk_builder_get_widget(builder,"StatusBox");
    window->HeaderLabel=yon_gtk_builder_get_widget(builder,"headerTopic");
    gtk_window_set_transient_for(GTK_WINDOW(window->MainWindow),GTK_WINDOW(widgets->Window));
    window->log_name=NULL;
    dictionary *dict = NULL;
    yon_dictionary_add_or_create_if_exists_with_data(dict,"widgets",widgets);
    yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
    g_signal_connect(G_OBJECT(window->SaveButton),"clicked", G_CALLBACK(on_log_save),dict);
    return window;
}

void on_log_path_remove(GtkWidget *self, add_log_window *window){
    GList *paths = gtk_container_get_children(GTK_CONTAINER(window->LogPathsBox));
    for (guint i=0;i<g_list_length(paths);i++){
        GtkWidget *box = (GtkWidget*)g_list_nth_data(paths,i);
        GList *list = gtk_container_get_children(GTK_CONTAINER(box));
        GtkWidget *button = (GtkWidget*)g_list_nth_data(list,4);
        if (self==button){
            if (g_list_length(list)>1&&gtk_widget_get_sensitive(g_list_nth_data(list,3))){
                GtkWidget *prevbox = (GtkWidget*)g_list_nth_data(paths,i-1);
                GList *prevlist = gtk_container_get_children(GTK_CONTAINER(prevbox));
                gtk_widget_show(g_list_nth_data(prevlist,3));
                g_list_free(prevlist);
                gtk_widget_destroy(box);
            }
        }
        g_list_free(list);
    }
    g_list_free(paths);
}

void on_log_path_filechooser_selection_changed(GtkWidget *, logrotate_filechooser_window *dialog){
    GFile *file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(dialog->FileChooser));
    GFileType type = g_file_query_file_type(file,G_FILE_QUERY_INFO_NONE, NULL);
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(dialog->FileOrDirCheck))){
        if (type==G_FILE_TYPE_REGULAR){
            gtk_widget_set_sensitive(dialog->AcceptButton,0);
        } else {
            gtk_widget_set_sensitive(dialog->AcceptButton,1);
        }
    } else {
        if (type==G_FILE_TYPE_REGULAR){
            gtk_widget_set_sensitive(dialog->AcceptButton,1);
        } else {
            gtk_widget_set_sensitive(dialog->AcceptButton,0);
        }
    }
}

void on_log_path_filechooser_toggled(GtkWidget *, logrotate_filechooser_window *dialog){
    on_log_path_filechooser_selection_changed(NULL,dialog);
}

void on_log_path_filechooser_save(GtkWidget *, dictionary *dict){
    GtkEntry *entry = yon_dictionary_get_data(dict->first,GtkEntry*);
    logrotate_filechooser_window *dialog = yon_dictionary_get_data(dict->first->next,logrotate_filechooser_window*);
    char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog->FileChooser));
    GFile *file = gtk_file_chooser_get_file(GTK_FILE_CHOOSER(dialog->FileChooser));
    if (!file){
        filename = gtk_file_chooser_get_current_folder(GTK_FILE_CHOOSER(dialog->FileChooser));
        if (yon_char_is_empty(filename)){
            yon_ubl_status_box_spawn(GTK_CONTAINER(dialog->StatusBox),NOTHING_CHOSEN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            return;
        }
            if (filename[strlen(filename)-1]=='/')
                filename = yon_char_append(filename,"*");
            else
                filename = yon_char_append(filename,"/*");
    } else {
        GFileType type = g_file_query_file_type(file,G_FILE_QUERY_INFO_NONE,NULL);
        if (type==G_FILE_TYPE_DIRECTORY){
            if (filename[strlen(filename)-1]=='/')
                filename = yon_char_append(filename,"*");
            else
                filename = yon_char_append(filename,"/*");
            
        }
    }
    gtk_entry_set_text(entry,filename);

    on_subwindow_close(dialog->FileChooser);
}

void on_log_path_filechooser_open(GtkWidget *, GtkEntry *entry){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_filechooser_path);
    logrotate_filechooser_window *dialog = malloc(sizeof(logrotate_filechooser_window));
    dialog->FileChooser = yon_gtk_builder_get_widget(builder,"FileChooser");
    dialog->FileOrDirCheck = yon_gtk_builder_get_widget(builder,"FileOrDirCheck");
    dialog->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    dialog->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    dialog->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    yon_window_config_custom_window_set(GTK_WINDOW(dialog->FileChooser),"FileDialog");
    gtk_window_set_transient_for(GTK_WINDOW(dialog->FileChooser),GTK_WINDOW(gtk_widget_get_toplevel(GTK_WIDGET(entry))));

    dictionary *dict = NULL;
    gtk_window_set_title(GTK_WINDOW(dialog->FileChooser),FILECHOOSER_TITLE_LABEL);
    yon_dictionary_add_or_create_if_exists_with_data(dict,"entry",entry);
    yon_dictionary_add_or_create_if_exists_with_data(dict,"dialog",dialog);

    g_signal_connect(G_OBJECT(dialog->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
        g_signal_connect(G_OBJECT(dialog->AcceptButton),"clicked",G_CALLBACK(on_log_path_filechooser_save),dict);
    g_signal_connect(G_OBJECT(dialog->FileChooser),"selection-changed",G_CALLBACK(on_log_path_filechooser_selection_changed),dialog);
    g_signal_connect(G_OBJECT(dialog->FileOrDirCheck),"toggled",G_CALLBACK(on_log_path_filechooser_toggled),dialog);
    gtk_widget_show(dialog->FileChooser);
}

void on_log_path_choose(GtkWidget *self,add_log_window *){
    GtkWidget *parent = gtk_widget_get_parent(self);
    GList *list = gtk_container_get_children(GTK_CONTAINER(parent));
    on_log_path_filechooser_open(NULL,(GtkEntry*)g_list_nth_data(list,1));
}

void on_log_path_append(GtkWidget *self, add_log_window *window){
    GList *paths = gtk_container_get_children(GTK_CONTAINER(window->LogPathsBox));
    GtkWidget *box = (GtkWidget*)g_list_nth_data(paths,g_list_length(paths)-1);
    GList *path_widgets;
    GtkWidget *entry;
    if (box){
        path_widgets = gtk_container_get_children(GTK_CONTAINER(box));
        entry = (GtkWidget*)g_list_nth_data(path_widgets,1);
        char *entry_path=(char*)gtk_entry_get_text(GTK_ENTRY(entry));
    
        if (!gtk_widget_get_sensitive(box)||!strcmp(entry_path,"")){
            return;           
        }else {
            GList *list = gtk_container_get_children(GTK_CONTAINER(window->LogPathsBox));
            for (guint i=0;i<g_list_length(list);i++){
                GList *wids = gtk_container_get_children(GTK_CONTAINER((GtkWidget*)g_list_nth_data(list,i)));
                char *check_path = (char*)gtk_entry_get_text((GtkEntry*)g_list_nth_data(wids,1));
                if (!strcmp(entry_path,check_path)&&entry!=(GtkWidget*)g_list_nth_data(wids,1)){
                    yon_ubl_status_highlight_incorrect(entry);
                    yon_ubl_status_highlight_incorrect((GtkWidget*)g_list_nth_data(wids,1));
                    yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),REPEATED_PATHS_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                    return;
                }
                g_list_free(wids);
            }
                g_list_free(list);
            gtk_widget_hide(self);
        }
    }
        GtkBuilder *builder = gtk_builder_new_from_resource(glade_add_path_path);
        GtkWidget *box_path = yon_gtk_builder_get_widget(builder,"PathBox");
        GtkWidget *AppendButton = yon_gtk_builder_get_widget(builder,"AppendButton");
        GtkWidget *SearchButton = yon_gtk_builder_get_widget(builder,"SearchButton");
        GtkWidget *RemoveButton = yon_gtk_builder_get_widget(builder,"RemoveButton");
        g_signal_connect(G_OBJECT(AppendButton),"clicked",G_CALLBACK(on_log_path_append),window);
        g_signal_connect(G_OBJECT(RemoveButton),"clicked",G_CALLBACK(on_log_path_remove),window);
        g_signal_connect(G_OBJECT(SearchButton),"clicked",G_CALLBACK(on_log_path_choose),window);
        gtk_widget_show(AppendButton);
        gtk_box_pack_start(GTK_BOX(window->LogPathsBox),box_path,0,0,5);
}

void on_log_add(GtkWidget *, main_window *widgets){
    add_log_window *window = on_add_open(widgets);
    yon_window_config_custom_window_get(GTK_WINDOW(window->MainWindow),"AddWindow");
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked", G_CALLBACK(on_subwindow_close),NULL);
    gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)));
    on_main_window_tree_selection_changed(NULL,widgets);
    gtk_widget_show(window->MainWindow);
    gtk_widget_hide(window->StatisticsFrame);
    gtk_widget_hide(window->ControlSectionBox);
    on_log_path_append(NULL,window);
}

void on_log_edit_activate(GtkWidget *, GtkTreePath *, GtkTreeViewColumn *, main_window *widgets);
void on_log_edit_activate(GtkWidget *, GtkTreePath *, GtkTreeViewColumn *, main_window *widgets){
    on_log_edit(widgets->ServicesEditButton,widgets);
}

void on_log_edit(GtkWidget *,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_log_window *window = on_add_open(widgets);
        g_signal_connect(G_OBJECT(window->CancelButton),"clicked", G_CALLBACK(on_subwindow_close),NULL);
        yon_window_config_custom_window_get(GTK_WINDOW(window->MainWindow),"EditWindow");
        gtk_widget_show(window->MainWindow);
        char *name;
        char *paths;
        int size;
        char *description;
        gboolean is_standard;
        int protect;
        gtk_tree_model_get(model,&iter,0,&name,1,&paths,2,&description,3,&is_standard,4,&protect,-1);
        gtk_entry_set_text(GTK_ENTRY(window->NameEntry),name);
        if(getuid()!=0){
            gtk_widget_set_sensitive(window->SyslogButton,0);
            gtk_widget_set_sensitive(window->MetalogButton,0);
            gtk_widget_set_sensitive(window->JournaldButton,0);
            gtk_widget_set_sensitive(window->LogrotateButton,0);
        }
            char *title = malloc(sizeof(char)*strlen(JOURNALD_LOG_TITLE_LABEL)+strlen(name)+1);
            sprintf(title,JOURNALD_LOG_TITLE_LABEL,name);
            gtk_label_set_text(GTK_LABEL(window->HeaderLabel),title);
            gtk_window_set_title(GTK_WINDOW(window->MainWindow),title);        
        if (!strcmp(name,"journald")){
            gtk_widget_hide(window->LogrotateButton);
            gtk_widget_hide(window->MetalogButton);
            gtk_widget_hide(window->SyslogButton);
            gtk_widget_show(window->StatisticsFrame);
            
            int sz;
            config_str logsize = yon_config_load(journald_size_command,&sz);
            if (logsize){
            if (logsize[0][strlen(logsize[0])-1]=='\n') logsize[0][strlen(logsize[0])-1]='\0';
                gtk_label_set_text(GTK_LABEL(window->LogSizeLabel),logsize[0]);
            }
            g_signal_connect(G_OBJECT(window->JournaldButton),"clicked", G_CALLBACK(on_journald_open),widgets);
        } else {
            gtk_widget_hide(window->JournaldButton);
            if (paths){
                int sz;
                float finalsize=0.0;
                char *temp_paths = yon_char_new(paths);
                config_str parsed_paths = yon_char_parse(temp_paths,&sz,"\n");

                int paths_size;
                config_str paths_overall_size = yon_config_load(yon_debug_output("%s\n",get_file_folder_size(yon_char_parsed_to_string(parsed_paths,sz," "))),&paths_size);
                char *overall = yon_char_divide_search(paths_overall_size[paths_size-1],"\t",-1);
                yon_char_parsed_free(paths_overall_size,paths_size);
                if (!yon_char_is_empty(overall)&&strcmp(overall,"0")){
                    gtk_label_set_text(GTK_LABEL(window->LogSizeLabel),overall);
                } else {
                    gtk_label_set_text(GTK_LABEL(window->LogSizeLabel),NO_FILES_FOUND_LABEL);
                }
                free(temp_paths);
                temp_paths=NULL;
                for (int i=0;i<sz;i++){
                    if (!access(parsed_paths[i],F_OK))
                        temp_paths = yon_char_unite(temp_paths ? yon_char_append(temp_paths," ") : "",parsed_paths[i],NULL);
                    else {
                        int mask_size=0;
                        char *mask = yon_char_new(parsed_paths[i]);
                        config_str mask_found = yon_config_load(get_all_files_in_path(mask),&mask_size);
                        if (mask_found){
                            for (int l=0;l<mask_size;l++){
                                yon_char_remove_last_symbol(mask_found[l],'\n');
                                temp_paths = yon_char_unite(temp_paths ? yon_char_append(temp_paths," ") : "",mask_found[l],NULL);
                            }
                        }
                    }
                }
                if (!yon_char_is_empty(temp_paths)){
                    config_str logsize = yon_config_load(logrotate_size_command(temp_paths),&sz);
                    for (int i=0;i<sz;i++){
                        if (logsize[i][strlen(logsize[i])-1]=='\n') logsize[i][strlen(logsize[i])-1]='\0';
                        finalsize+=atof(logsize[i]);
                    }
                    int sizenumber=0;
                    finalsize = yon_size_convert_automatic(finalsize,&sizenumber);
                    char *fin = yon_char_from_float(finalsize);
                    gtk_label_set_text(GTK_LABEL(window->LogSizeLabel),yon_char_append(fin,yon_size_get_letter_from_int(sizenumber)));
                    gtk_widget_show(window->StatisticsFrame);
                }
            }
            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->LogrotateButton),"clicked", G_CALLBACK(on_log_edit_logrotate_settings),dict);
        }
        window->log_name=name;
        gtk_entry_set_text(GTK_ENTRY(window->LogDescriptionEntry),description ? description : "");
        config_str paths_parsed = yon_char_parse(paths,&size,"\n");
        GtkWidget *last=NULL;
        int protected=0;
        for (int i=0;i<size;i++){
            GtkBuilder *builder = gtk_builder_new_from_resource(glade_add_path_path);
            GtkWidget *box = yon_gtk_builder_get_widget(builder,"PathBox");
            GtkWidget *entry = yon_gtk_builder_get_widget(builder,"PathEntry");
            GtkWidget *AppendButton = yon_gtk_builder_get_widget(builder,"AppendButton");
            GtkWidget *SearchButton = yon_gtk_builder_get_widget(builder,"SearchButton");
            GtkWidget *RemoveButton = yon_gtk_builder_get_widget(builder,"RemoveButton");
            if (protected<protect){
                gtk_widget_set_sensitive(RemoveButton,0);
                gtk_widget_set_sensitive(entry,0);
                gtk_widget_set_sensitive(SearchButton,0);
                protected++;
            } else {
                g_signal_connect(G_OBJECT(RemoveButton),"clicked",G_CALLBACK(on_log_path_remove),window);
                g_signal_connect(G_OBJECT(SearchButton),"clicked",G_CALLBACK(on_log_path_choose),window);
            }
            if (!strcmp(name,"journald")) {
                gtk_widget_hide(AppendButton);
                gtk_widget_hide(RemoveButton);
            }
            gtk_entry_set_text(GTK_ENTRY(entry),paths_parsed[i]);
            gtk_box_pack_start(GTK_BOX(window->LogPathsBox),box,0,0,5);
            last = AppendButton;
        }
        if (strcmp(name,"journald"))
        gtk_widget_show(last);
        g_signal_connect(G_OBJECT(last),"clicked",G_CALLBACK(on_log_path_append),window);
        gtk_widget_set_sensitive(window->LogTypeCombo,0);
        if (is_standard) {
            gtk_widget_set_sensitive(window->NameEntry,0);
            gtk_widget_set_sensitive(window->LogDescriptionEntry,0);
        gtk_entry_set_has_frame(GTK_ENTRY(window->NameEntry),0);
        gtk_widget_set_sensitive(window->NameEntry,0);
        gtk_style_context_add_class(gtk_widget_get_style_context(window->NameEntry),"nobackground"); 
        gtk_style_context_add_class(gtk_widget_get_style_context(window->NameEntry),"noborder"); 
        gtk_style_context_add_class(gtk_widget_get_style_context(window->NameEntry),"instant"); 
        } else {
            gtk_widget_set_sensitive(window->NameEntry,1);
            gtk_widget_set_sensitive(window->LogDescriptionEntry,1);    
        }
    }
}

void on_log_remove(GtkWidget *, 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 standard=0;
        gtk_tree_model_get(model,&iter,3,&standard,-1);
        if (standard==0)
            gtk_list_store_remove(widgets->list,&iter);
        if (standard==2){
            int stay_paths=0;
            char *paths;
            gtk_tree_model_get(model,&iter,1,&paths,4,&stay_paths,-1);
            int size;
            config_str parsed = yon_char_parse(paths,&size,"\n");
            char *stay = yon_char_parsed_to_string_for_iters(parsed,size,"\n",stay_paths);
            yon_char_parsed_free(parsed,size);
            gtk_list_store_set(widgets->list,&iter,1,stay,3,1,-1);
            gtk_widget_set_sensitive(widgets->RemoveButton,0);
        }
    }
    yon_logs_custom_save(widgets);
}

// Main window


void on_service_edit_activate(GtkWidget *, GtkTreePath *, GtkTreeViewColumn *, main_window *widgets);
void on_service_edit_activate(GtkWidget *, GtkTreePath *, GtkTreeViewColumn *, main_window *widgets){
    on_service_edit(NULL,widgets);
}


void on_service_edit(GtkWidget *self,main_window *widgets){
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->ServicesList);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->ServicesTree)),&model,&iter)){
        char *name;
        gtk_tree_model_get(model,&iter,2,&name,-1);
        if (!strcmp(name,"journald")){
            on_journald_open(self,widgets);
        } else {
            on_logrotate_tab_open(NULL,widgets);
        }
    }
}

void on_main_window_tree_selection_changed(GtkWidget *,main_window *widgets){
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->list);
    GtkTreeIter iter;
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->LogsRMBMenu->menu));

    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&model,&iter)){
        gboolean is_standard;
        gtk_tree_model_get(model,&iter,3,&is_standard,-1);
        gtk_widget_set_sensitive(widgets->AddButton,1);
        if (!is_standard||is_standard==2){
            gtk_widget_set_sensitive(widgets->RemoveButton,1);
            gtk_widget_set_sensitive(GTK_WIDGET(g_list_nth_data(list,2)),1);
        }
        else{ 
            gtk_widget_set_sensitive(widgets->RemoveButton,0);
            gtk_widget_set_sensitive(GTK_WIDGET(g_list_nth_data(list,2)),0);
        }
        gtk_widget_set_sensitive(widgets->InspectButton,1);
        gtk_widget_set_sensitive(widgets->ConfigureButton,1);
            gtk_widget_set_sensitive(GTK_WIDGET(g_list_nth_data(list,1)),1);
    } else {
            gtk_widget_set_sensitive(widgets->AddButton,1);
            gtk_widget_set_sensitive(widgets->RemoveButton,0);
            gtk_widget_set_sensitive(GTK_WIDGET(g_list_nth_data(list,1)),0);
            gtk_widget_set_sensitive(GTK_WIDGET(g_list_nth_data(list,2)),0);
            gtk_widget_set_sensitive(widgets->InspectButton,0);
            gtk_widget_set_sensitive(widgets->ConfigureButton,0);
    }
}

void on_main_window_services_tree_selection_changed(GtkWidget *,main_window *widgets){
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->ServicesList);
    GtkTreeIter iter;

    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->ServicesTree)),&model,&iter)){
            gtk_widget_set_sensitive(widgets->ServicesControlButton,1);
        gboolean is_active;
        gboolean is_enabled;
        char *name;
        GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->ServicesRMBMenu->menu));
        gtk_tree_model_get(model,&iter,0,&is_active,1,&is_enabled,2,&name,-1);
        if (is_active){
            gtk_widget_set_sensitive(widgets->ServicesEditButton,1);
            gtk_widget_set_sensitive(GTK_WIDGET(g_list_nth_data(list,1)),1);
            gtk_image_set_from_icon_name(GTK_IMAGE(gtk_button_get_image(GTK_BUTTON(widgets->ServicesControlButton))),stop_icon_name,GTK_ICON_SIZE_BUTTON);
            GList *item = gtk_container_get_children(GTK_CONTAINER(g_list_nth_data(list,0)));
            GList *items = gtk_container_get_children(GTK_CONTAINER(g_list_nth_data(item,0)));
            gtk_image_set_from_icon_name(GTK_IMAGE(g_list_nth_data(items,0)),stop_icon_name,GTK_ICON_SIZE_BUTTON);
            g_list_free(item);
            g_list_free(items);
        } else {
        gtk_widget_set_sensitive(widgets->ServicesEditButton,0);
            gtk_widget_set_sensitive(GTK_WIDGET(g_list_nth_data(list,1)),0);
            gtk_image_set_from_icon_name(GTK_IMAGE(gtk_button_get_image(GTK_BUTTON(widgets->ServicesControlButton))),start_icon_name,GTK_ICON_SIZE_BUTTON);
            GList *item = gtk_container_get_children(GTK_CONTAINER(g_list_nth_data(list,0)));
            GList *items = gtk_container_get_children(GTK_CONTAINER(g_list_nth_data(item,0)));
            gtk_image_set_from_icon_name(GTK_IMAGE(g_list_nth_data(items,0)),start_icon_name,GTK_ICON_SIZE_BUTTON);
            g_list_free(item);
            g_list_free(items);
        }
        if (!strcmp(name,"journald")){
        }
        g_list_free(list);
    } else {
        gtk_widget_set_sensitive(widgets->ServicesEditButton,0);
        gtk_widget_set_sensitive(widgets->ServicesControlButton,0);
    }
}

void on_start_stop_clicked(GtkButton *, main_window *widgets){
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->ServicesList);
    GtkTreeIter iter;
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->ServicesRMBMenu->buttons->first->data));
    GList *box = gtk_container_get_children(GTK_CONTAINER(g_list_nth_data(list,0)));
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->ServicesTree)),&model,&iter)){
        char *start_command;
        char *stop_command;
        gboolean is_started;
        gtk_tree_model_get(model,&iter,0,&is_started,4,&start_command,5,&stop_command,-1);
        if (is_started){
            g_spawn_command_line_sync(yon_debug_output("%s\n",stop_command),NULL,NULL,NULL,NULL);
            gtk_list_store_set(widgets->ServicesList,&iter,0,0,-1);
            gtk_image_set_from_icon_name(GTK_IMAGE(g_list_nth_data(box,0)),start_icon_name,GTK_ICON_SIZE_BUTTON);
        } else {
            g_spawn_command_line_sync(yon_debug_output("%s\n",start_command),NULL,NULL,NULL,NULL);
            gtk_list_store_set(widgets->ServicesList,&iter,0,1,-1);
            gtk_image_set_from_icon_name(GTK_IMAGE(g_list_nth_data(box,0)),stop_icon_name,GTK_ICON_SIZE_BUTTON);
        }
    }
}

queue_func *yon_queue_add_function(GSourceFunc func,void *data);
queue_func *yon_queue_add_function(GSourceFunc func,void *data){
    queue_func *queue = malloc(sizeof(queue_func));
    queue->func=func;
    queue->data=data;
    return queue;
}

void on_enable_disable_clicked(GtkButton *, main_window *widgets){
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->ServicesList);
    GtkTreeIter iter;
    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->ServicesRMBMenu->buttons->first->next->data));
    GList *box = gtk_container_get_children(GTK_CONTAINER(g_list_nth_data(list,0)));
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->ServicesTree)),&model,&iter)){
        char *start_command;
        char *stop_command;
        gboolean is_started;
        gtk_tree_model_get(model,&iter,1,&is_started,6,&start_command,7,&stop_command,-1);
        if (is_started){
            yon_launch(stop_command);
            gtk_list_store_set(widgets->ServicesList,&iter,1,0,-1);
            gtk_image_set_from_icon_name(GTK_IMAGE(gtk_button_get_image(GTK_BUTTON(widgets->ServicesControlButton))),start_icon_name,GTK_ICON_SIZE_BUTTON);
            gtk_image_set_from_icon_name(GTK_IMAGE(g_list_nth_data(box,0)),start_icon_name,GTK_ICON_SIZE_BUTTON);
        } else {
            yon_launch(start_command);
            gtk_list_store_set(widgets->ServicesList,&iter,1,1,-1);
            gtk_image_set_from_icon_name(GTK_IMAGE(gtk_button_get_image(GTK_BUTTON(widgets->ServicesControlButton))),stop_icon_name,GTK_ICON_SIZE_BUTTON);
            gtk_image_set_from_icon_name(GTK_IMAGE(g_list_nth_data(box,0)),stop_icon_name,GTK_ICON_SIZE_BUTTON);
        }
    }
}

// standard functions
// gboolean on_unelevate(main_window* widgets);
// gboolean on_unelevate(main_window* widgets){
//     yon_ubl_status_box_render(UNAUTHTORIZED_LABEL,BACKGROUND_IMAGE_INFO_TYPE);
//     gtk_widget_set_sensitive(widgets->UpgradePermissionsMenuItem,1);
//     g_object_unref(main_config.authorization_result);
//     main_config.authorization_result=NULL;
//     return G_SOURCE_REMOVE;
// }
// gboolean yon_timeoutcheck_authtorization(gpointer widgets);
// gboolean yon_timeoutcheck_authtorization(gpointer widgets){

//     if (!polkit_authorization_result_get_is_authorized(main_config.authorization_result)){
//         gdk_threads_add_idle((GSourceFunc)on_unelevate,widgets);
//         return G_SOURCE_REMOVE;
//     }
//     return G_SOURCE_CONTINUE;
// }

// void *yon_authorized_loop(void*);
// void *yon_authorized_loop(void*){
//     while (polkit_authorization_result_get_is_authorized(main_config.authorization_result)){
//         g_mutex_lock(&main_config.authorized_mutex);
//         if (main_config.queue){
//             for (int i=0;i<main_config.queue_size;i++){
//                 G_SOURCE_FUNC(main_config.queue[i]->func)(main_config.queue[i]->data);
//             }
//             free(main_config.queue);
//             main_config.queue=NULL;
//             main_config.queue_size=0;
//         }
//         g_mutex_unlock(&main_config.authorized_mutex);
//     }
//     return NULL;
// }

// void authorize_callback(GObject *source, GAsyncResult *res, main_window *widgets);
// void authorize_callback(GObject *source, GAsyncResult *res, main_window *widgets){
//     GError *err = NULL;
//     main_config.authorization_result = polkit_authority_check_authorization_finish(POLKIT_AUTHORITY(source),res,&err);
//     if (err){

//     }

//         g_thread_new("auth_loop",(GThreadFunc)yon_authorized_loop,NULL);
//         yon_ubl_status_box_render(AUTHTORIZED_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
//         g_timeout_add(10000, (GSourceFunc)yon_timeoutcheck_authtorization,widgets);
    
// }

// void on_permissions_changed(GtkMenuItem *,main_window *widgets){
//     PolkitAuthority *authority;
//     GError *error = NULL;
//     authority = polkit_authority_get_sync(NULL,&error);
//     if (!authority){
//         return;
//     }
//     polkit_authority_check_authorization(
//         authority,
//         main_config.process,
//         "org.freedesktop.policykit.exec",
//         NULL,
//         POLKIT_CHECK_AUTHORIZATION_FLAGS_ALLOW_USER_INTERACTION,
//         NULL,
//         (GAsyncReadyCallback)authorize_callback,
//         widgets
//     );
// }

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.remove_thread=-1;
    main_config.load_mode=YON_CONFIG_LOCAL;
    main_config.save_config=1;
    main_config.queue=NULL;
    main_config.queue_size=0;
    pid_t parent_pid;
    parent_pid = getppid ();
    if (parent_pid == 1)
    {
      g_printerr ("Parent process was reaped by init(1)\n");
    }
}

void on_service_manage(GtkWidget *,main_window *widgets){
    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->ServicesList);
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->ServicesTree)),&model,&iter)){
        char *name,*start_command,*stop_command,*enable_command,*disable_command,*status_command,*check_active_command,*check_enable_command;
        gtk_tree_model_get(model,&iter,
                            2,&name,5,&start_command,
                            6,&stop_command,7,&enable_command,
                            8,&disable_command,9,&status_command,
                            10,&check_active_command,11,&check_enable_command,
                            -1);
        service_window *window = yon_service_window_new(name,start_command,stop_command,
                                                        enable_command,disable_command,
                                                        status_command,check_active_command,
                                                        check_enable_command);
        gtk_window_set_transient_for(GTK_WINDOW(window->MainWindow),GTK_WINDOW(widgets->Window));
        yon_service_window_run(window);

    }
}

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


    widgets->InspectButton = yon_gtk_builder_get_widget(builder,"MainInspectButton");
    widgets->ConfigureButton = yon_gtk_builder_get_widget(builder,"MainConfigureButton");
    widgets->AddButton = yon_gtk_builder_get_widget(builder,"MainAddButton");
    widgets->RemoveButton = yon_gtk_builder_get_widget(builder,"MainRemoveButton");
    widgets->Notebook = yon_gtk_builder_get_widget(builder,"MainNotebook");
    widgets->ServicesEditButton = yon_gtk_builder_get_widget(builder,"ServicesEditButton");
    widgets->ServicesControlButton = yon_gtk_builder_get_widget(builder,"ServicesControlButton");
    widgets->ServicesTree = yon_gtk_builder_get_widget(builder,"ServicesTree");
    widgets->list = GTK_LIST_STORE(gtk_builder_get_object(builder,"liststore1"));
    widgets->ServicesList = GTK_LIST_STORE(gtk_builder_get_object(builder,"ServicesList"));

    widgets->UpgradePermissionsMenuItem = yon_gtk_builder_get_widget(builder,"UpgradePermissionsMenuItem");
    gtk_menu_shell_prepend(GTK_MENU_SHELL(gtk_widget_get_parent(widgets->AboutMenuItem)),widgets->UpgradePermissionsMenuItem);

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

    gtk_window_set_title(GTK_WINDOW(widgets->Window),TITLE_LABEL);

    {

        if (main_config.lock_load_global == 1){
            gtk_widget_set_sensitive(widgets->LoadGlobalMenuItem,0);
        }
        if (main_config.lock_save_global == 1){
            gtk_widget_set_sensitive(widgets->SaveGlobalMenuItem,0);
            gtk_widget_set_sensitive(widgets->SaveMenuItem,0);
        }
        if (main_config.lock_save_local == 1){
            gtk_widget_set_sensitive(widgets->SaveLocalMenuItem,0);
            gtk_widget_set_sensitive(widgets->SaveMenuItem,0);
        }
        int size;
        int service_size;
        int dirs_size;
        char *cname=NULL;
        char *cparams="";
        GtkTreeIter iter;
        config_str journals = yon_resource_open_file(journals_list_path,&size);
        for (int i=1;i<size;i++){
            int log_size;
            int psize;
            journals[i]=yon_char_divide_search(journals[i],"\n",-1);
            config_str log = yon_char_parse(journals[i],&log_size,";");
            log[1] = yon_char_replace(log[1],",","\n");
            gtk_list_store_append(widgets->list,&iter);
            config_str sfafa = yon_char_parse(log[1],&psize,"\n");
            if (sfafa)
                free(sfafa);
            gtk_list_store_set(widgets->list,&iter,0,log[0],1,log[1],2,_(log[2]),3,1,4,psize,-1);
        }
            config_str services = yon_resource_open_file(services_list_path,&service_size);
            for (int i=1;i<service_size;i++){
                if (services[i][0]!='#'){
                    int log_size;
                    services[i]=yon_char_divide_search(services[i],"\n",-1);
                    config_str log = yon_char_parse(services[i],&log_size,";");
                    int is_active, is_enabled;
                    is_active = !system(log[7]);
                    is_enabled = !system(log[8]);
                    gtk_list_store_append(widgets->ServicesList,&iter);
                    gtk_list_store_set(widgets->ServicesList,&iter,0,is_active,1,is_enabled,2,log[0],3,_(log[1]),4,log[2],5,log[2],6,log[3],7,log[4],8,log[5],9,log[6],10,log[7],11,log[8],-1);
                }
            }
        config_str dirs = yon_config_load(logrotate_config_command,&dirs_size);
        for (int i=0;i<dirs_size;i++){
            int filesize;
            gboolean bracket_found=0;
            yon_char_divide_search_self(dirs[i],"\n",-1);
            config_str file = yon_file_open(yon_char_append(logrotate_config_path,dirs[i]),&filesize);
            for (int j=0;j<filesize;j++){
                file[j]=yon_char_divide_search(file[j],"\n",-1);
                while (file[j][0]==' '){
                    yon_char_divide_search(file[j]," ",-1);
                }
                if (file[j][0]!='#'&&bracket_found==0&&strcmp(file[j],"")){
                    if (strstr(file[j],"{")) {
                        bracket_found = 1;
                        yon_char_divide_search_self(file[j],"{",-1);
                        if (!cname) cname = file[j];
                        continue;
                    }
                    if (!cname)
                        cname = file[j];
                } else if (file[j][0]!='#'&&bracket_found==1&&strcmp(file[j],"")) {
                    if (!strstr(file[j],"}"))
                        cparams = yon_char_unite(cparams,",",file[j],NULL);
                    else if (strstr(file[j],"}")) break;
                }
            }
            if (!cname) continue;
            cname = yon_char_replace(cname, " ", "\n");
            if (cname[strlen(cname)-1]=='\n') cname[strlen(cname)-1]='\0';
            gtk_list_store_append(main_config.logrotate_apps_list,&iter);
            gtk_list_store_set(main_config.logrotate_apps_list,&iter,0,dirs[i],1,cname,2,cparams,-1);
            free(cname);
            cname=NULL;
            cparams="";
        }
        
        GList *list = gtk_tree_view_get_columns(GTK_TREE_VIEW(widgets->MainTree));
        for (guint i=0; i<g_list_length(list);i++){
            yon_window_config_add_listener(g_list_nth_data(list,i),yon_char_append("column",yon_char_from_int(i)),"fixed-width",YON_TYPE_INT);
        }
        g_list_free(list);
        list = gtk_tree_view_get_columns(GTK_TREE_VIEW(widgets->ServicesTree));
        for (guint i=0; i<g_list_length(list);i++){
            yon_window_config_add_listener(g_list_nth_data(list,i),yon_char_append("servicecolumn",yon_char_from_int(i)),"fixed-width",YON_TYPE_INT);
        }

        main_config.groups = yon_file_open(group_list_path,&main_config.groups_size);
        for (int i=0;i<main_config.groups_size;i++){
            yon_char_divide_search_self(main_config.groups[i],":",-1);
        }
        
        size =0;
        config_str custom_watchers = yon_file_open(custom_logs_saving_file_path,&size);
        if (custom_watchers){
            for (int i=0;i<size;i++){
                if (custom_watchers[i][strlen(custom_watchers[i])-1]=='\n')
                custom_watchers[i][strlen(custom_watchers[i])-1]='\0';
                int cur_size=0;
                config_str cur_watcher = yon_char_parse(custom_watchers[i],&cur_size,";");
                if (cur_watcher){
                    cur_watcher = yon_char_parsed_append(cur_watcher,&cur_size,NULL);
                    char *cur_name;
                    int found=0;
                    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,&cur_name,-1);
                        if (!strcmp(cur_name,cur_watcher[0])){
                            found=1;
                            break;
                        }
                    }
                    if (!found)
                        gtk_list_store_append(widgets->list,&iter);
                    cur_watcher[1] = yon_char_replace(cur_watcher[1],",","\n");
                    gtk_list_store_set(widgets->list,&iter,0,cur_watcher[0],1,cur_watcher[1],2,cur_watcher[2]?cur_watcher[2]:"",3,found ? 2 : 0,-1);
                    yon_char_parsed_free(cur_watcher,cur_size);
                }
            }
        } else {
            if (access(custom_logs_saving_file_path,F_OK)){
                yon_file_create_full_path(custom_logs_saving_file_path,0777);
            }
            FILE *fl = fopen(custom_logs_saving_file_path,"w");
            fclose(fl);
        }
    }
    /* Widget registration for config monitoring | Регистрация виджетов для мониторинга конфига */

    /* 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_log_add),widgets);
    g_signal_connect(G_OBJECT(widgets->ConfigureButton),"clicked",G_CALLBACK(on_log_edit),widgets);
    g_signal_connect(G_OBJECT(widgets->MainTree),"row-activated",G_CALLBACK(on_log_edit_activate),widgets);
    g_signal_connect(G_OBJECT(widgets->RemoveButton),"clicked",G_CALLBACK(on_log_remove),widgets);
    g_signal_connect(G_OBJECT(widgets->ServicesEditButton),"clicked",G_CALLBACK(on_service_edit),widgets);
    g_signal_connect(G_OBJECT(widgets->ServicesTree),"row-activated",G_CALLBACK(on_service_edit_activate),widgets);

    g_signal_connect(G_OBJECT(widgets->SaveMenuItem),"activate",G_CALLBACK(on_save_global_local),NULL);
    g_signal_connect(G_OBJECT(widgets->SaveGlobalMenuItem),"activate",G_CALLBACK(on_save_global),NULL);
    g_signal_connect(G_OBJECT(widgets->SaveLocalMenuItem),"activate",G_CALLBACK(on_save_local),NULL);
    g_signal_connect(G_OBJECT(widgets->SaveCustomMenuItem),"activate",G_CALLBACK(on_config_custom_save),NULL);

    g_signal_connect(G_OBJECT(widgets->LoadGlobalMenuItem),"activate",G_CALLBACK(on_load_global),NULL);
    g_signal_connect(G_OBJECT(widgets->LoadLocalMenuItem),"activate",G_CALLBACK(on_load_local),NULL);
    g_signal_connect(G_OBJECT(widgets->LoadCustomMenuItem),"activate",G_CALLBACK(on_config_custom_load),NULL);

    // g_signal_connect(G_OBJECT(widgets->UpgradePermissionsMenuItem),"activate",G_CALLBACK(on_permissions_changed),widgets);

    g_signal_connect(G_OBJECT(widgets->MainTree),"cursor-changed",G_CALLBACK(on_main_window_tree_selection_changed),widgets);
    g_signal_connect(G_OBJECT(widgets->ServicesTree),"cursor-changed",G_CALLBACK(on_main_window_services_tree_selection_changed),widgets);
    
    g_signal_connect(G_OBJECT(widgets->ServicesControlButton),"clicked",G_CALLBACK(on_service_manage),widgets);

    widgets->LogsRMBMenu = yon_rmb_menu_setup(widgets->MainTree,(RmbCheck)gtk_widget_get_sensitive,widgets->MainTree,
    CONFIGURE_LOG_LABEL,settings_icon_name,G_CALLBACK(on_log_edit),widgets,
    CONFIGURE_LOG_LABEL,settings_icon_name,G_CALLBACK(on_log_edit),widgets,
    ADD_LOG_LABEL,add_icon_name,G_CALLBACK(on_log_add),widgets,
    REMOVE_LOG_LABEL,remove_icon_name,G_CALLBACK(on_log_remove),widgets,
    NULL);
    widgets->ServicesRMBMenu = yon_rmb_menu_setup(widgets->ServicesTree,(RmbCheck)gtk_widget_get_sensitive,widgets->ServicesTree,
    SERVICE_MANAGEMENT_TOOLTIP,start_icon_name,G_CALLBACK(on_service_manage),widgets,
    CONFIGURE_SERVICE_LABEL,settings_icon_name,G_CALLBACK(on_service_edit),widgets,
    NULL);

    yon_window_config_setup(GTK_WINDOW(widgets->Window));
    yon_window_config_load(config_path);

    {
        GList *list = gtk_tree_view_get_columns(GTK_TREE_VIEW(widgets->ServicesTree));
        for (guint i = 0; i < g_list_length(list); i++){
            yon_gtk_tree_view_column_minimal_fixed_size_set((GtkTreeViewColumn*)g_list_nth_data(list,i));
        }
        g_list_free(list);
        list = gtk_tree_view_get_columns(GTK_TREE_VIEW(widgets->MainTree));
        for (guint i = 0; i < g_list_length(list); i++){
            yon_gtk_tree_view_column_minimal_fixed_size_set((GtkTreeViewColumn*)g_list_nth_data(list,i));
        }
    }
    yon_save_window_set_postsave_function(on_save_done,widgets);
    yon_save_window_set_postsave_failure_function(on_save_failed,widgets);
    yon_load_proceed(YON_CONFIG_LOCAL);
    update_loaded_logrotate();
}

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.logrotate_list = gtk_list_store_new(3,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING);
    main_config.logrotate_apps_list = gtk_list_store_new(3,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING);

    template_main_window *widgets = yon_ubl_window_setup();
    yon_root_button_setup(widgets,argv,argc);
    yon_main_window_complete(&widgets);
    char *path = yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL);
    yon_window_config_load(path);
    main_config.launch_arguments=yon_char_parsed_copy(argv,argc);
    main_config.launch_size=argc;
    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;
}