aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk-main_window_file_publish.c
blob: 387736b66de5bffd3ec10cdbf69e775098ed5a05 (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
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
/*
     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_file_publish.c
 * @author Christian Grothoff
 */
#include "gnunet-fs-gtk-common.h"
#include "gnunet-fs-gtk.h"
#include "gnunet-fs-gtk-edit_publish_dialog.h"
#include <gnunet/gnunet_util_lib.h>
#include <gnunet/gnunet_fs_service.h>

#define MARKER_DIR_FILE_SIZE "-"

#define VERBOSE_PROGRESS GNUNET_NO

struct AddDirClientContext;

struct MainPublishingDialogContext
{
  GtkBuilder *builder;
  GtkBuilder *main_window_builder;
  GtkTreeView *pseudonym_treeview;
  GtkTreeSelection *pseudonym_selection;
  GtkTreeModel *pseudonym_treemodel;
  GtkWidget *up_button;
  GtkWidget *down_button;
  GtkWidget *left_button;
  GtkWidget *right_button;
  GtkWidget *delete_button;
  GtkWidget *edit_button;
  GtkWidget *execute_button;
  GtkWidget *cancel_button;
  GtkTreeView *file_info_treeview;
  GtkTreeSelection *file_info_selection;
  GtkTreeModel *file_info_treemodel;  
  GtkWindow *master_pubdialog;

  gulong open_directory_handler_id;
  GtkBuilder *open_directory_builder;

  gulong open_file_handler_id;
  GtkBuilder *open_file_builder;

  /* To keep multiple scanners running */
  struct AddDirClientContext *adddir_head;
  struct AddDirClientContext *adddir_tail;
};

/* One of these is kept for every directory being opened */
struct AddDirClientContext
{
  struct AddDirClientContext *prev;
  struct AddDirClientContext *next;

  struct GNUNET_FS_ProcessMetadataContext *pmc;

  struct MainPublishingDialogContext *ctx;
  struct GNUNET_FS_DirScanner *ds;

  struct GNUNET_FS_ShareTreeItem *directory_scan_result;

  struct GNUNET_FS_BlockOptions directory_scan_bo;
  int directory_scan_do_index;

  GtkBuilder *progress_dialog_builder;
  GtkWidget *progress_dialog;
  GtkProgressBar *progress_dialog_bar;
  GtkButton *progress_dialog_cancel;
  GtkTextView *progress_dialog_textview;
  GtkTextBuffer *progress_dialog_textbuffer;
  GtkTextMark *progress_dialog_textmark;
  GtkAdjustment *textview_vertial_adjustment;

  unsigned int done;
  unsigned int total;
};


static void
selection_changed_cb (GtkTreeSelection * ts, struct MainPublishingDialogContext *ctx);


/**
 * Check if two GtkTreeIters refer to the same element.
 *
 * @param tm tree model of the iterators
 * @param i1 first iterator
 * @param i2 second iterator
 * @return GNUNET_YES if they are equal
 */
static int
gtk_tree_iter_equals (GtkTreeModel * tm, GtkTreeIter * i1, GtkTreeIter * i2)
{
  GtkTreePath *p1;
  GtkTreePath *p2;
  int ret;

  p1 = gtk_tree_model_get_path (tm, i1);
  p2 = gtk_tree_model_get_path (tm, i2);
  ret = gtk_tree_path_compare (p1, p2);
  gtk_tree_path_free (p1);
  gtk_tree_path_free (p2);
  return (0 == ret) ? GNUNET_YES : GNUNET_NO;
}

/* Fill out the main publishing dialog context structure */
static void
init_ctx (struct MainPublishingDialogContext *ctx)
{
  ctx->pseudonym_treeview = GTK_TREE_VIEW (gtk_builder_get_object
      (ctx->builder, "GNUNET_GTK_master_publish_dialog_pseudonym_tree_view"));

  ctx->up_button = GTK_WIDGET (gtk_builder_get_object
      (ctx->builder, "GNUNET_GTK_master_publish_dialog_up_button"));
  ctx->down_button = GTK_WIDGET (gtk_builder_get_object
      (ctx->builder, "GNUNET_GTK_master_publish_dialog_down_button"));
  ctx->left_button = GTK_WIDGET (gtk_builder_get_object
      (ctx->builder, "GNUNET_GTK_master_publish_dialog_left_button"));
  ctx->right_button = GTK_WIDGET (gtk_builder_get_object
      (ctx->builder, "GNUNET_GTK_master_publish_dialog_right_button"));
  ctx->delete_button = GTK_WIDGET (gtk_builder_get_object
      (ctx->builder, "GNUNET_GTK_master_publish_dialog_delete_button"));
  ctx->edit_button = GTK_WIDGET (gtk_builder_get_object
      (ctx->builder, "GNUNET_GTK_master_publish_dialog_edit_button"));
  ctx->execute_button = GTK_WIDGET (gtk_builder_get_object
      (ctx->builder, "GNUNET_GTK_master_publish_dialog_execute_button"));
  ctx->cancel_button = GTK_WIDGET (gtk_builder_get_object
      (ctx->builder , "GNUNET_GTK_master_publish_dialog_cancel_button"));
  ctx->file_info_treeview = GTK_TREE_VIEW (gtk_builder_get_object
      (ctx->builder, "GNUNET_GTK_master_publish_dialog_file_information_tree_view"));

  ctx->master_pubdialog =
      GTK_WINDOW (gtk_builder_get_object
                  (ctx->builder, "GNUNET_GTK_master_publish_dialog"));

  ctx->file_info_selection = gtk_tree_view_get_selection (ctx->file_info_treeview);
  ctx->file_info_treemodel = gtk_tree_view_get_model (ctx->file_info_treeview);
  ctx->pseudonym_selection = gtk_tree_view_get_selection (ctx->pseudonym_treeview);
  ctx->pseudonym_treemodel = gtk_tree_view_get_model (ctx->pseudonym_treeview);

  g_signal_connect (G_OBJECT (ctx->file_info_selection), "changed",
                    G_CALLBACK (selection_changed_cb), ctx);
  g_signal_connect (G_OBJECT (ctx->pseudonym_selection), "changed",
                    G_CALLBACK (selection_changed_cb), ctx);
}

/**
 * Update selectivity in the master dialog.
 */
static void
update_selectivity (struct MainPublishingDialogContext *ctx)
{
  GtkTreeIter iter;
  GtkTreeIter parent;
  GtkTreeIter pred;
  int is_dir;
  struct GNUNET_FS_FileInformation *fip;
  int ns_ok;
  gchar *namespace_id;

  ns_ok = GNUNET_YES;
  if (TRUE == gtk_tree_selection_get_selected (ctx->pseudonym_selection, NULL, &iter))
  {
    gtk_tree_model_get (ctx->pseudonym_treemodel, &iter, 2, &namespace_id, -1);
    if (namespace_id == NULL)
      ns_ok = GNUNET_NO;
    else
      g_free (namespace_id);
  }
  /* Don't let the user close the dialog until all scanners are finished and
   * their windows are closed
   */
  if ((gtk_tree_model_get_iter_first (ctx->file_info_treemodel, &iter))
      && (ns_ok == GNUNET_YES) && ctx->adddir_head == NULL)
    gtk_widget_set_sensitive (ctx->execute_button, TRUE);
  else
    gtk_widget_set_sensitive (ctx->execute_button, FALSE);
  if (ctx->adddir_head == NULL)
    gtk_widget_set_sensitive (ctx->cancel_button, TRUE);
  else
    gtk_widget_set_sensitive (ctx->cancel_button, FALSE);
  if (TRUE != gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &iter))
  {
    gtk_widget_set_sensitive (ctx->up_button, FALSE);
    gtk_widget_set_sensitive (ctx->down_button, FALSE);
    gtk_widget_set_sensitive (ctx->left_button, FALSE);
    gtk_widget_set_sensitive (ctx->right_button, FALSE);
    gtk_widget_set_sensitive (ctx->delete_button, FALSE);
    gtk_widget_set_sensitive (ctx->edit_button, FALSE);
    return;
  }
  gtk_widget_set_sensitive (ctx->delete_button, TRUE);
  gtk_widget_set_sensitive (ctx->edit_button, TRUE);

  /* now figure out which move operations are currently legal */
  GNUNET_assert (TRUE == gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &iter));
  if (TRUE == gtk_tree_model_iter_next (ctx->file_info_treemodel, &iter))
  {
    gtk_widget_set_sensitive (ctx->down_button, TRUE);
  }
  else
  {
    gtk_widget_set_sensitive (ctx->down_button, FALSE);
  }
  GNUNET_assert (TRUE == gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &iter));
  if (TRUE == gtk_tree_model_iter_parent (ctx->file_info_treemodel, &parent, &iter))
  {
    gtk_widget_set_sensitive (ctx->left_button, TRUE);
    GNUNET_assert (TRUE == gtk_tree_model_iter_children (ctx->file_info_treemodel, &pred, &parent));
  }
  else
  {
    gtk_widget_set_sensitive (ctx->left_button, FALSE);
    GNUNET_assert (TRUE == gtk_tree_model_get_iter_first (ctx->file_info_treemodel, &pred));
  }
  /* iterate over 'next' of pred to find out if our
   * predecessor is a directory! */
  is_dir = GNUNET_SYSERR;
  while (GNUNET_YES != gtk_tree_iter_equals (ctx->file_info_treemodel, &pred, &iter))
  {
    gtk_tree_model_get (ctx->file_info_treemodel, &pred, 5, &fip, -1);
    is_dir = GNUNET_FS_file_information_is_directory (fip);
    GNUNET_assert (TRUE == gtk_tree_model_iter_next (ctx->file_info_treemodel, &pred));
  }
  if (GNUNET_YES == is_dir)
  {
    gtk_widget_set_sensitive (ctx->right_button, TRUE);
  }
  else
  {
    gtk_widget_set_sensitive (ctx->right_button, FALSE);
  }
  if (GNUNET_SYSERR != is_dir)
  {
    gtk_widget_set_sensitive (ctx->up_button, TRUE);
  }
  else
  {
    gtk_widget_set_sensitive (ctx->up_button, FALSE);
  }
}


