aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk-main_window_adv_pseudonym.c
blob: bb3265e02452b0f65677099f8918a4487a6361b7 (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
/*
     This file is part of GNUnet
     (C) 2005, 2006, 2010 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/fs/gnunet-fs-gtk-main_window_adv_pseudonym.c
 * @author Christian Grothoff
 */
#include "gnunet-fs-gtk-common.h"
#include "gnunet-fs-gtk.h"

static GtkBuilder *builder;


/**
 * Canonicalize spelling of currently selected keyword.
 */
void
GNUNET_GTK_advertise_pseudonym_keyword_list_normalize_button_clicked_cb ()
{
  GtkTreeView *tv;
  GtkTreeSelection *sel;
  GtkTreeModel *tm;
  GtkTreeIter iter;
  gchar *value;
  struct GNUNET_FS_Uri *uri;
  struct GNUNET_FS_Uri *nuri;
  char *nvalue;
  char *emsg;

  tv = GTK_TREE_VIEW (gtk_builder_get_object
                      (builder,
                       "GNUNET_GTK_advertise_pseudonym_keyword_list_tree_view"));
  sel = gtk_tree_view_get_selection (tv);
  if (TRUE != gtk_tree_selection_get_selected (sel, &tm, &iter))
  {
    GNUNET_break (0);
    return;
  }
  gtk_tree_model_get (tm, &iter, 0, &value, -1);
  emsg = NULL;
  uri = GNUNET_FS_uri_ksk_create (value, &emsg);
  if (uri == NULL)
  {
    GNUNET_break (0);
    GNUNET_free (emsg);
    g_free (value);
    return;
  }
  nuri = GNUNET_FS_uri_ksk_canonicalize (uri);
  nvalue = GNUNET_FS_uri_ksk_to_string_fancy (nuri);
  gtk_list_store_set (GTK_LIST_STORE (tm), &iter, 0, nvalue, -1);
  GNUNET_FS_uri_destroy (nuri);
  GNUNET_FS_uri_destroy (uri);
  GNUNET_free (nvalue);
  g_free (value);
}


/**
 * Remove the currently selected keyword from the list.
 */
void
GNUNET_GTK_advertise_pseudonym_keyword_list_del_button_clicked_cb ()
{
  GtkTreeView *tv;
  GtkTreeSelection *sel;
  GtkTreeModel *tm;
  GtkTreeIter iter;
  GtkWidget *w;

  tv = GTK_TREE_VIEW (gtk_builder_get_object
                      (builder,
                       "GNUNET_GTK_advertise_pseudonym_keyword_list_tree_view"));
  sel = gtk_tree_view_get_selection (tv);
  if (TRUE != gtk_tree_selection_get_selected (sel, &tm, &iter))
  {
    GNUNET_break (0);
    return;
  }
  gtk_list_store_remove (GTK_LIST_STORE (tm), &iter);
  if (TRUE != gtk_tree_model_get_iter_first (tm, &iter))
  {
    /* keyword list now empty, disable 'Ok' button! */
    w = GTK_WIDGET (gtk_builder_get_object (builder, 
					    "GNUNET_GTK_advertise_pseudonym_confirm_button"));
    gtk_widget_set_sensitive (w, FALSE);
  }
}


/**
 * Add keyword from entry line to keyword list.
 */
void
GNUNET_GTK_advertise_pseudonym_keyword_list_add_button_clicked_cb ()
{
  const char *keyword;
  GtkEntry *entry;
  GtkListStore *ls;
  GtkTreeIter iter;
  GtkWidget *w;

  ls = GTK_LIST_STORE (gtk_builder_get_object
                       (builder, "GNUNET_GTK_keyword_list_store"));
  entry =
      GTK_ENTRY (gtk_builder_get_object
                 (builder, "GNUNET_GTK_advertise_pseudonym_keyword_entry"));
  keyword = gtk_entry_get_text (entry);
  if (strlen (keyword) > 0)
  {
    gtk_list_store_insert_with_values (ls, &iter, G_MAXINT, 0, keyword, -1); 
    /* enable OK button */
    w = GTK_WIDGET (gtk_builder_get_object (builder, "GNUNET_GTK_advertise_pseudonym_confirm_button"));
    gtk_widget_set_sensitive (w, TRUE);
  }
  gtk_entry_set_text (entry, "");
}


/**
 * Update sensitivity of keyword add button.
 */
void
GNUNET_GTK_advertise_pseudonym_keyword_entry_changed_cb ()
{
  const char *keyword;
  GtkEntry *entry;
  GtkWidget *button;

  button =
      GTK_WIDGET (gtk_builder_get_object
                  (builder,
                   "GNUNET_GTK_advertise_pseudonym_keyword_list_add_button"));
  entry =
      GTK_ENTRY (gtk_builder_get_object
                 (builder, "GNUNET_GTK_advertise_pseudonym_keyword_entry"));
  keyword = gtk_entry_get_text (entry);
  gtk_widget_set_sensitive (button, (strlen (keyword) > 0) ? TRUE : FALSE);
}

/**
 * Update sensitivity of metadata delete button.
 */
static void
metadata_selection_changed_cb (GtkTreeSelection * ts, gpointer user_data)
{
  GtkTreeView *tv;
  GtkTreeSelection *sel;
  GtkWidget *button;

  tv = GTK_TREE_VIEW (gtk_builder_get_object
                      (builder,
                       "GNUNET_GTK_advertise_pseudonym_metadata_tree_view"));
  sel = gtk_tree_view_get_selection (tv);
  button =
      GTK_WIDGET (gtk_builder_get_object
                  (builder,
                   "GNUNET_GTK_advertise_pseudonym_metadata_delete_button"));
  gtk_widget_set_sensitive (button,
                            gtk_tree_selection_get_selected (sel, NULL, NULL));
}


/**
 * Update sensitivity of keyword delete and normalize button.
 */
static void
keyword_selection_changed_cb (GtkTreeSelection * ts, gpointer user_data)
{
  GtkTreeView *tv;
  GtkTreeSelection *sel;
  GtkWidget *button;

  tv = GTK_TREE_VIEW (gtk_builder_get_object
                      (builder,
                       "GNUNET_GTK_advertise_pseudonym_keyword_list_tree_view"));
  sel = gtk_tree_view_get_selection (tv);
  button =
      GTK_WIDGET (gtk_builder_get_object
                  (builder,
                   "GNUNET_GTK_advertise_pseudonym_keyword_list_del_button"));

  gtk_widget_set_sensitive (button,
                            gtk_tree_selection_get_selected (sel, NULL, NULL));
  button =
      GTK_WIDGET (gtk_builder_get_object
                  (builder,
                   "GNUNET_GTK_advertise_pseudonym_keyword_list_normalize_button"));
  gtk_widget_set_sensitive (button,
                            gtk_tree_selection_get_selected (sel, NULL, NULL));
}


/**
 * Update sensitivity of metadata add button.
 */
void
GNUNET_GTK_advertise_pseudonym_metadata_value_entry_changed_cb ()
{
  GtkTreeView *tv;
  GtkTreeSelection *sel;
  const char *value;
  GtkEntry *entry;
  GtkWidget *button;

  entry =
      GTK_ENTRY (gtk_builder_get_object
                 (builder,
                  "GNUNET_GTK_advertise_pseudonym_metadata_value_entry"));
  value = gtk_entry_get_text (entry);
  tv = GTK_TREE_VIEW (gtk_builder_get_object
                      (builder,
                       "GNUNET_GTK_advertise_pseudonym_metadata_type_tree_view"));
  sel = gtk_tree_view_get_selection (tv);
  button =
      GTK_WIDGET (gtk_builder_get_object
                  (builder,
                   "GNUNET_GTK_advertise_pseudonym_metadata_add_button"));
  gtk_widget_set_sensitive (button,
                            (strlen (value) >
                             0) ? gtk_tree_selection_get_selected (sel, NULL,
                                                                   NULL) :
                            FALSE);
}


/**
 * Add keyword to keyword list.
 */
void
GNUNET_GTK_advertise_pseudonym_keyword_entry_activate_cb ()
{
  GNUNET_GTK_advertise_pseudonym_keyword_list_add_button_clicked_cb ();
}


void
GNUNET_GTK_advertise_pseudonym_metadata_preview_file_chooser_button_file_set_cb
    ()
{
  GNUNET_break (0);
}

/**
 * Delete currently selected item from meta data list.
 */
void
GNUNET_GTK_advertise_pseudonym_metadata_delete_button_clicked_cb ()
{
  GtkTreeView *tv;
  GtkTreeSelection *sel;
  GtkTreeModel *tm;
  GtkTreeIter iter;

  tv = GTK_TREE_VIEW (gtk_builder_get_object
                      (builder,
                       "GNUNET_GTK_advertise_pseudonym_metadata_tree_view"));
  sel = gtk_tree_view_get_selection (tv);
  if (TRUE != gtk_tree_selection_get_selected (sel, &tm, &iter))
  {
    GNUNET_break (0);
    return;
  }
  gtk_list_store_remove (GTK_LIST_STORE (tm), &iter);
}


/**
 * Add metadata to metadata list.
 */
void
GNUNET_GTK_advertise_pseudonym_metadata_add_button_clicked_cb ()
{
  const char *value;
  char *avalue;
  const char *ivalue;
  GtkEntry *entry;
  GtkListStore *ls;
  GtkTreeModel *tm;
  GtkTreeView *tv;
  GtkTreeSelection *sel;
  GtkTreeIter iter;
  guint type;
  size_t slen;
  char *pos;

  entry =
      GTK_ENTRY (gtk_builder_get_object
                 (builder,
                  "GNUNET_GTK_advertise_pseudonym_metadata_value_entry"));
  value = gtk_entry_get_text (entry);
  if ((value == NULL) || (strlen (value) == 0))
  {
    GNUNET_break (0);
    return;
  }
  tv = GTK_TREE_VIEW (gtk_builder_get_object
                      (builder,
                       "GNUNET_GTK_advertise_pseudonym_metadata_type_tree_view"));
  tm = gtk_tree_view_get_model (tv);
  sel = gtk_tree_view_get_selection (tv);
  if (TRUE != gtk_tree_selection_get_selected (sel, &tm, &iter))
  {
    GNUNET_break (0);
    return;
  }
  gtk_tree_model_get (tm, &iter, 1, &type, -1);
  ls = GTK_LIST_STORE (gtk_builder_get_object
                       (builder, "GNUNET_GTK_meta_data_list_store"));
  if ((type == EXTRACTOR_METATYPE_FILENAME) &&
      (value[strlen (value) - 1] != '/'))
  {
    GNUNET_asprintf (&avalue, "%s/", value);
    /* if user typed '\' instead of '/', change it! */
    slen = strlen (avalue);
    while ((slen > 1) && (avalue[slen - 2] == '\\'))
    {
      avalue[slen - 2] = '/';
      avalue[slen - 1] = '\0';
      slen--;
    }
    while (NULL != (pos = strstr (avalue, "\\")))
      *pos = '/';
    /* remove '../' everywhere */
    while (NULL != (pos = strstr (avalue, "../")))
    {
      pos[0] = '_';
      pos[1] = '_';
      pos[2] = '_';
    }
    ivalue = avalue;
  }
  else
  {
    ivalue = value;
    avalue = NULL;
  }

  gtk_list_store_insert_with_values (ls, &iter, G_MAXINT, 0, type, 1,
                                     (guint) EXTRACTOR_METAFORMAT_UTF8, 2,
                                     EXTRACTOR_metatype_to_string (type), 3,
                                     ivalue, -1);
  GNUNET_free_non_null (avalue);
  gtk_entry_set_text (GTK_ENTRY (entry), "");
}


/**
 * Callback with information about local (!) namespaces.
 * Contains the names of the local namespace and the global
 * ID.  Adds the to the list store.
 *
 * @param cls closure, the list store.
 * @param name human-readable identifier of the namespace
 * @param id hash identifier for the namespace
 */
static void
add_to_list (void *cls, const char *name, const GNUNET_HashCode * id)
{
  GtkListStore *ls = cls;
  GtkTreeIter iter;

  gtk_list_store_insert_with_values (ls, &iter, -1, 0, name, 1,
                                     GNUNET_FS_namespace_create
                                     (GNUNET_FS_GTK_get_fs_handle (), name),
                                     -1);

}


static void
selection_changed_cb (GtkTreeSelection * ts, gpointer user_data)
{
  GtkTreeIter iter;
  GtkWidget *ok_button;

  ok_button =
      GTK_WIDGET (gtk_builder_get_object
                  (builder, "GNUNET_GTK_select_pseudonym_ok_button"));
  if (TRUE == gtk_tree_selection_get_selected (ts, NULL, &iter))
    gtk_widget_set_sensitive (ok_button, TRUE);
  else
    gtk_widget_set_sensitive (ok_button, FALSE);
}


/**
 * Advertise pseudonym selected.
 */
void
GNUNET_GTK_main_menu_file_advertise_pseudonym_activate_cb (GtkWidget * dummy,
                                                           gpointer data)
{
  GtkWidget *ad;
  struct GNUNET_FS_Namespace *ns;
  struct GNUNET_FS_Namespace *nso;
  GtkListStore *ls;
  GtkTreeView *tv;
  GtkTreeIter iter;
  GtkTreeSelection *sel;
  GtkTreeModel *tm;
  GtkSpinButton *sb;
  struct GNUNET_FS_BlockOptions bo;
  struct GNUNET_FS_Uri *nxt;
  struct GNUNET_FS_Uri *mrg;
  struct GNUNET_FS_Uri *uri;
  struct GNUNET_CONTAINER_MetaData *nm;
  const char *root;
  char *value;
  guint ntype;
  guint nformat;
  guint type;
  guint max_type;

  builder =
      GNUNET_GTK_get_new_builder
      ("gnunet_fs_gtk_select_pseudonym_dialog.glade");
  if (builder == NULL)
  {
    GNUNET_break (0);
    return;
  }
  ad = GTK_WIDGET (gtk_builder_get_object
                   (builder, "GNUNET_GTK_select_pseudonym_dialog"));
  ls = GTK_LIST_STORE (gtk_builder_get_object
                       (builder, "GNUNET_GTK_select_pseudonym_liststore"));
  GNUNET_FS_namespace_list (GNUNET_FS_GTK_get_fs_handle (), &add_to_list, ls);
  tv = GTK_TREE_VIEW (gtk_builder_get_object
                      (builder, "GNUNET_GTK_select_pseudonym_tree_view"));
  sel = gtk_tree_view_get_selection (tv);
  g_signal_connect (G_OBJECT (sel), "changed",
                    G_CALLBACK (selection_changed_cb), NULL);
  if (GTK_RESPONSE_OK != gtk_dialog_run (GTK_DIALOG (ad)))
  {
    gtk_widget_destroy (ad);
    g_object_unref (G_OBJECT (builder));
    builder = NULL;
    return;
  }
  GNUNET_assert (TRUE == gtk_tree_selection_get_selected (sel, &tm, &iter));
  gtk_tree_model_get (tm, &iter, 1, &ns, -1);
  /* free all namespaces from list store except "ns" */
  if (TRUE == gtk_tree_model_get_iter_first (tm, &iter))
  {
    do
    {
      gtk_tree_model_get (tm, &iter, 1, &nso, -1);
      if (ns != nso)
        GNUNET_FS_namespace_delete (nso, GNUNET_NO);
    }
    while (TRUE == gtk_tree_model_iter_next (tm, &iter));
  }
  gtk_widget_destroy (ad);
  g_object_unref (G_OBJECT (builder));
  builder = NULL;


  builder =
      GNUNET_GTK_get_new_builder
      ("gnunet_fs_gtk_advertise_pseudonym_dialog.glade");
  if (builder == NULL)
  {
    GNUNET_break (0);
    return;
  }
  GNUNET_FS_GTK_setup_expiration_year_adjustment (builder);
  ad = GTK_WIDGET (gtk_builder_get_object
                   (builder, "GNUNET_GTK_advertise_pseudonym_dialog"));
  ls = GTK_LIST_STORE (gtk_builder_get_object
                       (builder, "GNUNET_GTK_metatype_list_store"));
  max_type = EXTRACTOR_metatype_get_max ();
  type = 1;
  while (type < max_type - 1)
  {
    gtk_list_store_insert_with_values (ls, &iter, G_MAXINT, 0,
                                       EXTRACTOR_metatype_to_string (type), 1,
                                       type, 2,
                                       EXTRACTOR_metatype_to_description (type),
                                       -1);
    type++;
  }
  tv = GTK_TREE_VIEW (gtk_builder_get_object
                      (builder,
                       "GNUNET_GTK_advertise_pseudonym_metadata_type_tree_view"));
  sel = gtk_tree_view_get_selection (tv);
  g_signal_connect (G_OBJECT (sel), "changed",
                    G_CALLBACK
                    (GNUNET_GTK_advertise_pseudonym_metadata_value_entry_changed_cb),
                    NULL);

  tv = GTK_TREE_VIEW (gtk_builder_get_object
                      (builder,
                       "GNUNET_GTK_advertise_pseudonym_metadata_tree_view"));
  sel = gtk_tree_view_get_selection (tv);
  g_signal_connect (G_OBJECT (sel), "changed",
                    G_CALLBACK (metadata_selection_changed_cb), NULL);

  tv = GTK_TREE_VIEW (gtk_builder_get_object
                      (builder,
                       "GNUNET_GTK_advertise_pseudonym_keyword_list_tree_view"));
  sel = gtk_tree_view_get_selection (tv);
  g_signal_connect (G_OBJECT (sel), "changed",
                    G_CALLBACK (keyword_selection_changed_cb), NULL);
  if (GTK_RESPONSE_OK != gtk_dialog_run (GTK_DIALOG (ad)))
  {
    gtk_widget_destroy (ad);
    g_object_unref (G_OBJECT (builder));
    builder = NULL;
    return;
  }
  /* get keywords from list store */
  nxt = NULL;
  mrg = NULL;
  uri = NULL;
  tm = GTK_TREE_MODEL (gtk_builder_get_object
                       (builder, "GNUNET_GTK_keyword_list_store"));
  if (TRUE == gtk_tree_model_get_iter_first (tm, &iter))
  {
    do
    {
      gtk_tree_model_get (tm, &iter, 0, &value, -1);
      nxt = GNUNET_FS_uri_ksk_create_from_args (1, (const char **) &value);
      mrg = GNUNET_FS_uri_ksk_merge (nxt, uri);
      GNUNET_FS_uri_destroy (nxt);
      if (NULL != uri)
        GNUNET_FS_uri_destroy (uri);
      uri = mrg;
      g_free (value);
    }
    while (TRUE == gtk_tree_model_iter_next (tm, &iter));
  }
  /* get meta */
  nm = GNUNET_CONTAINER_meta_data_create ();
  tm = GTK_TREE_MODEL (gtk_builder_get_object
                       (builder, "GNUNET_GTK_meta_data_list_store"));
  if (TRUE == gtk_tree_model_get_iter_first (tm, &iter))
  {
    do
    {
      gtk_tree_model_get (tm, &iter, 0, &ntype, 1, &nformat, 3, &value, -1);
      GNUNET_CONTAINER_meta_data_insert (nm, "<user>", ntype, nformat,
                                         "text/plain", value,
                                         strlen (value) + 1);
      g_free (value);
    }
    while (TRUE == gtk_tree_model_iter_next (tm, &iter));
  }
  bo.anonymity_level =
      gtk_spin_button_get_value (GTK_SPIN_BUTTON
                                 (gtk_builder_get_object
                                  (builder,
                                   "GNUNET_GTK_advertise_pseudonym_anonymity_spin_button")));
  bo.content_priority =
      gtk_spin_button_get_value (GTK_SPIN_BUTTON
                                 (gtk_builder_get_object
                                  (builder,
                                   "GNUNET_GTK_advertise_pseudonym_priority_spin_button")));
  sb = GTK_SPIN_BUTTON (gtk_builder_get_object
                        (builder,
                         "GNUNET_GTK_advertise_pseudonym_expiration_year_spin_button"));
  bo.expiration_time = GNUNET_FS_GTK_get_expiration_time (sb);
  root =
      gtk_entry_get_text (GTK_ENTRY
                          (gtk_builder_get_object
                           (builder,
                            "GNUNET_GTK_advertise_pseudonym_root_entry")));
  GNUNET_FS_namespace_advertise (GNUNET_FS_GTK_get_fs_handle (), uri, ns, nm,
                                 &bo, root, NULL, NULL);
  GNUNET_FS_namespace_delete (ns, GNUNET_NO);
  GNUNET_CONTAINER_meta_data_destroy (nm);
  if (NULL != uri)
    GNUNET_FS_uri_destroy (uri);
  gtk_widget_destroy (ad);
  g_object_unref (G_OBJECT (builder));
  builder = NULL;
}


/* end of gnunet-fs-gtk-main_window_adv_pseudonym.c */