GCC Code Coverage Report


Directory: ./
File: panels/common/cc-list-row.c
Date: 2024-05-03 09:46:52
Exec Total Coverage
Lines: 47 71 66.2%
Functions: 9 11 81.8%
Branches: 11 29 37.9%

Line Branch Exec Source
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* cc-list-row.c
3 *
4 * Copyright 2019 Purism SPC
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU 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 * Author(s):
20 * Mohammed Sadiq <sadiq@sadiqpk.org>
21 *
22 * SPDX-License-Identifier: GPL-3.0-or-later
23 */
24
25 #undef G_LOG_DOMAIN
26 #define G_LOG_DOMAIN "cc-list-row"
27
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31
32 #include "cc-common-resources.h"
33 #include "cc-list-row.h"
34
35 struct _CcListRow
36 {
37 AdwActionRow parent_instance;
38
39 GtkLabel *secondary_label;
40
41 GtkImage *arrow;
42 gboolean show_arrow;
43 };
44
45
6/7
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 11 times.
28 G_DEFINE_TYPE (CcListRow, cc_list_row, ADW_TYPE_ACTION_ROW)
46
47
48 enum {
49 PROP_0,
50 PROP_SECONDARY_LABEL,
51 PROP_SHOW_ARROW,
52 N_PROPS
53 };
54
55 static GParamSpec *properties[N_PROPS];
56
57 static void
58 22 cc_list_row_get_property (GObject *object,
59 guint prop_id,
60 GValue *value,
61 GParamSpec *pspec)
62 {
63 22 CcListRow *self = (CcListRow *)object;
64
65
1/3
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
✗ Branch 2 not taken.
22 switch (prop_id)
66 {
67 case PROP_SECONDARY_LABEL:
68 g_value_set_string (value, gtk_label_get_label (self->secondary_label));
69 break;
70
71 22 case PROP_SHOW_ARROW:
72 22 g_value_set_boolean (value, self->show_arrow);
73 22 break;
74
75 default:
76 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
77 }
78 22 }
79
80 static void
81 22 cc_list_row_set_property (GObject *object,
82 guint prop_id,
83 const GValue *value,
84 GParamSpec *pspec)
85 {
86 22 CcListRow *self = (CcListRow *)object;
87
88
2/3
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
22 switch (prop_id)
89 {
90 11 case PROP_SECONDARY_LABEL:
91 11 gtk_label_set_label (self->secondary_label, g_value_get_string (value));
92 11 break;
93
94 11 case PROP_SHOW_ARROW:
95 11 cc_list_row_set_show_arrow (self, g_value_get_boolean (value));
96 11 break;
97
98 default:
99 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
100 }
101 22 }
102
103 static void
104 1 cc_list_row_class_init (CcListRowClass *klass)
105 {
106 1 GObjectClass *object_class = G_OBJECT_CLASS (klass);
107 1 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
108
109 1 object_class->get_property = cc_list_row_get_property;
110 1 object_class->set_property = cc_list_row_set_property;
111
112 1 properties[PROP_SECONDARY_LABEL] =
113 1 g_param_spec_string ("secondary-label",
114 "Secondary Label",
115 "Set Secondary Label",
116 NULL,
117 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
118
119 1 properties[PROP_SHOW_ARROW] =
120 1 g_param_spec_boolean ("show-arrow",
121 "Show Arrow",
122 "Whether to show an arrow at the end of the row",
123 FALSE,
124 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
125
126 1 g_object_class_install_properties (object_class, N_PROPS, properties);
127
128 1 gtk_widget_class_set_template_from_resource (widget_class,
129 "/org/gnome/control-center/"
130 "common/cc-list-row.ui");
131
132 1 gtk_widget_class_bind_template_child (widget_class, CcListRow, secondary_label);
133 1 gtk_widget_class_bind_template_child (widget_class, CcListRow, arrow);
134 1 }
135
136 static void
137 11 add_secondary_label_to_a11y_description (CcListRow *self)
138 {
139 // We're using the widget tree traversal because gtk_accessible_update_relation
140 // does not allow appending to the existing relation.
141 // FIXME: Use the new append to relation API when available.
142 GtkWidget *suffix_box;
143 GtkWidget *subtitle_label;
144
145 // In current Libadwaita, the subtitle label is in the previous box of the
146 // suffixes box (e. g. our secondary_label's parent)
147 // as the last child.
148 11 suffix_box = gtk_widget_get_parent (GTK_WIDGET (self->secondary_label));
149 11 subtitle_label = gtk_widget_get_last_child (gtk_widget_get_prev_sibling (suffix_box));
150 11 gtk_accessible_update_relation (GTK_ACCESSIBLE (self),
151 GTK_ACCESSIBLE_RELATION_DESCRIBED_BY,
152 subtitle_label,
153 self->secondary_label,
154 NULL,
155 -1);
156 11 }
157
158 static void
159 11 cc_list_row_init (CcListRow *self)
160 {
161 11 g_resources_register (cc_common_get_resource ());
162
163 11 gtk_widget_init_template (GTK_WIDGET (self));
164
165 11 add_secondary_label_to_a11y_description (self);
166 11 }
167
168 void
169 11 cc_list_row_set_show_arrow (CcListRow *self,
170 gboolean show_arrow)
171 {
172
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
11 g_return_if_fail (CC_IS_LIST_ROW (self));
173
174
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (self->show_arrow == show_arrow)
175 return;
176
177 11 self->show_arrow = show_arrow;
178 11 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SHOW_ARROW]);
179 }
180
181 void
182 cc_list_row_set_secondary_label (CcListRow *self,
183 const gchar *label)
184 {
185 g_return_if_fail (CC_IS_LIST_ROW (self));
186
187 if (!label)
188 label = "";
189
190 if (g_str_equal (label, gtk_label_get_label (self->secondary_label)))
191 return;
192
193 gtk_label_set_text (self->secondary_label, label);
194 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SECONDARY_LABEL]);
195 }
196
197 void
198 cc_list_row_set_secondary_markup (CcListRow *self,
199 const gchar *markup)
200 {
201 g_return_if_fail (CC_IS_LIST_ROW (self));
202
203 if (!markup)
204 markup = "";
205
206 if (g_str_equal (markup, gtk_label_get_label (self->secondary_label)))
207 return;
208
209 gtk_label_set_markup (self->secondary_label, markup);
210 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SECONDARY_LABEL]);
211 }
212