/**
 * asb_plugin_process_filename:
 */
static gboolean
asb_plugin_process_filename (AsbPlugin *plugin,
			     AsbPackage *pkg,
			     const gchar *filename,
			     GList **apps,
			     GError **error)
{
	_cleanup_object_unref_ AsbApp *app = NULL;

	app = asb_app_new (pkg, NULL);
	if (!as_app_parse_file (AS_APP (app), filename,
				AS_APP_PARSE_FLAG_APPEND_DATA,
				error))
		return FALSE;
	if (as_app_get_id_kind (AS_APP (app)) != AS_ID_KIND_ADDON &&
	    as_app_get_id_kind (AS_APP (app)) != AS_ID_KIND_FONT) {
		g_set_error (error,
			     ASB_PLUGIN_ERROR,
			     ASB_PLUGIN_ERROR_FAILED,
			     "%s is not an addon or font",
			     as_app_get_id (AS_APP (app)));
		return FALSE;
	}
	asb_app_set_requires_appdata (app, FALSE);
	asb_app_set_hidpi_enabled (app, asb_context_get_flag (plugin->ctx, ASB_CONTEXT_FLAG_HIDPI_ICONS));
	asb_plugin_add_app (apps, AS_APP (app));
	return TRUE;
}
/**
 * asb_plugin_process:
 */
GList *
asb_plugin_process (AsbPlugin *plugin,
		    AsbPackage *pkg,
		    const gchar *tmpdir,
		    GError **error)
{
	const gchar *tmp;
	gchar **split;
	GList *apps = NULL;
	GPtrArray *keywords;
	guint i;
	guint j;
	_cleanup_free_ gchar *app_id = NULL;
	_cleanup_object_unref_ AsbApp *app = NULL;
	_cleanup_object_unref_ AsIcon *icon = NULL;
	_cleanup_string_free_ GString *str = NULL;

	/* use the pkgname suffix as the app-id */
	tmp = asb_package_get_name (pkg);
	if (g_str_has_prefix (tmp, "gstreamer1-"))
		tmp += 11;
	if (g_str_has_prefix (tmp, "gstreamer-"))
		tmp += 10;
	if (g_str_has_prefix (tmp, "plugins-"))
		tmp += 8;
	app_id = g_strdup_printf ("gstreamer-%s", tmp);

	/* create app */
	app = asb_app_new (pkg, app_id);
	as_app_set_id_kind (AS_APP (app), AS_ID_KIND_CODEC);
	as_app_set_name (AS_APP (app), "C", "GStreamer Multimedia Codecs", -1);
	asb_app_set_requires_appdata (app, TRUE);
	asb_app_set_hidpi_enabled (app, asb_context_get_hidpi_enabled (plugin->ctx));
	as_app_add_category (AS_APP (app), "Addons", -1);
	as_app_add_category (AS_APP (app), "Codecs", -1);

	/* add icon */
	icon = as_icon_new ();
	as_icon_set_kind (icon, AS_ICON_KIND_STOCK);
	as_icon_set_name (icon, "application-x-executable", -1);
	as_app_add_icon (AS_APP (app), icon);

	for (i = 0; data[i].path != NULL; i++) {
		if (!asb_utils_is_file_in_tmpdir (tmpdir, data[i].path))
			continue;
		split = g_strsplit (data[i].text, "|", -1);
		for (j = 0; split[j] != NULL; j++)
			as_app_add_keyword (AS_APP (app), NULL, split[j], -1);
		g_strfreev (split);
	}

	/* no codecs we care about */
	keywords = as_app_get_keywords (AS_APP (app), NULL);
	if (keywords == NULL) {
		g_set_error (error,
			     ASB_PLUGIN_ERROR,
			     ASB_PLUGIN_ERROR_FAILED,
			     "nothing interesting in %s",
			     asb_package_get_basename (pkg));
		return NULL;
	}

	/* sort categories by name */
	g_ptr_array_sort (keywords, asb_utils_string_sort_cb);

	/* create a description */
	str = g_string_new ("Multimedia playback for ");
	if (keywords->len > 1) {
		for (i = 0; i < keywords->len - 1; i++) {
			tmp = g_ptr_array_index (keywords, i);
			g_string_append_printf (str, "%s, ", tmp);
		}
		g_string_truncate (str, str->len - 2);
		tmp = g_ptr_array_index (keywords, keywords->len - 1);
		g_string_append_printf (str, " and %s", tmp);
	} else {
		g_string_append (str, g_ptr_array_index (keywords, 0));
	}
	as_app_set_comment (AS_APP (app), "C", str->str, -1);

	/* add */
	asb_plugin_add_app (&apps, AS_APP (app));
	return apps;
}
/**
 * asb_plugin_process_filename:
 */
