aboutsummaryrefslogtreecommitdiff
path: root/src/conversation/gnunet-conversation-gtk_contacts.c
blob: aee0360478a89dcfd45ed2c40b32c7eb4d903031 (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
/*
     This file is part of GNUnet.
     (C) 2010-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/conversation/gnunet-conversation-gtk_contacts.c
 * @brief
 * @author yids
 * @author hark
 */
#include "gnunet-conversation-gtk.h"
#include "gnunet-conversation-gtk_contacts.h"
#include "gnunet-conversation-gtk_egos.h"


/**
 * Columns in the #contacts_liststore.
 */
enum ContactsListstoreValues
{
  /**
   * Human-readable name of the label in the zone.
   */
  CONTACTS_LS_NAME = 0,

  /**
   * Type of the label (as a 'const char *')
   */
  CONTACTS_LS_TYPE = 1
};


/**
 * Our ego.
 */
static struct GNUNET_IDENTITY_Ego *contacts_ego;

/**
 * List of contacts (records).
 */
static GtkListStore *contacts_liststore;

/**
 * zone treeview
 */
static GtkTreeView *zone_treeview;

/**
 * zone tree model
 */
static GtkTreeModel *zone_treemodel;

/**
 * The main tree view for 'gns' that shows the records.
 */
static GtkTreeView *contacts_treeview;

/**
 * Tree model (same object as 'contacts_treestore', just different type).
 */
static GtkTreeModel *contacts_treemodel;

/**
 * Monitor to view information in our current zone.
 */
static struct GNUNET_NAMESTORE_ZoneMonitor *zone_mon;

/**
 * Handle to the namestore.
 */
static struct GNUNET_NAMESTORE_Handle *ns;

/**
 * Private key for the our zone.
 */
static struct GNUNET_CRYPTO_EcdsaPrivateKey zone_pkey;

/**
 * Name of the records to add/list/remove.
 */
static char *name;

/**
 * Queue entry for the 'add' operation.
 */
static struct GNUNET_NAMESTORE_QueueEntry *add_qe;

/**
 * Queue entry for the 'del' operation.
 */
static struct GNUNET_NAMESTORE_QueueEntry *del_qe;



/**
 * Continuation called to notify client about result of the
 * operation.
 *
 * @param cls closure, location of the QueueEntry pointer to NULL out
 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
 *                #GNUNET_NO if content was already there
 *                #GNUNET_YES (or other positive value) on success
 * @param emsg NULL on success, otherwise an error message
 */
static void
add_continuation (void *cls,
                  int32_t success,
                  const char *emsg)
{

  struct GNUNET_NAMESTORE_QueueEntry **qe = cls;

  *qe = NULL;
  if (GNUNET_YES != success)
  {
    fprintf (stderr, _("Adding record failed: %s\n"),
             (GNUNET_NO == success) ? "record exists" : emsg);
    if (GNUNET_NO != success)
      GCG_log("GNUNET_NO != success");
        //ret = 1;
  }
  GCG_log("blaat");
  //ret = 0;
  //test_finished ();
}


/**
 * Continuation called to notify client about result of the
 * operation.
 *
 * @param cls closure, unused
 * @param success #GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
 *                #GNUNET_NO if content was already there
 *                #GNUNET_YES (or other positive value) on success
 * @param emsg NULL on success, otherwise an error message
 */
static void
del_continuation (void *cls,
                  int32_t success,
                  const char *emsg)
{
  del_qe = NULL;
  if (GNUNET_NO == success)
  {
    fprintf (stderr, _("Deleting record failed, record does not exist%s%s\n"),
             (NULL != emsg) ? ": " : "", (NULL != emsg) ? emsg : "");
  }
  if (GNUNET_SYSERR == success)
  {
    fprintf (stderr, _("Deleting record failed%s%s\n"),
             (NULL != emsg) ? ": " : "", (NULL != emsg) ? emsg : "");
  }
  // test_finished ();
}


/**
 * add a new contact
 *
 * @param name
 * @param address
 */
static void
add_contact (const gchar *name,
             const gchar *address)
{
  GtkTreeIter iter;
  struct GNUNET_GNSRECORD_Data rd;
  struct GNUNET_GNSRECORD_Data *rde;
  static void *data;
  static size_t data_size;
  struct GNUNET_CRYPTO_EcdsaPublicKey pkey;
  struct GNUNET_IDENTITY_Ego *ego;

  ego = GCG_EGOS_get_selected_ego ();
  zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (ego);
  GNUNET_CRYPTO_ecdsa_public_key_from_string (address,
                                              strlen (address),
                                              &pkey);
  rde = &rd;
  rd.data = &pkey;
  rd.data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
  rd.record_type = GNUNET_GNSRECORD_TYPE_PKEY;
  rd.flags |= GNUNET_GNSRECORD_RF_RELATIVE_EXPIRATION;  // always set to relative for testing purposes
  rde->flags |= GNUNET_GNSRECORD_RF_PRIVATE;
  rde->expiration_time = GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us;
  //FPRINTF (stderr, "adding\n");
  //FPRINTF (stderr, "name: %s\n", name);
  //FPRINTF (stderr, "address: %s\n", address);
  if (GNUNET_OK !=
      GNUNET_GNSRECORD_string_to_value (65536, address, &data, &data_size))
  {
    FPRINTF (stderr, "invalid address\n");
  }
  else
  {
    add_qe =
        GNUNET_NAMESTORE_records_store (ns, &zone_pkey, name, 1, rde,
                                        &add_continuation, &add_qe);
    gtk_list_store_append (contacts_liststore, &iter);
    gtk_list_store_set (contacts_liststore, &iter, 1, "PKEY", 0, name, -1);
  }
}


/**
 * executed when clicked on add contact
 * @param button
 * @param user_data
 */
void
GNUNET_CONVERSATION_GTK_on_add_clicked (GtkButton * button,
                                        gpointer * user_data)
{
  GtkEntry *nameEntry;
  GtkEntry *addressEntry;

  nameEntry = GTK_ENTRY (GCG_get_main_window_object  ("GNUNET_GTK_conversation_nameAdd"));
  addressEntry = GTK_ENTRY (GCG_get_main_window_object ("GNUNET_GTK_conversation_addressAdd"));
  add_contact (gtk_entry_get_text (nameEntry),gtk_entry_get_text(addressEntry));
}


void
GNUNET_CONVERSATION_GTK_on_remove_clicked (GtkButton * button,
                                           gpointer * user_data)
{
  GtkTreeSelection *selection;
  GtkTreeModel *model;
  GtkTreeIter iter;
  GtkDialog *confirm;
  GtkWindow *main_window;
  GtkLabel *notification;
  GtkHBox *content_area;

  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (contacts_treeview));
  if (gtk_tree_selection_get_selected (selection, &model, &iter))
  {
    gtk_tree_model_get (model, &iter, 0, &name, -1);
    //FPRINTF (stderr, "selected %s \n", name);
    main_window = GTK_WINDOW (GCG_get_main_window_object  ("GNUNET_GTK_conversation_window"));
    confirm =
        GTK_DIALOG (gtk_dialog_new_with_buttons
                    ("Removing contact", main_window,
                     GTK_DIALOG_DESTROY_WITH_PARENT, _("Yes"),
                     GTK_RESPONSE_ACCEPT, _("No"), GTK_RESPONSE_CANCEL, NULL));
    content_area =
        GTK_HBOX (gtk_dialog_get_content_area (GTK_DIALOG (confirm)));
    notification =
        GTK_LABEL (gtk_label_new
                   ("Do you really want to remove this contact?"));
    gtk_container_add (GTK_CONTAINER (content_area), GTK_WIDGET (notification));
    gtk_widget_show_all (GTK_WIDGET (confirm));
    switch (gtk_dialog_run (confirm))
    {
    case GTK_RESPONSE_ACCEPT:
      zone_pkey = *GNUNET_IDENTITY_ego_get_private_key (contacts_ego);
      del_qe =
          GNUNET_NAMESTORE_records_store (ns, &zone_pkey, name, 0, NULL,
                                          &del_continuation, NULL);
      gtk_list_store_remove (contacts_liststore, &iter);
      gtk_widget_destroy (GTK_WIDGET (confirm));
      break;
    case GTK_RESPONSE_CANCEL:
      FPRINTF (stderr, "not removing \n");
      gtk_widget_destroy (GTK_WIDGET (confirm));
      break;
    }

  }
}


