GnuCash 2.4.99
gnc-ab-getbalance.c
00001 /*
00002  * gnc-ab-getbalance.c --
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of
00007  * the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, contact:
00016  *
00017  * Free Software Foundation           Voice:  +1-617-542-5942
00018  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
00019  * Boston, MA  02110-1301,  USA       gnu@gnu.org
00020  */
00021 
00030 #include "config.h"
00031 
00032 #include <glib/gi18n.h>
00033 #include <aqbanking/banking.h>
00034 #include <aqbanking/jobgetbalance.h>
00035 
00036 #include "gnc-ab-getbalance.h"
00037 #include "gnc-ab-kvp.h"
00038 #include "gnc-ab-utils.h"
00039 #include "gnc-gwen-gui.h"
00040 #include "gnc-ui.h"
00041 
00042 /* This static indicates the debugging module that this .o belongs to.  */
00043 static QofLogModule log_module = G_LOG_DOMAIN;
00044 
00045 void
00046 gnc_ab_getbalance(GtkWidget *parent, Account *gnc_acc)
00047 {
00048     AB_BANKING *api;
00049     gboolean online = FALSE;
00050     AB_ACCOUNT *ab_acc;
00051     AB_JOB *job = NULL;
00052     AB_JOB_LIST2 *job_list = NULL;
00053     GncGWENGui *gui = NULL;
00054     AB_IMEXPORTER_CONTEXT *context = NULL;
00055     GncABImExContextImport *ieci = NULL;
00056     AB_JOB_STATUS job_status;
00057 
00058     g_return_if_fail(parent && gnc_acc);
00059 
00060     /* Get the API */
00061     api = gnc_AB_BANKING_new();
00062     if (!api)
00063     {
00064         g_warning("gnc_ab_gettrans: Couldn't get AqBanking API");
00065         return;
00066     }
00067     if (AB_Banking_OnlineInit(api
00068 #ifdef AQBANKING_VERSION_4_EXACTLY
00069                               , 0
00070 #endif
00071                              ) != 0)
00072     {
00073         g_warning("gnc_ab_gettrans: Couldn't initialize AqBanking API");
00074         goto cleanup;
00075     }
00076     online = TRUE;
00077 
00078     /* Get the AqBanking Account */
00079     ab_acc = gnc_ab_get_ab_account(api, gnc_acc);
00080     if (!ab_acc)
00081     {
00082         g_warning("gnc_ab_getbalance: No AqBanking account found");
00083         gnc_error_dialog(parent, _("No valid online banking account assigned."));
00084         goto cleanup;
00085     }
00086 
00087     /* Get a GetBalance job and enqueue it */
00088     job = AB_JobGetBalance_new(ab_acc);
00089     if (!job || AB_Job_CheckAvailability(job
00090 #ifndef AQBANKING_VERSION_5_PLUS
00091                                          , 0
00092 #endif
00093                                         ))
00094     {
00095         g_warning("gnc_ab_getbalance: JobGetBalance not available for this "
00096                   "account");
00097         gnc_error_dialog(parent, _("Online action \"Get Balance\" not available for this account."));
00098         goto cleanup;
00099     }
00100     job_list = AB_Job_List2_new();
00101     AB_Job_List2_PushBack(job_list, job);
00102 
00103     /* Get a GUI object */
00104     gui = gnc_GWEN_Gui_get(parent);
00105     if (!gui)
00106     {
00107         g_warning("gnc_ab_getbalance: Couldn't initialize Gwenhywfar GUI");
00108         goto cleanup;
00109     }
00110 
00111     /* Create a context to store the results */
00112     context = AB_ImExporterContext_new();
00113 
00114     /* Execute the job */
00115     AB_Banking_ExecuteJobs(api, job_list, context
00116 #ifndef AQBANKING_VERSION_5_PLUS
00117                            , 0
00118 #endif
00119                           );
00120     /* Ignore the return value of AB_Banking_ExecuteJobs(), as the job's
00121      * status always describes better whether the job was actually
00122      * transferred to and accepted by the bank.  See also
00123      * http://lists.gnucash.org/pipermail/gnucash-de/2008-September/006389.html
00124      */
00125     job_status = AB_Job_GetStatus(job);
00126     if (job_status != AB_Job_StatusFinished
00127             && job_status != AB_Job_StatusPending)
00128     {
00129         g_warning("gnc_ab_getbalance: Error on executing job");
00130         gnc_error_dialog(parent, _("Error on executing job.\n\nStatus: %s - %s")
00131                          , AB_Job_Status2Char(job_status)
00132                          , AB_Job_GetResultText(job));
00133         goto cleanup;
00134     }
00135 
00136     /* Import the results */
00137     ieci = gnc_ab_import_context(context, AWAIT_BALANCES, FALSE, NULL, parent);
00138 
00139 cleanup:
00140     if (ieci)
00141         g_free(ieci);
00142     if (context)
00143         AB_ImExporterContext_free(context);
00144     if (gui)
00145         gnc_GWEN_Gui_release(gui);
00146     if (job_list)
00147         AB_Job_List2_free(job_list);
00148     if (job)
00149         AB_Job_free(job);
00150     if (online)
00151 #ifdef AQBANKING_VERSION_4_EXACTLY
00152         AB_Banking_OnlineFini(api, 0);
00153 #else
00154         AB_Banking_OnlineFini(api);
00155 #endif
00156     gnc_AB_BANKING_fini(api);
00157 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines