From a0ec45ad4037cb85f0dec6d61f64f21d88472916 Mon Sep 17 00:00:00 2001 From: Derek Atkins Date: Tue, 12 Nov 2002 22:07:40 +0000 Subject: [PATCH] * src/import-export/gnc-import-match-map.[ch] Implement a generic Account Mapper for the import routines. Still needs a GUI. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@7475 57a11ea4-9604-0410-9ed3-97b8803252fd --- ChangeLog | 6 ++ src/import-export/Makefile.am | 5 + src/import-export/gnc-import-match-map.c | 124 +++++++++++++++++++++++ src/import-export/gnc-import-match-map.h | 49 +++++++++ 4 files changed, 184 insertions(+) create mode 100644 src/import-export/gnc-import-match-map.c create mode 100644 src/import-export/gnc-import-match-map.h diff --git a/ChangeLog b/ChangeLog index fa0d2285ea..93a667d3fa 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2002-11-12 Derek Atkins + + * src/import-export/gnc-import-match-map.[ch] + Implement a generic Account Mapper for the import routines. + Still needs a GUI. + 2002-11-12 David Hampton * src/register/ledger-core/split-register-control.c diff --git a/src/import-export/Makefile.am b/src/import-export/Makefile.am index 2908b0f798..6fd896f35c 100644 --- a/src/import-export/Makefile.am +++ b/src/import-export/Makefile.am @@ -7,8 +7,13 @@ libgncmod_generic_import_la_SOURCES = \ Commodity-matcher.c \ Transaction-matcher.c \ Utilities.c \ + gnc-import-match-map.c \ gncmod-generic-import.c +gncincludedir = ${GNC_INCLUDE_DIR} +gncinclude_HEADERS = \ + gnc-import-match-map.h + noinst_HEADERS = \ gnc-generic-import.h diff --git a/src/import-export/gnc-import-match-map.c b/src/import-export/gnc-import-match-map.c new file mode 100644 index 0000000000..80963b54e2 --- /dev/null +++ b/src/import-export/gnc-import-match-map.c @@ -0,0 +1,124 @@ +/* + * gnc-import-match-map.c: + * an import mapper service that stores Account Maps for the + * generic importer. This allows importers to map various + * "strings" to Gnucash accounts in a generic manner. + * + * Created by: Derek Atkins + * + */ + +#include "gnc-import-match-map.h" +#include "kvp_frame.h" + +struct _GncImportMatchMap { + kvp_frame * frame; + Account * acc; + GNCBook * book; +}; + +#define IMAP_FRAME "import-map" + +static GncImportMatchMap * +gnc_imap_create_from_frame (kvp_frame *frame, Account *acc, GNCBook *book) +{ + GncImportMatchMap *imap; + + g_return_val_if_fail (frame != NULL, NULL); + g_return_val_if_fail ((acc && !book) || (!acc && book), NULL); + + imap = g_new0(GncImportMatchMap, 1); + imap->frame = frame; + + /* Cache the book for easy lookups; store the account/book for + * marking dirtiness + */ + if (acc) + book = xaccAccountGetBook (acc); + imap->acc = acc; + imap->book = book; + + return imap; +} + +/* Obtain an ImportMatchMap object from an Account or a Book */ +GncImportMatchMap * gnc_imap_create_from_account (Account *acc) +{ + kvp_frame * frame; + + if (!acc) return NULL; + frame = xaccAccountGetSlots (acc); + g_return_val_if_fail (frame != NULL, NULL); + + return gnc_imap_create_from_frame (frame, acc, NULL); +} + +GncImportMatchMap * gnc_imap_create_from_book (GNCBook *book) +{ + kvp_frame * frame; + + if (!book) return NULL; + frame = gnc_book_get_slots (book); + g_return_val_if_fail (frame != NULL, NULL); + + return gnc_imap_create_from_frame (frame, NULL, book); +} + +/* Destroy an import map */ +void gnc_imap_destroy (GncImportMatchMap *imap) +{ + if (!imap) return; + g_free (imap); +} + +/* Clear an import map -- this removes ALL entries in the map */ +void gnc_imap_clear (GncImportMatchMap *imap) +{ + if (!imap) return; + + /* Clear the IMAP_FRAME kvp */ + kvp_frame_set_slot_path (imap->frame, NULL, IMAP_FRAME); + + /* XXX: mark the account (or book) as dirty! */ +} + +/* Look up an Account in the map */ +Account * gnc_imap_find_account (GncImportMatchMap *imap, const char *category, + const char *key) +{ + kvp_value *value; + GUID * guid; + + if (!imap || !key) return NULL; + if (!category) { + category = key; + key = NULL; + } + + value = kvp_frame_get_slot_path (imap->frame, IMAP_FRAME, category, key, NULL); + if (!value) return NULL; + + guid = kvp_value_get_guid (value); + return xaccAccountLookup (guid, imap->book); +} + +/* Store an Account in the map */ +void gnc_imap_add_account (GncImportMatchMap *imap, const char *category, + const char *key, Account *acc) +{ + kvp_value *value; + + if (!imap || !key || !acc) return; + if (!category) { + category = key; + key = NULL; + } + + value = kvp_value_new_guid (xaccAccountGetGUID (acc)); + g_return_if_fail (value != NULL); + + kvp_frame_set_slot_path (imap->frame, value, IMAP_FRAME, category, key, NULL); + kvp_value_delete (value); + + /* XXX Mark the account (or book) as dirty! */ +} diff --git a/src/import-export/gnc-import-match-map.h b/src/import-export/gnc-import-match-map.h new file mode 100644 index 0000000000..4d63d0ffc7 --- /dev/null +++ b/src/import-export/gnc-import-match-map.h @@ -0,0 +1,49 @@ +/* + * gnc-import-match-map.h: + * an import mapper service that stores Account Maps for the + * generic importer. This allows importers to map various + * "strings" to Gnucash accounts in a generic manner. + * + * Created by: Derek Atkins + * + */ + +#ifndef GNC_IMPORT_MATCH_MAP_H +#define GNC_IMPORT_MATCH_MAP_H + +typedef struct _GncImportMatchMap GncImportMatchMap; + +#include "Account.h" +#include "gnc-book.h" + +/* Obtain an ImportMatchMap object from an Account or a Book */ +GncImportMatchMap * gnc_imap_create_from_account (Account *acc); +GncImportMatchMap * gnc_imap_create_from_book (GNCBook *book); + +/* Destroy an import map */ +void gnc_imap_destroy (GncImportMatchMap *imap); + +/* Clear an import map -- this removes ALL entries in the map */ +void gnc_imap_clear (GncImportMatchMap *imap); + +/* Look up an Account in the map */ +Account * gnc_imap_find_account (GncImportMatchMap *imap, const char *category, + const char *key); + +/* Store an Account in the map */ +void gnc_imap_add_account (GncImportMatchMap *imap, const char *category, + const char *key, Account *acc); + + +/* some well-known categories + * + * NOTE: You DO NOT have to use these values in your importer -- these + * are just "well known" values, not "mandatory" values. You are free + * to use these if they apply, map your own fields to these labels, or + * create your own category strings. + */ +#define GNCIMPORT_DESC "desc" +#define GNCIMPORT_MEMO "memo" +#define GNCIMPORT_PAYEE "payee" + +#endif /* GNC_IMPORT_MATCH_MAP_H */