gdouble torrent_get_seed_ratio_limit(JsonObject * t) { return json_node_really_get_double(json_object_get_member (t, FIELD_SEED_RATIO_LIMIT)); }
static void on_socket_connect (GObject *object, GAsyncResult *result, gpointer user_data) { CockpitWebSocketStream *self = COCKPIT_WEB_SOCKET_STREAM (user_data); CockpitChannel *channel = COCKPIT_CHANNEL (self); const gchar *problem = "protocol-error"; gchar **protocols = NULL; GList *l, *names = NULL; GError *error = NULL; JsonObject *options; JsonObject *headers; const gchar *value; JsonNode *node; GIOStream *io; io = cockpit_connect_stream_finish (result, &error); if (error) { problem = cockpit_stream_problem (error, self->origin, "couldn't connect", NULL); goto out; } options = cockpit_channel_get_options (channel); if (!cockpit_json_get_strv (options, "protocols", NULL, &protocols)) { g_warning ("%s: invalid \"protocol\" value in WebSocket stream request", self->origin); goto out; } self->client = web_socket_client_new_for_stream (self->url, self->origin, (const gchar **)protocols, io); node = json_object_get_member (options, "headers"); if (node) { if (!JSON_NODE_HOLDS_OBJECT (node)) { g_warning ("%s: invalid \"headers\" field in WebSocket stream request", self->origin); goto out; } headers = json_node_get_object (node); names = json_object_get_members (headers); for (l = names; l != NULL; l = g_list_next (l)) { node = json_object_get_member (headers, l->data); if (!node || !JSON_NODE_HOLDS_VALUE (node) || json_node_get_value_type (node) != G_TYPE_STRING) { g_warning ("%s: invalid header value in WebSocket stream request: %s", self->origin, (gchar *)l->data); goto out; } value = json_node_get_string (node); g_debug ("%s: sending header: %s %s", self->origin, (gchar *)l->data, value); web_socket_client_include_header (WEB_SOCKET_CLIENT (self->client), l->data, value); } } self->sig_open = g_signal_connect (self->client, "open", G_CALLBACK (on_web_socket_open), self); self->sig_message = g_signal_connect (self->client, "message", G_CALLBACK (on_web_socket_message), self); self->sig_closing = g_signal_connect (self->client, "closing", G_CALLBACK (on_web_socket_closing), self); self->sig_close = g_signal_connect (self->client, "close", G_CALLBACK (on_web_socket_close), self); self->sig_error = g_signal_connect (self->client, "error", G_CALLBACK (on_web_socket_error), self); problem = NULL; out: if (problem) cockpit_channel_close (channel, problem); g_clear_error (&error); g_strfreev (protocols); if (io) g_object_unref (io); g_list_free (names); }
static void send_http_request (CockpitHttpStream *self) { CockpitChannel *channel = COCKPIT_CHANNEL (self); JsonObject *options; gboolean had_host; gboolean had_encoding; const gchar *method; const gchar *path; GString *string = NULL; JsonNode *node; JsonObject *headers; const gchar *header; const gchar *value; GList *request = NULL; GList *names = NULL; GBytes *bytes; GList *l; gsize total; options = cockpit_channel_get_options (channel); /* * The checks we do here for token validity are just enough to be able * to format an HTTP response, without leaking across lines etc. */ if (!cockpit_json_get_string (options, "path", NULL, &path)) { cockpit_channel_fail (channel, "protocol-error", "%s: bad \"path\" field in HTTP stream request", self->name); goto out; } else if (path == NULL) { cockpit_channel_fail (channel, "protocol-error", "%s: missing \"path\" field in HTTP stream request", self->name); goto out; } else if (!cockpit_web_response_is_simple_token (path)) { cockpit_channel_fail (channel, "protocol-error", "%s: invalid \"path\" field in HTTP stream request", self->name); goto out; } if (!cockpit_json_get_string (options, "method", NULL, &method)) { cockpit_channel_fail (channel, "protocol-error", "%s: bad \"method\" field in HTTP stream request", self->name); goto out; } else if (method == NULL) { cockpit_channel_fail (channel, "protocol-error", "%s: missing \"method\" field in HTTP stream request", self->name); goto out; } else if (!cockpit_web_response_is_simple_token (method)) { cockpit_channel_fail (channel, "protocol-error", "%s: invalid \"method\" field in HTTP stream request", self->name); goto out; } g_debug ("%s: sending %s request", self->name, method); string = g_string_sized_new (128); g_string_printf (string, "%s %s HTTP/1.1\r\n", method, path); had_encoding = had_host = FALSE; node = json_object_get_member (options, "headers"); if (node) { if (!JSON_NODE_HOLDS_OBJECT (node)) { cockpit_channel_fail (channel, "protocol-error", "%s: invalid \"headers\" field in HTTP stream request", self->name); goto out; } headers = json_node_get_object (node); names = json_object_get_members (headers); for (l = names; l != NULL; l = g_list_next (l)) { header = l->data; if (!cockpit_web_response_is_simple_token (header)) { cockpit_channel_fail (channel, "protocol-error", "%s: invalid header in HTTP stream request: %s", self->name, header); goto out; } node = json_object_get_member (headers, header); if (!node || !JSON_NODE_HOLDS_VALUE (node) || json_node_get_value_type (node) != G_TYPE_STRING) { cockpit_channel_fail (channel, "protocol-error", "%s: invalid header value in HTTP stream request: %s", self->name, header); goto out; } value = json_node_get_string (node); if (disallowed_header (header, value, self->binary)) { cockpit_channel_fail (channel, "protocol-error", "%s: disallowed header in HTTP stream request: %s", self->name, header); goto out; } if (!cockpit_web_response_is_header_value (value)) { cockpit_channel_fail (channel, "protocol-error", "%s: invalid header value in HTTP stream request: %s", self->name, header); goto out; } g_string_append_printf (string, "%s: %s\r\n", (gchar *)l->data, value); g_debug ("%s: sending header: %s %s", self->name, (gchar *)l->data, value); if (g_ascii_strcasecmp (l->data, "Host") == 0) had_host = TRUE; if (g_ascii_strcasecmp (l->data, "Accept-Encoding") == 0) had_encoding = TRUE; } } if (!had_host) { g_string_append (string, "Host: "); g_string_append_uri_escaped (string, self->client->connectable->name, "[]!%$&()*+,-.:;=\\_~", FALSE); g_string_append (string, "\r\n"); } if (!had_encoding) g_string_append (string, "Accept-Encoding: identity\r\n"); if (!self->binary) g_string_append (string, "Accept-Charset: UTF-8\r\n"); request = g_list_reverse (self->request); self->request = NULL; /* Calculate how much data we have to send */ total = 0; for (l = request; l != NULL; l = g_list_next (l)) total += g_bytes_get_size (l->data); if (request || g_ascii_strcasecmp (method, "POST") == 0) g_string_append_printf (string, "Content-Length: %" G_GSIZE_FORMAT "\r\n", total); g_string_append (string, "\r\n"); bytes = g_string_free_to_bytes (string); string = NULL; cockpit_stream_write (self->stream, bytes); g_bytes_unref (bytes); /* Now send all the data */ for (l = request; l != NULL; l = g_list_next (l)) cockpit_stream_write (self->stream, l->data); out: g_list_free (names); g_list_free_full (request, (GDestroyNotify)g_bytes_unref); if (string) g_string_free (string, TRUE); }
static void parse_new_messages(PurpleConnection *pc, FacebookAccount *fba, JsonArray *messages) { int i; PurpleBuddy *buddy; purple_debug_info("facebook", "parsing new messages\n"); for (i = 0; i < json_array_get_length(messages); i++) { const gchar *type; gchar *from, *to; JsonObject *object = json_node_get_object(json_array_get_element(messages, i)); type = json_node_get_string(json_object_get_member(object, "type")); from = g_strdup_printf("%" G_GINT64_FORMAT, (gint64)json_node_get_int(json_object_get_member(object, "from"))); to = g_strdup_printf("%" G_GINT64_FORMAT, (gint64)json_node_get_int(json_object_get_member(object, "to"))); /* Use the in-line buddy name if the buddy list hasn't been downloaded yet */ buddy = purple_find_buddy(pc->account, from); if (buddy == NULL || buddy->server_alias == NULL || buddy->alias == NULL) { if (json_object_has_member(object, "from_name")) { const gchar *from_name = json_node_get_string(json_object_get_member( object, "from_name")); fb_blist_set_alias(fba, from, from_name); } } if (from && to && g_str_equal(type, "msg")) { JsonObject *messageObj = json_node_get_object(json_object_get_member(object, "msg")); parse_message(pc, fba, messageObj, from, to, PURPLE_CONV_TYPE_IM); } else if (from && g_str_equal(type, "typ")) { purple_debug_info("facebook", "handling typing notification\n"); gint typing = json_node_get_int(json_object_get_member(object, "st")); if (typing == 0) { serv_got_typing(pc, from, 10, PURPLE_NOT_TYPING); } else { serv_got_typing(pc, from, 10, PURPLE_TYPING); } } else if (g_str_equal(type, "group_msg")) { PurpleConversation *conv = fb_find_chat(fba, to); if (conv != NULL) { if (json_object_has_member(object, "to_name")) { const gchar *to_name = json_node_get_string(json_object_get_member( object, "to_name")); purple_conversation_set_title(conv, to_name); PurpleChat *chat = purple_blist_find_chat(fba->account, to); purple_blist_alias_chat(chat, to_name); } if (!purple_conv_chat_cb_find(PURPLE_CONV_CHAT(conv), from)) { purple_conv_chat_add_user(PURPLE_CONV_CHAT(conv), from, NULL, PURPLE_CBFLAGS_NONE, FALSE); } if (!purple_find_buddy(fba->account, from)) { const char *from_name = json_node_get_string(json_object_get_member( object, "from_name")); buddy = purple_buddy_new(fba->account, from, from_name); purple_blist_node_set_flags((PurpleBlistNode *)buddy, PURPLE_BLIST_NODE_FLAG_NO_SAVE); purple_blist_add_buddy(buddy, NULL, purple_find_group(DEFAULT_GROUP_NAME), NULL); } } JsonObject *messageObj = json_node_get_object(json_object_get_member(object, "msg")); parse_message(pc, fba, messageObj, from, to, PURPLE_CONV_TYPE_CHAT); } /* * we've received something from a buddy, assume they're online * only if it's not from ourselves */ if (from && fba->uid != atoll(from)) { purple_prpl_got_user_status( fba->account, from, purple_primitive_get_id_from_type(PURPLE_STATUS_AVAILABLE), NULL); } g_free(from); g_free(to); fba->message_fetch_sequence++; } }
/** * json_reader_read_element: * @reader: a #JsonReader * @index_: the index of the element * * Advances the cursor of @reader to the element @index_ of the array * or the object at the current position. * * You can use the json_reader_get_value* family of functions to retrieve * the value of the element; for instance: * * |[ * json_reader_read_element (reader, 0); * int_value = json_reader_get_int_value (reader); * ]| * * After reading the value, json_reader_end_element() should be called to * reposition the cursor inside the #JsonReader, e.g.: * * |[ * json_reader_read_element (reader, 1); * str_value = json_reader_get_string_value (reader); * json_reader_end_element (reader); * * json_reader_read_element (reader, 2); * str_value = json_reader_get_string_value (reader); * json_reader_end_element (reader); * ]| * * If @reader is not currently on an array or an object, or if the @index_ is * bigger than the size of the array or the object, the #JsonReader will be * put in an error state until json_reader_end_element() is called. * * Return value: %TRUE on success, and %FALSE otherwise * * Since: 0.12 */ gboolean json_reader_read_element (JsonReader *reader, guint index_) { JsonReaderPrivate *priv; g_return_val_if_fail (JSON_READER (reader), FALSE); json_reader_return_val_if_error_set (reader, FALSE); priv = reader->priv; if (priv->current_node == NULL) priv->current_node = priv->root; if (!(JSON_NODE_HOLDS_ARRAY (priv->current_node) || JSON_NODE_HOLDS_OBJECT (priv->current_node))) return json_reader_set_error (reader, JSON_READER_ERROR_NO_ARRAY, _("The current node is of type '%s', but " "an array or an object was expected."), json_node_type_name (priv->current_node)); switch (json_node_get_node_type (priv->current_node)) { case JSON_NODE_ARRAY: { JsonArray *array = json_node_get_array (priv->current_node); if (index_ >= json_array_get_length (array)) return json_reader_set_error (reader, JSON_READER_ERROR_INVALID_INDEX, _("The index '%d' is greater than the size " "of the array at the current position."), index_); priv->previous_node = priv->current_node; priv->current_node = json_array_get_element (array, index_); } break; case JSON_NODE_OBJECT: { JsonObject *object = json_node_get_object (priv->current_node); GList *members; const gchar *name; if (index_ >= json_object_get_size (object)) return json_reader_set_error (reader, JSON_READER_ERROR_INVALID_INDEX, _("The index '%d' is greater than the size " "of the object at the current position."), index_); priv->previous_node = priv->current_node; g_free (priv->current_member); members = json_object_get_members (object); name = g_list_nth_data (members, index_); priv->current_node = json_object_get_member (object, name); priv->current_member = g_strdup (name); g_list_free (members); } break; default: g_assert_not_reached (); return FALSE; } return TRUE; }
gdouble peer_get_progress(JsonObject * p) { return json_double_to_progress(json_object_get_member(p, TPEER_PROGRESS)); }
static gboolean builder_options_deserialize_property (JsonSerializable *serializable, const gchar *property_name, GValue *value, GParamSpec *pspec, JsonNode *property_node) { if (strcmp (property_name, "arch") == 0) { if (JSON_NODE_TYPE (property_node) == JSON_NODE_NULL) { g_value_set_boxed (value, NULL); return TRUE; } else if (JSON_NODE_TYPE (property_node) == JSON_NODE_OBJECT) { JsonObject *object = json_node_get_object (property_node); g_autoptr(GHashTable) hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref); g_autoptr(GList) members = NULL; GList *l; members = json_object_get_members (object); for (l = members; l != NULL; l = l->next) { const char *member_name = l->data; JsonNode *val; GObject *option; val = json_object_get_member (object, member_name); option = json_gobject_deserialize (BUILDER_TYPE_OPTIONS, val); if (option == NULL) return FALSE; g_hash_table_insert (hash, g_strdup (member_name), option); } g_value_set_boxed (value, hash); return TRUE; } return FALSE; } else if (strcmp (property_name, "env") == 0) { if (JSON_NODE_TYPE (property_node) == JSON_NODE_NULL) { g_value_set_boxed (value, NULL); return TRUE; } else if (JSON_NODE_TYPE (property_node) == JSON_NODE_OBJECT) { JsonObject *object = json_node_get_object (property_node); g_autoptr(GPtrArray) env = g_ptr_array_new_with_free_func (g_free); g_autoptr(GList) members = NULL; GList *l; members = json_object_get_members (object); for (l = members; l != NULL; l = l->next) { const char *member_name = l->data; JsonNode *val; const char *val_str; val = json_object_get_member (object, member_name); val_str = json_node_get_string (val); if (val_str == NULL) return FALSE; g_ptr_array_add (env, g_strdup_printf ("%s=%s", member_name, val_str)); } g_ptr_array_add (env, NULL); g_value_take_boxed (value, g_ptr_array_free (g_steal_pointer (&env), FALSE)); return TRUE; } return FALSE; } else { return json_serializable_default_deserialize_property (serializable, property_name, value, pspec, property_node); } }
void fb_got_facepile(FacebookAccount *fba, const gchar *data, gsize data_len, gpointer user_data) { gchar *group = user_data; JsonParser *parser; JsonObject *object, *payload, *user_obj; JsonArray *facepile; PurpleConversation *conv; PurpleConvChat *chat; gchar *uid; guint i; PurpleGroup *pgroup; purple_debug_info("facebook", "got facepile %s\n", data?data:"(null)"); conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_CHAT, group, fba->account); chat = PURPLE_CONV_CHAT(conv); parser = fb_get_parser(data, data_len); if (!parser) { purple_debug_warning("facebook", "could not fetch facepile for group %s\n", group); g_free(group); return; } object = fb_get_json_object(parser, NULL); payload = json_node_get_object( json_object_get_member(object, "payload")); facepile = json_node_get_array( json_object_get_member(payload, "facepile_click_info")); pgroup = purple_find_group(DEFAULT_GROUP_NAME); if (!pgroup) { pgroup = purple_group_new(DEFAULT_GROUP_NAME); purple_blist_add_group(pgroup, NULL); } purple_conv_chat_clear_users(chat); uid = g_strdup_printf("%" G_GINT64_FORMAT, fba->uid); purple_conv_chat_add_user(chat, uid, NULL, PURPLE_CBFLAGS_NONE, FALSE); if (!purple_find_buddy(fba->account, uid)) { PurpleBuddy *buddy = purple_buddy_new(fba->account, uid, "You"); purple_blist_node_set_flags((PurpleBlistNode *)buddy, PURPLE_BLIST_NODE_FLAG_NO_SAVE); purple_blist_add_buddy(buddy, NULL, pgroup, NULL); } g_free(uid); for (i = 0; i < json_array_get_length(facepile); i++) { user_obj = json_node_get_object( json_array_get_element(facepile, i)); uid = g_strdup_printf("%" G_GINT64_FORMAT, (gint64)json_node_get_int(json_object_get_member(user_obj, "uid"))); purple_conv_chat_add_user(PURPLE_CONV_CHAT(conv), uid, NULL, PURPLE_CBFLAGS_NONE, FALSE); if (!purple_find_buddy(fba->account, uid)) { const char *alias = json_node_get_string(json_object_get_member(user_obj, "name")); PurpleBuddy *buddy = purple_buddy_new(fba->account, uid, alias); purple_blist_node_set_flags((PurpleBlistNode *)buddy, PURPLE_BLIST_NODE_FLAG_NO_SAVE); purple_blist_add_buddy(buddy, NULL, pgroup, NULL); } g_free(uid); } g_free(group); }
static void push_gcm_client_deliver_cb (SoupSession *session, SoupMessage *message, gpointer user_data) { GSimpleAsyncResult *simple = user_data; const gchar *str; JsonObject *obj; JsonParser *p = NULL; JsonArray *ar; JsonNode *root; JsonNode *node; gboolean removed; GError *error = NULL; GList *list; gsize length; guint i; ENTRY; g_assert(SOUP_IS_SESSION(session)); g_assert(SOUP_IS_MESSAGE(message)); g_assert(G_IS_SIMPLE_ASYNC_RESULT(simple)); switch (message->status_code) { case SOUP_STATUS_OK: break; case SOUP_STATUS_BAD_REQUEST: /* * TODO: Log that there was a JSON encoding error likely. */ break; case SOUP_STATUS_UNAUTHORIZED: g_simple_async_result_set_error( simple, SOUP_HTTP_ERROR, message->status_code, _("GCM request unauthorized. Check credentials.")); GOTO(failure); default: if (SOUP_STATUS_IS_SERVER_ERROR(message->status_code) && (str = soup_message_headers_get_one(message->response_headers, "Retry-After"))) { /* * TODO: Implement exponential back-off. */ } g_simple_async_result_set_error(simple, SOUP_HTTP_ERROR, message->status_code, _("Unknown failure occurred.")); break; } if (!message->response_body->data || !message->response_body->length) { g_simple_async_result_set_error(simple, SOUP_HTTP_ERROR, SOUP_STATUS_IO_ERROR, _("No data was received from GCM.")); GOTO(failure); } p = json_parser_new(); if (!json_parser_load_from_data(p, message->response_body->data, message->response_body->length, &error)) { g_simple_async_result_take_error(simple, error); GOTO(failure); } list = g_object_get_data(G_OBJECT(simple), "identities"); if ((root = json_parser_get_root(p)) && JSON_NODE_HOLDS_OBJECT(root) && (obj = json_node_get_object(root)) && json_object_has_member(obj, "results") && (node = json_object_get_member(obj, "results")) && JSON_NODE_HOLDS_ARRAY(node) && (ar = json_node_get_array(node))) { length = json_array_get_length(ar); for (i = 0; i < length && list; i++, list = list->next) { /* * TODO: Handle the case that the device_token has been renamed. */ removed = FALSE; if ((obj = json_array_get_object_element(ar, i)) && json_object_has_member(obj, "error") && (node = json_object_get_member(obj, "error")) && JSON_NODE_HOLDS_VALUE(node) && (str = json_node_get_string(node))) { if (!g_strcmp0(str, "MissingRegistration")) { removed = TRUE; } else if (!g_strcmp0(str, "InvalidRegistration")) { removed = TRUE; } else if (!g_strcmp0(str, "MismatchSenderId")) { } else if (!g_strcmp0(str, "NotRegistered")) { removed = TRUE; } else if (!g_strcmp0(str, "MessageTooBig")) { } else if (!g_strcmp0(str, "InvalidDataKey")) { } else if (!g_strcmp0(str, "InvalidTtl")) { } if (removed) { g_assert(PUSH_IS_GCM_IDENTITY(list->data)); g_signal_emit(session, gSignals[IDENTITY_REMOVED], 0, list->data); } } } } g_simple_async_result_set_op_res_gboolean(simple, TRUE); failure: g_simple_async_result_complete_in_idle(simple); g_object_unref(simple); if (p) { g_object_unref(p); } EXIT; }
static void _process_poll_resp(SoupSession *ss, SoupMessage *msg, gpointer user_data) { GWQSession *wqs; const guint8* data; gsize size; SoupBuffer *sBuf; gchar *tmpCStr; JsonParser *jParser; JsonNode *jn; JsonObject *jo; gint32 tmpInt; SoupURI *su; JsonArray *resJa; wqs = (GWQSession*)user_data; GWQ_DBG("poll responsed, retcode=%d, reason:%s\n", msg->status_code, msg->reason_phrase); if (wqs->st != GWQS_ST_IDLE) { goto ERR_OUT; } if (msg->status_code != 200) { GWQ_ERR_OUT(ERR_OUT, "\n"); } sBuf = soup_message_body_flatten(msg->response_body); if (!sBuf) { GWQ_ERR_OUT(ERR_OUT, "\n"); } soup_buffer_get_data(sBuf, &data, &size); if (!data || size <=0 ) { GWQ_ERR_OUT(ERR_FREE_SBUF, "\n"); } GWQ_DBG("bodySize=%d\nbody:%s\n", size, data); if (!(jParser = json_parser_new())) { GWQ_ERR_OUT(ERR_FREE_SBUF, "\n"); } resJa = NULL; if (!json_parser_load_from_data(jParser, (const gchar*)data, size, NULL)) { GWQ_ERR("\n"); } else if (!(jn = json_parser_get_root(jParser)) || !(jo = json_node_get_object(jn))) { GWQ_ERR("\n"); } else if ((tmpInt = json_object_get_int_member(jo, "retcode"))) { GWQ_ERR("poll retcode=%d\n", tmpInt); } else if (!(jn = json_object_get_member(jo, "result")) || !(resJa = json_node_get_array(jn))) { GWQ_ERR("poll no result found\n"); } if (resJa) { json_array_foreach_element(resJa, _PollResultArrayForeach, wqs); } g_object_unref(jParser); soup_buffer_free(sBuf); tmpCStr = g_strdup_printf("clientid=%s&psessionid=%s&t=%ld", wqs->clientId->str, wqs->psessionid->str, GetNowMillisecond() ); su = soup_message_get_uri(msg); soup_uri_set_query(su, tmpCStr); g_free(tmpCStr); GWQ_ERR("Fix me : why soup_session_requeue_message() fails here!!!!!!!!!!!!!!!!!!!!!\n"); //soup_session_requeue_message(wqs->sps, msg); GWQSessionDoPoll(wqs); return; ERR_FREE_J_PARSER: g_object_unref(jParser); ERR_FREE_SBUF: soup_buffer_free(sBuf); ERR_OUT: soup_session_cancel_message(ss, msg, SOUP_STATUS_CANCELLED); }
static GVariant * parse_json_dictionary (JsonNode *node, const GVariantType *entry_type, GError **error) { const GVariantType *key_type; const GVariantType *value_type; GVariant *result = NULL; GPtrArray *children; JsonObject *object; JsonNode *key_node; GList *members = NULL; gboolean is_string; GVariant *value; GVariant *key; GVariant *child; GList *l; children = g_ptr_array_new (); if (!check_type (node, JSON_NODE_OBJECT, 0, error)) goto out; object = json_node_get_object (node); key_type = g_variant_type_key (entry_type); value_type = g_variant_type_value (entry_type); is_string = (g_variant_type_equal (key_type, G_VARIANT_TYPE_STRING) || g_variant_type_equal (key_type, G_VARIANT_TYPE_OBJECT_PATH) || g_variant_type_equal (key_type, G_VARIANT_TYPE_SIGNATURE)); members = json_object_get_members (object); for (l = members; l != NULL; l = g_list_next (l)) { if (is_string) { key_node = json_node_init_string (json_node_alloc (), l->data); } else { key_node = cockpit_json_parse (l->data, -1, NULL); if (key_node == NULL) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, "Unexpected key '%s' in JSON object", (gchar *)l->data); goto out; } } key = parse_json (key_node, key_type, error); json_node_free (key_node); if (!key) goto out; value = parse_json (json_object_get_member (object, l->data), value_type, error); if (!value) { g_variant_unref (key); goto out; } child = g_variant_new_dict_entry (key, value); g_ptr_array_add (children, child); } result = g_variant_new_array (entry_type, (GVariant *const *)children->pdata, children->len); children->len = 0; out: g_list_free (members); g_ptr_array_foreach (children, (GFunc)g_variant_unref, NULL); g_ptr_array_free (children, TRUE); return result; }
static GObject * json_gobject_new (GType gtype, JsonObject *object) { JsonSerializableIface *iface = NULL; JsonSerializable *serializable = NULL; gboolean find_property; gboolean deserialize_property; gboolean set_property; GQueue *members; GList *l; GQueue members_left = G_QUEUE_INIT; guint n_members; GObjectClass *klass; GObject *retval; GArray *construct_params; gint i; klass = g_type_class_ref (gtype); n_members = json_object_get_size (object); members = json_object_get_members_internal (object); /* first pass: construct-only properties; here we cannot use Serializable * because we don't have an instance yet; we use the default implementation * of json_deserialize_pspec() to deserialize known types * * FIXME - find a way to allow deserialization for these properties */ construct_params = g_array_sized_new (FALSE, FALSE, sizeof (GParameter), n_members); for (l = members->head; l != NULL; l = l->next) { const gchar *member_name = l->data; GParamSpec *pspec; GParameter param = { NULL, }; JsonNode *val; gboolean res = FALSE; pspec = g_object_class_find_property (klass, member_name); if (!pspec) goto next_member; /* we only apply construct-only properties here */ if ((pspec->flags & G_PARAM_CONSTRUCT_ONLY) == 0) goto next_member; if (!(pspec->flags & G_PARAM_WRITABLE)) goto next_member; g_value_init (¶m.value, G_PARAM_SPEC_VALUE_TYPE (pspec)); val = json_object_get_member (object, member_name); res = json_deserialize_pspec (¶m.value, pspec, val); if (!res) { g_warning ("Failed to deserialize \"%s\" property of type \"%s\" for an object of type \"%s\"", pspec->name, G_VALUE_TYPE_NAME (¶m.value), g_type_name (gtype)); g_value_unset (¶m.value); } else { param.name = g_strdup (pspec->name); g_array_append_val (construct_params, param); continue; } next_member: g_queue_push_tail (&members_left, l->data); } G_GNUC_BEGIN_IGNORE_DEPRECATIONS retval = g_object_newv (gtype, construct_params->len, (GParameter *) construct_params->data); G_GNUC_END_IGNORE_DEPRECATIONS /* free the contents of the GArray */ for (i = 0; i < construct_params->len; i++) { GParameter *param = &g_array_index (construct_params, GParameter, i); g_free ((gchar *) param->name); g_value_unset (¶m->value); } g_array_free (construct_params, TRUE); /* do the Serializable type check once */ if (g_type_is_a (gtype, JSON_TYPE_SERIALIZABLE)) { serializable = JSON_SERIALIZABLE (retval); iface = JSON_SERIALIZABLE_GET_IFACE (serializable); find_property = (iface->find_property != NULL); deserialize_property = (iface->deserialize_property != NULL); set_property = (iface->set_property != NULL); } else { find_property = FALSE; deserialize_property = FALSE; set_property = FALSE; } g_object_freeze_notify (retval); for (l = members_left.head; l != NULL; l = l->next) { const gchar *member_name = l->data; GParamSpec *pspec; JsonNode *val; GValue value = { 0, }; gboolean res = FALSE; if (find_property) pspec = json_serializable_find_property (serializable, member_name); else pspec = g_object_class_find_property (klass, member_name); if (pspec == NULL) continue; /* we should have dealt with these above */ if (pspec->flags & G_PARAM_CONSTRUCT_ONLY) continue; if (!(pspec->flags & G_PARAM_WRITABLE)) continue; g_value_init (&value, G_PARAM_SPEC_VALUE_TYPE (pspec)); val = json_object_get_member (object, member_name); if (deserialize_property) { JSON_NOTE (GOBJECT, "Using JsonSerializable for property '%s'", pspec->name); res = json_serializable_deserialize_property (serializable, pspec->name, &value, pspec, val); } if (!res) { JSON_NOTE (GOBJECT, "Using json_deserialize_pspec for property '%s'", pspec->name); res = json_deserialize_pspec (&value, pspec, val); } if (res) { JSON_NOTE (GOBJECT, "Calling set_property('%s', '%s')", pspec->name, g_type_name (G_VALUE_TYPE (&value))); if (set_property) json_serializable_set_property (serializable, pspec, &value); else g_object_set_property (retval, pspec->name, &value); } else g_warning ("Failed to deserialize \"%s\" property of type \"%s\" for an object of type \"%s\"", pspec->name, g_type_name (G_VALUE_TYPE (&value)), g_type_name (gtype)); g_value_unset (&value); } g_queue_clear (&members_left); g_object_thaw_notify (retval); g_type_class_unref (klass); return retval; }
/** Refresh albums */ static void _piwigo_refresh_albums(dt_storage_piwigo_gui_data_t *ui, const gchar *select_album) { gtk_widget_set_sensitive(GTK_WIDGET(ui->album_list), FALSE); gtk_widget_set_sensitive(GTK_WIDGET(ui->parent_album_list), FALSE); if(ui->api == NULL || ui->api->authenticated == FALSE) { _piwigo_authenticate(ui); if(ui->api == NULL || !ui->api->authenticated) return; } gchar *to_select; int index = 0; // get the new album name, it will be checked in the if(select_album == NULL) { to_select = g_strdup(dt_bauhaus_combobox_get_text(ui->album_list)); if(to_select) { // cut the count of picture in album to get the name only gchar *p = to_select; while(*p) { if(*p == ' ' && *(p+1) == '(') { *p = '\0'; break; } p++; } } } else to_select = g_strdup(select_album); // First clear the combobox except first 2 items (none / create new album) dt_bauhaus_combobox_clear(ui->album_list); dt_bauhaus_combobox_clear(ui->parent_album_list); g_list_free(ui->albums); ui->albums = NULL; GList *args = NULL; args = _piwigo_query_add_arguments(args, "method", "pwg.categories.getList"); args = _piwigo_query_add_arguments(args, "cat_id", "0"); args = _piwigo_query_add_arguments(args, "recursive", "true"); _piwigo_api_post(ui->api, args, NULL, FALSE); g_list_free(args); if(ui->api->response && !ui->api->error_occured) { dt_bauhaus_combobox_add(ui->album_list, _("create new album")); dt_bauhaus_combobox_add(ui->parent_album_list, _("---")); JsonObject *result = json_node_get_object(json_object_get_member(ui->api->response, "result")); JsonArray *albums = json_object_get_array_member(result, "categories"); if(json_array_get_length(albums)>0 && index==0) index = 1; if(index > json_array_get_length(albums) - 1) index = json_array_get_length(albums) - 1; for(int i = 0; i < json_array_get_length(albums); i++) { char data[MAX_ALBUM_NAME_SIZE] = { 0 }; JsonObject *album = json_array_get_object_element(albums, i); _piwigo_album_t *new_album = g_malloc0(sizeof(struct _piwigo_album_t)); g_strlcpy(new_album->name, json_object_get_string_member(album, "name"), sizeof(new_album->name)); new_album->id = json_object_get_int_member(album, "id"); new_album->size = json_object_get_int_member(album, "nb_images"); const int isroot = json_object_get_null_member(album, "id_uppercat"); int indent = 0; if(!isroot) { const char *hierarchy = json_object_get_string_member(album, "uppercats"); char const *p = hierarchy; while(*p++) if(*p == ',') indent++; } snprintf(data, sizeof(data), "%*c%s (%"PRId64")", indent * 3, ' ', new_album->name, new_album->size); if(to_select && !strcmp(new_album->name, to_select)) index = i + 1; g_strlcpy(new_album->label, data, sizeof(new_album->label)); ui->albums = g_list_append(ui->albums, new_album); dt_bauhaus_combobox_add_aligned(ui->album_list, data, DT_BAUHAUS_COMBOBOX_ALIGN_LEFT); dt_bauhaus_combobox_add_aligned(ui->parent_album_list, data, DT_BAUHAUS_COMBOBOX_ALIGN_LEFT); } } else dt_control_log(_("cannot refresh albums")); g_free(to_select); gtk_widget_set_sensitive(GTK_WIDGET(ui->album_list), TRUE); gtk_widget_set_sensitive(GTK_WIDGET(ui->parent_album_list), TRUE); dt_bauhaus_combobox_set(ui->album_list, index); }
/* See "man 5 passwd" We just make sure the name and uid/gid match, and that none are missing. don't care about GECOS/dir/shell. */ static gboolean rpmostree_check_passwd_groups (gboolean passwd, OstreeRepo *repo, GFile *yumroot, GFile *treefile_dirpath, JsonObject *treedata, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; const char *direct = NULL; const char *chk_type = "previous"; const char *ref = NULL; const char *commit_filepath = passwd ? "usr/lib/passwd" : "usr/lib/group"; const char *json_conf_name = passwd ? "check-passwd" : "check-groups"; const char *json_conf_ign = passwd ? "ignore-removed-users" : "ignore-removed-groups"; gs_unref_object GFile *old_path = NULL; gs_unref_object GFile *new_path = g_file_resolve_relative_path (yumroot, commit_filepath); gs_unref_ptrarray GPtrArray *ignore_removed_ents = NULL; gboolean ignore_all_removed = FALSE; gs_free char *old_contents = NULL; gs_free char *new_contents = NULL; gs_unref_ptrarray GPtrArray *old_ents = NULL; gs_unref_ptrarray GPtrArray *new_ents = NULL; unsigned int oiter = 0; unsigned int niter = 0; if (json_object_has_member (treedata, json_conf_name)) { JsonObject *chk = json_object_get_object_member (treedata,json_conf_name); if (!chk) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "%s is not an object", json_conf_name); goto out; } chk_type = _rpmostree_jsonutil_object_require_string_member (chk, "type", error); if (!chk_type) goto out; if (g_str_equal (chk_type, "none")) { ret = TRUE; goto out; } else if (g_str_equal (chk_type, "file")) { direct = _rpmostree_jsonutil_object_require_string_member (chk, "filename", error); if (!direct) goto out; } else if (g_str_equal (chk_type, "data")) { JsonNode *ents_node = json_object_get_member (chk, "entries"); JsonObject *ents_obj = NULL; GList *ents; GList *iter; if (!ents_node) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "No entries member for data in %s", json_conf_name); goto out; } ents_obj = json_node_get_object (ents_node); if (passwd) old_ents = g_ptr_array_new_with_free_func (conv_passwd_ent_free); else old_ents = g_ptr_array_new_with_free_func (conv_group_ent_free); ents = json_object_get_members (ents_obj); for (iter = ents; iter; iter = iter->next) if (passwd) { const char *name = iter->data; JsonNode *val = json_object_get_member (ents_obj, name); JsonNodeType child_type = json_node_get_node_type (val); gint64 uid = 0; gint64 gid = 0; struct conv_passwd_ent *convent = g_new (struct conv_passwd_ent, 1); if (child_type != JSON_NODE_ARRAY) { if (!_rpmostree_jsonutil_object_require_int_member (ents_obj, name, &uid, error)) goto out; gid = uid; } else { JsonArray *child_array = json_node_get_array (val); guint len = json_array_get_length (child_array); if (!len || (len > 2)) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Array %s is only for uid and gid. Has length %u", name, len); goto out; } if (!_rpmostree_jsonutil_array_require_int_element (child_array, 0, &uid, error)) goto out; if (len == 1) gid = uid; else if (!_rpmostree_jsonutil_array_require_int_element (child_array, 1, &gid, error)) goto out; } convent->name = g_strdup (name); convent->uid = uid; convent->gid = gid; g_ptr_array_add (old_ents, convent); } else { const char *name = iter->data; gint64 gid = 0; struct conv_group_ent *convent = g_new (struct conv_group_ent, 1); if (!_rpmostree_jsonutil_object_require_int_member (ents_obj, name, &gid, error)) goto out; convent->name = g_strdup (name); convent->gid = gid; g_ptr_array_add (old_ents, convent); } }
gdouble torrent_get_percent_done(JsonObject * t) { return json_double_to_progress(json_object_get_member (t, FIELD_PERCENTDONE)); }
static void got_new_messages(FacebookAccount *fba, const gchar *data, gsize data_len, gpointer userdata) { JsonParser *parser; PurpleConnection *pc = userdata; purple_debug_misc("facebook", "got new messages:\n%s\n", data); /* for (;;);{"t":"msg","c":"p_800753867","ms":[{"type":"msg", "msg":{"text":"yes","time":1211176515861,"clientTime":1211176514750, "msgID":"367146364"},"from":596176850,"to":800753867, "from_name":"Jeremy Lawson","to_name":"Eion Robb", "from_first_name":"Jeremy","to_first_name":"Eion"}]} */ /* for (;;);{"t":"refresh"} */ /* for (;;);{"t":"msg","c":"p_800753867","ms":[{"type":"msg", "msg":{"text":"p**n head","time":1211177326689,"clientTime":1211177325, "msgID":"-1992480367"},"from":800753867,"to":596176850, "from_name":"Eion Robb","to_name":"Jeremy Lawson", "from_first_name":"Eion","to_first_name":"Jeremy"}]} */ /* for (;;);{"t":"msg","c":"p_800753867","ms":[{"type":"typ","st":1, "from":596176850,"to":800753867},{"type":"msg","msg":{"text":"nubile!", "time":1211177334019,"clientTime":1211177326690,"msgID":"696260545"}, "from":596176850,"to":800753867,"from_name":"Jeremy Lawson", "to_name":"Eion Robb","from_first_name":"Jeremy","to_first_name":"Eion"}, {"type":"msg","msg":{"text":"test2","time":1211177336688, "clientTime":1211177326691,"msgID":"1527815367"},"from":596176850, "to":800753867,"from_name":"Jeremy Lawson","to_name":"Eion Robb", "from_first_name":"Jeremy","to_first_name":"Eion"},{"type":"msg", "msg":{"text":"ahhhhhhh!","time":1211177344361,"clientTime":1211177326692, "msgID":"4028916254"},"from":596176850,"to":800753867, "from_name":"Jeremy Lawson","to_name":"Eion Robb", "from_first_name":"Jeremy","to_first_name":"Eion"}]} */ /* for (;;);{"t":"msg","c":"p_800753867","ms":[{"type":"msg", "msg":{"text":"2","time":1211178167261,"clientTime":1211178164656, "msgID":"3382240259"},"from":596176850,"to":800753867, "from_name":"Jeremy Lawson","to_name":"Eion Robb", "from_first_name":"Jeremy","to_first_name":"Eion"}]} */ /* for (;;);{"t":"refresh", "seq":1} */ parser = fb_get_parser(data, data_len); if (!parser) { /* Sometimes proxies will return incorrect data, so we just shrug * it off. * TODO: Only do this for proxies. And when we do it, keep track * of consecutive failures in the case something is actually * wrong with Facebook. Eventually this condition should cause * failure */ /* Continue looping, waiting for more messages */ purple_debug_error("facebook", "got data back, but it's not even json\n"); fb_get_new_messages(fba); return; } JsonObject *objnode = fb_get_json_object(parser, NULL); if (json_object_has_member(objnode, "t")) { const gchar* command = json_node_get_string(json_object_get_member(objnode, "t")); if (g_str_equal(command, "refresh")) { if (json_object_has_member(objnode, "seq")) { fba->message_fetch_sequence = json_node_get_int( json_object_get_member(objnode, "seq")); } /* grab history items for all open conversations */ GList *conversations = purple_get_conversations(); while (conversations != NULL) { PurpleConversation *conv = (PurpleConversation *)conversations->data; if (fb_conversation_is_fb(conv)) { purple_debug_info("facebook", "checking for dropped messages with %s\n", conv->name); fb_history_fetch(fba, conv->name, FALSE); } conversations = conversations->next; } /* refresh means that the channel is invalid */ fb_reconnect(fba); json_parser_free(parser); return; } else if (g_str_equal(command, "continue")) { /* continue means that the server wants us to remake the connection. * continue the loop and wait for messages. noop. */ } else if (g_str_equal(command, "msg")) { parse_new_messages(pc, fba, json_node_get_array(json_object_get_member(objnode, "ms"))); } } if (json_object_has_member(objnode, "s")) { gint new_seq = json_node_get_int(json_object_get_member(objnode, "s")); fba->message_fetch_sequence = new_seq; } json_parser_free(parser); /* Continue looping, waiting for more messages */ fb_get_new_messages(fba); }
gdouble torrent_get_recheck_progress(JsonObject * t) { return json_double_to_progress(json_object_get_member (t, FIELD_RECHECK_PROGRESS)); }
void got_reconnect_json(FacebookAccount *fba, const gchar *data, gsize data_len, gpointer userdata) { JsonParser *parser; JsonObject *objnode; gchar *error_message; parser = fb_get_parser(data, data_len); if (!parser) { purple_debug_error("facebook", "couldn't parse reconnect data\n"); purple_debug_info("facebook", "page content: %s\n", data); purple_connection_error_reason(fba->pc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, _("Chat service currently unavailable")); return; } objnode = fb_get_json_object(parser, &error_message); if (error_message != NULL) { if (json_node_get_int(json_object_get_member(objnode, "error")) == 1356007) { //There'll normally be an error message if chat is down for maintenance purple_connection_error_reason(fba->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_IMPOSSIBLE, error_message); g_free(error_message); json_parser_free(parser); return; } } JsonObject *payload = json_node_get_object(json_object_get_member(objnode, "payload")); /* eg {"host":"channel01"} */ const gchar *new_channel_host = json_node_get_string(json_object_get_member(payload, "host")); if (new_channel_host == NULL) { purple_debug_error("facebook", "couldn't find new channel number\n"); purple_debug_info("facebook", "page content: %s\n", data); purple_connection_error_reason(fba->pc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, _("Error fetching channel; did you log in elsewhere?")); json_parser_free(parser); return; } g_free(fba->channel_number); fba->channel_number = g_strdup(new_channel_host); gint new_seq = json_node_get_int(json_object_get_member(payload, "seq")); fba->message_fetch_sequence = new_seq; /* * Now that we have a channel number we can start looping and * waiting for messages */ fb_get_new_messages(fba); json_parser_free(parser); }
static gboolean process_includes (RpmOstreeTreeComposeContext *self, GFile *treefile_path, guint depth, JsonObject *root, GCancellable *cancellable, GError **error) { gboolean ret = FALSE; const char *include_path; const guint maxdepth = 50; if (depth > maxdepth) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Exceeded maximum include depth of %u", maxdepth); goto out; } { gs_unref_object GFile *parent = g_file_get_parent (treefile_path); gboolean existed = FALSE; if (self->treefile_context_dirs->len > 0) { GFile *prev = self->treefile_context_dirs->pdata[self->treefile_context_dirs->len-1]; if (g_file_equal (parent, prev)) existed = TRUE; } if (!existed) { g_ptr_array_add (self->treefile_context_dirs, parent); parent = NULL; /* Transfer ownership */ } } if (!_rpmostree_jsonutil_object_get_optional_string_member (root, "include", &include_path, error)) goto out; if (include_path) { gs_unref_object GFile *treefile_dirpath = g_file_get_parent (treefile_path); gs_unref_object GFile *parent_path = g_file_resolve_relative_path (treefile_dirpath, include_path); gs_unref_object JsonParser *parent_parser = json_parser_new (); JsonNode *parent_rootval; JsonObject *parent_root; GList *members; GList *iter; if (!json_parser_load_from_file (parent_parser, gs_file_get_path_cached (parent_path), error)) goto out; parent_rootval = json_parser_get_root (parent_parser); if (!JSON_NODE_HOLDS_OBJECT (parent_rootval)) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Treefile root is not an object"); goto out; } parent_root = json_node_get_object (parent_rootval); if (!process_includes (self, parent_path, depth + 1, parent_root, cancellable, error)) goto out; members = json_object_get_members (parent_root); for (iter = members; iter; iter = iter->next) { const char *name = iter->data; JsonNode *parent_val = json_object_get_member (parent_root, name); JsonNode *val = json_object_get_member (root, name); g_assert (parent_val); if (!val) json_object_set_member (root, name, json_node_copy (parent_val)); else { JsonNodeType parent_type = json_node_get_node_type (parent_val); JsonNodeType child_type = json_node_get_node_type (val); if (parent_type != child_type) { g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "Conflicting element type of '%s'", name); goto out; } if (child_type == JSON_NODE_ARRAY) { JsonArray *parent_array = json_node_get_array (parent_val); JsonArray *child_array = json_node_get_array (val); JsonArray *new_child = json_array_new (); guint i, len; len = json_array_get_length (parent_array); for (i = 0; i < len; i++) json_array_add_element (new_child, json_node_copy (json_array_get_element (parent_array, i))); len = json_array_get_length (child_array); for (i = 0; i < len; i++) json_array_add_element (new_child, json_node_copy (json_array_get_element (child_array, i))); json_object_set_array_member (root, name, new_child); } } } json_object_remove_member (root, "include"); } ret = TRUE; out: return ret; }
static gboolean parse_stream_options (CockpitChannel *self, CockpitConnectable *connectable) { gboolean ret = FALSE; GTlsCertificate *cert = NULL; GTlsDatabase *database = NULL; gboolean use_tls = FALSE; GError *error = NULL; GString *pem = NULL; JsonObject *options; JsonNode *node; /* No validation for local servers by default */ gboolean validate = !connectable->local; node = json_object_get_member (self->priv->open_options, "tls"); if (node && !JSON_NODE_HOLDS_OBJECT (node)) { cockpit_channel_fail (self, "protocol-error", "invalid \"tls\" option for channel"); goto out; } else if (node) { options = json_node_get_object (node); use_tls = TRUE; /* * The only function in GLib to parse private keys takes * them in PEM concatenated form. This is a limitation of GLib, * rather than concatenated form being a decent standard for * certificates and keys. So build a combined PEM as expected by * GLib here. */ pem = g_string_sized_new (8192); if (!parse_cert_option_as_pem (self, options, "certificate", pem)) goto out; if (pem->len) { if (!parse_cert_option_as_pem (self, options, "key", pem)) goto out; cert = g_tls_certificate_new_from_pem (pem->str, pem->len, &error); if (error != NULL) { cockpit_channel_fail (self, "internal-error", "invalid \"certificate\" or \"key\" content: %s", error->message); g_error_free (error); goto out; } } if (!parse_cert_option_as_database (self, options, "authority", &database)) goto out; if (!cockpit_json_get_bool (options, "validate", validate, &validate)) { cockpit_channel_fail (self, "protocol-error", "invalid \"validate\" option"); goto out; } } ret = TRUE; out: if (ret) { connectable->tls = use_tls; connectable->tls_cert = cert; cert = NULL; if (database) { connectable->tls_database = database; connectable->tls_flags = G_TLS_CERTIFICATE_VALIDATE_ALL; if (!validate) connectable->tls_flags &= ~(G_TLS_CERTIFICATE_INSECURE | G_TLS_CERTIFICATE_BAD_IDENTITY); database = NULL; } else { if (validate) connectable->tls_flags = G_TLS_CERTIFICATE_VALIDATE_ALL; else connectable->tls_flags = G_TLS_CERTIFICATE_GENERIC_ERROR; } } if (pem) g_string_free (pem, TRUE); if (cert) g_object_unref (cert); if (database) g_object_unref (database); return ret; }