GnuCash c935c2f+
Loading...
Searching...
No Matches
import-pending-matches.cpp
1/********************************************************************\
2 * This program is free software; you can redistribute it and/or *
3 * modify it under the terms of the GNU General Public License as *
4 * published by the Free Software Foundation; either version 2 of *
5 * the License, or (at your option) any later version. *
6 * *
7 * This program is distributed in the hope that it will be useful, *
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
10 * GNU General Public License for more details. *
11 * *
12 * You should have received a copy of the GNU General Public License*
13 * along with this program; if not, contact: *
14 * *
15 * Free Software Foundation Voice: +1-617-542-5942 *
16 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
17 * Boston, MA 02110-1301, USA gnu@gnu.org *
18\********************************************************************/
27#include <config.h>
28#include <glib.h>
29#include <glib/gi18n.h>
30#include <gtk/gtk.h> /* for references in import-backend.h */
31#include "guid.h"
32
34#include "import-backend.h"
35
36typedef struct _pending_matches
37{
38 gint num_manual_matches;
39 gint num_auto_matches;
41
42GNCImportPendingMatches *gnc_import_PendingMatches_new (void)
43{
44 /* Can't use guid_hash_table_new() here since we want values to be taken
45 care of for us on destroy. */
46 return g_hash_table_new_full (guid_hash_to_guint,
48 g_free,
49 g_free
50 );
51}
52
53void gnc_import_PendingMatches_delete (GNCImportPendingMatches *map)
54{
55 g_return_if_fail (map);
56
57 g_hash_table_destroy (map);
58}
59
60static const GncGUID *
61gnc_import_PendingMatches_get_key (GNCImportMatchInfo *match_info)
62{
63 Split *split;
64 const GncGUID *match_guid;
65
66 g_return_val_if_fail (match_info, NULL);
67
68 split = gnc_import_MatchInfo_get_split (match_info);
69 match_guid = qof_instance_get_guid (split);
70
71 return match_guid;
72}
73
74static GNCPendingMatches *
75gnc_import_PendingMatches_get_value (GNCImportPendingMatches *map,
76 GNCImportMatchInfo *match_info)
77{
78 const GncGUID *match_guid;
79
80 g_return_val_if_fail (map, NULL);
81 g_return_val_if_fail (match_info, NULL);
82
83 match_guid = gnc_import_PendingMatches_get_key (match_info);
84
85 return static_cast<GNCPendingMatches*>(g_hash_table_lookup (map, match_guid));
86}
87
88void
89gnc_import_PendingMatches_add_match (GNCImportPendingMatches *map,
90 GNCImportMatchInfo *match_info,
91 gboolean selected_manually)
92{
93 GNCPendingMatches *pending_matches;
94 const GncGUID *match_guid;
95 GncGUID *key;
96
97 g_return_if_fail (map);
98 g_return_if_fail (match_info);
99
100
101 pending_matches = gnc_import_PendingMatches_get_value (map, match_info);
102 match_guid = gnc_import_PendingMatches_get_key (match_info);
103
104 if (pending_matches == NULL)
105 {
106 pending_matches = g_new0 (GNCPendingMatches, 1);
107 key = g_new (GncGUID, 1);
108 *key = *match_guid;
109 g_hash_table_insert (map, key, pending_matches);
110 }
111
112 if (selected_manually)
113 {
114 pending_matches->num_manual_matches++;
115 }
116 else
117 {
118 pending_matches->num_auto_matches++;
119 }
120}
121
122void
123gnc_import_PendingMatches_remove_match (GNCImportPendingMatches *map,
124 GNCImportMatchInfo *match_info,
125 gboolean selected_manually)
126{
127 GNCPendingMatches *pending_matches;
128
129 g_return_if_fail (map);
130 g_return_if_fail (match_info);
131
132 pending_matches = gnc_import_PendingMatches_get_value (map, match_info);
133
134 g_return_if_fail (pending_matches);
135
136 if (selected_manually)
137 {
138 pending_matches->num_manual_matches--;
139 }
140 else
141 {
142 pending_matches->num_auto_matches--;
143 }
144
145 if (pending_matches->num_auto_matches == 0 &&
146 pending_matches->num_manual_matches == 0)
147 {
148 /* Key & Value are freed for us */
149 g_hash_table_remove (map,
150 gnc_import_PendingMatches_get_key (match_info));
151 }
152}
153
154GNCImportPendingMatchType
155gnc_import_PendingMatches_get_match_type (GNCImportPendingMatches *map,
156 GNCImportMatchInfo *match_info)
157{
158 GNCPendingMatches *pending_matches;
159
160 g_return_val_if_fail (map, GNCImportPending_NONE);
161 g_return_val_if_fail (match_info, GNCImportPending_NONE);
162
163 pending_matches = gnc_import_PendingMatches_get_value (map, match_info);
164
165 if (pending_matches == NULL)
166 {
167 return GNCImportPending_NONE;
168 }
169
170 if (pending_matches->num_manual_matches > 0)
171 {
172 return GNCImportPending_MANUAL;
173 }
174
175 if (pending_matches->num_auto_matches > 0)
176 {
177 return GNCImportPending_AUTO;
178 }
179
180 g_assert_not_reached();
181}
182
183const char *
184gnc_import_PendingMatches_get_type_str (GNCImportPendingMatchType type)
185{
186 switch (type)
187 {
188 case GNCImportPending_NONE:
189 return _("None");
190 case GNCImportPending_MANUAL:
191 return _("Manual");
192 case GNCImportPending_AUTO:
193 return _("Auto");
194 default:
195 g_assert_not_reached();
196 return NULL;
197 }
198}
guint guid_hash_to_guint(gconstpointer ptr)
Hash function for a GUID.
Definition guid.cpp:255
gint guid_g_hash_table_equal(gconstpointer guid_a, gconstpointer guid_b)
Equality function for two GUIDs in a GHashTable.
Definition guid.cpp:269
Split * gnc_import_MatchInfo_get_split(const GNCImportMatchInfo *info)
Get the split ('this-side split') of this MatchInfo.
const GncGUID * qof_instance_get_guid(gconstpointer inst)
Return the GncGUID of this instance.
globally unique ID User API
Generic importer backend interface.
Tracking container for pending match status.
The type used to store guids in C.
Definition guid.h:75