/**
 * gs_plugin_add_updates:
 */
gboolean
gs_plugin_add_updates (GsPlugin *plugin,
		       GList **list,
		       GCancellable *cancellable,
		       GError **error)
{
	GsApp *app;

	/* update UI as this might take some time */
	gs_plugin_status_update (plugin, NULL, GS_PLUGIN_STATUS_WAITING);

	/* spin */
	g_usleep (2 * G_USEC_PER_SEC);

	/* add a normal application */
	app = gs_app_new ("gnome-boxes");
	gs_app_set_name (app, GS_APP_QUALITY_NORMAL, "Boxes");
	gs_app_set_summary (app, GS_APP_QUALITY_NORMAL, "Do not segfault when using newer versons of libvirt.");
	gs_app_set_kind (app, GS_APP_KIND_NORMAL);
	gs_app_set_id_kind (app, AS_ID_KIND_DESKTOP);
	gs_plugin_add_app (list, app);
	g_object_unref (app);

	/* add an OS update */
	app = gs_app_new ("libvirt-glib-devel;0.0.1;noarch;fedora");
	gs_app_set_name (app, GS_APP_QUALITY_NORMAL, "libvirt-glib-devel");
	gs_app_set_summary (app, GS_APP_QUALITY_NORMAL, "Fix several memory leaks.");
	gs_app_set_kind (app, GS_APP_KIND_PACKAGE);
	gs_app_set_id_kind (app, AS_ID_KIND_DESKTOP);
	gs_plugin_add_app (list, app);
	g_object_unref (app);

	/* add a second OS update */
	app = gs_app_new ("gnome-boxes-libs;0.0.1;i386;updates-testing");
	gs_app_set_name (app, GS_APP_QUALITY_NORMAL, "gnome-boxes-libs");
	gs_app_set_summary (app, GS_APP_QUALITY_NORMAL, "Do not segfault when using newer versons of libvirt.");
	gs_app_set_kind (app, GS_APP_KIND_PACKAGE);
	gs_app_set_id_kind (app, AS_ID_KIND_DESKTOP);
	gs_plugin_add_app (list, app);
	g_object_unref (app);

	return TRUE;
}
/**
 * gs_plugin_packagekit_refresh_guess_app_id:
 */
static gboolean
gs_plugin_packagekit_refresh_guess_app_id (GsPlugin *plugin,
					   GsApp *app,
					   const gchar *filename,
					   GCancellable *cancellable,
					   GError **error)
{
	PkFiles *item;
	ProgressData data;
	guint i;
	guint j;
	gchar **fns;
	g_auto(GStrv) files = NULL;
	g_autoptr(PkResults) results = NULL;
	g_autoptr(GPtrArray) array = NULL;

	data.plugin = plugin;
	data.ptask = NULL;

	/* get file list so we can work out ID */
	files = g_strsplit (filename, "\t", -1);
	results = pk_client_get_files_local (PK_CLIENT (plugin->priv->task),
					     files,
					     cancellable,
					     gs_plugin_packagekit_progress_cb, &data,
					     error);
	if (results == NULL)
		return FALSE;
	array = pk_results_get_files_array (results);
	if (array->len == 0) {
		g_set_error (error,
			     GS_PLUGIN_ERROR,
			     GS_PLUGIN_ERROR_FAILED,
			     "no files for %s", filename);
		return FALSE;
	}

	/* find the first desktop file */
	for (i = 0; i < array->len; i++) {
		item = g_ptr_array_index (array, i);
		fns = pk_files_get_files (item);
		for (j = 0; fns[j] != NULL; j++) {
			if (g_str_has_prefix (fns[j], "/usr/share/applications/") &&
			    g_str_has_suffix (fns[j], ".desktop")) {
				g_autofree gchar *basename = g_path_get_basename (fns[j]);
				gs_app_set_id (app, basename);
				gs_app_set_kind (app, GS_APP_KIND_NORMAL);
				gs_app_set_id_kind (app, AS_ID_KIND_DESKTOP);
				break;
			}
		}
	}
	return TRUE;
}
/**
 * gs_plugin_add_popular:
 */
