aboutsummaryrefslogtreecommitdiff
path: root/src/identity/gnunet-identity-gtk.c
blob: 11800fce2cb71e890cbd12aa8baa4b6672bc3308 (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
/*
     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/identity/gnunet-identity-gtk.c
 * @brief Main function of gnunet-identity-gtk
 * @author Christian Grothoff
 */
#include "gnunet_gtk.h"
#include <gnunet/gnunet_identity_service.h>
#include "gnunet-identity-gtk_advertise.h"


/**
 * Columns in the identity model.
 */
enum IDENTITY_ModelColumns
  {
    /**
     * A gchararray
     */
    IDENTITY_MC_NAME = 0,

    /**
     * A gchararray
     */
    IDENTITY_MC_IDENTIFIER = 1,

    /**
     * A 'struct GNUNET_IDENTIFIER_Ego'
     */
    IDENTITY_MC_EGO = 2

  };


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

/**
 * Handle to IDENTITY service.
 */
static struct GNUNET_IDENTITY_Handle *identity;

/**
 * Main window list store.
 */
static GtkListStore *ls;


/**
 * We need to track active operations with the identity service.
 */
struct OperationContext
{

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

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

  /**
   * Operation handle with the identity service.
   */
  struct GNUNET_IDENTITY_Operation *op;

};


/**
 * Head of operations.
 */
static struct OperationContext *oc_head;

/**
 * Tail of operations.
 */
static struct OperationContext *oc_tail;


/**
 * Get our configuration.
 *
 * @return configuration handle
 */
const struct GNUNET_CONFIGURATION_Handle *
GIG_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
 */
static GObject *
get_object (const char *name)
{
  return GNUNET_GTK_main_loop_get_object (ml, name);
}


/**
 * Identity operation was finished, clean up.
 *
 * @param cls the 'struct OperationContext'
 * @param emsg error message (NULL on success)
 */
static void
operation_finished (void *cls,
		    const char *emsg)
{
  struct OperationContext *oc = cls;

  GNUNET_CONTAINER_DLL_remove (oc_head,
			       oc_tail,
			       oc);
  gtk_widget_set_sensitive (GTK_WIDGET (get_object ("GNUNET_GTK_identity_treeview")),
			    TRUE);
  GNUNET_free (oc);
}


/**
 * Context for the advertise popup menu.
 */
struct AdvertisePopupContext
{
  /**
   * Ego to advertise.
   */
  struct GNUNET_IDENTITY_Ego *ego;

};


/**
 * "Advertise" was selected in the current context menu.
 *
 * @param item the 'advertise' menu item
 * @param user_data the 'struct AdvertisePopupContext' of the menu
 */
static void
advertise_ctx_menu (GtkMenuItem *item,
		    gpointer user_data)
{
  struct AdvertisePopupContext *apc = user_data;
  const struct GNUNET_CRYPTO_EcdsaPrivateKey *priv;

  priv = GNUNET_IDENTITY_ego_get_private_key (apc->ego);
  GIG_advertise_dialog_start_ (priv);
}


/**
 * An item was selected from the context menu; destroy the menu shell.
 *
 * @param menushell menu to destroy
 * @param user_data the 'struct AdvertisePopupContext' of the menu
 */
static void
advertise_popup_selection_done (GtkMenuShell *menushell,
				gpointer user_data)
{
  struct AdvertisePopupContext *apc = user_data;

  gtk_widget_destroy (GTK_WIDGET (menushell));
  GNUNET_free (apc);
}


/**
 * User clicked in the treeview widget.  Check for right button
 * to possibly launch advertise window.
 *
 * @param widget the treeview widget
 * @param event the event, we only care about button events
 * @param user_data unused
 * @return FALSE if no menu could be popped up,
 *         TRUE if there is now a pop-up menu
 */
gboolean
GNUNET_GTK_identity_treeview_button_press_event_cb (GtkWidget *widget,
						    GdkEvent *event,
						    gpointer user_data)
{
  GtkTreeView *tv = GTK_TREE_VIEW (widget);
  GdkEventButton *event_button = (GdkEventButton *) event;
  GtkTreeModel *tm;
  GtkTreePath *path;
  GtkTreeIter iter;
  GtkMenu *menu;
  GtkWidget *child;
  struct AdvertisePopupContext *apc;
  struct GNUNET_IDENTITY_Ego *ego;

  if ( (GDK_BUTTON_PRESS != event->type) ||
       (3 != event_button->button) )
    return FALSE;
  if (! gtk_tree_view_get_path_at_pos (tv,
				       event_button->x, event_button->y,
                                       &path, NULL, NULL, NULL))
    return FALSE; /* click outside of area with values, ignore */
  tm = gtk_tree_view_get_model (tv);
  if (! gtk_tree_model_get_iter (tm, &iter, path))
    {
      gtk_tree_path_free (path);
      return FALSE; /* not sure how we got a path but no iter... */
    }
  gtk_tree_path_free (path);
  gtk_tree_model_get (GTK_TREE_MODEL (ls), &iter,
                      IDENTITY_MC_EGO, &ego,
                      -1);
  if (NULL == ego)
    return FALSE;
  apc = GNUNET_new (struct AdvertisePopupContext);
  apc->ego = ego;
  menu = GTK_MENU (gtk_menu_new ());
  child = gtk_menu_item_new_with_label (_("_Advertise"));
  g_signal_connect (child, "activate",
		    G_CALLBACK (advertise_ctx_menu), apc);
  gtk_label_set_use_underline (GTK_LABEL
			       (gtk_bin_get_child (GTK_BIN (child))),
			       TRUE);
  gtk_widget_show (child);
  gtk_menu_shell_append (GTK_MENU_SHELL (menu), child);
  g_signal_connect (menu, "selection-done",
		    G_CALLBACK (advertise_popup_selection_done), apc);
  gtk_menu_popup_at_pointer (menu,
                             event);
  return FALSE;
}


/**
 * User pushed a key (possibly DEL) in the treeview widget.
 * Delete the selected entry if the key was DEL.
 *
 * @param widget the entry widget
 * @param event the key stroke
 * @param user_data the main window context
 * @return FALSE if this was not ENTER, TRUE if it was
 */
gboolean
GNUNET_GTK_identity_treeview_key_press_event_cb (GtkWidget * widget,
						 GdkEventKey * event,
						 gpointer user_data)
{
  gchar *old;
  struct OperationContext *oc;
  GtkTreeSelection *sel;
  GtkTreeIter iter;

  if (GDK_KEY_Delete != event->keyval)
    return FALSE;
  sel = gtk_tree_view_get_selection (GTK_TREE_VIEW (get_object ("GNUNET_GTK_identity_treeview")));
  if (! gtk_tree_selection_get_selected (sel, NULL, &iter))
    return FALSE;
  gtk_tree_model_get (GTK_TREE_MODEL (ls),
		      &iter,
		      IDENTITY_MC_NAME, &old,
		      -1);
  oc = GNUNET_new (struct OperationContext);
  GNUNET_CONTAINER_DLL_insert (oc_head,
			       oc_tail,
			       oc);
  oc->op = GNUNET_IDENTITY_delete (identity,
				   old,
				   &operation_finished,
				   oc);
  return TRUE;
}


/**
 * The user edited one of the names of the egos.  Change it
 * in the IDENTITY service.
 *
 * @param renderer renderer where the change happened
 * @param path location in the model where the change happened
 * @param new_text updated text
 * @param user_data internal context (not used)
 */
void
GNUNET_GTK_namespace_organizer_namespaces_treeview_column_name_text_edited_cb
(GtkCellRendererText *renderer,
 gchar *path,
 gchar *new_text,
 gpointer user_data)
{
  GtkTreePath *treepath;
  GtkTreeIter iter;
  struct GNUNET_IDENTITY_Ego *ego;
  gchar *old;
  struct OperationContext *oc;

  treepath = gtk_tree_path_new_from_string (path);
  if (! gtk_tree_model_get_iter (GTK_TREE_MODEL (ls),
				 &iter,
				 treepath))
  {
    GNUNET_break (0);
    gtk_tree_path_free (treepath);
    return;
  }
  gtk_tree_path_free (treepath);
  gtk_tree_model_get (GTK_TREE_MODEL (ls),
		      &iter,
		      IDENTITY_MC_NAME, &old,
		      IDENTITY_MC_EGO, &ego,
		      -1);
  gtk_widget_set_sensitive (GTK_WIDGET (get_object ("GNUNET_GTK_identity_treeview")),
			    FALSE);
  oc = GNUNET_new (struct OperationContext);
  GNUNET_CONTAINER_DLL_insert (oc_head,
			       oc_tail,
			       oc);
  if (NULL == ego)
  {
    /* create operation */
    oc->op = GNUNET_IDENTITY_create (identity,
				     new_text,
				     &operation_finished,
				     oc);
  }
  else if (0 != strlen (new_text))
  {
    /* rename operation */
    oc->op = GNUNET_IDENTITY_rename (identity,
				     old, new_text,
				     &operation_finished,
				     oc);
  }
  else
  {
    /* delete operation */
    oc->op = GNUNET_IDENTITY_delete (identity,
				     old,
				     &operation_finished,
				     oc);
  }
}


/**
 * Task run on shutdown.
 *
 * @param cls unused
 */
static void
shutdown_task (void *cls)
{
  struct OperationContext *oc;

  GIG_advertise_shutdown_ ();
  while (NULL != (oc = oc_head))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
		_("Operation not completed due to shutdown\n"));
    GNUNET_IDENTITY_cancel (oc->op);
    GNUNET_CONTAINER_DLL_remove (oc_head,
				 oc_tail,
				 oc);
    GNUNET_free (oc);
  }
  if (NULL != identity)
  {
    GNUNET_IDENTITY_disconnect (identity);
    identity = NULL;
  }
  GNUNET_GTK_main_loop_quit (ml);
  ml = NULL;
}


