aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/fs/search.c
blob: f789e43fe4b70a8db98abcac1779be96a06e4b7d (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
/*
     This file is part of GNUnet.
     (C) 2005, 2006, 2007, 2008 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/plugins/fs/search.c
 * @brief code for searching with gnunet-gtk
 * @author Christian Grothoff
 */

#include "platform.h"
#include "gnunetgtk_common.h"
#include "search.h"
#include "status.h"
#include "helper.h"
#include "fs.h"
#include "meta.h"
#include <extractor.h>
#include <GNUnet/gnunet_util_crypto.h>
#include <GNUnet/gnunet_namespace_lib.h>
#ifdef HAVE_GIO
#include <gio/gio.h>
#endif


/**
 * The user has edited the search entry.
 * Update search button status.
 */
static void
on_fssearchSelectionChanged (gpointer signal, gpointer cls)
{
  SearchList *list = cls;
  GtkTreeSelection *selection;
  GtkWidget *downloadButton;

  selection = gtk_tree_view_get_selection (list->treeview);
  downloadButton = glade_xml_get_widget (list->searchXML, "downloadButton");
  gtk_widget_set_sensitive (downloadButton,
                            gtk_tree_selection_count_selected_rows (selection)
                            > 0);
}


/* **************** FSUI event handling ****************** */

/**
 * Update the number of results received in the label of the tab.
 */
static void
updateResultsCount (SearchList * searchContext)
{
  char *new_title;
  GtkLabel *label;

  /* update tab title with the number of results */
  new_title =
    g_strdup_printf ("%.*s%s (%u)",
                     20,
                     searchContext->searchString,
                     strlen (searchContext->searchString) > 20 ? "..." : "",
                     searchContext->resultsReceived);
  if (new_title == NULL)
    new_title =
      g_strdup_printf (_("invalid characters (%u)"),
		       searchContext->resultsReceived);
  label = GTK_LABEL (glade_xml_get_widget (searchContext->labelXML,
                                           "searchTabLabel"));
  if (new_title != NULL) 
    {
      gtk_label_set (label, new_title);
      GNUNET_free (new_title);
    }
  else
    {
      gtk_label_set (label, _("Internal error"));
    }
}

static GdkPixbuf *
make_ranking_pixbuf (int availability_rank,
                     unsigned int availability_certainty,
                     unsigned int applicability_rank, unsigned int kwords)
{
  GdkPixbuf *pixbuf;
  guchar *pixels;
  guchar *pixel;
  int n_channels;
  int rowstride;
  unsigned int x;
  unsigned int y;

#define P_HEIGHT 21
#define P_WIDTH 60
  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE,    /* alpha */
                           8,   /* bits per sample */
                           P_WIDTH,     /* width */
                           P_HEIGHT     /* height */
    );
  n_channels = gdk_pixbuf_get_n_channels (pixbuf);
  pixels = gdk_pixbuf_get_pixels (pixbuf);
  rowstride = gdk_pixbuf_get_rowstride (pixbuf);
  for (x = 0; x < P_WIDTH; x++)
    for (y = 0; y < P_HEIGHT; y++)
      {
        pixel = pixels + y * rowstride + x * n_channels;
#define PX_RED 0
#define PX_GREEN 1
#define PX_BLUE 2
#define PX_ALPHA 3
        pixel[PX_RED] = 0;
        pixel[PX_GREEN] = 0;
        pixel[PX_BLUE] = 0;
        pixel[PX_ALPHA] = 0;
        if (y < P_HEIGHT / 2)
          {
            /* applicability */
            if (x * kwords < applicability_rank * P_WIDTH)
              {
                pixel[PX_RED] = 0;
                pixel[PX_GREEN] = 0;
                pixel[PX_BLUE] = 255;
                pixel[PX_ALPHA] = 255;
              }
          }
        else if ((y > P_HEIGHT / 2) &&
                 ((y - P_HEIGHT / 2) * GNUNET_FSUI_MAX_PROBES
                  < availability_certainty * P_HEIGHT / 2))
          {
            /* availability */
            if (availability_rank < 0)
              {
                if ((x * GNUNET_FSUI_MAX_PROBES >
                     ((unsigned int) (GNUNET_FSUI_MAX_PROBES +
                                      availability_rank)) * P_WIDTH / 2)
                    && (x <= P_WIDTH / 2))
                  {
                    pixel[PX_RED] = 255;
                    pixel[PX_GREEN] = 0;
                    pixel[PX_BLUE] = 0;
                    pixel[PX_ALPHA] = 255;
                  }
              }
            else if (availability_rank > 0)
              {
                if ((x >= P_WIDTH / 2) &&
                    ((x - (P_WIDTH / 2)) * GNUNET_FSUI_MAX_PROBES <
                     ((unsigned int) availability_rank) * P_WIDTH / 2))
                  {
                    pixel[PX_RED] = 0;
                    pixel[PX_GREEN] = 255;
                    pixel[PX_BLUE] = 0;
                    pixel[PX_ALPHA] = 255;
                  }
              }
            else
              {
                if (x == P_WIDTH / 2)
                  {
                    /* yellow */
                    pixel[PX_RED] = 255;
                    pixel[PX_GREEN] = 255;
                    pixel[PX_BLUE] = 0;
                    pixel[PX_ALPHA] = 255;
                  }
              }
          }
      }
  return pixbuf;
}

/**
 * Add the given search result to the search
 * tree at the specified position.
 */
