aboutsummaryrefslogtreecommitdiff
path: root/src/identity/gnunet-identity-gtk_ego_organizer.c
blob: fbb1d13b1418d90c6b24c3468fc80d21e483c8c1 (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
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
/*
     This file is part of GNUnet
     (C) 2005, 2006, 2010, 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 3, 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/fs/gnunet-fs-gtk_namespace_organizer.c
 * @author LRN
 */
#include "gnunet-fs-gtk_common.h"
#include "gnunet-fs-gtk.h"
#include "gnunet-fs-gtk_advertise-pseudonym.h"
#include <gnunet/gnunet_util_lib.h>
#include <gnunet/gnunet_fs_service.h>

struct GNUNET_GTK_NamespaceSelectorContext
{
  GtkBuilder *builder;
  GtkWidget *my_ns;
  GtkTreeSelection *my_ns_sel;
  GtkListStore *my_ns_store;
  GtkWindow *namespace_organizer;
  GtkWidget *create_button;
  GtkWidget *delete_button;
  GtkWidget *advertise_button;
  GtkWidget *do_delete_button;
  GtkWidget *do_keep_button;
  GtkWidget *progress_spinner;
  GtkWidget *progress_label;
  GtkWidget *confirmation_label;
  char *default_ns;
  GtkTreeRowReference *default_ns_rref;
  struct GNUNET_GTK_MainWindowContext *main_ctx;
};

#define GNUNET_GTK_MY_NAMESPACES_NAME_COLUMN 0
#define GNUNET_GTK_MY_NAMESPACES_KEY_COLUMN 1
#define GNUNET_GTK_MY_NAMESPACES_KEY_BINARY_COLUMN 2
#define GNUNET_GTK_MY_NAMESPACES_USE_THIS_COLUMN 3
#define GNUNET_GTK_MY_NAMESPACES_ACTUAL_NAME_COLUMN 4

/**
 * User edited the name of a namespace.
 * Rename the namespace.
 *
 * @param renderer cell renderer
 * @param path path to the cell that was edited (as a string)
 * @param new_text new contents of the cell
 * @param user_data context
 */
void
GNUNET_GTK_namespace_organizer_namespaces_treeview_column_name_text_edited_cb (
    GtkCellRendererText *renderer, gchar *path, gchar *new_text,
    gpointer user_data)
{
  struct GNUNET_GTK_NamespaceSelectorContext *ctx = user_data;
  GtkTreePath *tree_path;
  GtkTreeIter iter;

  if (strlen (new_text) == 0)
    return;

  tree_path = gtk_tree_path_new_from_string (path);
  if (tree_path != NULL)
  {
    if (gtk_tree_model_get_iter (GTK_TREE_MODEL (ctx->my_ns_store),
        &iter, tree_path))
    {
      struct GNUNET_FS_PseudonymIdentifier *nsid;
      char *old_name;
      gtk_tree_model_get (GTK_TREE_MODEL (ctx->my_ns_store), &iter,
          GNUNET_GTK_MY_NAMESPACES_NAME_COLUMN, &old_name,
          GNUNET_GTK_MY_NAMESPACES_KEY_BINARY_COLUMN, &nsid,
          -1);

      if ((NULL == nsid) ||
          (GNUNET_OK == GNUNET_FS_namespace_rename (
              GNUNET_FS_GTK_get_fs_handle (), old_name, new_text)))
      {
        gtk_list_store_set (ctx->my_ns_store, &iter,
            GNUNET_GTK_MY_NAMESPACES_NAME_COLUMN, new_text, -1);
      }

      GNUNET_free (old_name);
    }
    gtk_tree_path_free (tree_path);
  }
}

/**
 * Frees contents of the namespace list (the nsid values that are stored as
 * untyped pointers in the tree).
 *
 * @param ctx NS organizer context
 */
static void
free_lists_contents (struct GNUNET_GTK_NamespaceSelectorContext *ctx)
{
  GtkTreeIter iter;
  gint i;
  gint row_count;
  struct GNUNET_FS_PseudonymIdentifier *nsid;

  row_count = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (
      ctx->my_ns_store), NULL);
  if (! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (
      ctx->my_ns_store), &iter))
    return;

  for (i = 0; i < row_count; i++)
  {
    gtk_tree_model_get (GTK_TREE_MODEL (ctx->my_ns_store), &iter,
        GNUNET_GTK_MY_NAMESPACES_KEY_BINARY_COLUMN, &nsid,
        -1);
    GNUNET_free (nsid);
    if (TRUE != gtk_tree_model_iter_next (GTK_TREE_MODEL (
      ctx->my_ns_store), &iter))
      break;
  }

  gtk_list_store_clear (ctx->my_ns_store);
}

