[gnome-software/wip/hughsie/cachekey2: 1/3] Allow plugins to subclass GsApp



commit 42fcea399b2dd189e33c2be01fc4e755744b25a3
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 1d406d99..3431a9d6 100644
--- a/lib/gs-plugin-vfuncs.h
+++ b/lib/gs-plugin-vfuncs.h
@@ -49,6 +49,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 4764ce7b..bc84837f 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
 {
        GHashTable              *cache;
@@ -373,6 +377,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 7dd2d864..2a52bc90 100644
--- a/lib/gs-plugin.h
+++ b/lib/gs-plugin.h
@@ -58,6 +58,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 2999ab44..3431e300 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]