Skip to content

Commit

Permalink
Eliminate gnc-csv-account-map.[ch]
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gjanssens committed Mar 13, 2023
1 parent 13ec4bb commit d75ab27
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 218 deletions.
2 changes: 0 additions & 2 deletions gnucash/import-export/csv-imp/CMakeLists.txt
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
48 changes: 44 additions & 4 deletions gnucash/import-export/csv-imp/assistant-csv-trans-import.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 ()
{
Expand All @@ -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);
Expand Down
139 changes: 0 additions & 139 deletions gnucash/import-export/csv-imp/gnc-csv-account-map.c

This file was deleted.

69 changes: 0 additions & 69 deletions gnucash/import-export/csv-imp/gnc-csv-account-map.h

This file was deleted.

5 changes: 2 additions & 3 deletions gnucash/import-export/csv-imp/gnc-imp-props-tx.cpp
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions gnucash/import-export/csv-imp/gnc-imp-props-tx.hpp
Expand Up @@ -77,6 +77,8 @@ enum class GncTransPropType {
SPLIT_PROPS = TREC_DATE
};

#define IMAP_CAT_CSV "csv-account-map"

using StrVec = std::vector<std::string>;
using ErrMap = std::map<GncTransPropType, std::string>;
using ErrPair = std::pair<GncTransPropType, std::string>;
Expand Down
25 changes: 25 additions & 0 deletions libgnucash/engine/Account.cpp
Expand Up @@ -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<Account*> (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,
Expand Down
5 changes: 5 additions & 0 deletions libgnucash/engine/Account.h
Expand Up @@ -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,
Expand Down

0 comments on commit d75ab27

Please sign in to comment.