diff --git a/.gitignore b/.gitignore index 5682f20..e53c747 100644 --- a/.gitignore +++ b/.gitignore @@ -63,3 +63,4 @@ Makefile.in /src/matrix-glib.h /src/matrix-api.c /src/matrix-client.c +/src/matrix-enums.c diff --git a/src/Makefile.am b/src/Makefile.am index f0474ec..ebdd306 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -18,6 +18,7 @@ lib_LTLIBRARIES = libmatrix-glib-0.0.la libmatrix_glib_0_0_la_VALA_SOURCES = \ matrix-api.vala \ matrix-client.vala \ + matrix-enums.vala \ $(NULL) AM_CPPFLAGS += \ diff --git a/src/matrix-enums.vala b/src/matrix-enums.vala new file mode 100644 index 0000000..2e4591d --- /dev/null +++ b/src/matrix-enums.vala @@ -0,0 +1,40 @@ +/* + * This file is part of matrix-glib-sdk + * + * matrix-glib-sdk is free software: you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * matrix-glib-sdk is distributed in the hope that it will be + * useful, but WITHOUT ANY WARRANTY; without even the implied + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with matrix-glib-sdk. If not, see + * . + */ + +namespace Matrix { + public string? + _g_enum_value_to_nick(Type enum_type, + int value, + bool convert_dashes = true) + { + EnumClass enum_class = (EnumClass)enum_type.class_ref(); + unowned EnumValue? enum_val = enum_class.get_value(value); + + if (enum_val != null) { + var nick = enum_val.value_nick; + + if (convert_dashes) { + return nick.replace("-", "_"); + } + + return nick; + } else { + return null; + } + } +} diff --git a/src/matrix-http-api.c b/src/matrix-http-api.c index f8c04f4..b8837a0 100644 --- a/src/matrix-http-api.c +++ b/src/matrix-http-api.c @@ -19,6 +19,7 @@ #include "config.h" #include "matrix-http-api.h" #include "matrix-enumtypes.h" +#include "matrix-glib.h" #include "utils.h" #include @@ -965,7 +966,7 @@ i_create_room(MatrixAPI *api, } if (preset != MATRIX_ROOM_PRESET_NONE) { - gchar *preset_string = _g_enum_to_string( + gchar *preset_string = _matrix_g_enum_value_to_nick( MATRIX_TYPE_ROOM_PRESET, preset, TRUE); if (preset_string) { @@ -988,7 +989,7 @@ i_create_room(MatrixAPI *api, } if (visibility != MATRIX_ROOM_VISIBILITY_DEFAULT) { - gchar *visibility_string = _g_enum_to_string( + gchar *visibility_string = _matrix_g_enum_value_to_nick( MATRIX_TYPE_ROOM_VISIBILITY, visibility, TRUE); if (visibility_string) { @@ -1343,7 +1344,8 @@ i_set_user_presence(MatrixAPI *api, json_builder_begin_object(builder); json_builder_set_member_name(builder, "presence"); - presence_string = _g_enum_to_string(MATRIX_TYPE_PRESENCE, presence, TRUE); + presence_string = _matrix_g_enum_value_to_nick(MATRIX_TYPE_PRESENCE, + presence, TRUE); json_builder_add_string_value(builder, presence_string); g_free(presence_string); @@ -1411,7 +1413,7 @@ i_delete_pusher(MatrixAPI *api, encoded_scope = soup_uri_encode(scope, NULL); encoded_rule_id = soup_uri_encode(rule_id, NULL); - kind_string = _g_enum_to_string(MATRIX_TYPE_PUSHER_KIND, kind, TRUE); + kind_string = _matrix_g_enum_value_to_nick(MATRIX_TYPE_PUSHER_KIND, kind, TRUE); path = g_strdup_printf("pushrules/%s/%s/%s", encoded_scope, @@ -1443,7 +1445,8 @@ i_get_pusher(MatrixAPI *api, encoded_scope = soup_uri_encode(scope, NULL); encoded_rule_id = soup_uri_encode(rule_id, NULL); - kind_string = _g_enum_to_string(MATRIX_TYPE_PUSHER_KIND, kind, TRUE); + kind_string = _matrix_g_enum_value_to_nick(MATRIX_TYPE_PUSHER_KIND, + kind, TRUE); path = g_strdup_printf("pushrules/%s/%s/%s", encoded_scope, @@ -1466,7 +1469,7 @@ static void add_condition_kind_object(MatrixPusherConditionKind kind, JsonBuilder *builder) { - gchar *kind_string = _g_enum_to_string( + gchar *kind_string = _matrix_g_enum_value_to_nick( MATRIX_TYPE_PUSHER_CONDITION_KIND, kind, TRUE); if (!kind_string) { @@ -1502,7 +1505,8 @@ static void i_add_pusher(MatrixAPI *api, encoded_scope = soup_uri_encode(scope, NULL); encoded_rule_id = soup_uri_encode(rule_id, NULL); - kind_string = _g_enum_to_string(MATRIX_TYPE_PUSHER_KIND, kind, TRUE); + kind_string = _matrix_g_enum_value_to_nick(MATRIX_TYPE_PUSHER_KIND, + kind, TRUE); path = g_strdup_printf("pushrules/%s/%s/%s", encoded_scope, @@ -1566,7 +1570,8 @@ i_toggle_pusher(MatrixAPI *api, encoded_scope = soup_uri_encode(scope, NULL); encoded_rule_id = soup_uri_encode(rule_id, NULL); - kind_string = _g_enum_to_string(MATRIX_TYPE_PUSHER_KIND, kind, TRUE); + kind_string = _matrix_g_enum_value_to_nick(MATRIX_TYPE_PUSHER_KIND, + kind, TRUE); path = g_strdup_printf("pushrules/%s/%s/%s", encoded_scope, @@ -1949,7 +1954,7 @@ i_send_event_receipt(MatrixAPI *api, encoded_room_id = soup_uri_encode(room_id, NULL); encoded_event_id = soup_uri_encode(event_id, NULL); - receipt_type_string = _g_enum_to_string(MATRIX_TYPE_RECEIPT_TYPE, + receipt_type_string = _matrix_g_enum_value_to_nick(MATRIX_TYPE_RECEIPT_TYPE, receipt_type, TRUE); path = g_strdup_printf("rooms/%s/receipt/%s/%s", @@ -2599,8 +2604,9 @@ i_register_account(MatrixAPI *api, g_object_unref(builder); if (account_kind != MATRIX_ACCOUNT_KIND_DEFAULT) { - gchar *kind_string = _g_enum_to_string(MATRIX_TYPE_ACCOUNT_KIND, - account_kind, TRUE); + gchar *kind_string = _matrix_g_enum_value_to_nick( + MATRIX_TYPE_ACCOUNT_KIND, + account_kind, TRUE); params = create_query_params(); diff --git a/src/matrix-types.c b/src/matrix-types.c index fce102e..6492dae 100644 --- a/src/matrix-types.c +++ b/src/matrix-types.c @@ -18,6 +18,7 @@ #include "matrix-types.h" #include "matrix-enumtypes.h" +#include "matrix-glib.h" #include "utils.h" /** @@ -1566,7 +1567,7 @@ matrix_filter_get_json_node(MatrixFilter *filter) json_builder_set_member_name(builder, "event_format"); json_builder_add_string_value(builder, - _g_enum_to_string( + _matrix_g_enum_value_to_nick( MATRIX_TYPE_EVENT_FORMAT, filter->event_format, TRUE)); diff --git a/src/utils.c b/src/utils.c index ecda5dc..114f01e 100644 --- a/src/utils.c +++ b/src/utils.c @@ -19,32 +19,6 @@ #include "utils.h" #include "matrix-types.h" -gchar * -_g_enum_to_string(GType enum_type, gint value, gboolean convert_dashes) -{ - GEnumClass *enum_class = g_type_class_ref(enum_type); - GEnumValue *enum_value = g_enum_get_value(enum_class, value); - gchar *nick = NULL; - - if (value) { - nick = g_strdup(enum_value->value_nick); - - if (convert_dashes) { - gchar *a; - - for (a = nick; *a; a++) { - if (*a == '-') { - *a = '_'; - } - } - } - } - - g_type_class_unref(enum_class); - - return nick; -} - gint _g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error) { diff --git a/src/utils.h b/src/utils.h index 628834e..4152b45 100644 --- a/src/utils.h +++ b/src/utils.h @@ -23,7 +23,6 @@ #include #include -gchar *_g_enum_to_string(GType enum_type, gint value, gboolean convert_dash); gint _g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error); JsonNode *_json_node_deep_copy(const JsonNode *node);