#include "ubinstall-gtk.h"

void on_autohostname_check(GtkWidget *, main_window *widgets){
    gtk_switch_set_active(GTK_SWITCH(widgets->HostnameSensitiveSwitch),1);
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->AutoHostnameCheck))){
        gtk_widget_set_sensitive(widgets->HotnameEntry,0);
        gtk_entry_set_placeholder_text(GTK_ENTRY(widgets->HotnameEntry),"auto");
    }else{
        gtk_widget_set_sensitive(widgets->HotnameEntry,1);
        gtk_entry_set_placeholder_text(GTK_ENTRY(widgets->HotnameEntry),"ublinux-install");
    }
}

void on_autohostname_sensitiveness_check(GtkWidget *, int status, main_window *widgets){
    if (status){
        gtk_widget_set_sensitive(widgets->AutoHostnameCheck,1);
        if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->AutoHostnameCheck))){
            gtk_widget_set_sensitive(widgets->HotnameEntry,0);
        } else 
            gtk_widget_set_sensitive(widgets->HotnameEntry,1);
    } else {
            gtk_widget_set_sensitive(widgets->HotnameEntry,0);
            gtk_widget_set_sensitive(widgets->AutoHostnameCheck,0);
    }
}

void on_hostname_entry_changed (GtkWidget *, main_window *widgets){
    gtk_switch_set_active(GTK_SWITCH(widgets->HostnameSensitiveSwitch),1);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->AutoHostnameCheck),0);
} 

void on_type_changed(GtkComboBox *self,network_info *info){
    gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(info->ConnectionCombo));
    gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(info->ConnectionCombo),DEFAULT_LABEL);
    const char *id = gtk_combo_box_get_active_id(self);
    GHashTable *table = g_hash_table_lookup(main_config.network_types,id);
    if (table){
        GList *list = g_hash_table_get_keys(table);
        GList *iter;
        for (iter=list;iter;iter=iter->next){
            gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(info->ConnectionCombo),iter->data,iter->data);
        }
        g_list_free(list);
    }
}

