r20376 - gnucash/trunk/src/import-export - Add option in OFX import to have newly encountered commodities automatically created.

Christian Stimming cstim at code.gnucash.org
Sat Mar 5 06:41:09 EST 2011


Author: cstim
Date: 2011-03-05 06:41:08 -0500 (Sat, 05 Mar 2011)
New Revision: 20376
Trac: http://svn.gnucash.org/trac/changeset/20376

Modified:
   gnucash/trunk/src/import-export/generic-import.glade
   gnucash/trunk/src/import-export/ofx/gnc-ofx-import.c
   gnucash/trunk/src/import-export/schemas/apps_gnucash_import_generic_matcher.schemas.in
Log:
Add option in OFX import to have newly encountered commodities automatically created.

Modified: gnucash/trunk/src/import-export/generic-import.glade
===================================================================
--- gnucash/trunk/src/import-export/generic-import.glade	2011-03-05 11:40:53 UTC (rev 20375)
+++ gnucash/trunk/src/import-export/generic-import.glade	2011-03-05 11:41:08 UTC (rev 20376)
@@ -730,7 +730,7 @@
     <child>
       <widget class="GtkTable" id="matcher_prefs">
         <property name="visible">True</property>
-        <property name="n_rows">9</property>
+        <property name="n_rows">10</property>
         <property name="n_columns">4</property>
         <child>
           <widget class="GtkCheckButton" id="gconf/dialogs/import/generic_matcher/enable_skip">
@@ -937,6 +937,26 @@
           </packing>
         </child>
         <child>
+          <widget class="GtkCheckButton" id="gconf/dialogs/import/generic_matcher/auto_create_commodity">
+            <property name="label" translatable="yes">Automatically create new commodities</property>
+            <property name="visible">True</property>
+            <property name="can_focus">True</property>
+            <property name="receives_default">False</property>
+            <property name="tooltip" translatable="yes">Enables the
+            automatic creation of new commodities if any
+        unknown commodity is encountered during import. Otherwise
+        the user will be asked what to do with each unknown
+        commodity.</property>
+            <property name="draw_indicator">True</property>
+          </widget>
+          <packing>
+            <property name="right_attach">4</property>
+            <property name="top_attach">8</property>
+            <property name="bottom_attach">9</property>
+            <property name="x_padding">12</property>
+          </packing>
+        </child>
+        <child>
           <placeholder/>
         </child>
         <child>

Modified: gnucash/trunk/src/import-export/ofx/gnc-ofx-import.c
===================================================================
--- gnucash/trunk/src/import-export/ofx/gnc-ofx-import.c	2011-03-05 11:40:53 UTC (rev 20375)
+++ gnucash/trunk/src/import-export/ofx/gnc-ofx-import.c	2011-03-05 11:41:08 UTC (rev 20376)
@@ -47,6 +47,7 @@
 #include "gnc-engine.h"
 #include "gnc-ui-util.h"
 #include "gnc-glib-utils.h"
+#include "core-utils/gnc-gconf-utils.h"
 
 #define GCONF_SECTION "dialogs/import/ofx"
 
@@ -60,7 +61,10 @@
 /* CS: Store the reference to the created importer gui so that the
    ofx_proc_transaction_cb can use it. */
 GNCImportMainMatcher *gnc_ofx_importer_gui = NULL;
+static gboolean auto_create_commodity = FALSE;
 
