GnuCash  5.6-150-g038405b370+
gnc-aqbanking-templates.cpp
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 
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 
47 {
48 public:
49  _GncABTransTempl () :
50  m_name(), m_recipient_name(), m_recipient_account(),
51  m_recipient_bankcode(), m_amount(gnc_numeric_zero()), m_purpose(),
52  m_purpose_continuation() {}
53  _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  const std::string& purpose_cont) :
60  m_name(name), m_recipient_name(recip_name),
61  m_recipient_account(recip_account), m_recipient_bankcode(recip_code),
62  m_amount(amount), m_purpose(purpose),
63  m_purpose_continuation(purpose_cont) {}
64  KvpFrame* make_kvp_frame();
65  const char* name() const { return m_name.c_str(); }
66  const char* recipient_name() const { return m_recipient_name.c_str(); }
67  const char* recipient_account() const
68  {
69  return m_recipient_account.c_str();
70  }
71  const char* recipient_bankcode() const
72  {
73  return m_recipient_bankcode.c_str();
74  }
75  const GncRational amount() const { return m_amount; }
76  const char* purpose() const { return m_purpose.c_str(); }
77  const char* purpose_continuation() const
78  {
79  return m_purpose_continuation.c_str();
80  }
81  void set_name (const char* name) { m_name = name; }
82  void set_recipient_name (const char* name) { m_recipient_name = name; }
83  void set_recipient_account (const char* account) {
84  m_recipient_account = account;
85  }
86  void set_recipient_bankcode (const char* code) {
87  m_recipient_bankcode = code;
88  }
89  void set_amount (GncRational amount) { m_amount = amount; }
90  void set_purpose (const char* purpose) { m_purpose = purpose; }
91  void set_purpose_continuation (const char* name) { m_name = name; }
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 _GncABTransTempl::make_kvp_frame()
104 {
105  auto frame = new KvpFrame;
106  frame->set({TT_NAME}, new KvpValue(g_strdup (m_name.c_str())));
107  frame->set({TT_RNAME}, new KvpValue(g_strdup (m_recipient_name.c_str())));
108  frame->set({TT_RACC}, new KvpValue(g_strdup (m_recipient_account.c_str())));
109  frame->set({TT_RBCODE}, new KvpValue(g_strdup (m_recipient_bankcode.c_str())));
110  frame->set({TT_AMOUNT}, new KvpValue(m_amount));
111  frame->set({TT_PURPOS}, new KvpValue(g_strdup (m_purpose.c_str())));
112  frame->set({TT_PURPOSCT}, new KvpValue(g_strdup (m_purpose_continuation.c_str())));
113  return frame;
114 }
115 
116 GncABTransTempl*
118 {
119  return new _GncABTransTempl;
120 }
121 
122 GncABTransTempl*
123 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  return new _GncABTransTempl(name, recp_name, recp_account, recp_bankcode,
129  amount, purpose, purpose_cont);
130 }
131 
132 GList*
134 {
135  GList *retval = NULL;
136  auto toplevel = qof_instance_get_slots (QOF_INSTANCE (b));
137  auto slot = toplevel->get_slot({"hbci", "template-list"});
138  if (slot == nullptr)
139  return retval;
140  auto list = slot->get<GList*>();
141  for (auto node = list; node != NULL; node = g_list_next (node))
142  {
143  KvpFrame *frame = static_cast<KvpValue*>(node->data)->get<KvpFrame*>();
144  auto c_func = [frame](const char* key)
145  { auto slot = frame->get_slot({key});
146  return slot == nullptr ? std::string("") : std::string(slot->get<const char*>());};
147  auto n_func = [frame](const char* key)
148  { auto slot = frame->get_slot({key});
149  return slot == nullptr ? gnc_numeric_zero() : slot->get<gnc_numeric>();};
150  auto templ = new _GncABTransTempl (c_func(TT_NAME), c_func(TT_RNAME),
151  c_func(TT_RACC), c_func(TT_RBCODE),
152  n_func(TT_AMOUNT), c_func(TT_PURPOS),
153  c_func(TT_PURPOSCT));
154  retval = g_list_prepend (retval, templ);
155  }
156  retval = g_list_reverse (retval);
157  return retval;
158 }
159 
160 void
161 gnc_ab_trans_templ_free (GncABTransTempl *t)
162 {
163  delete t;
164 }
165 
166 void
168 {
169  for(GList *node = l; node != NULL; node = g_list_next(node))
170  delete static_cast<_GncABTransTempl*>(node->data);
171 }
172 static void*
173 copy_list_value(const void* pvalue, void* pdata)
174 {
175  auto new_value = new KvpValue(*static_cast<const KvpValue*>(pvalue));
176  return new_value;
177 }
178 
179 void
180 gnc_ab_set_book_template_list (QofBook *b, GList *template_list)
181 {
182  GList *kvp_list = NULL;
183  for (auto node = template_list; node != NULL; node = g_list_next (node))
184  {
185  auto templ = static_cast<_GncABTransTempl*>(node->data);
186  auto value = new KvpValue(templ->make_kvp_frame());
187  kvp_list = g_list_prepend (kvp_list, value);
188  }
189  kvp_list = g_list_reverse (kvp_list);
190  auto value = new KvpValue(g_list_copy_deep(kvp_list, copy_list_value,
191  nullptr));
192  qof_book_begin_edit(b);
193  KvpFrame *toplevel = qof_instance_get_slots (QOF_INSTANCE (b));
194  delete toplevel->set_path({"hbci", "template-list"}, value);
195  qof_instance_set_dirty_flag (QOF_INSTANCE (b), TRUE);
196  qof_book_commit_edit(b);
197 }
198 
199 const gchar *
200 gnc_ab_trans_templ_get_name(const GncABTransTempl *t)
201 {
202  g_return_val_if_fail(t, NULL);
203  return t->name();
204 }
205 
206 const gchar *
207 gnc_ab_trans_templ_get_recp_name(const GncABTransTempl *t)
208 {
209  g_return_val_if_fail(t, NULL);
210  return t->recipient_name();
211 }
212 
213 const gchar *
214 gnc_ab_trans_templ_get_recp_account(const GncABTransTempl *t)
215 {
216  g_return_val_if_fail(t, NULL);
217  return t->recipient_account();
218 }
219 
220 const gchar *
221 gnc_ab_trans_templ_get_recp_bankcode(const GncABTransTempl *t)
222 {
223  g_return_val_if_fail(t, NULL);
224  return t->recipient_bankcode();
225 }
226 
227 gnc_numeric
228 gnc_ab_trans_templ_get_amount(const GncABTransTempl *t)
229 {
230  g_return_val_if_fail(t, gnc_numeric_zero());
231  return t->amount();
232 }
233 
234 const gchar *
235 gnc_ab_trans_templ_get_purpose(const GncABTransTempl *t)
236 {
237  g_return_val_if_fail(t, NULL);
238  return t->purpose();
239 }
240 
241 const gchar *
242 gnc_ab_trans_templ_get_purpose_cont(const GncABTransTempl *t)
243 {
244  g_return_val_if_fail(t, NULL);
245  return t->purpose_continuation();
246 }
247 
248 void
249 gnc_ab_trans_templ_set_name(GncABTransTempl *t, const gchar *name)
250 {
251  g_return_if_fail(t);
252  t->set_name(name);
253 }
254 
255 void
256 gnc_ab_trans_templ_set_recp_name(GncABTransTempl *t, const gchar *recp_name)
257 {
258  g_return_if_fail(t);
259  t->set_recipient_name(recp_name);
260 }
261 
262 void
264  const gchar *recp_account)
265 {
266  g_return_if_fail(t);
267  t->set_recipient_account(recp_account);
268 }
269 
270 void
272  const gchar *recp_bankcode)
273 {
274  g_return_if_fail(t);
275  t->set_recipient_bankcode(recp_bankcode);
276 }
277 
278 void
279 gnc_ab_trans_templ_set_amount(GncABTransTempl *t, gnc_numeric amount)
280 {
281  g_return_if_fail(t);
282  t->set_amount(amount);
283 }
284 
285 void
286 gnc_ab_trans_templ_set_purpose(GncABTransTempl *t, const gchar *purpose)
287 {
288  g_return_if_fail(t);
289  t->set_purpose(purpose);
290 }
291 
292 void
294  const gchar *purpose_cont)
295 {
296  g_return_if_fail(t);
297  t->set_purpose_continuation (purpose_cont);
298 }
const gchar * gnc_ab_trans_templ_get_purpose(const GncABTransTempl *t)
void gnc_ab_trans_templ_free(GncABTransTempl *t)
Free the memory used by a template.
void gnc_ab_trans_templ_set_purpose_cont(GncABTransTempl *t, const gchar *purpose_cont)
Replace the second purpose line stored in a template.
A template for an AqBanking transaction.
void gnc_ab_trans_templ_set_amount(GncABTransTempl *t, gnc_numeric amount)
Replace the amount stored in a template.
const gchar * gnc_ab_trans_templ_get_recp_account(const GncABTransTempl *t)
void gnc_ab_trans_templ_list_free(GList *l)
Free the memory used by a list of templates, including the list itself.
void gnc_ab_trans_templ_set_recp_account(GncABTransTempl *t, const gchar *recp_account)
Replace the Account Number of the recipient stored in a template.
void gnc_ab_trans_templ_set_name(GncABTransTempl *t, const gchar *name)
Set the name of a template.
void gnc_ab_trans_templ_set_recp_name(GncABTransTempl *t, const gchar *recp_name)
Replace the Account Number of the recipient stored in a template.
void gnc_ab_trans_templ_set_recp_bankcode(GncABTransTempl *t, const gchar *recp_bankcode)
Replace the Bank Code of the recipient stored in a template.
GncABTransTempl * gnc_ab_trans_templ_new()
Create a template with unset contents.
void gnc_ab_trans_templ_set_purpose(GncABTransTempl *t, const gchar *purpose)
Replace the first purpose line stored in a template.
const gchar * gnc_ab_trans_templ_get_name(const GncABTransTempl *t)
gnc_numeric gnc_ab_trans_templ_get_amount(const GncABTransTempl *t)
const gchar * gnc_ab_trans_templ_get_recp_bankcode(const GncABTransTempl *t)
Rational number class using GncInt128 for the numerator and denominator.
void gnc_ab_set_book_template_list(QofBook *b, GList *template_list)
Set the GList of kvp_frames of template transactions in the Book b to template_list.
const gchar * gnc_ab_trans_templ_get_recp_name(const GncABTransTempl *t)
GncABTransTempl * gnc_ab_trans_templ_new_full(const gchar *name, const gchar *recp_name, const gchar *recp_account, const gchar *recp_bankcode, gnc_numeric amount, const gchar *purpose, const gchar *purpose_cont)
Create a template with given contents.
const gchar * gnc_ab_trans_templ_get_purpose_cont(const GncABTransTempl *t)
GList * gnc_ab_trans_templ_list_new_from_book(QofBook *b)
Obtain the list of QofTemplates saved in a Book.