r17218 - gnucash/branches/aqbanking3/src/import-export/aqbanking - Readd the swift and dtaus file imports, except for the actual executing of ab jobs.

Andreas Köhler andi5 at cvs.gnucash.org
Wed Jun 11 19:59:47 EDT 2008


Author: andi5
Date: 2008-06-11 19:59:46 -0400 (Wed, 11 Jun 2008)
New Revision: 17218
Trac: http://svn.gnucash.org/trac/changeset/17218

Added:
   gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-file-aqb-import.c
   gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-file-aqb-import.h
Modified:
   gnucash/branches/aqbanking3/src/import-export/aqbanking/Makefile.am
   gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-ab-gettrans.c
   gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-ab-utils.c
   gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-ab-utils.h
   gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-plugin-aqbanking.c
Log:
Readd the swift and dtaus file imports, except for the actual executing of ab jobs.


Modified: gnucash/branches/aqbanking3/src/import-export/aqbanking/Makefile.am
===================================================================
--- gnucash/branches/aqbanking3/src/import-export/aqbanking/Makefile.am	2008-06-11 23:26:19 UTC (rev 17217)
+++ gnucash/branches/aqbanking3/src/import-export/aqbanking/Makefile.am	2008-06-11 23:59:46 UTC (rev 17218)
@@ -12,6 +12,7 @@
   gnc-ab-trans-templ.c \
   gnc-ab-transfer.c \
   gnc-ab-utils.c \
+  gnc-file-aqb-import.c \
   gnc-gwen-gui.c \
   gnc-plugin-aqbanking.c \
   gncmod-aqbanking.c
@@ -26,6 +27,7 @@
   gnc-ab-trans-templ.h \
   gnc-ab-transfer.h \
   gnc-ab-utils.h \
+  gnc-file-aqb-import.h \
   gnc-gwen-gui.h \
   gnc-plugin-aqbanking.h
 

Modified: gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-ab-gettrans.c
===================================================================
--- gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-ab-gettrans.c	2008-06-11 23:26:19 UTC (rev 17217)
+++ gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-ab-gettrans.c	2008-06-11 23:59:46 UTC (rev 17218)
@@ -40,7 +40,6 @@
 #include "gnc-ab-utils.h"
 #include "gnc-gwen-gui.h"
 #include "import-main-matcher.h"
-#include "import-utilities.h"
 
 /* This static indicates the debugging module that this .o belongs to.  */
 static QofLogModule log_module = G_LOG_DOMAIN;
@@ -50,8 +49,7 @@
 static gboolean gettrans_dates(GtkWidget *parent, Account *gnc_acc, GWEN_TIME **from_date, GWEN_TIME **to_date);
 static const AB_TRANSACTION *transaction_cb(const AB_TRANSACTION *ab_trans, gpointer user_data);
 
