[Gnucash-changes] port over prices to new api

Linas Vepstas linas at cvs.gnucash.org
Sun Jun 13 00:56:08 EDT 2004


Log Message:
-----------
port over prices to new api

Tags:
----
backend-work-1

Modified Files:
--------------
    gnucash/src/engine:
        Group.h
        gnc-pricedb-p.h
        gnc-pricedb.c
        gnc-pricedb.h

Revision Data
-------------
Index: gnc-pricedb-p.h
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/gnc-pricedb-p.h,v
retrieving revision 1.29.2.1
retrieving revision 1.29.2.2
diff -Lsrc/engine/gnc-pricedb-p.h -Lsrc/engine/gnc-pricedb-p.h -u -r1.29.2.1 -r1.29.2.2
--- src/engine/gnc-pricedb-p.h
+++ src/engine/gnc-pricedb-p.h
@@ -94,12 +94,7 @@
 } GNCPriceLookupHelper;
 
 #define  gnc_price_set_guid(P,G)  qof_entity_set_guid(QOF_ENTITY(P),(G))
-
-void     gnc_pricedb_set_db(QofBook *book, GNCPriceDB *db);
-void     gnc_collection_set_pricedb(QofCollection *col, GNCPriceDB *db);
-
-
-void     gnc_pricedb_mark_clean(GNCPriceDB *db);
+#define  gnc_pricedb_mark_clean(db) qof_instance_mark_clean(QOF_INSTANCE(db))
 void     gnc_pricedb_substitute_commodity(GNCPriceDB *db,
                                           gnc_commodity *old_c,
                                           gnc_commodity *new_c);
Index: gnc-pricedb.h
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/gnc-pricedb.h,v
retrieving revision 1.42
retrieving revision 1.42.2.1
diff -Lsrc/engine/gnc-pricedb.h -Lsrc/engine/gnc-pricedb.h -u -r1.42 -r1.42.2.1
--- src/engine/gnc-pricedb.h
+++ src/engine/gnc-pricedb.h
@@ -374,7 +374,7 @@
 
 /** gnc_pricedb_dirty - return FALSE if the database has not been
    modified. */
-gboolean gnc_pricedb_dirty(GNCPriceDB *db);
+#define gnc_pricedb_dirty(db) qof_instance_is_dirty(QOF_INSTANCE(db))
 
 /** gnc_pricedb_get_num_prices - return the number of prices
    in the database. */
Index: Group.h
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/Group.h,v
retrieving revision 1.73
retrieving revision 1.73.2.1
diff -Lsrc/engine/Group.h -Lsrc/engine/Group.h -u -r1.73 -r1.73.2.1
--- src/engine/Group.h
+++ src/engine/Group.h
@@ -59,7 +59,10 @@
  */
 AccountGroup * xaccGetAccountGroup (QofBook *book);
 
