Debian Codegraph @

libgnome-desktop/gnome-qr-gtk/gnome-qr-widget.c

raw symbols SCIP
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /*
3 * Copyright 2026 Canonical Ltd
4 *
5 * SPDX-License-Identifier: LGPL-3.0-or-later
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * Author: Marco Trevisan <[email protected]>
21 */
22
23 #include "config.h"
24
25 #include "gnome-qr-widget.h"
26
27 #include <glib/gi18n-lib.h>
28 #include <gnome-qr/gnome-qr.h>
29 #include <gdk/gdk.h>
30
31 /**
32 * GnomeQrWidget:
33 *
34 * The `GnomeQrWidget` widget displays a rendered QR Code.
35 *
36 * ## CSS nodes
37 *
38 * `GnomeQrWidget` has a single CSS node with the name `gnome-qr-code`.
39 * It can be themed using the `color` and `background-color` properties.
40 *
41 * ## Accessibility
42 *
43 * `GnomeQrWidget` uses the `GTK_ACCESSIBLE_ROLE_IMG` role.
44 */
45
46 struct _GnomeQrWidget
47 {
48 GtkWidget parent_instance;
49
50 char *text;
51 char *alternative_text;
52 size_t size;
53 GnomeQrEccLevel ecc_level;
54
55 GdkTexture *texture;
56 GCancellable *cancellable;
57 };
58
59 G_DEFINE_FINAL_TYPE (GnomeQrWidget, gnome_qr_widget, GTK_TYPE_WIDGET)
60
61 enum {
62 PROP_0,
63 PROP_TEXT,
64 PROP_ALTERNATIVE_TEXT,
65 PROP_SIZE,
66 PROP_ECC_LEVEL,
67 N_PROPS
68 };
69
70 enum {
71 SIGNAL_CHANGED,
72 N_SIGNALS
73 };
74
75 static GParamSpec *properties[N_PROPS] = { NULL, };
76 static guint signals[N_SIGNALS] = { 0 };
77
78 static void gnome_qr_widget_update (GnomeQrWidget *self);
79
80 static void
81 gnome_qr_widget_dispose (GObject *object)
82 {
83 GnomeQrWidget *self = GNOME_QR_WIDGET (object);
84
85 g_cancellable_cancel (self->cancellable);
86 g_clear_object (&self->cancellable);
87 g_clear_object (&self->texture);
88
89 G_OBJECT_CLASS (gnome_qr_widget_parent_class)->dispose (object);
90 }
91
92 static void
93 gnome_qr_widget_finalize (GObject *object)
94 {
95 GnomeQrWidget *self = GNOME_QR_WIDGET (object);
96
97 g_clear_pointer (&self->text, g_free);
98 g_clear_pointer (&self->alternative_text, g_free);
99
100 G_OBJECT_CLASS (gnome_qr_widget_parent_class)->finalize (object);
101 }
102
103 static void
104 gnome_qr_widget_get_property (GObject *object,
105 guint prop_id,
106 GValue *value,
107 GParamSpec *pspec)
108 {
109 GnomeQrWidget *self = GNOME_QR_WIDGET (object);
110
111 switch (prop_id) {
112 case PROP_TEXT:
113 g_value_set_string (value, self->text);
114 break;
115
116 case PROP_ALTERNATIVE_TEXT:
117 g_value_set_string (value, self->alternative_text);
118 break;
119
120 case PROP_SIZE:
121 g_value_set_uint64 (value, self->size);
122 break;
123
124 case PROP_ECC_LEVEL:
125 g_value_set_enum (value, self->ecc_level);
126 break;
127
128 default:
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
130 break;
131 }
132 }
133
134 static void
135 gnome_qr_widget_set_property (GObject *object,
136 guint prop_id,
137 const GValue *value,
138 GParamSpec *pspec)
139 {
140 GnomeQrWidget *self = GNOME_QR_WIDGET (object);
141
142 switch (prop_id) {
143 case PROP_TEXT:
144 gnome_qr_widget_set_text (self, g_value_get_string (value));
145 break;
146
147 case PROP_ALTERNATIVE_TEXT:
148 gnome_qr_widget_set_alternative_text (self, g_value_get_string (value));
149 break;
150
151 case PROP_SIZE:
152 gnome_qr_widget_set_size (self, g_value_get_uint64 (value));
153 break;
154
155 case PROP_ECC_LEVEL:
156 gnome_qr_widget_set_ecc_level (self, g_value_get_enum (value));
157 break;
158
159 default:
160 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
161 break;
162 }
163 }
164
165 static void
166 gnome_qr_widget_snapshot (GtkWidget *widget,
167 GtkSnapshot *snapshot)
168 {
169 GnomeQrWidget *self = GNOME_QR_WIDGET (widget);
170 graphene_matrix_t matrix;
171 graphene_vec4_t offset;
172 GdkRGBA fg;
173 int width, height, size;
174 int x, y;
175
176 if (!self->texture)
177 return;
178
179 width = gtk_widget_get_width (widget);
180 height = gtk_widget_get_height (widget);
181
182 size = MIN (width, height);
183 x = (width - size) / 2;
184 y = (height - size) / 2;
185
186 gtk_widget_get_color (widget, &fg);
187
188 /* We assume that the texture foreground color is black and bg color is white */
189 graphene_matrix_init_from_float (&matrix, (float[16]) {
190 /* solid white to transparent */
191 0, 0, 0, -1.0,
192 /* solid black to fg color */
193 -fg.red, -fg.green, -fg.blue, 0,
194 0, 0, 0, 0,
195 0, 0, 0, 0,
196 });
197 graphene_vec4_init (&offset, fg.red, fg.green, fg.blue, fg.alpha);
198
199 gtk_snapshot_push_color_matrix (snapshot, &matrix, &offset);
200
201 gtk_snapshot_append_scaled_texture (snapshot,
202 self->texture,
203 GSK_SCALING_FILTER_NEAREST,
204 &GRAPHENE_RECT_INIT (x, y, size, size));
205
206 gtk_snapshot_pop (snapshot);
207 }
208
209 static void
210 gnome_qr_widget_measure (GtkWidget *widget,
211 GtkOrientation orientation,
212 int for_size,
213 int *minimum,
214 int *natural,
215 int *minimum_baseline,
216 int *natural_baseline)
217 {
218 GnomeQrWidget *self = GNOME_QR_WIDGET (widget);
219 int texture_size = 0;
220
221 if (self->texture)
222 texture_size = gdk_texture_get_width (self->texture);
223
224 *natural = *minimum = MAX (texture_size, self->size);
225 }
226
227 static void
228 gnome_qr_widget_class_init (GnomeQrWidgetClass *klass)
229 {
230 GObjectClass *object_class = G_OBJECT_CLASS (klass);
231 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
232
233 object_class->dispose = gnome_qr_widget_dispose;
234 object_class->finalize = gnome_qr_widget_finalize;
235 object_class->get_property = gnome_qr_widget_get_property;
236 object_class->set_property = gnome_qr_widget_set_property;
237
238 widget_class->snapshot = gnome_qr_widget_snapshot;
239 widget_class->measure = gnome_qr_widget_measure;
240
241 /**
242 * GnomeQrWidget::changed:
243 * @self: the #GnomeQrWidget
244 *
245 * Emitted when the QR code texture has been regenerated.
246 *
247 * This signal is emitted after the widget's text or error correction
248 * level changes and the new QR code has been successfully generated.
249 */
250 signals[SIGNAL_CHANGED] =
251 g_signal_new ("changed",
252 G_TYPE_FROM_CLASS (klass),
253 G_SIGNAL_RUN_FIRST,
254 0,
255 NULL, NULL,
256 NULL,
257 G_TYPE_NONE, 0);
258
259 properties[PROP_TEXT] =
260 g_param_spec_string ("text", NULL, NULL,
261 NULL,
262 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
263 G_PARAM_EXPLICIT_NOTIFY | G_PARAM_CONSTRUCT);
264
265 properties[PROP_ALTERNATIVE_TEXT] =
266 g_param_spec_string ("alternative-text", NULL, NULL,
267 _("QR Code"),
268 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
269 G_PARAM_EXPLICIT_NOTIFY | G_PARAM_CONSTRUCT);
270
271 properties[PROP_SIZE] =
272 g_param_spec_uint64 ("size", NULL, NULL,
273 0, G_MAXSIZE, 0,
274 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
275 G_PARAM_EXPLICIT_NOTIFY | G_PARAM_CONSTRUCT);
276
277 properties[PROP_ECC_LEVEL] =
278 g_param_spec_enum ("ecc-level", NULL, NULL,
279 gnome_qr_ecc_level_get_type (),
280 GNOME_QR_ECC_LEVEL_MEDIUM,
281 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
282 G_PARAM_EXPLICIT_NOTIFY | G_PARAM_CONSTRUCT);
283
284 g_object_class_install_properties (object_class, N_PROPS, properties);
285
286 gtk_widget_class_set_css_name (widget_class, "gnome-qr-code");
287
288 gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_IMG);
289 }
290
291 static void
292 gnome_qr_widget_init (GnomeQrWidget *self)
293 {
294 }
295
296 /**
297 * gnome_qr_widget_new:
298 * @text: (nullable): the text to encode in the QR code
299 *
300 * Creates a new #GnomeQrWidget.
301 *
302 * Returns: (transfer full): a new #GnomeQrWidget
303 */
304 GtkWidget *
305 gnome_qr_widget_new (const char *text)
306 {
307 return g_object_new (GNOME_TYPE_QR_WIDGET,
308 "text", text,
309 NULL);
310 }
311
312 static void
313 on_qr_code_generated (GObject *source,
314 GAsyncResult *result,
315 gpointer user_data)
316 {
317 GnomeQrWidget *self = GNOME_QR_WIDGET (user_data);
318 g_autoptr (GBytes) qr_data = NULL;
319 g_autoptr (GError) error = NULL;
320 size_t pixel_size;
321
322 qr_data = gnome_qr_generate_qr_code_finish (result, &pixel_size, &error);
323
324 if (error)
325 {
326 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
327 g_warning ("Failed to generate QR code: %s", error->message);
328
329 return;
330 }
331
332 g_clear_object (&self->texture);
333 self->texture = gdk_memory_texture_new (pixel_size,
334 pixel_size,
335 GDK_MEMORY_G8,
336 qr_data,
337 pixel_size *
338 GNOME_QR_BYTES_PER_FORMAT (GNOME_QR_PIXEL_FORMAT_G_8));
339
340 gtk_widget_queue_resize (GTK_WIDGET (self));
341
342 g_signal_emit (self, signals[SIGNAL_CHANGED], 0, NULL);
343 }
344
345 static void
346 gnome_qr_widget_update (GnomeQrWidget *self)
347 {
348 g_cancellable_cancel (self->cancellable);
349 g_clear_object (&self->cancellable);
350
351 if (!self->text || !*self->text)
352 {
353 g_clear_object (&self->texture);
354 gtk_widget_queue_draw (GTK_WIDGET (self));
355 g_signal_emit (self, signals[SIGNAL_CHANGED], 0, NULL);
356 return;
357 }
358
359 self->cancellable = g_cancellable_new ();
360
361 gnome_qr_generate_qr_code_async (self->text,
362 0,
363 NULL,
364 NULL,
365 GNOME_QR_PIXEL_FORMAT_G_8,
366 self->ecc_level,
367 self->cancellable,
368 on_qr_code_generated,
369 self);
370 }
371
372 /**
373 * gnome_qr_widget_set_text:
374 * @self: a #GnomeQrWidget
375 * @text: (nullable): the text to encode in the QR code
376 *
377 * Sets the text to be encoded in the QR code.
378 *
379 * If @text is %NULL or empty, the QR code will not be displayed.
380 */
381 void
382 gnome_qr_widget_set_text (GnomeQrWidget *self,
383 const char *text)
384 {
385 g_return_if_fail (GNOME_IS_QR_WIDGET (self));
386
387 if (g_strcmp0 (self->text, text) == 0)
388 return;
389
390 g_set_str (&self->text, text);
391 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TEXT]);
392
393 gnome_qr_widget_update (self);
394
395 gtk_accessible_update_property (GTK_ACCESSIBLE (self),
396 GTK_ACCESSIBLE_PROPERTY_VALUE_TEXT,
397 text ? text : _("QR code value not set"),
398 -1);
399 }
400
401 /**
402 * gnome_qr_widget_get_text:
403 * @self: a #GnomeQrWidget
404 *
405 * Gets the text currently encoded in the QR code.
406 *
407 * Returns: (nullable): the text being encoded, or %NULL if none is set
408 */
409 const char *
410 gnome_qr_widget_get_text (GnomeQrWidget *self)
411 {
412 g_return_val_if_fail (GNOME_IS_QR_WIDGET (self), NULL);
413
414 return self->text;
415 }
416
417 /**
418 * gnome_qr_widget_set_alternative_text:
419 * @self: a #GnomeQrWidget
420 * @alternative_text: (nullable): the alternative text description for the QR code
421 *
422 * Sets an alternative textual description for the QR Code contents.
423 *
424 * It is equivalent to the “alt” attribute for images on websites.
425 *
426 * This text will be made available to accessibility tools.
427 *
428 * If the QR code cannot be described textually, set this property to NULL.
429 *
430 * By default, this is set to "QR Code".
431 */
432 void
433 gnome_qr_widget_set_alternative_text (GnomeQrWidget *self,
434 const char *alternative_text)
435 {
436 g_return_if_fail (GNOME_IS_QR_WIDGET (self));
437
438 if (g_strcmp0 (self->alternative_text, alternative_text) == 0)
439 return;
440
441 g_set_str (&self->alternative_text, alternative_text);
442
443 gtk_accessible_update_property (GTK_ACCESSIBLE (self),
444 GTK_ACCESSIBLE_PROPERTY_DESCRIPTION,
445 alternative_text ?
446 alternative_text : _("QR Code"), -1);
447
448 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ALTERNATIVE_TEXT]);
449 }
450
451 /**
452 * gnome_qr_widget_get_alternative_text:
453 * @self: a #GnomeQrWidget
454 *
455 * Gets the alternative textual description of the QR code.
456 *
457 * The returned string will be %NULL if the qr code cannot be described textually.
458 *
459 * Returns: (nullable) (transfer none): the alternative textual description of @self.
460 */
461 const char *
462 gnome_qr_widget_get_alternative_text (GnomeQrWidget *self)
463 {
464 g_return_val_if_fail (GNOME_IS_QR_WIDGET (self), NULL);
465
466 return self->alternative_text;
467 }
468
469 /**
470 * gnome_qr_widget_set_size:
471 * @self: a #GnomeQrWidget
472 * @size: the size in pixels for the widget's natural and minimum size
473 *
474 * Sets the natural and minimum size (width and height) of the widget.
475 *
476 * The QR code will be rendered to fit within this size while
477 * maintaining its aspect ratio (square).
478 *
479 * If the size is smaller than the minimum size of the QR code, it will
480 * be ignored and the QR code will be rendered at its minimum size
481 * (1 pixel per block).
482 */
483 void
484 gnome_qr_widget_set_size (GnomeQrWidget *self,
485 size_t size)
486 {
487 g_return_if_fail (GNOME_IS_QR_WIDGET (self));
488
489 if (self->size == size)
490 return;
491
492 self->size = size;
493 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SIZE]);
494
495 gtk_widget_queue_resize (GTK_WIDGET (self));
496 }
497
498 /**
499 * gnome_qr_widget_get_size:
500 * @self: a #GnomeQrWidget
501 *
502 * Gets the current natural size of the widget.
503 *
504 * Returns: the size in pixels
505 */
506 size_t
507 gnome_qr_widget_get_size (GnomeQrWidget *self)
508 {
509 g_return_val_if_fail (GNOME_IS_QR_WIDGET (self), 0);
510
511 return self->size;
512 }
513
514 /**
515 * gnome_qr_widget_set_ecc_level:
516 * @self: a #GnomeQrWidget
517 * @ecc: the error correction level
518 *
519 * Sets the error correction level for the QR code.
520 *
521 * Higher error correction levels provide better recovery from damage
522 * or obscured areas, but result in larger QR codes with more modules.
523 */
524 void
525 gnome_qr_widget_set_ecc_level (GnomeQrWidget *self,
526 GnomeQrEccLevel ecc)
527 {
528 g_return_if_fail (GNOME_IS_QR_WIDGET (self));
529
530 if (self->ecc_level == ecc)
531 return;
532
533 self->ecc_level = ecc;
534 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ECC_LEVEL]);
535
536 gnome_qr_widget_update (self);
537 }
538
539 /**
540 * gnome_qr_widget_get_ecc_level:
541 * @self: a #GnomeQrWidget
542 *
543 * Gets the current error correction level of the QR code.
544 *
545 * Returns: the error correction level
546 */
547 GnomeQrEccLevel
548 gnome_qr_widget_get_ecc_level (GnomeQrWidget *self)
549 {
550 g_return_val_if_fail (GNOME_IS_QR_WIDGET (self), GNOME_QR_ECC_LEVEL_MEDIUM);
551
552 return self->ecc_level;
553 }