#include "ubl-settings-usergroups.h"

gboolean on_login_focus_event(GtkWidget *self, GdkEvent *, ubl_settings_usergroups_user_window *window){
	gtk_popover_set_relative_to(GTK_POPOVER(window->LoginTooltipPopover),self);
	gtk_popover_set_position(GTK_POPOVER(window->LoginTooltipPopover),GTK_POS_RIGHT);
	g_signal_handlers_block_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_out_event),window);
	g_signal_handlers_block_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_event),window);
	gtk_popover_popup(GTK_POPOVER(window->LoginTooltipPopover));
	gtk_widget_grab_focus(self);
	g_signal_handlers_unblock_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_out_event),window);
	g_signal_handlers_unblock_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_event),window);
	return GDK_EVENT_PROPAGATE;
} 

gboolean on_login_focus_out_event(GtkWidget *self, GdkEvent *, ubl_settings_usergroups_user_window *window){
	gtk_popover_set_relative_to(GTK_POPOVER(window->LoginTooltipPopover),self);
	gtk_popover_set_position(GTK_POPOVER(window->LoginTooltipPopover),GTK_POS_RIGHT);
	gtk_popover_popdown(GTK_POPOVER(window->LoginTooltipPopover));
	return GDK_EVENT_PROPAGATE;
}
gboolean on_login_tooltip_event(GtkWidget *self, GdkEvent *, ubl_settings_usergroups_user_window *window){
	gtk_popover_set_relative_to(GTK_POPOVER(window->LoginTooltipPopover),self);
	gtk_popover_set_position(GTK_POPOVER(window->LoginTooltipPopover),GTK_POS_RIGHT);
	g_signal_handlers_block_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_out_event),window);
	g_signal_handlers_block_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_event),window);
	gtk_popover_popup(GTK_POPOVER(window->LoginTooltipPopover));
	g_signal_handlers_unblock_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_out_event),window);
	g_signal_handlers_unblock_by_func(G_OBJECT(self),G_CALLBACK(on_password_focus_event),window);
	return GDK_EVENT_PROPAGATE;
}

gboolean on_login_tooltip_out_event(GtkWidget *self, GdkEvent *, ubl_settings_usergroups_user_window *window){
	if (gtk_widget_has_focus(window->userLoginEntry)) return GDK_EVENT_PROPAGATE;
	gtk_popover_set_relative_to(GTK_POPOVER(window->LoginTooltipPopover),self);
	gtk_popover_set_position(GTK_POPOVER(window->LoginTooltipPopover),GTK_POS_RIGHT);
	gtk_popover_popdown(GTK_POPOVER(window->LoginTooltipPopover));
	return GDK_EVENT_PROPAGATE;
}

char *yon_user_get_system_groups(char *target_login){
	char *groups_string = NULL;
	groups_string="";
	int shadow_parsed_size=0;
	config_str shadow_parsed = NULL;
	if (main_config.shadow_size){
		for (int i=0;i<main_config.shadow_size;i++){
			if (strstr(main_config.shadow[i],target_login)){
				shadow_parsed = yon_char_parse(main_config.shadow[i],&shadow_parsed_size,":");
				break;
			}
		}
	}
	int target_pos=0;
	for (;target_pos<main_config.shadow_size;target_pos++){
		int parsed_gr_size;
		config_str parsed_group = yon_char_parse(main_config.groups[target_pos],&parsed_gr_size,":");
		if (shadow_parsed_size&&strstr(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);
	}
	yon_char_parsed_free(shadow_parsed,shadow_parsed_size);
	if (yon_char_is_empty(groups_string)) return NULL;
	return groups_string;
}

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

	
    for (i = 0; i < length; i++) {
        if ((text[i]>='a'&&text[i]<='z')||(text[i]>='0'&&text[i]<='9')||strchr("_-",text[i])) {
            new_text[j] = text[i];
            j++;
        }
    }

    if (j < length) {
        g_signal_handlers_block_by_func(editable, G_CALLBACK(on_login_insert_custom_restricted_check), NULL);
        gtk_editable_insert_text(editable, new_text, j, position);
        g_signal_handlers_unblock_by_func(editable, G_CALLBACK(on_login_insert_custom_restricted_check), NULL);
        g_signal_stop_emission_by_name(editable, "insert-text");
    }

    g_free(new_text);
}

void yon_gtk_login_block_symbols(GtkEntry *target){
    g_return_if_fail(GTK_IS_ENTRY(target));
    g_signal_connect(G_OBJECT(target),"insert-text",G_CALLBACK(on_login_insert_custom_restricted_check),NULL);
}

