30 #include <glib/gi18n.h> 31 #include <glib/gstdio.h> 36 #include "gnc-component-manager.h" 37 #include "csv-account-import.h" 40 static QofLogModule log_module = GNC_MOD_ASSISTANT;
44 fill_model_with_match(GMatchInfo *match_info,
45 const gchar *match_name,
52 if (!match_info || !match_name)
55 temp = g_match_info_fetch_named (match_info, match_name);
59 if (g_str_has_prefix (temp,
"\""))
61 if (strlen (temp) >= 2)
63 gchar *toptail = g_strndup (temp + 1, strlen (temp)-2);
64 gchar **parts = g_strsplit (toptail,
"\"\"", -1);
65 temp = g_strjoinv (
"\"", parts);
70 gtk_list_store_set (store, iterptr, column, temp, -1);
81 csv_import_read_file (GtkWindow *window,
const gchar *filename,
82 const gchar *parser_regexp,
83 GtkListStore *store, guint max_rows)
85 gchar *locale_cont, *contents;
86 GMatchInfo *match_info = NULL;
87 GRegex *regexpat = NULL;
90 gboolean match_found = FALSE;
95 if (!g_file_get_contents (filename, &locale_cont, NULL, NULL))
98 return RESULT_OPEN_FAILED;
101 contents = g_locale_to_utf8 (locale_cont, -1, NULL, NULL, NULL);
102 g_free (locale_cont);
107 g_regex_new (parser_regexp, G_REGEX_OPTIMIZE, 0, &err);
113 errmsg = g_strdup_printf (_(
"Error in regular expression '%s':\n%s"),
114 parser_regexp, err->message);
117 dialog = gtk_message_dialog_new (window,
120 GTK_BUTTONS_OK,
"%s", errmsg);
121 gtk_dialog_run (GTK_DIALOG (dialog));
122 gtk_widget_destroy (dialog);
126 return RESULT_ERROR_IN_REGEXP;
129 g_regex_match (regexpat, contents, 0, &match_info);
130 while (g_match_info_matches (match_info))
134 gtk_list_store_append (store, &iter);
135 fill_model_with_match (match_info,
"type", store, &iter, TYPE);
136 fill_model_with_match (match_info,
"full_name", store, &iter, FULL_NAME);
137 fill_model_with_match (match_info,
"name", store, &iter, NAME);
138 fill_model_with_match (match_info,
"code", store, &iter, CODE);
139 fill_model_with_match (match_info,
"description", store, &iter, DESCRIPTION);
140 fill_model_with_match (match_info,
"color", store, &iter, COLOR);
141 fill_model_with_match (match_info,
"notes", store, &iter, NOTES);
142 fill_model_with_match (match_info,
"symbol", store, &iter, SYMBOL);
143 fill_model_with_match (match_info,
"namespace", store, &iter, NAMESPACE);
144 fill_model_with_match (match_info,
"hidden", store, &iter, HIDDEN);
145 fill_model_with_match (match_info,
"tax", store, &iter, TAX);
146 fill_model_with_match (match_info,
"placeholder", store, &iter, PLACE_HOLDER);
147 gtk_list_store_set (store, &iter, ROW_COLOR, NULL, -1);
152 gtk_tree_model_get (GTK_TREE_MODEL(store), &iter, TYPE, &str_type, -1);
154 if (g_strcmp0 (_(
"Type"), str_type) == 0)
162 g_match_info_next (match_info, &err);
165 g_match_info_free (match_info);
166 g_regex_unref (regexpat);
171 g_printerr (
"Error while matching: %s\n", err->message);
175 if (match_found == TRUE)
195 gchar *type, *full_name, *name, *code, *description, *color;
196 gchar *notes, *symbol, *
namespace, *hidden, *tax, *place_holder;
200 book = gnc_get_current_book();
201 root = gnc_book_get_root_account (book);
204 info->num_updates = 0;
207 row = info->header_rows;
208 valid = gtk_tree_model_iter_nth_child (GTK_TREE_MODEL(info->store), &iter, NULL, row );
212 gtk_tree_model_get (GTK_TREE_MODEL (info->store), &iter,
214 FULL_NAME, &full_name,
217 DESCRIPTION, &description,
221 NAMESPACE, &
namespace,
224 PLACE_HOLDER, &place_holder, -1);
229 DEBUG(
"Row is %u and full name is %s", row, full_name);
233 if (g_strrstr (full_name, name) != NULL)
235 gint string_position;
236 gnc_commodity *commodity;
237 gnc_commodity_table *
table;
241 string_position = strlen (full_name) - strlen (name) - 1;
243 if (string_position == -1)
244 full_parent = g_strdup (full_name);
246 full_parent = g_strndup (full_name, string_position);
249 g_free (full_parent);
251 if (parent == NULL && string_position != -1)
253 gchar *text = g_strdup_printf (gettext(
"Row %u, path to account %s not found, added as top level\n"), row + 1, name);
254 info->error = g_strconcat (info->error, text, NULL);
256 PINFO(
"Unable to import Row %u for account %s, path not found!", row, name);
264 commodity = gnc_commodity_table_lookup (
table,
namespace, symbol);
268 DEBUG(
"We have a valid commodity and will add account %s", full_name);
269 info->num_new = info->num_new + 1;
270 gnc_suspend_gui_refresh ();
276 if (g_strcmp0 (notes,
"") != 0)
278 if (g_strcmp0 (description,
"") != 0)
280 if (g_strcmp0 (code,
"") != 0)
283 if (g_strcmp0 (color,
"") != 0)
285 if (gdk_rgba_parse (&testcolor, color))
291 if (g_strcmp0 (hidden,
"T") == 0)
293 if (g_strcmp0 (place_holder,
"T") == 0)
301 gnc_resume_gui_refresh ();
305 gchar *err_string = g_strdup_printf (gettext(
"Row %u, commodity %s / %s not found\n"), row + 1,
307 info->error = g_strconcat (info->error, err_string, NULL);
309 PINFO(
"Unable to import Row %u for account %s, commodity!", row, full_name);
314 gchar *err_string = g_strdup_printf (gettext(
"Row %u, account %s not in %s\n"), row + 1, name, full_name);
315 info->error = g_strconcat (info->error, err_string, NULL);
317 PINFO(
"Unable to import Row %u for account %s, name!", row, full_name);
323 DEBUG(
"Existing account, will try and update account %s", full_name);
324 info->num_updates = info->num_updates + 1;
325 if (g_strcmp0 (color,
"") != 0)
327 if (gdk_rgba_parse (&testcolor, color))
333 if (g_strcmp0 (notes,
"") != 0)
336 if (g_strcmp0 (description,
"") != 0)
339 if (g_strcmp0 (code,
"") != 0)
342 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (info->store), &iter);
350 g_free (description);
357 g_free (place_holder);
void xaccAccountSetType(Account *acc, GNCAccountType tip)
Set the account's type.
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
Returns the commodity table associated with a book.
void gnc_account_append_child(Account *new_parent, Account *child)
This function will remove from the child account any pre-existing parent relationship, and will then add the account as a child of the new parent.
void xaccAccountSetNotes(Account *acc, const char *str)
Set the account's notes.
utility functions for the GnuCash UI
#define PINFO(format, args...)
Print an informational note.
#define DEBUG(format, args...)
Print a debugging message.
void xaccAccountSetCode(Account *acc, const char *str)
Set the account's accounting code.
#define ENTER(format, args...)
Print a function entry debugging message.
GNCAccountType xaccAccountStringToEnum(const char *str)
Conversion routines for the account types to/from strings that are used in persistent storage...
Account handling public routines.
void xaccAccountSetPlaceholder(Account *acc, gboolean val)
Set the "placeholder" flag for an account.
void xaccAccountSetColor(Account *acc, const char *str)
Set the account's Color.
Account * gnc_account_lookup_by_full_name(const Account *any_acc, const gchar *name)
The gnc_account_lookup_full_name() subroutine works like gnc_account_lookup_by_name, but uses fully-qualified names using the given separator.
void xaccAccountSetHidden(Account *acc, gboolean val)
Set the "hidden" flag for an account.
void xaccAccountBeginEdit(Account *acc)
The xaccAccountBeginEdit() subroutine is the first phase of a two-phase-commit wrapper for account up...
#define LEAVE(format, args...)
Print a function exit debugging message.
Account * xaccMallocAccount(QofBook *book)
Constructor.
void xaccAccountSetDescription(Account *acc, const char *str)
Set the account's description.
void xaccAccountCommitEdit(Account *acc)
ThexaccAccountCommitEdit() subroutine is the second phase of a two-phase-commit wrapper for account u...
void xaccAccountSetName(Account *acc, const char *str)
Set the account's name.
void xaccAccountSetCommodity(Account *acc, gnc_commodity *com)
Set the account's commodity.