#include "ubl-settings-usergroups.h" 
 
config main_config; 
 
//functions 
 
int yon_load_proceed(YON_CONFIG_TYPE type){
	if (type!=YON_CONFIG_CUSTOM){
		yon_config_clean();
	}
	yon_config_load_config(YON_CONFIG_DEFAULT,config_get_default_command,NULL);
    if (type==YON_CONFIG_GLOBAL){
		yon_debug_output("%s\n",yon_char_unite(config_get_command("global"),NULL));
		yon_config_load_config(type,config_get_command("global"),NULL);
	}
	if (type==YON_CONFIG_LOCAL){
		yon_config_load_config(YON_CONFIG_GLOBAL,config_get_global_only_parameters,NULL);
		yon_config_load_config(type,config_get_command("system"),NULL);
		yon_config_set_status(DEFAULTPASSWD_parameter,-2);
		yon_config_set_status(DEFAULTROOTPASSWD_parameter,-2);
		int size;
		config_str passwords = yon_config_load(get_passwords_command,&size);
		for (int i=0;i<size;i++){
			char *key, *value;
			if (yon_config_parse_parameter(passwords[i],&key, &value)){
				int parsed_size;
				char *local_value = yon_config_get_by_key(key);
				config_str parsed = yon_char_parse(value,&parsed_size,":");
				if (strstr(key,USERADD_SEARCH_macro)&&local_value){
					if (parsed_size>5){
						char *temp = yon_char_append(local_value,parsed[5]);
						local_value=temp;
						yon_config_set(key,yon_char_new(local_value));
						yon_config_set_status(key,0);

					}

				} else if (strstr(key,GROUPADD_SEARCH_macro)&&local_value){
					if (parsed_size>4){
						char *temp = yon_char_append(local_value,parsed[4]);
						free(local_value);
						local_value=temp;
						yon_config_set(key,yon_char_new(local_value));
						yon_config_set_status(key,0);
					}
					
				}
				yon_char_parsed_free(parsed,parsed_size);
				free(key);
				free(value);
			}
		}
		return 1;
	}
	else if (type==YON_CONFIG_CUSTOM){
		char *path=NULL;
		textdomain(template_ui_LocaleName);
		char *open_string = OPEN_LABEL;
		textdomain(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_string,GTK_RESPONSE_ACCEPT,NULL);
		yon_gtk_window_setup(GTK_WINDOW(dialog),NULL,CHOOSE_CUSTOM_CONFIG_LABEL,icon_path,"config-open-window");
		GtkFileFilter *filter = gtk_file_filter_new();
		gtk_file_filter_set_name(filter,FILTER_NAME_CONFIG_LABEL);
		gtk_file_filter_add_pattern(filter,"*.ini");
		gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog),filter);
		gtk_widget_show(dialog);
		int response = gtk_dialog_run(GTK_DIALOG(dialog));
		if (response == GTK_RESPONSE_ACCEPT){
			char *file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
			if (!yon_char_is_empty(file)){
				path=file;
			}
			yon_config_clean();
			yon_config_load_config(type,config_get_command(path),NULL);
			gtk_widget_destroy(dialog);
		} else {
			gtk_widget_destroy(dialog);
			return 0;
		}
	}
    return 1;
}

void yon_gtk_tree_view_select_first(GtkTreeView *target){
	GtkTreeIter iter;
	GtkTreeModel *model = gtk_tree_view_get_model(target);
	if (gtk_tree_model_get_iter_first(model,&iter)){
		gtk_tree_selection_select_iter(gtk_tree_view_get_selection(target),&iter);
	}
}

int yon_config_check_valid(){
	int size;
	config_str parameters = yon_config_get_all_keys(&size);

	int invalid_found = 0;
	for (int i=0;i<size;i++){
		int open_brackets = yon_char_count(parameters[i],"[");
		int close_brackets = yon_char_count(parameters[i],"]");
		if (open_brackets!=close_brackets||strstr(parameters[i],"\n")){
			yon_config_remove_by_key(parameters[i]);
			invalid_found = 1;
		}
	}
	if (invalid_found){
		yon_ubl_status_box_render(CONFIG_INVALID_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
		return 0;
	}
	return 1;

}

void yon_interface_update(main_window *widgets){
	if (!yon_config_check_valid()) return;
	on_system_update(NULL,widgets);
	int size;
	GtkTreeIter iter;
	char *def_groups = yon_config_get_by_key(USERGROUPS_parameter);
		config_str users = yon_config_get_all_by_key(USERADD_SEARCH_macro,&size);
		if (users){
			for (int i=0;i<size;i++){
				gtk_list_store_append(widgets->UsersList,&iter);
				int parameters_size;
				config_str parameters = yon_char_parse(users[i],&parameters_size,":");
				free(yon_char_divide(parameters[0],7));
				char *login = NULL;
				if (strstr(parameters[0],"\"")){
					login = yon_char_divide_search(parameters[0],"\"",-1);
					login[strlen(login)-2]='\0';
				} else {
					login = yon_char_divide_search(parameters[0],"=",-1);
					login[strlen(login)-1]='\0';
				}
				if(!strcmp(parameters[0],"x"))
					parameters[0]="";
				if (parameters_size>1&&!strcmp(parameters[1],"x")) parameters[1]="";
				if (parameters_size>2&&!strcmp(parameters[2],"x")) parameters[2]=login;
				char *pars = NULL;
				char *tooltip = AUTH_ALLOWED_LABEL;
				if (parameters_size>5&&strcmp(parameters[5],"x")&&strcmp(parameters[5],"")){
					
					if (!strcmp(parameters[5]," ")){
						pars = NO_PASSWORD_LABEL;
					} else if (!strcmp(parameters[5],"!*")){
						pars = DENY_LOGIN_LABEL;
						tooltip = AUTH_FORBIDDEN_LABEL;
					} else if (parameters[5][0]=='!'){
						pars = BLOCK_LOGIN_LABEL;
						tooltip = AUTH_PASSWORD_LOCKED_LABEL;
					} else if (!check_is_password_hash(parameters[5])||(parameters[5][0]!='%'&&parameters[5][1]!='%')){
						pars = ENCRYPTED_LABEL;
					} else {
						pars = UNENCRYPTED_LABEL;
					}
				} else {
					pars = DEFAULT_USER_LABEL;
					
				}
				GdkRGBA rgba = {0};
				yon_char_check_element(parameters[3],"wheel",",");
				char *param = config(ADDADM_parameter);
				if ((parameters_size>1&&atoi(parameters[1])==1000&&!yon_char_is_empty(param)&&!strcmp(param,"yes"))||yon_char_check_element(parameters[3],"wheel",",")){
					rgba.red=255.0/255.0;
					rgba.green=191.0/255.0;
					rgba.blue=0;
					rgba.alpha=0.17;
				}
				gtk_list_store_set(widgets->UsersList,&iter,
				0,parameters_size>5?(parameters[5][0]=='!'&&parameters[5][1]=='*')?check_checked_icon_name:(parameters[5][0]=='!'||parameters[5][0]=='*')?check_attention_icon_name:NULL:NULL,
				2,login,
				3,parameters_size>0?parameters[0]:"",
				1,parameters_size>1&&!yon_char_is_empty(parameters[1])?parameters[1]:"",
				4,parameters_size>2&&!yon_char_is_empty(parameters[2])?parameters[2]:login,
				5,parameters_size>3?!strcmp(parameters[3],"x")||!strcmp(parameters[3],"")?def_groups:parameters[3]:def_groups,
				6,parameters_size>4?!strcmp(parameters[4],"x")?"":parameters[4]:"",
				7,!yon_char_is_empty(pars)?pars:"",
				8,!yon_char_is_empty(tooltip)?tooltip:"",
				9,rgba.alpha?&rgba:NULL,
				-1);
				if(parameters)
				free(parameters);
			}
			free(users);
		}
		config_str groups = yon_config_get_all_by_key(GROUPADD_SEARCH_macro,&size);
		if (groups){
			for (int i=0;i<size;i++){
				gtk_list_store_append(widgets->GroupsList,&iter);
				int parameters_size;
				config_str parameters = yon_char_parse(groups[i],&parameters_size,":");
				free(yon_char_divide(parameters[0],8));
				char *login;
				if (strstr(parameters[0],"\"")){
					login = yon_char_divide_search(parameters[0],"\"",-1);
					login[strlen(login)-2]='\0';
				} else {
					login = yon_char_divide_search(parameters[0],"=",-1);
					login[strlen(login)-1]='\0';
				}
				if (parameters_size>2&&!strcmp(parameters[2],"x")){
					parameters[2]="";
				}
				if (parameters_size>1&&!strcmp(parameters[1],"x")) parameters[1]="";
				char *pars="";
				char *tooltip=AUTH_ALLOWED_LABEL;
				if (parameters_size>4&&strcmp(parameters[4],"x")&&strcmp(parameters[4],"")){
					  if (!strcmp(parameters[4]," ")){
						pars = NO_PASSWORD_LABEL;
					} else if (!strcmp(parameters[4],"!*")){
						pars = DENY_LOGIN_LABEL;
						tooltip = AUTH_FORBIDDEN_LABEL;
					} else if (parameters[4][0]=='!'){
						pars = BLOCK_LOGIN_LABEL;
						tooltip = AUTH_PASSWORD_LOCKED_LABEL;
					} else if (!check_is_password_hash(parameters[4])&&parameters[4][0]!='%'&&parameters[4][1]!='%'){
						pars = ENCRYPTED_LABEL;
					} else {
						pars = UNENCRYPTED_LABEL;
					}
				} else {
					pars = DEFAULT_USER_LABEL;
					
				}
				gtk_list_store_set(widgets->GroupsList,&iter,
				1,login,
				0,parameters_size>2?parameters[1]:"",
				2,parameters_size>1?parameters[0]:"",
				3,parameters_size>3?parameters[3]:"",
				4,pars,
				5,tooltip,
				-1);
				if(parameters)
				free(parameters);
				
			}
		}

		yon_system_load(widgets);

	
		if (!yon_window_config_check_exist("window","UserColumn1")){
			yon_gtk_tree_view_set_fixed_size(GTK_TREE_VIEW(widgets->UsersTree), 1,-1);

		}
		if (!yon_window_config_check_exist("window","UserColumn2")){
			yon_gtk_tree_view_set_fixed_size(GTK_TREE_VIEW(widgets->UsersTree), 2,-1);
		}
		if (!yon_window_config_check_exist("window","UserColumn3")){
			yon_gtk_tree_view_set_fixed_size(GTK_TREE_VIEW(widgets->UsersTree), 3,-1);
		}

		if (!yon_window_config_check_exist("window","GroupColumn1")){
		yon_gtk_tree_view_set_fixed_size(GTK_TREE_VIEW(widgets->GroupsTree),1,-1);
		}
		if (!yon_window_config_check_exist("window","GroupColumn2")){
		yon_gtk_tree_view_set_fixed_size(GTK_TREE_VIEW(widgets->GroupsTree),2,-1);
		}

		if (!yon_window_config_check_exist("window","SystemUserColumn1")){
		yon_gtk_tree_view_set_fixed_size(GTK_TREE_VIEW(widgets->SystemTree),1,-1);
		}
		if (!yon_window_config_check_exist("window","SystemUserColumn2")){
		yon_gtk_tree_view_set_fixed_size(GTK_TREE_VIEW(widgets->SystemTree),2,-1);
		}
		if (!yon_window_config_check_exist("window","SystemUserColumn3")){
		yon_gtk_tree_view_set_fixed_size(GTK_TREE_VIEW(widgets->SystemTree),3,-1);
		}

		if (!yon_window_config_check_exist("window","SystemGroupColumn2")){
		yon_gtk_tree_view_set_fixed_size(GTK_TREE_VIEW(widgets->SystemGroupsTree),1,-1);
		}
		if (!yon_window_config_check_exist("window","SystemGroupColumn2")){
		yon_gtk_tree_view_set_fixed_size(GTK_TREE_VIEW(widgets->SystemGroupsTree),2,-1);
		}
	gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->ConfigUsersFilter));
	gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->ConfigGroupsFilter));
	gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->SystemUsersFilter));
	gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->SystemGroupsFilter));
	yon_gtk_tree_view_select_first(GTK_TREE_VIEW(widgets->UsersTree));
	yon_gtk_tree_view_select_first(GTK_TREE_VIEW(widgets->GroupsTree));
	yon_gtk_tree_view_select_first(GTK_TREE_VIEW(widgets->SystemTree));
	yon_gtk_tree_view_select_first(GTK_TREE_VIEW(widgets->SystemGroupsTree));

}

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

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