void on_parameter_changed(GtkWidget *,ubl_settings_usergroups_user_window *window){
	char *user_shell=NULL;
	char *user_home=NULL;
	// char *login=NULL;
	char *create_system_user=NULL;
	char *create_ununique=NULL;
	char *do_not_check=NULL;
	char *main_group=NULL;
	char *no_user_group=NULL;

	int do_not_check_actve = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userDontCheckCheck));
	if (do_not_check_actve)
		do_not_check = "--badnames";

	// login = (char*)gtk_entry_get_text(GTK_ENTRY(window->userLoginEntry));
	// if (login[0]>'0'&&login[0]<'9'&&!do_not_check_actve){
	// 		yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),USER_BEGINS_WITH_DIGIT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
	// 		gtk_notebook_set_current_page(GTK_NOTEBOOK(window->MainNotebook),0);
	// 		yon_ubl_status_highlight_incorrect(window->userLoginEntry);
	// 		return;
	// }
	// if (yon_char_is_empty(login)){
	// 		yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
	// 		gtk_notebook_set_current_page(GTK_NOTEBOOK(window->MainNotebook),0);
	// 		yon_ubl_status_highlight_incorrect(window->userLoginEntry);
	// 		return;
	// }

	char *user_lang = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->LanguageCombo));
	
	if (!yon_char_is_empty(user_lang)){
		user_lang = yon_char_unite("--lang ",user_lang,NULL);

	} else user_lang="";

	int user_shell_active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->userShellCombo));
	if (user_shell_active>0){
		user_shell = yon_char_unite("--shell ",!strcmp(gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->userShellCombo)),"Set")?(char*)gtk_entry_get_text(GTK_ENTRY(window->userShellEntry)):(char*)gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->userShellCombo)),NULL);
		
	} else user_shell="";

	int user_home_active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->userHomeCombo));
	if (user_home_active==2){
		char *path = (char*)gtk_entry_get_text(GTK_ENTRY(window->userHomeEntry));
		if (!yon_char_is_empty(path))
		user_home=yon_char_unite("--home-dir ",path,NULL);
		else user_home = ""; 
	} else if (user_home_active==1){
		user_home="--no-create-home";
	} else user_home = "";

	int system_user_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userCreateSystemCheck));
	if (system_user_active)
		create_system_user = "--system";

	int create_ununique_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userCreateUnuniqueCheck));
	if (create_ununique_active)
		create_ununique = "--non-unique";
	int main_group_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userGroupsCheck));
	if (!main_group_active){
		main_group = (char*)gtk_entry_get_text(GTK_ENTRY(window->userGroupsEntry));
		if (yon_char_is_empty(main_group)){
				no_user_group="--no-user-group";
		}
	} else {
		main_group = "";
	}
	char *final_string = user_home;
	final_string = yon_char_unite(final_string,!yon_char_is_empty(final_string)?" ":"",
	yon_char_return_if_exist(user_shell,""),
	!yon_char_is_empty(user_shell)?" ":"",
	yon_char_return_if_exist(user_lang,""),
	!yon_char_is_empty(user_lang)?" ":"",
	yon_char_return_if_exist(create_system_user,""),
	!yon_char_is_empty(create_system_user)?" ":"",
	yon_char_return_if_exist(create_ununique,""),
	!yon_char_is_empty(create_ununique)?" ":"",
	yon_char_return_if_exist(do_not_check,""),
	!yon_char_is_empty(do_not_check)?" ":"",
	yon_char_return_if_exist(no_user_group,""),
	NULL);
	if (final_string&&strlen(final_string)){
		if (final_string[strlen(final_string)-1]==' ') final_string[strlen(final_string)-1]='\0';
		gtk_entry_set_text(GTK_ENTRY(window->userExtraOptionsEntry),final_string);
		free(final_string);
	} else {
		gtk_entry_set_text(GTK_ENTRY(window->userExtraOptionsEntry),"");
	}
}

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

}

void on_home_changed(GtkWidget *self,ubl_settings_usergroups_user_window *window){
	const char *homepath = gtk_entry_get_text(GTK_ENTRY(self));
	if (yon_char_is_empty(homepath)) return;
	char *home_parameter = yon_char_unite("--home-dir ",homepath,NULL);
	int size;
	config_str parameters = yon_config_get_all_by_key(USERADD_SEARCH_macro,&size);
	for (int i=0;i<size;i++){
		int parsed_size;
		config_str parsed = yon_char_parse(parameters[i],&parsed_size,":");
		if (strstr(parsed[4],home_parameter)){
			yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),HOMEDIR_USED_LABEL,5,BACKGROUND_IMAGE_INFO_TYPE);
			yon_ubl_status_highlight_incorrect(window->userHomeEntry);
			break;
		}
		yon_char_parsed_free(parsed,parsed_size);
	}
	yon_char_parsed_free(parameters,size);

	for (int i=0;i<main_config.users_size; i++){
		int parsed_size;
		config_str parsed = yon_char_parse(main_config.users[i],&parsed_size,":");
		if (strstr(parsed[5],homepath)){
			yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),HOMEDIR_USED_LABEL,5,BACKGROUND_IMAGE_INFO_TYPE);
			yon_ubl_status_highlight_incorrect(window->userHomeEntry);
			break;
		}
		yon_char_parsed_free(parsed,parsed_size);
	}
	free(home_parameter);
}

gboolean yon_user_set_locales(GtkWidget *target){
	char *languages_string = config(LOCALE_parameter);
	int languages_size;
	config_str languages = yon_char_parse(languages_string,&languages_size,",");
	GHashTable *hash = main_config.locales_hash;
	char *config_lang = g_object_get_data(G_OBJECT(target),"value");
	for (int i=0;i<languages_size;i++){
		char *locale_str = g_hash_table_lookup(hash,languages[i]);
		if (!yon_char_is_empty(locale_str)){
			int parsed_size;
			config_str parsed = yon_char_parse(locale_str,&parsed_size,"|");
			gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(target),parsed[0],parsed[1]);
		}
	}
	if (!yon_char_is_empty(config_lang)){
		gtk_combo_box_set_active_id(GTK_COMBO_BOX(target),config_lang);
	}
	return G_SOURCE_REMOVE;
}

void *yon_load_languages(void *){
	int size;
	config_str locales = yon_config_load(languages_command,&size);
	for (int i=0;i<size;i++){
		yon_char_remove_last_symbol(locales[i],'\n');
		int parsed_size;
		config_str parsed = yon_char_parse(locales[i],&parsed_size,"|");
		g_hash_table_insert(main_config.locales_hash,yon_char_new(parsed[0]),yon_char_new(locales[i]));
	}
	g_thread_exit(NULL);
}

