#include "ubl-settings-system.h"

void on_domain_address_save(GtkEntry *self){
    char *adress = (char*)gtk_entry_get_text(self);
    if (!yon_char_is_empty(adress)){
        yon_config_register(DOMAIN,DOMAIN_get_command,adress);
    }
}

domain_info_window *yon_information_window_new(){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_domain_view_path);

    domain_info_window *window = malloc(sizeof(domain_info_window));
    window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->HeadLabel = yon_gtk_builder_get_widget(builder,"HeadLabel");
    window->ExecuteTerminal = yon_gtk_builder_get_widget(builder,"ExecuteTerminal");
    window->TerminalScroll = yon_gtk_builder_get_widget(builder,"TerminalScroll");
    yon_gtk_widget_set_scroll_window_for_scroll(window->Window,GTK_SCROLLBAR(window->TerminalScroll));
    GdkRGBA rgba;
    vte_terminal_get_color_background_for_draw(VTE_TERMINAL(window->ExecuteTerminal),&rgba);
    vte_terminal_set_color_cursor(VTE_TERMINAL(window->ExecuteTerminal),&rgba);
    yon_gtk_window_setup(GTK_WINDOW(window->Window),NULL,TITLE_LABEL,"com.ublinux.ubl-settings-system","InfoWindow");
    yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),LOADING_LABEL,0,BACKGROUND_IMAGE_INFO_TYPE);
        g_signal_connect(G_OBJECT(window->ExecuteTerminal), "child-exited", G_CALLBACK(on_terminal_command_ended), window);

    return window;
}

void on_domain_connect(GtkWidget *, dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    connection_window *window = yon_dictionary_get_data(dict->first->next,connection_window*);
    char *adress = (char*)gtk_entry_get_text(GTK_ENTRY(window->AdressEntry));
    if (yon_char_is_empty(adress)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->AdressEntry);
        return;
    }
    char *login = (char*)gtk_entry_get_text(GTK_ENTRY(window->LoginEntry));
    char *password = (char*)gtk_entry_get_text(GTK_ENTRY(window->PasswordEntry));
    char *kerberos = (char*)gtk_entry_get_text(GTK_ENTRY(window->KerberosServerEntry));
    char *dns = (char*)gtk_entry_get_text(GTK_ENTRY(window->DNSEntry));
    char *ou = (char*)gtk_entry_get_text(GTK_ENTRY(window->OuEntry));
    char *options = (char*)gtk_entry_get_text(GTK_ENTRY(window->AdditionalParametersEntry));
    char *client = NULL;
    if (gtk_combo_box_get_active(GTK_COMBO_BOX(window->ClientCombo))){
        client = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->ClientCombo));
    }
    char *final = yon_char_unite(!yon_char_is_empty(kerberos)?kerberos_addition_command(kerberos):"",!yon_char_is_empty(kerberos)?" ":"",!yon_char_is_empty(dns)?dns_addition_command(dns):"",!yon_char_is_empty(dns)?" ":"",!yon_char_is_empty(client)?client_addition_command(client):"",!yon_char_is_empty(client)?" ":"",NULL);
    char *command = domain_connect_command(adress,login,password,final,ou,options);
        yon_debug_output("%s\n",command);
        domain_info_window *dialog = yon_information_window_new();
        gtk_label_set_text(GTK_LABEL(dialog->HeadLabel),DOMAIN_CONNECTING_LABEL);
        gtk_window_set_title(GTK_WINDOW(dialog->Window),DOMAIN_CONNECTING_LABEL);
        yon_terminal_integrated_start(dialog->ExecuteTerminal,command);
        dictionary *dc = NULL;
        yon_dictionary_add_or_create_if_exists_with_data(dc,"widgets",widgets);
        yon_dictionary_add_or_create_if_exists_with_data(dc,"window",window->Window);
        g_signal_connect(G_OBJECT(dialog->ExecuteTerminal),"child-exited",G_CALLBACK(on_terminal_done),dc);
        gtk_widget_show(dialog->Window);
        gtk_entry_set_text(GTK_ENTRY(widgets->DomainEntry),adress);
    
        on_config_reload(NULL,widgets);
}

