#include "ubl-settings-resourcequota.h"

config main_config;

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 3:
			yon_ubl_status_box_render(GLOBAL_LOCAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
	}
	textdomain(LocaleName);
	
}

//functions

char *get_size_mod(GtkComboBox *combo){
    char *size_modifier = NULL;
    switch (gtk_combo_box_get_active(combo)){
        case 0: size_modifier = "K";
            break;
        case 1: size_modifier = "M";
            break;
        case 2: size_modifier = "G";
            break;
        case 3: size_modifier = "T";
            break;
    }
    return size_modifier;
}

void on_device_limits_update(GtkWidget *, quota_window *window){
    if (window->devices->data){
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->DeviceLimitReadCheck)))
        yon_dictionary_get_data(window->devices,device_limits*)->read=yon_char_append(yon_char_from_long(gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->DeviceLimitReadSpin))),get_size_mod(GTK_COMBO_BOX(window->DeviceLimitReadCombo)));
        else yon_dictionary_get_data(window->devices,device_limits*)->read = NULL;
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->DeviceLimitWriteCheck)))
        yon_dictionary_get_data(window->devices,device_limits*)->write=yon_char_append(yon_char_from_long(gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->DeviceLimitWriteSpin))),get_size_mod(GTK_COMBO_BOX(window->DeviceLimitWriteCombo)));
        else yon_dictionary_get_data(window->devices,device_limits*)->write = NULL;
        }
}

gboolean yon_manual_string_finish(GtkWidget *, GdkEvent *,quota_window *window){
    on_quota_configuration_update(NULL,window);
    return 0;
}

void on_quota_configuration_update(GtkWidget *, quota_window *window){
    char *output_line = NULL;
    long sft = (long)gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->SoftRestrictionSpin));
    long hrd = (long)gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->HardRestrictionSpin));
    long pgl = (long)gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->PagingLimitSpin));
    long cpu = (long)gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->CpuLimitSpin));
    char *soft_limit = "";
    char *hard_limit = "";
    char *paging_limit = "";
    char *cpu_limit = "";
    if (sft) soft_limit = yon_char_unite("MemoryHigh=",yon_char_from_long(sft),get_size_mod(GTK_COMBO_BOX(window->SoftRestrictionCombo)),NULL);
    if (hrd) hard_limit = yon_char_unite("MemoryMax=",yon_char_from_long(hrd),get_size_mod(GTK_COMBO_BOX(window->HardRestrictionCombo)),NULL);
    if (pgl) paging_limit = yon_char_unite("MemorySwapMax=",yon_char_from_long(pgl),get_size_mod(GTK_COMBO_BOX(window->PagingLimitCombo)),NULL);
    if (cpu) cpu_limit = yon_char_unite("CPUQuota=",yon_char_from_long(cpu),"%",NULL);

    char *read_limit = "";
    char *read_devices = "";
    dictionary *dict;
    for_dictionaries(dict,window->devices){
        device_limits *limits = yon_dictionary_get_data(dict,device_limits*);
        if (dict->data&&limits->read)
            if (!strstr(limits->read," 0 "))
                read_devices = yon_char_unite(read_devices,"IOReadBandwidthMax=",yon_char_divide_search(yon_char_new(dict->key)," ",-1)," ",yon_dictionary_get_data(dict,device_limits*)->read,",",NULL);
    }
    if (read_devices[strlen(read_devices)-1]==',') read_devices[strlen(read_devices)-1]='\0';
    read_limit = yon_char_unite(read_limit,read_devices,NULL);
    
    
    char *write_limit = "";
    char *write_devices = "";
    for_dictionaries(dict,window->devices){
        device_limits *limits = yon_dictionary_get_data(dict,device_limits*);
        if (dict->data&&limits->write)
            if (!strstr(limits->write," 0 "))
                write_devices = yon_char_unite(write_devices,"IOWriteBandwidthMax=",yon_char_divide_search(yon_char_new(dict->key)," ",-1)," ",yon_dictionary_get_data(dict,device_limits*)->write,",",NULL);
    }
    if (write_devices[strlen(write_devices)-1]==',') write_devices[strlen(write_devices)-1]='\0';
    write_limit = yon_char_unite(write_limit,write_devices,NULL);

    output_line = yon_char_unite(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->SoftRestrictionCheck))? yon_char_append(soft_limit,","): "",
    gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->HardRestrictionCheck))? yon_char_append(hard_limit,","):"",
    gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->PagingLimitCheck))? yon_char_append(paging_limit,","):"",
    gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->CpuLimitCheck))? yon_char_append(cpu_limit,","):"",
    (read_devices && strcmp(read_devices,"")!=0 ) ? yon_char_append(read_limit,","):"",
    (write_devices && strcmp(write_devices,"")!=0 ) ? yon_char_append(write_limit,","):"",NULL);
    if (output_line&&strcmp(output_line,"")!=0&&output_line[strlen(output_line)-1] == ',') output_line[strlen(output_line)-1] = '\0';
    gtk_entry_set_text(GTK_ENTRY(window->ManualInputEntry),output_line);
}

void on_limit_manual_input(GtkWidget *self, double *, quota_window *window){
    on_quota_configuration_update(self, window);
}

void on_check_maximum(GtkWidget *self, GtkWidget *pair){
    if (GTK_IS_SPIN_BUTTON(self)){
        if (GTK_IS_COMBO_BOX(pair)){
            int required_size = gtk_combo_box_get_active(GTK_COMBO_BOX(pair));
            if (main_config.avaliable_ram==0){
                struct sysinfo inf;
                sysinfo(&inf);
                main_config.avaliable_ram=inf.totalram;
                main_config.avaliable_swap=inf.totalswap;
            }
            double cur_ram = main_config.avaliable_ram;
            double cur_value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(self));
            for (int i=0;i<required_size;i++)
                cur_ram=cur_ram/1024;
            GtkAdjustment *adj = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(self));
            gtk_adjustment_set_upper(adj,cur_ram);
            if (cur_value>cur_ram)
                gtk_adjustment_set_value(adj,cur_ram);
        }
    } else if (GTK_IS_SPIN_BUTTON(pair)){
        if (GTK_IS_COMBO_BOX(self)){
            int required_size = gtk_combo_box_get_active(GTK_COMBO_BOX(self));
            if (main_config.avaliable_ram==0){
                struct sysinfo inf;
                sysinfo(&inf);
                main_config.avaliable_ram=inf.totalram;
                main_config.avaliable_swap=inf.totalswap;
            }
            double cur_ram = main_config.avaliable_ram;
            double cur_value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(pair));
            for (int i=0;i<required_size+1;i++)
                cur_ram=cur_ram/1024;
            GtkAdjustment *adj = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(pair));
            gtk_adjustment_set_upper(adj,(long)cur_ram);
            if (cur_value>cur_ram)
                gtk_adjustment_set_value(adj,cur_ram);
        }
    } 
}

void on_check_maximum_cpu(GtkWidget *self, GtkWidget *pair){
    if (GTK_IS_SPIN_BUTTON(self)){
        if (GTK_IS_COMBO_BOX(pair)){
            int required_size = gtk_combo_box_get_active(GTK_COMBO_BOX(pair));
            if (main_config.avaliable_ram==0){
                struct sysinfo inf;
                sysinfo(&inf);
                main_config.avaliable_swap=inf.totalswap;
            }
            double cur_ram = main_config.avaliable_swap;
            double cur_value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(self));
            for (int i=0;i<required_size;i++)
                cur_ram=cur_ram/1024;
            GtkAdjustment *adj = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(self));
            gtk_adjustment_set_upper(adj,cur_ram);
            if (cur_value>cur_ram)
                gtk_adjustment_set_value(adj,cur_ram);
        }
    } else if (GTK_IS_SPIN_BUTTON(pair)){
        if (GTK_IS_COMBO_BOX(self)){
            int required_size = gtk_combo_box_get_active(GTK_COMBO_BOX(self));
            if (main_config.avaliable_swap==0){
                struct sysinfo inf;
                sysinfo(&inf);
                main_config.avaliable_swap=inf.totalswap;
            }
            double cur_ram = main_config.avaliable_swap;
            double cur_value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(pair));
            for (int i=0;i<required_size+1;i++)
                cur_ram=cur_ram/1024;
            GtkAdjustment *adj = gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(pair));
            gtk_adjustment_set_upper(adj,(long)cur_ram);
            if (cur_value>cur_ram)
                gtk_adjustment_set_value(adj,cur_ram);
        }
    } 
}

