GnuCash  5.6-150-g038405b370+
import-commodity-matcher.cpp
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  DEBUG("Default fullname received: %s", default_fullname);
61  DEBUG("Default mnemonic received: %s", default_mnemonic);
62 
63  g_return_val_if_fail(cusip, NULL);
64  DEBUG("Looking for commodity with exchange_code: %s", cusip);
65 
66  g_assert(commodity_table);
67  GList *namespace_list = gnc_commodity_table_get_namespaces(commodity_table);
68 
69  for (GList *n = namespace_list; !retval && n; n = g_list_next (n))
70  {
71  auto ns = static_cast<const char*>(n->data);
72  DEBUG("Looking at namespace %s", ns);
73  GList *comm_list = gnc_commodity_table_get_commodities (commodity_table, ns);
74  for (GList *m = comm_list; !retval && m; m = g_list_next (m))
75  {
76  auto com = static_cast<gnc_commodity*>(m->data);
77  DEBUG("Looking at commodity %s", gnc_commodity_get_fullname (com));
78  if (!g_strcmp0 (gnc_commodity_get_cusip (com), cusip))
79  {
80  retval = com;
81  DEBUG("Commodity %s matches.", gnc_commodity_get_fullname (com));
82  }
83  }
84  g_list_free (comm_list);
85  }
86 
87  g_list_free(namespace_list);
88 
89  if (retval == NULL && ask_on_unknown != 0)
90  {
91  const gchar *message =
92  _("Please select a commodity to match the following exchange "
93  "specific code. Please note that the exchange code of the "
94  "commodity you select will be overwritten.");
96  NULL,
98  message,
99  cusip,
100  default_fullname,
101  default_mnemonic);
102 
103  }
104  /* There seems to be a problem here - if the matched commodity does not
105  have a cusip defined (gnc_commodity_get_cusip returns NULL) then
106  it does not get overwritten - which is not consistent with the
107  message - so Im adding it to do this. Looks like this is all
108  that was needed to fix the cash value used as stock units problem
109  for pre-defined commodities which didn't have the cusip defined! */
110  if (retval != NULL &&
111  gnc_commodity_get_cusip(retval) != NULL &&
112  cusip != NULL &&
113  (strncmp(gnc_commodity_get_cusip(retval), cusip, strlen(cusip)) != 0))
114  {
115  gnc_commodity_set_cusip(retval, cusip);
116  }
117  else if (gnc_commodity_get_cusip(retval) == NULL && cusip != NULL)
118  {
119  gnc_commodity_set_cusip(retval, cusip);
120  }
121  return retval;
122 }
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