/**
 * Called when dialog is being closed (for any reason).
 *
 * @param dialog dialog being closed
 * @param response_id identifies the event
 * @param user_data context
 */
void
GNUNET_GTK_namespace_organizer_dialog_response_cb (GtkDialog *dialog,
    gint response_id, gpointer user_data)
{
  struct GNUNET_GTK_NamespaceSelectorContext *ctx = user_data;
  switch (response_id)
  {
  case GTK_RESPONSE_CLOSE:
  default:
    free_lists_contents (ctx);
    gtk_widget_destroy (GTK_WIDGET (ctx->namespace_organizer));
    GNUNET_free (ctx);
  }
}

/**
 * Store a reference to the default namespace for later use.
 *
 * @param ctx context
 * @param iter points to the default namespace in the list
 */
static void
save_default_ns_reference (struct GNUNET_GTK_NamespaceSelectorContext *ctx, GtkTreeIter *iter)
{
  GtkTreeRowReference *ref;
  GtkTreePath *rowpath;

  rowpath = gtk_tree_model_get_path (GTK_TREE_MODEL (ctx->my_ns_store), iter);  
  if (rowpath)
  {
    ref = gtk_tree_row_reference_new (GTK_TREE_MODEL (ctx->my_ns_store), rowpath);
    if (ref)
    {
      gtk_tree_row_reference_free (ctx->default_ns_rref);
      ctx->default_ns_rref = ref;
    }
    gtk_tree_path_free (rowpath);
  }
}


/**
 * Try to mark an item in the list as the default namespace.
 *
 * @param ctx context
 * @param iter points to the new default namespace in the list
 */
static void
try_make_default (struct GNUNET_GTK_NamespaceSelectorContext *ctx, GtkTreeIter *toggled)
{
  GtkTreeIter iter;
  gchar *new_default_ns;
  gint i;
  gint row_count;
  GtkTreePath *path;

  row_count = gtk_tree_model_iter_n_children (
      GTK_TREE_MODEL (ctx->my_ns_store), NULL);
  if (! gtk_tree_model_get_iter_first (
      GTK_TREE_MODEL (ctx->my_ns_store), &iter))
    return;

  path = gtk_tree_row_reference_get_path (ctx->default_ns_rref);
  if (path)
  {
    /* We know where previous default was, only untoggle that item */
    GtkTreeIter old_default;
    if (gtk_tree_model_get_iter (GTK_TREE_MODEL (ctx->my_ns_store), &old_default, path))
    {
      gtk_list_store_set (ctx->my_ns_store, &old_default,
          GNUNET_GTK_MY_NAMESPACES_USE_THIS_COLUMN, FALSE,
          -1);
    }
    gtk_tree_path_free (path);
  }
  else
  {
    /* Just untoggle all items */
    for (i = 0; i < row_count; i++)
    {
      gtk_list_store_set (ctx->my_ns_store, &iter,
          GNUNET_GTK_MY_NAMESPACES_USE_THIS_COLUMN, FALSE,
          -1);
      if (TRUE != gtk_tree_model_iter_next (
          GTK_TREE_MODEL (ctx->my_ns_store), &iter))
        break;
    }
  }

  gtk_list_store_set (ctx->my_ns_store, toggled,
      GNUNET_GTK_MY_NAMESPACES_USE_THIS_COLUMN, TRUE, -1);
  gtk_tree_model_get (GTK_TREE_MODEL (ctx->my_ns_store), toggled,
      GNUNET_GTK_MY_NAMESPACES_NAME_COLUMN, &new_default_ns, -1);

  save_default_ns_reference (ctx, toggled);

  if (new_default_ns)
  {
    ctx->default_ns = GNUNET_strdup (new_default_ns);
    GNUNET_CONFIGURATION_set_value_string (
       (struct GNUNET_CONFIGURATION_Handle *) GNUNET_FS_GTK_get_configuration (),
      "gnunet-fs-gtk", "DEFAULT_NAMESPACE", ctx->default_ns);

    g_free (new_default_ns);
  }
}