/**
 * Add an empty directory to the tree model.
 *
 * @param name name for the directory
 * @param bo block options
 * @param iter parent entry, or NULL for top-level addition
 * @param pos iterator to set to the location of the new element
 */
static void
create_dir_at_iter (struct MainPublishingDialogContext *ctx, const char *name,
                    const struct GNUNET_FS_BlockOptions *bo, GtkTreeIter * iter,
                    GtkTreeIter * pos)
{
  struct GNUNET_FS_FileInformation *fi;
  GtkTreeRowReference *row_reference;
  GtkTreePath *path;
  struct GNUNET_CONTAINER_MetaData *meta;

  meta = GNUNET_CONTAINER_meta_data_create ();
  GNUNET_FS_meta_data_make_directory (meta);
  GNUNET_CONTAINER_meta_data_insert (meta, "<gnunet-gtk>",
                                     EXTRACTOR_METATYPE_FILENAME,
                                     EXTRACTOR_METAFORMAT_UTF8, "text/plain",
                                     name, strlen (name) + 1);
  gtk_tree_store_insert_before (GTK_TREE_STORE (ctx->file_info_treemodel), pos, iter, NULL);
  path = gtk_tree_model_get_path (ctx->file_info_treemodel, pos);
  row_reference = gtk_tree_row_reference_new (ctx->file_info_treemodel, path);
  gtk_tree_path_free (path);
  fi = GNUNET_FS_file_information_create_empty_directory
      (GNUNET_FS_GTK_get_fs_handle (), row_reference, NULL, meta, bo, name);
  GNUNET_CONTAINER_meta_data_destroy (meta);
  gtk_tree_store_set (GTK_TREE_STORE (ctx->file_info_treemodel), pos, 0, MARKER_DIR_FILE_SIZE, 1, (gboolean) GNUNET_NO,
                      2, name, 3, (guint) bo->anonymity_level, 4,
                      (guint) bo->content_priority, 5, fi,
		      6, (guint64) bo->expiration_time.abs_value,
		      7, (guint) bo->replication_level,
		      -1);
  update_selectivity (ctx);
}

static void
selection_changed_cb (GtkTreeSelection * ts, struct MainPublishingDialogContext *ctx)
{
  update_selectivity (ctx);
}

static void
remove_old_entry (GtkTreeStore * ts, GtkTreeIter * root)
{
  GtkTreeIter child;

  while (TRUE ==
         gtk_tree_model_iter_children (GTK_TREE_MODEL (ts), &child, root))
    remove_old_entry (ts, &child);
  gtk_tree_store_remove (ts, root);
}


/**
 * Move an entry in the tree.
 */
static void
move_entry (struct MainPublishingDialogContext *ctx, GtkTreeModel * tm, GtkTreeIter * old,
            GtkTreeIter * newpos, int dsel)
{
  struct GNUNET_FS_FileInformation *fip;
  gint do_index;
  gchar *short_fn;
  guint anonymity_level;
  guint priority;
  guint replication_level;
  guint64 expiration_time_abs;
  char *fsf;
  GtkTreePath *path;
  GtkTreeIter child;
  GtkTreeIter cnewpos;
  GtkTreeRowReference *rr;
  GtkTreeRowReference *rr2;

  gtk_tree_model_get (tm, old, 0, &fsf, 1, &do_index, 2, &short_fn, 3,
                      &anonymity_level, 4, &priority, 5, &fip, 
		      6, &expiration_time_abs, 7, &replication_level, -1);
  gtk_tree_store_set (GTK_TREE_STORE (tm), newpos, 0, fsf, 1, do_index, 2,
                      short_fn, 3, (guint) anonymity_level, 4, (guint) priority,
                      5, fip, 
		      6, expiration_time_abs,
		      7, replication_level, -1);
  if (dsel == GNUNET_YES)
  {
    path = gtk_tree_model_get_path (tm, newpos);
    rr = gtk_tree_row_reference_new (tm, path);
    gtk_tree_path_free (path);
  }
  else
  {
    rr = NULL;
  }
  if (TRUE == gtk_tree_model_iter_children (tm, &child, old))
  {
    do
    {
      path = gtk_tree_model_get_path (tm, &child);
      rr2 = gtk_tree_row_reference_new (tm, path);
      gtk_tree_path_free (path);
      gtk_tree_store_insert_before (GTK_TREE_STORE (tm), &cnewpos, newpos,
                                    NULL);
      move_entry (ctx, tm, &child, &cnewpos, GNUNET_NO);
      path = gtk_tree_row_reference_get_path (rr2);
      gtk_tree_row_reference_free (rr2);
      GNUNET_assert (TRUE == gtk_tree_model_get_iter (tm, &child, path));
      gtk_tree_path_free (path);
    }
    while (TRUE == gtk_tree_model_iter_next (tm, &child));
  }
  g_free (short_fn);
  g_free (fsf);
  if (dsel == GNUNET_YES)
  {
    path = gtk_tree_row_reference_get_path (rr);
    gtk_tree_row_reference_free (rr);
    gtk_tree_view_expand_to_path (ctx->file_info_treeview, path);
    GNUNET_assert (TRUE == gtk_tree_model_get_iter (tm, newpos, path));
    gtk_tree_path_free (path);
    gtk_tree_selection_select_iter (ctx->file_info_selection, newpos);
  }
  update_selectivity (ctx);
}