void on_config_custom_load(GtkWidget *,main_window *widgets){
	yon_load_proceed(YON_CONFIG_CUSTOM);
	textdomain(template_ui_LocaleName);
	yon_ubl_status_box_render(GLOBAL_LOAD_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
	textdomain(LocaleName);
	main_config.load_mode=YON_CONFIG_CUSTOM;
	yon_interface_update(widgets);
}
 
void yon_samba_sync_proceed(){
if (main_config.sync_users){
	for (int i=0;i<main_config.sync_size;i++){
		int size;
		config_str parsed = yon_char_parse(main_config.sync_users[i],&size,":");
		if (!yon_char_is_empty(parsed[1])){
			yon_launch (yon_debug_output("%s\n",samba_sync_password_command(parsed[0],parsed[1])));
			
		} else {
			yon_launch (yon_debug_output("%s\n",samba_sync_command(parsed[0])));

		}

		yon_char_parsed_free(parsed,size);
	}
}
}

void yon_accept_changes(){
	if (main_config.homes_size&&main_config.remove_homes){
		char *remove_homes = "";
		for (int i=0;i<main_config.homes_size;i++){
			remove_homes = yon_char_unite(remove_homes,yon_char_is_empty(remove_homes)?"":",",main_config.remove_homes[i],NULL);
		}
		if (!yon_char_is_empty(remove_homes)){
			if (system(remove_home_dirs_command(remove_homes))){};
		}
	}
	main_config.remove_homes=NULL;
	main_config.homes_size=0;
}

char *yon_char_parsed_to_string_include_empty(config_str parsed, int parsed_size, char *divider){
	if (parsed && parsed_size>0){
		char *final = NULL;
		char *temp;
		for (int i=0;i<parsed_size;i++){
				temp = yon_char_unite(final?final:"",final?divider:"",parsed[i],NULL);
				if (!yon_char_is_empty(final)) free(final);
				final=temp;
		}
		if (!yon_char_is_empty(final)){
			return final;
		} 
	} else if (parsed_size==0&&!parsed) return "";
	return NULL;
}

void yon_hide_passwords(template_saving_window *window){
	GtkTreeModel *model = GTK_TREE_MODEL(window->list);
	GtkTreeIter iter;
	int valid = gtk_tree_model_get_iter_first(model,&iter);
	for (;valid;valid = gtk_tree_model_iter_next(model,&iter)){
		char *name;
		char *old_value;
		char *new_value;
		gtk_tree_model_get(model,&iter,1,&name,2,&old_value,3,&new_value,-1);
		int status = yon_config_get_status(name);
		if (strstr(name,USERADD_SEARCH_macro)){
			if ((!yon_char_is_empty(new_value)&&!yon_char_is_empty(old_value)&&strstr(new_value,old_value)&&new_value[0]==old_value[0])&&!status){
				GtkTreeIter *iter_cur = gtk_tree_iter_copy(&iter);
				gtk_list_store_remove(window->list,iter_cur);
				if (!gtk_tree_model_get_iter_first(model,&iter)){
					gtk_widget_destroy(window->Window);
					yon_ubl_status_box_render(yon_char_get_localised_from_lib(NOTHING_TO_SAVE_LABEL),BACKGROUND_IMAGE_FAIL_TYPE);
					return;
				}
				continue;
			}
			char *old=NULL;
			char *newv=NULL;
			int size = 0;
			if (!yon_char_is_empty(new_value)){
				config_str parsed = NULL;
				parsed = yon_char_parse(new_value,&size,":");
				if (size>5&&!yon_char_is_empty(parsed[5])&&strcmp(parsed[5],"x")&&parsed[5][0]!='%'&&parsed[5][1]!='%'){
					free(new_value);
					free(parsed[5]);
					parsed[5]=yon_char_new("*******");
					new_value = yon_char_parsed_to_string_include_empty(parsed,size,":");
				} else if (size>5&&!yon_char_is_empty(parsed[5])&&strcmp(parsed[5],"x")) {
					free(new_value);
					free(yon_char_divide(parsed[5],1));
					new_value = yon_char_parsed_to_string_include_empty(parsed,size,":");
				}
				if (parsed){
					yon_char_parsed_free(parsed,size);
				}
			}
			if(!yon_char_is_empty(old_value)){
				config_str parsed = NULL;
				parsed = yon_char_parse(old_value,&size,":");
				if (size>5&&!yon_char_is_empty(parsed[5])&&!check_is_password_hash(parsed[5])){
					free(old_value);
					free(parsed[5]);
					parsed[5]=yon_char_new("*******");
					old_value = yon_char_parsed_to_string_include_empty(parsed,size,":");
				} else if (size>5&&!yon_char_is_empty(parsed[5])&&strcmp(parsed[5],"x")) {
					free(old_value);
					old_value = yon_char_parsed_to_string_include_empty(parsed,size,":");
				}
				if (parsed){
					yon_char_parsed_free(parsed,size);
				}

				old=yon_char_unite("<s>",old_value,"</s>",NULL);
			}
			if(!yon_char_is_empty(new_value)){
				newv=new_value;
			}
			char *new_string = yon_char_unite(!yon_char_is_empty(old)?old:"","\n",!yon_char_is_empty(newv)?newv:"",NULL);
			gtk_list_store_set(window->list,&iter,4,new_string,-1);

		} else if (strstr(name,GROUPADD_SEARCH_macro)){
			char *old=NULL;
			char *newv=NULL;
			int size = 0;
			if (new_value){
				config_str parsed = yon_char_parse(new_value,&size,":");
				free(new_value);
				if (size>4&&!yon_char_is_empty(parsed[4])&&strcmp(parsed[4],"x")&&parsed[4][0]!='%'&&parsed[4][1]!='%'){
					free(parsed[4]);
					parsed[4]=yon_char_new("*******");
				}
				new_value = yon_char_parsed_to_string_include_empty(parsed,size,":");
			}
			if(!yon_char_is_empty(old_value)){
				config_str parsed = NULL;
				parsed = yon_char_parse(old_value,&size,":");
				if (size>4&&!yon_char_is_empty(parsed[4])&&strcmp(parsed[4],"x")&&parsed[4][0]!='%'&&parsed[4][1]!='%'){
					free(old_value);
					free(parsed[4]);
					parsed[4]=yon_char_new("*******");
					old_value = yon_char_parsed_to_string_include_empty(parsed,size,":");
				} else if (size>4&&!yon_char_is_empty(parsed[4])&&strcmp(parsed[4],"x")) {
					free(old_value);
					free(yon_char_divide(parsed[4],1));
					old_value = yon_char_parsed_to_string_include_empty(parsed,size,":");
				}
				if (parsed){
					yon_char_parsed_free(parsed,size);
				}
				old=yon_char_unite("<s>",old_value,"</s>",NULL);
			}
			if(!yon_char_is_empty(new_value)){
				newv=new_value;
			}
			char *new_string = yon_char_unite(!yon_char_is_empty(old)?old:"","\n",!yon_char_is_empty(newv)?newv:"",NULL);
			gtk_list_store_set(window->list,&iter,4,new_string,-1);
			
			
		} else if (strstr(name,DEFAULTPASSWD_parameter)){
			char *old=NULL;
			char *newv=NULL;
			if (!yon_char_is_empty(new_value)&&strlen(new_value)>2&&new_value[0]=='%'&&new_value[1]=='%'){
				free(yon_char_divide(new_value,1));
				newv=new_value;
			} else if (!yon_char_is_empty(new_value)&&new_value[0]=='$') {
				if (main_config.load_mode==YON_CONFIG_LOCAL){
					newv=new_value;
				} else {
					newv="*******";
				}
			}else if (!yon_char_is_empty(new_value)){
				newv="*******";
			}
			if (!yon_char_is_empty(old_value)){
				if (main_config.load_mode==YON_CONFIG_LOCAL){

					old = yon_char_new("<s>*******</s>");
				} else {
					old = yon_char_unite("<s>",old_value,"</s>",NULL);
				}
			}
			char *new_string = yon_char_unite(!yon_char_is_empty(old)?old:"","\n",!yon_char_is_empty(newv)?newv:"",NULL);
			gtk_list_store_set(window->list,&iter,4,new_string,-1);
			
		} else if (strstr(name,DEFAULTROOTPASSWD_parameter)){
			char *old=NULL;
			char *newv=NULL;
			if (!yon_char_is_empty(new_value)&&strlen(new_value)>2&&new_value[0]=='%'&&new_value[1]=='%'){
				free(yon_char_divide(new_value,1));
				newv=new_value;
			} else if (!yon_char_is_empty(new_value)&&new_value[0]=='$') {
				if (main_config.load_mode==YON_CONFIG_LOCAL){
					newv=new_value;
				} else {
					newv="*******";
				}
			}else if (!yon_char_is_empty(new_value)){
				newv="*******";
			}
			if (!yon_char_is_empty(old_value)){
				if (main_config.load_mode==YON_CONFIG_LOCAL){
					old = yon_char_new("<s>*******</s>");
				} else {
					old = yon_char_unite("<s>",old_value,"</s>",NULL);
				}
			}
			char *new_string = yon_char_unite(!yon_char_is_empty(old)?old:"","\n",!yon_char_is_empty(newv)?newv:"",NULL);
			gtk_list_store_set(window->list,&iter,4,new_string,-1);
			
		}
	}
}

void on_save_done(main_window *widgets, 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);
	if (main_config.load_mode==YON_CONFIG_GLOBAL){
		on_config_global_load(NULL,widgets);
	} else {
		on_config_local_load(NULL,widgets);
	}
	if (main_config.load_mode==YON_CONFIG_LOCAL){
		yon_accept_changes();
		yon_samba_sync_proceed();
	}
	textdomain(template_ui_LocaleName);
	switch (main_config.save_config){
		case 0:
			yon_ubl_status_box_render(GLOBAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
		case 1:
			yon_ubl_status_box_render(LOCAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
		case 2:
			yon_ubl_status_box_render(GLOBAL_LOCAL_SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			break;
		case 3:
			yon_ubl_status_box_render(SAVE_SUCCESS_LABEL,BACKGROUND_IMAGE_SUCCESS_TYPE);
			
	}
	textdomain(LocaleName);
	
}
void on_save_failed(main_window *, config_str , int ){
	yon_samba_sync_proceed();
}


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

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

void on_config_local_save(GtkWidget *, main_window *){
	main_config.save_config=1;

	template_saving_window *window = yon_save_proceed("system",YON_CONFIG_LOCAL,config_get_command("system"),NULL);
	if (window){
		yon_hide_passwords(window);
	}
}

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

void on_saving_settings_save(GtkWidget *self, yon_savasettings_window *window){
	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->SambaCheck))){
		if (!yon_config_append_element(SAVE_ROOTCOPY_CHANGES_parameter,"/var/lib/samba",",")){
			yon_config_register(SAVE_ROOTCOPY_CHANGES_parameter,SAVE_ROOTCOPY_CHANGES_get_command,"/var/lib/samba");
		}
	} else {
		yon_config_remove_element(SAVE_ROOTCOPY_CHANGES_parameter,"/var/lib/samba",",");
	}
	on_subwindow_close(self);
}

void yon_samba_sync_add(char *user, char*password){
	int sync_place = yon_char_parsed_check_exist(main_config.sync_users,main_config.sync_size,yon_char_append(user,":"));
	if (sync_place==-1){
		yon_char_parsed_add_or_create_if_exists(main_config.sync_users,&main_config.sync_size,yon_char_unite(user,":",password,NULL));
	}
}

void yon_samba_sync_remove(char *user){
	int sync_place = yon_char_parsed_check_exist(main_config.sync_users,main_config.sync_size,yon_char_append(user,":"));
	if (sync_place>-1){
		config_str temp = yon_char_parsed_rip(main_config.sync_users,&main_config.sync_size,sync_place);
		main_config.sync_users = temp;
	}

}

int yon_samba_sync_get(char *user){
	int sync_place = yon_char_parsed_check_exist(main_config.sync_users,main_config.sync_size,yon_char_append(user,":"));
	if (sync_place>-1){
		if (!main_config.sync_users)
			return 0;
		int active = main_config.sync_users[sync_place][strlen(main_config.sync_users[sync_place])-1]==':'? 1:2;
		return active;
	}
	return 0;
}

void on_delete_confirmation_delete_home_check(GtkCellRenderer *, char *path, yon_confirmation_window *window){
	GtkTreeIter iter;
	gboolean status;
	gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(window->list),&iter,path);
	gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,3,&status,-1);
	gtk_list_store_set(window->list,&iter,3,!status,-1);

}

void on_delete_confirmation_delete_check(GtkCellRenderer *, char *path, yon_confirmation_window *window){
	GtkTreeIter iter;
	gboolean status;
	char *homedir;
	gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(window->list),&iter,path);
	gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,0,&status,2,&homedir,-1);
	gtk_list_store_set(window->list,&iter,0,!status,4,!status&&!yon_char_is_empty(homedir),-1);

}

char *yon_user_get_group_string(char *user){
	char *cur_user = config(USERADD(user));

	int size;
	config_str parsed = yon_char_parse(cur_user,&size,":");
	if (size>2&&!yon_char_is_empty(parsed[2])){
		char *current_group = parsed[2];
		size_t digits=0;
		for (guint k=0;k<strlen(current_group);k++){
			if (g_ascii_isdigit(current_group[k])){
				digits++;
			}
		}

		char *final_group_name=NULL;

		if (digits&&digits==strlen(current_group)){
			for (int i=0;i<main_config.groups_size;i++){
				int group_size=0;
				config_str groups_parsed = yon_char_parse(main_config.groups[i],&group_size,":");
				if (group_size>2&&!strcmp(groups_parsed[2],current_group)){
					final_group_name=yon_char_new(groups_parsed[0]);
					if (group_size) yon_char_parsed_free(groups_parsed,group_size);
					break;
				}
				if (group_size) yon_char_parsed_free(groups_parsed,group_size);
			}
		} else {
			final_group_name=yon_char_new(current_group);
		}
		return final_group_name;
	} else {
		return user;
	}
	return NULL;
}

char *yon_user_check_groups(GtkListStore *list, char *target){
	char *final="";
	GtkTreeIter iter;
	GtkTreeModel *model = GTK_TREE_MODEL(list);
	for_iter(model,&iter){
		char *name;
		char *group;
		gtk_tree_model_get(model,&iter,1,&name,6,&group,-1);
		if (!strcmp(group,target)&&strcmp(name,target)){
			char *temp = yon_char_unite(final,!yon_char_is_empty(final)?",":"",name,NULL);
			if (!yon_char_is_empty(final)) free(final);
			final = temp;
		}
	}
	return !yon_char_is_empty(final)?final:NULL;
}

void yon_delete_confirmation_save(GtkWidget *self, dictionary *dict){
	main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
	yon_confirmation_window *window = yon_dictionary_get_data(dict->first->next,yon_confirmation_window*);
	GtkTreeIter iter;
	GtkTreeModel *model = GTK_TREE_MODEL(window->list);
	char *single_target = NULL;
	char *main_group = NULL;
	int founds = 0;
	char *depends = NULL;
	for_iter(model,&iter){
		int selected;
		char *target;
		char *group;
		gtk_tree_model_get(model,&iter,0,&selected,1,&target,6,&group,-1);
		if (selected&&!yon_char_is_empty(target)&&!yon_char_is_empty(group)){
			if ((!strcmp(target,group))){
				depends = yon_user_check_groups(window->list,target);
				if (!yon_char_is_empty(depends)) {
					single_target = target;
					main_group = group;
					founds++;
				}
			}
		}
	}
	if (founds==1){
		dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
		data->action_text = CONFIRMATION_GROUP_DEPENDS_SINGLE_LABEL(single_target,depends,main_group);
		if(yon_confirmation_dialog_call(window->Window,data)!=GTK_RESPONSE_ACCEPT){
			return;
		}
	} else if (founds >1){
		dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
		data->action_text = CONFIRMATION_GROUP_DEPENDS_MULTIPLE_LABEL;
		if (yon_confirmation_dialog_call(window->Window,data)!=GTK_RESPONSE_ACCEPT){
			return;
		}
	}

	for_iter(model,&iter){
		int selected;
		char *target;
		gtk_tree_model_get(model,&iter,0,&selected,1,&target,-1);
		if (selected){
			yon_config_remove_by_key(USERADD(target));
			yon_config_remove_by_key(USERSHADOW(target));
			yon_config_remove_by_key(USERADD_SYNC(target));
			yon_config_remove_by_key(USERSHADOW_SYNC(target));
		}
	}

	yon_interface_update(widgets);
	
	on_subwindow_close(self);
}

yon_confirmation_window *yon_delete_confirmation_new(){
	yon_confirmation_window *window = malloc(sizeof(yon_confirmation_window));
	GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_ubl_settings_usergroups_confirmation);
	window->Window = yon_gtk_builder_get_widget(builder,"Window");
	window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
	window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
	window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
	window->DeletionTree = yon_gtk_builder_get_widget(builder,"DeletionTree");
	window->HatLabel = yon_gtk_builder_get_widget(builder,"userTitleNameLabel");
	window->DeleteCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"DeleteCell"));
	window->DeleteHomeCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"DeleteHomeCell"));
	window->UserCell = GTK_TREE_VIEW_COLUMN(gtk_builder_get_object(builder,"UserCell"));
	window->TitleLabel = yon_gtk_builder_get_widget(builder,"TitleLabel");
	window->list = GTK_LIST_STORE(gtk_builder_get_object(builder,"liststore1"));
	gtk_window_set_title(GTK_WINDOW(window->Window),DELETE_CONFIRMATION_TITLE_LABEL);
	g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
	g_signal_connect(G_OBJECT(window->DeleteCell),"toggled",G_CALLBACK(on_delete_confirmation_delete_check),window);
	g_signal_connect(G_OBJECT(window->DeleteHomeCell),"toggled",G_CALLBACK(on_delete_confirmation_delete_home_check),window);
	yon_window_config_custom_window_setup(GTK_WINDOW(window->Window),"DeleteConfirmationWindow");
	return window;
}