void on_status_clicked(GtkWidget *, connection_window *window){
    char *target = (char*)gtk_entry_get_text(GTK_ENTRY(window->AdressEntry));
    if (!yon_char_is_empty(target)){
        yon_debug_output("%s\n",domain_info(target));
        domain_info_window *dialog = yon_information_window_new();
        yon_terminal_integrated_start(dialog->ExecuteTerminal,domain_info(target));
        gtk_window_set_transient_for(GTK_WINDOW(dialog->Window),GTK_WINDOW(window->Window));
        gtk_widget_show(dialog->Window);

    }
}

void on_find_domains_clicked(GtkWidget *, main_window *window){
        
        domain_info_window *dialog = yon_information_window_new();
        yon_terminal_integrated_start(dialog->ExecuteTerminal,domains_seek_command);
        gtk_window_set_transient_for(GTK_WINDOW(dialog->Window),GTK_WINDOW(window->Window));
        gtk_widget_show(dialog->Window);
        // yon_terminal_window_launch(GTK_WINDOW(window->Window),yon_debug_output("%s\n",domains_seek_command),yon_char_get_localised_from_lib(SUCCESS_LABEL),yon_char_get_localised_from_lib(FAIL_LABEL));
}


void on_main_window_domain_status_clicked(GtkWidget *, GtkEntryIconPosition icon_pos,GdkEvent* ,main_window *widgets){
    if (icon_pos==GTK_ENTRY_ICON_SECONDARY&&main_config.domain_connected){
        char *target = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->DomainEntry));
        if (!yon_char_is_empty(target)){
        yon_debug_output("%s\n",get_domain_info_command);
        domain_info_window *dialog = yon_information_window_new();
        gtk_label_set_text(GTK_LABEL(dialog->HeadLabel),INFO_TITLE_LABEL);
        yon_terminal_integrated_start(dialog->ExecuteTerminal,get_domain_info_command);
        gtk_window_set_transient_for(GTK_WINDOW(dialog->Window),GTK_WINDOW(widgets->Window));
        gtk_widget_show(dialog->Window);
        }
    }
}

int update_thread_buzy = 0;

gboolean on_main_window_domain_status_update(connection_window *window){
    const char *target = gtk_entry_get_text(GTK_ENTRY(window->AdressEntry));
    if (!yon_char_is_empty(target)){
        int status = system(domain_check_alive(target));
        if (!status){
            gtk_entry_set_icon_from_icon_name(GTK_ENTRY(window->AdressEntry),GTK_ENTRY_ICON_SECONDARY,domain_connected_icon);
        } else {
            gtk_entry_set_icon_from_icon_name(GTK_ENTRY(window->AdressEntry),GTK_ENTRY_ICON_SECONDARY,domain_disconnected_icon);
        }
    } else {
        gtk_entry_set_icon_from_icon_name(GTK_ENTRY(window->AdressEntry),GTK_ENTRY_ICON_SECONDARY,domain_disconnected_icon);
    }
    g_mutex_lock(&main_config.ThreadMutex);
    update_thread_buzy = 0;
    g_mutex_unlock(&main_config.ThreadMutex);
    return G_SOURCE_REMOVE;
}

// gboolean yon_update_thread(GtkWidget *self, GdkEvent *event, connection_window *window){
    gboolean yon_update_thread(GtkWidget *, connection_window *window){
    g_mutex_lock(&main_config.ThreadMutex);
    if (!update_thread_buzy){
        update_thread_buzy=1;
        g_mutex_unlock(&main_config.ThreadMutex);
        g_idle_add((GSourceFunc)on_main_window_domain_status_update,window);
    } else {
        g_mutex_unlock(&main_config.ThreadMutex);
    }
    return 1;
}

