/src/fwupd/libfwupd/fwupd-request.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2021 Richard Hughes <richard@hughsie.com> |
3 | | * |
4 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | | */ |
6 | | |
7 | | #include "config.h" |
8 | | |
9 | | #include "fwupd-codec.h" |
10 | | #include "fwupd-enums-private.h" |
11 | | #include "fwupd-request-private.h" |
12 | | |
13 | | /** |
14 | | * FwupdRequest: |
15 | | * |
16 | | * A user request from the device. |
17 | | * |
18 | | * See also: [class@FwupdDevice] |
19 | | */ |
20 | | |
21 | | typedef struct { |
22 | | gchar *id; |
23 | | FwupdRequestKind kind; |
24 | | FwupdRequestFlags flags; |
25 | | guint64 created; |
26 | | gchar *device_id; |
27 | | gchar *message; |
28 | | gchar *image; |
29 | | } FwupdRequestPrivate; |
30 | | |
31 | | enum { SIGNAL_INVALIDATE, SIGNAL_LAST }; |
32 | | |
33 | | enum { |
34 | | PROP_0, |
35 | | PROP_ID, |
36 | | PROP_KIND, |
37 | | PROP_FLAGS, |
38 | | PROP_MESSAGE, |
39 | | PROP_IMAGE, |
40 | | PROP_DEVICE_ID, |
41 | | PROP_LAST |
42 | | }; |
43 | | |
44 | | static guint signals[SIGNAL_LAST] = {0}; |
45 | | |
46 | | static void |
47 | | fwupd_request_codec_iface_init(FwupdCodecInterface *iface); |
48 | | |
49 | | G_DEFINE_TYPE_EXTENDED(FwupdRequest, |
50 | | fwupd_request, |
51 | | G_TYPE_OBJECT, |
52 | | 0, |
53 | | G_ADD_PRIVATE(FwupdRequest) |
54 | | G_IMPLEMENT_INTERFACE(FWUPD_TYPE_CODEC, fwupd_request_codec_iface_init)); |
55 | | |
56 | 0 | #define GET_PRIVATE(o) (fwupd_request_get_instance_private(o)) |
57 | | |
58 | | /** |
59 | | * fwupd_request_kind_to_string: |
60 | | * @kind: a update message kind, e.g. %FWUPD_REQUEST_KIND_IMMEDIATE |
61 | | * |
62 | | * Converts an enumerated update message kind to a string. |
63 | | * |
64 | | * Returns: identifier string |
65 | | * |
66 | | * Since: 1.6.2 |
67 | | **/ |
68 | | const gchar * |
69 | | fwupd_request_kind_to_string(FwupdRequestKind kind) |
70 | 0 | { |
71 | 0 | if (kind == FWUPD_REQUEST_KIND_UNKNOWN) |
72 | 0 | return "unknown"; |
73 | 0 | if (kind == FWUPD_REQUEST_KIND_POST) |
74 | 0 | return "post"; |
75 | 0 | if (kind == FWUPD_REQUEST_KIND_IMMEDIATE) |
76 | 0 | return "immediate"; |
77 | 0 | return NULL; |
78 | 0 | } |
79 | | |
80 | | /** |
81 | | * fwupd_request_kind_from_string: |
82 | | * @kind: (nullable): a string, e.g. `immediate` |
83 | | * |
84 | | * Converts a string to an enumerated update message kind. |
85 | | * |
86 | | * Returns: enumerated value |
87 | | * |
88 | | * Since: 1.6.2 |
89 | | **/ |
90 | | FwupdRequestKind |
91 | | fwupd_request_kind_from_string(const gchar *kind) |
92 | 0 | { |
93 | 0 | if (g_strcmp0(kind, "unknown") == 0) |
94 | 0 | return FWUPD_REQUEST_KIND_UNKNOWN; |
95 | 0 | if (g_strcmp0(kind, "post") == 0) |
96 | 0 | return FWUPD_REQUEST_KIND_POST; |
97 | 0 | if (g_strcmp0(kind, "immediate") == 0) |
98 | 0 | return FWUPD_REQUEST_KIND_IMMEDIATE; |
99 | 0 | return FWUPD_REQUEST_KIND_LAST; |
100 | 0 | } |
101 | | |
102 | | /** |
103 | | * fwupd_request_flag_to_string: |
104 | | * @flag: a request flag, e.g. %FWUPD_REQUEST_FLAG_NONE |
105 | | * |
106 | | * Converts an enumerated request flag to a string. |
107 | | * |
108 | | * Returns: identifier string |
109 | | * |
110 | | * Since: 1.8.6 |
111 | | **/ |
112 | | const gchar * |
113 | | fwupd_request_flag_to_string(FwupdRequestFlags flag) |
114 | 0 | { |
115 | 0 | if (flag == FWUPD_REQUEST_FLAG_NONE) |
116 | 0 | return "none"; |
117 | 0 | if (flag == FWUPD_REQUEST_FLAG_ALLOW_GENERIC_MESSAGE) |
118 | 0 | return "allow-generic-message"; |
119 | 0 | if (flag == FWUPD_REQUEST_FLAG_ALLOW_GENERIC_IMAGE) |
120 | 0 | return "allow-generic-image"; |
121 | 0 | if (flag == FWUPD_REQUEST_FLAG_NON_GENERIC_MESSAGE) |
122 | 0 | return "non-generic-message"; |
123 | 0 | if (flag == FWUPD_REQUEST_FLAG_NON_GENERIC_IMAGE) |
124 | 0 | return "non-generic-image"; |
125 | 0 | return NULL; |
126 | 0 | } |
127 | | |
128 | | /** |
129 | | * fwupd_request_flag_from_string: |
130 | | * @flag: (nullable): a string, e.g. `none` |
131 | | * |
132 | | * Converts a string to an enumerated request flag. |
133 | | * |
134 | | * Returns: enumerated value |
135 | | * |
136 | | * Since: 1.8.6 |
137 | | **/ |
138 | | FwupdRequestFlags |
139 | | fwupd_request_flag_from_string(const gchar *flag) |
140 | 0 | { |
141 | 0 | if (g_strcmp0(flag, "allow-generic-message") == 0) |
142 | 0 | return FWUPD_REQUEST_FLAG_ALLOW_GENERIC_MESSAGE; |
143 | 0 | if (g_strcmp0(flag, "allow-generic-image") == 0) |
144 | 0 | return FWUPD_REQUEST_FLAG_ALLOW_GENERIC_IMAGE; |
145 | 0 | if (g_strcmp0(flag, "non-generic-message") == 0) |
146 | 0 | return FWUPD_REQUEST_FLAG_NON_GENERIC_MESSAGE; |
147 | 0 | if (g_strcmp0(flag, "non-generic-image") == 0) |
148 | 0 | return FWUPD_REQUEST_FLAG_NON_GENERIC_IMAGE; |
149 | 0 | return FWUPD_REQUEST_FLAG_NONE; |
150 | 0 | } |
151 | | |
152 | | /** |
153 | | * fwupd_request_emit_invalidate: |
154 | | * @self: a #FwupdRequest |
155 | | * |
156 | | * Emits an `invalidate` signal to signify that the request is no longer valid, and any visible |
157 | | * UI components should be hidden. |
158 | | * |
159 | | * Since: 1.9.17 |
160 | | **/ |
161 | | void |
162 | | fwupd_request_emit_invalidate(FwupdRequest *self) |
163 | 0 | { |
164 | 0 | g_return_if_fail(FWUPD_IS_REQUEST(self)); |
165 | 0 | g_debug("emitting FwupdRequest::invalidate()"); |
166 | 0 | g_signal_emit(self, signals[SIGNAL_INVALIDATE], 0); |
167 | 0 | } |
168 | | |
169 | | /** |
170 | | * fwupd_request_get_id: |
171 | | * @self: a #FwupdRequest |
172 | | * |
173 | | * Gets the ID. |
174 | | * |
175 | | * Returns: the ID, or %NULL if unset |
176 | | * |
177 | | * Since: 1.6.2 |
178 | | **/ |
179 | | const gchar * |
180 | | fwupd_request_get_id(FwupdRequest *self) |
181 | 0 | { |
182 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
183 | 0 | g_return_val_if_fail(FWUPD_IS_REQUEST(self), NULL); |
184 | 0 | return priv->id; |
185 | 0 | } |
186 | | |
187 | | /** |
188 | | * fwupd_request_set_id: |
189 | | * @self: a #FwupdRequest |
190 | | * @id: (nullable): the request ID, e.g. `USB:foo` |
191 | | * |
192 | | * Sets the ID. |
193 | | * |
194 | | * Since: 1.6.2 |
195 | | **/ |
196 | | void |
197 | | fwupd_request_set_id(FwupdRequest *self, const gchar *id) |
198 | 0 | { |
199 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
200 | 0 | g_return_if_fail(FWUPD_IS_REQUEST(self)); |
201 | | |
202 | | /* not changed */ |
203 | 0 | if (g_strcmp0(priv->id, id) == 0) |
204 | 0 | return; |
205 | | |
206 | 0 | g_free(priv->id); |
207 | 0 | priv->id = g_strdup(id); |
208 | 0 | } |
209 | | |
210 | | /** |
211 | | * fwupd_request_get_device_id: |
212 | | * @self: a #FwupdRequest |
213 | | * |
214 | | * Gets the device_id that created the request. |
215 | | * |
216 | | * Returns: the device_id, or %NULL if unset |
217 | | * |
218 | | * Since: 1.6.2 |
219 | | **/ |
220 | | const gchar * |
221 | | fwupd_request_get_device_id(FwupdRequest *self) |
222 | 0 | { |
223 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
224 | 0 | g_return_val_if_fail(FWUPD_IS_REQUEST(self), NULL); |
225 | 0 | return priv->device_id; |
226 | 0 | } |
227 | | |
228 | | /** |
229 | | * fwupd_request_set_device_id: |
230 | | * @self: a #FwupdRequest |
231 | | * @device_id: (nullable): the device_id, e.g. `colorhug` |
232 | | * |
233 | | * Sets the device_id that created the request. |
234 | | * |
235 | | * Since: 1.6.2 |
236 | | **/ |
237 | | void |
238 | | fwupd_request_set_device_id(FwupdRequest *self, const gchar *device_id) |
239 | 0 | { |
240 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
241 | 0 | g_return_if_fail(FWUPD_IS_REQUEST(self)); |
242 | | |
243 | | /* not changed */ |
244 | 0 | if (g_strcmp0(priv->device_id, device_id) == 0) |
245 | 0 | return; |
246 | | |
247 | 0 | g_free(priv->device_id); |
248 | 0 | priv->device_id = g_strdup(device_id); |
249 | 0 | } |
250 | | |
251 | | /** |
252 | | * fwupd_request_get_created: |
253 | | * @self: a #FwupdRequest |
254 | | * |
255 | | * Gets when the request was created. |
256 | | * |
257 | | * Returns: the UNIX time, or 0 if unset |
258 | | * |
259 | | * Since: 1.6.2 |
260 | | **/ |
261 | | guint64 |
262 | | fwupd_request_get_created(FwupdRequest *self) |
263 | 0 | { |
264 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
265 | 0 | g_return_val_if_fail(FWUPD_IS_REQUEST(self), 0); |
266 | 0 | return priv->created; |
267 | 0 | } |
268 | | |
269 | | /** |
270 | | * fwupd_request_set_created: |
271 | | * @self: a #FwupdRequest |
272 | | * @created: the UNIX time |
273 | | * |
274 | | * Sets when the request was created. |
275 | | * |
276 | | * Since: 1.6.2 |
277 | | **/ |
278 | | void |
279 | | fwupd_request_set_created(FwupdRequest *self, guint64 created) |
280 | 0 | { |
281 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
282 | 0 | g_return_if_fail(FWUPD_IS_REQUEST(self)); |
283 | 0 | priv->created = created; |
284 | 0 | } |
285 | | |
286 | | static void |
287 | | fwupd_request_add_variant(FwupdCodec *codec, GVariantBuilder *builder, FwupdCodecFlags flags) |
288 | 0 | { |
289 | 0 | FwupdRequest *self = FWUPD_REQUEST(codec); |
290 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
291 | |
|
292 | 0 | if (priv->id != NULL) { |
293 | 0 | g_variant_builder_add(builder, |
294 | 0 | "{sv}", |
295 | 0 | FWUPD_RESULT_KEY_APPSTREAM_ID, |
296 | 0 | g_variant_new_string(priv->id)); |
297 | 0 | } |
298 | 0 | if (priv->created > 0) { |
299 | 0 | g_variant_builder_add(builder, |
300 | 0 | "{sv}", |
301 | 0 | FWUPD_RESULT_KEY_CREATED, |
302 | 0 | g_variant_new_uint64(priv->created)); |
303 | 0 | } |
304 | 0 | if (priv->device_id != NULL) { |
305 | 0 | g_variant_builder_add(builder, |
306 | 0 | "{sv}", |
307 | 0 | FWUPD_RESULT_KEY_DEVICE_ID, |
308 | 0 | g_variant_new_string(priv->device_id)); |
309 | 0 | } |
310 | 0 | if (priv->message != NULL) { |
311 | 0 | g_variant_builder_add(builder, |
312 | 0 | "{sv}", |
313 | 0 | FWUPD_RESULT_KEY_UPDATE_MESSAGE, |
314 | 0 | g_variant_new_string(priv->message)); |
315 | 0 | } |
316 | 0 | if (priv->image != NULL) { |
317 | 0 | g_variant_builder_add(builder, |
318 | 0 | "{sv}", |
319 | 0 | FWUPD_RESULT_KEY_UPDATE_IMAGE, |
320 | 0 | g_variant_new_string(priv->image)); |
321 | 0 | } |
322 | 0 | if (priv->kind != FWUPD_REQUEST_KIND_UNKNOWN) { |
323 | 0 | g_variant_builder_add(builder, |
324 | 0 | "{sv}", |
325 | 0 | FWUPD_RESULT_KEY_REQUEST_KIND, |
326 | 0 | g_variant_new_uint32(priv->kind)); |
327 | 0 | } |
328 | 0 | if (priv->flags != FWUPD_REQUEST_FLAG_NONE) { |
329 | 0 | g_variant_builder_add(builder, |
330 | 0 | "{sv}", |
331 | 0 | FWUPD_RESULT_KEY_FLAGS, |
332 | 0 | g_variant_new_uint64(priv->flags)); |
333 | 0 | } |
334 | 0 | } |
335 | | |
336 | | static void |
337 | | fwupd_request_from_key_value(FwupdRequest *self, const gchar *key, GVariant *value) |
338 | 0 | { |
339 | 0 | if (g_strcmp0(key, FWUPD_RESULT_KEY_APPSTREAM_ID) == 0) { |
340 | 0 | fwupd_request_set_id(self, g_variant_get_string(value, NULL)); |
341 | 0 | return; |
342 | 0 | } |
343 | 0 | if (g_strcmp0(key, FWUPD_RESULT_KEY_CREATED) == 0) { |
344 | 0 | fwupd_request_set_created(self, g_variant_get_uint64(value)); |
345 | 0 | return; |
346 | 0 | } |
347 | 0 | if (g_strcmp0(key, FWUPD_RESULT_KEY_DEVICE_ID) == 0) { |
348 | 0 | fwupd_request_set_device_id(self, g_variant_get_string(value, NULL)); |
349 | 0 | return; |
350 | 0 | } |
351 | 0 | if (g_strcmp0(key, FWUPD_RESULT_KEY_UPDATE_MESSAGE) == 0) { |
352 | 0 | fwupd_request_set_message(self, g_variant_get_string(value, NULL)); |
353 | 0 | return; |
354 | 0 | } |
355 | 0 | if (g_strcmp0(key, FWUPD_RESULT_KEY_UPDATE_IMAGE) == 0) { |
356 | 0 | fwupd_request_set_image(self, g_variant_get_string(value, NULL)); |
357 | 0 | return; |
358 | 0 | } |
359 | 0 | if (g_strcmp0(key, FWUPD_RESULT_KEY_REQUEST_KIND) == 0) { |
360 | 0 | fwupd_request_set_kind(self, g_variant_get_uint32(value)); |
361 | 0 | return; |
362 | 0 | } |
363 | 0 | if (g_strcmp0(key, FWUPD_RESULT_KEY_FLAGS) == 0) { |
364 | 0 | fwupd_request_set_flags(self, g_variant_get_uint64(value)); |
365 | 0 | return; |
366 | 0 | } |
367 | 0 | } |
368 | | |
369 | | /** |
370 | | * fwupd_request_get_message: |
371 | | * @self: a #FwupdRequest |
372 | | * |
373 | | * Gets the update message, generating a generic one using the request ID if possible. |
374 | | * |
375 | | * Returns: the update message, or %NULL if unset |
376 | | * |
377 | | * Since: 1.6.2 |
378 | | **/ |
379 | | const gchar * |
380 | | fwupd_request_get_message(FwupdRequest *self) |
381 | 0 | { |
382 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
383 | 0 | g_return_val_if_fail(FWUPD_IS_REQUEST(self), NULL); |
384 | | |
385 | | /* something custom */ |
386 | 0 | if (priv->message != NULL) |
387 | 0 | return priv->message; |
388 | | |
389 | | /* untranslated canned messages */ |
390 | 0 | if (fwupd_request_has_flag(self, FWUPD_REQUEST_FLAG_ALLOW_GENERIC_MESSAGE)) { |
391 | 0 | if (g_strcmp0(priv->id, FWUPD_REQUEST_ID_REMOVE_REPLUG) == 0) |
392 | 0 | return "Please unplug and then re-insert the device USB cable."; |
393 | 0 | if (g_strcmp0(priv->id, FWUPD_REQUEST_ID_INSERT_USB_CABLE) == 0) |
394 | 0 | return "Please re-insert the device USB cable."; |
395 | 0 | if (g_strcmp0(priv->id, FWUPD_REQUEST_ID_REMOVE_USB_CABLE) == 0) |
396 | 0 | return "Please unplug the device USB cable."; |
397 | 0 | if (g_strcmp0(priv->id, FWUPD_REQUEST_ID_REPLUG_POWER) == 0) |
398 | 0 | return "Please unplug and then re-insert the device power cable."; |
399 | 0 | if (g_strcmp0(priv->id, FWUPD_REQUEST_ID_PRESS_UNLOCK) == 0) |
400 | 0 | return "Press unlock on the device."; |
401 | 0 | if (g_strcmp0(priv->id, FWUPD_REQUEST_ID_DO_NOT_POWER_OFF) == 0) |
402 | 0 | return "Do not turn off your computer or remove the AC adaptor."; |
403 | 0 | if (g_strcmp0(priv->id, FWUPD_REQUEST_ID_RESTART_DAEMON) == 0) |
404 | 0 | return "Please restart the fwupd service."; |
405 | 0 | } |
406 | | |
407 | | /* unknown */ |
408 | 0 | return NULL; |
409 | 0 | } |
410 | | |
411 | | /** |
412 | | * fwupd_request_set_message: |
413 | | * @self: a #FwupdRequest |
414 | | * @message: (nullable): the update message string |
415 | | * |
416 | | * Sets the update message. |
417 | | * |
418 | | * Since: 1.6.2 |
419 | | **/ |
420 | | void |
421 | | fwupd_request_set_message(FwupdRequest *self, const gchar *message) |
422 | 0 | { |
423 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
424 | 0 | g_return_if_fail(FWUPD_IS_REQUEST(self)); |
425 | | |
426 | | /* not changed */ |
427 | 0 | if (g_strcmp0(priv->message, message) == 0) |
428 | 0 | return; |
429 | | |
430 | 0 | g_free(priv->message); |
431 | 0 | priv->message = g_strdup(message); |
432 | 0 | g_object_notify(G_OBJECT(self), "message"); |
433 | 0 | } |
434 | | |
435 | | /** |
436 | | * fwupd_request_get_image: |
437 | | * @self: a #FwupdRequest |
438 | | * |
439 | | * Gets the update image. |
440 | | * |
441 | | * Returns: the update image URL, or %NULL if unset |
442 | | * |
443 | | * Since: 1.6.2 |
444 | | **/ |
445 | | const gchar * |
446 | | fwupd_request_get_image(FwupdRequest *self) |
447 | 0 | { |
448 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
449 | 0 | g_return_val_if_fail(FWUPD_IS_REQUEST(self), NULL); |
450 | 0 | return priv->image; |
451 | 0 | } |
452 | | |
453 | | /** |
454 | | * fwupd_request_set_image: |
455 | | * @self: a #FwupdRequest |
456 | | * @image: (nullable): the update image URL |
457 | | * |
458 | | * Sets the update image. |
459 | | * |
460 | | * Since: 1.6.2 |
461 | | **/ |
462 | | void |
463 | | fwupd_request_set_image(FwupdRequest *self, const gchar *image) |
464 | 0 | { |
465 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
466 | 0 | g_return_if_fail(FWUPD_IS_REQUEST(self)); |
467 | | |
468 | | /* not changed */ |
469 | 0 | if (g_strcmp0(priv->image, image) == 0) |
470 | 0 | return; |
471 | | |
472 | 0 | g_free(priv->image); |
473 | 0 | priv->image = g_strdup(image); |
474 | 0 | g_object_notify(G_OBJECT(self), "image"); |
475 | 0 | } |
476 | | |
477 | | /** |
478 | | * fwupd_request_get_kind: |
479 | | * @self: a #FwupdRequest |
480 | | * |
481 | | * Returns what the request is currently doing. |
482 | | * |
483 | | * Returns: the kind value, e.g. %FWUPD_STATUS_REQUEST_WRITE |
484 | | * |
485 | | * Since: 1.6.2 |
486 | | **/ |
487 | | FwupdRequestKind |
488 | | fwupd_request_get_kind(FwupdRequest *self) |
489 | 0 | { |
490 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
491 | 0 | g_return_val_if_fail(FWUPD_IS_REQUEST(self), 0); |
492 | 0 | return priv->kind; |
493 | 0 | } |
494 | | |
495 | | /** |
496 | | * fwupd_request_set_kind: |
497 | | * @self: a #FwupdRequest |
498 | | * @kind: the kind value, e.g. %FWUPD_STATUS_REQUEST_WRITE |
499 | | * |
500 | | * Sets what the request is currently doing. |
501 | | * |
502 | | * Since: 1.6.2 |
503 | | **/ |
504 | | void |
505 | | fwupd_request_set_kind(FwupdRequest *self, FwupdRequestKind kind) |
506 | 0 | { |
507 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
508 | 0 | g_return_if_fail(FWUPD_IS_REQUEST(self)); |
509 | 0 | if (priv->kind == kind) |
510 | 0 | return; |
511 | 0 | priv->kind = kind; |
512 | 0 | g_object_notify(G_OBJECT(self), "kind"); |
513 | 0 | } |
514 | | |
515 | | /** |
516 | | * fwupd_request_get_flags: |
517 | | * @self: a #FwupdRequest |
518 | | * |
519 | | * Gets the request flags. |
520 | | * |
521 | | * Returns: request flags, or 0 if unset |
522 | | * |
523 | | * Since: 1.8.6 |
524 | | **/ |
525 | | FwupdRequestFlags |
526 | | fwupd_request_get_flags(FwupdRequest *self) |
527 | 0 | { |
528 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
529 | 0 | g_return_val_if_fail(FWUPD_IS_REQUEST(self), 0); |
530 | 0 | return priv->flags; |
531 | 0 | } |
532 | | |
533 | | /** |
534 | | * fwupd_request_set_flags: |
535 | | * @self: a #FwupdRequest |
536 | | * @flags: request flags, e.g. %FWUPD_REQUEST_FLAG_NONE |
537 | | * |
538 | | * Sets the request flags. |
539 | | * |
540 | | * Since: 1.8.6 |
541 | | **/ |
542 | | void |
543 | | fwupd_request_set_flags(FwupdRequest *self, FwupdRequestFlags flags) |
544 | 0 | { |
545 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
546 | 0 | g_return_if_fail(FWUPD_IS_REQUEST(self)); |
547 | | |
548 | | /* not changed */ |
549 | 0 | if (priv->flags == flags) |
550 | 0 | return; |
551 | | |
552 | 0 | priv->flags = flags; |
553 | 0 | g_object_notify(G_OBJECT(self), "flags"); |
554 | 0 | } |
555 | | |
556 | | /** |
557 | | * fwupd_request_add_flag: |
558 | | * @self: a #FwupdRequest |
559 | | * @flag: the #FwupdRequestFlags |
560 | | * |
561 | | * Adds a specific flag to the request. |
562 | | * |
563 | | * Since: 1.8.6 |
564 | | **/ |
565 | | void |
566 | | fwupd_request_add_flag(FwupdRequest *self, FwupdRequestFlags flag) |
567 | 0 | { |
568 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
569 | 0 | g_return_if_fail(FWUPD_IS_REQUEST(self)); |
570 | 0 | priv->flags |= flag; |
571 | 0 | } |
572 | | |
573 | | /** |
574 | | * fwupd_request_remove_flag: |
575 | | * @self: a #FwupdRequest |
576 | | * @flag: the #FwupdRequestFlags |
577 | | * |
578 | | * Removes a specific flag from the request. |
579 | | * |
580 | | * Since: 1.8.6 |
581 | | **/ |
582 | | void |
583 | | fwupd_request_remove_flag(FwupdRequest *self, FwupdRequestFlags flag) |
584 | 0 | { |
585 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
586 | 0 | g_return_if_fail(FWUPD_IS_REQUEST(self)); |
587 | 0 | priv->flags &= ~flag; |
588 | 0 | } |
589 | | |
590 | | /** |
591 | | * fwupd_request_has_flag: |
592 | | * @self: a #FwupdRequest |
593 | | * @flag: the #FwupdRequestFlags |
594 | | * |
595 | | * Finds if the request has a specific flag. |
596 | | * |
597 | | * Returns: %TRUE if the flag is set |
598 | | * |
599 | | * Since: 1.8.6 |
600 | | **/ |
601 | | gboolean |
602 | | fwupd_request_has_flag(FwupdRequest *self, FwupdRequestFlags flag) |
603 | 0 | { |
604 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
605 | 0 | g_return_val_if_fail(FWUPD_IS_REQUEST(self), FALSE); |
606 | 0 | return (priv->flags & flag) > 0; |
607 | 0 | } |
608 | | |
609 | | static void |
610 | | fwupd_request_add_string(FwupdCodec *codec, guint idt, GString *str) |
611 | 0 | { |
612 | 0 | FwupdRequest *self = FWUPD_REQUEST(codec); |
613 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
614 | 0 | fwupd_codec_string_append(str, idt, FWUPD_RESULT_KEY_APPSTREAM_ID, priv->id); |
615 | 0 | if (priv->kind != FWUPD_REQUEST_KIND_UNKNOWN) { |
616 | 0 | fwupd_codec_string_append(str, |
617 | 0 | idt, |
618 | 0 | FWUPD_RESULT_KEY_REQUEST_KIND, |
619 | 0 | fwupd_request_kind_to_string(priv->kind)); |
620 | 0 | } |
621 | 0 | fwupd_codec_string_append(str, |
622 | 0 | idt, |
623 | 0 | FWUPD_RESULT_KEY_FLAGS, |
624 | 0 | fwupd_request_flag_to_string(priv->flags)); |
625 | 0 | fwupd_codec_string_append(str, idt, FWUPD_RESULT_KEY_DEVICE_ID, priv->device_id); |
626 | 0 | fwupd_codec_string_append_time(str, idt, FWUPD_RESULT_KEY_CREATED, priv->created); |
627 | 0 | fwupd_codec_string_append(str, idt, FWUPD_RESULT_KEY_UPDATE_MESSAGE, priv->message); |
628 | 0 | fwupd_codec_string_append(str, idt, FWUPD_RESULT_KEY_UPDATE_IMAGE, priv->image); |
629 | 0 | } |
630 | | |
631 | | static void |
632 | | fwupd_request_get_property(GObject *object, guint prop_id, GValue *value, GParamSpec *pspec) |
633 | 0 | { |
634 | 0 | FwupdRequest *self = FWUPD_REQUEST(object); |
635 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
636 | 0 | switch (prop_id) { |
637 | 0 | case PROP_ID: |
638 | 0 | g_value_set_string(value, priv->id); |
639 | 0 | break; |
640 | 0 | case PROP_MESSAGE: |
641 | 0 | g_value_set_string(value, priv->message); |
642 | 0 | break; |
643 | 0 | case PROP_IMAGE: |
644 | 0 | g_value_set_string(value, priv->image); |
645 | 0 | break; |
646 | 0 | case PROP_DEVICE_ID: |
647 | 0 | g_value_set_string(value, priv->device_id); |
648 | 0 | break; |
649 | 0 | case PROP_KIND: |
650 | 0 | g_value_set_uint(value, priv->kind); |
651 | 0 | break; |
652 | 0 | case PROP_FLAGS: |
653 | 0 | g_value_set_uint64(value, priv->flags); |
654 | 0 | break; |
655 | 0 | default: |
656 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
657 | 0 | break; |
658 | 0 | } |
659 | 0 | } |
660 | | |
661 | | static void |
662 | | fwupd_request_set_property(GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec) |
663 | 0 | { |
664 | 0 | FwupdRequest *self = FWUPD_REQUEST(object); |
665 | 0 | switch (prop_id) { |
666 | 0 | case PROP_ID: |
667 | 0 | fwupd_request_set_id(self, g_value_get_string(value)); |
668 | 0 | break; |
669 | 0 | case PROP_MESSAGE: |
670 | 0 | fwupd_request_set_message(self, g_value_get_string(value)); |
671 | 0 | break; |
672 | 0 | case PROP_IMAGE: |
673 | 0 | fwupd_request_set_image(self, g_value_get_string(value)); |
674 | 0 | break; |
675 | 0 | case PROP_DEVICE_ID: |
676 | 0 | fwupd_request_set_device_id(self, g_value_get_string(value)); |
677 | 0 | break; |
678 | 0 | case PROP_KIND: |
679 | 0 | fwupd_request_set_kind(self, g_value_get_uint(value)); |
680 | 0 | break; |
681 | 0 | case PROP_FLAGS: |
682 | 0 | fwupd_request_set_flags(self, g_value_get_uint64(value)); |
683 | 0 | break; |
684 | 0 | default: |
685 | 0 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
686 | 0 | break; |
687 | 0 | } |
688 | 0 | } |
689 | | |
690 | | static void |
691 | | fwupd_request_finalize(GObject *object) |
692 | 0 | { |
693 | 0 | FwupdRequest *self = FWUPD_REQUEST(object); |
694 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
695 | |
|
696 | 0 | g_free(priv->id); |
697 | 0 | g_free(priv->device_id); |
698 | 0 | g_free(priv->message); |
699 | 0 | g_free(priv->image); |
700 | |
|
701 | 0 | G_OBJECT_CLASS(fwupd_request_parent_class)->finalize(object); |
702 | 0 | } |
703 | | |
704 | | static void |
705 | | fwupd_request_init(FwupdRequest *self) |
706 | 0 | { |
707 | 0 | FwupdRequestPrivate *priv = GET_PRIVATE(self); |
708 | 0 | priv->created = g_get_real_time() / G_USEC_PER_SEC; |
709 | 0 | } |
710 | | |
711 | | static void |
712 | | fwupd_request_class_init(FwupdRequestClass *klass) |
713 | 0 | { |
714 | 0 | GObjectClass *object_class = G_OBJECT_CLASS(klass); |
715 | 0 | GParamSpec *pspec; |
716 | |
|
717 | 0 | object_class->finalize = fwupd_request_finalize; |
718 | 0 | object_class->get_property = fwupd_request_get_property; |
719 | 0 | object_class->set_property = fwupd_request_set_property; |
720 | | |
721 | | /** |
722 | | * FwupdRequest::invalidate: |
723 | | * @self: the #FwupdRequest instance that emitted the signal |
724 | | * |
725 | | * The ::invalidate signal is emitted when the request is no longer valid, and any visible |
726 | | * UI components should be hidden. |
727 | | * |
728 | | * Since: 1.9.17 |
729 | | **/ |
730 | 0 | signals[SIGNAL_INVALIDATE] = g_signal_new("invalidate", |
731 | 0 | G_TYPE_FROM_CLASS(object_class), |
732 | 0 | G_SIGNAL_RUN_LAST, |
733 | 0 | G_STRUCT_OFFSET(FwupdRequestClass, invalidate), |
734 | 0 | NULL, |
735 | 0 | NULL, |
736 | 0 | g_cclosure_marshal_VOID__VOID, |
737 | 0 | G_TYPE_NONE, |
738 | 0 | 0); |
739 | | |
740 | | /** |
741 | | * FwupdRequest:id: |
742 | | * |
743 | | * The request identifier. |
744 | | * |
745 | | * Since: 1.6.2 |
746 | | */ |
747 | 0 | pspec = |
748 | 0 | g_param_spec_string("id", NULL, NULL, NULL, G_PARAM_READWRITE | G_PARAM_STATIC_NAME); |
749 | 0 | g_object_class_install_property(object_class, PROP_ID, pspec); |
750 | | |
751 | | /** |
752 | | * FwupdRequest:kind: |
753 | | * |
754 | | * The kind of the request. |
755 | | * |
756 | | * Since: 1.6.2 |
757 | | */ |
758 | 0 | pspec = g_param_spec_uint("kind", |
759 | 0 | NULL, |
760 | 0 | NULL, |
761 | 0 | FWUPD_REQUEST_KIND_UNKNOWN, |
762 | 0 | FWUPD_REQUEST_KIND_LAST, |
763 | 0 | FWUPD_REQUEST_KIND_UNKNOWN, |
764 | 0 | G_PARAM_READWRITE | G_PARAM_STATIC_NAME); |
765 | 0 | g_object_class_install_property(object_class, PROP_KIND, pspec); |
766 | | |
767 | | /** |
768 | | * FwupdRequest:flags: |
769 | | * |
770 | | * The flags for the request. |
771 | | * |
772 | | * Since: 1.8.6 |
773 | | */ |
774 | 0 | pspec = g_param_spec_uint64("flags", |
775 | 0 | NULL, |
776 | 0 | NULL, |
777 | 0 | FWUPD_REQUEST_FLAG_NONE, |
778 | 0 | FWUPD_REQUEST_FLAG_UNKNOWN, |
779 | 0 | FWUPD_REQUEST_FLAG_UNKNOWN, |
780 | 0 | G_PARAM_READWRITE | G_PARAM_STATIC_NAME); |
781 | 0 | g_object_class_install_property(object_class, PROP_FLAGS, pspec); |
782 | | |
783 | | /** |
784 | | * FwupdRequest:message: |
785 | | * |
786 | | * The message text in the request. |
787 | | * |
788 | | * Since: 1.6.2 |
789 | | */ |
790 | 0 | pspec = g_param_spec_string("message", |
791 | 0 | NULL, |
792 | 0 | NULL, |
793 | 0 | NULL, |
794 | 0 | G_PARAM_READWRITE | G_PARAM_STATIC_NAME); |
795 | 0 | g_object_class_install_property(object_class, PROP_MESSAGE, pspec); |
796 | | |
797 | | /** |
798 | | * FwupdRequest:image: |
799 | | * |
800 | | * The image link for the request. |
801 | | * |
802 | | * Since: 1.6.2 |
803 | | */ |
804 | 0 | pspec = |
805 | 0 | g_param_spec_string("image", NULL, NULL, NULL, G_PARAM_READWRITE | G_PARAM_STATIC_NAME); |
806 | 0 | g_object_class_install_property(object_class, PROP_IMAGE, pspec); |
807 | | |
808 | | /** |
809 | | * FwupdRequest:device-id: |
810 | | * |
811 | | * The device ID for the request. |
812 | | * |
813 | | * Since: 1.8.2 |
814 | | */ |
815 | 0 | pspec = g_param_spec_string("device-id", |
816 | 0 | NULL, |
817 | 0 | NULL, |
818 | 0 | NULL, |
819 | 0 | G_PARAM_READWRITE | G_PARAM_STATIC_NAME); |
820 | 0 | g_object_class_install_property(object_class, PROP_DEVICE_ID, pspec); |
821 | 0 | } |
822 | | |
823 | | static void |
824 | | fwupd_request_from_variant_iter(FwupdCodec *codec, GVariantIter *iter) |
825 | 0 | { |
826 | 0 | FwupdRequest *self = FWUPD_REQUEST(codec); |
827 | 0 | GVariant *value; |
828 | 0 | const gchar *key; |
829 | 0 | while (g_variant_iter_next(iter, "{&sv}", &key, &value)) { |
830 | 0 | fwupd_request_from_key_value(self, key, value); |
831 | 0 | g_variant_unref(value); |
832 | 0 | } |
833 | 0 | } |
834 | | |
835 | | static void |
836 | | fwupd_request_codec_iface_init(FwupdCodecInterface *iface) |
837 | 0 | { |
838 | 0 | iface->add_string = fwupd_request_add_string; |
839 | 0 | iface->add_variant = fwupd_request_add_variant; |
840 | 0 | iface->from_variant_iter = fwupd_request_from_variant_iter; |
841 | 0 | } |
842 | | |
843 | | /** |
844 | | * fwupd_request_new: |
845 | | * |
846 | | * Creates a new request. |
847 | | * |
848 | | * Returns: a new #FwupdRequest |
849 | | * |
850 | | * Since: 1.6.2 |
851 | | **/ |
852 | | FwupdRequest * |
853 | | fwupd_request_new(void) |
854 | 0 | { |
855 | 0 | FwupdRequest *self; |
856 | 0 | self = g_object_new(FWUPD_TYPE_REQUEST, NULL); |
857 | 0 | return FWUPD_REQUEST(self); |
858 | 0 | } |