#include "ubl-settings-update.h"

void on_mirror_add(GtkWidget *,main_window *widgets){
    gtk_tree_selection_unselect_all(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MirrorTree)));
    mirror_add_window *window = yon_mirror_add_window_new();
    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),ADD_REPO_LABEL,icon_path,"mirror_window");

    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(on_mirror_accept),dict);
    
}

void on_mirror_remove(GtkWidget *self,main_window *widgets){
    dialog_confirmation_data *data = yon_confirmation_dialog_data_new();
    data->action_text=REMOVE_REPO_WARNING_LABEL;
    data->function=(void(*)(void*,void*))on_mirror_remove_accept;
    data->data = widgets;
    yon_confirmation_dialog_call(self,data);
    
}

void on_mirror_remove_accept(GtkWidget *, main_window *widgets){
    GtkTreeModel *model=NULL;
    GtkTreeIter iter;
    if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MirrorTree)),&model,&iter)) {
        return;
    }
    char *repo;
    gtk_tree_model_get(model,&iter,0,&repo,-1);
    yon_config_remove_by_key(REPOPUBLIC_CACHE_full(repo));
    yon_interface_update(widgets);
}

void on_mirror_edit(GtkWidget *,main_window *widgets){
    mirror_add_window *window = yon_mirror_add_window_new();
    gtk_label_set_text(GTK_LABEL(window->HeadLabel),EDIT_WEB_LABEL);
    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),ADD_WEB_LABEL,icon_path,"mirror_window");

    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(on_mirror_accept),dict);

    char *target;
    GtkTreeIter iter;
    GtkTreeModel *model;
    gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MirrorTree)),&model,&iter);
    gtk_tree_model_get(GTK_TREE_MODEL(widgets->MirrorList),&iter,0,&target,-1);
    gtk_entry_set_text(GTK_ENTRY(window->NameEntry),target);
    window->name = target;
    char *parameter = config(REPOPUBLIC_CACHE_full(target));
    int size;
    config_str parsed = yon_char_parse(parameter,&size,",");
    for (int i=0;i<size;i++){
        char *target = parsed[i];
        GtkBuilder *builder = gtk_builder_new_from_resource(glade_mirror_path_add_path);

        path_section *section = malloc(sizeof(path_section));
        section->PathRemovalBox = yon_gtk_builder_get_widget(builder,"PathRemovalBox");
        section->PathEditButton = yon_gtk_builder_get_widget(builder,"PathEditButton");
        section->PathEntry = yon_gtk_builder_get_widget(builder,"PathEntry");
        section->PathRemoveButton = yon_gtk_builder_get_widget(builder,"PathRemoveButton");
        section->TypeCombo = yon_gtk_builder_get_widget(builder,"TypeCombo");
        section->edits=0;

        char *type = yon_char_divide_search(parsed[i],"@",-1);
        gtk_combo_box_set_active_id(GTK_COMBO_BOX(section->TypeCombo),type);
        yon_dictionary_add_or_create_if_exists_with_data(window->sections,(char*)target,section);
        
        gtk_entry_set_text(GTK_ENTRY(section->PathEntry),parsed[i]);
        gtk_box_pack_start(GTK_BOX(window->PathBox),section->PathRemovalBox,0,0,0);
        
        g_signal_connect(gtk_builder_get_object(builder,"PathEditButton"),"clicked",G_CALLBACK(on_mirror_path_edit),window);
        g_signal_connect(gtk_builder_get_object(builder,"PathRemoveButton"),"clicked",G_CALLBACK(on_mirror_path_removed),window);
        
    }
    gtk_widget_show(window->Window);
}