/**
 * User has changed the "current" identifier for the content in
 * the GtkTreeView.  Update the model.
 */
void GNUNET_GTK_master_publish_dialog_pseudonym_updates_renderer_edited_cb
    (GtkCellRendererText * renderer, gchar * cpath, gchar * new_text,
     struct MainPublishingDialogContext *ctx)
{
  GtkTreeIter iter;

  if (TRUE !=
      gtk_tree_model_get_iter_from_string (ctx->pseudonym_treemodel, &iter, cpath))
  {
    GNUNET_break (0);
    return;
  }
  gtk_tree_store_set (GTK_TREE_STORE (ctx->pseudonym_treemodel), &iter, 5, new_text, -1);
  update_selectivity (ctx);
}


/**
 * User has changed the "current" identifier for the content in
 * the GtkTreeView.  Update the model.
 */
void GNUNET_GTK_master_publish_dialog_pseudonym_identifier_renderer_edited_cb
    (GtkCellRendererText * renderer, gchar * cpath, gchar * new_text,
     struct MainPublishingDialogContext *ctx)
{
  GtkTreeIter iter;

  if (TRUE !=
      gtk_tree_model_get_iter_from_string (ctx->pseudonym_treemodel, &iter, cpath))
  {
    GNUNET_break (0);
    return;
  }
  gtk_tree_store_set (GTK_TREE_STORE (ctx->pseudonym_treemodel), &iter, 2, new_text, -1);
  update_selectivity (ctx);
}


void
GNUNET_GTK_master_publish_dialog_right_button_clicked_cb (GtkWidget * dummy,
                                                          struct MainPublishingDialogContext *ctx)
{
  GtkTreeIter iter;
  GtkTreeIter parent;
  GtkTreeIter pred;
  GtkTreeIter prev;
  GtkTreeIter pos;

  if (TRUE != gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &iter))
  {
    GNUNET_break (0);
    return;
  }
  if (TRUE == gtk_tree_model_iter_parent (ctx->file_info_treemodel, &parent, &iter))
  {
    GNUNET_assert (TRUE == gtk_tree_model_iter_children (ctx->file_info_treemodel, &pred, &parent));
  }
  else if (TRUE != gtk_tree_model_get_iter_first (ctx->file_info_treemodel, &pred))
  {
    GNUNET_break (0);
    return;
  }
  /* iterate over 'next' of pred to find out who our predecessor is! */
  memset (&prev, 0, sizeof (GtkTreeIter));
  while (GNUNET_YES != gtk_tree_iter_equals (ctx->file_info_treemodel, &pred, &iter))
  {
    prev = pred;
    GNUNET_assert (TRUE == gtk_tree_model_iter_next (ctx->file_info_treemodel, &pred));
  }
  gtk_tree_store_insert_before (GTK_TREE_STORE (ctx->file_info_treemodel), &pos, &prev, NULL);
  if (TRUE != gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &iter))
  {
    GNUNET_break (0);
    return;
  }
  move_entry (ctx, ctx->file_info_treemodel, &iter, &pos, GNUNET_YES);
  remove_old_entry (GTK_TREE_STORE (ctx->file_info_treemodel), &iter);
}


void
GNUNET_GTK_master_publish_dialog_left_button_clicked_cb (GtkWidget * dummy,
                                                         struct MainPublishingDialogContext *ctx)
{
  GtkTreeIter iter;
  GtkTreeIter parent;
  GtkTreeIter pos;


  if (TRUE != gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &iter))
  {
    GNUNET_break (0);
    return;
  }
  if (TRUE != gtk_tree_model_iter_parent (ctx->file_info_treemodel, &parent, &iter))
  {
    GNUNET_break (0);
    return;
  }
  gtk_tree_store_insert_after (GTK_TREE_STORE (ctx->file_info_treemodel), &pos, NULL, &parent);
  if (TRUE != gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &iter))
  {
    GNUNET_break (0);
    return;
  }
  move_entry (ctx, ctx->file_info_treemodel, &iter, &pos, GNUNET_YES);
  remove_old_entry (GTK_TREE_STORE (ctx->file_info_treemodel), &iter);
}


void
GNUNET_GTK_master_publish_dialog_up_button_clicked_cb (GtkWidget * dummy,
                                                       struct MainPublishingDialogContext *ctx)
{
  GtkTreeIter iter;
  GtkTreeIter parent;
  GtkTreeIter pred;
  GtkTreeIter prev;
  GtkTreeIter *pprev;
  GtkTreeIter pos;

  if (TRUE != gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &iter))
  {
    GNUNET_break (0);
    return;
  }
  if (TRUE == gtk_tree_model_iter_parent (ctx->file_info_treemodel, &parent, &iter))
  {
    GNUNET_assert (TRUE == gtk_tree_model_iter_children (ctx->file_info_treemodel, &pred, &parent));
    pprev = &parent;
  }
  else if (TRUE == gtk_tree_model_get_iter_first (ctx->file_info_treemodel, &pred))
  {
    pprev = NULL;
  }
  else
  {
    GNUNET_break (0);
    return;
  }
  /* iterate over 'next' of pred to find out who our predecessor is! */
  while (GNUNET_YES != gtk_tree_iter_equals (ctx->file_info_treemodel, &pred, &iter))
  {
    prev = pred;
    pprev = &prev;
    GNUNET_assert (TRUE == gtk_tree_model_iter_next (ctx->file_info_treemodel, &pred));
  }
  gtk_tree_store_insert_before (GTK_TREE_STORE (ctx->file_info_treemodel), &pos, NULL, pprev);
  if (TRUE != gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &iter))
  {
    GNUNET_break (0);
    return;
  }
  move_entry (ctx, ctx->file_info_treemodel, &iter, &pos, GNUNET_YES);
  remove_old_entry (GTK_TREE_STORE (ctx->file_info_treemodel), &iter);
}


void
GNUNET_GTK_master_publish_dialog_down_button_clicked_cb (GtkWidget * dummy,
                                                         struct MainPublishingDialogContext *ctx)
{
  GtkTreeIter iter;
  GtkTreeIter next;
  GtkTreeIter pos;

  if (TRUE != gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &iter))
  {
    GNUNET_break (0);
    return;
  }
  if (TRUE != gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &next))
  {
    GNUNET_break (0);
    return;
  }
  GNUNET_assert (TRUE == gtk_tree_model_iter_next (ctx->file_info_treemodel, &next));
  gtk_tree_store_insert_after (GTK_TREE_STORE (ctx->file_info_treemodel), &pos, NULL, &next);
  if (TRUE != gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &iter))
  {
    GNUNET_break (0);
    return;
  }
  move_entry (ctx, ctx->file_info_treemodel, &iter, &pos, GNUNET_YES);
  remove_old_entry (GTK_TREE_STORE (ctx->file_info_treemodel), &iter);
}


void
GNUNET_GTK_master_publish_dialog_new_button_clicked_cb (GtkWidget * dummy,
                                                        struct MainPublishingDialogContext *ctx)
{
  GtkTreeIter iter;
  GtkTreeIter pos;
  struct GNUNET_FS_BlockOptions bo;