/**
 * FIXME: what was clicked where?
 */
void
GNUNET_CONVERSATION_GTK_on_current_clicked (GtkButton *button,
                                            gpointer *user_data)
{
  GtkEntry *addressEntry;

  addressEntry = GTK_ENTRY (GCG_get_main_window_object ("GNUNET_GTK_conversation_addressAdd"));
  gtk_entry_set_text (addressEntry,
                      "FIXME");

}


/**
 * A row was activated in the contacts list. Initiate call.
 *
 * @return void
 */
void
GNUNET_CONVERSATION_GTK_row_activated ()
{
  char *address;
  gchar *type;
  GtkTreeSelection *selection;
  GtkTreeIter iterA;
  GtkEntry *address_entry;

  selection = gtk_tree_view_get_selection (contacts_treeview);
  gtk_tree_selection_get_selected (selection, &contacts_treemodel, &iterA);
  gtk_tree_model_get (contacts_treemodel,
                      &iterA,
                      CONTACTS_LS_NAME, &name,
                      CONTACTS_LS_TYPE, &type,
                      -1);
  if (0 == strcmp (type, "PKEY"))
  {
    GNUNET_asprintf (&address, "call.%s.gnu", name);
  }
  else
  {
    GNUNET_assert (0 == strcmp (type, "PHONE"));
    GNUNET_asprintf (&address, "%s.gnu", name);
  }
  g_free (type);

  address_entry = GTK_ENTRY (GCG_get_main_window_object ("GNUNET_GTK_conversation_address"));
  gtk_entry_set_text (address_entry,
                      address);
  do_call (address);
  GNUNET_free (address);
}


