MateWPItem * mate_wp_item_new (const gchar * filename,
				 GHashTable * wallpapers,
				 MateDesktopThumbnailFactory * thumbnails) {
  MateWPItem *item = g_new0 (MateWPItem, 1);

  item->filename = g_strdup (filename);
  item->fileinfo = mate_wp_info_new (filename, thumbnails);

  if (item->fileinfo != NULL && item->fileinfo->mime_type != NULL &&
      (g_str_has_prefix (item->fileinfo->mime_type, "image/") ||
       strcmp (item->fileinfo->mime_type, "application/xml") == 0)) {

    if (g_utf8_validate (item->fileinfo->name, -1, NULL))
      item->name = g_strdup (item->fileinfo->name);
    else
      item->name = g_filename_to_utf8 (item->fileinfo->name, -1, NULL,
				       NULL, NULL);

    mate_wp_item_update (item);
    mate_wp_item_ensure_mate_bg (item);
    mate_wp_item_update_description (item);

    g_hash_table_insert (wallpapers, item->filename, item);
  } else {
    mate_wp_item_free (item);
    item = NULL;
  }

  return item;
}
static void mate_wp_xml_load_xml (AppearanceData *data,
                                  const gchar * filename) {
    xmlDoc * wplist;
    xmlNode * root, * list, * wpa;
    xmlChar * nodelang;
    const gchar * const * syslangs;
    GdkColor color1, color2;
    gint i;

    wplist = xmlParseFile (filename);

    if (!wplist)
        return;

    syslangs = g_get_language_names ();

    root = xmlDocGetRootElement (wplist);

    for (list = root->children; list != NULL; list = list->next) {
        if (!strcmp ((gchar *)list->name, "wallpaper")) {
            MateWPItem * wp;
            gchar *pcolor = NULL, *scolor = NULL;
            gchar *s;
            gboolean have_scale = FALSE, have_shade = FALSE;

            wp = g_new0 (MateWPItem, 1);

            wp->deleted = mate_wp_xml_get_bool (list, "deleted");

            for (wpa = list->children; wpa != NULL; wpa = wpa->next) {
                if (wpa->type == XML_COMMENT_NODE) {
                    continue;
                } else if (!strcmp ((gchar *)wpa->name, "filename")) {
                    if (wpa->last != NULL && wpa->last->content != NULL) {
                        const char * none = "(none)";
                        gchar *content = g_strstrip ((gchar *)wpa->last->content);

                        if (!strcmp (content, none))
                            wp->filename = g_strdup (content);
                        else if (g_utf8_validate (content, -1, NULL) &&
                                 g_file_test (content, G_FILE_TEST_EXISTS))
                            wp->filename = g_strdup (content);
                        else
                            wp->filename = g_filename_from_utf8 (content, -1, NULL, NULL, NULL);
                    } else {
                        break;
                    }
                } else if (!strcmp ((gchar *)wpa->name, "name")) {
                    if (wpa->last != NULL && wpa->last->content != NULL) {
                        nodelang = xmlNodeGetLang (wpa->last);

                        if (wp->name == NULL && nodelang == NULL) {
                            wp->name = g_strdup (g_strstrip ((gchar *)wpa->last->content));
                        } else {
                            for (i = 0; syslangs[i] != NULL; i++) {
                                if (!strcmp (syslangs[i], (gchar *)nodelang)) {
                                    g_free (wp->name);
                                    wp->name = g_strdup (g_strstrip ((gchar *)wpa->last->content));
                                    break;
                                }
                            }
                        }

                        xmlFree (nodelang);
                    } else {
                        break;
                    }
                } else if (!strcmp ((gchar *)wpa->name, "options")) {
                    if (wpa->last != NULL) {
                        wp->options = wp_item_string_to_option (g_strstrip ((gchar *)wpa->last->content));
                        have_scale = TRUE;
                    }
                } else if (!strcmp ((gchar *)wpa->name, "shade_type")) {
                    if (wpa->last != NULL) {
                        wp->shade_type = wp_item_string_to_shading (g_strstrip ((gchar *)wpa->last->content));
                        have_shade = TRUE;
                    }
                } else if (!strcmp ((gchar *)wpa->name, "pcolor")) {
                    if (wpa->last != NULL) {
                        pcolor = g_strdup (g_strstrip ((gchar *)wpa->last->content));
                    }
                } else if (!strcmp ((gchar *)wpa->name, "scolor")) {
                    if (wpa->last != NULL) {
                        scolor = g_strdup (g_strstrip ((gchar *)wpa->last->content));
                    }
                } else if (!strcmp ((gchar *)wpa->name, "text")) {
                    /* Do nothing here, libxml2 is being weird */
                } else {
                    g_warning ("Unknown Tag: %s", wpa->name);
                }
            }

            /* Make sure we don't already have this one and that filename exists */
            if (wp->filename == NULL ||
                    g_hash_table_lookup (data->wp_hash, wp->filename) != NULL) {

                mate_wp_item_free (wp);
                g_free (pcolor);
                g_free (scolor);
                continue;
            }

            /* Verify the colors and alloc some GdkColors here */
            if (!have_scale) {
                s = mateconf_client_get_string (data->client, WP_OPTIONS_KEY, NULL);
                wp->options = wp_item_string_to_option (s);
                g_free (s);
            }

            if (!have_shade) {
                s = mateconf_client_get_string (data->client, WP_SHADING_KEY, NULL);
                wp->shade_type = wp_item_string_to_shading (s);
                g_free (s);
            }

            if (pcolor == NULL) {
                pcolor = mateconf_client_get_string (data->client,
                                                     WP_PCOLOR_KEY, NULL);
            }
            if (scolor == NULL) {
                scolor = mateconf_client_get_string (data->client,
                                                     WP_SCOLOR_KEY, NULL);
            }
            gdk_color_parse (pcolor, &color1);
            gdk_color_parse (scolor, &color2);
            g_free (pcolor);
            g_free (scolor);

            wp->pcolor = gdk_color_copy (&color1);
            wp->scolor = gdk_color_copy (&color2);

            if ((wp->filename != NULL &&
                    g_file_test (wp->filename, G_FILE_TEST_EXISTS)) ||
                    !strcmp (wp->filename, "(none)")) {
                wp->fileinfo = mate_wp_info_new (wp->filename, data->thumb_factory);

                if (wp->name == NULL || !strcmp (wp->filename, "(none)")) {
                    g_free (wp->name);
                    wp->name = g_strdup (wp->fileinfo->name);
                }

                mate_wp_item_ensure_mate_bg (wp);
                mate_wp_item_update_description (wp);
                g_hash_table_insert (data->wp_hash, wp->filename, wp);
            } else {
                mate_wp_item_free (wp);
                wp = NULL;
            }
        }
    }
    xmlFreeDoc (wplist);
}