void on_quota_target_type_changed(GtkWidget *self, quota_window *window){
    int active = gtk_combo_box_get_active(GTK_COMBO_BOX(self));
    if (active == 0) {
        gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(window->TargetCombo));
        for (int i=0; i<main_config.users_size;i++){
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->TargetCombo),main_config.users[i]);
        }
    }
    if (active == 1) {
        gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(window->TargetCombo));
        for (int i=0; i<main_config.processes_size;i++){
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->TargetCombo),main_config.processes[i]);
        }
    }
    if (active == 2) {
        gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(window->TargetCombo));
        for (int i=0; i<main_config.slices_size;i++){
            gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->TargetCombo),main_config.slices[i]);
        }
    }
}

void on_target_check_chosen(GtkWidget *self, quota_window *window){
    if (gtk_combo_box_get_active(GTK_COMBO_BOX(self))!=-1)
        gtk_widget_set_sensitive(window->BlockUiBox,1);
    else
        gtk_widget_set_sensitive(window->BlockUiBox,0);
}

void yon_get_devices(quota_window *window){
    int size=0;
    config_str devices_parsed = yon_config_load(get_devices_command,&size);
    for (int i=0; i<size;i++){
        yon_char_remove_last_symbol(devices_parsed[i],'\n');
        char *pa = strstr(devices_parsed[i],"/");
        char *device_name = yon_char_new(pa);
        char *id = yon_char_divide_search(device_name," ",-1);
        free(device_name);
        device_limits *limits = malloc(sizeof(device_limits));
        limits->device_name = yon_char_new(pa);
        limits->read=NULL;
        limits->write=NULL;
        yon_dictionary_add_or_create_if_exists_with_data(window->devices,id,limits); 
    }
    yon_char_parsed_free(devices_parsed,size);
}

void on_device_set(GtkWidget *self, quota_window *window){
    char *id = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->DeviceLimitCombo));
    if (!yon_char_is_empty(id)){
        dictionary *current = yon_dictionary_get(&window->devices,id);
        if (current){
            device_limits *limits = yon_dictionary_get_data(current,device_limits*);
            if (self == window->DeviceLimitReadCheck||self == window->DeviceLimitReadSpin||self == window->DeviceLimitReadCombo){
                if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->DeviceLimitReadCheck))){
                    long value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->DeviceLimitReadSpin));
                    if (!value) {
                        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(window->DeviceLimitReadSpin);
                        return;
                    }
                    char *value_str = yon_char_from_long(value);
                    char *full_str = yon_char_unite(value_str,yon_size_get_mod(gtk_combo_box_get_active(GTK_COMBO_BOX(window->DeviceLimitReadCombo))),NULL);
                    if (!yon_char_is_empty(limits->read)) free(limits->read);
                    limits->read=full_str;
                } else {
                    limits->read=NULL;
                }
            } else if (self == window->DeviceLimitWriteCheck||self == window->DeviceLimitWriteSpin||self == window->DeviceLimitWriteCombo){
                if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->DeviceLimitWriteCheck))){
                    long value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->DeviceLimitWriteSpin));
                    if (!value) {
                        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                        yon_ubl_status_highlight_incorrect(window->DeviceLimitWriteSpin);
                        return;
                    }
                    char *value_str = yon_char_from_long(value);
                    char *full_str = yon_char_unite(value_str,yon_size_get_mod(gtk_combo_box_get_active(GTK_COMBO_BOX(window->DeviceLimitWriteCombo))),NULL);
                    if (!yon_char_is_empty(limits->write)) free(limits->write);
                    limits->write=full_str;
                } else {
                    limits->write=NULL;
                }
            }
        }
    }
}

void on_device_current_changed(GtkWidget *, quota_window *window){
        char *chosen = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->DeviceLimitCombo));
    if (!yon_char_is_empty(chosen)){
        dictionary *current = yon_dictionary_get(&window->devices,chosen);
        if (current){
            device_limits *limits = yon_dictionary_get_data(current,device_limits*);
            char *read = limits->read;
            char *write = limits->write;
            if (!yon_char_is_empty(read)) { 
                int size_mult = yon_get_size_get_from_letter(read[strlen(read)-1]);
                long hard_current_max = main_config.avaliable_ram;
                for (int i=0;i<size_mult+1;i++)
                    hard_current_max=hard_current_max/1024;
                gtk_adjustment_set_upper(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(window->DeviceLimitReadSpin)),hard_current_max);
                g_signal_handlers_block_by_func(G_OBJECT(window->DeviceLimitReadSpin),G_CALLBACK(on_device_set),window);
                gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->DeviceLimitReadSpin),atoi(read));
                g_signal_handlers_unblock_by_func(G_OBJECT(window->DeviceLimitReadSpin),G_CALLBACK(on_device_set),window);
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->DeviceLimitReadCheck),1);
                switch (read[strlen(read)-1]){
                    default:
                    case 'K': gtk_combo_box_set_active(GTK_COMBO_BOX(window->DeviceLimitReadCombo),0);
                    break;
                    case 'M': gtk_combo_box_set_active(GTK_COMBO_BOX(window->DeviceLimitReadCombo),1);
                    break;
                    case 'G': gtk_combo_box_set_active(GTK_COMBO_BOX(window->DeviceLimitReadCombo),2);
                    break;
                    case 'T': gtk_combo_box_set_active(GTK_COMBO_BOX(window->DeviceLimitReadCombo),3);
                    break;
                }
            } else {
                gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->DeviceLimitReadSpin),0);
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->DeviceLimitReadCheck),0);
                gtk_combo_box_set_active(GTK_COMBO_BOX(window->DeviceLimitReadCombo),0);
            }
            if (!yon_char_is_empty(write)) { 
                int size_mult = yon_get_size_get_from_letter(write[strlen(write)-1]);
                long hard_current_max = main_config.avaliable_ram;
                for (int i=0;i<size_mult+1;i++)
                    hard_current_max=hard_current_max/1024;
                gtk_adjustment_set_upper(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(window->DeviceLimitWriteSpin)),hard_current_max);
                g_signal_handlers_block_by_func(G_OBJECT(window->DeviceLimitWriteSpin),G_CALLBACK(on_device_set),window);
                gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->DeviceLimitWriteSpin),atoi(write));
                g_signal_handlers_unblock_by_func(G_OBJECT(window->DeviceLimitWriteSpin),G_CALLBACK(on_device_set),window);
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->DeviceLimitWriteCheck),1);
                switch (write[strlen(write)-1]){
                    default:
                    case 'K': gtk_combo_box_set_active(GTK_COMBO_BOX(window->DeviceLimitWriteCombo),0);
                    break;
                    case 'M': gtk_combo_box_set_active(GTK_COMBO_BOX(window->DeviceLimitWriteCombo),1);
                    break;
                    case 'G': gtk_combo_box_set_active(GTK_COMBO_BOX(window->DeviceLimitWriteCombo),2);
                    break;
                    case 'T': gtk_combo_box_set_active(GTK_COMBO_BOX(window->DeviceLimitWriteCombo),3);
                    break;
                }
            } else {
                gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->DeviceLimitWriteSpin),0);
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->DeviceLimitWriteCheck),0);
                gtk_combo_box_set_active(GTK_COMBO_BOX(window->DeviceLimitWriteCombo),0);
            }
        }
    }  else {
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->DeviceLimitReadCheck),0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->DeviceLimitReadSpin),0);
        gtk_combo_box_set_active(GTK_COMBO_BOX(window->DeviceLimitReadCombo),0);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->DeviceLimitWriteCheck),0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->DeviceLimitWriteSpin),0);
        gtk_combo_box_set_active(GTK_COMBO_BOX(window->DeviceLimitWriteCombo),0);
    }
}