void
addEntryToSearchTree (SearchList * searchContext,
                      DownloadList * downloadParent,
                      const GNUNET_ECRS_FileInfo * info, GtkTreeIter * iter)
{
  char *name;
  char *rawMime;
  char *mime;
  char *desc;
  unsigned long long size;
  char *size_h;
  GdkPixbuf *pixbuf;
  GdkPixbuf *rankbuf;
  GdkPixbuf *statusLogo;
#ifdef HAVE_GIO
  GdkPixbuf *icon = NULL;
  GIcon *gicon = NULL;
  const gchar **iconNames;
  int i = 0;
#endif
  enum GNUNET_URITRACK_STATE state;

  state = GNUNET_URITRACK_get_state (ectx, cfg, info->uri);
  rawMime = getMimeTypeFromMetaData (info->meta);
  desc = getDescriptionFromMetaData (info->meta);
  statusLogo = getStatusLogo (state);
  name = getFileNameFromMetaData (info->meta);
  size = GNUNET_ECRS_uri_test_chk (info->uri)
    || GNUNET_ECRS_uri_test_loc (info->uri) ?
    GNUNET_ECRS_uri_get_file_size (info->uri) : 0;
  pixbuf = getThumbnailFromMetaData (info->meta);
  size_h = GNUNET_get_byte_size_as_fancy_string (size);
  rankbuf = make_ranking_pixbuf (0, 0, 1,
                                 GNUNET_ECRS_uri_get_keyword_count_from_ksk
                                 (searchContext->uri));
#ifdef HAVE_GIO
  if (0 == strcmp (rawMime, GNUNET_DIRECTORY_MIME))
    {
      mime = GNUNET_strdup (_("Directory"));
      icon = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
                                       GTK_STOCK_DIRECTORY, 16, 0,
                                       (GError **) NULL);
    }
  else
    {
      mime = g_content_type_get_description (rawMime);
      gicon = g_content_type_get_icon (rawMime);
      if (G_IS_THEMED_ICON (gicon))
        {
          iconNames = (const gchar **) g_themed_icon_get_names
            (G_THEMED_ICON (gicon));
          do
            {
              icon = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (),
                                               iconNames[i], 16, 0,
                                               (GError **) NULL);
              i++;
            }
          while ((icon == NULL) && iconNames[i]);
        }
    }
#else
  mime = GNUNET_strdup (rawMime);
#endif

  gtk_tree_store_set (searchContext->tree, iter,
                      SEARCH_NAME, name, SEARCH_SIZE, size, SEARCH_HSIZE,
                      size_h, SEARCH_MIME, mime,
                      SEARCH_RAW_MIME, rawMime, SEARCH_DESC, desc,
                      SEARCH_PIXBUF, pixbuf, SEARCH_URI,
                      GNUNET_ECRS_uri_duplicate (info->uri), SEARCH_META,
                      GNUNET_meta_data_duplicate (info->meta),
                      SEARCH_INTERNAL, searchContext,
                      SEARCH_INTERNAL_PARENT, downloadParent,
                      SEARCH_STATUS, getStatusName (state),
                      SEARCH_STATUS_LOGO, statusLogo,
                      SEARCH_APPLICABILITY_RANK, 1, SEARCH_RANK_SORT,
                      (long long) 1, SEARCH_RANK_PIXBUF, rankbuf,
#ifdef HAVE_GIO
                      SEARCH_ICON, icon,
#endif
                      -1);
  g_object_unref (rankbuf);
  if (pixbuf != NULL)
    g_object_unref (pixbuf);
  if (statusLogo != NULL)
    g_object_unref (statusLogo);
#ifdef HAVE_GIO
  if (gicon != NULL)
    g_object_unref (gicon);
  if (icon != NULL)
    g_object_unref (icon);
#endif
  GNUNET_free (size_h);
  GNUNET_free (name);
  GNUNET_free (desc);
  GNUNET_free (rawMime);
  GNUNET_free (mime);
}

/**
 * Add the given result to the model (search result
 * list).
 *
 * @param info the information to add to the model
 * @param uri the search URI
 * @param searchContext identifies the search page
 */
void
fs_search_result_received (SearchList * searchContext,
                           const GNUNET_ECRS_FileInfo * info,
                           const struct GNUNET_ECRS_URI *uri)
{
  GtkTreeStore *model;
  GtkTreeIter iter;
  enum GNUNET_URITRACK_STATE state;
  struct GNUNET_ECRS_URI *have;

  state = GNUNET_URITRACK_get_state (ectx, cfg, info->uri);
  if ((state & (GNUNET_URITRACK_INSERTED |
                GNUNET_URITRACK_INDEXED)) &&
      (GNUNET_YES == GNUNET_GC_get_configuration_value_yesno (cfg,
                                                              "GNUNET-GTK",
                                                              "DISABLE-OWN",
                                                              GNUNET_NO)))
    return;
  model = GTK_TREE_STORE (gtk_tree_view_get_model (searchContext->treeview));
  /* Check that the entry does not already exist (for resume!) */
  if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter))
    {
      do
        {
          have = NULL;
          gtk_tree_model_get (GTK_TREE_MODEL (model),
                              &iter, SEARCH_URI, &have, -1);
          if ((have != NULL) && (GNUNET_ECRS_uri_test_equal (have, uri)))
            return;             /* duplicate */
        }
      while (gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter));
    }
  gtk_tree_store_append (model, &iter, NULL);
  addEntryToSearchTree (searchContext, NULL, info, &iter);
  searchContext->resultsReceived++;
  updateResultsCount (searchContext);
}

/**
 * Update the applicability and availability rating
 * for the given search result.
 *
 * @param info the search result (and metadata)
 * @param availability_rank availability estimate
 * @param applicability_rank relevance
 */