network_info *yon_network_info_new(){
    network_info *info = new(network_info);
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_path_network_info);
    info->MainBox = yon_gtk_builder_get_widget(builder,"MainBox");
    info->TypeCombo = yon_gtk_builder_get_widget(builder,"TypeCombo");
    info->ConnectionCombo = yon_gtk_builder_get_widget(builder,"ConnectionCombo");
    info->ConnectionEntry = yon_gtk_builder_get_widget(builder,"ConnectionEntry");
    info->EnabledSwitch = yon_gtk_builder_get_widget(builder,"EnabledSwitch");
    info->RemoveButton = yon_gtk_builder_get_widget(builder,"RemoveButton");
    info->AutoGetIPSwitch = yon_gtk_builder_get_widget(builder,"AutoGetIPSwitch");
    info->IpRevealer = yon_gtk_builder_get_widget(builder,"IpRevealer");
    info->IpAdressEntry = yon_gtk_builder_get_widget(builder,"IpAdressEntry");
    info->GatewayEntry = yon_gtk_builder_get_widget(builder,"GatewayEntry");
    info->MaskEntry = yon_gtk_builder_get_widget(builder,"MaskEntry");
    info->DNSEntry = yon_gtk_builder_get_widget(builder,"DNSEntry");

    g_object_set_data(G_OBJECT(info->RemoveButton),"network_info",info);
    g_object_set_data(G_OBJECT(info->MainBox),"network_info",info);

    yon_on_entry_set_allowed_symbols(GTK_ENTRY(info->IpAdressEntry),"01234567890.");
    yon_on_entry_set_allowed_symbols(GTK_ENTRY(info->MaskEntry),"01234567890.");
    yon_on_entry_set_allowed_symbols(GTK_ENTRY(info->GatewayEntry),"01234567890.");
    yon_on_entry_set_allowed_symbols(GTK_ENTRY(info->DNSEntry),"01234567890.");

    yon_entry_set_pattern(GTK_ENTRY(info->IpAdressEntry),"^(\\d{0,3})(\\.(\\d{0,3}))?(\\.(\\d{0,3}))?(\\.(\\d{0,3}))?$");
    yon_entry_set_pattern(GTK_ENTRY(info->MaskEntry),"^(\\d{0,3})(\\.(\\d{0,3}))?(\\.(\\d{0,3}))?(\\.(\\d{0,3}))?$");
    yon_entry_set_pattern(GTK_ENTRY(info->GatewayEntry),"^(\\d{0,3})(\\.(\\d{0,3}))?(\\.(\\d{0,3}))?(\\.(\\d{0,3}))?$");
    yon_entry_set_pattern(GTK_ENTRY(info->DNSEntry),"^(\\d{0,3})(\\.(\\d{0,3}))?(\\.(\\d{0,3}))?(\\.(\\d{0,3}))?$");
    
    g_signal_connect(G_OBJECT(info->AutoGetIPSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),info->IpAdressEntry);
    g_signal_connect(G_OBJECT(info->AutoGetIPSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),info->GatewayEntry);
    g_signal_connect(G_OBJECT(info->AutoGetIPSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),info->MaskEntry);
    g_signal_connect(G_OBJECT(info->AutoGetIPSwitch),"state-set",G_CALLBACK(yon_gtk_widget_set_sensitive_from_switch_inversed),info->DNSEntry);
    g_signal_connect(G_OBJECT(info->TypeCombo),"changed",G_CALLBACK(on_type_changed),info);
    
    yon_gtk_revealer_set_from_switch_inversed(GTK_REVEALER(info->IpRevealer),GTK_SWITCH(info->AutoGetIPSwitch));

    GList *list = g_hash_table_get_keys(main_config.network_types);
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(info->TypeCombo),iter->data,iter->data);
    }
    g_list_free(list);

    gtk_widget_show(info->MainBox);
    return info;
}

void on_ntp_sync(GtkWidget *, main_window *widgets){
    int mode = gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->NetworkNTPCombo));
            gtk_widget_set_sensitive(widgets->NetworkNTPEntry,0);
    switch(mode){
        case 0:
                gtk_entry_set_text(GTK_ENTRY(widgets->NetworkNTPEntry),"");
                gtk_entry_set_placeholder_text(GTK_ENTRY(widgets->NetworkNTPEntry),"");
            break;
        case 2:{
                int size;
                config_str parsed = yon_config_load(get_ntp_default_command,&size);
                if (size&&!yon_char_is_empty(parsed[0])&&strcmp(parsed[0],"(null)\n")){
                    free(yon_char_divide_search(parsed[0],"=",-1));
                    yon_char_remove_last_symbol(parsed[0],'\n');
                    yon_char_remove_brackets(parsed[0]);
                    gtk_entry_set_placeholder_text(GTK_ENTRY(widgets->NetworkNTPEntry),parsed[0]);
                    yon_char_parsed_free(parsed,size);
                    gtk_entry_set_text(GTK_ENTRY(widgets->NetworkNTPEntry),"");
                }
            }
            break;
        case 3:{
                int size;
                config_str parsed = yon_config_load(get_ntp_ru_command,&size);
                if (size&&!yon_char_is_empty(parsed[0])&&strcmp(parsed[0],"(null)\n")){
                    free(yon_char_divide_search(parsed[0],"=",-1));
                    yon_char_remove_last_symbol(parsed[0],'\n');
                    yon_char_remove_brackets(parsed[0]);
                    gtk_entry_set_placeholder_text(GTK_ENTRY(widgets->NetworkNTPEntry),parsed[0]);
                    yon_char_parsed_free(parsed,size);
                    gtk_entry_set_text(GTK_ENTRY(widgets->NetworkNTPEntry),"");
                }
            }
            break;
        case 1:
                gtk_entry_set_placeholder_text(GTK_ENTRY(widgets->NetworkNTPEntry),"DHCP");
                gtk_entry_set_text(GTK_ENTRY(widgets->NetworkNTPEntry),"");
            break;
        case 5:
                gtk_entry_set_placeholder_text(GTK_ENTRY(widgets->NetworkNTPEntry),"");
                gtk_entry_set_text(GTK_ENTRY(widgets->NetworkNTPEntry),"");
            break;
        case 4:
            gtk_widget_set_sensitive(widgets->NetworkNTPEntry,1);
                gtk_entry_set_placeholder_text(GTK_ENTRY(widgets->NetworkNTPEntry),"");
                gtk_entry_set_text(GTK_ENTRY(widgets->NetworkNTPEntry),"");
            break;
    }
}