/**
 * Process a record that was stored or modified the namestore by
 * adding/modifying/removing it in the 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 if label was deleted
 * @param rd array of records with data to store
 */
static void
display_record (void *cls,
                const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
                const char *rname,
                unsigned int rd_len,
                const struct GNUNET_GNSRECORD_Data *rd)
{
  unsigned int i;
  GtkTreeIter iter;
  gboolean do_display;
  const char *type;

  if (NULL == zone_key)
  {
    /* disconnect, clear (and possibly freeze) view */
    gtk_list_store_clear (contacts_liststore);
    return;
  }
  do_display = FALSE;
  for (i = 0; i < rd_len; i++)
  {
    switch (rd[i].record_type)
    {
    case GNUNET_GNSRECORD_TYPE_PKEY:
      type = "PKEY";
      do_display = TRUE;
      break;
    case GNUNET_GNSRECORD_TYPE_PHONE:
      type = "PHONE";
      do_display = TRUE;
      break;
    default:
      /* ignore, not useful for conversation */
      break;
    }
  }
  /* FIXME: check if exists, if so, update or delete */


  /* insert new record */
  if (! do_display)
    return;
  gtk_list_store_append (contacts_liststore,
                         &iter);
  gtk_list_store_set (contacts_liststore,
                      &iter,
                      CONTACTS_LS_NAME, rname,
                      CONTACTS_LS_TYPE, type,
                      -1);
}


/**
 * Function called once the monitor has caught up with the current
 * state of the database.  Will be called AGAIN after each disconnect
 * (record monitor called with 'NULL' for zone_key) once we're again
 * in sync.
 *
 * Could be used to optimize visuals if we block GTK updates while the
 * list is not in sync.
 *
 * @param cls closure
 */
static void
unfreeze_view (void *cls)
{
  // tbd
}


/**
 * A different zone was selected in the zone toggle bar.  Load the
 * appropriate zone.
 *
 * @param widget combobox that was changed, unused
 * @param user_data builder, unused
 */
void
gnunet_conversation_gtk_contacts_zone_combobox_changed_cb (GtkComboBox *widget,
                                                           gpointer user_data)
{
  struct GNUNET_IDENTITY_Ego *ego;
  const struct GNUNET_CRYPTO_EcdsaPrivateKey *temp_zone_pkey;

  ego = GCG_EGOS_get_selected_ego ();
  temp_zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
  if (NULL != zone_mon)
  {
    GNUNET_NAMESTORE_zone_monitor_stop (zone_mon);
    zone_mon = NULL;
  }
  gtk_list_store_clear (contacts_liststore);
  zone_mon = GNUNET_NAMESTORE_zone_monitor_start (GCG_get_configuration (),
                                                  temp_zone_pkey,
                                                  GNUNET_YES,
                                                  &display_record,
                                                  &unfreeze_view,
                                                  NULL);
}


/**
 * Initialize the contact list
 */
void
GCG_CONTACTS_init ()
{
  contacts_liststore
    = GTK_LIST_STORE (GCG_get_main_window_object
                      ("gnunet_conversation_gtk_contacts_liststore"));
  contacts_treemodel = GTK_TREE_MODEL (contacts_liststore);
  contacts_treeview
    = GTK_TREE_VIEW (GCG_get_main_window_object ("gnunet_conversation_gtk_treeview"));
  // gtk_tree_view_set_activate_on_single_click (contacts_treeview, TRUE);

  zone_treemodel
    =  GTK_TREE_MODEL (GCG_get_main_window_object ("gnunet_conversation_gtk_contacts_zone_liststore"));
  zone_treeview
    = GTK_TREE_VIEW (GCG_get_main_window_object ("gnunet_conversation_gtk_zone_treeview"));
  ns = GNUNET_NAMESTORE_connect (GCG_get_configuration ());
}


/**
 * Shutdown the contact list
 */
void
GCG_CONTACTS_shutdown ()
{
  if (NULL != zone_mon)
  {
    GNUNET_NAMESTORE_zone_monitor_stop (zone_mon);
    zone_mon = NULL;
  }
  if (NULL != ns)
  {
    GNUNET_NAMESTORE_disconnect (ns);
    ns = NULL;
  }
  GNUNET_CRYPTO_ecdsa_key_clear (&zone_pkey);
}

/* end of gnunet-conversation-gtk_contacts.c */