#include "controler.h"

main_window cfg_main_gui;
custom_window cfg_custom_gui;
config_settings_gui main_config;
config_all all_config;
int index_selected = -1;
int note_selected = -1;
// note_selected = 0; user
// note_selected = 1; group
// note_selected = 2; project

void update_time_table(vector* vec_temp) {
    for (int index = 0; index < all_config.v_disk_status.vectorList.total; index++) {
        disk_status* _config_device = (disk_status*)all_config.v_disk_status.pfVectorGet(&all_config.v_disk_status, index);
        for (int i = 0; i < vec_temp->vectorList.total; i++) {
            config_u_g_p* _config = (config_u_g_p*)vec_temp->pfVectorGet(vec_temp, i);
            if (strstr(_config->UNIX_file, _config_device->device)) {
                _config->deferring_size = _config_device->deferring_size;
                _config->deferring_file = _config_device->deferring_file;
            }
        }
    }
}


void entry_user_disk(GtkWidget *self, int* flag_update) {
    update_time_table(&all_config.v_user);
    if (flag_update) {
        all_config.flag_set_data = 1;
        all_config.flag_save = 3;
        temp_on_destroy_subwindow(self);
    }
    int find = fill_tree_view_user_group(&all_config, &cfg_custom_gui.user, "usrquota", 0);
    if (find == 0) {
        g_object_ref(cfg_custom_gui.user.liststore);
        gtk_list_store_clear(cfg_custom_gui.user.liststore);
    }
    wrapper_fill_tree_view_generate(&all_config, &cfg_custom_gui);
}

void entry_device_disk(GtkWidget *self, int* flag_update) {
    
    if (flag_update) {
        all_config.flag_set_data = 1;
        all_config.flag_save = 3;
        view_device_get_gui_data();
        view_device_on_destroy_subwindow(self);
    }
    int find = fill_tree_view_device(&all_config.v_disk_status, &cfg_custom_gui.device, 0);
    if (find == 0) {
        g_object_ref(cfg_custom_gui.device.liststore);
        gtk_list_store_clear(cfg_custom_gui.device.liststore);
    }
    entry_user_disk(0, 0);
    entry_groups_disk(0, 0);
    entry_project_disk(0, 0);
    wrapper_fill_tree_view_generate(&all_config, &cfg_custom_gui);
    tree_view_select_user(NULL);
    tree_view_select_group(NULL);
    tree_view_select_project(NULL);
}

void entry_groups_disk(GtkWidget *self,int* flag_update) {
    update_time_table(&all_config.v_group);
    if (flag_update) {
        all_config.flag_set_data = 1;
        all_config.flag_save = 3;
        temp_on_destroy_subwindow(self);
    }
    int find = fill_tree_view_user_group(&all_config, &cfg_custom_gui.group,"grpquota", 0);
    if (find == 0) {
        g_object_ref(cfg_custom_gui.group.liststore);
        gtk_list_store_clear(cfg_custom_gui.group.liststore);
    }
    wrapper_fill_tree_view_generate(&all_config, &cfg_custom_gui);
}

void entry_project_disk(GtkWidget *self,int* flag_update) {
    update_time_table(&all_config.v_project);
    if (flag_update) {
        all_config.flag_set_data = 1;
        all_config.flag_save = 3;
        temp_on_destroy_subwindow(self);
    }
    int find = fill_tree_view_project(&all_config, &cfg_custom_gui.project,"prjquota", 0);
    if (find == 0) {
        g_object_ref(cfg_custom_gui.project.liststore);
        gtk_list_store_clear(cfg_custom_gui.project.liststore);
    }
    wrapper_fill_tree_view_generate(&all_config, &cfg_custom_gui);
}

void wrapper_filters_user_show() {
    filters_get_tree_view(cfg_custom_gui.user.tree_view);
    filters_set_name("usrquota");
    filters_show(NULL, glade_path);
}
void wrapper_filters_group_show() {
    filters_get_tree_view(cfg_custom_gui.group.tree_view);
    filters_set_name("grpquota");
    filters_show(NULL, glade_path);
}
void wrapper_filters_project_show() {
    filters_get_tree_view(cfg_custom_gui.project.tree_view);
    filters_set_name("prjquota");
    filters_show(NULL, glade_path);
}

