示例#1
0
/**
 * 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;
}
示例#2
0
/**
 * hif_state_print_parent_chain:
 **/
static void
hif_state_print_parent_chain (HifState *state, guint level)
{
	if (state->priv->parent != NULL)
		hif_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);
}
示例#3
0
/**
 * hif_state_print_parent_chain:
 **/
static void
hif_state_print_parent_chain(HifState *state, guint level)
{
    HifStatePrivate *priv = GET_PRIVATE(state);
    if (priv->parent != NULL)
        hif_state_print_parent_chain(priv->parent, level + 1);
    g_print("%i) %s(%i/%i)\n",
            level, priv->id, priv->current, priv->steps);
}
示例#4
0
/**
 * hif_state_set_number_steps_real:
 **/
gboolean
hif_state_set_number_steps_real (HifState *state, guint steps, const gchar *strloc)
{
	gboolean ret = FALSE;

	g_return_val_if_fail (state != NULL, FALSE);

	/* nothing to do for 0 steps */
	if (steps == 0) {
		ret = TRUE;
		goto out;
	}

	/* do we care */
	if (!state->priv->report_progress) {
		ret = TRUE;
		goto out;
	}

	/* did we call done on a state that did not have a size set? */
	if (state->priv->steps != 0) {
		g_warning ("steps already set to %i, can't set %i! [%s]",
			     state->priv->steps, steps, strloc);
		hif_state_print_parent_chain (state, 0);
		goto out;
	}

	/* set id */
	g_free (state->priv->id);
	state->priv->id = g_strdup_printf ("%s", strloc);

	/* only use the timer if profiling; it's expensive */
	if (state->priv->enable_profile)
		g_timer_start (state->priv->timer);

	/* set steps */
	state->priv->steps = steps;

	/* success */
	ret = TRUE;
out:
	return ret;
}
示例#5
0
/**
 * hif_state_done_real:
 **/
gboolean
hif_state_done_real (HifState *state, GError **error, const gchar *strloc)
{
	gboolean ret;
	gdouble elapsed;
	gfloat percentage;

	g_return_val_if_fail (state != NULL, FALSE);
	g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

	/* check */
	ret = hif_state_check (state, error);
	if (!ret)
		goto out;

	/* do we care */
	if (!state->priv->report_progress)
		goto out;

	/* did we call done on a state that did not have a size set? */
	if (state->priv->steps == 0) {
		g_set_error (error, HIF_ERROR, PK_ERROR_ENUM_INTERNAL_ERROR,
			     "done on a state %p that did not have a size set! [%s]",
			     state, strloc);
		hif_state_print_parent_chain (state, 0);
		ret = FALSE;
		goto out;
	}

	/* check the interval was too big in allow_cancel false mode */
	if (state->priv->enable_profile) {
		elapsed = g_timer_elapsed (state->priv->timer, NULL);
		if (!state->priv->allow_cancel_changed_state && state->priv->current > 0) {
			if (elapsed > 0.1f) {
				g_warning ("%.1fms between hif_state_done() and no hif_state_set_allow_cancel()", elapsed * 1000);
				hif_state_print_parent_chain (state, 0);
			}
		}

		/* save the duration in the array */
		if (state->priv->step_profile != NULL)
			state->priv->step_profile[state->priv->current] = elapsed;
		g_timer_start (state->priv->timer);
	}

	/* is already at 100%? */
	if (state->priv->current >= state->priv->steps) {
		g_set_error (error, HIF_ERROR, PK_ERROR_ENUM_INTERNAL_ERROR,
			     "already at 100%% state [%s]", strloc);
		hif_state_print_parent_chain (state, 0);
		ret = FALSE;
		goto out;
	}

	/* is child not at 100%? */
	if (state->priv->child != NULL) {
		HifStatePrivate *child_priv = state->priv->child->priv;
		if (child_priv->current != child_priv->steps) {
			g_print ("child is at %i/%i steps and parent done [%s]\n",
				 child_priv->current, child_priv->steps, strloc);
			hif_state_print_parent_chain (state->priv->child, 0);
			ret = TRUE;
			/* do not abort, as we want to clean this up */
		}
	}

	/* we just checked for cancel, so it's not true to say we're blocking */
	hif_state_set_allow_cancel (state, TRUE);

	/* another */
	state->priv->current++;

	/* find new percentage */
	if (state->priv->step_data == NULL) {
		percentage = hif_state_discrete_to_percent (state->priv->current,
							    state->priv->steps);
	} else {
		/* this is cumalative, for speedy access */
		percentage = state->priv->step_data[state->priv->current - 1];
	}
	hif_state_set_percentage (state, (guint) percentage);

	/* show any profiling stats */
	if (state->priv->enable_profile &&
	    state->priv->current == state->priv->steps &&
	    state->priv->step_profile != NULL) {
		hif_state_show_profile (state);
	}

	/* reset child if it exists */
	if (state->priv->child != NULL)
		hif_state_reset (state->priv->child);
out:
	return ret;
}
示例#6
0
/**
 * 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;
}