void on_connection_add(GtkWidget *,main_window *widgets){
    network_info *info = yon_network_info_new();
    gtk_box_pack_start(GTK_BOX(widgets->NetworkConnectionsBox),info->MainBox,0,0,0);
    gtk_widget_show(info->MainBox);
}

int yon_network_save(main_window *widgets){
    if (gtk_switch_get_active(GTK_SWITCH(widgets->NetworkDomainSwitch))){
        const char *domain_name = gtk_entry_get_text(GTK_ENTRY(widgets->NetworkDomainNameEntry));
        if (yon_char_is_empty(domain_name)){
            yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(widgets->NetworkDomainNameEntry);
            return 0;
        }
        const char *domain_admin = gtk_entry_get_text(GTK_ENTRY(widgets->NetworkDomainAdminEntry));
        const char *domain_password = gtk_entry_get_text(GTK_ENTRY(widgets->NetworkDomainPasswordEntry));
        if (!yon_char_is_empty(domain_name)) yon_config_register(DOMAIN_parameter,DOMAIN_parameter_command,(char*)domain_name);
        else yon_config_remove_by_key(DOMAIN_parameter);
        if ((!yon_char_is_empty(domain_admin)&&yon_char_is_empty(domain_password))||(yon_char_is_empty(domain_admin)&&!yon_char_is_empty(domain_password))){
            yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(widgets->NetworkDomainAdminEntry);
            yon_ubl_status_highlight_incorrect(widgets->NetworkDomainPasswordEntry);
            return 0;
        }
        if (!yon_char_is_empty(domain_admin)&&!yon_char_is_empty(domain_password)){
            int size;
            config_str encrypted_password = yon_config_load(encrypt_domain_password_command(domain_password),&size);
            if (!size){
                yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),ENCRYPT_ERROR_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                yon_ubl_status_highlight_incorrect(widgets->NetworkDomainPasswordEntry);
                return 0;
            }
            yon_char_remove_last_symbol(encrypted_password[0],'\n');
            char *admin_string_full = yon_char_unite(domain_admin,":",encrypted_password[0],NULL);
            yon_config_register(DOMAIN_admanger_parameter,DOMAIN_parameter_command,admin_string_full);
        } else {
            yon_config_remove_by_key(DOMAIN_admanger_parameter);
        }
    }
    if (gtk_switch_get_active(GTK_SWITCH(widgets->NetworkNTPServerSwitch))){
        
        int mode = gtk_combo_box_get_active(GTK_COMBO_BOX(widgets->NetworkNTPCombo));
        switch(mode){
            case 0:yon_config_remove_by_key(NTPSERVERS_parameter);
                break;
            case 1:yon_config_register(NTPSERVERS_parameter,NTPSERVERS_parameter_command,"dhcp");
                break;
            case 2:yon_config_register(NTPSERVERS_parameter,NTPSERVERS_parameter_command,"default");
                break;
            case 3:yon_config_register(NTPSERVERS_parameter,NTPSERVERS_parameter_command,"ntp-ru");
                break;
            case 5:yon_config_register(NTPSERVERS_parameter,NTPSERVERS_parameter_command,"disable");
                break;
            case 6:yon_config_register(NTPSERVERS_parameter,NTPSERVERS_parameter_command,"stop");
                break;
            case 4:
                char *value = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->NetworkNTPEntry));
                if (yon_char_is_empty(value)) {
                    yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                    yon_ubl_status_highlight_incorrect(widgets->NetworkNTPEntry);
                    return 0;
                }
                yon_config_register(NTPSERVERS_parameter,NTPSERVERS_parameter_command,value);
                break;
        }
    } else {
        yon_config_remove_by_key(NTPSERVERS_parameter);
    }
    if (gtk_switch_get_active(GTK_SWITCH(widgets->HostnameSensitiveSwitch))){
        int autohostname = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->AutoHostnameCheck));
        char *hostname = NULL;
        if (autohostname) {
            hostname = "auto";
        } else {
            hostname = (char*)gtk_entry_get_text(GTK_ENTRY(widgets->HotnameEntry));
        }
        if (yon_char_is_empty(hostname)){
            yon_ubl_status_box_render(EMPTY_IMPORTANT_LABEL,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(widgets->HotnameEntry);
            return 0;
        }
        yon_config_register(hostname_parameter,hostname_parameter_command,hostname);
    } else {
        yon_config_remove_by_key(hostname_parameter);
    }

    GList *list = gtk_container_get_children(GTK_CONTAINER(widgets->NetworkConnectionsBox));
    GList *iter;
    for (iter=list;iter;iter=iter->next){
        network_info *info = g_object_get_data(G_OBJECT(iter->data),"network_info");
        // char *type = gtk_combo_box_get_active_id(GTK_COMBO_BOX(info->TypeCombo));
        char *connection = (char*)gtk_entry_get_text(GTK_ENTRY(info->ConnectionEntry));
        int active = gtk_switch_get_active(GTK_SWITCH(info->EnabledSwitch));
        int parameters_size=0;
        config_str parameters = NULL;
        if (!gtk_switch_get_active(GTK_SWITCH(info->AutoGetIPSwitch))){
            char *ip = (char*)gtk_entry_get_text(GTK_ENTRY(info->IpAdressEntry));
            char *mask = (char*)gtk_entry_get_text(GTK_ENTRY(info->MaskEntry));
            char *gateway = (char*)gtk_entry_get_text(GTK_ENTRY(info->GatewayEntry));
            char *dns = (char*)gtk_entry_get_text(GTK_ENTRY(info->DNSEntry));

            yon_char_parsed_add_or_create_if_exists(parameters,&parameters_size,"ipv4.method manual");
            if (!yon_ip_check(ip)){
                yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),WRONG_IP_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                yon_ubl_status_highlight_incorrect(info->IpAdressEntry);
                return 0;
            }
            if (!yon_ip_check(mask)){
                yon_ubl_status_box_spawn(GTK_CONTAINER(widgets->StatusBox),WRONG_IP_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
                yon_ubl_status_highlight_incorrect(info->MaskEntry);
                return 0;
            } else {
                int bitmask = yon_ip_mask_get_bits(mask);
                mask = yon_char_from_int(bitmask);
                char *ip_param = yon_char_unite("ipv4.addr", ip,"/",mask,NULL);
                yon_char_parsed_add_or_create_if_exists(parameters,&parameters_size,ip_param);
                free(ip_param);

            }
            if (yon_ip_check(gateway)){
                char *ip_param = yon_char_append("ipv4.gateway", gateway);
                yon_char_parsed_add_or_create_if_exists(parameters,&parameters_size,ip_param);
                free(ip_param);
            }
            if (yon_ip_check(dns)){
                char *ip_param = yon_char_append("ipv4.dns", dns);
                yon_char_parsed_add_or_create_if_exists(parameters,&parameters_size,ip_param);
                free(ip_param);
            }
            if (!active){
                yon_char_parsed_add_or_create_if_exists(parameters,&parameters_size,"");
            }

            char *value = yon_char_parsed_to_string(parameters,parameters_size,"yes");
            yon_config_register(NETWORK_devdown_parameter(connection),NETWORK_devdown_parameter_command(connection),value);
        } else {
            yon_config_register(NETWORK_parameter(connection),NETWORK_parameter_command(connection),network_value_auto);

        }
    }
    if (!main_config.configure_mode){
        int size, users_size, final_size, save_size;
        config_str users = yon_config_find_keys(NETWORK_parameter_search,&users_size);
        config_str save_parameters = yon_char_parsed_new(&size,DOMAIN_parameter,DOMAIN_admanger_parameter,NTPSERVERS_parameter,hostname_parameter,NULL);
        config_str final = yon_char_parsed_merge(save_parameters,size,users,users_size,&final_size);
        config_str parameters = yon_config_get_save_parameters_by_list(&save_size,final,final_size);
        if (parameters){
            char *command_parameters = yon_char_parsed_to_string(parameters,save_size," -- ");
            char *command = yon_char_unite(ubconfig_dull_command,"--target system ",command_parameters,NULL);
            if (!system(command)){}
            free(command);
            yon_char_parsed_free(parameters,save_size);
        }
        if (users)yon_char_parsed_free(users,users_size);
        if (save_parameters)yon_char_parsed_free(save_parameters,size);
    }
    return 1;
}