void wrapper_add_user_show() {
    set_temp_menu("usrquota", "add");
    set_all_config(&all_config);
    set_vector(&all_config.v_user);
    temp_show(NULL, glade_path);
    int i = 0;
    g_signal_connect(G_OBJECT(get_widget_temp()->btnSaveTempSave),"clicked",G_CALLBACK(entry_user_disk),&i);
}

void wrapper_edit_user_show() {
    int index = wrapper_select_tree_view(&cfg_custom_gui.user);
    set_temp_menu("usrquota", "edit");
    temp_set_index(index);
    set_all_config(&all_config);
    set_vector(&all_config.v_user);
    temp_show(NULL, glade_path);
    set_edit_gui_data_base(&all_config, index);
    int i = 0;
    g_signal_connect(G_OBJECT(get_widget_temp()->btnSaveTempSave),"clicked",G_CALLBACK(entry_user_disk),&i);
}

void wrapper_add_group_show() {
    set_temp_menu("grpquota", "add");
    set_all_config(&all_config);
    set_vector(&all_config.v_group);
    temp_show(NULL, glade_path);
    int i = 0;
    g_signal_connect(G_OBJECT(get_widget_temp()->btnSaveTempSave),"clicked",G_CALLBACK(entry_groups_disk),&i);
}

void wrapper_edit_group_show() {
    int index = wrapper_select_tree_view(&cfg_custom_gui.group);
    set_temp_menu("grpquota", "edit");
    temp_set_index(index);
    set_all_config(&all_config);
    set_vector(&all_config.v_group);
    temp_show(NULL, glade_path);
    set_edit_gui_data_base(&all_config, index);
    int i = 0;
    g_signal_connect(G_OBJECT(get_widget_temp()->btnSaveTempSave),"clicked",G_CALLBACK(entry_groups_disk),&i);

}

void wrapper_add_project_show() {
    set_temp_menu("prjquota", "add");
    set_all_config(&all_config);
    set_vector(&all_config.v_project);
    temp_show(NULL, glade_path);
    int i = 0;
    g_signal_connect(G_OBJECT(get_widget_temp()->btnSaveTempSave),"clicked",G_CALLBACK(entry_project_disk),&i);
}

void wrapper_edit_project_show(GtkWidget* self, vector* vec_temp) {
    int index = wrapper_select_tree_view(&cfg_custom_gui.project);
    set_temp_menu("prjquota", "edit");
    temp_set_index(index);
    set_all_config(&all_config);
    set_vector(&all_config.v_project);
    temp_show(NULL, glade_path);
    set_edit_gui_data_base(&all_config, index);
    int i = 0;
    g_signal_connect(G_OBJECT(get_widget_temp()->btnSaveTempSave),"clicked",G_CALLBACK(entry_project_disk),&i);
}

void wrapper_edit_view_device_show(GtkWidget* self) {
    view_device_set_vector_device(&all_config.v_disk_status);
    view_device_set_index(index_selected);
    set_view_device_menu("edit");
    view_device_all_config(&all_config);
    view_device_show(NULL, glade_path);
    int i = 0;
    g_signal_connect(G_OBJECT(get_widget_view_device()->btnSaveTempSave),"clicked",G_CALLBACK(entry_device_disk),&i);
}

void on_status_changed(){
    int active = gtk_combo_box_get_active(GTK_COMBO_BOX(get_widget_view_device()->cmbDeviceSetStatus));
    if (active == 0|| active ==1)
        gtk_widget_set_sensitive(get_widget_view_device()->boxBlockDisabledStatus,0);
    else
        gtk_widget_set_sensitive(get_widget_view_device()->boxBlockDisabledStatus,1);
}

void wrapper_add_view_device_show(GtkWidget* self) {
    view_device_set_vector_device(&all_config.v_disk_status);
    view_device_set_index(-1);
    set_view_device_menu("add");
    view_device_all_config(&all_config);
    view_device_show(NULL, glade_path);
    int i = 0;
    g_signal_connect(G_OBJECT(get_widget_view_device()->btnSaveTempSave),"clicked",G_CALLBACK(entry_device_disk),&i);
    g_signal_connect(G_OBJECT(get_widget_view_device()->cmbDeviceSetStatus),"changed",G_CALLBACK(on_status_changed),NULL);
}