void on_mirror_configure(GtkWidget *,main_window *widgets){
    mirror_configure_window *window = yon_mirror_configure_window_new();
    yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),NULL,icon_path,"mirror_window");

    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(on_mirror_configure_accept),dict);

    char *port_str = config(REPOPUBLIC_CACHE_full("port"));
    if (!yon_char_is_empty(port_str)){
        double port = atof(port_str);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->PortDefaultCheck),0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->PortSpin),port);
    }
    char *cache = config(REPOPUBLIC_CACHE_full("cache_dir"));
    if (!yon_char_is_empty(cache)){
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->CacheDefaultCheck),0);
        gtk_entry_set_text(GTK_ENTRY(window->CacheEntry),cache);
    }
    char *inactivity = config(REPOPUBLIC_CACHE_full("purge_files_after"));
    if (!yon_char_is_empty(inactivity)){
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->InactivityDurationDefaultCheck),0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->InactivityDurationSpin),atof(inactivity));
    }
    char *timeout = config(REPOPUBLIC_CACHE_full("download_timeout"));
    if (!yon_char_is_empty(timeout)){
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->CacheTimeoutDefaultCheck),0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->CacheTimeoutSpin),atof(timeout));    
    }
    char *proxy = config(REPOPUBLIC_CACHE_full("http_proxy"));
    if (!yon_char_is_empty(proxy)){
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->ProxyCheck),1);
        gtk_entry_set_text(GTK_ENTRY(window->ProxyEntry),proxy);
    }
    char *agent = config(REPOPUBLIC_CACHE_full("user_agent"));
    if (!yon_char_is_empty(agent)){
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->AgentDefaultCheck),0);
        gtk_entry_set_text(GTK_ENTRY(window->AgentEntry),agent);
    }
    char *cron = config(REPOPUBLIC_CACHE_full("prefetch.cron"));
    if (!yon_char_is_empty(cron)){
        gtk_entry_set_text(GTK_ENTRY(window->CronEntry),cron);    
    }
    char *delay = config(REPOPUBLIC_CACHE_full("prefetch.ttl_unaccessed_in_days"));
    if (!yon_char_is_empty(delay)){
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->UpdatesDelayDefaultCheck),0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->UpdatesDelaySpin),atof(delay));    
    }
    char *request_delay = config(REPOPUBLIC_CACHE_full("prefetch.ttl_unupdated_in_days"));
    if (!yon_char_is_empty(request_delay)){
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(window->UpdatesRequestDelayDefaultCheck),0);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(window->UpdatesRequestDelaySpin),atof(request_delay));    
    }
    
        gtk_widget_show(window->Window);
    
}

void on_mirror_accept(GtkWidget *,dictionary *dict){
    main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    mirror_add_window *window = yon_dictionary_get_data(dict->first->next,mirror_add_window*);
    if (yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(window->NameEntry)))){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->NameEntry);
        return;
    }
    if (!window->sections&&yon_char_is_empty(gtk_entry_get_text(GTK_ENTRY(window->TypeEntry)))) {
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->TypeEntry);
        return;
    }
    char *name = (char*)gtk_entry_get_text(GTK_ENTRY(window->NameEntry));
    char *paths = "";
    if (window->sections){
        dictionary *dact = NULL;
        for_dictionaries (dact, window->sections){
            char *cur = yon_char_unite(gtk_combo_box_get_active_id(GTK_COMBO_BOX(((path_section*)dact->data)->TypeCombo)),"@",(char*)gtk_entry_get_text(GTK_ENTRY(((path_section*)dact->data)->PathEntry)),NULL);
            char *temp = yon_char_unite(paths,!yon_char_is_empty(paths)?",":"",cur,NULL);
            if (!yon_char_is_empty(paths)) free(paths);
            paths = temp;
        }
    }
    char *source_main = (char*)gtk_entry_get_text(GTK_ENTRY(window->TypeEntry));
    if (!yon_char_is_empty(source_main)) {
        char *type = (char*)gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->TypeCombo));
        source_main = yon_char_unite(type?type:"",type?"@":"",source_main,NULL);
        char *temp = yon_char_unite(paths,!yon_char_is_empty(paths)?",":"",source_main,NULL);
        if (!yon_char_is_empty(paths)) free(paths);
        paths = temp;
    }
    if (!yon_char_is_empty(window->name)&&strcmp(name,window->name)){
        yon_config_remove_by_key(REPOPUBLIC_CACHE_full(window->name));
    }
    yon_config_register(REPOPUBLIC_CACHE_full(name),REPOPUBLIC_CACHE_full_command(name),paths);
    yon_interface_update(widgets);
    gtk_widget_destroy(window->Window);
}

