GCC Code Coverage Report


Directory: ./
File: panels/system/region/cc-format-preview.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 0 102 0.0%
Functions: 0 11 0.0%
Branches: 0 45 0.0%

Line Branch Exec Source
1 /* cc-format-preview.c
2 *
3 * Copyright (C) 2013 Red Hat, Inc.
4 * Copyright (C) 2020 System76, Inc.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 *
19 * Written by:
20 * Matthias Clasen
21 * Ian Douglas Scott <idscott@system76.com>
22 *
23 * SPDX-License-Identifier: GPL-2.0-or-later
24 */
25
26 #include "cc-format-preview.h"
27
28 #include <errno.h>
29 #include <locale.h>
30 #include <langinfo.h>
31 #include <string.h>
32 #include <glib/gi18n.h>
33
34 struct _CcFormatPreview {
35 GtkBox parent_instance;
36
37 GtkWidget *date_format_label;
38 GtkWidget *date_time_format_label;
39 GtkWidget *measurement_format_label;
40 GtkWidget *number_format_label;
41 GtkWidget *paper_format_label;
42 GtkWidget *time_format_label;
43
44 gchar *region;
45 };
46
47 enum
48 {
49 PROP_0,
50 PROP_REGION
51 };
52
53 G_DEFINE_TYPE (CcFormatPreview, cc_format_preview, GTK_TYPE_BOX)
54
55 static void
56 display_date (GtkWidget *label, GDateTime *dt, const gchar *format)
57 {
58 g_autofree gchar *s = g_date_time_format (dt, format);
59 gtk_label_set_text (GTK_LABEL (label), g_strstrip (s));
60 }
61
62 static void
63 update_format_examples (CcFormatPreview *self)
64 {
65 const gchar *region = self->region;
66 locale_t locale;
67 locale_t old_locale;
68 g_autoptr(GDateTime) dt = NULL;
69 g_autofree gchar *s = NULL;
70 #ifdef LC_MEASUREMENT
71 const gchar *fmt;
72 gboolean is_imperial = FALSE;
73 #endif
74 g_autoptr(GtkPaperSize) paper = NULL;
75
76 if (region == NULL || region[0] == '\0')
77 return;
78
79 locale = newlocale (LC_TIME_MASK, region, (locale_t) 0);
80 if (locale == (locale_t) 0)
81 g_warning ("Failed to create locale %s: %s", region, g_strerror (errno));
82 else
83 old_locale = uselocale (locale);
84
85 dt = g_date_time_new_now_local ();
86 display_date (self->date_format_label, dt, "%x");
87 display_date (self->time_format_label, dt, "%X");
88 display_date (self->date_time_format_label, dt, "%c");
89
90 if (locale != (locale_t) 0)
91 {
92 uselocale (old_locale);
93 freelocale (locale);
94 }
95
96 locale = newlocale (LC_NUMERIC_MASK, region, (locale_t) 0);
97 if (locale == (locale_t) 0)
98 g_warning ("Failed to create locale %s: %s", region, g_strerror (errno));
99 else
100 old_locale = uselocale (locale);
101
102 s = g_strdup_printf ("%'.2f", 123456789.00);
103 gtk_label_set_text (GTK_LABEL (self->number_format_label), s);
104
105 if (locale != (locale_t) 0)
106 {
107 uselocale (old_locale);
108 freelocale (locale);
109 }
110
111 #if 0
112 locale = newlocale (LC_MONETARY_MASK, region, (locale_t) 0);
113 if (locale == (locale_t) 0)
114 g_warning ("Failed to create locale %s: %s", region, g_strerror (errno));
115 else
116 old_locale = uselocale (locale);
117
118 num_info = localeconv ();
119 if (num_info != NULL)
120 gtk_label_set_text (GTK_LABEL (self->currency_format_label), num_info->currency_symbol);
121
122 if (locale != (locale_t) 0)
123 {
124 uselocale (old_locale);
125 freelocale (locale);
126 }
127 #endif
128
129 #ifdef LC_MEASUREMENT
130 locale = newlocale (LC_MEASUREMENT_MASK, region, (locale_t) 0);
131 if (locale == (locale_t) 0)
132 g_warning ("Failed to create locale %s: %s", region, g_strerror (errno));
133 else
134 old_locale = uselocale (locale);
135
136 fmt = nl_langinfo (_NL_MEASUREMENT_MEASUREMENT);
137 /* The returned pointer of nl_langinfo could be invalid after switching
138 locale, so we must use it here. */
139 is_imperial = fmt && *fmt == 2;
140
141 if (locale != (locale_t) 0)
142 {
143 uselocale (old_locale);
144 freelocale (locale);
145 }
146
147 if (is_imperial)
148 gtk_label_set_text (GTK_LABEL (self->measurement_format_label), C_("measurement format", "Imperial"));
149 else
150 gtk_label_set_text (GTK_LABEL (self->measurement_format_label), C_("measurement format", "Metric"));
151
152 #endif
153
154 #ifdef LC_PAPER
155 locale = newlocale (LC_PAPER_MASK, region, (locale_t) 0);
156 if (locale == (locale_t) 0)
157 g_warning ("Failed to create locale %s: %s", region, g_strerror (errno));
158 else
159 old_locale = uselocale (locale);
160
161 paper = gtk_paper_size_new (gtk_paper_size_get_default ());
162 gtk_label_set_text (GTK_LABEL (self->paper_format_label), gtk_paper_size_get_display_name (paper));
163
164 if (locale != (locale_t) 0)
165 {
166 uselocale (old_locale);
167 freelocale (locale);
168 }
169 #endif
170 }
171
172 static void
173 cc_format_preview_set_property (GObject *object,
174 guint prop_id,
175 const GValue *value,
176 GParamSpec *pspec)
177 {
178 CcFormatPreview *self;
179
180 self = CC_FORMAT_PREVIEW (object);
181
182 switch (prop_id) {
183 case PROP_REGION:
184 cc_format_preview_set_region (self, g_value_get_string (value));
185 break;
186 default:
187 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
188 break;
189 }
190 }
191
192 static void
193 cc_format_preview_get_property (GObject *object,
194 guint prop_id,
195 GValue *value,
196 GParamSpec *pspec)
197 {
198 CcFormatPreview *self;
199
200 self = CC_FORMAT_PREVIEW (object);
201
202 switch (prop_id) {
203 case PROP_REGION:
204 g_value_set_string (value, self->region);
205 break;
206 default:
207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208 break;
209 }
210 }
211
212 static void
213 cc_format_preview_finalize (GObject *object)
214 {
215 CcFormatPreview *self = CC_FORMAT_PREVIEW (object);
216
217 g_clear_pointer (&self->region, g_free);
218
219 G_OBJECT_CLASS (cc_format_preview_parent_class)->finalize (object);
220 }
221
222 void
223 cc_format_preview_class_init (CcFormatPreviewClass *klass)
224 {
225 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
226 GObjectClass *object_class = G_OBJECT_CLASS (klass);
227
228 object_class->get_property = cc_format_preview_get_property;
229 object_class->set_property = cc_format_preview_set_property;
230 object_class->finalize = cc_format_preview_finalize;
231
232 g_object_class_install_property (object_class,
233 PROP_REGION,
234 g_param_spec_string ("region",
235 "region",
236 "region",
237 NULL,
238 G_PARAM_READWRITE));
239
240 gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/system/region/cc-format-preview.ui");
241
242 gtk_widget_class_bind_template_child (widget_class, CcFormatPreview, date_format_label);
243 gtk_widget_class_bind_template_child (widget_class, CcFormatPreview, date_time_format_label);
244 gtk_widget_class_bind_template_child (widget_class, CcFormatPreview, measurement_format_label);
245 gtk_widget_class_bind_template_child (widget_class, CcFormatPreview, number_format_label);
246 gtk_widget_class_bind_template_child (widget_class, CcFormatPreview, paper_format_label);
247 gtk_widget_class_bind_template_child (widget_class, CcFormatPreview, time_format_label);
248 }
249
250 void
251 cc_format_preview_init (CcFormatPreview *self)
252 {
253 gtk_widget_init_template (GTK_WIDGET (self));
254 }
255
256 void
257 cc_format_preview_set_region (CcFormatPreview *preview,
258 const gchar *region)
259 {
260 g_free (preview->region);
261 preview->region = g_strdup (region);
262 update_format_examples (preview);
263 }
264