GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-sx-instance-dense-cal-adapter.c
1 /*
2  * gnc-sx-instance-dense-cal-adapter.c
3  *
4  * Copyright (C) 2006 Josh Sled <[email protected]>
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 [email protected]
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 
38 #undef G_LOG_DOMAIN
39 #define G_LOG_DOMAIN "gnc.gui.sx.adapter.sx-dense-cal"
40 
41 static void gnc_sx_instance_dense_cal_adapter_dispose(GObject *obj);
42 static void gnc_sx_instance_dense_cal_adapter_finalize(GObject *obj);
43 
44 static GList* gsidca_get_contained(GncDenseCalModel *model);
45 static gchar* gsidca_get_name(GncDenseCalModel *model, guint tag);
46 static gchar* gsidca_get_info(GncDenseCalModel *model, guint tag);
47 static gint gsidca_get_instance_count(GncDenseCalModel *model, guint tag);
48 static void gsidca_get_instance(GncDenseCalModel *model, guint tag, gint instance_index, GDate *date);
49 
50 static GObjectClass *parent_class = NULL;
51 
53 {
54  GObjectClass parent;
55 };
56 
58 {
59  GObject parent;
60  gboolean disposed;
61  GncSxInstanceModel *instances;
62 };
63 
64 static void
65 gnc_sx_instance_dense_cal_adapter_class_init(GncSxInstanceDenseCalAdapterClass *klass)
66 {
67  GObjectClass *obj_class = G_OBJECT_CLASS(klass);
68 
69  obj_class->dispose = gnc_sx_instance_dense_cal_adapter_dispose;
70  obj_class->finalize = gnc_sx_instance_dense_cal_adapter_finalize;
71 
72  parent_class = g_type_class_peek_parent(klass);
73 }
74 
75 static void
76 gnc_sx_instance_dense_cal_adapter_init(GTypeInstance *instance, gpointer klass)
77 {
78  /*GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(instance);*/
79  ; /* nop */
80 }
81 
82 static void
83 gnc_sx_instance_dense_cal_adapter_interface_init(gpointer g_iface, gpointer iface_data)
84 {
86  iface->get_contained = gsidca_get_contained;
87  iface->get_name = gsidca_get_name;
88  iface->get_info = gsidca_get_info;
89  iface->get_instance_count = gsidca_get_instance_count;
90  iface->get_instance = gsidca_get_instance;
91 }
92 
93 static void
94 gsidca_instances_added_cb(GncSxInstanceModel *model, SchedXaction *sx_added, gpointer user_data)
95 {
96  GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(user_data);
97  g_debug("instance added\n");
98  if (xaccSchedXactionGetEnabled(sx_added))
99  {
100  g_signal_emit_by_name(adapter, "added", GPOINTER_TO_UINT(sx_added));
101  }
102 }
103 
104 static void
105 gsidca_instances_updated_cb(GncSxInstanceModel *model, SchedXaction *sx_updated, gpointer user_data)
106 {
107  GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(user_data);
109  g_debug("instances updated\n");
110  if (xaccSchedXactionGetEnabled(sx_updated))
111  {
112  g_signal_emit_by_name(adapter, "update", GPOINTER_TO_UINT((gpointer)sx_updated));
113  }
114  else
115  {
116  g_signal_emit_by_name(adapter, "removing", GPOINTER_TO_UINT((gpointer)sx_updated));
117  }
118 }
119 
120 static void
121 gsidca_instances_removing_cb(GncSxInstanceModel *model, SchedXaction *sx_to_be_removed, gpointer user_data)
122 {
123  GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(user_data);
124  g_debug("removing instance...\n");
125  g_signal_emit_by_name(adapter, "removing", GPOINTER_TO_UINT(sx_to_be_removed));
126  gnc_sx_instance_model_remove_sx_instances(model, sx_to_be_removed);
127 }
128 
130 gnc_sx_instance_dense_cal_adapter_new(GncSxInstanceModel *instances)
131 {
132  GncSxInstanceDenseCalAdapter *adapter = g_object_new(GNC_TYPE_SX_INSTANCE_DENSE_CAL_ADAPTER, NULL);
133  adapter->instances = instances;
134  g_object_ref(G_OBJECT(adapter->instances));
135 
136  g_signal_connect(instances, "added", (GCallback)gsidca_instances_added_cb, adapter);
137  g_signal_connect(instances, "updated", (GCallback)gsidca_instances_updated_cb, adapter);
138  g_signal_connect(instances, "removing", (GCallback)gsidca_instances_removing_cb, adapter);
139  return adapter;
140 }
141 
142 GType
143 gnc_sx_instance_dense_cal_adapter_get_type(void)
144 {
145  static GType type = 0;
146  if (type == 0)
147  {
148  static const GTypeInfo info =
149  {
151  NULL, /* base init */
152  NULL, /* base finalize */
153  (GClassInitFunc)gnc_sx_instance_dense_cal_adapter_class_init,
154  NULL, /* class finalize */
155  NULL, /* class data */
157  0, /* n_preallocs */
158  (GInstanceInitFunc)gnc_sx_instance_dense_cal_adapter_init
159  };
160  static const GInterfaceInfo iDenseCalModelInfo =
161  {
162  (GInterfaceInitFunc)gnc_sx_instance_dense_cal_adapter_interface_init,
163  NULL, /* interface finalize */
164  NULL, /* interface data */
165  };
166 
167  type = g_type_register_static (G_TYPE_OBJECT,
168  "GncSxInstanceDenseCalAdapterType",
169  &info, 0);
170  g_type_add_interface_static(type,
171  GNC_TYPE_DENSE_CAL_MODEL,
172  &iDenseCalModelInfo);
173  }
174  return type;
175 }
176 
177 static gint
178 gsidca_find_sx_with_tag(gconstpointer list_data,
179  gconstpointer find_data)
180 {
181  GncSxInstances *sx_instances = (GncSxInstances*)list_data;
182  return (GUINT_TO_POINTER(GPOINTER_TO_UINT(sx_instances->sx)) == find_data ? 0 : 1);
183 }
184 
185 static GList*
186 gsidca_get_contained(GncDenseCalModel *model)
187 {
188  GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(model);
189  //"removing return gnc_g_list_map(instances->sxes, sx_to_tag, null);
190  GList *list = NULL, *sxes;
191  for (sxes = adapter->instances->sx_instance_list; sxes != NULL; sxes = sxes->next)
192  {
193  GncSxInstances *sx_instances = (GncSxInstances*)sxes->data;
194  if (xaccSchedXactionGetEnabled(sx_instances->sx))
195  {
196  list = g_list_append(list, GUINT_TO_POINTER(GPOINTER_TO_UINT(sx_instances->sx)));
197  }
198  }
199  return list;
200 }
201 
202 static gchar*
203 gsidca_get_name(GncDenseCalModel *model, guint tag)
204 {
205  GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(model);
206  GncSxInstances *insts
207  = (GncSxInstances*)g_list_find_custom(adapter->instances->sx_instance_list, GUINT_TO_POINTER(tag), gsidca_find_sx_with_tag)->data;
208  if (insts == NULL)
209  return NULL;
210  return xaccSchedXactionGetName(insts->sx);
211 }
212 
213 static gchar*
214 gsidca_get_info(GncDenseCalModel *model, guint tag)
215 {
216  GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(model);
217  // g_list_find(instances->sxes, {sx_to_tag, tag}).get_freq_spec().get_freq_str();
218  GList *schedule;
219  gchar *schedule_str;
220  GncSxInstances *insts
221  = (GncSxInstances*)g_list_find_custom(adapter->instances->sx_instance_list, GUINT_TO_POINTER(tag), gsidca_find_sx_with_tag)->data;
222  if (insts == NULL)
223  return NULL;
224  schedule = gnc_sx_get_schedule(insts->sx);
225  schedule_str = recurrenceListToCompactString(schedule);
226  return schedule_str;
227 }
228 
229 static gint
230 gsidca_get_instance_count(GncDenseCalModel *model, guint tag)
231 {
232  GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(model);
233  // g_list_find(instances->sxes, {sx_to_tag, tag}).length();
234  GncSxInstances *insts
235  = (GncSxInstances*)g_list_find_custom(adapter->instances->sx_instance_list, GUINT_TO_POINTER(tag), gsidca_find_sx_with_tag)->data;
236  if (insts == NULL)
237  return 0;
238  return g_list_length(insts->instance_list);
239 }
240 
241 static void
242 gsidca_get_instance(GncDenseCalModel *model, guint tag, gint instance_index, GDate *date)
243 {
244  GncSxInstanceDenseCalAdapter *adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(model);
245  GncSxInstance *inst;
246  GncSxInstances *insts
247  = (GncSxInstances*)g_list_find_custom(adapter->instances->sx_instance_list, GUINT_TO_POINTER(tag), gsidca_find_sx_with_tag)->data;
248  if (insts == NULL)
249  return;
250  inst = (GncSxInstance*)g_list_nth_data(insts->instance_list, instance_index);
251  g_date_valid(&inst->date);
252  *date = inst->date;
253  g_date_valid(date);
254 }
255 
256 static void
257 gnc_sx_instance_dense_cal_adapter_dispose(GObject *obj)
258 {
260  g_return_if_fail(obj != NULL);
261  adapter = GNC_SX_INSTANCE_DENSE_CAL_ADAPTER(obj);
262  // g_return_if_fail(!adapter->disposed);
263  if (adapter->disposed) return;
264  adapter->disposed = TRUE;
265 
266  g_object_unref(G_OBJECT(adapter->instances));
267  adapter->instances = NULL;
268 
269  G_OBJECT_CLASS(parent_class)->dispose(obj);
270 }
271 
272 static void gnc_sx_instance_dense_cal_adapter_finalize(GObject *obj)
273 {
274  g_return_if_fail(obj != NULL);
275  // nop
276  G_OBJECT_CLASS(parent_class)->finalize(obj);
277 }
GList * gnc_sx_get_schedule(const SchedXaction *sx)
Definition: SchedXaction.c:559
void gnc_sx_instance_model_update_sx_instances(GncSxInstanceModel *model, SchedXaction *sx)