/**
 * Callback with information about local (!) namespaces.
 * Contains the names of the local namespace and the global
 * ID.
 *
 * @param cls closure
 * @param name human-readable identifier of the namespace
 * @param id hash identifier for the namespace
 */
static void
populate_my_ns_list (void *cls, const char *name,
    const struct GNUNET_FS_PseudonymIdentifier *id)
{
  struct GNUNET_GTK_NamespaceSelectorContext *ctx = cls;

  char buf[1024];
  char *ret;
  GtkTreeIter my_iter;
  struct GNUNET_FS_PseudonymIdentifier *nsid;
  gboolean is_default;

  nsid = GNUNET_malloc (sizeof (struct GNUNET_FS_PseudonymIdentifier));
  *nsid = *id;

  ret = GNUNET_STRINGS_data_to_string (nsid,
      sizeof (struct GNUNET_FS_PseudonymIdentifier), buf, sizeof (buf));
  GNUNET_assert (NULL != ret);
  ret[0] = '\0';

  if (NULL == ctx->default_ns)
  {
    ctx->default_ns = GNUNET_strdup (name);
    GNUNET_CONFIGURATION_set_value_string (
       (struct GNUNET_CONFIGURATION_Handle *) GNUNET_FS_GTK_get_configuration (),
      "gnunet-fs-gtk", "DEFAULT_NAMESPACE", ctx->default_ns);
    is_default = TRUE;
  }
  else
    is_default = (0 == strcmp (name, ctx->default_ns)) ? TRUE : FALSE;

  gtk_list_store_insert_with_values (ctx->my_ns_store, &my_iter, G_MAXINT,
      GNUNET_GTK_MY_NAMESPACES_NAME_COLUMN, name,
      GNUNET_GTK_MY_NAMESPACES_KEY_COLUMN, buf,
      GNUNET_GTK_MY_NAMESPACES_KEY_BINARY_COLUMN, nsid,
      GNUNET_GTK_MY_NAMESPACES_USE_THIS_COLUMN, is_default,
      GNUNET_GTK_MY_NAMESPACES_ACTUAL_NAME_COLUMN, NULL,
      -1);

  if (is_default)
    save_default_ns_reference (ctx, &my_iter);
}

/**
 * Returns button visibility/sensitivity to normal.
 *
 * @param ctx context
 * @param anything_selected TRUE if a namespace is selected in the list,
 *                          FALSE otherwise
 */
static void
set_normal_state (struct GNUNET_GTK_NamespaceSelectorContext *ctx, gboolean anything_selected)
{
  gtk_widget_set_visible (ctx->do_delete_button, FALSE);
  gtk_widget_set_visible (ctx->do_keep_button, FALSE);
  gtk_widget_set_visible (ctx->confirmation_label, FALSE);

  gtk_widget_set_sensitive (ctx->advertise_button, anything_selected);
  gtk_widget_set_sensitive (ctx->delete_button, anything_selected);
  gtk_widget_set_sensitive (ctx->create_button, TRUE);
}

/**
 * Handles ns list selection changes.
 *
 * @param treeselection selection object
 * @param user_data context
 */
static void
my_ns_selection_changed (GtkTreeSelection *treeselection,
    gpointer user_data)
{
  struct GNUNET_GTK_NamespaceSelectorContext *ctx = user_data;
  GtkTreeIter iter;
  struct GNUNET_FS_PseudonymIdentifier *nsid;
  gboolean selected;

  nsid = NULL;
  selected = gtk_tree_selection_get_selected (ctx->my_ns_sel, NULL, &iter);
  if (selected)
    gtk_tree_model_get (GTK_TREE_MODEL (ctx->my_ns_store), &iter,
        GNUNET_GTK_MY_NAMESPACES_KEY_BINARY_COLUMN, &nsid, -1);
  if (NULL == nsid)
    selected = FALSE;
  set_normal_state (ctx, selected);
}

