<commit_before>/**
 * @file    gui.c
 * @brief  
 *
 * Copyright (C) 2010 Gummi-Dev Team <<EMAIL>>
 * All Rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "gui.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifndef WIN32
#   include <unistd.h>
#endif

#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "biblio.h"
#include "configfile.h"
#include "editor.h"
#include "environment.h"
#include "importer.h"
#include "updatecheck.h"
#include "utils.h"
#include "template.h"

extern Gummi* gummi;

/* Many of the functions in this file are based on the excellent GTK+
 * tutorials written by <NAME> that can be found on: 
 * http://www.micahcarrick.com/gtk-glade-tutorial-part-3.html */

GummiGui* gui_init(GtkBuilder* builder) {
    L_F_DEBUG;
    GtkWidget *hpaned;
    GtkWidget *errortext;
    gint width = 0, height = 0;

    GummiGui* g = (GummiGui*)g_malloc(sizeof(GummiGui));

    errortext = GTK_WIDGET(gtk_builder_get_object(builder, "errorfield"));
    g->mainwindow =
        GTK_WIDGET(gtk_builder_get_object(builder, "mainwindow"));
    g->toolbar =
        GTK_HBOX(gtk_builder_get_object(builder, "toolbar"));
    g->statusbar =
        GTK_STATUSBAR(gtk_builder_get_object(builder, "statusbar"));
    g->rightpane =
        GTK_VBOX(gtk_builder_get_object(builder, "rightpanebox"));
    g->previewoff = GTK_TOGGLE_TOOL_BUTTON(
            gtk_builder_get_object(builder, "tool_previewoff"));
    g->errorbuff =
        gtk_text_view_get_buffer(GTK_TEXT_VIEW(errortext));
    g->menu_spelling =
        GTK_CHECK_MENU_ITEM(gtk_builder_get_object(builder, "menu_spelling"));
    g->menu_toolbar =
        GTK_CHECK_MENU_ITEM(gtk_builder_get_object(builder, "menu_toolbar"));
    g->menu_statusbar =
        GTK_CHECK_MENU_ITEM(gtk_builder_get_object(builder, "menu_statusbar"));
    g->menu_rightpane =
        GTK_CHECK_MENU_ITEM(gtk_builder_get_object(builder, "menu_rightpane"));
    g->statusid =
        gtk_statusbar_get_context_id(GTK_STATUSBAR(g->statusbar), "Gummi");
    g->recent[0] =
        GTK_MENU_ITEM(gtk_builder_get_object(builder, "menu_recent1"));
    g->recent[1] =
        GTK_MENU_ITEM(gtk_builder_get_object(builder, "menu_recent2"));
    g->recent[2] =
        GTK_MENU_ITEM(gtk_builder_get_object(builder, "menu_recent3"));
    g->recent[3] =
        GTK_MENU_ITEM(gtk_builder_get_object(builder, "menu_recent4"));
    g->recent[4] =
        GTK_MENU_ITEM(gtk_builder_get_object(builder, "menu_recent5"));

    g->prefsgui = prefsgui_init(g);
    g->searchgui = searchgui_init(builder);
    g->importgui = importgui_init(builder);

    PangoFontDescription* font_desc = 
        pango_font_description_from_string("Monospace 8");
    gtk_widget_modify_font(errortext, font_desc);
    pango_font_description_free(font_desc);
    gtk_window_get_size(GTK_WINDOW(g->mainwindow), &width, &height);

    hpaned= GTK_WIDGET(gtk_builder_get_object(builder, "hpaned"));
    gtk_paned_set_position(GTK_PANED(hpaned), (width/2)); 

#ifndef USE_GTKSPELL
    gtk_widget_set_sensitive(GTK_WIDGET(g->menu_spelling), FALSE);
#else
    if (config_get_value("spelling"))
        gtk_check_menu_item_set_active(g->menu_spelling, TRUE);
#endif
    if (config_get_value("toolbar")) {
        gtk_check_menu_item_set_active(g->menu_toolbar, TRUE);
        gtk_widget_show(GTK_WIDGET(g->toolbar));
    }

    if (config_get_value("statusbar")) {
        gtk_check_menu_item_set_active(g->menu_statusbar, TRUE);
        gtk_widget_show(GTK_WIDGET(g->statusbar));
    }

    if (config_get_value("rightpane")) {
        gtk_check_menu_item_set_active(g->menu_rightpane, TRUE);
        gtk_widget_show(GTK_WIDGET(g->rightpane));
    } else {
        config_set_value("compile_status", "False");
        gtk_toggle_tool_button_set_active(g->previewoff, TRUE);
    }

    if (!config_get_value("compile_status"))
        gtk_toggle_tool_button_set_active(g->previewoff, TRUE);

    g->recent_list[0] = g_strdup(config_get_value("recent1"));
    g->recent_list[1] = g_strdup(config_get_value("recent2"));
    g->recent_list[2] = g_strdup(config_get_value("recent3"));
    g->recent_list[3] = g_strdup(config_get_value("recent4"));
    g->recent_list[4] = g_strdup(config_get_value("recent5"));

    display_recent_files(g);

    return g;
}

void gui_main(GtkBuilder* builder) {
    L_F_DEBUG;
    gtk_builder_connect_signals(builder, NULL);       
    g_signal_connect(g_e_buffer, "changed",
            G_CALLBACK(check_motion_timer), NULL);
    gtk_widget_show_all(gummi->gui->mainwindow);
    gtk_main();
}

