/
druid-qif-import.c
1978 lines (1604 loc) · 65.7 KB
/
druid-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
/********************************************************************\
* druid-qif-import.c -- window for importing QIF files *
* (GnuCash) *
* Copyright (C) 2000 Bill Gribble <grib@billgribble.com> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 59 Temple Place - Suite 330 Fax: +1-617-542-2652 *
* Boston, MA 02111-1307, USA gnu@gnu.org *
\********************************************************************/
#define _GNU_SOURCE
#include "config.h"
#include <gnome.h>
#include <guile/gh.h>
#include <stdio.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-utils.h"
#include "druid-qif-import.h"
#include "druid-utils.h"
#include "global-options.h"
#include "gnc-component-manager.h"
#include "gnc-engine-util.h"
#include "gnc-file-dialog.h"
#include "gnc-gui-query.h"
#include "gnc-menu-extensions.h"
#include "gnc-ui-util.h"
#include "gnc-ui.h"
#include "messages.h"
#include "window-help.h"
#include <g-wrap-wct.h>
#define DRUID_QIF_IMPORT_CM_CLASS "druid-qif-import"
struct _qifimportwindow {
GtkWidget * window;
GtkWidget * druid;
GtkWidget * filename_entry;
GtkWidget * acct_entry;
GtkWidget * date_format_combo;
GtkWidget * date_format_entry;
GtkWidget * selected_file_list;
GtkWidget * acct_list;
GtkWidget * cat_list;
GtkWidget * memo_list;
GtkWidget * currency_picker;
GtkWidget * currency_entry;
GtkWidget * new_transaction_list;
GtkWidget * old_transaction_list;
GList * pre_comm_pages;
GList * commodity_pages;
GList * post_comm_pages;
GList * doc_pages;
gboolean show_doc_pages;
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 stock_hash;
SCM new_stocks;
SCM ticker_map;
SCM imported_account_group;
SCM match_transactions;
int selected_transaction;
};
struct _qifdruidpage {
GtkWidget * page;
GtkWidget * new_type_combo;
GtkWidget * new_name_entry;
GtkWidget * new_mnemonic_entry;
gnc_commodity * commodity;
};
typedef struct _qifdruidpage QIFDruidPage;
static QIFDruidPage * make_qif_druid_page(gnc_commodity * comm);
static void update_file_page(QIFImportWindow * win);
static void update_accounts_page(QIFImportWindow * win);
static void update_categories_page(QIFImportWindow * win);
static void update_memo_page(QIFImportWindow * win);
static void update_account_picker_page(QIFImportWindow * wind,
SCM make_display, GtkWidget *list,
SCM map_info, SCM * display_info);
static void gnc_ui_qif_import_commodity_prepare_cb(GnomeDruidPage * page,
gpointer arg1,
gpointer user_data);
static GdkColor std_bg_color = { 0, 39835, 49087, 40092 };
static GdkColor std_logo_bg_color = { 0, 65535, 65535, 65535 };
static GdkColor std_title_color = { 0, 65535, 65535, 65535 };
static char *qif_import_wd = NULL;
#define NUM_PRE_PAGES 13
#define NUM_POST_PAGES 3
#define NUM_DOC_PAGES 6
static GnomeDruidPage *
get_named_page(QIFImportWindow * w, const char * name) {
return GNOME_DRUID_PAGE(gnc_glade_lookup_widget(w->window, name));
}
/********************************************************************\
* gnc_ui_qif_import_druid_destroy
* close the QIF Import druid window
\********************************************************************/
void
gnc_ui_qif_import_druid_destroy (QIFImportWindow * window) {
if (!window)
return;
/* FIXME -- commodity pages */
gnc_unregister_gui_component_by_data(DRUID_QIF_IMPORT_CM_CLASS, window);
gtk_widget_destroy(window->window);
scm_unprotect_object(window->imported_files);
scm_unprotect_object(window->selected_file);
scm_unprotect_object(window->gnc_acct_info);
scm_unprotect_object(window->cat_display_info);
scm_unprotect_object(window->cat_map_info);
scm_unprotect_object(window->memo_display_info);
scm_unprotect_object(window->memo_map_info);
scm_unprotect_object(window->acct_display_info);
scm_unprotect_object(window->acct_map_info);
scm_unprotect_object(window->stock_hash);
scm_unprotect_object(window->new_stocks);
scm_unprotect_object(window->ticker_map);
scm_unprotect_object(window->imported_account_group);
scm_unprotect_object(window->match_transactions);
g_free(window);
}
static GtkWidget *
get_next_druid_page(QIFImportWindow * wind, GnomeDruidPage * page) {
GList * current = NULL;
GList * next;
int where = 0;
if((current = g_list_find(wind->pre_comm_pages, page)) == NULL) {
if((current = g_list_find(wind->commodity_pages, page)) == NULL) {
if((current = g_list_find(wind->post_comm_pages, page)) == NULL) {
/* where are we? */
printf("QIF import: I'm lost!\n");
return FALSE;
}
else {
where = 3;
}
}
else {
where = 2;
}
}
else {
where = 1;
}
next = current->next;
while(!next ||
(!wind->show_doc_pages &&
g_list_find(wind->doc_pages, next->data))) {
if(next && next->next) {
next = next->next;
}
else {
where ++;
switch(where) {
case 2:
next = wind->commodity_pages;
break;
case 3:
next = wind->post_comm_pages;
break;
default:
printf("QIF import: something fishy.\n");
next = NULL;
if (where > 3)
return NULL;
break;
}
}
}
if(next) return (GtkWidget *)next->data;
else return NULL;
}
static GtkWidget *
get_prev_druid_page(QIFImportWindow * wind, GnomeDruidPage * page) {
GList * current = NULL;
GList * prev;
int where = 0;
if((current = g_list_find(wind->pre_comm_pages, page)) == NULL) {
if((current = g_list_find(wind->commodity_pages, page)) == NULL) {
if((current = g_list_find(wind->post_comm_pages, page)) == NULL) {
/* where are we? */
printf("QIF import: I'm lost!\n");
return FALSE;
}
else {
where = 3;
}
}
else {
where = 2;
}
}
else {
where = 1;
}
prev = current->prev;
while(!prev ||
(!wind->show_doc_pages &&
g_list_find(wind->doc_pages, prev->data))) {
if(prev && prev->prev) {
prev = prev->prev;
}
else {
where --;
switch(where) {
case 1:
prev = g_list_last(wind->pre_comm_pages);
break;
case 2:
if(wind->new_stocks != SCM_BOOL_F) {
prev = g_list_last(wind->commodity_pages);
}
else {
prev = g_list_last(wind->pre_comm_pages);
}
break;
default:
if (wind->show_doc_pages)
printf("QIF import: something fishy.\n");
prev = NULL;
if (where < 1)
return NULL;
break;
}
}
}
if(prev)
return (GtkWidget *)prev->data;
else
return NULL;
}
/********************************************************************
* gnc_ui_qif_import_generic_next_cb
* close the QIF Import druid window
********************************************************************/
static gboolean
gnc_ui_qif_import_generic_next_cb(GnomeDruidPage * page, gpointer arg1,
gpointer user_data) {
QIFImportWindow * wind = user_data;
GtkWidget * next_page = get_next_druid_page(wind, page);
if(next_page) {
gnome_druid_set_page(GNOME_DRUID(wind->druid),
GNOME_DRUID_PAGE(next_page));
return TRUE;
}
else {
return FALSE;
}
}
/********************************************************************
* gnc_ui_qif_import_generic_back_cb
* close the QIF Import druid window
********************************************************************/
static gboolean
gnc_ui_qif_import_generic_back_cb(GnomeDruidPage * page, gpointer arg1,
gpointer user_data) {
QIFImportWindow * wind = user_data;
GtkWidget * back_page = get_prev_druid_page(wind, page);
if(back_page) {
gnome_druid_set_page(GNOME_DRUID(wind->druid),
GNOME_DRUID_PAGE(back_page));
return TRUE;
}
else {
return FALSE;
}
}
/********************************************************************
* gnc_ui_qif_import_select_file_cb
* invoked when the "select file" button is clicked
* this is just to pick a file name and reset-to-defaults all the
* fields describing how to parse the file.
********************************************************************/
static void
gnc_ui_qif_import_select_file_cb(GtkButton * button,
gpointer user_data) {
QIFImportWindow * wind = user_data;
const char * new_file_name;
char *file_name, *tmp;
/* Default to whatever's already present */
tmp = gtk_entry_get_text(GTK_ENTRY(wind->filename_entry));
new_file_name = gnc_file_dialog (_("Select QIF File"), "*.qif", tmp);
/* Insure valid data, and something that can be freed. */
if (new_file_name == NULL)
file_name = g_strdup(qif_import_wd);
else if (*new_file_name != '/')
file_name = g_strdup_printf("%s%s", qif_import_wd, new_file_name);
else
file_name = g_strdup(new_file_name);
/* set the filename entry for what was selected */
gtk_entry_set_text(GTK_ENTRY(wind->filename_entry), file_name);
/* Update the working directory */
tmp = rindex(file_name, '/');
if (tmp != NULL) {
*(tmp+1) = '\0';
g_free(qif_import_wd);
qif_import_wd = g_strdup(file_name);
}
g_free(file_name);
}
/********************************************************************
* gnc_ui_qif_import_load_file_next_cb
*
* Invoked when the "next" button is clicked on the load file page.
********************************************************************/
static gboolean
gnc_ui_qif_import_load_file_next_cb(GnomeDruidPage * page,
gpointer arg1,
gpointer user_data) {
QIFImportWindow * wind = user_data;
char * path_to_load;
char * default_acctname = NULL;
GList * format_strings;
GList * listit;
SCM make_qif_file = gh_eval_str("make-qif-file");
SCM qif_file_load = gh_eval_str("qif-file:read-file");
SCM qif_file_parse = gh_eval_str("qif-file:parse-fields");
SCM qif_file_loaded = gh_eval_str("qif-dialog:qif-file-loaded?");
SCM unload_qif_file = gh_eval_str("qif-dialog:unload-qif-file");
SCM check_from_acct = gh_eval_str("qif-file:check-from-acct");
SCM default_acct = gh_eval_str("qif-file:path-to-accountname");
SCM date_formats;
SCM scm_filename;
SCM scm_qiffile;
SCM imported_files = SCM_EOL;
SCM load_return, parse_return;
int ask_date_format = FALSE;
/* get the file name */
path_to_load = gtk_entry_get_text(GTK_ENTRY(wind->filename_entry));
/* check a few error conditions before we get started */
if(strlen(path_to_load) == 0) {
/* stay here if no file specified */
gnc_error_dialog_parented(GTK_WINDOW(wind->window),
_("Please select a file to load.\n"));
return TRUE;
}
else if ((strlen(path_to_load) > 0) && access(path_to_load, R_OK) < 0) {
/* stay here if bad file */
gnc_error_dialog_parented(GTK_WINDOW(wind->window),
_("File not found or read permission denied.\n"
"Please select another file."));
return TRUE;
}
else {
/* convert filename to scm */
scm_filename = gh_str02scm(path_to_load);
imported_files = wind->imported_files;
if(gh_call2(qif_file_loaded, scm_filename, wind->imported_files)
== SCM_BOOL_T) {
gnc_error_dialog_parented(GTK_WINDOW(wind->window),
_("That QIF file is already loaded.\n"
"Please select another file."));
return TRUE;
}
/* turn on the busy cursor */
gnc_set_busy_cursor(NULL, TRUE);
/* create the <qif-file> object */
scm_qiffile = gh_call0(make_qif_file);
imported_files = gh_cons(scm_qiffile, imported_files);
scm_unprotect_object(wind->selected_file);
wind->selected_file = scm_qiffile;
scm_protect_object(wind->selected_file);
/* load the file */
load_return = gh_call3(qif_file_load, gh_car(imported_files),
scm_filename, wind->ticker_map);
/* a list returned is (#f error-message) for an error,
* (#t error-message) for a warning, or just #f for an
* exception. */
if(gh_list_p(load_return) &&
(gh_car(load_return) == SCM_BOOL_T)) {
char *warn_str = gh_scm2newstr(gh_cadr(load_return), NULL);
gnc_warning_dialog_parented(GTK_WIDGET(wind->window),
_("QIF file load warning:\n%s"),
warn_str ? warn_str : "(null)");
free (warn_str);
}
/* check success of the file load */
if(load_return == SCM_BOOL_F) {
gnc_error_dialog_parented(GTK_WINDOW(wind->window),
_( "An error occurred while "
"loading the QIF file."));
return TRUE;
}
else if ((load_return != SCM_BOOL_T) &&
(!gh_list_p(load_return) ||
(gh_car(load_return) != SCM_BOOL_T))) {
char *warn_str = gh_scm2newstr(gh_cadr(load_return), NULL);
gnc_error_dialog_parented(GTK_WINDOW(wind->window),
_("QIF file load failed:\n%s"),
warn_str ? warn_str : "(null)");
free (warn_str);
imported_files =
gh_call2(unload_qif_file, scm_qiffile, imported_files);
scm_unprotect_object(wind->imported_files);
wind->imported_files = imported_files;
scm_protect_object(wind->imported_files);
return TRUE;
}
else {
/* call the field parser */
parse_return = gh_call1(qif_file_parse, gh_car(imported_files));
/* warning means the date format is ambiguous. Set up the format
* selector page. FIXME: this can return warnings for things
* other than date format ambiguities. */
if(gh_list_p(parse_return) &&
(gh_car(parse_return) == SCM_BOOL_T)) {
date_formats = gh_cadr(parse_return);
format_strings = NULL;
while(gh_list_p(date_formats) && !gh_null_p(date_formats)) {
format_strings =
g_list_append(format_strings,
gh_symbol2newstr(gh_car(date_formats), NULL));
date_formats = gh_cdr(date_formats);
}
gtk_combo_set_popdown_strings(GTK_COMBO(wind->date_format_combo),
format_strings);
for(listit = format_strings; listit; listit=listit->next) {
free(listit->data);
listit->data = NULL;
}
g_list_free(format_strings);
ask_date_format = TRUE;
}
if(parse_return == SCM_BOOL_F) {
gnc_error_dialog_parented(GTK_WINDOW(wind->window),
_("An error occurred while parsing the "
"QIF file."));
imported_files =
gh_call2(unload_qif_file, scm_qiffile, imported_files);
return TRUE;
}
else if((parse_return != SCM_BOOL_T) &&
(!gh_list_p(parse_return) ||
(gh_car(parse_return) != SCM_BOOL_T))) {
char *warn_str = gh_scm2newstr(gh_cadr(parse_return), NULL);
gnc_error_dialog_parented(GTK_WINDOW(wind->window),
_("QIF file parse failed:\n%s"),
warn_str ? warn_str : "(null)");
free(warn_str);
imported_files =
gh_call2(unload_qif_file, scm_qiffile, imported_files);
return TRUE;
}
}
scm_unprotect_object(wind->imported_files);
wind->imported_files = imported_files;
scm_protect_object(wind->imported_files);
/* turn back the cursor */
gnc_unset_busy_cursor(NULL);
/* we're leaving the page, so clear the entry text */
gtk_entry_set_text(GTK_ENTRY(wind->filename_entry), qif_import_wd);
if(ask_date_format) {
/* we need to get a date format, so go to the next page */
return gnc_ui_qif_import_generic_next_cb(page, arg1, wind);
}
else if(gh_call1(check_from_acct, gh_car(imported_files)) != SCM_BOOL_T) {
/* skip to the "ask account name" page */
default_acctname = gh_scm2newstr(gh_call1(default_acct,
gh_car(imported_files)),
NULL);
gtk_entry_set_text(GTK_ENTRY(wind->acct_entry), default_acctname);
if(default_acctname) free(default_acctname);
gnome_druid_set_page(GNOME_DRUID(wind->druid),
get_named_page(wind, "account_name_page"));
return TRUE;
}
else {
/* skip ahead to the "loaded files" page */
gnome_druid_set_page(GNOME_DRUID(wind->druid),
get_named_page(wind, "loaded_files_page"));
return TRUE;
}
}
return FALSE;
}
static gboolean
gnc_ui_qif_import_date_format_next_cb(GnomeDruidPage * page,
gpointer arg1,
gpointer user_data) {
QIFImportWindow * wind = user_data;
SCM reparse_dates = gh_eval_str("qif-file:reparse-dates");
SCM check_from_acct = gh_eval_str("qif-file:check-from-acct");
SCM format_sym =
gh_symbol2scm(gtk_entry_get_text(GTK_ENTRY(wind->date_format_entry)));
gh_call2(reparse_dates, wind->selected_file, format_sym);
if(gh_call1(check_from_acct, wind->selected_file) != SCM_BOOL_T) {
SCM default_acct = gh_eval_str("qif-file:path-to-accountname");
char * default_acctname;
default_acctname = gh_scm2newstr(gh_call1(default_acct,
wind->selected_file),
NULL);
gtk_entry_set_text(GTK_ENTRY(wind->acct_entry), default_acctname);
if(default_acctname) free(default_acctname);
return FALSE;
}
else {
/* skip ahead to the "loaded files" page */
gnome_druid_set_page(GNOME_DRUID(wind->druid),
get_named_page(wind, "loaded_files_page"));
return TRUE;
}
}
/****************************************************************
* 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(GtkCList * list,
int row, int column,
GdkEvent * event,
gpointer user_data) {
QIFImportWindow * wind = user_data;
if(gh_list_p(wind->imported_files) &&
(gh_length(wind->imported_files) > row)) {
scm_unprotect_object(wind->selected_file);
wind->selected_file = gh_list_ref(wind->imported_files,
gh_int2scm(row));
scm_protect_object(wind->selected_file);
}
}
/********************************************************************
* gnc_ui_qif_import_loaded_files_prepare_cb
*
* Get the loaded files page ready for viewing
********************************************************************/
static void
gnc_ui_qif_import_loaded_files_prepare_cb(GnomeDruidPage * page,
gpointer arg1,
gpointer user_data) {
QIFImportWindow * wind = user_data;
update_file_page(wind);
gnome_druid_set_buttons_sensitive(GNOME_DRUID(wind->druid),
FALSE, TRUE, TRUE);
}
/********************************************************************
* gnc_ui_qif_import_load_another_cb
* Invoked when the "load another" button is clicked on the loaded
* files page.
********************************************************************/
static void
gnc_ui_qif_import_load_another_cb(GtkButton * button,
gpointer user_data) {
QIFImportWindow * wind = user_data;
gnome_druid_set_page(GNOME_DRUID(wind->druid),
get_named_page(wind, "load_file_page"));
gnome_druid_set_buttons_sensitive(GNOME_DRUID(wind->druid),
TRUE, TRUE, TRUE);
}
/********************************************************************
* gnc_ui_qif_import_unload_cb
* Invoked when the "unload" button is clicked on the loaded files
* page.
********************************************************************/
static void
gnc_ui_qif_import_unload_file_cb(GtkButton * button,
gpointer user_data) {
QIFImportWindow * wind = user_data;
SCM unload_qif_file = gh_eval_str("qif-dialog:unload-qif-file");
SCM imported_files;
if(wind->selected_file != SCM_BOOL_F) {
imported_files =
gh_call2(unload_qif_file, wind->selected_file, wind->imported_files);
scm_unprotect_object(wind->imported_files);
wind->imported_files = imported_files;
scm_protect_object(wind->imported_files);
scm_unprotect_object(wind->selected_file);
wind->selected_file = SCM_BOOL_F;
scm_protect_object(wind->selected_file);
update_file_page(wind);
}
}
/********************************************************************
* update_file_page
* update the list of loaded files
********************************************************************/
static void
update_file_page(QIFImportWindow * wind) {
SCM loaded_file_list = wind->imported_files;
SCM scm_qiffile = SCM_BOOL_F;
SCM qif_file_path;
int row;
int sel_item=-1;
char * row_text;
/* clear the list */
gtk_clist_clear(GTK_CLIST(wind->selected_file_list));
qif_file_path = gh_eval_str("qif-file:path");
/* iterate over all the imported files */
gtk_clist_freeze(GTK_CLIST(wind->selected_file_list));
while(!gh_null_p(loaded_file_list)) {
scm_qiffile = gh_car(loaded_file_list);
row_text = gh_scm2newstr(gh_call1(qif_file_path, scm_qiffile), NULL);
row = gtk_clist_append(GTK_CLIST(wind->selected_file_list),
&row_text);
free (row_text);
if(scm_qiffile == wind->selected_file) {
sel_item = row;
}
loaded_file_list = gh_cdr(loaded_file_list);
}
gtk_clist_thaw(GTK_CLIST(wind->selected_file_list));
if(sel_item >= 0) {
gtk_clist_select_row(GTK_CLIST(wind->selected_file_list), sel_item, 0);
}
/* Wheee! Look at me! I'm a little one-line hack to make stuff work! */
gtk_widget_queue_resize (wind->window);
}
/********************************************************************
* gnc_ui_qif_import_default_acct_next_cb
*
* Invoked when the "next" button is clicked on the default acct page.
********************************************************************/
static gboolean
gnc_ui_qif_import_default_acct_next_cb(GnomeDruidPage * page,
gpointer arg1,
gpointer user_data) {
QIFImportWindow * wind = user_data;
char * acct_name = gtk_entry_get_text(GTK_ENTRY(wind->acct_entry));
SCM fix_default = gh_eval_str("qif-import:fix-from-acct");
SCM scm_name;
if(!acct_name || acct_name[0] == 0) {
gnc_warning_dialog_parented(wind->window,
_("You must enter an account name."));
return TRUE;
}
else {
scm_name = gh_str02scm(acct_name);
gh_call2(fix_default, wind->selected_file, scm_name);
return FALSE;
}
}
/********************************************************************
* gnc_ui_qif_import_default_acct_back_cb
*
* Invoked when the "back" button is clicked on the default acct page.
* this unloads the current file.
********************************************************************/
static gboolean
gnc_ui_qif_import_default_acct_back_cb(GnomeDruidPage * page,
gpointer arg1,
gpointer user_data) {
QIFImportWindow * wind = user_data;
SCM unload = gh_eval_str("qif-dialog:unload-qif-file");
SCM files_list;
files_list = gh_call2(unload, wind->selected_file, wind->imported_files);
scm_unprotect_object(wind->imported_files);
wind->imported_files = files_list;
scm_protect_object(wind->imported_files);
scm_unprotect_object(wind->selected_file);
wind->selected_file = SCM_BOOL_F;
scm_protect_object(wind->selected_file);
gnome_druid_set_page(GNOME_DRUID(wind->druid),
get_named_page(wind, "load_file_page"));
gnome_druid_set_buttons_sensitive(GNOME_DRUID(wind->druid),
TRUE, TRUE, TRUE);
return TRUE;
}
/****************************************************************
* 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 CLIST. It asks the Scheme side to guess some account
* translations and then shows the account name and suggested
* translation in the Accounts page clist (acount picker list).
****************************************************************/
static void
update_account_picker_page(QIFImportWindow * wind, SCM make_display,
GtkWidget *list, SCM map_info, SCM * display_info) {
SCM get_qif_name = gh_eval_str("qif-map-entry:qif-name");
SCM get_gnc_name = gh_eval_str("qif-map-entry:gnc-name");
SCM get_new = gh_eval_str("qif-map-entry:new-acct?");
SCM accts_left;
int sel_row=0;
char * row_text[3];
int row;
/* get the old selection row */
sel_row = (GTK_CLIST(list))->focus_row;
/* now get the list of strings to display in the clist widget */
accts_left = gh_call3(make_display,
wind->imported_files,
map_info,
wind->gnc_acct_info);
scm_unprotect_object(*display_info);
*display_info = accts_left;
scm_protect_object(*display_info);
gtk_clist_column_titles_passive (GTK_CLIST(list));
/* clear the list */
gtk_clist_clear(GTK_CLIST(list));
/* update the text in the boxes */
gtk_clist_freeze(GTK_CLIST(list));
gtk_clist_set_column_justification(GTK_CLIST(list),
2,
GTK_JUSTIFY_CENTER);
row_text[2] = "";
while(!gh_null_p(accts_left)) {
row_text[0] = gh_scm2newstr(gh_call1(get_qif_name, gh_car(accts_left)),
NULL);
row_text[1] = gh_scm2newstr(gh_call1(get_gnc_name, gh_car(accts_left)),
NULL);
row = gtk_clist_append(GTK_CLIST(list), row_text);
gnc_clist_set_check (GTK_CLIST(list), row, 2,
gh_call1(get_new, gh_car(accts_left)) == SCM_BOOL_T);
accts_left = gh_cdr(accts_left);
free(row_text[0]);
free(row_text[1]);
}
gtk_clist_thaw(GTK_CLIST(list));
/* move to the old selected row */
(GTK_CLIST(list))->focus_row = sel_row;
gtk_clist_moveto(GTK_CLIST(list), sel_row, 0, 0.0, 0.0);
}
/****************************************************************
* update_accounts_page
* update the QIF account -> GNC Account picker
****************************************************************/
static void
update_accounts_page(QIFImportWindow * wind) {
SCM make_account_display = gh_eval_str("qif-dialog:make-account-display");
update_account_picker_page (wind, make_account_display, wind->acct_list,
wind->acct_map_info, &(wind->acct_display_info));
}
/****************************************************************
* update_categories_page
* update the QIF category -> GNC Account picker
****************************************************************/
static void
update_categories_page(QIFImportWindow * wind) {
SCM make_category_display = gh_eval_str("qif-dialog:make-category-display");
update_account_picker_page (wind, make_category_display, wind->cat_list,
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 = gh_eval_str("qif-dialog:make-memo-display");
update_account_picker_page (wind, make_memo_display, wind->memo_list,
wind->memo_map_info, &(wind->memo_display_info));
}
/********************************************************************
* select_line
* generic function to process the selection when a user tries to edit
* an account mapping in one of the "map QIF * to GNC" pages. This
* calls out to the account picker, and then then updates the
* appropriate data structures. Finally, it will call the update_page
* function.
********************************************************************/
static void
select_line (QIFImportWindow *wind, gint row, SCM display_info, SCM map_info,
void (*update_page)(QIFImportWindow *)) {
SCM get_name = gh_eval_str("qif-map-entry:qif-name");
SCM selected_acct;
/* find the <qif-map-entry> corresponding to the selected row */
selected_acct = gh_list_ref(display_info, gh_int2scm(row));
/* call the account picker to update it */
selected_acct = qif_account_picker_dialog(wind, selected_acct);
scm_hash_set_x(map_info, gh_call1(get_name, selected_acct), selected_acct);
/* update display */
update_page(wind);
}
/********************************************************************
* gnc_ui_qif_import_account_line_select_cb
* when an account is clicked for editing in the "map QIF accts to GNC"
* page.
********************************************************************/
static void
gnc_ui_qif_import_account_line_select_cb(GtkCList * clist, gint row,
gint column, GdkEvent * event,
gpointer user_data) {
QIFImportWindow * wind = user_data;
select_line (wind, row, wind->acct_display_info, wind->acct_map_info,
update_accounts_page);
}
/********************************************************************
* gnc_ui_qif_import_category_line_select_cb
* when a cat is clicked for editing in the "map QIF cats to GNC"
* page.
********************************************************************/
static void
gnc_ui_qif_import_category_line_select_cb(GtkCList * clist, gint row,
gint column, GdkEvent * event,
gpointer user_data) {
QIFImportWindow * wind = user_data;
select_line (wind, row, wind->cat_display_info, wind->cat_map_info,
update_categories_page);
}
/********************************************************************
* gnc_ui_qif_import_memo_line_select_cb
* when a memo is clicked for editing in the "map QIF memos to GNC"
* page.
********************************************************************/
static void
gnc_ui_qif_import_memo_line_select_cb(GtkCList * clist, gint row,
gint column, GdkEvent * event,
gpointer user_data) {
QIFImportWindow * wind = user_data;
select_line (wind, row, wind->memo_display_info, wind->memo_map_info,
update_memo_page);
}