-/** huh  ?? */
+/** 
+ * The xaccCollAccountGroup() routine will return the top-most
+ * account group associated with the indicated collection.
+ */
 AccountGroup * xaccCollGetAccountGroup (QofCollection *col);
 
 /** The xaccAccountDestroy() routine will destroy and free all 
Index: gnc-pricedb.c
===================================================================
RCS file: /home/cvs/cvsroot/gnucash/src/engine/gnc-pricedb.c,v
retrieving revision 1.75.2.1
retrieving revision 1.75.2.2
diff -Lsrc/engine/gnc-pricedb.c -Lsrc/engine/gnc-pricedb.c -u -r1.75.2.1 -r1.75.2.2
--- src/engine/gnc-pricedb.c
+++ src/engine/gnc-pricedb.c
@@ -176,81 +176,14 @@
 void 
 gnc_pricedb_begin_edit (GNCPriceDB *pdb)
 {
-  QofBackend *be;
-
-  if (!pdb) return;
-  pdb->editlevel++;
-  if (1 < pdb->editlevel) return;
-  ENTER ("pdb=%p\n", pdb);
-
-  if (0 >= pdb->editlevel)
-  {
-    PERR ("unbalanced call - resetting (was %d)", pdb->editlevel);
-    pdb->editlevel = 1;
-  }
-
-  /* See if there's a backend.  If there is, invoke it. */
-  be = xaccPriceDBGetBackend (pdb);
-  if (be && be->begin) 
-  {
-    (be->begin) (be, &(pdb->inst));
-  }
-
-  LEAVE ("pdb=%p\n", pdb);
+  QOF_BEGIN_EDIT (&pdb->inst);
 }
 
 void 
 gnc_pricedb_commit_edit (GNCPriceDB *pdb)
 {
-  QofBackend *be;
-  if (!pdb) return;
-
-  pdb->editlevel--;
-  if (0 < pdb->editlevel) return;
-
-  ENTER ("pdb=%p\n", pdb);
-  if (0 > pdb->editlevel)
-  {
-    PERR ("unbalanced call - resetting (was %d)", pdb->editlevel);
-    pdb->editlevel = 0;
-  }
-
-  /* See if there's a backend.  If there is, invoke it. */
-  /* We may not be able to find the backend, so make not of that .. */
-  be = xaccPriceDBGetBackend (pdb);
-  if (be && be->commit) 
-  {
-    QofBackendError errcode;
-
-    /* clear errors */
-    do {
-      errcode = qof_backend_get_error (be);
-    } while (ERR_BACKEND_NO_ERR != errcode);
-
-    /* if we haven't been able to call begin edit before, call it now */
-    if (TRUE == pdb->dirty) 
-    {
-      if (be->begin)
-      {
-        (be->begin) (be, GNC_ID_PRICEDB, pdb);
-      }
-    }
-
-    (be->commit) (be, GNC_ID_PRICEDB, pdb);
-    errcode = qof_backend_get_error (be);
-    if (ERR_BACKEND_NO_ERR != errcode) 
-    {
-      /* XXX hack alert FIXME implement price rollback */
-      PERR (" backend asked engine to rollback, but this isn't"
-            " handled yet. Return code=%d", errcode);
-
-      /* push error back onto the stack */
-      qof_backend_set_error (be, errcode);
-    }
-  }
-  pdb->dirty = FALSE;
-
-  LEAVE ("pdb=%p\n", pdb);
+  QOF_COMMIT_EDIT_PART1 (&pdb->inst);
+  QOF_COMMIT_EDIT_PART2 (&pdb->inst, commit_err, noop, noop);
 }
 
 /* ==================================================================== */
@@ -270,7 +203,7 @@
     remove_price (p->db, p, TRUE);
     gnc_price_begin_edit (p);
     p->commodity = c;
-    if(p->db) p->db->dirty = TRUE;
+    if(p->db) p->db->inst.dirty = TRUE;
     gnc_price_commit_edit (p);
     add_price (p->db, p);
     gnc_price_unref (p);
@@ -292,7 +225,7 @@
     remove_price (p->db, p, TRUE);
     gnc_price_begin_edit (p);
     p->currency = c;
-    if(p->db) p->db->dirty = TRUE;
+    if(p->db) p->db->inst.dirty = TRUE;
     gnc_price_commit_edit (p);
     add_price (p->db, p);
     gnc_price_unref (p);
@@ -312,7 +245,7 @@
     remove_price (p->db, p, FALSE);
     gnc_price_begin_edit (p);
     p->tmspec = t;
-    if(p->db) p->db->dirty = TRUE;
+    if(p->db) p->db->inst.dirty = TRUE;
     gnc_price_commit_edit (p);
     add_price (p->db, p);
     gnc_price_unref (p);
@@ -333,7 +266,7 @@
     tmp = g_cache_insert(cache, (gpointer) s);
     if(p->source) g_cache_remove(cache, p->source);
     p->source = tmp;
-    if(p->db) p->db->dirty = TRUE;
+    if(p->db) p->db->inst.dirty = TRUE;
     gnc_price_commit_edit (p);
   }
 }
@@ -352,7 +285,7 @@
     tmp = g_cache_insert(cache, (gpointer) type);
     if(p->type) g_cache_remove(cache, p->type);
     p->type = tmp;