void yon_system_delete_confirmation_open(main_window *widgets){
        yon_confirmation_window *dialog = yon_delete_confirmation_new();
        dictionary *dact = NULL;
        yon_dictionary_add_or_create_if_exists_with_data(dact,"widgets",widgets);
        yon_dictionary_add_or_create_if_exists_with_data(dact,"dialog",dialog);
        if (gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook))==2){
        g_signal_connect(G_OBJECT(dialog->AcceptButton),"clicked",G_CALLBACK(on_delete_system_user),dact);
			gtk_label_set_text(GTK_LABEL(dialog->TitleLabel),DELETE_CONFIRMATION_CONFIRM_LABEL);
			gtk_label_set_text(GTK_LABEL(dialog->HatLabel),DELETE_CONFIRMATION_TITLE_LABEL);
			yon_gtk_window_setup(GTK_WINDOW(dialog->Window),GTK_WINDOW(widgets->Window),DELETE_CONFIRMATION_TITLE_LABEL,icon_path,"system-user-deletion-window");
            GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(widgets->SystemTree));
            GtkTreeIter iter,child;
            if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SystemTree)),&model,&iter)){
                char *name,*username,*homedir=NULL;
			yon_tree_model_sort_filter_get_model_iter(GTK_TREE_MODEL_SORT(model),&iter,&child);
                gtk_tree_model_get(model,&child,2,&name,3,&username,6,&homedir,-1);
                gtk_list_store_append(dialog->list,&iter);
                gtk_list_store_set(dialog->list,&iter,0,1,1,name,2,homedir,4,!yon_char_is_empty(homedir),5,username,-1);
                gtk_widget_show(dialog->Window);
            }

        } else {
			g_signal_connect(G_OBJECT(dialog->AcceptButton),"clicked",G_CALLBACK(on_delete_system_group),dact);
			gtk_label_set_text(GTK_LABEL(dialog->TitleLabel),DELETE_GROUP_CONFIRMATION_CONFIRM_LABEL);
			gtk_label_set_text(GTK_LABEL(dialog->HatLabel),DELETE_GROUP_CONFIRMATION_TITLE_LABEL);
			yon_gtk_window_setup(GTK_WINDOW(dialog->Window),GTK_WINDOW(widgets->Window),DELETE_GROUP_CONFIRMATION_TITLE_LABEL,icon_path,"system-group-deletion-window");
            GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(widgets->SystemGroupsTree));
            GtkTreeIter iter,child;
            if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SystemGroupsTree)),&model,&iter)){
			g_signal_connect(G_OBJECT(dialog->AcceptButton),"clicked",G_CALLBACK(on_delete_system_group),dialog);
                char *name;
                gtk_tree_view_column_set_title(dialog->UserCell,GROUP_LABEL);
                gtk_tree_view_column_set_visible(gtk_tree_view_get_column(GTK_TREE_VIEW(dialog->DeletionTree),2),0);
                gtk_tree_view_column_set_visible(gtk_tree_view_get_column(GTK_TREE_VIEW(dialog->DeletionTree),3),0);
                gtk_tree_view_column_set_visible(gtk_tree_view_get_column(GTK_TREE_VIEW(dialog->DeletionTree),4),0);

			yon_tree_model_sort_filter_get_model_iter(GTK_TREE_MODEL_SORT(model),&iter,&child);
                gtk_tree_model_get(model,&child,1,&name,-1);
                gtk_list_store_append(dialog->list,&iter);
                gtk_list_store_set(dialog->list,&iter,0,1,1,name,-1);
                gtk_widget_show(dialog->Window);
            }
        }
}

void yon_delete_confirmation_open(main_window *widgets){
	yon_confirmation_window *window = yon_delete_confirmation_new();
	gtk_window_set_transient_for(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window));
	GtkTreeIter iter,itar,child;
	dictionary *dict = NULL;
	yon_dictionary_add_or_create_if_exists_with_data(dict,"widgets",widgets);
	yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
	g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(yon_delete_confirmation_save),dict);
	GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(widgets->UsersTree));
	GList *list = gtk_tree_selection_get_selected_rows(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->UsersTree)),&model);
	int chosen_size=0;
	config_str chosen = NULL;
	for (guint i=0;i<g_list_length(list);i++){
		char *current;
		gtk_tree_model_get_iter(model,&iter,(GtkTreePath*)g_list_nth_data(list,i));
			yon_tree_model_sort_filter_get_model_iter(GTK_TREE_MODEL_SORT(model),&iter,&child);
		gtk_tree_model_get(GTK_TREE_MODEL(widgets->UsersList),&child,2,&current,-1);
		yon_char_parsed_add_or_create_if_exists(chosen,&chosen_size,current);
	}
	int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(widgets->UsersList),&iter);
	for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(widgets->UsersList),&iter)){
		char *name,*username,*parameters,*homedir=NULL, *group=NULL;
		gtk_tree_model_get(GTK_TREE_MODEL(widgets->UsersList),&iter,2,&name,3,&username,4,&group,6,&parameters,-1);
		int size;
		config_str home = yon_config_load(get_home_command(name),&size);
		if (size>0&&!yon_char_is_empty(home[0])){
			if (home[0][strlen(home[0])-1]=='\n') home[0][strlen(home[0])-1]='\0';
			homedir=home[0];
		} else {
			homedir = NULL;
		}
	
		int status=0;
		if (homedir) status=1;
		gtk_list_store_append(window->list,&itar);
		group = yon_user_get_group_string(name);
		
		gtk_list_store_set(window->list,&itar,0,yon_char_parsed_check_exist(chosen,chosen_size,name)>-1?1:0,1,name,2,homedir,3,0,4,yon_char_parsed_check_exist(chosen,chosen_size,name)>-1&&status?1:0,5,username,6,group,-1);
		free(name);
		free(username);
		free(parameters);
		free(homedir);
	}
	gtk_widget_show(window->Window);
}

yon_savasettings_window *yon_saving_settings_new(){
	GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_ubl_settings_usergroups_savesettings);
	yon_savasettings_window *window = malloc(sizeof(yon_savasettings_window));
	window->Window = yon_gtk_builder_get_widget(builder,"Window");
	window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
	window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
	window->SambaCheck = yon_gtk_builder_get_widget(builder,"SambaSaveCheck");
	window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
	g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
	gtk_widget_show(window->Window);
	yon_window_config_custom_window_setup(GTK_WINDOW(window->Window),"savingSettingsWindow");
	gtk_window_set_title(GTK_WINDOW(window->Window),SAVESETTINGS_TITLE_LABEL);
	char *string = yon_config_get_by_key(SAVE_ROOTCOPY_CHANGES_parameter);
	if (!yon_char_is_empty(string)&&strstr(string,"/var/lib/samba")){
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->SambaCheck),1);
	}
	yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),SYSTEM_SAVEMODE_LABEL,0,BACKGROUND_IMAGE_INFO_TYPE);
	return window;
}

void on_saving_settings_open(GtkWidget *, main_window *widgets){
	yon_savasettings_window *window = yon_saving_settings_new();
	gtk_window_set_transient_for(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window));
	g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_saving_settings_save),window);
}

void on_password_combo_changed(GtkComboBox *self, GtkWidget *target){
		int active = gtk_combo_box_get_active(self);
	switch(active){
		case 0:
		case 2:
		case 3:
		case 5:
			gtk_widget_set_sensitive(target,0);
			break;
		case 1:
		case 4:
		gtk_widget_set_sensitive(target,1);
			break;
	}
	
}

 
void on_filechooser_open(GtkWidget *, GtkEntry *output_target){
	GtkWidget *dialog = gtk_file_chooser_dialog_new(FILECHOOSER_TITLE_LABEL,NULL,GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER,CANCEL_LABEL,GTK_RESPONSE_CANCEL,ACCEPT_LABEL,GTK_RESPONSE_ACCEPT,NULL);
	int responce = gtk_dialog_run(GTK_DIALOG(dialog));
	if (responce==GTK_RESPONSE_ACCEPT){
		char *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
		gtk_entry_set_text(output_target,filename);
	}
	on_subwindow_close(dialog);

}

void on_groups_save(GtkWidget *self, dictionary *dict){
	ubl_settings_usergroups_group_window *window = yon_dictionary_get_data(dict->first,ubl_settings_usergroups_group_window*);
	struct target_struct *output = yon_dictionary_get_data(dict->first->next,struct target_struct *);
	if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->DefaultCheck))){
		GtkTreeIter iter;
		GtkTreeModel *model = GTK_TREE_MODEL(window->list);
		if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->GroupsTree)),&model,&iter)){
			char *name;
			gtk_tree_model_get(model,&iter,1,&name,-1);
			gtk_entry_set_text(output->target,name);
			free(name);
		}
	} else {
			gtk_entry_set_text(output->target,"");
		gtk_toggle_button_set_active(output->defaut_toggle,1);
	}
	on_subwindow_close(self);
}

void on_groups_clicked(GtkWidget *, struct target_struct *output_target){
	ubl_settings_usergroups_group_window *window = yon_ubl_settings_usergroups_group_new(0);
	gtk_window_set_title(GTK_WINDOW(window->GroupsWindow),MAIN_GROUP_TITLE_LABEL);
	gtk_label_set_text(GTK_LABEL(window->HeaderLabel),MAIN_GROUP_TITLE_LABEL);
	GtkTreeIter iter, itar,itor;
	int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(main_config.groups_list),&iter);
	for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(main_config.groups_list),&iter)){
		char *groupname;
		gtk_tree_model_get(GTK_TREE_MODEL(main_config.groups_list),&iter,1,&groupname,-1);
		int valid1 = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(window->list),&itor);
		for (;valid1;valid1 = gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&itor)){
			char *checked;
			gtk_tree_model_get(GTK_TREE_MODEL(window->list),&itor,1,&checked,-1);
			if (!strcmp(groupname,checked)){
				break;
			}
		}
		if (!valid1){
			gtk_list_store_append(window->list,&itar);
			gtk_list_store_set(window->list,&itar,1,groupname,-1);
		}
		free(groupname);
	}
	gtk_tree_view_column_set_title(gtk_tree_view_get_column(GTK_TREE_VIEW(window->GroupsTree),1),GID_LABEL);
	gtk_tree_view_column_set_visible(gtk_tree_view_get_column(GTK_TREE_VIEW(window->GroupsTree),0),0);
	gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->GroupsTree)),GTK_SELECTION_SINGLE);
	const char *current_group=gtk_entry_get_text(output_target->target);
	size_t digits=0;
	for (guint k=0;k<strlen(current_group);k++){
		if (g_ascii_isdigit(current_group[k])){
			digits++;
		}
	}

	char *final_group_name=NULL;

	if (digits&&digits==strlen(current_group)){
		for (int i=0;i<main_config.groups_size;i++){
			int group_size=0;
			config_str groups_parsed = yon_char_parse(main_config.groups[i],&group_size,":");
			if (group_size>2&&!strcmp(groups_parsed[2],current_group)){
				final_group_name=yon_char_new(groups_parsed[0]);
				if (group_size) yon_char_parsed_free(groups_parsed,group_size);
				break;
			}
			if (group_size) yon_char_parsed_free(groups_parsed,group_size);
		}
	} else {
		final_group_name=yon_char_new(current_group);
	}
	for_iter(window->list,&iter){
		char *group;
		gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,1,&group,-1);
		if (!yon_char_is_empty(final_group_name)&& !strcmp(group,final_group_name)){
			gtk_tree_selection_select_iter(gtk_tree_view_get_selection(GTK_TREE_VIEW(window->GroupsTree)),&iter);
			gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(window->GroupsTree),gtk_tree_model_get_path(GTK_TREE_MODEL(window->list),&iter),gtk_tree_view_get_column(GTK_TREE_VIEW(window->GroupsTree),1),0,0,0);
			break;
		}
	}
	dictionary *dict = NULL;
	yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
	yon_dictionary_add_or_create_if_exists_with_data(dict,"output",output_target);
	
    g_signal_connect(G_OBJECT(window->GroupsOkButton),"clicked",G_CALLBACK(on_groups_save),dict);
    gtk_widget_show(window->GroupsWindow);

}

void on_groups_multiple_save(GtkWidget *self, dictionary *dict){
	ubl_settings_usergroups_group_window *window = yon_dictionary_get_data(dict->first,ubl_settings_usergroups_group_window*);
	GtkEntry *output = yon_dictionary_get_data(dict->first->next,GtkEntry*);
	if (!gtk_widget_get_visible(window->DefaultCheck)||!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->DefaultCheck))){
	GtkTreeIter iter;
	int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(window->list),&iter);
	char *full_groups="";
	for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&iter)){
		char *name;
		int status;
		gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,0,&status,1,&name,-1);
		if (status){
			char *extend_groups = yon_char_unite(full_groups,!yon_char_is_empty(full_groups)?",":"",name,NULL);
			if (!yon_char_is_empty(full_groups))
				free(full_groups);
			full_groups=extend_groups;
		}
	}
	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->AddNewCheck))){
		char *custom_groups = (char*)gtk_entry_get_text(GTK_ENTRY(window->AddNewEntry));
		if (!yon_char_is_empty(custom_groups)){
			char *temp = yon_char_unite(full_groups,",",custom_groups,NULL);
			if (!yon_char_is_empty(full_groups)) free(full_groups);
			full_groups=temp;
		} else {
			yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
			yon_ubl_status_highlight_incorrect(window->AddNewEntry);
			return;
		}
	}
	gtk_entry_set_text(output,full_groups);
	} else {
		gtk_entry_set_text(output,config(USERGROUPS_parameter));
	}
	on_subwindow_close(self);
}

void on_groups_multiple_clicked(GtkWidget *, GtkEntry *output_target){
	ubl_settings_usergroups_group_window *window = yon_ubl_settings_usergroups_group_new(0);
	gtk_widget_show(window->AddNewBox);
	gtk_tree_view_column_set_title(gtk_tree_view_get_column(GTK_TREE_VIEW(window->GroupsTree),1),GID_LABEL);
	gtk_window_set_title(GTK_WINDOW(window->GroupsWindow),ADDITIONAL_GROUPS_TITLE_LABEL);
	gtk_label_set_text(GTK_LABEL(window->HeaderLabel),ADDITIONAL_GROUPS_TITLE_LABEL);
	GtkTreeIter iter, itar,itor;
	char *output = (char*)gtk_entry_get_text(output_target);
	int size=0;
	config_str parsed=NULL;
	if (!yon_char_is_empty(output)){
		parsed = yon_char_parse(output,&size,",");
	}
	int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(main_config.groups_list),&iter);
	for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(main_config.groups_list),&iter)){
		char *groupname;
		gtk_tree_model_get(GTK_TREE_MODEL(main_config.groups_list),&iter,1,&groupname,-1);
		int valid1 = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(window->list),&itor);
		for (;valid1;valid1 = gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&itor)){
			char *checked;
			gtk_tree_model_get(GTK_TREE_MODEL(window->list),&itor,1,&checked,-1);
			if (!strcmp(groupname,checked)){
				break;
			}
		}
		if (!valid1){
			gtk_list_store_append(window->list,&itar);
			gtk_list_store_set(window->list,&itar,1,groupname,-1);
			}
		free(groupname);
	}
	valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(window->list),&iter);
	for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&iter)){
		char *groupname;
		gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,1,&groupname,-1);
		if (parsed){
			if (yon_char_parsed_check_exist(parsed,size,groupname)>-1){
				gtk_list_store_set(window->list,&iter,0,1,-1);
			}
		}
	}
	dictionary *dict = NULL;
	yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
	yon_dictionary_add_or_create_if_exists_with_data(dict,"output",output_target);
    g_signal_connect(G_OBJECT(window->GroupsOkButton),"clicked",G_CALLBACK(on_groups_multiple_save),dict);
    gtk_widget_show(window->GroupsWindow);

}

void on_expiration_clicked(GtkWidget *, ubl_settings_usergroups_user_window *window){
	gtk_popover_popup(GTK_POPOVER(window->CalendarPopup));
}

// standard functions 