static gboolean
asb_plugin_process_filename (AsbPlugin *plugin,
                             AsbPackage *pkg,
                             const gchar *filename,
                             GList **apps,
                             const gchar *tmpdir,
                             GError **error)
{
    gboolean ret = TRUE;
    gchar *error_msg = 0;
    gchar *filename_tmp;
    gint rc;
    guint i;
    sqlite3 *db = NULL;
    _cleanup_free_ gchar *basename = NULL;
    _cleanup_free_ gchar *description = NULL;
    _cleanup_free_ gchar *language_string = NULL;
    _cleanup_free_ gchar *name = NULL;
    _cleanup_free_ gchar *symbol = NULL;
    _cleanup_object_unref_ AsbApp *app = NULL;
    _cleanup_object_unref_ AsIcon *icon = NULL;
    _cleanup_strv_free_ gchar **languages = NULL;

    /* open IME database */
    filename_tmp = g_build_filename (tmpdir, filename, NULL);
    rc = sqlite3_open (filename_tmp, &db);
    if (rc) {
        ret = FALSE;
        g_set_error (error,
                     ASB_PLUGIN_ERROR,
                     ASB_PLUGIN_ERROR_FAILED,
                     "Can't open database: %s",
                     sqlite3_errmsg (db));
        goto out;
    }

    /* get name */
    rc = sqlite3_exec(db, "SELECT * FROM ime WHERE attr = 'name' LIMIT 1;",
                      asb_plugin_sqlite_callback_cb,
                      &name, &error_msg);
    if (rc != SQLITE_OK) {
        ret = FALSE;
        g_set_error (error,
                     ASB_PLUGIN_ERROR,
                     ASB_PLUGIN_ERROR_FAILED,
                     "Can't get IME name from %s: %s",
                     filename, error_msg);
        sqlite3_free(error_msg);
        goto out;
    }

    /* get symbol */
    rc = sqlite3_exec(db, "SELECT * FROM ime WHERE attr = 'symbol' LIMIT 1;",
                      asb_plugin_sqlite_callback_cb,
                      &symbol, &error_msg);
    if (rc != SQLITE_OK) {
        ret = FALSE;
        g_set_error (error,
                     ASB_PLUGIN_ERROR,
                     ASB_PLUGIN_ERROR_FAILED,
                     "Can't get IME symbol from %s: %s",
                     filename, error_msg);
        sqlite3_free(error_msg);
        goto out;
    }

    /* get languages */
    rc = sqlite3_exec(db, "SELECT * FROM ime WHERE attr = 'languages' LIMIT 1;",
                      asb_plugin_sqlite_callback_cb,
                      &language_string, &error_msg);
    if (rc != SQLITE_OK) {
        ret = FALSE;
        g_set_error (error,
                     ASB_PLUGIN_ERROR,
                     ASB_PLUGIN_ERROR_FAILED,
                     "Can't get IME languages from %s: %s",
                     filename, error_msg);
        sqlite3_free(error_msg);
        goto out;
    }

    /* get description */
    rc = sqlite3_exec(db, "SELECT * FROM ime WHERE attr = 'description' LIMIT 1;",
                      asb_plugin_sqlite_callback_cb,
                      &description, &error_msg);
    if (rc != SQLITE_OK) {
        ret = FALSE;
        g_set_error (error,
                     ASB_PLUGIN_ERROR,
                     ASB_PLUGIN_ERROR_FAILED,
                     "Can't get IME name from %s: %s",
                     filename, error_msg);
        sqlite3_free(error_msg);
        goto out;
    }

    /* this is _required_ */
    if (name == NULL || description == NULL) {
        ret = FALSE;
        g_set_error (error,
                     ASB_PLUGIN_ERROR,
                     ASB_PLUGIN_ERROR_FAILED,
                     "No 'name' and 'description' in %s",
                     filename);
        goto out;
    }

    /* create new app */
    basename = g_path_get_basename (filename);
    app = asb_app_new (pkg, basename);
    as_app_set_id_kind (AS_APP (app), AS_ID_KIND_INPUT_METHOD);
    as_app_add_category (AS_APP (app), "Addons");
    as_app_add_category (AS_APP (app), "InputSources");
    as_app_set_name (AS_APP (app), "C", name);
    as_app_set_comment (AS_APP (app), "C", description);
    if (symbol != NULL && symbol[0] != '\0')
        as_app_add_metadata (AS_APP (app), "X-IBus-Symbol", symbol);
    if (language_string != NULL) {
        languages = g_strsplit (language_string, ",", -1);
        for (i = 0; languages[i] != NULL; i++) {
            if (g_strcmp0 (languages[i], "other") == 0)
                continue;
            as_app_add_language (AS_APP (app),
                                 100, languages[i]);
        }
    }
    asb_app_set_requires_appdata (app, TRUE);
    asb_app_set_hidpi_enabled (app, asb_context_get_flag (plugin->ctx, ASB_CONTEXT_FLAG_HIDPI_ICONS));

    /* add icon */
    icon = as_icon_new ();
    as_icon_set_kind (icon, AS_ICON_KIND_STOCK);
    as_icon_set_name (icon, "system-run-symbolic");
    as_app_add_icon (AS_APP (app), icon);

    asb_plugin_add_app (apps, AS_APP (app));
out:
    if (db != NULL)
        sqlite3_close (db);
    return ret;
}