File: gsb_currency.c

package info (click to toggle)
grisbi 1.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 22,604 kB
  • ctags: 6,257
  • sloc: ansic: 117,322; sh: 11,246; makefile: 785; perl: 370; xml: 11
file content (1169 lines) | stat: -rw-r--r-- 40,862 bytes parent folder | download | duplicates (2)
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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
/* ************************************************************************** */
/*                                                                            */
/*     Copyright (C)    2000-2007 Cédric Auger (cedric@grisbi.org)            */
/*          2003-2009 Benjamin Drieu (bdrieu@april.org)                       */
/*                      2009 Pierre Biava (grisbi@pierre.biava.name)          */
/*          http://www.grisbi.org                                             */
/*                                                                            */
/*  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_currency.c
 * contains tools to work with the currencies
 */


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

#include "include.h"
#include <glib/gi18n.h>

/*START_INCLUDE*/
#include "gsb_currency.h"
#include "dialog.h"
#include "gsb_autofunc.h"
#include "gsb_color.h"
#include "gsb_currency_config.h"
#include "gsb_data_account.h"
#include "gsb_data_currency.h"
#include "gsb_data_currency_link.h"
#include "gsb_data_form.h"
#include "gsb_data_transaction.h"
#include "gsb_dirs.h"
#include "gsb_form_widget.h"
#include "gsb_real.h"
#include "structures.h"
#include "utils.h"
#include "utils_files.h"
#include "utils_real.h"
#include "erreur.h"
/*END_INCLUDE*/

/*START_STATIC*/
static struct cached_exchange_rate *gsb_currency_config_get_cached_exchange ( gint currency1_number,
                        gint currency2_number );
static gboolean gsb_currency_checkbutton_link_changed ( GtkWidget *checkbutton,
						  gboolean *value );
static void gsb_currency_config_set_cached_exchange ( gint currency1_number,
                        gint currency2_number,
                        gsb_real change, gsb_real fees );
static gboolean gsb_currency_create_combobox_store ( void );
static GtkWidget *gsb_currency_make_combobox_exchange_dialog ( gint transaction_currency_number,
                        gint account_currency_number,
                        gint set_index );
static gboolean gsb_currency_select_change_currency ( GtkWidget *combobox_1,
                        GtkWidget *combobox_2 );
static gboolean gsb_currency_select_double_amount ( GtkWidget *entry_1,
                        GtkWidget *entry_2 );
/*END_STATIC*/

/**
 * the currency list store, contains 3 columns :
 * 1 : the code of the currency
 * 2 : the name(code) of the currency
 * 3 : the number of the currency
 * used to be set in the combobox */
static GtkListStore *combobox_currency_store;

enum currency_list_columns {
    CURRENCY_COL_CODE = 0,
    CURRENCY_COL_NAME,
    CURRENCY_COL_NUMBER,
    CURRENCY_COL_FLAG,
    CURRENCY_NUM_COL,		/** Number of columns */
};

/** Exchange rates cache, used by
 * gsb_currency_config_set_cached_exchange
 * and
 * gsb_currency_config_get_cached_exchange */
static GSList * cached_exchange_rates = NULL;


/**
 * the 2 next variables are filled by gsb_currency_exchange_dialog
 * and permit to the form to get the result by the functions
 * gsb_currency_get_current_exchange
 * gsb_currency_get_current_exchange_fees
 * */
static gsb_real current_exchange;
static gsb_real current_exchange_fees;


/*START_EXTERN*/
extern GtkWidget *combo_devise_totaux_categ;
extern GtkWidget *combo_devise_totaux_ib;
extern GtkWidget *combo_devise_totaux_tiers;
extern GtkWidget *detail_devise_compte;
/*END_EXTERN*/


/**
 * set to NULL the static variables
 *
 * \param
 *
 * \return
 * */
void gsb_currency_init_variables ( void )
{
    if (combobox_currency_store
    &&
    GTK_IS_LIST_STORE (combobox_currency_store))
    gtk_list_store_clear (combobox_currency_store);

    combobox_currency_store = NULL;
    current_exchange = null_real;
    current_exchange_fees = null_real;
}

/**
 * create and return a combobox with the currencies
 * for automatic value changed in memory, see gsb_autofunc_currency_new
 *
 * \param set_name if TRUE, the currencies in the combobox will be name(code), else it will be only the code
 *
 * \return a widget combobox or NULL
 * */
GtkWidget *gsb_currency_make_combobox ( gboolean set_name )
{
    GtkCellRenderer *text_renderer, *flag_renderer;
    GtkWidget *combo_box;

    if ( !combobox_currency_store )
        gsb_currency_create_combobox_store ();

    combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL
                        (combobox_currency_store));

    /* Flag renderer */
    flag_renderer = gtk_cell_renderer_pixbuf_new ();
    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), flag_renderer, FALSE);
    gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), flag_renderer,
				    "pixbuf", CURRENCY_COL_FLAG, NULL );

    GTK_CELL_RENDERER(flag_renderer) -> xpad = 3; /* Ugly but how to set it otherwise ?*/

    text_renderer = gtk_cell_renderer_text_new ();
    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), text_renderer, FALSE);

    if (set_name)
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), text_renderer,
					"text", CURRENCY_COL_NAME,
					NULL);
    else
	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), text_renderer,
					"text", CURRENCY_COL_CODE,
					NULL);

    gtk_combo_box_set_active ( GTK_COMBO_BOX (combo_box),
			       0 );

    return (combo_box);
}