void on_user_choose(GtkWidget *, GtkEntry *target){
	ubl_settings_usergroups_group_window *window = yon_ubl_settings_usergroups_group_new(1);
	gtk_widget_hide(window->DefaultCheck);
	gtk_label_set_text(GTK_LABEL(window->HeaderLabel),GROUP_USERS_TITLE_LABEL);
	gtk_label_set_text(GTK_LABEL(window->HeaderLabel),GROUP_USERS_TITLE_LABEL);
	gtk_tree_view_column_set_title(gtk_tree_view_get_column(GTK_TREE_VIEW(window->GroupsTree),1),UID_LABEL);
	gtk_tree_view_column_set_title(GTK_TREE_VIEW_COLUMN(window->GroupColumn),USERS_LABEL);
	GtkTreeIter iter, itar,itor;
	char *output = (char*)gtk_entry_get_text(target);
	int size=0;
	config_str parsed=NULL;
	if (!yon_char_is_empty(output)){
		parsed = yon_char_parse(output,&size,",");
	}
	int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(main_config.users_list),&iter);
	for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(main_config.users_list),&iter)){
		char *groupname;
		gtk_tree_model_get(GTK_TREE_MODEL(main_config.users_list),&iter,2,&groupname,-1);
		int valid1 = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(window->list),&itor);
		for (;valid1;valid1 = gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&itor)){
			char *checked;
			gtk_tree_model_get(GTK_TREE_MODEL(window->list),&itor,1,&checked,-1);
			if (!strcmp(groupname,checked)){
				break;
			}
		}
			if (!valid1){
			gtk_list_store_append(window->list,&itar);
			gtk_list_store_set(window->list,&itar,1,groupname,-1);
		}
		free(groupname);
	}
	valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(window->list),&iter);
	for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&iter)){
		char *groupname;
		gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,1,&groupname,-1);
		if (parsed){
			if (yon_char_parsed_check_exist(parsed,size,groupname)>-1){
				gtk_list_store_set(window->list,&iter,0,1,-1);
			}
		}
	}
	gtk_list_store_new(2,G_TYPE_BOOLEAN,G_TYPE_STRING);
	dictionary *dict = NULL;
	yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
	yon_dictionary_add_or_create_if_exists_with_data(dict,"output",target);
    g_signal_connect(G_OBJECT(window->GroupsOkButton),"clicked",G_CALLBACK(on_groups_multiple_save),dict);
    gtk_widget_show(window->GroupsWindow);
}

void on_system_parameter_changed(GtkWidget *, ubl_settings_usergroups_group_creation_window *window){

	char *non_unique = NULL;
	char *gsystem = NULL;
	
	int non_unique_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UnuniqueGIDCheck));
	if(non_unique_active){
		non_unique="--non-unique";
	} else non_unique="";

	int gsystem_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->SystemGroupCheck));
	if(gsystem_active){
		gsystem="--system";
	} else gsystem="";

	char *final_str = yon_char_unite(
		yon_char_return_if_exist(non_unique,""),
		!yon_char_is_empty(non_unique)?" ":"",
		yon_char_return_if_exist(gsystem,""),
		NULL);
	gtk_entry_set_text(GTK_ENTRY(window->groupExtraOptionsEntry),final_str);
	free(final_str);
}
void on_group_clicked(GtkCellRenderer *, char *path, ubl_settings_usergroups_group_window *window){
	GtkTreeIter iter;
	gboolean status;
	gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(window->list),&iter,path);
	gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,0,&status,-1);
	gtk_list_store_set(window->list,&iter,0,!status,-1);
}

gboolean on_group_search_func(GtkTreeModel *model, gint ,const char *key, GtkTreeIter *iter, main_window *){
    char *target;
    int id;
    gtk_tree_model_get(model, iter, 1,&target, 2, &id,-1);
    char *string_utf = g_utf8_casefold(key,-1);
    char *target_utf = g_utf8_casefold(target,-1);
	char *id_str = yon_char_from_int(id);
    char *loc_target_utf = g_utf8_casefold(id_str,-1);

    char *string_normalized = g_utf8_strdown(g_utf8_normalize(string_utf,-1,G_NORMALIZE_DEFAULT),-1);
    char *target_normalized = g_utf8_strdown(g_utf8_normalize(target_utf,-1,G_NORMALIZE_DEFAULT),-1);
    char *loc_target_normalized = g_utf8_strdown(g_utf8_normalize(loc_target_utf,-1,G_NORMALIZE_DEFAULT),-1);

    return !(g_str_has_prefix(target_normalized,string_normalized) || g_str_has_prefix(loc_target_normalized,string_normalized));
}

ubl_settings_usergroups_group_window *yon_ubl_settings_usergroups_group_new(int mode){ // 1 - user, 0 - group
	ubl_settings_usergroups_group_window *window = malloc(sizeof(ubl_settings_usergroups_group_window));
	GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_ubl_settings_usergroups_group);
	window->GroupsWindow=yon_gtk_builder_get_widget(builder,"GroupsWindow");
	window->GroupsTree=yon_gtk_builder_get_widget(builder,"GroupsTree");
	window->GroupsCancelButton=yon_gtk_builder_get_widget(builder,"GroupsCancelButton");
	window->GroupsOkButton=yon_gtk_builder_get_widget(builder,"GroupsOkButton");
	window->HeaderLabel=yon_gtk_builder_get_widget(builder,"HeaderLabel");
	window->StatusCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"StatusCell"));
	window->GroupColumn = GTK_TREE_VIEW_COLUMN(gtk_builder_get_object(builder,"GroupColumn"));
	window->DefaultCheck=yon_gtk_builder_get_widget(builder,"DefaultCheck");
	window->AddNewCheck=yon_gtk_builder_get_widget(builder,"AddNewCheck");
	window->AddNewEntry=yon_gtk_builder_get_widget(builder,"AddNewEntry");
	window->AddNewBox=yon_gtk_builder_get_widget(builder,"AddNewBox");
	window->StatusBox=yon_gtk_builder_get_widget(builder,"StatusBox");
	window->list = gtk_list_store_new(3,G_TYPE_BOOLEAN,G_TYPE_STRING,G_TYPE_INT);
	gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(window->list),2,GTK_SORT_ASCENDING);
    gtk_tree_view_set_search_equal_func(GTK_TREE_VIEW(window->GroupsTree),(GtkTreeViewSearchEqualFunc)on_group_search_func,window,NULL);

	yon_gtk_entry_block_symbols(GTK_ENTRY(window->AddNewEntry),";.'\"*%$#!-=+\\|/_@^&()№:?<>");
	yon_window_config_custom_window_setup(GTK_WINDOW(window->GroupsWindow),"GroupWindow");
	gtk_window_set_title(GTK_WINDOW(window->GroupsWindow),GROUPS_TITLE_LABEL);
	g_signal_connect(G_OBJECT(window->AddNewCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->AddNewEntry);
	g_signal_connect(G_OBJECT(window->DefaultCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->GroupsTree);
	g_signal_connect(G_OBJECT(window->StatusCell),"toggled",G_CALLBACK(on_group_clicked),window);
	GtkTreeIter iter;
	config_str current;
	int current_size;
	if (mode==1){
		current = main_config.users;
		current_size = main_config.users_size;
	} else {
		current = main_config.groups;
		current_size = main_config.groups_size;
	}
	for (int i=0;i<current_size;i++){
		int parsed_size=0;
		config_str parsed = yon_char_parse(current[i],&parsed_size,":");
		if (parsed_size>2&&!yon_char_is_empty(parsed[2])){
			gtk_list_store_append(window->list,&iter);
			gtk_list_store_set(window->list,&iter,0,0,1,parsed[0],2,atoi(parsed[2]),-1);
		}
		if (parsed_size) yon_char_parsed_free(parsed,parsed_size);
	}
	gtk_tree_view_set_model(GTK_TREE_VIEW(window->GroupsTree),GTK_TREE_MODEL(window->list));


	g_signal_connect(G_OBJECT(window->GroupsCancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
return window;
}

void on_standard_groups_accept(GtkWidget *self, ubl_settings_usergroups_group_window *window){
	if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->DefaultCheck))){
	GtkTreeIter iter;
	GtkTreeModel *model = GTK_TREE_MODEL(window->list);
	int valid = gtk_tree_model_get_iter_first(model,&iter);
	char *standard_groups="";
	for (;valid;valid=gtk_tree_model_iter_next(model,&iter)){
		int status;
		char *group;
		gtk_tree_model_get(model,&iter,0,&status,1,&group,-1);
		if (status){
			char *extend_groups = yon_char_unite(standard_groups,!yon_char_is_empty(standard_groups)?",":"",group,NULL);
			if (!yon_char_is_empty(standard_groups)) 
				free(standard_groups);
			standard_groups=extend_groups;
		}
	}
	yon_config_register(USERGROUPS_parameter,USERGROUPS_get_command,standard_groups);
	} else {
		yon_config_remove_by_key(USERGROUPS_parameter);
	}
	on_subwindow_close(self);
}

void on_standard_groups_open(GtkWidget *, main_window *widgets){
    ubl_settings_usergroups_group_window *window = yon_ubl_settings_usergroups_group_new(0);
	if (yon_config_check_ignore(USERGROUPS_parameter)){
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->DefaultCheck),1);
	}
	gtk_window_set_title(GTK_WINDOW(window->GroupsWindow),DEFAULT_GROUPS_TITLE_LABEL);
	gtk_label_set_text(GTK_LABEL(window->HeaderLabel),DEFAULT_GROUPS_TITLE_LABEL);
	gtk_tree_view_column_set_title(gtk_tree_view_get_column(GTK_TREE_VIEW(window->GroupsTree),1),GID_LABEL);
	GtkTreeIter iter, itar,itor;
	int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(main_config.groups_list),&iter);
	for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(main_config.groups_list),&iter)){
		char *groupname;
		gtk_tree_model_get(GTK_TREE_MODEL(main_config.groups_list),&iter,1,&groupname,-1);
			int valid1 = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(window->list),&itor);
			for (;valid1;valid1 = gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&itor)){
				char *checked;
				gtk_tree_model_get(GTK_TREE_MODEL(window->list),&itor,1,&checked,-1);
				if (!strcmp(groupname,checked)){
					break;
				}
			}
		if (!valid1){
			gtk_list_store_append(window->list,&itar);
			gtk_list_store_set(window->list,&itar,1,groupname,-1);
		}
		free(groupname);
	}
    g_signal_connect(G_OBJECT(window->GroupsOkButton),"clicked",G_CALLBACK(on_standard_groups_accept),window);
    gtk_window_set_transient_for(GTK_WINDOW(window->GroupsWindow),GTK_WINDOW(widgets->Window));
	char *groups = yon_config_get_by_key(USERGROUPS_parameter);
	if (!yon_char_is_empty(groups)){
		GtkTreeIter iter;
		int size;
		config_str standard_groups = yon_char_parse(groups,&size,",");
		int valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(window->list),&iter);
		for (;valid;valid=gtk_tree_model_iter_next(GTK_TREE_MODEL(window->list),&iter)){
			char *cur_name;
			gtk_tree_model_get(GTK_TREE_MODEL(window->list),&iter,1,&cur_name,-1);
				for (int i=0;i<size;i++){
					if (!strcmp(standard_groups[i],cur_name)){
						gtk_list_store_set(window->list,&iter,0,1,-1);
					}
				}
			free(cur_name);
		}
	}
    gtk_widget_show(window->GroupsWindow);
}

void yon_system_load(main_window *window){
	gtk_list_store_clear(window->liststore1);
	gtk_list_store_clear(window->liststore2);

	if (main_config.groups_size>0){
		yon_char_parsed_free(main_config.groups,main_config.groups_size);
		main_config.groups = yon_file_open(groups_path,&main_config.groups_size);
	}
	if (main_config.users_size>0){
		yon_char_parsed_free(main_config.users,main_config.users_size);
		main_config.users = yon_file_open(users_path,&main_config.users_size);
	}
	if (main_config.shadow_size>0){
		yon_char_parsed_free(main_config.shadow,main_config.shadow_size);
		main_config.shadow = yon_file_open(shadow_path,&main_config.shadow_size);
	}
	if (main_config.group_shadow_size>0){
		yon_char_parsed_free(main_config.group_shadow,main_config.group_shadow_size);
		main_config.group_shadow = yon_file_open(group_shadow_path,&main_config.group_shadow_size);
	}

	GtkTreeIter iter;
	for (int i=0;i<main_config.groups_size;i++){
		int parsed_size;
		if (main_config.groups[i][strlen(main_config.groups[i])-1]=='\n') main_config.groups[i][strlen(main_config.groups[i])-1]='\0';
		config_str parsed = yon_char_parse(main_config.groups[i],&parsed_size,":");
		if (parsed_size){
			gtk_list_store_append(window->liststore2,&iter);
			gtk_list_store_set(window->liststore2,&iter,0,atol(parsed[2]),1,parsed[0],2,parsed[3],-1);
		}
	}
	for (int i=0;i<main_config.users_size;i++){
		int parsed_size;
		int shadow_parsed_size=0;
		config_str parsed = yon_char_parse(main_config.users[i],&parsed_size,":");
		config_str shadow_parsed = NULL;
		if (main_config.shadow_size){
			shadow_parsed = yon_char_parse(main_config.shadow[i],&shadow_parsed_size,":");
		}
			char *groups_string="";
			char *main_group_name="";
			for (int j=0;j<main_config.groups_size;j++){
				int parsed_gr_size;
				config_str parsed_group = yon_char_parse(main_config.groups[j],&parsed_gr_size,":");
				if (atol(parsed[3])==atol(parsed_group[2])){
					main_group_name=yon_char_new(parsed_group[0]);
				}
				if (shadow_parsed_size&&!strcmp(parsed_group[3],shadow_parsed[0])){
					groups_string = yon_char_unite(groups_string,!yon_char_is_empty(groups_string)?",":"",parsed_group[0],NULL);
				}
				yon_char_parsed_free(parsed_group,parsed_gr_size);
			}
                GdkRGBA rgba = {0};
				char *wheel = yon_char_parsed_check_exist_begins_with(main_config.shadow,main_config.shadow_size,"wheel");
				int wheel_size;
				config_str wheel_parsed = yon_char_parse(wheel,&wheel_size,":");
				char *param = config(ADDADM_parameter);
                if ((atoi(parsed[2])==1000&&!yon_char_is_empty(param)&&!strcmp(param,"yes"))||yon_char_parsed_check_exist(wheel_parsed,wheel_size,parsed[0])>-1){
					rgba.red=255.0/255.0;
					rgba.green=191.0/255.0;
					rgba.blue=0;
					rgba.alpha=0.17;
                }
			gtk_list_store_append(window->liststore1,&iter);
			gtk_list_store_set(window->liststore1,&iter,1,atol(parsed[2]),2,parsed[0],3,parsed[4],4,main_group_name,5,groups_string,6,parsed[5],14,&rgba,-1);
			yon_char_parsed_free(wheel_parsed,wheel_size);
		yon_char_parsed_free(parsed,parsed_size);
	}
	int shadow_size;
	config_str shadow = yon_file_open(shadow_path,&shadow_size);
	for (int i=0;i<shadow_size;i++){
		if (shadow[i][strlen(shadow[i])-1]=='\n') shadow[i][strlen(shadow[i])-1]='\0';
		int parsed_size=0;
		config_str parsed = yon_char_parse(shadow[i],&parsed_size,":");
		if (yon_gtk_list_store_find_text(window->liststore1,&iter,2,parsed[0])){
			GDateTime *date = g_date_time_new_from_unix_local(atol(parsed[2])*24*60*60);
			char *date_string;
			char *format = yon_settings_configuration_get(date_format_parameter);
				date_string = g_date_time_format(date,format?format:"%d.%m.%Y");
				g_date_time_unref(date);
				char *expiration_string="";
				if (parsed_size>7&&!yon_char_is_empty(parsed[7])){
					date = g_date_time_new_from_unix_local(atol(parsed[7])*24*60*60);
					expiration_string = g_date_time_format(date,format?format:"%d.%m.%Y");
					g_date_time_unref(date);
				}
			
			gtk_list_store_set(window->liststore1,&iter,0,!strcmp(parsed[1],"!*"),8,parsed_size>2?date_string:"",9,expiration_string,10,parsed_size>3?parsed[3]:"",11,parsed_size>4?parsed[4]:"",12,parsed_size>5?parsed[5]:"",13,parsed_size>2?parsed[6]:"",-1);
			if (!yon_char_is_empty(date_string)) free(date_string);
			if (!yon_char_is_empty(expiration_string)) free(expiration_string);
		}
			yon_char_parsed_free(parsed,parsed_size);
	}
}

