GnuCash 2.4.99
import-commodity-matcher.c
00001 /********************************************************************\
00002  * This program is free software; you can redistribute it and/or    *
00003  * modify it under the terms of the GNU General Public License as   *
00004  * published by the Free Software Foundation; either version 2 of   *
00005  * the License, or (at your option) any later version.              *
00006  *                                                                  *
00007  * This program is distributed in the hope that it will be useful,  *
00008  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00009  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00010  * GNU General Public License for more details.                     *
00011  *                                                                  *
00012  * You should have received a copy of the GNU General Public License*
00013  * along with this program; if not, contact:                        *
00014  *                                                                  *
00015  * Free Software Foundation           Voice:  +1-617-542-5942       *
00016  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00017  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
00018 \********************************************************************/
00026 #include "config.h"
00027 
00028 #include <gtk/gtk.h>
00029 #include <glib/gi18n.h>
00030 #include <stdlib.h>
00031 #include <math.h>
00032 
00033 #include "import-commodity-matcher.h"
00034 #include "Account.h"
00035 #include "Transaction.h"
00036 #include "dialog-commodity.h"
00037 #include "gnc-engine.h"
00038 #include "gnc-ui-util.h"
00039 
00040 /********************************************************************\
00041  *   Constants   *
00042 \********************************************************************/
00043 
00044 
00045 /********************************************************************\
00046  *   Constants, should ideally be defined a user preference dialog    *
00047 \********************************************************************/
00048 
00049 static QofLogModule log_module = GNC_MOD_IMPORT;
00050 
00051 
00052 
00053 gnc_commodity * gnc_import_select_commodity(const char * cusip,
00054         gboolean ask_on_unknown,
00055         const char * default_fullname,
00056         const char * default_mnemonic)
00057 {
00058     const gnc_commodity_table * commodity_table = gnc_get_current_commodities ();
00059     gnc_commodity * retval = NULL;
00060     gnc_commodity * tmp_commodity = NULL;
00061     char * tmp_namespace = NULL;
00062     GList * commodity_list = NULL;
00063     GList * namespace_list = NULL;
00064     DEBUG("Default fullname received: %s",
00065           default_fullname ? default_fullname : "(null)");
00066     DEBUG("Default mnemonic received: %s",
00067           default_mnemonic ? default_mnemonic : "(null)");
00068 
00069     g_return_val_if_fail(cusip, NULL);
00070     DEBUG("Looking for commodity with exchange_code: %s", cusip);
00071 
00072     g_assert(commodity_table);
00073     namespace_list = gnc_commodity_table_get_namespaces(commodity_table);
00074 
00075 
00076     namespace_list = g_list_first(namespace_list);
00077     while ( namespace_list != NULL && retval == NULL)
00078     {
00079         tmp_namespace = namespace_list->data;
00080         DEBUG("Looking at namespace %s", tmp_namespace);
00081 
00082 
00083         /*Nested loop*/
00084         commodity_list = gnc_commodity_table_get_commodities(commodity_table,
00085                          tmp_namespace);
00086         commodity_list  = g_list_first(commodity_list);
00087         while ( commodity_list != NULL && retval == NULL)
00088         {
00089             tmp_commodity = commodity_list->data;
00090             DEBUG("Looking at commodity %s", gnc_commodity_get_fullname(tmp_commodity));
00091 
00092             if (gnc_commodity_get_cusip(tmp_commodity) != NULL &&
00093                     cusip != NULL &&
00094                     strncmp(gnc_commodity_get_cusip(tmp_commodity), cusip, strlen(cusip)) == 0)
00095             {
00096                 retval = tmp_commodity;
00097                 DEBUG("Commodity %s%s", gnc_commodity_get_fullname(retval), " matches.");
00098             }
00099             commodity_list = g_list_next(commodity_list);
00100         }
00101         /*End nested loop*/
00102 
00103         namespace_list = g_list_next(namespace_list);
00104     }
00105 
00106 
00107 
00108 
00109     g_list_free(commodity_list);
00110     g_list_free(namespace_list);
00111 
00112     if (retval == NULL && ask_on_unknown != 0)
00113     {
00114         const gchar *message =
00115             _("Please select a commodity to match the following exchange "
00116               "specific code. Please note that the exchange code of the "
00117               "commodity you select will be overwritten.");
00118         retval = gnc_ui_select_commodity_modal_full(NULL,
00119                  NULL,
00120                  DIAG_COMM_ALL,
00121                  message,
00122                  cusip,
00123                  default_fullname,
00124                  default_mnemonic);
00125 
00126     }
00127     /* There seems to be a problem here - if the matched commodity does not
00128        have a cusip defined (gnc_commodity_get_cusip returns NULL) then
00129        it does not get overwritten - which is not consistent with the
00130        message - so Im adding it to do this.  Looks like this is all
00131        that was needed to fix the cash value used as stock units problem
00132        for pre-defined commodities which didnt have the cusip defined! */
00133     if (retval != NULL &&
00134             gnc_commodity_get_cusip(retval) != NULL &&
00135             cusip != NULL &&
00136             (strncmp(gnc_commodity_get_cusip(retval), cusip, strlen(cusip)) != 0))
00137     {
00138         gnc_commodity_set_cusip(retval, cusip);
00139     }
00140     else if (gnc_commodity_get_cusip(retval) == NULL && cusip != NULL)
00141     {
00142         gnc_commodity_set_cusip(retval, cusip);
00143     }
00144     return retval;
00145 };
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines