GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
gnc-account-merge.c
1 /********************************************************************\
2  * gnc-account-merge.c *
3  * Copyright (C) 2006 Joshua Sled <[email protected]> *
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 determine_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 
36 GncAccountMergeDisposition
37 determine_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 
49 void
50 account_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 }
void gnc_account_append_child(Account *new_parent, Account *child)
Definition: Account.c:2525
Account * gnc_account_lookup_by_name(const Account *parent, const char *name)
Definition: Account.c:2803
gchar * gnc_account_get_full_name(const Account *account)
Definition: Account.c:3038
Account handling public routines.
Account * gnc_account_lookup_by_full_name(const Account *any_acc, const gchar *name)
Definition: Account.c:2915
GList * gnc_account_get_children(const Account *account)
Definition: Account.c:2654
const char * xaccAccountGetName(const Account *acc)
Definition: Account.c:3031