QOF 0.8.4
|
00001 /********************************************************************\ 00002 * qofinstance.c -- handler for fields common to all objects * 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 \********************************************************************/ 00022 00023 /* 00024 * Object instance holds many common fields that most 00025 * gnucash objects use. 00026 * 00027 * Copyright (C) 2003 Linas Vepstas <linas@linas.org> 00028 */ 00029 00030 #include "config.h" 00031 #include <glib.h> 00032 #include "qof.h" 00033 #include "kvputil-p.h" 00034 #include "qofbook-p.h" 00035 #include "qofid-p.h" 00036 #include "qofinstance-p.h" 00037 00038 static QofLogModule log_module = QOF_MOD_ENGINE; 00039 00040 /* ========================================================== */ 00041 00042 QofInstance * 00043 qof_instance_create (QofIdType type, QofBook * book) 00044 { 00045 QofInstance *inst; 00046 00047 inst = g_new0 (QofInstance, 1); 00048 qof_instance_init (inst, type, book); 00049 return inst; 00050 } 00051 00052 void 00053 qof_instance_init (QofInstance * inst, QofIdType type, QofBook * book) 00054 { 00055 QofCollection *col; 00056 00057 inst->book = book; 00058 inst->kvp_data = kvp_frame_new (); 00059 inst->update_time = qof_time_get_current (); 00060 inst->editlevel = 0; 00061 inst->do_free = FALSE; 00062 inst->dirty = FALSE; 00063 00064 col = qof_book_get_collection (book, type); 00065 qof_entity_init (&inst->entity, type, col); 00066 } 00067 00068 void 00069 qof_instance_release (QofInstance * inst) 00070 { 00071 kvp_frame_delete (inst->kvp_data); 00072 inst->editlevel = 0; 00073 inst->do_free = FALSE; 00074 inst->dirty = FALSE; 00075 qof_entity_release (&inst->entity); 00076 } 00077 00078 const GUID * 00079 qof_instance_get_guid (QofInstance * inst) 00080 { 00081 if (!inst) 00082 return NULL; 00083 return &inst->entity.guid; 00084 } 00085 00086 QofBook * 00087 qof_instance_get_book (QofInstance * inst) 00088 { 00089 if (!inst) 00090 return NULL; 00091 return inst->book; 00092 } 00093 00094 KvpFrame * 00095 qof_instance_get_slots (QofInstance * inst) 00096 { 00097 if (!inst) 00098 return NULL; 00099 return inst->kvp_data; 00100 } 00101 00102 QofTime * 00103 qof_instance_get_update_time (QofInstance * inst) 00104 { 00105 if (!inst) 00106 { 00107 QofTime *time; 00108 00109 time = qof_time_get_current (); 00110 return time; 00111 } 00112 return inst->update_time; 00113 } 00114 00115 int 00116 qof_instance_version_cmp (QofInstance * left, QofInstance * right) 00117 { 00118 if (!left && !right) 00119 return 0; 00120 if (!left) 00121 return -1; 00122 if (!right) 00123 return +1; 00124 return qof_time_cmp (left->update_time, right->update_time); 00125 } 00126 00127 gboolean 00128 qof_instance_is_dirty (QofInstance * inst) 00129 { 00130 QofCollection *coll; 00131 00132 if (!inst) 00133 { 00134 return FALSE; 00135 } 00136 coll = inst->entity.collection; 00137 if (qof_collection_is_dirty (coll)) 00138 { 00139 return inst->dirty; 00140 } 00141 inst->dirty = FALSE; 00142 return FALSE; 00143 } 00144 00145 void 00146 qof_instance_set_dirty (QofInstance * inst) 00147 { 00148 QofCollection *coll; 00149 00150 inst->dirty = TRUE; 00151 coll = inst->entity.collection; 00152 qof_collection_mark_dirty (coll); 00153 } 00154 00155 gboolean 00156 qof_instance_check_edit (QofInstance * inst) 00157 { 00158 if (inst->editlevel > 0) 00159 { 00160 return TRUE; 00161 } 00162 return FALSE; 00163 } 00164 00165 gboolean 00166 qof_instance_do_free (QofInstance * inst) 00167 { 00168 return inst->do_free; 00169 } 00170 00171 void 00172 qof_instance_mark_free (QofInstance * inst) 00173 { 00174 inst->do_free = TRUE; 00175 } 00176 00177 /* ========================================================== */ 00178 /* setters */ 00179 00180 void 00181 qof_instance_mark_clean (QofInstance * inst) 00182 { 00183 if (!inst) 00184 return; 00185 inst->dirty = FALSE; 00186 } 00187 00188 void 00189 qof_instance_set_slots (QofInstance * inst, KvpFrame * frm) 00190 { 00191 if (!inst) 00192 return; 00193 if (inst->kvp_data && (inst->kvp_data != frm)) 00194 { 00195 kvp_frame_delete (inst->kvp_data); 00196 } 00197 00198 inst->dirty = TRUE; 00199 inst->kvp_data = frm; 00200 } 00201 00202 void 00203 qof_instance_set_update_time (QofInstance * inst, QofTime * time) 00204 { 00205 if (!inst) 00206 return; 00207 qof_time_free (inst->update_time); 00208 inst->update_time = time; 00209 } 00210 00211 void 00212 qof_instance_gemini (QofInstance * to, QofInstance * from) 00213 { 00214 QofTime *qt; 00215 00216 /* Books must differ for a gemini to be meaningful */ 00217 if (!from || !to || (from->book == to->book)) 00218 return; 00219 00220 qt = qof_time_get_current (); 00221 00222 /* Make a note of where the copy came from */ 00223 qof_kvp_bag_add (to->kvp_data, "gemini", qt, 00224 "inst_guid", &from->entity.guid, 00225 "book_guid", &from->book->inst.entity.guid, NULL); 00226 qof_kvp_bag_add (from->kvp_data, "gemini", qt, 00227 "inst_guid", &to->entity.guid, 00228 "book_guid", &to->book->inst.entity.guid, NULL); 00229 00230 to->dirty = TRUE; 00231 } 00232 00233 QofInstance * 00234 qof_instance_lookup_twin (QofInstance * src, QofBook * target_book) 00235 { 00236 QofCollection *col; 00237 KvpFrame *fr; 00238 GUID *twin_guid; 00239 QofInstance *twin; 00240 00241 if (!src || !target_book) 00242 return NULL; 00243 ENTER (" "); 00244 00245 fr = qof_kvp_bag_find_by_guid (src->kvp_data, "gemini", 00246 "book_guid", &target_book->inst.entity.guid); 00247 00248 twin_guid = kvp_frame_get_guid (fr, "inst_guid"); 00249 00250 col = qof_book_get_collection (target_book, src->entity.e_type); 00251 twin = (QofInstance *) qof_collection_lookup_entity (col, twin_guid); 00252 00253 LEAVE (" found twin=%p", twin); 00254 return twin; 00255 } 00256 00257 /* ========================== END OF FILE ======================= */