From fad343b4e2598282d04d4d62791d7bec0ac34e96 Mon Sep 17 00:00:00 2001 From: David Hampton Date: Sat, 6 May 2006 21:09:59 +0000 Subject: [PATCH] Add support for directly marking the book dirty, for recording the time that the book transitioned from clean to dirty, and for calling back a registered function when the book transitions from clean to dirty. git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@13931 57a11ea4-9604-0410-9ed3-97b8803252fd --- ChangeLog | 7 +++++++ lib/libqof/qof/qofbook-p.h | 13 +++++++++++++ lib/libqof/qof/qofbook.c | 39 ++++++++++++++++++++++++++++++++++++-- lib/libqof/qof/qofbook.h | 20 +++++++++++++++++++ lib/libqof/qof/qofutil.c | 4 +++- 5 files changed, 80 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7ec41eea50..7fe9a45851 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2006-05-06 David Hampton + * lib/libqof/qof/qofbook.[ch]: + * lib/libqof/qof/qofbook-p.h: + * lib/libqof/qof/qofutil.c: Add support for directly marking the + book dirty, for recording the time that the book transitioned from + clean to dirty, and for calling back a registered function when + the book transitions from clean to dirty. + * configure.in: Can't use external qof until it supports "alternate dirty mode". diff --git a/lib/libqof/qof/qofbook-p.h b/lib/libqof/qof/qofbook-p.h index 08fa2926da..f2899348c0 100644 --- a/lib/libqof/qof/qofbook-p.h +++ b/lib/libqof/qof/qofbook-p.h @@ -48,6 +48,19 @@ struct _QofBook { QofInstance inst; /* Unique guid for this book. */ + /* The time when the book was first dirtied. This is a secondary + * indicator. It should only be used when inst.dirty is TRUE. */ + time_t dirty_time; + + /* This callback function is called any time the book dirty flag + * changes state. Both clean->dirty and dirty->clean transitions + * trigger a callback. */ + QofBookDirtyCB dirty_cb; + + /* This is the user supplied data that is returned in the dirty + * callback function.*/ + gpointer dirty_data; + /* The entity table associates the GUIDs of all the objects * belonging to this book, with their pointers to the respective * objects. This allows a lookup of objects based on thier guid. diff --git a/lib/libqof/qof/qofbook.c b/lib/libqof/qof/qofbook.c index 10a7698aea..3f4ea3fa31 100644 --- a/lib/libqof/qof/qofbook.c +++ b/lib/libqof/qof/qofbook.c @@ -155,10 +155,33 @@ qof_book_not_saved (QofBook *book) void qof_book_mark_saved (QofBook *book) { + gboolean was_dirty; + if (!book) return; + was_dirty = book->inst.dirty; book->inst.dirty = FALSE; + book->dirty_time = 0; qof_object_mark_clean (book); + if (was_dirty) { + if (book->dirty_cb) + book->dirty_cb(book, FALSE, book->dirty_data); + } +} + +void qof_book_mark_dirty (QofBook *book) +{ + gboolean was_dirty; + + if (!book) return; + + was_dirty = book->inst.dirty; + book->inst.dirty = TRUE; + if (!was_dirty) { + book->dirty_time = time(NULL); + if (book->dirty_cb) + book->dirty_cb(book, TRUE, book->dirty_data); + } } void @@ -169,6 +192,19 @@ qof_book_print_dirty (QofBook *book) qof_book_foreach_collection(book, qof_collection_print_dirty, NULL); } +time_t +qof_book_get_dirty_time (QofBook *book) +{ + return book->dirty_time; +} + +void +qof_book_set_dirty_cb(QofBook *book, QofBookDirtyCB cb, gpointer user_data) +{ + book->dirty_data = user_data; + book->dirty_cb = cb; +} + /* ====================================================================== */ /* getters */ @@ -200,8 +236,7 @@ qof_book_set_backend (QofBook *book, QofBackend *be) void qof_book_kvp_changed (QofBook *book) { - if (!book) return; - book->inst.dirty = TRUE; + qof_book_mark_dirty(book); } /* ====================================================================== */ diff --git a/lib/libqof/qof/qofbook.h b/lib/libqof/qof/qofbook.h index 37c46d15d2..654b4a04d6 100644 --- a/lib/libqof/qof/qofbook.h +++ b/lib/libqof/qof/qofbook.h @@ -66,6 +66,7 @@ typedef struct _QofBook QofBook; typedef GList QofBookList; typedef void (*QofBookFinalCB) (QofBook *, gpointer key, gpointer user_data); +typedef void (*QofBookDirtyCB) (QofBook *, gboolean dirty, gpointer user_data); /** Register the book object with the QOF object system. */ gboolean qof_book_register (void); @@ -154,8 +155,27 @@ gboolean qof_book_not_saved (QofBook *book); * by the frontend when the used has said to abandon any changes. */ void qof_book_mark_saved(QofBook *book); + +/** The qof_book_mark_dirty() routine marks the book as having been + * modified. It can be used by frontend when the used has made a + * change at the book level. + */ +void qof_book_mark_dirty(QofBook *book); + +/** This debugging function can be used to traverse the book structure + * and all subsidiary structures, printing out which structures + * have been marked dirty. + */ void qof_book_print_dirty (QofBook *book); +/** Retrieve the earliest modification time on the book. */ +time_t qof_book_get_dirty_time(QofBook *book); + +/** Set the function to call when a book transitions from clean to + * dirty, or vice versa. + */ +void qof_book_set_dirty_cb(QofBook *book, QofBookDirtyCB cb, gpointer user_data); + /** Call this function when you change the book kvp, to make sure the book * is marked 'dirty'. */ void qof_book_kvp_changed (QofBook *book); diff --git a/lib/libqof/qof/qofutil.c b/lib/libqof/qof/qofutil.c index 818120773a..6e34c219a3 100644 --- a/lib/libqof/qof/qofutil.c +++ b/lib/libqof/qof/qofutil.c @@ -302,8 +302,10 @@ qof_commit_edit_part2(QofInstance *inst, return TRUE; } - if (dirty && qof_get_alt_dirty_mode()) + if (dirty && qof_get_alt_dirty_mode()) { qof_collection_mark_dirty(inst->entity.collection); + qof_book_mark_dirty(inst->book); + } if (on_done) on_done(inst); return TRUE;