ubl_settings_usergroups_user_window *yon_ubl_settings_usergroups_user_new(){
	ubl_settings_usergroups_user_window *window = malloc(sizeof(ubl_settings_usergroups_user_window));
	GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_ubl_settings_usergroups_user);
	window->CreateUserWindow=yon_gtk_builder_get_widget(builder,"CreateUserWindow");
	window->StatusBox=yon_gtk_builder_get_widget(builder,"StatusBox");
	window->userUIDAutoCheck=yon_gtk_builder_get_widget(builder,"userUIDAutoCheck");
	window->userUIDEntry=yon_gtk_builder_get_widget(builder,"userUIDEntry");
	window->userLoginEntry=yon_gtk_builder_get_widget(builder,"userLoginEntry");
	window->userPasswordCombo=yon_gtk_builder_get_widget(builder,"userPasswordCombo");
	window->userPasswordEntry=yon_gtk_builder_get_widget(builder,"userPasswordEntry");
	window->userPasswordButton=yon_gtk_builder_get_widget(builder,"userPasswordButton");
	window->userUserNameEntry=yon_gtk_builder_get_widget(builder,"userUserNameEntry");
	window->userGroupsCheck=yon_gtk_builder_get_widget(builder,"userGroupsCheck");
	window->userGroupsEntry=yon_gtk_builder_get_widget(builder,"userGroupsEntry");
	window->UserGroupsButton=yon_gtk_builder_get_widget(builder,"UserGroupsButton");
	window->userAdditionalGroupsEntry=yon_gtk_builder_get_widget(builder,"userAdditionalGroupsEntry");
	window->userAdditionalGroupsButton=yon_gtk_builder_get_widget(builder,"userAdditionalGroupsButton");
	window->userPasswordChangedEntry=yon_gtk_builder_get_widget(builder,"userPasswordChangedEntry");
	window->userPasswordExpirationEntry=yon_gtk_builder_get_widget(builder,"userPasswordExpirationEntry");
	window->userPasswordExpirationButton=yon_gtk_builder_get_widget(builder,"userPasswordExpirationButton");
	window->userPasswordChangeDelayMinimumSpin=yon_gtk_builder_get_widget(builder,"userPasswordChangeDelayMinimumSpin");
	window->userPasswordChangeDelayMaximumSpin=yon_gtk_builder_get_widget(builder,"userPasswordChangeDelayMaximumSpin");
	window->userWarningSpin=yon_gtk_builder_get_widget(builder,"userWarningSpin");
	window->userActivitySpin=yon_gtk_builder_get_widget(builder,"userActivitySpin");
	window->userForceChangeCheck=yon_gtk_builder_get_widget(builder,"userForceChangeCheck");
	window->LanguageCombo=yon_gtk_builder_get_widget(builder,"LanguageCombo");
	window->userShellCombo=yon_gtk_builder_get_widget(builder,"userShellCombo");
	window->userShellEntry=yon_gtk_builder_get_widget(builder,"userShellEntry");
	window->userHomeCombo=yon_gtk_builder_get_widget(builder,"userHomeCombo");
	window->userHomeEntry=yon_gtk_builder_get_widget(builder,"userHomeEntry");
	window->userHomeButton=yon_gtk_builder_get_widget(builder,"userHomeButton");
	window->userCreateSystemCheck=yon_gtk_builder_get_widget(builder,"userCreateSystemCheck");
	window->userCreateUnuniqueCheck=yon_gtk_builder_get_widget(builder,"userCreateUnuniqueCheck");
	window->userDontCheckCheck=yon_gtk_builder_get_widget(builder,"userDontCheckCheck");
	window->userSyncSAMBACheck=yon_gtk_builder_get_widget(builder,"userSyncSAMBACheck");
	window->userSyncSAMBAPasswordCheck=yon_gtk_builder_get_widget(builder,"userSyncSAMBAPasswordCheck");
	window->userDeactivatedCheck=yon_gtk_builder_get_widget(builder,"userDeactivatedCheck");
	window->userExtraOptionsEntry=yon_gtk_builder_get_widget(builder,"userExtraOptionsEntry");
	window->UserCancelButton=yon_gtk_builder_get_widget(builder,"UserCancelButton");
	window->UserOkButton=yon_gtk_builder_get_widget(builder,"UserOkButton");
	window->CalendarPopup=yon_gtk_builder_get_widget(builder,"CalendarPopover");
	window->UseraddBootCheck=yon_gtk_builder_get_widget(builder,"UseraddBootCheck");
	window->ExpirationCalendar=yon_gtk_builder_get_widget(builder,"ExpirationCalendar");
	window->UseraddShutdownCheck=yon_gtk_builder_get_widget(builder,"UseraddShutdownCheck");
	window->UsershadowBootCheck=yon_gtk_builder_get_widget(builder,"UsershadowBootCheck");
	window->UsershadowShutdownCheck=yon_gtk_builder_get_widget(builder,"UsershadowShutdownCheck");
	window->userTitleNameLabel=yon_gtk_builder_get_widget(builder,"userTitleNameLabel");
	window->MainNotebook=yon_gtk_builder_get_widget(builder,"MainNotebook");
	window->LoginTooltipPopover=yon_gtk_builder_get_widget(builder,"LoginTooltipPopover");
	window->expiration_unix=NULL;
	window->last_uid=NULL;
	yon_entry_set_pattern(GTK_ENTRY(window->userLoginEntry),"[a-zA-Z_][a-zA-Z0-9_.-]{1,}");
	yon_gtk_entry_block_symbols(GTK_ENTRY(window->userUserNameEntry),"'\":\n");
	yon_gtk_entry_block_symbols(GTK_ENTRY(window->userLoginEntry),"'\"=:\n");

	time_t t = time(NULL);
	struct tm tm = *localtime(&t);
	gtk_calendar_select_day(GTK_CALENDAR(window->ExpirationCalendar),tm.tm_mday);
	gtk_calendar_select_month(GTK_CALENDAR(window->ExpirationCalendar),tm.tm_mon,1900+tm.tm_year);

	on_UID_update(NULL,window);
	int shell_size=0;
	config_str shells = yon_resource_open_file(shell_list_path,&shell_size);
	for (int i=1;i<shell_size;i++){
		if (shells[i][0]!='!'){
			if (shells[i][strlen(shells[i])-1]=='\n') shells[i][strlen(shells[i])-1]='\0';
			int parsed_size=0;
			config_str parsed = yon_char_parse(shells[i],&parsed_size,";");
			gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->userShellCombo),parsed[0],parsed[0]);
			yon_char_parsed_free(parsed,parsed_size);
		}
	}
	gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->userShellCombo),"Set",SET_LABEL);
	yon_char_parsed_free(shells,shell_size);
	gtk_combo_box_set_active(GTK_COMBO_BOX(window->userShellCombo),0);
	window->old_password=NULL;
	window->old_username=NULL;
	dictionary *entry_dict = NULL;

	struct target_struct *target = malloc(sizeof(struct target_struct));
	target->target=GTK_ENTRY(window->userGroupsEntry);
	target->defaut_toggle=GTK_TOGGLE_BUTTON(window->userGroupsCheck);
	yon_window_config_custom_window_setup(GTK_WINDOW(window->CreateUserWindow),"UserWindow");
	yon_dictionary_add_or_create_if_exists_with_data(entry_dict,"combo",window->userPasswordCombo);
	yon_dictionary_add_or_create_if_exists_with_data(entry_dict,"entry",window->userPasswordEntry);
	// char *password = yon_config_get_by_key(USERADD((char*)gtk_entry_get_text(GTK_ENTRY(window->userLoginEntry))));
	g_signal_connect(G_OBJECT(window->userLoginEntry),"focus-in-event",G_CALLBACK(on_login_focus_event),window);
	g_signal_connect(G_OBJECT(window->userLoginEntry),"focus-out-event",G_CALLBACK(on_login_focus_out_event),window);
	g_signal_connect(G_OBJECT(window->userLoginEntry),"enter-notify-event",G_CALLBACK(on_login_tooltip_event),window);
	g_signal_connect(G_OBJECT(window->userLoginEntry),"leave-notify-event",G_CALLBACK(on_login_tooltip_out_event),window);
	g_signal_connect(G_OBJECT(window->UserCancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
	g_signal_connect(G_OBJECT(window->userUIDAutoCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->userUIDEntry);
	g_signal_connect(G_OBJECT(window->userHomeButton),"clicked",G_CALLBACK(on_filechooser_open),window->userHomeEntry);
	g_signal_connect(G_OBJECT(window->UserGroupsButton),"clicked",G_CALLBACK(on_groups_clicked),target);
	g_signal_connect(G_OBJECT(window->userPasswordButton),"clicked",G_CALLBACK(on_password_change),entry_dict);
	g_signal_connect(G_OBJECT(window->userAdditionalGroupsButton),"clicked",G_CALLBACK(on_groups_multiple_clicked),window->userAdditionalGroupsEntry);
	g_signal_connect(G_OBJECT(window->userPasswordExpirationButton),"clicked",G_CALLBACK(on_expiration_clicked),window);
	g_signal_connect(G_OBJECT(window->userUIDAutoCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->userUIDEntry);
	g_signal_connect(G_OBJECT(window->userHomeCombo),"changed",G_CALLBACK(on_homedir_changed),window->userHomeButton);
	g_signal_connect(G_OBJECT(window->userHomeCombo),"changed",G_CALLBACK(on_homedir_changed),window->userHomeEntry);
	g_signal_connect(G_OBJECT(window->userPasswordCombo),"changed",G_CALLBACK(on_password_combo_changed),window->userPasswordButton);
	g_signal_connect(G_OBJECT(window->userGroupsCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->userGroupsEntry);
	g_signal_connect(G_OBJECT(window->userGroupsCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->UserGroupsButton);
	g_signal_connect(G_OBJECT(window->userShellCombo),"changed",G_CALLBACK(on_user_shell_changed),window);
	g_signal_connect(G_OBJECT(window->ExpirationCalendar),"day-selected",G_CALLBACK(on_date_selected),window);
	g_signal_connect(G_OBJECT(window->userUIDAutoCheck),"toggled",G_CALLBACK(on_UID_update),window);
	g_signal_connect(G_OBJECT(window->userCreateSystemCheck),"toggled",G_CALLBACK(on_UID_update),window);
	g_signal_connect(G_OBJECT(window->userGroupsCheck),"toggled",G_CALLBACK(on_user_login_update),window);
	g_signal_connect(G_OBJECT(window->userLoginEntry),"changed",G_CALLBACK(on_user_login_update),window);
	g_signal_connect(G_OBJECT(window->userUIDAutoCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->userCreateSystemCheck);
	g_signal_connect(G_OBJECT(window->userUIDAutoCheck),"toggled",G_CALLBACK(yon_gtk_toggle_button_set_inactive_from_toggle_button),window->userCreateSystemCheck);
	g_signal_connect(G_OBJECT(window->userUIDAutoCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->userCreateUnuniqueCheck);
	g_signal_connect(G_OBJECT(window->userUIDAutoCheck),"toggled",G_CALLBACK(yon_gtk_toggle_button_set_inactive_from_toggle_button_inversed),window->userCreateUnuniqueCheck);
	g_signal_connect(G_OBJECT(window->userSyncSAMBACheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->userSyncSAMBAPasswordCheck);
	g_signal_connect(G_OBJECT(window->userSyncSAMBAPasswordCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->userSyncSAMBACheck);
	g_signal_connect(G_OBJECT(window->userSyncSAMBACheck),"toggled",G_CALLBACK(on_toggle_button_set_active_from_toggle_button_inversed),window);
	g_signal_connect(G_OBJECT(window->userSyncSAMBAPasswordCheck),"toggled",G_CALLBACK(on_toggle_button_set_active_from_toggle_button_inversed),window);
	g_signal_connect(G_OBJECT(window->userPasswordCombo),"changed",G_CALLBACK(on_toggle_button_set_active_from_combo_box),window);
	g_signal_connect(G_OBJECT(window->userPasswordCombo),"changed",G_CALLBACK(on_toggle_button_set_active_from_combo_box),window);
	g_signal_connect(G_OBJECT(window->userPasswordEntry),"changed",G_CALLBACK(on_password_user_changed),window);
	g_signal_connect(G_OBJECT(window->userHomeEntry),"changed",G_CALLBACK(on_parameter_changed),window);
	g_signal_connect(G_OBJECT(window->userHomeEntry),"changed",G_CALLBACK(on_home_changed),window);
	g_signal_connect(G_OBJECT(window->userHomeCombo),"changed",G_CALLBACK(on_parameter_changed),window);
	g_signal_connect(G_OBJECT(window->userShellCombo),"changed",G_CALLBACK(on_parameter_changed),window);
	g_signal_connect(G_OBJECT(window->LanguageCombo),"changed",G_CALLBACK(on_parameter_changed),window);
	g_signal_connect(G_OBJECT(window->userShellEntry),"changed",G_CALLBACK(on_parameter_changed),window);
	g_signal_connect(G_OBJECT(window->userCreateSystemCheck),"toggled",G_CALLBACK(on_parameter_changed),window);
	g_signal_connect(G_OBJECT(window->userCreateUnuniqueCheck),"toggled",G_CALLBACK(on_parameter_changed),window);
	g_signal_connect(G_OBJECT(window->userDontCheckCheck),"toggled",G_CALLBACK(on_parameter_changed),window);
	g_signal_connect(G_OBJECT(window->userGroupsCheck),"toggled",G_CALLBACK(on_parameter_changed),window);
	g_signal_connect(G_OBJECT(window->userGroupsEntry),"changed",G_CALLBACK(on_parameter_changed),window);
	g_signal_connect(G_OBJECT(window->userPasswordExpirationEntry),"icon-press",G_CALLBACK(on_remove_expired),window->userPasswordExpirationEntry);
	g_signal_connect(G_OBJECT(window->userUIDEntry),"insert-text",G_CALLBACK(yon_on_text_insert_only_digits),NULL);
return window;
}

void on_UID_update(GtkWidget *, ubl_settings_usergroups_user_window *window){
	if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userUIDAutoCheck))){
		if (window->last_uid){
			gtk_entry_set_text(GTK_ENTRY(window->userUIDEntry),window->last_uid);
		} else {
			int config_size=0;
			int final_size=0;
			config_str config_users = yon_config_get_all_by_key(USERADD_SEARCH_macro,&config_size);
			yon_char_parsed_prepend_strings(config_users,config_size,":");
			config_str final = yon_char_parsed_merge(main_config.users,main_config.users_size,config_users,config_size,&final_size);
			long prev_busy=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userCreateSystemCheck))?0:main_config.MINGID;
			for (int i=0;i<final_size;i++){
				int parsed_size;
				config_str parsed = yon_char_parse(final[i],&parsed_size,":");

			int minimum = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userCreateUnuniqueCheck))?0:main_config.MINGID;
			int maximum = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userCreateUnuniqueCheck))?68000:main_config.MAXGID;
			for (int i=0;i<final_size;i++){
				int parsed_size;
				config_str parsed = yon_char_parse(final[i],&parsed_size,":");
				if (minimum<atol(parsed[2])&&atol(parsed[2])<maximum){
					if (atol(parsed[2])>prev_busy){
						prev_busy=atol(parsed[2]);
					}
				}
			}
			yon_char_parsed_free(parsed, parsed_size);
			}
			gtk_entry_set_text(GTK_ENTRY(window->userUIDEntry),yon_char_from_long(prev_busy+1));
		}
	} else {
		gtk_entry_set_text(GTK_ENTRY(window->userUIDEntry),"");
	}
}

