r16282 - gnucash/branches/csv-import - Fixed some bugs, attempted to get STF code working with goffice 0.2
Benjamin Sperisen
lasindi at cvs.gnucash.org
Sun Jul 8 16:21:50 EDT 2007
Author: lasindi
Date: 2007-07-08 16:21:49 -0400 (Sun, 08 Jul 2007)
New Revision: 16282
Trac: http://svn.gnucash.org/trac/changeset/16282
Modified:
gnucash/branches/csv-import/lib/stf/stf-parse.c
gnucash/branches/csv-import/src/import-export/csv/gnc-csv-import.c
gnucash/branches/csv-import/src/import-export/csv/gnc-csv-model.c
gnucash/branches/csv-import/src/import-export/csv/gnc-csv-preview-dialog.glade
Log:
Fixed some bugs, attempted to get STF code working with goffice 0.2
Modified: gnucash/branches/csv-import/lib/stf/stf-parse.c
===================================================================
--- gnucash/branches/csv-import/lib/stf/stf-parse.c 2007-07-08 18:42:22 UTC (rev 16281)
+++ gnucash/branches/csv-import/lib/stf/stf-parse.c 2007-07-08 20:21:49 UTC (rev 16282)
@@ -43,7 +43,6 @@
/* #include "gutils.h" */
/* #include "parse-util.h" */
#include <goffice/utils/go-glib-extras.h>
-#include <goffice/utils/go-locale.h>
#include <goffice/utils/go-format.h>
#include <stdlib.h>
@@ -1343,7 +1342,9 @@
GPtrArray *lines;
int tabcount;
int sepcount;
- gunichar sepchar = go_locale_get_arg_sep ();
+ /* TODO In the future, use the goffice 0.3. */
+ /* gunichar sepchar = go_locale_get_arg_sep (); */
+ gunichar sepchar = ',';
g_return_val_if_fail (data != NULL, NULL);
@@ -1368,8 +1369,10 @@
* The order is mostly random, although ' ' and '!' which
* could very easily occur in text are put last.
*/
+ /* TODO Replace with the 0.3 goffice call in the future. */
if (count_character (lines, (c = sepchar), 0.5) > 0 ||
- count_character (lines, (c = go_locale_get_col_sep ()), 0.5) > 0 ||
+ /* count_character (lines, (c = go_locale_get_col_sep ()), 0.5) > 0 || */
+ count_character (lines, (c = ','), 0.5) > 0 ||
count_character (lines, (c = ':'), 0.5) > 0 ||
count_character (lines, (c = ','), 0.5) > 0 ||
count_character (lines, (c = ';'), 0.5) > 0 ||
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 18:42:22 UTC (rev 16281)
+++ gnucash/branches/csv-import/src/import-export/csv/gnc-csv-import.c 2007-07-08 20:21:49 UTC (rev 16282)
@@ -539,6 +539,7 @@
((GPtrArray*)(preview->parse_data->orig_lines->pdata[i]))->pdata[j],
-1);
}
+ error_lines = g_list_next(error_lines);
}
}
else /* Otherwise, put in all of the data. */
@@ -559,7 +560,8 @@
for(i = 0; i < ncols; i++)
{
gtk_list_store_set(ctstore, &iter, 2*i, cstores[i], 2*i+1,
- "None", -1);
+ column_type_strs[(int)(preview->parse_data->column_types->data[i])],
+ -1);
}
/* Insert columns into the data and column type treeviews. */
@@ -664,6 +666,7 @@
preview->approved = FALSE; /* This is FALSE until the user clicks "OK". */
/* Wait until the user clicks "OK" or "Cancel". */
+ gnc_csv_preview_treeview(preview, TRUE);
gtk_dialog_run(GTK_DIALOG(preview->dialog));
if(preview->approved)
@@ -751,13 +754,8 @@
* user gives up. */
while(!((parse_data->error_lines == NULL) || user_canceled))
{
- /* TODO remove printfs */
- printf("start %d %d\n", g_list_length(parse_data->transactions),
- g_list_length(parse_data->error_lines));
- gnc_csv_preview_errors(preview);
- user_canceled = gnc_parse_to_trans(parse_data, account, TRUE);
- printf("end %d %d\n", g_list_length(parse_data->transactions),
- g_list_length(parse_data->error_lines));
+ user_canceled = gnc_csv_preview_errors(preview);
+ gnc_parse_to_trans(parse_data, account, TRUE);
}
/* Create the genereic transaction importer GUI. */
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 18:42:22 UTC (rev 16281)
+++ gnucash/branches/csv-import/src/import-export/csv/gnc-csv-model.c 2007-07-08 20:21:49 UTC (rev 16282)
@@ -245,6 +245,7 @@
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.");
@@ -341,22 +342,30 @@
}
/** A struct encaspulating a property of a transaction. */
-/* TODO Comment */
typedef struct
{
- gboolean essential;
- int type;
- void* value;
- int date_format;
+ gboolean essential; /**< TRUE if every transaction needs this property */
+ int type; /**< A value from the GncCsvColumnType enum except
+ * GNC_CSV_NONE and GNC_CSV_NUM_COL_TYPES */
+ void* value; /**< Pointer to the data that will be used to configure a transaction */
+ /* TODO Try coming up with a more elegant way than storing this for
+ * every transaction safely. */
+ int date_format; /**< The format for parsing dates */
} TransProperty;
+/** Constructor TransProperty.
+ * @param type The type of the new property (see TransProperty.type for possible values)
+ * @param date_format The format used for parsing dates (index in date_format_internal)
+ */
static TransProperty* trans_property_new(int type, int date_format)
{
TransProperty* prop = g_malloc(sizeof(TransProperty));
prop->type = type;
prop->date_format = date_format;
+
switch(type)
{
+ /* Only the "Date" and "Amount" properties are essential. */
case GNC_CSV_DATE:
case GNC_CSV_AMOUNT:
prop->essential = TRUE;
@@ -372,6 +381,9 @@
{
switch(prop->type)
{
+ /* The types for "Date" and "Amount" (time_t and gnc_numeric,
+ * respectively) are typically not pointed to, we have to free
+ * them, unlike types like char* ("Description"). */
case GNC_CSV_DATE:
case GNC_CSV_AMOUNT:
g_free(prop->value);
@@ -389,6 +401,8 @@
*/
static gboolean trans_property_set(TransProperty* prop, char* str)
{
+ char* endptr;
+ double value;
switch(prop->type)
{
case GNC_CSV_DATE:
@@ -401,10 +415,15 @@
return TRUE;
case GNC_CSV_AMOUNT:
+ value = strtod(str, &endptr);
prop->value = g_malloc(sizeof(gnc_numeric));
- *((gnc_numeric*)(prop->value)) = double_to_gnc_numeric(atof(str), 1,
- GNC_RND_ROUND);
- /* TODO error handling */
+
+ /* If this isn't a valid numeric string, this is an error. */
+ if(endptr != str + strlen(str))
+ return FALSE;
+
+ *((gnc_numeric*)(prop->value)) = double_to_gnc_numeric(value, 1,
+ GNC_RND_ROUND);
return TRUE;
}
return FALSE; /* We should never actually get here. */
@@ -412,7 +431,7 @@
/** Creates a transaction from a list of "TransProperty"s.
*/
-static Transaction* trans_from_trans_properties(GList* properties, Account* account)
+static Transaction* trans_properties_to_trans(GList* properties, Account* account)
{
Transaction* trans;
Split* split;
@@ -481,9 +500,7 @@
{
int i, j;
GArray* column_types = parse_data->column_types;
- GNCBook* book = gnc_account_get_book(account);
GList *error_lines, *begin_error_lines;
- gnc_commodity* currency = xaccAccountGetCommodity(account);
GList* last_transaction;
/* Free parse_data->error_lines and parse_data->transactions if they
@@ -497,19 +514,6 @@
g_list_free(parse_data->error_lines);
}
parse_data->error_lines = NULL;
- if(parse_data->transactions != NULL)
- {
- GList* transactions = parse_data->transactions;
- /* We have to free the GncCsvTransLine's that are at each node in
- * the list before freeing the entire list. */
- do
- {
- g_free(transactions->data);
- transactions = g_list_next(transactions);
- } while(transactions != NULL);
- g_list_free(parse_data->transactions);
- parse_data->transactions = NULL;
- }
if(redo_errors) /* If we're looking only at error data ... */
{
@@ -528,28 +532,13 @@
}
while(i < parse_data->orig_lines->len)
{
- Transaction* trans = xaccMallocTransaction(book);
GPtrArray* line = parse_data->orig_lines->pdata[i];
- Split* split;
- /* This flag is FALSE if there are any errors in this row. */
- /* TODO This flag isn't used yet. */
- gboolean noErrors = TRUE;
+ /* This flag is TRUE if there are any errors in this row. */
+ gboolean errors = FALSE;
+ GList* properties = NULL;
+ GList* properties_begin;
+ Transaction* trans;
- /* By the time we traverse the column, essential_properties_left
- * should be 0; if it isn't the row is considered to have an
- * error. Each time an "essential" property is set, we subtract 1
- * from it. At the moment, the only essential properties are
- * "Date" and "Amount". */
- unsigned int essential_properties_left = 2;
-
- /* The data types that are used to create transactions */
- time_t date;
- const char* description;
- gnc_numeric amount;
-
- xaccTransBeginEdit(trans);
- xaccTransSetCurrency(trans, currency);
-
/* TODO There should eventually be a specification of what errors
* actually occurred, not just that one happened. */
@@ -559,43 +548,51 @@
if(column_types->data[j] != GNC_CSV_NONE)
{
/* Affect the transaction appropriately. */
- if(column_types->data[j] == GNC_CSV_DATE)
+ TransProperty* property = trans_property_new(column_types->data[j],
+ parse_data->date_format);
+ gboolean succeeded = trans_property_set(property, line->pdata[j]);
+ if(succeeded)
{
- date = parse_date(line->pdata[j], parse_data->date_format);
- if(date != -1)
- {
- xaccTransSetDatePostedSecs(trans, date);
- essential_properties_left--;
- }
- else
- noErrors = FALSE;
+ properties = g_list_append(properties, property);
}
- else if(column_types->data[j] == GNC_CSV_DESCRIPTION)
+ else
{
- description = line->pdata[j];
- xaccTransSetDescription(trans, description);
+ errors = TRUE;
+ trans_property_free(property);
+ break;
}
- else if(column_types->data[j] == GNC_CSV_AMOUNT)
- {
- amount = double_to_gnc_numeric(atof(line->pdata[j]), 1,
- GNC_RND_ROUND);
- split = xaccMallocSplit(book);
- xaccSplitSetAccount(split, account);
- xaccSplitSetParent(split, trans);
- xaccSplitSetAmount(split, amount);
- xaccSplitSetValue(split, amount);
- xaccSplitSetAction(split, "Deposit");
- essential_properties_left--;
- }
}
}
/* If we had success, add the transaction to parse_data->transaction. */
- if(noErrors && essential_properties_left == 0)
+ if(!errors)
{
+ trans = trans_properties_to_trans(properties, account);
+ errors = trans == NULL;
+ }
+
+ /* We need to free the properties and the properties list. */
+ properties_begin = properties;
+ while(properties != NULL)
+ {
+ trans_property_free((TransProperty*)(properties->data));
+ properties = g_list_next(properties);
+ }
+ g_list_free(properties_begin);
+
+ /* If there was no success, add this line to parse_data->error_lines. */
+ if(errors)
+ {
+ parse_data->error_lines = g_list_append(parse_data->error_lines,
+ GINT_TO_POINTER(i));
+ }
+ else
+ {
GncCsvTransLine* trans_line = g_malloc(sizeof(GncCsvTransLine));
- trans_line->line_no = i;
+
trans_line->trans = trans;
+ trans_line->line_no = i;
+
if(redo_errors)
{
while(last_transaction != NULL &&
@@ -611,18 +608,11 @@
parse_data->transactions = g_list_append(parse_data->transactions, trans_line);
}
}
- /* If there was no success, add this line to
- * parse_data->error_lines and free the transaction. */
- else
- {
- parse_data->error_lines = g_list_append(parse_data->error_lines,
- GINT_TO_POINTER(i));
- xaccTransDestroy(trans);
- }
+ /* Increment to the next row. */
if(redo_errors)
{
- /* Move to the next error line. */
+ /* Move to the next error line in the list. */
error_lines = g_list_next(error_lines);
if(error_lines == NULL)
i = parse_data->orig_lines->len; /* Don't continue the for loop. */
Modified: gnucash/branches/csv-import/src/import-export/csv/gnc-csv-preview-dialog.glade
===================================================================
--- gnucash/branches/csv-import/src/import-export/csv/gnc-csv-preview-dialog.glade 2007-07-08 18:42:22 UTC (rev 16281)
+++ gnucash/branches/csv-import/src/import-export/csv/gnc-csv-preview-dialog.glade 2007-07-08 20:21:49 UTC (rev 16282)
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
-<!--Generated with glade3 3.2.0 on Sun Jul 8 01:33:26 2007 by lasindi at pi-->
+<!--Generated with glade3 3.2.0 on Sun Jul 8 10:39:03 2007 by lasindi at pi-->
<glade-interface>
<widget class="GtkDialog" id="dialog">
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
@@ -85,7 +85,7 @@
<property name="visible">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<child>
- <widget class="GtkRadioButton" id="radiobutton1">
+ <widget class="GtkRadioButton" id="radiobutton">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
@@ -102,7 +102,7 @@
<property name="label" translatable="yes">Fixed-Width</property>
<property name="active">True</property>
<property name="draw_indicator">True</property>
- <property name="group">radiobutton1</property>
+ <property name="group">radiobutton</property>
</widget>
<packing>
<property name="position">1</property>
@@ -171,7 +171,7 @@
<property name="column_spacing">3</property>
<property name="row_spacing">3</property>
<child>
- <widget class="GtkEntry" id="custom_entry1">
+ <widget class="GtkEntry" id="custom_entry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
@@ -186,7 +186,7 @@
</packing>
</child>
<child>
- <widget class="GtkCheckButton" id="custom_cbutton1">
+ <widget class="GtkCheckButton" id="custom_cbutton">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
@@ -201,7 +201,7 @@
</packing>
</child>
<child>
- <widget class="GtkCheckButton" id="hyphen_cbutton1">
+ <widget class="GtkCheckButton" id="hyphen_cbutton">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
@@ -218,7 +218,7 @@
</packing>
</child>
<child>
- <widget class="GtkCheckButton" id="semicolon_cbutton1">
+ <widget class="GtkCheckButton" id="semicolon_cbutton">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
@@ -235,7 +235,7 @@
</packing>
</child>
<child>
- <widget class="GtkCheckButton" id="colon_cbutton1">
+ <widget class="GtkCheckButton" id="colon_cbutton">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
@@ -250,7 +250,7 @@
</packing>
</child>
<child>
- <widget class="GtkCheckButton" id="comma_cbutton1">
+ <widget class="GtkCheckButton" id="comma_cbutton">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
@@ -265,7 +265,7 @@
</packing>
</child>
<child>
- <widget class="GtkCheckButton" id="tab_cbutton1">
+ <widget class="GtkCheckButton" id="tab_cbutton">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
@@ -279,7 +279,7 @@
</packing>
</child>
<child>
- <widget class="GtkCheckButton" id="space_cbutton1">
+ <widget class="GtkCheckButton" id="space_cbutton">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
@@ -419,6 +419,7 @@
<property name="enable_grid_lines">GTK_TREE_VIEW_GRID_LINES_BOTH</property>
</widget>
<packing>
+ <property name="expand">False</property>
<property name="position">8</property>
</packing>
</child>
More information about the gnucash-changes
mailing list