void on_quotas_save(GtkWidget *self, dictionary *windows){
    quota_window *window = yon_dictionary_get_data(windows->first,quota_window*);
    // main_window *widgets = yon_dictionary_get_data(windows->first->next,main_window*);
    GtkTreeIter iter;
    if (!strcmp(gtk_entry_get_text(GTK_ENTRY(window->ManualInputEntry)),"")){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),OPERATION_FAILED_EMPTY_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        return;
    }
    const char *string = gtk_entry_get_text(GTK_ENTRY(window->ManualInputEntry));
    const char *type = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->TargetTypeCombo));
    const char *target = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->TargetCombo));
    char *soft = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->SoftRestrictionCheck)) ? yon_char_append(yon_char_from_long(gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->SoftRestrictionSpin))),get_size_mod(GTK_COMBO_BOX(window->SoftRestrictionCombo))) : NULL;
    char *hard = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->HardRestrictionCheck)) ? yon_char_append(yon_char_from_long(gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->HardRestrictionSpin))),get_size_mod(GTK_COMBO_BOX(window->HardRestrictionCombo))) : NULL;
    char *paging = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->PagingLimitCheck)) ? yon_char_append(yon_char_from_long(gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->PagingLimitSpin))),get_size_mod(GTK_COMBO_BOX(window->PagingLimitCombo))) : NULL;
    char *cpu = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->CpuLimitCheck)) ? yon_char_append(yon_char_from_long(gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->CpuLimitSpin))),"%") : NULL;

    int size_read=0;
    int size_write=0;
    config_str limits_read = NULL; 
    config_str limits_write = NULL; 
    dictionary *dict;
    for_dictionaries(dict,window->devices){
        device_limits *limit = yon_dictionary_get_data(dict,device_limits*);
        if (limit&&limit->read){
            char *temp = yon_char_unite(dict->key," ",limit->read,NULL);
            yon_char_parsed_add_or_create_if_exists(limits_read,&size_read,temp);
            free(temp);
        }
        if (limit&&limit->write){
            char *temp = yon_char_unite(dict->key," ",limit->write,NULL);
            yon_char_parsed_add_or_create_if_exists(limits_write,&size_write,temp);
            free(temp);
        }
    }
    
    gtk_list_store_append(main_config.list,&iter);
    gtk_list_store_set(main_config.list,&iter,0,type,1,target,2,soft,3,hard,4,paging,5,cpu,6,yon_char_parsed_to_string(limits_read,size_read,"\n"),7,yon_char_parsed_to_string(limits_write,size_write,"\n"),8,string,-1);
    yon_char_parsed_free(limits_read,size_read);
    yon_char_parsed_free(limits_write,size_write);
    yon_config_register(CGROUP_QUOTA((char*)target),CGROUP_QUOTA_comd((char*)target),yon_char_new((char*)string));
    on_subwindow_close(self);
        yon_ubl_status_box_render(SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
}

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

void yon_interface_update(main_window *){
    gtk_list_store_clear(main_config.list);
        int parameters_size;
        config_str rtn = yon_config_get_all(&parameters_size);
        char *quota = NULL;
        for (int i=0;i<parameters_size;i++){
            quota=yon_char_new(rtn[i]);
            char *name = yon_char_divide_search(quota,"=",-1);
            if (quota[0]=='\"') yon_char_divide(quota,0);
            if (quota[strlen(quota)-1]=='\"') quota[strlen(quota)-1]='\0';
            yon_char_divide_search(name,"[",-1);
            name = yon_char_divide_search(name,"]",-1);
            name = yon_char_divide_search(name,"=",-1);
            char *type = NULL; 
            if (strstr(name,".service")) type=STR_PROCESS;
            else if (strstr(name,".slice")) type=STR_SLICE;
            else type=STR_USER;
            int size=0;
            char *high = NULL;
            char *max = NULL;
            char *swap = NULL;
            char *cpu = NULL;
            char *read = "";
            char *write = "";
            config_str single_quota = yon_char_parse(quota,&size,",");
            if (!single_quota) {single_quota = g_malloc0(sizeof(char*));single_quota[0]=quota;}
            for (int j = 0; j < (size==0 ? 1 : size); j++){
                if (strstr(single_quota[j],"MemoryHigh")){
                    high=yon_char_new(single_quota[j]);
                    yon_char_divide_search(high,"=",-1);
                }
                if (strstr(single_quota[j],"MemoryMax")){
                    max=yon_char_new(single_quota[j]);
                    yon_char_divide_search(max,"=",-1);
                }
                if (strstr(single_quota[j],"MemorySwapMax")){
                    swap=yon_char_new(single_quota[j]);
                    yon_char_divide_search(swap,"=",-1);
                }
                if (strstr(single_quota[j],"CPUQuota")){
                    cpu=yon_char_new(single_quota[j]);
                    yon_char_divide_search(cpu,"=",-1);
                }
                if (strstr(single_quota[j],"IOReadBandwidthMax")){
                    char *qta = yon_char_new(single_quota[j]);
                    yon_char_divide_search(qta,"=",-1);
                    read=yon_char_unite(strcmp(read,"")!=0 ? yon_char_append(read,"\n"):read,qta,NULL);
                    free(qta);
                }
                if (strstr(single_quota[j],"IOWriteBandwidthMax")){
                    char *qta = yon_char_new(single_quota[j]);
                    yon_char_divide_search(qta,"=",-1);
                    write=yon_char_unite(strcmp(write,"")!=0 ? yon_char_append(write,"\n"):write,qta,NULL);
                    free(qta);
                }
            }
            GtkTreeIter iter;
            gtk_list_store_append(main_config.list,&iter);
            gtk_list_store_set(main_config.list,&iter,0,type,1,name,2,high,3,max,4,swap,5,cpu,6,read,7,write,8,quota,-1);
        }
    
}

void on_config_global_local_save(GtkWidget *, main_window *){
    main_config.save_config=3;
    yon_save_proceed(NULL,YON_CONFIG_BOTH,config_get_global_command,NULL);
}

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

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

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

void on_config_local_load(GtkWidget *,main_window *widgets){
        yon_load_proceed(YON_CONFIG_LOCAL);
        yon_interface_update(widgets);
        main_config.load_mode=1;
}

void on_config_global_load(GtkWidget *,main_window *widgets){
        yon_load_proceed(YON_CONFIG_GLOBAL);
        yon_interface_update(widgets);
        main_config.load_mode=0;
    
}

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

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

void on_apply_filters(GtkWidget *self, dictionary *dict){
    GList *columns = yon_dictionary_get_data(dict->first,GList*);
    GList *checks = yon_dictionary_get_data(dict->first->next,GList*);
    if (g_list_length(columns)==g_list_length(checks)){
        for (guint i=0;i<g_list_length(columns);i++){
            if (gtk_toggle_button_get_active((GtkToggleButton*)g_list_nth_data(checks,i)))
                gtk_tree_view_column_set_visible((GtkTreeViewColumn*)g_list_nth_data(columns,i),1);
            else {
                gtk_tree_view_column_set_visible((GtkTreeViewColumn*)g_list_nth_data(columns,i),0);
            }
        }
    } else {
        printf("Error! Unequal quantity of filters and tree columns!");
    }
    on_subwindow_close(self);
}

