/** * pk_console_resolve_package: **/ gchar * pk_console_resolve_package (PkClient *client, PkBitfield filter, const gchar *package_name, GError **error) { gchar *package_id = NULL; gboolean valid; gchar **tmp; PkResults *results; GPtrArray *array = NULL; guint i; gchar *printable; PkPackage *package; /* have we passed a complete package_id? */ valid = pk_package_id_check (package_name); if (valid) return g_strdup (package_name); /* split */ tmp = g_strsplit (package_name, ",", -1); /* get the list of possibles */ results = pk_client_resolve (client, filter, tmp, NULL, NULL, NULL, error); if (results == NULL) goto out; /* get the packages returned */ array = pk_results_get_package_array (results); if (array == NULL) { *error = g_error_new (1, 0, "did not get package struct for %s", package_name); goto out; } /* nothing found */ if (array->len == 0) { *error = g_error_new (1, 0, "could not find %s", package_name); goto out; } /* just one thing found */ if (array->len == 1) { package = g_ptr_array_index (array, 0); g_object_get (package, "package-id", &package_id, NULL); goto out; } /* TRANSLATORS: more than one package could be found that matched, to follow is a list of possible packages */ g_print ("%s\n", _("More than one package matches:")); for (i=0; i<array->len; i++) { package = g_ptr_array_index (array, i); g_object_get (package, "package-id", &package_id, NULL); printable = pk_package_id_to_printable (package_id); g_print ("%i. %s\n", i+1, printable); g_free (printable); g_free (package_id); } /* TRANSLATORS: This finds out which package in the list to use */ i = pk_console_get_number (_("Please choose the correct package: "), array->len); package = g_ptr_array_index (array, i-1); g_object_get (package, "package-id", &package_id, NULL); out: if (results != NULL) g_object_unref (results); if (array != NULL) g_ptr_array_unref (array); g_strfreev (tmp); return package_id; }
/** * 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; }