<commit_before>/* realmd -- Realm configuration service
 *
 * Copyright 2012 Red Hat Inc
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 of the licence or (at
 * your option) any later version.
 *
 * See the included COPYING file for more information.
 *
 * Author: <NAME> <<EMAIL>>
 */

#include "config.h"

#include "egg-task.h"
#include "realm-diagnostics.h"
#include "realm-daemon.h"
#include "realm-errors.h"
#include "realm-invocation.h"
#include "realm-options.h"
#include "realm-packages.h"
#include "realm-settings.h"

#include <glib/gi18n.h>

#define I_KNOW_THE_PACKAGEKIT_GLIB2_API_IS_SUBJECT_TO_CHANGE
#include <packagekit-glib2/packagekit.h>

typedef struct {
	PkTask *task;
	GHashTable *check;
	GDBusMethodInvocation *invocation;
	gboolean automatic;
} InstallClosure;

static void
install_closure_free (gpointer data)
{
	InstallClosure *install = data;
	g_object_ref (install->task);
	g_clear_object (&install->invocation);
	if (install->check)
		g_hash_table_destroy (install->check);
	g_free (install);
}

static void
on_install_progress (PkProgress *progress,
                     PkProgressType type,
                     gpointer user_data)
{
	gchar *string;
	guint unumber;
	gint number;

	if (type == PK_PROGRESS_TYPE_STATUS) {
#ifdef TODO
		PkStatusEnum status;
		g_object_get (progress, "status", &status, NULL);
		switch (status) {
		case PK_STATUS_WAIT:
			realm_status (install->invocation, "Waiting for package system");
			break;
		case PK_STATUS_ENUM_WAITING_FOR_AUTH:
			pk_status_enum_to_localised_text ();
		};
#endif
	}

	switch (type) {
	case PK_PROGRESS_TYPE_PACKAGE_ID:
		g_object_get (progress, "package-id", &string, NULL);
		g_debug ("package-id: %s", string);
		g_free (string);
		break;
	case PK_PROGRESS_TYPE_TRANSACTION_ID:
		g_object_get (progress, "transaction-id", &string, NULL);
		g_debug ("transaction-id: %s", string);
		g_free (string);
		break;
	case PK_PROGRESS_TYPE_PERCENTAGE:
		g_object_get (progress, "percentage", &number, NULL);
		g_debug ("percentage: %d", number);
		break;
	case PK_PROGRESS_TYPE_STATUS:
		g_object_get (progress, "status", &unumber, NULL);
		g_debug ("status: %s", pk_status_enum_to_string (unumber));
		break;
	case PK_PROGRESS_TYPE_ELAPSED_TIME:
		g_object_get (progress, "elapsed-time", &unumber, NULL);
		g_debug ("elapsed-time: %u", unumber);
		break;
	case PK_PROGRESS_TYPE_REMAINING_TIME:
		g_object_get (progress, "remaining-time", &unumber, NULL);
		g_debug ("remaining-time: %u", unumber);
		break;
	case PK_PROGRESS_TYPE_SPEED:
		g_object_get (progress, "speed", &unumber, NULL);
		g_debug ("speed: %u", unumber);
		break;
	case PK_PROGRESS_TYPE_INVALID:
	case PK_PROGRESS_TYPE_ALLOW_CANCEL:
	case PK_PROGRESS_TYPE_CALLER_ACTIVE:
	case PK_PROGRESS_TYPE_ROLE:
	case PK_PROGRESS_TYPE_UID:
	case PK_PROGRESS_TYPE_PACKAGE:
	case PK_PROGRESS_TYPE_ITEM_PROGRESS:
	default:
		break;
	}
}

static gchar *
package_names_to_list (GHashTable *packages)
{
	GString *string;
	GHashTableIter iter;
	const gchar *name;

	string = g_string_new ("");
	g_hash_table_iter_init (&iter, packages);
	while (g_hash_table_iter_next (&iter, (void **)&name, NULL)) {
		if (string->len)
			g_string_append (string, ", ");
		g_string_append (string, name);
	}

	return g_string_free (string, FALSE);
}

