Line data Source code
1 : /********************************************************************
2 : * gnc-aqbanking-templates.cpp implements transaction templates *
3 : * for AQBanking. *
4 : * Copyright 2015 John Ralls <jralls@ceridwen.us> *
5 : * *
6 : * This program is free software; you can redistribute it and/or *
7 : * modify it under the terms of the GNU General Public License as *
8 : * published by the Free Software Foundation; either version 2 of *
9 : * the License, or (at your option) any later version. *
10 : * *
11 : * This program is distributed in the hope that it will be useful, *
12 : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 : * GNU General Public License for more details. *
15 : * *
16 : * You should have received a copy of the GNU General Public License*
17 : * along with this program; if not, contact: *
18 : * *
19 : * Free Software Foundation Voice: +1-617-542-5942 *
20 : * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21 : * Boston, MA 02110-1301, USA gnu@gnu.org *
22 : * *
23 : ********************************************************************/
24 :
25 : /** Class for managing AQBanking Transaction Templates.
26 : */
27 :
28 : #include <string>
29 :
30 : #include "gnc-aqbanking-templates.h"
31 :
32 : #include "qofinstance-p.h"
33 : #include "kvp-frame.hpp"
34 : #include "gnc-rational.hpp"
35 :
36 : namespace {
37 : static const char* TT_NAME {"name"};
38 : static const char* TT_RNAME {"rnam"};
39 : static const char* TT_RACC {"racc"};
40 : static const char* TT_RBCODE {"rbcd"};
41 : static const char* TT_PURPOS {"purp"};
42 : static const char* TT_PURPOSCT {"purc"};
43 : static const char* TT_AMOUNT {"amou"};
44 : }
45 :
46 : struct _GncABTransTempl
47 : {
48 : public:
49 0 : _GncABTransTempl () :
50 0 : m_name(), m_recipient_name(), m_recipient_account(),
51 0 : m_recipient_bankcode(), m_amount(gnc_numeric_zero()), m_purpose(),
52 0 : m_purpose_continuation() {}
53 2 : _GncABTransTempl (const std::string& name,
54 : const std::string& recip_name,
55 : const std::string& recip_account,
56 : const std::string& recip_code,
57 : const GncRational& amount,
58 : const std::string& purpose,
59 2 : const std::string& purpose_cont) :
60 2 : m_name(name), m_recipient_name(recip_name),
61 2 : m_recipient_account(recip_account), m_recipient_bankcode(recip_code),
62 2 : m_amount(amount), m_purpose(purpose),
63 2 : m_purpose_continuation(purpose_cont) {}
64 : KvpFrame* make_kvp_frame();
65 2 : const char* name() const { return m_name.c_str(); }
66 0 : const char* recipient_name() const { return m_recipient_name.c_str(); }
67 0 : const char* recipient_account() const
68 : {
69 0 : return m_recipient_account.c_str();
70 : }
71 0 : const char* recipient_bankcode() const
72 : {
73 0 : return m_recipient_bankcode.c_str();
74 : }
75 0 : const GncRational amount() const { return m_amount; }
76 0 : const char* purpose() const { return m_purpose.c_str(); }
77 0 : const char* purpose_continuation() const
78 : {
79 0 : return m_purpose_continuation.c_str();
80 : }
81 1 : void set_name (const char* name) { m_name = name; }
82 0 : void set_recipient_name (const char* name) { m_recipient_name = name; }
83 0 : void set_recipient_account (const char* account) {
84 0 : m_recipient_account = account;
85 0 : }
86 0 : void set_recipient_bankcode (const char* code) {
87 0 : m_recipient_bankcode = code;
88 0 : }
89 0 : void set_amount (GncRational amount) { m_amount = amount; }
90 0 : void set_purpose (const char* purpose) { m_purpose = purpose; }
91 0 : void set_purpose_continuation (const char* cont) { m_purpose_continuation = cont; }
92 : private:
93 : std::string m_name;
94 : std::string m_recipient_name;
95 : std::string m_recipient_account;
96 : std::string m_recipient_bankcode;
97 : GncRational m_amount;
98 : std::string m_purpose;
99 : std::string m_purpose_continuation;
100 : };
101 :
102 : KvpFrame*
103 1 : _GncABTransTempl::make_kvp_frame()
104 : {
105 1 : auto frame = new KvpFrame;
106 6 : frame->set({TT_NAME}, new KvpValue(g_strdup (m_name.c_str())));
107 6 : frame->set({TT_RNAME}, new KvpValue(g_strdup (m_recipient_name.c_str())));
108 6 : frame->set({TT_RACC}, new KvpValue(g_strdup (m_recipient_account.c_str())));
109 6 : frame->set({TT_RBCODE}, new KvpValue(g_strdup (m_recipient_bankcode.c_str())));
110 5 : frame->set({TT_AMOUNT}, new KvpValue(m_amount));
111 6 : frame->set({TT_PURPOS}, new KvpValue(g_strdup (m_purpose.c_str())));
112 6 : frame->set({TT_PURPOSCT}, new KvpValue(g_strdup (m_purpose_continuation.c_str())));
113 1 : return frame;
114 14 : }
115 :
116 : GncABTransTempl*
117 0 : gnc_ab_trans_templ_new()
118 : {
119 0 : return new _GncABTransTempl;
120 : }
121 :
122 : GncABTransTempl*
123 0 : gnc_ab_trans_templ_new_full(const gchar *name, const gchar *recp_name,
124 : const gchar *recp_account,
125 : const gchar *recp_bankcode, gnc_numeric amount,
126 : const gchar *purpose, const gchar *purpose_cont)
127 : {
128 0 : return new _GncABTransTempl(name, recp_name, recp_account, recp_bankcode,
129 0 : amount, purpose, purpose_cont);
130 : }
131 :
132 : GList*
133 2 : gnc_ab_trans_templ_list_new_from_book(QofBook *b)
134 : {
135 2 : GList *retval = NULL;
136 2 : auto toplevel = qof_instance_get_slots (QOF_INSTANCE (b));
137 4 : auto slot = toplevel->get_slot({"hbci", "template-list"});
138 2 : if (slot == nullptr)
139 0 : return retval;
140 2 : auto list = slot->get<GList*>();
141 6 : for (auto node = list; node != NULL; node = g_list_next (node))
142 : {
143 2 : KvpFrame *frame = static_cast<KvpValue*>(node->data)->get<KvpFrame*>();
144 12 : auto c_func = [frame](const char* key)
145 36 : { auto slot = frame->get_slot({key});
146 60 : return slot == nullptr ? std::string("") : std::string(slot->get<const char*>());};
147 2 : auto n_func = [frame](const char* key)
148 6 : { auto slot = frame->get_slot({key});
149 6 : return slot == nullptr ? gnc_numeric_zero() : slot->get<gnc_numeric>();};
150 4 : auto templ = new _GncABTransTempl (c_func(TT_NAME), c_func(TT_RNAME),
151 4 : c_func(TT_RACC), c_func(TT_RBCODE),
152 4 : n_func(TT_AMOUNT), c_func(TT_PURPOS),
153 6 : c_func(TT_PURPOSCT));
154 2 : retval = g_list_prepend (retval, templ);
155 : }
156 2 : retval = g_list_reverse (retval);
157 2 : return retval;
158 : }
159 :
160 : void
161 2 : gnc_ab_trans_templ_free (GncABTransTempl *t)
162 : {
163 2 : delete t;
164 2 : }
165 :
166 : void
167 2 : gnc_ab_trans_templ_list_free (GList *l)
168 : {
169 2 : g_list_free_full (l, (GDestroyNotify)gnc_ab_trans_templ_free);
170 2 : }
171 :
172 : static void*
173 1 : copy_list_value(const void* pvalue, void* pdata)
174 : {
175 1 : auto new_value = new KvpValue(*static_cast<const KvpValue*>(pvalue));
176 1 : return new_value;
177 : }
178 :
179 : void
180 1 : gnc_ab_set_book_template_list (QofBook *b, GList *template_list)
181 : {
182 1 : GList *kvp_list = NULL;
183 3 : for (auto node = template_list; node != NULL; node = g_list_next (node))
184 : {
185 1 : auto templ = static_cast<_GncABTransTempl*>(node->data);
186 1 : auto value = new KvpValue(templ->make_kvp_frame());
187 1 : kvp_list = g_list_prepend (kvp_list, value);
188 : }
189 1 : kvp_list = g_list_reverse (kvp_list);
190 : auto value = new KvpValue(g_list_copy_deep(kvp_list, copy_list_value,
191 1 : nullptr));
192 1 : qof_book_begin_edit(b);
193 1 : KvpFrame *toplevel = qof_instance_get_slots (QOF_INSTANCE (b));
194 2 : delete toplevel->set_path({"hbci", "template-list"}, value);
195 1 : qof_instance_set_dirty_flag (QOF_INSTANCE (b), TRUE);
196 1 : qof_book_commit_edit(b);
197 1 : }
198 :
199 : const gchar *
200 2 : gnc_ab_trans_templ_get_name(const GncABTransTempl *t)
201 : {
202 2 : g_return_val_if_fail(t, NULL);
203 2 : return t->name();
204 : }
205 :
206 : const gchar *
207 0 : gnc_ab_trans_templ_get_recp_name(const GncABTransTempl *t)
208 : {
209 0 : g_return_val_if_fail(t, NULL);
210 0 : return t->recipient_name();
211 : }
212 :
213 : const gchar *
214 0 : gnc_ab_trans_templ_get_recp_account(const GncABTransTempl *t)
215 : {
216 0 : g_return_val_if_fail(t, NULL);
217 0 : return t->recipient_account();
218 : }
219 :
220 : const gchar *
221 0 : gnc_ab_trans_templ_get_recp_bankcode(const GncABTransTempl *t)
222 : {
223 0 : g_return_val_if_fail(t, NULL);
224 0 : return t->recipient_bankcode();
225 : }
226 :
227 : gnc_numeric
228 0 : gnc_ab_trans_templ_get_amount(const GncABTransTempl *t)
229 : {
230 0 : g_return_val_if_fail(t, gnc_numeric_zero());
231 0 : return t->amount();
232 : }
233 :
234 : const gchar *
235 0 : gnc_ab_trans_templ_get_purpose(const GncABTransTempl *t)
236 : {
237 0 : g_return_val_if_fail(t, NULL);
238 0 : return t->purpose();
239 : }
240 :
241 : const gchar *
242 0 : gnc_ab_trans_templ_get_purpose_cont(const GncABTransTempl *t)
243 : {
244 0 : g_return_val_if_fail(t, NULL);
245 0 : return t->purpose_continuation();
246 : }
247 :
248 : void
249 1 : gnc_ab_trans_templ_set_name(GncABTransTempl *t, const gchar *name)
250 : {
251 1 : g_return_if_fail(t);
252 1 : t->set_name(name);
253 : }
254 :
255 : void
256 0 : gnc_ab_trans_templ_set_recp_name(GncABTransTempl *t, const gchar *recp_name)
257 : {
258 0 : g_return_if_fail(t);
259 0 : t->set_recipient_name(recp_name);
260 : }
261 :
262 : void
263 0 : gnc_ab_trans_templ_set_recp_account(GncABTransTempl *t,
264 : const gchar *recp_account)
265 : {
266 0 : g_return_if_fail(t);
267 0 : t->set_recipient_account(recp_account);
268 : }
269 :
270 : void
271 0 : gnc_ab_trans_templ_set_recp_bankcode(GncABTransTempl *t,
272 : const gchar *recp_bankcode)
273 : {
274 0 : g_return_if_fail(t);
275 0 : t->set_recipient_bankcode(recp_bankcode);
276 : }
277 :
278 : void
279 0 : gnc_ab_trans_templ_set_amount(GncABTransTempl *t, gnc_numeric amount)
280 : {
281 0 : g_return_if_fail(t);
282 0 : t->set_amount(amount);
283 : }
284 :
285 : void
286 0 : gnc_ab_trans_templ_set_purpose(GncABTransTempl *t, const gchar *purpose)
287 : {
288 0 : g_return_if_fail(t);
289 0 : t->set_purpose(purpose);
290 : }
291 :
292 : void
293 0 : gnc_ab_trans_templ_set_purpose_cont(GncABTransTempl *t,
294 : const gchar *purpose_cont)
295 : {
296 0 : g_return_if_fail(t);
297 0 : t->set_purpose_continuation (purpose_cont);
298 : }
|