r16283 - gnucash/branches/csv-import/src/import-export/csv - Handled errors in guessing encoding

Benjamin Sperisen lasindi at cvs.gnucash.org
Mon Jul 9 13:09:39 EDT 2007


Author: lasindi
Date: 2007-07-09 13:09:37 -0400 (Mon, 09 Jul 2007)
New Revision: 16283
Trac: http://svn.gnucash.org/trac/changeset/16283

Modified:
   gnucash/branches/csv-import/src/import-export/csv/gnc-csv-import.c
   gnucash/branches/csv-import/src/import-export/csv/gnc-csv-model.c
Log:
Handled errors in guessing encoding


Modified: gnucash/branches/csv-import/src/import-export/csv/gnc-csv-import.c
===================================================================
--- gnucash/branches/csv-import/src/import-export/csv/gnc-csv-import.c	2007-07-08 20:21:49 UTC (rev 16282)
+++ gnucash/branches/csv-import/src/import-export/csv/gnc-csv-import.c	2007-07-09 17:09:37 UTC (rev 16283)
@@ -172,7 +172,7 @@
        gnc_csv_parse(preview->parse_data, FALSE, &error))
     {
       /* If it fails, change back to the old encoding. */
-      gnc_error_dialog(NULL, "Invalid encoding selected");
+      gnc_error_dialog(NULL, _("Invalid encoding selected"));
       preview->encoding_selected_called = FALSE;
       go_charmap_sel_set_encoding(selector, previous_encoding);
       return;
@@ -188,10 +188,10 @@
 }
 
 /* This array contains all of the different strings for different column types. */
-static char* column_type_strs[GNC_CSV_NUM_COL_TYPES] = {"None",
-                                                        "Date",
-                                                        "Description",
-                                                        "Amount"};
+static char* column_type_strs[GNC_CSV_NUM_COL_TYPES] = {N_("None"),
+                                                        N_("Date"),
+                                                        N_("Description"),
+                                                        N_("Amount")};
 
 /** Event handler for selecting a new date format.
  * @param format_selector The combo box for selecting date formats
@@ -234,7 +234,7 @@
     for(type = 0; type < GNC_CSV_NUM_COL_TYPES; type++)
     {
       /* ... we find one that matches with what's in the column. */
-      if(!strcmp(contents, column_type_strs[type]))
+      if(!strcmp(contents, _(column_type_strs[type])))
       {
         /* Set the column_types array appropriately and quit. */
         column_types->data[i] = type;
@@ -337,7 +337,7 @@
       {
         /* ... set this column to the "None" type. (We can't allow duplicate types.) */
         gtk_list_store_set(GTK_LIST_STORE(store), &iter, 2*i+1,
-                           column_type_strs[GNC_CSV_NONE], -1);
+                           _(column_type_strs[GNC_CSV_NONE]), -1);
       }
     }
     else /* If this is the column that was edited ... */
@@ -414,7 +414,7 @@
   preview->date_format_combo = GTK_COMBO_BOX(gtk_combo_box_new_text());
   for(i = 0; i < num_date_formats; i++)
   {
-    gtk_combo_box_append_text(preview->date_format_combo, date_format_user[i]);
+    gtk_combo_box_append_text(preview->date_format_combo, _(date_format_user[i]));
   }
   gtk_combo_box_set_active(preview->date_format_combo, 0);
   g_signal_connect(G_OBJECT(preview->date_format_combo), "changed",
@@ -502,26 +502,30 @@
     for(j = 0; j < GNC_CSV_NUM_COL_TYPES; j++)
     {
       gtk_list_store_append(cstores[i], &iter);
-      gtk_list_store_set(cstores[i], &iter, 0, column_type_strs[j], -1);
+      gtk_list_store_set(cstores[i], &iter, 0, _(column_type_strs[j]), -1);
     }
   }
 
   if(notEmpty)
   {
     GList *children, *children_begin;
-    int size;
+    GList *tv_columns, *tv_columns_begin, *ctv_columns, *ctv_columns_begin;
+    tv_columns = tv_columns_begin = gtk_tree_view_get_columns(preview->treeview);
+    ctv_columns = ctv_columns_begin = gtk_tree_view_get_columns(preview->ctreeview);
     /* Clear out exisiting columns in preview->treeview. */
-    do
+    while(tv_columns != NULL)
     {
-      GtkTreeViewColumn* col = gtk_tree_view_get_column(preview->treeview, 0);
-      size = gtk_tree_view_remove_column(preview->treeview, col);
-    } while(size);
+      gtk_tree_view_remove_column(preview->treeview, GTK_TREE_VIEW_COLUMN(tv_columns->data));
+      tv_columns = g_list_next(tv_columns);
+    }
     /* Do the same in preview->ctreeview. */
-    do
+    while(ctv_columns != NULL)
     {
-      GtkTreeViewColumn* col = gtk_tree_view_get_column(preview->ctreeview, 0);
-      size = gtk_tree_view_remove_column(preview->ctreeview, col);
-    } while(size);
+      gtk_tree_view_remove_column(preview->ctreeview, GTK_TREE_VIEW_COLUMN(ctv_columns->data));
+      ctv_columns = g_list_next(ctv_columns);
+    }
+    g_list_free(tv_columns_begin);
+    g_list_free(ctv_columns_begin);
   }
   
   /* Fill the data treeview with data from the file. */