// void on_mode_changed(GtkWidget *self, main_window *window){
// 	if (window->show_all==0){
// 		window->show_all=1;
// 		gtk_style_context_add_class(gtk_widget_get_style_context(window->ToggleAllButton),"chosenOutline");
// 	} else {
// 		window->show_all=0;
// 		gtk_style_context_remove_class(gtk_widget_get_style_context(window->ToggleAllButton),"chosenOutline");
// 	}
// 	yon_system_load(window);
// }

void on_system_update(GtkWidget *, main_window *window){
	
	gtk_list_store_clear(window->UsersList);
	gtk_list_store_clear(window->GroupsList);
	if (main_config.groups_size) yon_char_parsed_free(main_config.groups,main_config.groups_size);
	if (main_config.users_size) yon_char_parsed_free(main_config.users,main_config.users_size);
	if (main_config.shadow_size) yon_char_parsed_free(main_config.shadow,main_config.shadow_size);
	if (main_config.group_shadow_size) yon_char_parsed_free(main_config.group_shadow,main_config.group_shadow_size);
	main_config.groups = yon_file_open(groups_path,&main_config.groups_size);
	main_config.users = yon_file_open(users_path,&main_config.users_size);
	main_config.shadow = yon_file_open(shadow_path,&main_config.shadow_size);
	main_config.group_shadow = yon_file_open(group_shadow_path,&main_config.group_shadow_size);
	int login_size;
	config_str login_defs = yon_file_open(uid_path,&login_size);
	for (int i=0;i<login_size;i++){
		if (login_defs[i][0]!='#'){
			char *copy = yon_char_new(login_defs[i]);
			if (strlen(copy)>7){
				char *divided = yon_char_divide(copy,7);
				if (divided){
					if (!strcmp(divided,"UID_MAX")){
						main_config.MAXUID = atol(strstr(login_defs[i],"\t"));
					}
					else if (!strcmp(divided,"UID_MIN")){
						main_config.MINUID = atol(strstr(login_defs[i],"\t"));
					}
					else if (!strcmp(divided,"GID_MAX")){
						main_config.MAXGID = atol(strstr(login_defs[i],"\t"));
					}
					else if (!strcmp(divided,"GID_MIN")){
						main_config.MINGID = atol(strstr(login_defs[i],"\t"));
					}
				}
			}
		}
	}
	yon_char_parsed_free(login_defs,login_size);
	yon_system_load(window);
}

void on_toggle_button_set_active_from_combo_box(GtkComboBox *self,ubl_settings_usergroups_user_window *window){
	char *passw = (char*)gtk_entry_get_text(GTK_ENTRY(window->userPasswordEntry));
	if (gtk_combo_box_get_active(self)<=1||(passw[0]=='$')){
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBACheck),0);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBAPasswordCheck),0);
		gtk_widget_set_sensitive(GTK_WIDGET(window->userSyncSAMBAPasswordCheck),0);
	}else {
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBACheck),0);
		gtk_widget_set_sensitive(GTK_WIDGET(window->userSyncSAMBACheck),1);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBAPasswordCheck),0);
		gtk_widget_set_sensitive(GTK_WIDGET(window->userSyncSAMBAPasswordCheck),1);

	}
}

void on_toggle_button_set_active_from_toggle_button_inversed(GtkToggleButton *, ubl_settings_usergroups_user_window *window){
	char *passw = (char*)gtk_entry_get_text(GTK_ENTRY(window->userPasswordEntry));
	if ((passw[0]!='$')&&gtk_combo_box_get_active(GTK_COMBO_BOX(window->userPasswordCombo))==1){
		if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBACheck))){
			gtk_widget_set_sensitive(GTK_WIDGET(window->userSyncSAMBAPasswordCheck),0);
		} else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBAPasswordCheck))){
			gtk_widget_set_sensitive(GTK_WIDGET(window->userSyncSAMBACheck),0);

		} else {
			gtk_widget_set_sensitive(GTK_WIDGET(window->userSyncSAMBACheck),1);
			gtk_widget_set_sensitive(GTK_WIDGET(window->userSyncSAMBAPasswordCheck),1);
		}
	} else {
		gtk_widget_set_sensitive(GTK_WIDGET(window->userSyncSAMBACheck),1);
		gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBAPasswordCheck),0);
		gtk_widget_set_sensitive(GTK_WIDGET(window->userSyncSAMBAPasswordCheck),0);

	}
}

void on_date_selected(GtkWidget *, ubl_settings_usergroups_user_window *window){
	guint year=0, month=0, day=0;
	gtk_calendar_get_date(GTK_CALENDAR(window->ExpirationCalendar),&year,&month,&day);
	GDateTime *nowdate = g_date_time_new_now_local();
	GDateTime *chosendate = g_date_time_new_local(year,month+1,day,23,59,59);

	if (g_date_time_compare(nowdate,chosendate)>0){
		gtk_entry_set_text(GTK_ENTRY(window->userPasswordExpirationEntry), "");
		goto comperr;
		return;
	}
	char *dy = yon_char_from_int(day);
	char *mn = yon_char_from_int(month+1);
	char *yr = yon_char_from_int(year);
	GDateTime *datetime = g_date_time_new_local(year,month+1,day,0,0,0);
	char *format = yon_settings_configuration_get(date_format_parameter);
	char *date_string = g_date_time_format(datetime,format?format:"%d.%m.%Y");
	gtk_entry_set_text(GTK_ENTRY(window->userPasswordExpirationEntry), date_string);
	window->expiration_unix=date_string;
	free(dy);
	free(mn);
	free(yr);
	comperr:
	g_date_time_unref(nowdate);
	g_date_time_unref(chosendate);
}

void on_remove_expired(GtkWidget *, GtkEntryIconPosition icon_pos, GdkEvent *, GtkEntry *target){
	if (icon_pos == GTK_ENTRY_ICON_SECONDARY){
		gtk_entry_set_text(target,"");
	}
}


void on_main_add(GtkWidget *, main_window *widgets){
	if (gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook))==0){
		gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->UsersTree)));
			ubl_settings_usergroups_user_window *window = yon_ubl_settings_usergroups_user_new();
			gtk_window_set_title(GTK_WINDOW(window->CreateUserWindow),ADD_USER_TITLE_LABEL);
			gtk_label_set_text(GTK_LABEL(window->userTitleNameLabel),ADD_USER_TITLE_LABEL);
			char *default_groups = yon_config_get_by_key(USERGROUPS_parameter);
			gtk_entry_set_text(GTK_ENTRY(window->userAdditionalGroupsEntry), default_groups);
			dictionary *dict = NULL;
			yon_dictionary_add_or_create_if_exists_with_data(dict,"widgets",widgets);
			yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
			g_signal_connect(G_OBJECT(window->UserOkButton),"clicked",G_CALLBACK(on_user_save),dict);
			
			gtk_window_set_transient_for(GTK_WINDOW(window->CreateUserWindow),GTK_WINDOW(widgets->Window));
			yon_user_set_locales(window->LanguageCombo);
			gtk_widget_show(window->CreateUserWindow);
	} else {
		gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->GroupsTree)));
			ubl_settings_usergroups_group_creation_window *window = yon_ubl_settings_usergroups_group_creation_new();
			gtk_window_set_title(GTK_WINDOW(window->CreateGroupWindow),ADD_GROUP_TITLE_LABEL);
			gtk_label_set_text(GTK_LABEL(window->userTitleNameLabel),ADD_GROUP_TITLE_LABEL);
			dictionary *dict = NULL;
			yon_dictionary_add_or_create_if_exists_with_data(dict,"widgets",widgets);
			yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
			g_signal_connect(G_OBJECT(window->UserOkButton),"clicked",G_CALLBACK(on_group_save),dict);
			
			gtk_window_set_transient_for(GTK_WINDOW(window->CreateGroupWindow),GTK_WINDOW(widgets->Window));
			gtk_widget_show(window->CreateGroupWindow);
	}

}

