aboutsummaryrefslogtreecommitdiff
path: root/src/gns/gnunet-gns-gtk_zone.c
blob: bef4e1d01ef6249cfe552d1ac264cf9635400206 (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
/*
     This file is part of GNUnet
     (C) 2012 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 2, 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/gns/gnunet-gns-gtk_zone.c
 * @author Christian Grothoff
 * @brief everything releated to the zone tree view
 */
#include "gnunet_gtk.h"
#include "gnunet-gns-gtk.h"

#define NEW_RECORD_STR "<new record>"

enum
{
  TREE_COL_NAME = 0,
  TREE_COL_IS_PUBLIC,
  TREE_COL_RECORD_TYPE,
  TREE_COL_RECORD_TYPE_AS_STR,
  TREE_COL_EXP_TIME,
  TREE_COL_EXP_TIME_IS_REL,
  TREE_COL_EXP_TIME_AS_STR,
  TREE_COL_VAL_AS_STR,
  TREE_COL_VAL_COLOR,
  TREE_COL_NAME_IS_EDITABLE,
};

enum
{
  TYPE_LIST_TYPE = 0,
  TYPE_LIST_TYPENAME
};

/**
 * The user has selected a new record type.  Update the
 * model, possibly invalidating (marking 'red') the existing
 * value.
 *
 * @param renderer updated renderer
 * @param path the path identifying the edited cell
 * @param new_iter selected cell in the combo's model (with the record type)
 * @param user_data unused
 */
void
GNUNET_GNS_GTK_type_cellrenderercombo_changed_cb (GtkCellRendererCombo *combo,
						  gchar *path,
						  GtkTreeIter *new_iter,
						  gpointer user_data)
{
  struct GNUNET_GNS_Context *gns = user_data;
  GtkTreeIter it;
  GtkTreeModel *tm = GTK_TREE_MODEL(gns->ts);
  guint value;
  char * v_name;

  gtk_tree_model_get(GTK_TREE_MODEL(gns->ls), new_iter, 0, &value, -1);
  gtk_tree_model_get(GTK_TREE_MODEL(gns->ls), new_iter, TYPE_LIST_TYPENAME, &v_name, -1);
  gtk_tree_model_get_iter_from_string(tm, &it, path);
  gtk_tree_store_set(gns->ts, &it,
                     TREE_COL_RECORD_TYPE, value,
                     TREE_COL_RECORD_TYPE_AS_STR, v_name,
                     -1);

  /* check if value is still valid */

}


/**
 * The user has toggled the 'public' checkmark of a cell.  Update the
 * model.
 *
 * @param renderer updated renderer
 * @param path the path identifying the edited cell
 * @param user_data unused
 */
void
GNUNET_GNS_GTK_ispublic_cellrenderertoggle_toggled_cb (GtkCellRendererToggle *cell_renderer,
						       gchar *path,
						       gpointer user_data)
{
  struct GNUNET_GNS_Context *gns = user_data;
  GtkTreeIter it;
  GtkTreeModel *tm = GTK_TREE_MODEL(gns->ts);
  int value;

  gtk_tree_model_get_iter_from_string(tm, &it, path);
  gtk_tree_model_get(tm, &it, TREE_COL_IS_PUBLIC, &value, -1);
  gtk_tree_store_set(gns->ts, &it, TREE_COL_IS_PUBLIC, !value, -1);

  GNUNET_break (0); // FIXME, not implemented
}


/**
 * The user has edited a 'expiration' cell.  Update the model.
 *
 * @param renderer updated renderer
 * @param path the path identifying the edited cell
 * @param new_text the new expiration time
 * @param user_data unused
 */
void
GNUNET_GNS_GTK_expiration_cellrenderertext_edited_cb (GtkCellRendererText *renderer,
						      gchar *path,
						      gchar *new_text,
						      gpointer user_data)
{
  GNUNET_break (0); // FIXME, not implemented
}


/**
 * The user has edited a 'value' cell.  Update the model,
 * including the status on the consistency of the value with
 * the type.
 *
 * @param renderer updated renderer
 * @param path the path identifying the edited cell
 * @param new_text the new value
 * @param user_data unused
 */
void
GNUNET_GNS_GTK_value_cellrenderertext_edited_cb (GtkCellRendererText *renderer,
						 gchar *path,
						 gchar *new_text,
						 gpointer user_data)
{
  struct GNUNET_GNS_Context *gns = user_data;
  GtkTreeModel *tm = GTK_TREE_MODEL(gns->ts);
  GtkTreeIter it;
  size_t data_size;
  void * data;
  int type;

  if (0 != strcmp(new_text,""))
  {
    gtk_tree_model_get_iter_from_string(tm, &it, path);
    gtk_tree_model_get(tm, &it, TREE_COL_RECORD_TYPE, &type, -1);
    if (GNUNET_OK == GNUNET_NAMESTORE_string_to_value (type,
                                      new_text,
                                      &data,
                                      &data_size))
    {

      gtk_tree_store_set (gns->ts, &it, TREE_COL_VAL_COLOR, NULL, -1);
      gtk_tree_store_set (gns->ts, &it, TREE_COL_VAL_AS_STR, new_text, -1);
    }
    else
    {
      gtk_tree_store_set (gns->ts, &it, TREE_COL_VAL_COLOR, "red", -1);
      gtk_tree_store_set (gns->ts, &it, TREE_COL_VAL_AS_STR, new_text, -1);
    }
  }
}


/**
 * The user has edited a 'name' cell.  Update the model (and if needed
 * create another fresh line for additional records).
 *
 * @param renderer updated renderer
 * @param path the path identifying the edited cell
 * @param new_text the new name
 * @param user_data unused
 */
void
GNUNET_GNS_GTK_name_cellrenderertext_edited_cb (GtkCellRendererText *renderer,
						gchar *path,
						gchar *new_text,
						gpointer user_data)
{
  struct GNUNET_GNS_Context *gns = user_data;
  GtkTreeIter it;
  GtkTreeModel *tm = GTK_TREE_MODEL(gns->ts);

  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "New text for `%s' is `%s'\n", path, new_text);
  if ((0 == strcmp (path, "0")) && (0 != strcmp (new_text, NEW_RECORD_STR)) )
  {
    GNUNET_break (0);
    /* update name */
    gtk_tree_model_get_iter_from_string(tm, &it, path);
    gtk_tree_store_set (gns->ts, &it, TREE_COL_NAME, new_text, -1);

    /* add a new dummy line */
    gtk_tree_store_insert_with_values (gns->ts, &it,NULL, 0,
        TREE_COL_NAME, _(NEW_RECORD_STR),
        TREE_COL_RECORD_TYPE, 1,
        TREE_COL_NAME_IS_EDITABLE, 1,
        -1);
#if 0
    gtk_tree_store_append(gns->ts, &it, NULL);
    gtk_tree_store_set(gns->ts, &it,
                       TREE_COL_NAME, new_text,
                       TREE_COL_RECORD_TYPE, 1,
                       -1);
    /* add a new name */
    gtk_tree_store_append(gns->ts, &it, NULL);
    gtk_tree_store_set(gns->ts, &it,
                       TREE_COL_NAME, new_text,
                       TREE_COL_RECORD_TYPE, 1, -1);
#endif
  }
  else
  {
    /* update name */
    gtk_tree_model_get_iter_from_string(tm, &it, path);
    gtk_tree_store_set (gns->ts, &it, TREE_COL_NAME, new_text, -1);
  }
}