void on_quota_manual_input(GtkWidget *self, quota_window *window){
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->DeviceLimitCombo),-1);
    int size=0;
    char *string = yon_char_new((char*)gtk_entry_get_text(GTK_ENTRY(self)));
    config_str rtn = yon_char_parse(string,&size,",");
    if (rtn){
        for (int i=0; i<size; i++){
            char *value = yon_char_new(rtn[i]);
            yon_char_divide_search(value,"=",-1);
            int size = yon_get_size_request_from_letter(value[strlen(value)-1]);
            if (size!=-1||strstr(rtn[i],"CPUQuota=")){
                // value[strlen(value)-1]='\0';
                if (strstr(rtn[i],"MemoryHigh=")){
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->SoftRestrictionCheck),1);
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->SoftRestrictionSpin),strtol(value,NULL,10));
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->SoftRestrictionCombo),size);
                } else if (strstr(rtn[i],"MemoryMax=")){
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->HardRestrictionCheck),1);
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->HardRestrictionSpin),strtol(value,NULL,10));
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->HardRestrictionCombo),size);
                    
                } else if (strstr(rtn[i],"MemorySwapMax=")){
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->PagingLimitCheck),1);
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->PagingLimitSpin),strtol(value,NULL,10));
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->PagingLimitCombo),size);

                } else if (strstr(rtn[i],"CPUQuota=")){
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->CpuLimitCheck),1);
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window-> CpuLimitSpin),strtol(value,NULL,10));

                } else if (strstr(rtn[i],"IOReadBandwidthMax=")){
                    char *name = yon_char_divide_search(value, " ",-1);
                    dictionary *dict;
                    for_dictionaries(dict,window->devices){
                        if (!strcmp(dict->key,name)){
                            if (!((device_limits*)dict->data)){
                                dict->data = g_malloc0(sizeof(device_limits));
                                ((device_limits*)dict->data)->write=NULL;
                                ((device_limits*)dict->data)->read=NULL;
                            }
                            ((device_limits*)dict->data)->read = value;
                        }
                    }

                } else if (strstr(rtn[i],"IOWriteBandwidthMax=")){
                    char *name = yon_char_divide_search(value, " ",-1);
                    dictionary *dict;
                    for_dictionaries(dict,window->devices){
                        if (!strcmp(dict->key,name)){
                            if (!((device_limits*)dict->data)){
                                dict->data = g_malloc0(sizeof(device_limits));
                                ((device_limits*)dict->data)->write=NULL;
                                ((device_limits*)dict->data)->read=NULL;
                            }
                            ((device_limits*)dict->data)->write = value;
                        }
                    }

                } else return;
            }
            free(value);
        }
    } else {
            char *value = yon_char_new(string);
            yon_char_divide_search(value,"=",-1);
            int size = yon_get_size_request_from_letter(value[strlen(value)-1]);
            if (size!=-1||strstr(string,"CPUQuota=")){
                // value[strlen(value)-1]='\0';
                if (strstr(string,"MemoryHigh=")){
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->SoftRestrictionCheck),1);
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->SoftRestrictionSpin),strtol(value,NULL,10));
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->SoftRestrictionCombo),size);
                } else if (strstr(string,"MemoryMax=")){
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->HardRestrictionCheck),1);
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->HardRestrictionSpin),strtol(value,NULL,10));
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->HardRestrictionCombo),size);
                    
                } else if (strstr(string,"MemorySwapMax=")){
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->PagingLimitCheck),1);
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->PagingLimitSpin),strtol(value,NULL,10));
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->PagingLimitCombo),size);

                } else if (strstr(string,"CPUQuota=")){
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->CpuLimitCheck),1);
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window-> CpuLimitSpin),strtol(value,NULL,10));

                } else if (strstr(string,"IOReadBandwidthMax=")){
                    char *name = yon_char_divide_search(value, " ",-1);
                    dictionary *dict;
                    for_dictionaries(dict,window->devices){
                        if (strstr(dict->key,name)){
                            if (!((device_limits*)dict->data)){
                                dict->data = g_malloc0(sizeof(device_limits));
                                ((device_limits*)dict->data)->write=NULL;
                                ((device_limits*)dict->data)->read=NULL;
                            }
                            ((device_limits*)dict->data)->read = value;
                            break;
                        }
                    }

                } else if (strstr(string,"IOWriteBandwidthMax=")){
                    char *name = yon_char_divide_search(value, " ",-1);
                    dictionary *dict;
                    for_dictionaries(dict,window->devices){
                        if (strstr(dict->key,name)){
                            if (!((device_limits*)dict->data)){
                                dict->data = g_malloc0(sizeof(device_limits));
                                ((device_limits*)dict->data)->write=NULL;
                                ((device_limits*)dict->data)->read=NULL;
                            }
                            ((device_limits*)dict->data)->write = value;
                        }
                    }

                } else return;
            }
    }
}

void on_quota_manual_input_focus (GtkWidget *self,  GdkEventFocus *, quota_window *window){
    on_quota_manual_input(self,window);
}

void on_filters_opened(GtkWidget *, main_window *widgets) {
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_filters_path);
    filters_window *window = g_malloc0(sizeof(filters_window));

    window->Window = yon_gtk_builder_get_widget(builder, "FiltersWindow");
    window->QuotaTypeCheck = yon_gtk_builder_get_widget(builder, "QuotaTypeCheck");
    window->QuotaTargetCheck = yon_gtk_builder_get_widget(builder, "QuotaTargetCheck");
    window->SoftRamLimitCheck = yon_gtk_builder_get_widget(builder, "SoftRamLimitCheck");
    window->HardRamLimitCheck = yon_gtk_builder_get_widget(builder, "HardRamLimitCheck");
    window->PagingLimitCheck = yon_gtk_builder_get_widget(builder, "PagingLimitCheck");
    window->CpuLimitCheck = yon_gtk_builder_get_widget(builder, "CpuLimitCheck");
    window->ReadLimitCheck = yon_gtk_builder_get_widget(builder, "ReadLimitCheck");
    window->WriteLimitCheck = yon_gtk_builder_get_widget(builder, "WriteLimitCheck");

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

    GList *columns = gtk_tree_view_get_columns(GTK_TREE_VIEW(widgets->MainTree));
    GList *checks = gtk_container_get_children(GTK_CONTAINER(gtk_widget_get_parent(window->QuotaTypeCheck)));
    if (g_list_length(columns)==g_list_length(checks)){
        for (guint i=0;i<g_list_length(columns);i++){
            if (gtk_tree_view_column_get_visible((GtkTreeViewColumn*)g_list_nth_data(columns,i)))
                gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON((GtkToggleButton*)g_list_nth_data(checks,i)),1);
        }
    } else {
        printf("Error! Unequal quantity of filters and tree columns!");
    }

    dictionary *dict = NULL;
    yon_dictionary_add_or_create_if_exists_with_data(dict,"columns",columns);
    yon_dictionary_add_or_create_if_exists_with_data(dict,"checks",checks);

    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_apply_filters),dict);
    
    gtk_window_set_title(GTK_WINDOW(window->Window),TITLE_LABEL);
    gtk_widget_show(window->Window);
}

void on_add_open(GtkWidget *, main_window *widgets){
    quota_window *window = yon_quota_window_new();

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

    dictionary *windows = NULL;
    yon_dictionary_add_or_create_if_exists_with_data(windows,"window",window);
    yon_dictionary_add_or_create_if_exists_with_data(windows,"widgets",widgets);
    g_signal_connect(G_OBJECT(window->SaveButton),"clicked",G_CALLBACK(on_quotas_save),windows);


    gtk_adjustment_set_upper(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(window->SoftRestrictionSpin)),main_config.avaliable_ram);
    gtk_adjustment_set_upper(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(window->HardRestrictionSpin)),main_config.avaliable_ram);
    gtk_adjustment_set_upper(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(window->PagingLimitSpin)),main_config.avaliable_swap);
    gtk_adjustment_set_upper(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(window->CpuLimitSpin)),main_config.cores*100);
    gtk_adjustment_set_upper(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(window->DeviceLimitReadSpin)),main_config.avaliable_ram);
    gtk_adjustment_set_upper(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(window->DeviceLimitWriteSpin)),main_config.avaliable_ram);

    gtk_widget_show(window->Window);
}

void on_information_chosen_changed(GtkWidget *self, main_window *widgets){
    const char *chosen = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(self));
    if (chosen){
        char *command = get_information_command((char*)chosen);
        main_config.last_info = yon_char_new((char*)chosen);
        vte_terminal_reset(VTE_TERMINAL(widgets->InformationTerminal),1,1);
        yon_terminal_integrated_start(widgets->InformationTerminal, command);
    }
}

void on_information(GtkWidget *, main_window *widgets){
    GtkTreeModel *model = GTK_TREE_MODEL(main_config.list);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&model,&iter)){
        char *name, *group;
        gtk_tree_model_get(model,&iter,1,&name,0,&group,-1);
        g_signal_handlers_block_by_func(G_OBJECT(widgets->TargetCombo),on_information_chosen_changed,widgets);
        if (strcmp(group,STR_PROCESS)==0){
            gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->TargetTypeCombo),1);
        } else if (strcmp(group,STR_SLICE)==0){
            gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->TargetTypeCombo),2);
        } else {
            gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->TargetTypeCombo),0);
        }
        gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->TargetCombo),yon_gtk_combo_box_text_find(widgets->TargetCombo,name));
        g_signal_handlers_unblock_by_func(G_OBJECT(widgets->TargetCombo),on_information_chosen_changed,widgets);
        char *command = get_information_command((char*)name);
        main_config.last_info = yon_char_new((char*)name);
        vte_terminal_reset(VTE_TERMINAL(widgets->InformationTerminal),1,1);
        yon_terminal_integrated_start(widgets->InformationTerminal, command);
        gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->MainNotebook),3);
        yon_ubl_status_box_render(SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
    }
    else yon_ubl_status_box_render(NOTHING_CHOSEN_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
}

void on_information_update(GtkWidget *, main_window *widgets){
    char *command = get_information_command(main_config.last_info);
        vte_terminal_reset(VTE_TERMINAL(widgets->InformationTerminal),1,1);
    yon_terminal_integrated_start(widgets->InformationTerminal, command);
}