/**
 * Callback invoked if the application is supposed to exit.
 *
 * @param object
 * @param user_data unused
 */
void
GNUNET_GTK_identity_quit_cb (GObject * object, gpointer user_data)
{
  GNUNET_SCHEDULER_shutdown ();
}


/**
 * Add all updateable entries of the current namespace to the
 * tree store.
 *
 * @param cls our 'struct MainPublishingDialogContext'
 * @param ego identity of the namespace to add
 * @param ego_ctx where to store context data
 * @param name name of the namespace to add
 */
static void
add_ego (void *cls,
	 struct GNUNET_IDENTITY_Ego *ego,
	 void **ego_ctx,
	 const char *name)
{
  GtkTreePath *path;
  GtkTreeRowReference *rr;
  GtkTreeIter iter;
  char *id;
  struct GNUNET_CRYPTO_EcdsaPublicKey pub;

  if (NULL == ego)
    return; /* nothing to be done */
  rr = *ego_ctx;
  if (NULL == rr)
  {
    /* insert operation */
    GNUNET_assert (NULL != name);
    GNUNET_IDENTITY_ego_get_public_key (ego, &pub);
    id = GNUNET_CRYPTO_ecdsa_public_key_to_string (&pub);
    gtk_list_store_insert_with_values (ls,
				       &iter, G_MAXINT,
				       IDENTITY_MC_NAME, name,
				       IDENTITY_MC_IDENTIFIER, id,
				       IDENTITY_MC_EGO, ego,
				       -1);
    GNUNET_free (id);
    path = gtk_tree_model_get_path (GTK_TREE_MODEL (ls),
				    &iter);
    rr = gtk_tree_row_reference_new (GTK_TREE_MODEL (ls),
				     path);
    gtk_tree_path_free (path);
    *ego_ctx = rr;
  }
  else if (NULL == name)
  {
    /* delete operation */
    path = gtk_tree_row_reference_get_path (rr);
    gtk_tree_row_reference_free (rr);
    GNUNET_assert (gtk_tree_model_get_iter (GTK_TREE_MODEL (ls),
					    &iter, path));
    gtk_tree_path_free (path);
    gtk_list_store_remove (ls, &iter);
    *ego_ctx = NULL;
  }
  else
  {
    /* rename operation */
    path = gtk_tree_row_reference_get_path (rr);
    GNUNET_assert (gtk_tree_model_get_iter (GTK_TREE_MODEL (ls),
					    &iter, path));
    gtk_list_store_set (ls,
			&iter,
			IDENTITY_MC_NAME, name,
			-1);
    gtk_tree_path_free (path);
  }
}


/**
 * Actual main function run right after GNUnet's scheduler
 * is initialized.  Initializes up GTK and Glade.
 *
 * @param cls NULL
 */
static void
run (void *cls)
{
  GtkWidget *main_window;
  GtkTreeIter iter;

  ml = cls;
  if (GNUNET_OK != GNUNET_GTK_main_loop_build_window (ml, NULL))
    return;
  GNUNET_GTK_set_icon_search_path ();
  GNUNET_GTK_setup_nls ();
  /* setup main window */
  main_window = GTK_WIDGET (get_object ("GNUNET_GTK_identity_window"));
  main_window = GNUNET_GTK_plug_me ("GNUNET_IDENTITY_GTK_PLUG",
                                    main_window);
  ls = GTK_LIST_STORE (get_object ("GNUNET_GTK_identity_liststore"));
  GNUNET_assert (NULL != ls);
  gtk_list_store_insert_with_values (ls,
				     &iter, G_MAXINT,
				     IDENTITY_MC_NAME, "<create>",
				     -1);

  gtk_window_maximize (GTK_WINDOW (main_window));
  /* make GUI visible */
  gtk_widget_show (main_window);
  gtk_window_present (GTK_WINDOW (main_window));
  identity = GNUNET_IDENTITY_connect (GIG_get_configuration (),
				      &add_ego,
				      NULL);
  GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
}


/**
 * Main function of gnunet-identity-gtk.
 *
 * @param argc number of arguments
 * @param argv arguments
 * @return 0 on success
 */
int
main (int argc, char *const *argv)
{
  static struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_OPTION_END
  };

  if (GNUNET_OK !=
      GNUNET_GTK_main_loop_start ("gnunet-identity-gtk",
                                  "GTK GUI for managing egos", argc,
                                  argv, options,
                                  "gnunet_identity_gtk_main_window.glade",
                                  &run))
    return 1;
  return 0;
}


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