GnuCash 2.4.99
gnc-ab-trans-templ.c
00001 /*
00002  * gnc-ab-trans-templ.c --
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of
00007  * the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, contact:
00016  *
00017  * Free Software Foundation           Voice:  +1-617-542-5942
00018  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
00019  * Boston, MA  02110-1301,  USA       gnu@gnu.org
00020  */
00021 
00030 #include "config.h"
00031 
00032 #include "gnc-ab-trans-templ.h"
00033 
00034 /* This static indicates the debugging module that this .o belongs to.  */
00035 static QofLogModule log_module = G_LOG_DOMAIN;
00036 
00037 /* kvp_frame slot names */
00038 #define TT_NAME "name"
00039 #define TT_RNAME "rnam"
00040 #define TT_RACC "racc"
00041 #define TT_RBCODE "rbcd"
00042 #define TT_PURPOS "purp"
00043 #define TT_PURPOSCT "purc"
00044 #define TT_AMOUNT "amou"
00045 
00046 struct _GncABTransTempl
00047 {
00048     /* Name of this Template */
00049     gchar *name;
00050     gchar *name_key; /* Collation key */
00051 
00052     /* Recipient */
00053     gchar *recp_name;
00054     gchar *recp_account;
00055     gchar *recp_bankcode;
00056 
00057     /* Amount */
00058     gnc_numeric amount;
00059 
00060     /* Purpose, description */
00061     gchar *purpose;
00062     gchar *purpose_cont;
00063 };
00064 
00065 
00066 GncABTransTempl *
00067 gnc_ab_trans_templ_new(void)
00068 {
00069     return gnc_ab_trans_templ_new_full(NULL, NULL, NULL, NULL,
00070                                        gnc_numeric_zero(), NULL, NULL);
00071 }
00072 
00073 GncABTransTempl *
00074 gnc_ab_trans_templ_new_full(const char *name, const char *recp_name,
00075                             const char *recp_account, const char *recp_bankcode,
00076                             gnc_numeric amount, const char *purpose,
00077                             const char *purpose_cont)
00078 {
00079     GncABTransTempl *r = g_new(GncABTransTempl, 1);
00080     r->name = g_strdup(name);
00081     r->name_key = g_utf8_collate_key(name, -1);
00082     r->recp_name = g_strdup(recp_name);
00083     r->recp_account = g_strdup(recp_account);
00084     r->recp_bankcode = g_strdup(recp_bankcode);
00085     r->amount = amount;
00086     r->purpose = g_strdup(purpose);
00087     r->purpose_cont = g_strdup(purpose_cont);
00088 
00089     return r;
00090 }
00091 
00092 GncABTransTempl *
00093 gnc_ab_trans_templ_new_from_kvp(const kvp_frame *k)
00094 {
00095     g_return_val_if_fail(k, NULL);
00096 
00097     return gnc_ab_trans_templ_new_full(
00098                kvp_value_get_string(kvp_frame_get_slot(k, TT_NAME)),
00099                kvp_value_get_string(kvp_frame_get_slot(k, TT_RNAME)),
00100                kvp_value_get_string(kvp_frame_get_slot(k, TT_RACC)),
00101                kvp_value_get_string(kvp_frame_get_slot(k, TT_RBCODE)),
00102                kvp_value_get_numeric(kvp_frame_get_slot(k, TT_AMOUNT)),
00103                kvp_value_get_string(kvp_frame_get_slot(k, TT_PURPOS)),
00104                kvp_value_get_string(kvp_frame_get_slot(k, TT_PURPOSCT)));
00105 }
00106 
00107 GList *
00108 gnc_ab_trans_templ_list_new_from_kvp_list(GList *v)
00109 {
00110     GList *res = NULL;
00111     GList *iter;
00112 
00113     for (iter = v; iter; iter = iter->next)
00114     {
00115         kvp_frame *frame = kvp_value_get_frame((kvp_value*) iter->data);
00116         res = g_list_prepend(res, gnc_ab_trans_templ_new_from_kvp(frame));
00117     }
00118     res = g_list_reverse(res);
00119 
00120     return res;
00121 }
00122 
00123 void
00124 gnc_ab_trans_templ_free(GncABTransTempl *t)
00125 {
00126     if (!t) return;
00127     g_free(t->name);
00128     g_free(t->name_key);
00129     g_free(t->recp_name);
00130     g_free(t->recp_account);
00131     g_free(t->recp_bankcode);
00132     g_free(t->purpose);
00133     g_free(t->purpose_cont);
00134     g_free(t);
00135 }
00136 
00137 void
00138 gnc_ab_trans_templ_list_free(GList *l)
00139 {
00140     GList *iter;
00141     for (iter = l; iter; iter = iter->next)
00142         gnc_ab_trans_templ_free((GncABTransTempl*) iter->data);
00143     g_list_free(l);
00144 }
00145 
00146 kvp_frame *
00147 gnc_ab_trans_templ_to_kvp(const GncABTransTempl *t)
00148 {
00149     kvp_frame *k;
00150 
00151     g_return_val_if_fail(t, NULL);
00152 
00153     k = kvp_frame_new();
00154     kvp_frame_set_slot(k, TT_NAME, kvp_value_new_string(t->name));
00155     kvp_frame_set_slot(k, TT_RNAME, kvp_value_new_string(t->recp_name));
00156     kvp_frame_set_slot(k, TT_RACC, kvp_value_new_string(t->recp_account));
00157     kvp_frame_set_slot(k, TT_RBCODE, kvp_value_new_string(t->recp_bankcode));
00158     kvp_frame_set_slot(k, TT_AMOUNT, kvp_value_new_gnc_numeric(t->amount));
00159     kvp_frame_set_slot(k, TT_PURPOS, kvp_value_new_string(t->purpose));
00160     kvp_frame_set_slot(k, TT_PURPOSCT, kvp_value_new_string(t->purpose_cont));
00161 
00162     return k;
00163 }
00164 
00165 GList *
00166 gnc_ab_trans_templ_list_to_kvp_list(GList *k)
00167 {
00168     GList *res = NULL;
00169     GList *iter;
00170 
00171     for (iter = k; iter; iter = iter->next)
00172     {
00173         GncABTransTempl *t = (GncABTransTempl*) iter->data;
00174         kvp_value *value = kvp_value_new_frame_nc(gnc_ab_trans_templ_to_kvp(t));
00175         res = g_list_prepend(res, value);
00176     }
00177     res = g_list_reverse(res);
00178 
00179     return res;
00180 }
00181 
00182 const gchar *
00183 gnc_ab_trans_templ_get_name(const GncABTransTempl *t)
00184 {
00185     g_return_val_if_fail(t, NULL);
00186     return t->name;
00187 }
00188 
00189 const gchar *
00190 gnc_ab_trans_templ_get_recp_name(const GncABTransTempl *t)
00191 {
00192     g_return_val_if_fail(t, NULL);
00193     return t->recp_name;
00194 }
00195 
00196 const gchar *
00197 gnc_ab_trans_templ_get_recp_account(const GncABTransTempl *t)
00198 {
00199     g_return_val_if_fail(t, NULL);
00200     return t->recp_account;
00201 }
00202 
00203 const gchar *
00204 gnc_ab_trans_templ_get_recp_bankcode(const GncABTransTempl *t)
00205 {
00206     g_return_val_if_fail(t, NULL);
00207     return t->recp_bankcode;
00208 }
00209 
00210 gnc_numeric
00211 gnc_ab_trans_templ_get_amount(const GncABTransTempl *t)
00212 {
00213     g_return_val_if_fail(t, gnc_numeric_zero());
00214     return t->amount;
00215 }
00216 
00217 const gchar *
00218 gnc_ab_trans_templ_get_purpose(const GncABTransTempl *t)
00219 {
00220     g_return_val_if_fail(t, NULL);
00221     return t->purpose;
00222 }
00223 
00224 const gchar *
00225 gnc_ab_trans_templ_get_purpose_cont(const GncABTransTempl *t)
00226 {
00227     g_return_val_if_fail(t, NULL);
00228     return t->purpose_cont;
00229 }
00230 
00231 void
00232 gnc_ab_trans_templ_set_name(GncABTransTempl *t, const gchar *name)
00233 {
00234     g_return_if_fail(t);
00235     g_free(t->name);
00236     t->name = g_strdup(name);
00237 }
00238 
00239 void
00240 gnc_ab_trans_templ_set_recp_name(GncABTransTempl *t, const gchar *recp_name)
00241 {
00242     g_return_if_fail(t);
00243     g_free(t->recp_name);
00244     t->recp_name = g_strdup(recp_name);
00245 }
00246 
00247 void
00248 gnc_ab_trans_templ_set_recp_account(GncABTransTempl *t,
00249                                     const gchar *recp_account)
00250 {
00251     g_return_if_fail(t);
00252     g_free(t->recp_account);
00253     t->recp_account = g_strdup(recp_account);
00254 }
00255 
00256 void
00257 gnc_ab_trans_templ_set_recp_bankcode(GncABTransTempl *t,
00258                                      const gchar *recp_bankcode)
00259 {
00260     g_return_if_fail(t);
00261     g_free(t->recp_bankcode);
00262     t->recp_bankcode = g_strdup(recp_bankcode);
00263 }
00264 
00265 void
00266 gnc_ab_trans_templ_set_amount(GncABTransTempl *t, gnc_numeric amount)
00267 {
00268     g_return_if_fail(t);
00269     t->amount = amount;
00270 }
00271 
00272 void
00273 gnc_ab_trans_templ_set_purpose(GncABTransTempl *t, const gchar *purpose)
00274 {
00275     g_return_if_fail(t);
00276     g_free(t->purpose);
00277     t->purpose = g_strdup(purpose);
00278 }
00279 
00280 void
00281 gnc_ab_trans_templ_set_purpose_cont(GncABTransTempl *t,
00282                                     const gchar *purpose_cont)
00283 {
00284     g_return_if_fail(t);
00285     g_free(t->purpose_cont);
00286     t->purpose_cont = g_strdup(purpose_cont);
00287 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines