aboutsummaryrefslogtreecommitdiff
path: root/src/setup/gnunet-setup-gns-edit.c
blob: f790572b2c5dbec00e2c77fd634813892a5d0736 (plain) (blame)
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
     This file is part of GNUnet
     (C) 2012 Christian Grothoff (and other contributing authors)

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

     GNUnet 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 GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/
/**
 * @file src/setup/gnunet-setup-gns-edit.c
 * @author Christian Grothoff
 * @brief editing dialogs for GNS records
 */
#include "gnunet-setup-gns-edit.h"
#include <gnunet/gnunet_gns_service.h>


/**
 * Columns in the 'zone_liststore'.
 */
enum ZoneListColumn
{
  /**
   * A gchararray with the name of the zone for users.
   */
  ZONELIST_COL_NAME = 0,
  
  /**
   * A gchararray with the name of the configuration file option with 
   * the zone key.
   */
  ZONELIST_COL_OPTION = 1
};


/**
 * Disable 'save' button, dialog state is not acceptable.
 *
 * @param edc dialog to modify
 */
static void
edit_dialog_disable_save (struct EditDialogContext *edc)
{  
  gtk_widget_set_sensitive (GTK_WIDGET (gtk_builder_get_object (edc->builder,
								"edit_dialog_save_button")),
			    FALSE);
}


/**
 * Enable 'save' button, dialog state is acceptable.
 *
 * @param edc dialog to modify
 */
static void
edit_dialog_enable_save (struct EditDialogContext *edc)
{  
  gtk_widget_set_sensitive (GTK_WIDGET (gtk_builder_get_object (edc->builder,
								"edit_dialog_save_button")),
			    TRUE);
}


/**
 * Check that the common elements of the edit dialog are valid;
 * if so, call 'edit_dialog_enable_save', otherwise 'edit_dialog_disable_save'.
 *
 * @param edc edit dialog to check
 */
static void
edit_dialog_check_save (struct EditDialogContext *edc)
{
  GtkEditable *entry;
  const gchar *name;

  /* check name */
  entry = GTK_EDITABLE (gtk_builder_get_object (edc->builder,
						"edit_dialog_name_entry"));
  name = gtk_editable_get_chars (entry, 0, -1);
  if ( (GNUNET_SYSERR == GNUNET_DNSPARSER_check_label (name)) &&
       (0 != strcmp (name, GNUNET_GNS_MASTERZONE_STR)) )
  {
    edit_dialog_disable_save (edc);
    return;
  }
  /* any other checks should go here */
  edit_dialog_enable_save (edc);
}


/**
 * The user has edited the A record value.  Enable/disable 'save'
 * button depending on the validity of the value.
 *
 * @param entry editing widget
 * @param preedit new value
 * @param user_data the 'struct EditDialogContext' of the dialog
 */
void
GNS_edit_dialog_name_entry_changed_cb (GtkEditable *entry,
				       gpointer user_data)
{
  struct EditDialogContext *edc = user_data;

  edc->validator (edc);
}


/**
 * The 'relative' expiration time radiobutton was toggled (on or off).
 *
 * @param button the button
 * @param user_data the 'struct EditDialogContext' of the dialog
 */
void
GNS_edit_dialog_expiration_relative_radiobutton_toggled_cb (GtkToggleButton *button,
							    gpointer user_data)
{
  struct EditDialogContext *edc = user_data;

  if (gtk_toggle_button_get_active (button))  
    gtk_widget_show (GTK_WIDGET (gtk_builder_get_object (edc->builder,
							 "edit_dialog_expiration_relative_combobox")));  
  else
    gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (edc->builder,
							 "edit_dialog_expiration_relative_combobox")));
}


/**
 * The 'forever' expiration time radiobutton was toggled (on or off).
 *
 * @param button the button
 * @param user_data the 'struct EditDialogContext' of the dialog
 */
void
GNS_edit_dialog_expiration_forever_radiobutton_toggled_cb (GtkToggleButton *button,
							    gpointer user_data)
{
  /* nothing to do */
}


/**
 * The 'absolute' expiration time radiobutton was toggled (on or off).
 *
 * @param button the button
 * @param user_data the 'struct EditDialogContext' of the dialog
 */
