[glade] Plugins: Added GJS plugin for JavaScript objects support



commit a3b4c45dc390b169925b65134eef73cc4fdca2b0
Author: Juan Pablo Ugarte <ugarte endlessm com>
Date:   Sun Jul 31 21:08:52 2016 -0300

    Plugins: Added GJS plugin for JavaScript objects support

 meson.build             |  5 +++
 meson_options.txt       |  1 +
 plugins/gjs/glade-gjs.c | 98 +++++++++++++++++++++++++++++++++++++++++++++++++
 plugins/gjs/meson.build | 12 ++++++
 plugins/meson.build     |  4 ++
 5 files changed, 120 insertions(+)
---
diff --git a/meson.build b/meson.build
index 9e4b0c0c..984b11cd 100644
--- a/meson.build
+++ b/meson.build
@@ -141,6 +141,10 @@ if not cc.links(va_copy_src.format('va_copy', name: 'how to copy va_list'))
   endif
 endif
 
+# GJS support
+gjs_dep = dependency('gjs-1.0', version: '>= 1.64.0', required: get_option('gjs'))
+have_gjs = gjs_dep.found()
+
 # Python for optional python dev libs
 pygobject_version = '3.8.0'
 pygobject_dep = dependency('pygobject-3.0', version: '>= ' + pygobject_version, required: 
get_option('python'))
@@ -212,6 +216,7 @@ output += '\tCompiler:                ' + cc.get_id() + '\n'
 output += '\tSource code location:    ' + source_root + '\n'
 output += '\tDebug Enabled:           ' + glade_debug.to_string() + '\n'
 output += '\tGTK+ UNIX Print Widgets: ' + have_gtk_unix_print.to_string() + '\n'
+output += '\tGJS Widgets support:     ' + have_gjs.to_string() + '\n'
 output += '\tPYTHON Widgets support:  ' + have_python.to_string() + '\n'
 output += '\tGladeui Catalog:         ' + enable_gladeui.to_string() + '\n'
 output += '\tWebKit2GTK+ Catalog:     ' + have_webkit2gtk.to_string() + '\n'
diff --git a/meson_options.txt b/meson_options.txt
index 5d53ee23..cec67b78 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -1,4 +1,5 @@
 option('gladeui', type: 'boolean', value: false, description: 'enable installation of the Gladeui catalog')
+option('gjs', type: 'feature', value: 'auto', description: 'enable installation of GJS (JavaScript) catalog')
 option('python', type: 'feature', value: 'auto', description: 'enable installation of the python catalog')
 option('webkit2gtk', type: 'feature', value: 'auto', description: 'enable installation of the webkit2gtk 
catalog')
 
diff --git a/plugins/gjs/glade-gjs.c b/plugins/gjs/glade-gjs.c
new file mode 100644
index 00000000..7f84eeca
--- /dev/null
+++ b/plugins/gjs/glade-gjs.c
@@ -0,0 +1,98 @@
+/*
+ * Copyright (C) 2016 Endless Mobile Inc.
+ *               2020 Juan Pablo Ugarte.
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ *
+ * Authors:
+ *   Juan Pablo Ugarte <juanpablougarte gmail com>
+ */
+ 
+#include <config.h>
+#include <gjs/gjs.h>
+#include <gladeui/glade.h>
+
+static gboolean
+glade_gjs_setup ()
+{
+  GjsContext  *context;
+  const gchar *path;
+  const GList *l;
+  GArray *paths;
+
+  paths = g_array_new (TRUE, FALSE, sizeof (gchar *));
+
+  /* GLADE_ENV_MODULE_PATH has priority */
+  if ((path = g_getenv (GLADE_ENV_MODULE_PATH)))
+    g_array_append_val (paths, path);
+
+  /* Append modules directory */
+  if ((path = glade_app_get_modules_dir ()))
+    g_array_append_val (paths, path);
+
+  /* Append extra paths (declared in the Preferences) */
+  for (l = glade_catalog_get_extra_paths (); l; l = g_list_next (l))
+    g_array_append_val (paths, l->data);
+
+  /* Create new JS context and set it as default if needed */
+  context = gjs_context_new_with_search_path ((gchar **) paths->data);
+  if (gjs_context_get_current() != context)
+    gjs_context_make_current (context);
+
+  g_object_ref_sink (context);
+
+  g_array_free (paths, TRUE);
+
+  return FALSE;
+}
+
+void
+glade_gjs_init (const gchar *name)
+{
+  gchar *import_sentence, *cname;
+  static gsize init = 0;
+  int exit_status = 0;
+  GError *error = NULL;
+  gboolean retval;
+
+  if (g_once_init_enter (&init))
+    {
+      if (glade_gjs_setup ())
+        return;
+
+      g_once_init_leave (&init, TRUE);
+    }
+
+  cname = g_strdup (name);
+  if (cname[0])
+    cname[0] = g_ascii_toupper (cname[0]);
+
+  /* Yeah, we use the catalog name as the library */
+  import_sentence = g_strdup_printf ("const %s = imports.%s;", cname, name);
+
+  /* Importing the module will create all the GTypes so that glade can use them at runtime */
+  retval = gjs_context_eval (gjs_context_get_current (),
+                             import_sentence, -1, NULL,
+                             &exit_status,
+                             &error);
+  if (!retval && error)
+    {
+      g_warning ("GJS module '%s' import failed: '%s' %s", name, import_sentence, error->message);
+      g_error_free (error);
+    }
+
+  g_free (import_sentence);
+}
+
diff --git a/plugins/gjs/meson.build b/plugins/gjs/meson.build
new file mode 100644
index 00000000..83ac0573
--- /dev/null
+++ b/plugins/gjs/meson.build
@@ -0,0 +1,12 @@
+deps = [
+  libgladeui_dep,
+  gjs_dep,
+]
+
+shared_module(
+  'gladegjs',
+  sources: 'glade-gjs.c',
+  dependencies: deps,
+  c_args: '-DG_LOG_DOMAIN="GladeUI-GJS"',
+  kwargs: plugins_kwargs,
+)
diff --git a/plugins/meson.build b/plugins/meson.build
index 1a7a47eb..88abd5d7 100644
--- a/plugins/meson.build
+++ b/plugins/meson.build
@@ -12,6 +12,10 @@ plugins_kwargs = {
 
 subdir('gtk+')
 
+if have_gjs
+  subdir('gjs')
+endif
+
 if have_python
   subdir('python')
 endif


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