src/midgard_reflection_property.c

00001 /* 
00002  * Copyright (C) 2006 Piotr Pokora <piotr.pokora@infoglob.pl>
00003  *
00004  * This program is free software; you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License as published
00006  * by the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  * 
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  * 
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017  */
00018 
00019 #include "midgard/midgard_reflection_property.h"
00020 #include "schema.h"
00021 
00022 struct _MidgardReflectionProperty{
00023                 GObject parent;
00024 
00025                 MidgardObjectClass *klass;
00026 };
00027 
00028 static void _midgard_reflection_property_finalize(GObject *object)
00029 {
00030         g_assert(object != NULL);
00031         return;
00032 }
00033 
00034 /* Initialize class */
00035 static void _midgard_reflection_property_class_init(
00036                 gpointer g_class, gpointer g_class_data)
00037 {
00038         GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
00039         MidgardReflectionPropertyClass *klass = MIDGARD_REFLECTION_PROPERTY_CLASS (g_class);
00040         
00041         gobject_class->set_property = NULL;
00042         gobject_class->get_property = NULL;
00043         gobject_class->finalize = _midgard_reflection_property_finalize;
00044 
00045         klass->is_multilang = midgard_reflection_property_is_multilang; 
00046 }
00047 
00048 /* Register MidgardObjectProperty type */
00049 GType midgard_reflection_property_get_type (void)
00050 {
00051         static GType type = 0;
00052         if (type == 0) {
00053                 static const GTypeInfo info = {
00054                         sizeof (MidgardReflectionPropertyClass),
00055                         NULL,           /* base_init */
00056                         NULL,           /* base_finalize */
00057                         (GClassInitFunc) _midgard_reflection_property_class_init,
00058                         NULL,           /* class_finalize */
00059                         NULL,           /* class_data */
00060                         sizeof (MidgardReflectionProperty),
00061                         0,              /* n_preallocs */
00062                         NULL            /* instance_init */
00063                 };
00064                 type = g_type_register_static (G_TYPE_OBJECT,
00065                                 "midgard_reflection_property",
00066                                 &info, 0);
00067         }
00068         return type;
00069 }
00070 
00071 /* Object constructor */
00072 MidgardReflectionProperty *midgard_reflection_property_new(
00073                 MidgardObjectClass *klass)
00074 {
00075         g_assert(klass != NULL);
00076         MidgardReflectionProperty *self = 
00077                 g_object_new(MIDGARD_TYPE_REFLECTION_PROPERTY, NULL);
00078         self->klass = klass;
00079         return self;
00080 }
00081 
00082 /* Get Midgard Type of the property. */
00083 GType midgard_reflection_property_get_midgard_type(
00084                 MidgardReflectionProperty *object, const gchar *property)
00085 {
00086         g_assert(object != NULL);
00087         g_assert(property != NULL);
00088         MgdSchemaPropertyAttr *prop_attr;
00089         if((prop_attr = g_hash_table_lookup(object->klass->data->prophash,
00090                                         property)) == NULL)
00091                 return MGD_TYPE_NONE;
00092 
00093         return prop_attr->gtype;
00094 }
00095 
00096 /* Checks if property is a link to another type. */
00097 gboolean midgard_reflection_property_is_link(
00098                 MidgardReflectionProperty *object, const gchar *property)
00099 {
00100         g_assert(object != NULL);
00101         g_assert(property != NULL);
00102         MgdSchemaPropertyAttr *prop_attr;
00103         if((prop_attr = g_hash_table_lookup(object->klass->data->prophash,
00104                                         property)) == NULL)
00105                 return FALSE;
00106         
00107         return prop_attr->is_link;
00108 }
00109 
00110 /* Checks if property is linked with another type. */
00111 gboolean midgard_reflection_property_is_linked(
00112                 MidgardReflectionProperty *object, const gchar *property)
00113 {
00114         g_assert(object != NULL);
00115         g_assert(property != NULL);
00116         MgdSchemaPropertyAttr *prop_attr;
00117         if((prop_attr = g_hash_table_lookup(object->klass->data->prophash,
00118                                         property)) == NULL)
00119                 return FALSE;
00120 
00121         return prop_attr->is_linked;
00122 }
00123 
00124 /* Gets the class name of type which property is linked to. */
00125 const gchar *midgard_reflection_property_get_link_name(
00126                 MidgardReflectionProperty *object, const gchar *property)
00127 {
00128         g_assert(object != NULL);
00129         g_assert(property != NULL);     
00130         MgdSchemaPropertyAttr *prop_attr;
00131         if((prop_attr = g_hash_table_lookup(object->klass->data->prophash,
00132                                         property)) == NULL)
00133                 return NULL;
00134         
00135         return prop_attr->link;                                 
00136 }
00137 
00138 /* Gets the name of target property. */
00139 const gchar  *midgard_reflection_property_get_link_target(
00140                 MidgardReflectionProperty *object, const gchar *property)
00141 {
00142         g_assert(object != NULL);
00143         g_assert(property != NULL);
00144         MgdSchemaPropertyAttr *prop_attr;
00145         if((prop_attr = g_hash_table_lookup(object->klass->data->prophash,
00146                                         property)) == NULL)
00147                 return NULL;
00148 
00149         return prop_attr->link_target;  
00150 }
00151 
00152 
00153 /* Gets description of the property. */
00154 const gchar *midgard_reflection_property_description(
00155                 MidgardReflectionProperty *object, const gchar *property)
00156 {
00157         g_assert(object != NULL);
00158         g_assert(property != NULL);
00159         
00160         GParamSpec *prop = g_object_class_find_property(
00161                         G_OBJECT_CLASS(object->klass), property);
00162 
00163         if(!prop)
00164                 return NULL;
00165 
00166         return  g_param_spec_get_blurb(prop);
00167 }
00168 
00169 /* Checks if property is multilingual. */
00170 gboolean midgard_reflection_property_is_multilang(
00171                 MidgardReflectionProperty *object, const gchar *property)
00172 {
00173         g_assert(object != NULL);
00174         g_assert(property != NULL);             
00175         MgdSchemaPropertyAttr *prop_attr;
00176         if((prop_attr = g_hash_table_lookup(object->klass->data->prophash,
00177                                         property)) == NULL)
00178                 return FALSE;
00179         
00180         return prop_attr->is_multilang;
00181 }
00182 
00183 /* Returns the class that the named property is linked to. */
00184 extern MidgardObjectClass *midgard_reflection_property_get_link_class(
00185                 MidgardReflectionProperty *object, const gchar *property)
00186 {
00187         g_assert(object != NULL);
00188         g_assert(property != NULL);
00189 
00190         MgdSchemaPropertyAttr *prop_attr;
00191         if((prop_attr = g_hash_table_lookup(object->klass->data->prophash,
00192                                         property)) == NULL)
00193                 return NULL;
00194         
00195         if(prop_attr->is_link) {
00196                 MidgardObjectClass *link_klass = 
00197                         g_type_class_peek(g_type_from_name(prop_attr->link)); 
00198                 return link_klass;
00199         }
00200         return NULL;
00201 }

Generated on Thu Feb 22 06:15:18 2007 for midgard-core by  doxygen 1.4.6