30#include <glib/gi18n.h>
31#include <glib/gstdio.h>
37#include "gnc-component-manager.h"
38#include "csv-account-import.h"
41static QofLogModule log_module = GNC_MOD_ASSISTANT;
45fill_model_with_match(GMatchInfo *match_info,
46 const gchar *match_name,
53 if (!match_info || !match_name)
56 temp = g_match_info_fetch_named (match_info, match_name);
60 if (g_str_has_prefix (temp,
"\""))
62 if (strlen (temp) >= 2)
64 gchar *toptail = g_strndup (temp + 1, strlen (temp)-2);
65 gchar **parts = g_strsplit (toptail,
"\"\"", -1);
66 temp = g_strjoinv (
"\"", parts);
71 gtk_list_store_set (store, iterptr, column, temp, -1);
82csv_import_read_file (GtkWindow *window,
const gchar *filename,
83 const gchar *parser_regexp,
84 GtkListStore *store, guint max_rows)
86 gchar *locale_cont, *contents;
87 GMatchInfo *match_info = NULL;
88 GRegex *regexpat = NULL;
91 gboolean match_found = FALSE;
96 if (!g_file_get_contents (filename, &locale_cont, NULL, NULL))
99 return RESULT_OPEN_FAILED;
104 if (g_utf8_validate(locale_cont, -1, NULL))
106 contents = locale_cont;
110 contents = g_locale_to_utf8 (locale_cont, -1, NULL, NULL, NULL);
111 g_free (locale_cont);
121 g_regex_new (parser_regexp, G_REGEX_OPTIMIZE, 0, &err);
127 errmsg = g_strdup_printf (
_(
"Error in regular expression '%s':\n%s"),
128 parser_regexp, err->message);
131 dialog = gtk_message_dialog_new (window,
134 GTK_BUTTONS_OK,
"%s", errmsg);
135 gtk_dialog_run (GTK_DIALOG (dialog));
136 gtk_widget_destroy (dialog);
140 return RESULT_ERROR_IN_REGEXP;
143 g_regex_match (regexpat, contents, 0, &match_info);
144 while (g_match_info_matches (match_info))
148 gtk_list_store_append (store, &iter);
149 fill_model_with_match (match_info,
"type", store, &iter, TYPE);
150 fill_model_with_match (match_info,
"full_name", store, &iter, FULL_NAME);
151 fill_model_with_match (match_info,
"name", store, &iter, NAME);
152 fill_model_with_match (match_info,
"code", store, &iter, CODE);
153 fill_model_with_match (match_info,
"description", store, &iter, DESCRIPTION);
154 fill_model_with_match (match_info,
"color", store, &iter, COLOR);
155 fill_model_with_match (match_info,
"notes", store, &iter, NOTES);
156 fill_model_with_match (match_info,
"symbol", store, &iter, SYMBOL);
157 fill_model_with_match (match_info,
"namespace", store, &iter, NAMESPACE);
158 fill_model_with_match (match_info,
"hidden", store, &iter, HIDDEN);
159 fill_model_with_match (match_info,
"tax", store, &iter, TAX);
160 fill_model_with_match (match_info,
"placeholder", store, &iter, PLACE_HOLDER);
161 gtk_list_store_set (store, &iter, ROW_COLOR, NULL, -1);
166 gtk_tree_model_get (GTK_TREE_MODEL(store), &iter, TYPE, &str_type, -1);
168 if (g_strcmp0 (
_(
"Type"), str_type) == 0)
176 g_match_info_next (match_info, &err);
179 g_match_info_free (match_info);
180 g_regex_unref (regexpat);
185 g_printerr (
"Error while matching: %s\n", err->message);
189 if (match_found == TRUE)
209 gchar *type, *full_name, *name, *code, *description, *color;
210 gchar *notes, *symbol, *
namespace, *hidden, *tax, *place_holder;
214 book = gnc_get_current_book();
215 root = gnc_book_get_root_account (book);
218 info->num_updates = 0;
221 row = info->header_rows;
222 valid = gtk_tree_model_iter_nth_child (GTK_TREE_MODEL(info->store), &iter, NULL, row );
226 gtk_tree_model_get (GTK_TREE_MODEL (info->store), &iter,
228 FULL_NAME, &full_name,
231 DESCRIPTION, &description,
235 NAMESPACE, &
namespace,
238 PLACE_HOLDER, &place_holder, -1);
243 DEBUG(
"Row is %u and full name is %s", row, full_name);
247 if (g_strrstr (full_name, name) != NULL)
249 gint string_position;
250 gnc_commodity *commodity;
251 gnc_commodity_table *
table;
255 string_position = strlen (full_name) - strlen (name) - 1;
257 if (string_position == -1)
258 full_parent = g_strdup (full_name);
260 full_parent = g_strndup (full_name, string_position);
263 g_free (full_parent);
265 if (parent == NULL && string_position != -1)
267 gchar *text = g_strdup_printf (gettext(
"Row %u, path to account %s not found, added as top level\n"), row + 1, name);
268 info->error = g_strconcat (info->error, text, NULL);
270 PINFO(
"Unable to import Row %u for account %s, path not found!", row, name);
278 commodity = gnc_commodity_table_lookup (
table,
namespace, symbol);
282 DEBUG(
"We have a valid commodity and will add account %s", full_name);
283 info->num_new = info->num_new + 1;
284 gnc_suspend_gui_refresh ();
290 if (g_strcmp0 (notes,
"") != 0)
292 if (g_strcmp0 (description,
"") != 0)
294 if (g_strcmp0 (code,
"") != 0)
297 if (g_strcmp0 (color,
"") != 0)
299 if (gdk_rgba_parse (&testcolor, color))
305 if (g_strcmp0 (hidden,
"T") == 0)
307 if (g_strcmp0 (place_holder,
"T") == 0)
315 gnc_resume_gui_refresh ();
319 gchar *err_string = g_strdup_printf (gettext(
"Row %u, commodity %s / %s not found\n"), row + 1,
321 info->error = g_strconcat (info->error, err_string, NULL);
323 PINFO(
"Unable to import Row %u for account %s, commodity!", row, full_name);
328 gchar *err_string = g_strdup_printf (gettext(
"Row %u, account %s not in %s\n"), row + 1, name, full_name);
329 info->error = g_strconcat (info->error, err_string, NULL);
331 PINFO(
"Unable to import Row %u for account %s, name!", row, full_name);
337 DEBUG(
"Existing account, will try and update account %s", full_name);
338 info->num_updates = info->num_updates + 1;
339 if (g_strcmp0 (color,
"") != 0)
341 if (gdk_rgba_parse (&testcolor, color))
347 if (g_strcmp0 (notes,
"") != 0)
350 if (g_strcmp0 (description,
"") != 0)
353 if (g_strcmp0 (code,
"") != 0)
356 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (info->store), &iter);
364 g_free (description);
371 g_free (place_holder);
Account handling public routines.
utility functions for the GnuCash UI
void xaccAccountSetColor(Account *acc, const char *str)
Set the account's Color.
void xaccAccountCommitEdit(Account *acc)
ThexaccAccountCommitEdit() subroutine is the second phase of a two-phase-commit wrapper for account u...
void xaccAccountBeginEdit(Account *acc)
The xaccAccountBeginEdit() subroutine is the first phase of a two-phase-commit wrapper for account up...
void gnc_account_append_child(Account *new_parent, Account *child)
This function will remove from the child account any pre-existing parent relationship,...
void xaccAccountSetName(Account *acc, const char *str)
Set the account's name.
void xaccAccountSetDescription(Account *acc, const char *str)
Set the account's description.
void xaccAccountSetNotes(Account *acc, const char *str)
Set the account's notes.
void xaccAccountSetType(Account *acc, GNCAccountType tip)
Set the account's type.
Account * xaccMallocAccount(QofBook *book)
Constructor.
void xaccAccountSetCode(Account *acc, const char *str)
Set the account's accounting code.
void xaccAccountSetCommodity(Account *acc, gnc_commodity *com)
Set the account's commodity.
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
Returns the commodity table associated with a book.
GNCAccountType xaccAccountStringToEnum(const char *str)
Conversion routines for the account types to/from strings that are used in persistent storage,...
void xaccAccountSetHidden(Account *acc, gboolean val)
Set the "hidden" flag for an account.
void xaccAccountSetPlaceholder(Account *acc, gboolean val)
Set the "placeholder" flag for an account.
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,...
void gnc_utf8_strip_invalid(gchar *str)
Strip any non-UTF-8 characters from a string.
#define PINFO(format, args...)
Print an informational note.
#define DEBUG(format, args...)
Print a debugging message.
#define LEAVE(format, args...)
Print a function exit debugging message.
#define ENTER(format, args...)
Print a function entry debugging message.