]> Dogcows Code - chaz/homebank/blob - src/hb-payee.c
import homebank-4.6.3
[chaz/homebank] / src / hb-payee.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2014 Maxime DOYEN
3 *
4 * This file is part of HomeBank.
5 *
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * HomeBank is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "homebank.h"
21 #include "hb-payee.h"
22
23
24 /****************************************************************************/
25 /* Debug macros */
26 /****************************************************************************/
27 #define MYDEBUG 0
28
29 #if MYDEBUG
30 #define DB(x) (x);
31 #else
32 #define DB(x);
33 #endif
34
35 /* our global datas */
36 extern struct HomeBank *GLOBALS;
37
38 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
39
40 void
41 da_pay_free(Payee *item)
42 {
43 DB( g_print("da_pay_free\n") );
44 if(item != NULL)
45 {
46 DB( g_print(" => %d, %s\n", item->key, item->name) );
47
48 g_free(item->name);
49 g_free(item);
50 }
51 }
52
53
54 Payee *
55 da_pay_malloc(void)
56 {
57 DB( g_print("da_pay_malloc\n") );
58 return g_malloc0(sizeof(Payee));
59 }
60
61
62 void
63 da_pay_destroy(void)
64 {
65 DB( g_print("da_pay_destroy\n") );
66 g_hash_table_destroy(GLOBALS->h_pay);
67 }
68
69
70 void
71 da_pay_new(void)
72 {
73 Payee *item;
74
75 DB( g_print("da_pay_new\n") );
76 GLOBALS->h_pay = g_hash_table_new_full(g_int_hash, g_int_equal, (GDestroyNotify)g_free, (GDestroyNotify)da_pay_free);
77
78 // insert our 'no payee'
79 item = da_pay_malloc();
80 item->name = g_strdup("");
81 da_pay_insert(item);
82 }
83
84
85 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
86 static void da_pay_max_key_ghfunc(gpointer key, Payee *item, guint32 *max_key)
87 {
88 *max_key = MAX(*max_key, item->key);
89 }
90
91 static gboolean da_pay_name_grfunc(gpointer key, Payee *item, gchar *name)
92 {
93 if( name && item->name )
94 {
95 if(!strcasecmp(name, item->name))
96 return TRUE;
97 }
98 return FALSE;
99 }
100
101 /**
102 * da_pay_length:
103 *
104 * Return value: the number of elements
105 */
106 guint
107 da_pay_length(void)
108 {
109 return g_hash_table_size(GLOBALS->h_pay);
110 }
111
112 /*
113 gboolean
114 da_pay_create_none(void)
115 {
116 Payee *pay;
117 guint32 *new_key;
118
119 DB( g_print("da_pay_insert none\n") );
120
121 pay = da_pay_malloc();
122 new_key = g_new0(guint32, 1);
123 *new_key = 0;
124 pay->key = 0;
125 pay->name = g_strdup("");
126
127 DB( g_print(" -> insert id: %d\n", *new_key) );
128
129 g_hash_table_insert(GLOBALS->h_pay, new_key, pay);
130
131
132 return TRUE;
133 }
134 */
135
136
137 /**
138 * da_pay_remove:
139 *
140 * remove an payee from the GHashTable
141 *
142 * Return value: TRUE if the key was found and removed
143 *
144 */
145 gboolean
146 da_pay_remove(guint32 key)
147 {
148 DB( g_print("da_pay_remove %d\n", key) );
149
150 return g_hash_table_remove(GLOBALS->h_pay, &key);
151 }
152
153 /**
154 * da_pay_insert:
155 *
156 * insert an payee into the GHashTable
157 *
158 * Return value: TRUE if inserted
159 *
160 */
161 gboolean
162 da_pay_insert(Payee *item)
163 {
164 guint32 *new_key;
165
166 DB( g_print("da_pay_insert\n") );
167
168 new_key = g_new0(guint32, 1);
169 *new_key = item->key;
170 g_hash_table_insert(GLOBALS->h_pay, new_key, item);
171
172 return TRUE;
173 }
174
175
176 /**
177 * da_pay_append:
178 *
179 * append a new payee into the GHashTable
180 *
181 * Return value: TRUE if inserted
182 *
183 */
184 gboolean
185 da_pay_append(Payee *item)
186 {
187 Payee *existitem;
188 guint32 *new_key;
189
190 DB( g_print("da_pay_append\n") );
191
192 /* ensure no duplicate */
193 //g_strstrip(item->name);
194 if( item->name != NULL )
195 {
196 existitem = da_pay_get_by_name( item->name );
197 if( existitem == NULL )
198 {
199 new_key = g_new0(guint32, 1);
200 *new_key = da_pay_get_max_key() + 1;
201 item->key = *new_key;
202
203 DB( g_print(" -> append id: %d\n", *new_key) );
204
205 g_hash_table_insert(GLOBALS->h_pay, new_key, item);
206 return TRUE;
207 }
208 }
209
210 DB( g_print(" -> %s already exist: %d\n", item->name, item->key) );
211
212 return FALSE;
213 }
214
215 /**
216 * da_pay_get_max_key:
217 *
218 * Get the biggest key from the GHashTable
219 *
220 * Return value: the biggest key value
221 *
222 */
223 guint32
224 da_pay_get_max_key(void)
225 {
226 guint32 max_key = 0;
227
228 g_hash_table_foreach(GLOBALS->h_pay, (GHFunc)da_pay_max_key_ghfunc, &max_key);
229 return max_key;
230 }
231
232
233
234
235 /**
236 * da_pay_get_by_name:
237 *
238 * Get an payee structure by its name
239 *
240 * Return value: Payee * or NULL if not found
241 *
242 */
243 Payee *
244 da_pay_get_by_name(gchar *name)
245 {
246 DB( g_print("da_pay_get_by_name\n") );
247
248 return g_hash_table_find(GLOBALS->h_pay, (GHRFunc)da_pay_name_grfunc, name);
249 }
250
251
252
253 /**
254 * da_pay_get:
255 *
256 * Get an payee structure by key
257 *
258 * Return value: Payee * or NULL if not found
259 *
260 */
261 Payee *
262 da_pay_get(guint32 key)
263 {
264 //DB( g_print("da_pay_get\n") );
265
266 return g_hash_table_lookup(GLOBALS->h_pay, &key);
267 }
268
269
270 void da_pay_consistency(Payee *item)
271 {
272 g_strstrip(item->name);
273 }
274
275
276 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
277
278 #if MYDEBUG
279
280 static void
281 da_pay_debug_list_ghfunc(gpointer key, gpointer value, gpointer user_data)
282 {
283 guint32 *id = key;
284 Payee *item = value;
285
286 DB( g_print(" %d :: %s\n", *id, item->name) );
287
288 }
289
290 static void
291 da_pay_debug_list(void)
292 {
293
294 DB( g_print("\n** debug **\n") );
295
296 g_hash_table_foreach(GLOBALS->h_pay, da_pay_debug_list_ghfunc, NULL);
297
298 DB( g_print("\n** end debug **\n") );
299
300 }
301
302 #endif
303
304
305
306
307 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
308
309
310
311
312 gboolean
313 payee_is_used(guint32 key)
314 {
315 GList *lrul, *list;
316
317 list = g_list_first(GLOBALS->ope_list);
318 while (list != NULL)
319 {
320 Transaction *entry = list->data;
321 if( key == entry->kpay )
322 return TRUE;
323 list = g_list_next(list);
324 }
325
326 list = g_list_first(GLOBALS->arc_list);
327 while (list != NULL)
328 {
329 Archive *entry = list->data;
330 if( key == entry->kpay )
331 return TRUE;
332 list = g_list_next(list);
333 }
334
335 lrul = list = g_hash_table_get_values(GLOBALS->h_rul);
336 while (list != NULL)
337 {
338 Assign *entry = list->data;
339
340 if( key == entry->kpay)
341 return TRUE;
342 list = g_list_next(list);
343 }
344 g_list_free(lrul);
345
346
347 return FALSE;
348 }
349
350 void
351 payee_move(guint32 key1, guint32 key2)
352 {
353 GList *lrul, *list;
354
355 list = g_list_first(GLOBALS->ope_list);
356 while (list != NULL)
357 {
358 Transaction *entry = list->data;
359 if(entry->kpay == key1)
360 {
361 entry->kpay = key2;
362 entry->flags |= OF_CHANGED;
363 }
364 list = g_list_next(list);
365 }
366
367 list = g_list_first(GLOBALS->arc_list);
368 while (list != NULL)
369 {
370 Archive *entry = list->data;
371 if(entry->kpay == key1)
372 {
373 entry->kpay = key2;
374 }
375 list = g_list_next(list);
376 }
377
378 lrul = list = g_hash_table_get_values(GLOBALS->h_rul);
379 while (list != NULL)
380 {
381 Assign *entry = list->data;
382
383 if(entry->kpay == key1)
384 {
385 entry->kpay = key2;
386 }
387 list = g_list_next(list);
388 }
389 g_list_free(lrul);
390 }
391
392
393 gboolean
394 payee_rename(Payee *item, const gchar *newname)
395 {
396 Payee *existitem;
397 gchar *stripname;
398
399 stripname = g_strdup(newname);
400 g_strstrip(stripname);
401
402 existitem = da_pay_get_by_name(stripname);
403
404 if( existitem != NULL )
405 {
406 if( existitem->key == item->key )
407 return TRUE;
408 }
409 else
410 {
411 g_free(item->name);
412 item->name = g_strdup(stripname);
413 return TRUE;
414 }
415
416 g_free(stripname);
417
418 return FALSE;
419 }
420
421
422 /**
423 * payee_append_if_new:
424 *
425 * append a new payee into the GHashTable
426 *
427 * Return value: a new Payee or NULL
428 *
429 */
430 Payee *
431 payee_append_if_new(gchar *name)
432 {
433 gchar *stripname;
434 Payee *item;
435
436 stripname = g_strdup(name);
437 g_strstrip(stripname);
438 item = da_pay_get_by_name(stripname);
439
440 if(item == NULL)
441 {
442 item = da_pay_malloc();
443 item->name = g_strdup(stripname);
444 da_pay_append(item);
445 }
446 else
447 item = NULL;
448
449 g_free(stripname);
450
451 return item;
452 }
453
454 static gint
455 payee_glist_name_compare_func(Payee *a, Payee *b)
456 {
457 return hb_string_utf8_compare(a->name, b->name);
458 }
459
460
461 static gint
462 payee_glist_key_compare_func(Payee *a, Payee *b)
463 {
464 return a->key - b->key;
465 }
466
467
468 GList *payee_glist_sorted(gint column)
469 {
470 GList *list = g_hash_table_get_values(GLOBALS->h_pay);
471
472 if(column == 0)
473 return g_list_sort(list, (GCompareFunc)payee_glist_key_compare_func);
474 else
475 return g_list_sort(list, (GCompareFunc)payee_glist_name_compare_func);
476 }
477
478
479
480 void
481 payee_load_csv(gchar *filename)
482 {
483 GIOChannel *io;
484 gchar *tmpstr;
485 gint io_stat;
486 const gchar *encoding;
487
488 encoding = homebank_file_getencoding(filename);
489
490 io = g_io_channel_new_file(filename, "r", NULL);
491 if(io != NULL)
492 {
493 DB( g_print(" -> encoding should be %s\n", encoding) );
494 if( encoding != NULL )
495 {
496 g_io_channel_set_encoding(io, encoding, NULL);
497 }
498
499 for(;;)
500 {
501 io_stat = g_io_channel_read_line(io, &tmpstr, NULL, NULL, NULL);
502 if( io_stat == G_IO_STATUS_EOF)
503 break;
504 if( io_stat == G_IO_STATUS_NORMAL)
505 {
506 if( tmpstr != NULL)
507 {
508 hb_string_strip_crlf(tmpstr);
509
510 DB( g_print(" read %s\n", tmpstr) );
511
512 if( payee_append_if_new( tmpstr ) )
513 {
514 GLOBALS->changes_count++;
515 }
516
517 }
518 g_free(tmpstr);
519 }
520
521 }
522 g_io_channel_unref (io);
523 }
524
525 }
526
527
528 void
529 payee_save_csv(gchar *filename)
530 {
531 GIOChannel *io;
532 GList *lpay, *list;
533 gchar *outstr;
534
535 io = g_io_channel_new_file(filename, "w", NULL);
536 if(io != NULL)
537 {
538 lpay = list = payee_glist_sorted(1);
539
540 while (list != NULL)
541 {
542 Payee *item = list->data;
543
544 if(item->key != 0)
545 {
546 outstr = g_strdup_printf("%s\n", item->name);
547 g_io_channel_write_chars(io, outstr, -1, NULL, NULL);
548
549 DB( g_print("%s", outstr) );
550
551 g_free(outstr);
552 }
553 list = g_list_next(list);
554 }
555 g_list_free(lpay);
556
557 g_io_channel_unref (io);
558 }
559
560 }
561
562
This page took 0.058629 seconds and 4 git commands to generate.