File: gsb_data_currency_link.c

package info (click to toggle)
grisbi 0.8.9-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 15,172 kB
  • sloc: ansic: 111,365; sh: 11,062; makefile: 581; perl: 370
file content (698 lines) | stat: -rw-r--r-- 18,280 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
/* ************************************************************************** */
/*                                                                            */
/*     Copyright (C)    2000-2006 Cédric Auger (cedric@grisbi.org)            */
/*          http://www.grisbi.org                                             */
/*                      2009 Pierre Biava (grisbi@pierre.biava.name)          */
/*                                                                            */
/*  This program is free software; you can redistribute it and/or modify      */
/*  it under the terms of the GNU General Public License as published by      */
/*  the Free Software Foundation; either version 2 of the License, or         */
/*  (at your option) any later version.                                       */
/*                                                                            */
/*  This program is distributed in the hope that it will be useful,           */
/*  but WITHOUT ANY WARRANTY; without even the implied warranty of            */
/*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             */
/*  GNU General Public License for more details.                              */
/*                                                                            */
/*  You should have received a copy of the GNU General Public License         */
/*  along with this program; if not, write to the Free Software               */
/*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
/*                                                                            */
/* ************************************************************************** */

/**
 * \file gsb_data_currency_link.c
 * work with the currency_link structure, no GUI here
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "include.h"

/*START_INCLUDE*/
#include "gsb_data_currency_link.h"
#include "utils_dates.h"
#include "dialog.h"
#include "gsb_real.h"
/*END_INCLUDE*/


/**
 * \struct 
 * Describe a currency_link 
 */
typedef struct
{
    gint currency_link_number;

    gint first_currency;
    gint second_currency;
    gint fixed_link;  /* si = 1 le lien entre devises est fixe */
    gsb_real change_rate;
    GDate *modified_date;

    /* a link is invalid when :
     * - it's a comparison between 2 same currencies
     * - the same link exists before
     * it's set invalid while the configuration */
    gboolean invalid_link;

} struct_currency_link;

/*START_STATIC*/
static void _g_data_currency_link_free ( struct_currency_link *currency_link );
static gboolean gsb_data_currency_link_check_for_invalid ( gint currency_link_number );
static gpointer gsb_data_currency_link_get_structure ( gint currency_link_number );
static gint gsb_data_currency_link_max_number ( void );
/*END_STATIC*/

/*START_EXTERN*/
extern gsb_real null_real;
/*END_EXTERN*/

/** contains the g_slist of struct_currency_link */
static GSList *currency_link_list = NULL;

/** a pointer to the last currency_link used (to increase the speed) */
static struct_currency_link *currency_link_buffer;


/**
 * set the currency_links global variables to NULL, usually when we init all the global variables
 *
 * \param none
 *
 * \return FALSE
 * */
gboolean gsb_data_currency_link_init_variables ( void )
{
    if ( currency_link_list )
    {
        GSList* tmp_list = currency_link_list;
        while ( tmp_list )
        {
	    struct_currency_link *currency_link;
	    currency_link = tmp_list -> data;
	    tmp_list = tmp_list -> next;
            _g_data_currency_link_free ( currency_link );
        }
	g_slist_free ( currency_link_list );
    }
    currency_link_list = NULL;
    currency_link_buffer = NULL;
    return FALSE;
}


/**
 * find and return the structure of the currency_link asked
 *
 * \param currency_link_number number of currency_link
 *
 * \return the adr of the struct of the currency_link (NULL if doesn't exit)
 * */
gpointer gsb_data_currency_link_get_structure ( gint currency_link_number )
{
    GSList *tmp;

    if (!currency_link_number)
	return NULL;

    /* before checking all the currency_links, we check the buffer */

    if ( currency_link_buffer
	 &&
	 currency_link_buffer -> currency_link_number == currency_link_number )
	return currency_link_buffer;

    tmp = currency_link_list;

    while ( tmp )
    {
	struct_currency_link *currency_link;

	currency_link = tmp -> data;

	if ( currency_link -> currency_link_number == currency_link_number )
	{
	    currency_link_buffer = currency_link;
	    return currency_link;
	}
	tmp = tmp -> next;
    }
    return NULL;
}

