-
Notifications
You must be signed in to change notification settings - Fork 759
/
assistant-hierarchy.c
1171 lines (994 loc) · 37.2 KB
/
assistant-hierarchy.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
/********************************************************************\
* assistant-hierarchy.c -- account hierarchy creation functionality*
* Copyright (C) 2001 Gnumatic, Inc. *
* Copyright (C) 2006 David Hampton <hampton@employees.org> *
* Copyright (C) 2010 Geert Janssens *
* *
* 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 <glib/gstdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "gnc-account-merge.h"
#include "dialog-new-user.h"
#include "dialog-utils.h"
#include "dialog-file-access.h"
#include "assistant-hierarchy.h"
#include "assistant-utils.h"
#include "gnc-amount-edit.h"
#include "gnc-currency-edit.h"
#include "gnc-exp-parser.h"
#include "gnc-general-select.h"
#include "gnc-gconf-utils.h"
#include "gnc-hooks.h"
#include "gnc-component-manager.h"
#include "gnc-path.h"
#include "gnc-gui-query.h"
#include "gnc-tree-view-account.h"
#include "gnc-ui-util.h"
#include "io-example-account.h"
#include "top-level.h"
#include "gnc-main-window.h"
#include "gnc-plugin-page-account-tree.h"
#include "gnc-engine.h"
static QofLogModule log_module = GNC_MOD_IMPORT;
#define GCONF_SECTION "dialogs/new_hierarchy"
typedef enum
{
COL_CHECKED,
COL_TITLE,
COL_SHORT_DESCRIPTION,
COL_LONG_DESCRIPTION,
COL_ACCOUNT,
NUM_COLUMNS
} ColumnNames;
typedef struct
{
GtkWidget *dialog;
GtkWidget *assistant;
gboolean next_ok;
GtkWidget *currency_selector;
GtkTreeView *categories_tree;
GtkTreeRowReference *initial_category;
GtkTextView *category_description;
GtkWidget *category_accounts_container;
GtkLabel *category_accounts_label;
GtkTreeView *category_accounts_tree;
gboolean category_set_changed;
GncTreeViewAccount *final_account_tree;
GtkWidget *final_account_tree_container;
Account *selected_account;
/** Map<Account*,gnc_numeric*> **/
GHashTable *balance_hash;
Account *our_account_tree;
QofBook *temporary;
gboolean account_list_added;
gboolean use_defaults;
GncHierarchyAssistantFinishedCallback when_completed;
} hierarchy_data;
void on_prepare (GtkAssistant *assistant, GtkWidget *page,
hierarchy_data *data);
void on_choose_account_categories_prepare (hierarchy_data *data);
void select_all_clicked (GtkButton *button,
hierarchy_data *data);
void clear_all_clicked (GtkButton *button,
hierarchy_data *data);
void on_final_account_prepare (hierarchy_data *data);
void on_cancel (GtkAssistant *gtkassistant, hierarchy_data *data);
void on_finish (GtkAssistant *gtkassistant, hierarchy_data *data);
// ------------------------------------------------------------
static void
delete_hierarchy_dialog (hierarchy_data *data)
{
gtk_widget_destroy (data->dialog);
}
static void
destroy_hash_helper (gpointer key, gpointer value, gpointer user_data)
{
gnc_numeric *balance = value;
g_free (balance);
}
static void
gnc_hierarchy_destroy_cb (GtkObject *obj, hierarchy_data *data)
{
GHashTable *hash;
hash = data->balance_hash;
if (hash)
{
g_hash_table_foreach (hash, destroy_hash_helper, NULL);
g_hash_table_destroy (hash);
data->balance_hash = NULL;
}
}
static gnc_numeric
get_final_balance (GHashTable *hash, Account *account)
{
gnc_numeric *balance;
if (!hash || !account)
return gnc_numeric_zero ();
balance = g_hash_table_lookup(hash, account);
if (balance)
return *balance;
return gnc_numeric_zero ();
}
static void
set_final_balance (GHashTable *hash, Account *account, gnc_numeric in_balance)
{
gnc_numeric *balance;
if (!hash || !account)
return;
balance = g_hash_table_lookup (hash, account);
if (balance)
{
*balance = in_balance;
return;
}
balance = g_new (gnc_numeric, 1);
*balance = in_balance;
g_hash_table_insert (hash, account, balance);
}
static gchar*
gnc_get_ea_locale_dir(const char *top_dir)
{
static gchar *default_locale = "C";
gchar *ret;
gchar *locale;
struct stat buf;
int i;
#ifdef HAVE_LC_MESSAGES
locale = g_strdup(setlocale(LC_MESSAGES, NULL));
#else
# ifdef G_OS_WIN32
/* On win32, setlocale() doesn't say anything useful. Use
glib's function instead. */
locale = g_win32_getlocale();
if (!locale)
{
PWARN ("Couldn't retrieve locale. Falling back to default one.")
locale = g_strdup ("C");
}
# else
/*
* Mac OS X 10.1 and earlier, not only doesn't have LC_MESSAGES
* setlocale can sometimes return NULL instead of "C"
*/
locale = g_strdup(setlocale(LC_ALL, NULL) ?
setlocale(LC_ALL, NULL) : "C");
# endif
#endif
i = strlen(locale);
ret = g_build_filename(top_dir, locale, (char *)NULL);
while (g_stat(ret, &buf) != 0)
{
i--;
if (i < 1)
{
g_free(ret);
ret = g_build_filename(top_dir, default_locale, (char *)NULL);
break;
}
locale[i] = '\0';
g_free(ret);
ret = g_build_filename(top_dir, locale, (char *)NULL);
}
g_free(locale);
return ret;
}
/************************************************************
* Choose Categories Page *
************************************************************/
/** This is a helper function called on each item in the GtkTreeStore
* by categories_page_enable_next. The purpose is to determine if an
* account set has been selected.
*
* @param store The GtkListStore containing one line per account set.
*
* @param path A GtkTreePath for the entry in question.
*
* @param iter A GtkTreeIter for the entry in question.
*
* @param result A pointer to the result value passed to/from the
* caller. This function sets the result value to TRUE if the entry
* in question is checked.
*
* @return TRUE if the entry in question is checked to cancel the
* tree walk. FALSE if unchecked to that the rest of the tree will
* be tested. */
static gboolean
account_set_checked_helper (GtkListStore *store,
GtkTreePath *path,
GtkTreeIter *iter,
gboolean *result)
{
gboolean checked;
g_return_val_if_fail(GTK_IS_LIST_STORE(store), FALSE);
gtk_tree_model_get (GTK_TREE_MODEL(store), iter, COL_CHECKED, &checked, -1);
if (checked)
{
*result = TRUE;
return TRUE; /* Stop tree walk. */
}
return FALSE;
}
/** This function determines if the "Next" button on the account set
* selection page should be sensitive. The button should only be
* sensitive if one or more account sets has been selected.
*
* @param data A pointer to the data structure describing the
* hierarchy assistant. */
static void
categories_page_enable_next (hierarchy_data *data)
{
gint currentpagenum;
GtkWidget *currentpage;
GtkAssistant *assistant = GTK_ASSISTANT(data->dialog);
data->next_ok = FALSE;
gtk_tree_model_foreach (gtk_tree_view_get_model (data->categories_tree),
(GtkTreeModelForeachFunc)account_set_checked_helper,
&data->next_ok);
currentpagenum = gtk_assistant_get_current_page(assistant);
currentpage = gtk_assistant_get_nth_page(assistant, currentpagenum);
gtk_assistant_set_page_complete(assistant, currentpage, data->next_ok);
}
static void
categories_selection_changed (GtkTreeModel *treemodel,
GtkTreePath *arg1,
GtkTreeIter *arg2,
hierarchy_data *data)
{
data->category_set_changed = TRUE;
categories_page_enable_next(data);
}
static void
add_one_category (GncExampleAccount *acc,
hierarchy_data *data)
{
GtkTreeView *view;
GtkListStore *store;
GtkTreeIter iter;
GtkTreePath* path;
gboolean use_defaults;
g_return_if_fail(acc != NULL);
g_return_if_fail(data != NULL);
view = data->categories_tree;
store = GTK_LIST_STORE(gtk_tree_view_get_model(view));
use_defaults = data->use_defaults && acc->start_selected;
gtk_list_store_append(store, &iter);
gtk_list_store_set(store, &iter,
COL_CHECKED, use_defaults,
COL_TITLE, acc->title,
COL_SHORT_DESCRIPTION, acc->short_description,
COL_LONG_DESCRIPTION, acc->long_description,
COL_ACCOUNT, acc,
-1);
if (use_defaults)
{
data->category_set_changed = TRUE;
path = gtk_tree_model_get_path(GTK_TREE_MODEL(store), &iter);
data->initial_category = gtk_tree_row_reference_new(GTK_TREE_MODEL(store), path);
gtk_tree_path_free(path);
}
}
static void
category_checkbox_toggled (GtkCellRendererToggle *toggle,
gchar *path,
GtkListStore *store)
{
GtkTreeIter iter;
gboolean active;
if (!gtk_tree_model_get_iter_from_string(GTK_TREE_MODEL(store),
&iter, path))
return;
/* Get current state of the category */
active = gtk_cell_renderer_toggle_get_active(toggle);
gtk_list_store_set(store, &iter, COL_CHECKED, !active, -1);
}
static void
account_categories_tree_view_prepare (hierarchy_data *data)
{
GSList *list;
gchar *gnc_accounts_dir;
gchar *locale_dir;
GtkTreeView *tree_view;
GtkListStore *model;
GtkTreeViewColumn *column;
GtkCellRenderer *renderer;
GtkTreeSelection *selection;
GtkTreePath *path;
gnc_accounts_dir = gnc_path_get_accountsdir ();
locale_dir = gnc_get_ea_locale_dir (gnc_accounts_dir);
list = gnc_load_example_account_list (locale_dir);
g_free (gnc_accounts_dir);
g_free (locale_dir);
/* Prepare the account_categories GtkTreeView with a model and with some columns */
tree_view = data->categories_tree;
model = gtk_list_store_new(NUM_COLUMNS, G_TYPE_BOOLEAN, G_TYPE_STRING, G_TYPE_STRING,
G_TYPE_STRING, G_TYPE_POINTER);
gtk_tree_view_set_model (tree_view, GTK_TREE_MODEL(model));
g_object_unref (model);
g_slist_foreach(list, (GFunc)add_one_category, data);
g_signal_connect (G_OBJECT (model), "row_changed",
G_CALLBACK (categories_selection_changed),
data);
renderer = gtk_cell_renderer_toggle_new ();
g_object_set (G_OBJECT (renderer), "activatable", TRUE, NULL);
column = gtk_tree_view_column_new_with_attributes (_("Selected"),
renderer,
"active", COL_CHECKED,
NULL);
gtk_tree_view_append_column (tree_view, column);
gtk_tree_view_column_set_sort_column_id (column, COL_CHECKED);
g_signal_connect (G_OBJECT (renderer), "toggled",
G_CALLBACK (category_checkbox_toggled),
model);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes (_("Account Types"),
renderer,
"text", COL_TITLE,
NULL);
gtk_tree_view_append_column (tree_view, column);
gtk_tree_view_column_set_sort_column_id (column, COL_TITLE);
// renderer = gtk_cell_renderer_text_new ();
// column = gtk_tree_view_column_new_with_attributes (_("Description"),
// renderer,
// "text", COL_SHORT_DESCRIPTION,
// NULL);
// gtk_tree_view_append_column (tree_view, column);
// gtk_tree_view_column_set_sort_column_id (column, COL_SHORT_DESCRIPTION);
gtk_tree_view_set_headers_clickable(tree_view, TRUE);
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(model),
COL_TITLE,
GTK_SORT_ASCENDING);
if (data->initial_category)
{
path = gtk_tree_row_reference_get_path(data->initial_category);
selection = gtk_tree_view_get_selection(tree_view);
gtk_tree_view_scroll_to_cell(tree_view, path, NULL, TRUE, 0.5, 0.5);
gtk_tree_selection_select_path(selection, path);
gtk_tree_path_free(path);
}
}
void on_prepare (GtkAssistant *assistant, GtkWidget *page,
hierarchy_data *data)
{
gint currentpage = gtk_assistant_get_current_page(assistant);
switch (gtk_assistant_get_current_page(assistant))
{
case 2:
/* Current page is account selection */
on_choose_account_categories_prepare(data);
break;
case 3:
/* Current page is final account page */
on_final_account_prepare (data);
break;
}
}
void
on_choose_account_categories_prepare (hierarchy_data *data)
{
GtkTextBuffer* buffer;
if (!data->account_list_added)
{
/* clear out the description/tree */
if (data->category_accounts_tree)
gtk_widget_destroy(GTK_WIDGET(data->category_accounts_tree));
data->category_accounts_tree = NULL;
buffer = gtk_text_view_get_buffer(data->category_description);
gtk_text_buffer_set_text(buffer, "", -1);
data->account_list_added = TRUE;
/* Build the categories tree if necessary */
gnc_suspend_gui_refresh ();
account_categories_tree_view_prepare (data);
gnc_resume_gui_refresh ();
}
categories_page_enable_next(data);
}
static void
categories_tree_selection_changed (GtkTreeSelection *selection,
hierarchy_data *data)
{
GtkTreeView *tree_view;
GtkTreeModel *model;
GtkTreeViewColumn *column;
GtkTreeIter iter;
GncExampleAccount *gea;
GtkTextBuffer* buffer;
gchar *text;
/* Remove the old account tree */
if (data->category_accounts_tree)
gtk_widget_destroy(GTK_WIDGET(data->category_accounts_tree));
data->category_accounts_tree = NULL;
/* Add a new one if something selected */
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
gchar *text2;
gtk_tree_model_get (model, &iter, COL_ACCOUNT, &gea, -1);
/* Translators: '%s' is the name of the selected account hierarchy template. */
text2 = g_strdup_printf(_("Accounts in '%s'"), gea->title);
text = g_strdup_printf("<b>%s</b>", text2);
gtk_label_set_markup(data->category_accounts_label, text);
g_free(text2);
g_free(text);
buffer = gtk_text_view_get_buffer(data->category_description);
gtk_text_buffer_set_text(buffer, gea->long_description ?
gea->long_description :
_("No description provided."), -1);
tree_view = gnc_tree_view_account_new_with_root (gea->root, FALSE);
/* Override the normal fixed (user settable) sizing */
column = gtk_tree_view_get_column(GTK_TREE_VIEW(tree_view), 0);
gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
data->category_accounts_tree = tree_view;
gtk_tree_view_expand_all (tree_view);
gtk_container_add(GTK_CONTAINER(data->category_accounts_container), GTK_WIDGET(tree_view));
gtk_widget_show(GTK_WIDGET(tree_view));
}
else
{
gchar *text;
text = g_strdup_printf ("<b>%s</b>", _("Accounts in Category"));
gtk_label_set_markup(data->category_accounts_label, text);
g_free (text);
buffer = gtk_text_view_get_buffer(data->category_description);
gtk_text_buffer_set_text(buffer, "", -1);
}
}
static gboolean
select_helper (GtkListStore *store,
GtkTreePath *path,
GtkTreeIter *iter,
gpointer data)
{
GncExampleAccount *gea;
g_return_val_if_fail(GTK_IS_LIST_STORE(store), FALSE);
gtk_tree_model_get (GTK_TREE_MODEL(store), iter, COL_ACCOUNT, &gea, -1);
if ((gea != NULL) && !gea->exclude_from_select_all)
{
gtk_list_store_set(store, iter,
COL_CHECKED, GPOINTER_TO_INT(data),
-1);
}
return FALSE; /* Run entire tree */
}
void
select_all_clicked (GtkButton *button,
hierarchy_data *data)
{
gtk_tree_model_foreach (gtk_tree_view_get_model (data->categories_tree),
(GtkTreeModelForeachFunc)select_helper,
GINT_TO_POINTER(TRUE));
}
void
clear_all_clicked (GtkButton *button,
hierarchy_data *data)
{
gtk_tree_model_foreach (gtk_tree_view_get_model (data->categories_tree),
(GtkTreeModelForeachFunc)select_helper,
GINT_TO_POINTER(FALSE));
}
/************************************************************
* Opening Balances Page *
************************************************************/
static void
delete_our_account_tree (hierarchy_data *data)
{
if (data->our_account_tree != NULL)
{
xaccAccountBeginEdit (data->our_account_tree);
xaccAccountDestroy (data->our_account_tree);
data->our_account_tree = NULL;
}
}
static Account*
clone_account (const Account* from, gnc_commodity *com)
{
Account *ret;
ret = xaccCloneAccount (from, gnc_get_current_book ());
xaccAccountSetCommodity (ret, com);
return ret;
}
struct add_group_data_struct
{
Account *to;
Account *parent;
gnc_commodity *com;
};
static void
add_groups_for_each (Account *toadd, gpointer data)
{
struct add_group_data_struct *dadata = data;
Account *foundact;
foundact = gnc_account_lookup_by_name(dadata->to, xaccAccountGetName(toadd));
if (!foundact)
{
foundact = clone_account (toadd, dadata->com);
if (dadata->to)
gnc_account_append_child (dadata->to, foundact);
else if (dadata->parent)
gnc_account_append_child (dadata->parent, foundact);
else
{
g_warning ("add_groups_for_each: no valid parent");
}
}
{
if (gnc_account_n_children(toadd) > 0)
{
struct add_group_data_struct downdata;
downdata.to = foundact;
downdata.parent = foundact;
downdata.com = dadata->com;
gnc_account_foreach_child (toadd, add_groups_for_each, &downdata);
}
}
}
static void
add_new_accounts_with_random_guids (Account *into, Account *from,
gnc_commodity *com)
{
struct add_group_data_struct data;
data.to = into;
data.parent = NULL;
data.com = com;
gnc_account_foreach_child (from, add_groups_for_each, &data);
}
static Account *
hierarchy_merge_accounts (GSList *dalist, gnc_commodity *com)
{
GSList *mark;
Account *ret = xaccMallocAccount (gnc_get_current_book ());
for (mark = dalist; mark; mark = mark->next)
{
GncExampleAccount *xea = mark->data;
add_new_accounts_with_random_guids (ret, xea->root, com);
}
return ret;
}
static gboolean
accumulate_accounts (GtkListStore *store,
GtkTreePath *path,
GtkTreeIter *iter,
GSList **list)
{
GncExampleAccount *gea;
gboolean active;
g_return_val_if_fail(GTK_IS_LIST_STORE(store), FALSE);
gtk_tree_model_get (GTK_TREE_MODEL(store), iter,
COL_CHECKED, &active,
COL_ACCOUNT, &gea,
-1);
if (active && gea)
*list = g_slist_prepend(*list, gea);
return FALSE; /* Run entire list */
}
static GSList *
get_selected_account_list (GtkTreeView *tree_view)
{
GSList *actlist = NULL;
GtkTreeModel *model;
model = gtk_tree_view_get_model (tree_view);
gtk_tree_model_foreach (model,
(GtkTreeModelForeachFunc)accumulate_accounts,
&actlist);
return actlist;
}
static void
balance_cell_data_func (GtkTreeViewColumn *tree_column,
GtkCellRenderer *cell,
GtkTreeModel *model,
GtkTreeIter *iter,
gpointer user_data)
{
Account *account;
gnc_numeric balance;
const gchar *string;
GNCPrintAmountInfo print_info;
hierarchy_data *data = (hierarchy_data *)user_data;
gboolean allow_value;
g_return_if_fail (GTK_TREE_MODEL (model));
account = gnc_tree_view_account_get_account_from_iter (model, iter);
balance = get_final_balance (data->balance_hash, account);
if (gnc_numeric_zero_p (balance))
{
string = "";
}
else
{
print_info = gnc_account_print_info (account, FALSE);
string = xaccPrintAmount (balance, print_info);
}
if (xaccAccountGetType(account) == ACCT_TYPE_EQUITY ||
xaccAccountGetType(account) == ACCT_TYPE_TRADING)
{
allow_value = FALSE;
string = _("zero");
}
else
{
GncAccountMergeDisposition disp;
disp = determine_merge_disposition(gnc_book_get_root_account(gnc_get_current_book()), account);
if (disp == GNC_ACCOUNT_MERGE_DISPOSITION_CREATE_NEW)
{
allow_value = !xaccAccountGetPlaceholder(account);
}
else
{
allow_value = FALSE;
string = _("existing account");
}
}
g_object_set (G_OBJECT (cell),
"text", string,
"editable", allow_value,
"sensitive", allow_value,
NULL);
}
static void
balance_cell_edited (GtkCellRendererText *cell,
gchar *path,
gchar *new_text,
gpointer user_data)
{
Account *account;
char *error_loc;
gnc_numeric amount;
hierarchy_data *data = (hierarchy_data *)user_data;
g_return_if_fail(data != NULL);
account = gnc_tree_view_account_get_selected_account(data->final_account_tree);
if (account == NULL)
{
g_critical("account is null");
return;
}
error_loc = NULL;
if (!gnc_exp_parser_parse (new_text, &amount, &error_loc))
{
amount = gnc_numeric_zero();
g_object_set (G_OBJECT(cell), "text", "", NULL);
}
/* Bug#348364: Emulating price-cell, we need to ensure the denominator of
* the amount is in the SCU of the account's commodity (so
* gnc-ui-util.c:is_decimal_fraction() on the remainder denom for
* fractional values will be a "decimal").
*/
{
int account_cmdty_fraction = xaccAccountGetCommoditySCU(account);
amount = gnc_numeric_convert(amount, account_cmdty_fraction, GNC_HOW_RND_ROUND_HALF_UP);
}
set_final_balance (data->balance_hash, account, amount);
qof_event_gen (QOF_INSTANCE(account), QOF_EVENT_MODIFY, NULL);
}
static void
placeholder_cell_data_func (GtkTreeViewColumn *tree_column,
GtkCellRenderer *cell,
GtkTreeModel *model,
GtkTreeIter *iter,
gpointer user_data)
{
Account *account, *root;
gboolean willbe_placeholder = FALSE;
GncAccountMergeDisposition disp;
g_return_if_fail (GTK_TREE_MODEL (model));
account = gnc_tree_view_account_get_account_from_iter (model, iter);
root = gnc_book_get_root_account(gnc_get_current_book());
disp = determine_merge_disposition(root, account);
switch (disp)
{
case GNC_ACCOUNT_MERGE_DISPOSITION_USE_EXISTING:
{
/* find the existing account, do whatever it is. */
gchar *full_name;
Account *existing_acct;
full_name = gnc_account_get_full_name(account);
existing_acct = gnc_account_lookup_by_full_name(root, full_name);
willbe_placeholder = xaccAccountGetPlaceholder(existing_acct);
g_free(full_name);
}
break;
case GNC_ACCOUNT_MERGE_DISPOSITION_CREATE_NEW:
willbe_placeholder = xaccAccountGetPlaceholder(account);
break;
}
gtk_cell_renderer_toggle_set_active(GTK_CELL_RENDERER_TOGGLE(cell), willbe_placeholder);
}
static void
use_existing_account_data_func(GtkTreeViewColumn *tree_column,
GtkCellRenderer *cell,
GtkTreeModel *tree_model,
GtkTreeIter *iter,
gpointer user_data)
{
Account *new_acct;
Account *real_root;
GncAccountMergeDisposition disposition;
char *to_user = "(error; unknown condition)";
g_return_if_fail (GTK_TREE_MODEL (tree_model));
new_acct = gnc_tree_view_account_get_account_from_iter(tree_model, iter);
if (new_acct == NULL)
{
g_object_set (G_OBJECT(cell), "text", "(null account)", NULL);
return;
}
real_root = gnc_book_get_root_account(gnc_get_current_book());
disposition = determine_merge_disposition(real_root, new_acct);
switch (disposition)
{
case GNC_ACCOUNT_MERGE_DISPOSITION_USE_EXISTING:
to_user = _("Yes");
break;
case GNC_ACCOUNT_MERGE_DISPOSITION_CREATE_NEW:
to_user = _("No");
break;
}
g_object_set(G_OBJECT(cell), "text", to_user, NULL);
}
void
on_final_account_prepare (hierarchy_data *data)
{
GSList *actlist;
GtkTreeView *tree_view;
GtkTreeSelection *selection;
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
gnc_commodity *com;
/* Anything to do? */
if (!data->category_set_changed)
return;
data->category_set_changed = FALSE;
gnc_suspend_gui_refresh ();
/* Delete any existing account tree */
if (data->final_account_tree)
{
gtk_widget_destroy(GTK_WIDGET(data->final_account_tree));
data->final_account_tree = NULL;
}
delete_our_account_tree (data);
/* Build a new account list */
actlist = get_selected_account_list (data->categories_tree);
com = gnc_currency_edit_get_currency (GNC_CURRENCY_EDIT(data->currency_selector));
data->our_account_tree = hierarchy_merge_accounts (actlist, com);
/* Now build a new account tree */
data->final_account_tree
= GNC_TREE_VIEW_ACCOUNT(gnc_tree_view_account_new_with_root (data->our_account_tree, FALSE));
tree_view = GTK_TREE_VIEW(data->final_account_tree);
gnc_tree_view_account_set_name_edited(data->final_account_tree,
gnc_tree_view_account_name_edited_cb);
gnc_tree_view_account_set_code_edited(data->final_account_tree,
gnc_tree_view_account_code_edited_cb);
gnc_tree_view_account_set_description_edited(data->final_account_tree,
gnc_tree_view_account_description_edited_cb);
gnc_tree_view_account_set_notes_edited(data->final_account_tree,
gnc_tree_view_account_notes_edited_cb);
gtk_tree_view_set_headers_visible (tree_view, TRUE);
column = gnc_tree_view_find_column_by_name (
GNC_TREE_VIEW(data->final_account_tree), "type");
g_object_set_data(G_OBJECT(column), DEFAULT_VISIBLE, GINT_TO_POINTER(1));
gnc_tree_view_configure_columns (GNC_TREE_VIEW(data->final_account_tree));
gnc_tree_view_set_show_column_menu (GNC_TREE_VIEW(data->final_account_tree),
FALSE);
selection = gtk_tree_view_get_selection (tree_view);
gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
// This is a re-definition of the placeholder that the account-tree model
// provides, reflecting the to-be-created state of the account tree
// post-merge.
{
renderer = gtk_cell_renderer_toggle_new();
g_object_set(G_OBJECT (renderer),
"activatable", FALSE,
"sensitive", FALSE,
NULL);
column = gtk_tree_view_column_new_with_attributes(_("Placeholder"),
renderer, NULL);
gtk_tree_view_column_set_cell_data_func (column, renderer,
placeholder_cell_data_func,
(gpointer)data, NULL);
gnc_tree_view_append_column (GNC_TREE_VIEW(tree_view), column);
}
{
renderer = gtk_cell_renderer_text_new ();
g_object_set (G_OBJECT (renderer),
"xalign", 1.0,
(char *)NULL);
g_signal_connect (G_OBJECT (renderer), "edited",
G_CALLBACK (balance_cell_edited),
data);
column = gtk_tree_view_column_new_with_attributes (_("Opening Balance"),
renderer,
NULL);
gtk_tree_view_column_set_cell_data_func (column, renderer,
balance_cell_data_func,
(gpointer)data, NULL);
gnc_tree_view_append_column (GNC_TREE_VIEW(tree_view), column);
}
// only in the case where there *are* existing accounts...
if (gnc_account_n_descendants(gnc_book_get_root_account(gnc_get_current_book())) > 0)
{
GList *renderers;
column = gnc_tree_view_add_text_column(GNC_TREE_VIEW(tree_view),
_("Use Existing"),
NULL,
NULL,
"yes",
GNC_TREE_VIEW_COLUMN_DATA_NONE,
GNC_TREE_VIEW_COLUMN_VISIBLE_ALWAYS,
NULL);
renderers = gtk_tree_view_column_get_cell_renderers(column);
g_object_set(G_OBJECT(renderer), "xalign", 1.0, (char*)NULL);
gtk_tree_view_column_set_cell_data_func(column, GTK_CELL_RENDERER(renderers->data),
use_existing_account_data_func, (gpointer)data, NULL);
g_list_free(renderers);
}
gtk_container_add(GTK_CONTAINER(data->final_account_tree_container),
GTK_WIDGET(data->final_account_tree));
/* Expand the entire tree */
gtk_tree_view_expand_all (tree_view);
gtk_widget_show(GTK_WIDGET(data->final_account_tree));
gnc_resume_gui_refresh ();
}
void
on_cancel (GtkAssistant *gtkassistant,
hierarchy_data *data)
{
gnc_suspend_gui_refresh ();
delete_hierarchy_dialog (data);
delete_our_account_tree (data);
gncp_new_user_finish ();
g_free(data);
gnc_resume_gui_refresh ();
}