Bug 706021 Rename and Move Imap functions.

This patch renames the Imap functions in Account.c and
adds them to the .h file to make accessible. Also added
a delete function to the non Baysian functions along with
a test.
This commit is contained in:
Robert Fewell 2015-10-16 20:13:30 +01:00 committed by Geert Janssens
parent ad2c36e545
commit 1ef201c7d1
5 changed files with 151 additions and 110 deletions

View File

@ -5028,26 +5028,12 @@ xaccAccountForEachTransaction(const Account *acc, TransactionCallback proc,
* matching data. See src/import-export/import-backend.c for explanations.
*/
typedef struct _GncImportMatchMap
{
Account * acc;
QofBook * book;
} GncImportMatchMap;
#define IMAP_FRAME "import-map"
#define IMAP_FRAME_BAYES "import-map-bayes"
GncImportMatchMap * gnc_account_create_imap (Account *acc);
Account* gnc_imap_find_account(GncImportMatchMap *imap, const char* category,
const char *key);
void gnc_imap_add_account (GncImportMatchMap *imap, const char *category,
const char *key, Account *acc);
Account* gnc_imap_find_account_bayes (GncImportMatchMap *imap, GList* tokens);
void gnc_imap_add_account_bayes (GncImportMatchMap *imap, GList* tokens,
Account *acc);
/* Obtain an ImportMatchMap object from an Account or a Book */
GncImportMatchMap *
gnc_account_create_imap (Account *acc)
gnc_account_imap_create_imap (Account *acc)
{
GncImportMatchMap *imap;
@ -5066,9 +5052,9 @@ gnc_account_create_imap (Account *acc)
/* Look up an Account in the map */
Account*
gnc_imap_find_account (GncImportMatchMap *imap,
const char *category,
const char *key)
gnc_account_imap_find_account (GncImportMatchMap *imap,
const char *category,
const char *key)
{
GValue v = G_VALUE_INIT;
GncGUID * guid = NULL;
@ -5088,10 +5074,10 @@ gnc_imap_find_account (GncImportMatchMap *imap,
/* Store an Account in the map */
void
gnc_imap_add_account (GncImportMatchMap *imap,
const char *category,
const char *key,
Account *acc)
gnc_account_imap_add_account (GncImportMatchMap *imap,
const char *category,
const char *key,
Account *acc)
{
GValue v = G_VALUE_INIT;
char *kvp_path;
@ -5111,6 +5097,39 @@ gnc_imap_add_account (GncImportMatchMap *imap,
xaccAccountCommitEdit (imap->acc);
}
/* Remove a reference to an Account in the map */
void
gnc_account_imap_delete_account (GncImportMatchMap *imap,
const char *category,
const char *key)
{
char *kvp_path;
if (!imap || !key) return;
if (!category)
kvp_path = g_strdup_printf (IMAP_FRAME "/%s", key);
else
kvp_path = g_strdup_printf (IMAP_FRAME "/%s/%s", category, key);
xaccAccountBeginEdit (imap->acc);
if (qof_instance_has_slot (QOF_INSTANCE (imap->acc), kvp_path))
{
qof_instance_slot_delete (QOF_INSTANCE (imap->acc), kvp_path);
g_free (kvp_path);
if (category)
{
kvp_path = g_strdup_printf (IMAP_FRAME "/%s", category);
qof_instance_slot_delete_if_empty (QOF_INSTANCE (imap->acc), kvp_path);
g_free (kvp_path);
}
qof_instance_slot_delete_if_empty (QOF_INSTANCE (imap->acc), IMAP_FRAME);
}
qof_instance_set_dirty (QOF_INSTANCE (imap->acc));
xaccAccountCommitEdit (imap->acc);
}
/*--------------------------------------------------------------------------
Below here is the bayes transaction to account matching system
--------------------------------------------------------------------------*/
@ -5237,7 +5256,7 @@ highestProbability(gpointer key, gpointer value, gpointer data)
/** Look up an Account in the map */
Account*
gnc_imap_find_account_bayes (GncImportMatchMap *imap, GList *tokens)
gnc_account_imap_find_account_bayes (GncImportMatchMap *imap, GList *tokens)
{
struct token_accounts_info tokenInfo; /**< holds the accounts and total
* token count for a single token */
@ -5395,9 +5414,9 @@ gnc_imap_find_account_bayes (GncImportMatchMap *imap, GList *tokens)
/** Updates the imap for a given account using a list of tokens */
void
gnc_imap_add_account_bayes(GncImportMatchMap *imap,
GList *tokens,
Account *acc)
gnc_account_imap_add_account_bayes (GncImportMatchMap *imap,
GList *tokens,
Account *acc)
{
GList *current_token;
gint64 token_count;

View File

@ -65,6 +65,12 @@ typedef struct
QofInstanceClass parent_class;
} AccountClass;
typedef struct
{
Account *acc;
QofBook *book;
} GncImportMatchMap;
/* --- type macros --- */
#define GNC_TYPE_ACCOUNT (gnc_account_get_type ())
#define GNC_ACCOUNT(o) \
@ -1372,6 +1378,34 @@ int gnc_account_tree_staged_transaction_traversal(const Account *account,
int xaccAccountTreeForEachTransaction(Account *acc,
TransactionCallback proc, void *data);
/** Obtain an ImportMatchMap object from an Account or a Book
*/
GncImportMatchMap *gnc_account_imap_create_imap (Account *acc);
/* Look up an Account in the map non Baysian
*/
Account* gnc_account_imap_find_account (GncImportMatchMap *imap, const char* category,
const char *key);
/* Store an Account in the map non Baysian
*/
void gnc_account_imap_add_account (GncImportMatchMap *imap, const char *category,
const char *key, Account *acc);
/* Remove a reference to an Account in the map non Baysian
*/
void gnc_account_imap_delete_account (GncImportMatchMap *imap, const char *category,
const char *key);
/** Look up an Account in the map using Baysian
*/
Account* gnc_account_imap_find_account_bayes (GncImportMatchMap *imap, GList* tokens);
/** Updates the imap for a given account using a list of tokens
*/
void gnc_account_imap_add_account_bayes (GncImportMatchMap *imap, GList* tokens,
Account *acc);
/** @} */

View File

@ -1,5 +1,5 @@
/********************************************************************
* test-import-map.cpp: Test import match maps. *
* gtest-import-map.cpp: Test import match maps. *
* Copyright 2015 John Ralls <jralls@ceridwen.us> *
* *
* This program is free software; you can redistribute it and/or *
@ -26,25 +26,6 @@ extern "C"
#include "../Account.h"
#include <qof.h>
#include <qofinstance-p.h>
struct GncImportMatchMap
{
Account *acc;
QofBook *book;
};
extern GncImportMatchMap * gnc_account_create_imap (Account *acc);
extern Account* gnc_imap_find_account(GncImportMatchMap *imap,
const char* category,
const char *key);
extern void gnc_imap_add_account (GncImportMatchMap *imap,
const char *category,
const char *key, Account *acc);
extern Account* gnc_imap_find_account_bayes (GncImportMatchMap *imap,
GList* tokens);
extern void gnc_imap_add_account_bayes (GncImportMatchMap *imap,
GList* tokens,
Account *acc);
}
#include <kvp_frame.hpp>
@ -73,7 +54,7 @@ protected:
};
TEST_F(ImapTest, CreateImap) {
GncImportMatchMap *imap = gnc_account_create_imap (t_bank_account);
GncImportMatchMap *imap = gnc_account_imap_create_imap (t_bank_account);
EXPECT_NE(nullptr, imap);
EXPECT_EQ(t_bank_account, imap->acc);
EXPECT_EQ(gnc_account_get_book(t_bank_account), imap->book);
@ -89,7 +70,7 @@ class ImapPlainTest : public ImapTest
protected:
void SetUp() {
ImapTest::SetUp();
t_imap = gnc_account_create_imap (t_bank_account);
t_imap = gnc_account_imap_create_imap (t_bank_account);
}
void TearDown() {
@ -110,13 +91,13 @@ TEST_F(ImapPlainTest, FindAccount)
root->set_path({IMAP_FRAME, "pepper"}, acc1_val);
root->set_path({IMAP_FRAME, "salt"}, acc2_val);
EXPECT_EQ(t_expense_account1, gnc_imap_find_account(t_imap, "foo", "bar"));
EXPECT_EQ(t_expense_account1, gnc_account_imap_find_account(t_imap, "foo", "bar"));
EXPECT_EQ(t_expense_account2,
gnc_imap_find_account(t_imap, "baz", "waldo"));
gnc_account_imap_find_account(t_imap, "baz", "waldo"));
EXPECT_EQ(t_expense_account1,
gnc_imap_find_account(t_imap, NULL, "pepper"));
EXPECT_EQ(t_expense_account2, gnc_imap_find_account(t_imap, NULL, "salt"));
EXPECT_EQ(nullptr, gnc_imap_find_account(t_imap, "salt", NULL));
gnc_account_imap_find_account(t_imap, NULL, "pepper"));
EXPECT_EQ(t_expense_account2, gnc_account_imap_find_account(t_imap, NULL, "salt"));
EXPECT_EQ(nullptr, gnc_account_imap_find_account(t_imap, "salt", NULL));
}
TEST_F(ImapPlainTest, AddAccount)
@ -124,16 +105,16 @@ TEST_F(ImapPlainTest, AddAccount)
// prevent the embedded beginedit/commitedit from doing anything
qof_instance_increase_editlevel(QOF_INSTANCE(t_bank_account));
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
gnc_imap_add_account(t_imap, "foo", "bar", t_expense_account1);
gnc_imap_add_account(t_imap, "baz", "waldo", t_expense_account2);
gnc_imap_add_account(t_imap, NULL, "pepper", t_expense_account1);
gnc_imap_add_account(t_imap, NULL, "salt", t_expense_account2);
gnc_account_imap_add_account(t_imap, "foo", "bar", t_expense_account1);
gnc_account_imap_add_account(t_imap, "baz", "waldo", t_expense_account2);
gnc_account_imap_add_account(t_imap, NULL, "pepper", t_expense_account1);
gnc_account_imap_add_account(t_imap, NULL, "salt", t_expense_account2);
EXPECT_EQ(1, qof_instance_get_editlevel(QOF_INSTANCE(t_bank_account)));
EXPECT_TRUE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
gnc_imap_add_account(t_imap, NULL, NULL, t_expense_account2);
gnc_account_imap_add_account(t_imap, NULL, NULL, t_expense_account2);
EXPECT_FALSE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
gnc_imap_add_account(t_imap, "pork", "sausage", NULL);
gnc_account_imap_add_account(t_imap, "pork", "sausage", NULL);
EXPECT_FALSE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
qof_instance_reset_editlevel(QOF_INSTANCE(t_bank_account));
@ -152,6 +133,45 @@ TEST_F(ImapPlainTest, AddAccount)
EXPECT_EQ(nullptr, value);
}
TEST_F(ImapPlainTest, DeleteAccount)
{
Path path1 {IMAP_FRAME, "foo", "waldo"};
Path path2 {IMAP_FRAME, "foo"};
Path path3 {IMAP_FRAME};
// prevent the embedded beginedit/commitedit from doing anything
qof_instance_increase_editlevel(QOF_INSTANCE(t_bank_account));
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
gnc_account_imap_add_account(t_imap, "foo", "bar", t_expense_account1);
gnc_account_imap_add_account(t_imap, "foo", "waldo", t_expense_account2);
gnc_account_imap_add_account(t_imap, NULL, "pepper", t_expense_account1);
EXPECT_EQ(1, qof_instance_get_editlevel(QOF_INSTANCE(t_bank_account)));
EXPECT_TRUE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
gnc_account_imap_delete_account(t_imap, NULL, NULL);
EXPECT_FALSE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
gnc_account_imap_delete_account(t_imap, "foo", "waldo");
EXPECT_TRUE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
EXPECT_EQ(t_expense_account1, gnc_account_imap_find_account(t_imap, "foo", "bar"));
EXPECT_EQ(nullptr, gnc_account_imap_find_account(t_imap, "foo", "waldo"));
auto root = qof_instance_get_slots(QOF_INSTANCE(t_bank_account));
EXPECT_EQ(nullptr, root->get_slot(path1));
gnc_account_imap_delete_account(t_imap, "foo", "bar");
EXPECT_TRUE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
EXPECT_EQ(nullptr, root->get_slot(path2));
gnc_account_imap_delete_account(t_imap, NULL, "pepper");
EXPECT_TRUE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
EXPECT_EQ(nullptr, root->get_slot(path3));
qof_instance_reset_editlevel(QOF_INSTANCE(t_bank_account));
}
static const char* foo = "foo";
static const char* bar = "bar";
static const char* baz = "baz";
@ -212,15 +232,15 @@ TEST_F(ImapBayesTest, FindAccountBayes)
root->set_path({IMAP_FRAME_BAYES, pepper, acct1_name}, value);
root->set_path({IMAP_FRAME_BAYES, salt, acct2_name}, value);
auto account = gnc_imap_find_account_bayes(t_imap, t_list1);
auto account = gnc_account_imap_find_account_bayes(t_imap, t_list1);
EXPECT_EQ(t_expense_account1, account);
account = gnc_imap_find_account_bayes(t_imap, t_list2);
account = gnc_account_imap_find_account_bayes(t_imap, t_list2);
EXPECT_EQ(t_expense_account2, account);
account = gnc_imap_find_account_bayes(t_imap, t_list3);
account = gnc_account_imap_find_account_bayes(t_imap, t_list3);
EXPECT_EQ(t_expense_account1, account);
account = gnc_imap_find_account_bayes(t_imap, t_list4);
account = gnc_account_imap_find_account_bayes(t_imap, t_list4);
EXPECT_EQ(t_expense_account2, account);
account = gnc_imap_find_account_bayes(t_imap, t_list5);
account = gnc_account_imap_find_account_bayes(t_imap, t_list5);
EXPECT_EQ(nullptr, account);
}
@ -229,14 +249,14 @@ TEST_F(ImapBayesTest, AddAccountBayes)
// prevent the embedded beginedit/commitedit from doing anything
qof_instance_increase_editlevel(QOF_INSTANCE(t_bank_account));
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
gnc_imap_add_account_bayes(t_imap, t_list1, t_expense_account1);
gnc_imap_add_account_bayes(t_imap, t_list2, t_expense_account2);
gnc_imap_add_account_bayes(t_imap, t_list3, t_expense_account1);
gnc_imap_add_account_bayes(t_imap, t_list4, t_expense_account2);
gnc_account_imap_add_account_bayes(t_imap, t_list1, t_expense_account1);
gnc_account_imap_add_account_bayes(t_imap, t_list2, t_expense_account2);
gnc_account_imap_add_account_bayes(t_imap, t_list3, t_expense_account1);
gnc_account_imap_add_account_bayes(t_imap, t_list4, t_expense_account2);
EXPECT_EQ(1, qof_instance_get_editlevel(QOF_INSTANCE(t_bank_account)));
EXPECT_TRUE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
gnc_imap_add_account_bayes(t_imap, t_list5, NULL);
gnc_account_imap_add_account_bayes(t_imap, t_list5, NULL);
EXPECT_FALSE(qof_instance_get_dirty_flag(QOF_INSTANCE(t_bank_account)));
qof_instance_reset_editlevel(QOF_INSTANCE(t_bank_account));
@ -262,7 +282,7 @@ TEST_F(ImapBayesTest, AddAccountBayes)
EXPECT_EQ(nullptr, value);
qof_instance_increase_editlevel(QOF_INSTANCE(t_bank_account));
gnc_imap_add_account_bayes(t_imap, t_list2, t_expense_account2);
gnc_account_imap_add_account_bayes(t_imap, t_list2, t_expense_account2);
qof_instance_mark_clean(QOF_INSTANCE(t_bank_account));
qof_instance_reset_editlevel(QOF_INSTANCE(t_bank_account));
value = root->get_slot({IMAP_FRAME_BAYES, baz, acct2_name});

View File

@ -44,37 +44,6 @@
#include "gnc-prefs.h"
#include "gnc-ui-util.h"
/* Private interface to Account GncImportMatchMap functions */
/** @{
Obtain an ImportMatchMap object from an Account */
extern GncImportMatchMap * gnc_account_create_imap (Account *acc);
/*@}*/
/* Look up an Account in the map */
extern Account* gnc_imap_find_account(GncImportMatchMap *imap,
const char* category,
const char *key);
/* Store an Account in the map. This mapping is immediatly stored in
the underlying kvp frame, regardless of whether the MatchMap is
destroyed later or not. */
extern void gnc_imap_add_account (GncImportMatchMap *imap,
const char *category,
const char *key, Account *acc);
/* Look up an Account in the map from a GList* of pointers to strings(tokens)
from the current transaction */
extern Account* gnc_imap_find_account_bayes (GncImportMatchMap *imap,
GList* tokens);
/* Store an Account in the map. This mapping is immediatly stored in
the underlying kvp frame, regardless of whether the MatchMap is
destroyed later or not. */
extern void gnc_imap_add_account_bayes (GncImportMatchMap *imap,
GList* tokens,
Account *acc);
#define GNCIMPORT_DESC "desc"
#define GNCIMPORT_MEMO "memo"
#define GNCIMPORT_PAYEE "payee"
@ -514,7 +483,7 @@ matchmap_find_destination (GncImportMatchMap *matchmap, GNCImportTransInfo *info
g_assert (info);
tmp_map = ((matchmap != NULL) ? matchmap :
gnc_account_create_imap
gnc_account_imap_create_imap
(xaccSplitGetAccount
(gnc_import_TransInfo_get_fsplit (info))));
@ -525,13 +494,13 @@ matchmap_find_destination (GncImportMatchMap *matchmap, GNCImportTransInfo *info
tokens = TransactionGetTokens(info);
/* try to find the destination account for this transaction from its tokens */
result = gnc_imap_find_account_bayes(tmp_map, tokens);
result = gnc_account_imap_find_account_bayes(tmp_map, tokens);
}
else
{
/* old system of transaction to account matching */
result = gnc_imap_find_account
result = gnc_account_imap_find_account
(tmp_map, GNCIMPORT_DESC,
xaccTransGetDescription (gnc_import_TransInfo_get_trans (info)));
}
@ -542,7 +511,7 @@ matchmap_find_destination (GncImportMatchMap *matchmap, GNCImportTransInfo *info
* transaction is stored there.
if (result == NULL)
result = gnc_imap_find_account
result = gnc_account_imap_find_account
(tmp_map, GNCIMPORT_MEMO,
xaccSplitGetMemo (gnc_import_TransInfo_get_fsplit (info)));
*/
@ -584,7 +553,7 @@ matchmap_store_destination (GncImportMatchMap *matchmap,
tmp_matchmap = ((matchmap != NULL) ?
matchmap :
gnc_account_create_imap
gnc_account_imap_create_imap
(xaccSplitGetAccount
(gnc_import_TransInfo_get_fsplit (trans_info))));
@ -596,7 +565,7 @@ matchmap_store_destination (GncImportMatchMap *matchmap,
tokens = TransactionGetTokens(trans_info);
/* add the tokens to the imap with the given destination account */
gnc_imap_add_account_bayes(tmp_matchmap, tokens, dest);
gnc_account_imap_add_account_bayes(tmp_matchmap, tokens, dest);
}
else
@ -605,14 +574,14 @@ matchmap_store_destination (GncImportMatchMap *matchmap,
descr = xaccTransGetDescription
(gnc_import_TransInfo_get_trans (trans_info));
if (descr && (strlen (descr) > 0))
gnc_imap_add_account (tmp_matchmap,
gnc_account_imap_add_account (tmp_matchmap,
GNCIMPORT_DESC,
descr,
dest);
memo = xaccSplitGetMemo
(gnc_import_TransInfo_get_fsplit (trans_info));
if (memo && (strlen (memo) > 0))
gnc_imap_add_account (tmp_matchmap,
gnc_account_imap_add_account (tmp_matchmap,
GNCIMPORT_MEMO,
memo,
dest);

View File

@ -33,7 +33,6 @@
typedef struct _transactioninfo GNCImportTransInfo;
typedef struct _matchinfo GNCImportMatchInfo;
typedef struct _GncImportMatchMap GncImportMatchMap;
typedef enum _action
{