/** * as_utils_get_component_bundle_kind: * * Check which bundling system the component uses. */ AsBundleKind as_utils_get_component_bundle_kind (AsComponent *cpt) { GPtrArray *bundles; AsBundleKind bundle_kind; /* determine bundle - what should we do if there are multiple bundles of different types * defined for one component? */ bundle_kind = AS_BUNDLE_KIND_PACKAGE; bundles = as_component_get_bundles (cpt); if (bundles->len > 0) bundle_kind = as_bundle_get_kind (AS_BUNDLE (g_ptr_array_index (bundles, 0))); return bundle_kind; }
/** * as_pool_add_component: * @pool: An instance of #AsPool * @cpt: The #AsComponent to add to the pool. * @error: A #GError or %NULL * * Register a new component in the AppStream metadata pool. * * Returns: %TRUE if the new component was successfully added to the pool. */ gboolean as_pool_add_component (AsPool *pool, AsComponent *cpt, GError **error) { const gchar *cdid = NULL; AsComponent *existing_cpt; gint pool_priority; AsPoolPrivate *priv = GET_PRIVATE (pool); cdid = as_component_get_data_id (cpt); existing_cpt = g_hash_table_lookup (priv->cpt_table, cdid); /* add additional data to the component, e.g. external screenshots. Also refines * the component's icon paths */ as_component_complete (cpt, priv->screenshot_service_url, priv->icon_dirs); if (existing_cpt == NULL) { g_hash_table_insert (priv->cpt_table, g_strdup (cdid), g_object_ref (cpt)); return TRUE; } /* perform metadata merges if necessary */ if (as_component_get_merge_kind (cpt) != AS_MERGE_KIND_NONE) { g_autoptr(GPtrArray) matches = NULL; guint i; /* we merge the data into all components with matching IDs at time */ matches = as_pool_get_components_by_id (pool, as_component_get_id (cpt)); for (i = 0; i < matches->len; i++) { AsComponent *match = AS_COMPONENT (g_ptr_array_index (matches, i)); as_merge_components (match, cpt); } return TRUE; } /* if we are here, we might have duplicates and no merges, so check if we should replace a component * with data of higher priority, or if we have an actual error in the metadata */ pool_priority = as_component_get_priority (existing_cpt); if (pool_priority < as_component_get_priority (cpt)) { g_hash_table_replace (priv->cpt_table, g_strdup (cdid), g_object_ref (cpt)); g_debug ("Replaced '%s' with data of higher priority.", cdid); } else { /* bundles are treated specially here */ if ((!as_component_has_bundle (existing_cpt)) && (as_component_has_bundle (cpt))) { GPtrArray *bundles; /* propagate bundle information to existing component */ bundles = as_component_get_bundles (cpt); as_component_set_bundles_array (existing_cpt, bundles); return TRUE; } /* experimental multiarch support */ if (as_component_get_architecture (cpt) != NULL) { if (as_arch_compatible (as_component_get_architecture (cpt), priv->current_arch)) { const gchar *earch; /* this component is compatible with our current architecture */ earch = as_component_get_architecture (existing_cpt); if (earch != NULL) { if (as_arch_compatible (earch, priv->current_arch)) { g_hash_table_replace (priv->cpt_table, g_strdup (cdid), g_object_ref (cpt)); g_debug ("Preferred component for native architecture for %s (was %s)", cdid, earch); return TRUE; } else { g_debug ("Ignored additional entry for '%s' on architecture %s.", cdid, earch); return FALSE; } } } } if (pool_priority == as_component_get_priority (cpt)) { g_set_error (error, AS_POOL_ERROR, AS_POOL_ERROR_COLLISION, "Detected colliding ids: %s was already added with the same priority.", cdid); return FALSE; } else { g_set_error (error, AS_POOL_ERROR, AS_POOL_ERROR_COLLISION, "Detected colliding ids: %s was already added with a higher priority.", cdid); return FALSE; } } return TRUE; }
/** * as_merge_components: * * Merge selected data from two components. */ static void as_merge_components (AsComponent *dest_cpt, AsComponent *src_cpt) { guint i; AsMergeKind merge_kind; merge_kind = as_component_get_merge_kind (src_cpt); g_return_if_fail (merge_kind != AS_MERGE_KIND_NONE); /* FIXME: We need to merge more attributes */ /* merge stuff in append mode */ if (merge_kind == AS_MERGE_KIND_APPEND) { GPtrArray *suggestions; GPtrArray *cats; /* merge categories */ cats = as_component_get_categories (src_cpt); if (cats->len > 0) { g_autoptr(GHashTable) cat_table = NULL; GPtrArray *dest_categories; cat_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL); for (i = 0; i < cats->len; i++) { const gchar *cat = (const gchar*) g_ptr_array_index (cats, i); g_hash_table_add (cat_table, g_strdup (cat)); } dest_categories = as_component_get_categories (dest_cpt); if (dest_categories->len > 0) { for (i = 0; i < dest_categories->len; i++) { const gchar *cat = (const gchar*) g_ptr_array_index (dest_categories, i); g_hash_table_add (cat_table, g_strdup (cat)); } } g_ptr_array_set_size (dest_categories, 0); as_hash_table_string_keys_to_array (cat_table, dest_categories); } /* merge suggestions */ suggestions = as_component_get_suggested (src_cpt); if (suggestions != NULL) { for (i = 0; i < suggestions->len; i++) { as_component_add_suggested (dest_cpt, AS_SUGGESTED (g_ptr_array_index (suggestions, i))); } } } /* merge stuff in replace mode */ if (merge_kind == AS_MERGE_KIND_REPLACE) { gchar **pkgnames; /* merge names */ if (as_component_get_name (src_cpt) != NULL) as_component_set_name (dest_cpt, as_component_get_name (src_cpt), as_component_get_active_locale (src_cpt)); /* merge package names */ pkgnames = as_component_get_pkgnames (src_cpt); if ((pkgnames != NULL) && (pkgnames[0] != '\0')) as_component_set_pkgnames (dest_cpt, as_component_get_pkgnames (src_cpt)); /* merge bundles */ if (as_component_has_bundle (src_cpt)) as_component_set_bundles_array (dest_cpt, as_component_get_bundles (src_cpt)); } g_debug ("Merged data for '%s'", as_component_get_data_id (dest_cpt)); }
/** * as_pool_add_component_internal: * @pool: An instance of #AsPool * @cpt: The #AsComponent to add to the pool. * @pedantic_noadd: If %TRUE, always emit an error if component couldn't be added. * @error: A #GError or %NULL * * Internal. */ static gboolean as_pool_add_component_internal (AsPool *pool, AsComponent *cpt, gboolean pedantic_noadd, GError **error) { const gchar *cdid = NULL; AsComponent *existing_cpt; gint pool_priority; AsPoolPrivate *priv = GET_PRIVATE (pool); cdid = as_component_get_data_id (cpt); if (as_component_is_ignored (cpt)) { if (pedantic_noadd) g_set_error (error, AS_POOL_ERROR, AS_POOL_ERROR_FAILED, "Skipping '%s' from inclusion into the pool: Component is ignored.", cdid); return FALSE; } existing_cpt = g_hash_table_lookup (priv->cpt_table, cdid); if (as_component_get_origin_kind (cpt) == AS_ORIGIN_KIND_DESKTOP_ENTRY) { g_autofree gchar *tmp_cdid = NULL; /* .desktop entries might map to existing metadata data with or without .desktop suffix, we need to check for that. * (the .desktop suffix is optional for desktop-application metainfo files, and the desktop-entry parser will automatically * omit it if the desktop-entry-id is following the reverse DNS scheme) */ if (existing_cpt == NULL) { tmp_cdid = g_strdup_printf ("%s.desktop", cdid); existing_cpt = g_hash_table_lookup (priv->cpt_table, tmp_cdid); } } if (existing_cpt == NULL) { /* add additional data to the component, e.g. external screenshots. Also refines * the component's icon paths */ as_component_complete (cpt, priv->screenshot_service_url, priv->icon_dirs); g_hash_table_insert (priv->cpt_table, g_strdup (cdid), g_object_ref (cpt)); return TRUE; } /* perform metadata merges if necessary */ if (as_component_get_merge_kind (cpt) != AS_MERGE_KIND_NONE) { g_autoptr(GPtrArray) matches = NULL; guint i; /* we merge the data into all components with matching IDs at time */ matches = as_pool_get_components_by_id (pool, as_component_get_id (cpt)); for (i = 0; i < matches->len; i++) { AsComponent *match = AS_COMPONENT (g_ptr_array_index (matches, i)); as_merge_components (match, cpt); } return TRUE; } /* if we are here, we might have duplicates and no merges, so check if we should replace a component * with data of higher priority, or if we have an actual error in the metadata */ pool_priority = as_component_get_priority (existing_cpt); if (pool_priority < as_component_get_priority (cpt)) { g_hash_table_replace (priv->cpt_table, g_strdup (cdid), g_object_ref (cpt)); g_debug ("Replaced '%s' with data of higher priority.", cdid); } else { /* bundles are treated specially here */ if ((!as_component_has_bundle (existing_cpt)) && (as_component_has_bundle (cpt))) { GPtrArray *bundles; /* propagate bundle information to existing component */ bundles = as_component_get_bundles (cpt); as_component_set_bundles_array (existing_cpt, bundles); return TRUE; } /* experimental multiarch support */ if (as_component_get_architecture (cpt) != NULL) { if (as_arch_compatible (as_component_get_architecture (cpt), priv->current_arch)) { const gchar *earch; /* this component is compatible with our current architecture */ earch = as_component_get_architecture (existing_cpt); if (earch != NULL) { if (as_arch_compatible (earch, priv->current_arch)) { g_hash_table_replace (priv->cpt_table, g_strdup (cdid), g_object_ref (cpt)); g_debug ("Preferred component for native architecture for %s (was %s)", cdid, earch); return TRUE; } else { g_debug ("Ignored additional entry for '%s' on architecture %s.", cdid, earch); return FALSE; } } } } if (pool_priority == as_component_get_priority (cpt)) { g_set_error (error, AS_POOL_ERROR, AS_POOL_ERROR_COLLISION, "Detected colliding ids: %s was already added with the same priority.", cdid); return FALSE; } else { if (pedantic_noadd) g_set_error (error, AS_POOL_ERROR, AS_POOL_ERROR_COLLISION, "Detected colliding ids: %s was already added with a higher priority.", cdid); return FALSE; } } return TRUE; }