aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk-edit_publish_dialog.c
blob: d787bff8e3e0801430e5e37533d461bc2e5902bc (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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
/*
     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-edit_publish_dialog.c
 * @author Christian Grothoff
 */
#include "gnunet-fs-gtk-common.h"
#include "gnunet-fs-gtk-edit_publish_dialog.h"
#include "gnunet-fs-gtk.h"
#include <gnunet/gnunet_util_lib.h>

#include "metatypes.c"

struct EditPublicationDialogContext
{
  GtkBuilder *builder;

  GtkListStore *metatypes_liststore;

  GtkListStore *meta_liststore;
  GtkTreeView *meta_treeview;
  GtkTreeSelection *meta_selection;
  GtkButton *meta_add_button;
  GtkWidget *meta_del_button;

  GtkCellRendererCombo *meta_combo;

  GtkListStore *pubtypes_liststore;
  GtkComboBox *pubtypes_combo;

  GtkListStore *keywords_liststore;
  GtkTreeView *keywords_treeview;
  GtkTreeSelection *keywords_selection;
  GtkWidget *keyword_add_button;
  GtkWidget *keyword_del_button;

  GtkEntry *keyword_entry;

  GObject *pubwindow;

  GtkWidget *ok;
  GtkButton *cancel;

  GtkImage *image;

  GtkToggleButton *index_checkbutton;
  GtkLabel *index_label;
  GtkEntry *root_entry;
  GtkLabel *root_label;

  GtkSpinButton *priority_spin;
  GtkSpinButton *year_spin;
  GtkSpinButton *replication_spin;

  GtkComboBox *anonymity_combo;
  GtkListStore *anonymity_liststore;

  int do_index;
  char *short_fn;
  struct GNUNET_FS_BlockOptions bo;
  struct GNUNET_FS_FileInformation *fip;
  gint preview_changed;
  gboolean allow_no_keywords;
  gboolean is_directory;
  GNUNET_FS_GTK_EditPublishDialogCallback cb;
  gchar *root;
  gpointer cls;

  struct GNUNET_CONTAINER_MetaData *md;

  GtkTreeIter *meta_combo_selected_iter;
};

static void
change_metatypes (struct EditPublicationDialogContext *ctx, gint code)
{
  gint pub_type = 0, i;
  gint pubtype_count = 0;
  gint max_type;
  GtkTreeIter iter;

  for (pub_type = 0; types[pub_type] != NULL; pub_type++)
    pubtype_count += 1;

  if (code < pubtype_count)
    pub_type = code;
  else
    pub_type = 0;

  gtk_list_store_clear (ctx->metatypes_liststore);
  max_type = EXTRACTOR_metatype_get_max ();
  for (i = 0; types[pub_type][i] != EXTRACTOR_METATYPE_RESERVED; i++)
  {
    if (types[pub_type][i] < max_type && types[pub_type][i] > 0)
      gtk_list_store_insert_with_values (ctx->metatypes_liststore, &iter, G_MAXINT, 0,
                                         types[pub_type][i], 1,
                                         EXTRACTOR_METAFORMAT_UTF8, 2,
                                         EXTRACTOR_metatype_to_string (types
                                                                       [pub_type]
                                                                       [i]), 3,
                                         EXTRACTOR_metatype_to_description
                                         (types[pub_type][i]), -1);
  }
}


void
GNUNET_GTK_edit_publication_type_combo_changed_cb (GtkComboBox * widget,
                                                   struct EditPublicationDialogContext *ctx)
{
  GtkTreeIter iter;
  gint code;

  if (!gtk_combo_box_get_active_iter (widget, &iter))
    return;

  gtk_tree_model_get (GTK_TREE_MODEL (ctx->pubtypes_liststore), &iter, 0, &code, -1);

  change_metatypes (ctx, code);
}


static void
metadata_selection_changed_cb (GtkTreeSelection *ts, struct EditPublicationDialogContext *ctx)
{
  gtk_widget_set_sensitive (ctx->meta_del_button,
                            gtk_tree_selection_get_selected (ts, NULL, NULL));
}


static void
keywords_selection_changed_cb (GtkTreeSelection *ts, struct EditPublicationDialogContext *ctx)
{
  gtk_widget_set_sensitive (ctx->keyword_del_button,
                            gtk_tree_selection_get_selected (ts, NULL, NULL));
}


void
GNUNET_GTK_edit_publication_add_button_clicked_cb (GtkButton * button,
                                                   struct EditPublicationDialogContext *ctx)
{
  GtkTreeIter iter;

  gtk_list_store_insert (ctx->meta_liststore, &iter, 0);
  /* type == 0 means "not set" */
  gtk_list_store_set (ctx->meta_liststore, &iter, 0, 0, 1, EXTRACTOR_METAFORMAT_UTF8, 2,
                      _("Select a type"), 3, _("Specify a value"), 4, NULL, -1);
}


gboolean
GNUNET_GTK_edit_publication_keyword_entry_key_press_event_cb (GtkWidget *
                                                              widget,
                                                              GdkEventKey *
                                                              event,
                                                              struct EditPublicationDialogContext *ctx)
{
  if (event->keyval == GDK_KEY_Return)
  {
    GNUNET_GTK_edit_publication_add_button_clicked_cb (GTK_BUTTON (ctx->keyword_add_button), ctx);
    return TRUE;
  }
  return FALSE;
}


void GNUNET_GTK_edit_publication_metadata_tree_view_type_renderer_edited_cb
    (GtkCellRendererText * renderer, gchar * path, gchar * new_text,
     struct EditPublicationDialogContext *ctx)
{
  GtkTreeModel *combo_model;
  GtkTreeIter iter;
  gint type_id;
  gchar *description = NULL;

  if (!gtk_tree_model_get_iter_from_string
      (GTK_TREE_MODEL (ctx->meta_liststore), &iter, path))
    return;

  if (!ctx->meta_combo_selected_iter)
    return;
  g_object_get (ctx->meta_combo, "model", &combo_model, NULL);
  gtk_tree_model_get (combo_model, ctx->meta_combo_selected_iter, 0, &type_id, 2, &description, -1);
  g_object_unref (combo_model);
  g_free (ctx->meta_combo_selected_iter);
  ctx->meta_combo_selected_iter = NULL;

  gtk_list_store_set (ctx->meta_liststore, &iter, 0, type_id, 1,
                      EXTRACTOR_METAFORMAT_UTF8, 2, new_text, 4, description,
                      -1);
  g_free (description);
}


void GNUNET_GTK_edit_publication_metadata_tree_view_type_renderer_changed_cb
    (GtkCellRendererCombo * combo, gchar * path_string, GtkTreeIter * new_iter,
     struct EditPublicationDialogContext *ctx)
{
  if (ctx->meta_combo_selected_iter)
    g_free (ctx->meta_combo_selected_iter);
  ctx->meta_combo_selected_iter = g_new0 (GtkTreeIter, 1);
  *ctx->meta_combo_selected_iter = *new_iter;
}


void GNUNET_GTK_edit_publication_metadata_tree_view_value_renderer_edited_cb
    (GtkCellRendererText * renderer, gchar * path, gchar * new_text,
     struct EditPublicationDialogContext *ctx)
{
  GtkTreeIter iter;
  gint metatype;
  char *avalue;
  const char *ivalue;
  size_t slen;
  char *pos;

  if (!gtk_tree_model_get_iter_from_string
      (GTK_TREE_MODEL (ctx->meta_liststore), &iter, path))
    return;

  gtk_tree_model_get (GTK_TREE_MODEL (ctx->meta_liststore), &iter, 0, &metatype, -1);
  if (metatype == EXTRACTOR_METATYPE_FILENAME &&
      new_text[strlen (new_text) - 1] != '/' && ctx->is_directory)
  {
    GNUNET_asprintf (&avalue, "%s/", new_text);
    /* 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 = new_text;
    avalue = NULL;
  }

  gtk_list_store_set (ctx->meta_liststore, &iter, 3, ivalue, -1);
  GNUNET_free_non_null (avalue);
}


void
GNUNET_GTK_edit_publication_delete_button_clicked_cb (GtkButton * button,
                                                      struct EditPublicationDialogContext *ctx)
{
  GtkTreeIter iter;

  if (TRUE != gtk_tree_selection_get_selected (ctx->meta_selection, NULL, &iter))
  {
    GNUNET_break (0);
    return;
  }
  if (gtk_list_store_remove (ctx->meta_liststore, &iter))
    gtk_tree_selection_select_iter (ctx->meta_selection, &iter);
}


void
GNUNET_GTK_edit_publication_keyword_list_add_button_clicked_cb (GtkButton *
                                                                button,
                                                                struct EditPublicationDialogContext *ctx)
{
  const char *keyword;
  GtkTreeIter iter;

  keyword = gtk_entry_get_text (ctx->keyword_entry);
  if (strlen (keyword) > 0)
  {
    gtk_list_store_insert_with_values (ctx->keywords_liststore, &iter, G_MAXINT, 0, keyword, 1, TRUE,
                                       -1);
    gtk_widget_set_sensitive (ctx->ok, TRUE);
  }
  gtk_entry_set_text (ctx->keyword_entry, "");
}


static gboolean
gtk_tree_model_has_items_cb (GtkTreeModel * model, GtkTreePath * path,
                             GtkTreeIter * iter, gpointer data)
{
  gboolean *val = (gboolean *) data;

  *val = TRUE;
  return TRUE;
}


static gboolean
gtk_tree_model_has_items (GtkTreeModel * model)
{
  gboolean b = FALSE;

  gtk_tree_model_foreach (model, &gtk_tree_model_has_items_cb, &b);
  return b;
}


void
GNUNET_GTK_edit_publication_keyword_entry_changed_cb (GtkEditable * editable,
                                                      struct EditPublicationDialogContext *ctx)
{
  const char *keyword;

  keyword = gtk_entry_get_text (ctx->keyword_entry);
  gtk_widget_set_sensitive (ctx->keyword_add_button, (strlen (keyword) > 0) ? TRUE : FALSE);
}


void
GNUNET_GTK_edit_publication_keyword_list_del_button_clicked_cb (GtkButton *
                                                                button,
                                                                struct EditPublicationDialogContext *ctx)
{
  GtkTreeIter iter;

  if (TRUE != gtk_tree_selection_get_selected (ctx->keywords_selection, NULL, &iter))
  {
    GNUNET_break (0);
    return;
  }
  if (gtk_list_store_remove (GTK_LIST_STORE (ctx->keywords_liststore), &iter))
    gtk_tree_selection_select_iter (ctx->keywords_selection, &iter);

  if (!ctx->allow_no_keywords && !gtk_tree_model_has_items (GTK_TREE_MODEL (ctx->keywords_liststore)))
    gtk_widget_set_sensitive (ctx->ok, FALSE);
}


void GNUNET_GTK_edit_publication_metadata_preview_file_chooser_button_file_set_cb (
    GtkFileChooserButton * widget, struct EditPublicationDialogContext *ctx)
{
  gchar *fn;

  fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (widget));
  gtk_image_set_from_file (ctx->image, fn);
  g_free (fn);
  ctx->preview_changed = GNUNET_YES;
}


/**
 * Copy binary meta data from to the new container and also
 * preserve all entries that were not changed.
 *
 * @param cls closure, a 'struct FileInformationUpdateContext'
 * @param plugin_name name of the plugin that produced this value;
 *        special values can be used (i.e. '<zlib>' for zlib being
 *        used in the main libextractor library and yielding
 *        meta data).
 * @param type libextractor-type describing the meta data
 * @param format basic format information about data
 * @param data_mime_type mime-type of data (not of the original file);
 *        can be NULL (if mime-type is not known)
 * @param data actual meta-data found
 * @param data_len number of bytes in data
 * @return 0 to continue extracting
 */
static int
preserve_meta_items (void *cls, const char *plugin_name,
                     enum EXTRACTOR_MetaType type,
                     enum EXTRACTOR_MetaFormat format,
                     const char *data_mime_type, const char *data,
                     size_t data_len)
{
  struct EditPublicationDialogContext *ctx = cls;
  GtkTreeIter iter;
  gchar *value;
  guint ntype;
  guint nformat;
  int keep;

  keep = GNUNET_NO;
  switch (format)
  {
  case EXTRACTOR_METAFORMAT_UTF8:
  case EXTRACTOR_METAFORMAT_C_STRING:
    if (TRUE == gtk_tree_model_get_iter_first (GTK_TREE_MODEL (ctx->meta_liststore), &iter))
    {
      do
      {
        gtk_tree_model_get (GTK_TREE_MODEL (ctx->meta_liststore), &iter, 0, &ntype, 1, &nformat, 3, &value, -1);
        if ((ntype == type) && (nformat == format) &&
            (0 == strcmp (value, data)))
        {
          gtk_list_store_remove (ctx->meta_liststore, &iter);
          keep = GNUNET_YES;
          g_free (value);
          break;
        }
        g_free (value);
      }
      while (TRUE == gtk_tree_model_iter_next (GTK_TREE_MODEL (ctx->meta_liststore), &iter));
    }
    break;
  case EXTRACTOR_METAFORMAT_UNKNOWN:
    break;
  case EXTRACTOR_METAFORMAT_BINARY:
    if (ctx->preview_changed == GNUNET_NO)
      keep = GNUNET_YES;
    break;
  default:
    GNUNET_break (0);
    break;
  }
  if (GNUNET_YES == keep)
    GNUNET_break (GNUNET_OK ==
                  GNUNET_CONTAINER_meta_data_insert (ctx->md, plugin_name, type,
                                                     format, data_mime_type,
                                                     data, data_len));
  return 0;
}


/**
 * Function called to update the information in FI.
 *
 * @param cls closure with a 'struct FileInformationUpdateContext *'
 * @param fi the entry in the publish-structure
 * @param length length of the file or directory
 * @param meta metadata for the file or directory (can be modified)
 * @param uri pointer to the keywords that will be used for this entry (can be modified)
 * @param bo block options (can be modified)
 * @param do_index should we index (can be modified)
 * @param client_info pointer to client context set upon creation (can be modified)
 * @return GNUNET_SYSERR (aborts after first call)
 */
static int
file_information_update (void *cls, struct GNUNET_FS_FileInformation *fi,
                         uint64_t length,
                         struct GNUNET_CONTAINER_MetaData *meta,
                         struct GNUNET_FS_Uri **uri,
                         struct GNUNET_FS_BlockOptions *bo, int *do_index,
                         void **client_info)
{
  struct EditPublicationDialogContext *ctx = cls;
  GtkTreeIter iter;
  struct GNUNET_FS_Uri *nxt;
  struct GNUNET_FS_Uri *mrg;
  gchar *value;
  guint ntype;
  guint nformat;
  gchar *fn;
  char *data;
  gsize data_size;
  const char *mime;
  GFile *f;
  GFileInfo *finfo;

  if (!GNUNET_GTK_get_selected_anonymity_combo_level
      (ctx->anonymity_combo, &ctx->bo.anonymity_level))
    return GNUNET_SYSERR;
  ctx->bo.content_priority = gtk_spin_button_get_value (ctx->priority_spin);
  ctx->bo.replication_level = gtk_spin_button_get_value (ctx->replication_spin);
  *do_index = gtk_toggle_button_get_active (ctx->index_checkbutton);
  ctx->bo.expiration_time = GNUNET_FS_GTK_get_expiration_time (ctx->year_spin);

  if (!ctx->allow_no_keywords)
  {
    g_free (ctx->root);
    ctx->root = g_strdup (gtk_entry_get_text (ctx->root_entry));
  }
  /* update URI */
  if (NULL != (*uri))
    GNUNET_FS_uri_destroy (*uri);
  *uri = NULL;
  nxt = NULL;
  mrg = NULL;

  if (TRUE == gtk_tree_model_get_iter_first (GTK_TREE_MODEL (ctx->keywords_liststore), &iter))
  {
    do
    {
      gtk_tree_model_get (GTK_TREE_MODEL (ctx->keywords_liststore), &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 (GTK_TREE_MODEL (ctx->keywords_liststore), &iter));
  }

  /* update meta */
  ctx->md = GNUNET_CONTAINER_meta_data_create ();
  GNUNET_CONTAINER_meta_data_iterate (meta, &preserve_meta_items, ctx);

  GNUNET_CONTAINER_meta_data_clear (meta);
  if (TRUE == gtk_tree_model_get_iter_first (GTK_TREE_MODEL (ctx->meta_liststore), &iter))
  {
    do
    {
      gtk_tree_model_get (GTK_TREE_MODEL (ctx->meta_liststore), &iter, 0, &ntype, 1, &nformat, 3, &value, -1);
      if (ntype > 0)
        GNUNET_CONTAINER_meta_data_insert (ctx->md, "<user>", ntype, nformat,
                                           "text/plain", value,
                                           strlen (value) + 1);
      g_free (value);
    }
    while (TRUE == gtk_tree_model_iter_next (GTK_TREE_MODEL (ctx->meta_liststore), &iter));
  }
  GNUNET_CONTAINER_meta_data_merge (meta, ctx->md);
  if (ctx->preview_changed == GNUNET_YES)
  {
    fn = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER
                                        (gtk_builder_get_object
                                         (ctx->builder,
                                          "GNUNET_GTK_edit_publication_metadata_preview_file_chooser_button")));
    f = g_file_new_for_path (fn);
    finfo =
        g_file_query_info (f, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0, NULL,
                           NULL);
    if (FALSE == g_file_load_contents (f, NULL, &data, &data_size, NULL, NULL))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                  _("Could not load preview `%s' into memory\n"), fn);
    }
    else
    {
      mime =
          g_file_info_get_attribute_string (finfo,
                                            G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
      GNUNET_CONTAINER_meta_data_insert (meta, "<user>",
                                         EXTRACTOR_METATYPE_THUMBNAIL,
                                         EXTRACTOR_METAFORMAT_BINARY, mime,
                                         data, data_size);
    }
    g_object_unref (finfo);
    g_object_unref (f);
    g_free (fn);
  }
  GNUNET_CONTAINER_meta_data_destroy (ctx->md);

  /* update short_fn */
  GNUNET_free_non_null (ctx->short_fn);
  ctx->short_fn =
      GNUNET_CONTAINER_meta_data_get_first_by_types (meta,
                                                     EXTRACTOR_METATYPE_FILENAME,
                                                     -1);
  return GNUNET_SYSERR;         /* only visit top-level item */
}


void
GNUNET_GTK_edit_publication_cancel_button_clicked_cb (GtkButton * button,
                                                      struct EditPublicationDialogContext *ctx)
{
  ctx->cb (ctx->cls, ctx->do_index, ctx->short_fn,
      &ctx->bo, NULL, GTK_RESPONSE_CANCEL);
  gtk_widget_destroy (GTK_WIDGET (ctx->pubwindow));
}


void
GNUNET_GTK_edit_publication_confirm_button_clicked_cb (GtkButton * button,
                                                       struct EditPublicationDialogContext *ctx)
{
  gint year;

  GNUNET_FS_file_information_inspect (ctx->fip, &file_information_update, ctx);
  if (!GNUNET_GTK_get_selected_anonymity_combo_level (ctx->anonymity_combo,
      &ctx->bo.anonymity_level))
    ctx->bo.content_priority = gtk_spin_button_get_value (ctx->priority_spin);
  ctx->bo.replication_level = gtk_spin_button_get_value (ctx->replication_spin);

  ctx->do_index = gtk_toggle_button_get_active (ctx->index_checkbutton);
  year = gtk_spin_button_get_value (ctx->year_spin);
  ctx->bo.expiration_time = GNUNET_FS_year_to_time (year);
  ctx->cb (ctx->cls, ctx->do_index, ctx->short_fn,
      &ctx->bo, ctx->root, GTK_RESPONSE_OK);
  GNUNET_free_non_null (ctx->short_fn);
  gtk_widget_destroy (GTK_WIDGET (ctx->pubwindow));
}


gboolean
GNUNET_GTK_edit_publication_window_delete_event_cb (GtkWidget * widget,
                                                    GdkEvent * event,
                                                    struct EditPublicationDialogContext *ctx)
{
  GNUNET_GTK_edit_publication_cancel_button_clicked_cb (ctx->cancel, ctx);
  return TRUE;
}

/**
 * Add each of the keywords to the keyword list store.
 *
 * @param cls closure
 * @param keyword the keyword
 * @param is_mandatory is the keyword mandatory (in a search)
 * @return GNUNET_OK to continue to iterate
 */
static int
add_keyword (void *cls, const char *keyword, int is_mandatory)
{
  GtkListStore *ls;
  GtkTreeIter iter;

  ls = GTK_LIST_STORE (cls);
  gtk_list_store_insert_with_values (ls, &iter, G_MAXINT, 0, keyword, 1, FALSE,
                                     -1);
  return GNUNET_OK;
}


/**
 * Function called to extract the information from FI.
 *
 * @param cls closure
 * @param fi the entry in the publish-structure
 * @param length length of the file or directory
 * @param meta metadata for the file or directory (can be modified)
 * @param uri pointer to the keywords that will be used for this entry (can be modified)
 * @param bo block options
 * @param do_index should we index (can be modified)
 * @param client_info pointer to client context set upon creation (can be modified)
 * @return GNUNET_SYSERR (aborts after first call)
 */
static int
file_information_extract (void *cls, struct GNUNET_FS_FileInformation *fi,
                          uint64_t length,
                          struct GNUNET_CONTAINER_MetaData *meta,
                          struct GNUNET_FS_Uri **uri,
                          struct GNUNET_FS_BlockOptions *bo, int *do_index,
                          void **client_info)
{
  struct EditPublicationDialogContext *ctx = cls;
  GdkPixbuf *pixbuf;
  int year;

  if (NULL != *uri)
    GNUNET_FS_uri_ksk_get_keywords (*uri, &add_keyword, ctx->keywords_liststore);
  if (NULL != meta)
  {
    GNUNET_CONTAINER_meta_data_iterate (meta,
                                        &GNUNET_FS_GTK_add_meta_data_to_list_store,
                                        ctx->meta_liststore);
    pixbuf = GNUNET_FS_GTK_get_thumbnail_from_meta_data (meta);
    if (pixbuf != NULL)
    {
      gtk_image_set_from_pixbuf (ctx->image, pixbuf);
    }
  }
  year = (int) GNUNET_FS_time_to_year (ctx->bo.expiration_time);
  gtk_spin_button_set_value (ctx->year_spin, year);
  GNUNET_GTK_select_anonymity_combo_level (ctx->anonymity_combo,
      ctx->bo.anonymity_level);
  gtk_spin_button_set_value (ctx->priority_spin, ctx->bo.content_priority);
  gtk_spin_button_set_value (ctx->replication_spin, ctx->bo.replication_level);
  gtk_toggle_button_set_active (ctx->index_checkbutton, *do_index);
  return GNUNET_SYSERR;         /* only visit top-level item */
}


void
GNUNET_GTK_edit_publication_window_realize_cb (GtkWidget *widget, struct EditPublicationDialogContext *ctx)
{
  ctx->keywords_selection = gtk_tree_view_get_selection (ctx->keywords_treeview);
  ctx->meta_selection = gtk_tree_view_get_selection (ctx->meta_treeview);

  g_signal_connect (G_OBJECT (ctx->keywords_selection), "changed",
                    G_CALLBACK (keywords_selection_changed_cb), ctx);

  g_signal_connect (G_OBJECT (ctx->meta_selection), "changed",
                    G_CALLBACK (metadata_selection_changed_cb), ctx);
}

/**
 * Open the dialog to edit file information data.
 * short_fn MUST be UTF-8-encoded
 */
void
GNUNET_FS_GTK_edit_publish_dialog (GtkWindow * parent,
                                   int do_index, const char *short_fn,
				   const struct GNUNET_FS_BlockOptions bo,
                                   struct GNUNET_FS_FileInformation *fip,
                                   gboolean allow_no_keywords,
                                   GtkListStore *anon_liststore,
                                   GNUNET_FS_GTK_EditPublishDialogCallback cb,
                                   gpointer cls)
{
  GtkTreeIter iter;
  gint code;

  struct EditPublicationDialogContext *ctx;

  ctx = GNUNET_malloc (sizeof (struct EditPublicationDialogContext));
  ctx->builder = GNUNET_GTK_get_new_builder ("gnunet_fs_gtk_edit_publication.glade", ctx);

  if (ctx->builder == NULL)
  {
    GNUNET_free (ctx);
    return;
  }

  ctx->pubtypes_liststore =
      GTK_LIST_STORE (gtk_builder_get_object
                      (ctx->builder, "GNUNET_GTK_publication_types_liststore"));
  ctx->metatypes_liststore =
      GTK_LIST_STORE (gtk_builder_get_object
                      (ctx->builder,
                       "GNUNET_GTK_publication_metadata_types_liststore"));
  ctx->meta_treeview = GTK_TREE_VIEW (gtk_builder_get_object
                      (ctx->builder,
                       "GNUNET_GTK_edit_publication_metadata_tree_view"));
  ctx->meta_del_button =
      GTK_WIDGET (gtk_builder_get_object
                  (ctx->builder, "GNUNET_GTK_edit_publication_delete_button"));
  ctx->keywords_treeview = GTK_TREE_VIEW (gtk_builder_get_object
                      (ctx->builder,
                       "GNUNET_GTK_edit_publication_keyword_list_tree_view"));
  ctx->keyword_del_button =
      GTK_WIDGET (gtk_builder_get_object
                  (ctx->builder,
                   "GNUNET_GTK_edit_publication_keyword_list_del_button"));
  ctx->meta_add_button =
        GTK_BUTTON (gtk_builder_get_object
                    (ctx->builder, "GNUNET_GTK_edit_publication_add_button"));
  ctx->meta_combo =
      GTK_CELL_RENDERER_COMBO (gtk_builder_get_object
                               (ctx->builder,
                                "GNUNET_GTK_edit_publication_metadata_tree_view_type_renderer"));
  ctx->pubwindow =
      gtk_builder_get_object (ctx->builder, "GNUNET_GTK_edit_publication_window");
  ctx->keywords_liststore = GTK_LIST_STORE (gtk_builder_get_object
                       (ctx->builder, "GNUNET_GTK_publication_keywords_liststore"));
  ctx->keyword_entry =
      GTK_ENTRY (gtk_builder_get_object
                 (ctx->builder, "GNUNET_GTK_edit_publication_keyword_entry"));
  ctx->ok = GTK_WIDGET (gtk_builder_get_object
                     (ctx->builder, "GNUNET_GTK_edit_publication_confirm_button"));
  ctx->keyword_add_button =
      GTK_WIDGET (gtk_builder_get_object
                  (ctx->builder,
                   "GNUNET_GTK_edit_publication_keyword_list_add_button"));
  ctx->image =
      GTK_IMAGE (gtk_builder_get_object
                 (ctx->builder,
                  "GNUNET_GTK_edit_publication_metadata_preview_image"));
  ctx->meta_liststore = GTK_LIST_STORE (gtk_builder_get_object
                         (ctx->builder,
                          "GNUNET_GTK_publication_metadata_liststore"));
  ctx->cancel =
      GTK_BUTTON (gtk_builder_get_object
                  (ctx->builder, "GNUNET_GTK_edit_publication_cancel_button"));

  ctx->index_label = GTK_LABEL (gtk_builder_get_object
                    (ctx->builder, "GNUNET_GTK_edit_publication_index_label"));
  ctx->root_entry = GTK_ENTRY (gtk_builder_get_object
                    (ctx->builder, "GNUNET_GTK_edit_publication_root_entry"));
  ctx->root_label = GTK_LABEL (gtk_builder_get_object
                    (ctx->builder, "GNUNET_GTK_edit_publication_root_label"));

  ctx->pubtypes_combo =
      GTK_COMBO_BOX (gtk_builder_get_object
                     (ctx->builder, "GNUNET_GTK_edit_publication_type_combo"));

  ctx->year_spin = GTK_SPIN_BUTTON
                             (gtk_builder_get_object
                              (ctx->builder,
                               "GNUNET_GTK_edit_publication_expiration_year_spin_button"));
  ctx->priority_spin = GTK_SPIN_BUTTON
                             (gtk_builder_get_object
                              (ctx->builder,
                               "GNUNET_GTK_edit_publication_priority_spin_button"));
  ctx->replication_spin = GTK_SPIN_BUTTON
                             (gtk_builder_get_object
                              (ctx->builder,
                               "GNUNET_GTK_edit_publication_replication_spin_button"));
  ctx->index_checkbutton = GTK_TOGGLE_BUTTON
                                (gtk_builder_get_object
                                 (ctx->builder,
                                  "GNUNET_GTK_edit_publication_index_checkbutton"));
  ctx->anonymity_combo = GTK_COMBO_BOX (gtk_builder_get_object (ctx->builder,
      "GNUNET_GTK_edit_publication_anonymity_combobox"));
  gtk_combo_box_set_model (ctx->anonymity_combo, GTK_TREE_MODEL (anon_liststore));
  ctx->anonymity_liststore = anon_liststore;

  GNUNET_FS_GTK_setup_expiration_year_adjustment (ctx->builder);
  /* indexing does not apply to directories */
  gtk_widget_set_visible (GTK_WIDGET (ctx->index_checkbutton),
      !GNUNET_FS_file_information_is_directory (fip));
  gtk_widget_set_visible (GTK_WIDGET (ctx->index_label),
      !GNUNET_FS_file_information_is_directory (fip));
  gtk_widget_set_visible (GTK_WIDGET (ctx->root_entry),
      !allow_no_keywords);
  gtk_widget_set_visible (GTK_WIDGET (ctx->root_label),
      !allow_no_keywords);

  gtk_list_store_clear (ctx->keywords_liststore);
  gtk_list_store_clear (ctx->meta_liststore);

  if (NULL != short_fn)
    gtk_window_set_title (GTK_WINDOW (ctx->pubwindow), short_fn);
  else                                                       
    gtk_window_set_title (GTK_WINDOW (ctx->pubwindow), _("<unnamed>"));

  if (gtk_combo_box_get_active_iter (ctx->pubtypes_combo, &iter))
  {
    gtk_tree_model_get (GTK_TREE_MODEL (ctx->pubtypes_liststore), &iter, 0, &code, -1);
    change_metatypes (ctx, 0);
  }
  else
    gtk_combo_box_set_active (ctx->pubtypes_combo, 0);

  ctx->do_index = do_index;
  if (NULL != short_fn)
    ctx->short_fn = GNUNET_strdup (short_fn);
  ctx->bo = bo;
  ctx->fip = fip;
  ctx->preview_changed = GNUNET_NO;
  ctx->allow_no_keywords = allow_no_keywords;
  ctx->is_directory = GNUNET_FS_file_information_is_directory (fip);
  ctx->cb = cb;
  ctx->cls = cls;

  GNUNET_FS_file_information_inspect (fip, &file_information_extract, ctx);

  gtk_entry_set_text (ctx->keyword_entry, "");
  gtk_widget_set_sensitive (ctx->ok, allow_no_keywords ? TRUE : FALSE);
  gtk_window_set_transient_for (GTK_WINDOW (ctx->pubwindow), parent);
  gtk_window_present (GTK_WINDOW (ctx->pubwindow));
}


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