void on_remove(GtkWidget *, main_window *widgets){
    GtkTreeModel *model = GTK_TREE_MODEL(main_config.list);
    GtkTreeIter iter;
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&model,&iter)){
        char *name;
        gtk_tree_model_get(model,&iter,1,&name,-1);
        dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
        data->title = REMOVE_TITLE_LABEL;
        data->action_text = REMOVE_VALIDATION_LABEL(name);
        if (yon_confirmation_dialog_call(widgets->Window,data)==GTK_RESPONSE_ACCEPT){
            yon_config_remove_by_key(CGROUP_QUOTA(name));
            gtk_list_store_remove(main_config.list,&iter);
            yon_ubl_status_box_render(SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
        }
    }
    else yon_ubl_status_box_render(NOTHING_CHOSEN_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);

}

void on_quotas_edit(GtkWidget *self, dictionary *windows){
    GtkTreeModel *model = GTK_TREE_MODEL(main_config.list);
    quota_window *window = yon_dictionary_get_data(windows->first,quota_window*);
    main_window *widgets = yon_dictionary_get_data(windows->first->next,main_window*);
    GtkTreeIter iter;
    if (!strcmp(gtk_entry_get_text(GTK_ENTRY(window->ManualInputEntry)),"")){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),OPERATION_FAILED_EMPTY_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        return;
    }
    if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&model,&iter)){
        const char *string = gtk_entry_get_text(GTK_ENTRY(window->ManualInputEntry));
        const char *target = gtk_label_get_text(GTK_LABEL(window->TargetLabel));
        char *soft = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->SoftRestrictionCheck)) ? yon_char_append(yon_char_from_long(gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->SoftRestrictionSpin))),get_size_mod(GTK_COMBO_BOX(window->SoftRestrictionCombo))) : NULL;
        char *hard = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->HardRestrictionCheck)) ? yon_char_append(yon_char_from_long(gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->HardRestrictionSpin))),get_size_mod(GTK_COMBO_BOX(window->HardRestrictionCombo))) : NULL;
        char *paging = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->PagingLimitCheck)) ? yon_char_append(yon_char_from_long(gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->PagingLimitSpin))),get_size_mod(GTK_COMBO_BOX(window->PagingLimitCombo))) : NULL;
        char *cpu = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->CpuLimitCheck)) ? yon_char_append(yon_char_from_long(gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->CpuLimitSpin))),"%") : NULL;
        char *read_limit = NULL;
        char *read_devices = NULL;
        dictionary *dict;
        for_dictionaries(dict,window->devices){
            if (dict->data&&yon_dictionary_get_data(dict,device_limits*)->read)
            read_devices = yon_char_unite(read_devices?read_devices:"","",dict->key," ",yon_dictionary_get_data(dict,device_limits*)->read,"\n",NULL);
        }
        if (!yon_char_is_empty(read_devices)){
            yon_char_remove_last_symbol(read_devices,'\n');
        }
        read_limit = yon_char_unite(read_limit?read_limit:"",read_devices,NULL);
        
        
        char *write_limit = NULL;
        char *write_devices = NULL;
        for_dictionaries(dict,window->devices){
            if (dict->data&&yon_dictionary_get_data(dict,device_limits*)->write)
            write_devices = yon_char_unite(write_devices?write_devices:"","",dict->key," ",yon_dictionary_get_data(dict,device_limits*)->write,"\n",NULL);
        }
        if (!yon_char_is_empty(write_devices)){
            yon_char_remove_last_symbol(write_devices,'\n');
        }
        write_limit = yon_char_unite(write_limit?write_limit:"",write_devices,NULL);
        gtk_list_store_set(main_config.list,&iter,1,target,2,soft,3,hard,4,paging,5,cpu,6,read_limit,7,write_limit,8,string,-1);
        yon_config_register(CGROUP_QUOTA(target),CGROUP_QUOTA_comd(target),(char*)string);
        on_subwindow_close(self);
        yon_ubl_status_box_render(SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
    }
    else yon_ubl_status_box_render(NOTHING_CHOSEN_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
}

void yon_widget_set_sensitive(GtkComboBox *toggle, GtkWidget *target){
    if (gtk_combo_box_get_active(toggle)>-1){
        gtk_widget_set_sensitive(target,1);
    } else {
        gtk_widget_set_sensitive(target,0);
    }
}

void on_manual_edit(GtkWidget *, quota_window *window){
    yon_quota_update(window);
}