void on_main_edit(GtkWidget *, main_window *widgets){
	GtkTreeModel *model;
	GtkTreeIter iter,child;
	if (gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook))==0){
		model = GTK_TREE_MODEL(widgets->UsersList);
		if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->UsersTree)),&model,&iter)){
			ubl_settings_usergroups_user_window *window = yon_ubl_settings_usergroups_user_new();
			gtk_widget_show(window->CreateUserWindow);
			gtk_widget_set_sensitive(window->userLoginEntry,0);
			gtk_window_set_title(GTK_WINDOW(window->CreateUserWindow),CONFIGURE_USER_TITLE_LABEL);
			gtk_label_set_text(GTK_LABEL(window->userTitleNameLabel),CONFIGURE_USER_TITLE_LABEL);
			dictionary *dict = NULL;
			yon_dictionary_add_or_create_if_exists_with_data(dict,"widgets",widgets);
			yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
			g_signal_connect(G_OBJECT(window->UserOkButton),"clicked",G_CALLBACK(on_user_save),dict);
			char *name;
			yon_tree_model_sort_filter_get_model_iter(GTK_TREE_MODEL_SORT(model),&iter,&child);
			gtk_tree_model_get(GTK_TREE_MODEL(widgets->UsersList),&child,2,&name,-1);
			char *parameter_string = yon_config_get_by_key(USERADD(name));
			char *sync = config(USERADD_SYNC(name));
			if (!yon_char_is_empty(sync)){
				gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->UseraddBootCheck),!!strstr(sync,"boot")); 
				gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->UseraddShutdownCheck),!!strstr(sync,"shutdown")); 
			} else {
				gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->UseraddBootCheck),0); 
				gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->UseraddShutdownCheck),0);
			}
			int parsed_size=0;
			if (parameter_string){
				if (parameter_string[0]=='\"') yon_char_divide(parameter_string,0);
				if (parameter_string[strlen(parameter_string)-1]=='\"') parameter_string[strlen(parameter_string)-1]='\0';
				config_str parameters = yon_char_parse(parameter_string,&parsed_size,":");
				if(!strcmp(parameters[0],"x"))
					parameters[0]="";
				gtk_entry_set_text(GTK_ENTRY(window->userLoginEntry), name);
				gtk_entry_set_text(GTK_ENTRY(window->userGroupsEntry), name);
				gtk_entry_set_text(GTK_ENTRY(window->userUserNameEntry), parsed_size>0?parameters[0]:"");
				window->old_username=name;
				if (parsed_size>1){
					if ((parsed_size>1&&strcmp(parameters[1],"x")&&strcmp(parameters[1],""))||parsed_size<=1){
						gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userUIDAutoCheck),0);
						gtk_entry_set_text(GTK_ENTRY(window->userUIDEntry), parsed_size>1?parameters[1]:"");
					}
						if (!yon_char_is_empty(parameters[1])){
							window->last_uid=yon_char_new(parameters[1]);
							g_object_set_data(G_OBJECT(window->CreateUserWindow),"edit state",yon_char_new(name));
						}
				}
				if (parsed_size>2){
					if (!strcmp(parameters[2],"x")||!strcmp(parameters[2],""))
						gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userGroupsCheck),1);
					else {
						gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userGroupsCheck),0);
						gtk_entry_set_text(GTK_ENTRY(window->userGroupsEntry), parsed_size>2?parameters[2]:"");
					}
				}
				if (parsed_size>3){
					if (!yon_char_is_empty(parameters[3])&&strcmp(parameters[3],"x")){
						gtk_entry_set_text(GTK_ENTRY(window->userAdditionalGroupsEntry), parameters[3]);
					} else {
						char *default_groups = yon_config_get_by_key(USERGROUPS_parameter);
						gtk_entry_set_text(GTK_ENTRY(window->userAdditionalGroupsEntry), default_groups);
					}
					
				} else {
					char *default_groups = yon_config_get_by_key(USERGROUPS_parameter);
					gtk_entry_set_text(GTK_ENTRY(window->userAdditionalGroupsEntry), default_groups);
				}
				if (parsed_size>4&&!yon_char_is_empty(parameters[4])&&strcmp(parameters[4],"")&&strcmp(parameters[4],"x")){
					int params_size;
					config_str parameters_substring = yon_char_parse(parameters[4],&params_size," ");
					for (int i=0;i<params_size;i++){
						if (!strcmp(parameters_substring[i],"--home-dir")){
							gtk_entry_set_text(GTK_ENTRY(window->userHomeEntry),parameters_substring[i+1]);
							gtk_combo_box_set_active(GTK_COMBO_BOX(window->userHomeCombo),2);
							i++;
						} else if (!strcmp(parameters_substring[i],"--shell")||!strcmp(parameters_substring[i],"-s")){
							if (yon_char_is_empty(parameters_substring[i+1])){
								gtk_combo_box_set_active(GTK_COMBO_BOX(window->userShellCombo),0);
							} else {
								if (!gtk_combo_box_set_active_id(GTK_COMBO_BOX(window->userShellCombo),parameters_substring[i+1])){
									gtk_entry_set_text(GTK_ENTRY(window->userShellEntry),parameters_substring[i+1]);
									gtk_combo_box_set_active_id(GTK_COMBO_BOX(window->userShellCombo),"Set");
								}
							}
							i++;
						} else if (!strcmp(parameters_substring[i],"--lang")){
							g_object_set_data(G_OBJECT(window->LanguageCombo),"value",parameters_substring[i+1]);
							window->config_locale=yon_char_new(parameters_substring[i+1]);

						}else if (!strcmp(parameters_substring[i],"--system")||!strcmp(parameters_substring[i],"-r")){
							gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userCreateSystemCheck),1);
						}else if (!strcmp(parameters_substring[i],"--no-create-home")||!strcmp(parameters_substring[i],"-M")){
							gtk_combo_box_set_active(GTK_COMBO_BOX(window->userHomeCombo),1);
						}else if (!strcmp(parameters_substring[i],"--no-user-group")||!strcmp(parameters_substring[i],"-N")){
							gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userGroupsCheck),0);
							gtk_entry_set_text(GTK_ENTRY(window->userGroupsEntry),"");
						}else if (!strcmp(parameters_substring[i],"--non-unique")||!strcmp(parameters_substring[i],"-o")){
							gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userCreateUnuniqueCheck),1);
						}else if (!strcmp(parameters_substring[i],"--badnames")){
							gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userDontCheckCheck),1);
						}
					}
				}
				if (parsed_size>5){
					if (strcmp(parameters[5]," ")&&!yon_char_check_begins_with(parameters[5],"!")&&!yon_char_check_begins_with(parameters[5],"*")&&!yon_char_check_begins_with(parameters[5],"%%")){
						if (check_is_password_hash(parameters[5])){
								char *temp = yon_char_append("%%",parameters[5]);
								free(parameters[5]);
								parameters[5]=temp;
						}
					}
					window->old_password=yon_char_new(parameters[5]);
					if (strcmp(parameters[5],"x")&&strcmp(parameters[5],"")){
						if (!yon_char_is_empty(parameters[5])){
							if (check_is_password_hash(parameters[5])){
								if (yon_char_check_begins_with(parameters[5],"%%")==-1){
									char *temp = yon_char_append("%%",parameters[5]);
									free(parameters[5]);
									parameters[5]=temp;
								}
								gtk_widget_set_sensitive(window->userSyncSAMBAPasswordCheck,0);
								g_signal_handlers_block_by_func(G_OBJECT(window->userSyncSAMBACheck),G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->userSyncSAMBAPasswordCheck);
							}
							if (!strcmp(parameters[5],"!*")){
								gtk_combo_box_set_active(GTK_COMBO_BOX(window->userPasswordCombo),2);
							} else if (!strcmp(parameters[5]," ")) {
								gtk_combo_box_set_active(GTK_COMBO_BOX(window->userPasswordCombo),5);
							} else if (parameters[5][0]=='!'){
								char *password_or = yon_char_new(parameters[5]);
								free(yon_char_divide(password_or,0));
								gtk_entry_set_text(GTK_ENTRY(window->userPasswordEntry),password_or);
								gtk_combo_box_set_active(GTK_COMBO_BOX(window->userPasswordCombo),3);
								free(password_or);
							} else {
								gtk_entry_set_text(GTK_ENTRY(window->userPasswordEntry),parameters[5]);
								gtk_combo_box_set_active(GTK_COMBO_BOX(window->userPasswordCombo),1);
							}
						}
					}
				}
				int samba_sync = yon_samba_sync_get(name);
				if (samba_sync==1){
					gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBACheck),1);
				} else if (samba_sync==2){
					gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBAPasswordCheck),1);
				} 
				free(parameters);
				parameters=yon_char_parse(yon_config_get_by_key(USERSHADOW(name)),&parsed_size,":");
				if (parameters&&!strcmp(parameters[0],"0"))
					gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userForceChangeCheck),1);
				else
					gtk_entry_set_text(GTK_ENTRY(window->userPasswordChangedEntry),parsed_size>0?parameters[0]:"");
				gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->userPasswordChangeDelayMinimumSpin),atol(parsed_size>1?parameters[1]:"0"));
				gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->userPasswordChangeDelayMaximumSpin),atol(parsed_size>2?parameters[2]:"0"));
				gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->userWarningSpin),atol(parsed_size>3?parameters[3]:"0"));
				gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->userActivitySpin),atol(parsed_size>4?parameters[4]:"0"));
				
				if (parsed_size>5&&!yon_char_is_empty(parameters[5])&&strcmp(parameters[5],"-1")){
					gtk_entry_set_text(GTK_ENTRY(window->userPasswordExpirationEntry),parameters[5]);
				}
				gtk_window_set_transient_for(GTK_WINDOW(window->CreateUserWindow),GTK_WINDOW(widgets->Window));

				char *useradd_sync = NULL;
				useradd_sync = yon_config_get_by_key(USERADD_SYNC(name));
				if (useradd_sync){
					if (strstr(useradd_sync,"boot")){
						gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->UseraddBootCheck),1);
					}
					if (strstr(useradd_sync,"shutdown")){
						gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->UseraddShutdownCheck),1);
					}
				}
				char *usershadow_sync =yon_config_get_by_key(USERSHADOW_SYNC(name));
				if (usershadow_sync){
					if (strstr(usershadow_sync,"boot")){
						gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->UsershadowBootCheck),1);
					}
					if (strstr(usershadow_sync,"shutdown")){
						gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->UsershadowShutdownCheck),1);
					}
				}
			}
			yon_user_set_locales(window->LanguageCombo);
			
		}
	} else {
		model = GTK_TREE_MODEL(widgets->ConfigGroupsSort);
		if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->GroupsTree)),&model,&iter)){
			ubl_settings_usergroups_group_creation_window *window = yon_ubl_settings_usergroups_group_creation_new();
			gtk_widget_set_sensitive(window->userLoginEntry,0);
			gtk_window_set_title(GTK_WINDOW(window->CreateGroupWindow),CONFIGURE_GROUP_TITLE_LABEL);
			gtk_label_set_text(GTK_LABEL(window->userTitleNameLabel),CONFIGURE_GROUP_TITLE_LABEL);
			dictionary *dict = NULL;
			yon_dictionary_add_or_create_if_exists_with_data(dict,"widgets",widgets);
			yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
			g_signal_connect(G_OBJECT(window->UserOkButton),"clicked",G_CALLBACK(on_group_save),dict);
			char *name;
			yon_tree_model_sort_filter_get_model_iter(GTK_TREE_MODEL_SORT(model),&iter,&child);
			gtk_tree_model_get(GTK_TREE_MODEL(widgets->GroupsList),&child,1,&name,-1);
			char *config_string = yon_config_get_by_key(GROUPADD(name));
			char *sync_string = config(GROUPADD_SYNC(name));
			if (!yon_char_is_empty(config_string)){
				if (config_string[0]=='\"') yon_char_divide(config_string,0);
				if (config_string[strlen(config_string)-1]=='\"') config_string[strlen(config_string)-1]='\0';
				int size;
				config_str parsed = yon_char_parse(config_string,&size,":");
				gtk_entry_set_text(GTK_ENTRY(window->userLoginEntry),name);
				if (size>1&&strcmp(parsed[0],"x"))
					gtk_entry_set_text(GTK_ENTRY(window->userGroupsEntry),parsed[0]);
				if (size>1){
					if (strcmp(parsed[1],"")&&strcmp(parsed[1],"x")){
						gtk_entry_set_text(GTK_ENTRY(window->userUIDEntry),parsed[1]);
						g_signal_handlers_block_by_func(G_OBJECT(window->userUIDAutoCheck),G_CALLBACK(on_GID_update),window);
						gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->userUIDAutoCheck),0);
						g_signal_handlers_unblock_by_func(G_OBJECT(window->userUIDAutoCheck),G_CALLBACK(on_GID_update),window);
						window->last_gid=yon_char_new(name);
						g_object_set_data(G_OBJECT(window->CreateGroupWindow),"edit state",yon_char_new(name));
					}
				}
				if (size>2&&!yon_char_is_empty(parsed[2])){
					int parsed_size=0;
					config_str substring = yon_char_parse(parsed[2],&parsed_size," ");
					for (int i=0;i<parsed_size;i++){
						if (!yon_char_is_empty(substring[i])){
							if (!strcmp(substring[i],"--non-unique")||!strcmp(substring[i],"-o")){
								gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->UnuniqueGIDCheck),1);
							} else if (!strcmp(substring[i],"--system")||!strcmp(substring[i],"-r")){
								gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->SystemGroupCheck),1);
							}
						}
					}
					free(substring);
				}
				if (size>3){
					if (strcmp(parsed[3],"")&&strcmp(parsed[3],"x")){
						gtk_entry_set_text(GTK_ENTRY(window->AdminGroupsEntry),parsed[3]);
					}
				}
				if (size>4){
					if (strcmp(parsed[4]," ")&&!yon_char_check_begins_with(parsed[4],"!")&&!yon_char_check_begins_with(parsed[4],"*")&&!yon_char_check_begins_with(parsed[4],"%%")){
						if (check_is_password_hash(parsed[4])){
								char *temp = yon_char_append("%%",parsed[4]);
								free(parsed[4]);
								parsed[4]=temp;
						}
					}
					window->old_password=yon_char_new(parsed[4]);
					if (strcmp(parsed[4],"x")&&strcmp(parsed[4],"")){
						if (!yon_char_is_empty(parsed[4])){
							if (check_is_password_hash(parsed[4])){
								if (yon_char_check_begins_with(parsed[4],"%%")==-1){
									char *temp = yon_char_append("%%",parsed[4]);
									free(parsed[4]);
									parsed[4]=temp;
								}
							}
							if (!strcmp(parsed[4],"!*")){
								gtk_combo_box_set_active(GTK_COMBO_BOX(window->PasswordCombo),2);
							} else if (!strcmp(parsed[4]," ")) {
								gtk_combo_box_set_active(GTK_COMBO_BOX(window->PasswordCombo),5);
							} else if (parsed[4][0]=='!'){
								char *password_or = yon_char_new(parsed[4]);
								free(yon_char_divide(password_or,0));
								gtk_entry_set_text(GTK_ENTRY(window->PasswordEntry),password_or);
								gtk_combo_box_set_active(GTK_COMBO_BOX(window->PasswordCombo),3);
								free(password_or);
							} else {
								gtk_entry_set_text(GTK_ENTRY(window->PasswordEntry),parsed[4]);
								gtk_combo_box_set_active(GTK_COMBO_BOX(window->PasswordCombo),1);
							}
						}
					}
				} else {
					gtk_combo_box_set_active(GTK_COMBO_BOX(window->PasswordCombo),0);
				}
				char *sync = yon_config_get_by_key(GROUPADD_SYNC(name));
				if (sync){
					gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->GroupaddShutdownCheck),1);
				}
			} else {
				yon_ubl_status_box_render(LOADING_FAILED_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
			}
			if (!yon_char_is_empty(sync_string)){
				if (!strcmp(sync_string,"shutdown")){
					gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->GroupaddShutdownCheck),1);
				}
			}
			gtk_window_set_transient_for(GTK_WINDOW(window->CreateGroupWindow),GTK_WINDOW(widgets->Window));
			gtk_widget_show(window->CreateGroupWindow);
		}
	}

}

void on_main_edit_activate(GtkWidget *self, GtkTreePath *, GtkTreeViewColumn *, main_window *widgets){
	on_main_edit(self,widgets);
}

void on_main_delete(GtkWidget *, main_window *widgets){
	int active = gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook));
	switch(active){
		case 0:

		yon_delete_confirmation_open(widgets);
		break;
	case 1:
		yon_group_delete_confirmation_open(widgets);
		break;
	case 2:{
		GtkTreeModel *model = GTK_TREE_MODEL(widgets->liststore1);
		GtkTreeIter iter,child;
		if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SystemTree)),&model,&iter)){
			char *target;
			yon_tree_model_sort_filter_get_model_iter(GTK_TREE_MODEL_SORT(model),&iter,&child);
			gtk_tree_model_get(GTK_TREE_MODEL(widgets->liststore1),&child,2,&target,-1);
				system_remove_confirmation_window *window = yon_system_remove_confirmation_window_new();
				dictionary *dict = NULL;
				yon_dictionary_add_or_create_if_exists_with_data(dict,"widgets",widgets);
				yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
			if (config(USERADD(target))){
				yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),DELETE_CONFIRMATION_TITLE_LABEL,icon_path,"user-deletion-window");
				gtk_label_set_markup(GTK_LABEL(window->HeaderLabel),DELETE_CONFIRMATION_TITLE_LABEL);
				gtk_label_set_markup(GTK_LABEL(window->TitleLabel),SYSTEM_USER_CONFIG_REMOVE_CONFIRMATION_LABEL(target));
				g_signal_connect(G_OBJECT(window->ConfigAcceptButton),"clicked",G_CALLBACK(yon_remove_confirmation_window_accept_clicked),dict);
				g_signal_connect(G_OBJECT(window->SystemAcceptButton),"clicked",G_CALLBACK(yon_remove_confirmation_window_accept_clicked),dict);
			} else {
				gtk_label_set_markup(GTK_LABEL(window->HeaderLabel),DELETE_CONFIRMATION_TITLE_LABEL);
				gtk_label_set_markup(GTK_LABEL(window->TitleLabel),SYSTEM_USER_SYSTEM_REMOVE_CONFIRMATION_LABEL(target));
				gtk_widget_show(window->AcceptButton);
				gtk_widget_hide(window->ConfigAcceptButton);
				gtk_widget_hide(window->SystemAcceptButton);
				g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(yon_remove_confirmation_window_accept_clicked),dict);
			}
				gtk_widget_show(window->Window);
		}
	}break;
	case 3:{
		GtkTreeModel *model = GTK_TREE_MODEL(widgets->liststore2);
		GtkTreeIter iter,child;
		if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SystemGroupsTree)),&model,&iter)){
			char *target;
			yon_tree_model_sort_filter_get_model_iter(GTK_TREE_MODEL_SORT(model),&iter,&child);
			gtk_tree_model_get(GTK_TREE_MODEL(widgets->liststore2),&child,1,&target,-1);
				system_remove_confirmation_window *window = yon_system_remove_confirmation_window_new();
				dictionary *dict = NULL;
				yon_dictionary_add_or_create_if_exists_with_data(dict,"widgets",widgets);
				yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
				gtk_label_set_markup(GTK_LABEL(window->HeaderLabel),DELETE_GROUP_CONFIRMATION_TITLE_LABEL);
			if (config(GROUPADD(target))){
				yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),DELETE_GROUP_CONFIRMATION_TITLE_LABEL,icon_path,"user-deletion-window");
				gtk_label_set_markup(GTK_LABEL(window->TitleLabel),SYSTEM_GROUP_CONFIG_REMOVE_CONFIRMATION_LABEL(target));
				g_signal_connect(G_OBJECT(window->ConfigAcceptButton),"clicked",G_CALLBACK(yon_remove_confirmation_window_accept_clicked),dict);
				g_signal_connect(G_OBJECT(window->SystemAcceptButton),"clicked",G_CALLBACK(yon_remove_confirmation_window_accept_clicked),dict);
			} else {
				gtk_label_set_markup(GTK_LABEL(window->TitleLabel),SYSTEM_GROUP_SYSTEM_REMOVE_CONFIRMATION_LABEL(target));
				gtk_widget_show(window->AcceptButton);
				gtk_widget_hide(window->ConfigAcceptButton);
				gtk_widget_hide(window->SystemAcceptButton);
				g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(yon_remove_confirmation_window_accept_clicked),dict);
			}
				gtk_widget_show(window->Window);
		}
	}break;
	}
}

void on_user_info_print(GtkWidget *,yon_user_info_window *window){
	filechooser_window *dialog = yon_file_chooser_window_new(GTK_FILE_CHOOSER_ACTION_SAVE);
	yon_gtk_window_setup(GTK_WINDOW(dialog->Window),GTK_WINDOW(window->Window),SAVE_USER_INFO_LABEL,icon_path,"filechooser_window");
	GtkFileFilter *filter = gtk_file_filter_new();
	gtk_file_filter_add_pattern(filter,"*.pdf");
	gtk_file_filter_set_name(filter,"PDF");
	gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog->MainFileChooser),filter);
	if (yon_file_chooser_start(dialog)==GTK_RESPONSE_APPLY){
		char *path = dialog->last_success_selection;
		char *command = user_info_pdf_command(window->target_user,path);
		yon_launch(command);
		free(command);
		free(dialog);
	}
}

void on_user_info_save(GtkWidget *,yon_user_info_window *window){
	filechooser_window *dialog = yon_file_chooser_window_new(GTK_FILE_CHOOSER_ACTION_SAVE);
	yon_gtk_window_setup(GTK_WINDOW(dialog->Window),GTK_WINDOW(window->Window),SAVE_USER_INFO_LABEL,icon_path,"filechooser_window");
	GtkFileFilter *filter = gtk_file_filter_new();
	gtk_file_filter_add_pattern(filter,"*.txt");
	gtk_file_filter_set_name(filter,"TXT");
	gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog->MainFileChooser),filter);
	if (yon_file_chooser_start(dialog)==GTK_RESPONSE_APPLY){
		char *path = dialog->last_success_selection;
		char *command = user_info_txt_command(window->target_user,path);
		yon_launch(command);
		free(command);
		free(dialog);
	}
}