void on_user_shell_changed(GtkWidget *, ubl_settings_usergroups_user_window *window){
	const char *current = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(window->userShellCombo));
	if (!yon_char_is_empty(current)&&!strcmp(current,SET_LABEL)){
		if(gtk_combo_box_get_active(GTK_COMBO_BOX(window->userShellCombo))>1){
			gtk_widget_set_sensitive(window->userShellEntry,1);
		} else {
			gtk_widget_set_sensitive(window->userShellEntry,0);
			}
	} else {
		gtk_widget_set_sensitive(window->userShellEntry,0);
	}
}

void on_user_login_update(GtkWidget *, ubl_settings_usergroups_user_window *window){
	if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userGroupsCheck))){
		gtk_entry_set_text(GTK_ENTRY(window->userGroupsEntry),gtk_entry_get_text(GTK_ENTRY(window->userLoginEntry)));
	} else {
		gtk_entry_set_text(GTK_ENTRY(window->userGroupsEntry),"");
	}
}

void on_user_save(GtkWidget *self, dictionary *dict){
	main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
	ubl_settings_usergroups_user_window *window = yon_dictionary_get_data(dict->first->next,ubl_settings_usergroups_user_window*);
	// yon_ubl_status_box_despawn(GTK_CONTAINER(window->StatusBox));
	char *final_user="";
	char *uid_string = NULL;
	char *password = NULL;
	char *login = NULL;
	char *username = NULL;
	char *main_group = NULL;
	char *additional_groups = NULL;
	char *password_changed_date = NULL;
	char *password_expiration_date = NULL;
	char *password_change_interval_minimum = NULL;
	char *password_change_interval_maximum = NULL;
	char *warning_days = NULL;
	char *no_activity_days = NULL;
	int samba_sync = 0;
	int samba_password_sync = 0;
	int deactivate = 0;
	char *extra_options = NULL;
	char *useradd_boot = "";
	char *useradd_shutdown = "";
	char *usershadow_boot = "";
	char *usershadow_shutdown = "";
	

	int uid_auto_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userUIDAutoCheck));
	if (uid_auto_active){
		uid_string="";
	} else {
		int config_size=0;
		int final_size=0;
		config_str config_users = yon_config_get_all_by_key(USERADD_SEARCH_macro,&config_size);
		yon_char_parsed_prepend_strings(config_users,config_size,":");
		config_str final = yon_char_parsed_merge(main_config.users,main_config.users_size,config_users,config_size,&final_size);

		uid_string = (char*)gtk_entry_get_text(GTK_ENTRY(window->userUIDEntry));
		if (!window->last_uid||strcmp(uid_string,window->last_uid)){
			if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userCreateUnuniqueCheck))){
				int found=0;
				for (int i=0;i<final_size;i++){
					int parsed_size;
					config_str parsed = yon_char_parse(final[i],&parsed_size,":");
					if (parsed_size>2&&!strcmp(parsed[2],uid_string)){
						if (yon_char_is_empty(window->last_uid)||(!yon_char_is_empty(window->last_uid)&&strcmp(parsed[2],window->last_uid))){
							found = yon_char_parsed_check_exist(config_users,config_size,final[i])>-1?1:2;
							break;
						}
					}

					if (parsed_size) yon_char_parsed_free(parsed,parsed_size);
				}
				if (found){
					yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),found==1?UID_ALREADY_EXIST_CONFIG_LABEL:UID_ALREADY_EXIST_SYSTEM_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
					yon_ubl_status_highlight_incorrect(window->userUIDEntry);
					gtk_notebook_set_current_page(GTK_NOTEBOOK(window->MainNotebook),0);
					return;
				}
			}

			if (yon_char_is_empty(uid_string)){
				yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
				yon_ubl_status_highlight_incorrect(window->userUIDEntry);
						gtk_notebook_set_current_page(GTK_NOTEBOOK(window->MainNotebook),0);
				return;
			} 
		}
	}

	int do_not_check_actve = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userDontCheckCheck));
	login = (char*)gtk_entry_get_text(GTK_ENTRY(window->userLoginEntry));
	if (yon_char_is_empty(login)){
			yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
			yon_ubl_status_highlight_incorrect(window->userLoginEntry);
			return;

	}
	if (!g_regex_match_simple("[a-zA-Z_][a-zA-Z0-9_-]*",login,0,0)&&!do_not_check_actve){
		yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),INVALID_LOGIN_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
		yon_ubl_status_highlight_incorrect(window->userLoginEntry);
		return;
	}
	{
		GtkTreeIter iter;
		GtkTreeModel *model = GTK_TREE_MODEL(widgets->UsersList);
		for_iter(model,&iter){
			char *cur;
			gtk_tree_model_get(model,&iter,2, &cur,-1);
			if (!yon_char_is_empty(cur)&&!strcmp(cur,login)){
				GtkTreeIter child,sort;
				gtk_tree_model_filter_convert_child_iter_to_iter(GTK_TREE_MODEL_FILTER(widgets->ConfigUsersFilter),&child,&iter);
				gtk_tree_model_sort_convert_child_iter_to_iter(GTK_TREE_MODEL_SORT(widgets->ConfigUsersSort),&sort,&child);
				if (!gtk_tree_selection_iter_is_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->UsersTree)),&sort)){
					
					yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),USER_EXIST_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
					yon_ubl_status_highlight_incorrect(window->userLoginEntry);
					gtk_notebook_set_current_page(GTK_NOTEBOOK(window->MainNotebook),0);
					return;
				}
			}
		}
	}

	int password_active = gtk_combo_box_get_active(GTK_COMBO_BOX(window->userPasswordCombo));
	if (password_active == 0){
		password = "";
	} else if (password_active ==5){
		password = " ";
	} else if (password_active ==1){
		password = (char*)gtk_entry_get_text(GTK_ENTRY(window->userPasswordEntry));
		if (yon_char_is_empty(password)){
				yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
				yon_ubl_status_highlight_incorrect(window->userPasswordEntry);
					gtk_notebook_set_current_page(GTK_NOTEBOOK(window->MainNotebook),0);
				return;
		}
	} else if (password_active==2){
			password = "!*";
	} else if (password_active ==3){
		password = (char*)gtk_entry_get_text(GTK_ENTRY(window->userPasswordEntry));
		if (password[0]!='!') {
			char *temp = yon_char_append("!",password);
			password=temp;
		}
	} else if (password_active == 4){
		password = yon_char_new((char*)gtk_entry_get_text(GTK_ENTRY(window->userPasswordEntry)));
		if (yon_char_is_empty(password)){
			yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
			yon_ubl_status_highlight_incorrect(window->userPasswordEntry);
				gtk_notebook_set_current_page(GTK_NOTEBOOK(window->MainNotebook),0);
			return;
		}
		if (password[0]=='!'){
			free(yon_char_divide(password,0));
		}
	}

	char *old_password = NULL;
	if (window->old_password){
		old_password = window->old_password;
		yon_check_password_blocked(old_password);
	}
	if (!old_password||(old_password&&strcmp(old_password,password))){
		time_t t = time(NULL);
		struct tm tm = *localtime(&t);
		char *month = yon_char_from_int(tm.tm_mon+1);
		if (strlen(month)==1){
			month=yon_char_append("0",month);
		}
		char *day = yon_char_from_int(tm.tm_mday);
		if (strlen(day)<2){
			day=yon_char_append("0",day);
		}
		password_changed_date = yon_char_unite(yon_char_from_int(1900+tm.tm_year),"-",month,"-",day,NULL);
	} else {
		password_changed_date = (char*)gtk_entry_get_text(GTK_ENTRY(window->userPasswordChangedEntry));
		}

	deactivate = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userDeactivatedCheck));
	if (deactivate){
		if (strlen(password)>1&&password[0]=='%'&&password[1]=='%'){
			char *temp = yon_char_replace(password,"%%","!!");
			password = yon_char_replace(temp,"!!","%%!");
		} else if (!yon_char_is_empty(password)&&password[0]=='!'){
		} else {
			password = yon_char_append("!",password);
		}
	}

	username = (char*)gtk_entry_get_text(GTK_ENTRY(window->userUserNameEntry));
	if (yon_char_is_empty(username)){
		username="";
	}

	int main_group_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userGroupsCheck));
	if (!main_group_active){
		main_group = (char*)gtk_entry_get_text(GTK_ENTRY(window->userGroupsEntry));
	} else {
		main_group = "";
	}

	additional_groups = (char*)gtk_entry_get_text(GTK_ENTRY(window->userAdditionalGroupsEntry));
	char *default_groups = yon_config_get_by_key(USERGROUPS_parameter);
	if (!yon_char_is_empty(default_groups)&&!strcmp(default_groups,additional_groups)){
		additional_groups="";
	}
	long password_change_interval_minimum_active = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->userPasswordChangeDelayMinimumSpin));
	if (password_change_interval_minimum_active>0){
		password_change_interval_minimum = yon_char_from_long(password_change_interval_minimum_active);
	} else password_change_interval_minimum = 0;

	long password_change_interval_maximum_active = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->userPasswordChangeDelayMaximumSpin));
	if (password_change_interval_minimum_active>0){
		password_change_interval_maximum = yon_char_from_long(password_change_interval_maximum_active);
	} else password_change_interval_maximum = 0;
	
	if (!yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(window->userPasswordExpirationEntry)))){
		guint year, month, day;
		gtk_calendar_get_date(GTK_CALENDAR(window->ExpirationCalendar),&year,&month,&day);
		char *dy = yon_char_from_int(day);
		char *mn = yon_char_from_int(month+1);
		char *yr = yon_char_from_int(year);
		if (month+1<10) mn = yon_char_append("0",mn);
		if (day<10) dy = yon_char_append("0",dy);
		password_expiration_date = yon_char_unite(yr,"-",mn,"-",dy,NULL);
		// password_expiration_date = (char*)gtk_entry_get_text(GTK_ENTRY(window->userPasswordExpirationEntry));
		if (yon_char_is_empty(password_expiration_date)){
			password_expiration_date="-1";
		}
	}
	
	long warning_days_active = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->userWarningSpin));
	if (warning_days_active>0)
		warning_days = yon_char_from_long(warning_days_active);

	int no_activity_days_active = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->userActivitySpin));
	if (no_activity_days_active>0){
		no_activity_days = yon_char_from_long(no_activity_days_active);
	}

	samba_sync = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBACheck));
	samba_password_sync = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userSyncSAMBAPasswordCheck));
	if (samba_sync){
		if (yon_samba_sync_get(login)==2)
			yon_samba_sync_remove(login);
		yon_samba_sync_add(login,NULL);
	} else if (samba_password_sync){
		if (yon_samba_sync_get(login)==1)
			yon_samba_sync_remove(login);
		yon_samba_sync_add(login,password);
	} else {
		yon_samba_sync_remove(login);
	}

	extra_options=(char*)gtk_entry_get_text(GTK_ENTRY(window->userExtraOptionsEntry));

	int force_at_next_login_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->userForceChangeCheck));
	if (force_at_next_login_active){
		password_changed_date = "0";
	}

	int useradd_boot_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UseraddBootCheck));
	if (useradd_boot_active){
		useradd_boot = "boot";
	}

	int useradd_shutdown_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UseraddShutdownCheck));
	if (useradd_shutdown_active){
		useradd_shutdown = "shutdown";
	}

	int usershadow_boot_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UsershadowBootCheck));
	if (usershadow_boot_active){
		usershadow_boot = "boot";
	}

	int usershadow_shutdown_active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UsershadowShutdownCheck));
	if (usershadow_shutdown_active){
		usershadow_shutdown = "shutdown";
	}


	char *useradd_sync_string = NULL;
	useradd_sync_string = yon_char_unite(useradd_boot,!yon_char_is_empty(useradd_boot)&&!yon_char_is_empty(useradd_shutdown)?",":"",useradd_shutdown,NULL);
	char *usershadow_sync_string = NULL;
	usershadow_sync_string = yon_char_unite(usershadow_boot,!yon_char_is_empty(usershadow_boot)?",":"",usershadow_shutdown,NULL);
	char *shadow_string = NULL;
	shadow_string = yon_char_unite(yon_char_return_if_exist(password_changed_date,""),
	":",yon_char_return_if_exist(password_change_interval_minimum,""),
	":",yon_char_return_if_exist(password_change_interval_maximum,""),
	":",yon_char_return_if_exist(warning_days,""),
	":",yon_char_return_if_exist(no_activity_days,""),
	":",yon_char_return_if_exist(password_expiration_date,""),
	NULL);

	final_user = yon_char_unite(yon_char_return_if_exist(username,""),
	":",yon_char_return_if_exist(uid_string,""),
	":",yon_char_return_if_exist(main_group,""),
	":",yon_char_return_if_exist(additional_groups,""),
	":",yon_char_return_if_exist(extra_options,""),
	":",yon_char_return_if_exist(password,""),
	NULL);
	
		char *final_command = yon_config_parameter_prepare_command(dull_parameter_get_command,NULL,"users",USERADD(login));
	yon_config_register(USERADD(login),final_command,final_user);
	if (!yon_char_is_empty(window->old_username)&&strcmp(window->old_username,login)){
		yon_config_remove_by_key(USERADD(window->old_username));
	}
	// if (window->old_password&&strcmp(window->old_password,password)){
		final_command = yon_config_parameter_prepare_command(dull_parameter_get_command,NULL,"users",USERSHADOW(login));
		yon_config_register(USERSHADOW(login),final_command,shadow_string);
		if (!yon_char_is_empty(window->old_username)&&strcmp(window->old_username,login)){
			yon_config_remove_by_key(USERSHADOW(window->old_username));
		}
	// }
	int home_delete = yon_char_parsed_check_exist(main_config.remove_homes,main_config.homes_size,login);
	if (home_delete>-1){
		main_config.remove_homes = yon_char_parsed_rip(main_config.remove_homes,&main_config.homes_size,home_delete);
	}

	if (!yon_char_is_empty(useradd_sync_string)){
		final_command = yon_config_parameter_prepare_command(dull_parameter_get_command,NULL,"users",USERADD_SYNC(login));
		yon_config_register(USERADD_SYNC(login),final_command,useradd_sync_string);
		if (!yon_char_is_empty(window->old_username)&&strcmp(window->old_username,login)){
			yon_config_remove_by_key(USERADD_SYNC(window->old_username));
		}
	} else {
		yon_config_remove_by_key(USERADD_SYNC(window->old_username));
	}

	if (!yon_char_is_empty(usershadow_sync_string)){
		final_command = yon_config_parameter_prepare_command(dull_parameter_get_command,NULL,"users",USERSHADOW_SYNC(login));
		yon_config_register(USERSHADOW_SYNC(login),final_command,usershadow_sync_string);
		if (!yon_char_is_empty(window->old_username)&&strcmp(window->old_username,login)){
			yon_config_remove_by_key(USERSHADOW_SYNC(window->old_username));
		}
	} else {
			yon_config_remove_by_key(USERSHADOW_SYNC(window->old_username));
		}
	yon_interface_update(widgets);
		
	on_subwindow_close(self);
}

