GnuCash c935c2f+
Loading...
Searching...
No Matches
gnc-account-merge.c
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
25GncAccountMergeDisposition
26determine_account_merge_disposition(Account *existing_acct, Account *new_acct)
27{
28 g_assert(new_acct != NULL);
29
30 if (existing_acct == NULL)
31 return GNC_ACCOUNT_MERGE_DISPOSITION_CREATE_NEW;
32
33 return GNC_ACCOUNT_MERGE_DISPOSITION_USE_EXISTING;
34}
35
36GncAccountMergeDisposition
37determine_merge_disposition(Account *existing_root, Account *new_acct)
38{
39 Account *existing_acct;
40 gchar *full_name;
41
42 full_name = gnc_account_get_full_name(new_acct);
43 existing_acct = gnc_account_lookup_by_full_name(existing_root, full_name);
44 g_free(full_name);
45
46 return determine_account_merge_disposition(existing_acct, new_acct);
47}
48
49void
50account_trees_merge(Account *existing_root, Account *new_accts_root)
51{
52 GList *accounts, *node;
53 g_return_if_fail(new_accts_root != NULL);
54 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 accounts = gnc_account_get_children(new_accts_root);
60 for (node = accounts; node; node = g_list_next(node))
61 {
62 Account *existing_named, *new_acct;
63 const char *name;
64
65 new_acct = (Account*)node->data;
66 name = xaccAccountGetName(new_acct);
67 existing_named = gnc_account_lookup_by_name(existing_root, name);
68 switch (determine_account_merge_disposition(existing_named, new_acct))
69 {
70 case GNC_ACCOUNT_MERGE_DISPOSITION_USE_EXISTING:
71 /* recurse */
72 account_trees_merge(existing_named, new_acct);
73 break;
74 case GNC_ACCOUNT_MERGE_DISPOSITION_CREATE_NEW:
75 /* merge this one in. */
76 gnc_account_append_child(existing_root, new_acct);
77 break;
78 }
79 }
80 g_list_free(accounts);
81}
Account handling public routines.
const char * xaccAccountGetName(const Account *acc)
Get the account's name.
Definition Account.cpp:3289
gchar * gnc_account_get_full_name(const Account *account)
The gnc_account_get_full_name routine returns the fully qualified name of the account using the given...
Definition Account.cpp:3305
void gnc_account_append_child(Account *new_parent, Account *child)
This function will remove from the child account any pre-existing parent relationship,...
Definition Account.cpp:2836
GList * gnc_account_get_children(const Account *account)
This routine returns a GList of all children accounts of the specified account.
Definition Account.cpp:2960
Account * gnc_account_lookup_by_name(const Account *parent, const char *name)
The gnc_account_lookup_by_name() subroutine fetches the account by name from the descendants of the s...
Definition Account.cpp:3093
Account * gnc_account_lookup_by_full_name(const Account *any_acc, const gchar *name)
The gnc_account_lookup_full_name() subroutine works like gnc_account_lookup_by_name,...
Definition Account.cpp:3163
STRUCTS.