GnuCash  5.6-150-g038405b370+
gnc-file-aqb-import.c
1 /*
2  * gnc-file-aqb-import.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 gnu@gnu.org
20  */
21 
34 #include <config.h>
35 
36 #include <platform.h>
37 #if PLATFORM(WINDOWS)
38 #include <windows.h>
39 #endif
40 
41 #include <glib/gi18n.h>
42 #include <glib/gstdio.h>
43 #include <fcntl.h>
44 #include <unistd.h>
45 
46 #include "gnc-ab-utils.h"
47 
48 #include <gwenhywfar/syncio_file.h>
49 #include <gwenhywfar/syncio_buffered.h>
50 #include <gwenhywfar/gui.h>
51 typedef GWEN_SYNCIO GWEN_IO_LAYER;
52 
54 #include "dialog-ab-trans.h"
55 #include "dialog-utils.h"
56 #include "gnc-file.h"
57 #include "gnc-file-aqb-import.h"
58 #include "gnc-gwen-gui.h"
59 #include "gnc-ui.h"
60 #include "gnc-ui-util.h"
61 #include "import-account-matcher.h"
62 #include "import-main-matcher.h"
63 #include <gnc-state.h>
64 
65 /* This static indicates the debugging module that this .o belongs to. */
66 static QofLogModule log_module = GNC_MOD_IMPORT;
67 
68 static AB_IMEXPORTER_CONTEXT*
69 named_import_get_context (GtkWindow *parent, AB_BANKING *api,
70  const gchar *aqbanking_importername,
71  const gchar *aqbanking_profilename)
72 {
73  AB_IMEXPORTER_CONTEXT *context;
74  int success;
75  /* Select a file */
76  char *default_dir = gnc_get_default_directory(GNC_PREFS_GROUP_AQBANKING);
77  char *selected_filename =
78  gnc_file_dialog(parent, _("Select a file to import"),
79  NULL, default_dir, GNC_FILE_DIALOG_IMPORT);
80  g_free(default_dir);
81 
82  if (!selected_filename)
83  return NULL;
84  DEBUG("filename: %s", selected_filename);
85 
86  /* Remember the directory as the default */
87  default_dir = g_path_get_dirname(selected_filename);
88  gnc_set_default_directory(GNC_PREFS_GROUP_AQBANKING, default_dir);
89  g_free(default_dir);
90 
91 /* Create a context to store the results */
92  context = AB_ImExporterContext_new();
93  success =
94  AB_Banking_ImportFromFileLoadProfile(api, aqbanking_importername,
95  context, aqbanking_profilename,
96  NULL, selected_filename);
97  g_free (selected_filename);
98  if (success < 0)
99  {
100  AB_ImExporterContext_free(context);
101  g_warning("gnc_file_aqbanking_import: Error on import");
102  return NULL;
103  }
104  return context;
105 }
106 
107 static const char *GNC_STATE_SECTION = "dialogs.aqb.file-import";
108 static const char *STATE_KEY_LAST_FORMAT = "format";
109 static const char *STATE_KEY_LAST_PROFILE = "profile";
110 
111 static void
112 load_imexporter_and_profile(char** imexporter, char** profile)
113 {
114  GKeyFile *state_file = gnc_state_get_current();
115 
116  if (g_key_file_has_key(state_file, GNC_STATE_SECTION, STATE_KEY_LAST_FORMAT, NULL))
117  *imexporter = g_key_file_get_string (state_file, GNC_STATE_SECTION, STATE_KEY_LAST_FORMAT, NULL);
118 
119  if (g_key_file_has_key(state_file, GNC_STATE_SECTION, STATE_KEY_LAST_PROFILE, NULL))
120  *profile = g_key_file_get_string (state_file, GNC_STATE_SECTION, STATE_KEY_LAST_PROFILE, NULL);
121 }
122 
123 static void
124 save_imexporter_and_profile(const char* imexporter, const char *profile)
125 {
126  GKeyFile *state_file = gnc_state_get_current();
127 
128  g_key_file_set_string(state_file, GNC_STATE_SECTION, STATE_KEY_LAST_FORMAT, imexporter);
129  g_key_file_set_string(state_file, GNC_STATE_SECTION, STATE_KEY_LAST_PROFILE, profile);
130 }
131 
132 void
134 {
135  AB_BANKING* api = gnc_AB_BANKING_new ();
136  GncABSelectImExDlg* imexd =
137  gnc_ab_select_imex_dlg_new (GTK_WIDGET (parent), api);
138  char *imexporter = NULL, *profile = NULL;
139  AB_IMEXPORTER_CONTEXT* ctx = NULL;
140 
141  if (!imexd)
142  {
143 
144  PERR ("Failed to create select imex dialog.");
145  gnc_AB_BANKING_fini(api);
146  return;
147  }
148  load_imexporter_and_profile(&imexporter, &profile);
151 
152  if (!gnc_ab_select_imex_dlg_run (imexd))
153  {
155  return;
156  }
157 
158  imexporter = gnc_ab_select_imex_dlg_get_imexporter_name (imexd);
159  profile = gnc_ab_select_imex_dlg_get_profile_name (imexd);
160 
161  if (imexporter && profile)
162  {
163  ctx = named_import_get_context (parent, api, imexporter, profile);
165 
166  if (ctx)
167  {
168  GncABImExContextImport* ieci = NULL;
169  ieci = gnc_ab_import_context (ctx, AWAIT_TRANSACTIONS, FALSE, api, GTK_WIDGET(parent));
170  g_free(ieci);
171  AB_ImExporterContext_free(ctx);
172  }
173 
174  save_imexporter_and_profile(imexporter, profile);
175  g_free (imexporter);
176  g_free (profile);
177  }
178 
179  gnc_AB_BANKING_fini(api);
180 }
Dialog for AqBanking transaction data.
Functions to load, save and get gui state.
utility functions for the GnuCash UI
#define DEBUG(format, args...)
Print a debugging message.
Definition: qoflog.h:264
Transaction matcher main window.
Generic and very flexible account matcher/picker.
#define PERR(format, args...)
Log a serious error.
Definition: qoflog.h:244
GKeyFile * gnc_state_get_current(void)
Returns a pointer to the most recently loaded state.
Definition: gnc-state.c:248
char * gnc_ab_select_imex_dlg_get_profile_name(GncABSelectImExDlg *imexd)
Get the selected file format profile name.
void gnc_ab_select_imex_dlg_set_imexporter_name(GncABSelectImExDlg *imexd, const char *name)
Get the selected importer/exporter name.
void gnc_ab_select_imex_dlg_destroy(GncABSelectImExDlg *imexd)
Destroy an AQBanking importer/exporter selection dialog.
gboolean gnc_ab_select_imex_dlg_run(GncABSelectImExDlg *imexd)
Run an AQBanking importer/exporter selection dialog.
AB_BANKING * gnc_AB_BANKING_new(void)
If there is a cached AB_BANKING object, return it initialized.
Definition: gnc-ab-utils.c:163
GncABSelectImExDlg * gnc_ab_select_imex_dlg_new(GtkWidget *parent, AB_BANKING *abi)
Create an AQBanking importer/exporter selection dialog.
void gnc_ab_select_imex_dlg_set_profile_name(GncABSelectImExDlg *imexd, const char *name)
Get the selected file format profile name.
Dialog to select AQBanking importer/exporter and format profile.
gint gnc_AB_BANKING_fini(AB_BANKING *api)
Finish the AB_BANKING api.
Definition: gnc-ab-utils.c:226
char * gnc_ab_select_imex_dlg_get_imexporter_name(GncABSelectImExDlg *imexd)
Get the selected importer/exporter name.
GncABImExContextImport * gnc_ab_import_context(AB_IMEXPORTER_CONTEXT *context, guint awaiting, gboolean execute_txns, AB_BANKING *api, GtkWidget *parent)
Import balances and transactions found in a AB_IMEXPORTER_CONTEXT into GnuCash.
void gnc_file_aqbanking_import_dialog(GtkWindow *parent)
Import files via AQBanking&#39;s Import Dialog.
GUI callbacks for AqBanking.
AqBanking utility functions.