/** * pk_package_sack_add_packages_from_file: * @sack: a valid #PkPackageSack instance * @file: a valid package-list file * @error: a %GError to put the error code and message in, or %NULL * * Adds packages from package-list file to a PkPackageSack. * * Return value: %TRUE if there were no errors. * **/ gboolean pk_package_sack_add_packages_from_file (PkPackageSack *sack, GFile *file, GError **error) { GError *error_local = NULL; g_autoptr(GFileInputStream) is = NULL; g_autoptr(GDataInputStream) input = NULL; g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), FALSE); is = g_file_read (file, NULL, &error_local); if (is == NULL) { g_propagate_error (error, error_local); return FALSE; } input = g_data_input_stream_new (G_INPUT_STREAM (is)); /* read package info file line by line */ while (TRUE) { gchar *line; line = g_data_input_stream_read_line (input, NULL, NULL, NULL); if (line == NULL) break; g_strstrip (line); if (!pk_package_sack_add_packages_from_line (sack, line, error)) return FALSE; } return TRUE; }
/** * pk_package_sack_remove_package_by_id: * @sack: a valid #PkPackageSack instance * @package_id: a package_id descriptor * * Removes a package reference from the sack. As soon as one package is removed * the search is stopped. * * Return value: %TRUE if the package was removed from the sack * * Since: 0.5.2 **/ gboolean pk_package_sack_remove_package_by_id (PkPackageSack *sack, const gchar *package_id) { PkPackage *package; const gchar *id; gboolean ret = FALSE; guint i; GPtrArray *array; g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), FALSE); g_return_val_if_fail (package_id != NULL, FALSE); array = sack->priv->array; for (i=0; i<array->len; i++) { package = g_ptr_array_index (array, i); id = pk_package_get_id (package); if (g_strcmp0 (package_id, id) == 0) { g_ptr_array_remove_index (array, i); ret = TRUE; break; } } return ret; }
/** * pk_package_sack_resolve_async: * @sack: a valid #PkPackageSack instance * @cancellable: a #GCancellable or %NULL * @progress_callback: (scope notified): the function to run when the progress changes * @progress_user_data: data to pass to @progress_callback * @callback: the function to run on completion * @user_data: the data to pass to @callback * * Merges in details about packages using resolve. * * Since: 0.5.2 **/ void pk_package_sack_resolve_async (PkPackageSack *sack, GCancellable *cancellable, PkProgressCallback progress_callback, gpointer progress_user_data, GAsyncReadyCallback callback, gpointer user_data) { PkPackageSackState *state; g_autoptr(GSimpleAsyncResult) res = NULL; g_auto(GStrv) package_ids = NULL; g_return_if_fail (PK_IS_PACKAGE_SACK (sack)); g_return_if_fail (callback != NULL); res = g_simple_async_result_new (G_OBJECT (sack), callback, user_data, pk_package_sack_resolve_async); /* save state */ state = g_slice_new0 (PkPackageSackState); state->res = g_object_ref (res); state->sack = g_object_ref (sack); if (cancellable != NULL) state->cancellable = g_object_ref (cancellable); state->ret = FALSE; /* start resolve async */ package_ids = pk_package_sack_get_package_ids (sack); pk_client_resolve_async (sack->priv->client, pk_bitfield_value (PK_FILTER_ENUM_INSTALLED), package_ids, cancellable, progress_callback, progress_user_data, (GAsyncReadyCallback) pk_package_sack_resolve_cb, state); }
/** * pk_package_sack_remove_by_filter: * @sack: a valid #PkPackageSack instance * @filter_cb: (scope call): a #PkPackageSackFilterFunc, which returns %TRUE for the #PkPackage's to retain * @user_data: user data to pass to @filter_cb * * Removes from the package sack any packages that return %FALSE from the filter * function. * * Return value: %TRUE if a package was removed from the sack * * Since: 0.6.3 **/ gboolean pk_package_sack_remove_by_filter (PkPackageSack *sack, PkPackageSackFilterFunc filter_cb, gpointer user_data) { gboolean ret = FALSE; PkPackage *package; gint i; PkPackageSackPrivate *priv = sack->priv; g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), FALSE); g_return_val_if_fail (filter_cb != NULL, FALSE); /* add each that matches the info enum */ for (i = 0; i < (gint) priv->array->len; i++) { package = g_ptr_array_index (priv->array, i); if (!filter_cb (package, user_data)) { ret = TRUE; pk_package_sack_remove_package (sack, package); /* ensure we pick up subsequent matches */ i--; } } return ret; }
/** * pk_package_sack_get_size: * @sack: a valid #PkPackageSack instance * * Gets the number of packages in the sack * * Return value: the number of packages in the sack * * Since: 0.5.2 **/ guint pk_package_sack_get_size (PkPackageSack *sack) { g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), 0); return sack->priv->array->len; }
/** * pk_package_sack_find_by_id_name_arch: * @sack: a valid #PkPackageSack instance * @package_id: a package_id descriptor * * Finds a package in a sack by package name and architecture. As soon as one * package is found the search is stopped. * * Return value: (transfer full): the #PkPackage object, or %NULL if not found. * * Since: 0.8.16 */ PkPackage * pk_package_sack_find_by_id_name_arch (PkPackageSack *sack, const gchar *package_id) { PkPackage *pkg_tmp; guint i; g_auto(GStrv) split = NULL; g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), NULL); g_return_val_if_fail (package_id != NULL, NULL); /* does the package name feature in the array */ split = pk_package_id_split (package_id); if (split == NULL) return NULL; for (i = 0; i < sack->priv->array->len; i++) { pkg_tmp = g_ptr_array_index (sack->priv->array, i); if (g_strcmp0 (pk_package_get_name (pkg_tmp), split[PK_PACKAGE_ID_NAME]) == 0 && g_strcmp0 (pk_package_get_arch (pkg_tmp), split[PK_PACKAGE_ID_ARCH]) == 0) { return g_object_ref (pkg_tmp); } } return NULL; }
/** * pk_package_sack_get_details_async: * @sack: a valid #PkPackageSack instance * @cancellable: a #GCancellable or %NULL * @progress_callback: (scope call): the function to run when the progress changes * @progress_user_data: data to pass to @progress_callback * @callback: the function to run on completion * @user_data: the data to pass to @callback * * Merges in details about packages. **/ void pk_package_sack_get_details_async (PkPackageSack *sack, GCancellable *cancellable, PkProgressCallback progress_callback, gpointer progress_user_data, GAsyncReadyCallback callback, gpointer user_data) { GSimpleAsyncResult *res; PkPackageSackState *state; gchar **package_ids; g_return_if_fail (PK_IS_PACKAGE_SACK (sack)); g_return_if_fail (callback != NULL); res = g_simple_async_result_new (G_OBJECT (sack), callback, user_data, pk_package_sack_get_details_async); /* save state */ state = g_slice_new0 (PkPackageSackState); state->res = g_object_ref (res); state->sack = g_object_ref (sack); if (cancellable != NULL) { state->cancellable = g_object_ref (cancellable); } state->ret = FALSE; /* start details async */ package_ids = pk_package_sack_get_package_ids (sack); pk_client_get_details_async (sack->priv->client, package_ids, cancellable, progress_callback, progress_user_data, (GAsyncReadyCallback) pk_package_sack_get_details_cb, state); g_strfreev (package_ids); g_object_unref (res); }
/** * pk_package_sack_clear: * @sack: a valid #PkPackageSack instance * * Empty all the packages from the sack * * Since: 0.5.2 **/ void pk_package_sack_clear (PkPackageSack *sack) { g_return_if_fail (PK_IS_PACKAGE_SACK (sack)); g_ptr_array_set_size (sack->priv->array, 0); g_hash_table_remove_all (sack->priv->table); }
/** * pk_package_sack_remove_package: * @sack: a valid #PkPackageSack instance * @package: a valid #PkPackage instance * * Removes a package reference from the sack. The pointers have to match exactly. * * Return value: %TRUE if the package was removed from the sack * * Since: 0.5.2 **/ gboolean pk_package_sack_remove_package (PkPackageSack *sack, PkPackage *package) { g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), FALSE); g_return_val_if_fail (PK_IS_PACKAGE (package), FALSE); /* remove from array */ g_hash_table_remove (sack->priv->table, pk_package_get_id (package)); return g_ptr_array_remove (sack->priv->array, package); }
/** * pk_package_sack_add_package: * @sack: a valid #PkPackageSack instance * @package: a valid #PkPackage instance * * Adds a package to the sack. * * Return value: %TRUE if the package was added to the sack * * Since: 0.5.2 **/ gboolean pk_package_sack_add_package (PkPackageSack *sack, PkPackage *package) { g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), FALSE); g_return_val_if_fail (PK_IS_PACKAGE (package), FALSE); /* add to array */ g_ptr_array_add (sack->priv->array, g_object_ref (package)); return TRUE; }
/** * pk_package_sack_sort_package_id: * @sack: a valid #PkPackageSack instance * @type: the type of sorting, e.g. #PK_PACKAGE_SACK_SORT_TYPE_NAME * * Sorts the package sack * * Since: 0.6.1 **/ void pk_package_sack_sort (PkPackageSack *sack, PkPackageSackSortType type) { g_return_if_fail (PK_IS_PACKAGE_SACK (sack)); if (type == PK_PACKAGE_SACK_SORT_TYPE_NAME) g_ptr_array_sort (sack->priv->array, (GCompareFunc) pk_package_sack_sort_compare_name_func); else if (type == PK_PACKAGE_SACK_SORT_TYPE_PACKAGE_ID) g_ptr_array_sort (sack->priv->array, (GCompareFunc) pk_package_sack_sort_compare_package_id_func); else if (type == PK_PACKAGE_SACK_SORT_TYPE_SUMMARY) g_ptr_array_sort (sack->priv->array, (GCompareFunc) pk_package_sack_sort_compare_summary_func); else if (type == PK_PACKAGE_SACK_SORT_TYPE_INFO) g_ptr_array_sort (sack->priv->array, (GCompareFunc) pk_package_sack_sort_compare_info_func); }
/** * pk_package_sack_remove_package: * @sack: a valid #PkPackageSack instance * @package: a valid #PkPackage instance * * Removes a package reference from the sack. The pointers have to match exactly. * * Return value: %TRUE if the package was removed from the sack * * Since: 0.5.2 **/ gboolean pk_package_sack_remove_package (PkPackageSack *sack, PkPackage *package) { gboolean ret; g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), FALSE); g_return_val_if_fail (PK_IS_PACKAGE (package), FALSE); /* remove from array */ ret = g_ptr_array_remove (sack->priv->array, package); return ret; }
/** * pk_package_sack_find_by_id: * @sack: a valid #PkPackageSack instance * @package_id: a package_id descriptor * * Finds a package in a sack from reference. As soon as one package is found * the search is stopped. * * Return value: (transfer full): the #PkPackage object, or %NULL if unfound. Free with g_object_unref() * * Since: 0.5.2 **/ PkPackage * pk_package_sack_find_by_id (PkPackageSack *sack, const gchar *package_id) { PkPackage *package = NULL; g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), NULL); g_return_val_if_fail (package_id != NULL, NULL); package = g_hash_table_lookup (sack->priv->table, package_id); if (package != NULL) g_object_ref (package); return package; }
/** * pk_package_sack_add_package: * @sack: a valid #PkPackageSack instance * @package: a valid #PkPackage instance * * Adds a package to the sack. * * Return value: %TRUE if the package was added to the sack * * Since: 0.5.2 **/ gboolean pk_package_sack_add_package (PkPackageSack *sack, PkPackage *package) { g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), FALSE); g_return_val_if_fail (PK_IS_PACKAGE (package), FALSE); /* add to array */ g_ptr_array_add (sack->priv->array, g_object_ref (package)); g_hash_table_insert (sack->priv->table, g_strdup (pk_package_get_id (package)), g_object_ref (package)); return TRUE; }
/** * pk_package_sack_merge_generic_finish: * @sack: a valid #PkPackageSack instance * @res: the #GAsyncResult * @error: A #GError or %NULL * * Gets the result from the asynchronous function. * * Return value: %TRUE for success * * Since: 0.5.2 **/ gboolean pk_package_sack_merge_generic_finish (PkPackageSack *sack, GAsyncResult *res, GError **error) { GSimpleAsyncResult *simple; g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), FALSE); g_return_val_if_fail (G_IS_SIMPLE_ASYNC_RESULT (res), FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); simple = G_SIMPLE_ASYNC_RESULT (res); if (g_simple_async_result_propagate_error (simple, error)) return FALSE; return g_simple_async_result_get_op_res_gboolean (simple); }
/** * pk_package_sack_add_package_by_id: * @sack: a valid #PkPackageSack instance * @package_id: a package_id descriptor * @error: a %GError to put the error code and message in, or %NULL * * Adds a package reference to the sack. * * Return value: %TRUE if the package was added to the sack * * Since: 0.5.2 **/ gboolean pk_package_sack_add_package_by_id (PkPackageSack *sack, const gchar *package_id, GError **error) { g_autoptr(PkPackage) package = NULL; g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), FALSE); g_return_val_if_fail (package_id != NULL, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); /* create new object */ package = pk_package_new (); if (!pk_package_set_id (package, package_id, error)) return FALSE; /* add to array, array will own object */ pk_package_sack_add_package (sack, package); return TRUE; }
/** * pk_package_sack_add_package_by_id: * @sack: a valid #PkPackageSack instance * @package_id: a package_id descriptor * @error: a %GError to put the error code and message in, or %NULL * * Adds a package reference to the sack. * * Return value: %TRUE if the package was added to the sack * * Since: 0.5.2 **/ gboolean pk_package_sack_add_package_by_id (PkPackageSack *sack, const gchar *package_id, GError **error) { PkPackage *package; gboolean ret; g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), FALSE); g_return_val_if_fail (package_id != NULL, FALSE); g_return_val_if_fail (error == NULL || *error == NULL, FALSE); /* create new object */ package = pk_package_new (); ret = pk_package_set_id (package, package_id, error); if (!ret) goto out; /* add to array, array will own object */ g_ptr_array_add (sack->priv->array, g_object_ref (package)); out: g_object_unref (package); return ret; }
/** * pk_package_sack_get_total_bytes: * @sack: a valid #PkPackageSack instance * * Gets the total size of the package sack in bytes. * * Return value: the size in bytes * * Since: 0.5.2 **/ guint64 pk_package_sack_get_total_bytes (PkPackageSack *sack) { PkPackage *package = NULL; guint i; GPtrArray *array; guint64 bytes = 0; guint64 bytes_tmp = 0; g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), FALSE); array = sack->priv->array; for (i=0; i<array->len; i++) { package = g_ptr_array_index (array, i); g_object_get (package, "size", &bytes_tmp, NULL); bytes += bytes_tmp; } return bytes; }
/** * pk_package_sack_filter: * @sack: a valid #PkPackageSack instance * @filter_cb: (scope call): a #PkPackageSackFilterFunc, which returns %TRUE for the #PkPackage's to add * @user_data: user data to pass to @filter_cb * * Returns a new package sack which only matches packages that return %TRUE * from the filter function. * * Return value: (transfer full): a new #PkPackageSack, free with g_object_unref() * * Since: 0.6.3 **/ PkPackageSack * pk_package_sack_filter (PkPackageSack *sack, PkPackageSackFilterFunc filter_cb, gpointer user_data) { PkPackageSack *results; PkPackage *package; guint i; PkPackageSackPrivate *priv = sack->priv; g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), NULL); g_return_val_if_fail (filter_cb != NULL, NULL); /* create new sack */ results = pk_package_sack_new (); /* add each that matches the info enum */ for (i = 0; i < priv->array->len; i++) { package = g_ptr_array_index (priv->array, i); if (filter_cb (package, user_data)) pk_package_sack_add_package (results, package); } return results; }
/** * pk_package_sack_add_packages_from_line: **/ static void pk_package_sack_add_packages_from_line (PkPackageSack *sack, const gchar *package_str, GError **error) { GError *error_local = NULL; gboolean ret; PkPackage *package; gchar **pdata; PkInfoEnum info; g_return_if_fail (PK_IS_PACKAGE_SACK (sack)); package = pk_package_new (); pdata = g_strsplit (package_str, "\t", -1); if (g_strv_length (pdata) != 3) { g_set_error (error, 1, 0, "invalid package-info line: %s", package_str); goto out; } info = pk_info_enum_from_string (pdata[0]); g_object_set (package, "info", info, "summary", pdata[2], NULL); ret = pk_package_set_id (package, pdata[1], &error_local); if (!ret) { g_set_error (error, 1, 0, "invalid package-id in package-info line: %s", pdata[1]); goto out; } ret = pk_package_sack_add_package (sack, package); if (!ret) g_set_error (error, 1, 0, "could not add package '%s' to package-sack!", pdata[1]); out: g_strfreev (pdata); g_object_unref (package); }
/** * pk_package_sack_find_by_id: * @sack: a valid #PkPackageSack instance * @package_id: a package_id descriptor * * Finds a package in a sack from reference. As soon as one package is found * the search is stopped. * * Return value: (transfer full): the #PkPackage object, or %NULL if unfound. Free with g_object_unref() * * Since: 0.5.2 **/ PkPackage * pk_package_sack_find_by_id (PkPackageSack *sack, const gchar *package_id) { PkPackage *package_tmp; const gchar *id; PkPackage *package = NULL; guint i; guint len; g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), NULL); g_return_val_if_fail (package_id != NULL, NULL); len = sack->priv->array->len; for (i=0; i<len; i++) { package_tmp = g_ptr_array_index (sack->priv->array, i); id = pk_package_get_id (package_tmp); if (g_strcmp0 (package_id, id) == 0) { package = g_object_ref (package_tmp); break; } } return package; }
/** * pk_package_sack_filter_by_info: * @sack: a valid #PkPackageSack instance * @info: a %PkInfoEnum value to match * * Returns a new package sack which only matches packages that match the * specified info enum value. * * Return value: (transfer full): a new #PkPackageSack, free with g_object_unref() * * Since: 0.6.2 **/ PkPackageSack * pk_package_sack_filter_by_info (PkPackageSack *sack, PkInfoEnum info) { PkPackageSack *results; PkPackage *package; PkInfoEnum info_tmp; guint i; PkPackageSackPrivate *priv = sack->priv; g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), NULL); /* create new sack */ results = pk_package_sack_new (); /* add each that matches the info enum */ for (i = 0; i < priv->array->len; i++) { package = g_ptr_array_index (priv->array, i); info_tmp = pk_package_get_info (package); if (info_tmp == info) pk_package_sack_add_package (results, package); } return results; }
/** * pk_package_sack_get_array: * @sack: a valid #PkPackageSack instance * * Gets the package array from the sack * * Return value: (element-type PkPackage) (transfer container): A #GPtrArray, free with g_ptr_array_unref(). * * Since: 0.6.1 **/ GPtrArray * pk_package_sack_get_array (PkPackageSack *sack) { g_return_val_if_fail (PK_IS_PACKAGE_SACK (sack), NULL); return g_ptr_array_ref (sack->priv->array); }