void on_mirror_configure_accept(GtkWidget *, dictionary *dict){
    // main_window *widgets = yon_dictionary_get_data(dict->first,main_window*);
    mirror_configure_window *window = yon_dictionary_get_data(dict->first->next,mirror_configure_window*);
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->PortDefaultCheck))){
        yon_config_remove_by_key(REPOPUBLIC_CACHE_full("port"));
    } else {
        long port = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->PortSpin));
        yon_config_register(REPOPUBLIC_CACHE_full("port"),REPOPUBLIC_CACHE_full_command("port"),yon_char_from_long(port));
    }
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->CacheDefaultCheck))){
        yon_config_remove_by_key(REPOPUBLIC_CACHE_full("cache_dir"));
    } else {
        char *cache_path = (char*)gtk_entry_get_text(GTK_ENTRY(window->CacheEntry));
        if (!yon_char_is_empty(cache_path)){
            yon_config_register(REPOPUBLIC_CACHE_full("cache_dir"),REPOPUBLIC_CACHE_full_command("cache_dir"),cache_path);
        } else {
            yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
            yon_ubl_status_highlight_incorrect(window->CacheEntry);
            return;
        }
    }
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->InactivityDurationDefaultCheck))){
        yon_config_remove_by_key(REPOPUBLIC_CACHE_full("purge_files_after"));
    } else {
        long duration_inactivity = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->InactivityDurationSpin));
        char *duration_str = yon_char_from_long(duration_inactivity);
        yon_config_register(REPOPUBLIC_CACHE_full("purge_files_after"),REPOPUBLIC_CACHE_full_command("purge_files_after"),duration_str);
    }
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->CacheTimeoutDefaultCheck))){
        yon_config_remove_by_key(REPOPUBLIC_CACHE_full("download_timeout"));
    } else{
        long duration_inactivity = gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->CacheTimeoutSpin));
        char *duration_str = yon_char_from_long(duration_inactivity);
        yon_config_register(REPOPUBLIC_CACHE_full("download_timeout"),REPOPUBLIC_CACHE_full_command("download_timeout"),duration_str);
    }
    if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->ProxyCheck))){
        yon_config_remove_by_key(REPOPUBLIC_CACHE_full("http_proxy"));
    } else {
        char *proxy = (char*)gtk_entry_get_text(GTK_ENTRY(window->ProxyEntry));
        yon_config_register(REPOPUBLIC_CACHE_full("http_proxy"),REPOPUBLIC_CACHE_full_command("http_proxy"),proxy);
    }
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->AgentDefaultCheck))){
        yon_config_remove_by_key(REPOPUBLIC_CACHE_full("user_agent"));
    } else {
        char *agent = (char*)gtk_entry_get_text(GTK_ENTRY(window->AgentEntry));
        yon_config_register(REPOPUBLIC_CACHE_full("user_agent"),REPOPUBLIC_CACHE_full_command("user_agent"),agent);
    }
    char *expression = (char*)gtk_entry_get_text(GTK_ENTRY(window->CronEntry));
    if (!yon_char_is_empty(expression)){
        yon_config_register(REPOPUBLIC_CACHE_full("prefetch.cron"),REPOPUBLIC_CACHE_full_command("prefetch.cron"),expression);
    } else {
        yon_config_remove_by_key(REPOPUBLIC_CACHE_full("prefetch.cron"));
    }
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UpdatesDelayDefaultCheck))){
        yon_config_remove_by_key(REPOPUBLIC_CACHE_full("prefetch.ttl_unaccessed_in_days"));
    } else {
        char *agent = yon_char_from_long(gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->UpdatesDelaySpin)));
        yon_config_register(REPOPUBLIC_CACHE_full("prefetch.ttl_unaccessed_in_days"),REPOPUBLIC_CACHE_full_command("prefetch.ttl_unaccessed_in_days"),agent);
    }
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->UpdatesRequestDelayDefaultCheck))){
        yon_config_remove_by_key(REPOPUBLIC_CACHE_full("prefetch.ttl_unupdated_in_days"));
    } else {
        char *agent = yon_char_from_long(gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->UpdatesRequestDelaySpin)));
        yon_config_register(REPOPUBLIC_CACHE_full("prefetch.ttl_unupdated_in_days"),REPOPUBLIC_CACHE_full_command("prefetch.ttl_unupdated_in_days"),agent);
    }

    gtk_widget_destroy(window->Window);
}


