Rework _g_enum_to_string in Vala

Its new name is Matrix._g_enum_value_to_nick
This commit is contained in:
Gergely Polonkai 2016-03-04 08:58:56 +01:00
parent bd4efee0cf
commit 4d1d6562d0
7 changed files with 61 additions and 39 deletions

1
.gitignore vendored
View File

@ -63,3 +63,4 @@ Makefile.in
/src/matrix-glib.h /src/matrix-glib.h
/src/matrix-api.c /src/matrix-api.c
/src/matrix-client.c /src/matrix-client.c
/src/matrix-enums.c

View File

@ -18,6 +18,7 @@ lib_LTLIBRARIES = libmatrix-glib-0.0.la
libmatrix_glib_0_0_la_VALA_SOURCES = \ libmatrix_glib_0_0_la_VALA_SOURCES = \
matrix-api.vala \ matrix-api.vala \
matrix-client.vala \ matrix-client.vala \
matrix-enums.vala \
$(NULL) $(NULL)
AM_CPPFLAGS += \ AM_CPPFLAGS += \

40
src/matrix-enums.vala Normal file
View File

@ -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
* <http://www.gnu.org/licenses/>.
*/
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;
}
}
}

View File

@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "matrix-http-api.h" #include "matrix-http-api.h"
#include "matrix-enumtypes.h" #include "matrix-enumtypes.h"
#include "matrix-glib.h"
#include "utils.h" #include "utils.h"
#include <string.h> #include <string.h>
@ -965,7 +966,7 @@ i_create_room(MatrixAPI *api,
} }
if (preset != MATRIX_ROOM_PRESET_NONE) { 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); MATRIX_TYPE_ROOM_PRESET, preset, TRUE);
if (preset_string) { if (preset_string) {
@ -988,7 +989,7 @@ i_create_room(MatrixAPI *api,
} }
if (visibility != MATRIX_ROOM_VISIBILITY_DEFAULT) { 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); MATRIX_TYPE_ROOM_VISIBILITY, visibility, TRUE);
if (visibility_string) { if (visibility_string) {
@ -1343,7 +1344,8 @@ i_set_user_presence(MatrixAPI *api,
json_builder_begin_object(builder); json_builder_begin_object(builder);
json_builder_set_member_name(builder, "presence"); 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); json_builder_add_string_value(builder, presence_string);
g_free(presence_string); g_free(presence_string);
@ -1411,7 +1413,7 @@ i_delete_pusher(MatrixAPI *api,
encoded_scope = soup_uri_encode(scope, NULL); encoded_scope = soup_uri_encode(scope, NULL);
encoded_rule_id = soup_uri_encode(rule_id, 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", path = g_strdup_printf("pushrules/%s/%s/%s",
encoded_scope, encoded_scope,
@ -1443,7 +1445,8 @@ i_get_pusher(MatrixAPI *api,
encoded_scope = soup_uri_encode(scope, NULL); encoded_scope = soup_uri_encode(scope, NULL);
encoded_rule_id = soup_uri_encode(rule_id, 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", path = g_strdup_printf("pushrules/%s/%s/%s",
encoded_scope, encoded_scope,
@ -1466,7 +1469,7 @@ static void
add_condition_kind_object(MatrixPusherConditionKind kind, add_condition_kind_object(MatrixPusherConditionKind kind,
JsonBuilder *builder) 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); MATRIX_TYPE_PUSHER_CONDITION_KIND, kind, TRUE);
if (!kind_string) { if (!kind_string) {
@ -1502,7 +1505,8 @@ static void i_add_pusher(MatrixAPI *api,
encoded_scope = soup_uri_encode(scope, NULL); encoded_scope = soup_uri_encode(scope, NULL);
encoded_rule_id = soup_uri_encode(rule_id, 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", path = g_strdup_printf("pushrules/%s/%s/%s",
encoded_scope, encoded_scope,
@ -1566,7 +1570,8 @@ i_toggle_pusher(MatrixAPI *api,
encoded_scope = soup_uri_encode(scope, NULL); encoded_scope = soup_uri_encode(scope, NULL);
encoded_rule_id = soup_uri_encode(rule_id, 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", path = g_strdup_printf("pushrules/%s/%s/%s",
encoded_scope, encoded_scope,
@ -1949,7 +1954,7 @@ i_send_event_receipt(MatrixAPI *api,
encoded_room_id = soup_uri_encode(room_id, NULL); encoded_room_id = soup_uri_encode(room_id, NULL);
encoded_event_id = soup_uri_encode(event_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, receipt_type,
TRUE); TRUE);
path = g_strdup_printf("rooms/%s/receipt/%s/%s", path = g_strdup_printf("rooms/%s/receipt/%s/%s",
@ -2599,7 +2604,8 @@ i_register_account(MatrixAPI *api,
g_object_unref(builder); g_object_unref(builder);
if (account_kind != MATRIX_ACCOUNT_KIND_DEFAULT) { if (account_kind != MATRIX_ACCOUNT_KIND_DEFAULT) {
gchar *kind_string = _g_enum_to_string(MATRIX_TYPE_ACCOUNT_KIND, gchar *kind_string = _matrix_g_enum_value_to_nick(
MATRIX_TYPE_ACCOUNT_KIND,
account_kind, TRUE); account_kind, TRUE);
params = create_query_params(); params = create_query_params();

View File

@ -18,6 +18,7 @@
#include "matrix-types.h" #include "matrix-types.h"
#include "matrix-enumtypes.h" #include "matrix-enumtypes.h"
#include "matrix-glib.h"
#include "utils.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_set_member_name(builder, "event_format");
json_builder_add_string_value(builder, json_builder_add_string_value(builder,
_g_enum_to_string( _matrix_g_enum_value_to_nick(
MATRIX_TYPE_EVENT_FORMAT, MATRIX_TYPE_EVENT_FORMAT,
filter->event_format, filter->event_format,
TRUE)); TRUE));

View File

@ -19,32 +19,6 @@
#include "utils.h" #include "utils.h"
#include "matrix-types.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 gint
_g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error) _g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error)
{ {

View File

@ -23,7 +23,6 @@
#include <glib-object.h> #include <glib-object.h>
#include <json-glib/json-glib.h> #include <json-glib/json-glib.h>
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); gint _g_enum_nick_to_value(GType enum_type, const gchar *nick, GError **error);
JsonNode *_json_node_deep_copy(const JsonNode *node); JsonNode *_json_node_deep_copy(const JsonNode *node);