diff --git a/src/matrix-event-call-answer.vala b/src/matrix-event-call-answer.vala index cdfc63b..3b621a2 100644 --- a/src/matrix-event-call-answer.vala +++ b/src/matrix-event-call-answer.vala @@ -42,12 +42,10 @@ public class Matrix.Event.CallAnswer : Matrix.Event.Call { var answer_root = node.get_object(); if ((node = answer_root.get_member("type")) != null) { - CallAnswerType? typ = (CallAnswerType?)_g_enum_nick_to_value( - typeof(CallAnswerType), node.get_string()); - - if (typ != null) { - _answer_type = typ; - } else { + try { + _answer_type = (CallAnswerType)_g_enum_nick_to_value( + typeof(CallAnswerType), node.get_string()); + } catch (Matrix.Error e) { _answer_type = CallAnswerType.UNKNOWN; if (Config.DEBUG) { diff --git a/src/matrix-event-call-invite.vala b/src/matrix-event-call-invite.vala index 179c44b..fe16b3a 100644 --- a/src/matrix-event-call-invite.vala +++ b/src/matrix-event-call-invite.vala @@ -50,12 +50,10 @@ public class Matrix.Event.CallInvite : Matrix.Event.Call { var offer_node = node.get_object(); if ((node = offer_node.get_member("type")) != null) { - CallOfferType? typ = (CallOfferType?)_g_enum_nick_to_value( - typeof(CallOfferType), node.get_string()); - - if (typ != null) { - _offer_type = typ; - } else { + try { + _offer_type = (CallOfferType)_g_enum_nick_to_value( + typeof(CallOfferType), node.get_string()); + } catch (Matrix.Error e) { _offer_type = CallOfferType.UNKNOWN; if (Config.DEBUG) { diff --git a/src/matrix-event-presence.vala b/src/matrix-event-presence.vala index 4a7d083..d731538 100644 --- a/src/matrix-event-presence.vala +++ b/src/matrix-event-presence.vala @@ -95,12 +95,10 @@ public class Matrix.Event.Presence : Matrix.Event.Base { } if ((node = content_root.get_member("presence")) != null) { - Matrix.Presence? pres = (Matrix.Presence?)_g_enum_nick_to_value( - typeof(Matrix.Presence), node.get_string()); - - if (pres != null) { - _presence = pres; - } else { + try { + _presence = (Matrix.Presence)_g_enum_nick_to_value( + typeof(Matrix.Presence), node.get_string()); + } catch (Matrix.Error e) { _presence = Matrix.Presence.UNKNOWN; if (Config.DEBUG) { diff --git a/src/matrix-event-room-guest-access.vala b/src/matrix-event-room-guest-access.vala index ff86c02..c178838 100644 --- a/src/matrix-event-room-guest-access.vala +++ b/src/matrix-event-room-guest-access.vala @@ -48,12 +48,10 @@ public class Matrix.Event.RoomGuestAccess : Matrix.Event.State { } if ((node = content_root.get_member("guest_access")) != null) { - GuestAccess? rules = (GuestAccess?)_g_enum_nick_to_value( - typeof(GuestAccess), node.get_string()); - - if (rules != null) { - _guest_access = rules; - } else { + try { + _guest_access = (GuestAccess)_g_enum_nick_to_value( + typeof(GuestAccess), node.get_string()); + } catch (Matrix.Error e) { _guest_access = GuestAccess.UNKNOWN; if (Config.DEBUG) { diff --git a/src/matrix-event-room-history-visibility.vala b/src/matrix-event-room-history-visibility.vala index 2f2f5d0..fbd0502 100644 --- a/src/matrix-event-room-history-visibility.vala +++ b/src/matrix-event-room-history-visibility.vala @@ -47,13 +47,10 @@ public class Matrix.Event.RoomHistoryVisibility : Matrix.Event.State { } if ((node = content_root.get_member("history_visibility")) != null) { - Matrix.HistoryVisibility? vis = (Matrix.HistoryVisibility?)_g_enum_nick_to_value( - typeof(Matrix.HistoryVisibility), - node.get_string()); - - if (vis != null) { - _visibility = vis; - } else { + try { + _visibility = (Matrix.HistoryVisibility)_g_enum_nick_to_value( + typeof(Matrix.HistoryVisibility), node.get_string()); + } catch (Matrix.Error e) { _visibility = Matrix.HistoryVisibility.UNKNOWN; if (Config.DEBUG) { diff --git a/src/matrix-event-room-join-rules.vala b/src/matrix-event-room-join-rules.vala index eb45eb0..1042a06 100644 --- a/src/matrix-event-room-join-rules.vala +++ b/src/matrix-event-room-join-rules.vala @@ -43,12 +43,10 @@ public class Matrix.Event.RoomJoinRules : Matrix.Event.State { } if ((node = content_root.get_member("join_rule")) != null) { - Matrix.JoinRules? rules = (Matrix.JoinRules)_g_enum_nick_to_value( - typeof(Matrix.JoinRules), node.get_string()); - - if (rules != null) { - _join_rules = rules; - } else { + try { + _join_rules = (JoinRules)_g_enum_nick_to_value( + typeof(JoinRules), node.get_string()); + } catch (Matrix.Error e) { _join_rules = Matrix.JoinRules.UNKNOWN; if (Config.DEBUG) { diff --git a/src/matrix-event-room-member.vala b/src/matrix-event-room-member.vala index 5d9f5ad..ce2cf00 100644 --- a/src/matrix-event-room-member.vala +++ b/src/matrix-event-room-member.vala @@ -138,12 +138,15 @@ public class Matrix.Event.RoomMember : Matrix.Event.State { } if ((node = content_root.get_member("membership")) != null) { - Matrix.RoomMembership? mship = (Matrix.RoomMembership?)_g_enum_nick_to_value( - typeof(Matrix.RoomMembership), - node.get_string()); + try { + _membership = (Matrix.RoomMembership)_g_enum_nick_to_value( + typeof(Matrix.RoomMembership), node.get_string()); + } catch (Matrix.Error e) { + _membership = Matrix.RoomMembership.UNKNOWN; - if (mship != null) { - _membership = mship; + if (Config.DEBUG) { + warning("Unknown membership value %s", node.get_string()); + } } } else if (Config.DEBUG) { warning("membership key is missing from the m.room.member event"); diff --git a/src/matrix-types.vala b/src/matrix-types.vala index 3b772f4..e941a56 100644 --- a/src/matrix-types.vala +++ b/src/matrix-types.vala @@ -295,17 +295,4 @@ namespace Matrix { return node; } } - - private int? - _g_enum_nick_to_value(Type enum_type, string nick) - { - EnumClass enum_class = (EnumClass)enum_type.class_ref(); - unowned EnumValue? enum_val = enum_class.get_value_by_nick(nick); - - if (enum_val != null) { - return enum_val.value; - } else { - return null; - } - } } diff --git a/src/utils.c b/src/utils.c index 1a5dd27..f8a5639 100644 --- a/src/utils.c +++ b/src/utils.c @@ -17,6 +17,7 @@ */ #include "utils.h" +#include "matrix-types.h" // GTK-Doc looking comments in this file intentionally do not begin with a double star, as the // functions here are internal @@ -58,3 +59,50 @@ _matrix_g_enum_to_string(GType enum_type, gint value, gboolean convert_dashes) return nick; } + +/* + * _matrix_g_enum_nick_to_value: + * + * @enum_type: a #GEnumType + * @nick: a value nick registered in @enum_type + * @error: a #GError, or NULL to ignore errors + * + * Get the integer value of the enum nick @nick from @enum_type. If the nick contains underscores + * (`_`), they will be converted to dashes (`-`) first. + * + * If @nick cannot be found in @enum_type, this function returns NULL, and sets @error to + * #MATRIX_ERROR_UNKNOWN_VALUE. + * + * Returns: the integer value of @nick, or 0 if not found + */ +gint +_matrix_g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error) +{ + GEnumClass *enum_class = g_type_class_ref(enum_type); + GEnumValue *enum_value; + gchar *nick_c = NULL; + gchar *a; + gint ret = 0; + + nick_c = g_strdup(nick); + + for (a = nick_c; *a; a++) { + if (*a == '_') { + *a = '-'; + } + } + + enum_value = g_enum_get_value_by_nick(enum_class, nick_c); + g_free(nick_c); + + if (enum_value) { + ret = enum_value->value; + } else { + g_set_error(error, MATRIX_ERROR, MATRIX_ERROR_UNKNOWN_VALUE, + "Value %s is unknown", nick); + } + + g_type_class_unref(enum_class); + + return ret; +} diff --git a/src/utils.h b/src/utils.h index dc3c496..a5a14c5 100644 --- a/src/utils.h +++ b/src/utils.h @@ -24,6 +24,7 @@ G_BEGIN_DECLS gchar *_matrix_g_enum_to_string(GType enum_type, gint value, gboolean convert_dashes); +gint _matrix_g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error); G_END_DECLS diff --git a/vapi/c-api.vapi b/vapi/c-api.vapi index 9046e33..60e1d6d 100644 --- a/vapi/c-api.vapi +++ b/vapi/c-api.vapi @@ -208,6 +208,10 @@ namespace Matrix { [CCode (cheader_filename = "utils.h", cname = "_matrix_g_enum_to_string")] public string? _g_enum_value_to_nick(GLib.Type enum_type, int value, bool convert_dashes = true); + [CCode (cheader_filename = "utils.h", cname = "_matrix_g_enum_nick_to_value")] + public int _g_enum_nick_to_value(GLib.Type enum_type, string nick) + throws Matrix.Error; + /** * The major version number of the Matrix.org GLib SDK. */