From d75ab275f1c481014107ba8b66bca3daccb8c6ac Mon Sep 17 00:00:00 2001 From: Geert Janssens Date: Tue, 28 Feb 2023 15:49:10 +0100 Subject: [PATCH] Eliminate gnc-csv-account-map.[ch] The code is was only used by the csv import assistant and forced gtk as a dependency on gnc-imp-props-tx Part has been moved to Account, other bits have been moved to the importer code where they were used. --- gnucash/import-export/csv-imp/CMakeLists.txt | 2 - .../csv-imp/assistant-csv-trans-import.cpp | 48 +++++- .../csv-imp/gnc-csv-account-map.c | 139 ------------------ .../csv-imp/gnc-csv-account-map.h | 69 --------- .../csv-imp/gnc-imp-props-tx.cpp | 5 +- .../csv-imp/gnc-imp-props-tx.hpp | 2 + libgnucash/engine/Account.cpp | 25 ++++ libgnucash/engine/Account.h | 5 + po/POTFILES.in | 1 - 9 files changed, 78 insertions(+), 218 deletions(-) delete mode 100644 gnucash/import-export/csv-imp/gnc-csv-account-map.c delete mode 100644 gnucash/import-export/csv-imp/gnc-csv-account-map.h diff --git a/gnucash/import-export/csv-imp/CMakeLists.txt b/gnucash/import-export/csv-imp/CMakeLists.txt index e7bb7ecb281..5347cfdec24 100644 --- a/gnucash/import-export/csv-imp/CMakeLists.txt +++ b/gnucash/import-export/csv-imp/CMakeLists.txt @@ -12,7 +12,6 @@ set(csv_import_SOURCES assistant-csv-trans-import.cpp gnc-plugin-csv-import.c csv-account-import.c - gnc-csv-account-map.c gnc-csv-gnumeric-popup.c gnc-imp-props-price.cpp gnc-imp-props-tx.cpp @@ -42,7 +41,6 @@ set(csv_import_noinst_HEADERS assistant-csv-trans-import.h gnc-plugin-csv-import.h csv-account-import.h - gnc-csv-account-map.h gnc-csv-gnumeric-popup.h gnc-imp-props-price.hpp gnc-imp-props-tx.hpp diff --git a/gnucash/import-export/csv-imp/assistant-csv-trans-import.cpp b/gnucash/import-export/csv-imp/assistant-csv-trans-import.cpp index 71fe41c7757..55823b93b1b 100644 --- a/gnucash/import-export/csv-imp/assistant-csv-trans-import.cpp +++ b/gnucash/import-export/csv-imp/assistant-csv-trans-import.cpp @@ -51,7 +51,6 @@ #include "import-account-matcher.h" #include "import-main-matcher.h" #include "import-backend.h" -#include "gnc-csv-account-map.h" #include "gnc-account-sel.h" #include "gnc-csv-gnumeric-popup.h" @@ -82,6 +81,11 @@ namespace bl = boost::locale; /* This static indicates the debugging module that this .o belongs to. */ static QofLogModule log_module = GNC_MOD_ASSISTANT; +enum GncImportColumn { + MAPPING_STRING, + MAPPING_FULLPATH, + MAPPING_ACCOUNT +}; /* A note on memory management * @@ -1772,6 +1776,39 @@ void CsvImpTransAssist::acct_match_set_accounts () } } +static void +csv_tximp_acct_match_load_mappings (GtkTreeModel *mappings_store) +{ + // Set iter to first entry of store + GtkTreeIter iter; + auto valid = gtk_tree_model_get_iter_first (mappings_store, &iter); + + // Walk through the store trying to match to a map + while (valid) + { + // Walk through the list, reading each row + Account *account = nullptr; + gchar *map_string; + gtk_tree_model_get (GTK_TREE_MODEL(mappings_store), &iter, MAPPING_STRING, &map_string, MAPPING_ACCOUNT, &account, -1); + + // Look for an account matching the map_string + // It may already be set in the tree model. If not we try to match the map_string with + // - an entry in our saved account maps + // - a full name of any of our existing accounts + if (account || + (account = gnc_account_imap_find_any (gnc_get_current_book(), IMAP_CAT_CSV, map_string)) || + (account = gnc_account_lookup_by_full_name (gnc_get_current_root_account(), map_string))) + { + auto fullpath = gnc_account_get_full_name (account); + gtk_list_store_set (GTK_LIST_STORE(mappings_store), &iter, MAPPING_FULLPATH, fullpath, -1); + gtk_list_store_set (GTK_LIST_STORE(mappings_store), &iter, MAPPING_ACCOUNT, account, -1); + g_free (fullpath); + } + + g_free (map_string); + valid = gtk_tree_model_iter_next (mappings_store, &iter); + } +} static bool csv_tximp_acct_match_check_all (GtkTreeModel *model) @@ -1853,7 +1890,11 @@ CsvImpTransAssist::acct_match_select(GtkTreeModel *model, GtkTreeIter* iter) MAPPING_FULLPATH, fullpath, -1); // Update the account kvp mappings - gnc_csv_account_map_change_mappings (account, gnc_acc, text); + if (text && *text) + { + gnc_account_imap_delete_account (account, IMAP_CAT_CSV, text); + gnc_account_imap_add_account (gnc_acc, IMAP_CAT_CSV, text, gnc_acc); + } // Force reparsing of account columns - may impact multi-currency mode auto col_types = tx_imp->column_types(); @@ -2001,7 +2042,6 @@ CsvImpTransAssist::assist_preview_page_prepare () g_idle_add ((GSourceFunc)csv_imp_preview_queue_rebuild_table, this); } } - void CsvImpTransAssist::assist_account_match_page_prepare () { @@ -2011,7 +2051,7 @@ CsvImpTransAssist::assist_account_match_page_prepare () // Match the account strings to account maps from previous imports auto store = gtk_tree_view_get_model (GTK_TREE_VIEW(account_match_view)); - gnc_csv_account_map_load_mappings (store); + csv_tximp_acct_match_load_mappings (store); // Enable the view, possibly after an error gtk_widget_set_sensitive (account_match_view, true); diff --git a/gnucash/import-export/csv-imp/gnc-csv-account-map.c b/gnucash/import-export/csv-imp/gnc-csv-account-map.c deleted file mode 100644 index aeb942b6e6c..00000000000 --- a/gnucash/import-export/csv-imp/gnc-csv-account-map.c +++ /dev/null @@ -1,139 +0,0 @@ -/*******************************************************************\ - * gnc-csv-account-map.c -- Load and Update Mappings * - * * - * Copyright (C) 2015 Robert Fewell * - * * - * 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 * -\********************************************************************/ -/** @file gnc-csv-account-map.c - @brief Save and Load Mappings - @author Copyright (c) 2015 Robert Fewell -*/ -#include - -#include -#include - -#include "qof.h" -#include "gnc-ui-util.h" -#include "gnc-csv-account-map.h" - -#define CSV_CATEGORY "csv-account-map" - -#define IMPORT_STRING "String" -#define IMPORT_FULL_PATH "FullPath" -#define IMPORT_ACCOUNT "Account" - -#define UNUSED_VAR __attribute__ ((unused)) - -/* This static indicates the debugging module that this .o belongs to. */ -static QofLogModule UNUSED_VAR log_module = G_LOG_DOMAIN; - -/************************************************** - * gnc_csv_account_map_search - * - * search the existing mappings for the account - * linked to the import string. - **************************************************/ -Account * gnc_csv_account_map_search (const gchar *map_string) -{ - Account *root, *account = NULL; - GList *accts, *ptr; - - /* Get list of Accounts */ - root = gnc_book_get_root_account (gnc_get_current_book()); - accts = gnc_account_get_descendants_sorted (root); - - /* Go through list of accounts */ - for (ptr = accts; ptr; ptr = g_list_next (ptr)) - { - Account *tmp_acc = ptr->data; - - if (gnc_account_imap_find_account (tmp_acc, CSV_CATEGORY, map_string)) - { - account = tmp_acc; - break; - } - } - g_list_free (accts); - - return account; -} - - -/************************************************** - * gnc_csv_account_map_load_mappings - * - * load the existing mappings - **************************************************/ -void -gnc_csv_account_map_load_mappings (GtkTreeModel *mappings_store) -{ - GtkTreeIter iter; - gboolean valid; - - // Set iter to first entry of store - valid = gtk_tree_model_get_iter_first (mappings_store, &iter); - - // Walk through the store trying to match to a map - while (valid) - { - Account *account = NULL; - gchar *map_string; - gchar *fullpath; - - // Walk through the list, reading each row - gtk_tree_model_get (GTK_TREE_MODEL(mappings_store), &iter, MAPPING_STRING, &map_string, MAPPING_ACCOUNT, &account, -1); - - // Look for an account matching the map_string - // It may already be set in the tree model. If not we try to match the map_string with - // - an entry in our saved account maps - // - a full name of any of our existing accounts - if (account || - (account = gnc_csv_account_map_search (map_string)) || - (account = gnc_account_lookup_by_full_name (gnc_get_current_root_account(), map_string))) - { - fullpath = gnc_account_get_full_name (account); - gtk_list_store_set (GTK_LIST_STORE(mappings_store), &iter, MAPPING_FULLPATH, fullpath, -1); - gtk_list_store_set (GTK_LIST_STORE(mappings_store), &iter, MAPPING_ACCOUNT, account, -1); - g_free (fullpath); - } - - g_free (map_string); - valid = gtk_tree_model_iter_next (mappings_store, &iter); - } -} - - -/************************************************** - * gnc_csv_account_map_change_mappings - * - * change the existing mappings - **************************************************/ -void -gnc_csv_account_map_change_mappings (Account *old_account, Account *new_account, const gchar *map_string) -{ - if (strlen (map_string) == 0) - return; - - if (old_account) - gnc_account_imap_delete_account (old_account, CSV_CATEGORY, map_string); - - if (new_account) - gnc_account_imap_add_account (new_account, CSV_CATEGORY, map_string, new_account); -} diff --git a/gnucash/import-export/csv-imp/gnc-csv-account-map.h b/gnucash/import-export/csv-imp/gnc-csv-account-map.h deleted file mode 100644 index c6391e4af73..00000000000 --- a/gnucash/import-export/csv-imp/gnc-csv-account-map.h +++ /dev/null @@ -1,69 +0,0 @@ -/*******************************************************************\ - * gnc-csv-account-map.h -- Load and Update Mappings * - * * - * Copyright (C) 2015 Robert Fewell * - * * - * 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 * -\********************************************************************/ -/** @file gnc-csv-account-map.h - @brief Save and Load Mappings - @author Copyright (c) 2015 Robert Fewell -*/ -#ifndef GNC_CSV_ACCOUNT_MAP_H -#define GNC_CSV_ACCOUNT_MAP_H - -#include -#include "Account.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** Enumeration for the mappings liststore */ -enum GncImportColumn {MAPPING_STRING, MAPPING_FULLPATH, MAPPING_ACCOUNT}; - -/** Load the import mappings. - * - * For each mapping string in the tree model, try to find a - * corresponding account and account full path. - * - * - if the account was already set, just update the full path - * - if the mapping string matches an account in the account maps, - * use that account and its corresponding full name - * - otherwise search for an existing account whose full name matches the - * mapping string - * - */ -void gnc_csv_account_map_load_mappings (GtkTreeModel *mappings_store); - -/** Update the import mappings. - * - */ -void gnc_csv_account_map_change_mappings (Account *old_account, Account *new_account, const gchar *map_string); - -/** Returns a pointer to the account that matches the import string. - * - * @return A pointer to an account. - */ -Account * gnc_csv_account_map_search (const gchar *map_string); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/gnucash/import-export/csv-imp/gnc-imp-props-tx.cpp b/gnucash/import-export/csv-imp/gnc-imp-props-tx.cpp index 6298f8707d5..a2a36151246 100644 --- a/gnucash/import-export/csv-imp/gnc-imp-props-tx.cpp +++ b/gnucash/import-export/csv-imp/gnc-imp-props-tx.cpp @@ -29,7 +29,6 @@ #endif #include "engine-helpers.h" -#include "gnc-csv-account-map.h" #include "gnc-ui-util.h" #include "Account.h" #include "Transaction.h" @@ -446,7 +445,7 @@ void GncPreSplit::set (GncTransPropType prop_type, const std::string& value) m_account.reset(); if (value.empty()) throw std::invalid_argument (_("Account value can't be empty.")); - if ((acct = gnc_csv_account_map_search (value.c_str())) || + if ((acct = gnc_account_imap_find_any (gnc_get_current_book(), IMAP_CAT_CSV, value.c_str())) || (acct = gnc_account_lookup_by_full_name (gnc_get_current_root_account(), value.c_str()))) m_account = acct; else @@ -458,7 +457,7 @@ void GncPreSplit::set (GncTransPropType prop_type, const std::string& value) if (value.empty()) throw std::invalid_argument (_("Transfer account value can't be empty.")); - if ((acct = gnc_csv_account_map_search (value.c_str())) || + if ((acct = gnc_account_imap_find_any (gnc_get_current_book(), IMAP_CAT_CSV,value.c_str())) || (acct = gnc_account_lookup_by_full_name (gnc_get_current_root_account(), value.c_str()))) m_taccount = acct; else diff --git a/gnucash/import-export/csv-imp/gnc-imp-props-tx.hpp b/gnucash/import-export/csv-imp/gnc-imp-props-tx.hpp index f9d094e0c0d..967950e755c 100644 --- a/gnucash/import-export/csv-imp/gnc-imp-props-tx.hpp +++ b/gnucash/import-export/csv-imp/gnc-imp-props-tx.hpp @@ -77,6 +77,8 @@ enum class GncTransPropType { SPLIT_PROPS = TREC_DATE }; +#define IMAP_CAT_CSV "csv-account-map" + using StrVec = std::vector; using ErrMap = std::map; using ErrPair = std::pair; diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp index a599f158eaf..005a896699b 100644 --- a/libgnucash/engine/Account.cpp +++ b/libgnucash/engine/Account.cpp @@ -5646,6 +5646,31 @@ gnc_account_imap_find_account (Account *acc, return retval; } +Account* +gnc_account_imap_find_any (QofBook *book, const char* category, const char *key) +{ + Account *account = NULL; + + /* Get list of Accounts */ + auto root = gnc_book_get_root_account (book); + auto accts = gnc_account_get_descendants_sorted (root); + + /* Go through list of accounts */ + for (auto ptr = accts; ptr; ptr = g_list_next (ptr)) + { + auto tmp_acc = static_cast (ptr->data); + + if (gnc_account_imap_find_account (tmp_acc, category, key)) + { + account = tmp_acc; + break; + } + } + g_list_free (accts); + +return account; +} + /* Store an Account in the map */ void gnc_account_imap_add_account (Account *acc, diff --git a/libgnucash/engine/Account.h b/libgnucash/engine/Account.h index 5a2f2c45d1b..499ffbe0e8a 100644 --- a/libgnucash/engine/Account.h +++ b/libgnucash/engine/Account.h @@ -1575,6 +1575,11 @@ typedef enum Account* gnc_account_imap_find_account (Account* acc, const char* category, const char *key); + /* Look up an Account in the map non-Baysian, searching through the + * import map data of all accounts. Returns first match. + */ + Account* gnc_account_imap_find_any (QofBook *book, const char* category, const char *key); + /* Store an Account in the map non Baysian */ void gnc_account_imap_add_account (Account* acc, const char *category, diff --git a/po/POTFILES.in b/po/POTFILES.in index 81f99711c5c..2e8342a4d6d 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -333,7 +333,6 @@ gnucash/import-export/csv-imp/assistant-csv-account-import.c gnucash/import-export/csv-imp/assistant-csv-price-import.cpp gnucash/import-export/csv-imp/assistant-csv-trans-import.cpp gnucash/import-export/csv-imp/csv-account-import.c -gnucash/import-export/csv-imp/gnc-csv-account-map.c gnucash/import-export/csv-imp/gnc-csv-gnumeric-popup.c gnucash/import-export/csv-imp/gnc-import-price.cpp gnucash/import-export/csv-imp/gnc-import-tx.cpp