GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-sx-instance-dense-cal-adapter.c
1/*
2 * gnc-sx-instance-dense-cal-adapter.c
3 *
4 * Copyright (C) 2006 Josh Sled <jsled@asynchronous.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of version 2 and/or version 3 of the GNU General Public
8 * License as published by the Free Software Foundation.
9 *
10 * As a special exception, permission is granted to link the binary module
11 * resultant from this code with the OpenSSL project's "OpenSSL" library (or
12 * modified versions of it that use the same license as the "OpenSSL"
13 * library), and distribute the linked executable. You must obey the GNU
14 * General Public License in all respects for all of the code used other than
15 * "OpenSSL". If you modify this file, you may extend this exception to your
16 * version of the file, but you are not obligated to do so. If you do not
17 * wish to do so, delete this exception statement from your version of this
18 * file.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, contact:
27 *
28 * Free Software Foundation Voice: +1-617-542-5942
29 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
30 * Boston, MA 02110-1301, USA gnu@gnu.org
31 */
32
33#include <config.h>
34#include <glib.h>
35#include "gnc-sx-instance-dense-cal-adapter.h"
36#include "gnc-dense-cal.h"
37#include <qoflog.h>
38
39#undef G_LOG_DOMAIN
40#define G_LOG_DOMAIN "gnc.gui.sx.adapter.sx-dense-cal"
41static const QofLogModule log_module = G_LOG_DOMAIN;
42
43static void gnc_sx_instance_dense_cal_adapter_interface_init(GncDenseCalModelInterface *iface);
44static void gnc_sx_instance_dense_cal_adapter_dispose(GObject *obj);
45static void gnc_sx_instance_dense_cal_adapter_finalize(GObject *obj);
46
47static GList* gsidca_get_contained(GncDenseCalModel *model);
48static gchar* gsidca_get_name(GncDenseCalModel *model, guint tag);
49static gchar* gsidca_get_info(GncDenseCalModel *model, guint tag);
50static gint gsidca_get_instance_count(GncDenseCalModel *model, guint tag);
51static void gsidca_get_instance(GncDenseCalModel *model, guint tag, gint instance_index, GDate *date);
52
54{
55 GObject parent;
56 gboolean disposed;
57 GncSxInstanceModel *instances;
58};
59
60G_DEFINE_TYPE_WITH_CODE (GncSxInstanceDenseCalAdapter, gnc_sx_instance_dense_cal_adapter, G_TYPE_OBJECT,
61 G_IMPLEMENT_INTERFACE (GNC_TYPE_DENSE_CAL_MODEL, gnc_sx_instance_dense_cal_adapter_interface_init))
62
63static void
64gnc_sx_instance_dense_cal_adapter_class_init(GncSxInstanceDenseCalAdapterClass *klass)
65{
66 GObjectClass *obj_class = G_OBJECT_CLASS(klass);
67
68 obj_class->dispose = gnc_sx_instance_dense_cal_adapter_dispose;
69 obj_class->finalize = gnc_sx_instance_dense_cal_adapter_finalize;
70}
71
72static void
73gnc_sx_instance_dense_cal_adapter_init(GncSxInstanceDenseCalAdapter *instance)
74{
75 /*GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(instance);*/
76 ; /* nop */
77}
78
79static void
80gnc_sx_instance_dense_cal_adapter_interface_init(GncDenseCalModelInterface *iface)
81{
82 iface->get_contained = gsidca_get_contained;
83 iface->get_name = gsidca_get_name;
84 iface->get_info = gsidca_get_info;
85 iface->get_instance_count = gsidca_get_instance_count;
86 iface->get_instance = gsidca_get_instance;
87}
88
89static void
90gsidca_instances_added_cb(GncSxInstanceModel *model, SchedXaction *sx_added, gpointer user_data)
91{
92 GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(user_data);
93 DEBUG("instance added\n");
94 if (xaccSchedXactionGetEnabled(sx_added))
95 {
96 g_signal_emit_by_name(adapter, "added", GPOINTER_TO_UINT(sx_added));
97 }
98}
99
100static void
101gsidca_instances_updated_cb(GncSxInstanceModel *model, SchedXaction *sx_updated, gpointer user_data)
102{
103 GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(user_data);
104 gnc_sx_instance_model_update_sx_instances(model, sx_updated);
105 DEBUG("instances updated\n");
106 if (xaccSchedXactionGetEnabled(sx_updated))
107 {
108 g_signal_emit_by_name(adapter, "update", GPOINTER_TO_UINT((gpointer)sx_updated));
109 }
110 else
111 {
112 g_signal_emit_by_name(adapter, "removing", GPOINTER_TO_UINT((gpointer)sx_updated));
113 }
114}
115
116static void
117gsidca_instances_removing_cb(GncSxInstanceModel *model, SchedXaction *sx_to_be_removed, gpointer user_data)
118{
119 GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(user_data);
120 DEBUG("removing instance...\n");
121 g_signal_emit_by_name(adapter, "removing", GPOINTER_TO_UINT(sx_to_be_removed));
122 gnc_sx_instance_model_remove_sx_instances(model, sx_to_be_removed);
123}
124
125GncSxInstanceDenseCalAdapter*
126gnc_sx_instance_dense_cal_adapter_new(GncSxInstanceModel *instances)
127{
128 GncSxInstanceDenseCalAdapter *adapter = g_object_new(GNC_TYPE_SX_INSTANCE_DENSE_CAL_ADAPTER, NULL);
129 adapter->instances = instances;
130 g_object_ref(G_OBJECT(adapter->instances));
131
132 g_signal_connect(instances, "added", (GCallback)gsidca_instances_added_cb, adapter);
133 g_signal_connect(instances, "updated", (GCallback)gsidca_instances_updated_cb, adapter);
134 g_signal_connect(instances, "removing", (GCallback)gsidca_instances_removing_cb, adapter);
135 return adapter;
136}
137
138static gint
139gsidca_find_sx_with_tag(gconstpointer list_data,
140 gconstpointer find_data)
141{
142 GncSxInstances *sx_instances = (GncSxInstances*)list_data;
143 return (GUINT_TO_POINTER(GPOINTER_TO_UINT(sx_instances->sx)) == find_data ? 0 : 1);
144}
145
146static GList*
147gsidca_get_contained(GncDenseCalModel *model)
148{
149 GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(model);
150 //"removing return gnc_g_list_map(instances->sxes, sx_to_tag, null);
151 GList *list = NULL, *sxes;
152 for (sxes = gnc_sx_instance_model_get_sx_instances_list (adapter->instances); sxes != NULL; sxes = sxes->next)
153 {
154 GncSxInstances *sx_instances = (GncSxInstances*)sxes->data;
155 if (xaccSchedXactionGetEnabled(sx_instances->sx))
156 list = g_list_prepend (list, GUINT_TO_POINTER
157 (GPOINTER_TO_UINT (sx_instances->sx)));
158 }
159 return g_list_reverse (list);
160}
161
162static gchar*
163gsidca_get_name(GncDenseCalModel *model, guint tag)
164{
165 GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(model);
166 GncSxInstances *insts
167 = (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;
168 if (insts == NULL)
169 return NULL;
170 return xaccSchedXactionGetName(insts->sx);
171}
172
173static gchar*
174gsidca_get_info(GncDenseCalModel *model, guint tag)
175{
176 GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(model);
177 // g_list_find(instances->sxes, {sx_to_tag, tag}).get_freq_spec().get_freq_str();
178 GList *schedule;
179 gchar *schedule_str;
180 GncSxInstances *insts
181 = (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;
182 if (insts == NULL)
183 return NULL;
184 schedule = gnc_sx_get_schedule(insts->sx);
185 schedule_str = recurrenceListToCompactString(schedule);
186 return schedule_str;
187}
188
189static gint
190gsidca_get_instance_count(GncDenseCalModel *model, guint tag)
191{
192 GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(model);
193 // g_list_find(instances->sxes, {sx_to_tag, tag}).length();
194 GncSxInstances *insts
195 = (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;
196 if (insts == NULL)
197 return 0;
198 return g_list_length(insts->instance_list);
199}
200
201static void
202gsidca_get_instance(GncDenseCalModel *model, guint tag, gint instance_index, GDate *date)
203{
204 GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(model);
205 GncSxInstance *inst;
206 GncSxInstances *insts
207 = (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;
208 if (insts == NULL)
209 return;
210 inst = (GncSxInstance*)g_list_nth_data(insts->instance_list, instance_index);
211 g_date_valid(&inst->date);
212 *date = inst->date;
213 g_date_valid(date);
214}
215
216static void
217gnc_sx_instance_dense_cal_adapter_dispose(GObject *obj)
218{
219 GncSxInstanceDenseCalAdapter *adapter;
220 g_return_if_fail(obj != NULL);
221 adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(obj);
222 // g_return_if_fail(!adapter->disposed);
223 if (adapter->disposed) return;
224 adapter->disposed = TRUE;
225
226 g_object_unref(G_OBJECT(adapter->instances));
227 adapter->instances = NULL;
228
229 G_OBJECT_CLASS(gnc_sx_instance_dense_cal_adapter_parent_class)->dispose(obj);
230}
231
232static void gnc_sx_instance_dense_cal_adapter_finalize(GObject *obj)
233{
234 g_return_if_fail(obj != NULL);
235 // nop
236 G_OBJECT_CLASS(gnc_sx_instance_dense_cal_adapter_parent_class)->finalize(obj);
237}
#define DEBUG(format, args...)
Print a debugging message.
Definition qoflog.h:264
GList * gnc_sx_get_schedule(const SchedXaction *sx)
GDate date
the instance date.
GList * instance_list
GList<GncSxInstance*>