From 4b62305c086c47bf5fb62fe9d00f61ef85f0d178 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Fri, 15 Jan 2016 17:58:35 +0100 Subject: [PATCH] Move enum_to_string to utils.c as g_enum_to_string --- .../matrix-glib/matrix-glib-sections.txt | 1 + src/Makefile.am | 1 + src/matrix-http-api.c | 45 ++++----------- src/utils.c | 56 +++++++++++++++++++ src/utils.h | 27 +++++++++ 5 files changed, 95 insertions(+), 35 deletions(-) create mode 100644 src/utils.c create mode 100644 src/utils.h diff --git a/docs/reference/matrix-glib/matrix-glib-sections.txt b/docs/reference/matrix-glib/matrix-glib-sections.txt index 06c3444..7c67b31 100644 --- a/docs/reference/matrix-glib/matrix-glib-sections.txt +++ b/docs/reference/matrix-glib/matrix-glib-sections.txt @@ -184,6 +184,7 @@ MATRIX_TYPE_API_PUSHER matrix_api_pusher_get_type MATRIX_TYPE_API_STATE_EVENT matrix_api_state_event_get_type +g_enum_to_string
diff --git a/src/Makefile.am b/src/Makefile.am index 6f27081..32c55fb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -27,6 +27,7 @@ libmatrix_glib_0_0_la_SOURCES = \ matrix-api-types.c \ matrix-http-api.c \ matrix-enumtypes.c \ + utils.c \ $(INST_H_SRC_FILES) \ $(INST_H_BUILT_FILES) \ $(NULL) diff --git a/src/matrix-http-api.c b/src/matrix-http-api.c index 4f22600..73f5c1f 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 "utils.h" #include #include @@ -467,32 +468,6 @@ matrix_http_api_get_validate_certificate(MatrixHTTPAPI *api) return priv->validate_certificate; } -static gchar * -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; -} - static void _response_callback(SoupSession *session, SoupMessage *msg, @@ -922,7 +897,7 @@ i_create_room(MatrixAPI *api, } if (preset != MATRIX_API_ROOM_PRESET_NONE) { - gchar *preset_string = enum_to_string( + gchar *preset_string = g_enum_to_string( MATRIX_TYPE_API_ROOM_PRESET, preset, TRUE); if (preset_string) { @@ -945,7 +920,7 @@ i_create_room(MatrixAPI *api, } if (visibility != MATRIX_API_ROOM_VISIBILITY_DEFAULT) { - gchar *visibility_string = enum_to_string( + gchar *visibility_string = g_enum_to_string( MATRIX_TYPE_API_ROOM_VISIBILITY, visibility, TRUE); if (visibility_string) { @@ -1299,7 +1274,7 @@ i_set_user_presence(MatrixAPI *api, json_builder_begin_object(builder); json_builder_set_member_name(builder, "presence"); - presence_string = enum_to_string(MATRIX_TYPE_API_PRESENCE, presence, TRUE); + presence_string = g_enum_to_string(MATRIX_TYPE_API_PRESENCE, presence, TRUE); json_builder_add_string_value(builder, presence_string); g_free(presence_string); @@ -1367,7 +1342,7 @@ i_delete_pusher(MatrixAPI *api, encoded_scope = soup_uri_encode(scope, NULL); encoded_rule_id = soup_uri_encode(rule_id, NULL); - kind_string = enum_to_string(MATRIX_TYPE_API_PUSHER_KIND, kind, TRUE); + kind_string = g_enum_to_string(MATRIX_TYPE_API_PUSHER_KIND, kind, TRUE); path = g_strdup_printf("pushrules/%s/%s/%s", encoded_scope, @@ -1399,7 +1374,7 @@ i_get_pusher(MatrixAPI *api, encoded_scope = soup_uri_encode(scope, NULL); encoded_rule_id = soup_uri_encode(rule_id, NULL); - kind_string = enum_to_string(MATRIX_TYPE_API_PUSHER_KIND, kind, TRUE); + kind_string = g_enum_to_string(MATRIX_TYPE_API_PUSHER_KIND, kind, TRUE); path = g_strdup_printf("pushrules/%s/%s/%s", encoded_scope, @@ -1422,7 +1397,7 @@ static void add_condition_kind_object(MatrixAPIPusherConditionKind kind, JsonBuilder *builder) { - gchar *kind_string = enum_to_string( + gchar *kind_string = g_enum_to_string( MATRIX_TYPE_API_PUSHER_CONDITION_KIND, kind, TRUE); if (!kind_string) { @@ -1458,7 +1433,7 @@ 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 = enum_to_string(MATRIX_TYPE_API_PUSHER_KIND, kind, TRUE); + kind_string = g_enum_to_string(MATRIX_TYPE_API_PUSHER_KIND, kind, TRUE); path = g_strdup_printf("pushrules/%s/%s/%s", encoded_scope, @@ -1522,7 +1497,7 @@ i_toggle_pusher(MatrixAPI *api, encoded_scope = soup_uri_encode(scope, NULL); encoded_rule_id = soup_uri_encode(rule_id, NULL); - kind_string = enum_to_string(MATRIX_TYPE_API_PUSHER_KIND, kind, TRUE); + kind_string = g_enum_to_string(MATRIX_TYPE_API_PUSHER_KIND, kind, TRUE); path = g_strdup_printf("pushrules/%s/%s/%s", encoded_scope, @@ -1919,7 +1894,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 = enum_to_string(MATRIX_TYPE_API_RECEIPT_TYPE, + receipt_type_string = g_enum_to_string(MATRIX_TYPE_API_RECEIPT_TYPE, receipt_type, TRUE); path = g_strdup_printf("rooms/%s/receipt/%s/%s", diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..bf51e7e --- /dev/null +++ b/src/utils.c @@ -0,0 +1,56 @@ +/* + * 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 + * . + */ + +#include "utils.h" + +/** + * g_enum_to_string: (skip) + * @type: a #GEnumType + * @value: a value from @type + * @convert_dashes: convert dashes to underscores + * + * Get the value nick for @value, optionally converting dashes to + * underscores. + * + * Returns: (transfer full): :the value nick + */ +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; +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..8240bb2 --- /dev/null +++ b/src/utils.h @@ -0,0 +1,27 @@ +/* + * 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 + * . + */ + +#ifndef __MATRIX_UTILS_H__ +#define __MATRIX_UTILS_H__ + +#include +#include + +gchar *g_enum_to_string(GType enum_type, gint value, gboolean convert_dash); + +#endif /* __MATRIX_UTILS_H__ */