[grilo-plugins] grl-lua-library: Allow lua plugins to register keys by itself



commit b2ff70cc031d0cdbb1d309b00ffb47ae3cad1987
Author: 1PunMan <saurabhsingh412 gmail com>
Date:   Mon May 28 20:56:43 2018 +0530

    grl-lua-library: Allow lua plugins to register keys by itself
    
    Currently lua plugins have no way of registering keys by themselves,
    to use a new key it has to be added to system keys. This commit allows
    lua plugins to register app-specific keys by itself by using
    grl_data_set_for_id() and grl_data_add_for_id() introduced by Grilo in
    0.3.6 release.
    
    https://gitlab.gnome.org/GNOME/grilo/issues/1

 configure.ac                      |  3 +-
 meson.build                       |  4 +-
 src/lua-factory/grl-lua-library.c | 82 ++++++++++++++++++++++++++++++++++++++-
 3 files changed, 86 insertions(+), 3 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index e03c992..6f7e6e0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -91,11 +91,12 @@ fi
 # ----------------------------------------------------------
 
 GLIB2_REQUIRED=2.44
+GRILO_REQUIRED=0.3.6
 
 PKG_CHECK_MODULES([DEPS], glib-2.0 >= $GLIB2_REQUIRED \
                           gobject-2.0 >= $GLIB2_REQUIRED \
                           gmodule-2.0 >= $GLIB2_REQUIRED \
-                          ${GRL_NAME} >= 0.3.1)
+                          ${GRL_NAME} >= $GRILO_REQUIRED)
 
 GLIB_COMPILE_RESOURCES=`$PKG_CONFIG --variable glib_compile_resources gio-2.0`
 AC_SUBST(GLIB_COMPILE_RESOURCES)
diff --git a/meson.build b/meson.build
index b1ddace..d15f50c 100644
--- a/meson.build
+++ b/meson.build
@@ -27,12 +27,14 @@ datadir = get_option('datadir')
 pluginsdir =join_paths(libdir, 'grilo-@0@.@1@'.format(grl_major, grl_minor))
 
 glib2_required = '2.44'
+grilo_required = '0.3.6'
 glib2_required_info = '>= @0@'.format(glib2_required)
+grilo_required_info = '>= @0@'.format(grilo_required)
 
 glib_dep = dependency('glib-2.0', version: glib2_required_info, required: true)
 gmodule_dep = dependency('gmodule-2.0', version: glib2_required_info, required: true)
 gobject_dep = dependency('gobject-2.0', version: glib2_required_info, required: true)
-grilo_dep = dependency('grilo-0.3', version: '>= 0.3.1', required: true)
+grilo_dep = dependency('grilo-0.3', version: grilo_required_info, required: true)
 
 avahi_client_dep = dependency('avahi-client', required: false)
 avahi_glib_dep = dependency('avahi-glib', required: false)
diff --git a/src/lua-factory/grl-lua-library.c b/src/lua-factory/grl-lua-library.c
index 4020857..ef660bb 100644
--- a/src/lua-factory/grl-lua-library.c
+++ b/src/lua-factory/grl-lua-library.c
@@ -268,6 +268,73 @@ verify_plaintext_fetch (lua_State  *L,
   return TRUE;
 }
 
+static gboolean
+grl_util_init_value (lua_State *L,
+                     GValue *value,
+                     const gchar *key_name)
+{
+  if (lua_isinteger (L, -1)) {
+    g_value_init (value, G_TYPE_INT64);
+    g_value_set_int64 (value, lua_tointeger (L, -1));
+  } else if (lua_isstring (L, -1)) {
+    g_value_init (value, G_TYPE_STRING);
+    g_value_set_string (value, lua_tostring (L, -1));
+  } else if (lua_isnumber (L, -1)) {
+    g_value_init (value, G_TYPE_FLOAT);
+    g_value_set_float (value, lua_tonumber (L, -1));
+  } else if (lua_isboolean (L, -1)) {
+    g_value_init (value, G_TYPE_BOOLEAN);
+    g_value_set_boolean (value, lua_toboolean (L, -1));
+  } else {
+    GDateTime *date;
+    const char *date_str = lua_tostring (L, -1);
+    date = grl_date_time_from_iso8601 (date_str);
+    /* Try a number of seconds since Epoch */
+    if (date == NULL) {
+      gint64 date_int = g_ascii_strtoll (date_str, NULL, 0);
+      if (date_int != 0) {
+        date = g_date_time_new_from_unix_utc (date_int);
+      }
+    }
+    if (date != NULL) {
+      g_value_init (value, G_TYPE_DATE_TIME);
+      g_value_set_boxed (value, date);
+      g_date_time_unref (date);
+    } else {
+      GRL_WARNING ("'%s' is being ignored as '%s' is not being handled.",
+                   key_name, lua_typename (L, -1));
+      return FALSE;
+    }
+  }
+  return TRUE;
+}
+
+/* Top of the stack must be a table */
+static void
+grl_util_add_table_to_unregistered_media (lua_State *L,
+                                          GrlMedia *media,
+                                          const gchar *key_name)
+{
+  GValue value = { 0, };
+  gint i;
+  gint array_len = luaL_len (L, -1);
+  GRL_DEBUG ("Adding %u elements under %s metadata-key", array_len, key_name);
+
+  /* Insert new values */
+  for (i = 0; i < array_len; i++) {
+    lua_pushinteger (L, i + 1);
+    lua_gettable (L, -2);
+    grl_util_init_value (L, &value, key_name);
+
+    if (!grl_data_add_for_id (GRL_DATA (media), key_name, &value)) {
+      GRL_DEBUG("Failed to add element at index %d to %s", i+1, key_name);
+    }
+
+    g_value_unset (&value);
+    lua_pop (L, 1);
+  }
+}
+
 /* Top of the stack must be a table */
 static void
 grl_util_add_table_to_media (lua_State *L,
@@ -327,6 +394,8 @@ grl_util_build_media (lua_State *L,
 {
   GrlRegistry *registry;
   GrlMedia *media = user_media;
+  GValue value = { 0, };
+  gboolean success;
 
   if (!lua_istable (L, 1)) {
     if (!lua_isnil (L, 1))
@@ -457,7 +526,18 @@ grl_util_build_media (lua_State *L,
         }
       }
     } else {
-      GRL_WARNING ("'%s' is not a valid keyword", key_name);
+      GRL_DEBUG ("'%s' isn't a registered key.", key_name);
+      if (lua_istable (L, -1)){
+        grl_util_add_table_to_unregistered_media (L, media, key_name);
+      } else {
+        success = grl_util_init_value (L, &value, key_name);
+        if (success) {
+          success = grl_data_set_for_id (GRL_DATA (media), key_name, &value);
+          g_value_unset (&value);
+        }
+        if (!success)
+          GRL_DEBUG ("Value for %s was not set", key_name);
+      }
     }
 
 next_key:


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