[gnome-software/wip/hughsie/cachekey: 24/28] Allow plugins to subclass GsApp



commit a5cb1717f8775d235735bddf315adfe5ef747e22
Author: Richard Hughes <richard hughsie com>
Date:   Thu Jun 20 11:30:12 2019 +0100

    Allow plugins to subclass GsApp
    
    This allows us make the storing of metadata more efficient, and also allows us
    to use custom _to_string() functions for the types.
    
    NOTE: We have to be a bit careful when creating GsApp objects in modules outside
    of the plugin -- for instance GsAppstream.c needs to use gs_plugin_app_new()
    rather than gs_app_new() so the correct type gets used. Simlarly, plugins need
    to implement gs_plugin_create_app() if they are defining a custom GType.

 lib/gs-plugin-vfuncs.h      | 10 ++++++++++
 lib/gs-plugin.c             | 26 ++++++++++++++++++++++++++
 lib/gs-plugin.h             |  2 ++
 plugins/core/gs-appstream.c |  2 +-
 4 files changed, 39 insertions(+), 1 deletion(-)
---
diff --git a/lib/gs-plugin-vfuncs.h b/lib/gs-plugin-vfuncs.h
index d9527bfa..75f7ee44 100644
--- a/lib/gs-plugin-vfuncs.h
+++ b/lib/gs-plugin-vfuncs.h
@@ -50,6 +50,16 @@ void          gs_plugin_initialize                   (GsPlugin       *plugin);
  **/
 void            gs_plugin_destroy                      (GsPlugin       *plugin);
 
+/**
+ * gs_plugin_create_app:
+ * @plugin: a #GsPlugin
+ * @id: (allow-none): an AppStream ID
+ *
+ * Creates a GsApp, which may possibly be a subclassed type.
+ **/
+GsApp          *gs_plugin_create_app                   (GsPlugin       *plugin,
+                                                        const gchar    *id);
+
 /**
  * gs_plugin_adopt_app:
  * @plugin: a #GsPlugin
diff --git a/lib/gs-plugin.c b/lib/gs-plugin.c
index eab2287a..eeea069f 100644
--- a/lib/gs-plugin.c
+++ b/lib/gs-plugin.c
@@ -45,6 +45,10 @@
 #include "gs-plugin.h"
 #include "gs-utils.h"
 
+typedef GsApp          *(*GsPluginCreateAppFunc)       (GsPlugin       *plugin,
+                                                        const gchar    *id);
+
+
 typedef struct
 {
        GPtrArray               *auth_array;
@@ -376,6 +380,28 @@ gs_plugin_interactive_dec (GsPlugin *plugin)
                gs_plugin_remove_flags (plugin, GS_PLUGIN_FLAGS_INTERACTIVE);
 }
 
+/**
+ * gs_plugin_app_new:
+ * @plugin: a #GsPlugin
+ * @id: an application ID
+ *
+ * Creates a GsApp, which may possibly be a subclassed type.
+ * To set a custom type used when creating objects, plugins can use the
+ * gs_plugin_create_app() vfunc.
+ *
+ * Returns: (transfer full): a #GsApp
+ *
+ * Since: 3.34
+ **/
+GsApp *
+gs_plugin_app_new (GsPlugin *plugin, const gchar *id)
+{
+       GsPluginCreateAppFunc func = gs_plugin_get_symbol (plugin, "gs_plugin_create_app");
+       if (func != NULL)
+               return func (plugin, id);
+       return gs_app_new (id);
+}
+
 /**
  * gs_plugin_get_name:
  * @plugin: a #GsPlugin
diff --git a/lib/gs-plugin.h b/lib/gs-plugin.h
index d4595186..3cec1e68 100644
--- a/lib/gs-plugin.h
+++ b/lib/gs-plugin.h
@@ -59,6 +59,8 @@ void           gs_plugin_set_appstream_id             (GsPlugin       *plugin,
 gboolean        gs_plugin_get_enabled                  (GsPlugin       *plugin);
 void            gs_plugin_set_enabled                  (GsPlugin       *plugin,
                                                         gboolean        enabled);
+GsApp          *gs_plugin_app_new                      (GsPlugin       *plugin,
+                                                        const gchar    *id);
 gboolean        gs_plugin_has_flags                    (GsPlugin       *plugin,
                                                         GsPluginFlags   flags);
 void            gs_plugin_add_flags                    (GsPlugin       *plugin,
diff --git a/plugins/core/gs-appstream.c b/plugins/core/gs-appstream.c
index b6f60f9c..39999437 100644
--- a/plugins/core/gs-appstream.c
+++ b/plugins/core/gs-appstream.c
@@ -18,7 +18,7 @@ GsApp *
 gs_appstream_create_app (GsPlugin *plugin, XbSilo *silo, XbNode *component, GError **error)
 {
        GsApp *app;
-       g_autoptr(GsApp) app_new = gs_app_new (NULL);
+       g_autoptr(GsApp) app_new = gs_plugin_app_new (plugin, NULL);
 
        /* refine enough to get the unique ID */
        if (!gs_appstream_refine_app (plugin, app_new, silo, component,


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