QOF: qofinstance.c Source File

QOF 0.7.5

- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

qofinstance.c
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 *
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
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
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 *
79 qof_instance_get_guid (QofInstance * inst)
80 {
81 if (!inst)
82 return NULL;
83 return &inst->entity.guid;
84 }
85
86 QofBook *
87 qof_instance_get_book (QofInstance * inst)
88 {
89 if (!inst)
90 return NULL;
91 return inst->book;
92 }
93
94 KvpFrame *
95 qof_instance_get_slots (QofInstance * inst)
96 {
97 if (!inst)
98 return NULL;
99 return inst->kvp_data;
100 }
101
102 QofTime *
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
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
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
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
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
166 qof_instance_do_free (QofInstance * inst)
167 {
168 return inst->do_free;
169 }
170
171 void
172 qof_instance_mark_free (QofInstance * inst)
173 {
174 inst->do_free = TRUE;
175 }
176
177 /* ========================================================== */
178 /* setters */
179
180 void
181 qof_instance_mark_clean (QofInstance * inst)
182 {
183 if (!inst)
184 return;
185 inst->dirty = FALSE;
186 }
187
188 void
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
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
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 *
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 ======================= */
_QofBook::inst
QofInstance inst
Definition: qofbook-p.h:51
kvputil-p.h
misc odd-job kvp utils engine-private routines
QofIdType
const gchar * QofIdType
Definition: qofid.h:81
QofInstance_s
Definition: qofinstance-p.h:35
qof_book_get_collection
QofCollection * qof_book_get_collection(QofBook *book, QofIdType entity_type)
Definition: qofbook.c:220
qof_collection_is_dirty
gboolean qof_collection_is_dirty(QofCollection *col)
Definition: qofid.c:362
KvpFrame
struct _KvpFrame KvpFrame
Definition: kvpframe.h:74
qof_instance_is_dirty
gboolean qof_instance_is_dirty(QofInstance *inst)
Definition: qofinstance.c:128
QofInstance_s::kvp_data
KvpFrame * kvp_data
Definition: qofinstance-p.h:47
qof_instance_get_book
QofBook * qof_instance_get_book(QofInstance *inst)
Definition: qofinstance.c:87
LEAVE
#define LEAVE(format, args...)
Definition: qoflog.h:227
qof_instance_set_dirty
void qof_instance_set_dirty(QofInstance *inst)
Set the dirty flag.
Definition: qofinstance.c:146
qofbook-p.h
Private QofBook interface.
qof_time_get_current
QofTime * qof_time_get_current(void)
Get the current QofTime.
Definition: qoftime.c:362
kvp_frame_delete
void kvp_frame_delete(KvpFrame *frame)
Definition: kvpframe.c:118
QofCollection
struct QofCollection_s QofCollection
Definition: qofid.h:138
qof_instance_get_guid
const GUID * qof_instance_get_guid(QofInstance *inst)
Definition: qofinstance.c:79
qof_instance_get_slots
KvpFrame * qof_instance_get_slots(QofInstance *inst)
Definition: qofinstance.c:95
qof_kvp_bag_add
KvpFrame * qof_kvp_bag_add(KvpFrame *kvp_root, const gchar *path, QofTime *qt, const gchar *first_name,...)
Definition: kvputil.c:67
qof_collection_lookup_entity
QofEntity * qof_collection_lookup_entity(QofCollection *col, const GUID *guid)
Definition: qofid.c:321
qof_time_free
void qof_time_free(QofTime *qt)
Free a QofTime when no longer required.
Definition: qoftime.c:56
qof_time_cmp
gint qof_time_cmp(const QofTime *ta, const QofTime *tb)
Definition: qoftime.c:165
qof_entity_release
void qof_entity_release(QofEntity *ent)
Definition: qofid.c:79
qof_instance_get_update_time
QofTime * qof_instance_get_update_time(QofInstance *inst)
Definition: qofinstance.c:103
QofInstance_s::entity
QofEntity entity
Definition: qofinstance-p.h:38
_GUID
Definition: guid.h:53
QofInstance_s::update_time
QofTime * update_time
Definition: qofinstance-p.h:61
QofTime
struct QofTime64 QofTime
Use a 64-bit signed int QofTime.
Definition: qoftime.h:120
qof_kvp_bag_find_by_guid
KvpFrame * qof_kvp_bag_find_by_guid(KvpFrame *root, const gchar *path, const gchar *guid_name, GUID *desired_guid)
Definition: kvputil.c:89
qof_instance_release
void qof_instance_release(QofInstance *inst)
Definition: qofinstance.c:69
_QofBook
Definition: qofbook-p.h:48
kvp_frame_new
KvpFrame * kvp_frame_new(void)
Definition: kvpframe.c:100
qof_instance_version_cmp
int qof_instance_version_cmp(QofInstance *left, QofInstance *right)
Definition: qofinstance.c:116
qof_instance_init
void qof_instance_init(QofInstance *inst, QofIdType type, QofBook *book)
Definition: qofinstance.c:53
QofInstance_s::book
QofBook * book
Definition: qofinstance-p.h:41
qof_entity_init
void qof_entity_init(QofEntity *ent, QofIdType type, QofCollection *tab)
Definition: qofid.c:49
qof_instance_gemini
void qof_instance_gemini(QofInstance *to, QofInstance *from)
Definition: qofinstance.c:212
ENTER
#define ENTER(format, args...)
Definition: qoflog.h:217
QofLogModule
const gchar * QofLogModule
Definition: qofid.h:85
qof_instance_lookup_twin
QofInstance * qof_instance_lookup_twin(QofInstance *src, QofBook *target_book)
Definition: qofinstance.c:234