  /* FIXME: consider opening a dialog to get
   * anonymity, priority and expiration prior
   * to calling this function (currently we
   * use default values for those).
   * Or getting these values from the configuration.
   */
  bo.anonymity_level = 1;
  bo.content_priority = 1000;
  bo.expiration_time =
      GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_YEARS);
  bo.replication_level = 1;
  if (TRUE != gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &iter))
  {
    create_dir_at_iter (ctx, "unnamed/", &bo, NULL, &pos);
    return;
  }
  create_dir_at_iter (ctx, "unnamed/", &bo, &iter, &pos);
}


static void
insert_progress_dialog_text (struct AddDirClientContext *adcc,
			     const char *text)
{
  gtk_text_buffer_insert_at_cursor (adcc->progress_dialog_textbuffer,
				    text, -1);
  gtk_text_view_place_cursor_onscreen (adcc->progress_dialog_textview);
  gtk_adjustment_set_value (adcc->textview_vertial_adjustment,
			    gtk_adjustment_get_upper (adcc->textview_vertial_adjustment));
}


static void
add_item (struct AddDirClientContext *adcc, GtkTreeStore *ts,
	  struct GNUNET_FS_ShareTreeItem *item, 
	  GtkTreeIter *parent, 
	  GtkTreeIter *sibling,
	  GtkTreeIter *item_iter)
{
  char *file_size_fancy;
  struct GNUNET_FS_FileInformation *fi;
  GtkTreeRowReference *row_reference;
  GtkTreePath *path;
  struct stat sbuf;
  
  if (0 != stat (item->filename,
		 &sbuf))
  {
    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "stat", item->filename);
    return;
  }

  gtk_tree_store_insert_after (ts, item_iter, parent, sibling);
  path = gtk_tree_model_get_path (GTK_TREE_MODEL (ts), item_iter);
  row_reference = gtk_tree_row_reference_new (GTK_TREE_MODEL (ts), path);
  gtk_tree_path_free (path);

  if (item->is_directory)
  {
    if (NULL != item->meta)
      GNUNET_CONTAINER_meta_data_delete (item->meta,
					 EXTRACTOR_METATYPE_MIMETYPE, NULL, 0);
    else
      item->meta = GNUNET_CONTAINER_meta_data_create ();
    GNUNET_FS_meta_data_make_directory (item->meta);
    if (NULL == item->ksk_uri)
      item->ksk_uri = GNUNET_FS_uri_ksk_create (GNUNET_FS_DIRECTORY_MIME, NULL);
    else
      GNUNET_FS_uri_ksk_add_keyword (item->ksk_uri, GNUNET_FS_DIRECTORY_MIME,
				     GNUNET_NO);
    fi = GNUNET_FS_file_information_create_empty_directory (
        GNUNET_FS_GTK_get_fs_handle (), row_reference, item->ksk_uri,
        item->meta, &adcc->directory_scan_bo, item->filename);
  }
  else
  {
    fi = GNUNET_FS_file_information_create_from_file (
        GNUNET_FS_GTK_get_fs_handle (), row_reference, item->filename,
        item->ksk_uri, item->meta, adcc->directory_scan_do_index,
        &adcc->directory_scan_bo);
  }
  if (item->is_directory)
    file_size_fancy = GNUNET_strdup (MARKER_DIR_FILE_SIZE);
  else
    file_size_fancy = GNUNET_STRINGS_byte_size_fancy (sbuf.st_size);
  
  gtk_tree_store_set (ts, item_iter, 0, file_size_fancy,
      1, (gboolean) adcc->directory_scan_do_index,
      2, item->short_filename,
      3, (guint) adcc->directory_scan_bo.anonymity_level,
      4, (guint) adcc->directory_scan_bo.content_priority,
      5, fi,
      6, (guint64) adcc->directory_scan_bo.expiration_time.abs_value,
      7, (guint) adcc->directory_scan_bo.replication_level, -1);
  GNUNET_free (file_size_fancy);
}


/**
 * Traverse the share tree and add it to the tree store
 *
 */
static void
add_share_items_to_treestore (struct AddDirClientContext *adcc,
			      struct GNUNET_FS_ShareTreeItem *toplevel,
			      GtkTreeIter *parent_iter)
{
  struct MainPublishingDialogContext *ctx = adcc->ctx;
  GtkTreeStore *ts = GTK_TREE_STORE (ctx->file_info_treemodel);
  GtkTreeIter *sibling_iter;
  GtkTreeIter last_added;
  struct GNUNET_FS_ShareTreeItem *item;

  sibling_iter = NULL;  
  for (item = toplevel; item != NULL; item = item->next)
  {
    add_item (adcc, ts, item, parent_iter, sibling_iter, &last_added);
    sibling_iter = &last_added;
    if (item->is_directory) 
      add_share_items_to_treestore (adcc,
				    item->children_head,
				    sibling_iter);
  }
}


static void
close_scan (struct AddDirClientContext *adcc)
{
  gtk_widget_destroy (adcc->progress_dialog);
  g_object_unref (G_OBJECT (adcc->progress_dialog_builder));  
  GNUNET_CONTAINER_DLL_remove (adcc->ctx->adddir_head,
			       adcc->ctx->adddir_tail, 
			       adcc);
  update_selectivity (adcc->ctx);
  GNUNET_free (adcc);
}


static void
directory_scan_cb (void *cls, 
		   const char *filename, int is_directory,
		   enum GNUNET_FS_DirScannerProgressUpdateReason reason)
{
  static struct GNUNET_TIME_Absolute last_pulse;
  struct AddDirClientContext *adcc = cls;
  char *s;
  gdouble fraction;

  switch (reason)
  {
  case GNUNET_FS_DIRSCANNER_FILE_START:
    GNUNET_assert (filename != NULL);
    if (GNUNET_TIME_absolute_get_duration (last_pulse).rel_value > 100)
    {
      gtk_progress_bar_pulse (adcc->progress_dialog_bar);
      last_pulse = GNUNET_TIME_absolute_get ();
    }
#if VERBOSE_PROGRESS
    if (is_directory)
    {
      GNUNET_asprintf (&s, _("Scanning directory `%s'.\n"), filename);
      insert_progress_dialog_text (adcc, s);
      GNUNET_free (s);
    }
    else
      adcc->total++;
#else
    if (! is_directory)
      adcc->total++;
#endif
    break;
  case GNUNET_FS_DIRSCANNER_FILE_IGNORED:
    GNUNET_assert (filename != NULL);
    GNUNET_asprintf (&s,
		     _("Failed to scan `%s' (access error). Skipping.\n"),
		     filename);
#if ! VERBOSE_PROGRESS
    gtk_widget_show (GTK_WIDGET (gtk_builder_get_object (adcc->progress_dialog_builder,
							 "GNUNET_FS_GTK_progress_dialog_scrolled_window")));
#endif
    insert_progress_dialog_text (adcc, s);
    GNUNET_free (s);    
    break;
  case GNUNET_FS_DIRSCANNER_ALL_COUNTED:
    fraction = (adcc->total == 0) ? 1.0 : (1.0 * adcc->done) / adcc->total;
    GNUNET_asprintf (&s, "%u/%u (%3f%%)", 
		     adcc->done,
		     adcc->total,
		     100.0 * fraction);
    gtk_progress_bar_set_text (adcc->progress_dialog_bar,
			       s);    
    GNUNET_free (s);
    gtk_progress_bar_set_fraction (adcc->progress_dialog_bar,
				   fraction);
    break;
  case GNUNET_FS_DIRSCANNER_EXTRACT_FINISHED:
#if VERBOSE_PROGRESS
    GNUNET_asprintf (&s, _("Processed file `%s'.\n"), filename);
    insert_progress_dialog_text (adcc, s);
    GNUNET_free (s);
#endif
    adcc->done++;
    GNUNET_assert (adcc->done <= adcc->total);
    fraction = (adcc->total == 0) ? 1.0 : (1.0 * adcc->done) / adcc->total;
    GNUNET_asprintf (&s, "%u/%u (%3f%%)", 
		     adcc->done,
		     adcc->total,
		     100.0 * fraction);
    gtk_progress_bar_set_text (adcc->progress_dialog_bar,
			       s);    
    GNUNET_free (s);
    gtk_progress_bar_set_fraction (adcc->progress_dialog_bar,
				   fraction);
    break;
  case GNUNET_FS_DIRSCANNER_INTERNAL_ERROR:
    insert_progress_dialog_text (adcc, _("Operation failed (press cancel)\n"));
    GNUNET_FS_directory_scan_abort (adcc->ds);
    adcc->ds = NULL;
    break;
  case GNUNET_FS_DIRSCANNER_FINISHED:
    insert_progress_dialog_text (adcc, _("Scanner has finished.\n"));
    adcc->directory_scan_result = GNUNET_FS_directory_scan_get_result (adcc->ds);
    adcc->ds = NULL;
    GNUNET_FS_share_tree_trim (adcc->directory_scan_result);
    add_share_items_to_treestore (adcc, 
				  adcc->directory_scan_result,
				  NULL);
    GNUNET_FS_share_tree_free (adcc->directory_scan_result);
    adcc->directory_scan_result = NULL;
    update_selectivity (adcc->ctx);
    close_scan (adcc);
    break;
  default:
    GNUNET_break (0);
    break;
  }
}


