Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iter to stl #1751

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions gnucash/gnome-utils/gnc-tree-container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ template <typename ModelType>
class GncTreeIter
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = ModelType;
using difference_type = ModelType;
using pointer = ModelType*;
using reference = ModelType&;

GncTreeIter(GtkTreeModel* model, std::optional<GtkTreeIter> iter) : m_model(model), m_iter(iter) {}

GncTreeIter(GtkTreeModel* model) : m_model (model)
Expand All @@ -99,6 +105,13 @@ class GncTreeIter
return ModelType (m_model, *m_iter);
}

GtkTreeIter& get_gtk_tree_iter ()
{
if (!m_iter.has_value())
throw "no value, cannot dereference";
return *m_iter;
};

bool has_value() { return m_iter.has_value(); };

bool operator==(const GncTreeIter& other) const
Expand Down Expand Up @@ -136,6 +149,13 @@ class GncTreeContainer

TreeModelIterator end() { return TreeModelIterator(m_model, std::nullopt); };

TreeModelIterator append()
{
GtkTreeIter iter;
gtk_list_store_append (GTK_LIST_STORE(m_model), &iter);
return TreeModelIterator(m_model, iter);
};

bool empty() { return begin() == end(); };

private:
Expand Down
39 changes: 16 additions & 23 deletions gnucash/import-export/import-match-picker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@
#include <gtk/gtk.h>
#include <glib/gi18n.h>

#include "gnc-tree-container.hpp"
#include "import-match-picker.h"
#include "qof.h"
#include "gnc-ui-util.h"
#include "dialog-utils.h"
#include "gnc-prefs.h"

#include <algorithm>

/********************************************************************\
* Constants *
\********************************************************************/
Expand Down Expand Up @@ -98,32 +101,22 @@ downloaded_transaction_append(GNCImportMatchPicker * matcher,
g_return_if_fail (matcher);
g_return_if_fail (transaction_info);

auto found = false;
auto store = GTK_LIST_STORE(gtk_tree_view_get_model(matcher->downloaded_view));
auto split = gnc_import_TransInfo_get_fsplit(transaction_info);
auto trans = gnc_import_TransInfo_get_trans(transaction_info);
auto container = GncTreeContainer<GncTreeData> (GTK_TREE_MODEL(store));

/* Has the transaction already been added? */
GtkTreeIter iter;
if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(store), &iter))
{
do
{
GNCImportTransInfo *local_info;
gtk_tree_model_get(GTK_TREE_MODEL(store), &iter,
DOWNLOADED_COL_INFO_PTR, &local_info,
-1);
if (local_info == transaction_info)
{
found = TRUE;
break;
}
}
while (gtk_tree_model_iter_next(GTK_TREE_MODEL(store), &iter));
}
if (!found)
gtk_list_store_append(store, &iter);
auto it_matches = [&transaction_info](auto it)
{ return it.template get<GNCImportTransInfo*>(DOWNLOADED_COL_INFO_PTR) == transaction_info; };

// find the GncTreeIter whose DOWNLOADED_COL_INFO_PTR matches transaction_info
auto iter = std::find_if (container.begin(), container.end(), it_matches);

// not found. append a new GtkTreeIter in container.
if (iter == container.end())
iter = container.append();

// now iter is a GncTreeIter; iter.get_gtk_tree_iter() is the GtkTreeIter
auto account = xaccAccountGetName(xaccSplitGetAccount(split));
auto date = qof_print_date(xaccTransGetDate(trans));
auto amount = g_strdup (xaccPrintAmount(xaccSplitGetAmount(split), gnc_split_amount_print_info(split, TRUE)));
Expand All @@ -137,7 +130,7 @@ downloaded_transaction_append(GNCImportMatchPicker * matcher,
auto imbalance = g_strdup (xaccPrintAmount (xaccTransGetImbalanceValue(trans),
gnc_commodity_print_info (xaccTransGetCurrency (trans), TRUE)));

gtk_list_store_set (store, &iter,
gtk_list_store_set (store, &iter.get_gtk_tree_iter(),
DOWNLOADED_COL_ACCOUNT, account,
DOWNLOADED_COL_DATE, date,
DOWNLOADED_COL_AMOUNT, amount,
Expand All @@ -147,7 +140,7 @@ downloaded_transaction_append(GNCImportMatchPicker * matcher,
DOWNLOADED_COL_INFO_PTR, transaction_info,
-1);

gtk_tree_selection_select_iter (gtk_tree_view_get_selection(matcher->downloaded_view), &iter);
gtk_tree_selection_select_iter (gtk_tree_view_get_selection(matcher->downloaded_view), &iter.get_gtk_tree_iter());

g_free (date);
g_free (amount);
Expand Down
Loading