gboolean gui_quit(void) {
    L_F_DEBUG;
    gint ret = check_for_save();
    if (GTK_RESPONSE_YES == ret)
        on_menu_save_activate(NULL, NULL);  
    else if (GTK_RESPONSE_CANCEL == ret || GTK_RESPONSE_DELETE_EVENT == ret)
        return TRUE;
    gtk_main_quit();

    printf("   ___ \n"
            "  {o,o}    Thanks for using Gummi!\n" 
            "  |)__)    I welcome your feedback at:\n"
            "  -\"-\"-    http://gummi.midnightcoding.org\n\n");
    return FALSE;
}

void gui_update_title(void) {
    L_F_DEBUG;
    gchar* basename = NULL;
    gchar* dirname = NULL;
    gchar* title = NULL;
    if (gummi->finfo->filename) {
        basename = g_path_get_basename(gummi->finfo->filename);
        dirname = g_path_get_dirname(gummi->finfo->filename);
        title = g_strdup_printf("%s%s (%s) - %s",
                (gtk_text_buffer_get_modified(g_e_buffer)? "*": ""),
                basename, dirname, PACKAGE_NAME);
        g_free(basename);
        g_free(dirname);
    } else
        title = g_strdup_printf("%sUnsaved Document - %s",
                (gtk_text_buffer_get_modified(g_e_buffer)? "*": ""),
                PACKAGE_NAME);

    gtk_window_set_title(GTK_WINDOW(gummi->gui->mainwindow), title);
    g_free(title);
}

void on_menu_new_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    gint ret = check_for_save();
    if (GTK_RESPONSE_YES == ret)
        on_menu_save_activate(NULL, NULL);  
    else if (GTK_RESPONSE_CANCEL == ret || GTK_RESPONSE_DELETE_EVENT == ret)
        return;
    iofunctions_load_default_text(gummi->editor);
    gummi_create_environment(gummi, NULL);
}

void on_menu_template_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    gtk_list_store_clear(gummi->templ->list_templates);
    template_setup(gummi->templ);
    gtk_widget_show_all(GTK_WIDGET(gummi->templ->templatewindow));
}

void on_menu_exportpdf_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    gchar* filename = NULL;
    gchar* path = NULL;

    if (gummi->finfo->filename)
        path = g_path_get_dirname(gummi->finfo->filename);
    filename = get_save_filename(FILTER_PDF, path);
    if (path) g_free(path);
    if (filename)
        motion_export_pdffile(gummi->motion, filename);
}

void on_menu_recent_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    const gchar* name = gtk_menu_item_get_label(GTK_MENU_ITEM(widget));
    gchar* ptr;
    gint index = name[0] - '0' -1;
    gint ret = check_for_save();

    if (GTK_RESPONSE_YES == ret)
        on_menu_save_activate(NULL, NULL);  
    else if (GTK_RESPONSE_CANCEL == ret || GTK_RESPONSE_DELETE_EVENT == ret)
        return;

    if (utils_path_exists(gummi->gui->recent_list[index])) {
        iofunctions_load_file(gummi->editor, gummi->gui->recent_list[index]); 
        gummi_create_environment(gummi, gummi->gui->recent_list[index]);
    } else {
        ptr = g_strdup_printf(_("Error loading recent file: %s"),
                gummi->gui->recent_list[index]);
        statusbar_set_message(ptr);
        g_free(ptr);
        g_free(gummi->gui->recent_list[index]);
        while (index < 4) {
            gummi->gui->recent_list[index] = gummi->gui->recent_list[index+1];
            ++index;
        }
        gummi->gui->recent_list[4] = 0;
    }
    display_recent_files(gummi->gui);
}

void on_menu_open_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    gchar *filename = NULL;
    gint ret = check_for_save();

    if (GTK_RESPONSE_YES == ret)
        on_menu_save_activate(NULL, NULL);  
    else if (GTK_RESPONSE_CANCEL == ret || GTK_RESPONSE_DELETE_EVENT == ret)
        return;
    filename = get_open_filename(FILTER_LATEX);
    if (filename != NULL) {
        iofunctions_load_file(gummi->editor, filename); 
        gummi_create_environment(gummi, filename);
        add_to_recent_list(filename);
    }
}

void on_menu_save_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    gchar* filename = NULL;
    gboolean new = FALSE;

    if (!gummi->finfo->filename) {
        if ((filename = get_save_filename(FILTER_LATEX, NULL))) {
            if (strcmp(filename + strlen(filename) -4, ".tex")) {
                filename = g_strdup_printf("%s.tex", filename);
                new = TRUE;
            }
            fileinfo_set_filename(gummi->finfo, filename);
            iofunctions_write_file(gummi->editor, filename); 
            add_to_recent_list(filename);
            if (new) g_free(filename);
        }
    } else
        iofunctions_write_file(gummi->editor, gummi->finfo->filename); 
    gui_update_title();
}

void on_menu_saveas_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    gchar* filename = NULL;
    gboolean new = FALSE;
    if ((filename = get_save_filename(FILTER_LATEX, NULL))) {
        if (strcmp(filename + strlen(filename) -4, ".tex")) {
            filename = g_strdup_printf("%s.tex", filename);
            new = TRUE;
        }
        iofunctions_write_file(gummi->editor, filename); 
        gummi_create_environment(gummi, filename);
        add_to_recent_list(filename);
        if (new) g_free(filename);
    }
}