static void
scan_file_or_directory (struct MainPublishingDialogContext *ctx, 
			gchar *filename,
			struct GNUNET_FS_BlockOptions *bo, 
			int do_index)
{
  struct AddDirClientContext *adcc;
  GtkTextIter iter;

  adcc = GNUNET_malloc (sizeof (struct AddDirClientContext));
  adcc->ctx = ctx;
  GNUNET_CONTAINER_DLL_insert_tail (ctx->adddir_head, ctx->adddir_tail, adcc);
  adcc->ds = GNUNET_FS_directory_scan_start (filename,
      GNUNET_NO, NULL, &directory_scan_cb, adcc);
  adcc->directory_scan_bo = *bo;
  adcc->directory_scan_do_index = do_index;

  adcc->progress_dialog_builder = GNUNET_GTK_get_new_builder (
      "gnunet_fs_gtk_progress_dialog.glade", adcc);
  adcc->progress_dialog = GTK_WIDGET (gtk_builder_get_object (
      adcc->progress_dialog_builder,
      "GNUNET_FS_GTK_progress_dialog"));
  adcc->progress_dialog_bar = GTK_PROGRESS_BAR (gtk_builder_get_object (
      adcc->progress_dialog_builder,
      "GNUNET_FS_GTK_progress_dialog_progressbar"));
  adcc->progress_dialog_cancel = GTK_BUTTON (gtk_builder_get_object (
      adcc->progress_dialog_builder,
      "GNUNET_FS_GTK_progress_dialog_cancel_button"));
  adcc->progress_dialog_textview = GTK_TEXT_VIEW (
      gtk_builder_get_object (adcc->progress_dialog_builder,
      "GNUNET_FS_GTK_progress_dialog_textview"));
  adcc->textview_vertial_adjustment  = GTK_ADJUSTMENT (
      gtk_builder_get_object (adcc->progress_dialog_builder,
      "GNUNET_FS_GTK_progress_dialog_textview_vertical_adjustment"));
  adcc->progress_dialog_textbuffer = GTK_TEXT_BUFFER (
      gtk_builder_get_object (adcc->progress_dialog_builder,
      "GNUNET_FS_GTK_progress_dialog_textbuffer"));
  gtk_text_buffer_get_end_iter (adcc->progress_dialog_textbuffer,
      &iter);
  adcc->progress_dialog_textmark = gtk_text_buffer_create_mark (
      adcc->progress_dialog_textbuffer, "scroll",
      &iter, FALSE);
#if VERBOSE_PROGRESS
  gtk_widget_show (GTK_WIDGET (gtk_builder_get_object (adcc->progress_dialog_builder,
						       "GNUNET_FS_GTK_progress_dialog_scrolled_window")));
#endif

  gtk_window_set_transient_for (GTK_WINDOW (adcc->progress_dialog), adcc->ctx->master_pubdialog);
  gtk_window_set_title (GTK_WINDOW (adcc->progress_dialog), filename);
  gtk_window_present (GTK_WINDOW (adcc->progress_dialog));

  update_selectivity (ctx);
}


static void
publish_directory_dialog_response_cb (GtkDialog * dialog,
				      gint response_id,
				      struct MainPublishingDialogContext *ctx)
{
  char *filename;
  int do_index;
  GtkSpinButton *sb;
  struct GNUNET_FS_BlockOptions bo;
  GtkWidget *ad;

  if (g_signal_handler_is_connected (G_OBJECT (dialog), ctx->open_directory_handler_id))
    g_signal_handler_disconnect (G_OBJECT (dialog), ctx->open_directory_handler_id);
  ctx->open_directory_handler_id = 0;

  ad = GTK_WIDGET (gtk_builder_get_object
                   (ctx->open_directory_builder, "GNUNET_GTK_publish_directory_dialog"));
  if (response_id == -5)
  {
    filename = GNUNET_GTK_filechooser_get_filename_utf8 (GTK_FILE_CHOOSER (ad));
    sb = GTK_SPIN_BUTTON (gtk_builder_get_object
                          (ctx->open_directory_builder,
                           "GNUNET_GTK_publish_directory_dialog_expiration_year_spin_button"));
    if (!GNUNET_GTK_get_selected_anonymity_level
        (ctx->open_directory_builder, "GNUNET_GTK_publish_directory_dialog_anonymity_combobox",
         &bo.anonymity_level))
      bo.anonymity_level = 1;
    bo.content_priority =
      gtk_spin_button_get_value (GTK_SPIN_BUTTON
                                   (gtk_builder_get_object
                                    (ctx->open_directory_builder,
                                     "GNUNET_GTK_publish_directory_dialog_priority_spin_button")));
    bo.replication_level = 
      gtk_spin_button_get_value (GTK_SPIN_BUTTON
				 (gtk_builder_get_object
				  (ctx->open_directory_builder,
				   "GNUNET_GTK_publish_directory_dialog_replication_spin_button")));
    bo.expiration_time = GNUNET_FS_GTK_get_expiration_time (sb);
    do_index =
        gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
                                      (gtk_builder_get_object
                                       (ctx->open_directory_builder,
                                        "GNUNET_GTK_publish_directory_dialog_do_index_checkbutton")));

    scan_file_or_directory (ctx, filename, &bo, do_index);
    g_free (filename);
  }
  gtk_widget_destroy (ad);
  g_object_unref (G_OBJECT (ctx->open_directory_builder));
}


static void
publish_file_dialog_response_cb (GtkDialog * dialog,
				 gint response_id,
				 struct MainPublishingDialogContext *ctx)
{
  char *filename;
  struct GNUNET_FS_BlockOptions bo;
  int do_index;
  GtkSpinButton *sb;
  GtkWidget *ad;

  if (g_signal_handler_is_connected (G_OBJECT (dialog), ctx->open_file_handler_id))
    g_signal_handler_disconnect (G_OBJECT (dialog), ctx->open_file_handler_id);
  ctx->open_file_handler_id = 0;

  ad = GTK_WIDGET (gtk_builder_get_object
                   (ctx->open_file_builder, "GNUNET_GTK_publish_file_dialog"));