/**
 * set the combobox on the currency given in param
 *
 * \combo_box the combo-box to set
 * \currency_number the currency we want to set on the combo-box
 *
 * \return TRUE currency found, FALSE currency not found, nothing change
 * */
gboolean gsb_currency_set_combobox_history ( GtkWidget *combo_box,
                        gint currency_number )
{
    GtkTreeIter iter;
    gint result;

    if (!combobox_currency_store)
	gsb_currency_create_combobox_store ();

    result = gtk_tree_model_get_iter_first ( GTK_TREE_MODEL (combobox_currency_store),
					     &iter );
    while (result)
    {
	gint value;

	gtk_tree_model_get ( GTK_TREE_MODEL (combobox_currency_store),
			     &iter,
			     CURRENCY_COL_NUMBER, &value,
			     -1 );

	if (value == currency_number)
	{
	    gtk_combo_box_set_active_iter ( GTK_COMBO_BOX (combo_box),
					    &iter );
	    return TRUE;
	}
	result = gtk_tree_model_iter_next ( GTK_TREE_MODEL (combobox_currency_store),
					    &iter );
    }
    return FALSE;
}


/**
 * Get and return the number of the currency in the option_menu given
 * in param
 *
 * \param currency_option_menu an option menu with the currencies
 *
 * \return the number of currency
 * */
gint gsb_currency_get_currency_from_combobox ( GtkWidget *combo_box )
{
    gint currency_number = 0;
    GtkTreeIter iter;

    if (!combobox_currency_store)
	gsb_currency_create_combobox_store ();

    if (gtk_combo_box_get_active_iter ( GTK_COMBO_BOX (combo_box),
					&iter ))
	gtk_tree_model_get ( GTK_TREE_MODEL (combobox_currency_store),
			     &iter,
			     CURRENCY_COL_NUMBER, &currency_number,
			     -1 );
    return currency_number;
}



/**
 * update the list of the currencies for combobox, wich change all
 * the current combobox content
 *
 * \param
 *
 * \return FALSE
 */
