GnuCash 2.3.0
Functions
gnc-csv-import.h File Reference

CSV import GUI. More...

Go to the source code of this file.

Functions

void gnc_file_csv_import (void)

Detailed Description

CSV import GUI.

gnc-csv-import.h

Author:
Copyright (c) 2007 Benny Sperisen <lasindi@gmail.com>

Definition in file gnc-csv-import.h.


Function Documentation

void gnc_file_csv_import ( void  )

The gnc_file_csv_import() will let the user select a CSV/Fixed-Width file to open, select an account to import it to, and import the transactions into the account. It also allows the user to configure how the file is parsed.

Lets the user import a CSV/Fixed-Width file.

Definition at line 1106 of file gnc-csv-import.c.

{
    /* The name of the file the user selected. */
    char* selected_filename;
    /* The default directory for the user to select files. */
    char* default_dir = gnc_get_default_directory(GCONF_SECTION);
    /* The generic GUI for importing transactions. */
    GNCImportMainMatcher* gnc_csv_importer_gui = NULL;

    /* Let the user select a file. */
    selected_filename = gnc_file_dialog(_("Select an CSV/Fixed-Width file to import"),
                                        NULL, default_dir, GNC_FILE_DIALOG_IMPORT);
    g_free(default_dir); /* We don't need default_dir anymore. */

    /* If the user actually selected a file ... */
    if (selected_filename != NULL)
    {
        int i, user_canceled = 0;
        Account* account; /* The account the user will select */
        GError* error = NULL;
        GList* transactions; /* A list of the transactions we create */
        GncCsvParseData* parse_data;
        GncCsvPreview* preview;

        /* Remember the directory of the selected file as the default. */
        default_dir = g_path_get_dirname(selected_filename);
        gnc_set_default_directory(GCONF_SECTION, default_dir);
        g_free(default_dir);

        /* Load the file into parse_data. */
        parse_data = gnc_csv_new_parse_data();
        if (gnc_csv_load_file(parse_data, selected_filename, &error))
        {
            /* If we couldn't load the file ... */
            gnc_error_dialog(NULL, "%s", error->message);
            if (error->code == GNC_CSV_FILE_OPEN_ERR)
            {
                gnc_csv_parse_data_free(parse_data);
                g_free(selected_filename);
                return;
            }
            /* If we couldn't guess the encoding, we are content with just
             * displaying an error message and move on with a blank
             * display. */
        }
        /* Parse the data. */
        if (gnc_csv_parse(parse_data, TRUE, &error))
        {
            /* If we couldn't parse the data ... */
            gnc_error_dialog(NULL, "%s", error->message);
        }

        /* Preview the data. */
        preview = gnc_csv_preview_new();
        if (gnc_csv_preview(preview, parse_data))
        {
            /* If the user clicked "Cancel", free everything and quit. */
            gnc_csv_preview_free(preview);
            gnc_csv_parse_data_free(parse_data);
            g_free(selected_filename);
            return;
        }

        /* Let the user select an account to put the transactions in. */
        account = gnc_import_select_account(NULL, NULL, 1, NULL, NULL, 0, NULL, NULL);
        if (account == NULL) /* Quit if the user canceled. */
        {
            gnc_csv_preview_free(preview);
            gnc_csv_parse_data_free(parse_data);
            g_free(selected_filename);
            return;
        }

        /* Create transactions from the parsed data. */
        gnc_csv_parse_to_trans(parse_data, account, FALSE);

        /* If there are errors, let the user try and eliminate them by
         * previewing them. Repeat until either there are no errors or the
         * user gives up. */
        while (!((parse_data->error_lines == NULL) || user_canceled))
        {
            user_canceled = gnc_csv_preview_errors(preview);
            gnc_csv_parse_to_trans(parse_data, account, TRUE);
        }

        /* Create the genereic transaction importer GUI. */
        gnc_csv_importer_gui = gnc_gen_trans_list_new(NULL, NULL, FALSE, 42);

        /* Get the list of the transactions that were created. */
        transactions = parse_data->transactions;
        /* Copy all of the transactions to the importer GUI. */
        while (transactions != NULL)
        {
            GncCsvTransLine* trans_line = transactions->data;
            gnc_gen_trans_list_add_trans(gnc_csv_importer_gui,
                                         trans_line->trans);
            transactions = g_list_next(transactions);
        }
        /* Let the user load those transactions into the account, so long
         * as there is at least one transaction to be loaded. */
        if (parse_data->transactions != NULL)
            gnc_gen_trans_list_run(gnc_csv_importer_gui);
        else
            gnc_gen_trans_list_delete(gnc_csv_importer_gui);

        /* Free the memory we allocated. */
        gnc_csv_preview_free(preview);
        gnc_csv_parse_data_free(parse_data);
        g_free(selected_filename);
    }
}
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines