aboutsummaryrefslogtreecommitdiff
path: root/src/identity/gnunet-identity-gtk_advertise.c
blob: 9df469297d06ca1d4b5672f2a5d6ce75dbabc098 (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
/*
     This file is part of GNUnet
     (C) 2005-2013 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 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., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file src/identity/gnunet-identity-gtk_advertise.c
 * @author LRN
 * @author Christian Grothoff
 */
#include "gnunet-identity-gtk_advertise.h"
#include "gnunet-identity-gtk.h"
#include <gnunet/gnunet_fs_service.h>


#define NEW_KEYWORD_TEXT "<add>"


/**
 * Types of metadata we offer for namespaces.
 */
static gint types[] =
{
  EXTRACTOR_METATYPE_TITLE,
  EXTRACTOR_METATYPE_KEYWORDS,
  EXTRACTOR_METATYPE_SUBJECT,
  EXTRACTOR_METATYPE_DESCRIPTION,
  EXTRACTOR_METATYPE_COMMENT,
  EXTRACTOR_METATYPE_COPYRIGHT,
  EXTRACTOR_METATYPE_URI,
  EXTRACTOR_METATYPE_CREATOR,
  EXTRACTOR_METATYPE_CREATION_DATE,
  EXTRACTOR_METATYPE_RESERVED
};


/**
 * Columns in the metadata list store.
 */
enum METADATA_ModelColumns
{

  /**
   * A guint
   */
  METADATA_MC_TYPE_AS_ENUM = 0,

  /**
   * A gchararray
   */
  METADATA_MC_TYPE_AS_STRING = 1,

  /**
   * A gchararray
   */
  METADATA_MC_VALUE = 2

};


/**
 * Columns in the keyword list tree store.
 */
enum KEYWORDS_ModelColumns
{

  /**
   * A gchararray
   */
  KEYWORDS_MC_KEYWORD = 0

};


/**
 * Columns in the meta types list tree store.
 */
enum METATYPES_ModelColumns
{

  /**
   * A gchararray
   */
  METATYPES_MC_TYPE_AS_STRING = 0,

  /**
   * A guint
   */
  METATYPES_MC_TYPE_AS_ENUM = 1

};


/**
 * Context for advertisement operations.
 */
struct AdvertiseContext
{
  /**
   * Builder for accessing objects in the dialog.
   */
  GtkBuilder *builder;

  /**
   * Private key of the namespace we will be advertising.
   */
  struct GNUNET_CRYPTO_EcdsaPrivateKey priv;

  /**
   * Main dialog object.
   */
  GtkWidget *dialog;

  /**
   * List of keywords to advertise under.
   */
  GtkListStore *keywords;

  /**
   * Metadata to include in the advertisement.
   */
  GtkListStore *metadata;

  /**
   * Model with the meta types.
   */
  GtkListStore *meta_types;

  /**
   * Number of keywords in the view.
   */
  unsigned int num_keywords;

};


/**
 * Context for the publishing operation.
 */
struct PublishContext
{

  /**
   * Kept in a DLL.
   */
  struct PublishContext *next;

  /**
   * Kept in a DLL.
   */
  struct PublishContext *prev;

  /**
   * Handle to FS subsystem.
   */
  struct GNUNET_FS_Handle *fs;

  /**
   * Handle to the publish operation.
   */
  struct GNUNET_FS_PublishKskContext *pub;

};


/**
 * Kept in a DLL.
 */
static struct PublishContext *pc_head;

/**
 * Kept in a DLL.
 */
static struct PublishContext *pc_tail;


/**
 * Shutdown advertisement subsystem, this process is terminating.
 */
void
GIG_advertise_shutdown_ ()
{
  struct PublishContext *pc;

  while (NULL != (pc = pc_head))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		_("Aborting advertising operation due to shutdown.\n"));
    GNUNET_CONTAINER_DLL_remove (pc_head,
				 pc_tail,
				 pc);
    GNUNET_FS_publish_ksk_cancel (pc->pub);
    GNUNET_FS_stop (pc->fs);
    GNUNET_free (pc);
  }
}


