Debian Codegraph @

lib/common/font-manager-progress-data.c

raw
1 /* font-manager-progress-data.c
2 *
3 * Copyright (C) 2009-2023 Jerry Casiano
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.
17 *
18 * If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
19 */
20
21 #include "font-manager-progress-data.h"
22
23 /**
24 * SECTION: font-manager-progress-data
25 * @short_description: Progress data
26 * @title: Progress Data
27 * @include: font-manager-progress-data.h
28 *
29 * #FontManagerProgressData contains data necessary to display progress within the application.
30 */
31
32 struct _FontManagerProgressData
33 {
34 GObject parent;
35 };
36
37 typedef struct
38 {
39 guint processed;
40 guint total;
41 gchar *message;
42 }
43 FontManagerProgressDataPrivate;
44
45 G_DEFINE_TYPE_WITH_PRIVATE (FontManagerProgressData, font_manager_progress_data, G_TYPE_OBJECT)
46
47 enum
48 {
49 PROP_RESERVED,
50 PROP_PROCESSED,
51 PROP_TOTAL,
52 PROP_MESSAGE,
53 PROP_PROGRESS,
54 N_PROPERTIES
55 };
56
57 static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
58
59 static void
60 font_manager_progress_data_dispose (GObject *gobject)
61 {
62 g_return_if_fail(gobject != NULL);
63 FontManagerProgressData *self = FONT_MANAGER_PROGRESS_DATA(gobject);
64 FontManagerProgressDataPrivate *priv = font_manager_progress_data_get_instance_private(self);
65 g_clear_pointer(&priv->message, g_free);
66 G_OBJECT_CLASS(font_manager_progress_data_parent_class)->dispose(gobject);
67 return;
68 }
69
70 static void
71 font_manager_progress_data_get_property (GObject *gobject,
72 guint property_id,
73 GValue *value,
74 GParamSpec *pspec)
75 {
76 g_return_if_fail(gobject != NULL);
77 FontManagerProgressData *self = FONT_MANAGER_PROGRESS_DATA(gobject);
78 FontManagerProgressDataPrivate *priv = font_manager_progress_data_get_instance_private(self);
79 gdouble fraction = ((gdouble) priv->processed / (gdouble) priv->total);
80 switch (property_id) {
81 case PROP_PROCESSED:
82 g_value_set_uint(value, priv->processed);
83 break;
84 case PROP_TOTAL:
85 g_value_set_uint(value, priv->total);
86 break;
87 case PROP_MESSAGE:
88 g_value_set_string(value, priv->message);
89 break;
90 case PROP_PROGRESS:
91 g_value_set_double(value, fraction);
92 break;
93 default:
94 G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
95 break;
96 }
97 return;
98 }
99
100 static void
101 font_manager_progress_data_set_property (GObject *gobject,
102 guint property_id,
103 const GValue *value,
104 GParamSpec *pspec)
105 {
106 g_return_if_fail(gobject != NULL);
107 FontManagerProgressData *self = FONT_MANAGER_PROGRESS_DATA(gobject);
108 FontManagerProgressDataPrivate *priv = font_manager_progress_data_get_instance_private(self);
109 switch (property_id) {
110 case PROP_PROCESSED:
111 priv->processed = g_value_get_uint(value);
112 break;
113 case PROP_TOTAL:
114 priv->total = g_value_get_uint(value);
115 break;
116 case PROP_MESSAGE:
117 if (priv->message)
118 g_free(priv->message);
119 priv->message = g_value_dup_string(value);
120 break;
121 default:
122 G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec);
123 break;
124 }
125 return;
126 }
127
128 static void
129 font_manager_progress_data_class_init (FontManagerProgressDataClass *klass)
130 {
131 GObjectClass *object_class = G_OBJECT_CLASS(klass);
132 object_class->dispose = font_manager_progress_data_dispose;
133 object_class->get_property = font_manager_progress_data_get_property;
134 object_class->set_property = font_manager_progress_data_set_property;
135
136 /**
137 * FontManagerProgressData:processed
138 *
139 * Amount processed so far
140 */
141 obj_properties[PROP_PROCESSED] = g_param_spec_uint("processed",
142 NULL,
143 "Amount processed",
144 0, G_MAXUINT, 0,
145 G_PARAM_READWRITE);
146
147 /**
148 * FontManagerProgressData:total
149 *
150 * Total amount to process
151 */
152 obj_properties[PROP_TOTAL] = g_param_spec_uint("total",
153 NULL,
154 "Total to process",
155 0, G_MAXUINT, 0,
156 G_PARAM_READWRITE);
157
158 /**
159 * FontManagerProgressData:message
160 *
161 * String suitable for display
162 */
163 obj_properties[PROP_MESSAGE] = g_param_spec_string("message",
164 NULL,
165 "Message to display",
166 NULL,
167 G_PARAM_READWRITE);
168
169 /**
170 * FontManagerProgressData:progress
171 *
172 * Progress as fraction between 0.0 - 1.0
173 */
174 obj_properties[PROP_PROGRESS] = g_param_spec_double("progress",
175 NULL,
176 "Progress as a fraction between 0.0 - 1.0",
177 0.0, 1.0, 0.0,
178 G_PARAM_READABLE);
179
180 g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties);
181 return;
182 }
183
184 static void
185 font_manager_progress_data_init (G_GNUC_UNUSED FontManagerProgressData *self)
186 {
187 g_return_if_fail(self != NULL);
188 }
189
190 /**
191 * font_manager_progress_data_print:
192 * @self: #FontManagerProgressData
193 *
194 * Print progress to stdout.
195 *
196 * Returns: %G_SOURCE_REMOVE
197 */
198 gboolean
199 font_manager_progress_data_print (FontManagerProgressData *self)
200 {
201 gint width = 72;
202 gdouble progress;
203 g_object_get(self, "progress", &progress, NULL);
204 if (progress < 1.0) {
205 gint position = (gint) (((gdouble) width) * progress);
206 fprintf(stdout, "\r[");
207 for (gint i = 0; i < width; i++) {
208 if (i < position)
209 fprintf(stdout, "=");
210 else if (i == position)
211 fprintf(stdout, ">");
212 else
213 fprintf(stdout, " ");
214 }
215 if (progress >= 0.99)
216 fprintf(stdout, "] %i%% \r", 100);
217 else
218 fprintf(stdout, "] %i%% \r", (gint) (progress * 100.0));
219 fflush(stdout);
220 }
221 return G_SOURCE_REMOVE;
222 }
223
224 /**
225 * font_manager_progress_data_new:
226 * @message: (nullable): string suitable for display
227 * @processed: amount processed so far
228 * @total: total amount to process
229 *
230 * Returns: (transfer full): A newly created #FontManagerProgressData.
231 * Free the returned object using #g_object_unref().
232 */
233 FontManagerProgressData *
234 font_manager_progress_data_new (const gchar *message, guint processed, guint total)
235 {
236 return g_object_new(FONT_MANAGER_TYPE_PROGRESS_DATA,
237 "message", message, "processed", processed, "total", total,
238 NULL);
239 }
240