00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00026 #include "config.h"
00027
00028 #include "gnc-file-aqb-import.h"
00029
00030 #include <glib/gi18n.h>
00031 #include <glib/gstdio.h>
00032 #include <string.h>
00033 #include <sys/time.h>
00034 #include <fcntl.h>
00035
00036 #include <aqbanking/version.h>
00037 #include <aqbanking/banking.h>
00038 #include <aqbanking/imexporter.h>
00039
00040 #include "gnc-ui.h"
00041 #include "qof.h"
00042 #include "Transaction.h"
00043 #include "Account.h"
00044
00045 #include "gnc-engine.h"
00046 #include "gnc-file.h"
00047 #include "gnc-ui-util.h"
00048
00049 #include "gnc-hbci-utils.h"
00050 #include "gnc-hbci-gettrans.h"
00051 #include "hbci-interaction.h"
00052
00053
00054 static QofLogModule log_module = GNC_MOD_IMPORT;
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065 void gnc_file_aqbanking_import (const gchar *aqbanking_importername,
00066 const gchar *aqbanking_profilename,
00067 gboolean execute_transactions)
00068 {
00069 char *selected_filename;
00070 char *default_dir;
00071 int dtaus_fd;
00072
00073 DEBUG("gnc_file_dtaus_import(): Begin...\n");
00074
00075 default_dir = gnc_get_default_directory(GCONF_SECTION);
00076 selected_filename = gnc_file_dialog(_("Select a file to import"),
00077 NULL,
00078 default_dir,
00079 GNC_FILE_DIALOG_IMPORT);
00080 g_free(default_dir);
00081
00082 if (selected_filename != NULL)
00083 {
00084
00085 default_dir = g_path_get_dirname(selected_filename);
00086 gnc_set_default_directory(GCONF_SECTION, default_dir);
00087 g_free(default_dir);
00088
00089
00090 DEBUG("Filename found: %s", selected_filename);
00091
00092 DEBUG("Opening selected file");
00093 dtaus_fd = g_open(selected_filename, O_RDONLY, 0);
00094 if (dtaus_fd == -1)
00095 {
00096 DEBUG("Could not open file %s", selected_filename);
00097 return;
00098 }
00099 g_free(selected_filename);
00100
00101 {
00102 int result;
00103 AB_BANKING *ab;
00104 AB_IMEXPORTER *importer;
00105 AB_IMEXPORTER_CONTEXT *ctx = 0;
00106 GWEN_BUFFEREDIO *buffio;
00107 GWEN_DB_NODE *dbProfiles;
00108 GWEN_DB_NODE *dbProfile;
00109 GNCInteractor *interactor = NULL;
00110 const char *importerName = aqbanking_importername;
00111 const char *profileName = aqbanking_profilename;
00112
00113
00114 ab = gnc_AB_BANKING_new_currentbook (NULL, &interactor);
00115 if (ab == NULL)
00116 {
00117 g_message("gnc_file_dtaus_import: Couldn't get HBCI API. Nothing will happen.\n");
00118 return;
00119 }
00120 g_assert (interactor);
00121
00122
00123 importer = AB_Banking_GetImExporter(ab, importerName);
00124 if (!importer)
00125 {
00126 DEBUG("Import module %s not found", importerName);
00127 gnc_error_dialog(NULL, "%s", ("Import module for DTAUS import not found."));
00128 return;
00129 }
00130 g_assert(importer);
00131
00132
00133 dbProfiles = AB_Banking_GetImExporterProfiles(ab, importerName);
00134
00135
00136 dbProfile = GWEN_DB_GetFirstGroup(dbProfiles);
00137 while (dbProfile)
00138 {
00139 const char *name;
00140
00141 name = GWEN_DB_GetCharValue(dbProfile, "name", 0, 0);
00142 g_assert(name);
00143 if (strcasecmp(name, profileName) == 0)
00144 break;
00145 dbProfile = GWEN_DB_GetNextGroup(dbProfile);
00146 }
00147 if (!dbProfile)
00148 {
00149 g_warning("Profile \"%s\" for importer \"%s\" not found\n",
00150 profileName, importerName);
00151
00152 dbProfile = GWEN_DB_GetFirstGroup(dbProfiles);
00153 while (dbProfile)
00154 {
00155 const char *name;
00156 name = GWEN_DB_GetCharValue(dbProfile, "name", 0, 0);
00157 g_assert(name);
00158 g_warning("Only found profile \"%s\"\n", name);
00159 dbProfile = GWEN_DB_GetNextGroup(dbProfile);
00160 }
00161 return;
00162 }
00163 g_assert(dbProfile);
00164
00165
00166 ctx = AB_ImExporterContext_new();
00167 g_assert(ctx);
00168
00169
00170 buffio = GWEN_BufferedIO_File_new(dtaus_fd);
00171 g_assert(buffio);
00172 GWEN_BufferedIO_SetReadBuffer(buffio, 0, 1024);
00173
00174 result = AB_ImExporter_Import(importer,
00175 ctx,
00176 buffio,
00177 dbProfile);
00178
00179 DEBUG("Parsing result: %d\n", result);
00180
00181 GWEN_BufferedIO_Close(buffio);
00182 GWEN_BufferedIO_free(buffio);
00183 GWEN_DB_Group_free(dbProfiles);
00184
00185 {
00186
00187 GNCImportMainMatcher *importer_generic_gui;
00188 GtkWidget *parent = NULL;
00189 gboolean successful = FALSE;
00190 GList *ab_job_list;
00191
00192
00193 importer_generic_gui = gnc_gen_trans_list_new(parent, NULL, TRUE, 14);
00194
00195
00196 ab_job_list = gnc_hbci_import_ctx(ab, ctx, importer_generic_gui,
00197 execute_transactions);
00198
00199
00200
00201 AB_ImExporterContext_free(ctx);
00202
00203 if (execute_transactions)
00204 {
00205
00206
00207 result = gnc_gen_trans_list_run (importer_generic_gui);
00208
00209 if (result)
00210
00211
00212
00213 successful = gnc_hbci_multijob_execute (parent, ab, ab_job_list, interactor);
00214
00215
00216
00217 gnc_hbci_clearqueue (ab, ab_job_list);
00218 }
00219 else
00220 {
00221 successful = TRUE;
00222 }
00223
00224 if (successful)
00225 {
00226
00227
00228 gnc_AB_BANKING_fini (ab);
00229 gnc_AB_BANKING_delete (ab);
00230 }
00231 }
00232 }
00233 }
00234 }
00235
00236