Open Source Content Management System

Midgard core - how to write new application

This example desribes how to write midgard application using Midgard API. Compiled application will print message about types registered in Midgard Schema and defined in midgard configuration file.

int
main (int argc, char **argv)
{
    gchar *config_file = argv[1];

    g_type_init();
    /* GType system is already initialized. This is mandatory in applications 
       which use GObjects and Gtype system */

    GError *err = NULL;
    /* Error is initialized to NULL. 

    MidgardConnection *midgard = g_object_new(MIDGARD_TYPE_CONNECTION, NULL);
    /* New MidgardConnection object instance. Constructor. */ 

    MidgardConfig *config = g_object_new(MIDGARD_TYPE_CONFIG, NULL);
    /* New MidgardConfig object. Constructor. */

    if(!midgard_config_read_file(config, (const gchar *)config_file))
            return 1;
    /* read_file method was invoked for MidgardConfig object */

    if(!midgard_connection_open_config(midgard, config, err))
            return 1;
    /* open_config method was invoked for MidgardConnection object */

    guint n_types, i;
    GType *all_types = g_type_children(MIDGARD_TYPE_OBJECT, &n_types);
    /* All child types of MIDGARD_TYPE_OBJECT are returned as array of 
    their corresponding GTypes */ 

    g_message("Midgard classes registered in MgdSchema:");

    for (i = 0; i < n_types; i++) {
            typename = g_type_name(all_types[i]);
            /*  Get class name from Gtype value */ 

            MgdObject *object = midgard_object_new(midgard->mgd, typename);
            /* Initialize new object */

            g_message("Type %s", typename);

            g_object_unref(object);
            /* Destroy object */
    }
}
Designed by Nemein, hosted by Anykey