static gchar **
extract_results (InstallClosure *install,
                 PkResults *results,
                 GHashTable *names,
                 GError **error)
{
	GPtrArray *packages;
	GPtrArray *messages;
	PkPackage *package;
	GPtrArray *ids;
	const gchar *name;
	gchar *missing;
	guint i;

	messages = pk_results_get_message_array (results);
	for (i = 0; i < messages->len; i++) {
		realm_diagnostics_info (install->invocation, "%s",
		                        pk_message_get_details (messages->pdata[i]));
	}
	g_ptr_array_free (messages, TRUE);

	packages = pk_results_get_package_array (results);
	ids = g_ptr_array_new_with_free_func (g_free);

	for (i = 0; i < packages->len; i++) {
		package = PK_PACKAGE (packages->pdata[i]);
		name = pk_package_get_name (package);
		g_hash_table_remove (install->check, name);
		if (pk_package_get_info (package) != PK_INFO_ENUM_INSTALLED) {
			g_ptr_array_add (ids, g_strdup (pk_package_get_id (package)));
			g_hash_table_add (names, g_strdup (name));
		}
	}

	g_ptr_array_free (packages, TRUE);

	if (g_hash_table_size (install->check) == 0) {
		g_ptr_array_add (ids, NULL);
		return (gchar **)g_ptr_array_free (ids, FALSE);

	/* If not all packages were found, then this is an error */
	} else {
		missing = package_names_to_list (install->check);
		g_set_error (error, REALM_ERROR, REALM_ERROR_INTERNAL,
		             _("The following packages are not available for installation: %s"), missing);
		g_free (missing);
		g_ptr_array_free (ids, TRUE);
		return NULL;
	}<commit_msg>realm-packages: Build with PackageKit 0.8.14+ without complaining<commit_after>/* realmd -- Realm configuration service
 *
 * Copyright 2012 Red Hat Inc
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 of the licence or (at
 * your option) any later version.
 *
 * See the included COPYING file for more information.
 *
 * Author: <NAME> <<EMAIL>>
 */

#include "config.h"

#include "egg-task.h"
#include "realm-diagnostics.h"
#include "realm-daemon.h"
#include "realm-errors.h"
#include "realm-invocation.h"
#include "realm-options.h"
#include "realm-packages.h"
#include "realm-settings.h"

#include <glib/gi18n.h>

#define I_KNOW_THE_PACKAGEKIT_GLIB2_API_IS_SUBJECT_TO_CHANGE
#include <packagekit-glib2/packagekit.h>

typedef struct {
	PkTask *task;
	GHashTable *check;
	GDBusMethodInvocation *invocation;
	gboolean automatic;
} InstallClosure;

static void
install_closure_free (gpointer data)
{
	InstallClosure *install = data;
	g_object_ref (install->task);
	g_clear_object (&install->invocation);
	if (install->check)
		g_hash_table_destroy (install->check);
	g_free (install);
}

static void
on_install_progress (PkProgress *progress,
                     PkProgressType type,
                     gpointer user_data)
{
	gchar *string;
	guint unumber;
	gint number;

	if (type == PK_PROGRESS_TYPE_STATUS) {
#ifdef TODO
		PkStatusEnum status;
		g_object_get (progress, "status", &status, NULL);
		switch (status) {
		case PK_STATUS_WAIT:
			realm_status (install->invocation, "Waiting for package system");
			break;
		case PK_STATUS_ENUM_WAITING_FOR_AUTH:
			pk_status_enum_to_localised_text ();
		};
#endif
	}

	switch (type) {
	case PK_PROGRESS_TYPE_PACKAGE_ID:
		g_object_get (progress, "package-id", &string, NULL);
		g_debug ("package-id: %s", string);
		g_free (string);
		break;
	case PK_PROGRESS_TYPE_TRANSACTION_ID:
		g_object_get (progress, "transaction-id", &string, NULL);
		g_debug ("transaction-id: %s", string);
		g_free (string);
		break;
	case PK_PROGRESS_TYPE_PERCENTAGE:
		g_object_get (progress, "percentage", &number, NULL);
		g_debug ("percentage: %d", number);
		break;
	case PK_PROGRESS_TYPE_STATUS:
		g_object_get (progress, "status", &unumber, NULL);
		g_debug ("status: %s", pk_status_enum_to_string (unumber));
		break;
	case PK_PROGRESS_TYPE_ELAPSED_TIME:
		g_object_get (progress, "elapsed-time", &unumber, NULL);
		g_debug ("elapsed-time: %u", unumber);
		break;
	case PK_PROGRESS_TYPE_REMAINING_TIME:
		g_object_get (progress, "remaining-time", &unumber, NULL);
		g_debug ("remaining-time: %u", unumber);
		break;
	case PK_PROGRESS_TYPE_SPEED:
		g_object_get (progress, "speed", &unumber, NULL);
		g_debug ("speed: %u", unumber);
		break;
	case PK_PROGRESS_TYPE_INVALID:
	case PK_PROGRESS_TYPE_ALLOW_CANCEL:
	case PK_PROGRESS_TYPE_CALLER_ACTIVE:
	case PK_PROGRESS_TYPE_ROLE:
	case PK_PROGRESS_TYPE_UID:
	case PK_PROGRESS_TYPE_PACKAGE:
	case PK_PROGRESS_TYPE_ITEM_PROGRESS:
	default:
		break;
	}
}

static gchar *
package_names_to_list (GHashTable *packages)
{
	GString *string;
	GHashTableIter iter;
	const gchar *name;

	string = g_string_new ("");
	g_hash_table_iter_init (&iter, packages);
	while (g_hash_table_iter_next (&iter, (void **)&name, NULL)) {
		if (string->len)
			g_string_append (string, ", ");
		g_string_append (string, name);
	}

	return g_string_free (string, FALSE);
}

static gchar **
extract_results (InstallClosure *install,
                 PkResults *results,
                 GHashTable *names,
                 GError **error)
{
	GPtrArray *packages;
	PkPackage *package;
	GPtrArray *ids;
	const gchar *name;
	gchar *missing;
	guint i;

#if !PK_CHECK_VERSION(0, 8, 13)
	GPtrArray *messages;

	messages = pk_results_get_message_array (results);
	for (i = 0; i < messages->len; i++) {
		realm_diagnostics_info (install->invocation, "%s",
		                        pk_message_get_details (messages->pdata[i]));
	}
	g_ptr_array_free (messages, TRUE);
#endif

	packages = pk_results_get_package_array (results);
	ids = g_ptr_array_new_with_free_func (g_free);

	for (i = 0; i < packages->len; i++) {
		package = PK_PACKAGE (packages->pdata[i]);
		name = pk_package_get_name (package);
		g_hash_table_remove (install->check, name);
		if (pk_package_get_info (package) != PK_INFO_ENUM_INSTALLED) {
			g_ptr_array_add (ids, g_strdup (pk_package_get_id (package)));
			g_hash_table_add (names, g_strdup (name));
		}
	}

	g_ptr_array_free (packages, TRUE);

	if (g_hash_table_size (install->check) == 0) {
		g_ptr_array_add (ids, NULL);
		return (gchar **)g_ptr_array_free (ids, FALSE);

	/* If not all packages were found, then this is an error */
	} else {
		missing = package_names_to_list (install->check);
		g_set_error (error, REALM_ERROR, REALM_ERROR_INTERNAL,
		             _("The following packages are not available for installation: %s"), missing);
		g_free (missing);
		g_ptr_array_free (ids, TRUE);
		return NULL;
	}
{'fraction_non_alphanumeric': 0.11667000200120071, 'fraction_numerical': 0.003201921152691615, 'mean_word_length': 4.653280542986425, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '738906', 'n_tokens_mistral': 3840, 'n_tokens_neox': 3653, 'n_words': 865}