gboolean
gs_plugin_add_popular (GsPlugin *plugin,
		       GList **list,
		       GCancellable *cancellable,
		       GError **error)
{
	g_autoptr(GsApp) app = gs_app_new ("gnome-power-manager");
	gs_app_set_name (app, GS_APP_QUALITY_NORMAL, "Power Manager");
	gs_app_set_summary (app, GS_APP_QUALITY_NORMAL, "Power Management Program");
	gs_app_set_state (app, AS_APP_STATE_AVAILABLE);
	gs_app_set_kind (app, GS_APP_KIND_NORMAL);
	gs_plugin_add_app (list, app);
	gs_app_set_id_kind (app, AS_ID_KIND_DESKTOP);

	return TRUE;
}
/**
 * gs_plugin_add_category_apps:
 */
gboolean
gs_plugin_add_category_apps (GsPlugin *plugin,
			     GsCategory *category,
			     GList **list,
			     GCancellable *cancellable,
			     GError **error)
{
	g_autoptr(GsApp) app = gs_app_new ("gnome-boxes");
	gs_app_set_name (app, GS_APP_QUALITY_NORMAL, "Boxes");
	gs_app_set_summary (app, GS_APP_QUALITY_NORMAL, "View and use virtual machines");
	gs_app_set_url (app, AS_URL_KIND_HOMEPAGE, "http://www.box.org");
	gs_app_set_kind (app, GS_APP_KIND_NORMAL);
	gs_app_set_state (app, AS_APP_STATE_AVAILABLE);
	gs_app_set_pixbuf (app, gdk_pixbuf_new_from_file ("/usr/share/icons/hicolor/48x48/apps/gnome-boxes.png", NULL));
	gs_app_set_id_kind (app, AS_ID_KIND_DESKTOP);
	gs_plugin_add_app (list, app);
	return TRUE;
}
/**
 * gs_plugin_add_updates:
 */
gboolean
gs_plugin_add_updates (GsPlugin *plugin,
			GList **list,
			GCancellable *cancellable,
			GError **error)
{
	g_autoptr(GList) updates = NULL;
	GList *l;
	g_autoptr(GError) error_local = NULL;

	updates = li_manager_get_update_list (plugin->priv->mgr, &error_local);
	if (error_local != NULL) {
		g_set_error (error,
				GS_PLUGIN_ERROR,
				GS_PLUGIN_ERROR_FAILED,
				"Failed to list updates: %s",
				error_local->message);
		return FALSE;
	}

	for (l = updates; l != NULL; l = l->next) {
		LiPkgInfo *old_pki;
		LiPkgInfo *new_pki;
		const gchar *cptkind_str;
		g_autoptr(GsApp) app = NULL;
		LiUpdateItem *uitem = LI_UPDATE_ITEM (l->data);

		old_pki = li_update_item_get_installed_pkg (uitem);
		new_pki = li_update_item_get_available_pkg (uitem);

		cptkind_str = li_pkg_info_get_component_kind (old_pki);
		if ((cptkind_str != NULL) && (g_strcmp0 (cptkind_str, "desktop") == 0)) {
			g_autofree gchar *tmp = NULL;
			/* type=desktop AppStream components result in a Limba bundle name which has the .desktop stripped away.
			 * We need to re-add it for GNOME Software.
			 * In any other case, the Limba bundle name equals the AppStream ID of the component it contains */
			tmp = g_strdup_printf ("%s.desktop", li_pkg_info_get_name (old_pki));
			app = gs_app_new (tmp);
			gs_app_set_id_kind (app, AS_ID_KIND_DESKTOP);
		} else {
			app = gs_app_new (li_pkg_info_get_name (old_pki));
		}

		gs_app_set_management_plugin (app, "Limba");
		gs_app_set_state (app, AS_APP_STATE_UPDATABLE_LIVE);
		gs_app_set_kind (app, GS_APP_KIND_PACKAGE);
		gs_plugin_add_app (list, app);
		gs_app_set_name (app,
				 GS_APP_QUALITY_LOWEST,
				 li_pkg_info_get_name (old_pki));
		gs_app_set_summary (app,
				    GS_APP_QUALITY_LOWEST,
				    li_pkg_info_get_name (old_pki));
		gs_app_set_version (app,
				    li_pkg_info_get_version (old_pki));
		gs_app_set_update_version (app,
					   li_pkg_info_get_version (new_pki));
		gs_app_add_source (app,
				   li_pkg_info_get_id (old_pki));
		gs_plugin_add_app (list, app);
	}

	return TRUE;
}