void on_menu_cut_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    GtkClipboard     *clipboard;

    clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
    gtk_text_buffer_cut_clipboard(g_e_buffer, clipboard, TRUE);
}

void on_menu_copy_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    GtkClipboard     *clipboard;

    clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
    gtk_text_buffer_copy_clipboard(g_e_buffer, clipboard);
}
void on_menu_paste_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    GtkClipboard     *clipboard;

    clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
    gtk_text_buffer_paste_clipboard(g_e_buffer, clipboard, NULL, TRUE);
}

void on_menu_undo_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    editor_undo_change(gummi->editor);
}

void on_menu_redo_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    editor_redo_change(gummi->editor);
}

void on_menu_delete_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    gtk_text_buffer_delete_selection(g_e_buffer, FALSE, TRUE);
}

void on_menu_selectall_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    GtkTextIter start, end;
    gtk_text_buffer_get_bounds(g_e_buffer, &start, &end);
    gtk_text_buffer_select_range(g_e_buffer, &start, &end);
}

void on_menu_preferences_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    prefsgui_main();
}

void on_menu_statusbar_toggled(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) {
        gtk_widget_show(GTK_WIDGET(gummi->gui->statusbar));
        config_set_value("statusbar", "True");
    } else {
        gtk_widget_hide(GTK_WIDGET(gummi->gui->statusbar));
        config_set_value("statusbar", "False");
    }
}

void on_menu_toolbar_toggled(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) {
        gtk_widget_show(GTK_WIDGET(gummi->gui->toolbar));
        config_set_value("toolbar", "True");
    } else {
        gtk_widget_hide(GTK_WIDGET(gummi->gui->toolbar));
        config_set_value("toolbar", "False");
    }
}

void on_menu_rightpane_toggled(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) {
        gtk_widget_show(GTK_WIDGET(gummi->gui->rightpane));
        config_set_value("rightpane", "True");
    } else {
        gtk_widget_hide(GTK_WIDGET(gummi->gui->rightpane));
        config_set_value("rightpane", "False");
    }
}

    void on_menu_fullscreen_toggled(GtkWidget *widget, void * user) {
        if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
            gtk_window_fullscreen(GTK_WINDOW(gummi->gui->mainwindow));
        else
            gtk_window_unfullscreen(GTK_WINDOW(gummi->gui->mainwindow));
    }

void on_menu_find_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    gtk_entry_set_text(gummi->gui->searchgui->searchentry, "");
    gtk_entry_set_text(gummi->gui->searchgui->replaceentry, "");
    gtk_widget_grab_focus(GTK_WIDGET(gummi->gui->searchgui->searchentry));
    gtk_widget_show_all(GTK_WIDGET(gummi->gui->searchgui->searchwindow));
}

void on_menu_findnext_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    editor_jumpto_search_result(gummi->editor, 1);
}

void on_menu_findprev_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    editor_jumpto_search_result(gummi->editor, -1);
}

void on_menu_bibload_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    gchar *filename = NULL;
    filename = get_open_filename(FILTER_BIBLIO);
    if (biblio_check_valid_file(gummi->biblio, filename)) {
        biblio_setup_bibliography(gummi->biblio, gummi->editor);
        gtk_label_set_text(gummi->biblio->filenm_label,gummi->biblio->basename);
    }
}

void on_menu_bibupdate_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    biblio_compile_bibliography(gummi->biblio, gummi->motion);
}

void on_menu_docstat_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    GtkWidget* dialog = 0;
    gint i = 0;
    gchar* str = 0;
    gchar* output = 0;
    gchar* cmd = g_strdup_printf("texcount %s", gummi->finfo->workfile);
    pdata result = utils_popen_r(cmd);
    gchar* terms[] = { _("Words in text"),
                       _("Words in headers"),
                       _("Words in float captions"),
                       _("Number of headers"),
                       _("Number of floats"),
                       _("Number of math inlines"),
                       _("Number of math displayed")
                     };
    gchar* res[7];

    str = strtok(result.data, ":\n");
    while (str) {
      str = strtok(NULL, ":\n");
      if (strlen(str) > 10) continue;
      res[i++] = str;
      if (i == 7) break;
    }

    output = g_strconcat(terms[0], ": ", res[0], "\n",
                         terms[1], ": ", res[1], "\n",
                         terms[2], ": ", res[2], "\n",
                         terms[3], ": ", res[3], "\n",
                         terms[4], ": ", res[4], "\n",
                         terms[4], ": ", res[5], "\n",
                         terms[6], ": ", res[6], "\n",
                         NULL);

    dialog = gtk_message_dialog_new(GTK_WINDOW(gummi->gui->mainwindow),
            GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
            GTK_MESSAGE_INFO,
            GTK_BUTTONS_OK,
            "%s", output);
    gtk_window_set_title(GTK_WINDOW(dialog), _("Document Statistics"));
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
    g_free(cmd);
    g_free(output);
}

void on_menu_spelling_toggled(GtkWidget *widget, void * user) {
    L_F_DEBUG;
#ifdef USE_GTKSPELL
    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) {
        editor_activate_spellchecking(gummi->editor, TRUE);
        config_set_value("spelling", "True");
    } else {
        editor_activate_spellchecking(gummi->editor, FALSE);
        config_set_value("spelling", "False");
    }
#endif
}

void on_menu_update_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    gboolean ret = updatecheck(GTK_WINDOW(gummi->gui->mainwindow));
    if (!ret)
        slog(L_G_ERROR, "Update check failed!\n");
}