/**
 * return the number of the currency_link given in param
 *
 * \param currency_link_ptr a pointer to the struct of the currency_link
 *
 * \return the number of the currency_link, 0 if problem
 * */
gint gsb_data_currency_link_get_no_currency_link ( gpointer currency_link_ptr )
{
    struct_currency_link *currency_link;

    if ( !currency_link_ptr )
	return 0;

    currency_link = currency_link_ptr;
    currency_link_buffer = currency_link;
    return currency_link -> currency_link_number;
}


/**
 * give the g_slist of currency_link structure
 * usefull when want to check all currency_links
 *
 * \param none
 *
 * \return the g_slist of currency_links structure
 * */
GSList *gsb_data_currency_link_get_currency_link_list ( void )
{
    return currency_link_list;
}



/**
 * find and return the last number of currency_link
 * 
 * \param none
 * 
 * \return last number of currency_link
 * */
gint gsb_data_currency_link_max_number ( void )
{
    GSList *tmp;
    gint number_tmp = 0;

    tmp = currency_link_list;

    while ( tmp )
    {
	struct_currency_link *currency_link;

	currency_link = tmp -> data;

	if ( currency_link -> currency_link_number > number_tmp )
	    number_tmp = currency_link -> currency_link_number;

	tmp = tmp -> next;
    }
    return number_tmp;
}


/**
 * create a new currency_link, give him a number, append it to the list
 * and return the number
 *
 * \param currency_link_number the number of the new currency link or 0 for automatic
 * 		(should be always automatic, except while loading a file)
 *
 * \return the number of the new currency_link
 * */
gint gsb_data_currency_link_new ( gint currency_link_number )
{
    struct_currency_link *currency_link;

    currency_link = g_malloc0 ( sizeof ( struct_currency_link ));

    if (currency_link_number)
	currency_link -> currency_link_number = currency_link_number;
    else
	currency_link -> currency_link_number = gsb_data_currency_link_max_number () + 1;

    currency_link -> modified_date = gdate_today ( );

    currency_link_list = g_slist_append ( currency_link_list, currency_link );

    return currency_link -> currency_link_number;
}


/**
 * This internal function is called to free the memory used by a struct_currency_link structure.
 */
static void _g_data_currency_link_free ( struct_currency_link *currency_link )
{
    if ( ! currency_link )
        return ;
    if ( currency_link -> modified_date )
        g_date_free ( currency_link -> modified_date );
    g_free (currency_link);
    if ( currency_link_buffer == currency_link )
	currency_link_buffer = NULL;
}

/**
 * remove a currency_link
 * set all the currency_links of transaction which are this one to 0
 * update combofix and mark file as modified
 *
 * \param currency_link_number the currency_link we want to remove
 *
 * \return TRUE ok
 * */
gboolean gsb_data_currency_link_remove ( gint currency_link_number )
{
    struct_currency_link *currency_link;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if (!currency_link)
	return FALSE;

    currency_link_list = g_slist_remove ( currency_link_list,
					  currency_link );

    _g_data_currency_link_free ( currency_link );

    return TRUE;
}


/**
 * set a new number for the currency_link
 * normally used only while loading the file because
 * the number are given automaticly
 *
 * \param currency_link_number the number of the currency_link
 * \param new_no_currency_link the new number of the currency_link
 *
 * \return the new number or 0 if the currency_link doen't exist
 * */
gint gsb_data_currency_link_set_new_number ( gint currency_link_number,
                        gint new_no_currency_link )
{
    struct_currency_link *currency_link;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if (!currency_link)
	return 0;

    currency_link -> currency_link_number = new_no_currency_link;
    return new_no_currency_link;
}



/**
 * return the first_currency of the currency_link
 *
 * \param currency_link_number the number of the currency_link
 *
 * \return the first_currency of the currency_link or 0 if fail
 * */
gint gsb_data_currency_link_get_first_currency ( gint currency_link_number )
{
    struct_currency_link *currency_link;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if (!currency_link)
	return 0;

    return currency_link -> first_currency;
}