void
fs_search_update (SearchList * searchContext,
                  const GNUNET_ECRS_FileInfo * info,
                  int availability_rank,
                  unsigned int availability_certainty,
                  unsigned int applicability_rank)
{
  enum GNUNET_URITRACK_STATE state;
  GtkTreeStore *model;
  GtkTreeIter iter;
  struct GNUNET_ECRS_URI *have;
  GdkPixbuf *pixbuf;
  long long rank;
  unsigned int kwords;

  state = GNUNET_URITRACK_get_state (ectx, cfg, info->uri);
  if ((state & (GNUNET_URITRACK_INSERTED |
                GNUNET_URITRACK_INDEXED)) &&
      (GNUNET_YES == GNUNET_GC_get_configuration_value_yesno (cfg,
                                                              "GNUNET-GTK",
                                                              "DISABLE-OWN",
                                                              GNUNET_NO)))
    return;
  kwords = GNUNET_ECRS_uri_get_keyword_count_from_ksk (searchContext->uri);
  model = GTK_TREE_STORE (gtk_tree_view_get_model (searchContext->treeview));
  /* find existing entry */
  if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (model), &iter))
    {
      do
        {
          have = NULL;
          gtk_tree_model_get (GTK_TREE_MODEL (model),
                              &iter, SEARCH_URI, &have, -1);
          if ((have != NULL) &&
              (GNUNET_ECRS_uri_test_equal (have, info->uri)))
            {
              /* gotcha, create pixbuf and rank info! */
              rank =
                (int) applicability_rank +
                (int) (availability_rank * (int) availability_certainty *
                       65536);
              pixbuf =
                make_ranking_pixbuf (availability_rank,
                                     availability_certainty,
                                     applicability_rank, kwords);
              gtk_tree_store_set (searchContext->tree, &iter,
                                  SEARCH_AVAILABILITY_RANK, availability_rank,
                                  SEARCH_AVAILABILITY_CERTAINTY,
                                  availability_certainty,
                                  SEARCH_APPLICABILITY_RANK,
                                  applicability_rank, SEARCH_RANK_PIXBUF,
                                  pixbuf, SEARCH_RANK_SORT, rank, -1);
              g_object_unref (pixbuf);
              return;           /* done! */
            }
        }
      while (gtk_tree_model_iter_next (GTK_TREE_MODEL (model), &iter));
    }
  /* not found!? */
  GNUNET_GE_BREAK (NULL, 0);
}


static int
on_search_display_metadata_activate (void *cls, GtkWidget * searchEntry)
{
  SearchList *list = cls;
  GtkTreePath *path;
  GtkTreeIter iter;
  struct GNUNET_ECRS_URI *uri;
  struct GNUNET_MetaData *meta;
  char *str;

  path = NULL;
  if (FALSE == gtk_tree_view_get_path_at_pos (list->treeview,
                                              list->last_x,
                                              list->last_y,
                                              &path, NULL, NULL, NULL))
    {
      /* nothing selected */
      return FALSE;
    }
  if (FALSE == gtk_tree_model_get_iter (GTK_TREE_MODEL (list->tree),
                                        &iter, path))
    {
      GNUNET_GE_BREAK (NULL, 0);
      gtk_tree_path_free (path);
      return FALSE;
    }
  gtk_tree_path_free (path);
  uri = NULL;
  meta = NULL;
  gtk_tree_model_get (GTK_TREE_MODEL (list->tree),
                      &iter, SEARCH_URI, &uri, SEARCH_META, &meta, -1);
  str = GNUNET_ECRS_uri_to_string (uri);
  open_meta_data_display_dialog(meta, str);
  GNUNET_free_non_null (str);
  return FALSE;
}


static int
on_search_copy_uri_activate (void *cls, GtkWidget * searchEntry)
{
  SearchList *list = cls;
  GtkTreePath *path;
  GtkTreeIter iter;
  struct GNUNET_ECRS_URI *uri;
  char *str;
  GtkClipboard *clip;

  path = NULL;
  if (FALSE == gtk_tree_view_get_path_at_pos (list->treeview,
                                              list->last_x,
                                              list->last_y,
                                              &path, NULL, NULL, NULL))
    {
      GNUNET_GE_BREAK (NULL, 0);
      return FALSE;
    }
  if (FALSE == gtk_tree_model_get_iter (GTK_TREE_MODEL (list->tree),
                                        &iter, path))
    {
      GNUNET_GE_BREAK (NULL, 0);
      gtk_tree_path_free (path);
      return FALSE;
    }
  gtk_tree_path_free (path);
  uri = NULL;
  gtk_tree_model_get (GTK_TREE_MODEL (list->tree),
                      &iter, SEARCH_URI, &uri, -1);
  str = GNUNET_ECRS_uri_to_string (uri);
  clip = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
  gtk_clipboard_set_text (clip, str, strlen (str));
  GNUNET_free (str);
  return FALSE;
}


#ifndef MINGW
static char *
selectFile ()
{
  GladeXML *uploadXML;
  GtkFileChooser *dialog;
  char *ret;
  gint gret;

  uploadXML
    = glade_xml_new (GNUNET_GTK_get_glade_filename (),
                     "directorySaveDialog", PACKAGE_NAME);
  GNUNET_GTK_connect_glade_with_plugins (uploadXML);
  dialog = GTK_FILE_CHOOSER (glade_xml_get_widget (uploadXML,
                                                   "directorySaveDialog"));
  gret = gtk_dialog_run (GTK_DIALOG (dialog));
  gtk_widget_destroy (GTK_WIDGET (dialog));
  if (gret != GTK_RESPONSE_CANCEL)
    ret = gtk_file_chooser_get_filename (dialog);
  else
    ret = NULL;
  UNREF (uploadXML);
  return ret;
}

#else /* MINGW */

static char *
selectFile ()
{
  return
    plibc_ChooseFile (_
                      ("Choose the name under which you want to save the search results."),
                      OFN_SHAREAWARE);
}
#endif /* MINGW */

