[grilo-plugins] lua-factory: Load resources when available for each script



commit 440a92c1ee85e38b10198d8df469512149a42a8b
Author: Bastien Nocera <hadess hadess net>
Date:   Mon Dec 1 17:11:20 2014 +0100

    lua-factory: Load resources when available for each script
    
    When loading a script called "XXX.lua", we will try to load an
    "XXX.gresource" file from the same directory.
    
    This allows lua sources to have icons, addressed through resource:///
    
    https://bugzilla.gnome.org/show_bug.cgi?id=725147

 src/lua-factory/grl-lua-factory.c |   27 +++++++++++++++++++++++++++
 1 files changed, 27 insertions(+), 0 deletions(-)
---
diff --git a/src/lua-factory/grl-lua-factory.c b/src/lua-factory/grl-lua-factory.c
index 319a45a..c4ff5cd 100644
--- a/src/lua-factory/grl-lua-factory.c
+++ b/src/lua-factory/grl-lua-factory.c
@@ -193,6 +193,24 @@ GRL_PLUGIN_REGISTER (grl_lua_factory_plugin_init, NULL, LUA_FACTORY_PLUGIN_ID);
 
 /* ================== Lua-Factory GObject ================================== */
 
+static GResource *
+load_gresource (const char *script_path)
+{
+  GResource *resource;
+  GString *str;
+  char *resource_path;
+
+  str = g_string_new (NULL);
+  g_string_insert_len (str, -1, script_path, strlen (script_path) - strlen (".lua"));
+  g_string_insert (str, -1, ".gresource");
+  resource_path = g_string_free (str, FALSE);
+
+  resource = g_resource_load (resource_path, NULL);
+  g_free (resource_path);
+
+  return resource;
+}
+
 static GrlLuaFactorySource *
 grl_lua_factory_source_new (gchar *lua_plugin_path,
                             GList *configs)
@@ -208,6 +226,7 @@ grl_lua_factory_source_new (gchar *lua_plugin_path,
   guint auto_split_threshold;
   gchar **source_tags;
   gint ret = 0;
+  GResource *resource;
 
   GRL_DEBUG ("grl_lua_factory_source_new");
 
@@ -226,6 +245,11 @@ grl_lua_factory_source_new (gchar *lua_plugin_path,
   luaL_requiref (L, GRILO_LUA_LIBRARY_NAME, &luaopen_grilo, TRUE);
   lua_pop (L, 1);
 
+  /* Load .gresource file */
+  resource = load_gresource (lua_plugin_path);
+  if (resource)
+    g_resources_register (resource);
+
   /* Load the plugin */
   ret = luaL_loadfile (L, lua_plugin_path);
   if (ret != LUA_OK) {
@@ -261,6 +285,8 @@ grl_lua_factory_source_new (gchar *lua_plugin_path,
   g_clear_pointer (&source_tags, g_strfreev);
   g_clear_object (&source_icon);
 
+  g_object_set_data_full (G_OBJECT (source), "resources", resource, g_object_unref);
+
   ret = lua_plugin_source_operations (L, source->priv->fn);
   if (ret != LUA_OK)
     goto bail;
@@ -302,6 +328,7 @@ bail:
     g_list_free (source->priv->slow_keys);
   }
 
+  g_clear_object (&resource);
   g_free (source_id);
   lua_close (L);
   return NULL;


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