void block_tree_view(hotebook* widgets,void (*f)()) {
    g_signal_handlers_block_by_func(G_OBJECT(widgets->tree_view), G_CALLBACK(f), NULL);
}

void unlock_tree_view(hotebook* widgets,void (*f)()) {
    g_signal_handlers_unblock_by_func(G_OBJECT(widgets->tree_view), G_CALLBACK(f), NULL);
}

void tree_view_select(GtkWidget* self, vector* vec_temp, hotebook* widgets) {
    if (self) {}
    if (vec_temp->vectorList.total > 0 && all_config.v_disk_status.vectorList.total>0) {
        wrapper_select_tree_view(widgets);
        GtkTreeIter iter;
        GtkTreeModel *model = GTK_TREE_MODEL(widgets->liststore);
        GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->tree_view));
        if(gtk_tree_selection_get_selected(selection, &model, &iter)) {
            gtk_widget_set_sensitive(widgets->btnAdd, 1);
            gtk_widget_set_sensitive(widgets->btnDel, 1);
            gtk_widget_set_sensitive(widgets->btnEdit, 1);
        } 
        else {
            if (main_config.lock_save_global == 0 || main_config.lock_save_local == 0) {
                gtk_widget_set_sensitive(widgets->btnAdd,1);
            }
            gtk_widget_set_sensitive(widgets->btnDel, 0);
            gtk_widget_set_sensitive(widgets->btnEdit, 0);
        }
    }
    if (vec_temp->vectorList.total > 0 && all_config.v_disk_status.vectorList.total==0) {
        GtkTreeIter iter;
        GtkTreeModel *model = GTK_TREE_MODEL(widgets->liststore);
        GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->tree_view));
        if(gtk_tree_selection_get_selected(selection, &model, &iter)) {
            gtk_widget_set_sensitive(widgets->btnDel, 1);
        } 
        else {
            if (main_config.lock_save_global == 0 || main_config.lock_save_local == 0) {
                gtk_widget_set_sensitive(widgets->btnAdd,1);
            }
            gtk_widget_set_sensitive(widgets->btnAdd, 0);
            gtk_widget_set_sensitive(widgets->btnDel, 0);
            gtk_widget_set_sensitive(widgets->btnEdit, 0);
        }
    }
    else if (!all_config.v_disk_status.vectorList.total && note_selected != 3) {
        gtk_widget_set_sensitive(widgets->btnAdd, 0);
    }
    else if (all_config.v_disk_status.vectorList.total > 0 && note_selected != 3) {
        gtk_widget_set_sensitive(widgets->btnAdd, 1);
    }
    else {
        gtk_widget_set_sensitive(widgets->btnDel, 0);
        gtk_widget_set_sensitive(widgets->btnEdit, 0);
    }
    if (widgets->note_selected == note_selected && index_selected>= 0) {
        gtk_widget_set_sensitive(widgets->btnAdd, 1);
        gtk_widget_set_sensitive(widgets->btnDel, 1);
        gtk_widget_set_sensitive(widgets->btnEdit, 1);
    }
    else {
        gtk_widget_set_sensitive(widgets->btnDel, 0);
        gtk_widget_set_sensitive(widgets->btnEdit, 0);
    } 
}

