aboutsummaryrefslogtreecommitdiff
path: root/src/conversation/gnunet-conversation-gtk_contacts.c
blob: e131847b2e0f818652f80400b3d0c41fc21e6eb0 (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
/*
     This file is part of GNUnet.
     Copyright (C) 2010-2014, 2018 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/conversation/gnunet-conversation-gtk_contacts.c
 * @brief display the address book with the list of known contacts
 *        and launch calls if user activates entry in address book
 * @author yids
 * @author hark
 * @author Christian Grothoff
 */
#include "gnunet-conversation-gtk.h"
#include "gnunet-conversation-gtk_contacts.h"
#include "gnunet-conversation-gtk_get_label.h"
#include "gnunet-conversation-gtk_phone.h"
#include "gnunet-conversation-gtk_zones.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,

  /**
   * Value stored under the label (as a `const char *`)
   */
  CONTACTS_LS_VALUE = 2,

  /**
   * Is the record public (as a `gboolean`)
   */
  CONTACTS_LS_IS_PUBLIC = 3,

  /**
   * Is the 'public' flag editable (once implemented, negation
   * of #CONTACT_IS_PUBLIC), a `gboolean`.
   */
  CONTACTS_LS_IS_EDITABLE = 4
};


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

/**
 * Tree model of the contacts treeview, same objects as #contacts_liststore.
 */
static GtkTreeModel *contacts_treemodel;

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

/**
 * The tree view widget.
 */
static GtkWidget *contacts_treeview;

/**
 * The "paste" button.
 */
static GtkWidget *b_paste;


/**
 * A row was activated in the contacts list.  Initiate call.
 *
 * @param tree_view view where the row was activated
 * @param path path to the activated element
 * @param column column that was activated
 * @param user_data builder context (unused)
 */
void
gnunet_conversation_gtk_contact_list_treeview_row_activated_cb  (GtkTreeView *tree_view,
                                                                 GtkTreePath *path,
                                                                 GtkTreeViewColumn *column,
                                                                 gpointer user_data)
{
  char *address;
  gchar *type;
  GtkTreeIter iter;
  gchar *name;
  GtkEntry *address_entry;
  const char *tld;

  GNUNET_assert (NULL != GCG_ZONES_get_selected_zone (&tld));
  if (! gtk_tree_model_get_iter (contacts_treemodel,
                                 &iter,
                                 path))
  {
    GNUNET_break (0);
    return;
  }
  gtk_tree_model_get (contacts_treemodel,
                      &iter,
                      CONTACTS_LS_NAME, &name,
                      CONTACTS_LS_TYPE, &type,
                      -1);
  if (0 == strcmp (type, "PKEY"))
  {
    GNUNET_asprintf (&address,
                     "phone.%s.%s",
                     name,
                     tld);
  }
  else
  {
    GNUNET_asprintf (&address,
                     "%s.%s",
                     name,
                     tld);
  }
  g_free (name);
  g_free (type);
  address_entry = GTK_ENTRY (GCG_get_main_window_object
                             ("gnunet_conversation_gtk_address_entry"));
  gtk_entry_set_text (address_entry,
                      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_key private key of the zone; NULL on disconnect
 * @param rname label of the records; NULL on disconnect
 * @param rd_len 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)
{
  GtkTreeIter iter;
  gboolean do_display;
  const char *type;
  gchar *lname;
  gboolean update;
  gboolean is_public;
  char *value;

  GNUNET_NAMESTORE_zone_monitor_next (zone_mon,
                                      1);
  do_display = FALSE;
  value = NULL;
  for (unsigned int i = 0; i < rd_len; i++)
  {
    switch (rd[i].record_type)
    {
    case GNUNET_GNSRECORD_TYPE_PKEY:
      type = "PKEY";
      do_display = TRUE;
      break;
    case GNUNET_DNSPARSER_TYPE_CNAME:
      type = "CNAME";
      do_display = TRUE;
      break;
    case GNUNET_GNSRECORD_TYPE_PHONE:
      type = "PHONE";
      do_display = TRUE;
      break;
    default:
      /* ignore, not useful for conversation */
      type = NULL; /* make compiler happy */
      break;
    }
    if (do_display)
    {
      value = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
                                                rd[i].data,
                                                rd[i].data_size);
      if (NULL == value)
      {
	GNUNET_break (0);
	return;
      }
      is_public = (0 == (rd[i].flags & GNUNET_GNSRECORD_RF_PRIVATE));
      break;
    }
    else
    {
      /* make compiler happy */
      is_public = FALSE;
    }
  }

  /* check if exists, if so, update or remove */
  update = FALSE;
  if (gtk_tree_model_get_iter_first (contacts_treemodel,
                                     &iter))
  {
    do
    {
      gtk_tree_model_get (contacts_treemodel,
                          &iter,
                          CONTACTS_LS_NAME, &lname,
                          -1);
      if (0 == strcmp (lname,
                       rname))
      {
        if (! do_display)
        {
          /* remove */
          gtk_list_store_remove (contacts_liststore,
                                 &iter);
          g_free (lname);
          GNUNET_free_non_null (value);
          return;
        }
        /* update */
        update = TRUE;
        break;
      }
      g_free (lname);
    }
    while (gtk_tree_model_iter_next (contacts_treemodel,
                                     &iter));
  }

  /* insert new record */
  if (! do_display)
  {
    GNUNET_assert (NULL == value);
    return;
  }
  if (! update)
    gtk_list_store_append (contacts_liststore,
                           &iter);
  gtk_list_store_set (contacts_liststore,
                      &iter,
                      CONTACTS_LS_NAME, rname,
                      CONTACTS_LS_TYPE, type,
                      CONTACTS_LS_VALUE, value,
                      CONTACTS_LS_IS_PUBLIC, is_public,
                      CONTACTS_LS_IS_EDITABLE, FALSE /* FIXME: editing to be implemented */,
                      -1);
  GNUNET_free (value);
}