gboolean gsb_currency_update_combobox_currency_list ( void )
{
    GSList *list_tmp;
    gint handler_id;
    gint old_currency_number = -1;
	gchar* tmpstr;

    devel_debug (NULL);
    if (!combobox_currency_store
	||
	!gsb_data_currency_get_currency_list ())
	return FALSE;

    /* XXX still buggy, very slow on the gtk_list_store_clear() call,
     * try to find why. */
    if ( detail_devise_compte && G_IS_OBJECT ( detail_devise_compte ) )
    {
        handler_id = (gulong) g_object_get_data ( G_OBJECT
                            (detail_devise_compte), "changed-hook" );
        if ( handler_id > 0 )
        {
            g_signal_handler_block ( (gpointer *) detail_devise_compte,
                            handler_id );
            old_currency_number = gtk_combo_box_get_active (GTK_COMBO_BOX
                            (detail_devise_compte));
        }
    }
    if ( combo_devise_totaux_tiers && G_IS_OBJECT ( combo_devise_totaux_tiers ) )
    {
        handler_id = (gulong) g_object_get_data ( G_OBJECT
                            (combo_devise_totaux_tiers), "changed-hook" );
        if ( handler_id > 0 )
            g_signal_handler_block ( (gpointer *) combo_devise_totaux_tiers,
                            (gulong) g_object_get_data ( G_OBJECT
                            (combo_devise_totaux_tiers), "changed-hook" ) );
    }
    if ( combo_devise_totaux_categ  && G_IS_OBJECT ( combo_devise_totaux_categ ) )
    {
        handler_id = (gulong) g_object_get_data ( G_OBJECT
                            (combo_devise_totaux_categ), "changed-hook" );
        if ( handler_id > 0 )
            g_signal_handler_block ( (gpointer *) combo_devise_totaux_categ,
                            (gulong) g_object_get_data ( G_OBJECT
                            (combo_devise_totaux_categ), "changed-hook" ) );
    }
    if ( combo_devise_totaux_ib  && G_IS_OBJECT ( combo_devise_totaux_ib ) )
    {
        handler_id = (gulong) g_object_get_data ( G_OBJECT
                            (combo_devise_totaux_ib), "changed-hook" );
        if ( handler_id > 0 )
            g_signal_handler_block ( (gpointer *) combo_devise_totaux_ib,
                            (gulong) g_object_get_data ( G_OBJECT
                            (combo_devise_totaux_ib), "changed-hook" ) );
    }

    gtk_list_store_clear (GTK_LIST_STORE (combobox_currency_store));
    list_tmp = gsb_data_currency_get_currency_list ();

    while ( list_tmp )
    {
        GtkTreeIter iter;
        GdkPixbuf * pixbuf;
        gchar * string;
        gint currency_number;

        currency_number = gsb_data_currency_get_no_currency (list_tmp -> data);
        string = g_strconcat( gsb_dirs_get_pixmaps_dir ( ), G_DIR_SEPARATOR_S,
                    "flags", G_DIR_SEPARATOR_S,
                    gsb_data_currency_get_code_iso4217 (currency_number),
                    ".png", NULL );
        pixbuf = gdk_pixbuf_new_from_file ( string, NULL );
        g_free (string);


        gtk_list_store_append ( GTK_LIST_STORE (combobox_currency_store), &iter );
        tmpstr = g_strconcat ( gsb_data_currency_get_name (currency_number),
                    " (",
                    gsb_data_currency_get_code_or_isocode (currency_number),
                    ")",
                    NULL );
        gtk_list_store_set ( combobox_currency_store, &iter,
                    CURRENCY_COL_FLAG, pixbuf,
                    CURRENCY_COL_CODE, gsb_data_currency_get_code_or_isocode (currency_number),
                    CURRENCY_COL_NAME, tmpstr,
                    CURRENCY_COL_NUMBER, currency_number,
                    -1 );
        g_free ( tmpstr );
        list_tmp = list_tmp -> next;
    }

    run.mise_a_jour_liste_comptes_accueil = TRUE;
    run.mise_a_jour_liste_echeances_manuelles_accueil = TRUE;
    run.mise_a_jour_liste_echeances_auto_accueil = TRUE;

    if ( detail_devise_compte && G_IS_OBJECT ( detail_devise_compte ) )
    {
        handler_id = (gulong) g_object_get_data ( G_OBJECT
                            (detail_devise_compte), "changed-hook" );
        {
            gtk_combo_box_set_active ( GTK_COMBO_BOX (detail_devise_compte),
                            old_currency_number );
            g_signal_handler_unblock ( detail_devise_compte,
                            (gulong) g_object_get_data ( G_OBJECT
                            (detail_devise_compte), "changed-hook" ) );
        }
    }
    if ( combo_devise_totaux_tiers && G_IS_OBJECT ( combo_devise_totaux_tiers ) )
    {
        handler_id = (gulong) g_object_get_data ( G_OBJECT
                            (combo_devise_totaux_tiers), "changed-hook" );
        if ( handler_id > 0 )
        {
            gtk_combo_box_set_active ( GTK_COMBO_BOX (combo_devise_totaux_tiers),
                            old_currency_number );
            g_signal_handler_unblock ( (gpointer *) combo_devise_totaux_tiers,
                            (gulong) g_object_get_data ( G_OBJECT
                            (combo_devise_totaux_tiers), "changed-hook" ) );
        }
    }
    if ( combo_devise_totaux_categ  && G_IS_OBJECT ( combo_devise_totaux_categ ) )
    {
        handler_id = (gulong) g_object_get_data ( G_OBJECT
                            (combo_devise_totaux_categ), "changed-hook" );
        if ( handler_id > 0 )
        {
            gtk_combo_box_set_active ( GTK_COMBO_BOX (combo_devise_totaux_categ),
                            old_currency_number );
            g_signal_handler_unblock ( (gpointer *) combo_devise_totaux_categ,
                            (gulong) g_object_get_data ( G_OBJECT
                            (combo_devise_totaux_categ), "changed-hook" ) );
        }
    }
    if ( combo_devise_totaux_ib  && G_IS_OBJECT ( combo_devise_totaux_ib ) )
    {
        handler_id = (gulong) g_object_get_data ( G_OBJECT
                            (combo_devise_totaux_ib), "changed-hook" );
        if ( handler_id > 0 )
        {
            gtk_combo_box_set_active ( GTK_COMBO_BOX (combo_devise_totaux_ib),
                            old_currency_number );
            g_signal_handler_unblock ( (gpointer *) combo_devise_totaux_ib,
                            (gulong) g_object_get_data ( G_OBJECT
                            (combo_devise_totaux_ib), "changed-hook" ) );
        }
    }

    return FALSE;
}


/**
 * Check if a transaction need an exchange rate and fees with its
 * account
 * if yes, ask for that and set the in the transaction.
 *
 * \param transaction_number
 */
void gsb_currency_check_for_change ( gint transaction_number )
{
    gint transaction_currency_number;
    gint account_currency_number;
    gint link_number;

    account_currency_number = gsb_data_account_get_currency (
                        gsb_data_transaction_get_account_number ( transaction_number ) );
    transaction_currency_number = gsb_data_transaction_get_currency_number (
                        transaction_number );
    link_number = gsb_data_currency_link_search ( account_currency_number,
                        transaction_currency_number );

    if ( link_number )
    {
        if ( current_exchange_fees.mantissa )
            gsb_data_transaction_set_exchange_fees ( transaction_number,
                        current_exchange_fees );
        else
            gsb_data_transaction_set_exchange_fees ( transaction_number,
                        null_real );

        if ( current_exchange.mantissa == 0 )
            gsb_data_transaction_set_exchange_rate ( transaction_number,
                    gsb_real_abs (
                    gsb_data_currency_link_get_change_rate (
                    link_number ) ) );
        else
            gsb_data_transaction_set_exchange_rate ( transaction_number,
                    current_exchange );

        if ( gsb_data_currency_link_get_first_currency (
         link_number) == account_currency_number )
            gsb_data_transaction_set_change_between (transaction_number, 1 );
        else
            gsb_data_transaction_set_change_between (transaction_number, 0 );

        return;
    }

    if ( current_exchange.mantissa == 0 )
        gsb_currency_exchange_dialog ( account_currency_number,
                        transaction_currency_number,
                        0,
                        null_real,
                        null_real,
                        TRUE );

    gsb_data_transaction_set_exchange_rate ( transaction_number,
                        gsb_real_abs (current_exchange));
    if ( current_exchange_fees.mantissa )
        gsb_data_transaction_set_exchange_fees ( transaction_number,
                        current_exchange_fees );
    else
        gsb_data_transaction_set_exchange_fees ( transaction_number,
                        null_real );

    gsb_data_transaction_set_change_between (transaction_number, 0 );
}


