/** * hif_state_set_percentage: * @state: a #HifState instance. * @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. * * Returns: %TRUE if the signal was propagated, %FALSE otherwise * * Since: 0.1.0 **/ gboolean hif_state_set_percentage(HifState *state, guint percentage) { HifStatePrivate *priv = GET_PRIVATE(state); /* do we care */ if (!priv->report_progress) return TRUE; /* is it the same */ if (percentage == priv->last_percentage) return FALSE; /* is it invalid */ if (percentage > 100) { hif_state_print_parent_chain(state, 0); g_warning("percentage %i%% is invalid on %p!", percentage, state); return FALSE; } /* is it less */ if (percentage < priv->last_percentage) { if (priv->enable_profile) { hif_state_print_parent_chain(state, 0); g_warning("percentage should not go down from %i to %i on %p!", priv->last_percentage, percentage, state); } return FALSE; } /* we're done, so we're not preventing cancellation anymore */ if (percentage == 100 && !priv->allow_cancel) { g_debug("done, so allow cancel 1 for %p", state); hif_state_set_allow_cancel(state, TRUE); } /* automatically cancel any action */ if (percentage == 100 && priv->action != HIF_STATE_ACTION_UNKNOWN) hif_state_action_stop(state); /* speed no longer valid */ if (percentage == 100) hif_state_set_speed_internal(state, 0); /* release locks? */ if (percentage == 100) { if (!hif_state_release_locks(state)) return FALSE; } /* save */ priv->last_percentage = percentage; /* emit */ g_signal_emit(state, signals [SIGNAL_PERCENTAGE_CHANGED], 0, percentage); /* success */ return TRUE; }
/** * hif_state_set_speed: **/ void hif_state_set_speed (HifState *state, guint64 speed) { guint i; guint64 sum = 0; guint sum_cnt = 0; g_return_if_fail (HIF_IS_STATE (state)); /* move the data down one entry */ for (i=HIF_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 < HIF_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; hif_state_set_speed_internal (state, sum); }
/** * hif_state_set_speed: * @state: a #HifState instance. * @speed: The transaction speed. * * Sets the download or install transaction speed in bytes per second. * * Since: 0.1.0 **/ void hif_state_set_speed(HifState *state, guint64 speed) { HifStatePrivate *priv = GET_PRIVATE(state); guint i; guint64 sum = 0; guint sum_cnt = 0; /* move the data down one entry */ for (i=HIF_STATE_SPEED_SMOOTHING_ITEMS-1; i > 0; i--) priv->speed_data[i] = priv->speed_data[i-1]; priv->speed_data[0] = speed; /* get the average */ for (i = 0; i < HIF_STATE_SPEED_SMOOTHING_ITEMS; i++) { if (priv->speed_data[i] > 0) { sum += priv->speed_data[i]; sum_cnt++; } } if (sum_cnt > 0) sum /= sum_cnt; hif_state_set_speed_internal(state, sum); }
/** * hif_state_child_notify_speed_cb: **/ static void hif_state_child_notify_speed_cb (HifState *child, GParamSpec *pspec, HifState *state) { hif_state_set_speed_internal (state, hif_state_get_speed (child)); }
/** * hif_state_set_percentage: **/ gboolean hif_state_set_percentage (HifState *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) { hif_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) { hif_state_print_parent_chain (state, 0); g_warning ("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); hif_state_set_allow_cancel (state, TRUE); } /* automatically cancel any action */ if (percentage == 100 && state->priv->action != PK_STATUS_ENUM_UNKNOWN) { g_debug ("done, so cancelling action %s", pk_status_enum_to_string (state->priv->action)); hif_state_action_stop (state); } /* speed no longer valid */ if (percentage == 100) hif_state_set_speed_internal (state, 0); /* release locks? */ if (percentage == 100) { ret = hif_state_release_locks (state); if (!ret) goto out; } /* save */ state->priv->last_percentage = percentage; /* emit */ g_signal_emit (state, signals [SIGNAL_PERCENTAGE_CHANGED], 0, percentage); /* success */ ret = TRUE; out: return ret; }