yon_user_info_window *yon_user_info_window_new(){
	yon_user_info_window *window = new(yon_user_info_window);
	GtkBuilder *builder = gtk_builder_new_from_resource(ui_glade_path_debug);
	window->Window = yon_gtk_builder_get_widget(builder,"Window");
	window->Terminal = yon_gtk_builder_get_widget(builder,"DebuggerTerminal");
	window->HeaderImage = yon_gtk_builder_get_widget(builder,"HeaderImage");
	window->PrintButton = gtk_button_new_from_icon_name(icon_path_print,GTK_ICON_SIZE_BUTTON);
	window->SaveButton = gtk_button_new_from_icon_name(icon_path_save,GTK_ICON_SIZE_BUTTON);
	window->target_user = NULL;

	g_signal_connect(G_OBJECT(window->PrintButton),"clicked",G_CALLBACK(on_user_info_print),window);
	g_signal_connect(G_OBJECT(window->SaveButton),"clicked",G_CALLBACK(on_user_info_save),window);

	gtk_image_set_from_icon_name(GTK_IMAGE(window->HeaderImage),icon_path,GTK_ICON_SIZE_BUTTON);
	gtk_widget_set_tooltip_markup(window->PrintButton,PRINT_TOOLTIP_LABEL);
	gtk_widget_set_tooltip_markup(window->SaveButton,SAVE_LABEL);
	GtkWidget *titlebar = gtk_window_get_titlebar(GTK_WINDOW(window->Window));
	gtk_header_bar_pack_end(GTK_HEADER_BAR(titlebar),window->SaveButton);
	gtk_header_bar_pack_end(GTK_HEADER_BAR(titlebar),window->PrintButton);
	gtk_widget_show(window->PrintButton);
	gtk_widget_show(window->SaveButton);
	return window;
}

void on_user_info(GtkWidget *,main_window *widgets){
	int page = gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook));
	char *target_user = NULL;
	GtkTreeIter iter;
	GtkTreeModel *model = NULL;
	switch (page){
		case 1:
		case 3:{
			return;
		} break;
		case 0:{
			if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->UsersTree)),&model,&iter)){
				return;
			}
		} break;
		case 2:{
			if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SystemTree)),&model,&iter)){
				return;
			}
		}
	}
	gtk_tree_model_get(model,&iter,2,&target_user,-1);
	char *command = user_info_command(target_user);
	// yon_terminal_window_launch(GTK_WINDOW(widgets->Window),command,yon_char_get_localised_from_lib(SUCCESS_LABEL),yon_char_get_localised_from_lib(FAIL_LABEL));
	yon_user_info_window *window = yon_user_info_window_new();
	window->target_user = target_user;
	yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),USER_INFO_LABEL,icon_path,"user_info_window");
	yon_terminal_integrated_start(window->Terminal,command);
	gtk_widget_show(window->Window);
}

void on_config_update(GtkWidget *, main_window *widgets){

	int size;
	config_str parameters = yon_config_get_save_parameters(&size);
	if (size){
		dialog_confirmation_data *data = yon_confirmation_dialog_data_new();

		data->action_text = UPDATE_CONFIRMATION_LABEL;
		data->title = UPDATE_LABEL;
		if (yon_confirmation_dialog_call(widgets->Window,data)!=GTK_RESPONSE_ACCEPT)
			return;

		yon_char_parsed_free(parameters,size);
	}
	if (main_config.load_mode==YON_CONFIG_GLOBAL){
		on_config_global_load(NULL,widgets);
	} else {
		on_config_local_load(NULL,widgets);
	}

	// on_system_update(NULL,widgets);

}


 int yon_element_chosen(GtkWidget *){
	return (int)1;
 }


gboolean on_filter(GtkTreeModel* model, GtkTreeIter *iter, main_window *widgets){
	GtkWidget *check = NULL;
	GtkWidget *search = NULL;
	int id_col=0;
	int name_col = 0;
	int login_col = -1;
	int systemed = 0;
	if ((void*)model == (void*)widgets->UsersList){
		check = widgets->ConfigUserShowSystemCheck;
		search = widgets->ConfigUserSearchEntry;
		id_col=1;
		name_col=2;
		login_col=3;
	} else if ((void*)model == (void*)widgets->GroupsList){
		check = widgets->ConfigGroupShowSystemCheck;
		search = widgets->ConfigGroupSearchEntry;
		id_col=0;
		name_col=1;
	} else if ((void*)model == (void*)widgets->liststore1){
		check = widgets->SystemUserShowSystemCheck;
		search = widgets->SystemUserSearchEntry;
		id_col=1;
		name_col=2;
		login_col=3;
		systemed=1;
	} else if ((void*)model == (void*)widgets->liststore2){
		check = widgets->SystemGroupShowSystemCheck;
		search = widgets->SystemGroupSearchEntry;
		id_col=0;
		name_col=1;
		systemed=1;
	} else return 0;

	char *uid=NULL, *target=NULL, *login=NULL;
	int iuid=-1;
	if (systemed)
		gtk_tree_model_get(model,iter,id_col,&iuid,name_col,&target, login_col,&login,-1);
	else 
		gtk_tree_model_get(model,iter,id_col,&uid,name_col,&target,login_col,&login,-1);
	char *search_text = (char*)gtk_entry_get_text(GTK_ENTRY(search));
	int show_system = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(check));
	if (!target&&(search_text)) return 0;

	int system_id_found = 0;
	int search_found = 0;
	if (!yon_char_is_empty(search_text)){
		char *iuid_string = NULL;
		if (iuid>-1){
			iuid_string = yon_char_from_int(iuid);
		}
		if ((!yon_char_is_empty(uid)&&strstr(uid,search_text))||
		(!yon_char_is_empty(iuid_string)&&strstr(iuid_string,search_text))||
		(!yon_char_is_empty(login)&&strstr(login,search_text))||
		strstr(target,search_text)){
			search_found = 1;
		}
	} else {
		search_found = 1;
	}
	if (systemed){
		if (((iuid>=main_config.MINUID&&iuid<=main_config.MAXUID))||show_system){
			system_id_found = 1;
		}

	} else {
		if (yon_char_is_empty(uid)||((atoi(uid)>=main_config.MINUID&&atoi(uid)<=main_config.MAXUID))||show_system){
			system_id_found = 1;
		}

	}
	
	if (search_found&&system_id_found) return 1;
	return 0;



}

 void on_filter_changed(GtkWidget *self, main_window *widgets){
	if (self == widgets->ConfigUserShowSystemCheck||self == widgets->ConfigUserSearchEntry){
		gtk_tree_view_set_model(GTK_TREE_VIEW(widgets->UsersTree),NULL);
		gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->ConfigUsersFilter));
		gtk_tree_view_set_model(GTK_TREE_VIEW(widgets->UsersTree),GTK_TREE_MODEL(widgets->ConfigUsersSort));
	} else if (self == widgets->ConfigGroupShowSystemCheck||self == widgets->ConfigGroupSearchEntry) {
		gtk_tree_view_set_model(GTK_TREE_VIEW(widgets->GroupsTree),NULL);
		gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->ConfigGroupsFilter));
		gtk_tree_view_set_model(GTK_TREE_VIEW(widgets->GroupsTree),GTK_TREE_MODEL(widgets->ConfigGroupsSort));

	} else if (self == widgets->SystemUserShowSystemCheck||self == widgets->SystemUserSearchEntry) {
		gtk_tree_view_set_model(GTK_TREE_VIEW(widgets->SystemTree),NULL);
		gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->SystemUsersFilter));
		gtk_tree_view_set_model(GTK_TREE_VIEW(widgets->SystemTree),GTK_TREE_MODEL(widgets->SystemUsersSort));

	} else if (self == widgets->SystemGroupShowSystemCheck||self == widgets->SystemGroupSearchEntry) {
		gtk_tree_view_set_model(GTK_TREE_VIEW(widgets->SystemGroupsTree),NULL);
		gtk_tree_model_filter_refilter(GTK_TREE_MODEL_FILTER(widgets->SystemGroupsFilter));
		gtk_tree_view_set_model(GTK_TREE_VIEW(widgets->SystemGroupsTree),GTK_TREE_MODEL(widgets->SystemGroupsSort));

	}
 }

/**config_init() 
 * [EN] 
 *  
 * [RU] 
 * Функция инициализации всех параметров конфигурации 
*/ 
void config_init(){ 
    main_config.always_open_documentation=0; 
    main_config.socket_id=-1; 
    main_config.save_socket_id=-1; 
    main_config.load_socket_id=-1; 
    main_config.lock_help=0; 
    main_config.lock_load_global=0; 
    main_config.lock_save_global=0; 
    main_config.lock_save_local=0;
	main_config.groups_size=0;
	main_config.users_size=0;
	main_config.load_mode=YON_CONFIG_LOCAL;
	main_config.hash_default_id=-1;
	main_config.save_config=0;
	main_config.groups = yon_file_open(groups_path,&main_config.groups_size);
	main_config.users = yon_file_open(users_path,&main_config.users_size);
	main_config.shadow = yon_file_open(shadow_path,&main_config.shadow_size);
	main_config.group_shadow = yon_file_open(group_shadow_path,&main_config.group_shadow_size);
	main_config.users_list=NULL;
	main_config.groups_list=NULL;
	main_config.password_min_length=6;
	main_config.sync_users = NULL;
	main_config.sync_size=0;
	main_config.homes_size=0;
	main_config.remove_homes=NULL;
    main_config.launch_arguments=NULL;
    main_config.launch_size=0;
	int login_size;
	main_config.locales_hash = g_hash_table_new(g_str_hash,g_str_equal);
	config_str login_defs = yon_file_open(uid_path,&login_size);
	for (int i=0;i<login_size;i++){
		if (login_defs[i][0]!='#'){
			char *copy = yon_char_new(login_defs[i]);
			char *divided = yon_char_divide(copy,7);
			if (divided){
				if (!strcmp(divided,"UID_MAX")){
					main_config.MAXUID = atol(strstr(login_defs[i],"\t"));
				}
				else if (!strcmp(divided,"UID_MIN")){
					main_config.MINUID = atol(strstr(login_defs[i],"\t"));
				}
				else if (!strcmp(divided,"GID_MAX")){
					main_config.MAXGID = atol(strstr(login_defs[i],"\t"));
				}
				else if (!strcmp(divided,"GID_MIN")){
					main_config.MINGID = atol(strstr(login_defs[i],"\t"));
				}
			}
		}
	}
	yon_char_parsed_free(login_defs,login_size);
	login_defs = yon_file_open(password_limits_path,&login_size);
	for (int i=0;i<login_size;i++){
		if (login_defs[i][0]!='#'){
			char *copy = yon_char_new(login_defs[i]);
			char *divided = yon_char_divide(copy,7);
			if (divided){
				if (!strcmp(divided,"minlen")){
					main_config.password_min_length = atol(strstr(login_defs[i],"\t"));
				}
			}
		}
	}
	yon_char_parsed_free(login_defs,login_size);
	int size=0;
	main_config.default_home = NULL;
	config_str file_output = yon_file_open(default_home_initiator_path,&size);
	for (int i=0;i<size;i++){
		char *parameter = yon_char_divide_search(file_output[i],"=",-1);
		if (!strcmp(parameter,"HOME")){
			if (file_output[i][strlen(file_output[i])-1]=='\n') file_output[i][strlen(file_output[i])-1]='\0';
			main_config.default_home=yon_char_new(file_output[i]);
		}
		free(parameter);
	}
} 

void on_notebook_page_changed(GtkWidget *, GtkWidget *, int num, main_window *widgets){
	GtkTreeIter iter;
	if (num==0){
		gtk_widget_show(widgets->AddButton);
		gtk_widget_show(widgets->EditButton);
		gtk_widget_show(widgets->DeleteButton);
		gtk_widget_show(widgets->UserInfoButton);
		gtk_widget_hide(widgets->UserAddToConfigButton);
	} else if (num==1){
		gtk_widget_show(widgets->AddButton);
		gtk_widget_show(widgets->EditButton);
		gtk_widget_show(widgets->DeleteButton);
		gtk_widget_hide(widgets->UserAddToConfigButton);
		gtk_widget_hide(widgets->UserInfoButton);
	} else if (num==2){
		gtk_widget_hide(widgets->AddButton);
		gtk_widget_hide(widgets->EditButton);
		gtk_widget_show(widgets->UserInfoButton);
		gtk_widget_show(widgets->UserAddToConfigButton);
	} else if (num==3){
		
		gtk_widget_hide(widgets->AddButton);
		gtk_widget_hide(widgets->EditButton);
		gtk_widget_show(widgets->UserAddToConfigButton);
		gtk_widget_hide(widgets->UserInfoButton);
		GtkTreeModel *model = GTK_TREE_MODEL(widgets->liststore2);
		if (gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->SystemGroupsTree)),&model,&iter)){
			gtk_widget_set_sensitive(widgets->UserAddToConfigButton,1);
			gtk_widget_set_sensitive(widgets->DeleteButton,1);
		} else {
			gtk_widget_set_sensitive(widgets->UserAddToConfigButton,0);
			gtk_widget_set_sensitive(widgets->DeleteButton,0);
		}
	}
}

typedef int (*RmbCheck)(void*);

gboolean on_menu_open(GtkWidget *self,GdkEventButton *event, rmb_menu_window *window){
	if (event->button==3){
		GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(self));
		GtkTreeIter iter;
		if (gtk_tree_model_get_iter_first(model,&iter)&&gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(self)),&model,&iter)){
			gtk_widget_set_sensitive(GTK_WIDGET(window->buttons->first->next->data),1);
			gtk_widget_set_sensitive(GTK_WIDGET(window->buttons->first->next->next->data),1);
		} else {
			gtk_widget_set_sensitive(GTK_WIDGET(window->buttons->first->next->data),0);
			gtk_widget_set_sensitive(GTK_WIDGET(window->buttons->first->next->next->data),0);
		}
	}
	return 0;
}

gboolean on_system_menu_open(GtkWidget *self,GdkEventButton *event, rmb_menu_window *window){
	if (event->button==3){
		GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(self));
		GtkTreeIter iter;
		int chosen = gtk_tree_model_get_iter_first(model,&iter)&&gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(self)),&model,&iter);
		if (chosen){
			gtk_widget_set_sensitive(GTK_WIDGET(window->buttons->first->data),1);
			gtk_widget_set_sensitive(GTK_WIDGET(window->buttons->first->next->data),1);
		} else {
			gtk_widget_set_sensitive(GTK_WIDGET(window->buttons->first->data),0);
			gtk_widget_set_sensitive(GTK_WIDGET(window->buttons->first->next->data),0);
		}
	}
	return 0;
}

void on_date_format_changed(GtkWidget *, main_window *widgets){
    yon_interface_update(widgets);
}