static int
on_save_search_activate (void *cls, GtkWidget * searchEntry)
{
  SearchList *list = cls;
  char *name;
  char *directory;
  unsigned long long dir_len;
  unsigned int fis_size;
  GNUNET_ECRS_FileInfo *fis;
  struct GNUNET_MetaData *meta;
  GtkTreeModel *model;
  GtkTreeIter iter;
  unsigned int pos;

  model = gtk_tree_view_get_model (list->treeview);
  if (TRUE != gtk_tree_model_get_iter_first (model, &iter))
    {
      GNUNET_GTK_add_log_entry (_("No search results yet, cannot save!"));
      return FALSE;
    }

  name = selectFile ("");
  if (name == NULL)
    return FALSE;
  fis = NULL;
  fis_size = 0;
  GNUNET_array_grow (fis, fis_size, list->resultsReceived);
  pos = 0;
  do
    {
      if (pos == fis_size)
        GNUNET_array_grow (fis, fis_size, pos + 1);
      gtk_tree_model_get (model,
                          &iter,
                          SEARCH_URI, &fis[pos].uri,
                          SEARCH_META, &fis[pos].meta, -1);
      pos++;
    }
  while (gtk_tree_model_iter_next (model, &iter));
  meta = GNUNET_meta_data_create ();
  GNUNET_meta_data_insert (meta, EXTRACTOR_KEYWORDS, list->searchString);
  GNUNET_meta_data_insert (meta, EXTRACTOR_DESCRIPTION,
                           _("Saved search results"));
  GNUNET_meta_data_insert (meta, EXTRACTOR_SOFTWARE, "gnunet-gtk");
  if (GNUNET_OK != GNUNET_ECRS_directory_create (NULL,
                                                 &directory, &dir_len,
                                                 fis_size, fis, meta))
    {
      GNUNET_GTK_add_log_entry (_("Internal error."));
      GNUNET_GE_BREAK (NULL, 0);
      GNUNET_meta_data_destroy (meta);
      GNUNET_array_grow (fis, fis_size, 0);
      GNUNET_free (name);
      return FALSE;
    }
  GNUNET_meta_data_destroy (meta);
  GNUNET_array_grow (fis, fis_size, 0);
  if (GNUNET_OK !=
      GNUNET_disk_file_write (NULL, name, directory, dir_len, "644"))
    {
      GNUNET_GTK_add_log_entry (_("Error writing file `%s'."), name);
    }
  GNUNET_free (directory);
  GNUNET_free (name);
  return FALSE;
}

static gint
search_click_handler (void *cls, GdkEvent * event)
{
  SearchList *list = cls;
  GtkMenu *menu;
  GtkWidget *entry;
  GdkEventButton *event_button;

  if ((event == NULL) || (event->type != GDK_BUTTON_PRESS))
    return FALSE;
  event_button = (GdkEventButton *) event;
  if (event_button->button != 3)
    return FALSE;
  list->last_x = event_button->x;
  list->last_y = event_button->y;
  menu = GTK_MENU (gtk_menu_new ());

  entry = gtk_menu_item_new_with_label (_("_Display metadata"));
  g_signal_connect_swapped (entry,
                            "activate",
                            G_CALLBACK (on_search_display_metadata_activate),
                            list);
  gtk_label_set_use_underline (GTK_LABEL
                               (gtk_bin_get_child (GTK_BIN (entry))), TRUE);
  gtk_widget_show (entry);
  gtk_menu_shell_append (GTK_MENU_SHELL (menu), entry);

  entry = gtk_menu_item_new_with_label (_("_Copy URI to Clipboard"));
  g_signal_connect_swapped (entry,
                            "activate",
                            G_CALLBACK (on_search_copy_uri_activate), list);
  gtk_label_set_use_underline (GTK_LABEL
                               (gtk_bin_get_child (GTK_BIN (entry))), TRUE);
  gtk_widget_show (entry);
  gtk_menu_shell_append (GTK_MENU_SHELL (menu), entry);

  entry = gtk_menu_item_new_with_label (_("_Save results as directory"));
  g_signal_connect_swapped (entry,
                            "activate",
                            G_CALLBACK (on_save_search_activate), list);
  gtk_label_set_use_underline (GTK_LABEL
                               (gtk_bin_get_child (GTK_BIN (entry))), TRUE);
  gtk_widget_show (entry);
  gtk_menu_shell_append (GTK_MENU_SHELL (menu), entry);


  gtk_menu_popup (menu,
                  NULL,
                  NULL, NULL, NULL, event_button->button, event_button->time);
  return TRUE;
}

/**
 * FSUI event: a search was started; create the tab
 */