/**
 * The zone treeview pop up menu is supposed to be created.
 * (Note: this is not the only method that might need to be
 * written to handle events to create pop up menus; right-clicks
 * might need to be managed separately).
 *
 * @param widget the widget
 * @param user_data unused
 * @return TRUE if a menu was activated
 */
gboolean
GNUNET_GNS_GTK_main_treeview_popup_menu_cb (GtkWidget *widget,
					    gpointer user_data)
{  
  GNUNET_break (0); // FIXME, not implemented
  return FALSE;
}

struct ZoneIteration_Context
{
  struct GNUNET_GNS_Context *gns;
  GNUNET_HashCode zone;
  struct GNUNET_NAMESTORE_ZoneIterator * it;
};


void zone_iteration_proc (void *cls,
                          const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
                          struct GNUNET_TIME_Absolute expire,
                          const char *name,
                          unsigned int rd_count,
                          const struct GNUNET_NAMESTORE_RecordData *rd,
                          const struct GNUNET_CRYPTO_RsaSignature *signature)
{
  struct ZoneIteration_Context * zc_ctx = cls;
  GtkTreeIter iter_name;
  GtkTreeIter iter_record;
  int c;
  int time_is_relative;

  char *exp;
  char *val;
  int public;

  GNUNET_assert (zc_ctx != NULL);
  if ((NULL == zone_key) && (NULL == name))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Zone `%s 'iteration done\n", GNUNET_h2s(&zc_ctx->zone));
    GNUNET_free (zc_ctx);
    return;
  }
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Zone `%s' iteration result `%s', %u records\n",
      GNUNET_h2s(&zc_ctx->zone), name, rd_count);

  GNUNET_assert(GTK_IS_TREE_STORE(zc_ctx->gns->ts));
  gtk_tree_store_append(zc_ctx->gns->ts, &iter_name, NULL);
  /*
  GtkCellRenderer *renderer ;
  g_object_set(renderer,
               "editable", FALSE,
               NULL);
*/
  gtk_tree_store_set(zc_ctx->gns->ts, &iter_name,
                     TREE_COL_NAME, name,
                     TREE_COL_NAME_IS_EDITABLE, 1,
                     -1);


  /* Append elements for records */
  for (c = 0; c < rd_count; c ++)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Record %u: type %u flags %u expiration %llu data_size %u\n",
        c, rd[c].record_type, rd[c].flags, rd[c].expiration, rd[c].data_size);

    /* Set public toggle */
    if (rd[c].flags != GNUNET_NAMESTORE_RF_PRIVATE)
      public = GNUNET_YES;
    else
      public = GNUNET_NO;

    /* Expiration time */
    time_is_relative = GNUNET_NO;

    if (GNUNET_YES == time_is_relative)
    {
      /* FIX THIS WHEN WE HAVE RELATIVE TIME */
      struct GNUNET_TIME_Relative rel_time = GNUNET_TIME_relative_get_zero();
      exp = GNUNET_STRINGS_absolute_time_to_string (GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), rel_time));
    }
    else
      exp = GNUNET_STRINGS_absolute_time_to_string (rd[c].expiration);
    /* value */
    val = GNUNET_NAMESTORE_value_to_string (rd[c].record_type,
                                            rd[c].data,
                                            rd[c].data_size);

    gtk_tree_store_insert_with_values(zc_ctx->gns->ts, &iter_record , &iter_name, 0,
                                       TREE_COL_NAME, "",
                                       TREE_COL_RECORD_TYPE, rd[c].record_type,
                                       TREE_COL_RECORD_TYPE_AS_STR, GNUNET_NAMESTORE_number_to_typename(rd[c].record_type),
                                       TREE_COL_IS_PUBLIC, public,
                                       TREE_COL_EXP_TIME_AS_STR, exp,
                                       TREE_COL_EXP_TIME_IS_REL, time_is_relative,
                                       TREE_COL_VAL_AS_STR, val,
                                       -1);

    GNUNET_free (exp);
  }

  GNUNET_NAMESTORE_zone_iterator_next(zc_ctx->it);
}