void yon_domain_client_changed(GtkWidget*,connection_window *window){
    gtk_entry_set_text(GTK_ENTRY(window->AdditionalParametersEntry),"");
    const char *id = gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->ClientCombo));
    if (!gtk_switch_get_active(GTK_SWITCH(window->AdditionalParametersDefaultSwitch))&&!yon_char_is_empty(id)&&strcmp(id,"none")){
        gtk_widget_set_sensitive(window->EditButton,1);
        gtk_widget_set_sensitive(window->AdditionalParametersEntry,1);
        gtk_widget_set_sensitive(window->AdditionalParametersDefaultSwitch,1);
    } else if(!yon_char_is_empty(id)&&strcmp(id,"none")){
        gtk_widget_set_sensitive(window->AdditionalParametersDefaultSwitch,1);
        gtk_widget_set_sensitive(window->EditButton,0);
        gtk_widget_set_sensitive(window->AdditionalParametersEntry,0);
    } else {
        gtk_widget_set_sensitive(window->EditButton,0);
        gtk_widget_set_sensitive(window->AdditionalParametersEntry,0);
        gtk_widget_set_sensitive(window->AdditionalParametersDefaultSwitch,0);
    }

}

void yon_domain_client_changed_switch(GtkWidget*,int ,connection_window *window){
    yon_domain_client_changed(NULL,window);
}

void on_domain_options_accept(GtkWidget *,domain_options_window *dialog){
    connection_window *window = g_object_get_data(G_OBJECT(dialog->Window),"connection_window");
    const char *options = gtk_entry_get_text(GTK_ENTRY(dialog->OptionsEntry));
    gtk_entry_set_text(GTK_ENTRY(window->AdditionalParametersEntry),options?options:"");
    on_subwindow_close(dialog->Window);
}

void on_domain_options_cell_toggled(GtkCellRenderer*, gchar *path,domain_options_window *window){
     char *options = (char*)gtk_entry_get_text(GTK_ENTRY(window->OptionsEntry)); 
    GtkTreeModel *model = GTK_TREE_MODEL(window->List);
    GtkTreeIter iter;
    if (gtk_tree_model_get_iter_from_string(model,&iter,path)){
        int status;
        char *target;
        gtk_tree_model_get(model,&iter,0,&status,1,&target,-1);
        gtk_list_store_set(window->List,&iter,0,!status,-1);
        if (!status){
            char *temp = yon_char_append_element(options,target,",");
            gtk_entry_set_text(GTK_ENTRY(window->OptionsEntry),temp);
            if (!yon_char_is_empty(temp)) free(temp);
        } else {
            char *temp = yon_char_remove_element(options,target,",");
            gtk_entry_set_text(GTK_ENTRY(window->OptionsEntry),temp);
            if (!yon_char_is_empty(temp)) free(temp);
        }
    }
}

domain_options_window *yon_domain_options_window_new(){
    domain_options_window *window = malloc(sizeof(domain_options_window));
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_domain_options_path);
    window->Window = yon_gtk_builder_get_widget(builder,"Window");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->TitleLabel = yon_gtk_builder_get_widget(builder,"TitleLabel");
    window->OptionsTree = yon_gtk_builder_get_widget(builder,"OptionsTree");
    window->OptionsEntry = yon_gtk_builder_get_widget(builder,"OptionsEntry");
    window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->List = GTK_LIST_STORE(gtk_builder_get_object(builder,"List"));
    window->ChooseCell = GTK_CELL_RENDERER(gtk_builder_get_object(builder,"ChooseCell"));

        g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_domain_options_accept),window);
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->ChooseCell),"toggled",G_CALLBACK(on_domain_options_cell_toggled),window);
    return window;
}

