/**
 * pk_plugin_transaction_finished_end:
 */
void
pk_plugin_transaction_finished_end (PkPlugin *plugin,
                                    PkTransaction *transaction)
{
    PkBitfield transaction_flags;
    PkExitEnum exit_enum;
    PkResults *results;
    PkRoleEnum role;

    /* skip simulate actions */
    if (pk_bitfield_contain (pk_transaction_get_transaction_flags (transaction),
                             PK_TRANSACTION_FLAG_ENUM_SIMULATE)) {
        goto out;
    }

    /* check for success */
    results = pk_transaction_get_results (transaction);
    exit_enum = pk_results_get_exit_code (results);
    if (exit_enum != PK_EXIT_ENUM_SUCCESS)
        goto out;

    /* if we're doing only-download then update prepared-updates */
    role = pk_transaction_get_role (transaction);
    transaction_flags = pk_transaction_get_transaction_flags (transaction);
    if (role == PK_ROLE_ENUM_UPDATE_PACKAGES &&
            pk_bitfield_contain (transaction_flags,
                                 PK_TRANSACTION_FLAG_ENUM_ONLY_DOWNLOAD)) {
        pk_plugin_transaction_update_packages (transaction);
        goto out;
    }

    /* if we do get-updates and there's no updates then remove
     * prepared-updates so the UI doesn't display update & reboot */
    if (role == PK_ROLE_ENUM_GET_UPDATES) {
        pk_plugin_transaction_get_updates (transaction);
        goto out;
    }

    /* delete the prepared updates file as it's no longer valid */
    if (role == PK_ROLE_ENUM_UPDATE_PACKAGES ||
            role == PK_ROLE_ENUM_INSTALL_PACKAGES ||
            role == PK_ROLE_ENUM_REMOVE_PACKAGES ||
            role == PK_ROLE_ENUM_REFRESH_CACHE) {
        pk_plugin_state_changed (plugin);
    }
out:
    return;
}
void
gs_offline_updates_show_error (void)
{
	const gchar *title;
	gboolean show_geeky = FALSE;
	GtkWidget *dialog;
	_cleanup_error_free_ GError *error = NULL;
	_cleanup_object_unref_ PkError *pk_error = NULL;
	_cleanup_object_unref_ PkResults *results = NULL;
	_cleanup_string_free_ GString *msg = NULL;

	results = pk_offline_get_results (NULL);
	if (results == NULL)
		return;
	pk_error = pk_results_get_error_code (results);
	if (pk_error == NULL)
		return;

	/* can this happen in reality? */
	if (pk_results_get_exit_code (results) == PK_EXIT_ENUM_SUCCESS)
		return;

	/* TRANSLATORS: this is when the offline update failed */
	title = _("Failed To Update");
	msg = g_string_new ("");
	switch (pk_error_get_code (pk_error)) {
	case PK_ERROR_ENUM_UNFINISHED_TRANSACTION:
		/* TRANSLATORS: the transaction could not be completed
 		 * as a previous transaction was unfinished */
		g_string_append (msg, _("A previous update was unfinished."));
		show_geeky = TRUE;
		break;
	case PK_ERROR_ENUM_PACKAGE_DOWNLOAD_FAILED:
	case PK_ERROR_ENUM_NO_CACHE:
	case PK_ERROR_ENUM_NO_NETWORK:
	case PK_ERROR_ENUM_NO_MORE_MIRRORS_TO_TRY:
	case PK_ERROR_ENUM_CANNOT_FETCH_SOURCES:
		/* TRANSLATORS: the package manager needed to download
		 * something with no network available */
		g_string_append (msg, _("Network access was required but not available."));
		break;
	case PK_ERROR_ENUM_BAD_GPG_SIGNATURE:
	case PK_ERROR_ENUM_CANNOT_UPDATE_REPO_UNSIGNED:
	case PK_ERROR_ENUM_GPG_FAILURE:
	case PK_ERROR_ENUM_MISSING_GPG_SIGNATURE:
	case PK_ERROR_ENUM_PACKAGE_CORRUPT:
		/* TRANSLATORS: if the package is not signed correctly
		 *  */
		g_string_append (msg, _("An update was not signed in the correct way."));
		show_geeky = TRUE;
		break;
	case PK_ERROR_ENUM_DEP_RESOLUTION_FAILED:
	case PK_ERROR_ENUM_FILE_CONFLICTS:
	case PK_ERROR_ENUM_INCOMPATIBLE_ARCHITECTURE:
	case PK_ERROR_ENUM_PACKAGE_CONFLICTS:
		/* TRANSLATORS: the transaction failed in a way the user
		 * probably cannot comprehend. Package management systems
		 * really are teh suck.*/
		g_string_append (msg, _("The update could not be completed."));
		show_geeky = TRUE;
		break;
	case PK_ERROR_ENUM_TRANSACTION_CANCELLED:
		/* TRANSLATORS: the user aborted the update manually */
		g_string_append (msg, _("The update was cancelled."));
		break;
	case PK_ERROR_ENUM_NO_PACKAGES_TO_UPDATE:
	case PK_ERROR_ENUM_UPDATE_NOT_FOUND:
		/* TRANSLATORS: the user must have updated manually after
		 * the updates were prepared */
		g_string_append (msg, _("An offline update was requested but no packages required updating."));
		break;
	case PK_ERROR_ENUM_NO_SPACE_ON_DEVICE:
		/* TRANSLATORS: we ran out of disk space */
		g_string_append (msg, _("No space was left on the drive."));
		break;
	case PK_ERROR_ENUM_PACKAGE_FAILED_TO_BUILD:
	case PK_ERROR_ENUM_PACKAGE_FAILED_TO_INSTALL:
	case PK_ERROR_ENUM_PACKAGE_FAILED_TO_REMOVE:
		/* TRANSLATORS: the update process failed in a general
		 * way, usually this message will come from source distros
		 * like gentoo */
		g_string_append (msg, _("An update failed to install correctly."));
		show_geeky = TRUE;
		break;
	default:
		/* TRANSLATORS: We didn't handle the error type */
		g_string_append (msg, _("The offline update failed in an unexpected way."));
		show_geeky = TRUE;
		break;
	}
	if (show_geeky) {
		g_string_append_printf (msg, "\n%s\n\n%s",
					/* TRANSLATORS: these are geeky messages from the
					 * package manager no mortal is supposed to understand,
					 * but google might know what they mean */
					_("Detailed errors from the package manager follow:"),
					pk_error_get_details (pk_error));
	}
	dialog = gtk_message_dialog_new (NULL,
					 0,
					 GTK_MESSAGE_INFO,
					 GTK_BUTTONS_CLOSE,
					 "%s", title);
	gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
						  "%s", msg->str);
	g_signal_connect_swapped (dialog, "response",
				  G_CALLBACK (gtk_widget_destroy),
				  dialog);
	gtk_widget_show (dialog);

	if (!pk_offline_clear_results (NULL, &error)) {
		g_warning ("Failure clearing offline update message: %s",
			   error->message);
	}
}
/**
 * pk_plugin_transaction_finished_end:
 */