void on_menu_about_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    GError* err = NULL;
    GdkPixbuf* icon = gdk_pixbuf_new_from_file_at_size
        (DATADIR"/gummi-beta.png", 60, 60, &err);
    const gchar* authors[] = { "<NAME>\n"
        "<<EMAIL>>",
        "<NAME>\n"
            "<<EMAIL>>\n",
        "Contributors:",
        "<NAME>",
        "<NAME>", NULL };
    const gchar* artists[] = {"Windows version Icon set from Elemetary Project:\n"
        "http://www.elementary-project.com/", NULL};
                
    const gchar* translators =
        "Brazilian-Portugese: <NAME>\n"
        "Catalan: <NAME>\n"
        "Danish: <NAME>\n"
        "Dutch: <NAME>\n"
        "French: <NAME> & <NAME>\n"
        "German: <NAME>\n"
        "Greek: <NAME>\n"
        "Italian: <NAME>\n" 
        "Russian: Kruvalig\n"
        "Taiwanese: Wei-<NAME>\n";

    GtkAboutDialog* dialog = GTK_ABOUT_DIALOG(gtk_about_dialog_new());
    gtk_window_set_transient_for(GTK_WINDOW(dialog),
            GTK_WINDOW(gummi->gui->mainwindow));
    gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
    gtk_about_dialog_set_authors(dialog, authors);
    gtk_about_dialog_set_program_name(dialog, PACKAGE_NAME);
    gtk_about_dialog_set_version(dialog, PACKAGE_VERSION);
    gtk_about_dialog_set_website(dialog, PACKAGE_URL);
    gtk_about_dialog_set_copyright(dialog, PACKAGE_COPYRIGHT);
    gtk_about_dialog_set_license(dialog, PACKAGE_LICENSE);
    gtk_about_dialog_set_logo(dialog, icon);
    gtk_about_dialog_set_comments(dialog, PACKAGE_COMMENTS);
    gtk_about_dialog_set_artists(dialog, artists);
    gtk_about_dialog_set_translator_credits(dialog, translators);
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(GTK_WIDGET(dialog));
}

void on_tool_previewoff_toggled(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    gboolean value =
        gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(widget));
    config_set_value("compile_status", (!value)?"Ture":"False");
    if (value)
        motion_stop_updatepreview(gummi->motion);
    else
        motion_start_updatepreview(gummi->motion);
}

void on_tool_textstyle_bold_activate(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    editor_set_selection_textstyle(gummi->editor, "tool_bold");
}

void on_tool_textstyle_italic_activate(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    editor_set_selection_textstyle(gummi->editor, "tool_italic");
}

void on_tool_textstyle_underline_activate(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    editor_set_selection_textstyle(gummi->editor, "tool_unline");
}

void on_tool_textstyle_left_activate(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    editor_set_selection_textstyle(gummi->editor, "tool_left");
}

void on_tool_textstyle_center_activate(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    editor_set_selection_textstyle(gummi->editor, "tool_center");
}

void on_tool_textstyle_right_activate(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    editor_set_selection_textstyle(gummi->editor, "tool_right");
}



void on_button_template_add_clicked(GtkWidget* widget, void* user) {
    template_add_new_entry(gummi->templ);
}

void on_button_template_remove_clicked(GtkWidget* widget, void* user) {
    template_remove_entry(gummi->templ);
}

void on_button_template_open_clicked(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    gchar *text;
    text = template_open_selected(gummi->templ);
    if (text) {
        editor_fill_buffer(gummi->editor, text);
        gummi_create_environment(gummi, NULL);
        g_free(text);
        gtk_widget_hide(GTK_WIDGET(gummi->templ->templatewindow));
    }
}

void on_button_template_close_clicked(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    gtk_widget_set_sensitive(GTK_WIDGET(gummi->templ->template_add), TRUE);
    gtk_widget_set_sensitive(GTK_WIDGET(gummi->templ->template_remove), TRUE);
    gtk_widget_set_sensitive(GTK_WIDGET(gummi->templ->template_open), TRUE);
    gtk_widget_hide(GTK_WIDGET(gummi->templ->templatewindow));
}

void on_template_rowitem_editted(GtkWidget* widget, gchar *path, gchar* filenm,
        void* user) {
    GtkTreeModel *model;
    GtkTreeIter iter;
    GtkTreeSelection *selection;
    gchar *filepath = g_build_filename(g_get_user_config_dir(),
                                       "gummi", "templates", filenm, NULL);
    
    model = gtk_tree_view_get_model(gummi->templ->templateview);
    selection = gtk_tree_view_get_selection(gummi->templ->templateview);
    
    if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
        gtk_list_store_set(gummi->templ->list_templates, &iter, 0, filenm, 1,
                filepath, -1);
        gchar *text = editor_grab_buffer(gummi->editor);
        template_create_file(gummi->templ, filenm, text);
    }
    //gtk_list_store_clear(gummi->templ->list_templates);
    //template_setup(gummi->templ);
    g_free(filepath);
}

<commit_msg>remove comments<commit_after>/**
 * @file    gui.c
 * @brief  
 *
 * Copyright (C) 2010 Gummi-Dev Team <alexv<EMAIL>>
 * All Rights reserved.
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#include "gui.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#ifndef WIN32
#   include <unistd.h>
#endif

#include <glib.h>
#include <gdk-pixbuf/gdk-pixbuf.h>

#include "biblio.h"
#include "configfile.h"
#include "editor.h"
#include "environment.h"
#include "importer.h"
#include "updatecheck.h"
#include "utils.h"
#include "template.h"

extern Gummi* gummi;

/* Many of the functions in this file are based on the excellent GTK+
 * tutorials written by <NAME> that can be found on: 
 * http://www.micahcarrick.com/gtk-glade-tutorial-part-3.html */