/**
 * Function called once we published the advertisement.
 *
 * @param cls closure with the `struct PublishContext`
 * @param uri URI under which the block is now available, NULL on error
 * @param emsg error message, NULL on success
 */
static void
publish_continuation (void *cls,
		      const struct GNUNET_FS_Uri *uri,
		      const char *emsg)
{
  struct PublishContext *pc = cls;

  pc->pub = NULL;
  if (NULL == uri)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		_("Failed to advertise ego: %s\n"),
		emsg);
    /* FIXME: might want to output to GUI... */
  }
  GNUNET_FS_stop (pc->fs);
  GNUNET_CONTAINER_DLL_remove (pc_head,
			       pc_tail,
			       pc);
  GNUNET_free (pc);
}


/**
 * Notification of FS to a client about the progress of an
 * operation.  Callbacks of this type will be used for uploads,
 * downloads and searches.  Some of the arguments depend a bit
 * in their meaning on the context in which the callback is used.
 *
 * @param cls closure
 * @param info details about the event, specifying the event type
 *        and various bits about the event
 * @return client-context (for the next progress call
 *         for this operation; should be set to NULL for
 *         SUSPEND and STOPPED events).  The value returned
 *         will be passed to future callbacks in the respective
 *         field in the `struct GNUNET_FS_ProgressInfo`.
 */
static void *
progress_cb (void *cls,
	     const struct GNUNET_FS_ProgressInfo *info)
{
  return NULL;
}


/**
 * The user terminated the dialog.  Perform the appropriate action.
 *
 * @param dialog the advertisement dialog
 * @param response_id action selected by the user
 * @param user_data our 'struct AdvertiseContext' (to be cleaned up)
 */
void
GNUNET_GTK_identity_advertise_dialog_response_cb (GtkDialog *dialog,
						  gint response_id,
						  gpointer user_data)
{
  struct AdvertiseContext *ac = user_data;
  struct GNUNET_FS_Uri *ksk_uri;
  struct GNUNET_FS_Uri *uri;
  gchar *keyword;
  struct GNUNET_CONTAINER_MetaData *meta;
  guint ntype;
  gchar *value;
  GtkTreeIter iter;
  struct GNUNET_FS_BlockOptions bo;
  struct GNUNET_CRYPTO_EcdsaPublicKey pk;
  struct PublishContext *pc;
  guint anonymity;
  const char *id;

  if (GTK_RESPONSE_OK != response_id)
    goto cleanup;
  ksk_uri = NULL;
  if (! gtk_tree_model_get_iter_first (GTK_TREE_MODEL (ac->keywords), &iter))
  {
    GNUNET_break (0);
    goto cleanup;
  }
  do
  {
    gtk_tree_model_get (GTK_TREE_MODEL (ac->keywords), &iter,
			KEYWORDS_MC_KEYWORD, &keyword,
			-1);
    if (NULL == ksk_uri)
      ksk_uri = GNUNET_FS_uri_ksk_create_from_args (1, (const char **) &keyword);
    else
      GNUNET_FS_uri_ksk_add_keyword (ksk_uri, keyword, GNUNET_NO);
    g_free (keyword);
  }
  while (gtk_tree_model_iter_next (GTK_TREE_MODEL (ac->keywords), &iter));

  meta = GNUNET_CONTAINER_meta_data_create ();
  if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (ac->metadata),
				     &iter))
  {
    do
    {
      gtk_tree_model_get (GTK_TREE_MODEL (ac->metadata), &iter,
                          METADATA_MC_TYPE_AS_ENUM, &ntype,
                          METADATA_MC_VALUE, &value,
                          -1);
      if (ntype > 0)
      {
        GNUNET_CONTAINER_meta_data_insert (meta, "<user>",
					   ntype,
					   EXTRACTOR_METAFORMAT_UTF8,
                                           "text/plain", value,
                                           strlen (value) + 1);
      }
      g_free (value);
    }
    while (gtk_tree_model_iter_next (GTK_TREE_MODEL (ac->metadata), &iter));
  }

  bo.expiration_time = GNUNET_GTK_get_expiration_time
    (GTK_SPIN_BUTTON
     (gtk_builder_get_object
      (ac->builder,
       "GNUNET_GTK_identity_advertise_expiration_year_spin_button")));
  anonymity = 1;
  GNUNET_break (GNUNET_GTK_get_selected_anonymity_level (ac->builder,
							 "GNUNET_GTK_identity_advertise_anonymity_combobox",
							 &anonymity));
  bo.anonymity_level = anonymity;
  bo.content_priority
    = (unsigned int) gtk_spin_button_get_value
    (GTK_SPIN_BUTTON
     (gtk_builder_get_object
      (ac->builder,
       "GNUNET_GTK_identity_advertise_priority_spin_button")));
  bo.replication_level
    = (unsigned int) gtk_spin_button_get_value
    (GTK_SPIN_BUTTON
     (gtk_builder_get_object
      (ac->builder,
       "GNUNET_GTK_identity_advertise_replication_spin_button")));
  GNUNET_CRYPTO_ecdsa_key_get_public (&ac->priv,
				    &pk);
  id = gtk_entry_get_text
    (GTK_ENTRY
     (gtk_builder_get_object
      (ac->builder,
       "GNUNET_GTK_identity_advertise_root_entry")));
  uri = GNUNET_FS_uri_sks_create (&pk, id);

  pc = GNUNET_new (struct PublishContext);
  pc->fs = GNUNET_FS_start (GIG_get_configuration (),
			    "gnunet-identity-gtk",
			    &progress_cb, pc,
			    GNUNET_FS_FLAGS_NONE,
			    GNUNET_FS_OPTIONS_END);
  pc->pub = GNUNET_FS_publish_ksk (pc->fs,
				   ksk_uri,
				   meta,
				   uri,
				   &bo,
				   GNUNET_FS_PUBLISH_OPTION_NONE,
				   &publish_continuation, pc);
  GNUNET_CONTAINER_DLL_insert (pc_head,
			       pc_tail,
			       pc);
  GNUNET_FS_uri_destroy (uri);
  GNUNET_CONTAINER_meta_data_destroy (meta);
 cleanup:
  gtk_widget_destroy (ac->dialog);
  g_object_unref (G_OBJECT (ac->builder));
  GNUNET_free (ac);
}


