summaryrefslogtreecommitdiff
path: root/gcr/gcr-certificate-section.c
blob: 4741ca67c303327344fa8a713be1f0306aebde52 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * Copyright 2021 Collabora Ltd.
 * Copyright Corentin Noël <corentin.noel@collabora.com>
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "gcr-certificate-field.h"
#include "gcr-certificate-section.h"
#include "gcr-enum-types.h"

struct _GcrCertificateSection {
	GObject parent_instance;

	char *label;
	GcrCertificateSectionFlags flags;
	GListStore *fields;
};

G_DEFINE_FINAL_TYPE (GcrCertificateSection, gcr_certificate_section, G_TYPE_OBJECT)

enum {
	PROP_LABEL = 1,
	PROP_FIELDS,
	PROP_FLAGS,
	N_PROPERTIES
};

static GParamSpec *obj_properties [N_PROPERTIES];


/**
 * _gcr_certificate_section_new:
 * @label: the user-displayable label of the section
 * @important: whether this section should be visible by default
 *
 * Create a new certificate section usable in user interfaces.
 *
 * Returns: (transfer full): a new #GcrCertificateSection
 */
GcrCertificateSection *
_gcr_certificate_section_new (const char *label,
                              gboolean    important)
{
	return g_object_new (GCR_TYPE_CERTIFICATE_SECTION,
	                     "label", label,
	                     "flags", important ? GCR_CERTIFICATE_SECTION_IMPORTANT : GCR_CERTIFICATE_SECTION_NONE,
	                     NULL);
}

static void
gcr_certificate_section_finalize (GObject *object)
{
	GcrCertificateSection *self = (GcrCertificateSection *)object;
	g_clear_pointer (&self->label, g_free);

	G_OBJECT_CLASS (gcr_certificate_section_parent_class)->finalize (object);
}

static void
gcr_certificate_section_dispose (GObject *object)
{
	GcrCertificateSection *self = (GcrCertificateSection *)object;
	g_clear_object (&self->fields);

	G_OBJECT_CLASS (gcr_certificate_section_parent_class)->dispose (object);
}

static void
gcr_certificate_section_get_property (GObject    *object,
                                      guint       prop_id,
                                      GValue     *value,
                                      GParamSpec *pspec)
{
	GcrCertificateSection *self = GCR_CERTIFICATE_SECTION (object);

	switch (prop_id) {
	case PROP_LABEL:
		g_value_set_string (value, self->label);
		break;
	case PROP_FIELDS:
		g_value_set_object (value, &self->fields);
		break;
	case PROP_FLAGS:
		g_value_set_flags (value, self->flags);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	}
}

static void
gcr_certificate_section_set_property (GObject      *object,
                                      guint         prop_id,
                                      const GValue *value,
                                      GParamSpec   *pspec)
{
	GcrCertificateSection *self = GCR_CERTIFICATE_SECTION (object);

	switch (prop_id) {
	case PROP_LABEL:
		self->label = g_value_dup_string (value);
		break;
	case PROP_FLAGS:
		self->flags = g_value_get_flags (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	}
}

static void
gcr_certificate_section_class_init (GcrCertificateSectionClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = gcr_certificate_section_finalize;
	object_class->dispose = gcr_certificate_section_dispose;
	object_class->get_property = gcr_certificate_section_get_property;
	object_class->set_property = gcr_certificate_section_set_property;

	obj_properties[PROP_LABEL] =
		g_param_spec_string ("label",
		                     "Label",
		                     "Display name of the field.",
		                     NULL,
		                     G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);

	obj_properties[PROP_FIELDS] =
		g_param_spec_object ("fields",
		                     "Fields",
		                     "The list of fields.",
		                     G_TYPE_LIST_MODEL,
		                     G_PARAM_STATIC_STRINGS | G_PARAM_READABLE);

	obj_properties[PROP_FLAGS] =
		g_param_spec_flags ("flags",
		                    "Flags",
		                    "Flags defined for the section.",
		                    GCR_TYPE_CERTIFICATE_SECTION_FLAGS,
		                    GCR_CERTIFICATE_SECTION_NONE,
		                    G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);

	g_object_class_install_properties (object_class,
	                                   N_PROPERTIES,
	                                   obj_properties);
}

static void
gcr_certificate_section_init (GcrCertificateSection *self)
{
	self->fields = g_list_store_new (GCR_TYPE_CERTIFICATE_FIELD);
}

/**
 * gcr_certificate_section_get_label:
 * @self: the #GcrCertificateSection
 *
 * Get the displayable label of the section.
 *
 * Returns: the displayable label of the section
 */
const char *
gcr_certificate_section_get_label (GcrCertificateSection *self)
{
	g_return_val_if_fail (self != NULL, NULL);

	return self->label;
}

/**
 * gcr_certificate_section_get_flags:
 * @self: the #GcrCertificateSection
 *
 * Get the flags.
 *
 * Returns: the `GcrCertificateSectionFlags`
 */
GcrCertificateSectionFlags
gcr_certificate_section_get_flags (GcrCertificateSection *self)
{
	g_return_val_if_fail (self != NULL, FALSE);

	return self->flags;
}

/**
 * gcr_certificate_section_get_fields:
 * @self: the #GcrCertificateSection
 *
 * Get the list of all the fields in this section.
 *
 * Returns: (transfer none): a #GListModel of #GcrCertificateField
 */
GListModel *
gcr_certificate_section_get_fields (GcrCertificateSection *self)
{
	g_return_val_if_fail (self != NULL, NULL);

	return G_LIST_MODEL (self->fields);
}

void
_gcr_certificate_section_append_field (GcrCertificateSection *section,
                                       GcrCertificateField   *field)
{
	g_return_if_fail (GCR_IS_CERTIFICATE_SECTION (section));
	g_return_if_fail (GCR_IS_CERTIFICATE_FIELD (field));

	g_list_store_append (section->fields, field);
}