GnuCash c935c2f+
Loading...
Searching...
No Matches
Macros | Functions
assistant-csv-account-import.c File Reference

CSV Import Assistant. More...

#include <config.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>
#include "dialog-utils.h"
#include "gnc-ui.h"
#include "gnc-uri-utils.h"
#include "gnc-ui-util.h"
#include "gnc-component-manager.h"
#include "assistant-csv-account-import.h"
#include "csv-account-import.h"

Go to the source code of this file.

Macros

#define GNC_PREFS_GROUP   "dialogs.import.csv"
 
#define ASSISTANT_CSV_IMPORT_CM_CLASS   "assistant-csv-account-import"
 
#define CREATE_COLUMN(description, column_id)
 

Functions

void csv_import_assistant_prepare (GtkAssistant *assistant, GtkWidget *page, gpointer user_data)
 
void csv_import_assistant_finish (GtkAssistant *gtkassistant, gpointer user_data)
 
void csv_import_assistant_cancel (GtkAssistant *gtkassistant, gpointer user_data)
 
void csv_import_assistant_close (GtkAssistant *gtkassistant, gpointer user_data)
 
void csv_import_assistant_start_page_prepare (GtkAssistant *gtkassistant, gpointer user_data)
 
void csv_import_assistant_account_page_prepare (GtkAssistant *gtkassistant, gpointer user_data)
 
void csv_import_assistant_file_page_prepare (GtkAssistant *assistant, gpointer user_data)
 
void csv_import_assistant_finish_page_prepare (GtkAssistant *assistant, gpointer user_data)
 
void csv_import_assistant_summary_page_prepare (GtkAssistant *assistant, gpointer user_data)
 
void csv_import_sep_cb (GtkWidget *radio, gpointer user_data)
 
void csv_import_hrows_cb (GtkWidget *spin, gpointer user_data)
 
void csv_import_file_chooser_file_activated_cb (GtkFileChooser *chooser, CsvImportInfo *info)
 
void csv_import_file_chooser_selection_changed_cb (GtkFileChooser *chooser, CsvImportInfo *info)
 
void gnc_file_csv_account_import (void)
 The gnc_file_csv_account_import() will let the user import accounts from a delimited file.
 

Detailed Description

CSV Import Assistant.

Author
Copyright (c) 2012 Robert Fewell

Definition in file assistant-csv-account-import.c.

Macro Definition Documentation

◆ ASSISTANT_CSV_IMPORT_CM_CLASS

#define ASSISTANT_CSV_IMPORT_CM_CLASS   "assistant-csv-account-import"

Definition at line 44 of file assistant-csv-account-import.c.

◆ CREATE_COLUMN

#define CREATE_COLUMN (   description,
  column_id 
)
Value:
renderer = gtk_cell_renderer_text_new (); \
mnemonic_desc = mnemonic_escape (_(description)); \
column = gtk_tree_view_column_new_with_attributes (mnemonic_desc, renderer, "text", column_id, NULL); \
gtk_tree_view_column_add_attribute (column, renderer, "background", ROW_COLOR); \
gtk_tree_view_column_set_resizable (column, TRUE); \
gtk_tree_view_append_column (GTK_TREE_VIEW(info->tree_view), column); \
g_free (mnemonic_desc);

◆ GNC_PREFS_GROUP

#define GNC_PREFS_GROUP   "dialogs.import.csv"

Definition at line 43 of file assistant-csv-account-import.c.

Function Documentation

◆ csv_import_assistant_account_page_prepare()

void csv_import_assistant_account_page_prepare ( GtkAssistant *  gtkassistant,
gpointer  user_data 
)

Definition at line 378 of file assistant-csv-account-import.c.

