static void
show_week (UmHistoryDialog *um)
{
        GArray *login_history;
        GDateTime *datetime, *temp;
        gint64 from, to;
        gint i, line;
        GtkWidget *grid;
        UmLoginHistory history;

        show_week_label (um);
        clear_history (um);
        set_sensitivity (um);

        login_history = get_login_history (um->user);
        if (login_history == NULL) {
                return;
        }

        /* Find first record for week */
        from = g_date_time_to_unix (um->week);
        temp = g_date_time_add_weeks (um->week, 1);
        to = g_date_time_to_unix (temp);
        g_date_time_unref (temp);
        for (i = login_history->len - 1; i >= 0; i--) {
                history = g_array_index (login_history, UmLoginHistory, i);
                if (history.login_time < to) {
                        break;
                }
        }

        /* Add new session records */
        grid = get_widget (um, "history-grid");
        line = 0;
        for (;i >= 0; i--) {
                history = g_array_index (login_history, UmLoginHistory, i);
                if (history.logout_time > 0 && history.logout_time < from) {
                        break;
                }

                /* Display only x-sessions */
                if (g_strrstr (history.type, ":") == NULL) {
                        continue;
                }

                if (history.logout_time > 0 && history.logout_time < to) {
                        datetime = g_date_time_new_from_unix_local (history.logout_time);
                        add_record (grid, datetime, "Session Ended", line);
                        line++;
                }

                if (history.login_time >= from) {
                        datetime = g_date_time_new_from_unix_local (history.login_time);
                        add_record (grid, datetime, "Session Started", line);
                        line++;
                }
        }

        gtk_widget_show_all (grid);

        g_array_free (login_history, TRUE);
}
void TestCheckEntry() {
  GDateTime *now = g_date_time_new_now_utc();
  GDateTime *one_hour_ago = g_date_time_add_hours(now, -1);
  GDateTime *over_1w_ago = g_date_time_add_weeks(one_hour_ago, -1);
  GDateTime *over_2w_ago = g_date_time_add_weeks(one_hour_ago, -2);
  GDateTime *over_3w_ago = g_date_time_add_weeks(one_hour_ago, -3);

  CachePolicy *policy = CachePolicyNew();
  CacheEntry *entry = CacheEntryNew();
  GError *error = NULL;

  // Any entry works with an empty policy.
  entry->tries = 4;
  entry->last_verified = over_3w_ago;
  entry->last_used = over_3w_ago;
  g_assert(CachePolicyCheckEntry(policy, entry, now, &error));
  g_assert_no_error(error);

  // Try against a policy with all fields set from now on.
  CachePolicySetValue(policy, "tries", "3", NULL);
  CachePolicySetValue(policy, "refresh", "1w", NULL);
  CachePolicySetValue(policy, "renew", "2w", NULL);
  CachePolicySetValue(policy, "expire", "3w", NULL);

  entry->tries = 3;
  entry->last_verified = over_2w_ago;
  entry->last_used = one_hour_ago;
  g_assert(CachePolicyCheckEntry(policy, entry, now, &error));
  g_assert_no_error(error);

  entry->last_verified = over_2w_ago;
  entry->last_used = over_1w_ago;
  g_assert(!CachePolicyCheckEntry(policy, entry, now, &error));
  g_assert_error(error, CACHE_POLICY_ERROR, CACHE_POLICY_ENTRY_EXPIRED_ERROR);
  g_error_free(error);
  error = NULL;

  entry->last_verified = over_3w_ago;
  entry->last_used = one_hour_ago;
  g_assert(!CachePolicyCheckEntry(policy, entry, now, &error));
  g_assert_error(error, CACHE_POLICY_ERROR, CACHE_POLICY_ENTRY_EXPIRED_ERROR);
  g_error_free(error);
  error = NULL;

  entry->tries = 4;
  entry->last_verified = one_hour_ago;
  entry->last_used = one_hour_ago;
  g_assert(!CachePolicyCheckEntry(policy, entry, now, &error));
  g_assert_error(error, CACHE_POLICY_ERROR,
                 CACHE_POLICY_MAX_TRIES_EXCEEDED_ERROR);
  g_error_free(error);
  error = NULL;

  entry->last_verified = NULL;
  entry->last_used = NULL;
  CacheEntryUnref(entry);
  CachePolicyUnref(policy);
  g_date_time_unref(now);
  g_date_time_unref(one_hour_ago);
  g_date_time_unref(over_1w_ago);
  g_date_time_unref(over_2w_ago);
  g_date_time_unref(over_3w_ago);
}