bolt-enums.c 3.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*
 * Copyright © 2017 Red Hat, Inc
 *
 * This program 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 2.1 of the License, or (at your option) any later version.
 *
 * This library 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 this library. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Christian J. Kellner <christian@kellner.me>
 */

#include "config.h"

#include "bolt-enums.h"

G_DEFINE_AUTOPTR_CLEANUP_FUNC (GEnumClass, g_type_class_unref);

27
28
29
30
31
32
const char *
bolt_status_to_string (BoltStatus status)
{
  g_autoptr(GEnumClass) klass = NULL;
  GEnumValue *value;

33
34
35
  if (!bolt_status_validate (status))
    return NULL;

36
37
38
39
40
41
  klass = g_type_class_ref (BOLT_TYPE_STATUS);
  value = g_enum_get_value (klass, status);

  return value->value_nick;
}

42
43
44
45
46
47
48
gboolean
bolt_status_is_authorized (BoltStatus status)
{
  return status == BOLT_STATUS_AUTHORIZED ||
         status == BOLT_STATUS_AUTHORIZED_SECURE ||
         status == BOLT_STATUS_AUTHORIZED_NEWKEY;
}
49

50
51
52
53
54
55
gboolean
bolt_status_validate (BoltStatus status)
{
  return status < BOLT_STATUS_INVALID && status >= 0;
}

56
57
58
59
60
61
gboolean
bolt_status_is_connected (BoltStatus status)
{
  return status > BOLT_STATUS_DISCONNECTED;
}

62
63
64
65
66
67
68
BoltSecurity
bolt_security_from_string (const char *str)
{
  g_autoptr(GEnumClass) klass = NULL;
  GEnumValue *value;

  if (str == NULL)
69
    return BOLT_SECURITY_INVALID;
70
71
72
73
74

  klass = g_type_class_ref (BOLT_TYPE_SECURITY);
  value = g_enum_get_value_by_nick (klass, str);

  if (value == NULL)
75
    return BOLT_SECURITY_INVALID;
76
77
78
79
80
81
82
83
84
85

  return value->value;
}

const char *
bolt_security_to_string (BoltSecurity security)
{
  g_autoptr(GEnumClass) klass = NULL;
  GEnumValue *value;

86
87
88
  if (!bolt_security_validate (security))
    return NULL;

89
90
91
92
93
94
  klass = g_type_class_ref (BOLT_TYPE_SECURITY);
  value = g_enum_get_value (klass, security);

  return value->value_nick;
}

95
96
97
98
99
gboolean
bolt_security_validate (BoltSecurity security)
{
  return security < BOLT_SECURITY_INVALID && security >= 0;
}
100
101
102
103
104
105
106
107
108
109
110
111
112
113

BoltPolicy
bolt_policy_from_string (const char *str)
{
  g_autoptr(GEnumClass) klass = NULL;
  GEnumValue *value;

  if (str == NULL)
    return BOLT_POLICY_AUTO;

  klass = g_type_class_ref (BOLT_TYPE_POLICY);
  value = g_enum_get_value_by_nick (klass, str);

  if (value == NULL)
114
    return BOLT_POLICY_INVALID;
115
116
117
118
119
120
121
122
123
124

  return value->value;
}

const char *
bolt_policy_to_string (BoltPolicy policy)
{
  g_autoptr(GEnumClass) klass = NULL;
  GEnumValue *value;

125
126
127
  if (!bolt_policy_validate (policy))
    return NULL;

128
129
130
131
132
  klass = g_type_class_ref (BOLT_TYPE_POLICY);
  value = g_enum_get_value (klass, policy);

  return value->value_nick;
}
133
134
135
136
137
138

gboolean
bolt_policy_validate (BoltPolicy policy)
{
  return policy < BOLT_POLICY_INVALID && policy >= 0;
}
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183

BoltDeviceType
bolt_device_type_from_string (const char *str)
{
  g_autoptr(GEnumClass) klass = NULL;
  GEnumValue *value;

  if (str == NULL)
    return BOLT_DEVICE_PERIPHERAL;

  klass = g_type_class_ref (BOLT_TYPE_DEVICE_TYPE);
  value = g_enum_get_value_by_nick (klass, str);

  if (value == NULL)
    return BOLT_DEVICE_TYPE_INVALID;

  return value->value;
}

const char *
bolt_device_type_to_string (BoltDeviceType type)
{
  g_autoptr(GEnumClass) klass = NULL;
  GEnumValue *value;

  if (!bolt_device_type_validate (type))
    return NULL;

  klass = g_type_class_ref (BOLT_TYPE_DEVICE_TYPE);
  value = g_enum_get_value (klass, type);

  return value->value_nick;
}

gboolean
bolt_device_type_validate (BoltDeviceType type)
{
  return type < BOLT_DEVICE_TYPE_INVALID && type >= 0;
}

gboolean
bolt_device_type_is_host (BoltDeviceType type)
{
  return type == BOLT_DEVICE_HOST;
}