/**yon_main_window_complete(main_window *widgets) 
 * [EN] 
 *  
 * [RU] 
 * Функция настройки основного окна приложения. [widgets] - структура со стандартным интерфейсом. 
*/ 
template_main_window *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,"PlugBox"),1,1,0); 
    // Custom widgets configuration 
    
	widgets->CacheAlgList=GTK_LIST_STORE(gtk_builder_get_object(builder,"CacheAlgList"));
	widgets->GroupsList=GTK_LIST_STORE(gtk_builder_get_object(builder,"GroupsList"));
	widgets->UsersList=GTK_LIST_STORE(gtk_builder_get_object(builder,"UsersList"));
	widgets->liststore1=GTK_LIST_STORE(gtk_builder_get_object(builder,"liststore1"));
	widgets->liststore2=GTK_LIST_STORE(gtk_builder_get_object(builder,"liststore2"));
	widgets->PlugBox=yon_gtk_builder_get_widget(builder,"PlugBox");
	widgets->UserAddToConfigButton=yon_gtk_builder_get_widget(builder,"UserAddToConfigButton");
	widgets->SystemTree=yon_gtk_builder_get_widget(builder,"SystemTree");
	widgets->SystemGroupsTree=yon_gtk_builder_get_widget(builder,"SystemGroupsTree");
	widgets->button2=yon_gtk_builder_get_widget(builder,"button2");
	widgets->button3=yon_gtk_builder_get_widget(builder,"button3");
	widgets->AddButton=yon_gtk_builder_get_widget(builder,"AddButton");
	widgets->EditButton=yon_gtk_builder_get_widget(builder,"EditButton");
	widgets->DeleteButton=yon_gtk_builder_get_widget(builder,"DeleteButton");
	widgets->UpdateButton=yon_gtk_builder_get_widget(builder,"UpdateButton");
	widgets->UserInfoButton=yon_gtk_builder_get_widget(builder,"UserInfoButton");
	widgets->UsersTree=yon_gtk_builder_get_widget(builder,"UsersTree");
	widgets->GroupsTree=yon_gtk_builder_get_widget(builder,"GroupsTree");
	widgets->MainNotebook=yon_gtk_builder_get_widget(builder,"MainNotebook");
	widgets->SavingSettingsButton=yon_gtk_builder_get_widget(builder,"SavingSettingsButton");
    
	widgets->ConfigUserShowSystemCheck=yon_gtk_builder_get_widget(builder,"ConfigUserShowSystemCheck");
	widgets->ConfigUserSearchEntry=yon_gtk_builder_get_widget(builder,"ConfigUserSearchEntry");
	widgets->ConfigGroupShowSystemCheck=yon_gtk_builder_get_widget(builder,"ConfigGroupShowSystemCheck");
	widgets->ConfigGroupSearchEntry=yon_gtk_builder_get_widget(builder,"ConfigGroupSearchEntry");
	widgets->SystemUserShowSystemCheck=yon_gtk_builder_get_widget(builder,"SystemUserShowSystemCheck");
	widgets->SystemUserSearchEntry=yon_gtk_builder_get_widget(builder,"SystemUserSearchEntry");
	widgets->SystemGroupShowSystemCheck=yon_gtk_builder_get_widget(builder,"SystemGroupShowSystemCheck");
	widgets->SystemGroupSearchEntry=yon_gtk_builder_get_widget(builder,"SystemGroupSearchEntry");

	widgets->ConfigUsersFilter = GTK_TREE_MODEL(gtk_builder_get_object(builder,"ConfigUsersFilter"));
	widgets->ConfigUsersSort = GTK_TREE_MODEL(gtk_builder_get_object(builder,"ConfigUsersSort"));
	widgets->ConfigGroupsFilter = GTK_TREE_MODEL(gtk_builder_get_object(builder,"ConfigGroupsFilter"));
	widgets->ConfigGroupsSort = GTK_TREE_MODEL(gtk_builder_get_object(builder,"ConfigGroupsSort"));
	widgets->SystemUsersFilter = GTK_TREE_MODEL(gtk_builder_get_object(builder,"SystemUsersFilter"));
	widgets->SystemUsersSort = GTK_TREE_MODEL(gtk_builder_get_object(builder,"SystemUsersSort"));
	widgets->SystemGroupsFilter = GTK_TREE_MODEL(gtk_builder_get_object(builder,"SystemGroupsFilter"));
	widgets->SystemGroupsSort = GTK_TREE_MODEL(gtk_builder_get_object(builder,"SystemGroupsSort"));
    
	main_config.users_list = widgets->UsersList;
	main_config.groups_list = widgets->GroupsList;
	g_thread_new("locales",(GThreadFunc)yon_load_languages,NULL);


    /* Signal connection | Присоединение сигналов */
    g_signal_connect(G_OBJECT(widgets->AddButton),"clicked",G_CALLBACK(on_main_add),widgets);
    g_signal_connect(G_OBJECT(widgets->EditButton),"clicked",G_CALLBACK(on_main_edit),widgets);
    g_signal_connect(G_OBJECT(widgets->DeleteButton),"clicked",G_CALLBACK(on_main_delete),widgets);
    g_signal_connect(G_OBJECT(widgets->button3),"clicked",G_CALLBACK(on_standard_groups_open),widgets);
    g_signal_connect(G_OBJECT(widgets->button2),"clicked",G_CALLBACK(on_ubl_settings_usergroups_additional_settings_open),widgets);
    g_signal_connect(G_OBJECT(widgets->UserAddToConfigButton),"clicked",G_CALLBACK(on_user_sync_with_config),widgets);
    g_signal_connect(G_OBJECT(widgets->UpdateButton),"clicked",G_CALLBACK(on_config_update),widgets);
    g_signal_connect(G_OBJECT(widgets->UserInfoButton),"clicked",G_CALLBACK(on_user_info),widgets);
	g_signal_connect(G_OBJECT(widgets->SavingSettingsButton),"clicked",G_CALLBACK(on_saving_settings_open),widgets);
    g_signal_connect(G_OBJECT(widgets->SaveMenuItem),"activate",G_CALLBACK(on_config_save),widgets);
    g_signal_connect(G_OBJECT(widgets->UsersTree),"row-activated",G_CALLBACK(on_main_edit_activate),widgets);
    g_signal_connect(G_OBJECT(widgets->GroupsTree),"row-activated",G_CALLBACK(on_main_edit_activate),widgets);
    g_signal_connect(G_OBJECT(widgets->MainNotebook),"switch-page",G_CALLBACK(on_notebook_page_changed),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->ConfigUserShowSystemCheck),"toggled",G_CALLBACK(on_filter_changed),widgets);
	g_signal_connect(G_OBJECT(widgets->ConfigUserSearchEntry),"changed",G_CALLBACK(on_filter_changed),widgets);
	g_signal_connect(G_OBJECT(widgets->ConfigGroupShowSystemCheck),"toggled",G_CALLBACK(on_filter_changed),widgets);
	g_signal_connect(G_OBJECT(widgets->ConfigGroupSearchEntry),"changed",G_CALLBACK(on_filter_changed),widgets);
	g_signal_connect(G_OBJECT(widgets->SystemUserShowSystemCheck),"toggled",G_CALLBACK(on_filter_changed),widgets);
	g_signal_connect(G_OBJECT(widgets->SystemUserSearchEntry),"changed",G_CALLBACK(on_filter_changed),widgets);
	g_signal_connect(G_OBJECT(widgets->SystemGroupShowSystemCheck),"toggled",G_CALLBACK(on_filter_changed),widgets);
	g_signal_connect(G_OBJECT(widgets->SystemGroupSearchEntry),"changed",G_CALLBACK(on_filter_changed),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);
	widgets->UsersMenu = yon_rmb_menu_setup(widgets->UsersTree,(RmbCheck)yon_element_chosen,widgets->UsersTree,CREATE_NEW_LABEL,add_icon_name,G_CALLBACK(on_main_add),widgets,EDIT_LABEL,edit_icon_name,G_CALLBACK(on_main_edit),widgets, REMOVE_LABEL,delete_icon_name,G_CALLBACK(on_main_delete),widgets,UPDATE_LABEL,update_icon_name,G_CALLBACK(on_config_update),widgets,NULL);
	widgets->GroupsMenu = yon_rmb_menu_setup(widgets->GroupsTree,(RmbCheck)yon_element_chosen,widgets->GroupsTree,CREATE_NEW_LABEL,add_icon_name,G_CALLBACK(on_main_add),widgets,EDIT_LABEL,edit_icon_name,G_CALLBACK(on_main_edit),widgets,REMOVE_LABEL,delete_icon_name,G_CALLBACK(on_main_delete),widgets,UPDATE_LABEL,update_icon_name,G_CALLBACK(on_config_update),widgets,NULL);
	widgets->SystemUsersMenu = yon_rmb_menu_setup(widgets->SystemTree,(RmbCheck)yon_element_chosen,widgets->GroupsTree,SYNC_TOOLTIP_LABEL,sync_icon_name,G_CALLBACK(on_user_sync_with_config),widgets,REMOVE_LABEL,delete_icon_name,G_CALLBACK(on_main_delete),widgets,UPDATE_LABEL,update_icon_name,G_CALLBACK(on_config_update),widgets,NULL);
	widgets->SystemGroupsMenu = yon_rmb_menu_setup(widgets->SystemGroupsTree,(RmbCheck)yon_element_chosen,widgets->GroupsTree,SYNC_TOOLTIP_LABEL,sync_icon_name,G_CALLBACK(on_user_sync_with_config),widgets,REMOVE_LABEL,delete_icon_name,G_CALLBACK(on_main_delete),widgets,UPDATE_LABEL,update_icon_name,G_CALLBACK(on_config_update),widgets,NULL);

	g_signal_connect(G_OBJECT(widgets->UsersTree),"button-press-event",G_CALLBACK(on_menu_open),widgets->UsersMenu);
	g_signal_connect(G_OBJECT(widgets->GroupsTree),"button-press-event",G_CALLBACK(on_menu_open),widgets->GroupsMenu);
	g_signal_connect(G_OBJECT(widgets->SystemTree),"button-press-event",G_CALLBACK(on_system_menu_open),widgets->UsersMenu);
	g_signal_connect(G_OBJECT(widgets->SystemGroupsTree),"button-press-event",G_CALLBACK(on_system_menu_open),widgets->GroupsMenu);

	gtk_tree_view_column_clicked(gtk_tree_view_get_column(GTK_TREE_VIEW(widgets->UsersTree),1));
	gtk_tree_view_column_clicked(gtk_tree_view_get_column(GTK_TREE_VIEW(widgets->GroupsTree),0));
	gtk_tree_view_column_clicked(gtk_tree_view_get_column(GTK_TREE_VIEW(widgets->SystemTree),1));
	gtk_tree_view_column_clicked(gtk_tree_view_get_column(GTK_TREE_VIEW(widgets->SystemGroupsTree),0));

	gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(widgets->ConfigUsersFilter),(GtkTreeModelFilterVisibleFunc)on_filter,widgets,NULL);
	gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(widgets->ConfigGroupsFilter),(GtkTreeModelFilterVisibleFunc)on_filter,widgets,NULL);
	gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(widgets->SystemUsersFilter),(GtkTreeModelFilterVisibleFunc)on_filter,widgets,NULL);
	gtk_tree_model_filter_set_visible_func(GTK_TREE_MODEL_FILTER(widgets->SystemGroupsFilter),(GtkTreeModelFilterVisibleFunc)on_filter,widgets,NULL);
	// on_system_update(NULL,widgets);

    yon_root_button_setup((template_main_window*)widgets,main_config.launch_arguments,main_config.launch_size);

    gtk_widget_realize(GTK_WIDGET(widgets->UsersTree));
    gtk_widget_realize(GTK_WIDGET(widgets->GroupsTree));
	
	GList *list = gtk_tree_view_get_columns(GTK_TREE_VIEW(widgets->UsersTree));
	for (guint i=0;i<g_list_length(list);i++){
			yon_gtk_tree_view_column_minimal_fixed_size_set(GTK_TREE_VIEW_COLUMN(g_list_nth_data(list,i)));
		yon_window_config_add_listener((GtkWidget*)g_list_nth_data(list,i),yon_char_append("UserColumn",yon_char_from_int(i)),"fixed-width",YON_TYPE_INT);
	}
	g_list_free(list);
	list = gtk_tree_view_get_columns(GTK_TREE_VIEW(widgets->GroupsTree));
	for (guint i=0;i<g_list_length(list);i++){
			yon_gtk_tree_view_column_minimal_fixed_size_set(GTK_TREE_VIEW_COLUMN(g_list_nth_data(list,i)));
		yon_window_config_add_listener((GtkWidget*)g_list_nth_data(list,i),yon_char_append("GroupColumn",yon_char_from_int(i)),"fixed-width",YON_TYPE_INT);
	}
	g_list_free(list);
	list = gtk_tree_view_get_columns(GTK_TREE_VIEW(widgets->SystemTree));
	for (guint i=0;i<g_list_length(list);i++){
			yon_gtk_tree_view_column_minimal_fixed_size_set(GTK_TREE_VIEW_COLUMN(g_list_nth_data(list,i)));
		yon_window_config_add_listener((GtkWidget*)g_list_nth_data(list,i),yon_char_append("SystemUserColumn",yon_char_from_int(i)),"fixed-width",YON_TYPE_INT);
	}
	g_list_free(list);
	list = gtk_tree_view_get_columns(GTK_TREE_VIEW(widgets->SystemGroupsTree));
	for (guint i=0;i<g_list_length(list);i++){
			yon_gtk_tree_view_column_minimal_fixed_size_set(GTK_TREE_VIEW_COLUMN(g_list_nth_data(list,i)));
		yon_window_config_add_listener((GtkWidget*)g_list_nth_data(list,i),yon_char_append("SystemGroupColumn",yon_char_from_int(i)),"fixed-width",YON_TYPE_INT);
	}
	g_list_free(list);

	yon_window_config_load(config_path);

        yon_ubl_settings_window_init(GTK_MENU(gtk_widget_get_parent(widgets->DocumentationMenuItem)));
        GList *menu_items = gtk_container_get_children(GTK_CONTAINER(gtk_widget_get_parent(widgets->DocumentationMenuItem)));
        for (guint i=0;i<g_list_length(menu_items);i++){
            if (i==0){
                gtk_style_context_remove_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitemmiddle");
                gtk_style_context_add_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitemtop");
            } else if (i==g_list_length(menu_items)-1){
                gtk_style_context_remove_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitemmiddle");
                gtk_style_context_remove_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitemtop");
                gtk_style_context_add_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitembottom");
            } else {
                gtk_style_context_remove_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitembottom");
                gtk_style_context_remove_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitemtop");
                gtk_style_context_add_class(gtk_widget_get_style_context((GtkWidget*)g_list_nth_data(menu_items,i)),"menuitemmiddle");
            }
        }
        g_list_free(menu_items);
		struct yon_configuration_window_custom_parameter *parameter = malloc(sizeof(struct yon_configuration_window_custom_parameter));
		parameter->custom_parameter_create_func = yon_date_chooser_new;
		parameter->custom_parameter_save_value_func = yon_date_chooser_save;
		parameter->custom_parameter_update_func = yon_date_chooser_update;
		yon_configuration_window_add_custom_parameter(date_format_parameter,parameter);


	yon_system_load(widgets);
	yon_load_proceed(YON_CONFIG_LOCAL);
	yon_interface_update((main_window*)widgets);
	yon_save_window_set_postsave_function(on_save_done,widgets);
	yon_save_window_set_postsave_failure_function(on_save_failed,widgets);
return (template_main_window*)widgets;
} 
 
int main(int argc, char *argv[]){ 
	if (setlocale(LC_ALL,"")){};
    textdomain (LocaleName); 
    config_init();

    yon_ubl_connect_config((_template_config*)&main_config); 
    yon_ubl_window_init(TITLE_LABEL,TITLE_INFO_LABEL,LocaleName,CssPath,LocaleName,version_application,WIKI_LINK);
	config_str unfound = NULL;
	int size=0;
	yon_ubl_setup_arguments(argc,argv,&unfound,&size,NULL);
    gtk_init(&argc,&argv); 
    template_main_window *widgets = yon_ubl_window_setup(); 
    main_config.launch_arguments=yon_char_parsed_copy(argv,argc);
    main_config.launch_size=argc;
    widgets = yon_main_window_complete((main_window*)widgets);
	yon_window_set_exit_config_check(widgets,G_CALLBACK(yon_hide_passwords));
	    gtk_main();
}