src/midgard_object_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/schema.h"
00020 #include "midgard/midgard_object_property.h"
00021 #include "schema.h"
00022 
00023 #define _MGD_GET_PRIVATE_CLASS_DATA \
00024         g_assert(classname != NULL); \
00025         g_assert(property != NULL); \
00026         MidgardObjectClass *oc = \
00027                 (MidgardObjectClass*) g_type_class_peek(g_type_from_name(classname)); \
00028         MgdSchemaTypeAttr *type_attr = oc->data ;\
00029 
00030 /* Initialize class */
00031 static void _midgard_object_property_class_init(
00032                 gpointer g_class, gpointer g_class_data)
00033 {
00034         GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
00035         MidgardObjectPropertyClass *klass = MIDGARD_OBJECT_PROPERTY_CLASS (g_class);
00036         
00037         gobject_class->set_property = NULL;
00038         gobject_class->get_property = NULL;
00039         gobject_class->finalize = NULL;
00040 
00041         klass->is_multilang = midgard_object_property_is_multilang; 
00042 }
00043 
00044 /* Register MidgardObjectProperty type */
00045 GType
00046 midgard_object_property_get_type (void)
00047 {
00048         static GType type = 0;
00049         if (type == 0) {
00050                 static const GTypeInfo info = {
00051                         sizeof (MidgardObjectPropertyClass),
00052                         NULL,           /* base_init */
00053                         NULL,           /* base_finalize */
00054                         (GClassInitFunc) _midgard_object_property_class_init,
00055                         NULL,           /* class_finalize */
00056                         NULL,           /* class_data */
00057                         sizeof (MidgardObjectProperty),
00058                         0,              /* n_preallocs */
00059                         NULL            /* instance_init */
00060                 };
00061                 type = g_type_register_static (G_TYPE_OBJECT,
00062                                 "MidgardObjectProperty",
00063                                 &info, 0);
00064         }
00065         return type;
00066 }
00067 
00068 
00069 /* Get Midgard Type of the property. */
00070 GType 
00071 midgard_object_property_get_midgard_type(const gchar *classname, const gchar *property){
00072 
00073         _MGD_GET_PRIVATE_CLASS_DATA;
00074 
00075         MgdSchemaPropertyAttr *prop_attr;
00076         if((prop_attr = g_hash_table_lookup(type_attr->prophash, property)) == NULL)
00077                 return MGD_TYPE_NONE;
00078 
00079         return prop_attr->gtype;
00080 }
00081 
00082 /* Checks if property is a link to another type. */
00083 gboolean 
00084 midgard_object_property_is_link(const gchar *classname, const gchar *property){
00085 
00086         _MGD_GET_PRIVATE_CLASS_DATA;
00087 
00088         MgdSchemaPropertyAttr *prop_attr;
00089         if((prop_attr = g_hash_table_lookup(type_attr->prophash, property)) == NULL)
00090                 return FALSE;
00091         
00092         return prop_attr->is_link;
00093 }
00094 
00095 /* Checks if property is linked with another type. */
00096 gboolean
00097 midgard_object_property_is_linked(const gchar *classname, const gchar *property){
00098 
00099         _MGD_GET_PRIVATE_CLASS_DATA;
00100 
00101         MgdSchemaPropertyAttr *prop_attr;
00102         if((prop_attr = g_hash_table_lookup(type_attr->prophash, property)) == NULL)
00103                 return FALSE;
00104 
00105         return prop_attr->is_linked;
00106         
00107 }
00108 
00109 /* Gets the class name of type which property is linked to. */
00110 const gchar
00111 *midgard_object_property_get_link_name(const gchar *classname, const gchar *property){
00112 
00113         _MGD_GET_PRIVATE_CLASS_DATA;
00114         
00115         MgdSchemaPropertyAttr *prop_attr;
00116         if((prop_attr = g_hash_table_lookup(type_attr->prophash, property)) == NULL)
00117                 return NULL;
00118         
00119         return prop_attr->link;                                 
00120 }
00121 
00122 /* Gets description of the property. */
00123 const gchar 
00124 *midgard_object_property_description(const gchar *classname, const gchar *property){
00125 
00126         g_assert(classname != NULL);
00127         g_assert(property != NULL);
00128         
00129         GObject *object = g_object_new(
00130                         g_type_from_name(classname), NULL);
00131         
00132         GParamSpec *prop = g_object_class_find_property(
00133                         G_OBJECT_GET_CLASS(G_OBJECT(object)), property);
00134 
00135         if(!prop)
00136                 return NULL;
00137 
00138         return  g_param_spec_get_blurb(prop);
00139 }
00140 
00141 /*  Checks if property is multilingual. */
00142 gboolean
00143 midgard_object_property_is_multilang(const gchar *classname, const gchar *property){
00144 
00145         _MGD_GET_PRIVATE_CLASS_DATA;
00146         
00147         MgdSchemaPropertyAttr *prop_attr;
00148         if((prop_attr = g_hash_table_lookup(type_attr->prophash, property)) == NULL)
00149                 return FALSE;
00150         
00151         return prop_attr->is_multilang;
00152 }
00153 
00154 /*  Returns the class that the named property is linked to. */
00155 extern MidgardObjectClass
00156 *midgard_object_property_get_link_class(MidgardObjectClass *klass, const gchar *property)
00157 {
00158         g_assert(klass != NULL);
00159         g_assert(property != NULL);
00160 
00161         MgdSchemaPropertyAttr *prop_attr;
00162         if((prop_attr = g_hash_table_lookup(klass->data->prophash, property)) == NULL)
00163                 return NULL;
00164         
00165         if(prop_attr->is_link) {
00166                 
00167                 MidgardObjectClass *link_klass = 
00168                         g_type_class_peek(g_type_from_name(prop_attr->link)); 
00169                 return link_klass;
00170         }
00171         return NULL;
00172 }

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