qofinstance.c

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 "kvp-util-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->last_update.tv_sec = 0;
00060         inst->last_update.tv_nsec = -1;
00061         inst->editlevel = 0;
00062         inst->do_free = FALSE;
00063         inst->dirty = FALSE;
00064 
00065         col = qof_book_get_collection (book, type);
00066         qof_entity_init (&inst->entity, type, col);
00067 }
00068 
00069 void
00070 qof_instance_release (QofInstance *inst)
00071 {
00072         kvp_frame_delete (inst->kvp_data);
00073         inst->editlevel = 0;
00074         inst->do_free = FALSE;
00075         inst->dirty = FALSE;
00076         qof_entity_release (&inst->entity);
00077 }
00078 
00079 const GUID *
00080 qof_instance_get_guid (QofInstance *inst)
00081 {
00082         if (!inst) return NULL;
00083         return &inst->entity.guid;
00084 }
00085 
00086 QofBook *
00087 qof_instance_get_book (QofInstance *inst)
00088 {
00089         if (!inst) return NULL;
00090         return inst->book;
00091 }
00092 
00093 KvpFrame*
00094 qof_instance_get_slots (QofInstance *inst)
00095 {
00096   if (!inst) return NULL;
00097   return inst->kvp_data;
00098 }
00099 
00100 Timespec
00101 qof_instance_get_last_update (QofInstance *inst)
00102 {
00103         if (!inst)
00104         {
00105                 Timespec ts = {0,-1};
00106                 return ts;
00107         }
00108         return inst->last_update;
00109 }
00110 
00111 int
00112 qof_instance_version_cmp (QofInstance *left, QofInstance *right)
00113 {
00114         if (!left && !right) return 0;
00115         if (!left) return -1;
00116         if (!right) return +1;
00117         if (left->last_update.tv_sec < right->last_update.tv_sec) return -1;
00118         if (left->last_update.tv_sec > right->last_update.tv_sec) return +1;
00119         if (left->last_update.tv_nsec < right->last_update.tv_nsec) return -1;
00120         if (left->last_update.tv_nsec > right->last_update.tv_nsec) return +1;
00121         return 0;
00122 }
00123 
00124 gboolean
00125 qof_instance_is_dirty (QofInstance *inst)
00126 {
00127         QofCollection *coll;
00128 
00129         if (!inst) { return FALSE; }
00130         coll = inst->entity.collection;
00131         if(qof_collection_is_dirty(coll)) { return inst->dirty; }
00132         inst->dirty = FALSE;
00133         return FALSE;
00134 }
00135 
00136 void
00137 qof_instance_set_dirty(QofInstance* inst)
00138 {
00139         QofCollection *coll;
00140 
00141         inst->dirty = TRUE;
00142         coll = inst->entity.collection;
00143         qof_collection_mark_dirty(coll);
00144 }
00145 
00146 gboolean
00147 qof_instance_check_edit(QofInstance *inst)
00148 {
00149         if(inst->editlevel > 0) { return TRUE; }
00150         return FALSE;
00151 }
00152 
00153 gboolean
00154 qof_instance_do_free(QofInstance *inst)
00155 {
00156         return inst->do_free;
00157 }
00158 
00159 void
00160 qof_instance_mark_free(QofInstance *inst)
00161 {
00162         inst->do_free = TRUE;
00163 }
00164 
00165 /* ========================================================== */
00166 /* setters */
00167 
00168 void
00169 qof_instance_mark_clean (QofInstance *inst)
00170 {
00171   if(!inst) return;
00172   inst->dirty = FALSE;
00173 }
00174 
00175 void
00176 qof_instance_set_slots (QofInstance *inst, KvpFrame *frm)
00177 {
00178   if (!inst) return;
00179   if (inst->kvp_data && (inst->kvp_data != frm))
00180   {
00181     kvp_frame_delete(inst->kvp_data);
00182   }
00183 
00184   inst->dirty = TRUE;
00185   inst->kvp_data = frm;
00186 }
00187 
00188 void
00189 qof_instance_set_last_update (QofInstance *inst, Timespec ts)
00190 {
00191   if (!inst) return;
00192   inst->last_update = ts;
00193 }
00194 
00195 /* ========================================================== */
00196 
00197 void
00198 qof_instance_gemini (QofInstance *to, QofInstance *from)
00199 {
00200   time_t now;
00201 
00202   /* Books must differ for a gemini to be meaningful */
00203   if (!from || !to || (from->book == to->book)) return;
00204 
00205   now = time(0);
00206 
00207   /* Make a note of where the copy came from */
00208   gnc_kvp_bag_add (to->kvp_data, "gemini", now,
00209                                   "inst_guid", &from->entity.guid,
00210                                   "book_guid", &from->book->inst.entity.guid,
00211                                   NULL);
00212   gnc_kvp_bag_add (from->kvp_data, "gemini", now,
00213                                   "inst_guid", &to->entity.guid,
00214                                   "book_guid", &to->book->inst.entity.guid,
00215                                   NULL);
00216 
00217   to->dirty = TRUE;
00218 }
00219 
00220 QofInstance *
00221 qof_instance_lookup_twin (QofInstance *src, QofBook *target_book)
00222 {
00223         QofCollection *col;
00224         KvpFrame *fr;
00225         GUID * twin_guid;
00226         QofInstance * twin;
00227 
00228         if (!src || !target_book) return NULL;
00229         ENTER (" ");
00230 
00231         fr = gnc_kvp_bag_find_by_guid (src->kvp_data, "gemini",
00232                                      "book_guid", &target_book->inst.entity.guid);
00233 
00234         twin_guid = kvp_frame_get_guid (fr, "inst_guid");
00235 
00236         col = qof_book_get_collection (target_book, src->entity.e_type);
00237         twin = (QofInstance *) qof_collection_lookup_entity (col, twin_guid);
00238 
00239         LEAVE (" found twin=%p", twin);
00240         return twin;
00241 }
00242 
00243 /* ========================== END OF FILE ======================= */

Generated on Fri May 12 18:00:33 2006 for QOF by  doxygen 1.4.4