void on_mirror_path_removed(GtkWidget *self, mirror_add_window *window){
    int found=0;
    dictionary *dict = NULL;
    for_dictionaries(dict, window->sections){
        if (self == ((path_section*)dict->data)->PathRemoveButton){
            found =1;
            break;
        }
    }
    if (!found) return;
    if (GTK_IS_WIDGET(((path_section*)dict->data)->PathRemovalBox)){
        gtk_widget_destroy(((path_section*)dict->data)->PathRemovalBox);
        window->sections = yon_dictionary_rip(dict);
    }
}

void on_mirror_path_edit(GtkWidget *self,mirror_add_window *window){
    int active = 0;
    dictionary *cur = NULL;
    dictionary *dict = NULL;
    for_dictionaries(dict, window->sections){
        if (self == ((path_section*)dict->data)->PathEditButton){
            
            cur = dict;
            break;
        }
        if (((path_section*)dict->data)->edits) {
            active = 1;
        }
    }
    if (!cur||active) return;
    if (gtk_editable_get_editable(GTK_EDITABLE(((path_section*)dict->data)->PathEntry))){
        ((path_section*)dict->data)->edits=1;
        gtk_editable_set_editable(GTK_EDITABLE(((path_section*)dict->data)->PathEntry),0);
        gtk_widget_set_sensitive(((path_section*)dict->data)->TypeCombo,0);
        GtkWidget *image = gtk_button_get_image(GTK_BUTTON(((path_section*)dict->data)->PathEditButton));
        gtk_image_set_from_icon_name(GTK_IMAGE(image),edit_icon_name,GTK_ICON_SIZE_BUTTON);
    } else {
        ((path_section*)dict->data)->edits=0;
        gtk_widget_set_sensitive(((path_section*)dict->data)->TypeCombo,1);
        gtk_editable_set_editable(GTK_EDITABLE(((path_section*)dict->data)->PathEntry),1);
        GtkWidget *image = gtk_button_get_image(GTK_BUTTON(((path_section*)dict->data)->PathEditButton));
        gtk_image_set_from_icon_name(GTK_IMAGE(image),accept_icon_name,GTK_ICON_SIZE_BUTTON);
    }
}