GummiGui* gui_init(GtkBuilder* builder) {
    L_F_DEBUG;
    GtkWidget *hpaned;
    GtkWidget *errortext;
    gint width = 0, height = 0;

    GummiGui* g = (GummiGui*)g_malloc(sizeof(GummiGui));

    errortext = GTK_WIDGET(gtk_builder_get_object(builder, "errorfield"));
    g->mainwindow =
        GTK_WIDGET(gtk_builder_get_object(builder, "mainwindow"));
    g->toolbar =
        GTK_HBOX(gtk_builder_get_object(builder, "toolbar"));
    g->statusbar =
        GTK_STATUSBAR(gtk_builder_get_object(builder, "statusbar"));
    g->rightpane =
        GTK_VBOX(gtk_builder_get_object(builder, "rightpanebox"));
    g->previewoff = GTK_TOGGLE_TOOL_BUTTON(
            gtk_builder_get_object(builder, "tool_previewoff"));
    g->errorbuff =
        gtk_text_view_get_buffer(GTK_TEXT_VIEW(errortext));
    g->menu_spelling =
        GTK_CHECK_MENU_ITEM(gtk_builder_get_object(builder, "menu_spelling"));
    g->menu_toolbar =
        GTK_CHECK_MENU_ITEM(gtk_builder_get_object(builder, "menu_toolbar"));
    g->menu_statusbar =
        GTK_CHECK_MENU_ITEM(gtk_builder_get_object(builder, "menu_statusbar"));
    g->menu_rightpane =
        GTK_CHECK_MENU_ITEM(gtk_builder_get_object(builder, "menu_rightpane"));
    g->statusid =
        gtk_statusbar_get_context_id(GTK_STATUSBAR(g->statusbar), "Gummi");
    g->recent[0] =
        GTK_MENU_ITEM(gtk_builder_get_object(builder, "menu_recent1"));
    g->recent[1] =
        GTK_MENU_ITEM(gtk_builder_get_object(builder, "menu_recent2"));
    g->recent[2] =
        GTK_MENU_ITEM(gtk_builder_get_object(builder, "menu_recent3"));
    g->recent[3] =
        GTK_MENU_ITEM(gtk_builder_get_object(builder, "menu_recent4"));
    g->recent[4] =
        GTK_MENU_ITEM(gtk_builder_get_object(builder, "menu_recent5"));

    g->prefsgui = prefsgui_init(g);
    g->searchgui = searchgui_init(builder);
    g->importgui = importgui_init(builder);

    PangoFontDescription* font_desc = 
        pango_font_description_from_string("Monospace 8");
    gtk_widget_modify_font(errortext, font_desc);
    pango_font_description_free(font_desc);
    gtk_window_get_size(GTK_WINDOW(g->mainwindow), &width, &height);

    hpaned= GTK_WIDGET(gtk_builder_get_object(builder, "hpaned"));
    gtk_paned_set_position(GTK_PANED(hpaned), (width/2)); 

#ifndef USE_GTKSPELL
    gtk_widget_set_sensitive(GTK_WIDGET(g->menu_spelling), FALSE);
#else
    if (config_get_value("spelling"))
        gtk_check_menu_item_set_active(g->menu_spelling, TRUE);
#endif
    if (config_get_value("toolbar")) {
        gtk_check_menu_item_set_active(g->menu_toolbar, TRUE);
        gtk_widget_show(GTK_WIDGET(g->toolbar));
    }

    if (config_get_value("statusbar")) {
        gtk_check_menu_item_set_active(g->menu_statusbar, TRUE);
        gtk_widget_show(GTK_WIDGET(g->statusbar));
    }

    if (config_get_value("rightpane")) {
        gtk_check_menu_item_set_active(g->menu_rightpane, TRUE);
        gtk_widget_show(GTK_WIDGET(g->rightpane));
    } else {
        config_set_value("compile_status", "False");
        gtk_toggle_tool_button_set_active(g->previewoff, TRUE);
    }

    if (!config_get_value("compile_status"))
        gtk_toggle_tool_button_set_active(g->previewoff, TRUE);

    g->recent_list[0] = g_strdup(config_get_value("recent1"));
    g->recent_list[1] = g_strdup(config_get_value("recent2"));
    g->recent_list[2] = g_strdup(config_get_value("recent3"));
    g->recent_list[3] = g_strdup(config_get_value("recent4"));
    g->recent_list[4] = g_strdup(config_get_value("recent5"));

    display_recent_files(g);

    return g;
}

void gui_main(GtkBuilder* builder) {
    L_F_DEBUG;
    gtk_builder_connect_signals(builder, NULL);       
    g_signal_connect(g_e_buffer, "changed",
            G_CALLBACK(check_motion_timer), NULL);
    gtk_widget_show_all(gummi->gui->mainwindow);
    gtk_main();
}