/**
 * ask to the user the exchange and the fees for a transaction
 * fill the variables current_exchange and current_exchange_fees with
 * the data of the user
 * if this was asked before, will use the buffer cached exchange rate, except
 * if force is set to TRUE
 *
 * \param account_currency_number
 * \param transaction_currency_number
 * \param link_currency si = TRUE : 1 nom_devise = "change" devise_en_rapport
 * \param exchange_rate
 * \param exchange_fees
 * \param force if TRUE will not get the cached exchange rate and will really ask to the user
 *
 * \return
 * */
void gsb_currency_exchange_dialog ( gint account_currency_number,
                        gint transaction_currency_number ,
                        gboolean link_currency,
                        gsb_real exchange_rate,
                        gsb_real exchange_fees,
                        gboolean force )
{
    GtkWidget *dialog, *label, *hbox, *paddingbox, *table, *widget;
    GtkWidget *entry, *amount_entry, *amount_1_entry, *amount_2_entry, *fees_entry;
    GtkWidget *combobox_1;
    GtkWidget *combobox_2;
    struct cached_exchange_rate *cache;
    gchar* tmpstr;
    gint row = 0;
    gint result;
    gint link_number;
    gint change_link_currency = 1;

    if ( account_currency_number == 0 || transaction_currency_number == 0 )
        return;

    if ( !force
     &&
     ( cache = gsb_currency_config_get_cached_exchange (
     account_currency_number, transaction_currency_number ) ) )
    {
        current_exchange = cache -> rate;
        current_exchange_fees = cache -> fees;
        return;
    }

    dialog = gtk_dialog_new_with_buttons ( _("Enter exchange rate"),
                        GTK_WINDOW ( run.window ),
                        GTK_DIALOG_MODAL,
                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                        GTK_STOCK_OK, GTK_RESPONSE_OK,
                        NULL );

    gtk_window_set_position ( GTK_WINDOW ( dialog ), GTK_WIN_POS_CENTER_ON_PARENT );
    gtk_window_set_resizable ( GTK_WINDOW ( dialog ), FALSE );

    /* text for paddingbox */
    tmpstr = g_strdup_printf ( _("Please enter data for the transaction") );

    /* Ugly dance to avoid side effects on dialog's vbox. */
    hbox = gtk_hbox_new ( FALSE, 0 );
    gtk_box_pack_start ( GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, FALSE, FALSE, 0 );
    paddingbox = new_paddingbox_with_title ( hbox, TRUE, tmpstr );
    gtk_container_set_border_width ( GTK_CONTAINER(hbox), 6 );
    gtk_container_set_border_width ( GTK_CONTAINER(paddingbox), 6 );
    g_free ( tmpstr );

    /* table for layout */
    table = gtk_table_new ( 2, 5, FALSE );
    gtk_box_pack_start ( GTK_BOX ( paddingbox ), table, FALSE, FALSE, 6 );
    gtk_table_set_col_spacings ( GTK_TABLE(table), 6 );
    gtk_table_set_row_spacings ( GTK_TABLE(table), 6 );

    /* echange line label */
    label = gtk_label_new ( _("Currencies") );
    gtk_misc_set_alignment ( GTK_MISC ( label ), 0.0, 0.0 );
    gtk_table_attach ( GTK_TABLE(table), label, 0, 1, row, row+1,
                        GTK_SHRINK | GTK_FILL, 0, 0, 0 );

    /* echange line currency 1 */
    combobox_1 = gsb_currency_make_combobox_exchange_dialog (
                        transaction_currency_number,
                        account_currency_number,
                        link_currency );
    gtk_table_attach ( GTK_TABLE(table), combobox_1, 1, 2, row, row+1,
                        GTK_SHRINK | GTK_FILL, 0, 0, 0 );

    /* echange line label */
    label = gtk_label_new ( _("Exchange rate") );
    gtk_misc_set_alignment ( GTK_MISC ( label ), 0.5, 0.0 );
    gtk_table_attach ( GTK_TABLE(table), label, 2, 3, row, row+1,
                        GTK_SHRINK | GTK_FILL, 0, 0, 0 );

    /* echange line currency 2 */
    combobox_2 = gsb_currency_make_combobox_exchange_dialog (
                        transaction_currency_number,
                        account_currency_number,
                        !link_currency );
    gtk_table_attach ( GTK_TABLE(table), combobox_2, 3, 4, row, row+1,
                        GTK_SHRINK | GTK_FILL, 0, 0, 0 );
    row++;

    link_number = gsb_data_currency_link_search ( account_currency_number,
                        transaction_currency_number );
    if ( link_number )
    {
        gtk_widget_set_sensitive ( combobox_1, FALSE );
        gtk_widget_set_sensitive ( combobox_2, FALSE );
    }

    /* amount line */
    label = gtk_label_new ( _("Amounts: ") );
    gtk_misc_set_alignment ( GTK_MISC ( label ), 0.0, 0.0 );
    gtk_table_attach ( GTK_TABLE(table), label, 0, 1, row, row+1,
                        GTK_SHRINK | GTK_FILL, 0, 0, 0 );

    amount_1_entry = gtk_entry_new ();
    gtk_entry_set_activates_default ( GTK_ENTRY ( amount_1_entry ), TRUE );
    gtk_table_attach ( GTK_TABLE(table), amount_1_entry, 1, 2, row, row+1,
                        GTK_SHRINK | GTK_FILL, 0, 0, 0 );

    /* echange line input field */
    entry = gtk_entry_new ();
    gtk_widget_set_size_request ( entry, 100, -1 );
    gtk_entry_set_activates_default ( GTK_ENTRY ( entry ), TRUE );
    gtk_table_attach ( GTK_TABLE(table), entry, 2, 3, row, row+1,
                        GTK_SHRINK | GTK_FILL, 0, 0, 0 );

    amount_2_entry = gtk_entry_new ();
    gtk_entry_set_activates_default ( GTK_ENTRY ( amount_2_entry ), TRUE );
    gtk_table_attach ( GTK_TABLE(table), amount_2_entry, 3, 4, row, row+1,
                        GTK_SHRINK | GTK_FILL, 0, 0, 0 );

    /* if amount exist already, fill them here */
    if ( link_currency )
        amount_entry = amount_2_entry;
    else
        amount_entry = amount_1_entry;

    widget = gsb_form_widget_get_widget ( TRANSACTION_FORM_DEBIT );
    if ( !gsb_form_widget_check_empty ( widget ) )
        gtk_entry_set_text ( GTK_ENTRY ( amount_entry ),
                        gtk_entry_get_text ( GTK_ENTRY ( widget ) ) );
    else
    {
        widget = gsb_form_widget_get_widget ( TRANSACTION_FORM_CREDIT );
        if ( !gsb_form_widget_check_empty ( widget ) )
            gtk_entry_set_text ( GTK_ENTRY ( amount_entry ),
                        gtk_entry_get_text ( GTK_ENTRY ( widget ) ) );
    }

    /* set the connections */
    g_signal_connect ( G_OBJECT (combobox_1),
                        "changed",
                        G_CALLBACK ( gsb_currency_select_change_currency ),
                        combobox_2 );
    g_signal_connect ( G_OBJECT (combobox_2),
                        "changed",
                        G_CALLBACK ( gsb_currency_select_change_currency ),
                        combobox_1);
    g_object_set_data ( G_OBJECT ( combobox_1 ),
                        "amount_1_entry", amount_1_entry );
    g_object_set_data ( G_OBJECT ( combobox_1 ),
                        "amount_2_entry", amount_2_entry );

    g_object_set_data ( G_OBJECT ( combobox_2 ),
                        "amount_1_entry", amount_1_entry );
    g_object_set_data ( G_OBJECT ( combobox_2 ),
                        "amount_2_entry", amount_2_entry );

    g_signal_connect ( G_OBJECT ( amount_1_entry ),
                        "changed",
                        G_CALLBACK ( gsb_currency_select_double_amount ),
                        amount_2_entry );
    g_signal_connect_swapped ( G_OBJECT ( amount_2_entry ),
                        "changed",
                        G_CALLBACK ( gsb_currency_select_double_amount ),
                        amount_1_entry );
    g_object_set_data ( G_OBJECT ( amount_1_entry ), "exchange_rate", entry );
    g_object_set_data ( G_OBJECT ( amount_1_entry ), "link_currency",
                        GINT_TO_POINTER ( link_currency ) );
    row++;

    /* exchange fees line label */
    label = gtk_label_new ( _("Exchange fees: ") );
    gtk_misc_set_alignment ( GTK_MISC ( label ), 0.0, 0.0 );
    gtk_table_attach ( GTK_TABLE(table), label, 0, 1, row, row+1,
                        GTK_SHRINK | GTK_FILL, 0, 0, 0 );

    /* exchange fees line input field */
    fees_entry = gtk_entry_new ();
    gtk_entry_set_activates_default ( GTK_ENTRY ( fees_entry ), TRUE );
    gtk_table_attach ( GTK_TABLE(table), fees_entry, 1, 2, row, row+1,
                        GTK_SHRINK | GTK_FILL, 0, 0, 0 );

    /* exchange fees currency for fees */
    label = gtk_label_new (gsb_data_currency_get_name ( account_currency_number ) );
    gtk_misc_set_alignment ( GTK_MISC ( label ), 0.0, 0.0 );
    gtk_table_attach ( GTK_TABLE(table), label, 2, 3, row, row+1,
                        GTK_SHRINK | GTK_FILL, 0, 0, 0 );

    if ( link_number )
    {
        GtkWidget *checkbox;

        change_link_currency = !gsb_data_currency_link_get_fixed_link ( link_number );
        checkbox = gtk_check_button_new_with_label ( _("Change the link") );
        if ( change_link_currency )
            gtk_toggle_button_set_active ( GTK_TOGGLE_BUTTON ( checkbox ), TRUE );
        gtk_widget_set_sensitive ( checkbox, TRUE );
        g_signal_connect ( G_OBJECT ( checkbox ),
                        "toggled",
                        G_CALLBACK ( gsb_currency_checkbutton_link_changed ),
                        &change_link_currency );

        gtk_table_attach ( GTK_TABLE(table), checkbox, 3, 4, row, row+1,
                        GTK_SHRINK | GTK_FILL, 0, 0, 0 );
    }

    /* if the rate or fees exist already, fill them here */
    if ( exchange_rate.mantissa )
    {
        tmpstr = utils_real_get_string ( exchange_rate );
        gtk_entry_set_text ( GTK_ENTRY ( entry ), tmpstr );
        g_free ( tmpstr );
    }

    if ( exchange_fees.mantissa )
    {
        tmpstr = utils_real_get_string (gsb_real_abs (exchange_fees));
        gtk_entry_set_text ( GTK_ENTRY ( fees_entry ), tmpstr );
        g_free ( tmpstr );
    }

    gtk_widget_show_all ( dialog );

    /* show the dialog */
dialog_return:
    result = gtk_dialog_run ( GTK_DIALOG ( dialog ));

    if ( result == GTK_RESPONSE_OK )
    {
        gint new_link_number;
        gint new_link_currency;

        current_exchange = utils_real_get_from_string (
                        gtk_entry_get_text ( GTK_ENTRY ( entry ) ) );

        if ( strlen ( gtk_entry_get_text ( GTK_ENTRY ( fees_entry ) ) ) > 0
         ||
         strcmp ( gtk_entry_get_text ( GTK_ENTRY ( fees_entry ) ), "0" ) != 0 )
        {
            current_exchange_fees = utils_real_get_from_string (
                        gtk_entry_get_text ( GTK_ENTRY ( fees_entry ) ) );
        }
        else
            current_exchange_fees = null_real ;

        if ( current_exchange.mantissa == 0 )
        {
            tmpstr = g_strdup_printf ( _("The exchange rate or the transaction amount in "
                        "%s must be filled."),
                        gsb_data_currency_get_name ( account_currency_number ) );
            dialogue_warning_hint ( tmpstr, _("One field is not filled in") );

            goto dialog_return;
        }

        gsb_currency_config_set_cached_exchange ( account_currency_number,
                        transaction_currency_number,
                        current_exchange, current_exchange_fees );

        if ( link_number )
        {
            if ( change_link_currency
             &&
             gsb_real_cmp ( current_exchange,
             gsb_data_currency_link_get_change_rate ( link_number ) ) != 0 )
                gsb_data_currency_link_set_change_rate ( link_number,
                        current_exchange );
        }
        else
        {
            new_link_number = gsb_data_currency_link_new ( 0 );
            new_link_currency = gtk_combo_box_get_active (
                        GTK_COMBO_BOX ( combobox_1 ) );
            if ( new_link_currency )
            {
                gsb_data_currency_link_set_first_currency ( new_link_number,
                        account_currency_number );
                gsb_data_currency_link_set_second_currency ( new_link_number,
                        transaction_currency_number );
            }
            else
            {
                gsb_data_currency_link_set_first_currency ( new_link_number,
                        transaction_currency_number );
                gsb_data_currency_link_set_second_currency ( new_link_number,
                        account_currency_number );
            }
            gsb_data_currency_link_set_change_rate ( new_link_number,
                        current_exchange );
        }
    }
    else
        gsb_currency_init_exchanges ( );

    gtk_widget_destroy ( GTK_WIDGET ( dialog ));
}