/**
 * User edited keywords in the keyword tree view.  Update
 * the model accordingly.
 *
 * @param renderer the object that created the signal
 * @param path the path identifying the edited cell
 * @param new_text the new text
 * @param user_data the 'struct AdvertiseContext'
 */
void
GNUNET_GTK_identity_advertise_keywords_text_edited_cb (GtkCellRendererText *renderer,
						       gchar               *path,
						       gchar               *new_text,
						       gpointer             user_data)
{
  struct AdvertiseContext *ac = user_data;
  GtkTreePath *tree_path;
  GtkTreeIter iter;
  char *old_text;

  tree_path = gtk_tree_path_new_from_string (path);
  if (NULL == tree_path)
  {
    GNUNET_break (0);
    return;
  }
  if (! gtk_tree_model_get_iter (GTK_TREE_MODEL (ac->keywords),
				 &iter, tree_path))
  {
    GNUNET_break (0);
    gtk_tree_path_free (tree_path);
    return;
  }
  gtk_tree_path_free (tree_path);
  gtk_tree_model_get (GTK_TREE_MODEL (ac->keywords), &iter,
		      KEYWORDS_MC_KEYWORD, &old_text,
		      -1);
  if (0 == strcmp (NEW_KEYWORD_TEXT, old_text))
  {
    if ( (NULL != new_text) &&
	 (0 != strlen (new_text)) )
    {
      gtk_list_store_insert_with_values (ac->keywords, &iter, -1,
					 KEYWORDS_MC_KEYWORD, new_text,
					 -1);
      ac->num_keywords++;
    }
  }
  else
  {
    if ( (NULL != new_text) &&
	 (0 != strlen (new_text)) )
    {
      gtk_list_store_set (ac->keywords, &iter,
			  KEYWORDS_MC_KEYWORD, new_text,
			  -1);
    }
    else
    {
      gtk_list_store_remove (ac->keywords, &iter);
      ac->num_keywords--;
    }
  }
  g_free (old_text);
  gtk_widget_set_sensitive (GTK_WIDGET (gtk_builder_get_object
					(ac->builder,
					 "GNUNET_GTK_identity_advertise_ok_button")),
			    0 != ac->num_keywords);
}