gboolean gui_quit(void) {
    L_F_DEBUG;
    gint ret = check_for_save();
    if (GTK_RESPONSE_YES == ret)
        on_menu_save_activate(NULL, NULL);  
    else if (GTK_RESPONSE_CANCEL == ret || GTK_RESPONSE_DELETE_EVENT == ret)
        return TRUE;
    gtk_main_quit();

    printf("   ___ \n"
            "  {o,o}    Thanks for using Gummi!\n" 
            "  |)__)    I welcome your feedback at:\n"
            "  -\"-\"-    http://gummi.midnightcoding.org\n\n");
    return FALSE;
}

void gui_update_title(void) {
    L_F_DEBUG;
    gchar* basename = NULL;
    gchar* dirname = NULL;
    gchar* title = NULL;
    if (gummi->finfo->filename) {
        basename = g_path_get_basename(gummi->finfo->filename);
        dirname = g_path_get_dirname(gummi->finfo->filename);
        title = g_strdup_printf("%s%s (%s) - %s",
                (gtk_text_buffer_get_modified(g_e_buffer)? "*": ""),
                basename, dirname, PACKAGE_NAME);
        g_free(basename);
        g_free(dirname);
    } else
        title = g_strdup_printf("%sUnsaved Document - %s",
                (gtk_text_buffer_get_modified(g_e_buffer)? "*": ""),
                PACKAGE_NAME);

    gtk_window_set_title(GTK_WINDOW(gummi->gui->mainwindow), title);
    g_free(title);
}

void on_menu_new_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    gint ret = check_for_save();
    if (GTK_RESPONSE_YES == ret)
        on_menu_save_activate(NULL, NULL);  
    else if (GTK_RESPONSE_CANCEL == ret || GTK_RESPONSE_DELETE_EVENT == ret)
        return;
    iofunctions_load_default_text(gummi->editor);
    gummi_create_environment(gummi, NULL);
}

void on_menu_template_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    gtk_list_store_clear(gummi->templ->list_templates);
    template_setup(gummi->templ);
    gtk_widget_show_all(GTK_WIDGET(gummi->templ->templatewindow));
}

void on_menu_exportpdf_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    gchar* filename = NULL;
    gchar* path = NULL;

    if (gummi->finfo->filename)
        path = g_path_get_dirname(gummi->finfo->filename);
    filename = get_save_filename(FILTER_PDF, path);
    if (path) g_free(path);
    if (filename)
        motion_export_pdffile(gummi->motion, filename);
}

void on_menu_recent_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    const gchar* name = gtk_menu_item_get_label(GTK_MENU_ITEM(widget));
    gchar* ptr;
    gint index = name[0] - '0' -1;
    gint ret = check_for_save();

    if (GTK_RESPONSE_YES == ret)
        on_menu_save_activate(NULL, NULL);  
    else if (GTK_RESPONSE_CANCEL == ret || GTK_RESPONSE_DELETE_EVENT == ret)
        return;

    if (utils_path_exists(gummi->gui->recent_list[index])) {
        iofunctions_load_file(gummi->editor, gummi->gui->recent_list[index]); 
        gummi_create_environment(gummi, gummi->gui->recent_list[index]);
    } else {
        ptr = g_strdup_printf(_("Error loading recent file: %s"),
                gummi->gui->recent_list[index]);
        statusbar_set_message(ptr);
        g_free(ptr);
        g_free(gummi->gui->recent_list[index]);
        while (index < 4) {
            gummi->gui->recent_list[index] = gummi->gui->recent_list[index+1];
            ++index;
        }
        gummi->gui->recent_list[4] = 0;
    }
    display_recent_files(gummi->gui);
}

void on_menu_open_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    gchar *filename = NULL;
    gint ret = check_for_save();

    if (GTK_RESPONSE_YES == ret)
        on_menu_save_activate(NULL, NULL);  
    else if (GTK_RESPONSE_CANCEL == ret || GTK_RESPONSE_DELETE_EVENT == ret)
        return;
    filename = get_open_filename(FILTER_LATEX);
    if (filename != NULL) {
        iofunctions_load_file(gummi->editor, filename); 
        gummi_create_environment(gummi, filename);
        add_to_recent_list(filename);
    }
}

void on_menu_save_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    gchar* filename = NULL;
    gboolean new = FALSE;

    if (!gummi->finfo->filename) {
        if ((filename = get_save_filename(FILTER_LATEX, NULL))) {
            if (strcmp(filename + strlen(filename) -4, ".tex")) {
                filename = g_strdup_printf("%s.tex", filename);
                new = TRUE;
            }
            fileinfo_set_filename(gummi->finfo, filename);
            iofunctions_write_file(gummi->editor, filename); 
            add_to_recent_list(filename);
            if (new) g_free(filename);
        }
    } else
        iofunctions_write_file(gummi->editor, gummi->finfo->filename); 
    gui_update_title();
}

void on_menu_saveas_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    gchar* filename = NULL;
    gboolean new = FALSE;
    if ((filename = get_save_filename(FILTER_LATEX, NULL))) {
        if (strcmp(filename + strlen(filename) -4, ".tex")) {
            filename = g_strdup_printf("%s.tex", filename);
            new = TRUE;
        }
        iofunctions_write_file(gummi->editor, filename); 
        gummi_create_environment(gummi, filename);
        add_to_recent_list(filename);
        if (new) g_free(filename);
    }
}

void on_menu_cut_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    GtkClipboard     *clipboard;

    clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
    gtk_text_buffer_cut_clipboard(g_e_buffer, clipboard, TRUE);
}

