GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-dense-cal-store.c
1/********************************************************************\
2 * gnc-dense-cal-store.h *
3 * Copyright (C) 2006 Joshua Sled <jsled@asynchronous.org> *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation, under version 2 and *
8 * / or version 3 of the License. *
9 * *
10 * As a special exception, permission is granted to link the binary *
11 * module resultant from this code with the OpenSSL project's *
12 * "OpenSSL" library (or modified versions of it that use the same *
13 * license as the "OpenSSL" library), and distribute the linked *
14 * executable. You must obey the GNU General Public License in all *
15 * respects for all of the code used other than "OpenSSL". If you *
16 * modify this file, you may extend this exception to your version *
17 * of the file, but you are not obligated to do so. If you do not *
18 * wish to do so, delete this exception statement from your version *
19 * of this file. *
20 * *
21 * This program is distributed in the hope that it will be useful, *
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
24 * GNU General Public License for more details. *
25 * *
26 * You should have received a copy of the GNU General Public License*
27 * along with this program; if not, contact: *
28 * *
29 * Free Software Foundation Voice: +1-617-542-5942 *
30 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
31 * Boston, MA 02110-1301, USA gnu@gnu.org *
32\********************************************************************/
33
34#include <config.h>
35#include <glib.h>
36#include <glib-object.h>
37#include "gnc-date.h"
38#include "gnc-dense-cal.h"
39#include "gnc-dense-cal-model.h"
40#include "gnc-dense-cal-store.h"
41#include "Recurrence.h"
42
44{
45 GObject parent;
46
47 GDate start_date;
48 gdcs_end_type end_type;
49 GDate end_date;
50 gint n_occurrences;
51 gchar *name;
52 gchar *info;
53 int num_marks;
54 int num_real_marks;
55 GDate **cal_marks;
56};
57
58static void gnc_dense_cal_store_iface_init (GncDenseCalModelInterface *iface);
59static void gnc_dense_cal_store_finalize (GObject *obj);
60
61static GList* gdcs_get_contained (GncDenseCalModel *model);
62static gchar* gdcs_get_name (GncDenseCalModel *model, guint tag);
63static gchar* gdcs_get_info (GncDenseCalModel *model, guint tag);
64static gint gdcs_get_instance_count (GncDenseCalModel *model, guint tag);
65static void gdcs_get_instance (GncDenseCalModel *model, guint tag, gint instance_index, GDate *date);
66
67G_DEFINE_TYPE_WITH_CODE(GncDenseCalStore, gnc_dense_cal_store, G_TYPE_OBJECT,
68 G_IMPLEMENT_INTERFACE(GNC_TYPE_DENSE_CAL_MODEL, gnc_dense_cal_store_iface_init))
69
70static void
71gnc_dense_cal_store_class_init (GncDenseCalStoreClass *klass)
72{
73 GObjectClass *object_class = G_OBJECT_CLASS(klass);
74
75 object_class->finalize = gnc_dense_cal_store_finalize;
76}
77
78static void
79gnc_dense_cal_store_init (GncDenseCalStore *self)
80{
81}
82
83static void
84gnc_dense_cal_store_iface_init (GncDenseCalModelInterface *iface)
85{
86 iface->get_contained = gdcs_get_contained;
87 iface->get_name = gdcs_get_name;
88 iface->get_info = gdcs_get_info;
89 iface->get_instance_count = gdcs_get_instance_count;
90 iface->get_instance = gdcs_get_instance;
91}
92
93GncDenseCalStore*
94gnc_dense_cal_store_new (int num_marks)
95{
96 GncDenseCalStore *model = g_object_new (GNC_TYPE_DENSE_CAL_STORE, NULL);
97 model->num_marks = num_marks;
98 model->cal_marks = g_new0 (GDate*, num_marks);
99 {
100 for (int i = 0; i < model->num_marks; i++)
101 {
102 model->cal_marks[i] = g_date_new();
103 }
104 }
105 model->num_real_marks = 0;
106 g_date_clear (&model->start_date, 1);
107 gnc_gdate_set_today (&model->start_date);
108 model->end_type = NEVER_END;
109 g_date_clear (&model->end_date, 1);
110 gnc_gdate_set_today (&model->end_date);
111 model->n_occurrences = 0;
112 return model;
113}
114
115void
116gnc_dense_cal_store_clear (GncDenseCalStore *model)
117{
118 model->num_real_marks = 0;
119 g_signal_emit_by_name (model, "update", GUINT_TO_POINTER(1));
120}
121
122void
123gnc_dense_cal_store_update_name (GncDenseCalStore *model, const gchar *name)
124{
125 if (model->name != NULL)
126 g_free (model->name);
127
128 model->name = g_strdup (name);
129 //g_signal_emit_by_name(model, "update", GUINT_TO_POINTER(1));
130}
131
132void
133gnc_dense_cal_store_update_info (GncDenseCalStore *model, const gchar *info)
134{
135 if (model->info != NULL)
136 g_free (model->info);
137
138 model->info = g_strdup (info);
139 //g_signal_emit_by_name(model, "update", GUINT_TO_POINTER(1));
140}
141
142static void
143gdcs_generic_update_recurrences (GncDenseCalStore *trans, GDate *start, GList *recurrences)
144{
145 int i = 0;
146 GDate date, next;
147
148 date = *start;
149 recurrenceListNextInstance (recurrences, &date, &next);
150
151 while ((i < trans->num_marks)
152 && g_date_valid (&next)
153 /* Do checking against end restriction. */
154 && ((trans->end_type == NEVER_END)
155 || (trans->end_type == END_ON_DATE
156 && g_date_compare (&next, &trans->end_date) <= 0)
157 || (trans->end_type == END_AFTER_N_OCCS
158 && i < trans->n_occurrences)))
159 {
160 *trans->cal_marks[i++] = next;
161 date = next;
162 recurrenceListNextInstance (recurrences, &date, &next);
163 }
164 trans->num_real_marks = i;
165 /* cstim: Previously this was i-1 but that's just plain wrong for
166 * occurrences which are coming to an end, because then i contains
167 * the number of (rest) occurrences exactly! Subtracting one means
168 * we will miss the last one. */
169
170 g_signal_emit_by_name (trans, "update", GUINT_TO_POINTER(1));
171}
172
173void
174gnc_dense_cal_store_update_recurrences_no_end (GncDenseCalStore *model,
175 GDate *start,
176 GList *recurrences)
177{
178 model->end_type = NEVER_END;
179 gdcs_generic_update_recurrences (model, start, recurrences);
180}
181
182void
183gnc_dense_cal_store_update_recurrences_count_end (GncDenseCalStore *model,
184 GDate *start,
185 GList *recurrences,
186 int num_occur)
187{
188 model->end_type = END_AFTER_N_OCCS;
189 model->n_occurrences = num_occur;
190 gdcs_generic_update_recurrences (model, start, recurrences);
191}
192
193void
194gnc_dense_cal_store_update_recurrences_date_end (GncDenseCalStore *model,
195 GDate *start,
196 GList *recurrences,
197 GDate *end_date)
198{
199 model->end_type = END_ON_DATE;
200 model->end_date = *end_date;
201 gdcs_generic_update_recurrences (model, start, recurrences);
202}
203
204static GList*
205gdcs_get_contained (GncDenseCalModel *model)
206{
207 GList *rtn = NULL;
208 rtn = g_list_append (rtn, GUINT_TO_POINTER(1));
209 return rtn;
210}
211
212static gchar*
213gdcs_get_name (GncDenseCalModel *model, guint tag)
214{
215 GncDenseCalStore *mdl = GNC_DENSE_CAL_STORE(model);
216 // assert(tag == 1)
217 return mdl->name;
218}
219
220static gchar*
221gdcs_get_info (GncDenseCalModel *model, guint tag)
222{
223 GncDenseCalStore *mdl = GNC_DENSE_CAL_STORE(model);
224 // assert(tag == 1)
225 return g_strdup (mdl->info);
226}
227
228static gint
229gdcs_get_instance_count (GncDenseCalModel *model, guint tag)
230{
231 GncDenseCalStore *mdl = GNC_DENSE_CAL_STORE(model);
232 // assert(tag == 1)
233 return mdl->num_real_marks;
234}
235
236static void
237gdcs_get_instance (GncDenseCalModel *model, guint tag, gint instance_index, GDate *date)
238{
239 GncDenseCalStore *mdl = GNC_DENSE_CAL_STORE(model);
240 // assert(tag == 1)
241 // assert 0 < instance_index < model->num_marks;
242 *date = *mdl->cal_marks[instance_index];
243}
244
245static void
246gnc_dense_cal_store_finalize (GObject *obj)
247{
248 GncDenseCalStore *store;
249 g_return_if_fail (obj != NULL);
250
251 store = GNC_DENSE_CAL_STORE(obj);
252
253 if (store->name != NULL)
254 {
255 g_free (store->name);
256 store->name = NULL;
257 }
258
259 if (store->info != NULL)
260 {
261 g_free (store->info);
262 store->info = NULL;
263 }
264
265 for (int i = 0; i < store->num_marks; i++)
266 {
267 g_free (store->cal_marks[i]);
268 store->cal_marks[i] = NULL;
269 }
270 if (store->cal_marks != NULL)
271 {
272 g_free (store->cal_marks);
273 store->cal_marks = NULL;
274 }
275
276 G_OBJECT_CLASS(gnc_dense_cal_store_parent_class)->finalize (obj);
277}
Date and Time handling routines.
void gnc_gdate_set_today(GDate *gd)
Set a GDate to the current day.