/**
 * The zone treeview was realized.   Setup the model.
 *
 * @param widget the widget
 * @param user_data unused
 */
void
GNUNET_GNS_GTK_main_treeview_realize_cb (GtkWidget *widget,
					 gpointer user_data)
{
  struct GNUNET_GNS_Context *gns = user_data;
  struct ZoneIteration_Context *zc_ctx;
  GtkTreeIter toplevel;

  /* Append a top level row and leave it empty */
  gtk_tree_store_insert_with_values(gns->ts, &toplevel, NULL, 0,
                                     TREE_COL_NAME, _(NEW_RECORD_STR),
                                     TREE_COL_RECORD_TYPE, 1,
                                     TREE_COL_NAME_IS_EDITABLE, 1,
                                     -1);

  zc_ctx = GNUNET_malloc (sizeof (struct ZoneIteration_Context));
  zc_ctx->gns = user_data;
  zc_ctx->zone = gns->zone;
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Starting one `%s' iteration\n", GNUNET_h2s(&zc_ctx->zone));

  zc_ctx->it = GNUNET_NAMESTORE_zone_iteration_start(gns->ns, &gns->zone,
      GNUNET_NAMESTORE_RF_NONE,
      GNUNET_NAMESTORE_RF_NONE,
      &zone_iteration_proc,
      zc_ctx);
}


/* end of gnunet-gns-gtk_zone.c */