quota_window *yon_quota_window_new(){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_quota_path);
    quota_window *window = g_malloc0(sizeof(quota_window));

    window->Window = yon_gtk_builder_get_widget(builder,"QuotasWindow");

    window->TitleLabel = yon_gtk_builder_get_widget(builder,"TitleLabel");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->BlockUiBox = yon_gtk_builder_get_widget(builder,"BlockUiBox");
    window->TargetTypeCombo = yon_gtk_builder_get_widget(builder,"TargetTypeCombo");
    window->AddQuotaTargetBox = yon_gtk_builder_get_widget(builder,"AddQuotaTargetBox");
    window->EditQuotaTargetBox = yon_gtk_builder_get_widget(builder,"EditQuotaTargetBox");
    window->TargetCombo = yon_gtk_builder_get_widget(builder,"TargetCombo");
    window->TargetLabel = yon_gtk_builder_get_widget(builder,"TargetLabel");
    window->TypeLabel = yon_gtk_builder_get_widget(builder,"TypeLabel");

    window->SoftRestrictionCheck = yon_gtk_builder_get_widget(builder,"SoftRestrictionCheck");
    window->SoftRestrictionSpin = yon_gtk_builder_get_widget(builder,"SoftRestrictionSpin");
    window->SoftRestrictionCombo = yon_gtk_builder_get_widget(builder,"SoftRestrictionCombo");

    window->HardRestrictionCheck = yon_gtk_builder_get_widget(builder,"HardRestrictionCheck");
    window->HardRestrictionSpin = yon_gtk_builder_get_widget(builder,"HardRestrictionSpin");
    window->HardRestrictionCombo = yon_gtk_builder_get_widget(builder,"HardRestrictionCombo");

    window->PagingLimitCheck = yon_gtk_builder_get_widget(builder,"PagingLimitCheck");
    window->PagingLimitSpin = yon_gtk_builder_get_widget(builder,"PagingLimitSpin");
    window->PagingLimitCombo = yon_gtk_builder_get_widget(builder,"PagingLimitCombo");

    window->CpuLimitCheck = yon_gtk_builder_get_widget(builder,"CpuLimitCheck");
    window->CpuLimitSpin = yon_gtk_builder_get_widget(builder,"CpuLimitSpin");

    window->DeviceLimitCombo = yon_gtk_builder_get_widget(builder,"DeviceLimitCombo");

    window->DeviceLimitReadCheck = yon_gtk_builder_get_widget(builder,"DeviceLimitReadCheck");
    window->DeviceLimitReadSpin = yon_gtk_builder_get_widget(builder,"DeviceLimitReadSpin");
    window->DeviceLimitReadCombo = yon_gtk_builder_get_widget(builder,"DeviceLimitReadCombo");

    window->DeviceLimitWriteCheck = yon_gtk_builder_get_widget(builder,"DeviceLimitWriteCheck");
    window->DeviceLimitWriteSpin = yon_gtk_builder_get_widget(builder,"DeviceLimitWriteSpin");
    window->DeviceLimitWriteCombo = yon_gtk_builder_get_widget(builder,"DeviceLimitWriteCombo");
    window->ApplyChangesButton = yon_gtk_builder_get_widget(builder,"ApplyChangesButton");
    window->ManualInputEntry = yon_gtk_builder_get_widget(builder,"ManualInputEntry");

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

    window->devices = NULL;
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->SoftRestrictionCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->SoftRestrictionCombo);
    g_signal_connect(G_OBJECT(window->SoftRestrictionCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->SoftRestrictionSpin);
    g_signal_connect(G_OBJECT(window->HardRestrictionCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->HardRestrictionCombo);
    g_signal_connect(G_OBJECT(window->HardRestrictionCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->HardRestrictionSpin);
    g_signal_connect(G_OBJECT(window->PagingLimitCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->PagingLimitCombo);
    g_signal_connect(G_OBJECT(window->PagingLimitCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->PagingLimitSpin);
    g_signal_connect(G_OBJECT(window->CpuLimitCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->CpuLimitSpin);
    g_signal_connect(G_OBJECT(window->DeviceLimitReadCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->DeviceLimitReadCombo);
    g_signal_connect(G_OBJECT(window->DeviceLimitReadCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->DeviceLimitReadSpin);
    g_signal_connect(G_OBJECT(window->DeviceLimitWriteCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->DeviceLimitWriteCombo);
    g_signal_connect(G_OBJECT(window->DeviceLimitWriteCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->DeviceLimitWriteSpin);
    g_signal_connect(G_OBJECT(window->DeviceLimitCombo),"changed",G_CALLBACK(yon_widget_set_sensitive),window->DeviceLimitReadCheck);
    g_signal_connect(G_OBJECT(window->DeviceLimitCombo),"changed",G_CALLBACK(yon_widget_set_sensitive),window->DeviceLimitWriteCheck);
    g_signal_connect(G_OBJECT(window->DeviceLimitWriteCombo),"changed",G_CALLBACK(on_check_maximum),window->DeviceLimitWriteSpin);
    g_signal_connect(G_OBJECT(window->DeviceLimitReadCombo),"changed",G_CALLBACK(on_check_maximum),window->DeviceLimitReadSpin);
    gtk_combo_box_set_active(GTK_COMBO_BOX(window->DeviceLimitCombo),0);
    g_signal_connect(G_OBJECT(window->DeviceLimitCombo),"changed",G_CALLBACK(on_device_current_changed),window);
    g_signal_connect(G_OBJECT(window->DeviceLimitReadCheck),"toggled",G_CALLBACK(on_device_set),window);
    g_signal_connect(G_OBJECT(window->DeviceLimitReadSpin),"value-changed",G_CALLBACK(on_device_set),window);
    g_signal_connect(G_OBJECT(window->DeviceLimitReadCombo),"changed",G_CALLBACK(on_device_set),window);
    g_signal_connect(G_OBJECT(window->DeviceLimitWriteCheck),"toggled",G_CALLBACK(on_device_set),window);
    g_signal_connect(G_OBJECT(window->DeviceLimitWriteSpin),"value-changed",G_CALLBACK(on_device_set),window);
    g_signal_connect(G_OBJECT(window->DeviceLimitWriteCombo),"changed",G_CALLBACK(on_device_set),window);
    g_signal_connect(G_OBJECT(window->TargetCombo),"changed",G_CALLBACK(on_target_check_chosen),window);
    g_signal_connect(G_OBJECT(window->SoftRestrictionSpin),"change-value",G_CALLBACK(on_check_maximum),window->SoftRestrictionCombo);
    g_signal_connect(G_OBJECT(window->SoftRestrictionCombo),"changed",G_CALLBACK(on_check_maximum),window->SoftRestrictionSpin);
    g_signal_connect(G_OBJECT(window->HardRestrictionSpin),"change-value",G_CALLBACK(on_check_maximum),window->HardRestrictionCombo);
    g_signal_connect(G_OBJECT(window->HardRestrictionCombo),"changed",G_CALLBACK(on_check_maximum),window->HardRestrictionSpin);
    g_signal_connect(G_OBJECT(window->PagingLimitSpin),"change-value",G_CALLBACK(on_check_maximum_cpu),window->PagingLimitCombo);
    g_signal_connect(G_OBJECT(window->PagingLimitCombo),"changed",G_CALLBACK(on_check_maximum_cpu),window->PagingLimitSpin);
    g_signal_connect(G_OBJECT(window->DeviceLimitReadSpin),"change-value",G_CALLBACK(on_check_maximum),window->DeviceLimitReadCombo);
    g_signal_connect(G_OBJECT(window->DeviceLimitWriteSpin),"change-value",G_CALLBACK(on_check_maximum),window->DeviceLimitWriteCombo);

    g_signal_connect(G_OBJECT(window->SoftRestrictionSpin),"output",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->SoftRestrictionCombo),"changed",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->HardRestrictionSpin),"output",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->HardRestrictionCombo),"changed",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->PagingLimitSpin),"output",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->PagingLimitCombo),"changed",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->CpuLimitSpin),"output",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->DeviceLimitReadSpin),"output",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->DeviceLimitReadCombo),"changed",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->DeviceLimitWriteSpin),"output",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->DeviceLimitWriteCombo),"changed",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->SoftRestrictionCheck),"toggled",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->HardRestrictionCheck),"toggled",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->PagingLimitCheck),"toggled",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->CpuLimitCheck),"toggled",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->DeviceLimitReadCheck),"toggled",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->DeviceLimitWriteCheck),"toggled",G_CALLBACK(on_quota_configuration_update),window);
    g_signal_connect(G_OBJECT(window->TargetTypeCombo),"changed",G_CALLBACK(on_quota_target_type_changed),window);
    g_signal_connect(G_OBJECT(window->ManualInputEntry),"changed",G_CALLBACK(on_manual_edit),window);
    g_signal_connect(G_OBJECT(window->ManualInputEntry),"focus-out-event",G_CALLBACK(yon_manual_string_finish),window);

    yon_get_devices(window);
    window->devices=window->devices->first;

    dictionary *dict;
    for_dictionaries(dict,window->devices){
        char *temp = yon_char_new(yon_dictionary_get_data(dict,device_limits*)->device_name);
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->DeviceLimitCombo),dict->key,temp);
    }
    main_config.users = yon_ubl_get_all_users(&main_config.users_size);
    main_config.processes = yon_config_load(get_processes_command,&main_config.processes_size);
    main_config.slices = yon_config_load(get_slices_command,&main_config.slices_size);
    for(int i=0;i<main_config.users_size;i++){
        yon_char_remove_last_symbol(main_config.users[i],'\n');
    }
    for(int i=0;i<main_config.processes_size;i++){
        yon_char_remove_last_symbol(main_config.processes[i],'\n');
    }
    for(int i=0;i<main_config.slices_size;i++){
        yon_char_remove_last_symbol(main_config.slices[i],'\n');
    }
    for (int i=0; i<main_config.users_size;i++){
        gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(window->TargetCombo),main_config.users[i]);
    }

    struct sysinfo inf;
    sysinfo(&inf);
    main_config.avaliable_ram=inf.totalram;
    main_config.avaliable_swap=inf.totalswap;
    main_config.cores = sysconf(_SC_NPROCESSORS_ONLN);

    gtk_widget_show(window->Window);
    return window;
}