void
GNS_edit_dialog_expiration_absolute_radiobutton_toggled_cb (GtkToggleButton *button,
							    gpointer user_data)
{
  struct EditDialogContext *edc = user_data;

  if (gtk_toggle_button_get_active (button))  
  {
    gtk_widget_show (GTK_WIDGET (gtk_builder_get_object (edc->builder,
							 "edit_dialog_expiration_absolute_calendar")));
    gtk_widget_show (GTK_WIDGET (gtk_builder_get_object (edc->builder,
							 "edit_dialog_expiration_absolute_hbox")));
  }
  else
  {
    gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (edc->builder,
							 "edit_dialog_expiration_absolute_calendar")));
    gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (edc->builder,
							 "edit_dialog_expiration_absolute_hbox")));
  }
}


/**
 * Initialize widgets of the edit dialog that are the same regardless of
 * the type of the record.
 *
 * @param edc dialog context
 */
static void
edit_dialog_setup_common_elements (struct EditDialogContext *edc)
{
  GtkComboBox *cb;
  GtkTreeModel *tm;
  GtkListStore *ls;
  GtkTreeIter iter;
  gchar *opt;
  struct GNUNET_TIME_Absolute at;
  struct GNUNET_TIME_Relative rt;
  time_t tp;
  struct tm *ymd;
  GtkCalendar *cal;

  if (GNUNET_YES !=
      edc->old_record_in_namestore)
  {
    gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (edc->builder,
							 "edit_dialog_delete_button")));
    edit_dialog_disable_save (edc);
  }
  gtk_entry_set_text (GTK_ENTRY (gtk_builder_get_object (edc->builder,
							 "edit_dialog_name_entry")),
		      edc->n_new_name);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (edc->builder,
									   "edit_dialog_options_public_checkbutton")),
				edc->n_public);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (edc->builder,
									   "edit_dialog_options_shadow_checkbutton")),
				edc->n_is_shadow);
  if (GNUNET_TIME_UNIT_FOREVER_REL.rel_value == edc->n_exp_time)
  {
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (edc->builder,
									     "edit_dialog_expiration_never_radiobutton")),
				  TRUE);
    gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (edc->builder,
							 "edit_dialog_expiration_absolute_calendar")));
    gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (edc->builder,
							 "edit_dialog_expiration_absolute_hbox")));
    gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (edc->builder,
							 "edit_dialog_expiration_relative_combobox")));
  }
  if ( (edc->n_is_relative) &&
       (GNUNET_TIME_UNIT_FOREVER_REL.rel_value != edc->n_exp_time) )
  {
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (edc->builder,
									     "edit_dialog_expiration_relative_radiobutton")),
				  TRUE);
    gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (edc->builder,
							 "edit_dialog_expiration_absolute_calendar")));
    gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (edc->builder,
							 "edit_dialog_expiration_absolute_hbox")));
    rt.rel_value = edc->n_exp_time;
  }
  else
  {
    /* select a sane default */
    rt = GNUNET_TIME_UNIT_DAYS;
  }
  cb = GTK_COMBO_BOX (gtk_builder_get_object (edc->builder,
					      "edit_dialog_expiration_relative_combobox"));
  ls = GTK_LIST_STORE (gtk_combo_box_get_model (cb));
  gtk_list_store_insert_with_values (ls, &iter,
				     -1 /* position: append */,
				     0, GNUNET_STRINGS_relative_time_to_string (rt, GNUNET_NO),
				     -1);
  gtk_combo_box_set_active_iter (cb, &iter);    
  if ( (! edc->n_is_relative) &&
       (GNUNET_TIME_UNIT_FOREVER_ABS.abs_value != edc->n_exp_time) )
  {      
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (edc->builder,
									     "edit_dialog_expiration_absolute_radiobutton")),
				  TRUE);
    
    gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (edc->builder,
							 "edit_dialog_expiration_relative_combobox")));
    at.abs_value = edc->n_exp_time;
  }
  else
  {
    /* select a sane default: right now */
    at = GNUNET_TIME_absolute_get ();
  }
  tp = (time_t) (at.abs_value / 1000LL); /* convert to seconds */
  ymd = gmtime (&tp);
  cal = GTK_CALENDAR (gtk_builder_get_object (edc->builder,
					      "edit_dialog_expiration_absolute_calendar"));
  gtk_calendar_select_month (cal,
			     ymd->tm_mon,
			     ymd->tm_year + 1900); 
  gtk_calendar_mark_day (cal,
			 ymd->tm_mday);
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (gtk_builder_get_object (edc->builder,
								      "edit_dialog_expiration_absolute_hours_spinbutton")),
			     (double) ymd->tm_hour);
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (gtk_builder_get_object (edc->builder,
								      "edit_dialog_expiration_absolute_minutes_spinbutton")),
			     (double) ymd->tm_min);
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (gtk_builder_get_object (edc->builder,
								      "edit_dialog_expiration_absolute_seconds_spinbutton")),
			     (double) ymd->tm_sec);

  /* set source zone! */
  cb = GTK_COMBO_BOX (gtk_builder_get_object (edc->builder,
					      "edit_dialog_zone_combobox"));
  tm = gtk_combo_box_get_model (cb);
  opt = NULL;
  if (gtk_tree_model_get_iter_first (tm, &iter))
  {
    do
    {
      gtk_tree_model_get (tm, &iter,
			  ZONELIST_COL_OPTION, &opt,
			  -1);
      if ( (NULL != opt) &&
	   (0 == strcasecmp (opt,
			     edc->new_zone_option)) )
	break;
      g_free (opt);
      opt = NULL;
    } while (gtk_tree_model_iter_next (tm, &iter));    
  }
  if (NULL != opt)
  {
    gtk_combo_box_set_active_iter (cb, &iter);
    g_free (opt);
  }
}