/**
 * Find whether echange rate between two currencies is known.  If so,
 * returns a cached_exchange_rate structure with exchange rate
 * information.
 *
 * \param currency1 First currency
 * \param currency2 Second currency
 *
 * \return NULL on failure, a pointer to a cached_exchange_rate
 * structure on success.
 */
struct cached_exchange_rate *gsb_currency_config_get_cached_exchange ( gint currency1_number,
                        gint currency2_number )
{
    GSList * tmp_list = cached_exchange_rates;

    while ( tmp_list )
    {
	struct cached_exchange_rate * tmp;

	tmp = tmp_list -> data;
	if ( currency1_number == tmp -> currency1_number && currency2_number == tmp -> currency2_number )
	    return tmp;

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


/**
 * Update exchange rate cache according to arguments.
 *
 * \param currency1 First currency.
 * \param currency2 Second currency.
 * \param change    Exchange rate between two currencies.
 * \param fees      Fees of transaction.
 */
void gsb_currency_config_set_cached_exchange ( gint currency1_number,
                        gint currency2_number,
                        gsb_real change, gsb_real fees )
{
    struct cached_exchange_rate * tmp;

    tmp = (struct cached_exchange_rate *) g_malloc0(sizeof(struct cached_exchange_rate));

    tmp -> currency1_number = currency1_number;
    tmp -> currency2_number = currency2_number;
    tmp -> rate = change;
    tmp -> fees = fees;

    cached_exchange_rates = g_slist_append ( cached_exchange_rates, tmp );
}




/**
 * create and fill the list store of the currency
 * come here mean that combobox_currency_store is NULL
 *
 * \param
 *
 * \return TRUE ok, FALSE problem
 * */
gboolean gsb_currency_create_combobox_store ( void )
{
    /* the currency list store, contains 4 columns :
     * CURRENCY_COL_CODE : the code of the currency
     * CURRENCY_COL_NAME : the name(code) of the currency
     * CURRENCY_COL_NUMBER : the number of the currency
     * CURRENCY_COL_FLAG :  */

    combobox_currency_store = gtk_list_store_new ( CURRENCY_NUM_COL,
						   G_TYPE_STRING,
						   G_TYPE_STRING,
						   G_TYPE_INT,
						   GDK_TYPE_PIXBUF );
    gsb_currency_update_combobox_currency_list ();
    return TRUE;
}


/*
 * Handler that change the second combobox of a window that ask for
 * change.
 *
 * \param combobox_1 the combobox wich receive the signal
 * \param combobox_2 the combobox we want to change
 *
 * \return FALSE
 */
gboolean gsb_currency_select_change_currency ( GtkWidget *combobox_1,
                        GtkWidget *combobox_2 )
{
    GtkWidget *entry_1;
    GtkWidget *entry_2;
    gchar *string = NULL;

    /* we just need to set the same active menu on the second combobox */
    g_signal_handlers_block_by_func ( G_OBJECT ( combobox_1 ),
                        G_CALLBACK (gsb_currency_select_change_currency),
                        combobox_2 );
    g_signal_handlers_block_by_func ( G_OBJECT ( combobox_2 ),
                        G_CALLBACK (gsb_currency_select_change_currency),
                        combobox_1 );

    gtk_combo_box_set_active ( GTK_COMBO_BOX (combobox_2),
			       !gtk_combo_box_get_active ( GTK_COMBO_BOX (combobox_1)));

    entry_1 = g_object_get_data ( G_OBJECT ( combobox_1 ), "amount_1_entry" );
    entry_2 = g_object_get_data ( G_OBJECT ( combobox_1 ), "amount_2_entry" );

    g_signal_handlers_block_by_func ( G_OBJECT (entry_1),
                    G_CALLBACK (gsb_currency_select_double_amount),
                    entry_2 );
    g_signal_handlers_block_by_func ( G_OBJECT (entry_2),
                    G_CALLBACK (gsb_currency_select_double_amount),
                    entry_1 );

    string = g_strdup ( gtk_entry_get_text ( GTK_ENTRY ( entry_1 ) ) );
    if ( string && strlen ( string ) > 0 )
    {
        gtk_entry_set_text ( GTK_ENTRY ( entry_1 ), "" );
        gtk_entry_set_text ( GTK_ENTRY ( entry_2 ), string );
        g_free ( string );
    }
    else
    {
        string = g_strdup ( gtk_entry_get_text ( GTK_ENTRY ( entry_2 ) ) );
        if ( string && strlen ( string ) > 0 )
        {
            gtk_entry_set_text ( GTK_ENTRY ( entry_2 ), "" );
            gtk_entry_set_text ( GTK_ENTRY ( entry_1 ), string );
            g_free ( string );
        }
    }

    g_signal_handlers_unblock_by_func ( G_OBJECT (entry_1),
                    G_CALLBACK (gsb_currency_select_double_amount),
                    entry_2 );
    g_signal_handlers_unblock_by_func ( G_OBJECT (entry_2),
                    G_CALLBACK (gsb_currency_select_double_amount),
                    entry_1 );

    g_signal_handlers_unblock_by_func ( G_OBJECT ( combobox_1 ),
                        G_CALLBACK (gsb_currency_select_change_currency),
                        combobox_2 );
    g_signal_handlers_unblock_by_func ( G_OBJECT ( combobox_2 ),
                        G_CALLBACK (gsb_currency_select_change_currency),
                        combobox_1 );

    return FALSE;
}


/*
 * initialize current_exchange and current_exchange_fees
 *
 */
void gsb_currency_init_exchanges ( void )
{
    current_exchange = null_real;
    current_exchange_fees = null_real;
}


/*
 * Handler that change the entries and calculate the exchange_rate
 *
 *
 * \param entry_1 the entry wich receive the signal
 * \param entry_2 the other entry
 *
 * \return FALSE
 */
gboolean gsb_currency_select_double_amount ( GtkWidget *entry_1,
                        GtkWidget *entry_2 )
{
    GtkWidget *entry, *entry_3, *entry_4;
    gsb_real amount_1, amount_2, taux;
    gboolean link_currency;
    gboolean valide;

    entry = g_object_get_data ( G_OBJECT ( entry_1 ), "exchange_rate" );
    link_currency = GPOINTER_TO_INT ( g_object_get_data (
                         G_OBJECT ( entry_1 ), "link_currency" ) );

    if ( link_currency )
    {
        entry_3 = entry_2;
        entry_4 = entry_1;
    }
    else
    {
        entry_3 = entry_1;
        entry_4 = entry_2;
    }

    valide = gsb_form_widget_get_valide_amout_entry (
                gtk_entry_get_text ( GTK_ENTRY ( entry_1 ) ) );
    if ( valide )
    {
        /* the entry is valid, make it normal */
        gtk_widget_modify_base ( entry_1, GTK_STATE_NORMAL, NULL );
    }
    else
    {
        /* the entry is not valid, make it red */
        gtk_widget_modify_base ( entry_1, GTK_STATE_NORMAL,
                        gsb_color_get_couleur ( "entry_error_color" ) );
        return FALSE;
    }
    valide = gsb_form_widget_get_valide_amout_entry (
                gtk_entry_get_text ( GTK_ENTRY ( entry_2 ) ) );
    if ( valide )
    {
        /* the entry is valid, make it normal */
        gtk_widget_modify_base ( entry_2, GTK_STATE_NORMAL, NULL );
    }
    else
    {
        /* the entry is not valid, make it red */
        gtk_widget_modify_base ( entry_2, GTK_STATE_NORMAL,
                        gsb_color_get_couleur ( "entry_error_color" ) );
        return FALSE;
    }

    if ( strlen ( gtk_entry_get_text ( GTK_ENTRY ( entry_3 ) ) ) > 0 )
    {
        if ( !strlen ( gtk_entry_get_text ( GTK_ENTRY ( entry_4 ) ) ) )
		{
            gtk_entry_set_text ( GTK_ENTRY ( entry ), "");
			gtk_widget_set_sensitive ( GTK_WIDGET ( entry ), TRUE );
		}
		else
        {
            gtk_widget_set_sensitive ( GTK_WIDGET ( entry ), FALSE );
            amount_1 = utils_real_get_from_string ( gtk_entry_get_text ( GTK_ENTRY ( entry_1 ) ) );
            amount_2 = utils_real_get_from_string ( gtk_entry_get_text ( GTK_ENTRY ( entry_2 ) ) );
            taux = gsb_real_div ( amount_2, amount_1 );
            gtk_entry_set_text ( GTK_ENTRY ( entry ), utils_real_get_string ( taux ) );
        }
    }
    return FALSE;
}


/*
 * create the exchange rate dialog
 *
 */
GtkWidget *gsb_currency_make_combobox_exchange_dialog ( gint transaction_currency_number,
                        gint account_currency_number,
                        gint set_index )
{
    GtkWidget *combo_box = NULL;
    GtkListStore *combobox_store;
    GtkCellRenderer *text_renderer, *flag_renderer;
    GtkTreeIter iter;
    GdkPixbuf *pixbuf;
    gchar *string;

    combobox_store = gtk_list_store_new ( 3, G_TYPE_INT, GDK_TYPE_PIXBUF,
						G_TYPE_STRING);

    string = g_strconcat( gsb_dirs_get_pixmaps_dir ( ), G_DIR_SEPARATOR_S,
                        "flags", G_DIR_SEPARATOR_S,
                        gsb_data_currency_get_code_iso4217 (
                        transaction_currency_number ),
                        ".png", NULL );
    pixbuf = gdk_pixbuf_new_from_file ( string, NULL );
    g_free (string);

    gtk_list_store_append ( GTK_LIST_STORE ( combobox_store ), &iter );
    gtk_list_store_set ( combobox_store, &iter,
                    0, transaction_currency_number,
                    1, pixbuf,
                    2, gsb_data_currency_get_name ( transaction_currency_number ),
                    -1 );

    string = g_strconcat( gsb_dirs_get_pixmaps_dir ( ), G_DIR_SEPARATOR_S,
                        "flags", G_DIR_SEPARATOR_S,
                        gsb_data_currency_get_code_iso4217 (
                        account_currency_number ),
                        ".png", NULL );
    pixbuf = gdk_pixbuf_new_from_file ( string, NULL );
    g_free (string);

    gtk_list_store_append ( GTK_LIST_STORE ( combobox_store ), &iter );
    gtk_list_store_set ( combobox_store, &iter,
                    0, account_currency_number,
                    1, pixbuf,
                    2, gsb_data_currency_get_name ( account_currency_number ),
                    -1 );

    combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL
                        (combobox_store));

    /* Flag renderer */
    flag_renderer = gtk_cell_renderer_pixbuf_new ();
    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), flag_renderer, FALSE);
    gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), flag_renderer,
				    "pixbuf", 1, NULL );

    GTK_CELL_RENDERER(flag_renderer) -> xpad = 3; /* Ugly but how to set it otherwise ?*/

    text_renderer = gtk_cell_renderer_text_new ();
    gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), text_renderer, FALSE);

	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box), text_renderer,
					"text", 2, NULL);

    gtk_combo_box_set_active ( GTK_COMBO_BOX (combo_box), set_index );

    return (combo_box);
}


/**
 *
 *
**/
gsb_real gsb_currency_get_current_exchange ( void )
{
    return current_exchange;
}


/**
 *
 *
**/
gboolean gsb_currency_set_current_exchange ( gsb_real exchange )
{
    current_exchange.mantissa = exchange.mantissa;
    current_exchange.exponent = exchange.exponent;

    return FALSE;
}


/**
 *
 *
**/
gsb_real gsb_currency_get_current_exchange_fees ( void )
{
    return current_exchange_fees;
}


/**
 *
 *
**/
gboolean gsb_currency_set_current_exchange_fees ( gsb_real fees )
{
    current_exchange_fees.mantissa = fees.mantissa;
    current_exchange_fees.exponent = fees.exponent;

    return FALSE;
}


/**
 * met à jour value à chaque changement du check_button
 *
**/
static gboolean gsb_currency_checkbutton_link_changed ( GtkWidget *checkbutton,
						  gboolean *value )
{
    if ( value )
        *value = gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON ( checkbutton ) );

    return FALSE;
}


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