aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk.c
blob: a1230c030f53796cfadbf048145fe5d2f6423948 (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
/*
     This file is part of GNUnet.
     Copyright (C) 2010-2013 GNUnet e.V.

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
*/

/**
 * @file src/fs/gnunet-fs-gtk.c
 * @brief Main function of gnunet-fs-gtk
 * @author Christian Grothoff
 */
#include "gnunet-fs-gtk.h"
#include "gnunet-fs-gtk_common.h"
#include "gnunet-fs-gtk_event-handler.h"
#include "gnunet-fs-gtk_open-uri.h"

#if HAVE_LIBUNIQUE
#include <unique/unique.h>
#endif

#include <gnunet/gnunet_arm_service.h>

/**
 * How many block requests can we have outstanding in parallel at a time by
 * default?
 */
#define DEFAULT_MAX_PARALLEL_REQUESTS 100000

/**
 * How many downloads can we have outstanding in parallel at a time by default?
 */
#define DEFAULT_MAX_PARALLEL_DOWNLOADS 128


/**
 * The GNUNET_GTK_file_sharing_downloads_tree_store with the
 * information about active (or completed) downloads.
 */
GtkTreeStore *downloads_treestore;

/**
 * Handle to our main loop.
 */
static struct GNUNET_GTK_MainLoop *ml;

/**
 * Handle for file-sharing operations.
 */
static struct GNUNET_FS_Handle *fs;

/**
 * Handle for ARM monitoring.
 */
static struct GNUNET_ARM_MonitorHandle *armon;

/**
 * Handle for ARM controlling.
 */
static struct GNUNET_ARM_Handle *arm;

/**
 * Context for main window.
 */
static struct GNUNET_GTK_MainWindowContext main_context;


#if HAVE_LIBUNIQUE
static UniqueApp *unique_app;
#endif


struct GNUNET_GTK_MainWindowContext *
GNUNET_FS_GTK_get_main_context ()
{
  return &main_context;
}


/**
 * Return handle for file-sharing operations.
 *
 * @return NULL on error
 */
struct GNUNET_FS_Handle *
GNUNET_FS_GTK_get_fs_handle ()
{
  return fs;
}


/**
 * Remember FS handle if we don't have one yet.
 *
 * @param fsh file sharing handle to use
 */
void
GNUNET_FS_GTK_set_fs_handle (struct GNUNET_FS_Handle *fsh)
{
  if (NULL == fs)
    fs = fsh;
}


/**
 * Get our configuration.
 *
 * @return configuration handle
 */
const struct GNUNET_CONFIGURATION_Handle *
GNUNET_FS_GTK_get_configuration ()
{
  return GNUNET_GTK_main_loop_get_configuration (ml);
}


/**
 * Get an object from the main window.
 *
 * @param name name of the object
 * @return NULL on error
 */
GObject *
GNUNET_FS_GTK_get_main_window_object (const char *name)
{
  return GNUNET_GTK_main_loop_get_object (ml, name);
}


/**
 * Return the list store with anonymity levels.
 *
 * @return the list store
 */
GtkTreeModel *
GNUNET_FS_GTK_get_anonymity_level_list_store ()
{
  return GTK_TREE_MODEL (
    GNUNET_FS_GTK_get_main_window_object ("anonymity_level_liststore"));
}


/**
 * Obtains main window position and size before it's destroyed
 * and saves these into user's config file.
 *
 * @param main_window main window widget
 */
static void
main_window_save_position (GtkWidget *main_window)
{
  GdkWindow *main_window_gdk;
  gint window_x;
  gint window_y;
  gint window_width;
  gint window_height;
  int maximized;
  GdkWindowState window_state;
  struct GNUNET_CONFIGURATION_Handle *cfg;
  struct GNUNET_CONFIGURATION_Handle *cfgDefault;

  cfg = (struct GNUNET_CONFIGURATION_Handle *)
        GNUNET_GTK_main_loop_get_configuration (ml);
  main_window_gdk = gtk_widget_get_window (main_window);
  maximized = GNUNET_YES;
  if (NULL != main_window_gdk)
  {
    window_state = gdk_window_get_state (main_window_gdk);
    if (! (window_state & GDK_WINDOW_STATE_MAXIMIZED))
      maximized = GNUNET_NO;
  }

  gtk_window_get_position (GTK_WINDOW (main_window), &window_x, &window_y);
  gtk_window_get_size (GTK_WINDOW (main_window), &window_width, &window_height);

  GNUNET_CONFIGURATION_set_value_number (cfg,
                                         "gnunet-fs-gtk",
                                         "MAIN_WINDOW_X",
                                         window_x);
  GNUNET_CONFIGURATION_set_value_number (cfg,
                                         "gnunet-fs-gtk",
                                         "MAIN_WINDOW_Y",
                                         window_y);
  GNUNET_CONFIGURATION_set_value_number (cfg,
                                         "gnunet-fs-gtk",
                                         "MAIN_WINDOW_WIDTH",
                                         window_width);
  GNUNET_CONFIGURATION_set_value_number (cfg,
                                         "gnunet-fs-gtk",
                                         "MAIN_WINDOW_HEIGHT",
                                         window_height);
  GNUNET_CONFIGURATION_set_value_string (cfg,
                                         "gnunet-fs-gtk",
                                         "MAIN_WINDOW_MAXIMIZED",
                                         (maximized == GNUNET_YES) ? "YES"
                                                                   : "NO");

  cfgDefault = GNUNET_CONFIGURATION_create ();
  (void) GNUNET_CONFIGURATION_load (cfgDefault, NULL); /* load defaults only */
  GNUNET_CONFIGURATION_write_diffs (cfgDefault,
                                    cfg,
                                    GNUNET_GTK_main_loop_get_configuration_file (
                                      ml));
  GNUNET_CONFIGURATION_destroy (cfgDefault);
}


/**
 * Obtains main window position and size before it's destroyed
 * and saves these into user's config file.
 *
 * @param main_window main window widget
 * @param event the event that we are handling
 * @param user_data main window context
 */
gboolean
GNUNET_GTK_main_window_configure_event_cb (GtkWidget *main_window,
                                           GdkEventConfigure *event,
                                           gpointer user_data)
{
  struct GNUNET_GTK_MainWindowContext *main_context = user_data;

  main_window_save_position (main_context->main_window);
  return FALSE;
}


/**
 * Task run on shutdown.
 *
 * @param cls NULL
 */
static void
shutdown_task (void *cls)
{
  struct SearchLookup *sl;
  struct PseuLookupContext *lctx;
  struct SearchResult *sr;

  while (NULL != (sr = pl_head))
  {
    GNUNET_FS_probe_stop (sr->probe);
    sr->probe = NULL;
    GNUNET_CONTAINER_DLL_remove (pl_head, pl_tail, sr);
  }
  if (NULL != fs)
  {
    GNUNET_FS_stop (fs);
    fs = NULL;
  }
  if (NULL != armon)
  {
    GNUNET_ARM_monitor_stop (armon);
    armon = NULL;
  }
  if (NULL != arm)
  {
    GNUNET_ARM_disconnect (arm);
    arm = NULL;
  }
  GNUNET_FS_GTK_close_uri_tab_ ();
  if (NULL != ml)
  {
    GNUNET_GTK_main_loop_quit (ml);
    ml = NULL;
  }
  if (NULL != main_context.id_op)
  {
    GNUNET_IDENTITY_ego_lookup_cancel (main_context.id_op);
    main_context.id_op = NULL;
  }
  if (NULL != main_context.identity)
  {
    GNUNET_IDENTITY_disconnect (main_context.identity);
    main_context.identity = NULL;
  }
  while (NULL != (sl = main_context.sl_head))
    abort_search_lookup (sl);
  while (NULL != (lctx = main_context.lctx_head))
    abort_pseu_lookup (lctx);
  if (NULL != main_context.zm)
  {
    GNUNET_NAMESTORE_zone_monitor_stop (main_context.zm);
    main_context.zm = NULL;
  }
  if (NULL != main_context.gns)
  {
    GNUNET_GNS_disconnect (main_context.gns);
    main_context.gns = NULL;
  }
}


/**
 * Callback invoked if the application is supposed to exit.
 *
 * @param object origin of the quit event, unused
 * @param user_data global builder instance, unused
 */
void
GNUNET_FS_GTK_menu_quit_activate_cb (GtkMenuItem *object, gpointer user_data)
{
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Callback invoked if the application is supposed to exit.
 *
 * @param object origin of the quit event, unused
 * @param event the delete event
 * @param user_data global builder instance, unused
 */
void
GNUNET_FS_GTK_delete_event_cb (GtkWidget *object,
                               GdkEvent *event,
                               gpointer user_data)
{
  /* GNUNET_FS_GTK_delete_event_cb will eventually be called if we shut down
   * the scheduler, because shutting it down will make GTK delete the main
   * window. On the other hand, deleting the main window first (clicking on X
   * button) will not trigger scheduler shutdown, unlike
   * GNUNET_FS_GTK_menu_quit_activate_cb(). So we shut it down here again,
   * just to be sure it is dead (if it isn't, application will hang up).
   */
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Text was pasted into the main window.  Check if it is a URL
 * and perform the appropriate action.
 *
 * @param cb source clipboard
 * @param text pasted text
 * @param data NULL
 */
static void
process_paste (GtkClipboard *cb, const gchar *text, gpointer data)
{
  struct GNUNET_FS_Uri *kskuri;
  char *emsg;

  if (strlen (text) == 0)
    return;
  if (GNUNET_OK == GNUNET_FS_GTK_handle_uri_string (text, 1))
    return;
  emsg = NULL;
  kskuri = GNUNET_FS_uri_ksk_create (text, &emsg);
  if (NULL == kskuri)
  {
    GNUNET_free (emsg);
    return;
  }
  GNUNET_FS_GTK_handle_uri (kskuri, 1);
  GNUNET_FS_uri_destroy (kskuri);
}


/**
 * We got an event in the main window.  Check for clipboard action.
 *
 * @param widget the main window
 * @param event the event, we only care about button events
 * @param user_data the 'struct SearchTab' the widget is in
 * @return FALSE if no menu could be popped up,
 *         TRUE if there is now a pop-up menu
 */
gboolean
GNUNET_FS_GTK_main_window_button_press_event (GtkWidget *widget,
                                              GdkEvent *event,
                                              gpointer user_data)
{
  GdkEventButton *event_button = (GdkEventButton *) event;
  GtkClipboard *cb;

  if ((GDK_BUTTON_PRESS != event->type) || (2 != event_button->button))
    return FALSE;
  cb = gtk_clipboard_get (gdk_atom_intern ("PRIMARY", FALSE));
  gtk_clipboard_request_text (cb, &process_paste, NULL);
  return FALSE;
}


#if HAVE_LIBUNIQUE
/**
 * Function called whenever a second gnunet-fs-gtk process is started
 * with additional arguments for us.
 *
 * @param app unique handle
 * @param command command that was given
 * @param message_data command line message data
 * @param time_ timestamp of the event
 * @param user_data our 'main context'
 * @return response code for original process
 */
static UniqueResponse
unique_app_message_cb (UniqueApp *app,
                       gint command,
                       UniqueMessageData *message_data,
                       guint time_,
                       gpointer user_data)
{
  struct GNUNET_GTK_MainWindowContext *main_context = user_data;

  /* raise the window */
  if (GTK_IS_WINDOW (main_context->main_window))
    gtk_window_present_with_time (GTK_WINDOW (main_context->main_window),
                                  time_);

  switch (command)
  {
  case UNIQUE_NEW:
    /* this is unexpected... */
    GNUNET_break (0);
    break;
  case UNIQUE_OPEN: {
      gchar **uris;
      gint n_uris;
      gint i;

      uris = unique_message_data_get_uris (message_data);
      n_uris = g_strv_length (uris);
      for (i = 0; i < n_uris; i++)
      {
        if (GNUNET_OK !=
            GNUNET_FS_GTK_handle_uri_string (uris[i], 1 /* anonymity level */))
          return UNIQUE_RESPONSE_PASSTHROUGH;
      }
      g_strfreev (uris);
    }
    break;
  case UNIQUE_ACTIVATE:
    break;
  default:
    break;
  }
  return UNIQUE_RESPONSE_OK;
}


#endif


static char *
format_service_list (unsigned int count,
                     const struct GNUNET_ARM_ServiceInfo *list)
{
  size_t len = 0;
  int i;
  const char *header = _ ("GNUnet node appears to be on.");
  const char *listheader = _ ("Currently running services:\n");
  char *result;
  char *p;
  int r;

  len = strlen (header) + 1;
  if (list)
  {
    len += strlen (listheader);
    for (i = 0; i < count; i++)
      len += strlen (list[i].name) + 1;
  }
  len += 1;
  result = p = GNUNET_malloc (len * sizeof (char));
  r = GNUNET_snprintf (p, (size_t) (result + len - p), "%s\n", header);
  if (r < 0)
  {
    GNUNET_free (result);
    return NULL;
  }
  p += r;
  if (list)
  {
    r = GNUNET_snprintf (p, (size_t) (result + len - p), "%s", listheader);
    if (r < 0)
    {
      GNUNET_free (result);
      return NULL;
    }
    p += r;
    for (i = 0; i < count; i++)
    {
      size_t l = strlen (list[i].name);
      memcpy (p, list[i].name, l);
      p += l;
      if (i + 1 < count)
      {
        p[0] = '\n';
        p += 1;
      }
    }
  }
  p[0] = '\0';
  return result;
}


static void
service_list_callback (void *cls,
                       enum GNUNET_ARM_RequestStatus rs,
                       unsigned int count,
                       const struct GNUNET_ARM_ServiceInfo *list)
{
  char *service_list;

  if ((GNUNET_ARM_REQUEST_SENT_OK != rs) || (NULL == list))
    return;
  service_list = format_service_list (count, list);
  GNUNET_FS_GTK_update_connection_indicator (cls, TRUE, service_list);
  GNUNET_free (service_list);
}


static void
arm_connection_state_change (void *cls, int connected)
{
  char *service_list;

  if (connected)
  {
    service_list = format_service_list (0, NULL);
    GNUNET_FS_GTK_update_connection_indicator (cls, TRUE, service_list);
    GNUNET_free (service_list);
    GNUNET_ARM_request_service_list (arm, &service_list_callback, cls);
  }
  else
    GNUNET_FS_GTK_update_connection_indicator (
      cls,
      FALSE,
      _ ("Can't connect to the Automatic Restart Manager service."));
}


static void
service_status_change (void *cls,
                       const char *service,
                       enum GNUNET_ARM_ServiceMonitorStatus status)
{
  /* Very crude, we can probably do better.
   * Maybe keep a list of running services, and modify it in response
   * to service status changes, then update the indicator,
   * without requesting a list from ARM every goddamned time?
   */
  GNUNET_ARM_request_service_list (arm, &service_list_callback, cls);
}


static void
monitor_zone_error (void *cls)
{
  GtkListStore *ls;

  ls = GTK_LIST_STORE (
    GNUNET_FS_GTK_get_main_window_object ("namespace_label_liststore"));
  gtk_list_store_clear (ls);
  gtk_widget_hide (GTK_WIDGET (GNUNET_FS_GTK_get_main_window_object (
                                 "main_window_search_namespace_combobox")));
}


static void
monitor_zone_sync (void *cls)
{
  gtk_widget_show (GTK_WIDGET (GNUNET_FS_GTK_get_main_window_object (
                                 "main_window_search_namespace_combobox")));
}


/**
 * Process a record that was stored in the namestore in the
 * "sks_zone".  Adds (or removes) the respective label to the
 * drop-down menu for the SKS search by manipulating the
 * "namespace_label_liststore".
 *
 * @param cls closure
 * @param zone private key of the zone; NULL on disconnect
 * @param label label of the records; NULL on disconnect
 * @param rd_count number of entries in @a rd array; 0 on removal
 * @param rd array of records for the label
 */
static void
monitor_zone_records (void *cls,
                      const struct GNUNET_IDENTITY_PrivateKey *zone,
                      const char *label,
                      unsigned int rd_count,
                      const struct GNUNET_GNSRECORD_Data *rd)
{
  GtkListStore *ls;
  GtkTreeModel *tm;
  GtkTreeIter iter;
  gchar *id;
  gchar *label_gnu;

  GNUNET_NAMESTORE_zone_monitor_next (main_context.zm, 1);
  ls = GTK_LIST_STORE (
    GNUNET_FS_GTK_get_main_window_object ("namespace_label_liststore"));
  label_gnu = g_strdup_printf ("%s.%s", label, "gnu");
  if (0 == rd_count)
  {
    tm = GTK_TREE_MODEL (ls);
    if (gtk_tree_model_get_iter_first (tm, &iter))
      do
      {
        gtk_tree_model_get (tm, &iter, 0, &id, -1);
        if (0 == strcmp (id, label_gnu))
        {
          GNUNET_assert (gtk_list_store_remove (ls, &iter));
          g_free (id);
          g_free (label_gnu);
          return;
        }
        g_free (id);
      } while (gtk_tree_model_iter_next (tm, &iter));
    GNUNET_break (0);
    return;
  }
  gtk_list_store_insert_with_values (ls, &iter, -1, 0, label_gnu, -1);
  g_free (label_gnu);
}


/**
 * The identity service has given us the ego we should use for resolving
 * namepace names.
 *
 * @param cls closure
 * @param ego ego handle, NULL for none
 * @param ctx context for application to store data for this ego
 *                 (during the lifetime of this process, initially NULL)
 * @param name name assigned by the user for this ego,
 *                   NULL if the user just deleted the ego and it
 *                   must thus no longer be used
 */
static void
handle_sks_zone_identity (void *cls,
                          struct GNUNET_IDENTITY_Ego *ego)
{
  main_context.id_op = NULL;

  if (NULL == ego)
  {
    GNUNET_log (
      GNUNET_ERROR_TYPE_WARNING,
      _ (
        "No default ego specified for `fs-sks` service, will not enable namespace search.\n"));
    return;
  }
  main_context.sks_zone = ego;
  gtk_widget_show (GTK_WIDGET (GNUNET_FS_GTK_get_main_window_object (
                                 "main_window_search_namespace_label")));
  gtk_widget_show (GTK_WIDGET (GNUNET_FS_GTK_get_main_window_object (
                                 "main_window_search_namespace_combobox")));
  main_context.zm =
    GNUNET_NAMESTORE_zone_monitor_start (main_context.cfg,
                                         GNUNET_IDENTITY_ego_get_private_key (
                                           main_context.sks_zone),
                                         GNUNET_YES,
                                         &monitor_zone_error,
                                         NULL,
                                         &monitor_zone_records,
                                         NULL,
                                         &monitor_zone_sync,
                                         NULL);
}


/**
 * We must pass a non-NULL callback to the identity service,
 * but we don't actually care about the information here
 * (we will use GNUNET_IDENTITY_get() if and when we do care).
 *
 * @param cls closure
 * @param ego ego handle
 * @param ctx context for application to store data for this ego
 *                 (during the lifetime of this process, initially NULL)
 * @param name name assigned by the user for this ego,
 *                   NULL if the user just deleted the ego and it
 *                   must thus no longer be used
 */
static void
non_null_cb (void *cls,
             struct GNUNET_IDENTITY_Ego *ego,
             void **ctx,
             const char *name)
{
  (void) cls;
  (void) ego;
  (void) ctx;
  (void) name;
}


/**
 * Actual main function run right after GNUnet's scheduler
 * is initialized.  Initializes up GTK and Glade.
 *
 * @param cls handle to the main loop (`struct GNUNET_GTK_MainLoop`)
 */
static void
run (void *cls)
{
  unsigned long long dl_parallel;
  unsigned long long req_parallel;
  unsigned long long window_x;
  unsigned long long window_y;
  unsigned long long window_width;
  unsigned long long window_height;
  int maximized;
  char *default_ego_name;

  ml = cls;
  /* setup main context */
  if (GNUNET_OK != GNUNET_GTK_main_loop_build_window (ml, &main_context))
    return;
  main_context.builder = GNUNET_GTK_main_loop_get_builder (ml);
  main_context.cfg = GNUNET_GTK_main_loop_get_configuration (ml);
  main_context.search_ns_treestore =
    GTK_TREE_STORE (GNUNET_FS_GTK_get_main_window_object (
                      "main_window_search_namespace_treestore"));
  main_context.main_window = GTK_WIDGET (
    GNUNET_FS_GTK_get_main_window_object ("GNUNET_GTK_main_window"));
  main_context.ns_selector_treeview = GTK_TREE_VIEW (
    GNUNET_FS_GTK_get_main_window_object ("namespace_selector_treeview"));
  main_context.ns_selector_window = GTK_WIDGET (
    GNUNET_FS_GTK_get_main_window_object ("namespace_selector_window"));
  main_context.ns_dropdown_button =
    GTK_TOGGLE_BUTTON (GNUNET_FS_GTK_get_main_window_object (
                         "main_window_search_namespace_dropdown_button"));
  main_context.search_ns_label =
    GTK_LABEL (GNUNET_FS_GTK_get_main_window_object (
                 "main_window_search_selected_namespace_label"));

  main_context.search_entry = GTK_ENTRY (
    GNUNET_FS_GTK_get_main_window_object ("main_window_search_entry"));

  downloads_treestore = GTK_TREE_STORE (GNUNET_FS_GTK_get_main_window_object (
                                          "GNUNET_GTK_file_sharing_downloads_tree_store"));
  main_context.anonymity_combo =
    GTK_COMBO_BOX (GNUNET_FS_GTK_get_main_window_object (
                     "main_window_search_anonymity_combobox"));
  main_context.anonymity_level_liststore = GTK_LIST_STORE (
    GNUNET_FS_GTK_get_main_window_object ("anonymity_level_liststore"));

  main_context.preview_image = GTK_IMAGE (GNUNET_FS_GTK_get_main_window_object (
                                            "GNUNET_GTK_main_window_preview_image"));
  main_context.md_liststore = GTK_LIST_STORE (
    GNUNET_FS_GTK_get_main_window_object ("GNUNET_GTK_meta_data_list_store"));
  main_context.md_treeview =
    GTK_TREE_VIEW (GNUNET_FS_GTK_get_main_window_object (
                     "GNUNET_GTK_main_window_metadata_treeview"));
  main_context.ns_discovery_handle = NULL;

  main_context.download_location_chooser =
    GTK_FILE_CHOOSER (GNUNET_FS_GTK_get_main_window_object (
                        "GNUNET_GTK_search_frame_download_location_chooser"));
  main_context.download_name_entry =
    GTK_ENTRY (GNUNET_FS_GTK_get_main_window_object (
                 "GNUNET_GTK_search_frame_download_filename_entry"));
  main_context.download_anonymity_combo =
    GTK_COMBO_BOX (GNUNET_FS_GTK_get_main_window_object (
                     "main_window_download_anonymity_combobox"));
  main_context.download_recursive_checkbutton =
    GTK_CHECK_BUTTON (GNUNET_FS_GTK_get_main_window_object (
                        "GNUNET_GTK_search_frame_download_recursive_checkbox"));
  main_context.download_download_button =
    GTK_BUTTON (GNUNET_FS_GTK_get_main_window_object (
                  "GNUNET_GTK_search_frame_download_download_button"));
  main_context.download_panel = GTK_BOX (GNUNET_FS_GTK_get_main_window_object (
                                           "GNUNET_GTK_search_frame_download_vbox"));

  main_context.notebook = GTK_NOTEBOOK (
    GNUNET_FS_GTK_get_main_window_object ("GNUNET_GTK_main_window_notebook"));

  main_context.connection_indicator =
    GTK_IMAGE (GNUNET_FS_GTK_get_main_window_object (
                 "GNUNET_FS_GTK_main_window_connection_indicator"));

  GNUNET_GTK_set_icon_search_path ();
  GNUNET_GTK_setup_nls ();

  /* Make sure button class is realized */
  g_type_class_unref (g_type_class_ref (GTK_TYPE_BUTTON));
  /* GNUnet main window assumes that images on buttons are visible,
   * override the theme's gtkrc setting
   */
  g_object_set (gtk_settings_get_default (), "gtk-button-images", TRUE, NULL);

  /* setup main window */
  maximized = GNUNET_CONFIGURATION_get_value_yesno (main_context.cfg,
                                                    "gnunet-fs-gtk",
                                                    "MAIN_WINDOW_MAXIMIZED");
  if (GNUNET_SYSERR == maximized)
    maximized = GNUNET_YES;
  if ((GNUNET_NO == maximized) &&
      (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (main_context.cfg,
                                                           "gnunet-fs-gtk",
                                                           "MAIN_WINDOW_X",
                                                           &window_x)) &&
      (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (main_context.cfg,
                                                           "gnunet-fs-gtk",
                                                           "MAIN_WINDOW_Y",
                                                           &window_y)) &&
      (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (main_context.cfg,
                                                           "gnunet-fs-gtk",
                                                           "MAIN_WINDOW_WIDTH",
                                                           &window_width)) &&
      (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (main_context.cfg,
                                                           "gnunet-fs-gtk",
                                                           "MAIN_WINDOW_HEIGHT",
                                                           &window_height)))
  {
    gtk_window_move (GTK_WINDOW (main_context.main_window), window_x, window_y);
    gtk_window_resize (GTK_WINDOW (main_context.main_window),
                       window_width,
                       window_height);
  }
  else
  {
    /* If anything is wrong - play safe and show it maximized */
    gtk_window_maximize (GTK_WINDOW (main_context.main_window));
  }

  /* Allow multiple selection in metadata view; */
  /* FIXME-GTK3: this can be done within (modern versions of) glade
     (However, this needs GTK >= 3.2, we currently target 3.0) */
  gtk_tree_selection_set_mode (gtk_tree_view_get_selection (
                                 main_context.md_treeview),
                               GTK_SELECTION_MULTIPLE);

  /* FIXME: should these '1's be here? Maybe better to put them into
   * default config files?
   */
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_number (main_context.cfg,
                                             "gnunet-fs-gtk",
                                             "MAX_PARALLEL_DOWNLOADS",
                                             &dl_parallel))
    dl_parallel = DEFAULT_MAX_PARALLEL_DOWNLOADS;
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_number (main_context.cfg,
                                             "gnunet-fs-gtk",
                                             "MAX_PARALLEL_REQUESTS",
                                             &req_parallel))
    req_parallel = DEFAULT_MAX_PARALLEL_REQUESTS;
  if (GNUNET_OK !=
      GNUNET_CONFIGURATION_get_value_string (main_context.cfg,
                                             "fs-sks",
                                             "DEFAULT_IDENTITY",
                                             &default_ego_name))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Please set DEFAULT_IDENTITY under [fs-sks]\n");
    GNUNET_GTK_main_loop_quit (cls);
    return;
  }
  /* initialize file-sharing */
  fs = GNUNET_FS_start (main_context.cfg,
                        "gnunet-fs-gtk",
                        &GNUNET_GTK_fs_event_handler,
                        NULL,
                        GNUNET_FS_FLAGS_PERSISTENCE | GNUNET_FS_FLAGS_DO_PROBES,
                        GNUNET_FS_OPTIONS_DOWNLOAD_PARALLELISM,
                        (unsigned int) dl_parallel,
                        GNUNET_FS_OPTIONS_REQUEST_PARALLELISM,
                        (unsigned int) req_parallel,
                        GNUNET_FS_OPTIONS_END);
  if (NULL == fs)
  {
    GNUNET_GTK_main_loop_quit (cls);
    return;
  }
  arm = GNUNET_ARM_connect (main_context.cfg,
                            &arm_connection_state_change,
                            &main_context);
  armon = GNUNET_ARM_monitor_start (main_context.cfg,
                                    &service_status_change,
                                    &main_context);
  main_context.gns = GNUNET_GNS_connect (main_context.cfg);
  main_context.identity =
    GNUNET_IDENTITY_connect (main_context.cfg, &non_null_cb, NULL);
  main_context.id_op = GNUNET_IDENTITY_ego_lookup (main_context.cfg,
                                            default_ego_name,
                                            &handle_sks_zone_identity,
                                            NULL);
  GNUNET_free (default_ego_name);
#if HAVE_LIBUNIQUE
  unique_app_watch_window (unique_app, GTK_WINDOW (main_context.main_window));
  g_signal_connect (unique_app,
                    "message-received",
                    G_CALLBACK (unique_app_message_cb),
                    &main_context);
#endif
  /* make GUI visible */
  gtk_widget_show (main_context.main_window);
  gtk_window_present (GTK_WINDOW (main_context.main_window));

  {
    char *const *argv;
    int argc;
    int i;

    GNUNET_GTK_main_loop_get_args (ml, &argc, &argv);

    for (i = 0; i < argc; i++)
    {
      if (GNUNET_OK !=
          GNUNET_FS_GTK_handle_uri_string (argv[i], 1 /* anonymity level */))
        fprintf (stderr, "Invalid URI `%s'\n", argv[i]);
    }
  }
  GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
}


