30 #include <glib/gi18n.h>
31 #include <glib/gstdio.h>
36 #include "gnc-component-manager.h"
37 #include "csv-account-import.h"
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 (
const gchar *filename,
const gchar *parser_regexp,
82 GtkListStore *store, guint max_rows)
84 gchar *locale_cont, *contents;
85 GMatchInfo *match_info = NULL;
86 GRegex *regexpat = NULL;
89 gboolean match_found = FALSE;
94 if (!g_file_get_contents (filename, &locale_cont, NULL, NULL))
97 return RESULT_OPEN_FAILED;
100 contents = g_locale_to_utf8 (locale_cont, -1, NULL, NULL, NULL);
101 g_free (locale_cont);
106 g_regex_new (parser_regexp, G_REGEX_OPTIMIZE, 0, &err);
112 errmsg = g_strdup_printf (_(
"Error in regular expression '%s':\n%s"),
113 parser_regexp, err->message);
116 dialog = gtk_message_dialog_new (NULL,
119 GTK_BUTTONS_OK,
"%s", errmsg);
120 gtk_dialog_run (GTK_DIALOG (dialog));
121 gtk_widget_destroy (dialog);
125 return RESULT_ERROR_IN_REGEXP;
128 g_regex_match (regexpat, contents, 0, &match_info);
129 while (g_match_info_matches (match_info))
133 gtk_list_store_append (store, &iter);
134 fill_model_with_match (match_info,
"type", store, &iter, TYPE);
135 fill_model_with_match (match_info,
"full_name", store, &iter, FULL_NAME);
136 fill_model_with_match (match_info,
"name", store, &iter, NAME);
137 fill_model_with_match (match_info,
"code", store, &iter, CODE);
138 fill_model_with_match (match_info,
"description", store, &iter, DESCRIPTION);
139 fill_model_with_match (match_info,
"color", store, &iter, COLOR);
140 fill_model_with_match (match_info,
"notes", store, &iter, NOTES);
141 fill_model_with_match (match_info,
"commoditym", store, &iter, COMMODITYM);
142 fill_model_with_match (match_info,
"commodityn", store, &iter, COMMODITYN);
143 fill_model_with_match (match_info,
"hidden", store, &iter, HIDDEN);
144 fill_model_with_match (match_info,
"tax", store, &iter, TAX);
145 fill_model_with_match (match_info,
"place_holder", store, &iter, PLACE_HOLDER);
146 gtk_list_store_set (store, &iter, ROW_COLOR, NULL, -1);
151 g_match_info_next (match_info, &err);
154 g_match_info_free (match_info);
155 g_regex_unref (regexpat);
160 g_printerr (
"Error while matching: %s\n", err->message);
164 if (match_found == TRUE)
184 gchar *type, *full_name, *name, *code, *description, *color;
185 gchar *notes, *commoditym, *commodityn, *hidden, *tax, *place_holder;
189 book = gnc_get_current_book();
190 root = gnc_book_get_root_account (book);
193 info->num_updates = 0;
196 row = info->header_rows;
197 valid = gtk_tree_model_iter_nth_child (GTK_TREE_MODEL(info->store), &iter, NULL, row );
201 gtk_tree_model_get (GTK_TREE_MODEL (info->store), &iter,
203 FULL_NAME, &full_name,
206 DESCRIPTION, &description,
209 COMMODITYM, &commoditym,
210 COMMODITYN, &commodityn,
213 PLACE_HOLDER, &place_holder, -1);
218 DEBUG(
"Row is %u and full name is %s", row, full_name);
222 if (g_strrstr (full_name, name) != NULL)
224 gint string_position;
230 string_position = strlen (full_name) - strlen (name) - 1;
232 if (string_position == -1)
233 full_parent = g_strdup (full_name);
235 full_parent = g_strndup (full_name, string_position);
238 g_free (full_parent);
240 if (parent == NULL && string_position != -1)
242 gchar *text = g_strdup_printf (gettext(
"Row %u, path to account %s not found, added as top level\n"), row + 1, name);
243 info->error = g_strconcat (info->error, text, NULL);
245 PINFO(
"Unable to import Row %u for account %s, path not found!", row, name);
253 commodity = gnc_commodity_table_lookup (table, commodityn, commoditym);
257 DEBUG(
"We have a valid commodity and will add account %s", full_name);
258 info->num_new = info->num_new + 1;
259 gnc_suspend_gui_refresh ();
265 if (!g_strcmp0 (notes,
"") == 0)
267 if (!g_strcmp0 (description,
"") == 0)
269 if (!g_strcmp0 (code,
"") == 0)
272 if (!g_strcmp0 (color,
"") == 0)
274 if (gdk_color_parse (color, &testcolor))
278 if (g_strcmp0 (hidden,
"T") == 0)
280 if (g_strcmp0 (place_holder,
"T") == 0)
288 gnc_resume_gui_refresh ();
292 gchar *err_string = g_strdup_printf (gettext(
"Row %u, commodity %s / %s not found\n"), row + 1,
293 commoditym, commodityn);
294 info->error = g_strconcat (info->error, err_string, NULL);
296 PINFO(
"Unable to import Row %u for account %s, commodity!", row, full_name);
301 gchar *err_string = g_strdup_printf (gettext(
"Row %u, account %s not in %s\n"), row + 1, name, full_name);
302 info->error = g_strconcat (info->error, err_string, NULL);
304 PINFO(
"Unable to import Row %u for account %s, name!", row, full_name);
310 DEBUG(
"Existing account, will try and update account %s", full_name);
311 info->num_updates = info->num_updates + 1;
312 if (!g_strcmp0 (color,
"") == 0)
314 if (gdk_color_parse (color, &testcolor))
318 if (!g_strcmp0 (notes,
"") == 0)
321 if (!g_strcmp0 (description,
"") == 0)
324 if (!g_strcmp0 (code,
"") == 0)
327 valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (info->store), &iter);
335 g_free (description);
342 g_free (place_holder);
void xaccAccountSetType(Account *acc, GNCAccountType tip)
gnc_commodity_table * gnc_commodity_table_get_table(QofBook *book)
void gnc_account_append_child(Account *new_parent, Account *child)
void xaccAccountSetNotes(Account *acc, const char *str)
utility functions for the GnuCash UI
#define PINFO(format, args...)
#define DEBUG(format, args...)
void xaccAccountSetCode(Account *acc, const char *str)
#define ENTER(format, args...)
GNCAccountType xaccAccountStringToEnum(const char *str)
Account handling public routines.
void xaccAccountSetPlaceholder(Account *acc, gboolean val)
void xaccAccountSetColor(Account *acc, const char *str)
Account * gnc_account_lookup_by_full_name(const Account *any_acc, const gchar *name)
void xaccAccountSetHidden(Account *acc, gboolean val)
void xaccAccountBeginEdit(Account *acc)
#define LEAVE(format, args...)
Account * xaccMallocAccount(QofBook *book)
void xaccAccountSetDescription(Account *acc, const char *str)
void xaccAccountCommitEdit(Account *acc)
void xaccAccountSetName(Account *acc, const char *str)
const gchar * QofLogModule
void xaccAccountSetCommodity(Account *acc, gnc_commodity *com)