Branch data Line data Source code
1 : : /********************************************************************
2 : : * gnc-commodity.c -- api for tradable commodities (incl. currency) *
3 : : * Copyright (C) 2000 Bill Gribble *
4 : : * Copyright (C) 2001,2003 Linas Vepstas <linas@linas.org> *
5 : : * Copyright (c) 2006 David Hampton <hampton@employees.org> *
6 : : * *
7 : : * This program is free software; you can redistribute it and/or *
8 : : * modify it under the terms of the GNU General Public License as *
9 : : * published by the Free Software Foundation; either version 2 of *
10 : : * the License, or (at your option) any later version. *
11 : : * *
12 : : * This program is distributed in the hope that it will be useful, *
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 : : * GNU General Public License for more details. *
16 : : * *
17 : : * You should have received a copy of the GNU General Public License*
18 : : * along with this program; if not, contact: *
19 : : * *
20 : : * Free Software Foundation Voice: +1-617-542-5942 *
21 : : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
22 : : * Boston, MA 02110-1301, USA gnu@gnu.org *
23 : : * *
24 : : *******************************************************************/
25 : :
26 : : #include <config.h>
27 : :
28 : : #include <glib.h>
29 : : #include <glib/gi18n.h>
30 : : #include <ctype.h>
31 : : #include <limits.h>
32 : : #include <string.h>
33 : : #include <stdio.h>
34 : : #include <stdlib.h>
35 : : #include <regex.h>
36 : : #include <qofinstance-p.h>
37 : :
38 : : #include "gnc-commodity.hpp"
39 : : #include "gnc-commodity.h"
40 : : #include "gnc-locale-utils.h"
41 : : #include "gnc-prefs.h"
42 : : #include "guid.h"
43 : : #include "qofinstance.h"
44 : :
45 : : #include <list>
46 : : #include <unordered_map>
47 : :
48 : : static QofLogModule log_module = GNC_MOD_COMMODITY;
49 : :
50 : : /* Parts per unit is nominal, i.e. number of 'partname' units in
51 : : * a 'unitname' unit. fraction is transactional, i.e. how many
52 : : * of the smallest-transactional-units of the currency are there
53 : : * in a 'unitname' unit. */
54 : :
55 : : enum
56 : : {
57 : : PROP_0,
58 : : PROP_NAMESPACE, /* Table */
59 : : PROP_FULL_NAME, /* Table */
60 : : PROP_MNEMONIC, /* Table */
61 : : PROP_PRINTNAME, /* Constructed */
62 : : PROP_CUSIP, /* Table */
63 : : PROP_FRACTION, /* Table */
64 : : PROP_UNIQUE_NAME, /* Constructed */
65 : : PROP_QUOTE_FLAG, /* Table */
66 : : PROP_QUOTE_SOURCE, /* Table */
67 : : PROP_QUOTE_TZ, /* Table */
68 : : };
69 : :
70 : : struct gnc_commodity_s
71 : : {
72 : : QofInstance inst;
73 : : };
74 : :
75 : : typedef struct gnc_commodityPrivate
76 : : {
77 : : gnc_commodity_namespace *name_space;
78 : :
79 : : const char *fullname;
80 : : const char *mnemonic;
81 : : char *printname;
82 : : const char *cusip; /* CUSIP or other identifying code */
83 : : int fraction;
84 : : char *unique_name;
85 : : char *user_symbol;
86 : :
87 : : gboolean quote_flag; /* user wants price quotes */
88 : : gnc_quote_source *quote_source; /* current/old source of quotes */
89 : : const char *quote_tz;
90 : :
91 : : /* the number of accounts using this commodity - this field is not
92 : : * persisted */
93 : : int usage_count;
94 : :
95 : : /* the default display_symbol, set in iso-4217-currencies at start-up */
96 : : const char *default_symbol;
97 : : } gnc_commodityPrivate;
98 : :
99 : : #define GET_PRIVATE(o) \
100 : : ((gnc_commodityPrivate*)gnc_commodity_get_instance_private((gnc_commodity*)o))
101 : :
102 : : struct _GncCommodityClass
103 : : {
104 : : QofInstanceClass parent_class;
105 : : };
106 : :
107 : : static void commodity_free(gnc_commodity * cm);
108 : : static void gnc_commodity_set_default_symbol(gnc_commodity *, const char *);
109 : :
110 : : struct gnc_commodity_namespace_s
111 : : {
112 : : QofInstance inst;
113 : :
114 : : const gchar *name;
115 : : gboolean iso4217;
116 : : GHashTable * cm_table;
117 : : GList * cm_list;
118 : : };
119 : :
120 : : struct _GncCommodityNamespaceClass
121 : : {
122 : : QofInstanceClass parent_class;
123 : : };
124 : :
125 : : struct gnc_commodity_table_s
126 : : {
127 : : GHashTable * ns_table;
128 : : GList * ns_list;
129 : : };
130 : :
131 : : static const std::unordered_map<std::string,std::string> gnc_new_iso_codes =
132 : : {
133 : : {"RUR", "RUB"}, /* Russian Ruble: RUR through 1997-12, RUB from 1998-01 onwards; see bug #393185 */
134 : : {"PLZ", "PLN"}, /* Polish Zloty */
135 : : {"UAG", "UAH"}, /* Ukraine Hryvnia */
136 : : {"NIS", "ILS"}, /* New Israeli Shekel: The informal abbreviation may be "NIS", but
137 : : its iso-4217 is clearly ILS and only this! Incorrectly changed
138 : : due to bug#152755 (Nov 2004) and changed back again by bug#492417
139 : : (Oct 2008). */
140 : : {"MXP", "MXN"}, /* Mexican (Nuevo) Peso */
141 : : {"TRL", "TRY"}, /* New Turkish Lira: changed 2005 */
142 : :
143 : : /* Only add currencies to this table when the old currency no longer
144 : : * exists in the file iso-4217-currencies.xml */
145 : : };
146 : :
147 : : static std::string fq_version;
148 : :
149 : : struct gnc_quote_source_s
150 : : {
151 : : private:
152 : : gboolean m_supported;
153 : : QuoteSourceType m_type;
154 : : std::string m_user_name; /* User friendly name incl. region code*/
155 : : std::string m_internal_name; /* Name used internally and by finance::quote. */
156 : : public:
157 : 23 : bool get_supported () const { return m_supported; }
158 : 15 : void set_supported (bool supported) { m_supported = supported; }
159 : 0 : QuoteSourceType get_type () const { return m_type; }
160 : 2 : const char* get_user_name () const { return m_user_name.c_str(); }
161 : 126762 : const char* get_internal_name () const { return m_internal_name.c_str(); }
162 : 6053 : gnc_quote_source_s (gboolean supported, QuoteSourceType type,
163 : : const char* username, const char* int_name)
164 : 6053 : : m_supported{supported}
165 : 6053 : , m_type{type}
166 : 12106 : , m_user_name{username ? username : ""}
167 : 18159 : , m_internal_name{int_name ? int_name: ""} { };
168 : : };
169 : :
170 : : using QuoteSourceList = std::list<gnc_quote_source>;
171 : :
172 : : /* To update the following lists scan
173 : : * from github.com/finance-quote/finance-quote
174 : : * in lib/Finance/Quote/ all *.pm for "methods"
175 : : * because many of them have more than one -
176 : : * ideally after each release of them.
177 : : *
178 : : * Apply changes here also to the FQ appendix of help.
179 : : */
180 : : static QuoteSourceList currency_quote_sources =
181 : : {
182 : : { true, SOURCE_CURRENCY, "Currency", "currency" }
183 : : };
184 : :
185 : : /* The single quote method is usually the module name, but
186 : : * sometimes it gets the suffix "_direct"
187 : : * and the failover method is without suffix.
188 : : */
189 : : static QuoteSourceList single_quote_sources =
190 : : {
191 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Alphavantage"), "alphavantage" },
192 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Amsterdam Euronext eXchange, NL"), "aex" },
193 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Association of Mutual Funds in India"), "amfiindia" },
194 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Athens Exchange Group, GR"), "asegr" },
195 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Australian Stock Exchange, AU"), "asx" },
196 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Italian Stock Exchange, IT"), "borsa_italiana" },
197 : : { false, SOURCE_SINGLE, NC_("FQ Source", "BSE India, IN"), "bseindia" },
198 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Bucharest Stock Exchange, RO"), "bvb" },
199 : : { false, SOURCE_SINGLE, NC_("FQ Source", "China Merchants Bank, CN"), "cmbchina" },
200 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Colombo Stock Exchange, LK"), "cse" },
201 : : { false, SOURCE_SINGLE, NC_("FQ Source", "comdirect, DE"), "comdirect" },
202 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Consors Bank, DE"), "consorsbank" },
203 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Deka Investments, DE"), "deka" },
204 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Financial Times Funds service, GB"), "ftfunds" },
205 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Finanzpartner, DE"), "finanzpartner" },
206 : : { false, SOURCE_SINGLE, NC_("FQ Source", "FondsWeb, DE"), "fondsweb" },
207 : : { false, SOURCE_SINGLE, NC_("FQ Source", "GoldMoney precious metals"), "goldmoney" },
208 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Google Web, US Stocks"), "googleweb" },
209 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Market Watch"), "marketwatch" },
210 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Morningstar, CH"), "morningstarch" },
211 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Morningstar, GB"), "morningstaruk" },
212 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Morningstar, JP"), "morningstarjp" },
213 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Motley Fool"), "fool" },
214 : : { false, SOURCE_SINGLE, NC_("FQ Source", "New Zealand stock eXchange, NZ"), "nzx" },
215 : : { false, SOURCE_SINGLE, NC_("FQ Source", "NSE (National Stock Exchange), IN"), "nseindia" },
216 : : { false, SOURCE_SINGLE, NC_("FQ Source", "OnVista, DE"), "onvista"},
217 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Paris Stock Exchange/Boursorama, FR"), "bourso" },
218 : : { false, SOURCE_SINGLE, NC_("FQ Source", "S-Investor, DE"), "sinvestor"},
219 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Sharenet, ZA"), "za" },
220 : : { false, SOURCE_SINGLE, NC_("FQ Source", "SIX Swiss Exchange shares, CH"), "six" },
221 : : { false, SOURCE_SINGLE, NC_("FQ Source", "StockData"), "stockdata" },
222 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Stooq, PL"), "stooq" },
223 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Swiss Fund Data AG, CH"), "swissfunddata" },
224 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Tesouro Direto bonds, BR"), "tesouro_direto" },
225 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Toronto Stock eXchange, CA"), "tsx" },
226 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Tradegate, DE"), "tradegate" },
227 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Treasury Direct bonds, US"), "treasurydirect" },
228 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Twelve Data"), "twelvedata" },
229 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Union Investment, DE"), "unionfunds" },
230 : : { false, SOURCE_SINGLE, NC_("FQ Source", "US Govt. Thrift Savings Plan"), "tsp" },
231 : : { false, SOURCE_SINGLE, NC_("FQ Source", "XETRA, DE"), "xetra" },
232 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Yahoo as JSON"), "yahoo_json" },
233 : : { false, SOURCE_SINGLE, NC_("FQ Source", "Yahoo Web"), "yahooweb" },
234 : : { false, SOURCE_SINGLE, NC_("FQ Source", "YH Finance (FinanceAPI)"), "financeapi" },
235 : : };
236 : :
237 : : // Finance::Quote defines these as failover methods
238 : : static QuoteSourceList multiple_quote_sources =
239 : : {
240 : : { false, SOURCE_MULTI, NC_("FQ Source", "Canada (Alphavantage, TMX)"), "canada" },
241 : : { false, SOURCE_MULTI, NC_("FQ Source", "Europe (ASEGR, Bourso, …)"), "europe" },
242 : : { false, SOURCE_MULTI, NC_("FQ Source", "India (BSEIndia, NSEIndia)"), "india"},
243 : : { false, SOURCE_MULTI, NC_("FQ Source", "Nasdaq (Alphavantage, FinanceAPI, …)"), "nasdaq" },
244 : : { false, SOURCE_MULTI, NC_("FQ Source", "NYSE (Alphavantage, FinanceAPI, …)"), "nyse" },
245 : : { false, SOURCE_MULTI, NC_("FQ Source", "U.K. Funds (FTfunds, MorningstarUK)"), "ukfunds" },
246 : : { false, SOURCE_MULTI, NC_("FQ Source", "USA (Alphavantage, FinanceAPI, …)"), "usa" },
247 : : };
248 : :
249 : : static QuoteSourceList new_quote_sources;
250 : :
251 : : // cannot use map or unordered_map because order must be preserved
252 : : static const std::vector<std::pair<QuoteSourceType,QuoteSourceList&>> quote_sources_map =
253 : : {
254 : : { SOURCE_CURRENCY, currency_quote_sources },
255 : : { SOURCE_SINGLE, single_quote_sources },
256 : : { SOURCE_MULTI, multiple_quote_sources },
257 : : { SOURCE_UNKNOWN, new_quote_sources }
258 : : };
259 : :
260 : : /********************************************************************
261 : : * gnc_quote_source_fq_installed
262 : : *
263 : : * This function indicates whether or not the Finance::Quote module
264 : : * is installed on a users computer.
265 : : ********************************************************************/
266 : : gboolean
267 : 0 : gnc_quote_source_fq_installed (void)
268 : : {
269 : 0 : return (!fq_version.empty());
270 : : }
271 : :
272 : :
273 : : /********************************************************************
274 : : * gnc_quote_source_fq_version
275 : : *
276 : : * This function the version of the Finance::Quote module installed
277 : : * on a user's computer or nullptr if no installation is found.
278 : : ********************************************************************/
279 : : const char*
280 : 0 : gnc_quote_source_fq_version (void)
281 : : {
282 : 0 : return fq_version.c_str();
283 : : }
284 : :
285 : : static QuoteSourceList&
286 : 11 : get_quote_source_from_type (QuoteSourceType type)
287 : : {
288 : 11 : auto quote_sources_it = std::find_if (quote_sources_map.begin(), quote_sources_map.end(),
289 : 38 : [type] (const auto& qs) { return type == qs.first; });
290 : :
291 : 11 : if (quote_sources_it != quote_sources_map.end())
292 : 7 : return quote_sources_it->second;
293 : :
294 : 4 : PWARN ("Invalid Quote Source %d, returning new_quote_sources", type);
295 : 4 : return new_quote_sources;
296 : : }
297 : :
298 : : /********************************************************************
299 : : * gnc_quote_source_num_entries
300 : : *
301 : : * Return the number of entries for a given type of price source.
302 : : ********************************************************************/
303 : 5 : gint gnc_quote_source_num_entries(QuoteSourceType type)
304 : : {
305 : 5 : auto source{get_quote_source_from_type(type)};
306 : 15 : return std::distance(source.begin(), source.end());
307 : 5 : }
308 : :
309 : :
310 : :
311 : : /********************************************************************
312 : : * gnc_quote_source_add_new
313 : : *
314 : : * Add a new price source. Called when unknown source names are found
315 : : * either in the F::Q installation (a newly available source) or in
316 : : * the user's data file (a source that has vanished but needs to be
317 : : * tracked.)
318 : : ********************************************************************/
319 : : gnc_quote_source *
320 : 21 : gnc_quote_source_add_new (const char *source_name, gboolean supported)
321 : : {
322 : 21 : DEBUG("Creating new source %s", (!source_name ? "(null)" : source_name));
323 : : /* This name can be changed if/when support for this price source is
324 : : * integrated into gnucash. */
325 : : /* This name is permanent and must be kept the same if/when support
326 : : * for this price source is integrated into gnucash (i.e. for a
327 : : * nice user name). */
328 : 21 : return &new_quote_sources.emplace_back (supported, SOURCE_UNKNOWN, source_name, source_name);
329 : : }
330 : :
331 : : /********************************************************************
332 : : * gnc_quote_source_lookup_by_xxx
333 : : *
334 : : * Lookup a price source data structure based upon various criteria.
335 : : ********************************************************************/
336 : : gnc_quote_source *
337 : 6 : gnc_quote_source_lookup_by_ti (QuoteSourceType type, gint index)
338 : : {
339 : 6 : ENTER("type/index is %d/%d", type, index);
340 : 6 : auto& sources = get_quote_source_from_type (type);
341 : 6 : if ((size_t) index < sources.size())
342 : : {
343 : 4 : auto it = std::next(sources.begin(), index);
344 : 4 : LEAVE("found %s", it->get_user_name());
345 : 4 : return &*it;
346 : : }
347 : :
348 : 2 : LEAVE("not found");
349 : 2 : return nullptr;
350 : : }
351 : :
352 : : gnc_quote_source *
353 : 126103 : gnc_quote_source_lookup_by_internal(const char * name)
354 : : {
355 : 126103 : if (!name || !*name)
356 : 1 : return nullptr;
357 : :
358 : 126140 : for (const auto& [_, sources] : quote_sources_map)
359 : : {
360 : 126139 : auto source_it = std::find_if (sources.begin(), sources.end(),
361 : 126229 : [name] (const auto& qs)
362 : 126229 : { return (g_strcmp0(name, qs.get_internal_name()) == 0); });
363 : 126139 : if (source_it != sources.end())
364 : 126101 : return &(*source_it);
365 : : }
366 : :
367 : 1 : DEBUG("gnc_quote_source_lookup_by_internal: Unknown source %s", name);
368 : 1 : return nullptr;
369 : : }
370 : :
371 : : /********************************************************************
372 : : * gnc_quote_source_get_xxx
373 : : *
374 : : * Accessor functions - get functions only. There are no set functions.
375 : : ********************************************************************/
376 : : QuoteSourceType
377 : 0 : gnc_quote_source_get_type (const gnc_quote_source *source)
378 : : {
379 : 0 : ENTER("%p", source);
380 : 0 : if (!source)
381 : : {
382 : 0 : LEAVE("bad source");
383 : 0 : return SOURCE_SINGLE;
384 : : }
385 : :
386 : 0 : LEAVE("type is %d", source->get_type());
387 : 0 : return source->get_type();
388 : : }
389 : :
390 : : gint
391 : 0 : gnc_quote_source_get_index (const gnc_quote_source *source)
392 : : {
393 : 0 : if (!source)
394 : : {
395 : 0 : PWARN ("bad source");
396 : 0 : return 0;
397 : : }
398 : :
399 : 0 : auto& sources = get_quote_source_from_type (source->get_type());
400 : 0 : auto is_source = [&source](const auto& findif_source)
401 : 0 : { return &findif_source == source; };
402 : :
403 : 0 : auto iter = std::find_if (sources.begin(), sources.end(), is_source);
404 : 0 : if (iter != sources.end())
405 : 0 : return std::distance (sources.begin(), iter);
406 : :
407 : 0 : PWARN ("couldn't locate source");
408 : 0 : return 0;
409 : : }
410 : :
411 : : gboolean
412 : 23 : gnc_quote_source_get_supported (const gnc_quote_source *source)
413 : : {
414 : 23 : ENTER("%p", source);
415 : 23 : if (!source)
416 : : {
417 : 0 : LEAVE("bad source");
418 : 0 : return FALSE;
419 : : }
420 : :
421 : 23 : LEAVE("%s supported", source && source->get_supported() ? "" : "not ");
422 : 23 : return source->get_supported();
423 : : }
424 : :
425 : : const char *
426 : 2 : gnc_quote_source_get_user_name (const gnc_quote_source *source)
427 : : {
428 : 2 : ENTER("%p", source);
429 : 2 : if (!source)
430 : : {
431 : 0 : LEAVE("bad source");
432 : 0 : return nullptr;
433 : : }
434 : 2 : LEAVE("user name %s", source->get_user_name());
435 : 2 : return source->get_user_name();
436 : : }
437 : :
438 : : const char *
439 : 83 : gnc_quote_source_get_internal_name (const gnc_quote_source *source)
440 : : {
441 : 83 : ENTER("%p", source);
442 : 83 : if (!source)
443 : : {
444 : 6 : LEAVE("bad source");
445 : 6 : return nullptr;
446 : : }
447 : 77 : LEAVE("internal name %s", source->get_internal_name());
448 : 77 : return source->get_internal_name();
449 : : }
450 : :
451 : :
452 : : /********************************************************************
453 : : * gnc_quote_source_set_fq_installed
454 : : *
455 : : * Update gnucash internal tables on what Finance::Quote sources are
456 : : * installed.
457 : : ********************************************************************/
458 : : void
459 : 15 : gnc_quote_source_set_fq_installed (const char* version_string,
460 : : const std::vector<std::string>& sources_list)
461 : : {
462 : 15 : ENTER(" ");
463 : :
464 : 15 : if (sources_list.empty())
465 : 0 : return;
466 : :
467 : 15 : if (version_string)
468 : 15 : fq_version = version_string;
469 : : else
470 : 0 : fq_version.clear();
471 : :
472 : 30 : for (const auto& source_name_str : sources_list)
473 : : {
474 : 15 : auto source_name = source_name_str.c_str();
475 : 15 : auto source = gnc_quote_source_lookup_by_internal(source_name);
476 : :
477 : 15 : if (source)
478 : : {
479 : 15 : DEBUG("Found source %s: %s", source_name, source->get_user_name());
480 : 15 : source->set_supported (true);
481 : 15 : continue;
482 : : }
483 : :
484 : 0 : gnc_quote_source_add_new(source_name, TRUE);
485 : : }
486 : 15 : LEAVE(" ");
487 : : }
488 : :
489 : : /********************************************************************
490 : : * QoF Helpers
491 : : ********************************************************************/
492 : :
493 : : void
494 : 506807 : gnc_commodity_begin_edit (gnc_commodity *cm)
495 : : {
496 : 506807 : qof_begin_edit(&cm->inst);
497 : 506807 : }
498 : :
499 : 0 : static void commit_err (QofInstance *inst, QofBackendError errcode)
500 : : {
501 : 0 : PERR ("Failed to commit: %d", errcode);
502 : 0 : gnc_engine_signal_commit_error( errcode );
503 : 0 : }
504 : :
505 : 67888 : static void noop (QofInstance *inst) {}
506 : :
507 : : static void
508 : 41825 : comm_free(QofInstance* inst)
509 : : {
510 : 41825 : commodity_free( GNC_COMMODITY(inst) );
511 : 41825 : }
512 : :
513 : : void
514 : 506807 : gnc_commodity_commit_edit (gnc_commodity *cm)
515 : : {
516 : 506807 : if (!qof_commit_edit (QOF_INSTANCE(cm))) return;
517 : 109713 : qof_commit_edit_part2 (&cm->inst, commit_err, noop, comm_free);
518 : : }
519 : :
520 : : /********************************************************************
521 : : * gnc_commodity_new
522 : : ********************************************************************/
523 : :
524 : : static void
525 : 464768 : mark_commodity_dirty (gnc_commodity *cm)
526 : : {
527 : 464768 : qof_instance_set_dirty(&cm->inst);
528 : 464768 : qof_event_gen (&cm->inst, QOF_EVENT_MODIFY, nullptr);
529 : 464768 : }
530 : :
531 : : static void
532 : 267101 : reset_printname(gnc_commodityPrivate *priv)
533 : : {
534 : 267101 : g_free(priv->printname);
535 : 534202 : priv->printname = g_strdup_printf("%s (%s)",
536 : 267101 : priv->mnemonic ? priv->mnemonic : "",
537 : 267101 : priv->fullname ? priv->fullname : "");
538 : 267101 : }
539 : :
540 : : static void
541 : 200177 : reset_unique_name(gnc_commodityPrivate *priv)
542 : : {
543 : : gnc_commodity_namespace *ns;
544 : :
545 : 200177 : g_free(priv->unique_name);
546 : 200177 : ns = priv->name_space;
547 : 200177 : priv->unique_name = g_strdup_printf("%s::%s",
548 : : ns ? ns->name : "",
549 : 200177 : priv->mnemonic ? priv->mnemonic : "");
550 : 200177 : }
551 : :
552 : : /* GObject Initialization */
553 : 1126360 : G_DEFINE_TYPE_WITH_PRIVATE(gnc_commodity, gnc_commodity, QOF_TYPE_INSTANCE)
554 : :
555 : : static void
556 : 66793 : gnc_commodity_init(gnc_commodity* com)
557 : : {
558 : : gnc_commodityPrivate* priv;
559 : :
560 : 66793 : priv = GET_PRIVATE(com);
561 : :
562 : 66793 : priv->name_space = nullptr;
563 : 66793 : priv->fullname = CACHE_INSERT("");
564 : 66793 : priv->mnemonic = CACHE_INSERT("");
565 : 66793 : priv->cusip = CACHE_INSERT("");
566 : 66793 : priv->fraction = 10000;
567 : 66793 : priv->quote_flag = 0;
568 : 66793 : priv->quote_source = nullptr;
569 : 66793 : priv->quote_tz = CACHE_INSERT("");
570 : :
571 : 66793 : reset_printname(priv);
572 : 66793 : reset_unique_name(priv);
573 : 66793 : }
574 : :
575 : : static void
576 : 41983 : gnc_commodity_dispose(GObject *comp)
577 : : {
578 : 41983 : G_OBJECT_CLASS(gnc_commodity_parent_class)->dispose(comp);
579 : 41983 : }
580 : :
581 : : static void
582 : 41983 : gnc_commodity_finalize(GObject* comp)
583 : : {
584 : 41983 : G_OBJECT_CLASS(gnc_commodity_parent_class)->finalize(comp);
585 : 41983 : }
586 : : /* Note that g_value_set_object() refs the object, as does
587 : : * g_object_get(). But g_object_get() only unrefs once when it disgorges
588 : : * the object, leaving an unbalanced ref, which leaks. So instead of
589 : : * using g_value_set_object(), use g_value_take_object() which doesn't
590 : : * ref the object when used in get_property().
591 : : */
592 : : static void
593 : 318 : gnc_commodity_get_property (GObject *object,
594 : : guint prop_id,
595 : : GValue *value,
596 : : GParamSpec *pspec)
597 : : {
598 : : gnc_commodity *commodity;
599 : : gnc_commodityPrivate* priv;
600 : :
601 : 318 : g_return_if_fail(GNC_IS_COMMODITY(object));
602 : :
603 : 318 : commodity = GNC_COMMODITY(object);
604 : 318 : priv = GET_PRIVATE(commodity);
605 : 318 : switch (prop_id)
606 : : {
607 : 0 : case PROP_NAMESPACE:
608 : 0 : g_value_take_object(value, priv->name_space);
609 : 0 : break;
610 : 53 : case PROP_FULL_NAME:
611 : 53 : g_value_set_string(value, priv->fullname);
612 : 53 : break;
613 : 53 : case PROP_MNEMONIC:
614 : 53 : g_value_set_string(value, priv->mnemonic);
615 : 53 : break;
616 : 0 : case PROP_PRINTNAME:
617 : 0 : g_value_set_string(value, priv->printname);
618 : 0 : break;
619 : 53 : case PROP_CUSIP:
620 : 53 : g_value_set_string(value, priv->cusip);
621 : 53 : break;
622 : 53 : case PROP_FRACTION:
623 : 53 : g_value_set_int(value, priv->fraction);
624 : 53 : break;
625 : 0 : case PROP_UNIQUE_NAME:
626 : 0 : g_value_set_string(value, priv->unique_name);
627 : 0 : break;
628 : 53 : case PROP_QUOTE_FLAG:
629 : 53 : g_value_set_boolean(value, priv->quote_flag);
630 : 53 : break;
631 : 0 : case PROP_QUOTE_SOURCE:
632 : 0 : g_value_set_pointer(value, priv->quote_source);
633 : 0 : break;
634 : 53 : case PROP_QUOTE_TZ:
635 : 53 : g_value_set_string(value, priv->quote_tz);
636 : 53 : break;
637 : 0 : default:
638 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
639 : 0 : break;
640 : : }
641 : : }
642 : :
643 : : static void
644 : 48 : gnc_commodity_set_property (GObject *object,
645 : : guint prop_id,
646 : : const GValue *value,
647 : : GParamSpec *pspec)
648 : : {
649 : : gnc_commodity *commodity;
650 : :
651 : 48 : g_return_if_fail(GNC_IS_COMMODITY(object));
652 : :
653 : 48 : commodity = GNC_COMMODITY(object);
654 : 48 : g_assert (qof_instance_get_editlevel(commodity));
655 : :
656 : 48 : switch (prop_id)
657 : : {
658 : 0 : case PROP_NAMESPACE:
659 : 0 : gnc_commodity_set_namespace(commodity, static_cast<const char*>(g_value_get_object(value)));
660 : 0 : break;
661 : 8 : case PROP_FULL_NAME:
662 : 8 : gnc_commodity_set_fullname(commodity, g_value_get_string(value));
663 : 8 : break;
664 : 8 : case PROP_MNEMONIC:
665 : 8 : gnc_commodity_set_mnemonic(commodity, g_value_get_string(value));
666 : 8 : break;
667 : 8 : case PROP_CUSIP:
668 : 8 : gnc_commodity_set_cusip(commodity, g_value_get_string(value));
669 : 8 : break;
670 : 8 : case PROP_FRACTION:
671 : 8 : gnc_commodity_set_fraction(commodity, g_value_get_int(value));
672 : 8 : break;
673 : 8 : case PROP_QUOTE_FLAG:
674 : 8 : gnc_commodity_set_quote_flag(commodity, g_value_get_boolean(value));
675 : 8 : break;
676 : 0 : case PROP_QUOTE_SOURCE:
677 : 0 : gnc_commodity_set_quote_source(commodity, static_cast<gnc_quote_source*>(g_value_get_pointer(value)));
678 : 0 : break;
679 : 8 : case PROP_QUOTE_TZ:
680 : 8 : gnc_commodity_set_quote_tz(commodity, g_value_get_string(value));
681 : 8 : break;
682 : 0 : default:
683 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
684 : 0 : break;
685 : : }
686 : : }
687 : : static void
688 : 63 : gnc_commodity_class_init(struct _GncCommodityClass* klass)
689 : : {
690 : 63 : GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
691 : :
692 : 63 : gobject_class->dispose = gnc_commodity_dispose;
693 : 63 : gobject_class->finalize = gnc_commodity_finalize;
694 : 63 : gobject_class->set_property = gnc_commodity_set_property;
695 : 63 : gobject_class->get_property = gnc_commodity_get_property;
696 : :
697 : 63 : g_object_class_install_property(gobject_class,
698 : : PROP_NAMESPACE,
699 : : g_param_spec_object ("namespace",
700 : : "Namespace",
701 : : "The namespace field denotes the "
702 : : "namespace for this commodity, either "
703 : : "a currency or symbol from a quote source.",
704 : : GNC_TYPE_COMMODITY_NAMESPACE,
705 : : G_PARAM_READWRITE));
706 : 63 : g_object_class_install_property(gobject_class,
707 : : PROP_FULL_NAME,
708 : : g_param_spec_string ("fullname",
709 : : "Full Commodity Name",
710 : : "The fullname is the official full name of"
711 : : "the currency.",
712 : : nullptr,
713 : : G_PARAM_READWRITE));
714 : 63 : g_object_class_install_property(gobject_class,
715 : : PROP_MNEMONIC,
716 : : g_param_spec_string ("mnemonic",
717 : : "Commodity Mnemonic",
718 : : "The mnemonic is the official abbreviated"
719 : : "designation for the currency.",
720 : : nullptr,
721 : : G_PARAM_READWRITE));
722 : 63 : g_object_class_install_property(gobject_class,
723 : : PROP_PRINTNAME,
724 : : g_param_spec_string ("printname",
725 : : "Commodity Print Name",
726 : : "Printable form of the commodity name.",
727 : : nullptr,
728 : : G_PARAM_READABLE));
729 : 63 : g_object_class_install_property(gobject_class,
730 : : PROP_CUSIP,
731 : : g_param_spec_string ("cusip",
732 : : "Commodity CUSIP Code",
733 : : "?????",
734 : : nullptr,
735 : : G_PARAM_READWRITE));
736 : 63 : g_object_class_install_property(gobject_class,
737 : : PROP_FRACTION,
738 : : g_param_spec_int ("fraction",
739 : : "Fraction",
740 : : "The fraction is the number of sub-units that "
741 : : "the basic commodity can be divided into.",
742 : : 1,
743 : : GNC_COMMODITY_MAX_FRACTION,
744 : : 1,
745 : : G_PARAM_READWRITE));
746 : 63 : g_object_class_install_property(gobject_class,
747 : : PROP_UNIQUE_NAME,
748 : : g_param_spec_string ("unique-name",
749 : : "Commodity Unique Name",
750 : : "Unique form of the commodity name which combines "
751 : : "the namespace name and the commodity name.",
752 : : nullptr,
753 : : G_PARAM_READABLE));
754 : 63 : g_object_class_install_property(gobject_class,
755 : : PROP_QUOTE_FLAG,
756 : : g_param_spec_boolean ("quote_flag",
757 : : "Quote Flag",
758 : : "TRUE if prices are to be downloaded for this "
759 : : "commodity from a quote source.",
760 : : FALSE,
761 : : G_PARAM_READWRITE));
762 : 63 : g_object_class_install_property(gobject_class,
763 : : PROP_QUOTE_SOURCE,
764 : : g_param_spec_pointer("quote-source",
765 : : "Quote Source",
766 : : "The quote source from which prices are downloaded.",
767 : : G_PARAM_READWRITE));
768 : 63 : g_object_class_install_property(gobject_class,
769 : : PROP_QUOTE_TZ,
770 : : g_param_spec_string ("quote-tz",
771 : : "Commodity Quote Timezone",
772 : : "?????",
773 : : nullptr,
774 : : G_PARAM_READWRITE));
775 : 63 : }
776 : :
777 : : gnc_commodity *
778 : 66793 : gnc_commodity_new(QofBook *book, const char * fullname,
779 : : const char * name_space, const char * mnemonic,
780 : : const char * cusip, int fraction)
781 : : {
782 : 66793 : auto retval = GNC_COMMODITY(g_object_new(GNC_TYPE_COMMODITY, nullptr));
783 : :
784 : 66793 : qof_instance_init_data (&retval->inst, GNC_ID_COMMODITY, book);
785 : 66793 : gnc_commodity_begin_edit(retval);
786 : :
787 : 66793 : if ( name_space != nullptr )
788 : : {
789 : : /* Prevent setting anything except template in namespace template. */
790 : 66936 : if (g_strcmp0 (name_space, GNC_COMMODITY_NS_TEMPLATE) == 0 &&
791 : 275 : g_strcmp0 (mnemonic, "template") != 0)
792 : : {
793 : 0 : PWARN("Converting commodity %s from namespace template to "
794 : : "namespace User", mnemonic);
795 : 0 : name_space = "User";
796 : : }
797 : 66661 : gnc_commodity_set_namespace(retval, name_space);
798 : 66661 : if (gnc_commodity_namespace_is_iso(name_space))
799 : : {
800 : 63117 : gnc_commodity_set_quote_source(retval,
801 : : gnc_quote_source_lookup_by_internal("currency") );
802 : : }
803 : : }
804 : 66793 : gnc_commodity_set_fullname(retval, fullname);
805 : 66793 : gnc_commodity_set_mnemonic(retval, mnemonic);
806 : 66793 : gnc_commodity_set_cusip(retval, cusip);
807 : 66793 : gnc_commodity_set_fraction(retval, fraction);
808 : 66793 : mark_commodity_dirty (retval);
809 : 66793 : gnc_commodity_commit_edit(retval);
810 : :
811 : 66793 : qof_event_gen (&retval->inst, QOF_EVENT_CREATE, nullptr);
812 : :
813 : 66793 : return retval;
814 : : }
815 : :
816 : :
817 : : /********************************************************************
818 : : * gnc_commodity_destroy
819 : : ********************************************************************/
820 : :
821 : : static void
822 : 41825 : commodity_free(gnc_commodity * cm)
823 : : {
824 : : QofBook *book;
825 : : gnc_commodity_table *table;
826 : : gnc_commodityPrivate* priv;
827 : :
828 : 41825 : if (!cm) return;
829 : :
830 : 41825 : book = qof_instance_get_book(&cm->inst);
831 : 41825 : table = gnc_commodity_table_get_table(book);
832 : 41825 : gnc_commodity_table_remove(table, cm);
833 : 41825 : priv = GET_PRIVATE(cm);
834 : :
835 : 41825 : qof_event_gen (&cm->inst, QOF_EVENT_DESTROY, nullptr);
836 : :
837 : : /* Set at creation */
838 : 41825 : CACHE_REMOVE (priv->fullname);
839 : 41825 : CACHE_REMOVE (priv->cusip);
840 : 41825 : CACHE_REMOVE (priv->mnemonic);
841 : 41825 : CACHE_REMOVE (priv->quote_tz);
842 : 41825 : priv->name_space = nullptr;
843 : :
844 : : /* Set through accessor functions */
845 : 41825 : priv->quote_source = nullptr;
846 : :
847 : : /* Automatically generated */
848 : 41825 : g_free(priv->printname);
849 : 41825 : priv->printname = nullptr;
850 : :
851 : 41825 : g_free(priv->unique_name);
852 : 41825 : priv->unique_name = nullptr;
853 : :
854 : : #ifdef ACCOUNTS_CLEANED_UP
855 : : /* Account objects are not actually cleaned up when a book is closed (in fact
856 : : * a memory leak), but commodities are, so in currently this warning gets hit
857 : : * quite frequently. Disable the check until cleaning up of accounts objects
858 : : * on close is implemented. */
859 : : if (priv->usage_count != 0)
860 : : {
861 : : PWARN("Destroying commodity (%p) with non-zero usage_count (%d).", cm,
862 : : priv->usage_count);
863 : : }
864 : : #endif
865 : :
866 : : /* qof_instance_release (&cm->inst); */
867 : 41825 : g_object_unref(cm);
868 : : }
869 : :
870 : : void
871 : 41825 : gnc_commodity_destroy(gnc_commodity * cm)
872 : : {
873 : 41825 : gnc_commodity_begin_edit(cm);
874 : 41825 : qof_instance_set_destroying(cm, TRUE);
875 : 41825 : gnc_commodity_commit_edit(cm);
876 : 41825 : }
877 : :
878 : : void
879 : 64 : gnc_commodity_copy(gnc_commodity * dest, const gnc_commodity *src)
880 : : {
881 : 64 : gnc_commodityPrivate* src_priv = GET_PRIVATE(src);
882 : 64 : gnc_commodityPrivate* dest_priv = GET_PRIVATE(dest);
883 : :
884 : 64 : gnc_commodity_set_fullname (dest, src_priv->fullname);
885 : 64 : gnc_commodity_set_mnemonic (dest, src_priv->mnemonic);
886 : 64 : dest_priv->name_space = src_priv->name_space;
887 : 64 : gnc_commodity_set_fraction (dest, src_priv->fraction);
888 : 64 : gnc_commodity_set_cusip (dest, src_priv->cusip);
889 : 64 : gnc_commodity_set_quote_flag (dest, src_priv->quote_flag);
890 : 64 : gnc_commodity_set_quote_source (dest, gnc_commodity_get_quote_source (src));
891 : 64 : gnc_commodity_set_quote_tz (dest, src_priv->quote_tz);
892 : 64 : qof_instance_copy_kvp (QOF_INSTANCE (dest), QOF_INSTANCE (src));
893 : 64 : }
894 : :
895 : : gnc_commodity *
896 : 0 : gnc_commodity_clone(const gnc_commodity *src, QofBook *dest_book)
897 : : {
898 : : gnc_commodityPrivate* src_priv;
899 : : gnc_commodityPrivate* dest_priv;
900 : :
901 : 0 : auto dest = GNC_COMMODITY (g_object_new(GNC_TYPE_COMMODITY, nullptr));
902 : 0 : qof_instance_init_data (&dest->inst, GNC_ID_COMMODITY, dest_book);
903 : 0 : src_priv = GET_PRIVATE(src);
904 : 0 : dest_priv = GET_PRIVATE(dest);
905 : :
906 : 0 : dest_priv->fullname = CACHE_INSERT(src_priv->fullname);
907 : 0 : dest_priv->mnemonic = CACHE_INSERT(src_priv->mnemonic);
908 : 0 : dest_priv->cusip = CACHE_INSERT(src_priv->cusip);
909 : 0 : dest_priv->quote_tz = CACHE_INSERT(src_priv->quote_tz);
910 : :
911 : 0 : dest_priv->name_space = src_priv->name_space;
912 : :
913 : 0 : dest_priv->fraction = src_priv->fraction;
914 : 0 : dest_priv->quote_flag = src_priv->quote_flag;
915 : :
916 : 0 : gnc_commodity_set_quote_source (dest, gnc_commodity_get_quote_source (src));
917 : :
918 : 0 : qof_instance_copy_kvp (QOF_INSTANCE (dest), QOF_INSTANCE (src));
919 : :
920 : 0 : reset_printname(dest_priv);
921 : 0 : reset_unique_name(dest_priv);
922 : :
923 : 0 : return dest;
924 : : }
925 : :
926 : : /********************************************************************
927 : : * gnc_commodity_get_mnemonic
928 : : ********************************************************************/
929 : :
930 : : const char *
931 : 38187 : gnc_commodity_get_mnemonic(const gnc_commodity * cm)
932 : : {
933 : 38187 : if (!cm) return nullptr;
934 : 38187 : return GET_PRIVATE(cm)->mnemonic;
935 : : }
936 : :
937 : : /********************************************************************
938 : : * gnc_commodity_get_printname
939 : : ********************************************************************/
940 : :
941 : : const char *
942 : 1465 : gnc_commodity_get_printname(const gnc_commodity * cm)
943 : : {
944 : 1465 : if (!cm) return nullptr;
945 : 1465 : return GET_PRIVATE(cm)->printname;
946 : : }
947 : :
948 : :
949 : : /********************************************************************
950 : : * gnc_commodity_get_namespace
951 : : ********************************************************************/
952 : :
953 : : const char *
954 : 12546 : gnc_commodity_get_namespace(const gnc_commodity * cm)
955 : : {
956 : 12546 : if (!cm) return nullptr;
957 : 12546 : return gnc_commodity_namespace_get_name(GET_PRIVATE(cm)->name_space);
958 : : }
959 : :
960 : : gnc_commodity_namespace *
961 : 0 : gnc_commodity_get_namespace_ds(const gnc_commodity * cm)
962 : : {
963 : 0 : if (!cm) return nullptr;
964 : 0 : return GET_PRIVATE(cm)->name_space;
965 : : }
966 : :
967 : : /********************************************************************
968 : : * gnc_commodity_get_fullname
969 : : ********************************************************************/
970 : :
971 : : const char *
972 : 70 : gnc_commodity_get_fullname(const gnc_commodity * cm)
973 : : {
974 : 70 : if (!cm) return nullptr;
975 : 70 : return GET_PRIVATE(cm)->fullname;
976 : : }
977 : :
978 : :
979 : : /********************************************************************
980 : : * gnc_commodity_get_unique_name
981 : : ********************************************************************/
982 : :
983 : : const char *
984 : 16 : gnc_commodity_get_unique_name(const gnc_commodity * cm)
985 : : {
986 : 16 : if (!cm) return nullptr;
987 : 10 : return GET_PRIVATE(cm)->unique_name;
988 : : }
989 : :
990 : :
991 : : /********************************************************************
992 : : * gnc_commodity_get_cusip
993 : : ********************************************************************/
994 : :
995 : : const char *
996 : 46 : gnc_commodity_get_cusip(const gnc_commodity * cm)
997 : : {
998 : 46 : if (!cm) return nullptr;
999 : 46 : return GET_PRIVATE(cm)->cusip;
1000 : : }
1001 : :
1002 : : /********************************************************************
1003 : : * gnc_commodity_get_fraction
1004 : : ********************************************************************/
1005 : :
1006 : : int
1007 : 64597 : gnc_commodity_get_fraction(const gnc_commodity * cm)
1008 : : {
1009 : 64597 : if (!cm) return 0;
1010 : 64454 : return GET_PRIVATE(cm)->fraction;
1011 : : }
1012 : :
1013 : : /********************************************************************
1014 : : * gnc_commodity_get_auto_quote_control_flag
1015 : : ********************************************************************/
1016 : :
1017 : : gboolean
1018 : 870 : gnc_commodity_get_auto_quote_control_flag(const gnc_commodity *cm)
1019 : : {
1020 : 870 : if (!cm) return FALSE;
1021 : 1740 : auto str{qof_instance_get_path_kvp<const char*> (QOF_INSTANCE (cm), {"auto_quote_control"})};
1022 : 870 : return !str || g_strcmp0 (*str, "false");
1023 : : }
1024 : :
1025 : : /********************************************************************
1026 : : * gnc_commodity_get_quote_flag
1027 : : ********************************************************************/
1028 : :
1029 : : gboolean
1030 : 1042 : gnc_commodity_get_quote_flag(const gnc_commodity *cm)
1031 : : {
1032 : 1042 : if (!cm) return FALSE;
1033 : 1042 : return (GET_PRIVATE(cm)->quote_flag);
1034 : : }
1035 : :
1036 : : /********************************************************************
1037 : : * gnc_commodity_get_quote_source
1038 : : ********************************************************************/
1039 : :
1040 : : gnc_quote_source*
1041 : 167 : gnc_commodity_get_quote_source(const gnc_commodity *cm)
1042 : : {
1043 : : gnc_commodityPrivate* priv;
1044 : :
1045 : 167 : if (!cm) return nullptr;
1046 : 167 : priv = GET_PRIVATE(cm);
1047 : 167 : if (!priv->quote_source && gnc_commodity_is_iso(cm))
1048 : 0 : return ¤cy_quote_sources.front();
1049 : 167 : return priv->quote_source;
1050 : : }
1051 : :
1052 : : gnc_quote_source*
1053 : 146 : gnc_commodity_get_default_quote_source(const gnc_commodity *cm)
1054 : : {
1055 : 146 : if (cm && gnc_commodity_is_iso(cm))
1056 : 146 : return ¤cy_quote_sources.front();
1057 : : /* Should make this a user option at some point. */
1058 : 0 : return gnc_quote_source_lookup_by_internal("alphavantage");
1059 : : }
1060 : :
1061 : : /********************************************************************
1062 : : * gnc_commodity_get_quote_tz
1063 : : ********************************************************************/
1064 : :
1065 : : const char*
1066 : 4 : gnc_commodity_get_quote_tz(const gnc_commodity *cm)
1067 : : {
1068 : 4 : if (!cm) return nullptr;
1069 : 4 : return GET_PRIVATE(cm)->quote_tz;
1070 : : }
1071 : :
1072 : : /********************************************************************
1073 : : * gnc_commodity_get_user_symbol
1074 : : ********************************************************************/
1075 : : const char*
1076 : 8476 : gnc_commodity_get_user_symbol(const gnc_commodity *cm)
1077 : : {
1078 : 8476 : g_return_val_if_fail (GNC_IS_COMMODITY (cm), nullptr);
1079 : :
1080 : 16952 : auto sym{qof_instance_get_path_kvp<const char*> (QOF_INSTANCE(cm), {"user_symbol"})};
1081 : 8476 : return sym ? *sym : nullptr;
1082 : : }
1083 : :
1084 : : /********************************************************************
1085 : : * gnc_commodity_get_default_symbol
1086 : : *******************************************************************/
1087 : : const char*
1088 : 7980 : gnc_commodity_get_default_symbol(const gnc_commodity *cm)
1089 : : {
1090 : 7980 : if (!cm) return nullptr;
1091 : 7980 : return GET_PRIVATE(cm)->default_symbol;
1092 : : }
1093 : :
1094 : : /********************************************************************
1095 : : * gnc_commodity_get_nice_symbol
1096 : : *******************************************************************/
1097 : : const char*
1098 : 8474 : gnc_commodity_get_nice_symbol (const gnc_commodity *cm)
1099 : : {
1100 : : const char *nice_symbol;
1101 : : struct lconv *lc;
1102 : 8474 : if (!cm) return nullptr;
1103 : :
1104 : 8474 : nice_symbol = gnc_commodity_get_user_symbol(cm);
1105 : 8474 : if (nice_symbol && *nice_symbol)
1106 : 501 : return nice_symbol;
1107 : :
1108 : 7973 : lc = gnc_localeconv();
1109 : 7973 : nice_symbol = lc->currency_symbol;
1110 : 7973 : if (!g_strcmp0(gnc_commodity_get_mnemonic(cm), lc->int_curr_symbol))
1111 : 0 : return nice_symbol;
1112 : :
1113 : 7973 : nice_symbol = gnc_commodity_get_default_symbol(cm);
1114 : 7973 : if (nice_symbol && *nice_symbol)
1115 : 7688 : return nice_symbol;
1116 : :
1117 : 285 : return gnc_commodity_get_mnemonic(cm);
1118 : : }
1119 : :
1120 : : /********************************************************************
1121 : : * gnc_commodity_set_mnemonic
1122 : : ********************************************************************/
1123 : :
1124 : : void
1125 : 66988 : gnc_commodity_set_mnemonic(gnc_commodity * cm, const char * mnemonic)
1126 : : {
1127 : : gnc_commodityPrivate* priv;
1128 : :
1129 : 66988 : if (!cm) return;
1130 : 66988 : priv = GET_PRIVATE(cm);
1131 : 66988 : if (priv->mnemonic == mnemonic) return;
1132 : :
1133 : 66943 : gnc_commodity_begin_edit(cm);
1134 : 66943 : CACHE_REMOVE (priv->mnemonic);
1135 : 66943 : priv->mnemonic = CACHE_INSERT(mnemonic);
1136 : :
1137 : 66943 : mark_commodity_dirty (cm);
1138 : 66943 : reset_printname(priv);
1139 : 66943 : reset_unique_name(priv);
1140 : 66943 : gnc_commodity_commit_edit(cm);
1141 : : }
1142 : :
1143 : : /********************************************************************
1144 : : * gnc_commodity_set_namespace
1145 : : ********************************************************************/
1146 : :
1147 : : void
1148 : 66792 : gnc_commodity_set_namespace(gnc_commodity * cm, const char * name_space)
1149 : : {
1150 : : QofBook *book;
1151 : : gnc_commodity_table *table;
1152 : : gnc_commodity_namespace *nsp;
1153 : : gnc_commodityPrivate* priv;
1154 : :
1155 : 66792 : if (!cm) return;
1156 : 66792 : priv = GET_PRIVATE(cm);
1157 : 66792 : book = qof_instance_get_book (&cm->inst);
1158 : 66792 : table = gnc_commodity_table_get_table(book);
1159 : 66792 : nsp = gnc_commodity_table_add_namespace(table, name_space, book);
1160 : 66792 : if (priv->name_space == nsp)
1161 : 351 : return;
1162 : :
1163 : 66441 : gnc_commodity_begin_edit(cm);
1164 : 66441 : priv->name_space = nsp;
1165 : 66441 : if (nsp->iso4217)
1166 : 62908 : priv->quote_source = gnc_quote_source_lookup_by_internal("currency");
1167 : 66441 : mark_commodity_dirty(cm);
1168 : 66441 : reset_printname(priv);
1169 : 66441 : reset_unique_name(priv);
1170 : 66441 : gnc_commodity_commit_edit(cm);
1171 : : }
1172 : :
1173 : : /********************************************************************
1174 : : * gnc_commodity_set_fullname
1175 : : ********************************************************************/
1176 : :
1177 : : void
1178 : 66969 : gnc_commodity_set_fullname(gnc_commodity * cm, const char * fullname)
1179 : : {
1180 : : gnc_commodityPrivate* priv;
1181 : :
1182 : 66969 : if (!cm) return;
1183 : 66969 : priv = GET_PRIVATE(cm);
1184 : 66969 : if (priv->fullname == fullname) return;
1185 : :
1186 : 66924 : CACHE_REMOVE (priv->fullname);
1187 : 66924 : priv->fullname = CACHE_INSERT (fullname);
1188 : :
1189 : 66924 : gnc_commodity_begin_edit(cm);
1190 : 66924 : mark_commodity_dirty(cm);
1191 : 66924 : reset_printname(priv);
1192 : 66924 : gnc_commodity_commit_edit(cm);
1193 : : }
1194 : :
1195 : : /********************************************************************
1196 : : * gnc_commodity_set_cusip
1197 : : ********************************************************************/
1198 : :
1199 : : void
1200 : 66912 : gnc_commodity_set_cusip(gnc_commodity * cm,
1201 : : const char * cusip)
1202 : : {
1203 : : gnc_commodityPrivate* priv;
1204 : :
1205 : 66912 : if (!cm) return;
1206 : :
1207 : 66912 : priv = GET_PRIVATE(cm);
1208 : 66912 : if (priv->cusip == cusip) return;
1209 : :
1210 : 66867 : gnc_commodity_begin_edit(cm);
1211 : 66867 : CACHE_REMOVE (priv->cusip);
1212 : 66867 : priv->cusip = CACHE_INSERT (cusip);
1213 : 66867 : mark_commodity_dirty(cm);
1214 : 66867 : gnc_commodity_commit_edit(cm);
1215 : : }
1216 : :
1217 : : /********************************************************************
1218 : : * gnc_commodity_set_fraction
1219 : : ********************************************************************/
1220 : :
1221 : : void
1222 : 66969 : gnc_commodity_set_fraction(gnc_commodity * cm, int fraction)
1223 : : {
1224 : 66969 : if (!cm) return;
1225 : 66969 : gnc_commodity_begin_edit(cm);
1226 : 66969 : GET_PRIVATE(cm)->fraction = fraction;
1227 : 66969 : mark_commodity_dirty(cm);
1228 : 66969 : gnc_commodity_commit_edit(cm);
1229 : : }
1230 : :
1231 : : /********************************************************************
1232 : : * gnc_commodity_set_auto_quote_control_flag
1233 : : ********************************************************************/
1234 : :
1235 : : void
1236 : 2 : gnc_commodity_set_auto_quote_control_flag(gnc_commodity *cm,
1237 : : const gboolean flag)
1238 : : {
1239 : 2 : ENTER ("(cm=%p, flag=%d)", cm, flag);
1240 : :
1241 : 2 : if (!cm)
1242 : : {
1243 : 0 : LEAVE("");
1244 : 0 : return;
1245 : : }
1246 : 2 : gnc_commodity_begin_edit(cm);
1247 : 3 : auto val = flag ? std::nullopt : std::make_optional<const char*>(g_strdup("false"));
1248 : 4 : qof_instance_set_path_kvp<const char*> (QOF_INSTANCE (cm), val, {"auto_quote_control"});
1249 : 2 : mark_commodity_dirty(cm);
1250 : 2 : gnc_commodity_commit_edit(cm);
1251 : 2 : LEAVE("");
1252 : : }
1253 : :
1254 : : /********************************************************************
1255 : : * gnc_commodity_user_set_quote_flag
1256 : : ********************************************************************/
1257 : :
1258 : : void
1259 : 0 : gnc_commodity_user_set_quote_flag(gnc_commodity *cm, const gboolean flag)
1260 : : {
1261 : : gnc_commodityPrivate* priv;
1262 : :
1263 : 0 : ENTER ("(cm=%p, flag=%d)", cm, flag);
1264 : :
1265 : 0 : if (!cm)
1266 : : {
1267 : 0 : LEAVE("");
1268 : 0 : return;
1269 : : }
1270 : :
1271 : 0 : priv = GET_PRIVATE(cm);
1272 : 0 : gnc_commodity_begin_edit(cm);
1273 : 0 : gnc_commodity_set_quote_flag(cm, flag);
1274 : 0 : if (gnc_commodity_is_iso(cm))
1275 : : {
1276 : : /* For currencies, disable auto quote control if the quote flag is being
1277 : : * changed from its default value and enable it if the quote flag is being
1278 : : * reset to its default value. The defaults for the quote flag are
1279 : : * disabled if no accounts are using the currency, and true otherwise.
1280 : : * Thus enable auto quote control if flag is FALSE and there are not any
1281 : : * accounts using this currency OR flag is TRUE and there are accounts
1282 : : * using this currency; otherwise disable auto quote control */
1283 : 0 : gnc_commodity_set_auto_quote_control_flag(cm,
1284 : 0 : (!flag && (priv->usage_count == 0)) || (flag && (priv->usage_count != 0)));
1285 : : }
1286 : 0 : gnc_commodity_commit_edit(cm);
1287 : 0 : LEAVE("");
1288 : : }
1289 : :
1290 : : /********************************************************************
1291 : : * gnc_commodity_set_quote_flag
1292 : : ********************************************************************/
1293 : :
1294 : : void
1295 : 380 : gnc_commodity_set_quote_flag(gnc_commodity *cm, const gboolean flag)
1296 : : {
1297 : 380 : ENTER ("(cm=%p, flag=%d)", cm, flag);
1298 : :
1299 : 380 : if (!cm) return;
1300 : 380 : gnc_commodity_begin_edit(cm);
1301 : 380 : GET_PRIVATE(cm)->quote_flag = flag;
1302 : 380 : mark_commodity_dirty(cm);
1303 : 380 : gnc_commodity_commit_edit(cm);
1304 : 380 : LEAVE(" ");
1305 : : }
1306 : :
1307 : : /********************************************************************
1308 : : * gnc_commodity_set_quote_source
1309 : : ********************************************************************/
1310 : :
1311 : : void
1312 : 63415 : gnc_commodity_set_quote_source(gnc_commodity *cm, gnc_quote_source *src)
1313 : : {
1314 : 63415 : ENTER ("(cm=%p, src=%p(%s))", cm, src, src ? src->get_internal_name() : "unknown");
1315 : :
1316 : 63415 : if (!cm) return;
1317 : 63415 : gnc_commodity_begin_edit(cm);
1318 : 63415 : GET_PRIVATE(cm)->quote_source = src;
1319 : 63415 : mark_commodity_dirty(cm);
1320 : 63415 : gnc_commodity_commit_edit(cm);
1321 : 63415 : LEAVE(" ");
1322 : : }
1323 : :
1324 : : /********************************************************************
1325 : : * gnc_commodity_set_quote_tz
1326 : : ********************************************************************/
1327 : :
1328 : : void
1329 : 91 : gnc_commodity_set_quote_tz(gnc_commodity *cm, const char *tz)
1330 : : {
1331 : : gnc_commodityPrivate* priv;
1332 : :
1333 : 91 : if (!cm) return;
1334 : :
1335 : 91 : ENTER ("(cm=%p, tz=%s)", cm, tz ? tz : "(null)");
1336 : :
1337 : 91 : priv = GET_PRIVATE(cm);
1338 : :
1339 : 91 : if (tz == priv->quote_tz)
1340 : : {
1341 : 64 : LEAVE("Already correct TZ");
1342 : 64 : return;
1343 : : }
1344 : :
1345 : 27 : gnc_commodity_begin_edit(cm);
1346 : 27 : CACHE_REMOVE (priv->quote_tz);
1347 : 27 : priv->quote_tz = CACHE_INSERT (tz);
1348 : 27 : mark_commodity_dirty(cm);
1349 : 27 : gnc_commodity_commit_edit(cm);
1350 : 27 : LEAVE(" ");
1351 : : }
1352 : :
1353 : : /********************************************************************
1354 : : * gnc_commodity_set_user_symbol
1355 : : ********************************************************************/
1356 : :
1357 : : void
1358 : 7 : gnc_commodity_set_user_symbol(gnc_commodity * cm, const char * user_symbol)
1359 : : {
1360 : : struct lconv *lc;
1361 : :
1362 : 7 : if (!cm) return;
1363 : :
1364 : 7 : ENTER ("(cm=%p, symbol=%s)", cm, user_symbol ? user_symbol : "(null)");
1365 : :
1366 : 7 : lc = gnc_localeconv();
1367 : 7 : if (!user_symbol || !*user_symbol)
1368 : 0 : user_symbol = nullptr;
1369 : 7 : else if (!g_strcmp0(lc->int_curr_symbol, gnc_commodity_get_mnemonic(cm)) &&
1370 : 0 : !g_strcmp0(lc->currency_symbol, user_symbol))
1371 : : /* if the user gives the ISO symbol for the locale currency or the
1372 : : * default symbol, actually remove the user symbol */
1373 : 0 : user_symbol = nullptr;
1374 : 7 : else if (!g_strcmp0(user_symbol, gnc_commodity_get_default_symbol(cm)))
1375 : 0 : user_symbol = nullptr;
1376 : :
1377 : 7 : gnc_commodity_begin_edit (cm);
1378 : :
1379 : 14 : auto val = user_symbol ? std::make_optional<const char*>(g_strdup(user_symbol)) : std::nullopt;
1380 : 14 : qof_instance_set_path_kvp<const char*> (QOF_INSTANCE(cm), val, {"user_symbol"});
1381 : :
1382 : 7 : mark_commodity_dirty(cm);
1383 : 7 : gnc_commodity_commit_edit(cm);
1384 : :
1385 : 7 : LEAVE(" ");
1386 : : }
1387 : :
1388 : : /********************************************************************
1389 : : * gnc_commodity_set_default_symbol
1390 : : * Not made visible in gnc-commodity.h, it is only called from
1391 : : * iso-4217-currencies.c at startup.
1392 : : ********************************************************************/
1393 : : void
1394 : 60836 : gnc_commodity_set_default_symbol(gnc_commodity * cm,
1395 : : const char * default_symbol)
1396 : : {
1397 : 60836 : GET_PRIVATE(cm)->default_symbol = default_symbol;
1398 : 60836 : }
1399 : :
1400 : : /********************************************************************
1401 : : * gnc_commodity_increment_usage_count
1402 : : ********************************************************************/
1403 : :
1404 : : void
1405 : 3064 : gnc_commodity_increment_usage_count(gnc_commodity *cm)
1406 : : {
1407 : : gnc_commodityPrivate* priv;
1408 : :
1409 : 3064 : ENTER("(cm=%p)", cm);
1410 : :
1411 : 3064 : if (!cm)
1412 : : {
1413 : 1 : LEAVE("");
1414 : 1 : return;
1415 : : }
1416 : :
1417 : 3063 : priv = GET_PRIVATE(cm);
1418 : :
1419 : 809 : if ((priv->usage_count == 0) && !priv->quote_flag
1420 : 783 : && gnc_commodity_get_auto_quote_control_flag(cm)
1421 : 3872 : && gnc_commodity_is_iso(cm))
1422 : : {
1423 : : /* compatibility hack - Gnucash 1.8 gets currency quotes when a
1424 : : non-default currency is assigned to an account. */
1425 : 146 : gnc_commodity_begin_edit(cm);
1426 : 146 : gnc_commodity_set_quote_flag(cm, TRUE);
1427 : 146 : gnc_commodity_set_quote_source(cm,
1428 : : gnc_commodity_get_default_quote_source(cm));
1429 : 146 : gnc_commodity_commit_edit(cm);
1430 : : }
1431 : 3063 : priv->usage_count++;
1432 : 3063 : LEAVE("(usage_count=%d)", priv->usage_count);
1433 : : }
1434 : :
1435 : : /********************************************************************
1436 : : * gnc_commodity_decrement_usage_count
1437 : : ********************************************************************/
1438 : :
1439 : : void
1440 : 5164 : gnc_commodity_decrement_usage_count(gnc_commodity *cm)
1441 : : {
1442 : : gnc_commodityPrivate* priv;
1443 : :
1444 : 5164 : ENTER("(cm=%p)", cm);
1445 : :
1446 : 5164 : if (!cm)
1447 : : {
1448 : 3769 : LEAVE("");
1449 : 3769 : return;
1450 : : }
1451 : :
1452 : 1395 : priv = GET_PRIVATE(cm);
1453 : :
1454 : 1395 : if (priv->usage_count == 0)
1455 : : {
1456 : 0 : PWARN("usage_count already zero");
1457 : 0 : LEAVE("");
1458 : 0 : return;
1459 : : }
1460 : :
1461 : 1395 : priv->usage_count--;
1462 : 294 : if ((priv->usage_count == 0) && priv->quote_flag
1463 : 84 : && gnc_commodity_get_auto_quote_control_flag(cm)
1464 : 1689 : && gnc_commodity_is_iso(cm))
1465 : : {
1466 : : /* if this is a currency with auto quote control enabled and no more
1467 : : * accounts reference this currency, disable quote retrieval */
1468 : 82 : gnc_commodity_set_quote_flag(cm, FALSE);
1469 : : }
1470 : 1395 : LEAVE("(usage_count=%d)", priv->usage_count);
1471 : : }
1472 : :
1473 : : /********************************************************************\
1474 : : \********************************************************************/
1475 : :
1476 : :
1477 : : /********************************************************************
1478 : : * gnc_commodity_equiv
1479 : : * are two commodities the same?
1480 : : ********************************************************************/
1481 : :
1482 : : gboolean
1483 : 70328 : gnc_commodity_equiv(const gnc_commodity * a, const gnc_commodity * b)
1484 : : {
1485 : : gnc_commodityPrivate* priv_a;
1486 : : gnc_commodityPrivate* priv_b;
1487 : :
1488 : 70328 : if (a == b) return TRUE;
1489 : 33871 : if (!a || !b) return FALSE;
1490 : :
1491 : 6939 : priv_a = GET_PRIVATE(a);
1492 : 6939 : priv_b = GET_PRIVATE(b);
1493 : 6939 : if (priv_a->name_space != priv_b->name_space) return FALSE;
1494 : 2980 : if (g_strcmp0(priv_a->mnemonic, priv_b->mnemonic) != 0) return FALSE;
1495 : :
1496 : 69 : return TRUE;
1497 : : }
1498 : :
1499 : : gboolean
1500 : 5812 : gnc_commodity_equal(const gnc_commodity * a, const gnc_commodity * b)
1501 : : {
1502 : 5812 : return gnc_commodity_compare(a, b) == 0;
1503 : : }
1504 : :
1505 : : // Used as a sorting callback for deleting old prices, so it needs to be
1506 : : // stable but doesn't need to be in any particular order sensible to humans.
1507 : 5838 : int gnc_commodity_compare(const gnc_commodity * a, const gnc_commodity * b)
1508 : : {
1509 : 5838 : if (a == b) return 0;
1510 : 1740 : if (a && !b) return 1;
1511 : 1732 : if (b && !a) return -1;
1512 : 1706 : return qof_instance_guid_compare(a, b);
1513 : : }
1514 : :
1515 : : // Used as a callback to g_list_find_custom, it should return 0
1516 : : // when the commodities match.
1517 : 0 : int gnc_commodity_compare_void(const void * a, const void * b)
1518 : : {
1519 : 0 : return gnc_commodity_compare(GNC_COMMODITY (a), GNC_COMMODITY (b));
1520 : : }
1521 : :
1522 : : /************************************************************
1523 : : * Namespace functions *
1524 : : ************************************************************/
1525 : : const char *
1526 : 83253 : gnc_commodity_namespace_get_name (const gnc_commodity_namespace *ns)
1527 : : {
1528 : 83253 : if (ns == nullptr)
1529 : 16 : return nullptr;
1530 : 83237 : return ns->name;
1531 : : }
1532 : :
1533 : : const char *
1534 : 0 : gnc_commodity_namespace_get_gui_name (const gnc_commodity_namespace *ns)
1535 : : {
1536 : 0 : if (ns == nullptr)
1537 : 0 : return nullptr;
1538 : 0 : if (g_strcmp0 (ns->name, GNC_COMMODITY_NS_CURRENCY) == 0)
1539 : 0 : return GNC_COMMODITY_NS_ISO_GUI;
1540 : 0 : return ns->name;
1541 : : }
1542 : :
1543 : : GList *
1544 : 0 : gnc_commodity_namespace_get_commodity_list(const gnc_commodity_namespace *name_space)
1545 : : {
1546 : 0 : if (!name_space)
1547 : 0 : return nullptr;
1548 : :
1549 : 0 : return g_list_copy (name_space->cm_list);
1550 : : }
1551 : :
1552 : : gboolean
1553 : 67696 : gnc_commodity_namespace_is_iso(const char *name_space)
1554 : : {
1555 : 72630 : return ((g_strcmp0(name_space, GNC_COMMODITY_NS_ISO) == 0) ||
1556 : 72630 : (g_strcmp0(name_space, GNC_COMMODITY_NS_CURRENCY) == 0));
1557 : : }
1558 : :
1559 : : static const gchar *
1560 : 337293 : gnc_commodity_table_map_namespace(const char * name_space)
1561 : : {
1562 : 337293 : if (g_strcmp0(name_space, GNC_COMMODITY_NS_ISO) == 0)
1563 : 62669 : return GNC_COMMODITY_NS_CURRENCY;
1564 : 274624 : return name_space;
1565 : : }
1566 : :
1567 : : /********************************************************************
1568 : : * gnc_commodity_table_new
1569 : : * make a new commodity table
1570 : : ********************************************************************/
1571 : :
1572 : : gnc_commodity_table *
1573 : 292 : gnc_commodity_table_new(void)
1574 : : {
1575 : 292 : gnc_commodity_table * retval = g_new0(gnc_commodity_table, 1);
1576 : 292 : retval->ns_table = g_hash_table_new(&g_str_hash, &g_str_equal);
1577 : 292 : retval->ns_list = nullptr;
1578 : 292 : return retval;
1579 : : }
1580 : :
1581 : : /********************************************************************
1582 : : * book anchor functions
1583 : : ********************************************************************/
1584 : :
1585 : : gnc_commodity_table *
1586 : 118642 : gnc_commodity_table_get_table(QofBook *book)
1587 : : {
1588 : 118642 : if (!book) return nullptr;
1589 : 118642 : return static_cast<gnc_commodity_table*>(qof_book_get_data (book, GNC_COMMODITY_TABLE));
1590 : : }
1591 : :
1592 : : gnc_commodity *
1593 : 1 : gnc_commodity_obtain_twin (const gnc_commodity *from, QofBook *book)
1594 : : {
1595 : : gnc_commodity *twin;
1596 : : const char * ucom;
1597 : : gnc_commodity_table * comtbl;
1598 : :
1599 : 1 : if (!from) return nullptr;
1600 : 0 : comtbl = gnc_commodity_table_get_table (book);
1601 : 0 : if (!comtbl) return nullptr;
1602 : :
1603 : 0 : ucom = gnc_commodity_get_unique_name (from);
1604 : 0 : twin = gnc_commodity_table_lookup_unique (comtbl, ucom);
1605 : 0 : if (!twin)
1606 : : {
1607 : 0 : twin = gnc_commodity_clone (from, book);
1608 : 0 : twin = gnc_commodity_table_insert (comtbl, twin);
1609 : : }
1610 : 0 : return twin;
1611 : : }
1612 : :
1613 : : /********************************************************************
1614 : : * gnc_commodity_table_get_size
1615 : : * get the size of the commodity table
1616 : : ********************************************************************/
1617 : :
1618 : : static void
1619 : 172 : count_coms(gpointer key, gpointer value, gpointer user_data)
1620 : : {
1621 : 172 : GHashTable *tbl = ((gnc_commodity_namespace*)value)->cm_table;
1622 : 172 : guint *count = (guint*)user_data;
1623 : :
1624 : 172 : if (g_strcmp0((char*)key, GNC_COMMODITY_NS_CURRENCY) == 0)
1625 : : {
1626 : : /* don't count default commodities */
1627 : 8 : return;
1628 : : }
1629 : :
1630 : 164 : if (!value) return;
1631 : :
1632 : 164 : *count += g_hash_table_size(tbl);
1633 : : }
1634 : :
1635 : : guint
1636 : 49 : gnc_commodity_table_get_size(const gnc_commodity_table* tbl)
1637 : : {
1638 : 49 : guint count = 0;
1639 : 49 : g_return_val_if_fail(tbl, 0);
1640 : 49 : g_return_val_if_fail(tbl->ns_table, 0);
1641 : :
1642 : 49 : g_hash_table_foreach(tbl->ns_table, count_coms, (gpointer)&count);
1643 : :
1644 : 49 : return count;
1645 : : }
1646 : :
1647 : : /********************************************************************
1648 : : * gnc_commodity_table_lookup
1649 : : * locate a commodity by namespace and mnemonic.
1650 : : ********************************************************************/
1651 : :
1652 : : gnc_commodity *
1653 : 77268 : gnc_commodity_table_lookup(const gnc_commodity_table * table,
1654 : : const char * name_space, const char * mnemonic)
1655 : : {
1656 : 77268 : gnc_commodity_namespace * nsp = nullptr;
1657 : :
1658 : 77268 : if (!table || !name_space || !mnemonic) return nullptr;
1659 : :
1660 : 77226 : nsp = gnc_commodity_table_find_namespace(table, name_space);
1661 : :
1662 : 77226 : if (nsp)
1663 : : {
1664 : : /*
1665 : : * Backward compatibility support for currencies that have
1666 : : * recently changed.
1667 : : */
1668 : 76085 : if (nsp->iso4217)
1669 : : {
1670 : 68630 : auto it = gnc_new_iso_codes.find (mnemonic);
1671 : 68630 : if (it != gnc_new_iso_codes.end())
1672 : 0 : mnemonic = it->second.c_str();
1673 : : }
1674 : 76085 : return GNC_COMMODITY(g_hash_table_lookup(nsp->cm_table, (gpointer)mnemonic));
1675 : : }
1676 : : else
1677 : : {
1678 : 1141 : return nullptr;
1679 : : }
1680 : : }
1681 : :
1682 : : /********************************************************************
1683 : : * gnc_commodity_table_lookup
1684 : : * locate a commodity by unique name.
1685 : : ********************************************************************/
1686 : :
1687 : : gnc_commodity *
1688 : 0 : gnc_commodity_table_lookup_unique(const gnc_commodity_table *table,
1689 : : const char * unique_name)
1690 : : {
1691 : : char *name_space;
1692 : : char *mnemonic;
1693 : : gnc_commodity *commodity;
1694 : :
1695 : 0 : if (!table || !unique_name) return nullptr;
1696 : :
1697 : 0 : name_space = g_strdup (unique_name);
1698 : 0 : mnemonic = strstr (name_space, "::");
1699 : 0 : if (!mnemonic)
1700 : : {
1701 : 0 : g_free (name_space);
1702 : 0 : return nullptr;
1703 : : }
1704 : :
1705 : 0 : *mnemonic = '\0';
1706 : 0 : mnemonic += 2;
1707 : :
1708 : 0 : commodity = gnc_commodity_table_lookup (table, name_space, mnemonic);
1709 : :
1710 : 0 : g_free (name_space);
1711 : :
1712 : 0 : return commodity;
1713 : : }
1714 : :
1715 : : /********************************************************************
1716 : : * gnc_commodity_table_find_full
1717 : : * locate a commodity by namespace and printable name
1718 : : ********************************************************************/
1719 : :
1720 : : gnc_commodity *
1721 : 0 : gnc_commodity_table_find_full(const gnc_commodity_table * table,
1722 : : const char * name_space,
1723 : : const char * fullname)
1724 : : {
1725 : 0 : gnc_commodity * retval = nullptr;
1726 : : GList * all;
1727 : : GList * iterator;
1728 : :
1729 : 0 : if (!fullname || (fullname[0] == '\0'))
1730 : 0 : return nullptr;
1731 : :
1732 : 0 : all = gnc_commodity_table_get_commodities(table, name_space);
1733 : :
1734 : 0 : for (iterator = all; iterator; iterator = iterator->next)
1735 : : {
1736 : 0 : auto commodity = GNC_COMMODITY (iterator->data);
1737 : 0 : if (!strcmp(fullname,
1738 : : gnc_commodity_get_printname(commodity)))
1739 : : {
1740 : 0 : retval = commodity;
1741 : 0 : break;
1742 : : }
1743 : : }
1744 : :
1745 : 0 : g_list_free (all);
1746 : :
1747 : 0 : return retval;
1748 : : }
1749 : :
1750 : :
1751 : : /********************************************************************
1752 : : * gnc_commodity_table_insert
1753 : : * add a commodity to the table.
1754 : : ********************************************************************/
1755 : :
1756 : : gnc_commodity *
1757 : 62909 : gnc_commodity_table_insert(gnc_commodity_table * table,
1758 : : gnc_commodity * comm)
1759 : : {
1760 : 62909 : gnc_commodity_namespace * nsp = nullptr;
1761 : : gnc_commodity *c;
1762 : : const char *ns_name;
1763 : : gnc_commodityPrivate* priv;
1764 : : QofBook *book;
1765 : :
1766 : 62909 : if (!table) return nullptr;
1767 : 62909 : if (!comm) return nullptr;
1768 : :
1769 : 62909 : priv = GET_PRIVATE(comm);
1770 : :
1771 : 62909 : ENTER ("(table=%p, comm=%p) %s %s", table, comm,
1772 : : (priv->mnemonic == nullptr ? "(null)" : priv->mnemonic),
1773 : : (priv->fullname == nullptr ? "(null)" : priv->fullname));
1774 : 62909 : ns_name = gnc_commodity_namespace_get_name(priv->name_space);
1775 : 62909 : c = gnc_commodity_table_lookup (table, ns_name, priv->mnemonic);
1776 : :
1777 : 62909 : if (c)
1778 : : {
1779 : 48 : if (c == comm)
1780 : : {
1781 : 3 : LEAVE("already in table");
1782 : 3 : return c;
1783 : : }
1784 : :
1785 : : /* Backward compatibility support for currencies that have
1786 : : * recently changed. */
1787 : 45 : if (priv->name_space->iso4217)
1788 : : {
1789 : 25 : auto it = gnc_new_iso_codes.find (priv->mnemonic);
1790 : 25 : if (it != gnc_new_iso_codes.end())
1791 : 0 : gnc_commodity_set_mnemonic(comm, it->second.c_str());
1792 : : }
1793 : 45 : gnc_commodity_copy (c, comm);
1794 : 45 : gnc_commodity_destroy (comm);
1795 : 45 : LEAVE("found at %p", c);
1796 : 45 : return c;
1797 : : }
1798 : :
1799 : : /* Prevent setting anything except template in namespace template. */
1800 : 63129 : if (g_strcmp0 (ns_name, GNC_COMMODITY_NS_TEMPLATE) == 0 &&
1801 : 268 : g_strcmp0 (priv->mnemonic, "template") != 0)
1802 : : {
1803 : 0 : PWARN("Converting commodity %s from namespace template to "
1804 : : "namespace User", priv->mnemonic);
1805 : 0 : gnc_commodity_set_namespace (comm, "User");
1806 : 0 : ns_name = "User";
1807 : 0 : mark_commodity_dirty (comm);
1808 : : }
1809 : :
1810 : 62861 : book = qof_instance_get_book (&comm->inst);
1811 : 62861 : nsp = gnc_commodity_table_add_namespace(table, ns_name, book);
1812 : :
1813 : 62861 : PINFO ("insert %p %s into nsp=%p %s", priv->mnemonic, priv->mnemonic,
1814 : : nsp->cm_table, nsp->name);
1815 : 62861 : g_hash_table_insert(nsp->cm_table,
1816 : 62861 : (gpointer)CACHE_INSERT(priv->mnemonic),
1817 : : (gpointer)comm);
1818 : 62861 : nsp->cm_list = g_list_append(nsp->cm_list, comm);
1819 : :
1820 : 62861 : qof_event_gen (&comm->inst, QOF_EVENT_ADD, nullptr);
1821 : 62861 : LEAVE ("(table=%p, comm=%p)", table, comm);
1822 : 62861 : return comm;
1823 : : }
1824 : :
1825 : : /********************************************************************
1826 : : * gnc_commodity_table_remove
1827 : : * remove a commodity from the table.
1828 : : ********************************************************************/
1829 : :
1830 : : void
1831 : 41825 : gnc_commodity_table_remove(gnc_commodity_table * table,
1832 : : gnc_commodity * comm)
1833 : : {
1834 : : gnc_commodity_namespace * nsp;
1835 : : gnc_commodity *c;
1836 : : gnc_commodityPrivate* priv;
1837 : : const char *ns_name;
1838 : :
1839 : 41825 : if (!table) return;
1840 : 4593 : if (!comm) return;
1841 : :
1842 : 4593 : priv = GET_PRIVATE(comm);
1843 : 4593 : ns_name = gnc_commodity_namespace_get_name(priv->name_space);
1844 : 4593 : c = gnc_commodity_table_lookup (table, ns_name, priv->mnemonic);
1845 : 4593 : if (c != comm) return;
1846 : :
1847 : 65 : qof_event_gen (&comm->inst, QOF_EVENT_REMOVE, nullptr);
1848 : :
1849 : 65 : nsp = gnc_commodity_table_find_namespace(table, ns_name);
1850 : 65 : if (!nsp) return;
1851 : :
1852 : 65 : nsp->cm_list = g_list_remove(nsp->cm_list, comm);
1853 : 65 : g_hash_table_remove (nsp->cm_table, priv->mnemonic);
1854 : : /* XXX minor mem leak, should remove the key as well */
1855 : : }
1856 : :
1857 : : /********************************************************************
1858 : : * gnc_commodity_table_has_namespace
1859 : : * see if the commodities namespace exists. May have zero commodities.
1860 : : ********************************************************************/
1861 : :
1862 : : int
1863 : 20 : gnc_commodity_table_has_namespace(const gnc_commodity_table * table,
1864 : : const char * name_space)
1865 : : {
1866 : 20 : gnc_commodity_namespace * nsp = nullptr;
1867 : :
1868 : 20 : if (!table || !name_space)
1869 : : {
1870 : 0 : return 0;
1871 : : }
1872 : :
1873 : 20 : nsp = gnc_commodity_table_find_namespace(table, name_space);
1874 : 20 : if (nsp)
1875 : : {
1876 : 20 : return 1;
1877 : : }
1878 : : else
1879 : : {
1880 : 0 : return 0;
1881 : : }
1882 : : }
1883 : :
1884 : : static void
1885 : 1636 : hash_keys_helper(gpointer key, gpointer value, gpointer data)
1886 : : {
1887 : 1636 : auto l = (GList**)data;
1888 : 1636 : *l = g_list_prepend(*l, key);
1889 : 1636 : }
1890 : :
1891 : : static GList *
1892 : 238 : g_hash_table_keys(GHashTable * table)
1893 : : {
1894 : 238 : GList * l = nullptr;
1895 : 238 : g_hash_table_foreach(table, &hash_keys_helper, (gpointer) &l);
1896 : 238 : return l;
1897 : : }
1898 : :
1899 : : static void
1900 : 10164 : hash_values_helper(gpointer key, gpointer value, gpointer data)
1901 : : {
1902 : 10164 : auto l = (GList**)data;
1903 : 10164 : *l = g_list_prepend(*l, value);
1904 : 10164 : }
1905 : :
1906 : : static GList *
1907 : 240 : g_hash_table_values(GHashTable * table)
1908 : : {
1909 : 240 : GList * l = nullptr;
1910 : 240 : g_hash_table_foreach(table, &hash_values_helper, (gpointer) &l);
1911 : 240 : return l;
1912 : : }
1913 : :
1914 : : /********************************************************************
1915 : : * gnc_commodity_table_get_namespaces
1916 : : * see if any commodities in the namespace exist
1917 : : ********************************************************************/
1918 : :
1919 : : GList *
1920 : 238 : gnc_commodity_table_get_namespaces(const gnc_commodity_table * table)
1921 : : {
1922 : 238 : if (!table)
1923 : 0 : return nullptr;
1924 : :
1925 : 238 : return g_hash_table_keys(table->ns_table);
1926 : : }
1927 : :
1928 : : GList *
1929 : 2 : gnc_commodity_table_get_namespaces_list(const gnc_commodity_table * table)
1930 : : {
1931 : 2 : if (!table)
1932 : 0 : return nullptr;
1933 : :
1934 : 2 : return g_list_copy (table->ns_list);
1935 : : }
1936 : :
1937 : : /* Because gnc_commodity_table_add_namespace maps GNC_COMMODITY_NS_ISO to
1938 : : GNC_COMMODITY_NS_CURRENCY and then sets iso4217 if the namespace is
1939 : : either of these, the net result is that the iso4217 bit is set only
1940 : : for GNC_COMMODITY_NS_CURRENCY. This means that gnc_commodity_is_iso is
1941 : : a subset of gnc_commodity_is_currency. Most callers seem to use
1942 : : gnc_commodity_is_iso. */
1943 : : gboolean
1944 : 30829 : gnc_commodity_is_iso(const gnc_commodity * cm)
1945 : : {
1946 : : gnc_commodityPrivate* priv;
1947 : :
1948 : 30829 : if (!cm) return FALSE;
1949 : :
1950 : 30829 : priv = GET_PRIVATE(cm);
1951 : 30829 : if ( !priv->name_space) return FALSE;
1952 : 30641 : return priv->name_space->iso4217;
1953 : : }
1954 : :
1955 : : gboolean
1956 : 3204 : gnc_commodity_is_currency(const gnc_commodity *cm)
1957 : : {
1958 : : const char *ns_name;
1959 : 3204 : if (!cm) return FALSE;
1960 : :
1961 : 3201 : ns_name = gnc_commodity_namespace_get_name(GET_PRIVATE(cm)->name_space);
1962 : 6402 : return (!g_strcmp0(ns_name, GNC_COMMODITY_NS_LEGACY) ||
1963 : 6402 : !g_strcmp0(ns_name, GNC_COMMODITY_NS_CURRENCY));
1964 : : }
1965 : :
1966 : : /********************************************************************
1967 : : * gnc_commodity_table_get_commodities
1968 : : * list commodities in a given namespace
1969 : : ********************************************************************/
1970 : :
1971 : : static CommodityList*
1972 : 0 : commodity_table_get_all_noncurrency_commodities(const gnc_commodity_table* table)
1973 : : {
1974 : 0 : GList *node = nullptr, *nslist = gnc_commodity_table_get_namespaces(table);
1975 : 0 : CommodityList *retval = nullptr;
1976 : 0 : for (node = nslist; node; node=g_list_next(node))
1977 : : {
1978 : 0 : gnc_commodity_namespace *ns = nullptr;
1979 : 0 : if (g_strcmp0((char*)(node->data), GNC_COMMODITY_NS_CURRENCY) == 0
1980 : 0 : || g_strcmp0((char*)(node->data), GNC_COMMODITY_NS_TEMPLATE) == 0)
1981 : 0 : continue;
1982 : 0 : ns = gnc_commodity_table_find_namespace(table, (char*)(node->data));
1983 : 0 : if (!ns)
1984 : 0 : continue;
1985 : 0 : retval = g_list_concat(g_hash_table_values(ns->cm_table), retval);
1986 : : }
1987 : 0 : g_list_free(nslist);
1988 : 0 : return retval;
1989 : : }
1990 : :
1991 : : CommodityList *
1992 : 240 : gnc_commodity_table_get_commodities(const gnc_commodity_table * table,
1993 : : const char * name_space)
1994 : : {
1995 : 240 : gnc_commodity_namespace * ns = nullptr;
1996 : :
1997 : 240 : if (!table)
1998 : 0 : return nullptr;
1999 : 240 : if (g_strcmp0(name_space, GNC_COMMODITY_NS_NONISO_GUI) == 0)
2000 : 0 : return commodity_table_get_all_noncurrency_commodities(table);
2001 : 240 : ns = gnc_commodity_table_find_namespace(table, name_space);
2002 : 240 : if (!ns)
2003 : 0 : return nullptr;
2004 : :
2005 : 240 : return g_hash_table_values(ns->cm_table);
2006 : : }
2007 : :
2008 : : /********************************************************************
2009 : : * gnc_commodity_table_get_quotable_commodities
2010 : : * list commodities in a given namespace that get price quotes
2011 : : ********************************************************************/
2012 : :
2013 : : static void
2014 : 0 : get_quotables_helper1(gpointer key, gpointer value, gpointer data)
2015 : : {
2016 : 0 : auto comm = GNC_COMMODITY(value);
2017 : 0 : gnc_commodityPrivate* priv = GET_PRIVATE(comm);
2018 : 0 : auto l = static_cast<GList**>(data);
2019 : :
2020 : 0 : if (!priv->quote_flag || !priv->quote_source || !priv->quote_source->get_supported())
2021 : 0 : return;
2022 : 0 : *l = g_list_prepend(*l, value);
2023 : : }
2024 : :
2025 : : static gboolean
2026 : 0 : get_quotables_helper2 (gnc_commodity *comm, gpointer data)
2027 : : {
2028 : 0 : auto l = static_cast<GList**>(data);
2029 : 0 : gnc_commodityPrivate* priv = GET_PRIVATE(comm);
2030 : :
2031 : 0 : if (!priv->quote_flag || priv->quote_source || !priv->quote_source->get_supported())
2032 : 0 : return TRUE;
2033 : 0 : *l = g_list_prepend(*l, comm);
2034 : 0 : return TRUE;
2035 : : }
2036 : :
2037 : : CommodityList *
2038 : 0 : gnc_commodity_table_get_quotable_commodities(const gnc_commodity_table * table)
2039 : : {
2040 : 0 : gnc_commodity_namespace * ns = nullptr;
2041 : : const char *name_space;
2042 : : GList * nslist, * tmp;
2043 : 0 : GList * l = nullptr;
2044 : : regex_t pattern;
2045 : 0 : const char *expression = gnc_prefs_get_namespace_regexp();
2046 : :
2047 : 0 : ENTER("table=%p, expression=%s", table, expression);
2048 : 0 : if (!table)
2049 : 0 : return nullptr;
2050 : :
2051 : 0 : if (expression && *expression)
2052 : : {
2053 : 0 : if (regcomp(&pattern, expression, REG_EXTENDED | REG_ICASE) != 0)
2054 : : {
2055 : 0 : LEAVE("Cannot compile regex");
2056 : 0 : return nullptr;
2057 : : }
2058 : :
2059 : 0 : nslist = gnc_commodity_table_get_namespaces(table);
2060 : 0 : for (tmp = nslist; tmp; tmp = tmp->next)
2061 : : {
2062 : 0 : name_space = static_cast<const char*>(tmp->data);
2063 : 0 : if (regexec(&pattern, name_space, 0, nullptr, 0) == 0)
2064 : : {
2065 : 0 : DEBUG("Running list of %s commodities", name_space);
2066 : 0 : ns = gnc_commodity_table_find_namespace(table, name_space);
2067 : 0 : if (ns)
2068 : : {
2069 : 0 : g_hash_table_foreach(ns->cm_table, &get_quotables_helper1, (gpointer) &l);
2070 : : }
2071 : : }
2072 : : }
2073 : 0 : g_list_free(nslist);
2074 : 0 : regfree(&pattern);
2075 : : }
2076 : : else
2077 : : {
2078 : 0 : gnc_commodity_table_foreach_commodity(table, get_quotables_helper2,
2079 : : (gpointer) &l);
2080 : : }
2081 : 0 : LEAVE("list head %p", l);
2082 : 0 : return l;
2083 : : }
2084 : :
2085 : : /********************************************************************
2086 : : * gnc_commodity_table_add_namespace
2087 : : * add an empty namespace if it does not exist
2088 : : ********************************************************************/
2089 : :
2090 : : /* GObject Initialization */
2091 : 2292 : QOF_GOBJECT_IMPL(gnc_commodity_namespace, gnc_commodity_namespace, QOF_TYPE_INSTANCE)
2092 : :
2093 : : static void
2094 : 913 : gnc_commodity_namespace_init(gnc_commodity_namespace* ns)
2095 : : {
2096 : 913 : }
2097 : :
2098 : : static void
2099 : 564 : gnc_commodity_namespace_dispose_real (GObject *nsp)
2100 : : {
2101 : 564 : }
2102 : :
2103 : : static void
2104 : 564 : gnc_commodity_namespace_finalize_real(GObject* nsp)
2105 : : {
2106 : 564 : }
2107 : :
2108 : : gnc_commodity_namespace *
2109 : 129921 : gnc_commodity_table_add_namespace(gnc_commodity_table * table,
2110 : : const char * name_space,
2111 : : QofBook *book)
2112 : : {
2113 : 129921 : gnc_commodity_namespace * ns = nullptr;
2114 : :
2115 : 129921 : if (!table) return nullptr;
2116 : :
2117 : 129589 : name_space = gnc_commodity_table_map_namespace(name_space);
2118 : 129589 : ns = gnc_commodity_table_find_namespace(table, name_space);
2119 : 129589 : if (!ns)
2120 : : {
2121 : 913 : ns = static_cast<gnc_commodity_namespace*>(g_object_new(GNC_TYPE_COMMODITY_NAMESPACE, nullptr));
2122 : 913 : ns->cm_table = g_hash_table_new(g_str_hash, g_str_equal);
2123 : 913 : ns->name = CACHE_INSERT(static_cast<const char*>(name_space));
2124 : 913 : ns->iso4217 = gnc_commodity_namespace_is_iso(name_space);
2125 : 913 : qof_instance_init_data (&ns->inst, GNC_ID_COMMODITY_NAMESPACE, book);
2126 : 913 : qof_event_gen (&ns->inst, QOF_EVENT_CREATE, nullptr);
2127 : :
2128 : 913 : g_hash_table_insert(table->ns_table,
2129 : 913 : (gpointer) ns->name,
2130 : : (gpointer) ns);
2131 : 913 : table->ns_list = g_list_append(table->ns_list, ns);
2132 : 913 : qof_event_gen (&ns->inst, QOF_EVENT_ADD, nullptr);
2133 : : }
2134 : 129589 : return ns;
2135 : : }
2136 : :
2137 : :
2138 : : gnc_commodity_namespace *
2139 : 207704 : gnc_commodity_table_find_namespace(const gnc_commodity_table * table,
2140 : : const char * name_space)
2141 : : {
2142 : 207704 : if (!table || !name_space)
2143 : 0 : return nullptr;
2144 : :
2145 : 207704 : name_space = gnc_commodity_table_map_namespace(name_space);
2146 : 207704 : return static_cast<gnc_commodity_namespace*>(g_hash_table_lookup(table->ns_table, (gpointer)name_space));
2147 : : }
2148 : :
2149 : :
2150 : : gnc_commodity *
2151 : 38 : gnc_commodity_find_commodity_by_guid(const GncGUID *guid, QofBook *book)
2152 : : {
2153 : : QofCollection *col;
2154 : 38 : if (!guid || !book) return nullptr;
2155 : 38 : col = qof_book_get_collection (book, GNC_ID_COMMODITY);
2156 : 38 : return (gnc_commodity *) qof_collection_lookup_entity (col, guid);
2157 : : }
2158 : :
2159 : : /********************************************************************
2160 : : * gnc_commodity_table_delete_namespace
2161 : : * delete a namespace
2162 : : ********************************************************************/
2163 : :
2164 : : static int
2165 : 38141 : ns_helper(gpointer key, gpointer value, gpointer user_data)
2166 : : {
2167 : 38141 : auto c = GNC_COMMODITY(value);
2168 : 38141 : gnc_commodity_destroy(c);
2169 : 38141 : CACHE_REMOVE(static_cast<char*>(key)); /* key is commodity mnemonic */
2170 : 38141 : return TRUE;
2171 : : }
2172 : :
2173 : : void
2174 : 564 : gnc_commodity_table_delete_namespace(gnc_commodity_table * table,
2175 : : const char * name_space)
2176 : : {
2177 : : gnc_commodity_namespace * ns;
2178 : :
2179 : 564 : if (!table) return;
2180 : :
2181 : 564 : ns = gnc_commodity_table_find_namespace(table, name_space);
2182 : 564 : if (!ns)
2183 : 0 : return;
2184 : :
2185 : 564 : qof_event_gen (&ns->inst, QOF_EVENT_REMOVE, nullptr);
2186 : 564 : g_hash_table_remove(table->ns_table, name_space);
2187 : 564 : table->ns_list = g_list_remove(table->ns_list, ns);
2188 : :
2189 : 564 : g_list_free(ns->cm_list);
2190 : 564 : ns->cm_list = nullptr;
2191 : :
2192 : 564 : g_hash_table_foreach_remove(ns->cm_table, ns_helper, nullptr);
2193 : 564 : g_hash_table_destroy(ns->cm_table);
2194 : 564 : CACHE_REMOVE(ns->name);
2195 : :
2196 : 564 : qof_event_gen (&ns->inst, QOF_EVENT_DESTROY, nullptr);
2197 : : /* qof_instance_release(&ns->inst); */
2198 : 564 : g_object_unref(ns);
2199 : : }
2200 : :
2201 : : /********************************************************************
2202 : : * gnc_commodity_table_foreach_commodity
2203 : : * call user-defined function once for every commodity in every
2204 : : * namespace
2205 : : ********************************************************************/
2206 : :
2207 : : typedef struct
2208 : : {
2209 : : gboolean ok;
2210 : : gboolean (*func)(gnc_commodity *, gpointer);
2211 : : gpointer user_data;
2212 : : } IterData;
2213 : :
2214 : : static void
2215 : 5100 : iter_commodity (gpointer key, gpointer value, gpointer user_data)
2216 : : {
2217 : 5100 : IterData *iter_data = (IterData *) user_data;
2218 : 5100 : gnc_commodity *cm = (gnc_commodity *) value;
2219 : :
2220 : 5100 : if (iter_data->ok)
2221 : : {
2222 : 5100 : iter_data->ok = (iter_data->func)(cm, iter_data->user_data);
2223 : : }
2224 : 5100 : }
2225 : :
2226 : : static void
2227 : 67 : iter_namespace (gpointer key, gpointer value, gpointer user_data)
2228 : : {
2229 : 67 : GHashTable *namespace_hash = ((gnc_commodity_namespace *) value)->cm_table;
2230 : 67 : g_hash_table_foreach (namespace_hash, iter_commodity, user_data);
2231 : 67 : }
2232 : :
2233 : : gboolean
2234 : 26 : gnc_commodity_table_foreach_commodity (const gnc_commodity_table * tbl,
2235 : : gboolean (*f)(gnc_commodity *, gpointer),
2236 : : gpointer user_data)
2237 : : {
2238 : : IterData iter_data;
2239 : :
2240 : 26 : if (!tbl || !f) return FALSE;
2241 : :
2242 : 26 : iter_data.ok = TRUE;
2243 : 26 : iter_data.func = f;
2244 : 26 : iter_data.user_data = user_data;
2245 : :
2246 : 26 : g_hash_table_foreach(tbl->ns_table, iter_namespace, (gpointer)&iter_data);
2247 : :
2248 : 26 : return iter_data.ok;
2249 : : }
2250 : :
2251 : : /********************************************************************
2252 : : * gnc_commodity_table_destroy
2253 : : * cleanup and free.
2254 : : ********************************************************************/
2255 : :
2256 : : void
2257 : 188 : gnc_commodity_table_destroy(gnc_commodity_table * t)
2258 : : {
2259 : : gnc_commodity_namespace * ns;
2260 : : GList *item, *next;
2261 : :
2262 : 188 : if (!t) return;
2263 : 188 : ENTER ("table=%p", t);
2264 : :
2265 : 752 : for (item = t->ns_list; item; item = next)
2266 : : {
2267 : 564 : next = g_list_next(item);
2268 : 564 : ns = static_cast<gnc_commodity_namespace*>(item->data);
2269 : 564 : gnc_commodity_table_delete_namespace(t, ns->name);
2270 : : }
2271 : :
2272 : 188 : g_list_free(t->ns_list);
2273 : 188 : t->ns_list = nullptr;
2274 : 188 : g_hash_table_destroy(t->ns_table);
2275 : 188 : t->ns_table = nullptr;
2276 : 188 : LEAVE ("table=%p", t);
2277 : 188 : g_free(t);
2278 : : }
2279 : :
2280 : : /* =========================================================== */
2281 : :
2282 : : /********************************************************************
2283 : : * gnc_commodity_table_add_default_data
2284 : : ********************************************************************/
2285 : :
2286 : : #define CUR_I18N(String) dgettext ("iso_4217", String)
2287 : :
2288 : : gboolean
2289 : 268 : gnc_commodity_table_add_default_data(gnc_commodity_table *table, QofBook *book)
2290 : : {
2291 : : QofCollection *col;
2292 : : gnc_commodity* c;
2293 : :
2294 : 268 : ENTER ("table=%p", table);
2295 : 268 : gnc_commodity_table_add_namespace(table, GNC_COMMODITY_NS_TEMPLATE, book);
2296 : 268 : c = gnc_commodity_new(book, "template", GNC_COMMODITY_NS_TEMPLATE, "template", "template", 1);
2297 : 268 : gnc_commodity_table_insert(table, c);
2298 : :
2299 : : #include "iso-4217-currencies.c"
2300 : :
2301 : : /* We've just created the default namespaces and currencies. Mark
2302 : : * these collections as clean because there is no USER entered data
2303 : : * in these collections as of yet. */
2304 : 268 : col = qof_book_get_collection(book, GNC_ID_COMMODITY);
2305 : 268 : qof_collection_mark_clean(col);
2306 : 268 : col = qof_book_get_collection(book, GNC_ID_COMMODITY_NAMESPACE);
2307 : 268 : qof_collection_mark_clean(col);
2308 : :
2309 : 268 : LEAVE ("table=%p", table);
2310 : 268 : return TRUE;
2311 : : }
2312 : :
2313 : : /********************************************************************
2314 : : ********************************************************************/
2315 : : /* QofObject function implementation and registration */
2316 : :
2317 : : #ifdef _MSC_VER
2318 : : /* MSVC compiler doesn't have C99 "designated initializers"
2319 : : * so we wrap them in a macro that is empty on MSVC. */
2320 : : # define DI(x) /* */
2321 : : #else
2322 : : # define DI(x) x
2323 : : #endif
2324 : : static QofObject commodity_object_def =
2325 : : {
2326 : : DI(.interface_version = ) QOF_OBJECT_VERSION,
2327 : : DI(.e_type = ) GNC_ID_COMMODITY,
2328 : : DI(.type_label = ) "Commodity",
2329 : : DI(.create = ) nullptr,
2330 : : DI(.book_begin = ) nullptr,
2331 : : DI(.book_end = ) nullptr,
2332 : : DI(.is_dirty = ) qof_collection_is_dirty,
2333 : : DI(.mark_clean = ) qof_collection_mark_clean,
2334 : : DI(.foreach = ) qof_collection_foreach,
2335 : : DI(.printable = ) (const char * (*)(gpointer)) gnc_commodity_get_fullname,
2336 : : };
2337 : :
2338 : : static QofObject namespace_object_def =
2339 : : {
2340 : : DI(.interface_version = ) QOF_OBJECT_VERSION,
2341 : : DI(.e_type = ) GNC_ID_COMMODITY_NAMESPACE,
2342 : : DI(.type_label = ) "Namespace",
2343 : : DI(.create = ) nullptr,
2344 : : DI(.book_begin = ) nullptr,
2345 : : DI(.book_end = ) nullptr,
2346 : : DI(.is_dirty = ) nullptr,
2347 : : DI(.mark_clean = ) nullptr,
2348 : : DI(.foreach = ) nullptr,
2349 : : DI(.printable = ) nullptr,
2350 : : };
2351 : :
2352 : : static void
2353 : 279 : commodity_table_book_begin (QofBook *book)
2354 : : {
2355 : : gnc_commodity_table *ct;
2356 : 279 : ENTER ("book=%p", book);
2357 : :
2358 : 279 : if (gnc_commodity_table_get_table(book))
2359 : 15 : return;
2360 : :
2361 : 264 : ct = gnc_commodity_table_new ();
2362 : 264 : qof_book_set_data (book, GNC_COMMODITY_TABLE, ct);
2363 : :
2364 : 264 : if (!gnc_commodity_table_add_default_data(ct, book))
2365 : : {
2366 : 0 : PWARN("unable to initialize book's commodity_table");
2367 : : }
2368 : :
2369 : 264 : LEAVE ("book=%p", book);
2370 : : }
2371 : :
2372 : : static void
2373 : 175 : commodity_table_book_end (QofBook *book)
2374 : : {
2375 : : gnc_commodity_table *ct;
2376 : :
2377 : 175 : ct = gnc_commodity_table_get_table (book);
2378 : 175 : qof_book_set_data (book, GNC_COMMODITY_TABLE, nullptr);
2379 : 175 : gnc_commodity_table_destroy (ct);
2380 : 175 : }
2381 : :
2382 : : static QofObject commodity_table_object_def =
2383 : : {
2384 : : DI(.interface_version = ) QOF_OBJECT_VERSION,
2385 : : DI(.e_type = ) GNC_ID_COMMODITY_TABLE,
2386 : : DI(.type_label = ) "CommodityTable",
2387 : : DI(.create = ) nullptr,
2388 : : DI(.book_begin = ) commodity_table_book_begin,
2389 : : DI(.book_end = ) commodity_table_book_end,
2390 : : DI(.is_dirty = ) qof_collection_is_dirty,
2391 : : DI(.mark_clean = ) qof_collection_mark_clean,
2392 : : DI(.foreach = ) nullptr,
2393 : : DI(.printable = ) nullptr,
2394 : : DI(.version_cmp = ) nullptr,
2395 : : };
2396 : :
2397 : : gboolean
2398 : 100 : gnc_commodity_table_register (void)
2399 : : {
2400 : 100 : if (!qof_object_register (&commodity_object_def))
2401 : 1 : return FALSE;
2402 : 99 : if (!qof_object_register (&namespace_object_def))
2403 : 0 : return FALSE;
2404 : 99 : return qof_object_register (&commodity_table_object_def);
2405 : : }
2406 : :
2407 : : /* *******************************************************************
2408 : : * gnc_monetary methods
2409 : : ********************************************************************/
2410 : :
2411 : : /** Add a gnc_monetary to the list */
2412 : : MonetaryList *
2413 : 44 : gnc_monetary_list_add_monetary(MonetaryList *list, gnc_monetary add_mon)
2414 : : {
2415 : 44 : MonetaryList *l = list, *tmp;
2416 : 70 : for (tmp = list; tmp; tmp = tmp->next)
2417 : : {
2418 : 49 : auto list_mon = static_cast<gnc_monetary*>(tmp->data);
2419 : 49 : if (gnc_commodity_equiv(list_mon->commodity, add_mon.commodity))
2420 : : {
2421 : 23 : list_mon->value = gnc_numeric_add(list_mon->value, add_mon.value,
2422 : : GNC_DENOM_AUTO, GNC_HOW_DENOM_EXACT);
2423 : 23 : break;
2424 : : }
2425 : : }
2426 : :
2427 : : /* See if we found an entry, and add one if not */
2428 : 44 : if (tmp == nullptr)
2429 : : {
2430 : 21 : auto new_mon = static_cast<gnc_monetary*>(g_new0(gnc_monetary, 1));
2431 : 21 : *new_mon = add_mon;
2432 : 21 : l = g_list_prepend(l, new_mon);
2433 : : }
2434 : :
2435 : 44 : return l;
2436 : : }
2437 : :
2438 : : /** Delete all entries in the list that have zero value. Return list
2439 : : pointer will be a null pointer if there are no non-zero entries **/
2440 : : MonetaryList *
2441 : 12 : gnc_monetary_list_delete_zeros(MonetaryList *list)
2442 : : {
2443 : : MonetaryList *node, *next;
2444 : 33 : for (node = list; node; node = next)
2445 : : {
2446 : 21 : auto mon = static_cast<gnc_monetary*>(node->data);
2447 : 21 : next = node->next;
2448 : 21 : if (gnc_numeric_zero_p(mon->value))
2449 : : {
2450 : 10 : g_free(mon);
2451 : 10 : list = g_list_delete_link(list, node);
2452 : : }
2453 : : }
2454 : 12 : return list;
2455 : : }
2456 : :
2457 : : /** Free a MonetaryList and all the monetaries it points to */
2458 : : void
2459 : 11 : gnc_monetary_list_free(MonetaryList *list)
2460 : : {
2461 : : MonetaryList *tmp;
2462 : 22 : for (tmp = list; tmp; tmp = tmp->next)
2463 : : {
2464 : 11 : g_free(tmp->data);
2465 : : }
2466 : :
2467 : 11 : g_list_free(list);
2468 : 11 : }
2469 : :
2470 : : /* ========================= END OF FILE ============================== */
|