/**
 * Perform the reverse of the 'edit_dialog_setup_common_elements' function,
 * that is, extract the values from the (common) widgets and store the
 * values in 'edc'. 
 *
 * @param edc edit dialog to extract data from
 */
static void
edit_dialog_putes_common_elements (struct EditDialogContext *edc)
{
  GtkEntry *entry;
  const char *rt_s;
  struct GNUNET_TIME_Relative rt;
  GtkComboBox *cb;
  GtkTreeModel *tm;
  GtkTreeIter iter; 
  gchar *opt;

  /* record name */
  entry = GTK_ENTRY (gtk_builder_get_object (edc->builder,
					     "edit_dialog_name_entry"));
  g_free (edc->n_new_name);
  edc->n_new_name = g_strdup (gtk_entry_get_text (entry));

  /* is public flag */
  edc->n_public = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (edc->builder,
											   "edit_dialog_options_public_checkbutton")));
  /* is shadow flag */
  edc->n_is_shadow = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (edc->builder,
											      "edit_dialog_options_shadow_checkbutton")));
  
  /* 'forever' expiration time */
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (edc->builder,
									       "edit_dialog_expiration_never_radiobutton"))))
  {
    edc->n_exp_time = GNUNET_TIME_UNIT_FOREVER_REL.rel_value;
    edc->n_is_relative = TRUE; /* doesn't matter, but make sure it is well-defined anyway */
  }
  
  /* 'relative' expiration time */
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (edc->builder,
									       "edit_dialog_expiration_relative_radiobutton"))))
  {
    cb = GTK_COMBO_BOX (gtk_builder_get_object (edc->builder,
						"edit_dialog_expiration_relative_combobox"));
    tm = gtk_combo_box_get_model (cb);
    if (TRUE != gtk_combo_box_get_active_iter (cb, &iter))
    {
      GNUNET_break (0);
      return;
    }
    gtk_tree_model_get (tm, &iter, 
			0, &rt_s,
			-1);
    GNUNET_break (GNUNET_YES ==
		  GNUNET_STRINGS_fancy_time_to_relative (rt_s,
							 &rt));
    edc->n_exp_time = rt.rel_value;
    edc->n_is_relative = TRUE;
  }

  /* 'absolute' expiration time */
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (edc->builder,
									       "edit_dialog_expiration_absolute_radiobutton"))))
  {
    guint year;
    guint month;
    guint day;
    guint hour;
    guint minute;
    guint second;
    char fancydate[128];
    struct GNUNET_TIME_Absolute atime;
    
    gtk_calendar_get_date (GTK_CALENDAR (gtk_builder_get_object (edc->builder,
								 "edit_dialog_expiration_absolute_calendar")),
			   &year, &month, &day);
    hour = gtk_spin_button_get_value (GTK_SPIN_BUTTON (gtk_builder_get_object (edc->builder,
									       "edit_dialog_expiration_absolute_hours_spinbutton")));
    minute = gtk_spin_button_get_value (GTK_SPIN_BUTTON (gtk_builder_get_object (edc->builder,
									  "edit_dialog_expiration_absolute_minutes_spinbutton")));
    second = gtk_spin_button_get_value (GTK_SPIN_BUTTON (gtk_builder_get_object (edc->builder,
										 "edit_dialog_expiration_absolute_seconds_spinbutton")));
    GNUNET_snprintf (fancydate,
		     sizeof (fancydate),
		     "%u-%u-%u %u:%u:%u",
		     (unsigned int) year, 
		     (unsigned int) month + 1, 
		     (unsigned int) day,
		     (unsigned int) hour,
		     (unsigned int) minute,
		     (unsigned int) second);
    GNUNET_break (GNUNET_OK ==
		  GNUNET_STRINGS_fancy_time_to_absolute (fancydate,
							 &atime));
    edc->n_exp_time = atime.abs_value;
    edc->n_is_relative = FALSE;
  }

  /* extract target zone! */
  cb = GTK_COMBO_BOX (gtk_builder_get_object (edc->builder,
					      "edit_dialog_zone_combobox"));
  tm = gtk_combo_box_get_model (cb);
  if (! gtk_combo_box_get_active_iter (cb, &iter))
  {
    GNUNET_break (0);
  }
  else
  {
    gtk_tree_model_get (tm, &iter,
			ZONELIST_COL_OPTION, &opt,
			-1);
    if (NULL == opt)
      GNUNET_break (0);
    else
    {
      g_free (edc->new_zone_option);
      edc->new_zone_option = g_strdup (opt);
      g_free (opt);
    }
  }
}


