/**
 * appdata_validate_format_xml:
 **/
static void
appdata_validate_format_xml (const gchar *filename, GPtrArray *probs)
{
	AsProblem *problem;
	GString *tmp;
	guint i;

	g_print ("<results version=\"1\">\n");
	g_print ("  <filename>%s</filename>\n", filename);
	if (probs->len > 0) {
		g_print ("  <problems>\n");
		for (i = 0; i < probs->len; i++) {
			problem = g_ptr_array_index (probs, i);
			tmp = g_string_new (as_problem_get_message (problem));
			gs_string_replace (tmp, "&", "&amp;");
			gs_string_replace (tmp, "<", "");
			gs_string_replace (tmp, ">", "");
			if (as_problem_get_line_number (problem) > 0) {
				g_print ("    <problem type=\"%s\" line=\"%i\">%s</problem>\n",
					 as_problem_kind_to_string (as_problem_get_kind (problem)),
					 as_problem_get_line_number (problem),
					 tmp->str);
			} else {
				g_print ("    <problem type=\"%s\">%s</problem>\n",
					 as_problem_kind_to_string (as_problem_get_kind (problem)),
					 tmp->str);
			}
			g_string_free (tmp, TRUE);
		}
		g_print ("  </problems>\n");
	}
	g_print ("</results>\n");
}
/**
 * appdata_validate_format_html:
 **/
static void
appdata_validate_format_html (const gchar *filename, GPtrArray *probs)
{
	AsProblem *problem;
	GString *tmp;
	guint i;

	g_print ("<html>\n");
	g_print ("<head>\n");
	g_print ("<style type=\"text/css\">\n");
	g_print ("body {width: 70%%; font: 12px/20px Arial, Helvetica;}\n");
	g_print ("p {color: #333;}\n");
	g_print ("</style>\n");
	g_print ("<title>AppData Validation Results for %s</title>\n", filename);
	g_print ("</head>\n");
	g_print ("<body>\n");
	if (probs->len == 0) {
		g_print ("<h1>Success!</h1>\n");
		g_print ("<p>%s validated successfully.</p>\n", filename);
	} else {
		g_print ("<h1>Validation failed!</h1>\n");
		g_print ("<p>%s did not validate:</p>\n", filename);
		g_print ("<ul>\n");
		for (i = 0; i < probs->len; i++) {
			problem = g_ptr_array_index (probs, i);
			tmp = g_string_new (as_problem_get_message (problem));
			gs_string_replace (tmp, "&", "&amp;");
			gs_string_replace (tmp, "<", "[");
			gs_string_replace (tmp, ">", "]");
			g_print ("<li>");
			g_print ("%s\n", tmp->str);
			if (as_problem_get_line_number (problem) > 0) {
				g_print (" (line %i)",
					 as_problem_get_line_number (problem));
			}
			g_print ("</li>\n");
			g_string_free (tmp, TRUE);
		}
		g_print ("</ul>\n");
	}
	g_print ("</body>\n");
	g_print ("</html>\n");
}
/**
 * gs_plugin_parse_json:
 *
 * This is a quick and dirty JSON parser that extracts one line from the
 * JSON formatted data. Sorry JsonGlib, you look awesome, but you're just too
 * heavy for an error message.
 */
static gchar *
gs_plugin_parse_json (const gchar *data, gsize data_len, const gchar *key)
{
	gchar *key_full;
	gchar *value = NULL;
	guint i;
	gchar *tmp;
	guint len;
	_cleanup_string_free_ GString *string = NULL;
	_cleanup_strv_free_ gchar **split = NULL;

	/* format the key to match what JSON returns */
	key_full = g_strdup_printf ("\"%s\":", key);

	/* replace escaping with something sane */
	string = g_string_new_len (data, data_len);
	gs_string_replace (string, "\\\"", "'");

	/* find the line that corresponds to our key */
	split = g_strsplit (string->str, "\n", -1);
	for (i = 0; split[i] != NULL; i++) {
		tmp = g_strchug (split[i]);
		if (g_str_has_prefix (tmp, key_full)) {
			tmp += strlen (key_full);

			/* remove leading chars */
			tmp = g_strstrip (tmp);
			if (tmp[0] == '\"')
				tmp += 1;

			/* remove trailing chars */
			len = strlen (tmp);
			if (tmp[len-1] == ',')
				len -= 1;
			if (tmp[len-1] == '\"')
				len -= 1;
			value = g_strndup (tmp, len);
		}
	}
	return value;
}