From 9054fb2a72fd87057bcec26023dbfca92bbe8218 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Thu, 2 Nov 2017 07:33:14 +0100 Subject: [PATCH] Get rid of libgee It is said to be a bad idea to use in libraries anyway, but definitely makes porting back to C harder. --- configure.ac | 4 -- src/Makefile.am | 3 - src/Matrix-0.0.deps | 1 - src/matrix-event-receipt.vala | 96 +++++++++++++------------ src/matrix-event-room-power-levels.vala | 30 ++++---- src/matrix-glib-0.0.pc.in | 2 +- src/matrix-http-client.vala | 47 ++++++------ src/matrix-room.vala | 18 ++--- vala-globals.mk | 1 - 9 files changed, 104 insertions(+), 98 deletions(-) diff --git a/configure.ac b/configure.ac index 0a973eb..b688fe7 100644 --- a/configure.ac +++ b/configure.ac @@ -90,7 +90,6 @@ AC_SUBST([MATRIX_GLIB_VERSION], matrix_glib_base_version) GLIB_REQUIRED=2.40.0 VALA_REQUIRED=0.30.0 LIBVALA_REQUIRED=0.30 -GEE_REQUIRED=0.10.5 GIO_REQUIRED=2.22 SOUP_REQUIRED=2.44.2 JSON_REQUIRED=0.16.2 @@ -104,9 +103,6 @@ PKG_CHECK_MODULES([GLIB], # Check for vala VALAC_CHECK -# Check for libgee -PKG_CHECK_MODULES([GEE], [gee-0.8 >= $GEE_REQUIRED]) - # Check for GIO PKG_CHECK_MODULES([GIO], [gio-2.0 >= $GIO_REQUIRED]) diff --git a/src/Makefile.am b/src/Makefile.am index b3ebdd0..3aa7894 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -72,7 +72,6 @@ AM_CPPFLAGS += \ $(GOBJECT_CFLAGS) \ $(SOUP_CFLAGS) \ $(JSON_CFLAGS) \ - $(GEE_CFLAGS) \ $(VALA_CFLAGS) \ $(NULL) @@ -136,7 +135,6 @@ libmatrix_glib_0_0_la_CFLAGS = \ $(GOBJECT_CFLAGS) \ $(SOUP_CFLAGS) \ $(JSON_CFLAGS) \ - $(GEE_CFLAGS) \ $(NULL) libmatrix_glib_0_0_la_LIBADD = \ @@ -144,7 +142,6 @@ libmatrix_glib_0_0_la_LIBADD = \ $(GOBJECT_LIBS) \ $(SOUP_LIBS) \ $(JSON_LIBS) \ - $(GEE_LIBS) \ $(NULL) libmatrix_glib_0_0_la_LDFLAGS = \ diff --git a/src/Matrix-0.0.deps b/src/Matrix-0.0.deps index 784e0d5..b39c797 100644 --- a/src/Matrix-0.0.deps +++ b/src/Matrix-0.0.deps @@ -1,4 +1,3 @@ gio-2.0 Json-1.0 -gee-0.8 libsoup-2.4 diff --git a/src/matrix-event-receipt.vala b/src/matrix-event-receipt.vala index 634a1fd..2019eae 100644 --- a/src/matrix-event-receipt.vala +++ b/src/matrix-event-receipt.vala @@ -30,7 +30,7 @@ public class Matrix.Event.Receipt : Matrix.Event.Base { string user; } - private Gee.HashMap _receipt_data = null; + private HashTable _receipt_data = null; private static bool _rd_equal(ReceiptData k1, ReceiptData k2) @@ -43,7 +43,7 @@ public class Matrix.Event.Receipt : Matrix.Event.Base { private void _init_receipt_data() { - _receipt_data = new Gee.HashMap(null, (Gee.EqualDataFunc)_rd_equal); + _receipt_data = new HashTable(null, (EqualFunc)_rd_equal); } protected override void @@ -90,7 +90,7 @@ public class Matrix.Event.Receipt : Matrix.Event.Base { to_json(Json.Node json_data) throws Matrix.Error { - Json.Node? node; + Json.Node? node = null; if (_room_id == null) { throw new Matrix.Error.INCOMPLETE( @@ -100,57 +100,63 @@ public class Matrix.Event.Receipt : Matrix.Event.Base { var root = json_data.get_object(); var content_root = root.get_member("content").get_object(); int i = 0; + Matrix.Error? error = null; - foreach (var entry in _receipt_data.entries) { - Json.Object event_object; - Json.Object type_object; - Json.Object user_object; + _receipt_data.foreach( + (key, value) => { + Json.Object event_object; + Json.Object type_object; + Json.Object user_object; - if (entry.key.event_id == null) { - throw new Matrix.Error.INCOMPLETE( - "Won't generate a m.receipt event with an empty event ID"); - } + if (key.event_id == null) { + error = new Matrix.Error.INCOMPLETE( + "Won't generate a m.receipt event with an empty event ID"); + } - if (entry.key.typ == null) { - throw new Matrix.Error.INCOMPLETE( - "Won't generate a m.receipt event with an empty receipt type"); - } + if (key.typ == null) { + error = new Matrix.Error.INCOMPLETE( + "Won't generate a m.receipt event with an empty receipt type"); + } - if (entry.key.user == null) { - throw new Matrix.Error.INCOMPLETE( - "Won't generate a m.receipt event with an empty user ID"); - } + if (key.user == null) { + error = new Matrix.Error.INCOMPLETE( + "Won't generate a m.receipt event with an empty user ID"); + } - i++; + i++; - if ((node = content_root.get_member(entry.key.event_id)) == null) { - event_object = new Json.Object(); - node = new Json.Node(Json.NodeType.OBJECT); - node.set_object(event_object); - content_root.set_member(entry.key.event_id, node); - } else { - event_object = node.get_object(); - } + if ((node = content_root.get_member(key.event_id)) == null) { + event_object = new Json.Object(); + node = new Json.Node(Json.NodeType.OBJECT); + node.set_object(event_object); + content_root.set_member(key.event_id, node); + } else { + event_object = node.get_object(); + } - if ((node = event_object.get_member(entry.key.typ)) == null) { - type_object = new Json.Object(); - node = new Json.Node(Json.NodeType.OBJECT); - node.set_object(type_object); - event_object.set_member(entry.key.typ, node); - } else { - type_object = node.get_object(); - } + if ((node = event_object.get_member(key.typ)) == null) { + type_object = new Json.Object(); + node = new Json.Node(Json.NodeType.OBJECT); + node.set_object(type_object); + event_object.set_member(key.typ, node); + } else { + type_object = node.get_object(); + } - if ((node = type_object.get_member(entry.key.user)) == null) { - user_object = new Json.Object(); - node = new Json.Node(Json.NodeType.OBJECT); - node.set_object(user_object); - type_object.set_member(entry.key.user, node); - } else { - user_object = node.get_object(); - } + if ((node = type_object.get_member(key.user)) == null) { + user_object = new Json.Object(); + node = new Json.Node(Json.NodeType.OBJECT); + node.set_object(user_object); + type_object.set_member(key.user, node); + } else { + user_object = node.get_object(); + } - user_object.set_int_member("ts", entry.value); + user_object.set_int_member("ts", value); + }); + + if (error != null) { + throw error; } if (i == 0) { diff --git a/src/matrix-event-room-power-levels.vala b/src/matrix-event-room-power-levels.vala index dd0c782..49b26f6 100644 --- a/src/matrix-event-room-power-levels.vala +++ b/src/matrix-event-room-power-levels.vala @@ -73,7 +73,7 @@ public class Matrix.Event.RoomPowerLevels : Matrix.Event.State { /** * A hash map to store the required level to send specific events. */ - public Gee.HashMap event_levels { + public HashTable event_levels { get { return _event_levels; } @@ -82,17 +82,17 @@ public class Matrix.Event.RoomPowerLevels : Matrix.Event.State { /** * A hash map to store current level for individual users. */ - public Gee.HashMap user_levels { + public HashTable user_levels { get { return _user_levels; } } - private Gee.HashMap _event_levels = - new Gee.HashMap(); + private HashTable _event_levels = + new HashTable(str_hash, str_equal); - private Gee.HashMap _user_levels = - new Gee.HashMap(); + private HashTable _user_levels = + new HashTable(str_hash, str_equal); protected override void from_json(Json.Node json_data) @@ -147,7 +147,7 @@ public class Matrix.Event.RoomPowerLevels : Matrix.Event.State { } if ((node = content_root.get_member("events")) != null) { - _event_levels.clear(); + _event_levels.remove_all(); node.get_object().foreach_member((obj, event_name, event_node) => { _event_levels[event_name] = (int)event_node.get_int(); @@ -155,7 +155,7 @@ public class Matrix.Event.RoomPowerLevels : Matrix.Event.State { } if ((node = content_root.get_member("users")) != null) { - _user_levels.clear(); + _user_levels.remove_all(); node.get_object().foreach_member((obj, user_id, user_node) => { _user_levels[user_id] = (int)user_node.get_int(); @@ -197,9 +197,10 @@ public class Matrix.Event.RoomPowerLevels : Matrix.Event.State { var user_node = new Json.Node(Json.NodeType.OBJECT); user_node.set_object(user_obj); - foreach (var entry in _user_levels.entries) { - user_obj.set_int_member(entry.key, entry.value); - } + _user_levels.foreach( + (key, value) => { + user_obj.set_int_member(key, value); + }); content_root.set_member("users", user_node); @@ -207,9 +208,10 @@ public class Matrix.Event.RoomPowerLevels : Matrix.Event.State { var events_node = new Json.Node(Json.NodeType.OBJECT); events_node.set_object(events_obj); - foreach (var entry in _event_levels.entries) { - events_obj.set_int_member(entry.key, entry.value); - } + _event_levels.foreach( + (key, value) => { + events_obj.set_int_member(key, value); + }); content_root.set_member("users", events_node); diff --git a/src/matrix-glib-0.0.pc.in b/src/matrix-glib-0.0.pc.in index 9c1c242..4454a16 100644 --- a/src/matrix-glib-0.0.pc.in +++ b/src/matrix-glib-0.0.pc.in @@ -9,6 +9,6 @@ Name: libmatrix-glib Description: GObject API for communicating with a Matrix.org Homeserver URL: http://gergely.polonkai.eu/matrix-glib-sdk Version: @MATRIX_GLIB_VERSION@ -Requires: glib-2.0 gobject-2.0 gee-0.8 gio-2.0 json-glib-1.0 +Requires: glib-2.0 gobject-2.0 gio-2.0 json-glib-1.0 Libs: -L${libdir} -lmatrix-glib-@MATRIX_GLIB_API_VERSION@ Cflags: -I${includedir}/matrix-glib-@MATRIX_GLIB_API_VERSION@ diff --git a/src/matrix-http-client.vala b/src/matrix-http-client.vala index 8d67495..bced438 100644 --- a/src/matrix-http-client.vala +++ b/src/matrix-http-client.vala @@ -24,12 +24,12 @@ public class Matrix.HTTPClient : Matrix.HTTPAPI, Matrix.Client { private bool _polling = false; private ulong _event_timeout = 30000; private string? _last_sync_token; - private Gee.HashMap _user_global_profiles = - new Gee.HashMap(); - private Gee.HashMap _user_global_presence = - new Gee.HashMap(); - private Gee.HashMap _rooms = - new Gee.HashMap(); + private HashTable _user_global_profiles = + new HashTable(str_hash, str_equal); + private HashTable _user_global_presence = + new HashTable(str_hash, str_equal); + private HashTable _rooms = + new HashTable(str_hash, str_equal); private ulong _last_txn_id = 0; public @@ -211,13 +211,15 @@ public class Matrix.HTTPClient : Matrix.HTTPAPI, Matrix.Client { room.clear_user_levels(); room.clear_event_levels(); - foreach (var entry in levt.user_levels.entries) { - room.set_user_level(entry.key, entry.value); - } + levt.user_levels.foreach( + (key, value) => { + room.set_user_level(key, value); + }); - foreach (var entry in levt.event_levels.entries) { - room.set_event_level(entry.key, entry.value); - } + levt.event_levels.foreach( + (key, value) => { + room.set_event_level(key, value); + }); } else if (evt is Matrix.Event.RoomTopic) { var tevt = (Matrix.Event.RoomTopic)evt; @@ -501,16 +503,21 @@ public class Matrix.HTTPClient : Matrix.HTTPAPI, Matrix.Client { get_room_by_alias(string room_alias) throws Matrix.Error { - foreach (var entry in _rooms.entries) { - var room = entry.value; + Room? room_found = _rooms.find( + (key, room) => { + if (room.canonical_alias == room_alias) { + return true; + } - if (room.canonical_alias == room_alias) { - return room; - } + if (room_alias in room.aliases) { + return true; + } - if (room_alias in room.aliases) { - return room; - } + return false; + }); + + if (room_found != null) { + return room_found; } throw new Matrix.Error.UNAVAILABLE( diff --git a/src/matrix-room.vala b/src/matrix-room.vala index 33bde0d..25279ac 100644 --- a/src/matrix-room.vala +++ b/src/matrix-room.vala @@ -141,19 +141,19 @@ public class Matrix.Room : GLib.Object { */ public string[] typing_users { get; set; } - private Gee.HashMap _event_levels = - new Gee.HashMap(); + private HashTable _event_levels = + new HashTable(str_hash, str_equal); - private Gee.HashMap _user_levels = - new Gee.HashMap(); + private HashTable _user_levels = + new HashTable(str_hash, str_equal); private struct MemberData { Profile profile; bool thirdparty; } - private Gee.HashMap _members = - new Gee.HashMap(); + private HashTable _members = + new HashTable(str_hash, str_equal); /** * Create a new Room object. @@ -260,7 +260,7 @@ public class Matrix.Room : GLib.Object { "No such room member"); } - _members.unset(user_id); + _members.remove(user_id); } /** @@ -270,7 +270,7 @@ public class Matrix.Room : GLib.Object { public void clear_user_levels() { - _user_levels.clear(); + _user_levels.remove_all(); } /** @@ -312,7 +312,7 @@ public class Matrix.Room : GLib.Object { public void clear_event_levels() { - _event_levels.clear(); + _event_levels.remove_all(); } /** diff --git a/vala-globals.mk b/vala-globals.mk index 82a832a..cdfc1a9 100644 --- a/vala-globals.mk +++ b/vala-globals.mk @@ -2,7 +2,6 @@ VALA_PKG_LIST = \ --pkg=gio-2.0 \ --pkg=json-glib-1.0 \ --pkg=libsoup-2.4 \ - --pkg=gee-0.8 \ --pkg=c-api \ --pkg=config \ $(NULL)