/**
 * Run the edit dialog.  Performs all of the common initialization
 * steps to run an edit dialog for records.
 *
 * @param edc editing context
 */
static void
run_edit_dialog (struct EditDialogContext *edc)
{
  edit_dialog_setup_common_elements (edc);
  gtk_dialog_set_default_response (edc->dialog,
				   GTK_RESPONSE_OK);
  gtk_window_present (GTK_WINDOW (edc->dialog));
}


/* ************************ A records *********************** */

/**
 * Check validity of the value in the edit dialog for A-records.
 * Then call the shared validity check if the result is OK.
 *
 * @param edc edit dialog context
 */
static void
edit_dialog_a_validity_check (struct EditDialogContext *edc)
{
  GtkEditable *entry;
  const gchar *preedit;
  struct in_addr v4;

  entry = GTK_EDITABLE (gtk_builder_get_object (edc->builder,
						"edit_dialog_a_entry")),
  preedit = gtk_editable_get_chars (entry, 0, -1);
  if ( (NULL == preedit) ||
       (1 != inet_pton (AF_INET, preedit, &v4)) )
  {
    edit_dialog_disable_save (edc);
    return;
  }
  edit_dialog_check_save (edc);
}


/**
 * Editing dialog was closed, get the data and call the
 * continuation.
 *
 * @param dialog editing dialog
 * @param user_data the 'struct EditDialogContext'
 */
void
GNS_edit_a_dialog_response_cb (GtkDialog *dialog,
			       gint response_id,
			       gpointer user_data)
{
  struct EditDialogContext *edc = user_data;
  GtkEntry *entry;
  const gchar *value;
    
  if (GTK_RESPONSE_OK == response_id)
  {
    edit_dialog_putes_common_elements (edc);
    entry = GTK_ENTRY (gtk_builder_get_object (edc->builder,
					       "edit_dialog_a_entry"));
    value = gtk_entry_get_text (entry);
    g_free (edc->n_value);
    edc->n_value = g_strdup (value);
  }
  gtk_widget_destroy (GTK_WIDGET (edc->dialog));
  g_object_unref (edc->builder);
  edc->builder = NULL;
  edc->cont (edc, response_id);
}


/**
 * The user has edited the A record value.  Enable/disable 'save'
 * button depending on the validity of the value.
 *
 * @param entry editing widget
 * @param preedit new value
 * @param user_data the 'struct EditDialogContext' of the dialog
 */
void
GNS_edit_dialog_a_entry_changed_cb (GtkEditable *entry,
				    gpointer user_data)
{
  struct EditDialogContext *edc = user_data;

  edit_dialog_a_validity_check (edc);
}


/**
 * Run an GNS Edit dialog for an 'A' Record.
 *
 * @param cont continuation to call when done
 * @param edc editing context to use
 */
void
GNS_edit_dialog_a (struct EditDialogContext *edc)
{
  edc->builder = GNUNET_GTK_get_new_builder ("gnunet_setup_gns_edit_a.glade",
					     edc);
  if (NULL == edc->builder)
  {
    GNUNET_break (0);
    edc->cont (edc, GTK_RESPONSE_CANCEL);  /* treat as 'cancel' */
    return;    
  }
  if (GNUNET_YES ==
      edc->old_record_in_namestore)
  {
    /* set A record */
    gtk_entry_set_text (GTK_ENTRY (gtk_builder_get_object (edc->builder,
							   "edit_dialog_a_entry")),
			edc->n_value);
  }
  edc->validator = &edit_dialog_a_validity_check;
  edc->dialog = GTK_DIALOG (gtk_builder_get_object (edc->builder,
						    "edit_a_dialog"));
  run_edit_dialog (edc);
}


/* end of gnunet-setup-gns-edit.c */