void on_menu_copy_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    GtkClipboard     *clipboard;

    clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
    gtk_text_buffer_copy_clipboard(g_e_buffer, clipboard);
}
void on_menu_paste_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    GtkClipboard     *clipboard;

    clipboard = gtk_clipboard_get(GDK_SELECTION_CLIPBOARD);
    gtk_text_buffer_paste_clipboard(g_e_buffer, clipboard, NULL, TRUE);
}

void on_menu_undo_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    editor_undo_change(gummi->editor);
}

void on_menu_redo_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    editor_redo_change(gummi->editor);
}

void on_menu_delete_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    gtk_text_buffer_delete_selection(g_e_buffer, FALSE, TRUE);
}

void on_menu_selectall_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    GtkTextIter start, end;
    gtk_text_buffer_get_bounds(g_e_buffer, &start, &end);
    gtk_text_buffer_select_range(g_e_buffer, &start, &end);
}

void on_menu_preferences_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    prefsgui_main();
}

void on_menu_statusbar_toggled(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) {
        gtk_widget_show(GTK_WIDGET(gummi->gui->statusbar));
        config_set_value("statusbar", "True");
    } else {
        gtk_widget_hide(GTK_WIDGET(gummi->gui->statusbar));
        config_set_value("statusbar", "False");
    }
}

void on_menu_toolbar_toggled(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) {
        gtk_widget_show(GTK_WIDGET(gummi->gui->toolbar));
        config_set_value("toolbar", "True");
    } else {
        gtk_widget_hide(GTK_WIDGET(gummi->gui->toolbar));
        config_set_value("toolbar", "False");
    }
}

void on_menu_rightpane_toggled(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) {
        gtk_widget_show(GTK_WIDGET(gummi->gui->rightpane));
        config_set_value("rightpane", "True");
    } else {
        gtk_widget_hide(GTK_WIDGET(gummi->gui->rightpane));
        config_set_value("rightpane", "False");
    }
}

    void on_menu_fullscreen_toggled(GtkWidget *widget, void * user) {
        if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget)))
            gtk_window_fullscreen(GTK_WINDOW(gummi->gui->mainwindow));
        else
            gtk_window_unfullscreen(GTK_WINDOW(gummi->gui->mainwindow));
    }

void on_menu_find_activate(GtkWidget *widget, void* user) {
    L_F_DEBUG;
    gtk_entry_set_text(gummi->gui->searchgui->searchentry, "");
    gtk_entry_set_text(gummi->gui->searchgui->replaceentry, "");
    gtk_widget_grab_focus(GTK_WIDGET(gummi->gui->searchgui->searchentry));
    gtk_widget_show_all(GTK_WIDGET(gummi->gui->searchgui->searchwindow));
}

void on_menu_findnext_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    editor_jumpto_search_result(gummi->editor, 1);
}

void on_menu_findprev_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    editor_jumpto_search_result(gummi->editor, -1);
}

void on_menu_bibload_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    gchar *filename = NULL;
    filename = get_open_filename(FILTER_BIBLIO);
    if (biblio_check_valid_file(gummi->biblio, filename)) {
        biblio_setup_bibliography(gummi->biblio, gummi->editor);
        gtk_label_set_text(gummi->biblio->filenm_label,gummi->biblio->basename);
    }
}

void on_menu_bibupdate_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    biblio_compile_bibliography(gummi->biblio, gummi->motion);
}

void on_menu_docstat_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    GtkWidget* dialog = 0;
    gint i = 0;
    gchar* str = 0;
    gchar* output = 0;
    gchar* cmd = g_strdup_printf("texcount %s", gummi->finfo->workfile);
    pdata result = utils_popen_r(cmd);
    gchar* terms[] = { _("Words in text"),
                       _("Words in headers"),
                       _("Words in float captions"),
                       _("Number of headers"),
                       _("Number of floats"),
                       _("Number of math inlines"),
                       _("Number of math displayed")
                     };
    gchar* res[7];

    str = strtok(result.data, ":\n");
    while (str) {
      str = strtok(NULL, ":\n");
      if (strlen(str) > 10) continue;
      res[i++] = str;
      if (i == 7) break;
    }

    output = g_strconcat(terms[0], ": ", res[0], "\n",
                         terms[1], ": ", res[1], "\n",
                         terms[2], ": ", res[2], "\n",
                         terms[3], ": ", res[3], "\n",
                         terms[4], ": ", res[4], "\n",
                         terms[4], ": ", res[5], "\n",
                         terms[6], ": ", res[6], "\n",
                         NULL);

    dialog = gtk_message_dialog_new(GTK_WINDOW(gummi->gui->mainwindow),
            GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
            GTK_MESSAGE_INFO,
            GTK_BUTTONS_OK,
            "%s", output);
    gtk_window_set_title(GTK_WINDOW(dialog), _("Document Statistics"));
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
    g_free(cmd);
    g_free(output);
}

void on_menu_spelling_toggled(GtkWidget *widget, void * user) {
    L_F_DEBUG;
#ifdef USE_GTKSPELL
    if (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(widget))) {
        editor_activate_spellchecking(gummi->editor, TRUE);
        config_set_value("spelling", "True");
    } else {
        editor_activate_spellchecking(gummi->editor, FALSE);
        config_set_value("spelling", "False");
    }
#endif
}

void on_menu_update_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    gboolean ret = updatecheck(GTK_WINDOW(gummi->gui->mainwindow));
    if (!ret)
        slog(L_G_ERROR, "Update check failed!\n");
}