void device_remove() {
    if (note_selected == 3) {
        GtkTreeIter iter;
        GtkTreeModel *model = GTK_TREE_MODEL(cfg_custom_gui.device.liststore);
        GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cfg_custom_gui.device.tree_view));
        if(gtk_tree_selection_get_selected(selection, &model, &iter)) {
            int index = wrapper_select_tree_view(&cfg_custom_gui.device);
            gtk_list_store_remove(cfg_custom_gui.device.liststore, &iter);
            if (index!=-1) {
                index_selected = -1;
                disk_status* _config = (disk_status*)all_config.v_disk_status.pfVectorGet(&all_config.v_disk_status, index);
                yon_window_config_erase_custom_parameter(_config->device, "deferring_size");
                yon_window_config_erase_custom_parameter(_config->device, "deferring_file");
                yon_window_config_erase_custom_parameter(_config->device, _config->device);
                all_config.v_disk_remove.pfVectorAdd(&all_config.v_disk_remove, yon_char_new(_config->device));
                model_search_disk_remove(&all_config.v_user, &all_config.v_remove, _config->device);
                model_search_disk_remove(&all_config.v_group, &all_config.v_remove, _config->device);
                model_search_disk_remove_project(&all_config.v_project,&all_config.v_disk_status, &all_config.v_remove, _config->device);
                model_clear_cfg_device(_config);
                all_config.v_disk_status.pfVectorDelete(&all_config.v_disk_status, index);
                all_config.flag_save = 1;
                
            }    
        }
    }
    entry_user_disk(0, 0);
    entry_groups_disk(0, 0);
    entry_project_disk(0, 0);
    tree_view_select_user(NULL);
    tree_view_select_group(NULL);
    tree_view_select_project(NULL);
}


void device_update(){
    device_device_config_free(get_device_vec());
    vector_init(get_device_vec());
}

void user_remove() {
    if (note_selected == 0) {
        GtkTreeIter iter;
        GtkTreeModel *model = GTK_TREE_MODEL(cfg_custom_gui.user.liststore);
        GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cfg_custom_gui.user.tree_view));
        if(gtk_tree_selection_get_selected(selection, &model, &iter)) {
            int index = wrapper_select_tree_view(&cfg_custom_gui.user);
            gtk_list_store_remove(cfg_custom_gui.user.liststore, &iter);
            model_template_remove(&all_config.v_user, &all_config.v_remove, index);
            index_selected = -1;
            all_config.flag_save = 1;
        }
        
    }
}

void group_remove() {
    if (note_selected == 1) {
        GtkTreeIter iter;
        GtkTreeModel *model = GTK_TREE_MODEL(cfg_custom_gui.group.liststore);
        GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cfg_custom_gui.group.tree_view));
        if(gtk_tree_selection_get_selected(selection, &model, &iter)) {
            int index = wrapper_select_tree_view(&cfg_custom_gui.group);
            gtk_list_store_remove(cfg_custom_gui.group.liststore, &iter);
            model_template_remove(&all_config.v_group, &all_config.v_remove, index);
            index_selected = -1;
            all_config.flag_save = 1;
        }
    }
}

void project_remove() {
    if (note_selected == 2) {
        GtkTreeIter iter;
        GtkTreeModel *model = GTK_TREE_MODEL(cfg_custom_gui.project.liststore);
        GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(cfg_custom_gui.project.tree_view));
        if(gtk_tree_selection_get_selected(selection, &model, &iter)) {
            int index = wrapper_select_tree_view(&cfg_custom_gui.project);
            gtk_list_store_remove(cfg_custom_gui.project.liststore, &iter);
            model_template_remove(&all_config.v_project, &all_config.v_remove, index);
            index_selected = -1;
            all_config.flag_save = 1;
        }
        
    }
}

int wrapper_select_tree_view(hotebook* widgets) {
    int index = -1;
    GtkTreeIter iter;
    GtkTreeModel *model = GTK_TREE_MODEL(widgets->liststore);
    GtkTreeSelection *selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->tree_view));
    if(gtk_tree_selection_get_selected(selection, &model, &iter)) {
        char* str_iter = gtk_tree_model_get_string_from_iter(model, &iter);
        index = atoi(str_iter);
        index_selected = index;
    }
    else {
        index_selected = -1;
    }
    return index;
}