SearchList *
fs_search_started (struct GNUNET_FSUI_SearchList * fsui_list,
                   const struct GNUNET_ECRS_URI * uri,
                   unsigned int anonymityLevel,
                   unsigned int resultCount,
                   const GNUNET_ECRS_FileInfo * results,
                   GNUNET_FSUI_State state)
{
  SearchList *list;
  gint pages;
  char *description;
  GtkTreeViewColumn *column;
  GtkCellRenderer *renderer;
  GtkNotebook *notebook;
  int col;
  unsigned int i;

  /* check that search does not already exist
     with fsui_list == NULL;
     (and if so, hijack!) */
  list = search_head;
  while (list != NULL)
    {
      if ((list->fsui_list == NULL) &&
          (list->uri != NULL) &&
          (GNUNET_ECRS_uri_test_equal (list->uri, uri)))
        {
          list->fsui_list = fsui_list;
          for (i = 0; i < resultCount; i++)
            fs_search_result_received (list, &results[i], uri);
          if (resultCount == 0) /* otherwise already done! */
            updateResultsCount (list);
          return list;
        }
      list = list->next;
    }

  /* build new entry */
  if (GNUNET_ECRS_uri_test_ksk (uri))
    description = GNUNET_ECRS_ksk_uri_to_human_readable_string (uri);
  else
    description = GNUNET_NS_sks_uri_to_human_readable_string (ectx, cfg, uri);
  if (description == NULL)
    {
      GNUNET_GE_BREAK (ectx, 0);
      return NULL;
    }
  list = GNUNET_malloc (sizeof (SearchList));
  memset (list, 0, sizeof (SearchList));
  list->searchString = description;
  list->uri = GNUNET_ECRS_uri_duplicate (uri);
  list->fsui_list = fsui_list;
  list->next = search_head;
  list->anonymityLevel = anonymityLevel;

  search_head = list;
  list->searchXML
    =
    glade_xml_new (GNUNET_GTK_get_glade_filename (), "searchResultsFrame",
                   PACKAGE_NAME);
  GNUNET_GTK_connect_glade_with_plugins (list->searchXML);
  list->searchpage
    =
    GNUNET_GTK_extract_main_widget_from_window (list->searchXML,
                                                "searchResultsFrame");
  /* setup tree view and renderers */
  list->treeview = GTK_TREE_VIEW (glade_xml_get_widget (list->searchXML,
                                                        "searchResults"));
  g_signal_connect_swapped (list->treeview,
                            "button-press-event",
                            G_CALLBACK (search_click_handler), list);
  list->tree = gtk_tree_store_new (SEARCH_NUM, G_TYPE_STRING,   /* name */
                                   G_TYPE_UINT64,       /* size */
                                   G_TYPE_STRING,       /* human-readable size */
                                   G_TYPE_STRING,       /* mime-type */
                                   G_TYPE_STRING,       /* raw mime-type */
                                   G_TYPE_STRING,       /* meta-data (some) */
                                   GDK_TYPE_PIXBUF,     /* preview */
                                   G_TYPE_POINTER,      /* url */
                                   G_TYPE_POINTER,      /* meta */
                                   G_TYPE_POINTER,      /* internal: search list */
                                   G_TYPE_POINTER,      /* internal: download parent list */
                                   G_TYPE_STRING,       /* status */
                                   GDK_TYPE_PIXBUF,     /* status (icon) */
                                   G_TYPE_INT,  /* availability rank */
                                   G_TYPE_UINT, /* availability certainty */
                                   G_TYPE_UINT, /* applicability rank */
                                   GDK_TYPE_PIXBUF,     /* ranking visualization */
                                   G_TYPE_INT64 /* numeric sort */
#ifdef HAVE_GIO
                                   , GDK_TYPE_PIXBUF    /* icon */
#endif
    );

  gtk_tree_view_set_model (list->treeview, GTK_TREE_MODEL (list->tree));
  gtk_tree_selection_set_mode (gtk_tree_view_get_selection (list->treeview),
                               GTK_SELECTION_MULTIPLE);

  g_signal_connect_data (gtk_tree_view_get_selection (list->treeview),
                         "changed",
                         G_CALLBACK (&on_fssearchSelectionChanged),
                         list, NULL, 0);

  column = gtk_tree_view_column_new ();
  gtk_tree_view_column_set_title (column, _("Name"));
#ifdef HAVE_GIO
  renderer = gtk_cell_renderer_pixbuf_new ();
  gtk_tree_view_column_pack_start (column, renderer, FALSE);
  gtk_tree_view_column_set_attributes (column, renderer, "pixbuf",
                                       SEARCH_ICON, NULL);
#endif
  renderer = gtk_cell_renderer_text_new ();
  gtk_tree_view_column_pack_start (column, renderer, TRUE);
  g_object_set (G_OBJECT (renderer),
                "wrap-width", 45,
                "width-chars", 45, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
  gtk_tree_view_column_set_attributes (column, renderer, "text",
                                       SEARCH_NAME, NULL);
  col = gtk_tree_view_insert_column (list->treeview, column, 0);
  gtk_tree_view_column_set_resizable (column, TRUE);
  gtk_tree_view_column_set_clickable (column, TRUE);
  gtk_tree_view_column_set_reorderable (column, TRUE);
  gtk_tree_view_column_set_sort_column_id (column, SEARCH_NAME);

  renderer = gtk_cell_renderer_pixbuf_new ();
  col = gtk_tree_view_insert_column_with_attributes (list->treeview,
                                                     -1,
                                                     _("Status"),
                                                     renderer,
                                                     "pixbuf",
                                                     SEARCH_STATUS_LOGO,
                                                     NULL);
  column = gtk_tree_view_get_column (list->treeview, col - 1);
  gtk_tree_view_column_set_resizable (column, TRUE);
  gtk_tree_view_column_set_clickable (column, TRUE);
  gtk_tree_view_column_set_reorderable (column, TRUE);
  gtk_tree_view_column_set_sort_column_id (column, SEARCH_STATUS);
  gtk_tree_view_column_set_min_width (column, 20);

  renderer = gtk_cell_renderer_text_new ();
  g_object_set (renderer, "xalign", 1.00, NULL);
  col = gtk_tree_view_insert_column_with_attributes (list->treeview,
                                                     -1,
                                                     _("Size"),
                                                     renderer,
                                                     "text", SEARCH_HSIZE,
                                                     NULL);
  column = gtk_tree_view_get_column (list->treeview, col - 1);
  gtk_tree_view_column_set_resizable (column, TRUE);
  gtk_tree_view_column_set_clickable (column, TRUE);
  gtk_tree_view_column_set_reorderable (column, TRUE);
  gtk_tree_view_column_set_sort_column_id (column, SEARCH_SIZE);

#if 0
  /* colums for data visualized graphically */
  renderer = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (list->treeview,
                                               -1,
                                               _("Availability"),
                                               renderer,
                                               "text",
                                               SEARCH_AVAILABILITY_RANK,
                                               NULL);
  renderer = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (list->treeview,
                                               -1,
                                               _("Certainty"),
                                               renderer,
                                               "text",
                                               SEARCH_AVAILABILITY_CERTAINTY,
                                               NULL);
  renderer = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (list->treeview,
                                               -1,
                                               _("Applicability"),
                                               renderer,
                                               "text",
                                               SEARCH_APPLICABILITY_RANK,
                                               NULL);
  renderer = gtk_cell_renderer_text_new ();
  gtk_tree_view_insert_column_with_attributes (list->treeview,
                                               -1,
                                               _("Sort"),
                                               renderer,
                                               "text", SEARCH_RANK_SORT,
                                               NULL);
#endif

  renderer = gtk_cell_renderer_pixbuf_new ();
  col = gtk_tree_view_insert_column_with_attributes (list->treeview,
                                                     -1,
                                                     _("Ranking"),
                                                     renderer,
                                                     "pixbuf",
                                                     SEARCH_RANK_PIXBUF,
                                                     NULL);
  column = gtk_tree_view_get_column (list->treeview, col - 1);
  gtk_tree_view_column_set_resizable (column, FALSE);
  gtk_tree_view_column_set_clickable (column, TRUE);
  gtk_tree_view_column_set_reorderable (column, TRUE);
  gtk_tree_view_column_set_sort_column_id (column, SEARCH_RANK_SORT);


  if (GNUNET_YES != GNUNET_GC_get_configuration_value_yesno (cfg,
                                                             "GNUNET-GTK",
                                                             "DISABLE-PREVIEWS",
                                                             GNUNET_NO))
    {
      renderer = gtk_cell_renderer_pixbuf_new ();
      col = gtk_tree_view_insert_column_with_attributes (list->treeview,
                                                         -1,
                                                         _("Preview"),
                                                         renderer,
                                                         "pixbuf",
                                                         SEARCH_PIXBUF, NULL);
      column = gtk_tree_view_get_column (list->treeview, col - 1);
      gtk_tree_view_column_set_resizable (column, TRUE);
      gtk_tree_view_column_set_reorderable (column, TRUE);
      gtk_tree_view_column_set_resizable (column, TRUE);
    }

  renderer = gtk_cell_renderer_text_new ();
  col = gtk_tree_view_insert_column_with_attributes (list->treeview,
                                                     -1,
                                                     _("Meta-data"),
                                                     renderer,
                                                     "text", SEARCH_DESC,
                                                     NULL);
  column = gtk_tree_view_get_column (list->treeview, col - 1);
  g_object_set (G_OBJECT (renderer),
                "wrap-width", 60,
                "width-chars", 60,
                "wrap-mode", PANGO_WRAP_WORD_CHAR,
                "ellipsize", PANGO_ELLIPSIZE_END,
                "ellipsize-set", TRUE, NULL);
  gtk_tree_view_column_set_resizable (column, TRUE);
  gtk_tree_view_column_set_clickable (column, TRUE);
  gtk_tree_view_column_set_reorderable (column, TRUE);
  gtk_tree_view_column_set_sort_column_id (column, SEARCH_DESC);

  /* load label */
  list->labelXML
    = glade_xml_new (GNUNET_GTK_get_glade_filename (),
                     "searchTabLabelWindow", PACKAGE_NAME);
  GNUNET_GTK_connect_glade_with_plugins (list->labelXML);
  list->tab_label
    =
    GNUNET_GTK_extract_main_widget_from_window (list->labelXML,
                                                "searchTabLabelWindow");
  /* process existing results */
  for (i = 0; i < resultCount; i++)
    fs_search_result_received (list, &results[i], uri);
  if (resultCount == 0)         /* otherwise already done! */
    updateResultsCount (list);

  /* insert new page into search notebook */
  notebook
    =
    GTK_NOTEBOOK (glade_xml_get_widget
                  (GNUNET_GTK_get_main_glade_XML (), "downloadNotebook"));
  pages = gtk_notebook_get_n_pages (GTK_NOTEBOOK (notebook));
  gtk_notebook_append_page (notebook, list->searchpage, list->tab_label);
  gtk_notebook_set_current_page (notebook, pages);
  gtk_widget_show (GTK_WIDGET (notebook));      /* may have been hidden! */

  return list;
}

/**
 * Recursively free the (internal) model data fields
 * (uri and meta) from the search tree model.
 */
static void
freeIterSubtree (GtkTreeModel * tree, GtkTreeIter * iter)
{
  GtkTreeIter child;
  struct GNUNET_ECRS_URI *uri;
  struct GNUNET_MetaData *meta;

  do
    {
      uri = NULL;
      meta = NULL;
      gtk_tree_model_get (tree,
                          iter, SEARCH_URI, &uri, SEARCH_META, &meta, -1);
      if (uri != NULL)
        GNUNET_ECRS_uri_destroy (uri);
      if (meta != NULL)
        GNUNET_meta_data_destroy (meta);
      gtk_tree_store_set (GTK_TREE_STORE (tree),
                          iter, SEARCH_URI, NULL, SEARCH_META, NULL, -1);
      if (gtk_tree_model_iter_children (tree, &child, iter))
        freeIterSubtree (tree, &child);
    }
  while (gtk_tree_model_iter_next (tree, iter));
}

/**
 * FSUI event: a search was aborted.
 * Update views accordingly.
 */
void
fs_search_aborted (SearchList * list)
{
  gtk_widget_show (glade_xml_get_widget (list->searchXML,
                                         "searchResumeButton"));
  gtk_widget_show (glade_xml_get_widget (list->searchXML,
                                         "searchPauseButton"));
}

void
fs_search_paused (SearchList * list)
{
  /* nothing to be done */
}

void
fs_search_restarted (SearchList * list)
{
  /* nothing to be done */
}

/**
 * FSUI event: a search was stopped.
 * Remove the respective tab.
 */
void
fs_search_stopped (SearchList * list)
{
  GtkTreeIter iter;
  GtkNotebook *notebook;
  SearchList *prev;
  DownloadList *downloads;
  int index;
  int i;

  /* remove from linked list */
  if (search_head == list)
    {
      search_head = search_head->next;
    }
  else
    {
      prev = search_head;
      while (prev->next != list)
        prev = prev->next;
      prev->next = list->next;
    }

  /* remove links from download views */
  downloads = download_head;
  while (downloads != NULL)
    {
      if (downloads->searchList == list)
        {
          gtk_tree_row_reference_free (downloads->searchViewRowReference);
          downloads->searchViewRowReference = NULL;
          downloads->searchList = NULL;
        }
      downloads = downloads->next;
    }

  /* remove page from notebook */
  notebook
    =
    GTK_NOTEBOOK (glade_xml_get_widget
                  (GNUNET_GTK_get_main_glade_XML (), "downloadNotebook"));
  index = -1;
  for (i = gtk_notebook_get_n_pages (notebook) - 1; i >= 0; i--)
    if (list->searchpage == gtk_notebook_get_nth_page (notebook, i))
      index = i;
  GNUNET_GE_BREAK (ectx, index != -1);
  gtk_notebook_remove_page (notebook, index);
  if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list->tree), &iter))
    freeIterSubtree (GTK_TREE_MODEL (list->tree), &iter);
  /* free list state itself */
  UNREF (list->searchXML);
  UNREF (list->labelXML);
  GNUNET_free (list->searchString);
  GNUNET_ECRS_uri_destroy (list->uri);
  GNUNET_free (list);
}

