/
gnc-plugin-aqbanking.c
648 lines (554 loc) · 21.4 KB
/
gnc-plugin-aqbanking.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
* gnc-plugin-aqbanking.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@gnu.org
*/
/**
* @internal
* @file gnc-plugin-aqbanking.c
* @brief Plugin registration of the AqBanking module
* @author Copyright (C) 2002 Christian Stimming <stimming@tuhh.de>
* @author Copyright (C) 2003 David Hampton <hampton@employees.org>
* @author Copyright (C) 2008 Andreas Koehler <andi5.py@gmx.net>
*/
#include <config.h>
#include <glib/gi18n.h>
#include "Account.h"
#include "dialog-ab-trans.h"
#include "assistant-ab-initial.h"
#include "gnc-ab-getbalance.h"
#include "gnc-ab-gettrans.h"
#include "gnc-ab-transfer.h"
#include "gnc-ab-utils.h"
#include "gnc-ab-kvp.h"
#include "gnc-gwen-gui.h"
#include "gnc-file-aqb-import.h"
#include "gnc-plugin-aqbanking.h"
#include "gnc-plugin-manager.h"
#include "gnc-plugin-page-account-tree.h"
#include "gnc-plugin-page-register.h"
#include "gnc-main-window.h"
#include "gnc-prefs.h"
#include "gnc-ui-util.h" // for gnc_get_current_book
/* This static indicates the debugging module that this .o belongs to. */
static QofLogModule log_module = G_LOG_DOMAIN;
static void gnc_plugin_aqbanking_class_init(GncPluginAqBankingClass *klass);
static void gnc_plugin_aqbanking_init(GncPluginAqBanking *plugin);
static void gnc_plugin_aqbanking_add_to_window(GncPlugin *plugin, GncMainWindow *window, GQuark type);
static void gnc_plugin_aqbanking_remove_from_window(GncPlugin *plugin, GncMainWindow *window, GQuark type);
/* Object callbacks */
static void gnc_plugin_ab_main_window_page_added(GncMainWindow *window, GncPluginPage *page, gpointer user_data);
static void gnc_plugin_ab_main_window_page_changed(GncMainWindow *window, GncPluginPage *page, gpointer user_data);
static void gnc_plugin_ab_account_selected(GncPluginPage *plugin_page, Account *account, gpointer user_data);
/* Auxiliary functions */
static Account *main_window_to_account(GncMainWindow *window);
/* Command callbacks */
static void gnc_plugin_ab_cmd_setup (GSimpleAction *simple, GVariant *parameter, gpointer user_data);
static void gnc_plugin_ab_cmd_get_balance (GSimpleAction *simple, GVariant *parameter, gpointer user_data);
static void gnc_plugin_ab_cmd_get_transactions (GSimpleAction *simple, GVariant *parameter, gpointer user_data);
static void gnc_plugin_ab_cmd_issue_sepatransaction (GSimpleAction *simple, GVariant *parameter, gpointer user_data);
static void gnc_plugin_ab_cmd_issue_sepainternaltransaction (GSimpleAction *simple, GVariant *parameter, gpointer user_data);
static void gnc_plugin_ab_cmd_issue_inttransaction (GSimpleAction *simple, GVariant *parameter, gpointer user_data);
static void gnc_plugin_ab_cmd_issue_sepa_direct_debit (GSimpleAction *simple, GVariant *parameter, gpointer user_data);
static void gnc_plugin_ab_cmd_view_logwindow (GSimpleAction *simple, GVariant *parameter, gpointer user_data);
static void gnc_plugin_ab_cmd_aqb_import (GSimpleAction *simple, GVariant *parameter, gpointer user_data);
#define PLUGIN_ACTIONS_NAME "gnc-plugin-aqbanking-actions"
#define PLUGIN_UI_FILENAME "gnc-plugin-aqbanking.ui"
#define MENU_TOGGLE_ACTION_AB_VIEW_LOGWINDOW "ABViewLogwindowAction"
static void
change_state_logwindow (GSimpleAction *simple,
GVariant *state,
gpointer user_data)
{
g_simple_action_set_state (simple, state);
}
static GActionEntry gnc_plugin_actions [] =
{
{ "OnlineActionsAction", NULL, NULL, NULL, NULL },
{ "ABSetupAction", gnc_plugin_ab_cmd_setup, NULL, NULL, NULL },
{ "ABGetBalanceAction", gnc_plugin_ab_cmd_get_balance, NULL, NULL, NULL },
{ "ABGetTransAction", gnc_plugin_ab_cmd_get_transactions, NULL, NULL, NULL },
{ "ABIssueSepaTransAction", gnc_plugin_ab_cmd_issue_sepatransaction, NULL, NULL, NULL },
{ "ABIssueSepaIntTransAction", gnc_plugin_ab_cmd_issue_sepainternaltransaction, NULL, NULL, NULL },
{ "ABIssueIntTransAction", gnc_plugin_ab_cmd_issue_inttransaction, NULL, NULL, NULL },
{ "ABIssueSepaDirectDebitAction", gnc_plugin_ab_cmd_issue_sepa_direct_debit, NULL, NULL, NULL },
{ "AQBankingImportAction", gnc_plugin_ab_cmd_aqb_import, NULL, NULL, NULL },
{ MENU_TOGGLE_ACTION_AB_VIEW_LOGWINDOW, gnc_plugin_ab_cmd_view_logwindow, NULL, "true", change_state_logwindow },
};
/** The number of actions provided by this plugin. */
static guint gnc_plugin_n_actions = G_N_ELEMENTS(gnc_plugin_actions);
/** The default menu items that need to be add to the menu */
static const gchar *gnc_plugin_load_ui_items [] =
{
"FilePlaceholder1",
"ToolsPlaceholder0",
"ActionsPlaceholder1",
NULL,
};
static const gchar *need_account_actions[] =
{
"ABGetBalanceAction",
"ABGetTransAction",
"ABIssueSepaTransAction",
#if (AQBANKING_VERSION_INT >= 60400)
"ABIssueSepaIntTransAction",
#endif
"ABIssueIntTransAction",
"ABIssueSepaDirectDebitAction",
NULL
};
#if (AQBANKING_VERSION_INT < 60400)
static const gchar *inactive_account_actions[] =
{
"ABIssueSepaIntTransAction",
NULL
};
#endif
static const gchar *readonly_inactive_actions[] =
{
"OnlineActionsAction",
"ABSetupAction",
NULL
};
static GncMainWindow *gnc_main_window = NULL;
/************************************************************
* Object Implementation *
************************************************************/
G_DEFINE_TYPE(GncPluginAqBanking, gnc_plugin_aqbanking, GNC_TYPE_PLUGIN)
GncPlugin *
gnc_plugin_aqbanking_new(void)
{
return GNC_PLUGIN(g_object_new(GNC_TYPE_PLUGIN_AQBANKING, (gchar*) NULL));
}
static void
gnc_plugin_aqbanking_class_init(GncPluginAqBankingClass *klass)
{
GncPluginClass *plugin_class = GNC_PLUGIN_CLASS(klass);
/* plugin info */
plugin_class->plugin_name = GNC_PLUGIN_AQBANKING_NAME;
/* widget addition/removal */
plugin_class->actions_name = PLUGIN_ACTIONS_NAME;
plugin_class->actions = gnc_plugin_actions;
plugin_class->n_actions = gnc_plugin_n_actions;
plugin_class->ui_filename = PLUGIN_UI_FILENAME;
plugin_class->ui_updates = gnc_plugin_load_ui_items;
plugin_class->add_to_window = gnc_plugin_aqbanking_add_to_window;
plugin_class->remove_from_window = gnc_plugin_aqbanking_remove_from_window;
}
static void
gnc_plugin_aqbanking_init(GncPluginAqBanking *plugin)
{
}
/**
* Called when this plugin is added to a main window. Connect a few callbacks
* here to track page changes.
*/
static void
gnc_plugin_aqbanking_add_to_window(GncPlugin *plugin, GncMainWindow *window,
GQuark type)
{
GAction *action;
gnc_main_window = window;
g_signal_connect (window, "page_added",
G_CALLBACK(gnc_plugin_ab_main_window_page_added),
plugin);
g_signal_connect (window, "page_changed",
G_CALLBACK(gnc_plugin_ab_main_window_page_changed),
plugin);
action = gnc_main_window_find_action_in_group (window, PLUGIN_ACTIONS_NAME,
MENU_TOGGLE_ACTION_AB_VIEW_LOGWINDOW);
if (action)
{
GVariant *state = g_action_get_state (G_ACTION(action));
g_action_change_state (G_ACTION(action), g_variant_new_boolean (FALSE));
g_variant_unref (state);
}
}
static void
gnc_plugin_aqbanking_remove_from_window(GncPlugin *plugin, GncMainWindow *window,
GQuark type)
{
g_signal_handlers_disconnect_by_func(
window, G_CALLBACK(gnc_plugin_ab_main_window_page_changed), plugin);
g_signal_handlers_disconnect_by_func(
window, G_CALLBACK(gnc_plugin_ab_main_window_page_added), plugin);
}
/************************************************************
* Object Callbacks *
************************************************************/
/**
* A new page has been added to a main window. Connect a signal to it so that
* we can track when accounts are selected.
*/
static void
gnc_plugin_ab_main_window_page_added(GncMainWindow *window, GncPluginPage *page,
gpointer user_data)
{
const gchar *page_name;
ENTER("main window %p, page %p", window, page);
if (!GNC_IS_PLUGIN_PAGE(page))
{
LEAVE("no plugin_page");
return;
}
page_name = gnc_plugin_page_get_plugin_name(page);
if (!page_name)
{
LEAVE("no page_name of plugin_page");
return;
}
if (strcmp(page_name, GNC_PLUGIN_PAGE_ACCOUNT_TREE_NAME) == 0)
{
DEBUG("account tree page, adding signal");
g_signal_connect(page, "account_selected",
G_CALLBACK(gnc_plugin_ab_account_selected), NULL);
}
gnc_plugin_ab_main_window_page_changed(window, page, user_data);
LEAVE(" ");
}
/** Update the actions sensitivity
*/
static void update_inactive_actions(GncPluginPage *plugin_page)
{
GncMainWindow *window;
GSimpleActionGroup *simple_action_group;
// We are readonly - so we have to switch particular actions to inactive.
gboolean is_readwrite = !qof_book_is_readonly(gnc_get_current_book());
// We continue only if the current page is a plugin page
if (!plugin_page || !GNC_IS_PLUGIN_PAGE(plugin_page))
return;
window = GNC_MAIN_WINDOW(plugin_page->window);
g_return_if_fail (GNC_IS_MAIN_WINDOW(window));
simple_action_group = gnc_main_window_get_action_group (window, PLUGIN_ACTIONS_NAME);
g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP(simple_action_group));
/* Set the action's sensitivity */
gnc_plugin_set_actions_enabled (G_ACTION_MAP(simple_action_group), readonly_inactive_actions,
is_readwrite);
}
/**
* Whenever the current page has changed, update the aqbanking menus based upon
* the page that is currently selected.
*/
static void
gnc_plugin_ab_main_window_page_changed (GncMainWindow *window,
GncPluginPage *page, gpointer user_data)
{
Account *account = main_window_to_account (window);
/* Make sure not to call this with a NULL GncPluginPage */
if (page)
{
// Update the menu items according to the selected account
gnc_plugin_ab_account_selected (page, account, user_data);
// Also update the action sensitivity due to read-only
update_inactive_actions (page);
}
}
/**
* An account had been (de)selected either in an "account tree" page or by
* selecting another register page. Update the aqbanking menus appropriately.
*/
static void
gnc_plugin_ab_account_selected (GncPluginPage *plugin_page, Account *account,
gpointer user_data)
{
GncMainWindow *window;
GSimpleActionGroup *simple_action_group;
const gchar *bankcode = NULL;
const gchar *accountid = NULL;
g_return_if_fail(GNC_IS_PLUGIN_PAGE(plugin_page));
window = GNC_MAIN_WINDOW(plugin_page->window);
g_return_if_fail(GNC_IS_MAIN_WINDOW(window));
simple_action_group = gnc_main_window_get_action_group (window, PLUGIN_ACTIONS_NAME);
g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP(simple_action_group));
if (account)
{
bankcode = gnc_ab_get_account_bankcode(account);
accountid = gnc_ab_get_account_accountid(account);
gnc_plugin_set_actions_enabled (G_ACTION_MAP(simple_action_group), need_account_actions,
(account && bankcode && *bankcode
&& accountid && *accountid));
gnc_main_window_set_vis_of_items_by_action (window, need_account_actions,
TRUE);
#if (AQBANKING_VERSION_INT < 60400)
gnc_plugin_set_actions_enabled (G_ACTION_MAP(simple_action_group),
inactive_account_actions, FALSE);
gnc_main_window_set_vis_of_items_by_action (window, inactive_account_actions,
FALSE);
#endif
}
else
{
gnc_plugin_set_actions_enabled (G_ACTION_MAP(simple_action_group),
need_account_actions, FALSE);
gnc_main_window_set_vis_of_items_by_action (window, need_account_actions,
FALSE);
}
}
/************************************************************
* Auxiliary Functions *
************************************************************/
/**
* Given a pointer to a main window, try and extract an Account from it. If the
* current page is an "account tree" page, get the account corresponding to the
* selected account. (What if multiple accounts are selected?) If the current
* page is a "register" page, get the head account for the register. (Returns
* NULL for a general journal or search register.)
*
* @param window A pointer to a GncMainWindow object.
* @return A pointer to an account, if one can be determined from the current
* page. NULL otherwise.
*/
static Account *
main_window_to_account(GncMainWindow *window)
{
GncPluginPage *page;
const gchar *page_name;
Account *account = NULL;
const gchar *account_name;
ENTER("main window %p", window);
if (!GNC_IS_MAIN_WINDOW(window))
{
LEAVE("no main_window");
return NULL;
}
page = gnc_main_window_get_current_page(window);
if (!GNC_IS_PLUGIN_PAGE(page))
{
LEAVE("no plugin_page");
return NULL;
}
page_name = gnc_plugin_page_get_plugin_name(page);
if (!page_name)
{
LEAVE("no page_name of plugin_page");
return NULL;
}
if (strcmp(page_name, GNC_PLUGIN_PAGE_REGISTER_NAME) == 0)
{
DEBUG("register page");
account = gnc_plugin_page_register_get_account(
GNC_PLUGIN_PAGE_REGISTER(page));
}
else if (strcmp(page_name, GNC_PLUGIN_PAGE_ACCOUNT_TREE_NAME) == 0)
{
DEBUG("account tree page");
account = gnc_plugin_page_account_tree_get_current_account(
GNC_PLUGIN_PAGE_ACCOUNT_TREE(page));
}
else
{
account = NULL;
}
account_name = account ? xaccAccountGetName(account) : NULL;
LEAVE("account %s(%p)", account_name ? account_name : "(null)", account);
return account;
}
void
gnc_plugin_aqbanking_set_logwindow_visible (gboolean logwindow_visible)
{
GAction *action = gnc_main_window_find_action_in_group (gnc_main_window, PLUGIN_ACTIONS_NAME,
MENU_TOGGLE_ACTION_AB_VIEW_LOGWINDOW);
if (action)
{
GVariant *state = g_action_get_state (G_ACTION(action));
g_action_change_state (G_ACTION(action), g_variant_new_boolean (logwindow_visible));
g_variant_unref (state);
}
}
/************************************************************
* Command Callbacks *
************************************************************/
static void
gnc_plugin_ab_cmd_setup (GSimpleAction *simple,
GVariant *parameter,
gpointer user_data)
{
GncMainWindowActionData *data = user_data;
ENTER("action %p, main window data %p", simple, data);
gnc_main_window = data->window;
gnc_ab_initial_assistant();
LEAVE(" ");
}
static void
gnc_plugin_ab_cmd_get_balance (GSimpleAction *simple,
GVariant *parameter,
gpointer user_data)
{
GncMainWindowActionData *data = user_data;
Account *account;
ENTER("action %p, main window data %p", simple, data);
account = main_window_to_account(data->window);
if (account == NULL)
{
PINFO("No AqBanking account selected");
LEAVE("no account");
return;
}
gnc_main_window = data->window;
gnc_ab_getbalance(GTK_WIDGET(data->window), account);
LEAVE(" ");
}
static void
gnc_plugin_ab_cmd_get_transactions (GSimpleAction *simple,
GVariant *parameter,
gpointer user_data)
{
GncMainWindowActionData *data = user_data;
Account *account;
ENTER("action %p, main window data %p", simple, data);
account = main_window_to_account(data->window);
if (account == NULL)
{
PINFO("No AqBanking account selected");
LEAVE("no account");
return;
}
gnc_main_window = data->window;
gnc_ab_gettrans(GTK_WIDGET(data->window), account);
LEAVE(" ");
}
static void
gnc_plugin_ab_cmd_issue_sepatransaction (GSimpleAction *simple,
GVariant *parameter,
gpointer user_data)
{
GncMainWindowActionData *data = user_data;
Account *account;
ENTER("action %p, main window data %p", simple, data);
account = main_window_to_account(data->window);
if (account == NULL)
{
PINFO("No AqBanking account selected");
LEAVE("no account");
return;
}
gnc_main_window = data->window;
gnc_ab_maketrans(GTK_WIDGET(data->window), account, SEPA_TRANSFER);
LEAVE(" ");
}
#if (AQBANKING_VERSION_INT >= 60400)
static void
gnc_plugin_ab_cmd_issue_sepainternaltransaction (GSimpleAction *simple,
GVariant *parameter,
gpointer user_data)
{
GncMainWindowActionData *data = user_data;
Account *account;
ENTER("action %p, main window data %p", simple, data);
account = main_window_to_account(data->window);
if (account == NULL)
{
PINFO("No AqBanking account selected");
LEAVE("no account");
return;
}
gnc_main_window = data->window;
gnc_ab_maketrans(GTK_WIDGET(data->window), account, SEPA_INTERNAL_TRANSFER);
LEAVE(" ");
}
#else
static void
gnc_plugin_ab_cmd_issue_sepainternaltransaction (GSimpleAction *simple,
GVariant *parameter,
gpointer user_data)
{
GncMainWindowActionData *data = user_data;
ENTER("action %p, main window data %p", simple, data);
PINFO("Sepa Internal Transfer not supported by your aqbanking version!");
LEAVE("Sepa Internal Transfer not supported!");
}
#endif
static void
gnc_plugin_ab_cmd_issue_inttransaction (GSimpleAction *simple,
GVariant *parameter,
gpointer user_data)
{
GncMainWindowActionData *data = user_data;
Account *account;
ENTER("action %p, main window data %p", simple, data);
account = main_window_to_account(data->window);
if (account == NULL)
{
PINFO("No AqBanking account selected");
LEAVE("no account");
return;
}
gnc_main_window = data->window;
gnc_ab_maketrans(GTK_WIDGET(data->window), account,
SINGLE_INTERNAL_TRANSFER);
LEAVE(" ");
}
static void
gnc_plugin_ab_cmd_issue_sepa_direct_debit (GSimpleAction *simple,
GVariant *parameter,
gpointer user_data)
{
GncMainWindowActionData *data = user_data;
Account *account;
ENTER("action %p, main window data %p", simple, data);
account = main_window_to_account(data->window);
if (account == NULL)
{
PINFO("No AqBanking account selected");
LEAVE("no account");
return;
}
gnc_main_window = data->window;
gnc_ab_maketrans(GTK_WIDGET(data->window), account, SEPA_DEBITNOTE);
LEAVE(" ");
}
static void
gnc_plugin_ab_cmd_view_logwindow (GSimpleAction *simple,
GVariant *parameter,
gpointer user_data)
{
GncMainWindowActionData *data = user_data;
GVariant *state;
state = g_action_get_state (G_ACTION(simple));
if (g_variant_get_boolean (state))
{
if (!gnc_GWEN_Gui_show_dialog())
{
/* Log window could not be made visible */
g_action_change_state (G_ACTION(simple), g_variant_new_boolean (FALSE));
}
}
else
{
gnc_GWEN_Gui_hide_dialog();
}
g_variant_unref (state);
}
static void
gnc_plugin_ab_cmd_aqb_import (GSimpleAction *simple,
GVariant *parameter,
gpointer user_data)
{
GncMainWindowActionData *data = user_data;
gnc_main_window = data->window;
gnc_file_aqbanking_import_dialog (GTK_WINDOW (gnc_main_window));
}
/************************************************************
* Plugin Bootstrapping *
************************************************************/
void
gnc_plugin_aqbanking_create_plugin(void)
{
GncPlugin *plugin = gnc_plugin_aqbanking_new();
gnc_plugin_manager_add_plugin(gnc_plugin_manager_get(), plugin);
}