/**
 * Opens the organizer dialog. Called from outside (by the main window).
 *
 * @param main_ctx context of the main window
 * @return newly-created dialog window object
 */
GtkWindow *
GNUNET_GTK_namespace_organizer_open (struct GNUNET_GTK_MainWindowContext *main_ctx)
{
  struct GNUNET_GTK_NamespaceSelectorContext *ctx;
  const struct GNUNET_CONFIGURATION_Handle *cfg;

  ctx = GNUNET_malloc (sizeof (struct GNUNET_GTK_NamespaceSelectorContext));
  ctx->builder = GNUNET_GTK_get_new_builder ("gnunet_fs_gtk_namespace_organizer.glade", ctx);
  if (ctx->builder == NULL)
  {
    GNUNET_break (0);
    GNUNET_free (ctx);
    return NULL;
  }

  ctx->main_ctx = main_ctx;

  /* initialize widget references */
  ctx->my_ns = GTK_WIDGET (gtk_builder_get_object (ctx->builder,
      "GNUNET_GTK_namespace_organizer_namespaces_treeview"));
  ctx->my_ns_sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (
      ctx->my_ns));
  ctx->my_ns_store = GTK_LIST_STORE (gtk_tree_view_get_model (
      GTK_TREE_VIEW (ctx->my_ns)));
  ctx->namespace_organizer = GTK_WINDOW (gtk_builder_get_object (
      ctx->builder, "GNUNET_GTK_namespace_organizer_dialog"));
  ctx->create_button = GTK_WIDGET (gtk_builder_get_object (ctx->builder,
      "GNUNET_GTK_namespace_organizer_namespaces_create_namespace_button"));
  ctx->advertise_button = GTK_WIDGET (gtk_builder_get_object (ctx->builder,
      "GNUNET_GTK_namespace_organizer_namespaces_advertise_namespace_button"));
  ctx->delete_button = GTK_WIDGET (gtk_builder_get_object (ctx->builder,
      "GNUNET_GTK_namespace_organizer_namespaces_delete_namespace_button"));
  ctx->do_delete_button = GTK_WIDGET (gtk_builder_get_object (ctx->builder,
      "GNUNET_GTK_namespace_organizer_namespaces_delete_namespace_confirmation_button"));
  ctx->do_keep_button = GTK_WIDGET (gtk_builder_get_object (ctx->builder,
      "GNUNET_GTK_namespace_organizer_namespaces_delete_namespace_unconfirmation_button"));
  ctx->progress_spinner = GTK_WIDGET (gtk_builder_get_object (ctx->builder,
      "GNUNET_GTK_namespace_organizer_namespaces_create_namespace_progress_spinner"));
  ctx->progress_label = GTK_WIDGET (gtk_builder_get_object (ctx->builder,
      "GNUNET_GTK_namespace_organizer_namespaces_create_namespace_progress_label"));
  ctx->confirmation_label = GTK_WIDGET (gtk_builder_get_object (ctx->builder,
      "GNUNET_GTK_namespace_organizer_namespaces_delete_namespace_confirmation_label"));

  /* connect signals; FIXME-GTK3: these could be connected with (modern) Glade */
  g_signal_connect (G_OBJECT (ctx->my_ns_sel), "changed",
      G_CALLBACK (my_ns_selection_changed), ctx);

  cfg = GNUNET_FS_GTK_get_configuration ();

  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string (cfg,
      "gnunet-fs-gtk", "DEFAULT_NAMESPACE", &ctx->default_ns))
    ctx->default_ns = NULL;

  gtk_widget_set_sensitive (ctx->advertise_button, FALSE);
  gtk_widget_set_sensitive (ctx->delete_button, FALSE);

  gtk_widget_set_visible (ctx->do_delete_button, FALSE);
  gtk_widget_set_visible (ctx->do_keep_button, FALSE);
  gtk_widget_set_visible (ctx->progress_spinner, FALSE);
  gtk_widget_set_visible (ctx->progress_label, FALSE);
  gtk_widget_set_visible (ctx->confirmation_label, FALSE);

  /* populate namespace model */
  GNUNET_FS_namespace_list (GNUNET_FS_GTK_get_fs_handle (),
      populate_my_ns_list, ctx);

  /* show dialog */
  gtk_window_present (ctx->namespace_organizer);
  return ctx->namespace_organizer;
}