void on_mirror_path_add(GtkWidget *, mirror_add_window *window){
    const char *target = gtk_entry_get_text(GTK_ENTRY(window->TypeEntry));
    if (yon_char_is_empty(target)){
        yon_ubl_status_box_spawn(GTK_CONTAINER(window->StatusBox),EMPTY_IMPORTANT_LABEL,5,BACKGROUND_IMAGE_FAIL_TYPE);
        yon_ubl_status_highlight_incorrect(window->TypeEntry);
        return;
    }
   GtkBuilder *builder = gtk_builder_new_from_resource(glade_mirror_path_add_path);

    path_section *section = malloc(sizeof(path_section));
    section->PathRemovalBox = yon_gtk_builder_get_widget(builder,"PathRemovalBox");
    section->PathEditButton = yon_gtk_builder_get_widget(builder,"PathEditButton");
    section->PathEntry = yon_gtk_builder_get_widget(builder,"PathEntry");
    section->PathRemoveButton = yon_gtk_builder_get_widget(builder,"PathRemoveButton");
    section->TypeCombo = yon_gtk_builder_get_widget(builder,"TypeCombo");
    section->edits=0;

    gtk_combo_box_set_active_id(GTK_COMBO_BOX(section->TypeCombo),gtk_combo_box_get_active_id(GTK_COMBO_BOX(window->TypeCombo)));
    yon_dictionary_add_or_create_if_exists_with_data(window->sections,(char*)target,section);
    
    gtk_entry_set_text(GTK_ENTRY(section->PathEntry),target);
    gtk_entry_set_text(GTK_ENTRY(window->TypeEntry),"");
    gtk_box_pack_start(GTK_BOX(window->PathBox),section->PathRemovalBox,0,0,0);
    
    g_signal_connect(gtk_builder_get_object(builder,"PathEditButton"),"clicked",G_CALLBACK(on_mirror_path_edit),window);
    g_signal_connect(gtk_builder_get_object(builder,"PathRemoveButton"),"clicked",G_CALLBACK(on_mirror_path_removed),window);

}

