Branch data Line data Source code
1 : : /********************************************************************\
2 : : * gnc-account-merge.c *
3 : : * Copyright (C) 2006 Joshua Sled <jsled@asynchronous.org> *
4 : : * *
5 : : * This program is free software; you can redistribute it and/or *
6 : : * modify it under the terms of the GNU General Public License as *
7 : : * published by the Free Software Foundation; either version 2 of *
8 : : * the License, or (at your option) any later version. *
9 : : * *
10 : : * This program is distributed in the hope that it will be useful, *
11 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 : : * GNU General Public License for more details. *
14 : : * *
15 : : * You should have received a copy of the GNU General Public License*
16 : : * along with this program; if not, write to the Free Software *
17 : : * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
18 : : \********************************************************************/
19 : :
20 : : #include <config.h>
21 : : #include <glib.h>
22 : : #include "gnc-account-merge.h"
23 : : #include "Account.h"
24 : :
25 : : GncAccountMergeDisposition
26 : 0 : determine_account_merge_disposition(Account *existing_acct, Account *new_acct)
27 : : {
28 : 0 : g_assert(new_acct != NULL);
29 : :
30 : 0 : if (existing_acct == NULL)
31 : 0 : return GNC_ACCOUNT_MERGE_DISPOSITION_CREATE_NEW;
32 : :
33 : 0 : return GNC_ACCOUNT_MERGE_DISPOSITION_USE_EXISTING;
34 : : }
35 : :
36 : : GncAccountMergeDisposition
37 : 0 : determine_merge_disposition(Account *existing_root, Account *new_acct)
38 : : {
39 : : Account *existing_acct;
40 : : gchar *full_name;
41 : :
42 : 0 : full_name = gnc_account_get_full_name(new_acct);
43 : 0 : existing_acct = gnc_account_lookup_by_full_name(existing_root, full_name);
44 : 0 : g_free(full_name);
45 : :
46 : 0 : return determine_account_merge_disposition(existing_acct, new_acct);
47 : : }
48 : :
49 : : void
50 : 0 : account_trees_merge(Account *existing_root, Account *new_accts_root)
51 : : {
52 : : GList *accounts, *node;
53 : 0 : g_return_if_fail(new_accts_root != NULL);
54 : 0 : g_return_if_fail(existing_root != NULL);
55 : :
56 : : /* since we're have a chance of mutating the list (via
57 : : * gnc_account_add_child) while we're iterating over it, iterate
58 : : * over a copy. */
59 : 0 : accounts = gnc_account_get_children(new_accts_root);
60 : 0 : for (node = accounts; node; node = g_list_next(node))
61 : : {
62 : : Account *existing_named, *new_acct;
63 : : const char *name;
64 : :
65 : 0 : new_acct = (Account*)node->data;
66 : 0 : name = xaccAccountGetName(new_acct);
67 : 0 : existing_named = gnc_account_lookup_by_name(existing_root, name);
68 : 0 : switch (determine_account_merge_disposition(existing_named, new_acct))
69 : : {
70 : 0 : case GNC_ACCOUNT_MERGE_DISPOSITION_USE_EXISTING:
71 : : /* recurse */
72 : 0 : account_trees_merge(existing_named, new_acct);
73 : 0 : break;
74 : 0 : case GNC_ACCOUNT_MERGE_DISPOSITION_CREATE_NEW:
75 : : /* merge this one in. */
76 : 0 : gnc_account_append_child(existing_root, new_acct);
77 : 0 : break;
78 : : }
79 : : }
80 : 0 : g_list_free(accounts);
81 : : }
|