/**
 * set the first_currency of the currency_link
 * check and fill the invalid flag
 *
 * \param currency_link_number the number of the currency_link
 * \param first_currency the first_currency of the currency_link
 *
 * \return TRUE if ok or FALSE if problem
 * */
gboolean gsb_data_currency_link_set_first_currency ( gint currency_link_number,
                        gint first_currency )
{
    struct_currency_link *currency_link;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if (!currency_link)
	return FALSE;

    currency_link -> first_currency = first_currency;
    gsb_data_currency_link_check_for_invalid (currency_link_number);

    return TRUE;
}


/**
 * return the second_currency of the currency_link
 *
 * \param currency_link_number the number of the currency_link
 *
 * \return the second_currency of the currency_link or 0 if fail
 * */
gint gsb_data_currency_link_get_second_currency ( gint currency_link_number )
{
    struct_currency_link *currency_link;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if (!currency_link)
	return 0;

    return currency_link -> second_currency;
}


/**
 * set the second_currency of the currency_link
 * check and fill the invalid flag
 *
 * \param currency_link_number the number of the currency_link
 * \param second_currency the second_currency of the currency_link
 *
 * \return TRUE if ok or FALSE if problem
 * */
gboolean gsb_data_currency_link_set_second_currency ( gint currency_link_number,
                        gint second_currency )
{
    struct_currency_link *currency_link;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if (!currency_link)
	return FALSE;

    currency_link -> second_currency = second_currency;
    gsb_data_currency_link_check_for_invalid (currency_link_number);

    return TRUE;
}


/**
 * return the change_rate of the currency_link *
 * \param currency_link_number the number of the currency_link
 *
 * \return the change_rate of the currency_link or 0 if problem
 * */
gsb_real gsb_data_currency_link_get_change_rate ( gint currency_link_number )
{
    struct_currency_link *currency_link;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if (!currency_link)
	return null_real;

    return currency_link -> change_rate;
}


/**
 * set the change_rate of the currency_link
 *
 * \param currency_link_number the number of the currency_link
 * \param  the  of the currency_link
 *
 * \return TRUE if ok or FALSE if problem
 * */
gboolean gsb_data_currency_link_set_change_rate ( gint currency_link_number,
                        gsb_real change_rate )
{
    struct_currency_link *currency_link;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if (!currency_link)
	return FALSE;

    currency_link -> change_rate = change_rate;

    return TRUE;
}



/**
 * return the invalid_link of the currency_link
 * the flag invalid is set automatickly by gsb_data_currency_link_check_for_invalid
 * when change 1 of the currency
 *
 * \param currency_link_number the number of the currency_link
 *
 * \return TRUE if the link is invalid 
 * */
gint gsb_data_currency_link_get_invalid_link ( gint currency_link_number )
{
    struct_currency_link *currency_link;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if (!currency_link)
	return 0;

    return currency_link -> invalid_link;
}



/**
 * return the message error because of the invalid link
 *
 * \param currency_link_number the number of the currency_link
 *
 * \return a const gchar formatted with markup : error the message 
 * */
const gchar *gsb_data_currency_link_get_invalid_message ( gint currency_link_number )
{
    struct_currency_link *currency_link;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if (!currency_link)
        return 0;

    if (!currency_link -> invalid_link)
        return NULL;

    if ( currency_link -> first_currency == currency_link -> second_currency)
        return make_red ( _("Warning : the two currencies of the link are identicals.") );

    return make_red ( _("Warning : that link is already defined.") );
}


/**
 * check if the link is invalid and set the flag
 * a link is invalid if :
 * - the 2 currencies are the same
 * - another similar link exists already
 * an invalid link will not be used by grisbi and showed as invalid in the configuration
 *
 * \param currency_link_number the number of the currency_link to check
 *
 * \return TRUE  if invalid, FALSE if not
 * */
