Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gnc_commodity_table_get_namespaces returns a vector #1910

Draft
wants to merge 6 commits into
base: stable
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion bindings/engine-common.i
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ static const GncGUID * gncAccountGetGUID(Account *x)
%typemap(newfree) TransList * "g_list_free($1);"
%typemap(newfree) PriceList * "g_list_free($1);"
%typemap(newfree) LotList * "g_list_free($1);"
%typemap(newfree) CommodityList * "g_list_free($1);"

%typemap(freearg) AccountList * "g_list_free($1);"

Expand Down
2 changes: 1 addition & 1 deletion bindings/engine.i
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ GLIST_HELPER_INOUT(LotList, SWIGTYPE_p_GNCLot);
GLIST_HELPER_INOUT(AccountList, SWIGTYPE_p_Account);
GLIST_HELPER_INOUT(PriceList, SWIGTYPE_p_GNCPrice);
// TODO: free PriceList?
GLIST_HELPER_INOUT(CommodityList, SWIGTYPE_p_gnc_commodity);
VECTOR_HELPER_INOUT(CommVec, SWIGTYPE_p_gnc_commodity, gnc_commodity);
VECTOR_HELPER_INOUT(SplitsVec, SWIGTYPE_p_Split, Split);
VECTORREF_HELPER_INOUT(SplitsVec&, SWIGTYPE_p_Split, Split);
VECTOR_HELPER_INOUT(AccountVec, SWIGTYPE_p_Account, Account);
Expand Down
24 changes: 6 additions & 18 deletions bindings/guile/gnc-optiondb.i
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ namespace std {

%begin
%{
#include <gnc-commodity.hpp>
#include <gnc-optiondb.h>
#include <gnc-optiondb.hpp>
#include <gnc-optiondb-impl.hpp>
Expand Down Expand Up @@ -1975,26 +1976,13 @@ gnc_register_multichoice_callback_option(GncOptionDBPtr& db,
const char* key, const char* doc_string,
const char *value)
{
gnc_commodity* commodity{};
const auto book{qof_session_get_book(gnc_get_current_session())};
const auto commodity_table{gnc_commodity_table_get_table(book)};
const auto namespaces{gnc_commodity_table_get_namespaces(commodity_table)};
GncOption* rv = nullptr;
for (auto node = namespaces; node && commodity == nullptr;
node = g_list_next(node))
{
commodity = gnc_commodity_table_lookup(commodity_table,
(const char*)(node->data),
value);

if (commodity)
{
rv = gnc_make_commodity_option(section, name, key, doc_string, commodity);
break;
}
}
g_list_free (namespaces);
return rv;
for (const auto& name_space : gnc_commodity_table_get_namespaces(commodity_table))
if (auto commodity = gnc_commodity_table_lookup (commodity_table, name_space.c_str(),
value))
return gnc_make_commodity_option(section, name, key, doc_string, commodity);
return nullptr;
}

static GncOption*
Expand Down
8 changes: 8 additions & 0 deletions bindings/python/gnucash_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,14 @@ class GncCommodityTable(GnuCashCoreClass):
which includes most of the world's currencies.
"""

def get_quotable_commodities (self):
print ("hello from get_quotable_commodities")
return

def get_commodities(self):
print ("hello from get_commodities")
return

def _get_namespaces_py(self):
return [ns.get_name() for ns in self.get_namespaces_list()]

Expand Down
43 changes: 42 additions & 1 deletion common/base-typemaps.i
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ typedef char gchar;
}
}

%typemap(out) GList *, CommodityList *, SplitList *, AccountList *, LotList *,
%typemap(out) GList *, SplitList *, AccountList *, LotList *,
MonetaryList *, PriceList *, EntryList * {
gpointer data;
GList *l;
Expand Down Expand Up @@ -332,4 +332,45 @@ typedef char gchar;
}
$result = list;
}

%typemap(out) CommodityVec
{
PyObject *list = PyList_New(0);
for (auto data : $1)
{
if (GNC_IS_ACCOUNT(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_Account, 0));
else if (GNC_IS_SPLIT(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_Split, 0));
else if (GNC_IS_TRANSACTION(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_Transaction, 0));
else if (GNC_IS_COMMODITY(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_gnc_commodity, 0));
else if (GNC_IS_COMMODITY_NAMESPACE(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_gnc_commodity_namespace, 0));
else if (GNC_IS_LOT(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_GNCLot, 0));
else if (GNC_IS_PRICE(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_GNCPrice, 0));
else if (GNC_IS_INVOICE(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p__gncInvoice, 0));
else if (GNC_IS_ENTRY(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p__gncEntry, 0));
else if (GNC_IS_CUSTOMER(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p__gncCustomer, 0));
else if (GNC_IS_VENDOR(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p__gncVendor, 0));
else if (GNC_IS_EMPLOYEE(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p__gncEmployee, 0));
else if (GNC_IS_JOB(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p__gncJob, 0));
else if (GNC_IS_TAXTABLE(data))
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p__gncTaxTable, 0));
else if ($1_descriptor == $descriptor(MonetaryList *))
PyList_Append(list, SWIG_NewPointerObj(data, $descriptor(gnc_monetary *), 0));
else
PyList_Append(list, SWIG_NewPointerObj(data, SWIGTYPE_p_void, 0));
}
$result = list;
}
#endif
60 changes: 21 additions & 39 deletions gnucash/gnome-utils/dialog-commodity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@

#include "dialog-commodity.h"
#include "dialog-utils.h"
#include "gnc-commodity.hpp"
#include "gnc-engine.h"
#include "gnc-gtk-utils.h"
#include "gnc-gui-query.h"
#include "gnc-ui-util.h"
#include "gnc-ui.h"

#include <algorithm>

/* This static indicates the debugging module that this .o belongs to. */
static QofLogModule log_module = GNC_MOD_GUI;

Expand Down Expand Up @@ -426,32 +429,20 @@ gnc_ui_select_commodity_namespace_changed_cb (GtkComboBox *cbwe,
/********************************************************************
* gnc_ui_update_commodity_picker
********************************************************************/
static int
collate(gconstpointer a, gconstpointer b)
{
if (!a)
return -1;
if (!b)
return 1;
return g_utf8_collate (static_cast<const char*>(a), static_cast<const char*>(b));
}


void
gnc_ui_update_commodity_picker (GtkWidget *cbwe,
const gchar * name_space,
const gchar * init_string)
{
GList * commodities;
GList * iterator = nullptr;
GList * commodity_items = nullptr;
std::vector<std::string> commodity_items;
GtkComboBox *combo_box;
GtkEntry *entry;
GtkTreeModel *model;
GtkTreeIter iter;
gnc_commodity_table *table;
gint current = 0, match = 0;
gchar *name;

g_return_if_fail(GTK_IS_COMBO_BOX(cbwe));
g_return_if_fail(name_space);
Expand All @@ -468,29 +459,23 @@ gnc_ui_update_commodity_picker (GtkWidget *cbwe,
gtk_combo_box_set_active(combo_box, -1);

table = gnc_commodity_table_get_table (gnc_get_current_book ());
commodities = gnc_commodity_table_get_commodities(table, name_space);
for (iterator = commodities; iterator; iterator = iterator->next)
{
commodity_items =
g_list_prepend (commodity_items,
(gpointer) gnc_commodity_get_printname(GNC_COMMODITY(iterator->data)));
}
g_list_free(commodities);

commodity_items = g_list_sort(commodity_items, collate);
for (iterator = commodity_items; iterator; iterator = iterator->next)
for (auto comm : gnc_commodity_table_get_commodities(table, name_space))
commodity_items.push_back (gnc_commodity_get_printname(comm));

std::sort (commodity_items.end(), commodity_items.begin());

for (auto name : commodity_items)
{
name = (char *)iterator->data;
gtk_list_store_append(GTK_LIST_STORE(model), &iter);
gtk_list_store_set (GTK_LIST_STORE(model), &iter, 0, name, -1);
gtk_list_store_set (GTK_LIST_STORE(model), &iter, 0, name.c_str(), -1);

if (init_string && g_utf8_collate(name, init_string) == 0)
if (init_string && name == init_string)
match = current;
current++;
}

gtk_combo_box_set_active(combo_box, match);
g_list_free(commodity_items);
}


Expand Down Expand Up @@ -560,7 +545,7 @@ gnc_ui_update_namespace_picker (GtkWidget *cbwe,
GtkComboBox *combo_box;
GtkTreeModel *model;
GtkTreeIter iter, match;
GList *namespaces, *node;
std::vector<std::string> namespaces;
gboolean matched = FALSE;

g_return_if_fail(GTK_IS_COMBO_BOX (cbwe));
Expand All @@ -582,20 +567,18 @@ gnc_ui_update_namespace_picker (GtkWidget *cbwe,
case DIAG_COMM_NON_CURRENCY_SELECT:
namespaces =
gnc_commodity_table_get_namespaces (gnc_get_current_commodities());
node = g_list_find_custom (namespaces, GNC_COMMODITY_NS_CURRENCY, collate);
if (node)
{
namespaces = g_list_remove_link (namespaces, node);
g_list_free_1 (node);
}

if (auto it = std::find (namespaces.begin(), namespaces.end(), GNC_COMMODITY_NS_CURRENCY);
it != namespaces.end())
namespaces.erase (it);

if (gnc_commodity_namespace_is_iso (init_string))
init_string = nullptr;
break;

case DIAG_COMM_CURRENCY:
default:
namespaces = g_list_prepend (nullptr, (gpointer)GNC_COMMODITY_NS_CURRENCY);
namespaces = { GNC_COMMODITY_NS_CURRENCY };
break;
}

Expand Down Expand Up @@ -623,10 +606,10 @@ gnc_ui_update_namespace_picker (GtkWidget *cbwe,
}

/* add all others to the combobox */
namespaces = g_list_sort(namespaces, collate);
for (node = namespaces; node; node = node->next)
std::sort (namespaces.begin(), namespaces.end());
for (const auto& ns_str : namespaces)
{
auto ns = static_cast<const char*>(node->data);
auto ns = ns_str.c_str();
/* Skip template, legacy and currency namespaces.
The latter was added as first entry earlier */
if ((g_utf8_collate(ns, GNC_COMMODITY_NS_LEGACY) == 0) ||
Expand All @@ -650,7 +633,6 @@ gnc_ui_update_namespace_picker (GtkWidget *cbwe,

if (matched)
gtk_combo_box_set_active_iter (combo_box, &match);
g_list_free(namespaces);
}


Expand Down
11 changes: 6 additions & 5 deletions gnucash/gnome-utils/gnc-currency-edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@

#include <config.h>

#include <algorithm>

#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <string.h>
Expand All @@ -56,6 +58,7 @@

#include "gnc-currency-edit.h"
#include "gnc-commodity.h"
#include "gnc-commodity.hpp"
#include "gnc-gtk-utils.h"
#include "gnc-ui-util.h"
#include "gnc-engine.h"
Expand Down Expand Up @@ -311,12 +314,10 @@ add_item(gnc_commodity *commodity, GNCCurrencyEdit *gce)
static void
fill_currencies(GNCCurrencyEdit *gce)
{
GList *currencies;

currencies = gnc_commodity_table_get_commodities
auto currencies = gnc_commodity_table_get_commodities
(gnc_get_current_commodities (), GNC_COMMODITY_NS_CURRENCY);
g_list_foreach(currencies, (GFunc)add_item, gce);
g_list_free(currencies);
std::for_each (currencies.begin(), currencies.end(),
[gce](auto comm){ add_item (comm, gce); });
}


Expand Down
11 changes: 3 additions & 8 deletions gnucash/gnome/dialog-price-edit-db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,16 +242,13 @@ gnc_prices_dialog_load_view (GtkTreeView *view, GNCPriceDB *pdb)
auto oldest = gnc_time (nullptr);
auto model = gtk_tree_view_get_model (view);
const auto commodity_table = gnc_get_current_commodities ();
auto namespace_list = gnc_commodity_table_get_namespaces (commodity_table);

for (auto node_n = namespace_list; node_n; node_n = g_list_next (node_n))
for (const auto& tmp_namespace_str : gnc_commodity_table_get_namespaces (commodity_table))
{
auto tmp_namespace = static_cast<char*>(node_n->data);
auto tmp_namespace = tmp_namespace_str.c_str();
DEBUG("Looking at namespace %s", tmp_namespace);
auto commodity_list = gnc_commodity_table_get_commodities (commodity_table, tmp_namespace);
for (auto node_c = commodity_list; node_c; node_c = g_list_next (node_c))
for (auto tmp_commodity : gnc_commodity_table_get_commodities (commodity_table, tmp_namespace))
{
auto tmp_commodity = static_cast<gnc_commodity*>(node_c->data);
auto num = gnc_pricedb_num_prices (pdb, tmp_commodity);
DEBUG("Looking at commodity %s, Number of prices %d", gnc_commodity_get_fullname (tmp_commodity), num);

Expand Down Expand Up @@ -279,9 +276,7 @@ gnc_prices_dialog_load_view (GtkTreeView *view, GNCPriceDB *pdb)
g_list_free_full (list, (GDestroyNotify)gnc_price_unref);
}
}
g_list_free (commodity_list);
}
g_list_free (namespace_list);

return oldest;
}
Expand Down
15 changes: 4 additions & 11 deletions gnucash/import-export/csv-imp/assistant-csv-price-import.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "gnc-uri-utils.h"
#include "gnc-ui-util.h"
#include "dialog-utils.h"
#include "gnc-commodity.hpp"

#include "gnc-component-manager.h"

Expand Down Expand Up @@ -445,9 +446,6 @@ GtkTreeModel *get_model (bool all_commodity)
{
GtkTreeModel *store, *model;
const gnc_commodity_table *commodity_table = gnc_get_current_commodities ();
gnc_commodity *tmp_commodity = nullptr;
char *tmp_namespace = nullptr;
GList *namespace_list = gnc_commodity_table_get_namespaces (commodity_table);
GtkTreeIter iter;

store = GTK_TREE_MODEL(gtk_list_store_new (4, G_TYPE_STRING, G_TYPE_STRING,
Expand All @@ -460,18 +458,16 @@ GtkTreeModel *get_model (bool all_commodity)
gtk_list_store_set (GTK_LIST_STORE(store), &iter,
DISPLAYED_COMM, " ", SORT_COMM, " ", COMM_PTR, nullptr, SEP, false, -1);

for (auto node = namespace_list; node; node = g_list_next (node))
for (const auto& tmp_namespace_str : gnc_commodity_table_get_namespaces (commodity_table))
{
tmp_namespace = (char*)node->data;
auto tmp_namespace = tmp_namespace_str.c_str();
DEBUG("Looking at namespace %s", tmp_namespace);

/* Hide the template entry */
if (g_utf8_collate (tmp_namespace, "template" ) != 0)
{
if ((g_utf8_collate (tmp_namespace, GNC_COMMODITY_NS_CURRENCY ) == 0) || (all_commodity == true))
{
auto comm_list = gnc_commodity_table_get_commodities (commodity_table, tmp_namespace);

// if this is the CURRENCY, add a row to be identified as a separator row
if ((g_utf8_collate (tmp_namespace, GNC_COMMODITY_NS_CURRENCY) == 0) && (all_commodity == true))
{
Expand All @@ -480,11 +476,10 @@ GtkTreeModel *get_model (bool all_commodity)
SORT_COMM, "CURRENCY-", COMM_PTR, nullptr, SEP, true, -1);
}

for (auto node = comm_list; node; node = g_list_next (node))
for (auto tmp_commodity : gnc_commodity_table_get_commodities (commodity_table, tmp_namespace))
{
const gchar *name_str;
gchar *sort_str;
tmp_commodity = (gnc_commodity*)node->data;
DEBUG("Looking at commodity %s", gnc_commodity_get_fullname (tmp_commodity));

name_str = gnc_commodity_get_printname (tmp_commodity);
Expand All @@ -502,11 +497,9 @@ GtkTreeModel *get_model (bool all_commodity)

g_free (sort_str);
}
g_list_free (comm_list);
}
}
}
g_list_free (namespace_list);
g_object_unref (store);

return model;
Expand Down
Loading