/* ****************** User event handling ************* */


/**
 * The user has edited the search entry.
 * Update search button status.
 */
void
on_fssearchKeywordComboBoxEntry_changed_fs (gpointer dummy2,
                                            GtkWidget * searchEntry)
{
  const char *searchString;
  GtkWidget *searchButton;

  searchString = getEntryLineValue (GNUNET_GTK_get_main_glade_XML (),
                                    "fssearchKeywordComboBoxEntry");
  searchButton =
    glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (), "fssearchbutton");
  gtk_widget_set_sensitive (searchButton, strlen (searchString) > 0);
}

typedef struct
{
  unsigned int anonymity;
  struct GNUNET_ECRS_URI *uri;
} FSSS;

static void *
search_start_helper (void *cls)
{
  FSSS *fsss = cls;
  GNUNET_FSUI_search_start (ctx, fsss->anonymity, fsss->uri);
  return NULL;
}

/**
 * The user has clicked the "SEARCH" button.
 * Initiate a search.
 */
void
on_fssearchbutton_clicked_fs (gpointer dummy2, GtkWidget * searchButton)
{
  FSSS fsss;
  const char *searchString;
  gint pages;
  gint i;
  SearchList *list;
  GtkTreeIter iter;
  GtkComboBox *searchKeywordGtkCB;
  GtkWidget *searchNamespaceGtkCB;
  GtkNotebook *notebook;

  searchString = getEntryLineValue (GNUNET_GTK_get_main_glade_XML (),
                                    "fssearchKeywordComboBoxEntry");
  if ((searchString == NULL) || (strlen (searchString) == 0))
    {
      GNUNET_GE_LOG (ectx,
                     GNUNET_GE_ERROR | GNUNET_GE_USER | GNUNET_GE_IMMEDIATE,
                     _("Need a keyword to search!\n"));
      return;
    }
  /* add the keyword to the list of keywords that have
     been used so far */
  searchKeywordGtkCB
    = GTK_COMBO_BOX (glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                                           "fssearchKeywordComboBoxEntry"));
  i = gtk_combo_box_get_active (searchKeywordGtkCB);
  if (i == -1)
    {
      GtkListStore *model;

      model = GTK_LIST_STORE (gtk_combo_box_get_model (searchKeywordGtkCB));
      gtk_list_store_prepend (model, &iter);
      gtk_list_store_set (model, &iter, 0, searchString, -1);
    }
  fsss.uri = NULL;
  /* check for namespace search */
  searchNamespaceGtkCB
    =
    glade_xml_get_widget (GNUNET_GTK_get_main_glade_XML (),
                          "searchNamespaceComboBoxEntry");
  if (TRUE ==
      gtk_combo_box_get_active_iter (GTK_COMBO_BOX (searchNamespaceGtkCB),
                                     &iter))
    {
      char *descStr;
      char *nsName;

      nsName = NULL;
      descStr = NULL;
      gtk_tree_model_get (gtk_combo_box_get_model
                          (GTK_COMBO_BOX (searchNamespaceGtkCB)), &iter,
                          NS_SEARCH_DESCRIPTION, &descStr, NS_SEARCH_NAME,
                          &nsName, -1);

      if ((descStr != NULL) &&
	  ( (0 == strcmp (descStr, "")) ||
	    (0 == strcmp (descStr, _("globally")))))
        {
          nsName = NULL;
        }
      else
        {
          if ((descStr == NULL) && (nsName != NULL))
            descStr = GNUNET_strdup (nsName);
        }
      if (nsName != NULL)
        {
          char *ustring;
          GNUNET_EncName enc;
          GNUNET_HashCode nsid;

          GNUNET_GE_ASSERT (NULL,
                            GNUNET_OK ==
                            GNUNET_pseudonym_name_to_id (ectx, cfg,
                                                         nsName, &nsid));
          GNUNET_hash_to_enc (&nsid, &enc);

          ustring =
            GNUNET_malloc (strlen (searchString) + sizeof (GNUNET_EncName) +
                           strlen (GNUNET_ECRS_URI_PREFIX) +
                           strlen (GNUNET_ECRS_SUBSPACE_INFIX) + 10);
          strcpy (ustring, GNUNET_ECRS_URI_PREFIX);
          strcat (ustring, GNUNET_ECRS_SUBSPACE_INFIX);
          strcat (ustring, (const char *) &enc);
          strcat (ustring, "/");
          strcat (ustring, searchString);
          fsss.uri = GNUNET_ECRS_string_to_uri (ectx, ustring);
          if (fsss.uri == NULL)
            {
              GNUNET_GE_LOG (ectx,
                             GNUNET_GE_ERROR | GNUNET_GE_BULK |
                             GNUNET_GE_USER,
                             _("Failed to create namespace URI from `%s'.\n"),
                             ustring);
            }
          GNUNET_free (ustring);
        }
      if (descStr != NULL)
        free (descStr);
      if (nsName != NULL)
        free (nsName);
    }
  else
    {
      /* FIXME: may still be namespace-search
	 -- where user manually entered NS name*/
    }
  if (fsss.uri == NULL)
    fsss.uri = GNUNET_ECRS_keyword_string_to_uri (ectx, searchString);
  if (fsss.uri == NULL)
    {
      GNUNET_GE_BREAK (ectx, 0);
      return;
    }
  /* check if search is already running */
  notebook
    =
    GTK_NOTEBOOK (glade_xml_get_widget
                  (GNUNET_GTK_get_main_glade_XML (), "downloadNotebook"));
  pages = gtk_notebook_get_n_pages (notebook);
  list = search_head;
  while (list != NULL)
    {
      if (GNUNET_ECRS_uri_test_equal (list->uri, fsss.uri))
        {
          for (i = 0; i < pages; i++)
            {
              if (gtk_notebook_get_nth_page (notebook, i) == list->searchpage)
                {
                  gtk_notebook_set_current_page (notebook, i);
                  GNUNET_ECRS_uri_destroy (fsss.uri);
                  return;
                }
            }
          GNUNET_GE_BREAK (ectx, 0);
        }
      list = list->next;
    }
  fsss.anonymity = getSpinButtonValue (GNUNET_GTK_get_main_glade_XML (),
                                       "searchAnonymitySelectionSpinButton");
  GNUNET_GTK_run_with_save_calls (search_start_helper, &fsss);
  GNUNET_ECRS_uri_destroy (fsss.uri);
}

