Exemplo n.º 1
0
/**
 * json_gvariant_serialize:
 * @variant: A #GVariant to convert
 *
 * Converts @variant to a JSON tree.
 *
 * Return value: (transfer full): A #JsonNode representing the root of the
 *   JSON data structure obtained from @variant
 *
 * Since: 0.14
 */
JsonNode *
json_gvariant_serialize (GVariant *variant)
{
  JsonNode *json_node = NULL;
  GVariantClass class;

  g_return_val_if_fail (variant != NULL, NULL);

  class = g_variant_classify (variant);

  if (! g_variant_is_container (variant))
    {
      json_node = json_node_new (JSON_NODE_VALUE);

      switch (class)
        {
        case G_VARIANT_CLASS_BOOLEAN:
          json_node_set_boolean (json_node, g_variant_get_boolean (variant));
          break;

        case G_VARIANT_CLASS_BYTE:
          json_node_set_int (json_node, g_variant_get_byte (variant));
          break;
        case G_VARIANT_CLASS_INT16:
          json_node_set_int (json_node, g_variant_get_int16 (variant));
          break;
        case G_VARIANT_CLASS_UINT16:
          json_node_set_int (json_node, g_variant_get_uint16 (variant));
          break;
        case G_VARIANT_CLASS_INT32:
          json_node_set_int (json_node, g_variant_get_int32 (variant));
          break;
        case G_VARIANT_CLASS_UINT32:
          json_node_set_int (json_node, g_variant_get_uint32 (variant));
          break;
        case G_VARIANT_CLASS_INT64:
          json_node_set_int (json_node, g_variant_get_int64 (variant));
          break;
        case G_VARIANT_CLASS_UINT64:
          json_node_set_int (json_node, g_variant_get_uint64 (variant));
          break;
        case G_VARIANT_CLASS_HANDLE:
          json_node_set_int (json_node, g_variant_get_handle (variant));
          break;

        case G_VARIANT_CLASS_DOUBLE:
          json_node_set_double (json_node, g_variant_get_double (variant));
          break;

        case G_VARIANT_CLASS_STRING:
        case G_VARIANT_CLASS_OBJECT_PATH:
        case G_VARIANT_CLASS_SIGNATURE:
          json_node_set_string (json_node, g_variant_get_string (variant, NULL));
          break;

        default:
          break;
        }
    }
Exemplo n.º 2
0
static void
get_properties_cb (GObject      *source_object,
                   GAsyncResult *res,
                   gpointer     user_data)
{
        CMService    *cm_service;
        GVariant     *ret;
        GVariant     *dict;
        GVariant     *value;
        GVariantIter  iter;
        gchar        *key;
        GError       *error = NULL;

        ret = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object),
                                        res,
                                        &error);

        if (error != NULL) {
                if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
                        g_warning ("Error fetching properties: %s", error->message);
                g_error_free (error);

                return;
        }

        if (ret == NULL) {
                g_warning ("Failed fetching properties but no error");

                return;
        }

        if (g_variant_is_container (ret) != TRUE)
        {
                g_warning ("Unexpected result type: %s", g_variant_get_type_string (ret));
		g_variant_unref (ret);

                return;
        }

        cm_service = user_data;

        dict = g_variant_get_child_value (ret, 0);
        g_variant_iter_init (&iter, dict);

        while (g_variant_iter_loop (&iter, "{sv}", &key, &value))
                on_service_property_changed (cm_service, key, value);

        g_variant_unref (dict);
        g_variant_unref (ret);
}
Exemplo n.º 3
0
static void
get_services_cb (GObject      *source_object,
                 GAsyncResult *res,
                 gpointer     user_data)
{
        GUPnPConnmanManager *manager;
        GVariant            *ret;
        GVariant            *services_array;
        GError              *error = NULL;

        ret = g_dbus_proxy_call_finish (G_DBUS_PROXY (source_object),
                                        res,
                                        &error);

        if (error != NULL) {
                if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
                        g_warning ("Error fetching service list: %s", error->message);
                g_error_free (error);

                return;
        }

        if (ret == NULL) {
                g_warning ("Failed fetching list of services but no error");

                return;
        }

        if (g_variant_is_container (ret) != TRUE)
        {
                g_warning ("Wrong format result");
		g_variant_unref (ret);

                return;
        }

        manager = GUPNP_CONNMAN_MANAGER (user_data);
        services_array = g_variant_get_child_value (ret, 0);
        services_array_add (manager, services_array);

        g_variant_unref (services_array);
        g_variant_unref (ret);
}
Exemplo n.º 4
0
/**
 * cockpit_fake_manager_scrape:
 * @self: a fake manager
 * @variant: a variant to scrape
 *
 * Get all object paths out of the variant in question
 * (which is usually the parameters to a signal) and
 * try and poke those object paths.
 */