/**
 * 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)
{
  gtk_widget_show (contacts_treeview);
}


/**
 * Function called once the monitor has disconnected from the
 * database.
 *
 * @param cls closure
 */
static void
freeze_view (void *cls)
{
  gtk_list_store_clear (contacts_liststore);
  gtk_widget_hide (contacts_treeview);
}


/**
 * 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;
  const char *tld;

  if (NULL != zone_mon)
  {
    GNUNET_NAMESTORE_zone_monitor_stop (zone_mon);
    zone_mon = NULL;
  }
  gtk_list_store_clear (contacts_liststore);
  ego = GCG_ZONES_get_selected_zone (&tld);
  if (NULL == ego)
  {
    /* ego deselected (likely shutdown) */
    gtk_widget_set_sensitive (b_paste, FALSE);
    return;
  }
  gtk_widget_hide (contacts_treeview);
  gtk_widget_set_sensitive (b_paste, TRUE);
  temp_zone_pkey = GNUNET_IDENTITY_ego_get_private_key (ego);
  zone_mon = GNUNET_NAMESTORE_zone_monitor_start (GCG_get_configuration (),
                                                  temp_zone_pkey,
                                                  GNUNET_YES,
                                                  &freeze_view,
                                                  NULL,
                                                  &display_record,
                                                  NULL,
                                                  &unfreeze_view,
                                                  NULL);
}


/**
 * User clicked the "paste" button.  Handle data from the clipboard,
 * if it is a "gnunet://gns/"-URI, import it into our current zone /
 * address book.
 *
 * @param clipboard the clipboard
 * @param text the text from the board
 * @param data NULL
 */
static void
handle_paste_data (GtkClipboard *clipboard,
                   const gchar *text,
                   gpointer data)
{
  char *name;
  char *slash;
  const char *label;

  if (NULL == text)
    return; /* clipboard empty */
  if (0 != strncasecmp ("gnunet://gns/",
                        text,
                        strlen ("gnunet://gns/")))
  {
    GCG_log (_("Invalid URI `%s'\n"),
             text);
    return;
  }
  name = GNUNET_strdup (&text[strlen ("gnunet://gns/")]);
  slash = strchr (name, '/');
  if ( (NULL != slash) &&
       ('\0' != slash[1]) )
  {
    *slash = '\0';
    label = &slash[1];
    if (GNUNET_OK !=
        GNUNET_DNSPARSER_check_label (label))
    {
      GCG_log (_("Invalid label `%s' in URI `%s'\n"),
               label,
               text);
      GNUNET_free (name);
      return;
    }
    else
    {
      /* got a label already, try to use it */
      GSC_get_label_for_name (name,
                              label);
      GNUNET_free (name);
      return;
    }
  }
  if (NULL != slash)
    *slash = '\0';
  /* don't have a label yet, need to prompt! */
  GSC_get_label_for_name (name,
                          NULL);
  GNUNET_free (name);
}


/**
 * User clicked the "paste" button.  Copy address information
 * from the clipboard into our current zone / address book.
 *
 * @param button the button
 * @param user_data builder (unused)
 */
void
gnunet_conversation_gtk_contacts_paste_button_clicked_cb (GtkButton *button,
							  gpointer user_data)
{
  GtkClipboard *cb;

  cb = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
  gtk_clipboard_request_text (cb,
                              &handle_paste_data,
                              NULL);
}


/**
 * Initialize the contact list
 */
void
GCG_CONTACTS_init ()
{
  contacts_treeview
    = GTK_WIDGET (GCG_get_main_window_object
		  ("gnunet_conversation_gtk_contact_list_treeview"));
#if GTK_CHECK_VERSION(3,10,0)
  gtk_tree_view_set_activate_on_single_click (GTK_TREE_VIEW (contacts_treeview),
                                              TRUE);
#endif
  contacts_liststore
    = GTK_LIST_STORE (GCG_get_main_window_object
                      ("gnunet_conversation_gtk_contacts_liststore"));
  contacts_treemodel
    = GTK_TREE_MODEL (contacts_liststore);
  b_paste = GTK_WIDGET (GCG_get_main_window_object
                        ("gnunet_conversation_gtk_contacts_paste_button"));

}


/**
 * Shutdown the contact list
 */
void
GCG_CONTACTS_shutdown ()
{
  if (NULL != zone_mon)
  {
    GNUNET_NAMESTORE_zone_monitor_stop (zone_mon);
    zone_mon = NULL;
  }
  gtk_list_store_clear (contacts_liststore);
  contacts_liststore = NULL;
  contacts_treemodel = NULL;
}

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