@@ -560,7 +564,7 @@
   for(i = 0; i < ncols; i++)
   {
     gtk_list_store_set(ctstore, &iter, 2*i, cstores[i], 2*i+1,
-                       column_type_strs[(int)(preview->parse_data->column_types->data[i])],
+                       _(column_type_strs[(int)(preview->parse_data->column_types->data[i])]),
                        -1);
   }
 
@@ -657,7 +661,7 @@
   gtk_image_get_stock(instructions_image, &name, &size);
   gtk_image_set_from_stock(instructions_image, GTK_STOCK_DIALOG_ERROR, size);
   gtk_label_set_text(instructions_label,
-                     "The rows displayed below had errors. You can attempt to correct these errors by changing the configuration.");
+                     _("The rows displayed below had errors. You can attempt to correct these errors by changing the configuration."));
   gtk_widget_show(GTK_WIDGET(instructions_image));
   gtk_widget_show(GTK_WIDGET(instructions_label));
 
@@ -696,7 +700,7 @@
     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. */
+    GList* transactions; /* A list of the transactions we create */
     GncCsvParseData* parse_data;
     GncCsvPreview* preview;
     
@@ -717,6 +721,9 @@
         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))

Modified: gnucash/branches/csv-import/src/import-export/csv/gnc-csv-model.c
===================================================================
--- gnucash/branches/csv-import/src/import-export/csv/gnc-csv-model.c	2007-07-08 20:21:49 UTC (rev 16282)
+++ gnucash/branches/csv-import/src/import-export/csv/gnc-csv-model.c	2007-07-09 17:09:37 UTC (rev 16283)
@@ -2,6 +2,7 @@
 
 #include "gnc-book.h"
 
+#include <glib/gi18n.h>
 #include <goffice/utils/go-glib-extras.h>
 
 #include <string.h>
@@ -19,14 +20,14 @@
 
 const int num_date_formats = 8;
 /* A set of date formats that the user sees. */
-const gchar* date_format_user[] = {"yyyy/mm/dd",
-                                   "yy/mm/dd",
-                                   "dd/mm/yyyy",
-                                   "dd/mm/yy",
-                                   "dd/mm/yyyy",
-                                   "dd/mm/yy",
-                                   "mm/dd/yyyy",
-                                   "mm/dd/yy"};
+const gchar* date_format_user[] = {N_("yyyy/mm/dd"),
+                                   N_("yy/mm/dd"),
+                                   N_("dd/mm/yyyy"),
+                                   N_("dd/mm/yy"),
+                                   N_("dd/mm/yyyy"),
+                                   N_("dd/mm/yy"),
+                                   N_("mm/dd/yyyy"),
+                                   N_("mm/dd/yy")};
 
 /* Matching formats for date_format_user to be used with strptime. */
 const gchar* date_format_internal[] = {"%Y/%m/%d",
@@ -245,7 +246,6 @@
   guess_enc = go_guess_encoding((const char*)(parse_data->raw_str.begin),
                                 (size_t)(parse_data->raw_str.end - parse_data->raw_str.begin),
                                 "UTF-8", NULL);
-  guess_enc = NULL; /* TODO Get rid of, testing */
   if(guess_enc == NULL)
   {
     g_set_error(error, 0, GNC_CSV_ENCODING_ERR, "Unknown encoding.");
@@ -283,14 +283,24 @@
   /* max_cols is the number of columns in the row with the most columns. */
   int i, max_cols = 0;
 
-  /* Do the actual parsing. */
-  /* TODO: This size might have to change ... because I'm not exactly
-   * sure what it's for. ... */
-  chunk = g_string_chunk_new(100);
-  parse_data->orig_lines = stf_parse_general(parse_data->options, chunk,
-                                             parse_data->file_str.begin,
-                                             parse_data->file_str.end);
-  g_string_chunk_free(chunk);
+  /* If everything is fine ... */
+  if(parse_data->file_str.begin != NULL)
+  {
+    /* Do the actual parsing. */
+    /* TODO: This size might have to change ... because I'm not exactly
+     * sure what it's for. ... */
+    chunk = g_string_chunk_new(100);
+    parse_data->orig_lines = stf_parse_general(parse_data->options, chunk,
+                                               parse_data->file_str.begin,
+                                               parse_data->file_str.end);
+    g_string_chunk_free(chunk);
+  }
+  /* If we couldn't get the encoding right, we just want an empty array. */
+  else
+  {
+    parse_data->orig_lines = g_ptr_array_new();
+  }
+
   /* If it failed, generate an error. */
   if(parse_data->orig_lines == NULL)
   {
@@ -304,7 +314,6 @@
     if(max_cols < ((GPtrArray*)(parse_data->orig_lines->pdata[i]))->len)
       max_cols = ((GPtrArray*)(parse_data->orig_lines->pdata[i]))->len;
   }
-  g_debug("max_cols %d\n", max_cols);
 
   if(guessColTypes)
   {



More information about the gnucash-changes mailing list