int yon_system_user_sync(char *target_user){
	char *user = yon_char_parsed_check_exist_begins_with(main_config.users,main_config.users_size,target_user);
	char *shadow = yon_char_parsed_check_exist_begins_with(main_config.shadow,main_config.shadow_size,target_user);
	yon_char_remove_last_symbol(user,'\n');
	yon_char_remove_last_symbol(shadow,'\n');
	int user_size=0;
	int shadow_size=0;
	config_str parsed = yon_char_parse(user,&user_size,":");
	config_str parsed_shadow = yon_char_parse(shadow,&shadow_size,":");

	char *login = parsed[0];
	char *uid = parsed[2];
	char *gid = parsed[3];
	char *gecos = parsed[4];
	char *groups_string = yon_user_get_system_groups(login); 
	char *optionals = yon_char_unite(user_size>5? "--home-dir ":"", user_size>5? parsed[5]:"",user_size>5?" ":"",user_size>6? "--shell ":"",user_size>6?parsed[6]:"",NULL);

	char *config_user = yon_char_unite(user_size>4?gecos:"",":",
	user_size>2?uid:"",":",
	user_size>3?gid:"",":",
	!yon_char_is_empty(groups_string)?groups_string:"",":",
	!yon_char_is_empty(optionals)?optionals:"",":",
	user_size>1&&strcmp(parsed[1],"x")?yon_char_return_if_exist(parsed[1],""):yon_char_return_if_exist(parsed_shadow[1],""),NULL);

	char *config_shadow = yon_char_unite(yon_char_return_if_exist(parsed_shadow[2],""),":",
	user_size>3?parsed_shadow[3]:"",":",
	user_size>4?parsed_shadow[4]:"",":",
	user_size>5?parsed_shadow[5]:"",":",
	user_size>6?parsed_shadow[6]:"",":",
	user_size>7?parsed_shadow[7]:"",":",NULL);
	free(groups_string);
	free(optionals);

	if (!yon_char_is_empty(config_user)){
		char *parameter_name = USERADD(target_user);
		char *user_command = yon_config_parameter_prepare_command(dull_parameter_get_command,NULL,"users",parameter_name);
		yon_config_register(parameter_name,user_command,config_user);
		yon_config_remove_ignore(parameter_name);
		free(config_user);
		free(user_command);
		free(parameter_name);
	} else return 0;

	if (!yon_char_is_empty(config_shadow)){
		char *shadow_command = yon_config_parameter_prepare_command(dull_parameter_get_command,NULL,"users",USERSHADOW(target_user));
		yon_config_register(USERSHADOW(target_user),shadow_command,config_shadow);
		free(config_shadow);
	} else return 0;
	return 1;
}

