Port _g_enum_nick_to_value() to C
This commit is contained in:
parent
091bb38286
commit
f6f6f78cdc
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
@ -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");
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
48
src/utils.c
48
src/utils.c
@ -17,6 +17,7 @@
|
||||
*/
|
||||
|
||||
#include "utils.h"
|
||||
#include "matrix-enumtypes.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;
|
||||
}
|
||||
|
@ -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
|
||||
|
||||
|
@ -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.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user