void yon_domain_additional_options_open(GtkWidget *, connection_window *window){
    char *command = NULL;
    const char *id = gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->ClientCombo));
    if (yon_char_is_empty(id)||!strcmp(id,"none")){
    } else if (!strcmp(id,"realmd_sssd")){
        command = sssd_client_parameters_command;
    } else if (!strcmp(id,"realmd_winbind")){
        command = windbind_client_parameters_command;
    } else if (!strcmp(id,"samba")){
        command = samba_client_parameters_command;
    } 

    if (yon_char_is_empty(command)){
        return;
    } else {
        GtkTreeIter iter;

        domain_options_window *dialog = yon_domain_options_window_new();
        yon_gtk_window_setup(GTK_WINDOW(dialog->Window),GTK_WINDOW(window->Window),ADDITIONAL_OPTIONS_LABEL,main_icon,"options_window");
        g_object_set_data(G_OBJECT(dialog->Window),"connection_window",window);
        int size;
        config_str options = yon_config_load(command,&size);
        for (int i=0;i<size;i++){
            yon_char_remove_last_symbol(options[i],'\n');
            gtk_list_store_append(dialog->List,&iter);
            gtk_list_store_set(dialog->List,&iter,1,options[i],-1);
        }
        yon_char_parsed_free(options,size);
    }
}

connection_window *yon_connection_window_new(){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_connection_path);
    connection_window *window = malloc(sizeof(connection_window));
    window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->AdressEntry = yon_gtk_builder_get_widget(builder,"AdressEntry");
    window->HeadLabel = yon_gtk_builder_get_widget(builder,"HeadLabel");
    window->StatusEntry = yon_gtk_builder_get_widget(builder,"StatusEntry");
    window->LoginEntry = yon_gtk_builder_get_widget(builder,"LoginEntry");
    window->PasswordEntry = yon_gtk_builder_get_widget(builder,"PasswordEntry");
    window->KerberosServerEntry = yon_gtk_builder_get_widget(builder,"KerberosServerEntry");
    window->DNSEntry = yon_gtk_builder_get_widget(builder,"DNSEntry");
    window->ClientCombo = yon_gtk_builder_get_widget(builder,"ClientCombo");
    window->SaveButton = yon_gtk_builder_get_widget(builder,"SaveButton");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->CheckButton = yon_gtk_builder_get_widget(builder,"CheckButton");
    window->OuEntry = yon_gtk_builder_get_widget(builder,"OuEntry");
    window->EditButton = yon_gtk_builder_get_widget(builder,"EditButton");
    window->AdditionalParametersEntry = yon_gtk_builder_get_widget(builder,"AdditionalParametersEntry");
    window->AdditionalParametersDefaultSwitch = yon_gtk_builder_get_widget(builder,"AdditionalParametersDefaultSwitch");
    yon_gtk_entry_block_symbols(GTK_ENTRY(window->OuEntry),"!\"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~.");
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->StatusEntry),"clicked",G_CALLBACK(on_status_clicked),window);
    g_signal_connect(G_OBJECT(window->CheckButton),"clicked",G_CALLBACK(yon_update_thread),window);
    g_signal_connect(G_OBJECT(window->ClientCombo),"changed",G_CALLBACK(yon_domain_client_changed),window);
    g_signal_connect(G_OBJECT(window->AdditionalParametersDefaultSwitch),"state-set",G_CALLBACK(yon_domain_client_changed_switch),window);
    g_signal_connect(G_OBJECT(window->EditButton),"clicked",G_CALLBACK(yon_domain_additional_options_open),window);

    yon_gtk_entry_set_password_visibility_icon(GTK_ENTRY(window->PasswordEntry));
    return window;
}

void on_domain_disconnect(GtkWidget *, dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    domain_window *window = yon_dictionary_get_data(dict->first->next,domain_window*);
    char *adress = (char*)gtk_entry_get_text(GTK_ENTRY(window->AdressEntry));
    char *login = (char*)gtk_entry_get_text(GTK_ENTRY(window->LoginEntry));
    char *password = (char*)gtk_entry_get_text(GTK_ENTRY(window->PasswordEntry));
    if (yon_char_is_empty(adress)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->AdressEntry);
        return;
    }
        yon_debug_output("%s\n",domain_disconnect_command(adress,login,password));
        domain_info_window *dialog = yon_information_window_new();
        gtk_label_set_text(GTK_LABEL(dialog->HeadLabel),DISCONNECTION_LABEL);
        gtk_window_set_title(GTK_WINDOW(dialog->Window),DISCONNECTION_LABEL);
        yon_terminal_integrated_start(dialog->ExecuteTerminal,domain_disconnect_command(adress,login,password));
        dictionary *dc = NULL;
        yon_dictionary_add_or_create_if_exists_with_data(dc,"widgets",widgets);
        yon_dictionary_add_or_create_if_exists_with_data(dc,"window",window->Window);
        g_signal_connect(G_OBJECT(dialog->ExecuteTerminal),"child-exited",G_CALLBACK(on_terminal_done),dc);
    gtk_entry_set_text(GTK_ENTRY(widgets->DomainEntry),adress);
        gtk_widget_show(dialog->Window);
}

