/
assistant-csv-trans-import.cpp
2207 lines (1898 loc) · 86.4 KB
/
assistant-csv-trans-import.cpp
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-csv-trans-import.c -- An assistant for importing *
* Transactions from a file. *
* *
* Copyright (C) 2012 Robert Fewell *
* Copyright (c) 2007 Benny Sperisen <lasindi@gmail.com> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation; either version 2 of *
* the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License*
* along with this program; if not, contact: *
* *
* Free Software Foundation Voice: +1-617-542-5942 *
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
* Boston, MA 02110-1301, USA gnu@gnu.org *
\********************************************************************/
/** @file assistant-csv-trans-import.cpp
@brief CSV Import Assistant
@author Copyright (c) 2012 Robert Fewell
@author Copyright (c) 2016 Geert Janssens
*/
#include <guid.hpp>
extern "C"
{
#include <config.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include <stdlib.h>
#include "gnc-path.h"
#include "gnc-ui.h"
#include "gnc-uri-utils.h"
#include "gnc-ui-util.h"
#include "dialog-utils.h"
#include "gnc-component-manager.h"
#include "gnc-state.h"
#include "assistant-csv-trans-import.h"
#include "import-account-matcher.h"
#include "import-main-matcher.h"
#include "gnc-csv-account-map.h"
#include "gnc-account-sel.h"
#include "gnc-csv-gnumeric-popup.h"
#include "go-charmap-sel.h"
}
#include "gnc-imp-settings-csv-tx.hpp"
#include "gnc-import-tx.hpp"
#include "gnc-tokenizer-fw.hpp"
#include "gnc-tokenizer-csv.hpp"
#include <algorithm>
#include <exception>
#include <iostream>
#include <memory>
#include <string>
#include <tuple>
#include <gnc-locale-utils.hpp>
#include <boost/locale.hpp>
namespace bl = boost::locale;
#define MIN_COL_WIDTH 70
#define GNC_PREFS_GROUP "dialogs.import.csv"
#define ASSISTANT_CSV_IMPORT_TRANS_CM_CLASS "assistant-csv-trans-import"
/* This static indicates the debugging module that this .o belongs to. */
static QofLogModule log_module = GNC_MOD_ASSISTANT;
/* A note on memory management
*
* This source file is mixture of several completely different memory models
* - it defines a c++ class which is managed the c++ way
* - the c++ class encapsulates a gtk based assistant which is managed according to
* the GObject/Gtk memory model.
* - gnucash manages gui objects via its "Component Manager". Dialogs and windows are
* registered with this component manager when they are opened and the component
* manager handles the lifecycle of toplevel widgets. When a dialog is closed
* the component manager invokes a close handler which is responsible for cleaning up.
*
* Care must be taken in places where these models intersect. Here is how it is
* handled for this source file:
* - First in the context of the import assistant the gnucash component manager is
* merely a wrapper to keep track of which gui objects exist so they can be cleanly
* destroyed. But the component manager itself doesn't do any memory management
* on the objects it tracks. Instead it delegates this back to the objects themselves
* via callbacks which the objects have to supply. It merely helps in the coordination.
* So we can further ignore it in the memory management discussion.
*
* - Next there is only one entry point to start this assistant: gnc_file_csv_trans_import
* - The full assistant functionality is wrapped in a C++ class using RAII.
* - The entry point function will create one instance of this class using the c++
* "new" method. This in turn will create several objects like a (GObject managed)
* GtkAssistant, and a few C++ member objects.
* - The entry point function will also register the created object in the
* component manager. This works because the (plain C) component manager just stores
* the (C++) pointer to the object, it doesn't act on it directly in any way.
* - When the assistant is closed the component manager will invoke a
* close handler on the class pointer. We supply this close handler ourselves
* in csv_tximp_close_handler. Aside from some component management administration
* the essential action of this function is to (c++) "delete"
* the class object again. As the C++ class implements RAII this destruction will take care
* of freeing all the member objects it manages.
* - Note the component manager's only benefit in this context is that at gnucash shutdown
* all still open dialogs can be closed cleanly. Whether this benefit is enough to
* justify the added complexity is debatable. However currently the calling code is not
* c++ yet so we can't use RAII in the calling object to better handle this right now.
*
* - Let's zoom in on the c++ member objects and in particular the GtkAssistant and related objects.
* These are created the gtk way in the c++ class constructor. That means the main GtkAssistant widget
* will be responsible for the lifecycle of its child widgets.
* - Thanks to the RAII implementation the destruction of this widget is commanded in the c++ class
* destructor. This gets activated when the user clicks the assistant's close button via the component
* manager callback mechanism as mentioned above.
*
* - There is one case that needs some additional attention. At some point the csv importer assistant hands
* over control to a generic import matcher (created via gnc_gen_trans_assist_new). This generic import
* matcher unfortunately destroys itself when run. However it is not run in all our possible user scenarios.
* This means we sometimes have to free it and sometimes we don't. This could have been
* avoided if we didn't have to track the object across several gtk callback functions and
* instead just create it only right before using it. To handle this we start with RAII:
* the c++ class object assumes ownership of the generic import matcher object and the class destructor will
* attempt to free it. This is safe both if the object is effectively allocated or when it's nullified.
* Then to handle the case were the generic import matcher will free the matcher object, the c++ class object
* will release ownership of the generic pointer object right before starting the generic import matcher.
*
* TODO this is pretty confusing and should be cleaned up when we rewrite the generic importer.
*/
class CsvImpTransAssist
{
public:
CsvImpTransAssist ();
~CsvImpTransAssist ();
/* Delete copy and move constructor/assignments
* We don't want gui elements to be moved around or copied at all */
CsvImpTransAssist(const CsvImpTransAssist&) = delete; // copy constructor
CsvImpTransAssist& operator=(const CsvImpTransAssist&) = delete; // copy assignment
CsvImpTransAssist(CsvImpTransAssist&&) = delete; // move constructor
CsvImpTransAssist& operator=(CsvImpTransAssist&&) = delete; // move assignment
void assist_prepare_cb (GtkWidget *page);
void assist_file_page_prepare ();
void assist_preview_page_prepare ();
void assist_account_match_page_prepare ();
void assist_doc_page_prepare ();
void assist_match_page_prepare ();
void assist_summary_page_prepare ();
void assist_finish ();
void assist_compmgr_close ();
void file_activated_cb ();
void file_selection_changed_cb ();
void preview_settings_delete ();
void preview_settings_save ();
void preview_settings_name (GtkEntry* entry);
void preview_settings_load ();
void preview_update_skipped_rows ();
void preview_multi_split (bool multi);
void preview_update_separators (GtkWidget* widget);
void preview_update_file_format ();
void preview_update_account ();
void preview_update_encoding (const char* encoding);
void preview_update_date_format ();
void preview_update_currency_format ();
void preview_update_col_type (GtkComboBox* cbox);
void preview_update_fw_columns (GtkTreeView* treeview, GdkEventButton* event);
void preview_populate_settings_combo();
void preview_handle_save_del_sensitivity (GtkComboBox* combo);
void preview_split_column (int col, int offset);
void preview_refresh_table ();
void preview_refresh ();
void preview_validate_settings ();
void acct_match_via_button ();
bool acct_match_via_view_dblclick (GdkEventButton *event);
void acct_match_select(GtkTreeModel *model, GtkTreeIter* iter);
void acct_match_set_accounts ();
friend gboolean
fixed_context_menu_handler (GnumericPopupMenuElement const *element,
gpointer userdata);
private:
/* helper functions to manage the context menu for fixed with columns */
uint32_t get_new_col_rel_pos (GtkTreeViewColumn *tcol, int dx);
void fixed_context_menu (GdkEventButton *event, int col, int dx);
/* helper function to calculate row colors for the preview table (to visualize status) */
void preview_row_fill_state_cells (GtkListStore *store, GtkTreeIter *iter,
std::string& err_msg, bool skip);
/* helper function to create preview header cell combo boxes listing available column types */
GtkWidget* preview_cbox_factory (GtkTreeModel* model, uint32_t colnum);
/* helper function to set rendering parameters for preview data columns */
void preview_style_column (uint32_t col_num, GtkTreeModel* model);
/* helper function to check for a valid filename as opposed to a directory */
bool check_for_valid_filename ();
GtkAssistant *csv_imp_asst;
GtkWidget *file_page; /**< Assistant file page widget */
GtkWidget *file_chooser; /**< The widget for the file chooser */
std::string m_file_name; /**< The import file name */
GtkWidget *preview_page; /**< Assistant preview page widget */
GtkComboBox *settings_combo; /**< The Settings Combo */
GtkWidget *save_button; /**< The Save Settings button */
GtkWidget *del_button; /**< The Delete Settings button */
GtkWidget *acct_selector; /**< The Account selector */
GtkWidget *combo_hbox; /**< The Settings Combo hbox */
GtkSpinButton *start_row_spin; /**< The widget for the start row spinner */
GtkSpinButton *end_row_spin; /**< The widget for the end row spinner */
GtkWidget *skip_alt_rows_button; /**< The widget for Skip alternate rows from start row */
GtkWidget *skip_errors_button; /**< The widget for Skip error rows*/
GtkWidget *csv_button; /**< The widget for the CSV button */
GtkWidget *fixed_button; /**< The widget for the Fixed Width button */
GtkWidget *multi_split_cbutton; /**< The widget for Multi-split */
GOCharmapSel *encselector; /**< The widget for selecting the encoding */
GtkWidget *separator_table; /**< Container for the separator checkboxes */
GtkCheckButton *sep_button[SEP_NUM_OF_TYPES]; /**< Checkbuttons for common separators */
GtkWidget *fw_instructions_hbox; /**< Container for fixed-width instructions */
GtkCheckButton *custom_cbutton; /**< The checkbutton for a custom separator */
GtkEntry *custom_entry; /**< The entry for custom separators */
GtkComboBoxText *date_format_combo; /**< The Combo Text widget for selecting the date format */
GtkComboBoxText *currency_format_combo; /**< The Combo Text widget for selecting the currency format */
GtkTreeView *treeview; /**< The treeview containing the data */
GtkLabel *instructions_label; /**< The instructions label */
GtkImage *instructions_image; /**< The instructions image */
bool encoding_selected_called; /**< Before encoding_selected is first called, this is false.
* error lines, instead of all the file data. */
int fixed_context_col; /**< The number of the column the user has clicked */
int fixed_context_offset; /**< The offset (in characters) in the column
* the user has clicked */
GtkWidget *account_match_page; /**< Assistant account matcher page widget */
GtkWidget *account_match_view; /**< Assistant account matcher view widget */
GtkWidget *account_match_label; /**< Assistant account matcher label widget */
GtkWidget *account_match_btn; /**< Assistant account matcher button widget */
GtkWidget *doc_page; /**< Assistant doc page widget */
GtkWidget *match_page; /**< Assistant match page widget, to be packed with the transaction matcher */
GtkWidget *match_label; /**< The match label at the bottom of the page */
GNCImportMainMatcher *gnc_csv_importer_gui; /**< The GNCImportMainMatcher structure */
GtkWidget *help_button; /**< The widget for the help button on the matcher page */
GtkWidget *cancel_button; /**< The widget for the new cancel button when going back is blocked */
GtkWidget *summary_page; /**< Assistant summary page widget */
GtkWidget *summary_label; /**< The summary text */
bool new_book; /**< Are we importing into a new book?; if yes, call book options */
std::unique_ptr<GncTxImport> tx_imp; /**< The actual data we are previewing */
};
/*******************************************************
* Assistant call back functions
*******************************************************/
extern "C"
{
void csv_tximp_assist_prepare_cb (GtkAssistant *assistant, GtkWidget *page, CsvImpTransAssist* info);
void csv_tximp_assist_close_cb (GtkAssistant *gtkassistant, CsvImpTransAssist* info);
void csv_tximp_assist_finish_cb (GtkAssistant *gtkassistant, CsvImpTransAssist* info);
void csv_tximp_file_activated_cb (GtkFileChooser *chooser, CsvImpTransAssist *info);
void csv_tximp_file_selection_changed_cb (GtkFileChooser *chooser, CsvImpTransAssist *info);
void csv_tximp_preview_del_settings_cb (GtkWidget *button, CsvImpTransAssist *info);
void csv_tximp_preview_save_settings_cb (GtkWidget *button, CsvImpTransAssist *info);
void csv_tximp_preview_settings_sel_changed_cb (GtkComboBox *combo, CsvImpTransAssist *info);
void csv_tximp_preview_settings_text_inserted_cb (GtkEditable *entry, gchar *new_text,
gint new_text_length, gint *position, CsvImpTransAssist *info);
void csv_tximp_preview_settings_text_changed_cb (GtkEntry *entry, CsvImpTransAssist *info);
void csv_tximp_preview_srow_cb (GtkSpinButton *spin, CsvImpTransAssist *info);
void csv_tximp_preview_erow_cb (GtkSpinButton *spin, CsvImpTransAssist *info);
void csv_tximp_preview_skiprows_cb (GtkToggleButton *checkbox, CsvImpTransAssist *info);
void csv_tximp_preview_skiperrors_cb (GtkToggleButton *checkbox, CsvImpTransAssist *info);
void csv_tximp_preview_multisplit_cb (GtkToggleButton *checkbox, CsvImpTransAssist *info);
void csv_tximp_preview_sep_button_cb (GtkWidget* widget, CsvImpTransAssist* info);
void csv_tximp_preview_sep_fixed_sel_cb (GtkToggleButton* csv_button, CsvImpTransAssist* info);
void csv_tximp_preview_acct_sel_cb (GtkWidget* widget, CsvImpTransAssist* info);
void csv_tximp_preview_enc_sel_cb (GOCharmapSel* selector, const char* encoding,
CsvImpTransAssist* info);
void csv_tximp_acct_match_button_clicked_cb (GtkWidget *widget, CsvImpTransAssist* info);
bool csv_tximp_acct_match_view_clicked_cb (GtkWidget *widget, GdkEventButton *event, CsvImpTransAssist* info);
}
void
csv_tximp_assist_prepare_cb (GtkAssistant *assistant, GtkWidget *page,
CsvImpTransAssist* info)
{
info->assist_prepare_cb(page);
}
void
csv_tximp_assist_close_cb (GtkAssistant *assistant, CsvImpTransAssist* info)
{
gnc_close_gui_component_by_data (ASSISTANT_CSV_IMPORT_TRANS_CM_CLASS, info);
}
void
csv_tximp_assist_finish_cb (GtkAssistant *assistant, CsvImpTransAssist* info)
{
info->assist_finish ();
}
void csv_tximp_file_activated_cb (GtkFileChooser *chooser, CsvImpTransAssist *info)
{
info->file_activated_cb();
}
void csv_tximp_file_selection_changed_cb (GtkFileChooser *chooser, CsvImpTransAssist *info)
{
info->file_selection_changed_cb();
}
void csv_tximp_preview_del_settings_cb (GtkWidget *button, CsvImpTransAssist *info)
{
info->preview_settings_delete();
}
void csv_tximp_preview_save_settings_cb (GtkWidget *button, CsvImpTransAssist *info)
{
info->preview_settings_save();
}
void csv_tximp_preview_settings_sel_changed_cb (GtkComboBox *combo, CsvImpTransAssist *info)
{
info->preview_settings_load();
}
void
csv_tximp_preview_settings_text_inserted_cb (GtkEditable *entry, gchar *new_text,
gint new_text_length, gint *position, CsvImpTransAssist *info)
{
if (!new_text)
return;
/* Prevent entering [], which are invalid characters in key files */
auto base_txt = std::string (new_text);
auto mod_txt = base_txt;
std::replace (mod_txt.begin(), mod_txt.end(), '[', '(');
std::replace (mod_txt.begin(), mod_txt.end(), ']', ')');
if (base_txt == mod_txt)
return;
g_signal_handlers_block_by_func (entry, (gpointer) csv_tximp_preview_settings_text_inserted_cb, info);
gtk_editable_insert_text (entry, mod_txt.c_str(), mod_txt.size() , position);
g_signal_handlers_unblock_by_func (entry, (gpointer) csv_tximp_preview_settings_text_inserted_cb, info);
g_signal_stop_emission_by_name (entry, "insert_text");
}
void
csv_tximp_preview_settings_text_changed_cb (GtkEntry *entry, CsvImpTransAssist *info)
{
info->preview_settings_name(entry);
}
void csv_tximp_preview_srow_cb (GtkSpinButton *spin, CsvImpTransAssist *info)
{
info->preview_update_skipped_rows();
}
void csv_tximp_preview_erow_cb (GtkSpinButton *spin, CsvImpTransAssist *info)
{
info->preview_update_skipped_rows();
}
void csv_tximp_preview_skiprows_cb (GtkToggleButton *checkbox, CsvImpTransAssist *info)
{
info->preview_update_skipped_rows();
}
void csv_tximp_preview_skiperrors_cb (GtkToggleButton *checkbox, CsvImpTransAssist *info)
{
info->preview_update_skipped_rows();
}
void csv_tximp_preview_multisplit_cb (GtkToggleButton *checkbox, CsvImpTransAssist *info)
{
info->preview_multi_split (gtk_toggle_button_get_active (checkbox));
}
void csv_tximp_preview_sep_button_cb (GtkWidget* widget, CsvImpTransAssist* info)
{
info->preview_update_separators(widget);
}
void csv_tximp_preview_sep_fixed_sel_cb (GtkToggleButton* csv_button, CsvImpTransAssist* info)
{
info->preview_update_file_format();
}
void csv_tximp_preview_acct_sel_cb (GtkWidget* widget, CsvImpTransAssist* info)
{
info->preview_update_account();
}
void csv_tximp_preview_enc_sel_cb (GOCharmapSel* selector, const char* encoding,
CsvImpTransAssist* info)
{
info->preview_update_encoding(encoding);
}
static void csv_tximp_preview_date_fmt_sel_cb (GtkComboBox* format_selector, CsvImpTransAssist* info)
{
info->preview_update_date_format();
}
static void csv_tximp_preview_currency_fmt_sel_cb (GtkComboBox* format_selector, CsvImpTransAssist* info)
{
info->preview_update_currency_format();
}
static void csv_tximp_preview_col_type_changed_cb (GtkComboBox* cbox, CsvImpTransAssist* info)
{
info->preview_update_col_type (cbox);
}
static bool
csv_tximp_preview_treeview_clicked_cb (GtkTreeView* treeview, GdkEventButton* event,
CsvImpTransAssist* info)
{
info->preview_update_fw_columns(treeview, event);
return false;
}
void csv_tximp_acct_match_button_clicked_cb (GtkWidget *widget, CsvImpTransAssist* info)
{
info->acct_match_via_button();
}
bool csv_tximp_acct_match_view_clicked_cb (GtkWidget *widget, GdkEventButton *event, CsvImpTransAssist* info)
{
return info->acct_match_via_view_dblclick(event);
}
/*******************************************************
* Assistant Constructor
*******************************************************/
CsvImpTransAssist::CsvImpTransAssist ()
{
auto builder = gtk_builder_new();
gnc_builder_add_from_file (builder , "assistant-csv-trans-import.glade", "start_row_adj");
gnc_builder_add_from_file (builder , "assistant-csv-trans-import.glade", "end_row_adj");
gnc_builder_add_from_file (builder , "assistant-csv-trans-import.glade", "account_match_store");
gnc_builder_add_from_file (builder , "assistant-csv-trans-import.glade", "csv_transaction_assistant");
csv_imp_asst = GTK_ASSISTANT(gtk_builder_get_object (builder, "csv_transaction_assistant"));
// Set the name for this assistant so it can be easily manipulated with css
gtk_widget_set_name (GTK_WIDGET(csv_imp_asst), "gnc-id-assistant-csv-transaction-import");
gnc_widget_style_context_add_class (GTK_WIDGET(csv_imp_asst), "gnc-class-imports");
/* Enable buttons on all page. */
gtk_assistant_set_page_complete (csv_imp_asst,
GTK_WIDGET(gtk_builder_get_object (builder, "start_page")),
true);
gtk_assistant_set_page_complete (csv_imp_asst,
GTK_WIDGET(gtk_builder_get_object (builder, "file_page")),
false);
gtk_assistant_set_page_complete (csv_imp_asst,
GTK_WIDGET(gtk_builder_get_object (builder, "preview_page")),
false);
gtk_assistant_set_page_complete (csv_imp_asst,
GTK_WIDGET(gtk_builder_get_object (builder, "account_match_page")),
false);
gtk_assistant_set_page_complete (csv_imp_asst,
GTK_WIDGET(gtk_builder_get_object (builder, "doc_page")),
true);
gtk_assistant_set_page_complete (csv_imp_asst,
GTK_WIDGET(gtk_builder_get_object (builder, "match_page")),
true);
gtk_assistant_set_page_complete (csv_imp_asst,
GTK_WIDGET(gtk_builder_get_object (builder, "summary_page")),
true);
/* File chooser Page */
file_page = GTK_WIDGET(gtk_builder_get_object (builder, "file_page"));
file_chooser = gtk_file_chooser_widget_new (GTK_FILE_CHOOSER_ACTION_OPEN);
g_signal_connect (G_OBJECT(file_chooser), "selection-changed",
G_CALLBACK(csv_tximp_file_selection_changed_cb), this);
g_signal_connect (G_OBJECT(file_chooser), "file-activated",
G_CALLBACK(csv_tximp_file_activated_cb), this);
auto box = GTK_WIDGET(gtk_builder_get_object (builder, "file_page"));
gtk_box_pack_start (GTK_BOX(box), file_chooser, TRUE, TRUE, 6);
gtk_widget_show (file_chooser);
/* Preview Settings Page */
{
preview_page = GTK_WIDGET(gtk_builder_get_object (builder, "preview_page"));
// Add Settings combo
auto settings_store = gtk_list_store_new (2, G_TYPE_POINTER, G_TYPE_STRING);
settings_combo = GTK_COMBO_BOX(gtk_combo_box_new_with_model_and_entry (GTK_TREE_MODEL(settings_store)));
gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX(settings_combo), SET_NAME);
gtk_combo_box_set_active (GTK_COMBO_BOX(settings_combo), 0);
combo_hbox = GTK_WIDGET(gtk_builder_get_object (builder, "combo_hbox"));
gtk_box_pack_start (GTK_BOX(combo_hbox), GTK_WIDGET(settings_combo), true, true, 6);
gtk_widget_show (GTK_WIDGET(settings_combo));
g_signal_connect (G_OBJECT(settings_combo), "changed",
G_CALLBACK(csv_tximp_preview_settings_sel_changed_cb), this);
// Additionally connect to the changed signal of the embedded GtkEntry
auto emb_entry = gtk_bin_get_child (GTK_BIN (settings_combo));
g_signal_connect (G_OBJECT(emb_entry), "changed",
G_CALLBACK(csv_tximp_preview_settings_text_changed_cb), this);
g_signal_connect (G_OBJECT(emb_entry), "insert-text",
G_CALLBACK(csv_tximp_preview_settings_text_inserted_cb), this);
// Add Save Settings button
save_button = GTK_WIDGET(gtk_builder_get_object (builder, "save_settings"));
// Add Delete Settings button
del_button = GTK_WIDGET(gtk_builder_get_object (builder, "delete_settings"));
/* The table containing the separator configuration widgets */
start_row_spin = GTK_SPIN_BUTTON(gtk_builder_get_object (builder, "start_row"));
end_row_spin = GTK_SPIN_BUTTON(gtk_builder_get_object (builder, "end_row"));
skip_alt_rows_button = GTK_WIDGET(gtk_builder_get_object (builder, "skip_rows"));
skip_errors_button = GTK_WIDGET(gtk_builder_get_object (builder, "skip_errors_button"));
multi_split_cbutton = GTK_WIDGET(gtk_builder_get_object (builder, "multi_split_button"));
separator_table = GTK_WIDGET(gtk_builder_get_object (builder, "separator_table"));
fw_instructions_hbox = GTK_WIDGET(gtk_builder_get_object (builder, "fw_instructions_hbox"));
/* Load the separator buttons from the glade builder file into the
* sep_buttons array. */
const char* sep_button_names[] = {
"space_cbutton",
"tab_cbutton",
"comma_cbutton",
"colon_cbutton",
"semicolon_cbutton",
"hyphen_cbutton"
};
for (int i = 0; i < SEP_NUM_OF_TYPES; i++)
sep_button[i]
= (GtkCheckButton*)GTK_WIDGET(gtk_builder_get_object (builder, sep_button_names[i]));
/* Load and connect the custom separator checkbutton in the same way
* as the other separator buttons. */
custom_cbutton
= (GtkCheckButton*)GTK_WIDGET(gtk_builder_get_object (builder, "custom_cbutton"));
/* Load the entry for the custom separator entry. Connect it to the
* sep_button_clicked event handler as well. */
custom_entry = (GtkEntry*)GTK_WIDGET(gtk_builder_get_object (builder, "custom_entry"));
/* Add account selection widget */
acct_selector = gnc_account_sel_new();
auto account_hbox = GTK_WIDGET(gtk_builder_get_object (builder, "account_hbox"));
gtk_box_pack_start (GTK_BOX(account_hbox), acct_selector, TRUE, TRUE, 6);
gnc_account_sel_set_hexpand (GNC_ACCOUNT_SEL(acct_selector), true);
gtk_widget_show (acct_selector);
g_signal_connect(G_OBJECT(acct_selector), "account_sel_changed",
G_CALLBACK(csv_tximp_preview_acct_sel_cb), this);
/* Create the encoding selector widget and add it to the assistant */
encselector = GO_CHARMAP_SEL(go_charmap_sel_new(GO_CHARMAP_SEL_TO_UTF8));
/* Connect the selector to the encoding_selected event handler. */
g_signal_connect (G_OBJECT(encselector), "charmap_changed",
G_CALLBACK(csv_tximp_preview_enc_sel_cb), this);
auto encoding_container = GTK_CONTAINER(gtk_builder_get_object (builder, "encoding_container"));
gtk_container_add (encoding_container, GTK_WIDGET(encselector));
gtk_widget_set_hexpand (GTK_WIDGET(encselector), true);
gtk_widget_show_all (GTK_WIDGET(encoding_container));
/* The instructions label and image */
instructions_label = GTK_LABEL(gtk_builder_get_object (builder, "instructions_label"));
instructions_image = GTK_IMAGE(gtk_builder_get_object (builder, "instructions_image"));
/* Add in the date format combo box and hook it up to an event handler. */
date_format_combo = GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new());
for (auto& date_fmt : GncDate::c_formats)
gtk_combo_box_text_append_text (date_format_combo, _(date_fmt.m_fmt.c_str()));
gtk_combo_box_set_active (GTK_COMBO_BOX(date_format_combo), 0);
g_signal_connect (G_OBJECT(date_format_combo), "changed",
G_CALLBACK(csv_tximp_preview_date_fmt_sel_cb), this);
/* Add it to the assistant. */
auto date_format_container = GTK_CONTAINER(gtk_builder_get_object (builder, "date_format_container"));
gtk_container_add (date_format_container, GTK_WIDGET(date_format_combo));
gtk_widget_set_hexpand (GTK_WIDGET(date_format_combo), true);
gtk_widget_show_all (GTK_WIDGET(date_format_container));
/* Add in the currency format combo box and hook it up to an event handler. */
currency_format_combo = GTK_COMBO_BOX_TEXT(gtk_combo_box_text_new());
for (int i = 0; i < num_currency_formats; i++)
{
gtk_combo_box_text_append_text (currency_format_combo, _(currency_format_user[i]));
}
/* Default will the locale */
gtk_combo_box_set_active (GTK_COMBO_BOX(currency_format_combo), 0);
g_signal_connect (G_OBJECT(currency_format_combo), "changed",
G_CALLBACK(csv_tximp_preview_currency_fmt_sel_cb), this);
/* Add it to the assistant. */
auto currency_format_container = GTK_CONTAINER(gtk_builder_get_object (builder, "currency_format_container"));
gtk_container_add (currency_format_container, GTK_WIDGET(currency_format_combo));
gtk_widget_set_hexpand (GTK_WIDGET(currency_format_combo), true);
gtk_widget_show_all (GTK_WIDGET(currency_format_container));
/* Connect the CSV/Fixed-Width radio button event handler. */
csv_button = GTK_WIDGET(gtk_builder_get_object (builder, "csv_button"));
fixed_button = GTK_WIDGET(gtk_builder_get_object (builder, "fixed_button"));
/* Load the data treeview and connect it to its resizing event handler. */
treeview = (GtkTreeView*)GTK_WIDGET(gtk_builder_get_object (builder, "treeview"));
gtk_tree_view_set_headers_clickable (treeview, true);
/* This is true only after encoding_selected is called, so we must
* set it initially to false. */
encoding_selected_called = false;
}
/* Account Match Page */
account_match_page = GTK_WIDGET(gtk_builder_get_object (builder, "account_match_page"));
account_match_view = GTK_WIDGET(gtk_builder_get_object (builder, "account_match_view"));
account_match_label = GTK_WIDGET(gtk_builder_get_object (builder, "account_match_label"));
account_match_btn = GTK_WIDGET(gtk_builder_get_object (builder, "account_match_change"));
/* Doc Page */
doc_page = GTK_WIDGET(gtk_builder_get_object (builder, "doc_page"));
/* Matcher page */
match_page = GTK_WIDGET(gtk_builder_get_object (builder, "match_page"));
match_label = GTK_WIDGET(gtk_builder_get_object (builder, "match_label"));
/* Create the generic transaction importer GUI.
Note, this will call g_new0 internally. The returned object is g_freed again
either directly by the main matcher or in our assistant_finish code of the matcher
is never reached. */
gnc_csv_importer_gui = gnc_gen_trans_assist_new (GTK_WIDGET(csv_imp_asst),
match_page, nullptr, false, 42);
/* Summary Page */
summary_page = GTK_WIDGET(gtk_builder_get_object (builder, "summary_page"));
summary_label = GTK_WIDGET(gtk_builder_get_object (builder, "summary_label"));
gnc_restore_window_size (GNC_PREFS_GROUP,
GTK_WINDOW(csv_imp_asst), gnc_ui_get_main_window(nullptr));
gtk_builder_connect_signals (builder, this);
g_object_unref (G_OBJECT(builder));
gtk_widget_show_all (GTK_WIDGET(csv_imp_asst));
gnc_window_adjust_for_screen (GTK_WINDOW(csv_imp_asst));
/* In order to trigger a book options display on the creation of a new book,
* we need to detect when we are dealing with a new book. */
new_book = gnc_is_new_book();
}
/*******************************************************
* Assistant Destructor
*******************************************************/
CsvImpTransAssist::~CsvImpTransAssist ()
{
/* This function is safe to call on a null pointer */
gnc_gen_trans_list_delete (gnc_csv_importer_gui);
/* The call above frees gnc_csv_importer_gui but can't nullify it.
* Do it here so no one accidentally can access it still */
gnc_csv_importer_gui = nullptr;
gtk_widget_destroy (GTK_WIDGET(csv_imp_asst));
}
/**************************************************
* Code related to the file chooser page
**************************************************/
/* check_for_valid_filename for a valid file to activate the "Next" button
*/
bool
CsvImpTransAssist::check_for_valid_filename ()
{
auto file_name = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(file_chooser));
if (!file_name || g_file_test (file_name, G_FILE_TEST_IS_DIR))
return false;
auto filepath = gnc_uri_get_path (file_name);
auto starting_dir = g_path_get_dirname (filepath);
m_file_name = file_name;
gnc_set_default_directory (GNC_PREFS_GROUP, starting_dir);
DEBUG("file_name selected is %s", m_file_name.c_str());
DEBUG("starting directory is %s", starting_dir);
g_free (filepath);
g_free (file_name);
g_free (starting_dir);
return true;
}
/* csv_tximp_file_activated_cb
*
* call back for file chooser widget
*/
void
CsvImpTransAssist::file_activated_cb ()
{
gtk_assistant_set_page_complete (csv_imp_asst, file_page, false);
/* Test for a valid filename and not a directory */
if (check_for_valid_filename ())
{
gtk_assistant_set_page_complete (csv_imp_asst, file_page, true);
gtk_assistant_next_page (csv_imp_asst);
}
}
/* csv_tximp_file_selection_changed_cb
*
* call back for file chooser widget
*/
void
CsvImpTransAssist::file_selection_changed_cb ()
{
/* Enable the "Next" button based on a valid filename */
gtk_assistant_set_page_complete (csv_imp_asst, file_page,
check_for_valid_filename ());
}
/**************************************************
* Code related to the preview page
**************************************************/
/* Set the available presets in the settings combo box
*/
void CsvImpTransAssist::preview_populate_settings_combo()
{
// Clear the list store
auto model = gtk_combo_box_get_model (settings_combo);
gtk_list_store_clear (GTK_LIST_STORE(model));
// Append the default entry
auto presets = get_import_presets_trans ();
for (auto preset : presets)
{
GtkTreeIter iter;
gtk_list_store_append (GTK_LIST_STORE(model), &iter);
/* FIXME we store the raw pointer to the preset, while it's
* managed by a shared pointer. This is dangerous because
* when the shared pointer goes out of scope, our pointer will dangle.
* For now this is safe, because the shared pointers in this case are
* long-lived, but this may need refactoring.
*/
gtk_list_store_set (GTK_LIST_STORE(model), &iter, SET_GROUP, preset.get(), SET_NAME, _(preset->m_name.c_str()), -1);
}
}
/* Enable or disable the save and delete settings buttons
* depending on what is selected and entered as settings name
*/
void CsvImpTransAssist::preview_handle_save_del_sensitivity (GtkComboBox* combo)
{
GtkTreeIter iter;
auto can_delete = false;
auto can_save = false;
auto entry = gtk_bin_get_child (GTK_BIN(combo));
auto entry_text = gtk_entry_get_text (GTK_ENTRY(entry));
/* Handle sensitivity of the delete and save button */
if (gtk_combo_box_get_active_iter (combo, &iter))
{
CsvTransImpSettings *preset;
GtkTreeModel *model = gtk_combo_box_get_model (combo);
gtk_tree_model_get (model, &iter, SET_GROUP, &preset, -1);
if (preset && !preset_is_reserved_name (preset->m_name))
{
/* Current preset is not read_only, so buttons can be enabled */
can_delete = true;
can_save = true;
}
}
else if (entry_text && (strlen (entry_text) > 0) &&
!preset_is_reserved_name (std::string(entry_text)))
can_save = true;
gtk_widget_set_sensitive (save_button, can_save);
gtk_widget_set_sensitive (del_button, can_delete);
}
void
CsvImpTransAssist::preview_settings_name (GtkEntry* entry)
{
auto text = gtk_entry_get_text (entry);
if (text)
tx_imp->settings_name(text);
auto box = gtk_widget_get_parent (GTK_WIDGET(entry));
auto combo = gtk_widget_get_parent (GTK_WIDGET(box));
preview_handle_save_del_sensitivity (GTK_COMBO_BOX(combo));
}
/* Use selected preset to configure the import. Triggered when
* a preset is selected in the settings combo.
*/
void
CsvImpTransAssist::preview_settings_load ()
{
// Get the Active Selection
GtkTreeIter iter;
if (!gtk_combo_box_get_active_iter (settings_combo, &iter))
return;
CsvTransImpSettings *preset = nullptr;
auto model = gtk_combo_box_get_model (settings_combo);
gtk_tree_model_get (model, &iter, SET_GROUP, &preset, -1);
if (!preset)
return;
tx_imp->settings (*preset);
if (preset->m_load_error)
gnc_error_dialog (GTK_WINDOW (csv_imp_asst),
"%s", _("There were problems reading some saved settings, continuing to load.\n"
"Please review and save again."));
preview_refresh ();
preview_handle_save_del_sensitivity (settings_combo);
}
/* Callback to delete a settings entry
*/
void
CsvImpTransAssist::preview_settings_delete ()
{
// Get the Active Selection
GtkTreeIter iter;
if (!gtk_combo_box_get_active_iter (settings_combo, &iter))
return;
CsvTransImpSettings *preset = nullptr;
auto model = gtk_combo_box_get_model (settings_combo);
gtk_tree_model_get (model, &iter, SET_GROUP, &preset, -1);
auto response = gnc_ok_cancel_dialog (GTK_WINDOW (csv_imp_asst),
GTK_RESPONSE_CANCEL,
"%s", _("Delete the Import Settings."));
if (response == GTK_RESPONSE_OK)
{
preset->remove();
preview_populate_settings_combo();
gtk_combo_box_set_active (settings_combo, 0); // Default
preview_refresh (); // Reset the widgets
}
}
/* Callback to save the current settings to the gnucash state file.
*/
void
CsvImpTransAssist::preview_settings_save ()
{
auto new_name = tx_imp->settings_name();
/* Check if the entry text matches an already existing preset */
GtkTreeIter iter;
if (!gtk_combo_box_get_active_iter (settings_combo, &iter))
{
auto model = gtk_combo_box_get_model (settings_combo);
bool valid = gtk_tree_model_get_iter_first (model, &iter);
while (valid)
{
// Walk through the list, reading each row
CsvTransImpSettings *preset;
gtk_tree_model_get (model, &iter, SET_GROUP, &preset, -1);
if (preset && (preset->m_name == std::string(new_name)))
{
auto response = gnc_ok_cancel_dialog (GTK_WINDOW (csv_imp_asst),
GTK_RESPONSE_OK,
"%s", _("Setting name already exists, overwrite?"));
if (response != GTK_RESPONSE_OK)
return;
break;
}
valid = gtk_tree_model_iter_next (model, &iter);
}
}
/* All checks passed, let's save this preset */
if (!tx_imp->save_settings())
{
gnc_info_dialog (GTK_WINDOW (csv_imp_asst),
"%s", _("The settings have been saved."));
// Update the settings store
preview_populate_settings_combo();
auto model = gtk_combo_box_get_model (settings_combo);
// Get the first entry in model
GtkTreeIter iter;
bool valid = gtk_tree_model_get_iter_first (model, &iter);
while (valid)
{
// Walk through the list, reading each row
gchar *name = nullptr;
gtk_tree_model_get (model, &iter, SET_NAME, &name, -1);
if (g_strcmp0 (name, new_name.c_str()) == 0) // Set Active, the one Saved.
gtk_combo_box_set_active_iter (settings_combo, &iter);
g_free (name);
valid = gtk_tree_model_iter_next (model, &iter);
}
}
else
gnc_error_dialog (GTK_WINDOW (csv_imp_asst),
"%s", _("There was a problem saving the settings, please try again."));
}
/* Callback triggered when user adjusts skip start lines
*/
void CsvImpTransAssist::preview_update_skipped_rows ()
{
/* Update skip rows in the parser */
tx_imp->update_skipped_lines (gtk_spin_button_get_value_as_int (start_row_spin),
gtk_spin_button_get_value_as_int (end_row_spin),
gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(skip_alt_rows_button)),
gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(skip_errors_button)));
/* And adjust maximum number of lines that can be skipped at each end accordingly */
auto adj = gtk_spin_button_get_adjustment (end_row_spin);
gtk_adjustment_set_upper (adj, tx_imp->m_parsed_lines.size()
- tx_imp->skip_start_lines() -1);
adj = gtk_spin_button_get_adjustment (start_row_spin);
gtk_adjustment_set_upper (adj, tx_imp->m_parsed_lines.size()
- tx_imp->skip_end_lines() - 1);
preview_refresh_table ();
}
void CsvImpTransAssist::preview_multi_split (bool multi)
{
tx_imp->multi_split(multi);
preview_refresh ();
}
/** Event handler for separator changes. This function is called
* whenever one of the widgets for configuring the separators (the
* separator checkbuttons or the custom separator entry) is
* changed.
* @param widget The widget that was changed
*/
void CsvImpTransAssist::preview_update_separators (GtkWidget* widget)
{
/* Only manipulate separator characters if the currently open file is
* csv separated. */
if (tx_imp->file_format() != GncImpFileFormat::CSV)
return;
/* Add the corresponding characters to checked_separators for each
* button that is checked. */
auto checked_separators = std::string();
const auto stock_sep_chars = std::string (" \t,:;-");
for (int i = 0; i < SEP_NUM_OF_TYPES; i++)
{
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(sep_button[i])))
checked_separators += stock_sep_chars[i];
}
/* Add the custom separator if the user checked its button. */