  if (response_id == -5)
  {
    /* OK */
    sb = GTK_SPIN_BUTTON (gtk_builder_get_object
                          (ctx->open_file_builder,
                           "GNUNET_GTK_publish_file_dialog_expiration_year_spin_button"));

    if (!GNUNET_GTK_get_selected_anonymity_level
        (ctx->open_file_builder, "GNUNET_GTK_publish_file_dialog_anonymity_combobox",
         &bo.anonymity_level))
      bo.anonymity_level = 1;
    bo.content_priority =
        gtk_spin_button_get_value (GTK_SPIN_BUTTON
                                   (gtk_builder_get_object
                                    (ctx->open_file_builder,
                                     "GNUNET_GTK_publish_file_dialog_priority_spin_button")));
    bo.expiration_time = GNUNET_FS_GTK_get_expiration_time (sb);
    bo.replication_level =
      gtk_spin_button_get_value (GTK_SPIN_BUTTON
				 (gtk_builder_get_object
				  (ctx->open_file_builder,
				   "GNUNET_GTK_publish_file_dialog_replication_spin_button")));
    do_index =
        gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON
                                      (gtk_builder_get_object
                                       (ctx->open_file_builder,
                                        "GNUNET_GTK_publish_file_dialog_do_index_checkbutton")));

    filename = GNUNET_GTK_filechooser_get_filename_utf8 (GTK_FILE_CHOOSER (ad));

    scan_file_or_directory (ctx, filename, &bo, do_index);

    g_free (filename);
  }
  else
  {
    /* Cancel/Escape/close/etc */
  }
  gtk_widget_destroy (ad);
}


void
GNUNET_GTK_master_publish_dialog_add_button_clicked_cb (GtkWidget * dummy,
                                                        struct MainPublishingDialogContext *ctx)
{
  GtkWidget *ad;

  GtkComboBox *combo;
  GtkTreeModel *anon_treemodel;

  ctx->open_file_builder = GNUNET_GTK_get_new_builder ("gnunet_fs_gtk_publish_file_dialog.glade", ctx);
  GNUNET_FS_GTK_setup_expiration_year_adjustment (ctx->open_file_builder);
  ad = GTK_WIDGET (gtk_builder_get_object
                   (ctx->open_file_builder, "GNUNET_GTK_publish_file_dialog"));

  /* FIXME: Use some kind of adjustable defaults instead of 1000, 0 and TRUE */
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (gtk_builder_get_object (
      ctx->open_file_builder,
      "GNUNET_GTK_publish_file_dialog_priority_spin_button")), 1000);
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (gtk_builder_get_object (
      ctx->open_file_builder,
      "GNUNET_GTK_publish_file_dialog_replication_spin_button")), 0);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (
      ctx->open_file_builder,
      "GNUNET_GTK_publish_file_dialog_do_index_checkbutton")), TRUE);

  ctx->open_file_handler_id = g_signal_connect (G_OBJECT (ad), "response", G_CALLBACK (publish_file_dialog_response_cb), ctx);

  anon_treemodel = GTK_TREE_MODEL (gtk_builder_get_object (ctx->main_window_builder,
      "main_window_search_anonymity_liststore"));
  combo = GTK_COMBO_BOX (gtk_builder_get_object (ctx->open_file_builder,
      "GNUNET_GTK_publish_file_dialog_anonymity_combobox"));
  gtk_combo_box_set_model (combo, anon_treemodel);

  gtk_window_set_transient_for (GTK_WINDOW (ad), ctx->master_pubdialog);

  gtk_window_present (GTK_WINDOW (ad));
}


struct EditPublishContext
{
  GtkTreeModel *tm;
  GtkTreeIter iter;
};


/**
 * Function called when the edit publish dialog has been closed.
 *
 * @param cls closure
 * @param do_index index flag set?
 * @param short_fn short filename
 * @param bo block options for publishing
 * @param root always NULL here
 * @param ret GTK_RESPONSE_OK if the dialog was closed with "OK"
 */
static void
master_publish_edit_publish_dialog_cb (gpointer cls, int do_index,
                                       const char *short_fn,
                                       const struct GNUNET_FS_BlockOptions *bo,
                                       const char *root,
                                       gint ret)
{
  struct EditPublishContext *cbargs = cls;
  struct GNUNET_FS_FileInformation *fi;

  if (ret == GTK_RESPONSE_OK)
  {
    gtk_tree_store_set (GTK_TREE_STORE (cbargs->tm), &cbargs->iter, 
			1, do_index,
                        2, short_fn,
			3, (guint) bo->anonymity_level, 
			4, (guint) bo->content_priority, 
			6, (guint64) bo->expiration_time.abs_value,
			7, (guint) bo->replication_level,			
			-1);
    gtk_tree_model_get (cbargs->tm, &cbargs->iter, 5, &fi, -1);
    GNUNET_FS_file_information_set_filename (fi, short_fn);
  }
  GNUNET_free (cbargs);
}


void
GNUNET_GTK_master_publish_dialog_edit_button_clicked_cb (GtkWidget * dummy,
                                                         struct MainPublishingDialogContext *ctx)
{
  struct EditPublishContext *cbargs;
  gint do_index;
  char *short_fn;
  guint anonymity_level;
  guint priority;
  struct GNUNET_FS_FileInformation *fip;
  guint64 abs_etime;
  guint replication_level;
  struct GNUNET_FS_BlockOptions bo;
  GtkListStore *anon_liststore;

  anon_liststore = GTK_LIST_STORE (gtk_builder_get_object (ctx->main_window_builder, "main_window_search_anonymity_liststore"));

  cbargs = GNUNET_malloc (sizeof (struct EditPublishContext));
  cbargs->tm = ctx->file_info_treemodel;
  if (TRUE != gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &cbargs->iter))
  {
    GNUNET_break (0);
    GNUNET_free (cbargs);
    return;
  }

  gtk_tree_model_get (ctx->file_info_treemodel, &cbargs->iter,
		      1, &do_index, 
		      2, &short_fn, 
		      3, &anonymity_level,
		      4, &priority, 
		      5, &fip, 
		      6, &abs_etime,
		      7, &replication_level,
		      -1);
  bo.anonymity_level = anonymity_level;
  bo.content_priority = priority;
  bo.expiration_time.abs_value = (uint64_t) abs_etime;
  bo.replication_level = replication_level;
  /* FIXME: can we just give our anon_liststore out like this? What about
     (unintended) sharing of state? */
  GNUNET_FS_GTK_edit_publish_dialog (ctx->master_pubdialog, 
                                     short_fn, fip, TRUE, anon_liststore,
                                     &master_publish_edit_publish_dialog_cb,
                                     cbargs);
}


/**
 * Free row reference stored in the file information's
 * client-info pointer.
 */
static int
free_fi_row_reference (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)
{
  GtkTreeRowReference *row = *client_info;

  if (row == NULL)
  {
    GNUNET_break (0);
    return GNUNET_OK;
  }
  gtk_tree_row_reference_free (row);
  return GNUNET_OK;
}


void
GNUNET_GTK_master_publish_dialog_delete_button_clicked_cb (GtkWidget * dummy,
                                                           struct MainPublishingDialogContext *ctx)
{
  GtkTreeIter iter;
  struct GNUNET_FS_FileInformation *fip;

  if (TRUE != gtk_tree_selection_get_selected (ctx->file_info_selection, NULL, &iter))
  {
    GNUNET_break (0);
    return;
  }
  gtk_tree_model_get (ctx->file_info_treemodel, &iter, 5, &fip, -1);
  GNUNET_FS_file_information_destroy (fip, &free_fi_row_reference, NULL);
  gtk_tree_store_remove (GTK_TREE_STORE (ctx->file_info_treemodel), &iter);
  update_selectivity (ctx);
}