void
pk_plugin_transaction_finished_end (PkPlugin *plugin,
				    PkTransaction *transaction)
{
	PkBitfield transaction_flags;
	PkExitEnum exit_enum;
	PkResults *results;
	PkRoleEnum role;

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

	/* skip simulate actions */
	if (pk_bitfield_contain (pk_transaction_get_transaction_flags (transaction),
				 PK_TRANSACTION_FLAG_ENUM_SIMULATE)) {
		goto out;
	}

	/* don't do anything if the method failed */
	results = pk_transaction_get_results (transaction);
	exit_enum = pk_results_get_exit_code (results);
	if (exit_enum != PK_EXIT_ENUM_SUCCESS)
		goto out;

	/* if we're doing UpdatePackages[only-download] then update the
	 * prepared-updates file */
	role = pk_transaction_get_role (transaction);
	transaction_flags = pk_transaction_get_transaction_flags (transaction);
	if (role == PK_ROLE_ENUM_UPDATE_PACKAGES &&
	    pk_bitfield_contain (transaction_flags,
				 PK_TRANSACTION_FLAG_ENUM_ONLY_DOWNLOAD)) {
		pk_plugin_transaction_update_packages (transaction);
		goto out;
	}

	/* if we do get-updates and there's no updates then remove
	 * prepared-updates so the UI doesn't display update & reboot */
	if (role == PK_ROLE_ENUM_GET_UPDATES) {
		pk_plugin_transaction_get_updates (transaction);
		goto out;
	}

	/* delete the prepared updates file as it's no longer valid */
	if (role == PK_ROLE_ENUM_REFRESH_CACHE ||
	    role == PK_ROLE_ENUM_REPO_SET_DATA ||
	    role == PK_ROLE_ENUM_REPO_ENABLE) {
		pk_plugin_state_changed (plugin);
		goto out;
	}

	/* delete the file if the action affected any package already listed in
	 * the prepared updates file */
	if (role == PK_ROLE_ENUM_UPDATE_PACKAGES ||
	    role == PK_ROLE_ENUM_INSTALL_PACKAGES ||
	    role == PK_ROLE_ENUM_REMOVE_PACKAGES) {
		pk_plugin_transaction_action_method (plugin, transaction, results);
	}
out:
	return;
}