GnuCash  4.8a-134-g214de30c7+
import-commodity-matcher.c
1 /********************************************************************\
2  * This program is free software; you can redistribute it and/or *
3  * modify it under the terms of the GNU General Public License as *
4  * published by the Free Software Foundation; either version 2 of *
5  * the License, or (at your option) any later version. *
6  * *
7  * This program is distributed in the hope that it will be useful, *
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
10  * GNU General Public License for more details. *
11  * *
12  * You should have received a copy of the GNU General Public License*
13  * along with this program; if not, contact: *
14  * *
15  * Free Software Foundation Voice: +1-617-542-5942 *
16  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
17  * Boston, MA 02110-1301, USA gnu@gnu.org *
18 \********************************************************************/
26 #include <config.h>
27 
28 #include <gtk/gtk.h>
29 #include <glib/gi18n.h>
30 #include <stdlib.h>
31 #include <math.h>
32 
34 #include "Account.h"
35 #include "Transaction.h"
36 #include "dialog-commodity.h"
37 #include "gnc-engine.h"
38 #include "gnc-ui-util.h"
39 
40 /********************************************************************\
41  * Constants *
42 \********************************************************************/
43 
44 
45 /********************************************************************\
46  * Constants, should ideally be defined a user preference dialog *
47 \********************************************************************/
48 
49 static QofLogModule log_module = GNC_MOD_IMPORT;
50 
51 
52 
53 gnc_commodity * gnc_import_select_commodity(const char * cusip,
54  gboolean ask_on_unknown,
55  const char * default_fullname,
56  const char * default_mnemonic)
57 {
58  const gnc_commodity_table * commodity_table = gnc_get_current_commodities ();
59  gnc_commodity * retval = NULL;
60  gnc_commodity * tmp_commodity = NULL;
61  char * tmp_namespace = NULL;
62  GList * commodity_list = NULL;
63  GList * namespace_list = NULL;
64  DEBUG("Default fullname received: %s",
65  default_fullname ? default_fullname : "(null)");
66  DEBUG("Default mnemonic received: %s",
67  default_mnemonic ? default_mnemonic : "(null)");
68 
69  g_return_val_if_fail(cusip, NULL);
70  DEBUG("Looking for commodity with exchange_code: %s", cusip);
71 
72  g_assert(commodity_table);
73  namespace_list = gnc_commodity_table_get_namespaces(commodity_table);
74 
75 
76  namespace_list = g_list_first(namespace_list);
77  while ( namespace_list != NULL && retval == NULL)
78  {
79  tmp_namespace = namespace_list->data;
80  DEBUG("Looking at namespace %s", tmp_namespace);
81  commodity_list = gnc_commodity_table_get_commodities(commodity_table,
82  tmp_namespace);
83  commodity_list = g_list_first(commodity_list);
84  while ( commodity_list != NULL && retval == NULL)
85  {
86  const char* tmp_cusip = NULL;
87  tmp_commodity = commodity_list->data;
88  DEBUG("Looking at commodity %s",
89  gnc_commodity_get_fullname(tmp_commodity));
90  tmp_cusip = gnc_commodity_get_cusip(tmp_commodity);
91  if (tmp_cusip != NULL && cusip != NULL)
92  {
93  int len = strlen(cusip) > strlen(tmp_cusip) ? strlen(cusip) :
94  strlen(tmp_cusip);
95  if (strncmp(tmp_cusip, cusip, len) == 0)
96  {
97  retval = tmp_commodity;
98  DEBUG("Commodity %s%s",
99  gnc_commodity_get_fullname(retval), " matches.");
100  }
101  }
102  commodity_list = g_list_next(commodity_list);
103  }
104  namespace_list = g_list_next(namespace_list);
105  }
106 
107  g_list_free(commodity_list);
108  g_list_free(namespace_list);
109 
110  if (retval == NULL && ask_on_unknown != 0)
111  {
112  const gchar *message =
113  _("Please select a commodity to match the following exchange "
114  "specific code. Please note that the exchange code of the "
115  "commodity you select will be overwritten.");
117  NULL,
119  message,
120  cusip,
121  default_fullname,
122  default_mnemonic);
123 
124  }
125  /* There seems to be a problem here - if the matched commodity does not
126  have a cusip defined (gnc_commodity_get_cusip returns NULL) then
127  it does not get overwritten - which is not consistent with the
128  message - so Im adding it to do this. Looks like this is all
129  that was needed to fix the cash value used as stock units problem
130  for pre-defined commodities which didn't have the cusip defined! */
131  if (retval != NULL &&
132  gnc_commodity_get_cusip(retval) != NULL &&
133  cusip != NULL &&
134  (strncmp(gnc_commodity_get_cusip(retval), cusip, strlen(cusip)) != 0))
135  {
136  gnc_commodity_set_cusip(retval, cusip);
137  }
138  else if (gnc_commodity_get_cusip(retval) == NULL && cusip != NULL)
139  {
140  gnc_commodity_set_cusip(retval, cusip);
141  }
142  return retval;
143 };
const char * gnc_commodity_get_cusip(const gnc_commodity *cm)
Retrieve the &#39;exchange code&#39; for the specified commodity.
gnc_commodity * gnc_import_select_commodity(const char *cusip, gboolean ask_on_unknown, const char *default_fullname, const char *default_mnemonic)
Must be called with a string containing a unique identifier for the commodity.
utility functions for the GnuCash UI
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
Dialog box should allow selection of anything.
Account handling public routines.
GList * gnc_commodity_table_get_namespaces(const gnc_commodity_table *table)
Return a list of all namespaces in the commodity table.
void gnc_commodity_set_cusip(gnc_commodity *cm, const char *cusip)
Set the &#39;exchange code&#39; for the specified commodity.
gnc_commodity * gnc_ui_select_commodity_modal_full(gnc_commodity *orig_sel, GtkWidget *parent, dialog_commodity_mode mode, const char *user_message, const char *cusip, const char *fullname, const char *mnemonic)
Ask the user to select a commodity from the existing set of commodities.
const char * gnc_commodity_get_fullname(const gnc_commodity *cm)
Retrieve the full name for the specified commodity.
All type declarations for the whole Gnucash engine.
CommodityList * gnc_commodity_table_get_commodities(const gnc_commodity_table *table, const char *name_space)
Return a list of all commodities in the commodity table that are in the given namespace.
A Generic commodity matcher/picker.
API for Transactions and Splits (journal entries)
"select" and "new" commodity windows