GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-ab-getbalance.c
1 /*
2  * gnc-ab-getbalance.c --
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of
7  * the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, contact:
16  *
17  * Free Software Foundation Voice: +1-617-542-5942
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
19  * Boston, MA 02110-1301, USA [email protected]
20  */
21 
30 #include "config.h"
31 
32 #include <glib/gi18n.h>
33 #include <aqbanking/banking.h>
34 #include <aqbanking/jobgetbalance.h>
35 
36 #include "gnc-ab-getbalance.h"
37 #include "gnc-ab-kvp.h"
38 #include "gnc-ab-utils.h"
39 #include "gnc-gwen-gui.h"
40 #include "gnc-ui.h"
41 
42 /* This static indicates the debugging module that this .o belongs to. */
43 G_GNUC_UNUSED static QofLogModule log_module = G_LOG_DOMAIN;
44 
45 void
46 gnc_ab_getbalance(GtkWidget *parent, Account *gnc_acc)
47 {
48  AB_BANKING *api;
49  gboolean online = FALSE;
50  AB_ACCOUNT *ab_acc;
51  AB_JOB *job = NULL;
52  AB_JOB_LIST2 *job_list = NULL;
53  GncGWENGui *gui = NULL;
54  AB_IMEXPORTER_CONTEXT *context = NULL;
55  GncABImExContextImport *ieci = NULL;
56  AB_JOB_STATUS job_status;
57 
58  g_return_if_fail(parent && gnc_acc);
59 
60  /* Get the API */
61  api = gnc_AB_BANKING_new();
62  if (!api)
63  {
64  g_warning("gnc_ab_gettrans: Couldn't get AqBanking API");
65  return;
66  }
67  if (AB_Banking_OnlineInit(api
68 #ifdef AQBANKING_VERSION_4_EXACTLY
69  , 0
70 #endif
71  ) != 0)
72  {
73  g_warning("gnc_ab_gettrans: Couldn't initialize AqBanking API");
74  goto cleanup;
75  }
76  online = TRUE;
77 
78  /* Get the AqBanking Account */
79  ab_acc = gnc_ab_get_ab_account(api, gnc_acc);
80  if (!ab_acc)
81  {
82  g_warning("gnc_ab_getbalance: No AqBanking account found");
83  gnc_error_dialog(parent, _("No valid online banking account assigned."));
84  goto cleanup;
85  }
86 
87  /* Get a GetBalance job and enqueue it */
88  job = AB_JobGetBalance_new(ab_acc);
89  if (!job || AB_Job_CheckAvailability(job
90 #ifndef AQBANKING_VERSION_5_PLUS
91  , 0
92 #endif
93  ))
94  {
95  g_warning("gnc_ab_getbalance: JobGetBalance not available for this "
96  "account");
97  gnc_error_dialog(parent, _("Online action \"Get Balance\" not available for this account."));
98  goto cleanup;
99  }
100  job_list = AB_Job_List2_new();
101  AB_Job_List2_PushBack(job_list, job);
102 
103  /* Get a GUI object */
104  gui = gnc_GWEN_Gui_get(parent);
105  if (!gui)
106  {
107  g_warning("gnc_ab_getbalance: Couldn't initialize Gwenhywfar GUI");
108  goto cleanup;
109  }
110 
111  /* Create a context to store the results */
112  context = AB_ImExporterContext_new();
113 
114  /* Execute the job */
115  AB_Banking_ExecuteJobs(api, job_list, context
116 #ifndef AQBANKING_VERSION_5_PLUS
117  , 0
118 #endif
119  );
120  /* Ignore the return value of AB_Banking_ExecuteJobs(), as the job's
121  * status always describes better whether the job was actually
122  * transferred to and accepted by the bank. See also
123  * http://lists.gnucash.org/pipermail/gnucash-de/2008-September/006389.html
124  */
125  job_status = AB_Job_GetStatus(job);
126  if (job_status != AB_Job_StatusFinished
127  && job_status != AB_Job_StatusPending)
128  {
129  g_warning("gnc_ab_getbalance: Error on executing job");
130  gnc_error_dialog(parent, _("Error on executing job.\n\nStatus: %s - %s")
131  , AB_Job_Status2Char(job_status)
132  , AB_Job_GetResultText(job));
133  goto cleanup;
134  }
135 
136  /* Import the results */
137  ieci = gnc_ab_import_context(context, AWAIT_BALANCES, FALSE, NULL, parent);
138 
139 cleanup:
140  if (ieci)
141  g_free(ieci);
142  if (context)
143  AB_ImExporterContext_free(context);
144  if (gui)
146  if (job_list)
147  AB_Job_List2_free(job_list);
148  if (job)
149  AB_Job_free(job);
150  if (online)
151 #ifdef AQBANKING_VERSION_4_EXACTLY
152  AB_Banking_OnlineFini(api, 0);
153 #else
154  AB_Banking_OnlineFini(api);
155 #endif
156  gnc_AB_BANKING_fini(api);
157 }
#define G_LOG_DOMAIN
Functions providing the SX List as a plugin page.
GncGWENGui * gnc_GWEN_Gui_get(GtkWidget *parent)
Definition: gnc-gwen-gui.c:311
AqBanking getbalance functions.
void gnc_GWEN_Gui_release(GncGWENGui *gui)
Definition: gnc-gwen-gui.c:346
AB_BANKING * gnc_AB_BANKING_new(void)
Definition: gnc-ab-utils.c:137
gint gnc_AB_BANKING_fini(AB_BANKING *api)
Definition: gnc-ab-utils.c:237
AB_ACCOUNT * gnc_ab_get_ab_account(const AB_BANKING *api, Account *gnc_acc)
Definition: gnc-ab-utils.c:264
GncABImExContextImport * gnc_ab_import_context(AB_IMEXPORTER_CONTEXT *context, guint awaiting, gboolean execute_txns, AB_BANKING *api, GtkWidget *parent)
void gnc_ab_getbalance(GtkWidget *parent, Account *gnc_acc)
AqBanking KVP handling.
GUI callbacks for AqBanking.
AqBanking utility functions.
const gchar * QofLogModule
Definition: qofid.h:89