#include "ubl-settings-manager.h"


void yon_open_browser(GtkWidget *, char *link){
    GtkWidget *window = yon_ubl_browser_window_open(link,template_app_information.app_title);
    if (window)
    gtk_window_set_icon_name(GTK_WINDOW(window),yon_char_append("com.ublinux.",template_app_information.app_tech_name));
}

void on_open_documentation_confirmation(GtkWidget *self, char *link){
    if (main_config.always_open_documentation==0){
        GtkBuilder *builder = gtk_builder_new_from_resource(ui_glade_path_documentation);
        template_documentation_confirmation_window *window = malloc(sizeof(template_documentation_confirmation_window));
        window->Window = yon_gtk_builder_get_widget(builder,"helpConfirmationWindow");
        window->AcceptButton = yon_gtk_builder_get_widget(builder,"ReadHelpButton");
        window->CloseButton = yon_gtk_builder_get_widget(builder,"CancelHelpButton");
        window->HeaderLabel = yon_gtk_builder_get_widget(builder,"webHeaderNameLabel");
        window->AlwaysOpenCheck = yon_gtk_builder_get_widget(builder,"AlwaysOpenDocumentationCheckbox");
        gtk_label_set_text(GTK_LABEL(window->HeaderLabel),TITLE_LABEL);
        gtk_window_set_title(GTK_WINDOW(window->Window),TITLE_LABEL);
        gtk_window_set_icon_name(GTK_WINDOW(window->Window),yon_char_append("com.ublinux.",template_app_information.app_tech_name));
        gtk_widget_show_all(window->Window);
        g_signal_connect(G_OBJECT(window->CloseButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
        g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(yon_open_browser),yon_char_new(link));
        g_signal_connect(G_OBJECT(window->AcceptButton),"clicked",G_CALLBACK(on_subwindow_close),NULL);
        
    } else {
        yon_open_browser(self,link);
    }
}

/**on_link(GtkWidget *self, char* uri, gpointer user_data)
 * [EN]
 * Signal for hadnling AboutDialog links.
 * Connect to "activate-link" signal.
 * [self] is AboutDialog window;
 * [uri] is activated link;
 * [user_data] is pointer for user data, hasn't used in standard handler;
 * [RU]
 * Функция для обработки сигнала нажатия на ссылку окна AboutDialog.
 * Присоединять к сигналу "activate-link".
 * [self] - окно AboutDialog;
 * [uri] - ссылка, по которой совершается переход;
 * [user_data] - указатель на любые другие данные, не используется в стандартном обработчике;
*/
void on_link(GtkWidget *self, char* uri, gpointer){
    gtk_widget_destroy(self);
    on_open_documentation_confirmation(self,uri);
}

void on_about(GtkWidget *, char *version_application){
    GtkBuilder *builder=gtk_builder_new_from_resource(ui_glade_path_about);
    GtkWidget *window=yon_gtk_builder_get_widget(builder,"AboutWindow");
    GtkWidget *title=yon_gtk_builder_get_widget(builder,"headerAboutTopic");
    GtkWidget *hideButtonBox=yon_gtk_builder_get_widget(builder,"buttonBoxHide");
    gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(window),version_application);
    gtk_about_dialog_set_logo_icon_name(GTK_ABOUT_DIALOG(window),yon_char_append("com.ublinux.",LocaleName));
    gtk_window_set_icon_name(GTK_WINDOW(window),yon_char_append("com.ublinux.",LocaleName));
    char *title_str = about_label(TITLE_LABEL);
    gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(window),title_str);
    gtk_about_dialog_set_program_name(GTK_ABOUT_DIALOG(window),LocaleName);
    gtk_label_set_text(GTK_LABEL(title),title_str);
    g_signal_connect(G_OBJECT(window),"activate-link",G_CALLBACK(on_link),NULL);
    gtk_widget_set_visible(hideButtonBox,0);
    gtk_widget_destroy(hideButtonBox);
    gtk_window_set_title(GTK_WINDOW(window),title_str);
    gtk_widget_show(window);
    free(title_str);
}