-    if(p->db) p->db->dirty = TRUE;
+    if(p->db) p->db->inst.dirty = TRUE;
     gnc_price_commit_edit (p);
   }
 }
@@ -365,7 +298,7 @@
   {
     gnc_price_begin_edit (p);
     p->value = value;
-    if(p->db) p->db->dirty = TRUE;
+    if(p->db) p->db->inst.dirty = TRUE;
     gnc_price_commit_edit (p);
   }
 }
@@ -623,10 +556,22 @@
 
   g_return_val_if_fail (book, NULL);
 
+  /* There can only be one pricedb per book.  So if one exits already,
+   * then use that.  Warn user, they shouldn't be creating two ... 
+   */
+  QofCollection *col = qof_book_get_collection (book, GNC_ID_PRICEDB);
+  result = qof_collection_get_data (col);
+  if (result) 
+  {
+    PWARN ("A price database already exists for this book!");
+    return result;
+  }
+
   result = g_new0(GNCPriceDB, 1);
-  result->book = book;
-  result->editlevel = 0;
-  result->dirty = FALSE;
+  qof_instance_init (&result->inst, GNC_ID_PRICEDB, book);
+
+  qof_collection_set_data (col, result);
+
   result->commodity_hash = g_hash_table_new(commodity_hash, commodity_equal);
   g_return_val_if_fail (result->commodity_hash, NULL);
   return result;
@@ -672,11 +617,16 @@
                         NULL);
   g_hash_table_destroy (db->commodity_hash);
   db->commodity_hash = NULL;
-  db->book = NULL;
+  qof_instance_release (&db->inst);
   g_free(db);
 }
 
 /* ==================================================================== */
+/* This is kind of weird, the way its done.  Each collection of prices
+ * for a given commodity should get its own guid, be its own entity, etc.
+ * We really shouldn't be using the collection data.  But, hey I guess its OK,
+ * yeah?
+ */
 
 GNCPriceDB *
 gnc_collection_get_pricedb(QofCollection *col)
@@ -689,50 +639,10 @@
 {
   QofCollection *col;
   if (!book) return NULL;
-  col = qof_book_get_collection (book, GNC_ID_PRICE);
+  col = qof_book_get_collection (book, GNC_ID_PRICEDB);
   return gnc_collection_get_pricedb (col);
 }
 
-void
-gnc_collection_set_pricedb(QofCollection *col, GNCPriceDB *db)
-{
-  GNCPriceDB *old_db;
-  
-  if(!col) return;
-  
-  old_db = qof_collection_get_data (col);
-  if (db == old_db) return;
-  
-  qof_collection_set_data (col, db);
-  gnc_pricedb_destroy (old_db);
-}
-
-void
-gnc_pricedb_set_db(QofBook *book, GNCPriceDB *db)
-{
-  QofCollection *col;
-  if(!book) return;
-  col = qof_book_get_collection (book, GNC_ID_PRICE);
-  gnc_collection_set_pricedb (col, db);
-  if (db) db->book = book;
-}
-
-/* ==================================================================== */
-
-gboolean
-gnc_pricedb_dirty(GNCPriceDB *p)
-{
-  if(!p) return FALSE;
-  return p->dirty;
-}
-
-void
-gnc_pricedb_mark_clean(GNCPriceDB *p)
-{
-  if(!p) return;
-  p->dirty = FALSE;
-}
-
 /* ==================================================================== */
 
 static gboolean
@@ -841,9 +751,7 @@
   ENTER ("db=%p, pr=%p dirty=%d do-free=%d",
          db, p, p->inst.dirty, p->inst.do_free);
 
