~ubuntu-branches/debian/jessie/gnome-shell/jessie

« back to all changes in this revision

Viewing changes to src/shell-js.c

  • Committer: Package Import Robot
  • Author(s): Michael Biebl
  • Date: 2012-05-30 13:19:38 UTC
  • mfrom: (18.1.24 experimental)
  • Revision ID: package-import@ubuntu.com-20120530131938-i3trc1g1p3is2u6x
Tags: 3.4.1-3
Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
 
2
 
 
3
#include "config.h"
 
4
 
 
5
#include "shell-js.h"
 
6
 
 
7
#include <jsapi.h>
 
8
#include <gio/gio.h>
 
9
#include <gjs/gjs.h>
 
10
#include <gjs/gjs-module.h>
 
11
 
 
12
/**
 
13
 * shell_js_add_extension_importer:
 
14
 * @target_object_script: JavaScript code evaluating to a target object
 
15
 * @target_property: Name of property to use for importer
 
16
 * @directory: Source directory:
 
17
 * @error: A #GError
 
18
 *
 
19
 * This function sets a property named @target_property on the object
 
20
 * resulting from the evaluation of @target_object_script code, which
 
21
 * acts as a GJS importer for directory @directory.
 
22
 *
 
23
 * Returns: %TRUE on success
 
24
 */
 
25
gboolean
 
26
shell_js_add_extension_importer (const char  *target_object_script,
 
27
                                 const char  *target_property,
 
28
                                 const char  *directory,
 
29
                                 GError     **error)
 
30
{
 
31
  jsval target_object;
 
32
  GList *contexts;
 
33
  JSContext *context;
 
34
  char *search_path[2] = { 0, 0 };
 
35
  gboolean ret = FALSE;
 
36
 
 
37
  /* Take the first GjsContext from all of them --
 
38
   * we should only ever have one context, so this
 
39
   * should be alright. */
 
40
  contexts = gjs_context_get_all ();
 
41
  context = gjs_context_get_native_context (contexts->data);
 
42
  g_list_free_full (contexts, g_object_unref);
 
43
 
 
44
  JS_BeginRequest (context);
 
45
 
 
46
  /* This is a bit of a hack; ideally we'd be able to pass our target
 
47
   * object directly into this function, but introspection doesn't
 
48
   * support that at the moment.  Instead evaluate a string to get it. */
 
49
  if (!JS_EvaluateScript(context,
 
50
                         JS_GetGlobalObject(context),
 
51
                         target_object_script,
 
52
                         strlen (target_object_script),
 
53
                         "<target_object_script>",
 
54
                         0,
 
55
                         &target_object))
 
56
    {
 
57
      char *message;
 
58
      gjs_log_exception(context,
 
59
                        &message);
 
60
      g_set_error(error,
 
61
                  G_IO_ERROR,
 
62
                  G_IO_ERROR_FAILED,
 
63
                  "%s", message ? message : "(unknown)");
 
64
      g_free(message);
 
65
      goto out;
 
66
    }
 
67
 
 
68
  if (!JSVAL_IS_OBJECT (target_object))
 
69
    {
 
70
      g_error ("shell_js_add_extension_importer: invalid target object");
 
71
      goto out;
 
72
    }
 
73
 
 
74
  search_path[0] = (char*)directory;
 
75
  gjs_define_importer (context, JSVAL_TO_OBJECT (target_object), target_property, (const char **)search_path, FALSE);
 
76
  ret = TRUE;
 
77
 
 
78
 out:
 
79
  JS_EndRequest (context);
 
80
  return ret;
 
81
}
 
82
 
 
83
/**
 
84
 * shell_js_format_int_alternative_output:
 
85
 * @intval:
 
86
 *
 
87
 * Returns: (transfer full):
 
88
 */
 
89
gchar *
 
90
shell_js_format_int_alternative_output (gint intval)
 
91
{
 
92
  return g_strdup_printf ("%Id", intval);
 
93
}