void
GNUNET_FS_GTK_progress_dialog_cancel_button_clicked_cb (GtkButton *button,
							void *cls)
{
  struct AddDirClientContext *adcc = cls;

  if (adcc->ds != NULL)
  {
    /* Still scanning - signal the scanner to finish */
    GNUNET_FS_directory_scan_abort (adcc->ds);
    adcc->ds = NULL;
  }
  close_scan (adcc);
}


gboolean
GNUNET_FS_GTK_progress_dialog_delete_event_cb (GtkWidget *widget,
					       GdkEvent * event,
					       void *cls)
{
  /* Don't allow GTK to kill the window, until the scan is finished */
  return GNUNET_NO;
}


void
GNUNET_GTK_master_publish_dialog_open_button_clicked_cb (GtkWidget * dummy,
                                                         struct MainPublishingDialogContext *ctx)
{
  GtkWidget *ad;

  GtkComboBox *combo;
  GtkTreeModel *anon_treemodel;

  ctx->open_directory_builder = GNUNET_GTK_get_new_builder ("gnunet_fs_gtk_publish_directory_dialog.glade", ctx);
  GNUNET_FS_GTK_setup_expiration_year_adjustment (ctx->open_directory_builder);

  /* FIXME: Use some kind of adjustable defaults instead of 1000, 0 and TRUE */
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (gtk_builder_get_object (
      ctx->open_directory_builder,
      "GNUNET_GTK_publish_directory_dialog_priority_spin_button")), 1000);
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (gtk_builder_get_object (
      ctx->open_directory_builder,
      "GNUNET_GTK_publish_directory_dialog_replication_spin_button")), 0);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (gtk_builder_get_object (
      ctx->open_directory_builder,
      "GNUNET_GTK_publish_directory_dialog_do_index_checkbutton")), TRUE);

  ad = GTK_WIDGET (gtk_builder_get_object
                   (ctx->open_directory_builder, "GNUNET_GTK_publish_directory_dialog"));

  ctx->open_directory_handler_id = g_signal_connect (G_OBJECT (ad), "response", G_CALLBACK (publish_directory_dialog_response_cb), ctx);

  anon_treemodel = GTK_TREE_MODEL (gtk_builder_get_object (ctx->main_window_builder,
      "main_window_search_anonymity_liststore"));
  combo = GTK_COMBO_BOX (gtk_builder_get_object (ctx->open_directory_builder,
      "GNUNET_GTK_publish_directory_dialog_anonymity_combobox"));
  gtk_combo_box_set_model (combo, anon_treemodel);

  gtk_window_set_transient_for (GTK_WINDOW (ad), ctx->master_pubdialog);

  gtk_window_present (GTK_WINDOW (ad));
}


/**
 * Get the file information struct corresponding to the
 * given iter in the publish dialog tree model.  Recursively
 * builds the file information struct from the subtree.
 *
 * @param tm model to grab fi from
 * @param iter position to grab fi from
 * @return file information from the given position (never NULL)
 */
static struct GNUNET_FS_FileInformation *
get_file_information (GtkTreeModel * tm, GtkTreeIter * iter)
{
  struct GNUNET_FS_FileInformation *fi;
  struct GNUNET_FS_FileInformation *fic;
  GtkTreeIter child;

  gtk_tree_model_get (tm, iter, 5, &fi, -1);
  gtk_tree_store_set (GTK_TREE_STORE (tm), iter, 5, NULL, -1);
  GNUNET_assert (fi != NULL);
  if (gtk_tree_model_iter_children (tm, &child, iter))
  {
    GNUNET_break (GNUNET_YES == GNUNET_FS_file_information_is_directory (fi));
    do
    {
      fic = get_file_information (tm, &child);
      GNUNET_break (GNUNET_OK == GNUNET_FS_file_information_add (fi, fic));
    }
    while (gtk_tree_model_iter_next (tm, &child));
  }
  return fi;
}


/**
 * Closure for 'add_updateable_to_ts'.
 */
struct UpdateableContext
{
  /**
   * Parent of current insertion.
   */
  GtkTreeIter *parent;

  /**
   * Tree store we are modifying.
   */
  GtkTreeStore *ts;

  /**
   * Name of the namespace.
   */
  const char *namespace_name;

  /**
   * Handle to the namespace.
   */
  struct GNUNET_FS_Namespace *ns;

  /**
   * Hash codes of identifiers already added to tree store.
   */
  struct GNUNET_CONTAINER_MultiHashMap *seen;

  /**
   * Did the iterator get called?
   */
  int update_called;
};


/**
 * Add updateable entries to the tree view.
 *
 * @param cls closure
 * @param last_id ID to add
 * @param last_uri associated URI
 * @param last_meta associate meta data
 * @param next_id ID for future updates
 */