domain_window *yon_domain_window_new(){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_domain_path);

    domain_window *window = malloc(sizeof(domain_window));
    window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->HeadLabel = yon_gtk_builder_get_widget(builder,"HeadLabel");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->SaveButton = yon_gtk_builder_get_widget(builder,"SaveButton");
    window->AdressEntry = yon_gtk_builder_get_widget(builder,"AdressEntry");
    window->LoginEntry = yon_gtk_builder_get_widget(builder,"LoginEntry");
    window->PasswordEntry = yon_gtk_builder_get_widget(builder,"PasswordEntry");
    window->DNSEntry = yon_gtk_builder_get_widget(builder,"DNSEntry");
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    yon_gtk_entry_set_password_visibility_icon(GTK_ENTRY(window->PasswordEntry));

    return window;
}

void on_domain_opened(GtkWidget *, main_window *widgets){
    if (main_config.domain_connected){
        domain_window *window = yon_domain_window_new();
        gtk_label_set_text(GTK_LABEL(window->HeadLabel),DISCONNECTION_LABEL);
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),DISCONNECTION_LABEL,"com.ublinux.ubl-settings-system","DomainWindow");
        char *domain = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->DomainEntry));
        char *password = yon_config_get_by_key(DOMAIN_ADMANGER);
        char *login = yon_char_divide_search(password,":",-1);
        char *dns = config(DOMAIN_DNS);
        if (!yon_char_is_empty(login))
        gtk_entry_set_text(GTK_ENTRY(window->LoginEntry),login);
        if (!yon_char_is_empty(password))
        gtk_entry_set_text(GTK_ENTRY(window->PasswordEntry),password);
        if (!yon_char_is_empty(domain))
        gtk_entry_set_text(GTK_ENTRY(window->AdressEntry),domain);
        if (!yon_char_is_empty(dns))
        gtk_entry_set_text(GTK_ENTRY(window->DNSEntry),dns);
        dictionary *dict = NULL;
        yon_dictionary_add_or_create_if_exists_with_data(dict,"widgets",widgets);
        yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
        g_signal_connect(G_OBJECT(window->SaveButton),"clicked",G_CALLBACK(on_domain_disconnect),dict);
        g_signal_connect(G_OBJECT(window->Window),"destroy",G_CALLBACK(on_check_domain_connected),widgets);

        gtk_widget_show(window->Window);
    } else {
        connection_window *window = yon_connection_window_new();
        gtk_label_set_text(GTK_LABEL(window->HeadLabel),DOMAIN_CONNECTING_LABEL);
        yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),DOMAIN_CONNECTING_LABEL,"com.ublinux.ubl-settings-system","ConnectWindow");
        char *domain = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->DomainEntry));
        char *dns = config(DOMAIN_DNS);
        char *client = yon_config_get_by_key(DOMAIN_CLIENT);
        char *kerberos = yon_config_get_by_key(DOMAIN_SERVER);
        char *password = yon_config_get_by_key(DOMAIN_ADMANGER);
        char *login = yon_char_divide_search(password,":",-1);
    if (!yon_char_is_empty(login))
        gtk_entry_set_text(GTK_ENTRY(window->LoginEntry),login);
    if (!yon_char_is_empty(password))
        gtk_entry_set_text(GTK_ENTRY(window->PasswordEntry),password);
    if (!yon_char_is_empty(domain))
        gtk_entry_set_text(GTK_ENTRY(window->AdressEntry),domain);
    if (!yon_char_is_empty(kerberos))
        gtk_entry_set_text(GTK_ENTRY(window->KerberosServerEntry),kerberos);
    if (!yon_char_is_empty(dns))
        gtk_entry_set_text(GTK_ENTRY(window->DNSEntry),dns);
    if (!yon_char_is_empty(client)){
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(window->ClientCombo),client);
    }
        dictionary *dict = NULL;
        yon_dictionary_add_or_create_if_exists_with_data(dict,"widgets",widgets);
        yon_dictionary_add_or_create_if_exists_with_data(dict,"window",window);
        g_signal_connect(G_OBJECT(window->SaveButton),"clicked",G_CALLBACK(on_domain_connect),dict);
        g_signal_connect(G_OBJECT(window->Window),"destroy",G_CALLBACK(on_check_domain_connected),widgets);
        // yon_update_thread(NULL,NULL,window);
        gtk_widget_show(window->Window);
    }

}

