void
csv_import_assistant_finish (GtkAssistant *assistant, gpointer user_data)
{
    CsvImportInfo *info = user_data;

    gtk_list_store_clear (info->store);
    csv_import_read_file (info->file_name, info->regexp->str, info->store, 0 );
    csv_account_import (info);
}
void
csv_import_assistant_account_page_prepare (GtkAssistant *assistant,
        gpointer user_data)
{
    CsvImportInfo *info = user_data;

    gtk_list_store_clear (info->store);

    if (csv_import_read_file (info->file_name, info->regexp->str, info->store, 11 ) == MATCH_FOUND)
        gtk_widget_set_sensitive (info->header_row_spin, TRUE);
    else
        gtk_widget_set_sensitive (info->header_row_spin, FALSE);
}
void
csv_import_assistant_account_page_prepare (GtkAssistant *assistant,
        gpointer user_data)
{
    CsvImportInfo *info = user_data;

    gtk_list_store_clear (info->store);

    gtk_widget_set_sensitive (info->header_row_spin, TRUE);

    if (csv_import_read_file (info->file_name, info->regexp->str, info->store, 11 ) == MATCH_FOUND)
        gtk_spin_button_set_value (GTK_SPIN_BUTTON(info->header_row_spin), 1); // set header spin to 1
    else
        gtk_spin_button_set_value (GTK_SPIN_BUTTON(info->header_row_spin), 0); //reset header spin to 0
}
/*******************************************************
 * csv_import_sep_cb
 *
 * call back for type of separartor required
 *******************************************************/
void csv_import_sep_cb (GtkWidget *radio, gpointer user_data )
{
    CsvImportInfo *info = user_data;
    const gchar *name;
    gchar *temp;

    if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio)))
    {
        LEAVE("1st callback of pair. Defer to 2nd callback.");
        return;
    }

    name = gtk_buildable_get_name(GTK_BUILDABLE(radio));

    if (g_strcmp0(name, g_strdup("radio_semi")) == 0)
        g_string_assign (info->regexp, "^(?<type>[^;]*);(?<full_name>[^;]*);(?<name>[^;]*);(?<code>[^;]*);?(?<description>[^;]*);?(?<color>[^;]*);?(?<notes>[^;]*);?(?<commoditym>[^;]*);?(?<commodityn>[^;]*);?(?<hidden>[^;]*);?(?<tax>[^;]*);?(?<place_holder>[^;]*)$");

    if (g_strcmp0(name, g_strdup("radio_comma")) == 0)
        g_string_assign (info->regexp, "^(?<type>[^,]*),(?<full_name>[^,]*),(?<name>[^,]*),(?<code>[^,]*),?(?<description>[^,]*),?(?<color>[^,]*),?(?<notes>[^,]*),?(?<commoditym>[^,]*),?(?<commodityn>[^,]*),?(?<hidden>[^,]*),?(?<tax>[^,]*),?(?<place_holder>[^,]*)$");

    if (g_strcmp0(name, g_strdup("radio_semiq")) == 0)
        g_string_assign (info->regexp, "^((?<type>[^\";]*)|\"(?<type>[^\"]*)\");((?<full_name>[^\";]*)|\"(?<full_name>[^\"]*)\");((?<name>[^\";]*)|\"(?<name>[^\"]*)\");((?<code>[^\";]*)|\"(?<code>[^\"]*)\");((?<description>[^\";]*)|\"(?<description>[^\"]*)\");((?<color>[^\";]*)|\"(?<color>[^\"]*)\");((?<notes>[^\";]*)|\"(?<notes>[^\"]*)\");((?<commoditym>[^\";]*)|\"(?<commoditym>[^\"]*)\");((?<commodityn>[^\";]*)|\"(?<commodityn>[^\"]*)\");((?<hidden>[^\";]*)|\"(?<hidden>[^\"]*)\");((?<tax>[^\";]*)|\"(?<tax>[^\"]*)\");((?<place_holder>[^\";]*)|\"(?<place_holder>[^\"]*)\")$");

    if (g_strcmp0(name, g_strdup("radio_commaq")) == 0)
        g_string_assign (info->regexp, "^((?<type>[^\",]*)|\"(?<type>[^\"]*)\"),((?<full_name>[^\",]*)|\"(?<full_name>[^\"]*)\"),((?<name>[^\",]*)|\"(?<name>[^\"]*)\"),((?<code>[^\",]*)|\"(?<code>[^\"]*)\"),((?<description>[^\",]*)|\"(?<description>[^\"]*)\"),((?<color>[^\",]*)|\"(?<color>[^\"]*)\"),((?<notes>[^\",]*)|\"(?<notes>[^\"]*)\"),((?<commoditym>[^\",]*)|\"(?<commoditym>[^\"]*)\"),((?<commodityn>[^\",]*)|\"(?<commodityn>[^\"]*)\"),((?<hidden>[^\",]*)|\"(?<hidden>[^\"]*)\"),((?<tax>[^\",]*)|\"(?<tax>[^\"]*)\"),((?<place_holder>[^\",]*)|\"(?<place_holder>[^\"]*)\")$");

    if (g_strcmp0(name, g_strdup("radio_custom")) == 0)
    {
        temp = gnc_input_dialog (0, _("Adjust regular expression used for import"), _("This regular expression is used to parse the import file. Modify according to your needs.\n"), info->regexp->str);
        if (temp)
        {
            g_string_assign (info->regexp, temp);
            g_free (temp);
        }
    }

    /* Generate preview */
    gtk_list_store_clear (info->store);

    if (csv_import_read_file (info->file_name, info->regexp->str, info->store, 11 ) == MATCH_FOUND)
        gtk_widget_set_sensitive (info->header_row_spin, TRUE);
    else
        gtk_widget_set_sensitive (info->header_row_spin, FALSE);

    /* Reset Header spin to 0 */
    gtk_spin_button_set_value( GTK_SPIN_BUTTON(info->header_row_spin), 0 );
}
/**************************************************
 * csv_file_chooser_confirm_cb
 *
 * call back for ok button in file chooser widget
 **************************************************/
