GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 [email protected] *
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 
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 
82 
83  /*Nested loop*/
84  commodity_list = gnc_commodity_table_get_commodities(commodity_table,
85  tmp_namespace);
86  commodity_list = g_list_first(commodity_list);
87  while ( commodity_list != NULL && retval == NULL)
88  {
89  tmp_commodity = commodity_list->data;
90  DEBUG("Looking at commodity %s", gnc_commodity_get_fullname(tmp_commodity));
91 
92  if (gnc_commodity_get_cusip(tmp_commodity) != NULL &&
93  cusip != NULL &&
94  strncmp(gnc_commodity_get_cusip(tmp_commodity), cusip, strlen(cusip)) == 0)
95  {
96  retval = tmp_commodity;
97  DEBUG("Commodity %s%s", gnc_commodity_get_fullname(retval), " matches.");
98  }
99  commodity_list = g_list_next(commodity_list);
100  }
101  /*End nested loop*/
102 
103  namespace_list = g_list_next(namespace_list);
104  }
105 
106 
107 
108 
109  g_list_free(commodity_list);
110  g_list_free(namespace_list);
111 
112  if (retval == NULL && ask_on_unknown != 0)
113  {
114  const gchar *message =
115  _("Please select a commodity to match the following exchange "
116  "specific code. Please note that the exchange code of the "
117  "commodity you select will be overwritten.");
119  NULL,
121  message,
122  cusip,
123  default_fullname,
124  default_mnemonic);
125 
126  }
127  /* There seems to be a problem here - if the matched commodity does not
128  have a cusip defined (gnc_commodity_get_cusip returns NULL) then
129  it does not get overwritten - which is not consistent with the
130  message - so Im adding it to do this. Looks like this is all
131  that was needed to fix the cash value used as stock units problem
132  for pre-defined commodities which didnt have the cusip defined! */
133  if (retval != NULL &&
134  gnc_commodity_get_cusip(retval) != NULL &&
135  cusip != NULL &&
136  (strncmp(gnc_commodity_get_cusip(retval), cusip, strlen(cusip)) != 0))
137  {
138  gnc_commodity_set_cusip(retval, cusip);
139  }
140  else if (gnc_commodity_get_cusip(retval) == NULL && cusip != NULL)
141  {
142  gnc_commodity_set_cusip(retval, cusip);
143  }
144  return retval;
145 };
const char * gnc_commodity_get_cusip(const gnc_commodity *cm)
gnc_commodity * gnc_import_select_commodity(const char *cusip, gboolean ask_on_unknown, const char *default_fullname, const char *default_mnemonic)
utility functions for the GnuCash UI
#define DEBUG(format, args...)
Definition: qoflog.h:255
Account handling public routines.
GList * gnc_commodity_table_get_namespaces(const gnc_commodity_table *table)
void gnc_commodity_set_cusip(gnc_commodity *cm, const char *cusip)
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)
const char * gnc_commodity_get_fullname(const gnc_commodity *cm)
All type declarations for the whole Gnucash engine.
CommodityList * gnc_commodity_table_get_commodities(const gnc_commodity_table *table, const char *name_space)
A Generic commodity matcher/picker.
API for Transactions and Splits (journal entries)
"select" and "new" commodity windows
const gchar * QofLogModule
Definition: qofid.h:89