GtkWidget *trg_json_widget_spin_double_new(GList ** wl, JsonObject * obj,
                                    const gchar * key,
                                    GtkWidget * toggleDep, gdouble min,
                                    gdouble max, gdouble step)
{
    GtkWidget *w = gtk_spin_button_new_with_range(min, max, step);

    trg_json_widget_desc *wd = g_new0(trg_json_widget_desc, 1);

    wd->saveFunc = trg_json_widget_spin_double_save;
    wd->key = g_strdup(key);
    wd->widget = w;

    if (toggleDep) {
        gtk_widget_set_sensitive(w,
                                 gtk_toggle_button_get_active
                                 (GTK_TOGGLE_BUTTON(toggleDep)));
        g_signal_connect(G_OBJECT(toggleDep), "toggled",
                         G_CALLBACK(toggle_active_arg_is_sensitive), w);
    }

    gtk_spin_button_set_value(GTK_SPIN_BUTTON(w),
                              json_object_get_double_member(obj, key));

    *wl = g_list_append(*wl, wd);

    return w;
}
/**
 * couchdb_struct_field_get_double_field:
 * @sf: A #CouchdbStructField object
 * @field: Name of the field
 *
 * Retrieve the value of a decimal number field from the given struct field.
 *
 * Return value: The value of the given field.
 */