380{
381 CsvImportInfo *info = user_data;
382 csv_import_result res;
383
384 /* Disable the "Next" Assistant Button */
385 gtk_assistant_set_page_complete (assistant, info->account_page, FALSE);
386
387 /* test read one line */
388 gtk_list_store_clear (info->store);
389 res = csv_import_read_file (GTK_WINDOW (info->assistant), info->file_name, info->regexp->str, info->store, 1 );
390 if (res == RESULT_OPEN_FAILED)
391 {
392 gnc_error_dialog (GTK_WINDOW (info->assistant), _("The input file can not be opened."));
393 gtk_assistant_previous_page (assistant);
394 }
395 else if (res == RESULT_OK)
396 gtk_assistant_set_page_complete (assistant, info->account_page, TRUE);
397 else if (res == MATCH_FOUND)
398 gtk_assistant_set_page_complete (assistant, info->account_page, TRUE);
399
400 // generate preview
401 gtk_list_store_clear (info->store);
402
403 gtk_widget_set_sensitive (info->header_row_spin, TRUE);
404
405 if (csv_import_read_file (GTK_WINDOW (info->assistant), info->file_name, info->regexp->str, info->store, 11 ) == MATCH_FOUND)
406 gtk_spin_button_set_value (GTK_SPIN_BUTTON(info->header_row_spin), 1); // set header spin to 1
407 else
408 gtk_spin_button_set_value (GTK_SPIN_BUTTON(info->header_row_spin), 0); //reset header spin to 0
409
410 /* if the store has rows, enable "Next" button */
411 csv_import_assistant_enable_account_forward (info);
412}

◆ csv_import_assistant_cancel()

void csv_import_assistant_cancel ( GtkAssistant *  gtkassistant,
gpointer  user_data 
)

Definition at line 521 of file assistant-csv-account-import.c.

522{
523 CsvImportInfo *info = user_data;
524 gnc_close_gui_component_by_data (ASSISTANT_CSV_IMPORT_CM_CLASS, info);
525}

◆ csv_import_assistant_close()

void csv_import_assistant_close ( GtkAssistant *  gtkassistant,
gpointer  user_data 
)

Definition at line 528 of file assistant-csv-account-import.c.

529{
530 CsvImportInfo *info = user_data;
531 gnc_close_gui_component_by_data (ASSISTANT_CSV_IMPORT_CM_CLASS, info);
532}

◆ csv_import_assistant_file_page_prepare()

void csv_import_assistant_file_page_prepare ( GtkAssistant *  assistant,
gpointer  user_data 
)

Definition at line 363 of file assistant-csv-account-import.c.

365{
366 CsvImportInfo *info = user_data;
367
368 /* Set the default directory */
369 if (info->starting_dir)
370 gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(info->file_chooser), info->starting_dir);
371
372 /* Disable the "Next" Assistant Button */
373 gtk_assistant_set_page_complete (assistant, info->file_page, FALSE);
374}

◆ csv_import_assistant_finish()

void csv_import_assistant_finish ( GtkAssistant *  gtkassistant,
gpointer  user_data 
)

Definition at line 535 of file assistant-csv-account-import.c.

536{
537 CsvImportInfo *info = user_data;
538
539 gtk_list_store_clear (info->store);
540 csv_import_read_file (GTK_WINDOW (info->assistant), info->file_name, info->regexp->str, info->store, 0 );
541 csv_account_import (info);
542}

◆ csv_import_assistant_finish_page_prepare()

void csv_import_assistant_finish_page_prepare ( GtkAssistant *  assistant,
gpointer  user_data 
)

Definition at line 416 of file assistant-csv-account-import.c.

418{
419 CsvImportInfo *info = user_data;
420 gchar *text;
421
422 /* Set Finish page text */
423 /* Before creating accounts, if this is a new book, tell user they can
424 * specify book options, since they affect how transactions are created */
425 if (info->new_book)
426 text = g_strdup_printf (gettext (new_book_finish_tree_string), info->file_name);
427 else
428 text = g_strdup_printf (gettext (finish_tree_string), info->file_name);
429
430 gtk_label_set_text (GTK_LABEL(info->finish_label), text);
431 g_free (text);
432
433 /* Save the Window size and directory */
434 gnc_set_default_directory (GNC_PREFS_GROUP, info->starting_dir);
435
436 /* Enable the Assistant Buttons */
437 gtk_assistant_set_page_complete (assistant, info->finish_label, TRUE);
438}

◆ csv_import_assistant_prepare()

void csv_import_assistant_prepare ( GtkAssistant *  assistant,
GtkWidget *  page,
gpointer  user_data 
)

Definition at line 478 of file assistant-csv-account-import.c.