struct FCBC
{
  int (*method) (struct GNUNET_FSUI_SearchList * list);
  struct GNUNET_FSUI_SearchList *argument;
};

static void *
fsui_callback (void *cls)
{
  struct FCBC *fcbc = cls;
  fcbc->method (fcbc->argument);
  return NULL;
}

/**
 * This method is called when the user clicks on either
 * the "CLOSE" button (at the bottom of the search page)
 * or on the "CANCEL (X)" button in the TAB of the
 * search notebook.  Note that "searchPage" can thus
 * either refer to the main page in the tab or to the
 * main entry of the tab label.
 */
void
on_closeSearchButton_clicked_fs (GtkWidget * searchPage,
                                 GtkWidget * closeButton)
{
  SearchList *list;
  struct FCBC fcbc;

  list = search_head;
  while (list != NULL)
    {
      if ((list->searchpage == searchPage) || (list->tab_label == searchPage))
        break;
      list = list->next;
    }
  GNUNET_GE_ASSERT (ectx, list != NULL);
  if (list->fsui_list == NULL)
    {
      /* open directory or paused search;
         close directly */
      fs_search_stopped (list);
    }
  else
    {
      /* actual search - close via FSUI */
      fcbc.method = &GNUNET_FSUI_search_abort;
      fcbc.argument = list->fsui_list;
      GNUNET_GTK_run_with_save_calls (&fsui_callback, &fcbc);
      fcbc.method = &GNUNET_FSUI_search_stop;
      GNUNET_GTK_run_with_save_calls (&fsui_callback, &fcbc);
    }
}


