1 /********************************************************************\
2 * qofinstance.c -- handler for fields common to all objects *
3 * *
4 * This program is free software; you can redistribute it and/or *
5 * modify it under the terms of the GNU General Public License as *
6 * published by the Free Software Foundation; either version 2 of *
7 * the License, or (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License*
15 * along with this program; if not, contact: *
16 * *
17 * Free Software Foundation Voice: +1-617-542-5942 *
18 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19 * Boston, MA 02110-1301, USA gnu@gnu.org *
20 * *
21 \********************************************************************/
22
23 /*
24 * Object instance holds many common fields that most
25 * gnucash objects use.
26 *
27 * Copyright (C) 2003 Linas Vepstas <linas@linas.org>
28 */
29
30 #include "config.h"
31 #include <glib.h>
32 #include "qof.h"
33 #include "kvputil-p.h"
34 #include "qofbook-p.h"
35 #include "qofid-p.h"
36 #include "qofinstance-p.h"
37
38 static QofLogModule log_module = QOF_MOD_ENGINE;
39
40 /* ========================================================== */
41
42 QofInstance *
qof_instance_create(QofIdType type,QofBook * book)43 qof_instance_create (QofIdType type, QofBook * book)
44 {
45 QofInstance *inst;
46
47 inst = g_new0 (QofInstance, 1);
48 qof_instance_init (inst, type, book);
49 return inst;
50 }
51
52 void
qof_instance_init(QofInstance * inst,QofIdType type,QofBook * book)53 qof_instance_init (QofInstance * inst, QofIdType type, QofBook * book)
54 {
55 QofCollection *col;
56
57 inst->book = book;
58 inst->kvp_data = kvp_frame_new ();
59 inst->update_time = qof_time_get_current ();
60 inst->editlevel = 0;
61 inst->do_free = FALSE;
62 inst->dirty = FALSE;
63
64 col = qof_book_get_collection (book, type);
65 qof_entity_init (&inst->entity, type, col);
66 }
67
68 void
qof_instance_release(QofInstance * inst)69 qof_instance_release (QofInstance * inst)
70 {
71 kvp_frame_delete (inst->kvp_data);
72 inst->editlevel = 0;
73 inst->do_free = FALSE;
74 inst->dirty = FALSE;
75 qof_entity_release (&inst->entity);
76 }
77
78 const GUID *
qof_instance_get_guid(QofInstance * inst)79 qof_instance_get_guid (QofInstance * inst)
80 {
81 if (!inst)
82 return NULL;
83 return &inst->entity.guid;
84 }
85
86 QofBook *
qof_instance_get_book(QofInstance * inst)87 qof_instance_get_book (QofInstance * inst)
88 {
89 if (!inst)
90 return NULL;
91 return inst->book;
92 }
93
94 KvpFrame *
qof_instance_get_slots(QofInstance * inst)95 qof_instance_get_slots (QofInstance * inst)
96 {
97 if (!inst)
98 return NULL;
99 return inst->kvp_data;
100 }
101
102 QofTime *
qof_instance_get_update_time(QofInstance * inst)103 qof_instance_get_update_time (QofInstance * inst)
104 {
105 if (!inst)
106 {
107 QofTime *time;
108
109 time = qof_time_get_current ();
110 return time;
111 }
112 return inst->update_time;
113 }
114
115 int
qof_instance_version_cmp(QofInstance * left,QofInstance * right)116 qof_instance_version_cmp (QofInstance * left, QofInstance * right)
117 {
118 if (!left && !right)
119 return 0;
120 if (!left)
121 return -1;
122 if (!right)
123 return +1;
124 return qof_time_cmp (left->update_time, right->update_time);
125 }
126
127 gboolean
qof_instance_is_dirty(QofInstance * inst)128 qof_instance_is_dirty (QofInstance * inst)
129 {
130 QofCollection *coll;
131
132 if (!inst)
133 {
134 return FALSE;
135 }
136 coll = inst->entity.collection;
137 if (qof_collection_is_dirty (coll))
138 {
139 return inst->dirty;
140 }
141 inst->dirty = FALSE;
142 return FALSE;
143 }
144
145 void
qof_instance_set_dirty(QofInstance * inst)146 qof_instance_set_dirty (QofInstance * inst)
147 {
148 QofCollection *coll;
149
150 inst->dirty = TRUE;
151 coll = inst->entity.collection;
152 qof_collection_mark_dirty (coll);
153 }
154
155 gboolean
qof_instance_check_edit(QofInstance * inst)156 qof_instance_check_edit (QofInstance * inst)
157 {
158 if (inst->editlevel > 0)
159 {
160 return TRUE;
161 }
162 return FALSE;
163 }
164
165 gboolean
qof_instance_do_free(QofInstance * inst)166 qof_instance_do_free (QofInstance * inst)
167 {
168 return inst->do_free;
169 }
170
171 void
qof_instance_mark_free(QofInstance * inst)172 qof_instance_mark_free (QofInstance * inst)
173 {
174 inst->do_free = TRUE;
175 }
176
177 /* ========================================================== */
178 /* setters */
179
180 void
qof_instance_mark_clean(QofInstance * inst)181 qof_instance_mark_clean (QofInstance * inst)
182 {
183 if (!inst)
184 return;
185 inst->dirty = FALSE;
186 }
187
188 void
qof_instance_set_slots(QofInstance * inst,KvpFrame * frm)189 qof_instance_set_slots (QofInstance * inst, KvpFrame * frm)
190 {
191 if (!inst)
192 return;
193 if (inst->kvp_data && (inst->kvp_data != frm))
194 {
195 kvp_frame_delete (inst->kvp_data);
196 }
197
198 inst->dirty = TRUE;
199 inst->kvp_data = frm;
200 }
201
202 void
qof_instance_set_update_time(QofInstance * inst,QofTime * time)203 qof_instance_set_update_time (QofInstance * inst, QofTime * time)
204 {
205 if (!inst)
206 return;
207 qof_time_free (inst->update_time);
208 inst->update_time = time;
209 }
210
211 void
qof_instance_gemini(QofInstance * to,QofInstance * from)212 qof_instance_gemini (QofInstance * to, QofInstance * from)
213 {
214 QofTime *qt;
215
216 /* Books must differ for a gemini to be meaningful */
217 if (!from || !to || (from->book == to->book))
218 return;
219
220 qt = qof_time_get_current ();
221
222 /* Make a note of where the copy came from */
223 qof_kvp_bag_add (to->kvp_data, "gemini", qt,
224 "inst_guid", &from->entity.guid,
225 "book_guid", &from->book->inst.entity.guid, NULL);
226 qof_kvp_bag_add (from->kvp_data, "gemini", qt,
227 "inst_guid", &to->entity.guid,
228 "book_guid", &to->book->inst.entity.guid, NULL);
229
230 to->dirty = TRUE;
231 }
232
233 QofInstance *
qof_instance_lookup_twin(QofInstance * src,QofBook * target_book)234 qof_instance_lookup_twin (QofInstance * src, QofBook * target_book)
235 {
236 QofCollection *col;
237 KvpFrame *fr;
238 GUID *twin_guid;
239 QofInstance *twin;
240
241 if (!src || !target_book)
242 return NULL;
243 ENTER (" ");
244
245 fr = qof_kvp_bag_find_by_guid (src->kvp_data, "gemini",
246 "book_guid", &target_book->inst.entity.guid);
247
248 twin_guid = kvp_frame_get_guid (fr, "inst_guid");
249
250 col = qof_book_get_collection (target_book, src->entity.e_type);
251 twin = (QofInstance *) qof_collection_lookup_entity (col, twin_guid);
252
253 LEAVE (" found twin=%p", twin);
254 return twin;
255 }
256
257 /* ========================== END OF FILE ======================= */
258