-struct _TransListData
-{
+struct _TransListData {
     Account *gnc_acc;
     GNCImportMainMatcher *importer_generic;
 };
@@ -109,86 +107,13 @@
 transaction_cb(const AB_TRANSACTION *ab_trans, gpointer user_data)
 {
     TransListData *data = user_data;
-    Account *gnc_acc;
-    QofBook *book;
     Transaction *gnc_trans;
-    const gchar *fitid;
-    const GWEN_TIME *valuta_date;
-    time_t current_time;
-    const char *custref;
-    gchar *description;
-    Split *split;
-    const AB_VALUE *ab_value;
-    gnc_numeric gnc_amount;
-    gchar *memo;
 
     g_return_val_if_fail(ab_trans && data, NULL);
 
-    gnc_acc = data->gnc_acc;
-    book = gnc_account_get_book(gnc_acc);
+    /* Create a GnuCash transaction from ab_trans */
+    gnc_trans = gnc_ab_trans_to_gnc(ab_trans, data->gnc_acc);
 
-    /* Create new GnuCash transaction for the given AqBanking one */
-    gnc_trans = xaccMallocTransaction(book);
-    xaccTransBeginEdit(gnc_trans);
-
-    /* Set OFX unique transaction ID */
-    fitid = AB_Transaction_GetFiId(ab_trans);
-    if (fitid && *fitid)
-        gnc_import_set_trans_online_id(gnc_trans, fitid);
-
-    /* Date / Time */
-    valuta_date = AB_Transaction_GetValutaDate(ab_trans);
-    if (!valuta_date) {
-        const GWEN_TIME *normal_date = AB_Transaction_GetDate(ab_trans);
-        if (normal_date)
-            valuta_date = normal_date;
-    }
-    if (valuta_date)
-        xaccTransSetDateSecs(gnc_trans, GWEN_Time_toTime_t(valuta_date));
-    else
-        g_warning("transaction_cb: Oops, date 'valuta_date' was NULL");
-
-    current_time = time(NULL);
-    xaccTransSetDateEnteredSecs(gnc_trans, mktime(localtime(&current_time)));
-
-    /* Currency.  We take simply the default currency of the gnucash account */
-    xaccTransSetCurrency(gnc_trans, xaccAccountGetCommodity(gnc_acc));
-
-    /* Number.  We use the "customer reference", if there is one. */
-    custref = AB_Transaction_GetCustomerReference(ab_trans);
-    if (custref && *custref
-        && g_ascii_strncasecmp(custref, "NONREF", 6) != 0)
-        xaccTransSetNum(gnc_trans, custref);
-
-    /* Description */
-    description = gnc_ab_description_to_gnc(ab_trans);
-    xaccTransSetDescription(gnc_trans, description);
-    g_free(description);
-
-    /* Notes. */
-    /* xaccTransSetNotes(gnc_trans, g_notes); */
-    /* But Nobody ever uses the Notes field? */
-
-    /* Add one split */
-    split = xaccMallocSplit(book);
-    xaccSplitSetParent(split, gnc_trans);
-    xaccSplitSetAccount(split, gnc_acc);
-
-    /* Amount into the split */
-    ab_value = AB_Transaction_GetValue(ab_trans);
-    gnc_amount = double_to_gnc_numeric(
-        ab_value ? AB_Value_GetValueAsDouble(ab_value) : 0.0,
-        xaccAccountGetCommoditySCU(gnc_acc),
-        GNC_RND_ROUND);
-    if (!ab_value)
-        g_warning("transaction_cb: Oops, value was NULL.  Using 0");
-    xaccSplitSetBaseValue(split, gnc_amount, xaccAccountGetCommodity(gnc_acc));
-
-    /* Memo in the Split. */
-    memo = gnc_ab_memo_to_gnc(ab_trans);
-    xaccSplitSetMemo(split, memo);
-    g_free(memo);
-
     /* Instead of xaccTransCommitEdit(gnc_trans)  */
     gnc_gen_trans_list_add_trans(data->importer_generic, gnc_trans);
 

Modified: gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-ab-utils.c
===================================================================
--- gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-ab-utils.c	2008-06-11 23:26:19 UTC (rev 17217)
+++ gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-ab-utils.c	2008-06-11 23:59:46 UTC (rev 17218)
@@ -33,10 +33,12 @@
 #include <gwenhywfar/gwenhywfar.h>
 #include <aqbanking/banking.h>
 
+#include "Transaction.h"
 #include "gnc-ab-kvp.h"
 #include "gnc-ab-utils.h"
 #include "gnc-glib-utils.h"
 #include "gnc-gwen-gui.h"
+#include "import-utilities.h"
 #include "qof.h"
 
 /* This static indicates the debugging module that this .o belongs to.  */
@@ -308,6 +310,89 @@
     return retval;
 }
 
+Transaction *
+gnc_ab_trans_to_gnc(const AB_TRANSACTION *ab_trans, Account *gnc_acc)
+{
+    QofBook *book;
+    Transaction *gnc_trans;
+    const gchar *fitid;
+    const GWEN_TIME *valuta_date;
+    time_t current_time;
+    const char *custref;
+    gchar *description;
+    Split *split;
+    const AB_VALUE *ab_value;
+    gnc_numeric gnc_amount;
+    gchar *memo;
+
+    g_return_val_if_fail(ab_trans && gnc_acc, NULL);
+
+    /* Create new GnuCash transaction for the given AqBanking one */
+    book = gnc_account_get_book(gnc_acc);
+    gnc_trans = xaccMallocTransaction(book);
+    xaccTransBeginEdit(gnc_trans);
+
+    /* Set OFX unique transaction ID */
+    fitid = AB_Transaction_GetFiId(ab_trans);
+    if (fitid && *fitid)
+        gnc_import_set_trans_online_id(gnc_trans, fitid);
+
+    /* Date / Time */
+    valuta_date = AB_Transaction_GetValutaDate(ab_trans);
+    if (!valuta_date) {
+        const GWEN_TIME *normal_date = AB_Transaction_GetDate(ab_trans);
+        if (normal_date)
+            valuta_date = normal_date;
+    }
+    if (valuta_date)
+        xaccTransSetDateSecs(gnc_trans, GWEN_Time_toTime_t(valuta_date));
+    else
+        g_warning("transaction_cb: Oops, date 'valuta_date' was NULL");
+
+    current_time = time(NULL);
+    xaccTransSetDateEnteredSecs(gnc_trans, mktime(localtime(&current_time)));
+
+    /* Currency.  We take simply the default currency of the gnucash account */
+    xaccTransSetCurrency(gnc_trans, xaccAccountGetCommodity(gnc_acc));
+
+    /* Number.  We use the "customer reference", if there is one. */
+    custref = AB_Transaction_GetCustomerReference(ab_trans);
+    if (custref && *custref
+        && g_ascii_strncasecmp(custref, "NONREF", 6) != 0)
+        xaccTransSetNum(gnc_trans, custref);
+
+    /* Description */
+    description = gnc_ab_description_to_gnc(ab_trans);
+    xaccTransSetDescription(gnc_trans, description);
+    g_free(description);
+
+    /* Notes. */
+    /* xaccTransSetNotes(gnc_trans, g_notes); */
+    /* But Nobody ever uses the Notes field? */
+
+    /* Add one split */
+    split = xaccMallocSplit(book);
+    xaccSplitSetParent(split, gnc_trans);
+    xaccSplitSetAccount(split, gnc_acc);
+
+    /* Amount into the split */
+    ab_value = AB_Transaction_GetValue(ab_trans);
+    gnc_amount = double_to_gnc_numeric(
+        ab_value ? AB_Value_GetValueAsDouble(ab_value) : 0.0,
+        xaccAccountGetCommoditySCU(gnc_acc),
+        GNC_RND_ROUND);
+    if (!ab_value)
+        g_warning("transaction_cb: Oops, value was NULL.  Using 0");
+    xaccSplitSetBaseValue(split, gnc_amount, xaccAccountGetCommodity(gnc_acc));
+
+    /* Memo in the Split. */
+    memo = gnc_ab_memo_to_gnc(ab_trans);
+    xaccSplitSetMemo(split, memo);
+    g_free(memo);
+
+    return gnc_trans;
+}
+
 AB_ACCOUNT_STATUS *
 gnc_ab_get_best_accountstatus(AB_IMEXPORTER_ACCOUNTINFO *acc_info)
 {

Modified: gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-ab-utils.h
===================================================================
--- gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-ab-utils.h	2008-06-11 23:26:19 UTC (rev 17217)
+++ gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-ab-utils.h	2008-06-11 23:59:46 UTC (rev 17218)
@@ -41,6 +41,9 @@
 G_BEGIN_DECLS
 
 #define GCONF_SECTION_AQBANKING "dialogs/import/hbci"
+#define KEY_FORMAT_SWIFT940 "format_swift_mt940"
+#define KEY_FORMAT_SWIFT942 "format_swift_mt942"
+#define KEY_FORMAT_DTAUS "format_dtaus"
 
 /**
  * Initialize the gwenhywfar library by calling GWEN_Init() and setting up
@@ -143,6 +146,16 @@
 gchar *gnc_ab_memo_to_gnc(const AB_TRANSACTION *ab_trans);
 
 /**
+ * Create an unbalanced and dirty GnuCash transaction with a split to @a gnc_acc
+ * from the information available in the AqBanking transaction @a ab_trans.
+ *
+ * @param ab_trans AqBanking transaction
+ * @param gnc_acc Account of to use for the split
+ * @return A dirty GnuCash transaction or NULL otherwise
+ */
+Transaction *gnc_ab_trans_to_gnc(const AB_TRANSACTION *ab_trans, Account *gnc_acc);
+
+/**
  * Lookup the most recent ACCOUNT_STATUS available in an ACCOUNTINFO as
  * extracted from an IMEXPORTER_CONTEXT.  This can be used to determine the
  * reported account balance most up-to-date.

Added: gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-file-aqb-import.c
===================================================================
--- gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-file-aqb-import.c	                        (rev 0)
+++ gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-file-aqb-import.c	2008-06-11 23:59:46 UTC (rev 17218)
@@ -0,0 +1,344 @@
+/*
+ * gnc-file-aqb-import.c --
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+/**
+ * @internal
+ * @file gnc-file-aqb-import.c
+ * @brief DTAUS import module code
+ * @author Copyright (C) 2002 Benoit Grégoire <bock at step.polymtl.ca>
+ * @author Copyright (C) 2003 Jan-Pascal van Best <janpascal at vanbest.org>
+ * @author Copyright (C) 2006 Florian Steinel
+ * @author Copyright (C) 2006 Christian Stimming
+ * @author Copyright (C) 2008 Andreas Koehler <andi5.py at gmx.net>
+ */
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+#include <glib/gstdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <gwenhywfar/io_file.h>
+#include <gwenhywfar/io_buffered.h>
+#include <gwenhywfar/iomanager.h>
+
+#include "dialog-ab-trans.h"
+#include "gnc-ab-utils.h"
+#include "gnc-file.h"
+#include "gnc-file-aqb-import.h"
+#include "gnc-gwen-gui.h"
+#include "gnc-ui.h"
+#include "gnc-ui-util.h"
+#include "import-account-matcher.h"
+#include "import-main-matcher.h"
+
+/* This static indicates the debugging module that this .o belongs to.  */
+static QofLogModule log_module = GNC_MOD_IMPORT;
+
+typedef struct _ImportData ImportData;
+
+static const AB_TRANSACTION *transaction_cb(
+    const AB_TRANSACTION *element, gpointer user_data);
+static AB_IMEXPORTER_ACCOUNTINFO *accountinfo_cb(
+    AB_IMEXPORTER_ACCOUNTINFO *element, gpointer user_data);
+static AB_JOB_LIST2 *import_context(
+    AB_BANKING *api, AB_IMEXPORTER_CONTEXT *context,
+    GNCImportMainMatcher *importer_generic_gui, gboolean execute_transactions);
+
+struct _ImportData {
+    AB_BANKING *api;
+    GNCImportMainMatcher *importer_generic;
+    gboolean execute_transactions;
+    AB_JOB_LIST2 *job_list;
+    Account *gnc_acc;
+    AB_ACCOUNT *ab_acc;
+};
+
+static const AB_TRANSACTION *transaction_cb(const AB_TRANSACTION *element,
+                                            gpointer user_data)
+{
+    ImportData *data = user_data;
+    Transaction *gnc_trans;
+    AB_JOB *job;
+
+    g_return_val_if_fail(element && data, NULL);
+
+    /* Create a GnuCash transaction from ab_trans */
+    gnc_trans = gnc_ab_trans_to_gnc(element, data->gnc_acc);
+
+    /* Instead of xaccTransCommitEdit(gnc_trans)  */
+    gnc_gen_trans_list_add_trans(data->importer_generic, gnc_trans);
+
+    if (data->ab_acc) {
+        AB_TRANSACTION *ab_trans = AB_Transaction_dup(element);
+
+        /* NEW: The imported transaction has been imported into gnucash.
+         * Now also add it as a job to aqbanking */
+        AB_Transaction_SetLocalBankCode(
+            ab_trans, AB_Account_GetBankCode(data->ab_acc));
+        AB_Transaction_SetLocalAccountNumber(
+            ab_trans, AB_Account_GetAccountNumber(data->ab_acc));
+        AB_Transaction_SetLocalCountry(ab_trans, "DE");
+
+        job = gnc_ab_get_trans_job(data->ab_acc, ab_trans, SINGLE_DEBITNOTE);
+
+        /* Check whether we really got a job */
+        if (!job) {
+            /* Oops, no job, probably not supported by bank */
+            if (gnc_verify_dialog(
+                    NULL, FALSE, "%s",
+                    _("The backend found an error during the preparation "
+                      "of the job. It is not possible to execute this job. \n"
+                      "\n"
+                      "Most probable the bank does not support your chosen "
+                      "job or your Online Banking account does not have the permission "
+                      "to execute this job. More error messages might be "
+                      "visible on your console log.\n"
+                      "\n"
+                      "Do you want to enter the job again?"))) {
+                gnc_error_dialog(NULL, "Sorry, not implemented yet.");
+            }
+            /* else */
+        }
+        AB_Job_List2_PushBack(data->job_list, job);
+
+        AB_Transaction_free(ab_trans);
+    }
+
+    return NULL;
+}
+
+static AB_IMEXPORTER_ACCOUNTINFO *
+accountinfo_cb(AB_IMEXPORTER_ACCOUNTINFO *element, gpointer user_data)
+{
+    Account *gnc_acc;
+    ImportData *data = user_data;
+    const gchar *bank_code =
+        AB_ImExporterAccountInfo_GetBankCode(element);
+    const gchar *account_number =
+        AB_ImExporterAccountInfo_GetAccountNumber(element);
+    const gchar *account_name =
+        AB_ImExporterAccountInfo_GetAccountName(element);
+    gchar *online_id;
+
+    g_return_val_if_fail(element && data, NULL);
+
+    online_id = g_strconcat(bank_code, account_number, (gchar*)NULL);
+    gnc_acc = gnc_import_select_account(NULL, online_id, 1, account_name, NULL,
+                                        ACCT_TYPE_NONE, NULL, NULL);
+    g_free(online_id);
+
+    if (gnc_acc) {
+        /* Store chosen gnucash account in callback data */
+        data->gnc_acc = gnc_acc;
+
+        if (data->execute_transactions) {
+            /* Retrieve the aqbanking account that belongs to this gnucash
+             * account */
+            data->ab_acc = gnc_ab_get_ab_account(data->api, gnc_acc);
+            if (!data->ab_acc) {
+                gnc_error_dialog(NULL, "%s",
+                                 _("No Online Banking account found for this "
+                                   "gnucash account. These transactions will "
+                                   "not be executed by Online Banking."));
+            }
+        } else {
+            data->ab_acc = NULL;
+        }
+
+        /* Iterate through all transactions.  */
+        AB_ImExporterAccountInfo_TransactionsForEach(element, transaction_cb,
+                                                     data);
+    }
+    return NULL;
+}
+
+static AB_JOB_LIST2 *
+import_context(AB_BANKING *api, AB_IMEXPORTER_CONTEXT *context,
+               GNCImportMainMatcher *importer_generic_gui,
+               gboolean execute_transactions)
+{
+    ImportData data;
+
+    g_return_val_if_fail(api && context && importer_generic_gui, NULL);
+    data.api = api;
+    data.importer_generic = importer_generic_gui;
+    data.execute_transactions = execute_transactions;
+    data.job_list = NULL;
+
+    /* Iterate through all accounts */
+    AB_ImExporterContext_AccountInfoForEach(context, accountinfo_cb, &data);
+
+    return data.job_list;
+}
+
+void
+gnc_file_aqbanking_import(const gchar *aqbanking_importername,
+                          const gchar *aqbanking_profilename,
+                          gboolean execute_transactions)
+{
+    gchar *default_dir;
+    gchar *selected_filename = NULL;
+    gint dtaus_fd = -1;
+    AB_BANKING *api = NULL;
+    gboolean online = FALSE;
+    GncGWENGui *gui = NULL;
+    AB_IMEXPORTER *importer;
+    GWEN_DB_NODE *db_profiles = NULL;
+    GWEN_DB_NODE *db_profile;
+    AB_IMEXPORTER_CONTEXT *context = NULL;
+    GWEN_IO_LAYER *io, *buffio;
+    GNCImportMainMatcher *importer_generic_gui = NULL;
+    AB_JOB_LIST2 *job_list = NULL;
+
+    /* Select a file */
+    default_dir = gnc_get_default_directory(GCONF_SECTION_AQBANKING);
+    selected_filename = gnc_file_dialog(_("Select a file to import"),
+                                        NULL, default_dir,
+                                        GNC_FILE_DIALOG_IMPORT);
+    g_free(default_dir);
+
+    if (!selected_filename)
+        goto cleanup;
+    DEBUG("filename: %s", selected_filename);
+
+    /* Remember the directory as the default */
+    default_dir = g_path_get_dirname(selected_filename);
+    gnc_set_default_directory(GCONF_SECTION_AQBANKING, default_dir);
+    g_free(default_dir);
+
+    dtaus_fd = g_open(selected_filename, O_RDONLY, 0);
+    if (dtaus_fd == -1) {
+      DEBUG("Could not open file %s", selected_filename);
+      goto cleanup;
+    }
+
+    /* Get the API */
+    api = gnc_AB_BANKING_new();
+    if (!api) {
+        g_warning("gnc_file_aqbanking_import: Couldn't get AqBanking API");
+        goto cleanup;
+    }
+    if (AB_Banking_OnlineInit(api) != 0) {
+        g_warning("gnc_file_aqbanking_import: "
+                  "Couldn't initialize AqBanking API");
+        goto cleanup;
+    }
+    online = TRUE;
+
+    /* Get a GUI object */
+    gui = gnc_GWEN_Gui_get(NULL);
+    if (!gui) {
+        g_warning("gnc_ab_getbalance: Couldn't initialize Gwenhywfar GUI");
+        goto cleanup;
+    }
+
+    /* Get import module */
+    importer = AB_Banking_GetImExporter(api, aqbanking_importername);
+    if (!importer) {
+        g_warning("Import module %s not found", aqbanking_importername);
+        gnc_error_dialog(NULL, "%s",
+                         _("Import module for DTAUS import not found."));
+        goto cleanup;
+    }
+
+    /* Load the import profile */
+    db_profiles = AB_Banking_GetImExporterProfiles(api, aqbanking_importername);
+
+    /* Select profile */
+    db_profile = GWEN_DB_GetFirstGroup(db_profiles);
+    while (db_profile) {
+        const gchar *name;
+
+        name = GWEN_DB_GetCharValue(db_profile, "name", 0, 0);
+        g_return_if_fail(name);
+        if (g_ascii_strcasecmp(name, aqbanking_profilename)==0)
+            break;
+        db_profile = GWEN_DB_GetNextGroup(db_profile);
+    }
+    if (!db_profile) {
+        g_warning("Profile \"%s\" for importer \"%s\" not found",
+                  aqbanking_profilename, aqbanking_importername);
+        /* For debugging: Print those available names that have been found */
+        db_profile = GWEN_DB_GetFirstGroup(db_profiles);
+        while (db_profile) {
+            const char *name = GWEN_DB_GetCharValue(db_profile, "name", 0, 0);
+            g_warning("Only found profile \"%s\"\n", name ? name : "(null)");
+            db_profile = GWEN_DB_GetNextGroup(db_profile);
+        }
+        goto cleanup;
+    }
+
+    /* Create a context to store the results */
+    context = AB_ImExporterContext_new();
+
+    /* Wrap file in buffered gwen io */
+    io = GWEN_Io_LayerFile_new(dtaus_fd, -1);
+    dtaus_fd = -1;
+    buffio = GWEN_Io_LayerBuffered_new(io);
+    if (GWEN_Io_Manager_RegisterLayer(buffio)) {
+        g_warning("gnc_file_aqbanking_import: Failed to wrap file");
+        goto cleanup;
+    }
+
+    /* Run the import */
+    if (AB_ImExporter_Import(importer, context, buffio, db_profile, 0)) {
+        g_warning("gnc_file_aqbanking_import: Error on import");
+        goto cleanup;
+    }
+
+    /* Close the file */
+    GWEN_Io_Layer_free(buffio);
+
+    /* Create importer GUI */
+    importer_generic_gui = gnc_gen_trans_list_new(NULL, NULL, TRUE, 14);
+
+    /* Import the transactions from the context into gnucash */
+    job_list = import_context(api, context, importer_generic_gui,
+                              execute_transactions);
+
+    if (execute_transactions) {
+        if (gnc_gen_trans_list_run(importer_generic_gui)) {
+            /* FIXME */
+            /* gnc_hbci_multijob_execute(NULL, api, job_list, gui); */
+        }
+    }
+
+cleanup:
+    if (job_list)
+        AB_Job_List2_free(job_list);
+    if (importer_generic_gui)
+        gnc_gen_trans_list_delete(importer_generic_gui);
+    if (context)
+        AB_ImExporterContext_free(context);
+    if (db_profiles)
+        GWEN_DB_Group_free(db_profiles);
+    if (gui)
+        gnc_GWEN_Gui_release(gui);
+    if (online)
+        AB_Banking_OnlineFini(api);
+    if (api)
+        gnc_AB_BANKING_fini(api);
+    if (dtaus_fd != -1)
+        close(dtaus_fd);
+    if (selected_filename)
+        g_free(selected_filename);
+}

Added: gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-file-aqb-import.h
===================================================================
--- gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-file-aqb-import.h	                        (rev 0)
+++ gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-file-aqb-import.h	2008-06-11 23:59:46 UTC (rev 17218)
@@ -0,0 +1,69 @@
+/*
+ * gnc-file-aqb-import.h --
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of
+ * the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, contact:
+ *
+ * Free Software Foundation           Voice:  +1-617-542-5942
+ * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
+ * Boston, MA  02110-1301,  USA       gnu at gnu.org
+ */
+
+/**
+ * @addtogroup Import_Export
+ * @{
+ * @addtogroup AqBanking
+ * @{
+ * @file gnc-file-aqb-import.h
+ * @brief DTAUS import module interface
+ * @author Copyright (C) 2002 Benoit Grégoire <bock at step.polymtl.ca>
+ * @author Copyright (C) 2008 Andreas Koehler <andi5.py at gmx.net>
+ */
+
+#ifndef GNC_FILE_AQB_IMPORT_H
+#define GNC_FILE_AQB_IMPORT_H
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+/**
+ * This routine will pop up a standard file selection dialog asking the user to
+ * pick a file to import.  This file will be opened and read.  Its contents will
+ * be imported into the current book, using the import matcher from
+ * import-main-matcher.h.
+ *
+ * @param aqbanking_importername The aqbanking importer module that should be
+ * used.  Possible values: "dtaus", "csv", "swift", or more.
+ *
+ * @param aqbanking_formatname In aqbanking, each importer has one or more data
+ * formats available which define the actual data fields that should be used.
+ * In aqbanking, such a different format is called a "profile".  Possible values
+ * for swift: "swift-mt940" or "swift-mt942", but for all others: "default", or
+ * more precisely: Look into $datadir/aqbanking/imexporters and look into the
+ * "name" field of the foo.conf files.
+ *
+ * @param exec_as_aqbanking_jobs If TRUE, additionally queue the imported
+ * transactions as online jobs over aqbanking/HBCI.  If FALSE, just import the
+ * transactions and that's it.
+ */
+void gnc_file_aqbanking_import (const gchar *aqbanking_importername,
+                                const gchar *aqbanking_formatname,
+                                gboolean exec_as_aqbanking_jobs);
+
+G_END_DECLS
+
+/** @} */
+/** @} */
+
+#endif /* GNC_FILE_AQB_IMPORT_H */

Modified: gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-plugin-aqbanking.c
===================================================================
--- gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-plugin-aqbanking.c	2008-06-11 23:26:19 UTC (rev 17217)
+++ gnucash/branches/aqbanking3/src/import-export/aqbanking/gnc-plugin-aqbanking.c	2008-06-11 23:59:46 UTC (rev 17218)
@@ -38,6 +38,9 @@
 #include "gnc-ab-getbalance.h"
 #include "gnc-ab-gettrans.h"
 #include "gnc-ab-transfer.h"
+#include "gnc-ab-utils.h"
+#include "gnc-file-aqb-import.h"
+#include "gnc-gconf-utils.h"
 #include "gnc-plugin-aqbanking.h"
 #include "gnc-plugin-manager.h"
 #include "gnc-plugin-page-account-tree.h"
@@ -62,10 +65,14 @@
 /* Command callbacks */
 static void gnc_plugin_ab_cmd_setup(GtkAction *action, GncMainWindowActionData *data);
 static void gnc_plugin_ab_cmd_get_balance(GtkAction *action, GncMainWindowActionData *data);
-static void gnc_plugin_ab_cmd_get_transactions (GtkAction *action, GncMainWindowActionData *data);
-static void gnc_plugin_ab_cmd_issue_transaction (GtkAction *action, GncMainWindowActionData *data);
-static void gnc_plugin_ab_cmd_issue_inttransaction (GtkAction *action, GncMainWindowActionData *data);
-static void gnc_plugin_ab_cmd_issue_direct_debit (GtkAction *action, GncMainWindowActionData *data);
+static void gnc_plugin_ab_cmd_get_transactions(GtkAction *action, GncMainWindowActionData *data);
+static void gnc_plugin_ab_cmd_issue_transaction(GtkAction *action, GncMainWindowActionData *data);
+static void gnc_plugin_ab_cmd_issue_inttransaction(GtkAction *action, GncMainWindowActionData *data);
+static void gnc_plugin_ab_cmd_issue_direct_debit(GtkAction *action, GncMainWindowActionData *data);
+static void gnc_plugin_ab_cmd_mt940_import(GtkAction *action, GncMainWindowActionData *data);
+static void gnc_plugin_ab_cmd_mt942_import(GtkAction *action, GncMainWindowActionData *data);
+static void gnc_plugin_ab_cmd_dtaus_import(GtkAction *action, GncMainWindowActionData *data);
+static void gnc_plugin_ab_cmd_dtaus_importsend(GtkAction *action, GncMainWindowActionData *data);
 
 #define PLUGIN_ACTIONS_NAME "gnc-plugin-aqbanking-actions"
 #define PLUGIN_UI_FILENAME  "gnc-plugin-aqbanking-ui.xml"
@@ -94,16 +101,16 @@
       N_("Issue a new direct debit note online through Online Banking"),
       G_CALLBACK(gnc_plugin_ab_cmd_issue_direct_debit) },
 
-/*   /\* File -> Import menu item *\/ */
-/*     { "Mt940ImportAction", GTK_STOCK_CONVERT, N_("Import _MT940"), NULL, */
-/*       N_("Import a MT940 file into GnuCash"), */
-/*       G_CALLBACK(gnc_plugin_ab_cmd_mt940_import) }, */
-/*     { "Mt942ImportAction", GTK_STOCK_CONVERT, N_("Import MT94_2"), NULL, */
-/*       N_("Import a MT942 file into GnuCash"), */
-/*       G_CALLBACK(gnc_plugin_ab_cmd_mt942_import) }, */
-/*     { "DtausImportAction", GTK_STOCK_CONVERT, N_("Import _DTAUS"), NULL, */
-/*       N_("Import a DTAUS file into GnuCash"), */
-/*       G_CALLBACK(gnc_plugin_ab_cmd_dtaus_import) }, */
+  /* File -> Import menu item */
+    { "Mt940ImportAction", GTK_STOCK_CONVERT, N_("Import _MT940"), NULL,
+      N_("Import a MT940 file into GnuCash"),
+      G_CALLBACK(gnc_plugin_ab_cmd_mt940_import) },
+    { "Mt942ImportAction", GTK_STOCK_CONVERT, N_("Import MT94_2"), NULL,
+      N_("Import a MT942 file into GnuCash"),
+      G_CALLBACK(gnc_plugin_ab_cmd_mt942_import) },
+    { "DtausImportAction", GTK_STOCK_CONVERT, N_("Import _DTAUS"), NULL,
+      N_("Import a DTAUS file into GnuCash"),
+      G_CALLBACK(gnc_plugin_ab_cmd_dtaus_import) },
 /* #ifdef CSV_IMPORT_FUNCTIONAL */
 /*     { "CsvImportAction", GTK_STOCK_CONVERT, N_("Import _CSV"), NULL, */
 /*       N_("Import a CSV file into GnuCash"), */
@@ -112,9 +119,9 @@
 /*       N_("Import a CSV file into GnuCash and send the transfers online through Online Banking"), */
 /*       G_CALLBACK(gnc_plugin_ab_cmd_csv_importsend) }, */
 /* #endif */
-/*     { "DtausImportSendAction", GTK_STOCK_CONVERT, N_("Import DTAUS and _send..."), NULL, */
-/*       N_("Import a DTAUS file into GnuCash and send the transfers online through Online Banking"), */
-/*       G_CALLBACK(gnc_plugin_ab_cmd_dtaus_importsend) }, */
+    { "DtausImportSendAction", GTK_STOCK_CONVERT, N_("Import DTAUS and _send..."), NULL,
+      N_("Import a DTAUS file into GnuCash and send the transfers online through Online Banking"),
+      G_CALLBACK(gnc_plugin_ab_cmd_dtaus_importsend) },
 };
 static guint gnc_plugin_n_actions = G_N_ELEMENTS(gnc_plugin_actions);
 
@@ -414,6 +421,43 @@
     LEAVE(" ");
 }
 
+static void
+gnc_plugin_ab_cmd_mt940_import(GtkAction *action, GncMainWindowActionData *data)
+{
+    gchar *format = gnc_gconf_get_string(GCONF_SECTION_AQBANKING,
+                                         KEY_FORMAT_SWIFT940, NULL);
+    gnc_file_aqbanking_import("swift", format ? format : "swift-mt940", FALSE);
+    g_free (format);
+}
+
+static void
+gnc_plugin_ab_cmd_mt942_import(GtkAction *action, GncMainWindowActionData *data)
+{
+    gchar *format = gnc_gconf_get_string(GCONF_SECTION_AQBANKING,
+                                         KEY_FORMAT_SWIFT942, NULL);
+    gnc_file_aqbanking_import("swift", format ? format : "swift-mt942", FALSE);
+    g_free (format);
+}
+
+static void
+gnc_plugin_ab_cmd_dtaus_import(GtkAction *action, GncMainWindowActionData *data)
+{
+    gchar *format = gnc_gconf_get_string(GCONF_SECTION_AQBANKING,
+                                         KEY_FORMAT_DTAUS, NULL);
+    gnc_file_aqbanking_import("dtaus", format ? format : "default", FALSE);
+    g_free (format);
+}
+
+static void
+gnc_plugin_ab_cmd_dtaus_importsend(GtkAction *action,
+                                   GncMainWindowActionData *data)
+{
+    gchar *format = gnc_gconf_get_string(GCONF_SECTION_AQBANKING,
+                                         KEY_FORMAT_DTAUS, NULL);
+    gnc_file_aqbanking_import("dtaus", format ? format : "default", TRUE);
+    g_free (format);
+}
+
 /************************************************************
  *                    Plugin Bootstrapping                  *
  ************************************************************/



More information about the gnucash-changes mailing list