-
Notifications
You must be signed in to change notification settings - Fork 760
/
gnc-tree-model-account.c
1550 lines (1314 loc) · 50.4 KB
/
gnc-tree-model-account.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-tree-model-account.c -- GtkTreeModel implementation to
* display accounts in a GtkTreeView.
*
* Copyright (C) 2003 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2003 David Hampton <hampton@employees.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, 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 <gtk/gtk.h>
#include <glib/gi18n.h>
#include <string.h>
#include "gnc-tree-model-account.h"
#include "gnc-component-manager.h"
#include "Account.h"
#include "dialog-utils.h"
#include "gnc-accounting-period.h"
#include "gnc-commodity.h"
#include "gnc-prefs.h"
#include "gnc-engine.h"
#include "gnc-event.h"
#include "gnc-gobject-utils.h"
#include "gnc-ui-balances.h"
#include "gnc-ui-util.h"
#include <gnc-locale-tax.h>
#define TREE_MODEL_ACCOUNT_CM_CLASS "tree-model-account"
/** Static Globals *******************************************************/
static QofLogModule log_module = GNC_MOD_GUI;
/** Declarations *********************************************************/
static void gnc_tree_model_account_class_init (GncTreeModelAccountClass *klass);
static void gnc_tree_model_account_init (GncTreeModelAccount *model);
static void gnc_tree_model_account_finalize (GObject *object);
static void gnc_tree_model_account_dispose (GObject *object);
/** Implementation of GtkTreeModel **************************************/
static void gnc_tree_model_account_tree_model_init (GtkTreeModelIface *iface);
static GtkTreeModelFlags gnc_tree_model_account_get_flags (GtkTreeModel *tree_model);
static int gnc_tree_model_account_get_n_columns (GtkTreeModel *tree_model);
static GType gnc_tree_model_account_get_column_type (GtkTreeModel *tree_model,
int index);
static gboolean gnc_tree_model_account_get_iter (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreePath *path);
static GtkTreePath *gnc_tree_model_account_get_path (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static void gnc_tree_model_account_get_value (GtkTreeModel *tree_model,
GtkTreeIter *iter,
int column,
GValue *value);
static gboolean gnc_tree_model_account_iter_next (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gboolean gnc_tree_model_account_iter_children (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent);
static gboolean gnc_tree_model_account_iter_has_child (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static int gnc_tree_model_account_iter_n_children (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gboolean gnc_tree_model_account_iter_nth_child (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent,
int n);
static gboolean gnc_tree_model_account_iter_parent (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *child);
/** Component Manager Callback ******************************************/
static void gnc_tree_model_account_event_handler (QofInstance *entity,
QofEventId event_type,
GncTreeModelAccount *model,
GncEventData *ed);
/** The instance private data for an account tree model. */
typedef struct GncTreeModelAccountPrivate
{
QofBook *book;
Account *root;
gint event_handler_id;
gchar *negative_color;
GHashTable *account_values_hash;
} GncTreeModelAccountPrivate;
G_DEFINE_TYPE_WITH_CODE (GncTreeModelAccount,
gnc_tree_model_account, GNC_TYPE_TREE_MODEL,
G_ADD_PRIVATE (GncTreeModelAccount)
G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
gnc_tree_model_account_tree_model_init))
#define GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(o) \
((GncTreeModelAccountPrivate*)gnc_tree_model_account_get_instance_private((GncTreeModelAccount*)o))
/************************************************************/
/* Account Tree Model - Misc Functions */
/************************************************************/
/** Tell the GncTreeModelAccount code to update the color that it will
* use for negative numbers. This function will iterate over all
* existing models and update their setting.
*
* @internal
*/
static void
gnc_tree_model_account_update_color (gpointer gsettings, gchar *key, gpointer user_data)
{
GncTreeModelAccountPrivate *priv;
GncTreeModelAccount *model;
gboolean use_red;
g_return_if_fail (GNC_IS_TREE_MODEL_ACCOUNT(user_data));
model = user_data;
priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
// destroy/recreate the cached account value hash to force update
g_hash_table_destroy (priv->account_values_hash);
priv->account_values_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, g_free);
use_red = gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_NEGATIVE_IN_RED);
if (priv->negative_color)
g_free (priv->negative_color);
if (use_red)
priv->negative_color = gnc_get_negative_color ();
else
priv->negative_color = NULL;
}
/************************************************************/
/* g_object required functions */
/************************************************************/
/** A pointer to the parent class of an account tree model. */
static GObjectClass *parent_class = NULL;
static void
gnc_tree_model_account_class_init (GncTreeModelAccountClass *klass)
{
GObjectClass *o_class;
parent_class = g_type_class_peek_parent (klass);
o_class = G_OBJECT_CLASS(klass);
/* GObject signals */
o_class->finalize = gnc_tree_model_account_finalize;
o_class->dispose = gnc_tree_model_account_dispose;
}
static void
gnc_tree_model_account_init (GncTreeModelAccount *model)
{
GncTreeModelAccountPrivate *priv;
gboolean use_red;
ENTER("model %p", model);
while (model->stamp == 0)
{
model->stamp = g_random_int ();
}
use_red = gnc_prefs_get_bool (GNC_PREFS_GROUP_GENERAL, GNC_PREF_NEGATIVE_IN_RED);
priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
priv->book = NULL;
priv->root = NULL;
if (priv->negative_color)
g_free (priv->negative_color);
if (use_red)
priv->negative_color = gnc_get_negative_color ();
else
priv->negative_color = NULL;
// create the account values cache hash
priv->account_values_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, g_free);
gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL, GNC_PREF_NEGATIVE_IN_RED,
gnc_tree_model_account_update_color,
model);
LEAVE(" ");
}
static void
gnc_tree_model_account_finalize (GObject *object)
{
GncTreeModelAccountPrivate *priv;
GncTreeModelAccount *model;
g_return_if_fail (object != NULL);
g_return_if_fail (GNC_IS_TREE_MODEL_ACCOUNT(object));
ENTER("model %p", object);
model = GNC_TREE_MODEL_ACCOUNT(object);
priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
priv->book = NULL;
if (G_OBJECT_CLASS(parent_class)->finalize)
G_OBJECT_CLASS(parent_class)->finalize (object);
LEAVE(" ");
}
static void
gnc_tree_model_account_dispose (GObject *object)
{
GncTreeModelAccountPrivate *priv;
GncTreeModelAccount *model;
g_return_if_fail (object != NULL);
g_return_if_fail (GNC_IS_TREE_MODEL_ACCOUNT(object));
ENTER("model %p", object);
model = GNC_TREE_MODEL_ACCOUNT(object);
priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
if (priv->event_handler_id)
{
qof_event_unregister_handler (priv->event_handler_id);
priv->event_handler_id = 0;
}
if (priv->negative_color)
g_free (priv->negative_color);
// destroy the cached account values
g_hash_table_destroy (priv->account_values_hash);
gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL, GNC_PREF_NEGATIVE_IN_RED,
gnc_tree_model_account_update_color,
model);
if (G_OBJECT_CLASS(parent_class)->dispose)
G_OBJECT_CLASS(parent_class)->dispose (object);
LEAVE(" ");
}
/************************************************************/
/* New Model Creation */
/************************************************************/
GtkTreeModel *
gnc_tree_model_account_new (Account *root)
{
GncTreeModelAccount *model;
GncTreeModelAccountPrivate *priv;
const GList *item;
ENTER("root %p", root);
item = gnc_gobject_tracking_get_list (GNC_TREE_MODEL_ACCOUNT_NAME);
for ( ; item; item = g_list_next (item))
{
model = (GncTreeModelAccount *)item->data;
priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
if (priv->root == root)
{
g_object_ref (G_OBJECT(model));
LEAVE("returning existing model %p", model);
return GTK_TREE_MODEL(model);
}
}
model = g_object_new (GNC_TYPE_TREE_MODEL_ACCOUNT, NULL);
priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
priv->book = gnc_get_current_book();
priv->root = root;
priv->event_handler_id = qof_event_register_handler
((QofEventHandler)gnc_tree_model_account_event_handler, model);
LEAVE("model %p", model);
return GTK_TREE_MODEL(model);
}
/************************************************************/
/* Gnc Tree Model Debugging Utility Function */
/************************************************************/
#define ITER_STRING_LEN 128
static const gchar *
iter_to_string (GtkTreeIter *iter)
{
#ifdef G_THREADS_ENABLED
static GPrivate gtmits_buffer_key = G_PRIVATE_INIT(g_free);
gchar *string;
string = g_private_get (>mits_buffer_key);
if (string == NULL)
{
string = g_malloc(ITER_STRING_LEN + 1);
g_private_set (>mits_buffer_key, string);
}
#else
static char string[ITER_STRING_LEN + 1];
#endif
if (iter)
snprintf (string, ITER_STRING_LEN,
"[stamp:%x data:%p (%s), %p, %d]",
iter->stamp, iter->user_data,
xaccAccountGetName ((Account *) iter->user_data),
iter->user_data2, GPOINTER_TO_INT(iter->user_data3));
else
strcpy (string, "(null)");
return string;
}
/************************************************************/
/* Gtk Tree Model Required Interface Functions */
/************************************************************/
static void
gnc_tree_model_account_tree_model_init (GtkTreeModelIface *iface)
{
iface->get_flags = gnc_tree_model_account_get_flags;
iface->get_n_columns = gnc_tree_model_account_get_n_columns;
iface->get_column_type = gnc_tree_model_account_get_column_type;
iface->get_iter = gnc_tree_model_account_get_iter;
iface->get_path = gnc_tree_model_account_get_path;
iface->get_value = gnc_tree_model_account_get_value;
iface->iter_next = gnc_tree_model_account_iter_next;
iface->iter_children = gnc_tree_model_account_iter_children;
iface->iter_has_child = gnc_tree_model_account_iter_has_child;
iface->iter_n_children = gnc_tree_model_account_iter_n_children;
iface->iter_nth_child = gnc_tree_model_account_iter_nth_child;
iface->iter_parent = gnc_tree_model_account_iter_parent;
}
static GtkTreeModelFlags
gnc_tree_model_account_get_flags (GtkTreeModel *tree_model)
{
return 0;
}
static int
gnc_tree_model_account_get_n_columns (GtkTreeModel *tree_model)
{
g_return_val_if_fail (GNC_IS_TREE_MODEL_ACCOUNT(tree_model), -1);
return GNC_TREE_MODEL_ACCOUNT_NUM_COLUMNS;
}
static GType
gnc_tree_model_account_get_column_type (GtkTreeModel *tree_model, int index)
{
g_return_val_if_fail (GNC_IS_TREE_MODEL_ACCOUNT (tree_model), G_TYPE_INVALID);
g_return_val_if_fail ((index < GNC_TREE_MODEL_ACCOUNT_NUM_COLUMNS) && (index >= 0), G_TYPE_INVALID);
switch (index)
{
case GNC_TREE_MODEL_ACCOUNT_COL_NAME:
case GNC_TREE_MODEL_ACCOUNT_COL_TYPE:
case GNC_TREE_MODEL_ACCOUNT_COL_COMMODITY:
case GNC_TREE_MODEL_ACCOUNT_COL_CODE:
case GNC_TREE_MODEL_ACCOUNT_COL_DESCRIPTION:
case GNC_TREE_MODEL_ACCOUNT_COL_PRESENT:
case GNC_TREE_MODEL_ACCOUNT_COL_PRESENT_REPORT:
case GNC_TREE_MODEL_ACCOUNT_COL_BALANCE:
case GNC_TREE_MODEL_ACCOUNT_COL_BALANCE_REPORT:
case GNC_TREE_MODEL_ACCOUNT_COL_BALANCE_PERIOD:
case GNC_TREE_MODEL_ACCOUNT_COL_BALANCE_LIMIT:
case GNC_TREE_MODEL_ACCOUNT_COL_BALANCE_LIMIT_EXPLANATION:
case GNC_TREE_MODEL_ACCOUNT_COL_CLEARED:
case GNC_TREE_MODEL_ACCOUNT_COL_CLEARED_REPORT:
case GNC_TREE_MODEL_ACCOUNT_COL_RECONCILED:
case GNC_TREE_MODEL_ACCOUNT_COL_RECONCILED_REPORT:
case GNC_TREE_MODEL_ACCOUNT_COL_RECONCILED_DATE:
case GNC_TREE_MODEL_ACCOUNT_COL_FUTURE_MIN:
case GNC_TREE_MODEL_ACCOUNT_COL_FUTURE_MIN_REPORT:
case GNC_TREE_MODEL_ACCOUNT_COL_TOTAL:
case GNC_TREE_MODEL_ACCOUNT_COL_TOTAL_REPORT:
case GNC_TREE_MODEL_ACCOUNT_COL_TOTAL_PERIOD:
case GNC_TREE_MODEL_ACCOUNT_COL_NOTES:
case GNC_TREE_MODEL_ACCOUNT_COL_TAX_INFO:
case GNC_TREE_MODEL_ACCOUNT_COL_TAX_INFO_SUB_ACCT:
case GNC_TREE_MODEL_ACCOUNT_COL_LASTNUM:
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_PRESENT:
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_ACCOUNT:
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_BALANCE:
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_BALANCE_PERIOD:
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_CLEARED:
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_RECONCILED:
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_FUTURE_MIN:
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_TOTAL:
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_TOTAL_PERIOD:
return G_TYPE_STRING;
case GNC_TREE_MODEL_ACCOUNT_COL_HIDDEN:
case GNC_TREE_MODEL_ACCOUNT_COL_PLACEHOLDER:
case GNC_TREE_MODEL_ACCOUNT_COL_OPENING_BALANCE:
return G_TYPE_BOOLEAN;
default:
g_assert_not_reached ();
return G_TYPE_INVALID;
}
}
static gboolean
gnc_tree_model_account_get_iter (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreePath *path)
{
GncTreeModelAccountPrivate *priv;
GncTreeModelAccount *model;
Account *account, *parent;
gint i, *indices;
g_return_val_if_fail (GNC_IS_TREE_MODEL_ACCOUNT(tree_model), FALSE);
{
gchar *path_string = gtk_tree_path_to_string (path);
ENTER("model %p, iter %p, path %s", tree_model, iter, path_string);
g_free (path_string);
}
model = GNC_TREE_MODEL_ACCOUNT(tree_model);
priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
if (gtk_tree_path_get_depth (path) <= 0)
{
LEAVE("bad depth");
return FALSE;
}
indices = gtk_tree_path_get_indices (path);
if (indices[0] != 0)
{
LEAVE("bad root index");
return FALSE;
}
parent = NULL;
account = priv->root;
for (i = 1; i < gtk_tree_path_get_depth (path); i++)
{
parent = account;
account = gnc_account_nth_child (parent, indices[i]);
if (account == NULL)
{
iter->stamp = 0;
LEAVE("bad index");
return FALSE;
}
}
iter->stamp = model->stamp;
iter->user_data = account;
iter->user_data2 = parent;
iter->user_data3 = GINT_TO_POINTER(indices[i - 1]);
LEAVE("iter %s", iter_to_string (iter));
return TRUE;
}
static GtkTreePath *
gnc_tree_model_account_get_path (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
GncTreeModelAccount *model = GNC_TREE_MODEL_ACCOUNT(tree_model);
GncTreeModelAccountPrivate *priv;
Account *account, *parent;
GtkTreePath *path;
gint i;
g_return_val_if_fail (GNC_IS_TREE_MODEL_ACCOUNT(model), NULL);
g_return_val_if_fail (iter != NULL, NULL);
g_return_val_if_fail (iter->user_data != NULL, NULL);
g_return_val_if_fail (iter->stamp == model->stamp, NULL);
ENTER("model %p, iter %s", model, iter_to_string (iter));
priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
if (priv->root == NULL)
{
LEAVE("failed (1)");
return NULL;
}
account = (Account *) iter->user_data;
parent = (Account *) iter->user_data2;
path = gtk_tree_path_new ();
while (parent)
{
i = gnc_account_child_index (parent, account);
if (i == -1)
{
gtk_tree_path_free (path);
LEAVE("failed (3)");
return NULL;
}
gtk_tree_path_prepend_index (path, i);
account = parent;
parent = gnc_account_get_parent (account);
};
/* Add the root node. */
gtk_tree_path_prepend_index (path, 0);
{
gchar *path_string = gtk_tree_path_to_string (path);
LEAVE("path (4) %s", path_string);
g_free (path_string);
}
return path;
}
static void
gnc_tree_model_account_set_color (GncTreeModelAccount *model,
gboolean negative,
GValue *value)
{
GncTreeModelAccountPrivate *priv;
priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
if (negative)
g_value_set_static_string (value, priv->negative_color);
else
g_value_set_static_string (value, NULL);
}
static gchar *
gnc_tree_model_account_compute_period_balance (GncTreeModelAccount *model,
Account *acct,
gboolean recurse,
gboolean *negative)
{
GncTreeModelAccountPrivate *priv;
GNCPrintAmountInfo print_info;
time64 t1, t2;
gnc_numeric b3;
if (negative)
*negative = FALSE;
priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
if (acct == priv->root)
return g_strdup ("");
t1 = gnc_accounting_period_fiscal_start ();
t2 = gnc_accounting_period_fiscal_end ();
if (t1 > t2)
return g_strdup ("");
b3 = xaccAccountGetBalanceChangeForPeriod (acct, t1, t2, recurse);
if (gnc_reverse_balance (acct))
b3 = gnc_numeric_neg (b3);
if (negative)
*negative = gnc_numeric_negative_p (b3);
print_info = gnc_account_print_info (acct, TRUE);
return g_strdup (gnc_print_amount_with_bidi_ltr_isolate (b3, print_info));
}
static gboolean
row_changed_foreach_func (GtkTreeModel *model, GtkTreePath *path,
GtkTreeIter *iter, gpointer user_data)
{
gtk_tree_model_row_changed (model, path, iter);
return FALSE;
}
void
gnc_tree_model_account_clear_cache (GncTreeModelAccount *model)
{
if (model)
{
GncTreeModelAccountPrivate *priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
// destroy the cached account values and recreate
g_hash_table_destroy (priv->account_values_hash);
priv->account_values_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
g_free, g_free);
gtk_tree_model_foreach (GTK_TREE_MODEL(model), row_changed_foreach_func, NULL);
}
}
static void
clear_account_cached_values (GncTreeModelAccount *model, GHashTable *hash, Account *account)
{
GtkTreeIter iter;
gchar acct_guid_str[GUID_ENCODING_LENGTH + 1];
if (!account)
return;
// make sure tree view sees the change
if (gnc_tree_model_account_get_iter_from_account (model, account, &iter))
{
GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL(model), &iter);
gtk_tree_model_row_changed (GTK_TREE_MODEL(model), path, &iter);
gtk_tree_path_free (path);
}
guid_to_string_buff (xaccAccountGetGUID (account), acct_guid_str);
// loop over the columns and remove any found
for (gint col = 0; col <= GNC_TREE_MODEL_ACCOUNT_NUM_COLUMNS; col++)
{
gchar *key = g_strdup_printf ("%s,%d", acct_guid_str, col);
g_hash_table_remove (hash, key);
g_free (key);
}
}
static void
gnc_tree_model_account_clear_cached_values (GncTreeModelAccount *model, Account *account)
{
GncTreeModelAccountPrivate *priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
Account *parent;
// no hash table or account, return
if ((!priv->account_values_hash) || (!account))
return;
clear_account_cached_values (model, priv->account_values_hash, account);
parent = gnc_account_get_parent (account);
// clear also all parent accounts, this will update any balances/totals
while (parent)
{
clear_account_cached_values (model, priv->account_values_hash, parent);
parent = gnc_account_get_parent (parent);
}
}
static gboolean
gnc_tree_model_account_get_cached_value (GncTreeModelAccount *model, Account *account,
gint column, gchar **cached_string)
{
GncTreeModelAccountPrivate *priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
gchar acct_guid_str[GUID_ENCODING_LENGTH + 1];
gchar *key = NULL;
gpointer value;
gboolean found;
if ((!priv->account_values_hash) || (!account))
return FALSE;
guid_to_string_buff (xaccAccountGetGUID (account), acct_guid_str);
key = g_strdup_printf ("%s,%d", acct_guid_str, column);
found = g_hash_table_lookup_extended (priv->account_values_hash, key,
NULL, &value);
if (found)
*cached_string = g_strdup (value);
g_free (key);
return found;
}
static void
gnc_tree_model_account_set_cached_value (GncTreeModelAccount *model, Account *account,
gint column, GValue *value)
{
GncTreeModelAccountPrivate *priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
if ((!priv->account_values_hash) || (!account))
return;
// only interested in string values
if (G_VALUE_HOLDS_STRING(value))
{
gchar acct_guid_str[GUID_ENCODING_LENGTH + 1];
const gchar *str = g_value_get_string (value);
gchar *key = NULL;
guid_to_string_buff (xaccAccountGetGUID (account), acct_guid_str);
key = g_strdup_printf ("%s,%d", acct_guid_str, column);
g_hash_table_insert (priv->account_values_hash, key, g_strdup (str));
}
}
static void
gnc_tree_model_account_get_value (GtkTreeModel *tree_model,
GtkTreeIter *iter,
int column,
GValue *value)
{
GncTreeModelAccount *model = GNC_TREE_MODEL_ACCOUNT(tree_model);
GncTreeModelAccountPrivate *priv;
Account *account;
gboolean negative; /* used to set "deficit style" also known as red numbers */
gchar *string;
gchar *cached_string = NULL;
time64 last_date;
g_return_if_fail (GNC_IS_TREE_MODEL_ACCOUNT(model));
g_return_if_fail (iter != NULL);
g_return_if_fail (iter->user_data != NULL);
g_return_if_fail (iter->stamp == model->stamp);
ENTER("model %p, iter %s, col %d", tree_model,
iter_to_string (iter), column);
account = (Account *) iter->user_data;
priv = GNC_TREE_MODEL_ACCOUNT_GET_PRIVATE(model);
// lets see if the value is in the cache
if (gnc_tree_model_account_get_cached_value (model, account, column, &cached_string))
{
g_value_init (value, G_TYPE_STRING);
g_value_take_string (value, cached_string);
LEAVE("value in cache, '%s'", cached_string);
return;
}
switch (column)
{
case GNC_TREE_MODEL_ACCOUNT_COL_NAME:
g_value_init (value, G_TYPE_STRING);
if (account == priv->root)
g_value_set_string (value, _("New top level account"));
else
g_value_set_string (value, xaccAccountGetName (account));
break;
case GNC_TREE_MODEL_ACCOUNT_COL_TYPE:
g_value_init (value, G_TYPE_STRING);
g_value_set_string (value,
xaccAccountGetTypeStr (xaccAccountGetType (account)));
break;
case GNC_TREE_MODEL_ACCOUNT_COL_CODE:
g_value_init (value, G_TYPE_STRING);
g_value_set_string (value, xaccAccountGetCode (account));
break;
case GNC_TREE_MODEL_ACCOUNT_COL_COMMODITY:
g_value_init (value, G_TYPE_STRING);
g_value_set_string (value,
gnc_commodity_get_fullname(xaccAccountGetCommodity (account)));
break;
case GNC_TREE_MODEL_ACCOUNT_COL_DESCRIPTION:
g_value_init (value, G_TYPE_STRING);
g_value_set_string (value, xaccAccountGetDescription (account));
break;
case GNC_TREE_MODEL_ACCOUNT_COL_PRESENT:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_balance (xaccAccountGetPresentBalanceInCurrency,
account, TRUE, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_PRESENT_REPORT:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_report_balance (xaccAccountGetPresentBalanceInCurrency,
account, TRUE, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_PRESENT:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_balance (xaccAccountGetPresentBalanceInCurrency,
account, TRUE, &negative);
gnc_tree_model_account_set_color (model, negative, value);
g_free (string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_BALANCE:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_balance (xaccAccountGetBalanceInCurrency,
account, FALSE, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_BALANCE_REPORT:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_report_balance (xaccAccountGetBalanceInCurrency,
account, FALSE, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_BALANCE:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_balance (xaccAccountGetBalanceInCurrency,
account, FALSE, &negative);
gnc_tree_model_account_set_color (model, negative, value);
g_free (string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_BALANCE_PERIOD:
g_value_init (value, G_TYPE_STRING);
string = gnc_tree_model_account_compute_period_balance (model, account, FALSE, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_BALANCE_PERIOD:
g_value_init (value, G_TYPE_STRING);
string = gnc_tree_model_account_compute_period_balance (model, account, FALSE, &negative);
gnc_tree_model_account_set_color (model, negative, value);
g_free (string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_BALANCE_LIMIT:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_balance_limit_icon_name (account);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_BALANCE_LIMIT_EXPLANATION:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_balance_limit_explanation (account);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_CLEARED:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_balance (xaccAccountGetClearedBalanceInCurrency,
account, TRUE, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_CLEARED_REPORT:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_report_balance (xaccAccountGetClearedBalanceInCurrency,
account, TRUE, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_CLEARED:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_balance (xaccAccountGetClearedBalanceInCurrency,
account, TRUE, &negative);
gnc_tree_model_account_set_color (model, negative, value);
g_free (string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_RECONCILED:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_balance (xaccAccountGetReconciledBalanceInCurrency,
account, TRUE, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_RECONCILED_REPORT:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_report_balance (xaccAccountGetReconciledBalanceInCurrency,
account, TRUE, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_RECONCILED_DATE:
g_value_init (value, G_TYPE_STRING);
if (xaccAccountGetReconcileLastDate (account, &last_date))
g_value_take_string (value, qof_print_date (last_date));
break;
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_RECONCILED:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_balance (xaccAccountGetReconciledBalanceInCurrency,
account, TRUE, &negative);
gnc_tree_model_account_set_color (model, negative, value);
g_free (string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_FUTURE_MIN:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_balance (xaccAccountGetProjectedMinimumBalanceInCurrency,
account, TRUE, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_FUTURE_MIN_REPORT:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_report_balance (xaccAccountGetProjectedMinimumBalanceInCurrency,
account, TRUE, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_FUTURE_MIN:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_balance (xaccAccountGetProjectedMinimumBalanceInCurrency,
account, TRUE, &negative);
gnc_tree_model_account_set_color (model, negative, value);
g_free (string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_TOTAL:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_balance (xaccAccountGetBalanceInCurrency,
account, TRUE, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_TOTAL_REPORT:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_report_balance (xaccAccountGetBalanceInCurrency,
account, TRUE, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_TOTAL:
g_value_init (value, G_TYPE_STRING);
string = gnc_ui_account_get_print_balance (xaccAccountGetBalanceInCurrency,
account, TRUE, &negative);
gnc_tree_model_account_set_color (model, negative, value);
g_free (string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_TOTAL_PERIOD:
g_value_init (value, G_TYPE_STRING);
string = gnc_tree_model_account_compute_period_balance (model, account, TRUE, &negative);
g_value_take_string (value, string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_TOTAL_PERIOD:
g_value_init (value, G_TYPE_STRING);
string = gnc_tree_model_account_compute_period_balance (model, account, TRUE, &negative);
gnc_tree_model_account_set_color (model, negative, value);
g_free (string);
break;
case GNC_TREE_MODEL_ACCOUNT_COL_COLOR_ACCOUNT:
g_value_init (value, G_TYPE_STRING);
g_value_set_string (value, xaccAccountGetColor (account));
break;
case GNC_TREE_MODEL_ACCOUNT_COL_NOTES:
g_value_init (value, G_TYPE_STRING);
g_value_set_string (value, xaccAccountGetNotes (account));
break;
case GNC_TREE_MODEL_ACCOUNT_COL_TAX_INFO:
g_value_init (value, G_TYPE_STRING);
g_value_take_string (value, gnc_ui_account_get_tax_info_string (account));
break;
case GNC_TREE_MODEL_ACCOUNT_COL_TAX_INFO_SUB_ACCT:
g_value_init (value, G_TYPE_STRING);
g_value_take_string (value, gnc_ui_account_get_tax_info_sub_acct_string (account));
break;
case GNC_TREE_MODEL_ACCOUNT_COL_LASTNUM:
g_value_init (value, G_TYPE_STRING);
g_value_set_string (value, xaccAccountGetLastNum (account));
break;
case GNC_TREE_MODEL_ACCOUNT_COL_HIDDEN:
g_value_init (value, G_TYPE_BOOLEAN);
g_value_set_boolean (value, xaccAccountGetHidden (account));
break;
case GNC_TREE_MODEL_ACCOUNT_COL_PLACEHOLDER:
g_value_init (value, G_TYPE_BOOLEAN);
g_value_set_boolean (value, xaccAccountGetPlaceholder (account));
break;
case GNC_TREE_MODEL_ACCOUNT_COL_OPENING_BALANCE:
g_value_init (value, G_TYPE_BOOLEAN);
g_value_set_boolean (value, xaccAccountGetIsOpeningBalance (account));
break;
default:
g_assert_not_reached ();
break;
}
// save the value to the account values cache
gnc_tree_model_account_set_cached_value (model, account, column, value);
LEAVE(" ");
}
static gboolean
gnc_tree_model_account_iter_next (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
GncTreeModelAccount *model = GNC_TREE_MODEL_ACCOUNT(tree_model);
Account *account, *parent;