480{
481 gint currentpage = gtk_assistant_get_current_page (assistant);
482
483 switch (currentpage)
484 {
485 case 0:
486 /* Current page is Import Start page */
487 csv_import_assistant_start_page_prepare (assistant, user_data);
488 break;
489 case 1:
490 /* Current page is File select page */
491 csv_import_assistant_file_page_prepare (assistant, user_data);
492 break;
493 case 2:
494 /* Current page is Account page */
495 csv_import_assistant_account_page_prepare (assistant, user_data);
496 break;
497 case 3:
498 /* Current page is Finish page */
499 csv_import_assistant_finish_page_prepare (assistant, user_data);
500 break;
501 case 4:
502 /* Current page is Summary page */
503 csv_import_assistant_summary_page_prepare (assistant, user_data);
504 break;
505 }
506}

◆ csv_import_assistant_start_page_prepare()

void csv_import_assistant_start_page_prepare ( GtkAssistant *  gtkassistant,
gpointer  user_data 
)

Definition at line 351 of file assistant-csv-account-import.c.

353{
354 gint num = gtk_assistant_get_current_page (assistant);
355 GtkWidget *page = gtk_assistant_get_nth_page (assistant, num);
356
357 /* Enable the Assistant Buttons */
358 gtk_assistant_set_page_complete (assistant, page, TRUE);
359}

◆ csv_import_assistant_summary_page_prepare()

void csv_import_assistant_summary_page_prepare ( GtkAssistant *  assistant,
gpointer  user_data 
)

Definition at line 442 of file assistant-csv-account-import.c.

444{
445 CsvImportInfo *info = user_data;
446 gchar *text, *errtext, *mtext;
447
448 /* Before creating accounts, if this is a new book, let user specify
449 * book options, since they affect how transactions are created */
450 if (info->new_book)
451 info->new_book = gnc_new_book_option_display (info->assistant);
452
453 if (g_strcmp0 (info->error, "") != 0)
454 {
455 GtkTextBuffer *buffer;
456
457 buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW(info->summary_error_view));
458 text = g_strdup_printf (gettext ("Import completed but with errors!\n\nThe number of Accounts added was %u and "
459 "%u were updated.\n\nSee below for errors…"), info->num_new, info->num_updates);
460 errtext = g_strdup_printf ("%s", info->error);
461 gtk_text_buffer_set_text (buffer, errtext, -1);
462 g_free (errtext);
463 g_free (info->error);
464 }
465 else
466 text = g_strdup_printf (gettext ("Import completed successfully!\n\nThe number of Accounts added was %u and "
467 "%u were updated.\n"), info->num_new, info->num_updates);
468
469 mtext = g_strdup_printf ("<span size=\"medium\"><b>%s</b></span>", text);
470 gtk_label_set_markup (GTK_LABEL(info->summary_label), mtext);
471
472 g_free (text);
473 g_free (mtext);
474}

◆ csv_import_file_chooser_file_activated_cb()

void csv_import_file_chooser_file_activated_cb ( GtkFileChooser *  chooser,
CsvImportInfo info 
)

Definition at line 179 of file assistant-csv-account-import.c.

181{
182 GtkAssistant *assistant = GTK_ASSISTANT(info->assistant);
183 gtk_assistant_set_page_complete (assistant, info->file_page, FALSE);
184
185 /* Test for a valid filename and not a directory */
186 if (csv_import_assistant_check_filename (chooser, info))
187 {
188 gtk_assistant_set_page_complete (assistant, info->file_page, TRUE);
189 gtk_assistant_next_page (assistant);
190 }
191}

◆ csv_import_file_chooser_selection_changed_cb()

void csv_import_file_chooser_selection_changed_cb ( GtkFileChooser *  chooser,
CsvImportInfo info 
)

Definition at line 200 of file assistant-csv-account-import.c.

202{
203 GtkAssistant *assistant = GTK_ASSISTANT(info->assistant);
204 gtk_assistant_set_page_complete (assistant, info->account_page, FALSE);
205
206 /* Enable the "Next" button based on a valid filename */
207 gtk_assistant_set_page_complete (assistant, info->file_page,
208 csv_import_assistant_check_filename (chooser, info));
209}

◆ csv_import_hrows_cb()

void csv_import_hrows_cb ( GtkWidget *  spin,
gpointer  user_data 
)