+GList *ofx_created_commodites = NULL;
+
 /*
 int ofx_proc_status_cb(struct OfxStatusData data)
 {
@@ -75,27 +79,74 @@
 
 int ofx_proc_security_cb(const struct OfxSecurityData data, void * security_user_data)
 {
-    const char* tmp_cusip = NULL;
-    const char* tmp_default_fullname = NULL;
-    const char* tmp_default_mnemonic = NULL;
+    const char* cusip = NULL;
+    const char* default_fullname = NULL;
+    const char* default_mnemonic = NULL;
 
     if (data.unique_id_valid == true)
     {
-        tmp_cusip = data.unique_id;
+        cusip = data.unique_id;
     }
     if (data.secname_valid == true)
     {
-        tmp_default_fullname = data.secname;
+        default_fullname = data.secname;
     }
     if (data.ticker_valid == true)
     {
-        tmp_default_mnemonic = data.ticker;
+        default_mnemonic = data.ticker;
     }
 
-    gnc_import_select_commodity(tmp_cusip,
-                                true,
-                                tmp_default_fullname,
-                                tmp_default_mnemonic);
+    if (auto_create_commodity)
+    {
+        gnc_commodity *commodity =
+            gnc_import_select_commodity(cusip,
+                                        FALSE,
+                                        default_fullname,
+                                        default_mnemonic);
+
+        if (!commodity)
+        {
+            QofBook *book = gnc_get_current_book();
+            gnc_quote_source *source;
+            gint source_selection = 0; // FIXME: This is just a wild guess
+            const char *commodity_namespace = NULL;
+            int fraction = 1;
+
+            if (data.unique_id_type_valid)
+            {
+                commodity_namespace = data.unique_id_type;
+            }
+
+            g_warning("Creating a new commodity, cusip=%s", cusip);
+            /* Create the new commodity */
+            commodity = gnc_commodity_new(book,
+                                          default_fullname,
+                                          commodity_namespace,
+                                          default_mnemonic,
+                                          cusip,
+                                          fraction);
+
+            /* Also set a single quote source */
+            gnc_commodity_begin_edit(commodity);
+            gnc_commodity_user_set_quote_flag (commodity, TRUE);
+            source = gnc_quote_source_lookup_by_ti (SOURCE_SINGLE, source_selection);
+            gnc_commodity_set_quote_source(commodity, source);
+            gnc_commodity_commit_edit(commodity);
+
+            /* Remember the commodity */
+            gnc_commodity_table_insert(gnc_get_current_commodities(), commodity);
+
+            /* Remember this new commodity for us as well */
+            ofx_created_commodites = g_list_prepend(ofx_created_commodites, commodity);
+        }
+    }
+    else
+    {
+        gnc_import_select_commodity(cusip,
+                                    TRUE,
+                                    default_fullname,
+                                    default_mnemonic);
+    }
     return 0;
 }
 
@@ -775,6 +826,10 @@
         /* Create the Generic transaction importer GUI. */
         gnc_ofx_importer_gui = gnc_gen_trans_list_new(NULL, NULL, FALSE, 42);
 
+        /* Look up the needed gconf options */
+        auto_create_commodity =
+            gnc_gconf_get_bool(GCONF_IMPORT_SECTION, "auto_create_commodity", NULL);
+
         /* Initialize libofx */
 
         /*ofx_set_statement_cb(libofx_context, ofx_proc_statement_cb, 0);*/
@@ -794,6 +849,18 @@
         g_free(selected_filename);
     }
 
+    if (ofx_created_commodites)
+    {
+        /* FIXME: Present some result window about the newly created
+         * commodities */
+        g_warning("Created %d new commodities during import", g_list_length(ofx_created_commodites));
+        g_list_free(ofx_created_commodites);
+        ofx_created_commodites = NULL;
+    }
+    else
+    {
+        //g_warning("No new commodities created");
+    }
 }
 
 

Modified: gnucash/trunk/src/import-export/schemas/apps_gnucash_import_generic_matcher.schemas.in
===================================================================
--- gnucash/trunk/src/import-export/schemas/apps_gnucash_import_generic_matcher.schemas.in	2011-03-05 11:40:53 UTC (rev 20375)
+++ gnucash/trunk/src/import-export/schemas/apps_gnucash_import_generic_matcher.schemas.in	2011-03-05 11:41:08 UTC (rev 20376)
@@ -158,5 +158,20 @@
       </locale>
     </schema>
 
+    <schema>
+      <key>/schemas/apps/gnucash/dialogs/import/generic_matcher/auto_create_commodity</key>
+      <applyto>/apps/gnucash/dialogs/import/generic_matcher/auto_create_commodity</applyto>
+      <owner>gnucash</owner>
+      <type>bool</type>
+      <default>FALSE</default>
+      <locale name="C">
+        <short>Automatically create new commodities</short>
+        <long>Enables the automatic creation of new commodities if any
+        unknown commodity is encountered during import. Otherwise
+        the user will be asked what to do with each unknown
+        commodity.</long>
+      </locale>
+    </schema>
+
   </schemalist>
 </gconfschemafile>



More information about the gnucash-changes mailing list