35 #include <glib/gi18n.h>
36 #include <glib/gstdio.h>
42 #ifdef AQBANKING_VERSION_5_PLUS
43 # include <gwenhywfar/syncio_file.h>
44 # include <gwenhywfar/syncio_buffered.h>
45 typedef GWEN_SYNCIO GWEN_IO_LAYER;
47 # include <gwenhywfar/io_file.h>
48 # include <gwenhywfar/io_buffered.h>
49 # include <gwenhywfar/iomanager.h>
53 #include "dialog-utils.h"
55 #include "gnc-file-aqb-import.h"
67 const gchar *aqbanking_profilename,
68 gboolean execute_transactions)
71 gchar *selected_filename = NULL;
73 AB_BANKING *api = NULL;
74 gboolean online = FALSE;
75 GncGWENGui *gui = NULL;
76 AB_IMEXPORTER *importer;
77 GWEN_DB_NODE *db_profiles = NULL;
78 GWEN_DB_NODE *db_profile;
79 AB_IMEXPORTER_CONTEXT *context = NULL;
80 GWEN_IO_LAYER *io = NULL;
82 AB_JOB_LIST2 *job_list = NULL;
83 AB_JOB_LIST2_ITERATOR *jit;
85 AB_JOB_STATUS job_status;
86 gboolean successful = TRUE;
88 int num_jobs_failed = 0;
90 GString *errstr = NULL;
93 default_dir = gnc_get_default_directory(GNC_PREFS_GROUP_AQBANKING);
94 selected_filename = gnc_file_dialog(_(
"Select a file to import"),
96 GNC_FILE_DIALOG_IMPORT);
99 if (!selected_filename)
101 DEBUG(
"filename: %s", selected_filename);
104 default_dir = g_path_get_dirname(selected_filename);
105 gnc_set_default_directory(GNC_PREFS_GROUP_AQBANKING, default_dir);
108 dtaus_fd = g_open(selected_filename, O_RDONLY, 0);
111 DEBUG(
"Could not open file %s", selected_filename);
119 g_warning(
"gnc_file_aqbanking_import: Couldn't get AqBanking API");
122 if (AB_Banking_OnlineInit(api
123 #ifdef AQBANKING_VERSION_4_EXACTLY
128 g_warning(
"gnc_file_aqbanking_import: "
129 "Couldn't initialize AqBanking API");
135 importer = AB_Banking_GetImExporter(api, aqbanking_importername);
138 g_warning(
"Import module %s not found", aqbanking_importername);
139 gnc_error_dialog(NULL,
"%s",
140 _(
"Import module for DTAUS import not found."));
145 db_profiles = AB_Banking_GetImExporterProfiles(api, aqbanking_importername);
148 db_profile = GWEN_DB_GetFirstGroup(db_profiles);
153 name = GWEN_DB_GetCharValue(db_profile,
"name", 0, 0);
154 g_return_if_fail(name);
155 if (g_ascii_strcasecmp(name, aqbanking_profilename) == 0)
157 db_profile = GWEN_DB_GetNextGroup(db_profile);
161 g_warning(
"Profile \"%s\" for importer \"%s\" not found",
162 aqbanking_profilename, aqbanking_importername);
164 db_profile = GWEN_DB_GetFirstGroup(db_profiles);
167 const char *name = GWEN_DB_GetCharValue(db_profile,
"name", 0, 0);
168 g_warning(
"Only found profile \"%s\"\n", name ? name :
"(null)");
169 db_profile = GWEN_DB_GetNextGroup(db_profile);
175 context = AB_ImExporterContext_new();
178 #ifdef AQBANKING_VERSION_5_PLUS
180 io = GWEN_SyncIo_File_new(selected_filename, GWEN_SyncIo_File_CreationMode_OpenExisting);
182 GWEN_SyncIo_AddFlags(io, GWEN_SYNCIO_FILE_FLAGS_READ);
186 int rv = GWEN_SyncIo_Connect(io);
189 g_warning(
"gnc_file_aqbanking_import: Failed to open file %s: %d", selected_filename, rv);
192 g_assert(GWEN_SyncIo_GetStatus(io) == GWEN_SyncIo_Status_Connected);
195 io = GWEN_Io_LayerFile_new(dtaus_fd, -1);
197 if (GWEN_Io_Manager_RegisterLayer(io))
199 g_warning(
"gnc_file_aqbanking_import: Failed to wrap file");
206 if (AB_ImExporter_Import(importer, context, io, db_profile
207 #ifndef AQBANKING_VERSION_5_PLUS
212 g_warning(
"gnc_file_aqbanking_import: Error on import");
217 #ifdef AQBANKING_VERSION_5_PLUS
218 GWEN_SyncIo_free(io);
220 GWEN_Io_Layer_free(io);
226 if (gnc_is_new_book())
231 execute_transactions,
232 execute_transactions ? api : NULL,
235 if (execute_transactions)
239 AB_IMEXPORTER_CONTEXT *execution_context;
245 execution_context = AB_ImExporterContext_new();
251 g_warning(
"gnc_file_aqbanking_import: Couldn't initialize Gwenhywfar GUI");
256 AB_Banking_ExecuteJobs(api, job_list, execution_context
257 #ifndef AQBANKING_VERSION_5_PLUS
272 jit = AB_Job_List2_First(job_list);
276 job = AB_Job_List2Iterator_Data(jit);
280 job_status = AB_Job_GetStatus(job);
281 if (job_status != AB_Job_StatusFinished
282 && job_status != AB_Job_StatusPending)
285 num_jobs_failed += 1;
287 if (num_jobs_failed <= max_failures)
289 if (num_jobs_failed == 1)
291 errstr = g_string_new(
"Failed jobs:\n");
293 g_string_append_printf(errstr, _(
"Job %d status %d - %s: %s \n")
296 , AB_Job_Status2Char(job_status)
297 , AB_Job_GetResultText(job));
301 if (num_jobs_failed == (max_failures + 1) )
304 g_string_append(errstr, _(
"...\n"));
308 job = AB_Job_List2Iterator_Next(jit);
311 AB_Job_List2Iterator_free(jit);
316 g_warning(
"%s", errstr->str);
317 gnc_error_dialog(NULL,
318 _(
"An error occurred while executing jobs: %d of %d failed. "
319 "Please check the log window or gnucash.trace for the exact "
320 "error message.\n\n%s")
321 , num_jobs_failed, num_jobs, errstr->str);
327 gnc_info_dialog(NULL,
328 _(
"No jobs to be send.")
333 gnc_info_dialog(NULL, ngettext
334 (
"The job was executed successfully, but as a precaution "
335 "please check the log window for potential errors.",
336 "All %d jobs were executed successfully, but as a precaution "
337 "please check the log window for potential errors.",
338 num_jobs), num_jobs);
341 AB_ImExporterContext_free(execution_context);
348 #ifdef AQBANKING_VERSION_5_PLUS
349 GWEN_SyncIo_free(io);
351 GWEN_Io_Layer_free(io);
356 AB_Job_List2_FreeAll(job_list);
360 AB_ImExporterContext_free(context);
362 GWEN_DB_Group_free(db_profiles);
366 #ifdef AQBANKING_VERSION_4_EXACTLY
367 AB_Banking_OnlineFini(api, 0);
369 AB_Banking_OnlineFini(api);
375 if (selected_filename)
376 g_free(selected_filename);
378 g_string_free(errstr, TRUE);
Dialog for AqBanking transaction data.
utility functions for the GnuCash UI
AB_JOB_LIST2 * gnc_ab_ieci_get_job_list(GncABImExContextImport *ieci)
#define DEBUG(format, args...)
Transaction matcher main window.
gboolean gnc_ab_ieci_run_matcher(GncABImExContextImport *ieci)
Generic and very flexible account matcher/picker.
GncGWENGui * gnc_GWEN_Gui_get(GtkWidget *parent)
void gnc_GWEN_Gui_release(GncGWENGui *gui)
void gnc_file_aqbanking_import(const gchar *aqbanking_importername, const gchar *aqbanking_profilename, gboolean execute_transactions)
AB_BANKING * gnc_AB_BANKING_new(void)
gint gnc_AB_BANKING_fini(AB_BANKING *api)
GncABImExContextImport * gnc_ab_import_context(AB_IMEXPORTER_CONTEXT *context, guint awaiting, gboolean execute_txns, AB_BANKING *api, GtkWidget *parent)
GtkWidget * gnc_ui_get_toplevel(void)
GUI callbacks for AqBanking.
AqBanking utility functions.
const gchar * QofLogModule