qofobject.c

00001 /********************************************************************\
00002  * qofobject.c -- the Core Object Registration/Lookup Interface     *
00003  * This program is free software; you can redistribute it and/or    *
00004  * modify it under the terms of the GNU General Public License as   *
00005  * published by the Free Software Foundation; either version 2 of   *
00006  * the License, or (at your option) any later version.              *
00007  *                                                                  *
00008  * This program is distributed in the hope that it will be useful,  *
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00011  * GNU General Public License for more details.                     *
00012  *                                                                  *
00013  * You should have received a copy of the GNU General Public License*
00014  * along with this program; if not, contact:                        *
00015  *                                                                  *
00016  * Free Software Foundation           Voice:  +1-617-542-5942       *
00017  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00018  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
00019  *                                                                  *
00020 \********************************************************************/
00021 /*
00022  * qofobject.c -- the Core Object Object Registry
00023  * Copyright (C) 2001 Derek Atkins
00024  * Author: Derek Atkins <warlord@MIT.EDU>
00025  */
00026 
00027 #include "config.h"
00028 
00029 #include <glib.h>
00030 
00031 #include "qof.h"
00032 #include "qofobject-p.h"
00033 
00034 static QofLogModule log_module = QOF_MOD_OBJECT;
00035 
00036 static gboolean object_is_initialized = FALSE;
00037 static GList *object_modules = NULL;
00038 static GList *book_list = NULL;
00039 static GHashTable *backend_data = NULL;
00040 
00041 gpointer
00042 qof_object_new_instance (QofIdTypeConst type_name, QofBook * book)
00043 {
00044     const QofObject *obj;
00045 
00046     if (!type_name)
00047         return NULL;
00048 
00049     obj = qof_object_lookup (type_name);
00050     if (!obj)
00051         return NULL;
00052 
00053     if (obj->create)
00054         return (obj->create (book));
00055 
00056     return NULL;
00057 }
00058 
00059 void
00060 qof_object_book_begin (QofBook * book)
00061 {
00062     GList *l;
00063 
00064     if (!book)
00065         return;
00066     ENTER (" ");
00067     for (l = object_modules; l; l = l->next)
00068     {
00069         QofObject *obj = l->data;
00070         if (obj->book_begin)
00071             obj->book_begin (book);
00072     }
00073 
00074     /* Remember this book for later */
00075     book_list = g_list_prepend (book_list, book);
00076     LEAVE (" ");
00077 }
00078 
00079 void
00080 qof_object_book_end (QofBook * book)
00081 {
00082     GList *l;
00083 
00084     if (!book)
00085         return;
00086     ENTER (" ");
00087     for (l = object_modules; l; l = l->next)
00088     {
00089         QofObject *obj = l->data;
00090         if (obj->book_end)
00091             obj->book_end (book);
00092     }
00093 
00094     /* Remove it from the list */
00095     book_list = g_list_remove (book_list, book);
00096     LEAVE (" ");
00097 }
00098 
00099 gboolean
00100 qof_object_is_dirty (QofBook * book)
00101 {
00102     GList *l;
00103 
00104     if (!book)
00105         return FALSE;
00106     for (l = object_modules; l; l = l->next)
00107     {
00108         QofObject *obj = l->data;
00109         if (obj->is_dirty)
00110         {
00111             QofCollection *col;
00112             col = qof_book_get_collection (book, obj->e_type);
00113             if (obj->is_dirty (col))
00114                 return TRUE;
00115         }
00116     }
00117     return FALSE;
00118 }
00119 
00120 void
00121 qof_object_mark_clean (QofBook * book)
00122 {
00123     GList *l;
00124 
00125     if (!book)
00126         return;
00127     for (l = object_modules; l; l = l->next)
00128     {
00129         QofObject *obj = l->data;
00130         if (obj->mark_clean)
00131         {
00132             QofCollection *col;
00133             col = qof_book_get_collection (book, obj->e_type);
00134             (obj->mark_clean) (col);
00135         }
00136     }
00137 }
00138 
00139 void
00140 qof_object_foreach_type (QofForeachTypeCB cb, gpointer user_data)
00141 {
00142     GList *l;
00143 
00144     if (!cb)
00145         return;
00146 
00147     for (l = object_modules; l; l = l->next)
00148     {
00149         QofObject *obj = l->data;
00150         (cb) (obj, user_data);
00151     }
00152 }
00153 
00154 gboolean
00155 qof_object_compliance (QofIdTypeConst type_name, gboolean warn)
00156 {
00157     const QofObject *obj;
00158 
00159     obj = qof_object_lookup (type_name);
00160     if ((obj->create == NULL) || (obj->foreach == NULL))
00161     {
00162         if (warn)
00163         {
00164             PINFO (" Object type %s is not fully QOF compliant",
00165                 obj->e_type);
00166         }
00167         return FALSE;
00168     }
00169     return TRUE;
00170 }
00171 
00172 
00173 void
00174 qof_object_foreach (QofIdTypeConst type_name, QofBook * book,
00175     QofEntityForeachCB cb, gpointer user_data)
00176 {
00177     QofCollection *col;
00178     const QofObject *obj;
00179 
00180     if (!book || !type_name)
00181     {
00182         return;
00183     }
00184     PINFO ("type=%s", type_name);
00185 
00186     obj = qof_object_lookup (type_name);
00187     if (!obj)
00188     {
00189         PERR ("No object of type %s", type_name);
00190         return;
00191     }
00192     col = qof_book_get_collection (book, obj->e_type);
00193     if (!obj)
00194     {
00195         return;
00196     }
00197     if (obj->foreach)
00198     {
00199         obj->foreach (col, cb, user_data);
00200     }
00201     return;
00202 }
00203 
00204 const gchar *
00205 qof_object_printable (QofIdTypeConst type_name, gpointer obj)
00206 {
00207     const QofObject *b_obj;
00208 
00209     if (!type_name || !obj)
00210         return NULL;
00211 
00212     b_obj = qof_object_lookup (type_name);
00213     if (!b_obj)
00214         return NULL;
00215 
00216     if (b_obj->printable)
00217         return (b_obj->printable (obj));
00218 
00219     return NULL;
00220 }
00221 
00222 const gchar *
00223 qof_object_get_type_label (QofIdTypeConst type_name)
00224 {
00225     const QofObject *obj;
00226 
00227     if (!type_name)
00228         return NULL;
00229 
00230     obj = qof_object_lookup (type_name);
00231     if (!obj)
00232         return NULL;
00233 
00234     return (obj->type_label);
00235 }
00236 
00237 static gboolean
00238 clear_table (gpointer key, gpointer value, gpointer user_data)
00239 {
00240     g_hash_table_destroy (value);
00241     return TRUE;
00242 }
00243 
00244 /* INITIALIZATION and PRIVATE FUNCTIONS */
00245 
00246 void
00247 qof_object_initialize (void)
00248 {
00249     if (object_is_initialized)
00250         return;
00251     backend_data = g_hash_table_new (g_str_hash, g_str_equal);
00252     object_is_initialized = TRUE;
00253 }
00254 
00255 void
00256 qof_object_shutdown (void)
00257 {
00258     g_return_if_fail (object_is_initialized == TRUE);
00259 
00260     g_hash_table_foreach_remove (backend_data, clear_table, NULL);
00261     g_hash_table_destroy (backend_data);
00262     backend_data = NULL;
00263 
00264     g_list_free (object_modules);
00265     object_modules = NULL;
00266     g_list_free (book_list);
00267     book_list = NULL;
00268     object_is_initialized = FALSE;
00269 }
00270 
00271 /* Register new types of object objects.
00272  * Return TRUE if successful,
00273  * return FALSE if it fails, invalid arguments, or if the object
00274  * already exists
00275  */
00276 gboolean
00277 qof_object_register (const QofObject * object)
00278 {
00279     g_return_val_if_fail (object_is_initialized, FALSE);
00280 
00281     if (!object)
00282         return FALSE;
00283     g_return_val_if_fail (object->interface_version == QOF_OBJECT_VERSION,
00284         FALSE);
00285 
00286     if (g_list_index (object_modules, (gpointer) object) == -1)
00287         object_modules =
00288             g_list_prepend (object_modules, (gpointer) object);
00289     else
00290         return FALSE;
00291 
00292     /* Now initialize all the known books */
00293     if (object->book_begin && book_list)
00294     {
00295         GList *node;
00296         for (node = book_list; node; node = node->next)
00297             object->book_begin (node->data);
00298     }
00299 
00300     return TRUE;
00301 }
00302 
00303 const QofObject *
00304 qof_object_lookup (QofIdTypeConst name)
00305 {
00306     GList *qiter;
00307     const QofObject *obj;
00308 
00309     g_return_val_if_fail (object_is_initialized, NULL);
00310 
00311     if (!name)
00312         return NULL;
00313 
00314     for (qiter = object_modules; qiter; qiter = qiter->next)
00315     {
00316         obj = qiter->data;
00317         if (!safe_strcmp (obj->e_type, name))
00318             return obj;
00319     }
00320     return NULL;
00321 }
00322 
00323 gboolean
00324 qof_object_register_backend (QofIdTypeConst type_name,
00325     const gchar * backend_name, gpointer be_data)
00326 {
00327     GHashTable *ht;
00328     g_return_val_if_fail (object_is_initialized, FALSE);
00329 
00330     if (!type_name || *type_name == '\0' ||
00331         !backend_name || *backend_name == '\0' || !be_data)
00332         return FALSE;
00333 
00334     ht = g_hash_table_lookup (backend_data, backend_name);
00335 
00336     /* If it doesn't already exist, create a new table for this backend */
00337     if (!ht)
00338     {
00339         ht = g_hash_table_new (g_str_hash, g_str_equal);
00340         g_hash_table_insert (backend_data, (gchar *) backend_name, ht);
00341     }
00342 
00343     /* Now insert the data */
00344     g_hash_table_insert (ht, (gchar *) type_name, be_data);
00345 
00346     return TRUE;
00347 }
00348 
00349 gpointer
00350 qof_object_lookup_backend (QofIdTypeConst type_name,
00351     const gchar * backend_name)
00352 {
00353     GHashTable *ht;
00354 
00355     if (!type_name || *type_name == '\0' ||
00356         !backend_name || *backend_name == '\0')
00357         return NULL;
00358 
00359     ht = g_hash_table_lookup (backend_data, (gchar *) backend_name);
00360     if (!ht)
00361         return NULL;
00362 
00363     return g_hash_table_lookup (ht, (gchar *) type_name);
00364 }
00365 
00366 struct foreach_data
00367 {
00368     QofForeachBackendTypeCB cb;
00369     gpointer user_data;
00370 };
00371 
00372 static void
00373 foreach_backend (gpointer key, gpointer be_item, gpointer arg)
00374 {
00375     gchar *data_type = key;
00376     struct foreach_data *cb_data = arg;
00377 
00378     g_return_if_fail (key && be_item && arg);
00379 
00380     /* Call the callback for this data type */
00381     (cb_data->cb) (data_type, be_item, cb_data->user_data);
00382 }
00383 
00384 void
00385 qof_object_foreach_backend (const gchar * backend_name,
00386     QofForeachBackendTypeCB cb, gpointer user_data)
00387 {
00388     GHashTable *ht;
00389     struct foreach_data cb_data;
00390 
00391     if (!backend_name || *backend_name == '\0' || !cb)
00392         return;
00393 
00394     ht = g_hash_table_lookup (backend_data, (gchar *) backend_name);
00395     if (!ht)
00396         return;
00397 
00398     cb_data.cb = cb;
00399     cb_data.user_data = user_data;
00400 
00401     g_hash_table_foreach (ht, foreach_backend, &cb_data);
00402 }
00403 
00404 /* ========================= END OF FILE =================== */

Generated on Fri Sep 1 15:35:56 2006 for QOF by  doxygen 1.4.6