/**
 * Called when the organizer dialog is destoryed
 * (happens at some point after it's closed).
 * Cleans up references to the organizer dialog that main window stores.
 *
 * @param object the object that is being destroyed
 * @param user_data main window context
 */
void
GNUNET_GTK_namespace_organizer_dialog_destroy_cb (GtkWindow *object, gpointer user_data)
{
  struct GNUNET_GTK_MainWindowContext *ctx = user_data;
  g_signal_handler_disconnect (object, ctx->ns_organizer_delete_handler_id);
  g_object_unref (G_OBJECT (ctx->ns_organizer));
  ctx->ns_organizer = NULL;
  ctx->ns_organizer_delete_handler_id = 0;
  return;
}

/**
 * Wrapper for GNUNET_GTK_namespace_organizer_open(), ensures that
 * only one instance is opened.
 * @param ctx main window context
 */
void
GNUNET_GTK_open_ns_organizer (struct GNUNET_GTK_MainWindowContext *ctx)
{
  if (ctx->ns_organizer != NULL)
  {
    gtk_window_present (ctx->ns_organizer);
    return;
  }

  ctx->ns_organizer = GNUNET_GTK_namespace_organizer_open (ctx);
  if (ctx->ns_organizer == NULL)
    return;

  g_object_ref (G_OBJECT (ctx->ns_organizer));
  ctx->ns_organizer_delete_handler_id = g_signal_connect (G_OBJECT (ctx->ns_organizer), "destroy", G_CALLBACK (GNUNET_GTK_namespace_organizer_dialog_destroy_cb), ctx);
}

/**
 * Called when user clicks at 'Advertise namespace' button.
 * Opens the advertising dialog (calls GNUNET_FS_GTK_advertise_namespace()).
 *
 * @param button button that was clicked
 * @param user_data context
 */
void
GNUNET_GTK_namespace_organizer_namespaces_advertise_namespace_button_clicked_cb (
    GtkButton *button, gpointer user_data)
{
  struct GNUNET_GTK_NamespaceSelectorContext *ctx = user_data;
  GtkTreeIter iter;
  gchar *name;
  struct GNUNET_FS_PseudonymIdentifier *nsid;

  nsid = NULL;
  name = NULL;
  if (gtk_tree_selection_get_selected (ctx->my_ns_sel, NULL, &iter))
    gtk_tree_model_get (GTK_TREE_MODEL (ctx->my_ns_store), &iter,
        GNUNET_GTK_MY_NAMESPACES_NAME_COLUMN, &name, 
        GNUNET_GTK_MY_NAMESPACES_KEY_BINARY_COLUMN, &nsid,
        -1);
  if (NULL != nsid)
  {
    struct GNUNET_FS_Namespace *ns;
    ns = GNUNET_FS_namespace_open_existing (GNUNET_FS_GTK_get_fs_handle (), name);
    if (ns)
      GNUNET_FS_GTK_advertise_namespace (ns, ctx->namespace_organizer);
  }
  g_free (name);
}

/**
 * Called when user clicks at 'Do not delete' button.
 * Returns the state back to normal (hides [un]confirmation buttons
 * and the warning label).
 *
 * @param button button that was clicked
 * @param user_data context
 */
void
GNUNET_GTK_namespace_organizer_namespaces_delete_namespace_unconfirmation_button_clicked_cb (
    GtkButton *button, gpointer user_data)
{
  set_normal_state (user_data, TRUE);
}


/**
 * Called when user clicks at 'Do delete' button.
 * Deletes selected namespace after double-checking that everything is
 * in order.
 * Returns the state back to normal (hides [un]confirmation buttons
 * and the warning label).
 *
 * @param button button that was clicked
 * @param user_data context
 */
