Debian Codegraph @

src/libsysprof/sysprof-document-task.c

raw
1 /*
2 * sysprof-document-task.c
3 *
4 * Copyright 2024 Christian Hergert <[email protected]>
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 * SPDX-License-Identifier: GPL-3.0-or-later
20 */
21
22 #include "config.h"
23
24 #include "sysprof-document-loader-private.h"
25 #include "sysprof-document-task-private.h"
26
27 typedef struct
28 {
29 GMutex mutex;
30 char *message;
31 char *title;
32 double progress;
33 GCancellable *cancellable;
34 guint notify_source;
35 GWeakRef loader_wr;
36 } SysprofDocumentTaskPrivate;
37
38 enum {
39 PROP_0,
40 PROP_CANCELLED,
41 PROP_MESSAGE,
42 PROP_PROGRESS,
43 PROP_TITLE,
44 N_PROPS
45 };
46
47 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (SysprofDocumentTask, sysprof_document_task, G_TYPE_OBJECT)
48
49 static GParamSpec *properties[N_PROPS];
50
51 static void
52 sysprof_document_task_finalize (GObject *object)
53 {
54 SysprofDocumentTask *self = (SysprofDocumentTask *)object;
55 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
56
57 g_mutex_clear (&priv->mutex);
58 g_clear_handle_id (&priv->notify_source, g_source_remove);
59 g_clear_pointer (&priv->message, g_free);
60 g_clear_pointer (&priv->title, g_free);
61 g_clear_object (&priv->cancellable);
62 g_weak_ref_clear (&priv->loader_wr);
63
64 G_OBJECT_CLASS (sysprof_document_task_parent_class)->finalize (object);
65 }
66
67 static void
68 sysprof_document_task_get_property (GObject *object,
69 guint prop_id,
70 GValue *value,
71 GParamSpec *pspec)
72 {
73 SysprofDocumentTask *self = SYSPROF_DOCUMENT_TASK (object);
74
75 switch (prop_id)
76 {
77 case PROP_CANCELLED:
78 g_value_set_boolean (value, sysprof_document_task_is_cancelled (self));
79 break;
80
81 case PROP_MESSAGE:
82 g_value_take_string (value, sysprof_document_task_dup_message (self));
83 break;
84
85 case PROP_PROGRESS:
86 g_value_set_double (value, sysprof_document_task_get_progress (self));
87 break;
88
89 case PROP_TITLE:
90 g_value_take_string (value, sysprof_document_task_dup_title (self));
91 break;
92
93 default:
94 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
95 }
96 }
97
98 static void
99 sysprof_document_task_class_init (SysprofDocumentTaskClass *klass)
100 {
101 GObjectClass *object_class = G_OBJECT_CLASS (klass);
102
103 object_class->finalize = sysprof_document_task_finalize;
104 object_class->get_property = sysprof_document_task_get_property;
105
106 properties[PROP_CANCELLED] =
107 g_param_spec_boolean ("cancelled", NULL, NULL,
108 FALSE,
109 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
110
111 properties[PROP_MESSAGE] =
112 g_param_spec_string ("message", NULL, NULL,
113 NULL,
114 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
115
116 properties[PROP_PROGRESS] =
117 g_param_spec_double ("progress", NULL, NULL,
118 0., 1., 0.,
119 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
120
121 properties[PROP_TITLE] =
122 g_param_spec_string ("title", NULL, NULL,
123 NULL,
124 (G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
125
126 g_object_class_install_properties (object_class, N_PROPS, properties);
127 }
128
129 static void
130 sysprof_document_task_init (SysprofDocumentTask *self)
131 {
132 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
133
134 g_weak_ref_init (&priv->loader_wr, NULL);
135
136 priv->cancellable = g_cancellable_new ();
137 }
138
139 static gboolean
140 notify_in_idle_cb (gpointer data)
141 {
142 SysprofDocumentTask *self = data;
143 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
144
145 g_assert (SYSPROF_IS_DOCUMENT_TASK (self));
146
147 g_mutex_lock (&priv->mutex);
148 priv->notify_source = 0;
149 g_mutex_unlock (&priv->mutex);
150
151 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_CANCELLED]);
152 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_MESSAGE]);
153 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_PROGRESS]);
154 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_TITLE]);
155
156 return G_SOURCE_REMOVE;
157 }
158
159 static void
160 notify_in_idle_locked (SysprofDocumentTask *self)
161 {
162 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
163
164 g_assert (SYSPROF_IS_DOCUMENT_TASK (self));
165
166 if (priv->notify_source == 0)
167 priv->notify_source = g_idle_add_full (G_PRIORITY_LOW,
168 notify_in_idle_cb,
169 g_object_ref (self),
170 g_object_unref);
171 }
172
173 char *
174 sysprof_document_task_dup_message (SysprofDocumentTask *self)
175 {
176 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
177 char *ret;
178
179 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_TASK (self), NULL);
180
181 g_mutex_lock (&priv->mutex);
182 ret = g_strdup (priv->message);
183 g_mutex_unlock (&priv->mutex);
184
185 return ret;
186 }
187
188 char *
189 sysprof_document_task_dup_title (SysprofDocumentTask *self)
190 {
191 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
192 char *ret;
193
194 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_TASK (self), NULL);
195
196 g_mutex_lock (&priv->mutex);
197 ret = g_strdup (priv->title);
198 g_mutex_unlock (&priv->mutex);
199
200 return ret;
201 }
202
203 void
204 _sysprof_document_task_take_message (SysprofDocumentTask *self,
205 char *message)
206 {
207 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
208
209 g_return_if_fail (SYSPROF_IS_DOCUMENT_TASK (self));
210
211 g_mutex_lock (&priv->mutex);
212 g_free (priv->message);
213 priv->message = message;
214 notify_in_idle_locked (self);
215 g_mutex_unlock (&priv->mutex);
216 }
217
218 double
219 sysprof_document_task_get_progress (SysprofDocumentTask *self)
220 {
221 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
222 double ret;
223
224 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_TASK (self), 0);
225
226 g_mutex_lock (&priv->mutex);
227 ret = priv->progress;
228 g_mutex_unlock (&priv->mutex);
229
230 return ret;
231 }
232
233 void
234 _sysprof_document_task_set_progress (SysprofDocumentTask *self,
235 double progress)
236 {
237 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
238
239 g_return_if_fail (SYSPROF_IS_DOCUMENT_TASK (self));
240
241 progress = CLAMP (progress, 0., 1.);
242
243 g_mutex_lock (&priv->mutex);
244 priv->progress = progress;
245 notify_in_idle_locked (self);
246 g_mutex_unlock (&priv->mutex);
247 }
248
249 void
250 _sysprof_document_task_set_title (SysprofDocumentTask *self,
251 const char *title)
252 {
253 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
254
255 g_return_if_fail (SYSPROF_IS_DOCUMENT_TASK (self));
256
257 g_mutex_lock (&priv->mutex);
258 if (g_set_str (&priv->title, title))
259 notify_in_idle_locked (self);
260 g_mutex_unlock (&priv->mutex);
261 }
262
263 gboolean
264 sysprof_document_task_is_cancelled (SysprofDocumentTask *self)
265 {
266 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
267 gboolean ret;
268
269 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_TASK (self), FALSE);
270
271 g_mutex_lock (&priv->mutex);
272 ret = g_cancellable_is_cancelled (priv->cancellable);
273 g_mutex_unlock (&priv->mutex);
274
275 return ret;
276 }
277
278 GCancellable *
279 _sysprof_document_task_get_cancellable (SysprofDocumentTask *self)
280 {
281 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
282
283 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_TASK (self), NULL);
284
285 return priv->cancellable;
286 }
287
288 SysprofDocumentTaskScope *
289 _sysprof_document_task_register (SysprofDocumentTask *self,
290 SysprofDocumentLoader *loader)
291 {
292 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
293
294 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_TASK (self), NULL);
295 g_return_val_if_fail (SYSPROF_IS_DOCUMENT_LOADER (loader), NULL);
296
297 g_weak_ref_set (&priv->loader_wr, loader);
298
299 _sysprof_document_loader_add_task (loader, self);
300
301 return self;
302 }
303
304 void
305 _sysprof_document_task_unregister (SysprofDocumentTask *self)
306 {
307 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
308 g_autoptr(SysprofDocumentLoader) loader = NULL;
309
310 g_return_if_fail (SYSPROF_IS_DOCUMENT_TASK (self));
311
312 if ((loader = g_weak_ref_get (&priv->loader_wr)))
313 _sysprof_document_loader_remove_task (loader, self);
314 }
315
316 void
317 sysprof_document_task_cancel (SysprofDocumentTask *self)
318 {
319 SysprofDocumentTaskPrivate *priv = sysprof_document_task_get_instance_private (self);
320
321 g_return_if_fail (SYSPROF_IS_DOCUMENT_TASK (self));
322
323 g_cancellable_cancel (priv->cancellable);
324
325 if (SYSPROF_DOCUMENT_TASK_GET_CLASS (self)->cancel)
326 SYSPROF_DOCUMENT_TASK_GET_CLASS (self)->cancel (self);
327 }