Refactor ImageInfo

* Add a set_from_json() method
* Start using it in the m.room.avatar handler
This commit is contained in:
Gergely Polonkai 2016-03-10 17:46:16 +01:00 committed by Gergely Polonkai
parent 0bcb72d5aa
commit 5bb3620395
2 changed files with 38 additions and 50 deletions

View File

@ -72,61 +72,13 @@ public class Matrix.Event.RoomAvatar : Matrix.Event.State {
} }
if ((node = content_root.get_member("info")) != null) { if ((node = content_root.get_member("info")) != null) {
var info_obj = node.get_object();
_info = ImageInfo(); _info = ImageInfo();
_info.set_from_json(node);
if ((node = info_obj.get_member("size")) != null) {
_info.size = (int)node.get_int();
} else {
warning("content.info.size is missing from a m.room.avatar event");
}
if ((node = info_obj.get_member("h")) != null) {
_info.height = (int)node.get_int();
} else {
warning("content.info.h is missing from a m.room.avatar event");
}
if ((node = info_obj.get_member("w")) != null) {
_info.width = (int)node.get_int();
} else {
warning("content.info.w is missing from a m.room.avatar event");
}
if ((node = info_obj.get_member("mimetype")) != null) {
_info.mimetype = node.get_string();
} else {
warning("content.info.mimetype is missing from a m.room.avatar event");
}
} }
if ((node = content_root.get_member("thumbnail_info")) != null) { if ((node = content_root.get_member("thumbnail_info")) != null) {
var thumbnail_info_obj = node.get_object();
_thumbnail_info = ImageInfo(); _thumbnail_info = ImageInfo();
_thumbnail_info.set_from_json(node);
if ((node = thumbnail_info_obj.get_member("size")) != null) {
_thumbnail_info.size = (int)node.get_int();
} else {
warning("content.thumbnail_info.size is missing from a m.room.avatar event");
}
if ((node = thumbnail_info_obj.get_member("h")) != null) {
_thumbnail_info.height = (int)node.get_int();
} else {
warning("content.thumbnail_info.h is missing from a m.room.avatar event");
}
if ((node = thumbnail_info_obj.get_member("w")) != null) {
_thumbnail_info.width = (int)node.get_int();
} else {
warning("content.thumbnail_info.w is missing from a m.room.avatar event");
}
if ((node = thumbnail_info_obj.get_member("mimetype")) != null) {
_thumbnail_info.mimetype = node.get_string();
} else {
warning("content.thumbnail_info.mimetype is missing from a m.room.avatar event");
}
} }
base.from_json(json_data); base.from_json(json_data);

View File

@ -297,6 +297,42 @@ namespace Matrix {
int? width; int? width;
string? mimetype; string? mimetype;
public void
set_from_json(Json.Node json_data)
{
size = null;
mimetype = null;
height = null;
width = null;
var root = json_data.get_object();
Json.Node? node;
if ((node = root.get_member("w")) != null) {
width = (int)node.get_int();
} else if (Config.DEBUG) {
warning("w is missing from an ImageInfo");
}
if ((node = root.get_member("h")) != null) {
height = (int)node.get_int();
} else if (Config.DEBUG) {
warning("h is missing from an ImageInfo");
}
if ((node = root.get_member("size")) != null) {
size = (int)node.get_int();
} else if (Config.DEBUG) {
warning("size is missing from an ImageInfo");
}
if ((node = root.get_member("mimetype")) != null) {
mimetype = node.get_string();
} else if (Config.DEBUG) {
warning("mimetype is missing from an ImageInfo");
}
}
public Json.Node public Json.Node
get_json_node() get_json_node()
throws Matrix.Error throws Matrix.Error