static void
add_updateable_to_ts (void *cls, const char *last_id,
                      const struct GNUNET_FS_Uri *last_uri,
                      const struct GNUNET_CONTAINER_MetaData *last_meta,
                      const char *next_id)
{
  struct UpdateableContext *uc = cls;
  struct UpdateableContext sc;
  GtkTreeIter iter;
  GtkTreeIter titer;
  char *desc;
  GNUNET_HashCode hc;

  uc->update_called = GNUNET_YES;
  GNUNET_CRYPTO_hash (last_id, strlen (last_id), &hc);
  if (NULL != GNUNET_CONTAINER_multihashmap_get (uc->seen, &hc))
    return;
  GNUNET_CONTAINER_multihashmap_put (uc->seen, &hc, "dummy",
                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
  /* FIXME: what if this put fails?  Not convinced it cannot... */
  desc =
      GNUNET_CONTAINER_meta_data_get_first_by_types (last_meta,
                                                     EXTRACTOR_METATYPE_DESCRIPTION,
                                                     EXTRACTOR_METATYPE_TITLE,
                                                     EXTRACTOR_METATYPE_BOOK_TITLE,
                                                     EXTRACTOR_METATYPE_FILENAME,
                                                     EXTRACTOR_METATYPE_SUMMARY,
                                                     EXTRACTOR_METATYPE_ALBUM,
                                                     EXTRACTOR_METATYPE_COMMENT,
                                                     EXTRACTOR_METATYPE_SUBJECT,
                                                     -1);
  if (desc == NULL)
    desc = GNUNET_strdup (_("no description supplied"));
  else
  {
    char *utf8_desc = NULL;

    utf8_desc =
        GNUNET_FS_GTK_dubious_meta_to_utf8 (EXTRACTOR_METAFORMAT_UTF8, desc,
                                            strlen (desc) + 1);
    GNUNET_free (desc);
    if (utf8_desc != NULL)
      desc = utf8_desc;
    else
      desc = NULL;
  }
  gtk_tree_store_insert_with_values (uc->ts, &iter, uc->parent, G_MAXINT, 0,
                                     uc->namespace_name, 1, uc->ns, 2, last_id,
                                     3, GNUNET_FS_uri_dup (last_uri), 4,
                                     GNUNET_CONTAINER_meta_data_duplicate
                                     (last_meta), 5, "", 6, desc, 7,
                                     TRUE /* update editable (always) */ ,
                                     8, FALSE
                                     /* current not editable (only for top-level) */
                                     , -1);
  GNUNET_free_non_null (desc);
  sc.parent = &iter;
  sc.ts = uc->ts;
  sc.namespace_name = uc->namespace_name;
  sc.ns = uc->ns;
  sc.seen = uc->seen;
  sc.update_called = GNUNET_NO;
  GNUNET_FS_namespace_list_updateable (uc->ns, next_id, &add_updateable_to_ts,
                                       &sc);
  if ((sc.update_called == GNUNET_NO) && (next_id != NULL) &&
      (strlen (next_id) > 0))
  {
    /* add leaf */
    gtk_tree_store_insert_with_values (uc->ts, &titer, &iter, G_MAXINT, 0,
                                       uc->namespace_name, 1, uc->ns, 2,
                                       next_id, 3, NULL, 4, NULL, 5, "", 6, "",
                                       7, TRUE /* update editable (always) */ ,
                                       8, FALSE
                                       /* current not editable (only for top-level) */
                                       , -1);
  }
}


/**
 * Add all updateable entries of the current namespace to the
 * tree store.
 *
 * @param cls the 'GtkTreeStore' to update
 * @param name name of the namespace to add
 * @param id identity of the namespace to add
 */
static void
add_namespace_to_ts (void *cls, const char *name, const GNUNET_HashCode * id)
{
  GtkTreeStore *ts = cls;
  struct UpdateableContext uc;
  GtkTreeIter iter;

  uc.parent = &iter;
  uc.namespace_name = name;
  uc.ts = ts;
  uc.ns = GNUNET_FS_namespace_create (GNUNET_FS_GTK_get_fs_handle (), name);
  uc.update_called = GNUNET_NO;
  gtk_tree_store_insert_with_values (ts, &iter, NULL, G_MAXINT, 0, name, 1,
                                     uc.ns, 2, NULL /* last-id */ ,
                                     3, NULL /* last-uri (as string!) */ ,
                                     4, NULL /* meta */ ,
                                     5, NULL /* next-ID */ ,
                                     6, NULL /* last-description */ ,
                                     7, TRUE /* update editable */ ,
                                     8, TRUE /* current editable */ ,
                                     -1);
  uc.seen = GNUNET_CONTAINER_multihashmap_create (128);
  GNUNET_FS_namespace_list_updateable (uc.ns, NULL, &add_updateable_to_ts, &uc);
  GNUNET_CONTAINER_multihashmap_destroy (uc.seen);
}


static void
free_pseudonym_tree_store (GtkTreeModel * tm, GtkTreeIter * iter)
{
  struct GNUNET_CONTAINER_MetaData *meta;
  struct GNUNET_FS_Namespace *ns;
  GtkTreeIter child;

  gtk_tree_model_get (tm, iter, 1, &ns, 4, &meta, -1);
  if (meta != NULL)
    GNUNET_CONTAINER_meta_data_destroy (meta);
  if (ns != NULL)
  {
    // FIXME: delete ns?
    // GNUNET_FS_namespace_delete (nso, GNUNET_NO);
  }
  if (TRUE == gtk_tree_model_iter_children (tm, &child, iter))
  {
    do
    {
      free_pseudonym_tree_store (tm, &child);
    }
    while (TRUE == gtk_tree_model_iter_next (tm, &child));
  }
}


static void
free_file_information_tree_store (GtkTreeModel * tm, GtkTreeIter * iter)
{
  GtkTreeIter child;
  struct GNUNET_FS_FileInformation *fip;

  gtk_tree_model_get (tm, iter, 5, &fip, -1);
  if (fip != NULL)
    GNUNET_FS_file_information_destroy (fip, NULL, NULL);
  if (TRUE == gtk_tree_model_iter_children (tm, &child, iter))
  {
    do
    {
      free_file_information_tree_store (tm, &child);
    }
    while (TRUE == gtk_tree_model_iter_next (tm, &child));
  }
}

static int
hide_master_publish_dialog (struct MainPublishingDialogContext *ctx, gint ret)
{
  GtkTreeIter iter;
  gpointer namespace;
  gchar *namespace_id;
  gchar *namespace_uid;
  struct GNUNET_FS_FileInformation *fi;

  /* Don't close until all scanners are finished */
  if (ctx->adddir_head != NULL)
    return GNUNET_NO;

  if (ret == GTK_RESPONSE_OK)
  {
    if (TRUE == gtk_tree_selection_get_selected (ctx->pseudonym_selection, NULL, &iter))
    {
      gtk_tree_model_get (ctx->pseudonym_treemodel, &iter, 1, &namespace, 2, &namespace_id, 5,
                          &namespace_uid, -1);
    }
    else
    {
      namespace = NULL;
      namespace_id = NULL;
      namespace_uid = NULL;
    }
    if (gtk_tree_model_get_iter_first (ctx->file_info_treemodel, &iter))
      do
      {
        fi = get_file_information (ctx->file_info_treemodel, &iter);
        /* FIXME: should we convert namespace id and uid from UTF8? */
        GNUNET_FS_publish_start (GNUNET_FS_GTK_get_fs_handle (), fi, namespace,
                                 namespace_id, namespace_uid,
                                 GNUNET_FS_PUBLISH_OPTION_NONE);
      }
      while (gtk_tree_model_iter_next (ctx->file_info_treemodel, &iter));
    g_free (namespace_id);
    g_free (namespace_uid);
  }

  /* free state from 'ptm' */
  if (TRUE == gtk_tree_model_get_iter_first (ctx->pseudonym_treemodel, &iter))
    do
    {
      free_pseudonym_tree_store (ctx->pseudonym_treemodel, &iter);
    }
    while (TRUE == gtk_tree_model_iter_next (ctx->pseudonym_treemodel, &iter));
  gtk_tree_store_clear (GTK_TREE_STORE (ctx->pseudonym_treemodel));

  /* free state from 'tm' */
  if (TRUE == gtk_tree_model_get_iter_first (ctx->file_info_treemodel, &iter))
    do
    {
      free_file_information_tree_store (ctx->file_info_treemodel, &iter);
    }
    while (TRUE == gtk_tree_model_iter_next (ctx->file_info_treemodel, &iter));
  gtk_tree_store_clear (GTK_TREE_STORE (ctx->file_info_treemodel));
  gtk_widget_destroy (GTK_WIDGET (ctx->master_pubdialog));
  g_object_unref (G_OBJECT (ctx->builder));
  GNUNET_free (ctx);
  return GNUNET_YES;
}


void
GNUNET_GTK_master_publish_dialog_execute_button_clicked_cb (GtkButton * button,
                                                            struct MainPublishingDialogContext *ctx)
{
  hide_master_publish_dialog (ctx, GTK_RESPONSE_OK);
}


void
GNUNET_GTK_master_publish_dialog_cancel_button_clicked_cb (GtkButton * button,
                                                           struct MainPublishingDialogContext *ctx)
{
  hide_master_publish_dialog (ctx, GTK_RESPONSE_CANCEL);
}


gboolean
GNUNET_GTK_master_publish_dialog_delete_event_cb (GtkWidget * widget,
                                                  GdkEvent * event,
                                                  struct MainPublishingDialogContext *ctx)
{
  if (GNUNET_NO == hide_master_publish_dialog (ctx, GTK_RESPONSE_CANCEL))
    return TRUE;
  return FALSE;
}


/**
 */
void
GNUNET_GTK_main_menu_file_publish_activate_cb (GtkWidget * dummy, gpointer user_data)
{
  struct MainPublishingDialogContext *ctx;

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

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

  init_ctx (ctx);
  ctx->main_window_builder = GTK_BUILDER (user_data);
  GNUNET_FS_namespace_list (GNUNET_FS_GTK_get_fs_handle (),
                            &add_namespace_to_ts, GTK_TREE_STORE (ctx->pseudonym_treemodel));
  gtk_window_present (GTK_WINDOW (ctx->master_pubdialog));
}


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