Skip to content

Commit

Permalink
iter to stl
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherlam committed Aug 10, 2023
1 parent beec420 commit 28314bb
Showing 1 changed file with 54 additions and 63 deletions.
117 changes: 54 additions & 63 deletions gnucash/import-export/import-main-matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include <stdbool.h>

#include <vector>
#include <string>

#include "import-main-matcher.h"

Expand Down Expand Up @@ -192,32 +193,45 @@ defer_bal_computation (GNCImportMainMatcher *info, Account* acc)
}
}

struct TreeViewIter
{
public:
TreeViewIter (GtkTreeView *view)
: m_model{gtk_tree_view_get_model (view)}
, m_value{(bool)gtk_tree_model_get_iter_first (m_model, &m_iter)}
{};
gpointer get (gint col)
{
gpointer rv;
gtk_tree_model_get (m_model, &m_iter, col, &rv, -1);
return rv;
}
GNCImportTransInfo* get_trans_info () { return static_cast<GNCImportTransInfo*>(get(DOWNLOADED_COL_DATA)); };
std::string get_account_name ()
{
auto str = static_cast<char*>(get(DOWNLOADED_COL_ACCOUNT));
std::string rv{str};
g_free (str);
return rv;
}
bool has_value () { return m_value; };
bool next () { return (m_value = gtk_tree_model_iter_next (m_model, &m_iter)); };
private:
GtkTreeModel* m_model;
bool m_value;
GtkTreeIter m_iter;
};

void
gnc_gen_trans_list_delete (GNCImportMainMatcher *info)
{

if (info == NULL)
return;

GtkTreeModel *model = gtk_tree_view_get_model (info->view);
GtkTreeIter iter;
if (gtk_tree_model_get_iter_first (model, &iter))
{
do
{
GNCImportTransInfo *trans_info;
gtk_tree_model_get (model, &iter,
DOWNLOADED_COL_DATA, &trans_info,
-1);

if (info->transaction_processed_cb)
{
info->transaction_processed_cb (trans_info, false,
info->user_data);
}
}
while (gtk_tree_model_iter_next (model, &iter));
}
if (info->transaction_processed_cb)
for (auto iter = TreeViewIter(info->view); iter.has_value(); iter.next())
info->transaction_processed_cb (iter.get_trans_info(), false, info->user_data);

if (GTK_IS_DIALOG(info->main_widget))
{
Expand Down Expand Up @@ -264,48 +278,29 @@ gnc_gen_trans_list_show_accounts_column (GNCImportMainMatcher *info)
{
g_assert (info);

GtkTreeModel *model = gtk_tree_view_get_model (info->view);
if (gtk_tree_model_iter_n_children (model, NULL) > 1)
{
bool multiple_accounts = false;
GtkTreeIter iter;
std::string first_acct_name;
bool multiple_accounts = false;

/* Get first row in list store */
bool valid = gtk_tree_model_get_iter_first (model, &iter);
if (valid)
{
gchar *account_name;
gtk_tree_model_get (model, &iter, DOWNLOADED_COL_ACCOUNT, &account_name, -1);

valid = gtk_tree_model_iter_next (model, &iter);

while (valid)
{
gchar *test_account_name;
gtk_tree_model_get (model, &iter, DOWNLOADED_COL_ACCOUNT, &test_account_name, -1);
if (g_strcmp0 (account_name, test_account_name) != 0)
{
multiple_accounts = true;
g_free (test_account_name);
break;
}
valid = gtk_tree_model_iter_next (model, &iter);
g_free (test_account_name);
}
g_free (account_name);
}
// now toggle the column
if (multiple_accounts)
{
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(info->show_account_column), true);
gtk_tree_view_expand_all (info->view);
}
else
for (auto iter = TreeViewIter (info->view); iter.has_value(); iter.next())
{
auto acct_name = iter.get_account_name ();
if (acct_name.empty())
continue;
else if (first_acct_name.empty())
{
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(info->show_account_column), false);
gtk_tree_view_collapse_all (info->view);
first_acct_name.swap(acct_name);
continue;
}
else if (acct_name == first_acct_name)
continue;
multiple_accounts = true;
break;
}
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON(info->show_account_column), multiple_accounts);
if (multiple_accounts)
gtk_tree_view_expand_all (info->view);
else
gtk_tree_view_collapse_all (info->view);
}

// This returns the transaction ID of the first match candidate in match_list
Expand Down Expand Up @@ -452,18 +447,14 @@ resolve_conflicts (GNCImportMainMatcher *info)
static void
load_hash_tables (GNCImportMainMatcher *info)
{
GtkTreeModel *model = gtk_tree_view_get_model (info->view);
GtkTreeIter import_iter;
GList *accounts_list = NULL;
bool valid = gtk_tree_model_get_iter_first (model, &import_iter);
while (valid)
for (auto iter = TreeViewIter(info->view); iter.has_value(); iter.next())
{
GNCImportTransInfo *trans_info = get_trans_info (model, &import_iter);
auto trans_info = iter.get_trans_info ();
Split *s = gnc_import_TransInfo_get_fsplit (trans_info);
Account *acc = xaccSplitGetAccount (s);
if (!g_list_find (accounts_list, acc))
accounts_list = g_list_prepend (accounts_list, acc);
valid = gtk_tree_model_iter_next (model, &import_iter);
}
for (GList *m = accounts_list; m; m = m->next)
{
Expand Down

0 comments on commit 28314bb

Please sign in to comment.