void yon_quota_update(quota_window *window){
    g_signal_handlers_block_by_func(G_OBJECT(window->SoftRestrictionSpin),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->SoftRestrictionCombo),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->HardRestrictionSpin),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->HardRestrictionCombo),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->PagingLimitSpin),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->PagingLimitCombo),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->CpuLimitSpin),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->DeviceLimitReadSpin),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->DeviceLimitReadCombo),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->DeviceLimitWriteSpin),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->DeviceLimitWriteCombo),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->SoftRestrictionCheck),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->HardRestrictionCheck),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->PagingLimitCheck),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->CpuLimitCheck),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->DeviceLimitReadCheck),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->DeviceLimitWriteCheck),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->TargetTypeCombo),G_CALLBACK(on_quota_target_type_changed),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->DeviceLimitReadCheck),G_CALLBACK(on_device_set),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->DeviceLimitReadSpin),G_CALLBACK(on_device_set),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->DeviceLimitReadCombo),G_CALLBACK(on_device_set),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->DeviceLimitWriteCheck),G_CALLBACK(on_device_set),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->DeviceLimitWriteSpin),G_CALLBACK(on_device_set),window);
    g_signal_handlers_block_by_func(G_OBJECT(window->DeviceLimitWriteCombo),G_CALLBACK(on_device_set),window);

    char *parameter = yon_char_new(gtk_entry_get_text(GTK_ENTRY(window->ManualInputEntry)));
    int size;
    config_str parsed = yon_char_parse(parameter,&size,",");
    long soft_restr=0;
    long hard_restr=0;
    long swap_restr=0;
    int cpu=0;

    for (int i=0;i<size;i++){
        int parsed_size=0;
        config_str parsed_parameter = yon_char_parse(parsed[i],&parsed_size,"=");
        if (parsed_size){
            if (!strcmp(parsed_parameter[0],"MemoryHigh")&&parsed_size>1){
                soft_restr = atol(parsed_parameter[1]);
                int size_mult = yon_get_size_get_from_letter(parsed_parameter[1][strlen(parsed_parameter[1])-1]);
                if (size_mult==-1) size_mult=0;
                long hard_current_max = main_config.avaliable_ram;
                for (int i=0;i<size_mult+1;i++)
                    hard_current_max=hard_current_max/1024;
                gtk_adjustment_set_upper(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(window->SoftRestrictionSpin)),hard_current_max);
                if (soft_restr) {
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->SoftRestrictionSpin),soft_restr);
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->SoftRestrictionCombo),size_mult);
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->SoftRestrictionCheck),1);
                } else {
                        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->SoftRestrictionSpin),0);
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->SoftRestrictionCombo),0);
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->SoftRestrictionCheck),0);
                
                }
            } else if (!strcmp(parsed_parameter[0],"MemoryMax")&&parsed_size>1){

                hard_restr = atol(parsed_parameter[1]);
                int size_mult = yon_get_size_get_from_letter(parsed_parameter[1][strlen(parsed_parameter[1])-1]);
                if (size_mult==-1) size_mult=0;
                long hard_current_max = main_config.avaliable_ram;
                for (int i=0;i<size_mult+1;i++)
                    hard_current_max=hard_current_max/1024;
                gtk_adjustment_set_upper(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(window->HardRestrictionSpin)),hard_current_max);
                if (hard_restr){
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->HardRestrictionSpin),hard_restr);
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->HardRestrictionCombo),size_mult);
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->HardRestrictionCheck),1);
                } else {
                        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->HardRestrictionSpin),0);
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->HardRestrictionCombo),0);
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->HardRestrictionCheck),0);
                }
            } else if (!strcmp(parsed_parameter[0],"MemorySwapMax")&&parsed_size>1){
                swap_restr = atol(parsed_parameter[1]);
                int size_mult = yon_get_size_get_from_letter(parsed_parameter[1][strlen(parsed_parameter[1])-1]);
                if (size_mult==-1) size_mult=0;
                long hard_current_max = main_config.avaliable_swap;
                for (int i=0;i<size_mult+1;i++)
                    hard_current_max=hard_current_max/1024;
                gtk_adjustment_set_upper(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(window->PagingLimitSpin)),hard_current_max);
                if (swap_restr){
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->PagingLimitSpin),swap_restr);
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->PagingLimitCombo),size_mult);
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->PagingLimitCheck),1);
                } else {
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->PagingLimitSpin),0);
                    gtk_combo_box_set_active(GTK_COMBO_BOX(window->PagingLimitCombo),0);
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->PagingLimitCheck),0);
                
                }

            } else if (!strcmp(parsed_parameter[0],"CPUQuota")&&parsed_size>1){
                cpu = atoi(parsed_parameter[1]);
                gtk_adjustment_set_upper(gtk_spin_button_get_adjustment(GTK_SPIN_BUTTON(window->CpuLimitSpin)),main_config.cores*100);
                if (cpu){
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->CpuLimitSpin),cpu);
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->CpuLimitCheck),1);
                } else {
                    gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->CpuLimitSpin),0);
                    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->CpuLimitCheck),0);    
                }
            } else if (!strcmp(parsed_parameter[0],"IOReadBandwidthMax")&&parsed_size>1){
                char *device = yon_char_divide_search(parsed_parameter[1]," ",-1);
                dictionary *dict = yon_dictionary_get(&window->devices,device);
                if (dict){
                    device_limits *limit = yon_dictionary_get_data(dict,device_limits*);
                    limit->read = yon_char_new(parsed_parameter[1]);
                }
            } else if (!strcmp(parsed_parameter[0],"IOWriteBandwidthMax")&&parsed_size>1){
                char *device = yon_char_divide_search(parsed_parameter[1]," ",-1);
                dictionary *dict = yon_dictionary_get(&window->devices,device);
                if (dict){
                    device_limits *limit = yon_dictionary_get_data(dict,device_limits*);
                    limit->write = yon_char_new(parsed_parameter[1]);
                }
            }
        }

    }
    if (!soft_restr) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->SoftRestrictionCheck),0);
    if (!hard_restr) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->HardRestrictionCheck),0);
    if (!swap_restr) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->PagingLimitCheck),0);
    if (!cpu) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->CpuLimitCheck),0);

    struct sysinfo inf;
    sysinfo(&inf);
    main_config.avaliable_ram=inf.totalram;
    main_config.avaliable_swap=inf.totalswap;
    main_config.cores = sysconf(_SC_NPROCESSORS_ONLN);

    g_signal_handlers_unblock_by_func(G_OBJECT(window->SoftRestrictionSpin),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->SoftRestrictionCombo),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->HardRestrictionSpin),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->HardRestrictionCombo),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->PagingLimitSpin),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->PagingLimitCombo),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->CpuLimitSpin),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->DeviceLimitReadSpin),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->DeviceLimitReadCombo),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->DeviceLimitWriteSpin),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->DeviceLimitWriteCombo),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->SoftRestrictionCheck),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->HardRestrictionCheck),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->PagingLimitCheck),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->CpuLimitCheck),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->DeviceLimitReadCheck),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->DeviceLimitWriteCheck),G_CALLBACK(on_quota_configuration_update),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->TargetTypeCombo),G_CALLBACK(on_quota_target_type_changed),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->DeviceLimitReadCheck),G_CALLBACK(on_device_set),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->DeviceLimitReadSpin),G_CALLBACK(on_device_set),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->DeviceLimitReadCombo),G_CALLBACK(on_device_set),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->DeviceLimitWriteCheck),G_CALLBACK(on_device_set),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->DeviceLimitWriteSpin),G_CALLBACK(on_device_set),window);
    g_signal_handlers_unblock_by_func(G_OBJECT(window->DeviceLimitWriteCombo),G_CALLBACK(on_device_set),window);
}

void on_edit_open(GtkWidget *, main_window *widgets){
    GtkTreeModel *model = GTK_TREE_MODEL(main_config.list);
    GtkTreeIter iter;
    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&model,&iter)) return;
    quota_window *window = yon_quota_window_new();
    gtk_widget_set_sensitive(window->BlockUiBox,1);
    char *target, *type;
    gtk_tree_model_get(model,&iter,0,&type,1,&target,-1);

        gtk_label_set_text(GTK_LABEL(window->TypeLabel),type);
        gtk_label_set_text(GTK_LABEL(window->TargetLabel),target);
    gtk_window_set_title(GTK_WINDOW(window->Window),EDITING_LABEL);
    gtk_label_set_text(GTK_LABEL(window->TitleLabel),EDITING_LABEL);

    gtk_widget_show(window->EditQuotaTargetBox);
    gtk_widget_hide(window->AddQuotaTargetBox);

    char *parameter = config(CGROUP_QUOTA(target));
    gtk_entry_set_text(GTK_ENTRY(window->ManualInputEntry),parameter);
    yon_quota_update(window);
    dictionary *windows = NULL;
    yon_dictionary_add_or_create_if_exists_with_data(windows,"window",window);
    yon_dictionary_add_or_create_if_exists_with_data(windows,"widgets",widgets);
    g_signal_connect(G_OBJECT(window->SaveButton),"clicked",G_CALLBACK(on_quotas_edit),windows);
}

gboolean on_dispatcher_update(GtkWidget *,main_window *widgets){
    int interval = gtk_spin_button_get_value(GTK_SPIN_BUTTON(widgets->DispatcherUpdateSpin));
    if (interval>0) {
        char* str_second =  yon_char_from_int(interval);
        char *command = yon_char_unite(get_dispatcher_command," -d ",str_second,NULL);
        if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->DispatcherUpdateCheck))) command = yon_char_append(command," -n 1");
        vte_terminal_reset(VTE_TERMINAL(widgets->DispatcherTerminal),1,1);
        yon_terminal_integrated_start_shell(widgets->DispatcherTerminal, command,NULL,NULL);
        free(str_second);
        GValue *val = g_malloc0(sizeof(GValue));
        g_object_get_property(G_OBJECT(widgets->DispatcherUpdateSpin),"has-focus",val);
        g_value_set_boolean(val,0);
        g_object_set_property(G_OBJECT(widgets->DispatcherUpdateSpin),"has-focus",val);
        g_free(val);
    }
    return 0;
}

void on_processes_update(GtkWidget *,main_window *widgets){
    int show_all = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->ShowAllCheck));
    int show_core = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->ShowCoreCheck));
    char *command = get_processes_tree_command;
    if (show_all) yon_char_append(command, " -k");
    if (show_core) yon_char_append(command, " -l");
    yon_terminal_integrated_start_shell(widgets->ProcessesTerminal, command,NULL,NULL);
}

void on_tab_changed(GtkWidget *, GtkWidget *, int , main_window *){
    
}

