示例#1
0
/**
 * pk_debuginfo_install_packages_install:
 **/
static gboolean
pk_debuginfo_install_packages_install (PkDebuginfoInstallPrivate *priv, GPtrArray *array, GError **error)
{
	gboolean ret = TRUE;
	PkResults *results = NULL;
	gchar **package_ids;
	GError *error_local = NULL;
	PkError *error_code = NULL;

	/* mush back into a char** */
	package_ids = pk_ptr_array_to_strv (array);

	/* TRANSLATORS: we are starting to install the packages */
	pk_progress_bar_start (priv->progress_bar, _("Starting install"));

	/* enable this repo */
	results = pk_task_install_packages_sync (PK_TASK(priv->client), package_ids, NULL,
						 (PkProgressCallback) pk_debuginfo_install_progress_cb, priv, &error_local);
	if (results == NULL) {
		*error = g_error_new (1, 0, "failed to install packages: %s", error_local->message);
		g_error_free (error_local);
		ret = FALSE;
		goto out;
	}

	/* check error code */
	error_code = pk_results_get_error_code (results);
	if (error_code != NULL) {
		*error = g_error_new (1, 0, "failed to resolve: %s, %s", pk_error_enum_to_string (pk_error_get_code (error_code)), pk_error_get_details (error_code));
		ret = FALSE;
		goto out;
	}

	/* end progressbar output */
	pk_progress_bar_end (priv->progress_bar);
out:
	if (error_code != NULL)
		g_object_unref (error_code);
	if (results != NULL)
		g_object_unref (results);
	g_strfreev (package_ids);
	return ret;
}
示例#2
0
/**
 * main:
 **/
int
main (int argc, char *argv[])
{
	gboolean ret;
	PkCnfPolicyConfig *config = NULL;
	guint i;
	guint len;
	gchar *text;
	const gchar *possible;
	gchar **parts;
	guint retval = EXIT_COMMAND_NOT_FOUND;
	_cleanup_ptrarray_unref_ GPtrArray *array = NULL;
	_cleanup_strv_free_ gchar **package_ids = NULL;

	setlocale (LC_ALL, "");
	bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);

#if (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 31)
	if (! g_thread_supported ())
		g_thread_init (NULL);
#endif
#if (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION < 35)
	g_type_init ();