void
csv_import_file_chooser_confirm_cb (GtkWidget *button, CsvImportInfo *info)
{
    GtkAssistant *assistant = GTK_ASSISTANT(info->window);
    gint num = gtk_assistant_get_current_page (assistant);
    GtkWidget *page = gtk_assistant_get_nth_page (assistant, num);

    gchar *file_name;
    csv_import_result res;

    gtk_assistant_set_page_complete (assistant, page, FALSE);

    file_name = gtk_file_chooser_get_filename ( GTK_FILE_CHOOSER(info->file_chooser ));

    if (file_name)
    {
        gchar *filepath = gnc_uri_get_path ( file_name );
        gchar *filedir = g_path_get_dirname( filepath );
        info->starting_dir = g_strdup(filedir);
        g_free ( filedir );
        g_free ( filepath );

        info->file_name = g_strdup(file_name);

        // generate preview
        gtk_list_store_clear (info->store);
        res = csv_import_read_file (info->file_name, info->regexp->str, info->store, 1 );
        if (res == RESULT_OPEN_FAILED)
            gnc_error_dialog (info->window, _("The input file can not be opened."));
        else if (res == RESULT_OK)
            gtk_assistant_set_page_complete (assistant, page, TRUE);
        else if (res == MATCH_FOUND)
            gtk_assistant_set_page_complete (assistant, page, TRUE);
    }
    g_free(file_name);

    DEBUG("file_name selected is %s", info->file_name);
    DEBUG("starting directory is %s", info->starting_dir);

    /* Step to next page if page is complete */
    if(gtk_assistant_get_page_complete(assistant, page))
        gtk_assistant_set_current_page (assistant, num + 1);

}
/*******************************************************
 * csv_import_sep_cb
 *
 * call back for type of separartor required
 *******************************************************/
void csv_import_sep_cb (GtkWidget *radio, gpointer user_data)
{
    CsvImportInfo *info = user_data;
    const gchar *name;
    gchar *temp;
    gchar *sep = NULL;

    if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(radio)))
    {
        LEAVE("1st callback of pair. Defer to 2nd callback.");
        return;
    }

    name = gtk_buildable_get_name (GTK_BUILDABLE(radio));
    if (g_strcmp0 (name, "radio_semi") == 0)
        sep = ";";
    else if (g_strcmp0 (name, "radio_colon") == 0)
        sep = ":";
    else
        sep = ","; /* Use as default as well */

    create_regex (info->regexp, sep);

    if (g_strcmp0 (name, "radio_custom") == 0)
    {
        temp = gnc_input_dialog (0, _("Adjust regular expression used for import"), _("This regular expression is used to parse the import file. Modify according to your needs.\n"), info->regexp->str);
        if (temp)
        {
            g_string_assign (info->regexp, temp);
            g_free (temp);
        }
    }

    /* Generate preview */
    gtk_list_store_clear (info->store);

    if (csv_import_read_file (info->file_name, info->regexp->str, info->store, 11) == MATCH_FOUND)
        gtk_widget_set_sensitive (info->header_row_spin, TRUE);
    else
        gtk_widget_set_sensitive (info->header_row_spin, FALSE);

    /* Reset Header spin to 0 */
    gtk_spin_button_set_value (GTK_SPIN_BUTTON(info->header_row_spin), 0);
}