int yon_system_group_sync(char *target_group){
	char *group = yon_char_parsed_check_exist_begins_with(main_config.groups,main_config.groups_size,target_group);
	char *group_shadow = yon_char_parsed_check_exist_begins_with(main_config.group_shadow,main_config.group_shadow_size,target_group);
	yon_char_remove_last_symbol(group,'\n');
	yon_char_remove_last_symbol(group_shadow,'\n');
	int group_size=0;
	int group_shadow_size=0;
	config_str parsed = yon_char_parse(group,&group_size,":");
	config_str parsed_shadow = yon_char_parse(group_shadow,&group_shadow_size,":");
	char *config_group = yon_char_unite(yon_char_return_if_exist(parsed[3],""),
	":",yon_char_return_if_exist(parsed[2],""),
	"::",yon_char_return_if_exist(parsed_shadow[2],""),
	":",strcmp(parsed[1],"x")?yon_char_return_if_exist(parsed[1],""):yon_char_return_if_exist(parsed_shadow[1],""),
	NULL);

	if (!yon_char_is_empty(config_group)){
		char *group_command = yon_config_parameter_prepare_command(dull_parameter_get_command,NULL,"users",GROUPADD(target_group));
		yon_config_register(GROUPADD(target_group),group_command,config_group);
	} else return 0;
	return 1;
}