Definition at line 217 of file assistant-csv-account-import.c.

218{
219 CsvImportInfo *info = user_data;
220
221 GtkTreeIter iter;
222 gboolean valid;
223 int num_rows;
224
225 /* Get number of rows for header */
226 info->header_rows = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON(spin));
227
228 /* Get number of rows displayed */
229 num_rows = gtk_tree_model_iter_n_children (GTK_TREE_MODEL(info->store), NULL);
230
231 /* Modify background color for header rows */
232 if (info->header_rows == 0)
233 {
234 valid = gtk_tree_model_iter_nth_child (GTK_TREE_MODEL(info->store), &iter, NULL, 0 );
235 if (valid)
236 gtk_list_store_set (info->store, &iter, ROW_COLOR, NULL, -1);
237 }
238 else
239 {
240 if (info->header_rows - 1 < num_rows)
241 {
242 valid = gtk_tree_model_iter_nth_child (GTK_TREE_MODEL(info->store), &iter, NULL, info->header_rows - 1 );
243 if (valid)
244 gtk_list_store_set (info->store, &iter, ROW_COLOR, "pink", -1);
245 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL(info->store), &iter);
246 if (valid)
247 gtk_list_store_set (info->store, &iter, ROW_COLOR, NULL, -1);
248 }
249 }
250}

◆ csv_import_sep_cb()

void csv_import_sep_cb ( GtkWidget *  radio,
gpointer  user_data 
)

Definition at line 276 of file assistant-csv-account-import.c.

277{
278 CsvImportInfo *info = user_data;
279 const gchar *name;
280 gchar *temp;
281 gchar *sep = NULL;
282
283 if (!gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON(radio)))
284 {
285 LEAVE("1st callback of pair. Defer to 2nd callback.");
286 return;
287 }
288
289 name = gtk_buildable_get_name (GTK_BUILDABLE(radio));
290 if (g_strcmp0 (name, "radio_semi") == 0)
291 sep = ";";
292 else if (g_strcmp0 (name, "radio_colon") == 0)
293 sep = ":";
294 else
295 sep = ","; /* Use as default as well */
296
297 create_regex (info->regexp, sep);
298
299 if (g_strcmp0 (name, "radio_custom") == 0)
300 {
301 temp = gnc_input_dialog (GTK_WIDGET (info->assistant),
302 _("Adjust regular expression used for import"),
303 _("This regular expression is used to parse the import file. Modify according to your needs.\n"),
304 info->regexp->str);
305 if (temp)
306 {
307 g_string_assign (info->regexp, temp);
308 g_free (temp);
309 }
310 }
311
312 /* Generate preview */
313 gtk_list_store_clear (info->store);
314 gtk_widget_set_sensitive (info->header_row_spin, TRUE);
315
316 if (csv_import_read_file (GTK_WINDOW (info->assistant), info->file_name, info->regexp->str, info->store, 11) == MATCH_FOUND)
317 gtk_spin_button_set_value (GTK_SPIN_BUTTON(info->header_row_spin), 1); // set header spin to 1
318 else
319 gtk_spin_button_set_value (GTK_SPIN_BUTTON(info->header_row_spin), 0); //reset header spin to 0
320
321 /* if the store has rows, enable "Next" button */
322 csv_import_assistant_enable_account_forward (info);
323}
#define LEAVE(format, args...)
Print a function exit debugging message.
Definition qoflog.h:282

◆ gnc_file_csv_account_import()

void gnc_file_csv_account_import ( void  )

The gnc_file_csv_account_import() will let the user import accounts from a delimited file.

Definition at line 672 of file assistant-csv-account-import.c.

673{
674 CsvImportInfo *info;
675
676 info = g_new0 (CsvImportInfo, 1);
677
678 /* In order to trigger a book options display on the creation of a new book,
679 * we need to detect when we are dealing with a new book. */
680 info->new_book = gnc_is_new_book();
681
682 csv_import_assistant_create (info);
683
684 gnc_register_gui_component (ASSISTANT_CSV_IMPORT_CM_CLASS,
685 NULL, csv_import_close_handler,
686 info);
687
688 gtk_widget_show_all (info->assistant);
689
690 gnc_window_adjust_for_screen (GTK_WINDOW(info->assistant));
691}