void
GNUNET_GTK_namespace_organizer_namespaces_delete_namespace_confirmation_button_clicked_cb (
    GtkButton *button, gpointer user_data)
{
  struct GNUNET_GTK_NamespaceSelectorContext *ctx = user_data;
  GtkTreeIter iter;
  gchar *name;
  gboolean selected;
  struct GNUNET_FS_PseudonymIdentifier id;
  struct GNUNET_FS_PseudonymIdentifier *nsid;

  nsid = NULL;
  name = NULL;
  selected = gtk_tree_selection_get_selected (ctx->my_ns_sel, NULL, &iter);
  if (selected)
    gtk_tree_model_get (GTK_TREE_MODEL (ctx->my_ns_store), &iter,
        GNUNET_GTK_MY_NAMESPACES_NAME_COLUMN, &name, 
        GNUNET_GTK_MY_NAMESPACES_KEY_BINARY_COLUMN, &nsid,
        -1);
  if (NULL == nsid)
    selected = FALSE;

  if (selected)
  {
    struct GNUNET_FS_Namespace *ns;
    ns = GNUNET_FS_namespace_open_existing (GNUNET_FS_GTK_get_fs_handle (), name);
    if (ns)
    {
      if (GNUNET_FS_namespace_get_public_identifier (ns, &id))
      {
        if (0 == memcmp (&id, nsid, sizeof (id)))
        {
          GtkTreeIter first;

          GNUNET_free (nsid);
          gtk_list_store_remove (ctx->my_ns_store, &iter);
          GNUNET_FS_namespace_delete (ns, GNUNET_YES);

          if (gtk_tree_model_get_iter_first (
              GTK_TREE_MODEL (ctx->my_ns_store), &first))
            try_make_default (ctx, &first);
        }
        else
        {
          /* We were trying to delete namespace FOO with key X
           * but when we opened FOO, we discovered that its key is Y.
           * Clearly this is not the right namespace (something changed
           * the namespace list right under us?).
           */
           /* TODO: display error message */
          GNUNET_FS_namespace_delete (ns, GNUNET_NO);
        }
      }
      else
      {
        /* We were trying to delete namespace FOO,
         * but we can't even get its key
         */
        /* TODO: display error message */
        GNUNET_FS_namespace_delete (ns, GNUNET_NO);
      }
    }
  }
  g_free (name);
  set_normal_state (user_data, selected);
}

/**
 * Called when user clicks at 'Delete' button.
 * Shows the [un]confirmation buttons, as well as the warning.
 *
 * @param button button that was clicked
 * @param user_data context
 */
void
GNUNET_GTK_namespace_organizer_namespaces_delete_namespace_button_clicked_cb (
    GtkButton *button, gpointer user_data)
{
  struct GNUNET_GTK_NamespaceSelectorContext *ctx = user_data;
  gtk_widget_set_visible (ctx->do_delete_button, TRUE);
  gtk_widget_set_visible (ctx->do_keep_button, TRUE);
  gtk_widget_set_visible (ctx->confirmation_label, TRUE);
  gtk_widget_set_sensitive (ctx->advertise_button, FALSE);
  gtk_widget_set_sensitive (ctx->delete_button, FALSE);
  gtk_widget_set_sensitive (ctx->create_button, FALSE);
}

/**
 * Generates a random ns name.
 *
 * @return new name (free with GNUNET_free()).
 */
char *
generate_local_ns_name ()
{
  const char *table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  size_t tablelen = strlen (table);
  char random_str[9];
  char *result;
  int i;
  for (i = 0; i < 8; i++)
  {
    uint32_t index = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, tablelen);
    GNUNET_assert (index < tablelen);
    random_str[i] = table[index];
  }
  random_str[8] = '\0';

  GNUNET_asprintf (&result, _("MyNamespace-%s"), random_str);
  return result;
}


/**
 * Called when key generation for a namespace is finished.
 *
 * @param cls context
 * @param ns newly-created namespace (NULL on failure)
 * @param emsg error message (NULL on success)
 */