gdouble
couchdb_struct_field_get_double_field (CouchdbStructField *sf, const char *field)
{
	g_return_val_if_fail (sf != NULL, 0);
	g_return_val_if_fail (field != NULL, 0);

	return json_object_get_double_member (sf->json_object, field);
}
Exemple #3
0
static void
test_response_data (gconstpointer data)
{
        JsonParser *parser;
        JsonNode *node;
        JsonObject *object;
        double latitude, longitude;

        parser = json_parser_new ();

        g_assert (json_parser_load_from_data (parser, (const char *) data, -1, NULL));

        node = json_parser_get_root (parser);
        g_assert (JSON_NODE_HOLDS_OBJECT (node));
        object = json_node_get_object (node);
        g_assert (object != NULL);

        g_assert (json_object_has_member (object, "ip"));
        g_assert (strcmp (json_object_get_string_member (object, "ip"), "213.243.180.91") == 0);

        g_assert (json_object_has_member (object, "latitude"));
        latitude = json_object_get_double_member (object, "latitude");
        g_assert (latitude > 60.1755 && latitude <= 60.1756);
        g_assert (json_object_has_member (object, "longitude"));
        longitude = json_object_get_double_member (object, "longitude");
        g_assert (longitude >= 24.9342 && longitude < 24.9343);

        g_assert (json_object_has_member (object, "city"));
        g_assert (strcmp (json_object_get_string_member (object, "city"), "Helsinki") == 0);
        g_assert (json_object_has_member (object, "region_name"));
        g_assert (strcmp (json_object_get_string_member (object, "region_name"), "Southern Finland") == 0);
        g_assert (json_object_has_member (object, "country_name"));
        g_assert (strcmp (json_object_get_string_member (object, "country_name"), "Finland") == 0);

        if (json_object_has_member (object, "accuracy"))
            g_assert (strcmp (json_object_get_string_member (object, "accuracy"), "city") == 0);
        if (json_object_has_member (object, "timezone"))
            g_assert (strcmp (json_object_get_string_member (object, "timezone"), "Europe/Helsinki") == 0);

        g_object_unref (parser);
}
static void flist_sfc_report(PurpleConnection *pc, JsonObject *root) {
    PurpleAccount *pa = purple_connection_get_account(pc);
    const gchar *callid, *reporter, *report;
    gchar *s, *escaped_reporter, *escaped_report, *message;
    GString *message_str;
    gdouble timestamp;
    gint logid; gboolean has_logid;

    callid = json_object_get_string_member(root, "callid");
    reporter = json_object_get_string_member(root, "character");
    report = json_object_get_string_member(root, "report");
    logid = json_object_get_parse_int_member(root, "logid", &has_logid);
    timestamp = json_object_get_double_member(root, "timestamp");

    g_return_if_fail(callid);
    g_return_if_fail(reporter);
    g_return_if_fail(report);

    message_str = g_string_new(NULL);
    s = g_strdup(purple_url_encode(flist_serialize_account(pa)));
    escaped_reporter = purple_markup_escape_text(reporter, -1);
    escaped_report = purple_markup_escape_text(report, -1);

    g_string_append_printf(message_str, "Moderator Alert. %s writes:\n", escaped_reporter);
    g_string_append_printf(message_str, "%s\n", escaped_report);
    g_string_append_printf(message_str, "<a href=\"flistsfc://%s/%s\">Confirm Alert</a>", s, purple_url_encode(callid));
    g_string_append(message_str, ", ");
    if(has_logid) {
        g_string_append_printf(message_str, "<a href=\"http://www.f-list.net/fchat/getLog.php?log=%d\">View Log</a>", logid);
    } else {
        g_string_append_printf(message_str, "(No Log)");
    }

    message = g_string_free(message_str, FALSE);
    serv_got_im(pc, GLOBAL_NAME, message, PURPLE_MESSAGE_RECV, time(NULL));

    g_free(escaped_report);
    g_free(escaped_reporter);
    g_free(message);
    g_free(s);
}
static void flist_sfc_confirm(PurpleConnection *pc, JsonObject *root) {
    FListAccount *fla = pc->proto_data;
    const gchar *moderator, *reporter;
    gchar *message, *escaped_message, *bbcode_message;
    gdouble timestamp;

    moderator = json_object_get_string_member(root, "moderator");
    reporter = json_object_get_string_member(root, "character");
    timestamp = json_object_get_double_member(root, "timestamp");

    g_return_if_fail(moderator);
    g_return_if_fail(reporter);

    message = g_strdup_printf("Alert Confirmed. [b]%s[/b] is handling [b]%s[/b]'s report.", moderator, reporter);
    escaped_message = purple_markup_escape_text(message, -1);
    bbcode_message = flist_bbcode_to_html(fla, NULL, escaped_message);
    serv_got_im(pc, GLOBAL_NAME, bbcode_message, PURPLE_MESSAGE_RECV, time(NULL));
    g_free(bbcode_message);
    g_free(escaped_message);
    g_free(message);
}
Exemple #6
0
// FIXME: check for memory leaks
JSONcontext* Network::readJSON(const char* file){
   JsonParser *parser;
   JsonObject *obj;
   JsonArray *jnodes;
   JsonArray *jedges;
   int i,k;
   JSONcontext* ctx=new JSONcontext();
   GError *error;
   g_type_init ();
   parser = json_parser_new ();
   
   error = NULL;
   json_parser_load_from_file (parser, file, &error);
   if (error)
   {
      g_print ("Unable to parse `%s': %s\n", file, error->message);
      g_error_free (error);
      g_object_unref (parser);
      abort();
   }
   addCompartment(0,"unknown"); // add dummy compartment, may be overwritten by compartment from json
   ctx->root = json_node_copy(json_parser_get_root(parser)); // needs to be copied ??
   obj=json_node_get_object (ctx->root);
   jnodes=json_object_get_array_member(obj,"nodes");
   jedges=json_object_get_array_member(obj,"edges");
   g_print ("%i nodes\n",json_array_get_length(jnodes));
   for (i=0;i<json_array_get_length(jnodes);i++){
      JsonObject *n=json_array_get_object_element(jnodes,i);
      JsonObject *data=json_object_get_object_member(n,"data");
      const char* type=json_object_get_string_member(n,"type");
      const char* id=json_object_get_string_member(n,"id");
      if (json_object_has_member(n,"is_abstract") && (json_object_get_boolean_member(n,"is_abstract") || json_object_get_int_member(n,"is_abstract"))){
         continue;
      }
      if (!json_object_has_member(n,"index")){
         g_print ("no index defined for %s %s\n",type,id);
         abort();
      }
      int idx=json_object_get_int_member(n,"index");
      g_print ("node %d %s id %s\n",idx,type,id);
      if(strcmp(type,"Compartment")==0){
         addCompartment(idx,id);
      } else {
         double x=json_object_get_double_member(data,"x");
         double y=json_object_get_double_member(data,"y");
         double w=json_object_get_double_member(data,"width");
         double h=json_object_get_double_member(data,"height");
         double d=json_object_get_double_member(data,"dir");
         int c=json_object_get_int_member(data,"compartmentidx");
         for(k=0;k<7;k++){
            if(strcmp(jnodetypes[k],type)==0)break;
         }
         if (k==7){
            g_print ("unknown node type %s\n",type);
            abort();
         }
         addNode(idx,inodetypes[k],id,w,h,x,y,d,c);
         if (idx>=ctx->nodeidx->size()) ctx->nodeidx->resize(idx+1);
         (*(ctx->nodeidx))[idx]=i;
      }
   }
   g_print ("%i edges\n",json_array_get_length(jedges));
   for (i=0;i<json_array_get_length(jedges);i++){
      JsonObject *e=json_array_get_object_element(jedges,i);
      const char* type=json_object_get_string_member(e,"type");
      if ((!json_object_has_member(e,"sourceidx")) || (!json_object_has_member(e,"targetidx"))){
         g_print ("no source/targetindex defined for edge %s %i",type,i);
         abort;
      }
      for(k=0;k<7;k++){
         if(strcmp(edgetypes[k],type)==0)break;
      }
      int from=json_object_get_int_member(e,"sourceidx");
      int to=json_object_get_int_member(e,"targetidx");
      addEdge(from,to,(Edgetype)k);
      g_print ("edge %s %i -> %i\n",edgetypes[k],from,to);
   }
   g_object_unref(parser);
   return ctx;
}
static void
pb_got_everything(PushBulletAccount *pba, JsonNode *node, gpointer user_data)
{
	JsonObject *rootobj = json_node_get_object(node);
	JsonArray *devices = json_object_has_member(rootobj, "devices") ? json_object_get_array_member(rootobj, "devices") : NULL;
	JsonArray *pushes = json_object_has_member(rootobj, "pushes") ? json_object_get_array_member(rootobj, "pushes") : NULL;
	JsonArray *contacts = json_object_has_member(rootobj, "contacts") ? json_object_get_array_member(rootobj, "contacts") : NULL;
	JsonArray *chats = json_object_has_member(rootobj, "chats") ? json_object_get_array_member(rootobj, "chats") : NULL;
	JsonArray *texts = json_object_has_member(rootobj, "texts") ? json_object_get_array_member(rootobj, "texts") : NULL;
	gint i;
	guint len;
	PurpleGroup *pbgroup;
	
	pbgroup = purple_find_group("PushBullet");
	if (!pbgroup)
	{
		pbgroup = purple_group_new("PushBullet");
		purple_blist_add_group(pbgroup, NULL);
	}
	
	if (json_object_has_member(rootobj, "error")) {
		JsonObject *error = json_object_get_object_member(rootobj, "error");
		const gchar *type = json_object_get_string_member(error, "type");
		const gchar *message = json_object_get_string_member(error, "message");
		
		//TODO check type
		purple_connection_error_reason(pba->pc, PURPLE_CONNECTION_ERROR_AUTHENTICATION_FAILED, message);
		return;
	}
	
	if (devices != NULL) {
		for(i = 0, len = json_array_get_length(devices); i < len; i++) {
			JsonObject *device = json_array_get_object_element(devices, i);
			
			if (pba->main_sms_device == NULL && json_object_get_boolean_member(device, "has_sms")) {
				pba->main_sms_device = g_strdup(json_object_get_string_member(device, "iden"));
				purple_account_set_string(pba->account, "main_sms_device", pba->main_sms_device);
				
				pb_get_phonebook(pba, pba->main_sms_device);
				
				if (!pba->websocket) {
					pb_start_polling(pba);
				}
				
				break; //TODO handle more than one
			}
		}
	}
	
	if (pushes != NULL) {
		gint last_message_timestamp = purple_account_get_int(pba->account, "last_message_timestamp", 0);
		for(i = json_array_get_length(pushes); i > 0; i--) {
			JsonObject *push = json_array_get_object_element(pushes, i - 1);
			const gchar *type = json_object_get_string_member(push, "type");
			gdouble modified;
			time_t timestamp;
			gboolean dismissed;
			
			if (!type)
				continue;
			
			modified = json_object_get_double_member(push, "modified");
			timestamp = (time_t) modified;
			dismissed = json_object_get_boolean_member(push, "dismissed");
			
			if (timestamp <= last_message_timestamp || dismissed) {
				continue;
			}
			
			// {"active":true,"iden":"uffvytgsjApuAUIFRk","created":1.438895081423904e+09,"modified":1.438895081432786e+09,"type":"file","dismissed":false,"guid":"153b70f0-f7a6-4db9-a6f4-28b99fa416f1","direction":"self","sender_iden":"uffvytg","sender_email":"*****@*****.**","sender_email_normalized":"*****@*****.**","sender_name":"Eion Robb","receiver_iden":"uffvytg","receiver_email":"*****@*****.**","receiver_email_normalized":"*****@*****.**","target_device_iden":"uffvytgsjz7O3P0Jl6","source_device_iden":"uffvytgsjAoIRwhIL6","file_name":"IMG_20150807_084618.jpg","file_type":"image/jpeg","file_url":"https://dl.pushbulletusercontent.com/FHOZdyzfvnoYZY0DP6oK1rGKiJpWCPc0/IMG_20150807_084618.jpg","image_width":4128,"image_height":2322,"image_url":"https://lh3.googleusercontent.com/WY5TK7h3mzD32qMcnxtqt-4PrYcWW1uWDHnRW2x1oJK8mnYk2v4HbZrRjIQkiYdxMKQSdNI8GGPqfO6s6tEyuRVLzeA"}
			
			if (purple_strequal(type, "note") || purple_strequal(type, "link") || purple_strequal(type, "file")) {
				const gchar *from = json_object_get_string_member(push, "sender_email_normalized");
				const gchar *body = json_object_get_string_member(push, "body");
				const gchar *direction = json_object_get_string_member(push, "direction");
				gchar *body_html;
				
				if (from == NULL) {
					if (!json_object_has_member(push, "sender_name")) {
						purple_debug_error("pushbullet", "no sender name/email\n");
						continue;
					}
					from = json_object_get_string_member(push, "sender_name");
				}
				
				if (body && *body) {
					body_html = purple_strdup_withhtml(body);
				} else {
					const gchar *title = json_object_get_string_member(push, "title");
					if (title && *title) {
						body_html = purple_strdup_withhtml(title);
					} else {
						body_html = "Message";
					}
				}
				
				if (json_object_has_member(push, "url")) {
					gchar *body_with_link = g_strconcat("<a href=\"", json_object_get_string_member(push, "url"), "\">", body_html, "</a>", NULL);
					g_free(body_html);
					body_html = body_with_link;
					
				} else if (json_object_has_member(push, "image_url")) {
					const gchar *image_url = json_object_get_string_member(push, "image_url");
					PurpleConversation *conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, from, pba->account);
					
					if (conv == NULL)
					{
						conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, pba->account, from);
					}
					pb_download_image_to_conv(image_url, conv);
					
				} else if (json_object_has_member(push, "file_url")) {
					gchar *body_with_link;
					const gchar *file_name = json_object_get_string_member(push, "file_name");
					
					if (file_name && *file_name) {
						g_free(body_html);
						body_html = purple_strdup_withhtml(file_name);
					}
					
					body_with_link= g_strconcat("<a href=\"", json_object_get_string_member(push, "file_url"), "\">", json_object_get_string_member(push, "file_name"), "</a>", NULL);
					g_free(body_html);
					body_html = body_with_link;
				}
				
				if (direction[0] != 'o') {
					serv_got_im(pba->pc, from, body_html, PURPLE_MESSAGE_RECV, timestamp);
				} else {
					const gchar *guid = json_object_get_string_member(push, "guid");
					from = json_object_get_string_member(push, "receiver_email_normalized");
					
					if (!guid || !g_hash_table_remove(pba->sent_messages_hash, guid)) {
						PurpleConversation *conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, from, pba->account);
						if (conv == NULL)
						{
							conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, pba->account, from);
						}
						purple_conversation_write(conv, from, body_html, PURPLE_MESSAGE_SEND, timestamp);
					}
				}
				
				g_free(body_html);
			}
				
			purple_account_set_int(pba->account, "last_message_timestamp", MAX(purple_account_get_int(pba->account, "last_message_timestamp", 0), timestamp));
		}
	}
	
	if (contacts != NULL) {
		for(i = 0, len = json_array_get_length(contacts); i < len; i++) {
			JsonObject *contact = json_array_get_object_element(contacts, i);
			const gchar *email = json_object_get_string_member(contact, "email_normalized");
			const gchar *name = json_object_get_string_member(contact, "name");
			const gchar *image_url = json_object_get_string_member(contact, "image_url");
			
			PurpleBuddy *buddy = purple_find_buddy(pba->account, email);
			if (buddy == NULL)
			{
				buddy = purple_buddy_new(pba->account, email, name);
				purple_blist_add_buddy(buddy, NULL, pbgroup, NULL);
			}
			purple_prpl_got_user_status(pba->account, email, purple_primitive_get_id_from_type(PURPLE_STATUS_AVAILABLE), NULL);
		}
	}
	
	if (chats != NULL) {
		for(i = 0, len = json_array_get_length(chats); i < len; i++) {
			JsonObject *chat = json_array_get_object_element(chats, i);
			JsonObject *contact = json_object_get_object_member(chat, "with");
			const gchar *email = json_object_get_string_member(contact, "email_normalized");
			const gchar *name = json_object_get_string_member(contact, "name");
			const gchar *image_url = json_object_get_string_member(contact, "image_url");
			
			PurpleBuddy *buddy = purple_find_buddy(pba->account, email);
			if (buddy == NULL)
			{
				buddy = purple_buddy_new(pba->account, email, name);
				purple_blist_add_buddy(buddy, NULL, pbgroup, NULL);
			}
			purple_prpl_got_user_status(pba->account, email, purple_primitive_get_id_from_type(PURPLE_STATUS_AVAILABLE), NULL);
		}
	}
	
	if (texts != NULL) {
		for(i = 0, len = json_array_get_length(texts); i < len; i++) {
			JsonObject *text = json_array_get_object_element(texts, i);
		}
	}
}
Exemple #8
0
i3ipcCon *i3ipc_con_new(i3ipcCon *parent, JsonObject *data, i3ipcConnection *conn) {
  i3ipcCon *con;
  con = g_object_new(I3IPC_TYPE_CON, NULL);

  g_object_ref(conn);
  con->priv->conn = conn;

  if (!json_object_get_null_member(data, "percent"))
    con->priv->percent = json_object_get_double_member(data, "percent");

  if (!json_object_get_null_member(data, "window"))
    con->priv->window = json_object_get_int_member(data, "window");

  if (json_object_has_member(data, "window_properties")) {
    JsonObject *window_properties = json_object_get_object_member(data, "window_properties");

    if (json_object_has_member(window_properties, "class"))
      con->priv->window_class = g_strdup(json_object_get_string_member(window_properties, "class"));
  }

  if (json_object_has_member(data, "mark")) {
    con->priv->mark = g_strdup(json_object_get_string_member(data, "mark"));
  }

  con->priv->name = g_strdup(json_object_get_string_member(data, "name"));
  con->priv->focused = json_object_get_boolean_member(data, "focused");
  con->priv->fullscreen_mode = json_object_get_boolean_member(data, "fullscreen_mode");
  con->priv->urgent = json_object_get_boolean_member(data, "urgent");
  con->priv->layout = g_strdup(json_object_get_string_member(data, "layout"));
  con->priv->orientation = g_strdup(json_object_get_string_member(data, "orientation"));
  con->priv->current_border_width = json_object_get_int_member(data, "current_border_width");
  con->priv->border = g_strdup(json_object_get_string_member(data, "border"));
  con->priv->id = json_object_get_int_member(data, "id");

  JsonNode *con_type_node = json_object_get_member(data, "type");

  /* XXX: In the development version, the "type" property is a string of the
   * type, but in the current stable version (4.7.2) it is an integer as
   * defined in i3's data header. When the next version comes out, the case
   * where type is a number should be removed. */
  if (json_node_get_value_type(con_type_node) == G_TYPE_STRING) {
    con->priv->type = g_strdup(json_node_get_string(con_type_node));
  } else {
    int con_type_int = (int)json_node_get_int(con_type_node);
    switch (con_type_int)
    {
      case 0:
        con->priv->type = g_strdup("root");
        break;
      case 1:
        con->priv->type = g_strdup("output");
        break;
      case 2:
      case 3:
        con->priv->type = g_strdup("con");
        break;
      case 4:
        con->priv->type = g_strdup("workspace");
        break;
      case 5:
        con->priv->type = g_strdup("dockarea");
        break;
    }
  }

  if (parent) {
    g_object_ref(parent);
    con->priv->parent = parent;
  }

  JsonObject *rect_data = json_object_get_object_member(data, "rect");

  con->priv->rect->x = json_object_get_int_member(rect_data, "x");
  con->priv->rect->y = json_object_get_int_member(rect_data, "y");
  con->priv->rect->width = json_object_get_int_member(rect_data, "width");
  con->priv->rect->height = json_object_get_int_member(rect_data, "height");

  if (json_object_has_member(data, "deco_rect")) {
    JsonObject *deco_rect_data = json_object_get_object_member(data, "deco_rect");

    con->priv->deco_rect->x = json_object_get_int_member(deco_rect_data, "x");
    con->priv->deco_rect->y = json_object_get_int_member(deco_rect_data, "y");
    con->priv->deco_rect->width = json_object_get_int_member(deco_rect_data, "width");
    con->priv->deco_rect->height = json_object_get_int_member(deco_rect_data, "height");
  }

  JsonArray *nodes_array = json_object_get_array_member(data, "nodes");
  json_array_foreach_element(nodes_array, i3ipc_con_initialize_nodes, con);

  JsonArray *floating_nodes_array = json_object_get_array_member(data, "floating_nodes");
  json_array_foreach_element(floating_nodes_array, i3ipc_con_initialize_floating_nodes, con);

  JsonArray *focus_array = json_object_get_array_member(data, "focus");
  guint len = json_array_get_length(focus_array);
  for (int i = 0; i < len; i += 1) {
    con->priv->focus = g_list_append(con->priv->focus, GINT_TO_POINTER(json_array_get_int_element(focus_array, i)));
  }

  return con;
}