This repository has been archived by the owner on Aug 22, 2019. It is now read-only.
/
zif-state.c
1827 lines (1635 loc) · 46.4 KB
/
zif-state.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
*
* Copyright (C) 2009-2010 Richard Hughes <richard@hughsie.com>
*
* Licensed under the GNU General Public License Version 2
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* SECTION:zif-state
* @short_description: Progress reporting
*
* Objects can use zif_state_set_percentage() if the absolute percentage
* is known. Percentages should always go up, not down.
*
* Modules usually set the number of steps that are expected using
* zif_state_set_number_steps() and then after each section is completed,
* the zif_state_done() function should be called. This will automatically
* call zif_state_set_percentage() with the correct values.
*
* #ZifState allows sub-modules to be "chained up" to the parent module
* so that as the sub-module progresses, so does the parent.
* The child can be reused for each section, and chains can be deep.
*
* To get a child object, you should use zif_state_get_child() and then
* use the result in any sub-process. You should ensure that the child
* is not re-used without calling zif_state_done().
*
* There are a few nice touches in this module, so that if a module only has
* one progress step, the child progress is used for updates.
*
* <example>
* <title>Using a #ZifState.</title>
* <programlisting>
* static void
* _do_something (ZifState *state)
* {
* ZifState *state_local;
*
* // setup correct number of steps
* zif_state_set_number_steps (state, 2);
*
* // we can't cancel this function
* zif_state_set_allow_cancel (state, FALSE);
*
* // run a sub function
* state_local = zif_state_get_child (state);
* _do_something_else1 (state_local);
*
* // this section done
* zif_state_done (state);
*
* // run another sub function
* state_local = zif_state_get_child (state);
* _do_something_else2 (state_local);
*
* // this section done (all complete)
* zif_state_done (state);
* }
* </programlisting>
* </example>
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <glib.h>
#include <glib-unix.h>
#include <signal.h>
#include <rpm/rpmsq.h>
#include "zif-utils.h"
#include "zif-state-private.h"
#define ZIF_STATE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), ZIF_TYPE_STATE, ZifStatePrivate))
struct _ZifStatePrivate
{
gboolean allow_cancel;
gboolean allow_cancel_changed_state;
gboolean allow_cancel_child;
gboolean enable_profile;
gboolean report_progress;
gboolean process_event_sources;
GCancellable *cancellable;
gchar *action_hint;
gchar *id;
gdouble global_share;
gdouble *step_profile;
gpointer error_handler_user_data;
gpointer lock_handler_user_data;
GTimer *timer;
guint64 speed;
guint64 *speed_data;
guint current;
guint last_percentage;
guint *step_data;
guint steps;
gulong action_child_id;
gulong package_progress_child_id;
gulong notify_speed_child_id;
gulong allow_cancel_child_id;
gulong percentage_child_id;
gulong subpercentage_child_id;
ZifStateAction action;
ZifStateAction last_action;
ZifStateAction child_action;
ZifState *child;
ZifStateErrorHandlerCb error_handler_cb;
ZifStateLockHandlerCb lock_handler_cb;
ZifState *parent;
GPtrArray *lock_ids;
ZifLock *lock;
};
enum {
SIGNAL_PERCENTAGE_CHANGED,
SIGNAL_SUBPERCENTAGE_CHANGED,
SIGNAL_ALLOW_CANCEL_CHANGED,
SIGNAL_ACTION_CHANGED,
SIGNAL_PACKAGE_PROGRESS_CHANGED,
SIGNAL_LAST
};
enum {
PROP_0,
PROP_SPEED,
PROP_LAST
};
static guint signals [SIGNAL_LAST] = { 0 };
G_DEFINE_TYPE (ZifState, zif_state, G_TYPE_OBJECT)
#define ZIF_STATE_SPEED_SMOOTHING_ITEMS 5
/**
* zif_state_error_quark:
*
* Return value: An error quark.
*
* Since: 0.1.0
**/
GQuark
zif_state_error_quark (void)
{
static GQuark quark = 0;
if (!quark)
quark = g_quark_from_static_string ("zif_state_error");
return quark;
}
/**
* zif_state_set_report_progress:
* @state: A #ZifState
* @report_progress: if we care about percentage status
*
* This disables progress tracking for ZifState. This is generally a bad
* thing to do, except when you know you cannot guess the number of steps
* in the state.
*
* Using this function also reduced the amount of time spent getting a
* child state using zif_state_get_child() as a refcounted version of
* the parent is returned instead.
*
* Since: 0.1.3
**/
void
zif_state_set_report_progress (ZifState *state, gboolean report_progress)
{
g_return_if_fail (ZIF_IS_STATE (state));
state->priv->report_progress = report_progress;
}
/**
* zif_state_set_enable_profile:
* @state: A #ZifState
* @enable_profile: if profiling should be enabled
*
* This enables profiling of ZifState. This may be useful in development,
* but be warned; enabling profiling makes #ZifState very slow.
*
* Since: 0.1.3
**/
void
zif_state_set_enable_profile (ZifState *state, gboolean enable_profile)
{
g_return_if_fail (ZIF_IS_STATE (state));
state->priv->enable_profile = enable_profile;
}
/**
* zif_state_set_error_handler:
* @state: A #ZifState
* @error_handler_cb: (scope async): A #ZifStateErrorHandlerCb which returns %FALSE if the error is fatal
* @user_data: A user_data to be passed to the #ZifStateErrorHandlerCb
*
* Since: 0.1.0
**/
void
zif_state_set_error_handler (ZifState *state,
ZifStateErrorHandlerCb error_handler_cb,
gpointer user_data)
{
state->priv->error_handler_cb = error_handler_cb;
state->priv->error_handler_user_data = user_data;
/* if there is an existing child, set the handler on this too */
if (state->priv->child != NULL) {
zif_state_set_error_handler (state->priv->child,
error_handler_cb,
user_data);
}
}
/**
* zif_state_error_handler:
* @state: A #ZifState
* @error: A #GError
*
* Return value: %FALSE if the error is fatal, %TRUE otherwise
*
* Since: 0.1.0
**/
gboolean
zif_state_error_handler (ZifState *state, const GError *error)
{
gboolean ret = FALSE;
/* no handler */
if (state->priv->error_handler_cb == NULL) {
g_debug ("no error handler installed");
goto out;
}
/* run the handler */
ret = state->priv->error_handler_cb (error, state->priv->error_handler_user_data);
g_debug ("error handler reported %s", ret ? "IGNORE" : "FAILURE");
out:
return ret;
}
/**
* zif_state_set_lock_handler:
* @state: A #ZifState
* @lock_handler_cb: (scope async): A #ZifStateLockHandlerCb which returns %FALSE if the lock cannot be got
* @user_data: A user_data to be passed to the #ZifStateLockHandlerCb
*
* Since: 0.1.6
**/
void
zif_state_set_lock_handler (ZifState *state,
ZifStateLockHandlerCb lock_handler_cb,
gpointer user_data)
{
state->priv->lock_handler_cb = lock_handler_cb;
state->priv->lock_handler_user_data = user_data;
/* if there is an existing child, set the handler on this too */
if (state->priv->child != NULL) {
zif_state_set_lock_handler (state->priv->child,
lock_handler_cb,
user_data);
}
}
/**
* zif_state_take_lock:
* @state: A #ZifState
* @lock_type: A #ZifLockType, e.g. %ZIF_LOCK_TYPE_RPMDB
* @lock_mode: A #ZifLockMode, e.g. %ZIF_LOCK_MODE_PROCESS
* @error: A #GError
*
* Takes a lock of a specified type.
* The lock is automatically free'd when the ZifState has been completed.
*
* You can call zif_state_take_lock() multiple times with different or
* even the same @lock_type value.
*
* Return value: %FALSE if the lock is fatal, %TRUE otherwise
*
* Since: 0.3.0
**/
gboolean
zif_state_take_lock (ZifState *state,
ZifLockType lock_type,
ZifLockMode lock_mode,
GError **error)
{
gboolean ret = TRUE;
guint lock_id = 0;
/* no custom handler */
if (state->priv->lock_handler_cb == NULL) {
lock_id = zif_lock_take (state->priv->lock,
lock_type,
lock_mode,
error);
if (lock_id == 0)
ret = FALSE;
} else {
lock_id = G_MAXUINT;
ret = state->priv->lock_handler_cb (state,
state->priv->lock,
lock_type,
error,
state->priv->lock_handler_user_data);
}
if (!ret)
goto out;
/* add the lock to an array so we can release on completion */
g_debug ("adding lock %i", lock_id);
g_ptr_array_add (state->priv->lock_ids,
GUINT_TO_POINTER (lock_id));
out:
return ret;
}
/**
* zif_state_discrete_to_percent:
**/
static gfloat
zif_state_discrete_to_percent (guint discrete, guint steps)
{
/* check we are in range */
if (discrete > steps)
return 100;
if (steps == 0) {
g_warning ("steps is 0!");
return 0;
}
return ((gfloat) discrete * (100.0f / (gfloat) (steps)));
}
/**
* zif_state_print_parent_chain:
**/
static void
zif_state_print_parent_chain (ZifState *state, guint level)
{
if (state->priv->parent != NULL)
zif_state_print_parent_chain (state->priv->parent, level + 1);
g_print ("%i) %s (%i/%i)\n",
level, state->priv->id, state->priv->current, state->priv->steps);
}
/**
* zif_state_get_cancellable:
* @state: A #ZifState
*
* Gets the #GCancellable for this operation
*
* Return value: (transfer none): The #GCancellable or %NULL
*
* Since: 0.1.0
**/
GCancellable *
zif_state_get_cancellable (ZifState *state)
{
g_return_val_if_fail (ZIF_IS_STATE (state), NULL);
return state->priv->cancellable;
}
/**
* zif_state_set_cancellable:
* @state: A #ZifState
* @cancellable: The #GCancellable which is used to cancel tasks, or %NULL
*
* Sets the #GCancellable object to use.
*
* Since: 0.1.0
**/
void
zif_state_set_cancellable (ZifState *state, GCancellable *cancellable)
{
g_return_if_fail (ZIF_IS_STATE (state));
g_return_if_fail (state->priv->cancellable == NULL);
state->priv->cancellable = g_object_ref (cancellable);
}
/**
* zif_state_get_allow_cancel:
* @state: A #ZifState
*
* Gets if the sub-task (or one of it's sub-sub-tasks) is cancellable
*
* Return value: %TRUE if the translation has a chance of being cancelled
*
* Since: 0.1.0
**/
gboolean
zif_state_get_allow_cancel (ZifState *state)
{
g_return_val_if_fail (ZIF_IS_STATE (state), FALSE);
return state->priv->allow_cancel && state->priv->allow_cancel_child;
}
/**
* zif_state_set_allow_cancel:
* @state: A #ZifState
* @allow_cancel: If this sub-task can be cancelled
*
* Set is this sub task can be cancelled safely.
*
* Since: 0.1.0
**/
void
zif_state_set_allow_cancel (ZifState *state, gboolean allow_cancel)
{
g_return_if_fail (ZIF_IS_STATE (state));
state->priv->allow_cancel_changed_state = TRUE;
/* quick optimisation that saves lots of signals */
if (state->priv->allow_cancel == allow_cancel)
return;
state->priv->allow_cancel = allow_cancel;
/* just emit if both this and child is okay */
g_signal_emit (state, signals [SIGNAL_ALLOW_CANCEL_CHANGED], 0,
state->priv->allow_cancel && state->priv->allow_cancel_child);
}
/**
* zif_state_get_speed:
* @state: A #ZifState
*
* Gets the transaction speed in bytes per second.
*
* Return value: speed, or 0 for unknown.
*
* Since: 0.1.5
**/
guint64
zif_state_get_speed (ZifState *state)
{
g_return_val_if_fail (ZIF_IS_STATE (state), 0);
return state->priv->speed;
}
/**
* zif_state_set_speed_internal:
**/
static void
zif_state_set_speed_internal (ZifState *state, guint64 speed)
{
if (state->priv->speed == speed)
return;
state->priv->speed = speed;
g_object_notify (G_OBJECT (state), "speed");
}
/**
* zif_state_set_speed:
* @state: A #ZifState
* @speed: The transaction speed.
*
* Sets the download or install transaction speed in bytes per second.
*
* Since: 0.1.5
**/
void
zif_state_set_speed (ZifState *state, guint64 speed)
{
guint i;
guint64 sum = 0;
guint sum_cnt = 0;
g_return_if_fail (ZIF_IS_STATE (state));
/* move the data down one entry */
for (i=ZIF_STATE_SPEED_SMOOTHING_ITEMS-1; i > 0; i--)
state->priv->speed_data[i] = state->priv->speed_data[i-1];
state->priv->speed_data[0] = speed;
/* get the average */
for (i = 0; i < ZIF_STATE_SPEED_SMOOTHING_ITEMS; i++) {
if (state->priv->speed_data[i] > 0) {
sum += state->priv->speed_data[i];
sum_cnt++;
}
}
if (sum_cnt > 0)
sum /= sum_cnt;
g_debug ("speed = %" G_GUINT64_FORMAT " kb/sec", sum / 1024);
zif_state_set_speed_internal (state, sum);
}
/**
* zif_state_release_locks:
**/
static gboolean
zif_state_release_locks (ZifState *state)
{
gboolean ret = TRUE;
guint i;
guint lock_id;
/* release each one */
for (i = 0; i < state->priv->lock_ids->len; i++) {
lock_id = GPOINTER_TO_UINT (g_ptr_array_index (state->priv->lock_ids, i));
g_debug ("releasing lock %i", lock_id);
ret = zif_lock_release (state->priv->lock,
lock_id,
NULL);
if (!ret)
goto out;
}
g_ptr_array_set_size (state->priv->lock_ids, 0);
out:
return ret;
}
/**
* zif_state_set_percentage:
* @state: A #ZifState
* @percentage: Percentage value between 0% and 100%
*
* Set a percentage manually.
* NOTE: this must be above what was previously set, or it will be rejected.
*
* Return value: %TRUE if the signal was propagated, %FALSE otherwise
*
* Since: 0.1.0
**/
gboolean
zif_state_set_percentage (ZifState *state, guint percentage)
{
gboolean ret = FALSE;
/* do we care */
if (!state->priv->report_progress) {
ret = TRUE;
goto out;
}
/* is it the same */
if (percentage == state->priv->last_percentage)
goto out;
/* is it invalid */
if (percentage > 100) {
zif_state_print_parent_chain (state, 0);
g_warning ("percentage %i%% is invalid on %p!",
percentage, state);
goto out;
}
/* is it less */
if (percentage < state->priv->last_percentage) {
if (state->priv->enable_profile) {
zif_state_print_parent_chain (state, 0);
g_critical ("percentage should not go down from %i to %i on %p!",
state->priv->last_percentage, percentage, state);
}
goto out;
}
/* we're done, so we're not preventing cancellation anymore */
if (percentage == 100 && !state->priv->allow_cancel) {
g_debug ("done, so allow cancel 1 for %p", state);
zif_state_set_allow_cancel (state, TRUE);
}
/* automatically cancel any action */
if (percentage == 100 && state->priv->action != ZIF_STATE_ACTION_UNKNOWN) {
g_debug ("done, so cancelling action %s", zif_state_action_to_string (state->priv->action));
zif_state_action_stop (state);
}
/* speed no longer valid */
if (percentage == 100)
zif_state_set_speed_internal (state, 0);
/* release locks? */
if (percentage == 100) {
ret = zif_state_release_locks (state);
if (!ret)
goto out;
}
/* save */
state->priv->last_percentage = percentage;
/* are we so low we don't care */
if (state->priv->global_share < 0.001)
goto out;
/* emit */
g_signal_emit (state, signals [SIGNAL_PERCENTAGE_CHANGED], 0, percentage);
/* success */
ret = TRUE;
out:
return ret;
}
/**
* zif_state_valid:
* @state: A #ZifState
*
* Returns if the %ZifState is a valid object and ready for use.
* This is very useful in self-testing situations like:
* {{{
* g_return_val_if_fail (zif_state_valid (md), FALSE);
* }}}
*
* Return value: %TRUE if the %ZifState is okay to use, and has not been
* already used.
*
* Since: 0.1.2
**/
gboolean
zif_state_valid (ZifState *state)
{
if (state == NULL)
return FALSE;
if (state->priv->current != 0) {
zif_state_print_parent_chain (state, 0);
g_warning ("current not zero");
return FALSE;
}
return TRUE;
}
/**
* zif_state_get_percentage:
* @state: A #ZifState
*
* Get the percentage state.
*
* Return value: The percentage value, or %G_MAXUINT for error
*
* Since: 0.1.0
**/
guint
zif_state_get_percentage (ZifState *state)
{
return state->priv->last_percentage;
}
/**
* zif_state_set_subpercentage:
**/
static gboolean
zif_state_set_subpercentage (ZifState *state, guint percentage)
{
/* are we so low we don't care */
if (state->priv->global_share < 0.01)
goto out;
/* just emit */
g_signal_emit (state, signals [SIGNAL_SUBPERCENTAGE_CHANGED], 0, percentage);
out:
return TRUE;
}
/**
* zif_state_action_start:
* @state: A #ZifState
* @action: An action, e.g. %ZIF_STATE_ACTION_DECOMPRESSING
* @action_hint: A hint on what the action is doing, e.g. "/var/cache/yum/i386/15/koji/primary.sqlite"
*
* Sets the action which is being performed. This is emitted up the chain
* to any parent %ZifState objects, using the action-changed signal.
*
* If a %ZifState reaches 100% then it is automatically stopped with a
* call to zif_state_action_stop().
*
* It is allowed to call zif_state_action_start() more than once for a
* given %ZifState instance.
*
* Return value: %TRUE if the signal was propagated, %FALSE otherwise
*
* Since: 0.1.2
**/
gboolean
zif_state_action_start (ZifState *state, ZifStateAction action, const gchar *action_hint)
{
g_return_val_if_fail (ZIF_IS_STATE (state), FALSE);
/* ignore this */
if (action == ZIF_STATE_ACTION_UNKNOWN) {
g_warning ("cannot set action ZIF_STATE_ACTION_UNKNOWN");
return FALSE;
}
/* is different? */
if (state->priv->action == action &&
g_strcmp0 (action_hint, state->priv->action_hint) == 0) {
g_debug ("same action as before, ignoring");
return FALSE;
}
/* remember for stop */
state->priv->last_action = state->priv->action;
/* save hint */
g_free (state->priv->action_hint);
state->priv->action_hint = g_strdup (action_hint);
/* save */
state->priv->action = action;
/* just emit */
g_signal_emit (state, signals [SIGNAL_ACTION_CHANGED], 0, action, action_hint);
return TRUE;
}
/**
* zif_state_set_package_progress:
* @state: A #ZifState
* @package_id: A package_id
* @action: A #ZifStateAction
* @percentage: A percentage
*
* Sets any package progress.
*
* Since: 0.3.1
**/
void
zif_state_set_package_progress (ZifState *state,
const gchar *package_id,
ZifStateAction action,
guint percentage)
{
g_return_if_fail (ZIF_IS_STATE (state));
g_return_if_fail (package_id != NULL);
g_return_if_fail (action != ZIF_STATE_ACTION_UNKNOWN);
g_return_if_fail (percentage <= 100);
/* just emit */
g_signal_emit (state, signals [SIGNAL_PACKAGE_PROGRESS_CHANGED], 0,
package_id, action, percentage);
}
/**
* zif_state_action_stop:
* @state: A #ZifState
*
* Returns the ZifState to it's previous value.
* It is not expected you will ever need to use this funtion.
*
* Return value: %TRUE if the signal was propagated, %FALSE otherwise
*
* Since: 0.1.2
**/
gboolean
zif_state_action_stop (ZifState *state)
{
g_return_val_if_fail (ZIF_IS_STATE (state), FALSE);
/* nothing ever set */
if (state->priv->action == ZIF_STATE_ACTION_UNKNOWN) {
g_debug ("cannot unset action ZIF_STATE_ACTION_UNKNOWN");
return FALSE;
}
/* pop and reset */
state->priv->action = state->priv->last_action;
state->priv->last_action = ZIF_STATE_ACTION_UNKNOWN;
if (state->priv->action_hint != NULL) {
g_free (state->priv->action_hint);
state->priv->action_hint = NULL;
}
/* just emit */
g_signal_emit (state, signals [SIGNAL_ACTION_CHANGED], 0, state->priv->action, NULL);
return TRUE;
}
/**
* zif_state_get_action_hint:
* @state: A #ZifState
*
* Gets the action hint, which may be useful to the users.
*
* Return value: An a ction hint, e.g. "/var/cache/yum/i386/15/koji/primary.sqlite"
*
* Since: 0.1.2
**/
const gchar *
zif_state_get_action_hint (ZifState *state)
{
g_return_val_if_fail (ZIF_IS_STATE (state), NULL);
return state->priv->action_hint;
}
/**
* zif_state_get_action:
* @state: A #ZifState
*
* Gets the last set action value.
*
* Return value: An action, e.g. %ZIF_STATE_ACTION_DECOMPRESSING
*
* Since: 0.1.2
**/
ZifStateAction
zif_state_get_action (ZifState *state)
{
g_return_val_if_fail (ZIF_IS_STATE (state), ZIF_STATE_ACTION_UNKNOWN);
return state->priv->action;
}
/**
* zif_state_action_to_string:
* @action: A %ZifStateAction value
*
* Converts the %ZifStateAction to a string.
*
* Return value: A string, or %NULL for unknown.
*
* Since: 0.1.2
**/
const gchar *
zif_state_action_to_string (ZifStateAction action)
{
if (action == ZIF_STATE_ACTION_CHECKING)
return "checking";
if (action == ZIF_STATE_ACTION_DOWNLOADING)
return "downloading";
if(action == ZIF_STATE_ACTION_LOADING_REPOS)
return "loading-repos";
if(action == ZIF_STATE_ACTION_DECOMPRESSING)
return "decompressing";
if (action == ZIF_STATE_ACTION_DEPSOLVING_INSTALL)
return "depsolving-install";
if (action == ZIF_STATE_ACTION_DEPSOLVING_REMOVE)
return "depsolving-remove";
if (action == ZIF_STATE_ACTION_DEPSOLVING_UPDATE)
return "depsolving-update";
if (action == ZIF_STATE_ACTION_DEPSOLVING_CONFLICTS)
return "depsolving-conflicts";
if (action == ZIF_STATE_ACTION_INSTALLING)
return "installing";
if (action == ZIF_STATE_ACTION_REMOVING)
return "removing";
if (action == ZIF_STATE_ACTION_PREPARING)
return "preparing";
if (action == ZIF_STATE_ACTION_UPDATING)
return "updating";
if (action == ZIF_STATE_ACTION_CLEANING)
return "cleaning";
if (action == ZIF_STATE_ACTION_TEST_COMMIT)
return "test-commit";
if (action == ZIF_STATE_ACTION_LOADING_RPMDB)
return "loading-rpmdb";
if (action == ZIF_STATE_ACTION_CHECKING_UPDATES)
return "checking-updates";
if (action == ZIF_STATE_ACTION_UNKNOWN)
return "unknown";
return NULL;
}
/**
* zif_state_child_percentage_changed_cb:
**/
static void
zif_state_child_percentage_changed_cb (ZifState *child, guint percentage, ZifState *state)
{
gfloat offset;
gfloat range;
gfloat extra;
guint parent_percentage;
/* propagate up the stack if ZifState has only one step */
if (state->priv->steps == 1) {
zif_state_set_percentage (state, percentage);
return;
}
/* did we call done on a state that did not have a size set? */
if (state->priv->steps == 0)
return;
/* always provide two levels of signals */
zif_state_set_subpercentage (state, percentage);
/* already at >= 100% */
if (state->priv->current >= state->priv->steps) {
g_warning ("already at %i/%i steps on %p", state->priv->current, state->priv->steps, state);
return;
}
/* we have to deal with non-linear steps */
if (state->priv->step_data != NULL) {
/* we don't store zero */
if (state->priv->current == 0) {
parent_percentage = percentage * state->priv->step_data[state->priv->current] / 100;
} else {
/* bilinearly interpolate for speed */
parent_percentage = (((100 - percentage) * state->priv->step_data[state->priv->current-1]) +
(percentage * state->priv->step_data[state->priv->current])) / 100;
}
goto out;
}
/* get the offset */
offset = zif_state_discrete_to_percent (state->priv->current, state->priv->steps);
/* get the range between the parent step and the next parent step */
range = zif_state_discrete_to_percent (state->priv->current+1, state->priv->steps) - offset;
if (range < 0.01) {
g_warning ("range=%f (from %i to %i), should be impossible", range, state->priv->current+1, state->priv->steps);
return;
}
/* restore the pre-child action */
if (percentage == 100) {
state->priv->last_action = state->priv->child_action;
g_debug ("restoring last action %s",
zif_state_action_to_string (state->priv->child_action));
}
/* get the extra contributed by the child */
extra = ((gfloat) percentage / 100.0f) * range;
/* emit from the parent */
parent_percentage = (guint) (offset + extra);
out:
zif_state_set_percentage (state, parent_percentage);
}
/**
* zif_state_child_subpercentage_changed_cb:
**/
static void
zif_state_child_subpercentage_changed_cb (ZifState *child, guint percentage, ZifState *state)
{
/* discard this, unless the ZifState has only one step */
if (state->priv->steps != 1)
return;
/* propagate up the stack as if the parent didn't exist */
zif_state_set_subpercentage (state, percentage);
}
/**
* zif_state_child_allow_cancel_changed_cb:
**/
static void
zif_state_child_allow_cancel_changed_cb (ZifState *child, gboolean allow_cancel, ZifState *state)
{
/* save */
state->priv->allow_cancel_child = allow_cancel;
/* just emit if both this and child is okay */
g_signal_emit (state, signals [SIGNAL_ALLOW_CANCEL_CHANGED], 0,
state->priv->allow_cancel && state->priv->allow_cancel_child);
}
/**
* zif_state_child_action_changed_cb:
**/
static void
zif_state_child_action_changed_cb (ZifState *child, ZifStateAction action, const gchar *action_hint, ZifState *state)
{
/* save */
state->priv->action = action;
/* just emit */
g_signal_emit (state, signals [SIGNAL_ACTION_CHANGED], 0, action, action_hint);
}
/**
* zif_state_child_package_progress_changed_cb:
**/
static void
zif_state_child_package_progress_changed_cb (ZifState *child,
const gchar *package_id,
ZifStateAction action,
guint progress,
ZifState *state)
{
/* just emit */
g_signal_emit (state, signals [SIGNAL_PACKAGE_PROGRESS_CHANGED], 0,
package_id, action, progress);
}
/**
* zif_state_reset:
* @state: A #ZifState
*
* Resets the #ZifState object to unset