GnuCash 2.3.0
Data Structures | Defines | Enumerations | Functions
gnc-csv-import.c File Reference

CSV Import GUI code. More...

#include "config.h"
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <goffice/gtk/go-charmap-sel.h>
#include "import-account-matcher.h"
#include "import-main-matcher.h"
#include "gnc-file.h"
#include "gnc-ui-util.h"
#include "gnc-glib-utils.h"
#include "gnc-gui-query.h"
#include "dialog-utils.h"
#include "gnc-csv-import.h"
#include "gnc-csv-model.h"
#include "gnc-csv-gnumeric-popup.h"

Go to the source code of this file.

Data Structures

struct  GncCsvPreview

Defines

#define GCONF_SECTION   "dialogs/import/csv"
#define MIN_COL_WIDTH   70

Enumerations

enum  SEP_BUTTON_TYPES {
  SEP_SPACE, SEP_TAB, SEP_COMMA, SEP_COLON,
  SEP_SEMICOLON, SEP_HYPHEN, SEP_NUM_OF_TYPES, SEP_SPACE,
  SEP_TAB, SEP_COMMA, SEP_COLON, SEP_SEMICOLON,
  SEP_HYPHEN, SEP_NUM_OF_TYPES
}
enum  {
  CONTEXT_STF_IMPORT_MERGE_LEFT = 1, CONTEXT_STF_IMPORT_MERGE_RIGHT = 2, CONTEXT_STF_IMPORT_SPLIT = 3, CONTEXT_STF_IMPORT_WIDEN = 4,
  CONTEXT_STF_IMPORT_NARROW = 5
}

Functions

void gnc_file_csv_import (void)

Detailed Description

CSV Import GUI code.

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

Definition in file gnc-csv-import.c.


Enumeration Type Documentation

Enumeration for separator checkbutton types. These are the different types of checkbuttons that the user can click to configure separators in a delimited file.

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

                      {SEP_SPACE, SEP_TAB, SEP_COMMA, SEP_COLON, SEP_SEMICOLON, SEP_HYPHEN,
                       SEP_NUM_OF_TYPES
                      };

Function Documentation

void gnc_file_csv_import ( void  )

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