// standard functions

void on_check_domain_connected(GtkWidget *, main_window *widgets){
    int ansv = system(domain_connect_check);
    if (!ansv){
            gtk_entry_set_icon_from_icon_name(GTK_ENTRY(widgets->DomainEntry),GTK_ENTRY_ICON_SECONDARY,domain_connected_icon);
            gtk_button_set_label(GTK_BUTTON(widgets->DomainButton),DOMAIN_DISCONNECT_LABEL);
            gtk_widget_set_tooltip_markup(widgets->DomainButton,DDISCONNECTION_LABEL);
            gtk_entry_set_icon_tooltip_markup(GTK_ENTRY(widgets->DomainEntry),GTK_ENTRY_ICON_SECONDARY,DOMAIN_CONNECTED_LABEL);
        main_config.domain_connected=1;
    } else {
            gtk_entry_set_icon_from_icon_name(GTK_ENTRY(widgets->DomainEntry),GTK_ENTRY_ICON_SECONDARY,domain_disconnected_icon);
            gtk_button_set_label(GTK_BUTTON(widgets->DomainButton),DOMAIN_CONNECT_LABEL);
            gtk_widget_set_tooltip_markup(widgets->DomainButton,CONNECTION_LABEL);
            gtk_entry_set_icon_tooltip_markup(GTK_ENTRY(widgets->DomainEntry),GTK_ENTRY_ICON_SECONDARY,DOMAIN_DISCONNECTED_LABEL);
        main_config.domain_connected=0;
    }
    YON_CONFIG_TYPE type;
    switch(main_config.load_mode){
        case 0: type=YON_CONFIG_GLOBAL;
            yon_config_load_config(type,yon_config_parameter_prepare_command(DOMAIN_get_command,"global",NULL,NULL), yon_config_parameter_prepare_command(HOSTNAME_get_command,"global",NULL,NULL),NULL);
            break;
        case 1: type=YON_CONFIG_LOCAL;
            yon_config_load_config(type,yon_config_parameter_prepare_command(DOMAIN_get_command,"system",NULL,NULL), yon_config_parameter_prepare_command(HOSTNAME_get_command,"system",NULL,NULL),NULL);
            break;
        case 3: type=YON_CONFIG_CUSTOM;
            if (!yon_char_is_empty(main_config.custom_load_path))
                yon_config_load_config(type,yon_config_parameter_prepare_command(DOMAIN_get_command,main_config.custom_load_path,NULL,NULL), yon_config_parameter_prepare_command(HOSTNAME_get_command,main_config.custom_load_path,NULL,NULL),NULL);
            break;
    }
    char *hostname = yon_config_get_by_key(hostname_parameter);
    char *domain = yon_config_get_by_key(DOMAIN);
    if (!yon_char_is_empty(hostname))
        gtk_entry_set_text(GTK_ENTRY(widgets->HostnameEntry),hostname);
    if (!yon_char_is_empty(domain))
        gtk_entry_set_text(GTK_ENTRY(widgets->DomainEntry),domain);
    
}