void yon_network_init(main_window *widgets){
    int size;
    config_str network_types = yon_resource_open_file(network_path,&size);
    for (int i=1;i<size;i++){
        yon_char_remove_last_symbol(network_types[i],'\n');
        int parsed_size;
        config_str parsed = yon_char_parse(network_types[i],&parsed_size,";");
        if (!g_hash_table_contains(main_config.network_types,parsed[0])){
            GHashTable *table = g_hash_table_new(g_str_hash,g_str_equal);
            g_hash_table_insert(main_config.network_types,yon_char_new(parsed[0]),table);

        }
        g_hash_table_add(g_hash_table_lookup(main_config.network_types,parsed[0]),yon_char_new(parsed[1]));
        yon_char_parsed_free(parsed,parsed_size);
    }
    yon_char_parsed_free(network_types,size);

    char *domain = config(DOMAIN_parameter);
    char *domain_admanger = config(DOMAIN_admanger_parameter);
    char *ntp = config(NTPSERVERS_parameter);
    char *hostname = config(hostname_parameter);

    if (!yon_char_is_empty(domain)){
        gtk_switch_set_active(GTK_SWITCH(widgets->NetworkDomainSwitch),1);
        gtk_entry_set_text(GTK_ENTRY(widgets->NetworkDomainNameEntry),domain);
        if (!yon_char_is_empty(domain_admanger)){
            char *domain_password = yon_char_new(domain_admanger);
            char *domain_admin = yon_char_divide_search(domain_password,":",-1);
            gtk_entry_set_text(GTK_ENTRY(widgets->NetworkDomainAdminEntry),domain_admin);
            gtk_entry_set_text(GTK_ENTRY(widgets->NetworkDomainPasswordEntry),domain_password);
        }
    }
    if (!yon_char_is_empty(ntp)){
        gtk_switch_set_active(GTK_SWITCH(widgets->NetworkNTPServerSwitch),1);
            gtk_widget_set_sensitive(widgets->NetworkNTPEntry,0);
        if (!strcmp(ntp,"dhcp")){
            gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->NetworkNTPCombo),1);
        } else if (!strcmp(ntp,"default")){
            gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->NetworkNTPCombo),2);
        } else if (!strcmp(ntp,"ntp-ru")){
            gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->NetworkNTPCombo),3);
        } else if (!strcmp(ntp,"no")||!strcmp(ntp,"disable")){
            gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->NetworkNTPCombo),5);
        } else if (!strcmp(ntp,"stop")){
            gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->NetworkNTPCombo),6);
        } else {
            gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->NetworkNTPCombo),4);
            gtk_widget_set_sensitive(widgets->NetworkNTPEntry,1);
            gtk_entry_set_text(GTK_ENTRY(widgets->NetworkNTPEntry),ntp);
        }
    } else {
        gtk_widget_set_sensitive(widgets->NetworkNTPEntry,0);
        gtk_entry_set_text(GTK_ENTRY(widgets->NetworkNTPEntry),"");
        gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->NetworkNTPCombo),0);
    }
    gtk_entry_set_text(GTK_ENTRY(widgets->HotnameEntry),"");
    if (!yon_char_is_empty(hostname)){
        gtk_switch_set_active(GTK_SWITCH(widgets->HostnameSensitiveSwitch),1);
        if (!strcmp(hostname,"auto")){
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->AutoHostnameCheck),1);
        } else {
            gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widgets->AutoHostnameCheck),0);
            gtk_entry_set_text(GTK_ENTRY(widgets->HotnameEntry),hostname);
        }
    }
    config_str parameters = yon_config_get_all_by_key(NETWORK_parameter_search,&size);
    for (int i=0;i<size;i++){
        char *parameter = yon_char_new(parameters[i]);
        char *parameter_name = yon_char_divide_search(parameter,"=",-1);
        char *key = yon_config_parameter_get_key(parameter_name);
        char *key_sub = key+yon_char_find_last(key,'[');
        char *subkey = yon_config_parameter_get_key(key_sub);
        char *key_type = yon_char_divide_search(subkey,"@",-1);

        if (!strcmp(subkey,"connmod")){
            network_info *info = yon_network_info_new();
            gtk_box_pack_start(GTK_BOX(widgets->NetworkConnectionsBox),info->MainBox,0,0,0);
            gtk_widget_show(info->MainBox);

            int connection_size;
            config_str connection = yon_char_parse(parameter,&connection_size," ");
            if (yon_char_parsed_check_exist(connection,connection_size,"ipv4.method")>-1){
                    gtk_switch_set_active(GTK_SWITCH(info->AutoGetIPSwitch),1);
            } else {
                int addr_active = yon_char_parsed_check_exist(connection,connection_size,"ipv4.addr");
                if (addr_active>-1){
                        char *mask = yon_char_new(connection[addr_active+1]);
                        char *ip = yon_char_divide_search(mask,"/",-1);
                        gtk_entry_set_text(GTK_ENTRY(info->MaskEntry),mask);
                        gtk_entry_set_text(GTK_ENTRY(info->IpAdressEntry),ip);
                        free(mask);
                        free(ip);
                }
                addr_active = yon_char_parsed_check_exist(connection,connection_size,"ipv4.gateway");
                if (addr_active>-1){
                        gtk_entry_set_text(GTK_ENTRY(info->GatewayEntry),connection[addr_active+1]);

                }
                addr_active = yon_char_parsed_check_exist(connection,connection_size,"ipv4.dns");
                if (addr_active>-1){
                        gtk_entry_set_text(GTK_ENTRY(info->DNSEntry),connection[addr_active+1]);

                }
            }

            GList *list = g_hash_table_get_keys(main_config.network_types);
            GList *iter;
            for (iter=list;iter;iter=iter->next){
                if (g_hash_table_contains(g_hash_table_lookup(main_config.network_types,(char*)iter->data),key_type)){
                    gtk_combo_box_set_active_id(GTK_COMBO_BOX(info->TypeCombo),(char*)iter->data);
                    gtk_combo_box_set_active_id(GTK_COMBO_BOX(info->ConnectionCombo),key_type);
                    break;
                }
            }
        }
    }
}