void
cockpit_fake_manager_scrape (CockpitFakeManager *self,
                             GVariant *variant)
{
  GVariantIter iter;
  GVariant *child;
  const gchar *path;

  if (g_variant_is_of_type (variant, G_VARIANT_TYPE_OBJECT_PATH))
    {
      path = g_variant_get_string (variant, NULL);
      if (!g_str_equal (path, "/"))
        cockpit_fake_manager_poke (self, path);
    }
  else if (g_variant_is_container (variant))
    {
      g_variant_iter_init (&iter, variant);
      while ((child = g_variant_iter_next_value (&iter)) != NULL)
        {
          cockpit_fake_manager_scrape (self, child);
          g_variant_unref (child);
        }
    }
}
Exemplo n.º 5
0
static void
ide_lsp_formatter_apply_changes (IdeLspFormatter *self,
                                 IdeBuffer       *buffer,
                                 GVariant        *text_edits)
{
  g_autoptr(GPtrArray) edits = NULL;
  g_autoptr(IdeContext) context = NULL;
  IdeBufferManager *buffer_manager;
  GVariant *text_edit;
  GFile *file;
  GVariantIter iter;

  IDE_ENTRY;

  g_assert (IDE_IS_LSP_FORMATTER (self));
  g_assert (text_edits != NULL);

  if (!g_variant_is_container (text_edits))
    {
      g_warning ("variant is not a container, ignoring");
      IDE_EXIT;
    }

  file = ide_buffer_get_file (buffer);
  edits = g_ptr_array_new_with_free_func (g_object_unref);

  g_variant_iter_init (&iter, text_edits);

  while (g_variant_iter_loop (&iter, "v", &text_edit))
    {
      g_autoptr(IdeLocation) begin_location = NULL;
      g_autoptr(IdeLocation) end_location = NULL;
      g_autoptr(IdeRange) range = NULL;
      const gchar *new_text = NULL;
      gboolean success;
      struct {
        gint64 line;
        gint64 column;
      } begin, end;

      success = JSONRPC_MESSAGE_PARSE (text_edit,
        "range", "{",
          "start", "{",
            "line", JSONRPC_MESSAGE_GET_INT64 (&begin.line),
            "character", JSONRPC_MESSAGE_GET_INT64 (&begin.column),
          "}",
          "end", "{",
            "line", JSONRPC_MESSAGE_GET_INT64 (&end.line),
            "character", JSONRPC_MESSAGE_GET_INT64 (&end.column),
          "}",
        "}",
        "newText", JSONRPC_MESSAGE_GET_STRING (&new_text)
      );

      if (!success)
        {
          IDE_TRACE_MSG ("Failed to extract change from variant");
          continue;
        }

      begin_location = ide_location_new (file, begin.line, begin.column);
      end_location = ide_location_new (file, end.line, end.column);
      range = ide_range_new (begin_location, end_location);

      g_ptr_array_add (edits, ide_text_edit_new (range, new_text));
    }

  context = ide_buffer_ref_context (buffer);
  buffer_manager = ide_buffer_manager_from_context (context);

  ide_buffer_manager_apply_edits_async (buffer_manager,
                                        IDE_PTR_ARRAY_STEAL_FULL (&edits),
                                        NULL, NULL, NULL);

  IDE_EXIT;
}
Exemplo n.º 6
0
/* Returns a new floating variant (essentially a fixed-up copy of @value) */
static GVariant *
_my_replace (GVariant *value)
{
  GVariant *ret;
  const gchar *dbus_type;

  if (g_variant_is_of_type (value, G_VARIANT_TYPE_VARDICT) &&
      g_variant_lookup (value, "_dbus_type", "&s", &dbus_type))
    {
      GVariant *passed_value;
      passed_value = g_variant_lookup_value (value, "value", NULL);
      if (passed_value != NULL)
        {
          JsonNode *serialized;
          GError *error;

          serialized = json_gvariant_serialize (passed_value);
          error = NULL;
          ret = json_gvariant_deserialize (serialized,
                                           dbus_type,
                                           &error);
          json_node_free (serialized);
          if (ret == NULL)
            {
              /*
               * HACK: Work around bug in JSON-glib, see:
               * https://bugzilla.gnome.org/show_bug.cgi?id=724319
               */
              if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_INVALID_DATA &&
                  g_variant_is_of_type (passed_value, G_VARIANT_TYPE_INT64) &&
                  g_strcmp0 (dbus_type, "d") == 0)
                {
                  ret = g_variant_new_double (g_variant_get_int64 (passed_value));
                  g_clear_error (&error);
                }
              else
                {
                  g_warning ("Error converting JSON to requested type %s: %s (%s, %d)",
                             dbus_type,
                             error->message, g_quark_to_string (error->domain), error->code);
                  g_error_free (error);
                  ret = g_variant_ref (value);
                }
            }
        }
      else
        {
          g_warning ("Malformed _dbus_type vardict");
          ret = g_variant_ref (value);
        }
    }
  else if (g_variant_is_container (value))
    {
      GVariantBuilder builder;
      GVariantIter iter;
      GVariant *child;

      g_variant_builder_init (&builder, g_variant_get_type (value));

      g_variant_iter_init (&iter, value);
      while ((child = g_variant_iter_next_value (&iter)) != NULL)
        {
          g_variant_builder_add_value (&builder, _my_replace (child));
          g_variant_unref (child);
        }
      ret = g_variant_builder_end (&builder);
    }
  else
    {
      ret = g_variant_ref (value);
    }
  return ret;
}