/**
 * The pause button in a search results tab was clicked.
 */
void
on_searchPauseButton_clicked_fs (GtkWidget * searchPage,
                                 GtkWidget * pauseButton)
{
  SearchList *list;
  struct FCBC fcbc;

  list = search_head;
  while (list != NULL)
    {
      if (list->searchpage == searchPage)
        break;
      list = list->next;
    }
  GNUNET_GE_ASSERT (ectx, list != NULL);
  gtk_widget_hide (pauseButton);
  gtk_widget_show (glade_xml_get_widget (list->searchXML,
                                         "searchResumeButton"));
  if (list->fsui_list != NULL)
    {
      fcbc.method = &GNUNET_FSUI_search_pause;
      fcbc.argument = list->fsui_list;
      GNUNET_GTK_run_with_save_calls (&fsui_callback, &fcbc);
    }
}

/**
 * The resume button in a search results tab was clicked.
 */
void
on_searchResumeButton_clicked_fs (GtkWidget * searchPage,
                                  GtkWidget * resumeButton)
{
  SearchList *list;
  struct FCBC fcbc;

  list = search_head;
  while (list != NULL)
    {
      if (list->searchpage == searchPage)
        break;
      list = list->next;
    }
  GNUNET_GE_ASSERT (ectx, list != NULL);
  gtk_widget_hide (resumeButton);
  gtk_widget_show (glade_xml_get_widget (list->searchXML,
                                         "searchPauseButton"));
  if (list->fsui_list != NULL)
    {
      fcbc.method = &GNUNET_FSUI_search_restart;
      fcbc.argument = list->fsui_list;
      GNUNET_GTK_run_with_save_calls (&fsui_callback, &fcbc);
    }
}

/* end of search.c */