/**
 * User pushed a key in the metadata tree view.  Check if it was
 * "DEL" and if so, remove the selected values.
 *
 * @param widget widget creating the signal
 * @param event the event to process
 * @param user_data the 'struct AdvertiseContext'
 * @return TRUE if we handled the event, FALSE if not
 */
gboolean
GNUNET_GTK_identity_advertise_metadata_treeview_key_press_event_cb (GtkWidget *widget,
								    GdkEventKey *event,
								    gpointer user_data)
{
  struct AdvertiseContext *ac = user_data;
  GtkTreeSelection *sel;
  GtkTreeModel *model;
  GtkTreeIter iter;

  if (event->keyval != GDK_KEY_Delete)
    return FALSE;
  sel = gtk_tree_view_get_selection (GTK_TREE_VIEW
				     (gtk_builder_get_object
				      (ac->builder,
				       "GNUNET_GTK_identity_advertise_metadata_treeview")));
  if (! gtk_tree_selection_get_selected (sel,
					 &model,
					 &iter))
    {
      gdk_beep ();
      return TRUE;
    }
  gtk_list_store_remove (ac->metadata,
			 &iter);
  return TRUE;
}


/**
 * User edited metadata value in the tree view. Update the model.
 *
 * @param renderer the object that created the signal
 * @param path the path identifying the edited cell
 * @param new_text the new text, if empty, remove the line
 * @param user_data the 'struct AdvertiseContext'
 */
void
GNUNET_GTK_identity_advertise_metadata_value_text_edited_cb (GtkCellRendererText *renderer,
							     gchar *path,
							     gchar *new_text,
							     gpointer user_data)
{
  struct AdvertiseContext *ac = user_data;
  GtkTreePath *tree_path;
  GtkTreeIter iter;

  tree_path = gtk_tree_path_new_from_string (path);
  if (NULL == tree_path)
  {
    GNUNET_break (0);
    return;
  }
  if (! gtk_tree_model_get_iter (GTK_TREE_MODEL (ac->metadata),
				 &iter, tree_path))
  {
    GNUNET_break (0);
    gtk_tree_path_free (tree_path);
    return;
  }
  gtk_tree_path_free (tree_path);
  if ( (NULL != new_text) &&
       (0 != strlen (new_text)) )
    gtk_list_store_set (ac->metadata, &iter,
			METADATA_MC_VALUE, new_text,
			-1);
  else
    gtk_list_store_remove (ac->metadata, &iter);
}


/**
 * User edited the 'value' field for inserting meta data.   Update
 * sensitivity of the 'add' button accordingly.
 *
 * @param editable the widget that was edited
 * @param user_data the 'struct AdvertiseContext'
 */
void
GNUNET_GTK_identity_advertise_metadata_value_entry_changed_cb (GtkEditable *editable,
							       gpointer user_data)
{
  struct AdvertiseContext *ac = user_data;
  const char *value;
  GtkEntry *value_entry;
  GtkWidget * add_button;

  value_entry = GTK_ENTRY (gtk_builder_get_object (ac->builder,
						   "GNUNET_GTK_identity_advertise_metadata_value_entry"));
  value = gtk_entry_get_text (value_entry);
  add_button = GTK_WIDGET (gtk_builder_get_object (ac->builder,
						   "GNUNET_GTK_identity_advertise_metadata_add_button"));
  if ( (NULL == value) ||
       (0 == strlen (value)) )
  {
    gtk_widget_set_sensitive (add_button,
			      FALSE);
    return;
  }
  gtk_widget_set_sensitive (add_button,
			    TRUE);
}


/**
 * User clicked the 'add' button, get the type and value and update
 * the metadata model.
 *
 * @param button the 'add' button
 * @param user_data the 'struct AdvertiseContext'
 */