int
main (int argc, char **argv)
{
  static struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_OPTION_END
  };
#if HAVE_LIBUNIQUE
  int arge;

  gtk_init (&argc, &argv);
  unique_app = unique_app_new ("org.gnunet.gnunet-fs-gtk", NULL);
  if (unique_app_is_running (unique_app))
  {
    UniqueResponse response;

    arge = GNUNET_GETOPT_run ("gnunet-fs-gtk", options, argc, argv);
    response = unique_app_send_message (unique_app, UNIQUE_ACTIVATE, NULL);
    while (arge < argc)
    {
      UniqueMessageData *msg;

      msg = unique_message_data_new ();
      unique_message_data_set_text (msg, argv[arge], strlen (argv[arge]) + 1);
      if (UNIQUE_RESPONSE_OK == response)
        response = unique_app_send_message (unique_app, UNIQUE_OPEN, msg);
      unique_message_data_free (msg);
      arge++;
    }
    g_object_unref (unique_app);

    return (UNIQUE_RESPONSE_OK == response) ? 0 : 1;
  }

#endif
  if (GNUNET_OK !=
      GNUNET_GTK_main_loop_start ("gnunet-fs-gtk",
                                  "GTK GUI for GNUnet",
                                  argc,
                                  argv,
                                  options,
                                  "gnunet_fs_gtk_main_window.glade",
                                  &run))
  {
#if HAVE_LIBUNIQUE
    g_object_unref (unique_app);
#endif
    return 1;
  }
#if HAVE_LIBUNIQUE
  g_object_unref (unique_app);
#endif
  return 0;
}


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