static void
ns_created (void *cls, struct GNUNET_FS_Namespace *ns, char *name, const char *emsg)
{
  struct GNUNET_GTK_NamespaceSelectorContext *ctx = cls;

  GtkTreeIter iter;
  struct GNUNET_FS_PseudonymIdentifier *nsid;
  struct GNUNET_FS_PseudonymIdentifier id;

  /* If ns was created, get its public key hash */
  nsid = NULL;
  if ((NULL != ns) && (GNUNET_OK == GNUNET_FS_namespace_get_public_identifier (ns, &id)))
  {
    nsid = GNUNET_malloc (sizeof (struct GNUNET_FS_PseudonymIdentifier));
    memcpy (nsid, &id, sizeof (struct GNUNET_FS_PseudonymIdentifier));
  }

  /* If anything of the above went wrong, or ns creation failed, we abort */
  if (NULL == nsid)
  {
    GNUNET_FS_namespace_delete (ns, GNUNET_NO);
  }
  else
  {
    char buf[1024];
    char *ret;
    gint row_count;

    ret = GNUNET_STRINGS_data_to_string (nsid,
				         sizeof (struct GNUNET_FS_PseudonymIdentifier),
				         buf,
				         sizeof (buf));
    GNUNET_assert (NULL != ret);
    ret[0] = '\0';

    gtk_list_store_insert_with_values (ctx->my_ns_store, &iter, G_MAXINT,
        GNUNET_GTK_MY_NAMESPACES_KEY_COLUMN, buf,
        GNUNET_GTK_MY_NAMESPACES_KEY_BINARY_COLUMN, nsid,
        GNUNET_GTK_MY_NAMESPACES_NAME_COLUMN, name,
        -1);

    row_count = gtk_tree_model_iter_n_children (
        GTK_TREE_MODEL (ctx->my_ns_store), NULL);
    if (row_count == 1)
      try_make_default (ctx, &iter);
  }
}


/**
 * Create a namespace.
 * Adds a dummy to the list, starts the key generation process,
 * shows the progress spinner, disables some buttons.
 *
 * @param ctx context
 */
static void
create_namespace (struct GNUNET_GTK_NamespaceSelectorContext *ctx)
{
  gchar *name;
  struct GNUNET_FS_Namespace *ns;

  ns = NULL;
  name = NULL;
  do
  {
    if (ns)
      GNUNET_FS_namespace_delete (ns, GNUNET_NO);
    GNUNET_free_non_null (name);
    name = generate_local_ns_name ();
    ns = GNUNET_FS_namespace_open_existing (GNUNET_FS_GTK_get_fs_handle (), name);
  } while (NULL != ns);

  ns = GNUNET_FS_namespace_create (GNUNET_FS_GTK_get_fs_handle (), name);

  if (NULL == ns)
  {
    GNUNET_free (name);
    return;
  }

  ns_created (ctx, ns, name, NULL);

  GNUNET_free (name);
}

/**
 * Called when user clicks at 'Create' button.
 * Creates a namespace.
 *
 * @param button button that was clicked
 * @param user_data context
 */
void
GNUNET_GTK_namespace_organizer_namespaces_create_namespace_button_clicked_cb (
    GtkButton *button, gpointer user_data)
{
  create_namespace (user_data);
}

/**
 * Called when user toggles the radio button at a namespace in the list
 * Tries to make that namespace the new default.
 *
 * @param cell_renderer renderer of the call that was toggled.
 * @param path path to the cell that was toggled
 * @param user_data context
 */
void
GNUNET_GTK_namespace_organizer_namespaces_treeview_column_usethis_toggle_toggled_cb (
    GtkCellRendererToggle *cell_renderer, gchar *path, gpointer user_data)
{
  struct GNUNET_GTK_NamespaceSelectorContext *ctx = user_data;
  GtkTreeIter iter;
  if (gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL (ctx->my_ns_store),
      &iter, path))
    try_make_default (ctx, &iter);
}

/**
 * Called when user activates the appropriate item in main window `File' menu.
 * Opens Namespace organizer dialog.
 *
 * @param menuitem item that was activated
 * @param user_data main window context
 */
void
GNUNET_GTK_main_menu_file_organize_namespaces_activate_cb (GtkMenuItem *menuitem,
    gpointer user_data)
{
  GNUNET_GTK_open_ns_organizer (user_data);
}

/* end of gnunet-fs-gtk_namespace_organizer.c */