From aa151f0ae03f87a7a3963a63e2043b6ce54f64a5 Mon Sep 17 00:00:00 2001 From: Christian Stimming Date: Mon, 10 Jan 2011 21:39:06 +0000 Subject: [PATCH] Bug #638543: Centralize the counter formatting in qofbook. Patch by Matthijs Kooijman: Instead of querying qofbook for the next counter number and then doing the formatting in all the different business modules (all using the same format string), the formatting is now moved inside qof_book_increment_and_get_counter (which is renamed to qof_book_increment_and_format_counter). This changes the return value of a bunch of helper functions from gint64 (the counter value) to gchar* (the formatted counter value), but does not have any user-visible changes. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@20054 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/business/business-gnome/dialog-customer.c | 3 +-- src/business/business-gnome/dialog-employee.c | 3 +-- src/business/business-gnome/dialog-invoice.c | 3 +-- src/business/business-gnome/dialog-job.c | 3 +-- src/business/business-gnome/dialog-order.c | 3 +-- src/business/business-gnome/dialog-vendor.c | 3 +-- src/engine/gncCustomer.c | 4 ++-- src/engine/gncCustomerP.h | 2 +- src/engine/gncEmployee.c | 4 ++-- src/engine/gncEmployeeP.h | 2 +- src/engine/gncInvoice.c | 12 ++++++------ src/engine/gncInvoiceP.h | 2 +- src/engine/gncJob.c | 4 ++-- src/engine/gncJobP.h | 2 +- src/engine/gncOrder.c | 4 ++-- src/engine/gncOrderP.h | 2 +- src/engine/gncVendor.c | 4 ++-- src/engine/gncVendorP.h | 2 +- src/libqof/qof/qofbook.c | 15 ++++++++------- src/libqof/qof/qofbook.h | 8 ++++---- 20 files changed, 40 insertions(+), 45 deletions(-) diff --git a/src/business/business-gnome/dialog-customer.c b/src/business/business-gnome/dialog-customer.c index b9c383a9d9..b6a628ec8b 100644 --- a/src/business/business-gnome/dialog-customer.c +++ b/src/business/business-gnome/dialog-customer.c @@ -305,8 +305,7 @@ gnc_customer_window_ok_cb (GtkWidget *widget, gpointer data) /* Set the customer id if one has not been chosen */ if (safe_strcmp (gtk_entry_get_text (GTK_ENTRY (cw->id_entry)), "") == 0) { - string = g_strdup_printf ("%.6" G_GINT64_FORMAT, - gncCustomerNextID (cw->book)); + string = gncCustomerNextID (cw->book); gtk_entry_set_text (GTK_ENTRY (cw->id_entry), string); g_free(string); } diff --git a/src/business/business-gnome/dialog-employee.c b/src/business/business-gnome/dialog-employee.c index 4608b9cb80..5fb726f58d 100644 --- a/src/business/business-gnome/dialog-employee.c +++ b/src/business/business-gnome/dialog-employee.c @@ -241,8 +241,7 @@ gnc_employee_window_ok_cb (GtkWidget *widget, gpointer data) /* Set the employee id if one has not been chosen */ if (safe_strcmp (gtk_entry_get_text (GTK_ENTRY (ew->id_entry)), "") == 0) { - string = g_strdup_printf ("%.6" G_GINT64_FORMAT, - gncEmployeeNextID (ew->book)); + string = gncEmployeeNextID (ew->book); gtk_entry_set_text (GTK_ENTRY (ew->id_entry), string); g_free(string); } diff --git a/src/business/business-gnome/dialog-invoice.c b/src/business/business-gnome/dialog-invoice.c index e99f4b4c23..38aa937ba8 100644 --- a/src/business/business-gnome/dialog-invoice.c +++ b/src/business/business-gnome/dialog-invoice.c @@ -364,8 +364,7 @@ gnc_invoice_window_verify_ok (InvoiceWindow *iw) Therefore we pass the GncOwer to gncInvoiceNextID so it knows whether we are creating a bill or an invoice. */ - string = g_strdup_printf ("%.6" G_GINT64_FORMAT, - gncInvoiceNextID(iw->book, &(iw->owner))); + string = gncInvoiceNextID(iw->book, &(iw->owner)); gtk_entry_set_text (GTK_ENTRY (iw->id_entry), string); g_free(string); } diff --git a/src/business/business-gnome/dialog-job.c b/src/business/business-gnome/dialog-job.c index 3953bf14ff..c7db22ebb7 100644 --- a/src/business/business-gnome/dialog-job.c +++ b/src/business/business-gnome/dialog-job.c @@ -149,8 +149,7 @@ gnc_job_verify_ok (JobWindow *jw) res = gtk_entry_get_text (GTK_ENTRY (jw->id_entry)); if (safe_strcmp (res, "") == 0) { - string = g_strdup_printf ("%.6" G_GINT64_FORMAT, - gncJobNextID(jw->book)); + string = gncJobNextID(jw->book); gtk_entry_set_text (GTK_ENTRY (jw->id_entry), string); g_free(string); } diff --git a/src/business/business-gnome/dialog-order.c b/src/business/business-gnome/dialog-order.c index 04eba801b0..24b989a501 100644 --- a/src/business/business-gnome/dialog-order.c +++ b/src/business/business-gnome/dialog-order.c @@ -705,8 +705,7 @@ gnc_order_window_new_order (QofBook *bookp, GncOwner *owner) ow); /* Setup initial values */ ow->order_guid = *gncOrderGetGUID (order); - string = g_strdup_printf ("%.6" G_GINT64_FORMAT, - gncOrderNextID(bookp)); + string = gncOrderNextID(bookp); gtk_entry_set_text (GTK_ENTRY (ow->id_entry), string); g_free(string); diff --git a/src/business/business-gnome/dialog-vendor.c b/src/business/business-gnome/dialog-vendor.c index 9fc019daa9..749c5524b4 100644 --- a/src/business/business-gnome/dialog-vendor.c +++ b/src/business/business-gnome/dialog-vendor.c @@ -225,8 +225,7 @@ gnc_vendor_window_ok_cb (GtkWidget *widget, gpointer data) /* Check for valid id and set one if necessary */ if (safe_strcmp (gtk_entry_get_text (GTK_ENTRY (vw->id_entry)), "") == 0) { - string = g_strdup_printf ("%.6" G_GINT64_FORMAT, - gncVendorNextID(vw->book)); + string = gncVendorNextID(vw->book); gtk_entry_set_text (GTK_ENTRY (vw->id_entry), string); g_free(string); } diff --git a/src/engine/gncCustomer.c b/src/engine/gncCustomer.c index 01ab6ea7a6..280cd9ced3 100644 --- a/src/engine/gncCustomer.c +++ b/src/engine/gncCustomer.c @@ -949,7 +949,7 @@ gboolean gncCustomerRegister (void) return qof_object_register (&gncCustomerDesc); } -gint64 gncCustomerNextID (QofBook *book) +gchar *gncCustomerNextID (QofBook *book) { - return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME); + return qof_book_increment_and_format_counter (book, _GNC_MOD_NAME); } diff --git a/src/engine/gncCustomerP.h b/src/engine/gncCustomerP.h index 80e2055988..463fc1ffd6 100644 --- a/src/engine/gncCustomerP.h +++ b/src/engine/gncCustomerP.h @@ -32,7 +32,7 @@ #include "gncCustomer.h" gboolean gncCustomerRegister (void); -gint64 gncCustomerNextID (QofBook *book); +gchar *gncCustomerNextID (QofBook *book); /** The gncCloneCustomer() routine makes a copy of the indicated * customer, placing it in the indicated book. It copies diff --git a/src/engine/gncEmployee.c b/src/engine/gncEmployee.c index 0fce09cefb..978f14fda0 100644 --- a/src/engine/gncEmployee.c +++ b/src/engine/gncEmployee.c @@ -740,7 +740,7 @@ gboolean gncEmployeeRegister (void) return qof_object_register (&gncEmployeeDesc); } -gint64 gncEmployeeNextID (QofBook *book) +gchar *gncEmployeeNextID (QofBook *book) { - return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME); + return qof_book_increment_and_format_counter (book, _GNC_MOD_NAME); } diff --git a/src/engine/gncEmployeeP.h b/src/engine/gncEmployeeP.h index 07d1ef9d79..b5ca3d5475 100644 --- a/src/engine/gncEmployeeP.h +++ b/src/engine/gncEmployeeP.h @@ -32,7 +32,7 @@ #include "gncEmployee.h" gboolean gncEmployeeRegister (void); -gint64 gncEmployeeNextID (QofBook *book); +gchar *gncEmployeeNextID (QofBook *book); /** The gncCloneEmployee() routine makes a copy of the indicated * employee, placing it in the indicated book. It copies diff --git a/src/engine/gncInvoice.c b/src/engine/gncInvoice.c index ba68a1edf5..297cf0ea3d 100644 --- a/src/engine/gncInvoice.c +++ b/src/engine/gncInvoice.c @@ -2125,22 +2125,22 @@ gboolean gncInvoiceRegister (void) return qof_object_register (&gncInvoiceDesc); } -gint64 gncInvoiceNextID (QofBook *book, GncOwner *owner) +gchar *gncInvoiceNextID (QofBook *book, GncOwner *owner) { - gint64 nextID; + gchar *nextID; switch (gncOwnerGetType(gncOwnerGetEndOwner(owner))) { case GNC_OWNER_CUSTOMER: - nextID = qof_book_increment_and_get_counter (book, "gncInvoice"); + nextID = qof_book_increment_and_format_counter (book, "gncInvoice"); break; case GNC_OWNER_VENDOR: - nextID = qof_book_increment_and_get_counter (book, "gncBill"); + nextID = qof_book_increment_and_format_counter (book, "gncBill"); break; case GNC_OWNER_EMPLOYEE: - nextID = qof_book_increment_and_get_counter (book, "gncExpVoucher"); + nextID = qof_book_increment_and_format_counter (book, "gncExpVoucher"); break; default: - nextID = qof_book_increment_and_get_counter (book, _GNC_MOD_NAME); + nextID = qof_book_increment_and_format_counter (book, _GNC_MOD_NAME); break; } return nextID; diff --git a/src/engine/gncInvoiceP.h b/src/engine/gncInvoiceP.h index 53fe5569ad..c16de29122 100644 --- a/src/engine/gncInvoiceP.h +++ b/src/engine/gncInvoiceP.h @@ -35,7 +35,7 @@ #include "gncOwner.h" gboolean gncInvoiceRegister (void); -gint64 gncInvoiceNextID (QofBook *book, GncOwner *owner); +gchar *gncInvoiceNextID (QofBook *book, GncOwner *owner); void gncInvoiceSetPostedAcc (GncInvoice *invoice, Account *acc); void gncInvoiceSetPostedTxn (GncInvoice *invoice, Transaction *txn); void gncInvoiceSetPostedLot (GncInvoice *invoice, GNCLot *lot); diff --git a/src/engine/gncJob.c b/src/engine/gncJob.c index c704d474fb..4d319c7685 100644 --- a/src/engine/gncJob.c +++ b/src/engine/gncJob.c @@ -583,7 +583,7 @@ gboolean gncJobRegister (void) return qof_object_register (&gncJobDesc); } -gint64 gncJobNextID (QofBook *book) +gchar *gncJobNextID (QofBook *book) { - return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME); + return qof_book_increment_and_format_counter (book, _GNC_MOD_NAME); } diff --git a/src/engine/gncJobP.h b/src/engine/gncJobP.h index 2da0a4f5b4..55f5991c83 100644 --- a/src/engine/gncJobP.h +++ b/src/engine/gncJobP.h @@ -32,7 +32,7 @@ #include "gncJob.h" gboolean gncJobRegister (void); -gint64 gncJobNextID (QofBook *book); +gchar *gncJobNextID (QofBook *book); /** The gncCloneTaxTable() routine makes a copy of the indicated * tax table, placing it in the indicated book. It copies diff --git a/src/engine/gncOrder.c b/src/engine/gncOrder.c index 4c34821c4a..9a916ef039 100644 --- a/src/engine/gncOrder.c +++ b/src/engine/gncOrder.c @@ -604,7 +604,7 @@ gboolean gncOrderRegister (void) return qof_object_register (&gncOrderDesc); } -gint64 gncOrderNextID (QofBook *book) +gchar *gncOrderNextID (QofBook *book) { - return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME); + return qof_book_increment_and_format_counter (book, _GNC_MOD_NAME); } diff --git a/src/engine/gncOrderP.h b/src/engine/gncOrderP.h index 7a5dd81bb7..82cd953719 100644 --- a/src/engine/gncOrderP.h +++ b/src/engine/gncOrderP.h @@ -32,7 +32,7 @@ #include "gncOrder.h" gboolean gncOrderRegister (void); -gint64 gncOrderNextID (QofBook *book); +gchar *gncOrderNextID (QofBook *book); /** The gncCloneOrder() routine makes a copy of the indicated * order, placing it in the indicated book. It copies diff --git a/src/engine/gncVendor.c b/src/engine/gncVendor.c index 9cbe36a371..dcab1e96c8 100644 --- a/src/engine/gncVendor.c +++ b/src/engine/gncVendor.c @@ -859,7 +859,7 @@ gboolean gncVendorRegister (void) return qof_object_register (&gncVendorDesc); } -gint64 gncVendorNextID (QofBook *book) +gchar *gncVendorNextID (QofBook *book) { - return qof_book_increment_and_get_counter (book, _GNC_MOD_NAME); + return qof_book_increment_and_format_counter (book, _GNC_MOD_NAME); } diff --git a/src/engine/gncVendorP.h b/src/engine/gncVendorP.h index 3c96324def..d146c6994a 100644 --- a/src/engine/gncVendorP.h +++ b/src/engine/gncVendorP.h @@ -31,7 +31,7 @@ #include "gncVendor.h" gboolean gncVendorRegister (void); -gint64 gncVendorNextID (QofBook *book); +gchar *gncVendorNextID (QofBook *book); /** The gncCloneVendor() routine makes a copy of the indicated * vendor, placing it in the indicated book. It copies diff --git a/src/libqof/qof/qofbook.c b/src/libqof/qof/qofbook.c index c87b873718..5482dca729 100644 --- a/src/libqof/qof/qofbook.c +++ b/src/libqof/qof/qofbook.c @@ -429,8 +429,8 @@ qof_book_get_counter (QofBook *book, const char *counter_name) } } -gint64 -qof_book_increment_and_get_counter (QofBook *book, const char *counter_name) +gchar * +qof_book_increment_and_format_counter (QofBook *book, const char *counter_name) { QofBackend *be; KvpFrame *kvp; @@ -440,13 +440,13 @@ qof_book_increment_and_get_counter (QofBook *book, const char *counter_name) if (!book) { PWARN ("No book!!!"); - return -1; + return NULL; } if (!counter_name || *counter_name == '\0') { PWARN ("Invalid counter name."); - return -1; + return NULL; } /* Get the current counter value from the KVP in the book. */ @@ -454,7 +454,7 @@ qof_book_increment_and_get_counter (QofBook *book, const char *counter_name) /* Check if an error occured */ if (counter < 0) - return -1; + return NULL; /* Increment the counter */ counter++; @@ -465,7 +465,7 @@ qof_book_increment_and_get_counter (QofBook *book, const char *counter_name) if (!kvp) { PWARN ("Book has no KVP_Frame"); - return -1; + return NULL; } /* Save off the new counter */ @@ -476,7 +476,8 @@ qof_book_increment_and_get_counter (QofBook *book, const char *counter_name) qof_book_mark_dirty(book); qof_book_commit_edit(book); - return counter; + /* Generate a string version of the counter */ + return g_strdup_printf ("%.6" G_GINT64_FORMAT, counter); } /* Determine whether this book uses trading accounts */ diff --git a/src/libqof/qof/qofbook.h b/src/libqof/qof/qofbook.h index d74686cb83..662d214be9 100644 --- a/src/libqof/qof/qofbook.h +++ b/src/libqof/qof/qofbook.h @@ -283,11 +283,11 @@ gboolean qof_book_equal (const QofBook *book_1, const QofBook *book_2); */ gint64 qof_book_get_counter (QofBook *book, const char *counter_name); -/** This will increment the named counter for this book and return it. - * The return value is -1 on error or the (new) value of the - * counter. +/** This will increment the named counter for this book and format it. + * The return value is NULL on error or the formatted (new) value of + * the counter. The caller should free the result with g_gree. */ -gint64 qof_book_increment_and_get_counter (QofBook *book, const char *counter_name); +gchar *qof_book_increment_and_format_counter (QofBook *book, const char *counter_name); const char* qof_book_get_string_option(const QofBook* book, const char* opt_name); void qof_book_set_string_option(QofBook* book, const char* opt_name, const char* opt_val);