void on_info_target_type_switched(GtkWidget *self, main_window *widgets){
    int active = gtk_combo_box_get_active(GTK_COMBO_BOX(self));
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(widgets->TargetCombo));
    int size=0;
    config_str str = NULL;
    switch (active){
        case 0:
            str = main_config.users;
            size = main_config.users_size;
        break;
        case 1:
            str = main_config.processes;
            size = main_config.processes_size;
        break;
        case 2:
            str = main_config.slices;
            size = main_config.slices_size;
        break;
    }
    for (int i=0; i<size;i++){
                gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(widgets->TargetCombo),str[i]);
            }
}

// standard functions

// void config_init(){
//     main_config.avaliable_ram=0;
// }

/**yon_main_window_complete(main_window *widgets) 
 * [EN] 
 *  
 * [RU] 
 * Функция настройки основного окна приложения. [widgets] - структура со стандартным интерфейсом. 
*/ 
void yon_main_window_complete(main_window *widgets){ 
    widgets = yon_remalloc(widgets,sizeof(main_window)); 
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path); 
    gtk_box_pack_start(GTK_BOX(widgets->InterfaceBox),yon_gtk_builder_get_widget(builder,"BoxMain"),1,1,0); 
    // Custom widgets configuration 
    {
        widgets->FiltersButton = yon_gtk_builder_get_widget(builder,"FiltersButton");
        widgets->InformationButton = yon_gtk_builder_get_widget(builder,"InformationButton");
        widgets->RemoveButton = yon_gtk_builder_get_widget(builder,"RemoveButton");
        widgets->EditButton = yon_gtk_builder_get_widget(builder,"EditButton");
        widgets->AddButton = yon_gtk_builder_get_widget(builder,"AddButton");

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

        widgets->DispatcherUpdateCheck = yon_gtk_builder_get_widget(builder,"DispatcherUpdateCheck");
        widgets->DispatcherUpdateSpin = yon_gtk_builder_get_widget(builder,"DispatcherUpdateSpin");
        widgets->DispatcherUpdateButton = yon_gtk_builder_get_widget(builder,"DispatcherUpdateButton");
        widgets->DispatcherTerminal = yon_gtk_builder_get_widget(builder,"DispatcherTerminal");

        widgets->ShowAllCheck = yon_gtk_builder_get_widget(builder,"ShowAllCheck");
        widgets->ShowCoreCheck = yon_gtk_builder_get_widget(builder,"ShowCoreCheck");
        widgets->ProcessesUpdateButton = yon_gtk_builder_get_widget(builder,"ProcessesUpdateButton");
        widgets->ProcessesTerminal = yon_gtk_builder_get_widget(builder,"ProcessesTerminal");

        widgets->TargetTypeCombo = yon_gtk_builder_get_widget(builder,"TargetTypeCombo");
        widgets->TargetCombo = yon_gtk_builder_get_widget(builder,"TargetCombo");
        widgets->InformationUpdateButton = yon_gtk_builder_get_widget(builder,"InformationUpdateButton");
        widgets->InformationTerminal = yon_gtk_builder_get_widget(builder,"InformationTerminal");

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

        /* Signal connection | Присоединение сигналов */
        g_signal_connect(G_OBJECT(widgets->FiltersButton),"clicked",G_CALLBACK(on_filters_opened),widgets);
        g_signal_connect(G_OBJECT(widgets->InformationButton),"clicked",G_CALLBACK(on_information),widgets);
        g_signal_connect(G_OBJECT(widgets->RemoveButton),"clicked",G_CALLBACK(on_remove),widgets);
        g_signal_connect(G_OBJECT(widgets->EditButton),"clicked",G_CALLBACK(on_edit_open),widgets);
        g_signal_connect(G_OBJECT(widgets->AddButton),"clicked",G_CALLBACK(on_add_open),widgets);
        g_signal_connect(G_OBJECT(widgets->MainNotebook),"switch-page",G_CALLBACK(on_tab_changed),widgets);
        g_signal_connect(G_OBJECT(widgets->DispatcherUpdateCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),widgets->DispatcherUpdateSpin);
        g_signal_connect(G_OBJECT(widgets->DispatcherUpdateCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),widgets->DispatcherUpdateButton);
        g_signal_connect(G_OBJECT(widgets->DispatcherUpdateCheck),"toggled",G_CALLBACK(on_dispatcher_update),widgets);
        g_signal_connect(G_OBJECT(widgets->DispatcherUpdateSpin),"output",G_CALLBACK(on_dispatcher_update),widgets);
        g_signal_connect(G_OBJECT(widgets->DispatcherUpdateButton),"clicked",G_CALLBACK(on_dispatcher_update),widgets);
        g_signal_connect(G_OBJECT(widgets->ShowAllCheck),"toggled",G_CALLBACK(on_processes_update),widgets);
        g_signal_connect(G_OBJECT(widgets->ShowCoreCheck),"toggled",G_CALLBACK(on_processes_update),widgets);
        g_signal_connect(G_OBJECT(widgets->TargetTypeCombo),"changed",G_CALLBACK(on_info_target_type_switched),widgets);
        g_signal_connect(G_OBJECT(widgets->TargetCombo),"changed",G_CALLBACK(on_information_chosen_changed),widgets);
        g_signal_connect(G_OBJECT(widgets->ProcessesUpdateButton),"clicked",G_CALLBACK(on_processes_update),widgets);
        g_signal_connect(G_OBJECT(widgets->InformationUpdateButton),"clicked",G_CALLBACK(on_information_update),widgets);

        g_signal_connect(G_OBJECT(widgets->SaveMenuItem),"activate",G_CALLBACK(on_config_global_local_save),widgets);
        g_signal_connect(G_OBJECT(widgets->SaveGlobalMenuItem),"activate",G_CALLBACK(on_config_global_save),widgets);
        g_signal_connect(G_OBJECT(widgets->SaveLocalMenuItem),"activate",G_CALLBACK(on_config_local_save),widgets);
        g_signal_connect(G_OBJECT(widgets->SaveCustomMenuItem),"activate",G_CALLBACK(on_config_custom_save),widgets);
        
        g_signal_connect(G_OBJECT(widgets->LoadGlobalMenuItem),"activate",G_CALLBACK(on_config_global_load),widgets);
        g_signal_connect(G_OBJECT(widgets->LoadLocalMenuItem),"activate",G_CALLBACK(on_config_local_load),widgets);
        g_signal_connect(G_OBJECT(widgets->LoadCustomMenuItem),"activate",G_CALLBACK(on_config_custom_load),widgets);

        on_dispatcher_update(NULL,widgets);
        on_processes_update(NULL,widgets);
        yon_window_config_add_listener(widgets->DispatcherUpdateCheck,"DispatcherAutoupdate","active",YON_TYPE_BOOLEAN);
        yon_window_config_add_listener(widgets->ShowAllCheck,"ProcessShowAll","active",YON_TYPE_BOOLEAN);
        yon_window_config_add_listener(widgets->ShowCoreCheck,"ProcessShowCore","active",YON_TYPE_BOOLEAN);
        yon_gtk_tree_view_minimal_fixed_size_set_full(GTK_TREE_VIEW(widgets->MainTree));
        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((GtkWidget*)g_list_nth_data(list,i),yon_char_append("column",yon_char_from_int(i)),"fixed-width",YON_TYPE_INT);
        }
        main_config.users = yon_ubl_get_all_users(&main_config.users_size);
        main_config.processes = yon_config_load(get_processes_command,&main_config.processes_size);
        main_config.slices = yon_config_load(get_slices_command,&main_config.slices_size);
        for(int i=0;i<main_config.users_size;i++){
            yon_char_remove_last_symbol(main_config.users[i],'\n');
        }
        for(int i=0;i<main_config.processes_size;i++){
            yon_char_remove_last_symbol(main_config.processes[i],'\n');
        }
        for(int i=0;i<main_config.slices_size;i++){
            yon_char_remove_last_symbol(main_config.slices[i],'\n');
        }
        on_info_target_type_switched(widgets->TargetTypeCombo,widgets);
    }
    on_config_local_load(NULL,widgets);
    yon_root_button_setup((template_main_window*)widgets,main_config.launch_arguments,main_config.launch_size);
    yon_save_window_set_postsave_function(on_save_done,widgets);
}

int main(int argc, char *argv[]){
    setlocale(LC_ALL, "");
    textdomain (LocaleName);
    
    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);
    template_main_window *widgets = yon_ubl_window_setup(); 

    //turn off if custom presented
    // yon_ubl_settings_window_set_standard_callbacks(widgets,config_get_global_command,config_get_local_command,yon_config_get_custom_command("./"),config_get_default_command,config_get_global_only_parameters,config_get_local_only_parameters);

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