mirror_configure_window *yon_mirror_configure_window_new(){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_mirror_configure_path);
    mirror_configure_window *window = malloc(sizeof(mirror_configure_window));
    window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->HeadLabel = yon_gtk_builder_get_widget(builder,"HeadLabel");
    window->PortDefaultCheck = yon_gtk_builder_get_widget(builder,"PortDefaultCheck");
    window->PortSpin = yon_gtk_builder_get_widget(builder,"PortSpin");
    window->CacheDefaultCheck = yon_gtk_builder_get_widget(builder,"CacheDefaultCheck");
    window->CacheEntry = yon_gtk_builder_get_widget(builder,"CacheEntry");
    window->CacheButton = yon_gtk_builder_get_widget(builder,"CacheButton");
    window->InactivityDurationDefaultCheck = yon_gtk_builder_get_widget(builder,"InactivityDurationDefaultCheck");
    window->InactivityDurationSpin = yon_gtk_builder_get_widget(builder,"InactivityDurationSpin");
    window->CacheTimeoutDefaultCheck = yon_gtk_builder_get_widget(builder,"CacheTimeoutDefaultCheck");
    window->CacheTimeoutSpin = yon_gtk_builder_get_widget(builder,"CacheTimeoutSpin");
    window->ProxyCheck = yon_gtk_builder_get_widget(builder,"ProxyCheck");
    window->ProxyEntry = yon_gtk_builder_get_widget(builder,"ProxyEntry");
    window->AgentDefaultCheck = yon_gtk_builder_get_widget(builder,"AgentDefaultCheck");
    window->AgentEntry = yon_gtk_builder_get_widget(builder,"AgentEntry");
    window->CronEntry = yon_gtk_builder_get_widget(builder,"CronEntry");
    window->UpdatesDelayDefaultCheck = yon_gtk_builder_get_widget(builder,"UpdatesDelayDefaultCheck");
    window->UpdatesDelaySpin = yon_gtk_builder_get_widget(builder,"UpdatesDelaySpin");
    window->UpdatesRequestDelayDefaultCheck = yon_gtk_builder_get_widget(builder,"UpdatesRequestDelayDefaultCheck");
    window->UpdatesRequestDelaySpin = yon_gtk_builder_get_widget(builder,"UpdatesRequestDelaySpin");


    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->PortDefaultCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->PortSpin);
    g_signal_connect(G_OBJECT(window->CacheDefaultCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->CacheEntry);
    g_signal_connect(G_OBJECT(window->CacheDefaultCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->CacheButton);
    g_signal_connect(G_OBJECT(window->CacheButton),"clicked",G_CALLBACK(on_file_chooser_open),window->CacheEntry);
    g_signal_connect(G_OBJECT(window->InactivityDurationDefaultCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->InactivityDurationSpin);
    g_signal_connect(G_OBJECT(window->CacheTimeoutDefaultCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->CacheTimeoutSpin);
    g_signal_connect(G_OBJECT(window->ProxyCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button),window->ProxyEntry);
    g_signal_connect(G_OBJECT(window->AgentDefaultCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->AgentEntry);
    g_signal_connect(G_OBJECT(window->UpdatesDelayDefaultCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->UpdatesDelaySpin);
    g_signal_connect(G_OBJECT(window->UpdatesRequestDelayDefaultCheck),"toggled",G_CALLBACK(yon_gtk_widget_set_sensitive_from_toggle_button_inversed),window->UpdatesRequestDelaySpin);
    gtk_widget_show(window->Window);
    
    return window;
}

void on_mirror_type_changed(GtkComboBox *self, mirror_add_window *window){
    int active = gtk_combo_box_get_active(self);
    if (active == 0){
        gtk_widget_set_sensitive(window->TypeButton,0);
        gtk_entry_set_placeholder_text(GTK_ENTRY(window->TypeEntry),"http://repo.ublinux.ru/2405");
    } else if (active == 1){
        gtk_widget_set_sensitive(window->TypeButton,0);
        gtk_entry_set_placeholder_text(GTK_ENTRY(window->TypeEntry),"http://proxy.localnet.ru:8080");
    } else if (active == 2){
        gtk_widget_set_sensitive(window->TypeButton,1);
        gtk_entry_set_placeholder_text(GTK_ENTRY(window->TypeEntry),"/etc/pacman.d/reflector_mirrorlis");
    }
}

mirror_add_window *yon_mirror_add_window_new(){
    GtkBuilder *builder = gtk_builder_new_from_resource(glade_mirror_add_path);
    mirror_add_window *window = malloc(sizeof(mirror_add_window));
    window->Window = yon_gtk_builder_get_widget(builder,"MainWindow");
    window->StatusBox = yon_gtk_builder_get_widget(builder,"StatusBox");
    window->NameEntry = yon_gtk_builder_get_widget(builder,"NameEntry");
    window->TypeCombo = yon_gtk_builder_get_widget(builder,"TypeCombo");
    window->TypeEntry = yon_gtk_builder_get_widget(builder,"TypeEntry");
    window->TypeButton = yon_gtk_builder_get_widget(builder,"TypeButton");
    window->AddButton = yon_gtk_builder_get_widget(builder,"AddButton");
    window->AcceptButton = yon_gtk_builder_get_widget(builder,"AcceptButton");
    window->CancelButton = yon_gtk_builder_get_widget(builder,"CancelButton");
    window->HeadLabel = yon_gtk_builder_get_widget(builder,"HeadLabel");
    window->PathBox = yon_gtk_builder_get_widget(builder,"PathBox");
    yon_gtk_entry_block_symbols(GTK_ENTRY(window->NameEntry),"$");
    yon_on_entry_set_allowed_symbols(GTK_ENTRY(window->TypeEntry),"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/1234567890!$.#-_()~$&;{}[]|<>^:?+\\,'*`%");

    yon_on_entry_set_allowed_symbols(GTK_ENTRY(window->NameEntry),"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_$");

    window->sections = NULL;
    window->name=NULL;

    g_signal_connect(G_OBJECT(window->TypeButton),"clicked",G_CALLBACK(on_file_chooser_open),window->TypeEntry);
    g_signal_connect(G_OBJECT(window->AddButton),"clicked",G_CALLBACK(on_mirror_path_add),window);
    g_signal_connect(G_OBJECT(window->CancelButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
    g_signal_connect(G_OBJECT(window->TypeCombo),"changed",G_CALLBACK(on_mirror_type_changed),window);
    gtk_widget_show(window->Window);
    
    return window;
}