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