-
Notifications
You must be signed in to change notification settings - Fork 760
/
gnc-ui-util.c
2442 lines (2089 loc) · 75.3 KB
/
gnc-ui-util.c
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
/********************************************************************\
* gnc-ui-util.c -- utility functions for the GnuCash UI *
* Copyright (C) 2000 Dave Peticolas <dave@krondo.com> *
* *
* 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, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/
#include "config.h"
#include "gnc-ui-util.h"
#include <glib.h>
#include <glib/gi18n.h>
#include <gio/gio.h>
#include <libguile.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <locale.h>
#include <math.h>
#if defined(G_OS_WIN32) && !defined(_MSC_VER)
# include <pow.h>
#endif
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "qof.h"
#include "guile-mappings.h"
#include "gnc-prefs.h"
#include "gnc-module/gnc-module.h"
#include "engine/Account.h"
#include "engine/Transaction.h"
#include "engine/gnc-engine.h"
#include "app-utils/gnc-euro.h"
#include "engine/gnc-hooks.h"
#include "engine/gnc-session.h"
#include "engine-helpers.h"
#include "gnc-locale-utils.h"
#include "gnc-component-manager.h"
#include "gnc-features.h"
#include "gnc-guile-utils.h"
#define GNC_PREF_CURRENCY_CHOICE_LOCALE "currency-choice-locale"
#define GNC_PREF_CURRENCY_CHOICE_OTHER "currency-choice-other"
#define GNC_PREF_CURRENCY_OTHER "currency-other"
#define GNC_PREF_REVERSED_ACCTS_NONE "reversed-accounts-none"
#define GNC_PREF_REVERSED_ACCTS_CREDIT "reversed-accounts-credit"
#define GNC_PREF_REVERSED_ACCTS_INC_EXP "reversed-accounts-incomeexpense"
static QofLogModule log_module = GNC_MOD_GUI;
static gboolean auto_decimal_enabled = FALSE;
static int auto_decimal_places = 2; /* default, can be changed */
static gboolean reverse_balance_inited = FALSE;
static gboolean reverse_type[NUM_ACCOUNT_TYPES];
/* Cache currency ISO codes and only look them up in the settings when
* absolutely necessary. Can't cache a pointer to the data structure
* as that will change any time the book changes. */
static gchar *user_default_currency = NULL;
static gchar *user_report_currency = NULL;
gchar *gnc_normalize_account_separator (const gchar* separator)
{
gchar *new_sep=NULL;
if (!separator || !*separator || g_strcmp0(separator, "colon") == 0)
new_sep = g_strdup (":");
else if (g_strcmp0(separator, "slash") == 0)
new_sep = g_strdup ("/");
else if (g_strcmp0(separator, "backslash") == 0)
new_sep = g_strdup ("\\");
else if (g_strcmp0(separator, "dash") == 0)
new_sep = g_strdup ("-");
else if (g_strcmp0(separator, "period") == 0)
new_sep = g_strdup (".");
else
new_sep = g_strdup (separator);
return new_sep;
}
/********************************************************************\
* gnc_configure_account_separator *
* updates the current account separator character *
* *
* Args: none *
\*******************************************************************/
static void
gnc_configure_account_separator (void)
{
gchar *separator;
char *string;
string = gnc_prefs_get_string(GNC_PREFS_GROUP_GENERAL, GNC_PREF_ACCOUNT_SEPARATOR);
separator = gnc_normalize_account_separator (string);
gnc_set_account_separator(separator);
g_free(string);
g_free(separator);
}
static void
gnc_configure_reverse_balance (void)
{
gint i;
for (i = 0; i < NUM_ACCOUNT_TYPES; i++)
reverse_type[i] = FALSE;
if (gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_REVERSED_ACCTS_INC_EXP))
{
reverse_type[ACCT_TYPE_INCOME] = TRUE;
reverse_type[ACCT_TYPE_EXPENSE] = TRUE;
}
else if (gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_REVERSED_ACCTS_CREDIT))
{
reverse_type[ACCT_TYPE_LIABILITY] = TRUE;
reverse_type[ACCT_TYPE_PAYABLE] = TRUE;
reverse_type[ACCT_TYPE_EQUITY] = TRUE;
reverse_type[ACCT_TYPE_INCOME] = TRUE;
reverse_type[ACCT_TYPE_CREDIT] = TRUE;
}
else if (!gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_REVERSED_ACCTS_NONE))
PWARN("no reversed account preference set, using none");
}
static void
gnc_reverse_balance_init (void)
{
gnc_configure_reverse_balance ();
reverse_balance_inited = TRUE;
}
gboolean
gnc_reverse_balance (const Account *account)
{
int type;
if (account == NULL)
return FALSE;
type = xaccAccountGetType (account);
if ((type < 0) || (type >= NUM_ACCOUNT_TYPES))
return FALSE;
if (!reverse_balance_inited)
gnc_reverse_balance_init ();
return reverse_type[type];
}
gchar *
gnc_get_default_directory (const gchar *section)
{
gchar *dir;
dir = gnc_prefs_get_string (section, GNC_PREF_LAST_PATH);
if (!dir)
#ifdef G_OS_WIN32
dir = g_strdup (g_get_user_data_dir ()); /* equivalent of "My Documents" */
#else
dir = g_strdup (g_get_home_dir ());
#endif
return dir;
}
void
gnc_set_default_directory (const gchar *section, const gchar *directory)
{
gnc_prefs_set_string(section, GNC_PREF_LAST_PATH, directory);
}
QofBook *
gnc_get_current_book (void)
{
return qof_session_get_book (gnc_get_current_session ());
}
/* If there is no current session, there is no book and we must be dealing
* with a new book. When gnucash is started with --nofile, there is
* initially no session (and no book), but by the time we check, one
* could have been created (for example, if an empty account tree tab is
* opened, a session is created which creates a new, but empty, book).
* A session is created and a book is loaded from a backend when gnucash is
* started with a file, but selecting 'new file' keeps a session open. So we
* need to check as well for a book with no accounts (root with no children). */
gboolean
gnc_is_new_book (void)
{
return ((!gnc_current_session_exist() ||
(gnc_current_session_exist() &&
!gnc_account_get_children(
gnc_book_get_root_account(
gnc_get_current_book()))))
? TRUE : FALSE);
}
#define OPTION_TAXUS_NAME "book/tax_US/name"
#define OPTION_TAXUS_TYPE "book/tax_US/type"
void
gnc_set_current_book_tax_name (const gchar *tax_name)
{
qof_book_set_string_option(gnc_get_current_book(), OPTION_TAXUS_NAME, tax_name);
}
const gchar *
gnc_get_current_book_tax_name (void)
{
return qof_book_get_string_option(gnc_get_current_book(), OPTION_TAXUS_NAME);
}
void
gnc_set_current_book_tax_type (const gchar *tax_type)
{
qof_book_set_string_option(gnc_get_current_book(), OPTION_TAXUS_TYPE, tax_type);
}
const gchar *
gnc_get_current_book_tax_type (void)
{
return qof_book_get_string_option(gnc_get_current_book(), OPTION_TAXUS_TYPE);
}
/** Calls gnc_book_option_num_field_source_change to initiate registered
* callbacks when num_field_source book option changes so that
* registers/reports can update themselves; sets feature flag */
void
gnc_book_option_num_field_source_change_cb (gboolean num_action)
{
gnc_suspend_gui_refresh ();
if (num_action)
{
/* Set a feature flag in the book for use of the split action field as number.
* This will prevent older GnuCash versions that don't support this feature
* from opening this file. */
gnc_features_set_used (gnc_get_current_book(),
GNC_FEATURE_NUM_FIELD_SOURCE);
}
gnc_book_option_num_field_source_change (num_action);
gnc_resume_gui_refresh ();
}
/** Calls gnc_book_option_book_currency_selected to initiate registered
* callbacks when currency accounting book option changes to book-currency so
* that registers/reports can update themselves; sets feature flag */
void
gnc_book_option_book_currency_selected_cb (gboolean use_book_currency)
{
gnc_suspend_gui_refresh ();
if (use_book_currency)
{
/* Set a feature flag in the book for use of book currency. This will
* prevent older GnuCash versions that don't support this feature from
* opening this file. */
gnc_features_set_used (gnc_get_current_book(),
GNC_FEATURE_BOOK_CURRENCY);
}
gnc_book_option_book_currency_selected (use_book_currency);
gnc_resume_gui_refresh ();
}
/** Returns TRUE if both book-currency and default gain/loss policy KVPs exist
* and are valid and trading accounts are not used. */
gboolean
gnc_book_use_book_currency (QofBook *book)
{
const gchar *policy;
const gchar *currency;
if (!book) return FALSE;
policy = qof_book_get_default_gains_policy (book);
currency = qof_book_get_book_currency_name (book);
/* If either a default gain/loss policy or a book-currency does not exist,
book-currency accounting method not valid */
if (!policy || !currency)
return FALSE;
/* If both exist, both must be valid */
if (!gnc_valid_policy_name (policy) || !gnc_commodity_table_lookup
(gnc_commodity_table_get_table
(gnc_get_current_book()),
GNC_COMMODITY_NS_CURRENCY,
currency))
return FALSE;
/* If both exist and are valid, there must be no trading accounts flag */
if (qof_book_use_trading_accounts (book))
return FALSE;
return TRUE;
}
/** Returns pointer to Book Currency name for book or NULL; determines
* that both book-currency and default gain/loss policy KVPs exist and that
* both are valid, a requirement for the 'book-currency' currency accounting
* method to apply. */
const gchar *
gnc_book_get_book_currency_name (QofBook *book)
{
if (!book) return NULL;
if (gnc_book_use_book_currency (book))
return qof_book_get_book_currency_name (book);
return NULL;
}
/** Returns pointer to Book Currency for book or NULL; determines
* that both book-currency and default gain/loss policy KVPs exist and that
* both are valid, a requirement for the 'book-currency' currency accounting
* method to apply. */
gnc_commodity *
gnc_book_get_book_currency (QofBook *book)
{
if (!book) return NULL;
if (gnc_book_use_book_currency (book))
return gnc_commodity_table_lookup
(gnc_commodity_table_get_table(book),
GNC_COMMODITY_NS_CURRENCY,
qof_book_get_book_currency_name (book));
return NULL;
}
/** Returns pointer to default gain/loss policy for book or NULL; determines
* that both book-currency and default gain/loss policy KVPs exist and that
* both are valid, a requirement for the 'book-currency' currency accounting
* method to apply. */
const gchar *
gnc_book_get_default_gains_policy (QofBook *book)
{
if (!book) return NULL;
if (gnc_book_use_book_currency (book))
return qof_book_get_default_gains_policy (book);
return NULL;
}
/** Returns pointer to default gain/loss account for book or NULL; determines
* that both book-currency and default gain/loss policy KVPs exist and that
* both are valid, a requirement for the 'book-currency' currency accounting
* method to apply. */
Account *
gnc_book_get_default_gain_loss_acct (QofBook *book)
{
Account *gains_account = NULL;
if (!book) return NULL;
if (gnc_book_use_book_currency (book))
gains_account = xaccAccountLookup
(qof_book_get_default_gain_loss_acct_guid (book), book);
return gains_account;
}
Account *
gnc_get_current_root_account (void)
{
return gnc_book_get_root_account (gnc_get_current_book ());
}
gnc_commodity_table *
gnc_get_current_commodities (void)
{
return gnc_commodity_table_get_table (gnc_get_current_book ());
}
gchar *
gnc_get_account_name_for_register(const Account *account)
{
gboolean show_leaf_accounts;
show_leaf_accounts = gnc_prefs_get_bool(GNC_PREFS_GROUP_GENERAL_REGISTER,
GNC_PREF_SHOW_LEAF_ACCT_NAMES);
if (show_leaf_accounts)
return g_strdup (xaccAccountGetName (account));
else
return gnc_account_get_full_name (account);
}
Account *
gnc_account_lookup_for_register(const Account *base_account, const char *name)
{
gboolean show_leaf_accounts;
show_leaf_accounts = gnc_prefs_get_bool(GNC_PREFS_GROUP_GENERAL_REGISTER,
GNC_PREF_SHOW_LEAF_ACCT_NAMES);
if (show_leaf_accounts)
return gnc_account_lookup_by_name (base_account, name);
else
return gnc_account_lookup_by_full_name (base_account, name);
}
/* Caller is responsible for g_free'ing returned memory */
char *
gnc_ui_account_get_tax_info_string (const Account *account)
{
static SCM get_form = SCM_UNDEFINED;
static SCM get_desc = SCM_UNDEFINED;
gboolean tax_related = FALSE;
const char *code;
if (!account)
return NULL;
tax_related = xaccAccountGetTaxRelated (account);
code = xaccAccountGetTaxUSCode (account);
if (!code)
{
if (!tax_related)
return NULL;
/* tax_related && !code */
else
/* Translators: This and the following strings appear on
* the account tab if the Tax Info column is displayed,
* i.e. if the user wants to record the tax form number
* and location on that tax form which corresponds to this
* gnucash account. For the US Income Tax support in
* gnucash, each tax code that can be assigned to an
* account generally corresponds to a specific line number
* on a paper form and each form has a unique
* identification (e.g., Form 1040, Schedule A). */
return g_strdup (_("Tax-related but has no tax code"));
}
else /* with tax code */
{
const gchar *tax_type;
GNCAccountType atype;
SCM tax_entity_type;
SCM category;
gchar *num_code = NULL;
const gchar *prefix = "N";
gchar *return_string = NULL;
tax_type = gnc_get_current_book_tax_type ();
if (tax_type == NULL || (g_strcmp0 (tax_type, "") == 0))
return g_strdup (_("Tax entity type not specified"));
atype = xaccAccountGetType (account);
tax_entity_type = scm_from_utf8_string (tax_type);
if (get_form == SCM_UNDEFINED)
{
GNCModule module;
const gchar *tax_module;
/* load the tax info */
#ifdef LOCALE_SPECIFIC_TAX
/* This is a very simple hack that loads the (new, special) German
tax definition file in a German locale, or (default) loads the
previous US tax file. */
# ifdef G_OS_WIN32
gchar *thislocale = g_win32_getlocale();
gboolean is_de_DE = (strncmp(thislocale, "de_DE", 5) == 0);
g_free(thislocale);
# else /* !G_OS_WIN32 */
const char *thislocale = setlocale(LC_ALL, NULL);
gboolean is_de_DE = (strncmp(thislocale, "de_DE", 5) == 0);
# endif /* G_OS_WIN32 */
#else /* LOCALE_SPECIFIC_TAX */
gboolean is_de_DE = FALSE;
#endif /* LOCALE_SPECIFIC_TAX */
tax_module = is_de_DE ?
"gnucash/tax/de_DE" :
"gnucash/tax/us";
module = gnc_module_load ((char *)tax_module, 0);
g_return_val_if_fail (module, NULL);
get_form = scm_c_eval_string
("(false-if-exception gnc:txf-get-form)");
get_desc = scm_c_eval_string
("(false-if-exception gnc:txf-get-description)");
}
g_return_val_if_fail (scm_is_procedure (get_form), NULL);
g_return_val_if_fail (scm_is_procedure (get_desc), NULL);
category = scm_c_eval_string (atype == ACCT_TYPE_INCOME ?
"txf-income-categories" :
(atype == ACCT_TYPE_EXPENSE ?
"txf-expense-categories" :
(((atype == ACCT_TYPE_BANK) ||
(atype == ACCT_TYPE_CASH) ||
(atype == ACCT_TYPE_ASSET) ||
(atype == ACCT_TYPE_STOCK) ||
(atype == ACCT_TYPE_MUTUAL) ||
(atype == ACCT_TYPE_RECEIVABLE)) ?
"txf-asset-categories" :
(((atype == ACCT_TYPE_CREDIT) ||
(atype == ACCT_TYPE_LIABILITY) ||
(atype == ACCT_TYPE_EQUITY) ||
(atype == ACCT_TYPE_PAYABLE)) ?
"txf-liab-eq-categories" : ""))));
if (g_str_has_prefix (code, prefix))
{
const gchar *num_code_tmp;
num_code_tmp = g_strdup (code);
num_code_tmp++; /* to lose the leading N */
num_code = g_strdup (num_code_tmp);
num_code_tmp--;
g_free ((gpointer *) num_code_tmp);
}
else
{
num_code = g_strdup (code);
}
if (category == SCM_UNDEFINED)
{
if (tax_related)
return_string = g_strdup_printf
(_("Tax type %s: invalid code %s for account type"),
tax_type, num_code);
else
return_string = g_strdup_printf
(_("Not tax-related; tax type %s: invalid code %s for account type"),
tax_type, num_code);
}
else
{
SCM code_scm;
SCM form_scm;
code_scm = scm_from_locale_symbol (code);
form_scm = scm_call_3 (get_form, category, code_scm, tax_entity_type);
if (!scm_is_string (form_scm))
{
if (tax_related)
return_string = g_strdup_printf
(_("Invalid code %s for tax type %s"),
num_code, tax_type);
else
return_string = g_strdup_printf
(_("Not tax-related; invalid code %s for tax type %s"),
num_code, tax_type);
}
else
{
gchar *form = NULL;
/* Note: using scm_to_utf8_string directly here instead
of our wrapper gnc_scm_to_utf8_string. 'form' should
be freed with 'free' instead of 'g_free'. This will
be taken care of automatically during scm_dynwind_end,
because we inform guile of this memory allocation via
scm_dynwind_free a little further. */
form = scm_to_utf8_string (form_scm);
if (!form)
{
if (tax_related)
return_string = g_strdup_printf
(_("No form: code %s, tax type %s"), num_code,
tax_type);
else
return_string = g_strdup_printf
(_("Not tax-related; no form: code %s, tax type %s"),
num_code, tax_type);
}
else
{
SCM desc_scm;
/* Create a dynwind context because we will be calling (scm) functions
that potentially exit non-locally */
scm_dynwind_begin (0);
scm_dynwind_free (form);
desc_scm = scm_call_3 (get_desc, category, code_scm,
tax_entity_type);
if (!scm_is_string (desc_scm))
{
if (tax_related)
return_string = g_strdup_printf
(_("No description: form %s, code %s, tax type %s"),
form, num_code, tax_type);
else
return_string = g_strdup_printf
(_("Not tax-related; no description: form %s, code %s, tax type %s"),
form, num_code, tax_type);
}
else
{
gchar *desc = NULL;
desc = gnc_scm_to_utf8_string (desc_scm);
if (!desc)
{
if (tax_related)
return_string = g_strdup_printf
(_("No description: form %s, code %s, tax type %s"),
form, num_code, tax_type);
else
return_string = g_strdup_printf
(_("Not tax-related; no description: form %s, code %s, tax type %s"),
form, num_code, tax_type);
}
else
{
gint64 copy_number;
gchar *copy_txt = NULL;
copy_number = xaccAccountGetTaxUSCopyNumber (account);
copy_txt = (copy_number == 1) ?
g_strdup ("") :
g_strdup_printf ("(%d)",
(gint) copy_number);
if (tax_related)
{
if (g_strcmp0 (form, "") == 0)
return_string = g_strdup_printf ("%s", desc);
else
return_string = g_strdup_printf ("%s%s: %s",
form, copy_txt, desc);
}
else
{
return_string = g_strdup_printf
(_("Not tax-related; %s%s: %s (code %s, tax type %s)"),
form, copy_txt, desc, num_code, tax_type);
}
g_free (copy_txt);
}
g_free (desc);
}
scm_dynwind_end ();
}
}
}
g_free (num_code);
return return_string;
}
}
/* Caller is responsible for g_free'ing returned memory */
char *
gnc_ui_account_get_tax_info_sub_acct_string (const Account *account)
{
GList *descendant, *account_descendants;
if (!account)
return NULL;
account_descendants = gnc_account_get_descendants (account);
if (account_descendants)
{
gint sub_acct_tax_number = 0;
for (descendant = account_descendants; descendant;
descendant = g_list_next(descendant))
{
if (xaccAccountGetTaxRelated (descendant->data))
sub_acct_tax_number++;
}
g_list_free (account_descendants);
g_list_free (descendant);
/* Translators: This and the following strings appear on
* the account tab if the Tax Info column is displayed,
* i.e. if the user wants to record the tax form number
* and location on that tax form which corresponds to this
* gnucash account. For the US Income Tax support in
* gnucash, each tax code that can be assigned to an
* account generally corresponds to a specific line number
* on a paper form and each form has a unique
* identification (e.g., Form 1040, Schedule A). */
return (sub_acct_tax_number == 0) ? NULL :
g_strdup_printf (_("(Tax-related subaccounts: %d)"),
sub_acct_tax_number);
}
else
return NULL;
}
static const char *
string_after_colon (const char *msgstr)
{
const char *string_at_colon;
g_assert(msgstr);
string_at_colon = strchr(msgstr, ':');
if (string_at_colon)
return string_at_colon + 1;
else
/* No colon found; we assume the translation contains only the
part after the colon, similar to the disambiguation prefixes */
return msgstr;
}
/********************************************************************\
* gnc_get_reconcile_str *
* return the i18n'd string for the given reconciled flag *
* *
* Args: reconciled_flag - the flag to convert into a string *
* Returns: the i18n'd reconciled string *
\********************************************************************/
const char *
gnc_get_reconcile_str (char reconciled_flag)
{
switch (reconciled_flag)
{
case NREC:
/* Translators: For the following strings, the single letters
after the colon are abbreviations of the word before the
colon. You should only translate the letter *after* the colon. */
return string_after_colon(_("not cleared:n"));
case CREC:
/* Translators: Please only translate the letter *after* the colon. */
return string_after_colon(_("cleared:c"));
case YREC:
/* Translators: Please only translate the letter *after* the colon. */
return string_after_colon(_("reconciled:y"));
case FREC:
/* Translators: Please only translate the letter *after* the colon. */
return string_after_colon(_("frozen:f"));
case VREC:
/* Translators: Please only translate the letter *after* the colon. */
return string_after_colon(_("void:v"));
default:
PERR("Bad reconciled flag\n");
return NULL;
}
}
/********************************************************************\
* gnc_get_reconcile_valid_flags *
* return a string containing the list of reconciled flags *
* *
* Returns: the i18n'd reconciled flags string *
\********************************************************************/
const char *
gnc_get_reconcile_valid_flags (void)
{
static const char flags[] = { NREC, CREC, YREC, FREC, VREC, 0 };
return flags;
}
/********************************************************************\
* gnc_get_reconcile_flag_order *
* return a string containing the reconciled-flag change order *
* *
* Args: reconciled_flag - the flag to convert into a string *
* Returns: the i18n'd reconciled string *
\********************************************************************/
const char *
gnc_get_reconcile_flag_order (void)
{
static const char flags[] = { NREC, CREC, 0 };
return flags;
}
static const char *
equity_base_name (GNCEquityType equity_type)
{
switch (equity_type)
{
case EQUITY_OPENING_BALANCE:
return N_("Opening Balances");
case EQUITY_RETAINED_EARNINGS:
return N_("Retained Earnings");
default:
return NULL;
}
}
Account *
gnc_find_or_create_equity_account (Account *root,
GNCEquityType equity_type,
gnc_commodity *currency)
{
Account *parent;
Account *account;
gboolean name_exists;
gboolean base_name_exists;
const char *base_name;
char *name;
g_return_val_if_fail (equity_type >= 0, NULL);
g_return_val_if_fail (equity_type < NUM_EQUITY_TYPES, NULL);
g_return_val_if_fail (currency != NULL, NULL);
g_return_val_if_fail (root != NULL, NULL);
base_name = equity_base_name (equity_type);
account = gnc_account_lookup_by_name(root, base_name);
if (account && xaccAccountGetType (account) != ACCT_TYPE_EQUITY)
account = NULL;
if (!account)
{
base_name = base_name && *base_name ? _(base_name) : "";
account = gnc_account_lookup_by_name(root, base_name);
if (account && xaccAccountGetType (account) != ACCT_TYPE_EQUITY)
account = NULL;
}
base_name_exists = (account != NULL);
if (account &&
gnc_commodity_equiv (currency, xaccAccountGetCommodity (account)))
return account;
name = g_strconcat (base_name, " - ",
gnc_commodity_get_mnemonic (currency), NULL);
account = gnc_account_lookup_by_name(root, name);
if (account && xaccAccountGetType (account) != ACCT_TYPE_EQUITY)
account = NULL;
name_exists = (account != NULL);
if (account &&
gnc_commodity_equiv (currency, xaccAccountGetCommodity (account)))
return account;
/* Couldn't find one, so create it */
if (name_exists && base_name_exists)
{
PWARN ("equity account with unexpected currency");
g_free (name);
return NULL;
}
if (!base_name_exists &&
gnc_commodity_equiv (currency, gnc_default_currency ()))
{
g_free (name);
name = g_strdup (base_name);
}
parent = gnc_account_lookup_by_name(root, _("Equity"));
if (!parent || xaccAccountGetType (parent) != ACCT_TYPE_EQUITY)
parent = root;
g_assert(parent);
account = xaccMallocAccount (gnc_account_get_book(root));
xaccAccountBeginEdit (account);
xaccAccountSetName (account, name);
xaccAccountSetType (account, ACCT_TYPE_EQUITY);
xaccAccountSetCommodity (account, currency);
xaccAccountBeginEdit (parent);
gnc_account_append_child (parent, account);
xaccAccountCommitEdit (parent);
xaccAccountCommitEdit (account);
g_free (name);
return account;
}
gboolean
gnc_account_create_opening_balance (Account *account,
gnc_numeric balance,
time64 date,
QofBook *book)
{
Account *equity_account;
Transaction *trans;
Split *split;
if (gnc_numeric_zero_p (balance))
return TRUE;
g_return_val_if_fail (account != NULL, FALSE);
equity_account =
gnc_find_or_create_equity_account (gnc_account_get_root(account),
EQUITY_OPENING_BALANCE,
xaccAccountGetCommodity (account));
if (!equity_account)
return FALSE;
xaccAccountBeginEdit (account);
xaccAccountBeginEdit (equity_account);
trans = xaccMallocTransaction (book);
xaccTransBeginEdit (trans);
xaccTransSetCurrency (trans, gnc_account_or_default_currency (account, NULL));
xaccTransSetDatePostedSecsNormalized (trans, date);
xaccTransSetDescription (trans, _("Opening Balance"));
split = xaccMallocSplit (book);
xaccTransAppendSplit (trans, split);
xaccAccountInsertSplit (account, split);
xaccSplitSetAmount (split, balance);
xaccSplitSetValue (split, balance);
balance = gnc_numeric_neg (balance);
split = xaccMallocSplit (book);
xaccTransAppendSplit (trans, split);
xaccAccountInsertSplit (equity_account, split);
xaccSplitSetAmount (split, balance);
xaccSplitSetValue (split, balance);
xaccTransCommitEdit (trans);
xaccAccountCommitEdit (equity_account);
xaccAccountCommitEdit (account);
return TRUE;
}
#if 0 /* Not Used */
static void
gnc_lconv_set_utf8 (char **p_value, char *default_value)
{
char *value = *p_value;
*p_value = NULL;
if ((value == NULL) || (value[0] == 0))
value = default_value;
#ifdef G_OS_WIN32
{
/* get number of resulting wide characters */
size_t count = mbstowcs (NULL, value, 0);
if (count > 0)
{
/* malloc and convert */
wchar_t *wvalue = g_malloc ((count + 1) * sizeof(wchar_t));
count = mbstowcs (wvalue, value, count + 1);
if (count > 0)
{
*p_value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
}
g_free (wvalue);
}
}
#else /* !G_OS_WIN32 */
*p_value = g_locale_to_utf8 (value, -1, NULL, NULL, NULL);
#endif
if (*p_value == NULL)
{
// The g_locale_to_utf8 conversion failed. FIXME: Should we rather
// use an empty string instead of the default_value? Not sure.
*p_value = default_value;
}
}
static void
gnc_lconv_set_char (char *p_value, char default_value)
{
if ((p_value != NULL) && (*p_value == CHAR_MAX))
*p_value = default_value;
}
#endif /* Not Used */
gnc_commodity *
gnc_locale_default_currency_nodefault (void)
{
gnc_commodity * currency;
gnc_commodity_table *table;
const char *code;
table = gnc_get_current_commodities ();
code = gnc_locale_default_iso_currency_code ();
currency = gnc_commodity_table_lookup (table, GNC_COMMODITY_NS_CURRENCY, code);