gchar *
mdm_get_layout_from_name (const char *name)
{
#ifdef HAVE_LIBXKLAVIER
        XklConfigItem *item;
        char          *layout;
        char          *variant;
        char          *result;
        char          *id1;
        char          *id2;
        char          *p;

        init_xkl ();

        id1 = g_strdup (name);
        p = strchr (id1, '\t');

        if (p != NULL) {
                id2 = p + 1;
                *p = 0;
        } else {
                id2 = NULL;
        }

        item = xkl_config_item_new ();

        g_snprintf (item->name, XKL_MAX_CI_NAME_LENGTH, "%s", id1);
        if (xkl_config_registry_find_layout (config_registry, item)) {
                layout = g_strdup(item->description);
        } else {
                layout =  g_strdup_printf ("Layout %s", id1);
        }

        if (id2 != NULL) {
                g_snprintf (item->name, XKL_MAX_CI_NAME_LENGTH, "%s", id2);
                if (xkl_config_registry_find_variant (config_registry, id1, item))
                        variant = g_strdup(item->description);
                else
                        variant = g_strdup_printf ("Variant %s", id2);
        } else {
                variant = NULL;
        }

        g_object_unref (item);

        g_free (id1);

        if (variant != NULL) {
                result = g_strdup_printf ("%s (%s)", layout, variant);
                g_free (layout);
                g_free (variant);
        } else {
                result = layout;
        }

        return result;
#else
        return NULL;
#endif
}
gboolean
mdm_layout_is_valid (const char *layout_variant)
{
#ifdef HAVE_LIBXKLAVIER
        XklConfigItem *item;
        char          *layout;
        char          *variant;
        gboolean       retval;

        layout = g_strdup (layout_variant);
        variant = strchr (layout, '\t');
        if (variant != NULL) {
                variant[0] = '\0';
                variant++;
        }

        item = xkl_config_item_new ();
        g_snprintf (item->name, XKL_MAX_CI_NAME_LENGTH, "%s", layout);

        retval = xkl_config_registry_find_layout (config_registry, item);

        if (retval && variant != NULL) {
                g_snprintf (item->name, XKL_MAX_CI_NAME_LENGTH, "%s", variant);
                retval = xkl_config_registry_find_variant (config_registry, layout, item);
        }

        g_object_unref (item);
        g_free (layout);

        return retval;
#else
        return TRUE;
#endif
}
static gboolean
filter_xkb_config (void)
{
	XklConfigItem *item;
	gchar *lname;
	gchar *vname;
	gchar **lv;
	gboolean any_change = FALSE;

	xkl_debug (100, "Filtering configuration against the registry\n");
	if (!ensure_xkl_registry ())
		return FALSE;

	lv = current_kbd_config.layouts_variants;
	item = xkl_config_item_new ();
	while (*lv) {
		xkl_debug (100, "Checking [%s]\n", *lv);
		if (gkbd_keyboard_config_split_items (*lv, &lname, &vname)) {
			gboolean should_be_dropped = FALSE;
			g_snprintf (item->name, sizeof (item->name), "%s",
				    lname);
			if (!xkl_config_registry_find_layout
			    (xkl_registry, item)) {
				xkl_debug (100, "Bad layout [%s]\n",
					   lname);
				should_be_dropped = TRUE;
			} else if (vname) {
				g_snprintf (item->name,
					    sizeof (item->name), "%s",
					    vname);
				if (!xkl_config_registry_find_variant
				    (xkl_registry, lname, item)) {
					xkl_debug (100,
						   "Bad variant [%s(%s)]\n",
						   lname, vname);
					should_be_dropped = TRUE;
				}
			}
			if (should_be_dropped) {
				gkbd_strv_behead (lv);
				any_change = TRUE;
				continue;
			}
		}
		lv++;
	}
	g_object_unref (item);
	return any_change;
}
static gboolean
filter_xkb_config (void)
{
	XklConfigItem *item;
	gchar *lname;
	gchar *vname;
	GSList *lv;
	GSList *filtered;
	gboolean any_change = FALSE;

	xkl_debug (100, "Filtering configuration against the registry\n");
	if (!xkl_registry) {
		xkl_registry =
		    xkl_config_registry_get_instance (xkl_engine);
		/* load all materials, unconditionally! */
		if (!xkl_config_registry_load (xkl_registry, TRUE)) {
			g_object_unref (xkl_registry);
			xkl_registry = NULL;
			return FALSE;
		}
	}
	lv = current_kbd_config.layouts_variants;
	item = xkl_config_item_new ();
	while (lv) {
		xkl_debug (100, "Checking [%s]\n", lv->data);
		if (gkbd_keyboard_config_split_items
		    (lv->data, &lname, &vname)) {
			g_snprintf (item->name, sizeof (item->name), "%s",
				    lname);
			if (!xkl_config_registry_find_layout
			    (xkl_registry, item)) {
				xkl_debug (100, "Bad layout [%s]\n",
					   lname);
				filtered = lv;
				lv = lv->next;
				g_free (filtered->data);
				current_kbd_config.layouts_variants =
				    g_slist_delete_link
				    (current_kbd_config.layouts_variants,
				     filtered);
				any_change = TRUE;
				continue;
			}
			if (vname) {
				g_snprintf (item->name,
					    sizeof (item->name), "%s",
					    vname);
				if (!xkl_config_registry_find_variant
				    (xkl_registry, lname, item)) {
					xkl_debug (100,
						   "Bad variant [%s(%s)]\n",
						   lname, vname);
					filtered = lv;
					lv = lv->next;
					g_free (filtered->data);
					current_kbd_config.layouts_variants
					    =
					    g_slist_delete_link
					    (current_kbd_config.
					     layouts_variants, filtered);
					any_change = TRUE;
					continue;
				}
			}
		}
		lv = lv->next;
	}
	g_object_unref (item);
	return any_change;
}