/**
 * pk_plugin_transaction_update_packages:
 */
static void
pk_plugin_transaction_update_packages (PkTransaction *transaction)
{
    gboolean ret;
    gchar **package_ids;
    gchar *packages_str = NULL;
    gchar *path = NULL;
    GError *error = NULL;
    GPtrArray *packages;
    guint i;
    PkConf *conf;

    /* only do this if we have systemd */
#ifndef PK_BUILD_SYSTEMD
    g_debug ("No systemd, so no PreparedUpdates");
    return;
#endif

    /* check the config file */
    conf = pk_transaction_get_conf (transaction);
    ret = pk_conf_get_bool (conf, "WritePreparedUpdates");
    if (!ret)
        goto out;

    /* get the existing prepared updates */
    path = g_build_filename (LOCALSTATEDIR,
                             "lib",
                             "PackageKit",
                             "prepared-update",
                             NULL);
    packages = pk_plugin_get_existing_prepared_updates (path);

    /* add any new ones */
    package_ids = pk_transaction_get_package_ids (transaction);
    for (i = 0; package_ids[i] != NULL; i++) {
        if (!pk_plugin_array_str_exists (packages, package_ids[i])) {
            g_ptr_array_add (packages,
                             g_strdup (package_ids[i]));
        }
    }
    g_ptr_array_add (packages, NULL);

    /* write filename */
    packages_str = g_strjoinv ("\n", (gchar **) packages->pdata);
    ret = g_file_set_contents (path,
                               packages_str,
                               -1,
                               &error);
    if (!ret) {
        g_warning ("failed to write %s: %s",
                   path, error->message);
        g_error_free (error);
        goto out;
    }
out:
    g_free (packages_str);
    g_free (path);
    return;
}
/**
 * pk_plugin_transaction_action_method:
 */
static void
pk_plugin_transaction_action_method (PkPlugin *plugin,
				     PkTransaction *transaction,
				     PkResults *results)
{
	GPtrArray *invalidated = NULL;
	PkPackage *pkg;
	PkPackageSack *sack;
	const gchar *package_id;
	gchar **package_ids;
	guint i;

	/* get the existing prepared updates */
	sack = pk_plugin_get_existing_prepared_updates (PK_OFFLINE_PREPARED_UPDATE_FILENAME);
	if (pk_package_sack_get_size (sack) == 0)
		goto out;

	/* are there any requested packages that match in prepared-updates */
	package_ids = pk_transaction_get_package_ids (transaction);
	for (i = 0; package_ids[i] != NULL; i++) {
		pkg = pk_package_sack_find_by_id_name_arch (sack, package_ids[i]);
		if (pkg != NULL) {
			g_debug ("%s modified %s, invalidating prepared-updates",
				 package_ids[i], pk_package_get_id (pkg));
			pk_plugin_state_changed (plugin);
			g_object_unref (pkg);
			goto out;
		}
	}

	/* are there any changed deps that match a package in prepared-updates */
	invalidated = pk_results_get_package_array (results);
	for (i = 0; i < invalidated->len; i++) {
		package_id = pk_package_get_id (g_ptr_array_index (invalidated, i));
		pkg = pk_package_sack_find_by_id_name_arch (sack, package_id);
		if (pkg != NULL) {
			g_debug ("%s modified %s, invalidating prepared-updates",
				 package_id, pk_package_get_id (pkg));
			pk_plugin_state_changed (plugin);
			g_object_unref (pkg);
			goto out;
		}
	}
out:
	if (invalidated != NULL)
		g_ptr_array_unref (invalidated);
	g_object_unref (sack);
}