From 9713683ee35097d78ff670bedd24b51a8278b779 Mon Sep 17 00:00:00 2001 From: Richard Cohen Date: Fri, 23 Jun 2023 16:44:41 +0100 Subject: [PATCH 1/3] Refactor: define and use api for GncSxInstanceModel --- .../gnc-sx-instance-dense-cal-adapter.c | 10 +++++----- .../gnome-utils/gnc-sx-list-tree-model-adapter.c | 4 ++-- gnucash/gnome/dialog-sx-since-last-run.c | 10 +++++----- libgnucash/app-utils/gnc-sx-instance-model.c | 5 +++++ libgnucash/app-utils/gnc-sx-instance-model.h | 8 +++++++- libgnucash/app-utils/test/test-sx.cpp | 14 +++++++------- 6 files changed, 31 insertions(+), 20 deletions(-) diff --git a/gnucash/gnome-utils/gnc-sx-instance-dense-cal-adapter.c b/gnucash/gnome-utils/gnc-sx-instance-dense-cal-adapter.c index a0ee9b5afc..dc7c9da416 100644 --- a/gnucash/gnome-utils/gnc-sx-instance-dense-cal-adapter.c +++ b/gnucash/gnome-utils/gnc-sx-instance-dense-cal-adapter.c @@ -154,7 +154,7 @@ gsidca_get_contained(GncDenseCalModel *model) GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(model); //"removing return gnc_g_list_map(instances->sxes, sx_to_tag, null); GList *list = NULL, *sxes; - for (sxes = adapter->instances->sx_instance_list; sxes != NULL; sxes = sxes->next) + for (sxes = gnc_sx_instance_model_get_sx_instances_list (adapter->instances); sxes != NULL; sxes = sxes->next) { GncSxInstances *sx_instances = (GncSxInstances*)sxes->data; if (xaccSchedXactionGetEnabled(sx_instances->sx)) @@ -169,7 +169,7 @@ gsidca_get_name(GncDenseCalModel *model, guint tag) { GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(model); GncSxInstances *insts - = (GncSxInstances*)g_list_find_custom(adapter->instances->sx_instance_list, GUINT_TO_POINTER(tag), gsidca_find_sx_with_tag)->data; + = (GncSxInstances*)g_list_find_custom(gnc_sx_instance_model_get_sx_instances_list (adapter->instances), GUINT_TO_POINTER(tag), gsidca_find_sx_with_tag)->data; if (insts == NULL) return NULL; return xaccSchedXactionGetName(insts->sx); @@ -183,7 +183,7 @@ gsidca_get_info(GncDenseCalModel *model, guint tag) GList *schedule; gchar *schedule_str; GncSxInstances *insts - = (GncSxInstances*)g_list_find_custom(adapter->instances->sx_instance_list, GUINT_TO_POINTER(tag), gsidca_find_sx_with_tag)->data; + = (GncSxInstances*)g_list_find_custom(gnc_sx_instance_model_get_sx_instances_list(adapter->instances), GUINT_TO_POINTER(tag), gsidca_find_sx_with_tag)->data; if (insts == NULL) return NULL; schedule = gnc_sx_get_schedule(insts->sx); @@ -197,7 +197,7 @@ gsidca_get_instance_count(GncDenseCalModel *model, guint tag) GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(model); // g_list_find(instances->sxes, {sx_to_tag, tag}).length(); GncSxInstances *insts - = (GncSxInstances*)g_list_find_custom(adapter->instances->sx_instance_list, GUINT_TO_POINTER(tag), gsidca_find_sx_with_tag)->data; + = (GncSxInstances*)g_list_find_custom(gnc_sx_instance_model_get_sx_instances_list(adapter->instances), GUINT_TO_POINTER(tag), gsidca_find_sx_with_tag)->data; if (insts == NULL) return 0; return g_list_length(insts->instance_list); @@ -209,7 +209,7 @@ gsidca_get_instance(GncDenseCalModel *model, guint tag, gint instance_index, GDa GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(model); GncSxInstance *inst; GncSxInstances *insts - = (GncSxInstances*)g_list_find_custom(adapter->instances->sx_instance_list, GUINT_TO_POINTER(tag), gsidca_find_sx_with_tag)->data; + = (GncSxInstances*)g_list_find_custom(gnc_sx_instance_model_get_sx_instances_list(adapter->instances), GUINT_TO_POINTER(tag), gsidca_find_sx_with_tag)->data; if (insts == NULL) return; inst = (GncSxInstance*)g_list_nth_data(insts->instance_list, instance_index); diff --git a/gnucash/gnome-utils/gnc-sx-list-tree-model-adapter.c b/gnucash/gnome-utils/gnc-sx-list-tree-model-adapter.c index 14c689d45c..71541c2544 100644 --- a/gnucash/gnome-utils/gnc-sx-list-tree-model-adapter.c +++ b/gnucash/gnome-utils/gnc-sx-list-tree-model-adapter.c @@ -475,7 +475,7 @@ gsltma_populate_tree_store (GncSxListTreeModelAdapter *model) GtkTreeIter iter; GList *list; - for (list = model->instances->sx_instance_list; list != NULL; list = list->next) + for (list = gnc_sx_instance_model_get_sx_instances_list (model->instances); list != NULL; list = list->next) { GncSxInstances *instances = (GncSxInstances*)list->data; gchar *frequency_str; @@ -563,7 +563,7 @@ gsltma_get_sx_instances_from_orig_iter (GncSxListTreeModelAdapter *model, GtkTre index = indices[0]; gtk_tree_path_free (path); - return (GncSxInstances*)g_list_nth_data (model->instances->sx_instance_list, index); + return (GncSxInstances*)g_list_nth_data (gnc_sx_instance_model_get_sx_instances_list (model->instances), index); } GncSxInstances* diff --git a/gnucash/gnome/dialog-sx-since-last-run.c b/gnucash/gnome/dialog-sx-since-last-run.c index be82415924..676aa9685d 100644 --- a/gnucash/gnome/dialog-sx-since-last-run.c +++ b/gnucash/gnome/dialog-sx-since-last-run.c @@ -411,7 +411,7 @@ gsslrtma_populate_tree_store (GncSxSlrTreeModelAdapter *model) GList *sx_iter; int instances_index = -1; - for (sx_iter = model->instances->sx_instance_list; sx_iter != NULL; sx_iter = sx_iter->next) + for (sx_iter = gnc_sx_instance_model_get_sx_instances_list (model->instances); sx_iter != NULL; sx_iter = sx_iter->next) { GncSxInstances *instances = (GncSxInstances*)sx_iter->data; char last_occur_date_buf[MAX_DATE_LENGTH+1]; @@ -549,7 +549,7 @@ _gnc_sx_slr_tree_model_adapter_get_sx_instances (GncSxSlrTreeModelAdapter *model index = indices[0]; gtk_tree_path_free (path); - return (GncSxInstances*)g_list_nth_data (model->instances->sx_instance_list, index); + return (GncSxInstances*)g_list_nth_data (gnc_sx_instance_model_get_sx_instances_list (model->instances), index); } GncSxInstance* @@ -575,7 +575,7 @@ _gnc_sx_slr_model_get_instance (GncSxSlrTreeModelAdapter *model, GtkTreeIter *it instance_index = indices[1]; gtk_tree_path_free (path); - instances = (GncSxInstances*)g_list_nth_data (model->instances->sx_instance_list, instances_index); + instances = (GncSxInstances*)g_list_nth_data (gnc_sx_instance_model_get_sx_instances_list (model->instances), instances_index); if (instance_index < 0 || instance_index >= g_list_length (instances->instance_list)) { return NULL; @@ -667,7 +667,7 @@ _get_path_for_variable (GncSxSlrTreeModelAdapter *model, GncSxInstance *instance int indices[3]; GtkTreePath *path; - indices[0] = g_list_index (model->instances->sx_instance_list, instance->parent); + indices[0] = g_list_index (gnc_sx_instance_model_get_sx_instances_list (model->instances), instance->parent); if (indices[0] == -1) return NULL; indices[1] = g_list_index (instance->parent->instance_list, instance); @@ -706,7 +706,7 @@ gsslrtma_removing_cb (GncSxInstanceModel *instances, SchedXaction *to_remove_sx, GList *iter; int index = 0; // get index, create path, remove - for (iter = instances->sx_instance_list; iter != NULL; iter = iter->next, index++) + for (iter = gnc_sx_instance_model_get_sx_instances_list (instances); iter != NULL; iter = iter->next, index++) { GncSxInstances *instances = (GncSxInstances*)iter->data; if (instances->sx == to_remove_sx) diff --git a/libgnucash/app-utils/gnc-sx-instance-model.c b/libgnucash/app-utils/gnc-sx-instance-model.c index d23659c538..5628c8a9be 100644 --- a/libgnucash/app-utils/gnc-sx-instance-model.c +++ b/libgnucash/app-utils/gnc-sx-instance-model.c @@ -1898,3 +1898,8 @@ GHashTable* gnc_sx_all_instantiate_cashflow_all(GDate range_start, GDate range_e result_map, NULL); return result_map; } + +GList *gnc_sx_instance_model_get_sx_instances_list (GncSxInstanceModel *model) +{ + return model->sx_instance_list; +} diff --git a/libgnucash/app-utils/gnc-sx-instance-model.h b/libgnucash/app-utils/gnc-sx-instance-model.h index e3de13afe3..c51739cc8b 100644 --- a/libgnucash/app-utils/gnc-sx-instance-model.h +++ b/libgnucash/app-utils/gnc-sx-instance-model.h @@ -54,7 +54,6 @@ typedef struct _GncSxInstanceModel /* void (*updated)(SchedXaction *sx); // gpointer user_data */ /* void (*removing)(SchedXaction *sx); // gpointer user_data */ - /* public */ GDate range_end; gboolean include_disabled; GList *sx_instance_list; /* */ @@ -256,6 +255,13 @@ void gnc_sx_all_instantiate_cashflow(GList *all_sxes, * g_hash_table_destroy. */ GHashTable* gnc_sx_all_instantiate_cashflow_all(GDate range_start, GDate range_end); +/** Returns the list of GncSxInstances in the model + * (Each element in the list has type GncSxInstances) + * + * The returned list is owned by the model + */ +GList *gnc_sx_instance_model_get_sx_instances_list (GncSxInstanceModel *model); + G_END_DECLS diff --git a/libgnucash/app-utils/test/test-sx.cpp b/libgnucash/app-utils/test/test-sx.cpp index b38fc22e87..04b7468ed0 100644 --- a/libgnucash/app-utils/test/test-sx.cpp +++ b/libgnucash/app-utils/test/test-sx.cpp @@ -60,8 +60,8 @@ test_basic() GncSxInstances *insts; GList *iter; - do_test(g_list_length(model->sx_instance_list) == 1, "1 GncSxInstances"); - insts = (GncSxInstances*)model->sx_instance_list->data; + do_test(g_list_length(gnc_sx_instance_model_get_sx_instances_list(model)) == 1, "1 GncSxInstances"); + insts = (GncSxInstances*)gnc_sx_instance_model_get_sx_instances_list(model)->data; do_test(g_list_length(insts->instance_list) == 3, "yesterday, today and tomorrow"); for (iter = insts->instance_list; iter != NULL; iter = iter->next) { @@ -93,7 +93,7 @@ test_empty() end = g_date_new_dmy(31, (GDateMonth)12, way_in_the_future_year); model = gnc_sx_get_instances(end, TRUE); - do_test(g_list_length(model->sx_instance_list) == 0, "no instances"); + do_test(g_list_length(gnc_sx_instance_model_get_sx_instances_list(model)) == 0, "no instances"); g_object_unref(G_OBJECT(model)); g_date_free(end); success("empty"); @@ -128,8 +128,8 @@ test_once() model = gnc_sx_get_instances(end, TRUE); - do_test(g_list_length(model->sx_instance_list) == 1, "1 instances"); - instances = (GncSxInstances*)model->sx_instance_list->data; + do_test(g_list_length(gnc_sx_instance_model_get_sx_instances_list(model)) == 1, "1 instances"); + instances = (GncSxInstances*)gnc_sx_instance_model_get_sx_instances_list(model)->data; do_test(g_list_length(instances->instance_list) == 1, "1 instance"); instance = (GncSxInstance*)instances->instance_list->data; do_test(g_date_compare(when, &instances->next_instance_date) == 0, "next instance is expected"); @@ -165,8 +165,8 @@ test_state_changes() foo = add_daily_sx("foo", start, NULL, NULL); model = gnc_sx_get_instances(end, TRUE); - do_test(g_list_length(model->sx_instance_list) == 1, "one sx"); - insts = (GncSxInstances*)g_list_nth_data(model->sx_instance_list, 0); + do_test(g_list_length(gnc_sx_instance_model_get_sx_instances_list(model)) == 1, "one sx"); + insts = (GncSxInstances*)g_list_nth_data(gnc_sx_instance_model_get_sx_instances_list(model), 0); do_test(g_list_length(insts->instance_list) == 4, "4 instances"); inst = _nth_instance(insts, 2); From d30fdd8071390cb22ec5fe655760a5dbc2582f07 Mon Sep 17 00:00:00 2001 From: Richard Cohen Date: Wed, 28 Jun 2023 13:26:09 +0100 Subject: [PATCH 2/3] Refactor: DECLARE_FINAL GncSxInstanceModel < GObject --- libgnucash/app-utils/gnc-sx-instance-model.c | 14 +++++++++++--- libgnucash/app-utils/gnc-sx-instance-model.h | 17 +---------------- 2 files changed, 12 insertions(+), 19 deletions(-) diff --git a/libgnucash/app-utils/gnc-sx-instance-model.c b/libgnucash/app-utils/gnc-sx-instance-model.c index 5628c8a9be..c0ba5c90ac 100644 --- a/libgnucash/app-utils/gnc-sx-instance-model.c +++ b/libgnucash/app-utils/gnc-sx-instance-model.c @@ -90,6 +90,14 @@ typedef struct gnc_numeric amount; } ScrubItem; +enum +{ + REMOVING, UPDATED, ADDED, + LAST_SIGNAL +}; + +static guint signals[LAST_SIGNAL] = { 0 }; + static void scrub_sx_split_numeric (Split* split, gboolean is_credit, GList **changes) { @@ -670,7 +678,7 @@ gnc_sx_instance_model_class_init (GncSxInstanceModelClass *klass) object_class->dispose = gnc_sx_instance_model_dispose; object_class->finalize = gnc_sx_instance_model_finalize; - klass->removing_signal_id = + signals[REMOVING] = g_signal_new("removing", GNC_TYPE_SX_INSTANCE_MODEL, G_SIGNAL_RUN_FIRST, @@ -682,7 +690,7 @@ gnc_sx_instance_model_class_init (GncSxInstanceModelClass *klass) 1, G_TYPE_POINTER); - klass->updated_signal_id = + signals[UPDATED] = g_signal_new("updated", GNC_TYPE_SX_INSTANCE_MODEL, G_SIGNAL_RUN_FIRST, @@ -694,7 +702,7 @@ gnc_sx_instance_model_class_init (GncSxInstanceModelClass *klass) 1, G_TYPE_POINTER); - klass->added_signal_id = + signals[ADDED] = g_signal_new("added", GNC_TYPE_SX_INSTANCE_MODEL, G_SIGNAL_RUN_FIRST, diff --git a/libgnucash/app-utils/gnc-sx-instance-model.h b/libgnucash/app-utils/gnc-sx-instance-model.h index c51739cc8b..fc1b5fca3f 100644 --- a/libgnucash/app-utils/gnc-sx-instance-model.h +++ b/libgnucash/app-utils/gnc-sx-instance-model.h @@ -35,11 +35,7 @@ G_BEGIN_DECLS #define GNC_TYPE_SX_INSTANCE_MODEL (gnc_sx_instance_model_get_type ()) -#define GNC_SX_INSTANCE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GNC_TYPE_SX_INSTANCE_MODEL, GncSxInstanceModel)) -#define GNC_SX_INSTANCE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GNC_TYPE_SX_INSTANCE_MODEL, GncSxInstanceModelClass)) -#define GNC_IS_SX_INSTANCE_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GNC_TYPE_SX_INSTANCE_MODEL)) -#define GNC_IS_SX_INSTANCE_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GNC_TYPE_SX_INSTANCE_MODEL)) -#define GNC_SX_INSTANCE_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GNC_TYPE_SX_INSTANCE_MODEL, GncSxInstanceModelClass)) +G_DECLARE_FINAL_TYPE (GncSxInstanceModel, gnc_sx_instance_model, GNC, SX_INSTANCE_MODEL, GObject) typedef struct _GncSxInstanceModel { @@ -59,15 +55,6 @@ typedef struct _GncSxInstanceModel GList *sx_instance_list; /* */ } GncSxInstanceModel; -typedef struct _GncSxInstanceModelClass -{ - GObjectClass parent; - - guint removing_signal_id; - guint updated_signal_id; - guint added_signal_id; -} GncSxInstanceModelClass; - typedef struct _GncSxInstances { SchedXaction *sx; @@ -113,8 +100,6 @@ typedef struct _GncSxVariableNeeded GncSxVariable *variable; } GncSxVariableNeeded; -GType gnc_sx_instance_model_get_type(void); - /** Shorthand for get_instances(now, FALSE); */ GncSxInstanceModel* gnc_sx_get_current_instances(void); From 52e8524ec55374e71c5fda3cfc6431fd00cc480a Mon Sep 17 00:00:00 2001 From: Richard Cohen Date: Wed, 28 Jun 2023 13:43:27 +0100 Subject: [PATCH 3/3] Refactor: hide implementation of GncSxInstanceModel (move from .h -> .c) --- libgnucash/app-utils/gnc-sx-instance-model.c | 12 ++++++++++++ libgnucash/app-utils/gnc-sx-instance-model.h | 18 ------------------ 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/libgnucash/app-utils/gnc-sx-instance-model.c b/libgnucash/app-utils/gnc-sx-instance-model.c index c0ba5c90ac..27e19ae582 100644 --- a/libgnucash/app-utils/gnc-sx-instance-model.c +++ b/libgnucash/app-utils/gnc-sx-instance-model.c @@ -72,6 +72,18 @@ typedef struct _SxTxnCreationData GList **creation_errors; } SxTxnCreationData; +struct _GncSxInstanceModel +{ + GObject parent; + gboolean disposed; + + gint qof_event_handler_id; + + GDate range_end; + gboolean include_disabled; + GList *sx_instance_list; /* */ +}; + static GncSxInstanceModel* gnc_sx_instance_model_new(void); static GncSxInstance* gnc_sx_instance_new(GncSxInstances *parent, GncSxInstanceState state, GDate *date, void *temporal_state, gint sequence_num); diff --git a/libgnucash/app-utils/gnc-sx-instance-model.h b/libgnucash/app-utils/gnc-sx-instance-model.h index fc1b5fca3f..6d2c6da3c4 100644 --- a/libgnucash/app-utils/gnc-sx-instance-model.h +++ b/libgnucash/app-utils/gnc-sx-instance-model.h @@ -37,24 +37,6 @@ G_BEGIN_DECLS #define GNC_TYPE_SX_INSTANCE_MODEL (gnc_sx_instance_model_get_type ()) G_DECLARE_FINAL_TYPE (GncSxInstanceModel, gnc_sx_instance_model, GNC, SX_INSTANCE_MODEL, GObject) -typedef struct _GncSxInstanceModel -{ - GObject parent; - gboolean disposed; - - /* private */ - gint qof_event_handler_id; - - /* signals */ - /* void (*added)(SchedXaction *sx); // gpointer user_data */ - /* void (*updated)(SchedXaction *sx); // gpointer user_data */ - /* void (*removing)(SchedXaction *sx); // gpointer user_data */ - - GDate range_end; - gboolean include_disabled; - GList *sx_instance_list; /* */ -} GncSxInstanceModel; - typedef struct _GncSxInstances { SchedXaction *sx;