diff --git a/src/engine/kvp_doc.txt b/src/engine/kvp_doc.txt index 3ce0bff876..f39f53f0b2 100644 --- a/src/engine/kvp_doc.txt +++ b/src/engine/kvp_doc.txt @@ -175,6 +175,20 @@ Use: Holders for a bunch of counters for various types. Used specifically that follows /counters/, e.g. "/counters/GncCustomer" \endverbatim +\verbatim +Name: /counter_formats/... +Type: string +Entities: Book +Use: Holders for a bunch of counter formats for various types. Used + specifically in the business objects, to format a counter value + into a (string) ID. The counter name is the path that follows + /counter_formats/, e.g. "/counter_formats/GncCustomer" + + These formats are printf-style format strings that contain exactly + one format specifier for an int64 (optionally preceded or followed + by arbitrary other strings). +\endverbatim + \subsection kvpD D \subsection kvpE E diff --git a/src/libqof/qof/qofbook.c b/src/libqof/qof/qofbook.c index 5482dca729..8222e00727 100644 --- a/src/libqof/qof/qofbook.c +++ b/src/libqof/qof/qofbook.c @@ -436,6 +436,7 @@ qof_book_increment_and_format_counter (QofBook *book, const char *counter_name) KvpFrame *kvp; KvpValue *value; gint64 counter; + gchar* format; if (!book) { @@ -476,8 +477,58 @@ qof_book_increment_and_format_counter (QofBook *book, const char *counter_name) qof_book_mark_dirty(book); qof_book_commit_edit(book); + format = qof_book_get_counter_format(book, counter_name); + + if (!format) + { + PWARN("Cannot get format for counter"); + return NULL; + } + /* Generate a string version of the counter */ - return g_strdup_printf ("%.6" G_GINT64_FORMAT, counter); + return g_strdup_printf(format, counter); +} + +gchar * +qof_book_get_counter_format(const QofBook *book, const char *counter_name) +{ + KvpFrame *kvp; + gchar *format; + KvpValue *value; + + if (!book) + { + PWARN ("No book!!!"); + return NULL; + } + + if (!counter_name || *counter_name == '\0') + { + PWARN ("Invalid counter name."); + return NULL; + } + + /* Get the KVP from the current book */ + kvp = qof_book_get_slots (book); + + if (!kvp) + { + PWARN ("Book has no KVP_Frame"); + return NULL; + } + + /* Get the format string */ + value = kvp_frame_get_slot_path (kvp, "counter_formats", counter_name, NULL); + if (value) + { + format = kvp_value_get_string (value); + } + else + { + /* Use the default format */ + format = "%.6" G_GINT64_FORMAT; + } + return format; } /* Determine whether this book uses trading accounts */ diff --git a/src/libqof/qof/qofbook.h b/src/libqof/qof/qofbook.h index 662d214be9..33a034ff33 100644 --- a/src/libqof/qof/qofbook.h +++ b/src/libqof/qof/qofbook.h @@ -289,6 +289,12 @@ gint64 qof_book_get_counter (QofBook *book, const char *counter_name); */ gchar *qof_book_increment_and_format_counter (QofBook *book, const char *counter_name); +/** Get the format string to use for the named counter. + * The return value is NULL on error or the format string of the + * counter. The string should not be freed. + */ +gchar *qof_book_get_counter_format (const 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);