-  /* initialize the book pointer for the first time, if needed */
-  if (NULL == db->book) db->book = p->inst.book;
-  if (db->book != p->inst.book)
+  if (db->inst.book != p->inst.book)
   {
      PERR ("attempted to mix up prices across different books");
      return FALSE;
@@ -897,7 +805,7 @@
   if (TRUE == p->inst.dirty) 
   {
     gnc_price_begin_edit(p);
-    db->dirty = TRUE;
+    db->inst.dirty = TRUE;
     gnc_price_commit_edit(p);
   }
 
@@ -976,7 +884,7 @@
 
   /* invoke the backend to delete this price */
   gnc_price_begin_edit (p);
-  db->dirty = TRUE;
+  db->inst.dirty = TRUE;
   p->inst.do_free = TRUE;
   gnc_price_commit_edit (p);
 
@@ -1001,14 +909,14 @@
   ENTER ("db=%p commodity=%p currency=%p", db, commodity, currency);
   if(!db || !commodity || !currency) return NULL;
 
-  if (db->book && db->book->backend && db->book->backend->price_lookup)
+  if (db->inst.book->backend && db->inst.book->backend->price_lookup)
   {
      GNCPriceLookup pl;
      pl.type = LOOKUP_LATEST;
      pl.prdb = db;
      pl.commodity = commodity;
      pl.currency = currency;
-     (db->book->backend->price_lookup) (db->book->backend, &pl);
+     (db->inst.book->backend->price_lookup) (db->inst.book->backend, &pl);
   }
 
   currency_hash = g_hash_table_lookup(db->commodity_hash, commodity);
@@ -1052,14 +960,14 @@
   ENTER ("db=%p commodity=%p", db, commodity);
   if(!db || !commodity) return NULL;
 
-  if (db->book && db->book->backend && db->book->backend->price_lookup)
+  if (db->inst.book->backend && db->inst.book->backend->price_lookup)
   {
      GNCPriceLookup pl;
      pl.type = LOOKUP_LATEST;
      pl.prdb = db;
      pl.commodity = commodity;
      pl.currency = NULL;  /* can the backend handle this??? */
-     (db->book->backend->price_lookup) (db->book->backend, &pl);
+     (db->inst.book->backend->price_lookup) (db->inst.book->backend, &pl);
   }
 
   currency_hash = g_hash_table_lookup(db->commodity_hash, commodity);
@@ -1096,14 +1004,14 @@
   ENTER ("db=%p commodity=%p currency=%p", db, commodity, currency);
   if(!db || !commodity) return NULL;
 
-  if (db->book && db->book->backend && db->book->backend->price_lookup)
+  if (db->inst.book->backend && db->inst.book->backend->price_lookup)
   {
      GNCPriceLookup pl;
      pl.type = LOOKUP_ALL;
      pl.prdb = db;
      pl.commodity = commodity;
      pl.currency = currency;
-     (db->book->backend->price_lookup) (db->book->backend, &pl);
+     (db->inst.book->backend->price_lookup) (db->inst.book->backend, &pl);
   }
 
   currency_hash = g_hash_table_lookup(db->commodity_hash, commodity);
@@ -1142,7 +1050,7 @@
   /* Convert to noon local time. */
   t = timespecCanonicalDayTime(t);
 
-  if (db->book && db->book->backend && db->book->backend->price_lookup)
+  if (db->inst.book->backend && db->inst.book->backend->price_lookup)
   {
      GNCPriceLookup pl;
      pl.type = LOOKUP_AT_TIME;
@@ -1150,7 +1058,7 @@
      pl.commodity = c;
      pl.currency = currency;
      pl.date = t;
-     (db->book->backend->price_lookup) (db->book->backend, &pl);
+     (db->inst.book->backend->price_lookup) (db->inst.book->backend, &pl);
   }
 
   currency_hash = g_hash_table_lookup(db->commodity_hash, c);
@@ -1210,7 +1118,7 @@
   /* Convert to noon local time. */
   t = timespecCanonicalDayTime(t);
 
-  if (db->book && db->book->backend && db->book->backend->price_lookup)
+  if (db->inst.book->backend && db->inst.book->backend->price_lookup)
   {
      GNCPriceLookup pl;
      pl.type = LOOKUP_AT_TIME;
@@ -1218,7 +1126,7 @@
      pl.commodity = c;
      pl.currency = NULL;  /* can the backend handle this??? */
      pl.date = t;
-     (db->book->backend->price_lookup) (db->book->backend, &pl);
+     (db->inst.book->backend->price_lookup) (db->inst.book->backend, &pl);
   }
 
   currency_hash = g_hash_table_lookup(db->commodity_hash, c);
@@ -1251,7 +1159,7 @@
   ENTER ("db=%p commodity=%p currency=%p", db, c, currency);
   if(!db || !c || !currency) return NULL;
 
-  if (db->book && db->book->backend && db->book->backend->price_lookup)
+  if (db->inst.book->backend && db->inst.book->backend->price_lookup)
   {
      GNCPriceLookup pl;
      pl.type = LOOKUP_AT_TIME;
@@ -1259,7 +1167,7 @@
      pl.commodity = c;
      pl.currency = currency;
      pl.date = t;
-     (db->book->backend->price_lookup) (db->book->backend, &pl);
+     (db->inst.book->backend->price_lookup) (db->inst.book->backend, &pl);
   }
 
   currency_hash = g_hash_table_lookup(db->commodity_hash, c);
@@ -1315,7 +1223,7 @@
   ENTER ("db=%p commodity=%p", db, c);
   if(!db || !c) return NULL;
 
-  if (db->book && db->book->backend && db->book->backend->price_lookup)
+  if (db->inst.book->backend && db->inst.book->backend->price_lookup)
   {
      GNCPriceLookup pl;
      pl.type = LOOKUP_AT_TIME;
@@ -1323,7 +1231,7 @@
      pl.commodity = c;
      pl.currency = NULL;  /* can the backend handle this??? */
      pl.date = t;
-     (db->book->backend->price_lookup) (db->book->backend, &pl);
+     (db->inst.book->backend->price_lookup) (db->inst.book->backend, &pl);
   }
 
   currency_hash = g_hash_table_lookup(db->commodity_hash, c);
@@ -1358,7 +1266,7 @@
   ENTER ("db=%p commodity=%p currency=%p", db, c, currency);
   if(!db || !c || !currency) return NULL;
 
-  if (db->book && db->book->backend && db->book->backend->price_lookup)
+  if (db->inst.book->backend && db->inst.book->backend->price_lookup)
   {
      GNCPriceLookup pl;
      pl.type = LOOKUP_NEAREST_IN_TIME;
@@ -1366,7 +1274,7 @@
      pl.commodity = c;
      pl.currency = currency;
      pl.date = t;
-     (db->book->backend->price_lookup) (db->book->backend, &pl);
+     (db->inst.book->backend->price_lookup) (db->inst.book->backend, &pl);
   }
 
   currency_hash = g_hash_table_lookup(db->commodity_hash, c);
@@ -1482,7 +1390,7 @@
   ENTER ("db=%p commodity=%p", db, c);
   if(!db || !c) return NULL;
 
-  if (db->book && db->book->backend && db->book->backend->price_lookup)
+  if (db->inst.book->backend && db->inst.book->backend->price_lookup)
   {
      GNCPriceLookup pl;
      pl.type = LOOKUP_NEAREST_IN_TIME;
@@ -1490,7 +1398,7 @@
      pl.commodity = c;
      pl.currency = NULL;  /* can the backend handle this??? */
      pl.date = t;
-     (db->book->backend->price_lookup) (db->book->backend, &pl);
+     (db->inst.book->backend->price_lookup) (db->inst.book->backend, &pl);
   }
 
   currency_hash = g_hash_table_lookup(db->commodity_hash, c);
@@ -1931,28 +1839,18 @@
 }
 
 /* ==================================================================== */
-
-QofBackend *
-xaccPriceDBGetBackend (GNCPriceDB *prdb)
-{
-  if (!prdb || !prdb->book) return NULL;
-  return prdb->book->backend;
-}
-
-/* ==================================================================== */
 /* gncObject function implementation and registration */
 
 static void 
 pricedb_book_begin (QofBook *book)
 {
-  gnc_pricedb_set_db (book, gnc_pricedb_create(book));
+  gnc_pricedb_create(book);
 }
 
 static void 
 pricedb_book_end (QofBook *book)
 {
-  /* unhook the prices */
-  gnc_pricedb_set_db (book, NULL);
+  /* ????? */
 }
 
 static gboolean


More information about the gnucash-changes mailing list