#endif

	/* don't show debugging, unless VERBOSE is specified */
	pk_debug_add_log_domain (G_LOG_DOMAIN);

	/* no input */
	if (argv[1] == NULL)
		goto out;

	/* do stuff on ctrl-c */
	signal (SIGINT, pk_cnf_sigint_handler);

	/* get policy config */
	config = pk_cnf_get_config ();
	task = PK_TASK(pk_task_text_new ());
	g_object_set (task,
		      "cache-age", G_MAXUINT,
		      "interactive", FALSE,
		      "background", FALSE,
		      NULL);
	cancellable = g_cancellable_new ();

	/* get length */
	len = strlen (argv[1]);
	if (len < 1)
		goto out;
	if (argv[1][0] == '.')
		goto out;

	/* TRANSLATORS: the prefix of all the output telling the user
	 * why it's not executing. NOTE: this is lowercase to mimic
	 * the style of bash itself -- apologies */
	g_printerr ("bash: %s: %s...\n", argv[1], _("command not found"));

	/* user is not allowing CNF to do anything useful */
	if (!config->software_source_search &&
	    !config->similar_name_search) {
		goto out;
	}

	/* generate swizzles */
	if (config->similar_name_search)
		array = pk_cnf_find_alternatives (argv[1], len);

	/* one exact possibility */
	if (array != NULL && array->len == 1) {
		possible = g_ptr_array_index (array, 0);
		if (config->single_match == PK_CNF_POLICY_WARN) {
			/* TRANSLATORS: tell the user what we think the command is */
			g_printerr ("%s '%s'\n", _("Similar command is:"), possible);
			goto out;
		}

		/* run */
		if (config->single_match == PK_CNF_POLICY_RUN) {
			retval = pk_cnf_spawn_command (possible, &argv[2]);
			goto out;
		}

		/* ask */
		if (config->single_match == PK_CNF_POLICY_ASK) {
			/* TRANSLATORS: Ask the user if we should run the similar command */
			text = g_strdup_printf ("%s %s", _("Run similar command:"), possible);
			ret = pk_console_get_prompt (text, TRUE);
			if (ret)
				retval = pk_cnf_spawn_command (possible, &argv[2]);
			g_free (text);
		}
		goto out;

	/* multiple choice */
	} else if (array != NULL && array->len > 1) {
		if (config->multiple_match == PK_CNF_POLICY_WARN) {
			/* TRANSLATORS: show the user a list of commands that they could have meant */
			g_printerr ("%s:\n", _("Similar commands are:"));
			for (i = 0; i < array->len; i++) {
				possible = g_ptr_array_index (array, i);
				g_printerr ("'%s'\n", possible);
			}

		/* ask */
		} else if (config->multiple_match == PK_CNF_POLICY_ASK) {
			/* TRANSLATORS: show the user a list of commands we could run */
			g_printerr ("%s:\n", _("Similar commands are:"));
			for (i = 0; i < array->len; i++) {
				possible = g_ptr_array_index (array, i);
				g_printerr ("%i\t'%s'\n", i+1, possible);
			}

			/* TRANSLATORS: ask the user to choose a file to run */
			i = pk_console_get_number (_("Please choose a command to run"), array->len);

			/* run command */
			possible = g_ptr_array_index (array, i);
			retval = pk_cnf_spawn_command (possible, &argv[2]);
		}
		goto out;

	/* only search using PackageKit if configured to do so */
	} else if (config->software_source_search) {
		package_ids = pk_cnf_find_available (argv[1], config->max_search_time);
		if (package_ids == NULL)
			goto out;
		len = g_strv_length (package_ids);
		if (len == 1) {
			parts = pk_package_id_split (package_ids[0]);
			if (config->single_install == PK_CNF_POLICY_WARN) {
				/* TRANSLATORS: tell the user what package provides the command */
				g_printerr ("%s '%s'\n", _("The package providing this file is:"), parts[PK_PACKAGE_ID_NAME]);
				goto out;
			}

			/* ask */
			if (config->single_install == PK_CNF_POLICY_ASK) {
				/* TRANSLATORS: as the user if we want to install a package to provide the command */
				text = g_strdup_printf (_("Install package '%s' to provide command '%s'?"), parts[PK_PACKAGE_ID_NAME], argv[1]);
				ret = pk_console_get_prompt (text, FALSE);
				g_free (text);
				if (ret) {
					ret = pk_cnf_install_package_id (package_ids[0]);
					if (ret)
						retval = pk_cnf_spawn_command (argv[1], &argv[2]);
				}
				g_print ("\n");
				goto out;
			}

			/* install */
			if (config->single_install == PK_CNF_POLICY_INSTALL) {
				ret = pk_cnf_install_package_id (package_ids[0]);
				if (ret)
					retval = pk_cnf_spawn_command (argv[1], &argv[2]);
			}
			g_strfreev (parts);
			goto out;
		} else if (len > 1) {
			if (config->multiple_install == PK_CNF_POLICY_WARN) {
				/* TRANSLATORS: Show the user a list of packages that provide this command */
				g_printerr ("%s\n", _("Packages providing this file are:"));
				for (i = 0; package_ids[i] != NULL; i++) {
					parts = pk_package_id_split (package_ids[i]);
					g_printerr ("'%s'\n", parts[PK_PACKAGE_ID_NAME]);
					g_strfreev (parts);
				}

			/* ask */
			} else if (config->multiple_install == PK_CNF_POLICY_ASK) {
				/* TRANSLATORS: Show the user a list of packages that they can install to provide this command */
				g_printerr ("%s:\n", _("Suitable packages are:"));
				for (i = 0; package_ids[i] != NULL; i++) {
					parts = pk_package_id_split (package_ids[i]);
					g_printerr ("%i\t'%s'\n", i+1, parts[PK_PACKAGE_ID_NAME]);
					g_strfreev (parts);
				}

				/* TRANSLATORS: ask the user to choose a file to install */
				i = pk_console_get_number (_("Please choose a package to install"), len);
				if (i == 0) {
					g_printerr ("%s\n", _("User aborted selection"));
					goto out;
				}

				/* run command */
				ret = pk_cnf_install_package_id (package_ids[i - 1]);
				if (ret)
					retval = pk_cnf_spawn_command (argv[1], &argv[2]);
			}
			goto out;
		}
	}
out:
	if (task != NULL)
		g_object_unref (task);
	if (cancellable != NULL)
		g_object_unref (cancellable);
	if (config != NULL) {
		g_strfreev (config->locations);
		g_free (config);
	}
	return retval;
}