1 /* -*- Mode: C; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3    Copyright (C) 2010, 2011, 2018 Red Hat, Inc.
4 
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22 
23 #include "utils.h"
24 
25 /* These 2 functions come from
26  * libvirt-glib/libvirt-gconfig/libvirt-gconfig-helpers.c
27  * Copyright (C) 2010, 2011 Red Hat, Inc.
28  * LGPLv2.1+ licensed */
29 
30 const char *
spice_genum_get_nick(GType enum_type,gint value)31 spice_genum_get_nick(GType enum_type, gint value)
32 {
33     GEnumClass *enum_class;
34     GEnumValue *enum_value;
35 
36     g_return_val_if_fail(G_TYPE_IS_ENUM(enum_type), NULL);
37 
38     enum_class = g_type_class_ref(enum_type);
39     enum_value = g_enum_get_value(enum_class, value);
40     g_type_class_unref(enum_class);
41 
42     if (enum_value != NULL) {
43         return enum_value->value_nick;
44     }
45 
46     g_return_val_if_reached(NULL);
47 }
48 
49 int
spice_genum_get_value(GType enum_type,const char * nick,gint default_value)50 spice_genum_get_value(GType enum_type, const char *nick,
51                       gint default_value)
52 {
53     GEnumClass *enum_class;
54     GEnumValue *enum_value;
55 
56     g_return_val_if_fail(G_TYPE_IS_ENUM(enum_type), default_value);
57     g_return_val_if_fail(nick != NULL, default_value);
58 
59     enum_class = g_type_class_ref(enum_type);
60     enum_value = g_enum_get_value_by_nick(enum_class, nick);
61     g_type_class_unref(enum_class);
62 
63     if (enum_value != NULL) {
64         return enum_value->value;
65     }
66 
67     g_return_val_if_reached(default_value);
68 }
69