void wrapper_all_save() {
    int flag_mess_save = 0;
    if (all_config.flag_load == 0) {
        flag_mess_save = wrapper_template_save(&all_config, CMD_ALL_SAVE, CMD_ALL_REMOVE);
    }
    else {
        flag_mess_save = wrapper_template_save(&all_config, CMD_ALL_SAVE, CMD_ALL_REMOVE);
    }
    if (flag_mess_save) {
        yon_ubl_status_box_render(GLOBAL_LOCAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
        all_config.flag_load=0;
    }
    else {
        yon_ubl_status_box_render(MESS_NOTHING_SAVE, BACKGROUND_IMAGE_SUCCESS_TYPE);
    }
}

void wrapper_global_save() {
    if (wrapper_template_save(&all_config, CMD_SAVE_GLOBAL, CMD_REMOVE_GLOBAL)) {
        yon_ubl_status_box_render(GLOBAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
        all_config.flag_load-=1;
    }
    else {
        yon_ubl_status_box_render(MESS_NOTHING_SAVE, BACKGROUND_IMAGE_SUCCESS_TYPE);
    }
}
void wrapper_system_save() {
    if (wrapper_template_save(&all_config, CMD_SAVE_SYSTEM, CMD_REMOVE_SYSTEM)) {
        yon_ubl_status_box_render(LOCAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
        all_config.flag_load-=1;
    }
    else {
        yon_ubl_status_box_render(MESS_NOTHING_SAVE, BACKGROUND_IMAGE_SUCCESS_TYPE);
    }
}
void load_system_cfg() {
    all_config.flag_load = 0;
    load_template_load_cfg(&all_config, CMD_LOAD_SYSTEM);
    yon_ubl_status_box_render(LOCAL_LOAD_SUCCESS_LABEL, BACKGROUND_IMAGE_SUCCESS_TYPE);
    entry_device_disk(0, 0);
    entry_user_disk(0, 0);
    entry_groups_disk(0, 0);
    entry_project_disk(0, 0);
    tree_view_select_user(NULL);
    tree_view_select_group(NULL);
    tree_view_select_project(NULL);
    tree_view_select_device(NULL);
}

void load_global_cfg() {
    all_config.flag_load = 1;
    load_template_load_cfg(&all_config, CMD_LOAD_GLOBAL);
    yon_ubl_status_box_render(GLOBAL_LOAD_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
    entry_device_disk(0, 0);
    entry_user_disk(0, 0);
    entry_groups_disk(0, 0);
    entry_project_disk(0, 0);
    tree_view_select_user(NULL);
    tree_view_select_group(NULL);
    tree_view_select_project(NULL);
    tree_view_select_device(NULL);
}

void tree_view_select_device(GtkWidget* self) {
    note_selected = 3;
    tree_view_select(NULL,&all_config.v_disk_status , &cfg_custom_gui.device);
}

void tree_view_select_user(GtkWidget* self) {
    note_selected = 0;
    tree_view_select(NULL,&all_config.v_user , &cfg_custom_gui.user);
}
void tree_view_select_group(GtkWidget* self) {
    note_selected = 1;
    tree_view_select(NULL,&all_config.v_group ,&cfg_custom_gui.group);
}
void tree_view_select_project(GtkWidget* self) {
    note_selected = 2;
    tree_view_select(NULL,&all_config.v_project , &cfg_custom_gui.project);
}

void control_event(main_window* _config_main_gui, custom_window* _config_custom_gui) {
    g_signal_connect(G_OBJECT(_config_main_gui->LoadLocalMenuItem), "activate", G_CALLBACK(load_system_cfg), NULL);
    g_signal_connect(G_OBJECT(_config_main_gui->LoadGlobalMenuItem), "activate", G_CALLBACK(load_global_cfg), NULL);
    g_signal_connect(G_OBJECT(_config_main_gui->SaveGlobalMenuItem), "activate", G_CALLBACK(wrapper_global_save), NULL);
    g_signal_connect(G_OBJECT(_config_main_gui->SaveLocalMenuItem), "activate", G_CALLBACK(wrapper_system_save), NULL);
    g_signal_connect(G_OBJECT(_config_main_gui->SaveMenuItem), "activate", G_CALLBACK(wrapper_all_save), NULL);
    g_signal_connect(G_OBJECT(_config_custom_gui->user.btnAdd),"clicked",G_CALLBACK(wrapper_add_user_show), glade_path);
    g_signal_connect(G_OBJECT(_config_custom_gui->user.btnEdit),"clicked",G_CALLBACK(wrapper_edit_user_show), glade_path);
    g_signal_connect(G_OBJECT(_config_custom_gui->user.btnDel),"clicked",G_CALLBACK(user_remove), glade_path);
    g_signal_connect(G_OBJECT(_config_custom_gui->group.btnAdd),"clicked",G_CALLBACK(wrapper_add_group_show), glade_path);
    g_signal_connect(G_OBJECT(_config_custom_gui->group.btnEdit),"clicked",G_CALLBACK(wrapper_edit_group_show), glade_path);
    g_signal_connect(G_OBJECT(_config_custom_gui->group.btnDel),"clicked",G_CALLBACK(group_remove), glade_path);
    g_signal_connect(G_OBJECT(_config_custom_gui->project.btnAdd),"clicked",G_CALLBACK(wrapper_add_project_show), glade_path);
    g_signal_connect(G_OBJECT(_config_custom_gui->project.btnEdit),"clicked",G_CALLBACK(wrapper_edit_project_show), glade_path);
    g_signal_connect(G_OBJECT(_config_custom_gui->project.btnDel),"clicked",G_CALLBACK(project_remove), glade_path);
    g_signal_connect(G_OBJECT(_config_custom_gui->user.btnFilter),"clicked",G_CALLBACK(wrapper_filters_user_show), glade_path);
    g_signal_connect(G_OBJECT(_config_custom_gui->group.btnFilter),"clicked",G_CALLBACK(wrapper_filters_group_show), glade_path);
    g_signal_connect(G_OBJECT(_config_custom_gui->project.btnFilter),"clicked",G_CALLBACK(wrapper_filters_project_show), glade_path);
    
    g_signal_connect(G_OBJECT(_config_custom_gui->user.tree_view), "cursor-changed", G_CALLBACK(tree_view_select_user), NULL);
    g_signal_connect(G_OBJECT(_config_custom_gui->group.tree_view), "cursor-changed", G_CALLBACK(tree_view_select_group), NULL);
    g_signal_connect(G_OBJECT(_config_custom_gui->project.tree_view), "cursor-changed", G_CALLBACK(tree_view_select_project), NULL);
    g_signal_connect(G_OBJECT(_config_custom_gui->device.tree_view), "cursor-changed", G_CALLBACK(tree_view_select_device), NULL);
    g_signal_connect(G_OBJECT(_config_custom_gui->device.btnEdit), "clicked", G_CALLBACK(wrapper_edit_view_device_show), glade_path);
    g_signal_connect(G_OBJECT(_config_custom_gui->device.btnAdd), "clicked", G_CALLBACK(wrapper_add_view_device_show), glade_path);
    g_signal_connect(G_OBJECT(_config_custom_gui->device.btnDel),"clicked",G_CALLBACK(device_remove), glade_path);
    g_signal_connect(G_OBJECT(_config_custom_gui->device.Update),"clicked",G_CALLBACK(device_disk_parsed), NULL);
    
}

int main(int argc, char *argv[]){
    all_config.flag_not_data = 0;
    vector_init(&all_config.v_user);
    vector_init(&all_config.v_group);
    vector_init(&all_config.v_project);
    vector_init(&all_config.v_disk_status);
    vector_init(&all_config.v_remove);
    local=setlocale(LC_ALL, "");
    textdomain(LocaleName);
    config_init(&main_config);
    int size_array_size_pow = 0;
    array_size_pow = yon_char_parsed_append(array_size_pow,&size_array_size_pow, STR_KB);
    array_size_pow = yon_char_parsed_append(array_size_pow,&size_array_size_pow, STR_MB);
    array_size_pow = yon_char_parsed_append(array_size_pow,&size_array_size_pow, STR_GB);
    array_size_pow = yon_char_parsed_append(array_size_pow,&size_array_size_pow, STR_TB);
    
    int option_index=0;
    int show_help=0;
    int debug_mode=0;
    {
        struct option long_options[] = {
                {"help", 0, 0, 'h'},
                {"version", 0, 0, 'V'},
                {"lock-help", 0,0, 1},
                {"lock-save", 0,0, 2},
                {"lock-save-local", 0,0, 3},
                {"lock-save-global", 0,0, 4},
                {"lock-load-global", 0,0, 5},
                {"socket-id", 1, 0, 's'},
                {"socket-ext-id", 1,0, 'e'},
                {"socket-trd-id", 1,0, 't'},
                {"debug", 0,0, 'd'},
                {"clean-config", 0,0, 'c'},
                { NULL, 0, NULL, 0 }
        };
        for (int i=0;i<argc;i++){
            int argument=getopt_long(argc,argv,"hVvs:e:t:dc",long_options,&option_index);
            switch(argument){
                case 'h':
                    show_help=1;
                    break;
                case 'v':
                case 'V':
                    printf("%s\n",VERSION_LABEL);
                    exit(0);
                    break;
                case 's':
                    if(optarg)
                        main_config.socket_id=atoi(optarg);
                    break;
                case 'e':
                    if(optarg)
                        main_config.save_socket_id=atoi(optarg);
                    break;
                case 't':
                    if(optarg)
                        main_config.load_socket_id=atoi(optarg); 
                    break;
                case 'c': rmdir(yon_char_unite(yon_ubl_user_get_home_directory(),"/",LocaleName,"/",LocaleName,".conf",NULL));
                    break;
                case 1:
                    main_config.lock_help=1;
                    break;
                case 2:
                    main_config.lock_save_local=1;
                    main_config.lock_save_global=1;
                    break;
                case 3:
                    main_config.lock_save_local=1;
                    break;
                case 4:
                    main_config.lock_save_global=1;
                    break;
                case 5:
                    main_config.lock_load_global=1;
                    break;
            }
        }
        if (show_help==1&&main_config.lock_help!=1){
            printf("%s\n",HELP_LABEL);
            exit(0);
            }
    }

    if (getuid()!=0){
        main_config.lock_load_global=1;
        main_config.lock_save_global=1;
        main_config.lock_save_local=1;
    }
    gtk_init(&argc,&argv);
    
    main_window *widgets = setup_window(&cfg_main_gui,&cfg_custom_gui);
    filters_set_name("prjquota");
    filters_init_flag();
    control_event(widgets, &cfg_custom_gui);
    
    yon_ubl_header_setup_resource(widgets->HeadOverlay,widgets->HeadBox,widgets->HeadImage,banner_path);
    
    yon_ubl_status_box_setup(widgets->StatusIcon,widgets->StatusBox,widgets->StatusLabel);
    yon_window_config_setup(GTK_WINDOW(widgets->Window));
    yon_window_config_load(config_path);
    if (getuid()!=0) {
        load_system_cfg();
        yon_ubl_status_box_render(ROOT_WARNING_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
    } 
    else {
        yon_ubl_status_box_render(LOAD_LOCAL_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
        if (main_config.lock_load_global == 0) {
            load_global_cfg();
        }
        if (main_config.lock_save_local == 0) {
            load_system_cfg();
        }
    }
    
    yon_ubl_setup_sockets(widgets->PlugBox,widgets->LeftBox,widgets->RightBox,main_config.socket_id,main_config.load_socket_id,main_config.save_socket_id);
    GtkCssProvider *css=gtk_css_provider_new();
    gtk_css_provider_load_from_resource(css,CssPath);
    gtk_style_context_add_provider_for_screen(gdk_screen_get_default(),
                            GTK_STYLE_PROVIDER(css),
                            -1);
    tree_view_select_user(NULL);
    tree_view_select_group(NULL);
    tree_view_select_project(NULL);
    tree_view_select_device(NULL);

    GList *list = gtk_tree_view_get_columns(GTK_TREE_VIEW(cfg_custom_gui.treeViewGeneral));
    for (int i = 0; i < g_list_length(list); i++){
        yon_gtk_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(cfg_custom_gui.device.tree_view));
    for (int i = 0; i < g_list_length(list); i++){
        yon_gtk_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(cfg_custom_gui.user.tree_view));
    for (int i = 0; i < g_list_length(list); i++){
        yon_gtk_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(cfg_custom_gui.group.tree_view));
    for (int i = 0; i < g_list_length(list); i++){
        yon_gtk_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(cfg_custom_gui.project.tree_view));
    for (int i = 0; i < g_list_length(list); i++){
        yon_gtk_column_minimal_fixed_size_set((GtkTreeViewColumn*)g_list_nth_data(list,i));
    }
    g_list_free(list);
    gtk_main();
}