[gnome-software/1649-support-appstream-merging] gs-plugin-appstream: Use gs_appstream_load_desktop_files()



commit acee1aa420827d61eb722704a944d586147e4f68
Author: Milan Crha <mcrha redhat com>
Date:   Wed Mar 30 10:27:40 2022 +0200

    gs-plugin-appstream: Use gs_appstream_load_desktop_files()
    
    To avoid code duplication.

 plugins/core/gs-plugin-appstream.c | 118 ++-----------------------------------
 1 file changed, 6 insertions(+), 112 deletions(-)
---
diff --git a/plugins/core/gs-plugin-appstream.c b/plugins/core/gs-plugin-appstream.c
index cec8160e3..b73bb9194 100644
--- a/plugins/core/gs-plugin-appstream.c
+++ b/plugins/core/gs-plugin-appstream.c
@@ -284,112 +284,6 @@ gs_plugin_appstream_load_appdata (GsPluginAppstream  *self,
        return TRUE;
 }
 
-static GInputStream *
-gs_plugin_appstream_load_desktop_cb (XbBuilderSource *self,
-                                    XbBuilderSourceCtx *ctx,
-                                    gpointer user_data,
-                                    GCancellable *cancellable,
-                                    GError **error)
-{
-       g_autofree gchar *xml = NULL;
-       g_autoptr(AsComponent) cpt = as_component_new ();
-       g_autoptr(AsContext) actx = as_context_new ();
-       g_autoptr(GBytes) bytes = NULL;
-       gboolean ret;
-
-       bytes = xb_builder_source_ctx_get_bytes (ctx, cancellable, error);
-       if (bytes == NULL)
-               return NULL;
-
-       as_component_set_id (cpt, xb_builder_source_ctx_get_filename (ctx));
-       ret = as_component_load_from_bytes (cpt,
-                                          actx,
-                                          AS_FORMAT_KIND_DESKTOP_ENTRY,
-                                          bytes,
-                                          error);
-       if (!ret)
-               return NULL;
-       xml = as_component_to_xml_data (cpt, actx, error);
-       if (xml == NULL)
-               return NULL;
-       return g_memory_input_stream_new_from_data (g_steal_pointer (&xml), (gssize) -1, g_free);
-}
-
-static gboolean
-gs_plugin_appstream_load_desktop_fn (GsPluginAppstream  *self,
-                                     XbBuilder          *builder,
-                                     const gchar        *filename,
-                                     GCancellable       *cancellable,
-                                     GError            **error)
-{
-       g_autoptr(GFile) file = g_file_new_for_path (filename);
-       g_autoptr(XbBuilderNode) info = NULL;
-       g_autoptr(XbBuilderSource) source = xb_builder_source_new ();
-
-       /* add support for desktop files */
-       xb_builder_source_add_adapter (source, "application/x-desktop",
-                                      gs_plugin_appstream_load_desktop_cb, NULL, NULL);
-
-       /* add source */
-       if (!xb_builder_source_load_file (source, file,
-#if LIBXMLB_CHECK_VERSION(0, 2, 0)
-                                         XB_BUILDER_SOURCE_FLAG_WATCH_DIRECTORY,
-#else
-                                         XB_BUILDER_SOURCE_FLAG_WATCH_FILE,
-#endif
-                                         cancellable,
-                                         error)) {
-               return FALSE;
-       }
-
-       /* add metadata */
-       info = xb_builder_node_insert (NULL, "info", NULL);
-       xb_builder_node_insert_text (info, "filename", filename, NULL);
-       xb_builder_source_set_info (source, info);
-
-       /* success */
-       xb_builder_import_source (builder, source);
-       return TRUE;
-}
-
-static gboolean
-gs_plugin_appstream_load_desktop (GsPluginAppstream  *self,
-                                  XbBuilder          *builder,
-                                  const gchar        *path,
-                                  GCancellable       *cancellable,
-                                  GError            **error)
-{
-       const gchar *fn;
-       g_autoptr(GDir) dir = NULL;
-       g_autoptr(GFile) parent = g_file_new_for_path (path);
-       if (!g_file_query_exists (parent, cancellable))
-               return TRUE;
-
-       dir = g_dir_open (path, 0, error);
-       if (dir == NULL)
-               return FALSE;
-
-       while ((fn = g_dir_read_name (dir)) != NULL) {
-               if (g_str_has_suffix (fn, ".desktop")) {
-                       g_autofree gchar *filename = g_build_filename (path, fn, NULL);
-                       g_autoptr(GError) error_local = NULL;
-                       if (g_strcmp0 (fn, "mimeinfo.cache") == 0)
-                               continue;
-                       if (!gs_plugin_appstream_load_desktop_fn (self,
-                                                                 builder,
-                                                                 filename,
-                                                                 cancellable,
-                                                                 &error_local)) {
-                               g_debug ("ignoring %s: %s", filename, error_local->message);
-                               continue;
-                       }
-               }
-       }
-
-       /* success */
-       return TRUE;
-}
-
 static GInputStream *
 gs_plugin_appstream_load_dep11_cb (XbBuilderSource *self,
                                   XbBuilderSourceCtx *ctx,
@@ -780,15 +674,15 @@ gs_plugin_appstream_check_silo (GsPluginAppstream  *self,
                                                               cancellable, error))
                                return FALSE;
                }
-               if (!gs_plugin_appstream_load_desktop (self, builder,
-                                                      DATADIR "/applications",
-                                                      cancellable, error)) {
+               if (!gs_appstream_load_desktop_files (builder,
+                                                     DATADIR "/applications",
+                                                     cancellable, error)) {
                        return FALSE;
                }
                if (g_strcmp0 (DATADIR, "/usr/share") != 0 &&
-                   !gs_plugin_appstream_load_desktop (self, builder,
-                                                      "/usr/share/applications",
-                                                      cancellable, error)) {
+                   !gs_appstream_load_desktop_files (builder,
+                                                     "/usr/share/applications",
+                                                     cancellable, error)) {
                        return FALSE;
                }
        }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]