void on_menu_about_activate(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    GError* err = NULL;
    GdkPixbuf* icon = gdk_pixbuf_new_from_file_at_size
        (DATADIR"/gummi-beta.png", 60, 60, &err);
    const gchar* authors[] = { "<NAME>\n"
        "<<EMAIL>>",
        "<NAME>\n"
            "<<EMAIL>>\n",
        "Contributors:",
        "<NAME>",
        "<NAME>", NULL };
    const gchar* artists[] = {"Windows version Icon set from Elemetary Project:\n"
        "http://www.elementary-project.com/", NULL};
                
    const gchar* translators =
        "Brazilian-Portugese: <NAME>\n"
        "Catalan: <NAME>\n"
        "Danish: <NAME>\n"
        "Dutch: <NAME>\n"
        "French: <NAME> & <NAME>\n"
        "German: <NAME>\n"
        "Greek: Dimitris Leventeas\n"
        "Italian: Salvatore Vassallo\n" 
        "Russian: Kruvalig\n"
        "Taiwanese: Wei-Ning Huang\n";

    GtkAboutDialog* dialog = GTK_ABOUT_DIALOG(gtk_about_dialog_new());
    gtk_window_set_transient_for(GTK_WINDOW(dialog),
            GTK_WINDOW(gummi->gui->mainwindow));
    gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
    gtk_about_dialog_set_authors(dialog, authors);
    gtk_about_dialog_set_program_name(dialog, PACKAGE_NAME);
    gtk_about_dialog_set_version(dialog, PACKAGE_VERSION);
    gtk_about_dialog_set_website(dialog, PACKAGE_URL);
    gtk_about_dialog_set_copyright(dialog, PACKAGE_COPYRIGHT);
    gtk_about_dialog_set_license(dialog, PACKAGE_LICENSE);
    gtk_about_dialog_set_logo(dialog, icon);
    gtk_about_dialog_set_comments(dialog, PACKAGE_COMMENTS);
    gtk_about_dialog_set_artists(dialog, artists);
    gtk_about_dialog_set_translator_credits(dialog, translators);
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(GTK_WIDGET(dialog));
}

void on_tool_previewoff_toggled(GtkWidget *widget, void * user) {
    L_F_DEBUG;
    gboolean value =
        gtk_toggle_tool_button_get_active(GTK_TOGGLE_TOOL_BUTTON(widget));
    config_set_value("compile_status", (!value)?"Ture":"False");
    if (value)
        motion_stop_updatepreview(gummi->motion);
    else
        motion_start_updatepreview(gummi->motion);
}

void on_tool_textstyle_bold_activate(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    editor_set_selection_textstyle(gummi->editor, "tool_bold");
}

void on_tool_textstyle_italic_activate(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    editor_set_selection_textstyle(gummi->editor, "tool_italic");
}

void on_tool_textstyle_underline_activate(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    editor_set_selection_textstyle(gummi->editor, "tool_unline");
}

void on_tool_textstyle_left_activate(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    editor_set_selection_textstyle(gummi->editor, "tool_left");
}

void on_tool_textstyle_center_activate(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    editor_set_selection_textstyle(gummi->editor, "tool_center");
}

void on_tool_textstyle_right_activate(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    editor_set_selection_textstyle(gummi->editor, "tool_right");
}



void on_button_template_add_clicked(GtkWidget* widget, void* user) {
    template_add_new_entry(gummi->templ);
}

void on_button_template_remove_clicked(GtkWidget* widget, void* user) {
    template_remove_entry(gummi->templ);
}

void on_button_template_open_clicked(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    gchar *text;
    text = template_open_selected(gummi->templ);
    if (text) {
        editor_fill_buffer(gummi->editor, text);
        gummi_create_environment(gummi, NULL);
        g_free(text);
        gtk_widget_hide(GTK_WIDGET(gummi->templ->templatewindow));
    }
}

void on_button_template_close_clicked(GtkWidget* widget, void* user) {
    L_F_DEBUG;
    gtk_widget_set_sensitive(GTK_WIDGET(gummi->templ->template_add), TRUE);
    gtk_widget_set_sensitive(GTK_WIDGET(gummi->templ->template_remove), TRUE);
    gtk_widget_set_sensitive(GTK_WIDGET(gummi->templ->template_open), TRUE);
    gtk_widget_hide(GTK_WIDGET(gummi->templ->templatewindow));
}

void on_template_rowitem_editted(GtkWidget* widget, gchar *path, gchar* filenm,
        void* user) {
    GtkTreeModel *model;
    GtkTreeIter iter;
    GtkTreeSelection *selection;
    gchar *filepath = g_build_filename(g_get_user_config_dir(),
                                       "gummi", "templates", filenm, NULL);
    
    model = gtk_tree_view_get_model(gummi->templ->templateview);
    selection = gtk_tree_view_get_selection(gummi->templ->templateview);
    
    if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
        gtk_list_store_set(gummi->templ->list_templates, &iter, 0, filenm, 1,
                filepath, -1);
        gchar *text = editor_grab_buffer(gummi->editor);
        template_create_file(gummi->templ, filenm, text);
    }
    g_free(filepath);
}


{'fraction_non_alphanumeric': 0.12203497759887198, 'fraction_numerical': 0.0032294011962429783, 'mean_word_length': 3.5986195356619954, 'pattern_counts': {'":': 24, '<': 56, '<?xml version=': 0, '>': 389, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2160175', 'n_tokens_mistral': 16313, 'n_tokens_neox': 15679, 'n_words': 2520}