gboolean gsb_data_currency_link_check_for_invalid ( gint currency_link_number )
{
    struct_currency_link *currency_link;
    GSList *tmp_list;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if (!currency_link)
	return FALSE;

    /* first check : if the 2 currencies are identical */

    if ( currency_link -> first_currency == currency_link -> second_currency)
    {
	currency_link -> invalid_link = TRUE;
	return TRUE;
    }

    /* second check : if that link exists already */

    tmp_list = currency_link_list;

    while (tmp_list)
    {
	struct_currency_link *tmp_currency_link;

	tmp_currency_link = tmp_list -> data;

	if ( tmp_currency_link -> currency_link_number != currency_link -> currency_link_number
	     &&
	     (( tmp_currency_link -> first_currency == currency_link -> first_currency
		&&
		tmp_currency_link -> second_currency == currency_link -> second_currency )
	      ||
	      ( tmp_currency_link -> first_currency == currency_link -> second_currency
		&&
		tmp_currency_link -> second_currency == currency_link -> first_currency )))
	{
	    currency_link -> invalid_link = TRUE;
	    return TRUE;
	}

	tmp_list = tmp_list -> next;
    }

    /* it's ok, the link is valid */
    currency_link -> invalid_link = FALSE;

    return FALSE;
}


/**
 * look for a link between the 2 currencies given in the param
 *
 * \param currency_1 the number of the first currency
 * \param currency_2 the number of the second currency
 *
 * \return the number of the link, 0 if not found, -1 if they are the same currencies
 * */
gint gsb_data_currency_link_search ( gint currency_1,
                        gint currency_2 )
{
    GSList *tmp_list;

    if (!currency_1
	||
	!currency_2 )
	return 0;

    if ( currency_1 == currency_2 )
	return -1;

    tmp_list = currency_link_list;

    while (tmp_list)
    {
	struct_currency_link *tmp_currency_link;

	tmp_currency_link = tmp_list -> data;

	if ( !tmp_currency_link -> invalid_link
	     &&
	     (( tmp_currency_link -> first_currency == currency_1
		&&
		tmp_currency_link -> second_currency == currency_2 )
	      ||
	      ( tmp_currency_link -> first_currency == currency_2
		&&
		tmp_currency_link -> second_currency == currency_1 )))
	    return tmp_currency_link -> currency_link_number;

	tmp_list = tmp_list -> next;
    }
    return 0;
}


/**
 * get the GDate of the currency link 
 * 
 * \param currency_link_number
 * 
 * \return the GDate of the currency link
 * */
GDate *gsb_data_currency_link_get_modified_date ( gint currency_link_number )
{
    struct_currency_link *currency_link;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if (!currency_link)
        return NULL;

    return currency_link -> modified_date;
}


/**
 * set the GDate of the currency link
 * 
 * \param currency_link_number
 * \param date
 * 
 * \return TRUE if ok
 * */
gboolean gsb_data_currency_link_set_modified_date ( gint currency_link_number,
                        GDate *date )
{
    struct_currency_link *currency_link;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if ( !currency_link )
        return FALSE;

    if ( !date )
        return FALSE;

    if ( currency_link -> modified_date )
        g_date_free ( currency_link -> modified_date );

    currency_link -> modified_date = gsb_date_copy ( date );

    return TRUE;
}


/**
 * return the change_link_currency flag
 *
 * \param currency_link_number the number of the currency_link
 *
 * \return TRUE if ok or FALSE if no ok 
 * */
gboolean gsb_data_currency_link_get_fixed_link ( gint currency_link_number )
{
    struct_currency_link *currency_link;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if ( !currency_link )
	    return FALSE;

    return currency_link -> fixed_link;
}


/**
 * set the change_link_currency flag
 *
 * \param currency_link_number the number of the currency_link
 * \param  the change_link_currency flag for the currency_link
 *
 * \return TRUE if ok or FALSE if problem
 * */
gboolean gsb_data_currency_link_set_fixed_link ( gint currency_link_number,
                        gboolean fixed_link )
{
    struct_currency_link *currency_link;

    currency_link = gsb_data_currency_link_get_structure ( currency_link_number );

    if ( !currency_link )
	    return FALSE;

    currency_link -> fixed_link = fixed_link;

    return TRUE;
}


/* Local Variables: */
/* c-basic-offset: 4 */
/* End: */