void
GNUNET_GTK_identity_advertise_metadata_add_button_clicked_cb (GtkButton * button,
							      gpointer user_data)
{
  struct AdvertiseContext *ac = user_data;
  GtkTreeIter iter;
  guint type;
  const char *type_as_string;
  const char *value;
  GtkEntry *value_entry;
  GtkComboBox *type_box;

  type_box = GTK_COMBO_BOX (gtk_builder_get_object (ac->builder,
						    "GNUNET_GTK_identity_advertise_metadata_type_combobox"));
  if (! gtk_combo_box_get_active_iter (type_box,
				       &iter))
  {
    GNUNET_break (0);
    return;
  }
  gtk_tree_model_get (GTK_TREE_MODEL (ac->meta_types),
		      &iter,
		      METATYPES_MC_TYPE_AS_ENUM, &type,
		      -1);
  type_as_string = EXTRACTOR_metatype_to_string (type);
  value_entry = GTK_ENTRY (gtk_builder_get_object (ac->builder,
						   "GNUNET_GTK_identity_advertise_metadata_value_entry"));
  value = gtk_entry_get_text (value_entry);
  if ( (NULL == value) ||
       (0 == strlen (value)) )
  {
    GNUNET_break (0);
    return;
  }
  gtk_list_store_insert_with_values (ac->metadata,
                                     &iter, 0,
                                     METADATA_MC_TYPE_AS_ENUM, type,
                                     METADATA_MC_TYPE_AS_STRING, type_as_string,
                                     METADATA_MC_VALUE, value,
                                     -1);
  gtk_entry_set_text (value_entry, "");
}


/**
 * Run the dialog for advertising a namespace.
 *
 * @param priv private key of the namespace to advertise
 */
void
GIG_advertise_dialog_start_ (const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv)
{
  struct AdvertiseContext *ac;
  gint i;
  GtkTreeIter iter;

  ac = GNUNET_new (struct AdvertiseContext);
  ac->priv = *priv;
  ac->builder =
    GNUNET_GTK_get_new_builder ("gnunet_identity_gtk_advertise_dialog.glade",
				ac);
  if (NULL == ac->builder)
  {
    GNUNET_break (0);
    GNUNET_free (ac);
    return;
  }
  GNUNET_GTK_setup_expiration_year_adjustment (ac->builder);
  ac->dialog = GTK_WIDGET (gtk_builder_get_object
			   (ac->builder, "GNUNET_GTK_identity_advertise_dialog"));
  ac->keywords = GTK_LIST_STORE (gtk_builder_get_object
				(ac->builder, "GNUNET_GTK_identity_advertise_keywords_liststore"));
  ac->metadata = GTK_LIST_STORE (gtk_builder_get_object
			     (ac->builder, "GNUNET_GTK_identity_advertise_metadata_liststore"));
  ac->meta_types = GTK_LIST_STORE (gtk_builder_get_object
				   (ac->builder, "GNUNET_GTK_identity_metadata_types_liststore"));
  for (i = 0; EXTRACTOR_METATYPE_RESERVED != types[i]; i++)
  {
    gtk_list_store_insert_with_values (ac->meta_types,
				       &iter, G_MAXINT,
				       METATYPES_MC_TYPE_AS_STRING,
				       EXTRACTOR_metatype_to_string (types[i]),
				       METATYPES_MC_TYPE_AS_ENUM,
				       types[i],
				       -1);
    if (0 == i)
      gtk_combo_box_set_active_iter (GTK_COMBO_BOX
				     (gtk_builder_get_object
				      (ac->builder,
				       "GNUNET_GTK_identity_advertise_metadata_type_combobox")),
				     &iter);
  }
  gtk_list_store_insert_with_values (ac->keywords,
				     &iter, G_MAXINT,
				     KEYWORDS_MC_KEYWORD, NEW_KEYWORD_TEXT,
				     -1);
  gtk_window_present (GTK_WINDOW (ac->dialog));
}


/* end of gnunet-identity-gtk_advertise.c */