aboutsummaryrefslogtreecommitdiff
path: root/src/anastasis/anastasis-gtk_handle-expiration-change.c
blob: 7c698b72a4a3b19c86dd4f2fd2b237b6f08509ce (plain)
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
/*
     This file is part of anastasis-gtk.
     Copyright (C) 2021 Anastasis SARL

     Anastasis 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 3, or (at your
     option) any later version.

     Anastasis 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 Anastasis; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/
/**
 * @file src/anastasis/anastasis-gtk_handle-expiration-change.c
 * @brief
 * @author Christian Grothoff
 */
#include <gnunet/platform.h>
#include <gnunet/gnunet_util_lib.h>
#include "anastasis-gtk_helper.h"
#include "anastasis-gtk_action.h"
#include <jansson.h>


struct GNUNET_TIME_Absolute
AG_get_desired_expiration ()
{
  GtkSpinButton *spin_button;
  gint value;
  struct GNUNET_TIME_Absolute res;
  struct GNUNET_TIME_Absolute exp_time;
  struct GNUNET_JSON_Specification spec[] = {
    GNUNET_JSON_spec_absolute_time ("expiration",
                                    &exp_time),
    GNUNET_JSON_spec_end ()
  };
  struct tm tv;

  if (GNUNET_OK !=
      GNUNET_JSON_parse (AG_redux_state,
                         spec,
                         NULL, NULL))
  {
    GNUNET_break (0);
    AG_error ("State did not parse correctly: lacks expiration");
    return GNUNET_TIME_UNIT_ZERO_ABS;
  }

  {
    time_t t;

    t = exp_time.abs_value_us / GNUNET_TIME_UNIT_SECONDS.rel_value_us;
    GNUNET_assert (NULL !=
                   localtime_r (&t,
                                &tv));
  }

  spin_button = GTK_SPIN_BUTTON (GCG_get_main_window_object (
                                   "expiration_year_spin_button"));
  value = gtk_spin_button_get_value_as_int (spin_button);
  tv.tm_year = value - 1900;
  {
    time_t t = mktime (&tv);

    res.abs_value_us = t * GNUNET_TIME_UNIT_SECONDS.rel_value_us;
  }
  return res;
}


/**
 * Callback invoked if the user changed when the
 * backup may expire. Update cost.
 *
 * @param spin_button the spin button that changed
 * @param user_data NULL
 */
void
expiration_year_spin_button_value_changed_cb (
  GtkSpinButton *spin_button,
  gpointer user_data)
{
  json_t *arg;
  struct GNUNET_TIME_Absolute expiration;

  expiration = AG_get_desired_expiration ();
  if (0 == expiration.abs_value_us)
    return; /* failured */
  (void) GNUNET_TIME_round_abs (&expiration);
  arg = GNUNET_JSON_PACK (
    GNUNET_JSON_pack_time_abs ("expiration",
                               (expiration)));
  AG_freeze ();
  AG_ra = ANASTASIS_redux_action (AG_redux_state,
                                  "update_expiration",
                                  arg,
                                  &AG_action_cb,
                                  NULL);
  json_decref (arg);
}