/
assistant-qif-import.c
3897 lines (3245 loc) · 143 KB
/
assistant-qif-import.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-qif-import.c -- window for importing QIF files *
* (GnuCash) *
* Copyright (C) 2000 Bill Gribble <grib@billgribble.com> *
* Copyright (c) 2006 David Hampton <hampton@employees.org> *
* Copyright (c) 2011 Robert Fewell *
* *
* 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 <platform.h>
#include <libguile.h>
#if PLATFORM(WINDOWS)
#include <windows.h>
#endif
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <libguile.h>
#include <sys/time.h>
#include <unistd.h>
#include "Account.h"
#include "Transaction.h"
#include "dialog-account-picker.h"
#include "dialog-commodity.h"
#include "dialog-progress.h"
#include "dialog-utils.h"
#include "dialog-file-access.h"
#include "assistant-qif-import.h"
#include "gnc-component-manager.h"
#include "qof.h"
#include "gnc-file.h"
#include "gnc-gui-query.h"
#include "gnc-guile-utils.h"
#include "gnc-currency-edit.h"
#include "gnc-ui-util.h"
#include "gnc-gtk-utils.h"
#include "gnc-main-window.h"
#include "gnc-plugin-page-account-tree.h"
#include "gnc-prefs.h"
#include "gnc-ui.h"
#include "swig-runtime.h"
#include "guile-mappings.h"
#include <gfec.h>
#define ASSISTANT_QIF_IMPORT_CM_CLASS "assistant-qif-import"
#define GNC_PREFS_GROUP "dialogs.import.qif"
#define GNC_PREF_SHOW_DOC "show-doc"
#define GNC_PREF_DEFAULT_TRANS_STATUS_CLEARED "default-status-cleared"
#define GNC_PREF_DEFAULT_TRANS_STATUS_NOTCLEARED "default-status-notcleared"
#define GNC_PREF_DEFAULT_TRANS_STATUS_RECONCILED "default-status-reconciled"
#define PREV_ROW "prev_row"
static QofLogModule log_module = GNC_MOD_ASSISTANT;
enum filename_cols
{
FILENAME_COL_INDEX = 0,
FILENAME_COL_NAME,
NUM_FILENAME_COLS
};
enum account_cols
{
ACCOUNT_COL_INDEX = 0,
ACCOUNT_COL_QIF_NAME,
ACCOUNT_COL_GNC_NAME,
ACCOUNT_COL_NEW,
ACCOUNT_COL_ELLIPSIZE,
NUM_ACCOUNT_COLS
};
/* to simplify sorting and hence use the default sort function
* we store the date as an int64 and convert the gnc_numeric
* to a double which can be stored in the liststore.
*/
enum qif_trans_cols
{
QIF_TRANS_COL_INDEX = 0,
QIF_TRANS_COL_DATE,
QIF_TRANS_COL_DATE_INT64, // used only for sorting
QIF_TRANS_COL_DESCRIPTION,
QIF_TRANS_COL_AMOUNT,
QIF_TRANS_COL_AMOUNT_DOUBLE, // used only for sorting
QIF_TRANS_COL_CHECKED,
NUM_QIF_TRANS_COLS
};
struct _qifimportwindow
{
GtkWidget * window;
GtkWidget * assistant;
/* Widgets on the file selection page. */
GtkWidget * filename_entry;
/* File loading progress page. */
GtkWidget * load_pause;
GtkWidget * load_start;
GtkWidget * load_log;
GNCProgressDialog *load_progress;
/* Widgets on the default account page. */
GtkWidget * acct_entry;
/* Widgets on the date format page. */
GtkWidget * date_format_combo;
/* Widgets on the files loaded page. */
GtkWidget * selected_file_view;
GtkWidget * unload_file_btn;
/* Widgets on the account matching page. */
GtkWidget * acct_view;
GtkWidget * acct_view_count;
GtkWidget * acct_view_btn;
/* Widgets on the category matching page. */
GtkWidget * cat_view;
GtkWidget * cat_view_count;
GtkWidget * cat_view_btn;
/* Widgets on the memo matching page. */
GtkWidget * memo_view;
GtkWidget * memo_view_count;
GtkWidget * memo_view_btn;
/* Widgets on the currency & book options page. */
GtkWidget * currency_picker;
GtkWidget * book_option_label;
GtkWidget * book_option_message;
/* Widgets on the commodity page. */
gint num_new_pages;
GtkWidget * commodity_notebook;
GList * commodity_notebook_pages;
gint timeout_id;
/* Conversion progress page. */
GtkWidget * convert_pause;
GtkWidget * convert_start;
GtkWidget * convert_log;
GNCProgressDialog *convert_progress;
/* Widgets on the duplicates page. */
GtkWidget * new_transaction_view;
GtkWidget * old_transaction_view;
/* Widgets on the summary page. */
GtkWidget * summary_text;
gboolean show_doc_pages;
gboolean ask_date_format;
gboolean busy;
gboolean load_stop;
gboolean acct_tree_found;
gboolean new_book;
SCM imported_files;
SCM selected_file;
SCM acct_map_info;
SCM acct_display_info;
SCM cat_map_info;
SCM cat_display_info;
SCM memo_map_info;
SCM memo_display_info;
SCM gnc_acct_info;
SCM security_hash;
SCM security_prefs;
SCM new_securities;
GList * new_namespaces;
SCM ticker_map;
SCM imported_account_tree;
SCM match_transactions;
SCM transaction_status;
int selected_transaction;
gchar *date_format;
};
struct _qifnotebookpage
{
GtkWidget *notebook_page;
GtkWidget *namespace_combo;
GtkWidget *name_entry;
GtkWidget *mnemonic_entry;
gnc_commodity *commodity;
gboolean page_complete;
SCM hash_key;
};
typedef struct _qifnotebookpage QIFCommNotebookPage;
static void gnc_ui_qif_import_assistant_destroy (GtkWidget *object, gpointer user_data);
static void gnc_ui_qif_import_assistant_close_handler (gpointer user_data);
void gnc_ui_qif_import_cancel_cb (GtkAssistant *gtkassistant, gpointer user_data);
void gnc_ui_qif_import_prepare_cb (GtkAssistant *assistant, GtkWidget *page, gpointer user_data);
void gnc_ui_qif_import_finish_cb (GtkAssistant *gtkassistant, gpointer user_data);
void gnc_ui_qif_import_close_cb (GtkAssistant *gtkassistant, gpointer user_data);
void gnc_ui_qif_import_intro_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_load_file_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_select_file_cb (GtkButton *button, gpointer user_data);
void gnc_ui_qif_import_load_progress_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_load_progress_pause_cb (GtkButton *button, gpointer user_data);
void gnc_ui_qif_import_load_progress_start_cb (GtkButton * button, gpointer user_data);
void gnc_ui_qif_import_date_format_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_date_valid_cb (GtkWidget *widget, gpointer user_data);
void gnc_ui_qif_import_account_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_acct_valid_cb (GtkWidget *widget, gpointer user_data);
void gnc_ui_qif_import_acct_enter_cb (GtkWidget * widget, gpointer user_data);
void gnc_ui_qif_import_loaded_files_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_load_another_cb (GtkButton *button, gpointer user_data);
void gnc_ui_qif_import_unload_file_cb (GtkButton *button, gpointer user_data);
static void update_file_page (QIFImportWindow * wind);
void gnc_ui_qif_import_account_match_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_account_doc_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_account_rematch_cb (GtkButton *button, gpointer user_data);
void gnc_ui_qif_import_catagory_match_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_catagory_doc_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_category_rematch_cb (GtkButton *button, gpointer user_data);
void gnc_ui_qif_import_memo_match_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_memo_doc_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_memo_rematch_cb (GtkButton *button, gpointer user_data);
void gnc_ui_qif_import_currency_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_commodity_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_comm_changed_cb (GtkWidget *widget, gpointer user_data);
void gnc_ui_qif_import_comm_namespace_changed_cb (GtkWidget *widget, gpointer user_data);
void gnc_ui_qif_import_convert_progress_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_convert_progress_pause_cb (GtkButton * button, gpointer user_data);
void gnc_ui_qif_import_convert_progress_start_cb (GtkButton * button, gpointer user_data);
void gnc_ui_qif_import_duplicates_match_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_duplicates_doc_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_end_page_prepare (GtkAssistant *assistant, gpointer user_data);
void gnc_ui_qif_import_summary_page_prepare (GtkAssistant *assistant, gpointer user_data);
/****************************************************************
* update_account_picker_page
*
* Generic function to update an account_picker page. This
* generalizes the code shared whenever any QIF -> GNC mapper is
* updating it's LIST STORE. It asks the Scheme side to guess some account
* translations and then shows the account name and suggested
* translation in the Accounts page view (acount picker list).
****************************************************************/
static void
update_account_picker_page (QIFImportWindow * wind, SCM make_display,
GtkWidget *view, SCM map_info, SCM * display_info)
{
SCM get_qif_name = scm_c_eval_string ("qif-map-entry:qif-name");
SCM get_gnc_name = scm_c_eval_string ("qif-map-entry:gnc-name");
SCM get_new = scm_c_eval_string ("qif-map-entry:new-acct?");
SCM accts_left;
gchar *qif_name = NULL;
gchar *gnc_name = NULL;
gboolean checked;
gint row = 0;
gint prev_row;
GtkListStore *store;
GtkTreeIter iter;
GtkTreePath *path;
GtkTreeSelection *selection;
store = GTK_LIST_STORE(gtk_tree_view_get_model (GTK_TREE_VIEW(view)));
/* now get the list of strings to display in the gtk_list_store widget */
accts_left = scm_call_3 (make_display,
wind->imported_files,
map_info,
wind->gnc_acct_info);
scm_gc_unprotect_object (*display_info);
*display_info = accts_left;
scm_gc_protect_object (*display_info);
/* clear the list */
gtk_list_store_clear (store);
while (!scm_is_null (accts_left))
{
qif_name = gnc_scm_call_1_to_string (get_qif_name, SCM_CAR(accts_left));
gnc_name = gnc_scm_call_1_to_string (get_gnc_name, SCM_CAR(accts_left));
checked = (scm_call_1 (get_new, SCM_CAR(accts_left)) == SCM_BOOL_T);
gtk_list_store_append (store, &iter);
gtk_list_store_set (store, &iter,
ACCOUNT_COL_INDEX, row++,
ACCOUNT_COL_QIF_NAME, qif_name,
ACCOUNT_COL_GNC_NAME, gnc_name,
ACCOUNT_COL_NEW, checked,
ACCOUNT_COL_ELLIPSIZE, PANGO_ELLIPSIZE_START,
-1);
accts_left = SCM_CDR(accts_left);
g_free (qif_name);
g_free (gnc_name);
}
/* move to the old selected row */
prev_row = GPOINTER_TO_INT(g_object_get_data (G_OBJECT(store), PREV_ROW));
selection = gtk_tree_view_get_selection (GTK_TREE_VIEW(view));
if (prev_row != -1)
path = gtk_tree_path_new_from_indices (prev_row, -1);
else
path = gtk_tree_path_new_from_indices (0, -1);
gtk_tree_selection_select_path (selection, path);
/* scroll the tree view so the selection is visable if there are rows */
if (gtk_tree_model_iter_n_children (GTK_TREE_MODEL(store), NULL) > 0)
gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW(view), path, NULL, TRUE, 0.5, 0.0);
gtk_tree_path_free (path);
}
/****************************************************************
* update_account_page
*
* update the QIF account -> GNC Account picker
****************************************************************/
static void
update_account_page (QIFImportWindow * wind)
{
SCM make_account_display = scm_c_eval_string ("qif-dialog:make-account-display");
update_account_picker_page (wind, make_account_display, wind->acct_view,
wind->acct_map_info, &(wind->acct_display_info));
}
/****************************************************************
* update_category_page
*
* update the QIF category -> GNC Account picker
****************************************************************/
static void
update_category_page (QIFImportWindow * wind)
{
SCM make_category_display = scm_c_eval_string ("qif-dialog:make-category-display");
update_account_picker_page (wind, make_category_display, wind->cat_view,
wind->cat_map_info, &(wind->cat_display_info));
}
/****************************************************************
* update_memo_page
*
* update the QIF memo -> GNC Account picker
****************************************************************/
static void
update_memo_page (QIFImportWindow * wind)
{
SCM make_memo_display = scm_c_eval_string ("qif-dialog:make-memo-display");
update_account_picker_page (wind, make_memo_display, wind->memo_view,
wind->memo_map_info, &(wind->memo_display_info));
}
/****************************************************************
* gnc_ui_qif_import_commodity_destroy
*
* This function destroys any commodity pages.
****************************************************************/
static void
gnc_ui_qif_import_commodity_destroy (QIFImportWindow * wind)
{
GList *pageptr;
GtkWidget *notebook_page;
QIFCommNotebookPage *comm_nb_page;
for (pageptr = wind->commodity_notebook_pages; pageptr; pageptr = pageptr->next)
{
notebook_page = pageptr->data;
comm_nb_page = g_object_get_data (G_OBJECT(notebook_page), "page_struct");
/* Unprotect the Scheme hash key. */
scm_gc_unprotect_object (comm_nb_page->hash_key);
/* Free the memory allocated for the page's struct. */
g_free (comm_nb_page);
}
/* Free the list of pages. */
g_list_free (wind->commodity_notebook_pages);
wind->commodity_notebook_pages = NULL;
}
/**********************************************
* gnc_ui_qif_import_assistant_destroy
* close the QIF Import assistant window
**********************************************/
static void
gnc_ui_qif_import_assistant_destroy (GtkWidget *object, gpointer user_data)
{
QIFImportWindow * wind = user_data;
/* Destroy the progress dialog helpers. */
gnc_progress_dialog_destroy (wind->load_progress);
/* Destroy any commodity pages. */
gnc_ui_qif_import_commodity_destroy (wind);
gnc_unregister_gui_component_by_data (ASSISTANT_QIF_IMPORT_CM_CLASS, wind);
gtk_widget_destroy (wind->window);
scm_gc_unprotect_object (wind->imported_files);
scm_gc_unprotect_object (wind->selected_file);
scm_gc_unprotect_object (wind->gnc_acct_info);
scm_gc_unprotect_object (wind->cat_display_info);
scm_gc_unprotect_object (wind->cat_map_info);
scm_gc_unprotect_object (wind->memo_display_info);
scm_gc_unprotect_object (wind->memo_map_info);
scm_gc_unprotect_object (wind->acct_display_info);
scm_gc_unprotect_object (wind->acct_map_info);
scm_gc_unprotect_object (wind->security_hash);
scm_gc_unprotect_object (wind->security_prefs);
scm_gc_unprotect_object (wind->new_securities);
scm_gc_unprotect_object (wind->ticker_map);
scm_gc_unprotect_object (wind->imported_account_tree);
scm_gc_unprotect_object (wind->match_transactions);
g_free (wind);
}
/****************************************************************
* gnc_ui_qif_import_select_loaded_file_cb
* callback when a file is clicked in the "loaded files" page
****************************************************************/
static void
gnc_ui_qif_import_select_loaded_file_cb (GtkTreeSelection *selection,
gpointer user_data)
{
QIFImportWindow * wind = user_data;
GtkTreeModel *model;
GtkTreeIter iter;
gint row;
GtkWidget *button;
button = (wind->unload_file_btn);
if (gtk_tree_selection_get_selected (selection, &model, &iter))
{
gtk_tree_model_get (model, &iter, FILENAME_COL_INDEX, &row, -1);
if (scm_is_list (wind->imported_files) &&
(scm_ilength (wind->imported_files) > row))
{
scm_gc_unprotect_object (wind->selected_file);
wind->selected_file = scm_list_ref (wind->imported_files,
scm_from_int (row));
scm_gc_protect_object (wind->selected_file);
g_object_set (button, "sensitive", TRUE, (gchar*)NULL);
}
}
else
{
scm_gc_unprotect_object (wind->selected_file);
wind->selected_file = SCM_BOOL_F;
scm_gc_protect_object (wind->selected_file);
g_object_set (button, "sensitive", FALSE, (gchar*)NULL);
}
}
/****************************************************
* create_account_picker_view
****************************************************/
static void
create_account_picker_view (GtkWidget *widget,
const gchar *col_name,
GCallback activate_cb,
GCallback select_cb,
gpointer user_data)
{
GtkTreeView *view = GTK_TREE_VIEW(widget);
GtkTreeSelection *selection = gtk_tree_view_get_selection (view);
GtkListStore *store;
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
store = gtk_list_store_new (NUM_ACCOUNT_COLS, G_TYPE_INT, G_TYPE_STRING,
G_TYPE_STRING, G_TYPE_BOOLEAN,
PANGO_TYPE_ELLIPSIZE_MODE);
gtk_tree_view_set_model (view, GTK_TREE_MODEL(store));
/* prevent the rows being dragged to a different order */
gtk_tree_view_set_reorderable (view, FALSE);
/* default sort order */
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE(store),
ACCOUNT_COL_QIF_NAME,
GTK_SORT_ASCENDING);
g_object_unref (store);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes (col_name,
renderer,
"text",
ACCOUNT_COL_QIF_NAME,
"ellipsize",
ACCOUNT_COL_ELLIPSIZE,
NULL);
g_object_set (G_OBJECT(column), "expand", TRUE, "reorderable",
TRUE, "resizable", TRUE, NULL);
gtk_tree_view_append_column (view, column);
gtk_tree_view_column_set_sort_column_id (column, ACCOUNT_COL_QIF_NAME);
renderer = gtk_cell_renderer_text_new ();
column = gtk_tree_view_column_new_with_attributes (_("GnuCash account name"),
renderer,
"text",
ACCOUNT_COL_GNC_NAME,
"ellipsize",
ACCOUNT_COL_ELLIPSIZE,
NULL);
g_object_set (G_OBJECT(column), "expand", TRUE, "reorderable",
TRUE, "resizable", TRUE, NULL);
gtk_tree_view_append_column (view, column);
gtk_tree_view_column_set_sort_column_id (column, ACCOUNT_COL_GNC_NAME);
renderer = gtk_cell_renderer_toggle_new ();
g_object_set(renderer, "activatable", FALSE, NULL);
column = gtk_tree_view_column_new_with_attributes (_("New?"),
renderer,
"active",
ACCOUNT_COL_NEW,
NULL);
gtk_tree_view_append_column (view, column);
g_object_set_data (G_OBJECT(store), PREV_ROW, GINT_TO_POINTER(-1));
/* Connect the signal handlers. */
g_signal_connect (view, "row-activated", G_CALLBACK(activate_cb), user_data);
g_signal_connect (selection, "changed", G_CALLBACK(select_cb), user_data);
/* Allow multiple rows to be selected. */
gtk_tree_selection_set_mode (selection, GTK_SELECTION_MULTIPLE);
}
/********************************************************************
* rematch_line
*
* This is a helper function for tree controls used by some assistant
* pages for mapping QIF values to GnuCash accounts. It processes
* the selected rows when a user tries to edit the account mappings.
* The account picker is displayed, and the chosen GnuCash account
* becomes the new mapping for each row. Finally, the update_page
* function is called.
********************************************************************/
static void
rematch_line (QIFImportWindow *wind, GtkTreeSelection *selection,
SCM display_info, SCM map_info,
void (*update_page)(QIFImportWindow *))
{
SCM get_qif_name = scm_c_eval_string ("qif-map-entry:qif-name");
SCM get_gnc_name = scm_c_eval_string ("qif-map-entry:gnc-name");
SCM set_gnc_name = scm_c_eval_string ("qif-map-entry:set-gnc-name!");
SCM map_entry;
SCM gnc_name;
GList *pathlist;
GList *current;
GtkTreeModel *model;
GtkTreeIter iter;
gint row;
/* Get a list of selected rows. */
pathlist = gtk_tree_selection_get_selected_rows (selection, &model);
if (!pathlist)
return;
/*
* Update the first selected row.
*/
/* Get the row number of the first selected row. */
if (!gtk_tree_model_get_iter (model, &iter, (GtkTreePath *) pathlist->data))
return;
gtk_tree_model_get (model, &iter, ACCOUNT_COL_INDEX, &row, -1);
/* Save the row number. */
g_object_set_data (G_OBJECT(model), PREV_ROW, GINT_TO_POINTER(row));
if (row == -1)
return;
/* Find the <qif-map-entry> corresponding to the selected row. */
map_entry = scm_list_ref (display_info, scm_from_int (row));
/* Call the account picker to update it. */
if (!qif_account_picker_dialog (GTK_WINDOW(wind->window), wind, map_entry))
return;
gnc_name = scm_call_1 (get_gnc_name, map_entry);
/* Update the mapping hash table. */
scm_hash_set_x (map_info, scm_call_1 (get_qif_name, map_entry), map_entry);
/*
* Map all other selected rows to the same GnuCash account.
*/
for (current = pathlist->next; current; current = current->next)
{
/* Get the row number. */
gtk_tree_model_get_iter (model, &iter, (GtkTreePath *) current->data);
gtk_tree_model_get (model, &iter, ACCOUNT_COL_INDEX, &row, -1);
/* Update the <qif-map-entry> for the selected row. */
map_entry = scm_list_ref (display_info, scm_from_int (row));
scm_call_2 (set_gnc_name, map_entry, gnc_name);
/* Update the mapping hash table. */
scm_hash_set_x (map_info, scm_call_1 (get_qif_name, map_entry), map_entry);
}
/* Free the path list. */
g_list_foreach (pathlist, (GFunc) gtk_tree_path_free, NULL);
g_list_free (pathlist);
/* Update the display. */
update_page (wind);
}
/********************************************************************
* gnc_ui_qif_import_account_activate_cb
*
* This handler is invoked when a row is double-clicked in the "Match
* QIF accounts to GnuCash accounts" page.
********************************************************************/
static void
gnc_ui_qif_import_account_activate_cb (GtkTreeView *view, GtkTreePath *path,
GtkTreeViewColumn *column,
gpointer user_data)
{
QIFImportWindow *wind = user_data;
g_return_if_fail (wind);
rematch_line (wind, gtk_tree_view_get_selection (view),
wind->acct_display_info, wind->acct_map_info,
update_account_page);
}
/********************************************************************
* gnc_ui_qif_import_account_select_cb
*
* This handler is invoked when the selection of account matchings
* has changed. It updates the selection count and enables/disables
* the "Change" button.
********************************************************************/
static void
gnc_ui_qif_import_account_select_cb (GtkTreeSelection *selection,
gpointer user_data)
{
QIFImportWindow *wind = user_data;
gint count = gtk_tree_selection_count_selected_rows (selection);
gchar *count_str;
g_return_if_fail (wind);
/* Update the "items selected" count. */
if (wind->acct_view_count)
{
count_str = g_strdup_printf ("%d", count);
gtk_label_set_text (GTK_LABEL(wind->acct_view_count), count_str);
g_free (count_str);
}
/* Enable/disable the Change button. */
if (wind->acct_view_btn)
{
if (count)
gtk_widget_set_sensitive (wind->acct_view_btn, TRUE);
else
gtk_widget_set_sensitive (wind->acct_view_btn, FALSE);
}
}
/********************************************************************
* gnc_ui_qif_import_category_activate_cb
*
* This handler is invoked when a row is double-clicked in the "Match
* QIF categories to GnuCash accounts" page.
********************************************************************/
static void
gnc_ui_qif_import_category_activate_cb (GtkTreeView *view, GtkTreePath *path,
GtkTreeViewColumn *column,
gpointer user_data)
{
QIFImportWindow *wind = user_data;
GtkTreeSelection *selection;
g_return_if_fail (view && wind);
selection = gtk_tree_view_get_selection (view);
rematch_line (wind, selection, wind->cat_display_info, wind->cat_map_info,
update_category_page);
}
/********************************************************************
* gnc_ui_qif_import_category_select_cb
*
* This handler is invoked when the selection of category matchings
* has changed. It updates the selection count and enables/disables
* the "Change" button.
********************************************************************/
static void
gnc_ui_qif_import_category_select_cb (GtkTreeSelection *selection,
gpointer user_data)
{
QIFImportWindow *wind = user_data;
gint count = gtk_tree_selection_count_selected_rows (selection);
gchar *count_str;
g_return_if_fail (wind);
/* Update the "items selected" count. */
if (wind->cat_view_count)
{
count_str = g_strdup_printf ("%d", count);
gtk_label_set_text (GTK_LABEL(wind->cat_view_count), count_str);
g_free (count_str);
}
/* Enable/disable the Change button. */
if (wind->cat_view_btn)
{
if (count)
gtk_widget_set_sensitive (wind->cat_view_btn, TRUE);
else
gtk_widget_set_sensitive (wind->cat_view_btn, FALSE);
}
}
/********************************************************************
* gnc_ui_qif_import_memo_activate_cb
*
* This handler is invoked when a row is double-clicked in the "Match
* QIF payee/memo to GnuCash accounts" page.
********************************************************************/
static void
gnc_ui_qif_import_memo_activate_cb (GtkTreeView *view, GtkTreePath *path,
GtkTreeViewColumn *column,
gpointer user_data)
{
QIFImportWindow *wind = user_data;
GtkTreeSelection *selection;
g_return_if_fail (view && wind);
selection = gtk_tree_view_get_selection (view);
rematch_line (wind, selection, wind->memo_display_info, wind->memo_map_info,
update_memo_page);
}
/********************************************************************
* gnc_ui_qif_import_memo_select_cb
*
* This handler is invoked when the selection of memo matchings
* has changed. It updates the selection count and enables/disables
* the "Change" button.
********************************************************************/
static void
gnc_ui_qif_import_memo_select_cb (GtkTreeSelection *selection,
gpointer user_data)
{
QIFImportWindow *wind = user_data;
gint count = gtk_tree_selection_count_selected_rows (selection);
gchar *count_str;
g_return_if_fail (wind);
/* Update the "items selected" count. */
if (wind->memo_view_count)
{
count_str = g_strdup_printf ("%d", count);
gtk_label_set_text (GTK_LABEL(wind->memo_view_count), count_str);
g_free (count_str);
}
/* Enable/disable the Change button. */
if (wind->memo_view_btn)
{
if (count)
gtk_widget_set_sensitive (wind->memo_view_btn, TRUE);
else
gtk_widget_set_sensitive (wind->memo_view_btn, FALSE);
}
}
/*********************************************
* new_security_notebook_page
*********************************************/
static QIFCommNotebookPage *
new_security_notebook_page (SCM security_hash_key, gnc_commodity *comm, QIFImportWindow *wind)
{
QIFCommNotebookPage *comm_nb_page = g_new0(QIFCommNotebookPage, 1);
GtkListStore *store;
GtkWidget *table;
GtkWidget *label;
gchar *title = NULL;
const char *str;
GtkWidget *notebook_page;
GtkWidget *notebook_label;
GtkWidget *entry;
char *name_tooltip =
_("Enter a name or short description, such as \"Red Hat Stock\".");
char *mnemonic_tooltip =
_("Enter the ticker symbol or other well known abbreviation, such as"
" \"RHT\". If there isn't one, or you don't know it, create your own.");
char *namespace_tooltip =
_("Select the exchange on which the symbol is traded, or select the"
" type of investment (such as FUND for mutual funds.) If you don't"
" see your exchange or an appropriate investment type, you can"
" enter a new one.");
/* Make the page widget. */
notebook_page = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_box_set_homogeneous (GTK_BOX (notebook_page), FALSE);
comm_nb_page->notebook_page = notebook_page;
g_object_set_data (G_OBJECT(notebook_page), "page_struct", comm_nb_page);
/* Save the commodity and the hash table key. */
comm_nb_page->commodity = comm;
comm_nb_page->hash_key = security_hash_key;
scm_gc_protect_object (comm_nb_page->hash_key);
/* Set the page title. */
str = gnc_commodity_get_mnemonic (comm);
str = str ? str : "";
title = g_strdup_printf ("\"%s\"", str);
/* Insert the new notebook page */
notebook_label = gtk_label_new (title);
gnc_label_set_alignment (notebook_label, 0.0, 0.5);
gtk_notebook_append_page (GTK_NOTEBOOK(wind->commodity_notebook),
notebook_page, notebook_label);
g_free (title);
/* set the page complete flag as on creation all fields will be OK */
comm_nb_page->page_complete = TRUE;
/* Add all the widgets to the page. */
table = gtk_grid_new ();
gtk_grid_set_row_spacing (GTK_GRID(table), 6);
gtk_grid_set_column_spacing (GTK_GRID(table), 12);
/* Name entry */
comm_nb_page->name_entry = gtk_entry_new ();
gtk_entry_set_text (GTK_ENTRY(comm_nb_page->name_entry),
gnc_commodity_get_fullname (comm));
label = gtk_label_new_with_mnemonic (_("_Name or description:"));
gtk_label_set_mnemonic_widget (GTK_LABEL(label), comm_nb_page->name_entry);
gnc_label_set_alignment (label, 0, 0.5);
gtk_widget_set_tooltip_text (label, name_tooltip);
gtk_widget_set_tooltip_text (comm_nb_page->name_entry, name_tooltip);
gtk_grid_attach (GTK_GRID(table), label, 0, 0, 1, 1);
gtk_widget_set_halign (label, GTK_ALIGN_FILL);
gtk_widget_set_valign (label, GTK_ALIGN_FILL);
gtk_widget_set_hexpand (label, TRUE);
gtk_widget_set_vexpand (label, FALSE);
g_object_set (label, "margin", 0, NULL);
gtk_grid_attach (GTK_GRID(table), comm_nb_page->name_entry, 1, 0, 1, 1);
g_signal_connect (comm_nb_page->name_entry, "changed",
G_CALLBACK(gnc_ui_qif_import_comm_changed_cb), wind);
/* Mnemonic entry */
comm_nb_page->mnemonic_entry = gtk_entry_new ();
gtk_entry_set_text (GTK_ENTRY(comm_nb_page->mnemonic_entry),
gnc_commodity_get_mnemonic (comm));
label = gtk_label_new_with_mnemonic (
_("_Ticker symbol or other abbreviation:"));
gtk_label_set_mnemonic_widget (GTK_LABEL(label), comm_nb_page->mnemonic_entry);
gnc_label_set_alignment (label, 0, 0.5);
gtk_widget_set_tooltip_text (label, mnemonic_tooltip);
gtk_widget_set_tooltip_text (comm_nb_page->mnemonic_entry, mnemonic_tooltip);
gtk_grid_attach (GTK_GRID(table), label, 0, 1, 1, 1);
gtk_widget_set_halign (label, GTK_ALIGN_FILL);
gtk_widget_set_valign (label, GTK_ALIGN_FILL);
gtk_widget_set_hexpand (label, TRUE);
gtk_widget_set_vexpand (label, FALSE);
g_object_set (label, "margin", 0, NULL);
gtk_grid_attach (GTK_GRID(table), comm_nb_page->mnemonic_entry, 1, 1, 1, 1);
g_signal_connect (comm_nb_page->mnemonic_entry, "changed",
G_CALLBACK(gnc_ui_qif_import_comm_changed_cb), wind);
/* Namespace entry */
store = gtk_list_store_new (1, G_TYPE_STRING);
comm_nb_page->namespace_combo = gtk_combo_box_new_with_model_and_entry (GTK_TREE_MODEL(store));
g_object_unref (store);
entry = gtk_bin_get_child (GTK_BIN(comm_nb_page->namespace_combo));
gtk_widget_set_events (GTK_WIDGET(entry), GDK_FOCUS_CHANGE_MASK);
g_signal_connect (G_OBJECT (entry), "changed",
G_CALLBACK(gnc_ui_qif_import_comm_namespace_changed_cb), wind);
/* Set the column for the text */
gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX(comm_nb_page->namespace_combo), 0);
gnc_cbwe_add_completion (GTK_COMBO_BOX(comm_nb_page->namespace_combo));
label = gtk_label_new_with_mnemonic (
_("_Exchange or abbreviation type:"));
gtk_label_set_mnemonic_widget (GTK_LABEL(label), comm_nb_page->namespace_combo);
gnc_label_set_alignment (label, 0, 0.5);
gtk_widget_set_tooltip_text (label, namespace_tooltip);
gtk_widget_set_tooltip_text (comm_nb_page->namespace_combo, namespace_tooltip);
gtk_grid_attach (GTK_GRID(table), label, 0, 2, 1, 1);
gtk_widget_set_halign (label, GTK_ALIGN_FILL);
gtk_widget_set_valign (label, GTK_ALIGN_FILL);
gtk_widget_set_hexpand (label, TRUE);
gtk_widget_set_vexpand (label, FALSE);
g_object_set (label, "margin", 0, NULL);
gtk_grid_attach (GTK_GRID(table), comm_nb_page->namespace_combo, 1, 2, 1, 1);
gtk_container_set_border_width (GTK_CONTAINER(notebook_page), 12);
gtk_box_pack_start (GTK_BOX(notebook_page), table, FALSE, FALSE, 12);
gtk_widget_show_all (GTK_WIDGET(wind->commodity_notebook));
return comm_nb_page;
}
/********************************************************************
* prepare_security_pages
*
* Prepare the assistant page for each security.
********************************************************************/
static void
prepare_security_pages (QIFImportWindow * wind)
{
SCM hash_ref = scm_c_eval_string ("hash-ref");
SCM securities;
SCM comm_ptr_token;
GList * current;
gnc_commodity * commodity;
QIFCommNotebookPage * new_comm_nb_page;