Simplify QofSession to own only exactly one QofBook.

No more, no less. This object isn't used in any other way in gnucash, so
we better get rid of the extra potential complexity here.

git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@21548 57a11ea4-9604-0410-9ed3-97b8803252fd
This commit is contained in:
Christian Stimming 2011-11-09 21:45:12 +00:00
parent 66ddf0c74f
commit e782c612f6
2 changed files with 39 additions and 73 deletions

View File

@ -41,8 +41,8 @@ struct _QofSession
QofInstance entity; QofInstance entity;
/* A book holds pointers to the various types of datasets. /* A book holds pointers to the various types of datasets.
* A session may have multiple books. */ * A session has exactly one book. */
GList *books; QofBook *book;
/* The requested book id, in the form or a URI, such as /* The requested book id, in the form or a URI, such as
* file:/some/where, or sql:server.host.com:555 * file:/some/where, or sql:server.host.com:555

View File

@ -214,7 +214,7 @@ qof_session_init (QofSession *session)
if (!session) return; if (!session) return;
session->entity.e_type = QOF_ID_SESSION; session->entity.e_type = QOF_ID_SESSION;
session->books = g_list_append (NULL, qof_book_new ()); session->book = qof_book_new ();
session->book_id = NULL; session->book_id = NULL;
session->backend = NULL; session->backend = NULL;
session->lock = 1; session->lock = 1;
@ -235,13 +235,16 @@ qof_session_get_book (const QofSession *session)
{ {
GList *node; GList *node;
if (!session) return NULL; if (!session) return NULL;
if (!session->book) return NULL;
for (node = session->books; node; node = node->next) if ('y' == session->book->book_open)
{ {
QofBook *book = node->data; return session->book;
if ('y' == book->book_open) return book;
} }
else
{
return NULL; return NULL;
}
} }
QofBackend * QofBackend *
@ -295,14 +298,10 @@ static void
qof_session_load_backend(QofSession * session, const char * access_method) qof_session_load_backend(QofSession * session, const char * access_method)
{ {
GSList *p; GSList *p;
GList *node;
QofBackendProvider *prov; QofBackendProvider *prov;
QofBook *book;
char *msg; char *msg;
gint num;
gboolean prov_type; gboolean prov_type;
gboolean (*type_check) (const char*); gboolean (*type_check) (const char*);
gchar *libdir_from_env = NULL;
ENTER (" list=%d, initted=%s", g_slist_length(provider_list), ENTER (" list=%d, initted=%s", g_slist_length(provider_list),
qof_providers_initialized ? "true" : "false"); qof_providers_initialized ? "true" : "false");
@ -340,12 +339,8 @@ qof_session_load_backend(QofSession * session, const char * access_method)
/* Use the providers creation callback */ /* Use the providers creation callback */
session->backend = (*(prov->backend_new))(); session->backend = (*(prov->backend_new))();
session->backend->provider = prov; session->backend->provider = prov;
/* Tell the books about the backend that they'll be using. */ /* Tell the book about the backend that they'll be using. */
for (node = session->books; node; node = node->next) qof_book_set_backend (session->book, session->backend);
{
book = node->data;
qof_book_set_backend (book, session->backend);
}
LEAVE (" "); LEAVE (" ");
return; return;
} }
@ -494,8 +489,7 @@ void
qof_session_load (QofSession *session, qof_session_load (QofSession *session,
QofPercentageFunc percentage_func) QofPercentageFunc percentage_func)
{ {
QofBook *newbook, *ob; QofBook *newbook, *oldbook;
QofBookList *oldbooks, *node;
QofBackend *be; QofBackend *be;
QofBackendError err; QofBackendError err;
@ -508,14 +502,14 @@ qof_session_load (QofSession *session,
/* At this point, we should are supposed to have a valid book /* At this point, we should are supposed to have a valid book
* id and a lock on the file. */ * id and a lock on the file. */
oldbooks = session->books; oldbook = session->book;
/* XXX why are we creating a book here? I think the books /* XXX why are we creating a book here? I think the books
* need to be handled by the backend ... especially since * need to be handled by the backend ... especially since
* the backend may need to load multiple books ... XXX. FIXME. * the backend may need to load multiple books ... XXX. FIXME.
*/ */
newbook = qof_book_new(); newbook = qof_book_new();
session->books = g_list_append (NULL, newbook); session->book = newbook;
PINFO ("new book=%p", newbook); PINFO ("new book=%p", newbook);
qof_session_clear_error (session); qof_session_clear_error (session);
@ -560,18 +554,12 @@ qof_session_load (QofSession *session,
/* Something broke, put back the old stuff */ /* Something broke, put back the old stuff */
qof_book_set_backend (newbook, NULL); qof_book_set_backend (newbook, NULL);
qof_book_destroy (newbook); qof_book_destroy (newbook);
g_list_free (session->books); session->book = oldbook;
session->books = oldbooks;
LEAVE("error from backend %d", qof_session_get_error(session)); LEAVE("error from backend %d", qof_session_get_error(session));
return; return;
} }
for (node = oldbooks; node; node = node->next) qof_book_set_backend (oldbook, NULL);
{ qof_book_destroy (oldbook);
ob = node->data;
qof_book_set_backend (ob, NULL);
qof_book_destroy (ob);
}
g_list_free (oldbooks);
LEAVE ("sess = %p, book_id=%s", session, session->book_id LEAVE ("sess = %p, book_id=%s", session, session->book_id
? session->book_id : "(null)"); ? session->book_id : "(null)");
@ -597,12 +585,11 @@ void
qof_session_save (QofSession *session, qof_session_save (QofSession *session,
QofPercentageFunc percentage_func) QofPercentageFunc percentage_func)
{ {
GList *node;
QofBackend *be; QofBackend *be;
gboolean partial, change_backend; gboolean partial, change_backend;
QofBackendProvider *prov; QofBackendProvider *prov;
GSList *p; GSList *p;
QofBook *book, *abook; QofBook *book;
int err; int err;
gint num; gint num;
char *msg = NULL; char *msg = NULL;
@ -688,12 +675,8 @@ qof_session_save (QofSession *session,
msg = NULL; msg = NULL;
} }
} }
/* Tell the books about the backend that they'll be using. */ /* Tell the book about the backend that they'll be using. */
for (node = session->books; node; node = node->next) qof_book_set_backend (session->book, session->backend);
{
book = node->data;
qof_book_set_backend (book, session->backend);
}
p = NULL; p = NULL;
} }
if (p) if (p)
@ -723,19 +706,16 @@ qof_session_save (QofSession *session,
be = session->backend; be = session->backend;
if (be) if (be)
{ {
for (node = session->books; node; node = node->next)
{
abook = node->data;
/* if invoked as SaveAs(), then backend not yet set */ /* if invoked as SaveAs(), then backend not yet set */
qof_book_set_backend (abook, be); qof_book_set_backend (session->book, be);
be->percentage = percentage_func; be->percentage = percentage_func;
if (be->sync) if (be->sync)
{ {
(be->sync)(be, abook); (be->sync)(be, session->book);
if (save_error_handler(be, session)) if (save_error_handler(be, session))
goto leave; goto leave;
} }
}
/* If we got to here, then the backend saved everything /* If we got to here, then the backend saved everything
* just fine, and we are done. So return. */ * just fine, and we are done. So return. */
/* Return the book_id to previous value. */ /* Return the book_id to previous value. */
@ -812,7 +792,6 @@ qof_session_end (QofSession *session)
void void
qof_session_destroy (QofSession *session) qof_session_destroy (QofSession *session)
{ {
GList *node;
if (!session) return; if (!session) return;
ENTER ("sess=%p book_id=%s", session, session->book_id ENTER ("sess=%p book_id=%s", session, session->book_id
@ -823,14 +802,9 @@ qof_session_destroy (QofSession *session)
/* destroy the backend */ /* destroy the backend */
qof_session_destroy_backend(session); qof_session_destroy_backend(session);
for (node = session->books; node; node = node->next) qof_book_set_backend (session->book, NULL);
{ qof_book_destroy (session->book);
QofBook *book = node->data; session->book = NULL;
qof_book_set_backend (book, NULL);
qof_book_destroy (book);
}
session->books = NULL;
g_free (session); g_free (session);
@ -843,29 +817,21 @@ qof_session_destroy (QofSession *session)
void void
qof_session_swap_data (QofSession *session_1, QofSession *session_2) qof_session_swap_data (QofSession *session_1, QofSession *session_2)
{ {
GList *books_1, *books_2, *node; QofBook *book_1, *book_2;
if (session_1 == session_2) return; if (session_1 == session_2) return;
if (!session_1 || !session_2) return; if (!session_1 || !session_2) return;
ENTER ("sess1=%p sess2=%p", session_1, session_2); ENTER ("sess1=%p sess2=%p", session_1, session_2);
books_1 = session_1->books; book_1 = session_1->book;
books_2 = session_2->books; book_2 = session_2->book;
session_1->books = books_2; session_1->book = book_2;
session_2->books = books_1; session_2->book = book_1;
for (node = books_1; node; node = node->next)
{
QofBook *book_1 = node->data;
qof_book_set_backend (book_1, session_2->backend); qof_book_set_backend (book_1, session_2->backend);
}
for (node = books_2; node; node = node->next)
{
QofBook *book_2 = node->data;
qof_book_set_backend (book_2, session_1->backend); qof_book_set_backend (book_2, session_1->backend);
}
LEAVE (" "); LEAVE (" ");
} }