void on_user_sync_with_config(GtkWidget *,main_window *widgets){
	int active = gtk_notebook_get_current_page(GTK_NOTEBOOK(widgets->MainNotebook));
	switch (active){
		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, *group;
				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,4,&group,-1);
				dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
				if (config(USERADD(target))){
					data->action_text = SYSTEM_USER_SYNC_CONFIRMATION_LABEL(target);
					data->data=NULL;
					data->function=NULL;
				} else {
					data->action_text = SYSTEM_USER_ADD_CONFIRMATION_LABEL(target);
					data->data=NULL;
					data->function=NULL;
				}
				if (yon_confirmation_dialog_call(widgets->Window,data)!=GTK_RESPONSE_ACCEPT){
					return;
				}

				if (yon_system_user_sync(target)) {
					yon_system_group_sync(group);
					yon_interface_update(widgets);
				}
			}
		}break;
		case 3:{
			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->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);
				dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
				if (config(GROUPADD(target))){
					data->action_text = SYSTEM_GROUP_ADD_CONFIRMATION_LABEL(target);
					data->data=NULL;
					data->function=NULL;
				} else {
					data->action_text = SYSTEM_GROUP_SYNC_CONFIRMATION_LABEL(target);
					data->data=NULL;
					data->function=NULL;
				}
				if (yon_confirmation_dialog_call(widgets->Window,data)!=GTK_RESPONSE_ACCEPT){
					return;
				}
				if (yon_system_group_sync(target)){
					yon_interface_update(widgets);
				}
			}
			
		}break;
	}
}