summaryrefslogtreecommitdiff
path: root/src/adwaita/disfluid-cache-entry.c
blob: e0245c48d3cb930b1f87f22b1f6af40e67b80771 (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
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
#include <config.h>

#include "attribute.h"
#include <disfluid-adwaita.h>

#define STREQ(a, b) (strcmp ((a), (b)) == 0)
#define STRNEQ(a, b) (! (STREQ (a, b)))

#include <locale.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include "gettext.h"
#include "relocatable.h"

#define _(String) dgettext (PACKAGE, (String))
#define N_(String) (String)

#include <disfluid.h>
#include <glib-object.h>
#include <adwaita.h>
#include <gtk/gtk.h>

struct _DisfluidAdwCacheEntry
{
  AdwPreferencesGroup parent_instance;
  struct disfluid_cache_entry *cache_entry;
  GtkLabel *row_request_date;
  GtkLabel *row_response_date;
  GtkLabel *row_invalidation_status;
};

struct _DisfluidAdwCacheEntryClass
{
  AdwPreferencesGroupClass parent_instance;
};

/* *INDENT-OFF* */
G_DEFINE_TYPE (DisfluidAdwCacheEntry,
               disfluid_adw_cache_entry,
               ADW_TYPE_PREFERENCES_GROUP)
/* *INDENT-ON* */

typedef enum
{
  PROP_CACHE_ENTRY = 1,

  /* ... */
  N_PROPERTIES
} DisfluidAdwCacheEntryProperty;

static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };

static void
disfluid_adw_cache_entry_set_property (GObject * object,
				       guint property_id,
				       const GValue * value,
				       GParamSpec * pspec)
{
  DisfluidAdwCacheEntry *self = DISFLUID_ADW_CACHE_ENTRY (object);
  switch ((DisfluidAdwCacheEntryProperty) property_id)
    {
    case PROP_CACHE_ENTRY:
      assert (G_VALUE_TYPE (value) == DISFLUID_TYPE_CACHE_ENTRY);
      disfluid_adw_cache_entry_set_cache_entry (self,
						g_value_get_boxed (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
disfluid_adw_cache_entry_get_property (GObject * object,
				       guint property_id,
				       GValue * value, GParamSpec * pspec)
{
  DisfluidAdwCacheEntry *self = DISFLUID_ADW_CACHE_ENTRY (object);
  switch ((DisfluidAdwCacheEntryProperty) property_id)
    {
    case PROP_CACHE_ENTRY:
      if (self->cache_entry != NULL)
	{
	  g_value_set_boxed (value,
			     disfluid_cache_entry_dup (self->cache_entry));
	}
      else
	{
	  g_value_set_boxed (value, NULL);
	}
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
disfluid_adw_cache_entry_finalize (GObject * obj)
{
  DisfluidAdwCacheEntry *self = DISFLUID_ADW_CACHE_ENTRY (obj);
  disfluid_cache_entry_free (self->cache_entry);
  G_OBJECT_CLASS (disfluid_adw_cache_entry_parent_class)->finalize (obj);
}

static void
disfluid_adw_cache_entry_class_init (DisfluidAdwCacheEntryClass * klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  object_class->set_property = disfluid_adw_cache_entry_set_property;
  object_class->get_property = disfluid_adw_cache_entry_get_property;
  object_class->finalize = disfluid_adw_cache_entry_finalize;
  obj_properties[PROP_CACHE_ENTRY] =
    g_param_spec_boxed ("cache-entry",
			_("Cache entry"), _("Value of the cache entry \
that is to be displayed by this widget"), DISFLUID_TYPE_CACHE_ENTRY, G_PARAM_READWRITE);
  g_object_class_install_properties (object_class, N_PROPERTIES,
				     obj_properties);
}

static void
disfluid_adw_cache_entry_init (DisfluidAdwCacheEntry * self)
{
  /* Perform default initialization of self. */
  self->cache_entry = NULL;
  self->row_request_date = GTK_LABEL (gtk_label_new (_("Unset")));
  self->row_response_date = GTK_LABEL (gtk_label_new (_("Unset")));
  self->row_invalidation_status = GTK_LABEL (gtk_label_new (_("Unset")));
  GtkLabel *my_labels[3] = { self->row_request_date, self->row_response_date,
    self->row_invalidation_status
  };
  for (size_t i = 0; i < sizeof (my_labels) / sizeof (my_labels[0]); i++)
    {
      gtk_label_set_wrap (my_labels[i], true);
      const int margin = 8;
      gtk_widget_set_margin_top (GTK_WIDGET (my_labels[i]), margin);
      gtk_widget_set_margin_bottom (GTK_WIDGET (my_labels[i]), margin);
      gtk_widget_set_margin_start (GTK_WIDGET (my_labels[i]), margin);
      gtk_widget_set_margin_end (GTK_WIDGET (my_labels[i]), margin);
    }
  GtkListBoxRow *row_request_date =
    GTK_LIST_BOX_ROW (g_object_new (ADW_TYPE_PREFERENCES_ROW,
				    "title", _("Request date"),
				    "child", self->row_request_date,
				    "selectable", false,
				    NULL));
  GtkListBoxRow *row_response_date =
    GTK_LIST_BOX_ROW (g_object_new (ADW_TYPE_PREFERENCES_ROW,
				    "title", _("Response date"),
				    "child", self->row_response_date,
				    "selectable", false,
				    NULL));
  GtkListBoxRow *row_invalidation_status =
    GTK_LIST_BOX_ROW (g_object_new (ADW_TYPE_PREFERENCES_ROW,
				    "title", _("Invalidation status"),
				    "child", self->row_invalidation_status,
				    "selectable", false,
				    NULL));
  adw_preferences_group_set_title (ADW_PREFERENCES_GROUP (self),
				   _("Unknown cache entry"));
  adw_preferences_group_set_description (ADW_PREFERENCES_GROUP (self),
					 _("Unknown URI"));
  adw_preferences_group_add (ADW_PREFERENCES_GROUP (self),
			     GTK_WIDGET (row_request_date));
  adw_preferences_group_add (ADW_PREFERENCES_GROUP (self),
			     GTK_WIDGET (row_response_date));
  adw_preferences_group_add (ADW_PREFERENCES_GROUP (self),
			     GTK_WIDGET (row_invalidation_status));
}

void
disfluid_adw_cache_entry_set_cache_entry (DisfluidAdwCacheEntry * self,
					  struct disfluid_cache_entry *value)
{
  disfluid_cache_entry_free (self->cache_entry);
  if (value != NULL)
    {
      self->cache_entry = disfluid_cache_entry_dup (value);
      struct timespec request_ts, response_ts;
      disfluid_cache_entry_get_request_date (self->cache_entry, &request_ts);
      disfluid_cache_entry_get_response_date (self->cache_entry,
					      &response_ts);
      GDateTime *request_date_gross =
	g_date_time_new_from_unix_local (request_ts.tv_sec);
      const double request_frac = request_ts.tv_nsec * 1e-9;
      GDateTime *request_date = g_date_time_add_seconds (request_date_gross,
							 request_frac);
      g_date_time_unref (request_date_gross);
      GDateTime *response_date_gross =
	g_date_time_new_from_unix_local (response_ts.tv_sec);
      const double response_frac = response_ts.tv_nsec * 1e-9;
      GDateTime *response_date = g_date_time_add_seconds (response_date_gross,
							  response_frac);
      g_date_time_unref (response_date_gross);
      gchar *request_format = g_date_time_format (request_date, "%c");
      gchar *response_format = g_date_time_format (response_date, "%c");
      g_date_time_unref (request_date);
      g_date_time_unref (response_date);
      char *request_title;
      char *response_title;
      char *invalidation_status_title;
      /* TRANSLATORS: The argument is a date and time. */
      if (asprintf (&request_title, _("Requested %s"), request_format) < 0)
	{
	  abort ();
	}
      /* TRANSLATORS: The argument is a date and time. */
      if (asprintf (&response_title, _("Responded %s"), response_format) < 0)
	{
	  abort ();
	}
      if (disfluid_cache_entry_invalidated (self->cache_entry))
	{
	  if (asprintf
	      (&invalidation_status_title,
	       _("This response has been invalidated.")) < 0)
	    {
	      abort ();
	    }
	}
      else
	{
	  if (asprintf
	      (&invalidation_status_title,
	       _("This response is still valid.")) < 0)
	    {
	      abort ();
	    }
	}
      g_free (request_format);
      g_free (response_format);
      gtk_label_set_label (self->row_request_date, request_title);
      gtk_label_set_label (self->row_response_date, response_title);
      gtk_label_set_label (self->row_invalidation_status,
			   invalidation_status_title);
      g_free (request_title);
      g_free (response_title);
      g_free (invalidation_status_title);
    }
  else
    {
      self->cache_entry = NULL;
      gtk_label_set_label (self->row_request_date, _("No content"));
      gtk_label_set_label (self->row_response_date, _("No content"));
      gtk_label_set_label (self->row_invalidation_status, _("No content"));
    }
}