Branch data Line data Source code
1 : : /*
2 : : * Copyright 2021 Collabora Ltd.
3 : : * Copyright Corentin Noël <corentin.noel@collabora.com>
4 : : * SPDX-License-Identifier: LGPL-2.1-or-later
5 : : */
6 : :
7 : : #include "gcr-certificate-field.h"
8 : : #include "gcr-certificate-field-private.h"
9 : : #include "gcr-enum-types.h"
10 : :
11 : : struct _GcrCertificateField
12 : : {
13 : : GObject parent_instance;
14 : :
15 : : char *label;
16 : : GValue value;
17 : : GcrCertificateSection *section;
18 : : };
19 : :
20 [ + + + - : 410 : G_DEFINE_FINAL_TYPE (GcrCertificateField, gcr_certificate_field, G_TYPE_OBJECT)
+ + ]
21 : :
22 : : enum {
23 : : PROP_LABEL = 1,
24 : : PROP_VALUE,
25 : : PROP_SECTION,
26 : : N_PROPERTIES
27 : : };
28 : :
29 : : static GParamSpec *obj_properties [N_PROPERTIES];
30 : :
31 : : static void
32 : 47 : gcr_certificate_field_finalize (GObject *object)
33 : : {
34 : 47 : GcrCertificateField *self = (GcrCertificateField *)object;
35 : :
36 [ + - ]: 47 : g_clear_pointer (&self->label, g_free);
37 : 47 : g_value_unset (&self->value);
38 : :
39 : 47 : G_OBJECT_CLASS (gcr_certificate_field_parent_class)->finalize (object);
40 : 47 : }
41 : :
42 : : static void
43 : 0 : gcr_certificate_field_get_property (GObject *object,
44 : : guint prop_id,
45 : : GValue *value,
46 : : GParamSpec *pspec)
47 : : {
48 : 0 : GcrCertificateField *self = GCR_CERTIFICATE_FIELD (object);
49 : :
50 [ # # # # ]: 0 : switch (prop_id) {
51 : 0 : case PROP_LABEL:
52 : 0 : g_value_set_string (value, self->label);
53 : 0 : break;
54 : 0 : case PROP_VALUE:
55 : 0 : g_value_set_boxed (value, &self->value);
56 : 0 : break;
57 : 0 : case PROP_SECTION:
58 : 0 : g_value_set_object (value, self->section);
59 : 0 : break;
60 : 0 : default:
61 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
62 : : }
63 : 0 : }
64 : :
65 : : static void
66 : 94 : gcr_certificate_field_set_property (GObject *object,
67 : : guint prop_id,
68 : : const GValue *value,
69 : : GParamSpec *pspec)
70 : : {
71 : 94 : GcrCertificateField *self = GCR_CERTIFICATE_FIELD (object);
72 : :
73 [ + + - ]: 94 : switch (prop_id) {
74 : 47 : case PROP_LABEL:
75 : 47 : self->label = g_value_dup_string (value);
76 : 47 : break;
77 : 47 : case PROP_SECTION:
78 : 47 : self->section = g_value_get_object (value);
79 : 47 : break;
80 : 0 : default:
81 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
82 : : }
83 : 94 : }
84 : :
85 : : static void
86 : 1 : gcr_certificate_field_class_init (GcrCertificateFieldClass *klass)
87 : : {
88 : 1 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
89 : :
90 : 1 : object_class->finalize = gcr_certificate_field_finalize;
91 : 1 : object_class->get_property = gcr_certificate_field_get_property;
92 : 1 : object_class->set_property = gcr_certificate_field_set_property;
93 : :
94 : 1 : obj_properties[PROP_LABEL] =
95 : 1 : g_param_spec_string ("label",
96 : : "Label",
97 : : "Display name of the field.",
98 : : NULL,
99 : : G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
100 : :
101 : 1 : obj_properties[PROP_VALUE] =
102 : 1 : g_param_spec_boxed ("value",
103 : : "Value",
104 : : "Display name of the value.",
105 : : G_TYPE_VALUE,
106 : : G_PARAM_STATIC_STRINGS | G_PARAM_READABLE);
107 : :
108 : 1 : obj_properties[PROP_SECTION] =
109 : 1 : g_param_spec_object ("section",
110 : : "Section",
111 : : "The section it is included.",
112 : : GCR_TYPE_CERTIFICATE_SECTION,
113 : : G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
114 : :
115 : 1 : g_object_class_install_properties (object_class,
116 : : N_PROPERTIES,
117 : : obj_properties);
118 : 1 : }
119 : :
120 : : static void
121 : 47 : gcr_certificate_field_init (GcrCertificateField *self)
122 : : {
123 : 47 : }
124 : :
125 : : /**
126 : : * gcr_certificate_field_get_label:
127 : : * @self: the #GcrCertificateField
128 : : *
129 : : * Get the display label of the field.
130 : : *
131 : : * Returns: the display label of the field
132 : : */
133 : : const char *
134 : 47 : gcr_certificate_field_get_label (GcrCertificateField *self)
135 : : {
136 [ - + ]: 47 : g_return_val_if_fail (GCR_IS_CERTIFICATE_FIELD (self), NULL);
137 : :
138 : 47 : return self->label;
139 : : }
140 : :
141 : : /**
142 : : * gcr_certificate_field_get_value:
143 : : * @self: the #GcrCertificateField
144 : : * @value: (out caller-allocates): the `GValue` to fill
145 : : *
146 : : * Get the value of the field.
147 : : *
148 : : * The @value will have been initialized to the `GType` the value should be
149 : : * provided in.
150 : : *
151 : : * Returns: %TRUE if the value was set successfully.
152 : : */
153 : : gboolean
154 : 47 : gcr_certificate_field_get_value (GcrCertificateField *self,
155 : : GValue *value)
156 : : {
157 [ - + ]: 47 : g_return_val_if_fail (GCR_IS_CERTIFICATE_FIELD (self), FALSE);
158 [ - + ]: 47 : g_return_val_if_fail (G_IS_VALUE (value), FALSE);
159 : :
160 [ - + + - : 47 : if (G_VALUE_HOLDS (&self->value, G_VALUE_TYPE (value))) {
+ - ]
161 : 47 : g_value_copy (&self->value, value);
162 : 47 : return TRUE;
163 : : }
164 : :
165 : 0 : return FALSE;
166 : : }
167 : :
168 : : /**
169 : : * gcr_certificate_field_get_value_type:
170 : : * @self: the #GcrCertificateField
171 : : *
172 : : * Get the type associated with the value.
173 : : *
174 : : * Returns: The `GType` of the value
175 : : */
176 : : GType
177 : 47 : gcr_certificate_field_get_value_type (GcrCertificateField *self)
178 : : {
179 [ - + ]: 47 : g_return_val_if_fail (GCR_IS_CERTIFICATE_FIELD (self), FALSE);
180 : :
181 : 47 : return G_VALUE_TYPE (&self->value);
182 : : }
183 : :
184 : : /**
185 : : * gcr_certificate_field_get_section:
186 : : * @self: the #GcrCertificateField
187 : : *
188 : : * Get the parent #GcrCertificateSection.
189 : : *
190 : : * Returns: (transfer none): the parent #GcrCertificateSection
191 : : */
192 : : GcrCertificateSection *
193 : 47 : gcr_certificate_field_get_section (GcrCertificateField *self)
194 : : {
195 [ - + ]: 47 : g_return_val_if_fail (GCR_IS_CERTIFICATE_FIELD (self), NULL);
196 : :
197 : 47 : return self->section;
198 : : }
199 : :
200 : : GcrCertificateField *
201 : 34 : _gcr_certificate_field_new_take_value (GcrCertificateSection *section,
202 : : const char *label,
203 : : char *value)
204 : : {
205 : : GcrCertificateField *self;
206 : :
207 [ - + ]: 34 : g_return_val_if_fail (GCR_IS_CERTIFICATE_SECTION (section), NULL);
208 [ - + ]: 34 : g_return_val_if_fail (label != NULL, NULL);
209 [ - + ]: 34 : g_return_val_if_fail (value != NULL, NULL);
210 : :
211 : 34 : self = g_object_new (GCR_TYPE_CERTIFICATE_FIELD,
212 : : "section", section,
213 : : "label", label,
214 : : NULL);
215 : 34 : g_value_init (&self->value, G_TYPE_STRING);
216 : 34 : g_value_take_string (&self->value, value);
217 : :
218 : 34 : return self;
219 : : }
220 : :
221 : : GcrCertificateField *
222 : 2 : _gcr_certificate_field_new_take_values (GcrCertificateSection *section,
223 : : const char *label,
224 : : GStrv values)
225 : : {
226 : : GcrCertificateField *self;
227 : :
228 [ - + ]: 2 : g_return_val_if_fail (GCR_IS_CERTIFICATE_SECTION (section), NULL);
229 [ - + ]: 2 : g_return_val_if_fail (label != NULL, NULL);
230 [ - + ]: 2 : g_return_val_if_fail (values != NULL, NULL);
231 : :
232 : 2 : self = g_object_new (GCR_TYPE_CERTIFICATE_FIELD,
233 : : "section", section,
234 : : "label", label,
235 : : NULL);
236 : 2 : g_value_init (&self->value, G_TYPE_STRV);
237 : 2 : g_value_take_boxed (&self->value, values);
238 : :
239 : 2 : return self;
240 : : }
241 : :
242 : : GcrCertificateField *
243 : 11 : _gcr_certificate_field_new_take_bytes (GcrCertificateSection *section,
244 : : const char *label,
245 : : GBytes *bytes)
246 : : {
247 : : GcrCertificateField *self;
248 : :
249 [ - + ]: 11 : g_return_val_if_fail (GCR_IS_CERTIFICATE_SECTION (section), NULL);
250 [ - + ]: 11 : g_return_val_if_fail (label != NULL, NULL);
251 [ - + ]: 11 : g_return_val_if_fail (bytes != NULL, NULL);
252 : :
253 : 11 : self = g_object_new (GCR_TYPE_CERTIFICATE_FIELD,
254 : : "section", section,
255 : : "label", label,
256 : : NULL);
257 : 11 : g_value_init (&self->value, G_TYPE_BYTES);
258 : 11 : g_value_take_boxed (&self->value, bytes);
259 : :
260 : 11 : return self;
261 : : }
|