aboutsummaryrefslogtreecommitdiff
path: root/src/gns/gnunet-gns-gtk.c
blob: 4751f24a43cc3974aa905acde6c143daa5bf18b2 (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
/*
     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.c
 * @brief Main function of gnunet-gns-gtk
 * @author Christian Grothoff
 */
#include "gnunet_gtk.h"
#include <gnunet/gnunet_namestore_service.h>

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

/**
 * Should gnunet-gns-gtk start in tray mode?
 */
static int tray_only;

/**
 * Hash of the public key of our zone.
 */
static GNUNET_HashCode zone;

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

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

/**
 * Name of our zone as a string.
 */
static char *zone_as_string;

/**
 * Default directory of zone files as a string.
 */
static char *zonefile_directory;

static GtkWidget *main_window;

/**
 * Get cfg.
 */
static const struct GNUNET_CONFIGURATION_Handle *
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);
}


/**
 * The user edited the preferred name (PSEU) of this namespace.
 * Push the update to the namestore.
 *
 * @param editable the edited widget
 * @param user_data unused
 */
void
GNUNET_GNS_GTK_pseu_entry_changed_cb (GtkEditable *editable,
				      gpointer user_data)
{
  GNUNET_break (0); // FIXME, not implemented
}


/**
 * The user toggled the 'autoshort' option.  Update the configuration.
 *
 * @param checkmenuitem the menu item
 * @param user_data unused
 */
void
GNUNET_GNS_GTK_autoshort_imagemenuitem_toggled_cb (GtkCheckMenuItem *checkmenuitem,
						   gpointer user_data)
{
  GNUNET_break (0); // FIXME, not implemented
}


/**
 * The user selected 'NEW' in the menu.  Open a dialog to enter a filename
 * to create a new zone (for editing).
 *
 * @param checkmenuitem the menu item
 * @param user_data unused
 */
void
GNUNET_GNS_GTK_new_imagemenuitem_activate_cb (GtkMenuItem *menuitem,
					      gpointer user_data)
{
  GNUNET_break (0); // FIXME, not implemented
}


/**
 * Function called from the open-directory dialog upon completion.
 *
 * @param dialog the pseudonym selection dialog
 * @param response_id response code from the dialog
 * @param user_data the builder of the dialog
 */
void
GNUNET_GNS_GTK_zone_open_dialog_response_cb (GtkDialog * dialog,
					     gint response_id,
					     gpointer user_data)
{
  GtkBuilder *builder = user_data;
  char *filename;

  if (GTK_RESPONSE_OK != response_id)
  {
    gtk_widget_destroy (GTK_WIDGET (dialog));
    g_object_unref (G_OBJECT (builder));
    return;
  }
  filename = GNUNET_GTK_filechooser_get_filename_utf8 (GTK_FILE_CHOOSER (dialog));
  gtk_widget_destroy (GTK_WIDGET (dialog));
  g_object_unref (G_OBJECT (builder));

  /* FIXME: move to new zone 'filename' */
  fprintf (stderr, "Got zone `%s'\n", filename);
  GNUNET_free (filename);  
}


/**
 * The user selected 'OPEN' in the menu.  Open a dialog to select
 * a different zonefile (for editing).
 *
 * @param checkmenuitem the menu item
 * @param user_data unused
 */
void
GNUNET_GNS_GTK_open_imagemenuitem_activate_cb (GtkMenuItem *menuitem,
					       gpointer user_data)
{
  GtkWidget *ad;
  GtkBuilder *builder;
  GtkWidget *toplevel;

  builder =
    GNUNET_GTK_get_new_builder ("gnunet_gns_gtk_zone_open.glade", NULL);
  if (NULL == builder)
  {
    GNUNET_break (0);
    return;
  }
  ad = GTK_WIDGET (gtk_builder_get_object
                   (builder, "GNUNET_GNS_GTK_zone_open_filechooserdialog"));
  toplevel = gtk_widget_get_toplevel (menuitem);
  if (GTK_IS_WINDOW (toplevel))
    gtk_window_set_transient_for (GTK_WINDOW (ad), GTK_WINDOW (toplevel));
  gtk_window_present (GTK_WINDOW (ad));
}


/**
 * The user clicked on the 'copy' button.  Copy the full string
 * with the hash of our public key to the clipboard.
 *
 * @param button the button that was clicked
 * @param user_data unused
 */
void
GNUNET_GNS_GTK_public_key_copy_button_clicked_cb (GtkButton *button,
						  gpointer user_data)
{
  GtkClipboard *cb;

  cb = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
  gtk_clipboard_set_text (cb, zone_as_string, -1);
}


/**
 * Task run on shutdown.
 *
 * @param cls unused
 * @param tc scheduler context, unused
 */
static void
shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  if (NULL != ns)
  {
    GNUNET_NAMESTORE_disconnect (ns, GNUNET_NO);
    ns = NULL;
  }
  if (NULL != zone_pkey)
  {
    GNUNET_CRYPTO_rsa_key_free (zone_pkey);
    zone_pkey = NULL;
  }
  if (NULL != zonefile_directory)
  {
    GNUNET_free (zonefile_directory);
    zonefile_directory = NULL;
  }
}


/**
 * Callback invoked if the application is supposed to exit (via menu).
 *
 * @param menuitem the quit menu
 * @param user_data unused
 */
void
GNUNET_GNS_GTK_quit_imagemenuitem_activate_cb (GtkMenuItem *menuitem,
					       gpointer user_data)
{
  GNUNET_GTK_tray_icon_destroy ();
  GNUNET_GTK_main_loop_quit (ml);
  GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
}


/**
 * Callback invoked if the application is supposed to exit (via window-close).
 *
 * @param widget the main window
 * @param event deletion event
 * @param user_data unused
 */
void
GNUNET_GNS_GTK_main_window_delete_event_cb (GtkWidget *widget,
					    GdkEvent *event,
					    gpointer user_data)
{
  GNUNET_GTK_tray_icon_destroy ();
  GNUNET_GTK_main_loop_quit (ml);
  GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
}


/**
 * Actual main function run right after GNUnet's scheduler
 * is initialized.  Initializes up GTK and Glade.
 *
 * @param cls the 'struct GNUNET_GTK_MainLoop'
 * @param tc scheduler context
 */
static void
run (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;

  char *keyfile;
  char *label;

  ml = cls;
  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (get_configuration (),
                                                            "namestore",
                                                            "ZONEFILE_DIRECTORY",
                                                            &zonefile_directory))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                _("Option `%s' missing in section `%s'\n"),
                "ZONEFILE_DIRECTORY", "namestore");
    return;
  }

  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (get_configuration (),
							    "gns",
							    "ZONEKEY",
							    &keyfile))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		_("Option `%s' missing in section `%s'\n"),
		"ZONEKEY", "gns");
    return;
  }
  zone_pkey = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
  GNUNET_free (keyfile);
  keyfile = NULL;
  if (NULL == zone_pkey)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		_("Failed to read or create private zone key\n"));
    return;
  }
  GNUNET_CRYPTO_rsa_key_get_public (zone_pkey,
				    &pub);
  GNUNET_CRYPTO_hash (&pub, sizeof (pub), &zone);

  ns = GNUNET_NAMESTORE_connect (get_configuration ());
  if (NULL == ns)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
		_("Failed to connect to namestore\n"));
    GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
    return;
  }
  if (GNUNET_OK != GNUNET_GTK_main_loop_build_window (ml, NULL))
  {
    GNUNET_break (0);
    GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
    return;
  }
  zone_as_string = GNUNET_strdup (GNUNET_h2s_full (&zone));
  label = g_markup_printf_escaped (_("<b>Editing zone %s</b>"),
				   zone_as_string);
  gtk_label_set_markup (GTK_LABEL (get_object ("GNUNET_GNS_GTK_zone_label")),
			label);
  g_free (label);

  GNUNET_GTK_set_icon_search_path ();
  GNUNET_GTK_setup_nls ();
  /* setup main window */
  main_window = GTK_WIDGET (get_object ("GNUNET_GNS_GTK_main_window"));
  //gtk_window_maximize (GTK_WINDOW (main_window));
  GNUNET_GTK_tray_icon_create (GTK_WINDOW (main_window),
                               "gnunet-gtk" /* FIXME: different icon? */ ,
                               "gnunet-gns-gtk");

  /* make GUI visible */
  if (!tray_only)
  {
    gtk_widget_show (main_window);
    gtk_window_present (GTK_WINDOW (main_window));
  }
}


int
main (int argc, char *const *argv)
{
  static struct GNUNET_GETOPT_CommandLineOption options[] = {
    {'t', "tray", NULL,
     gettext_noop ("start in tray mode"), 0,
     &GNUNET_GETOPT_set_one, &tray_only},
    GNUNET_GETOPT_OPTION_END
  };

  if (GNUNET_OK !=
      GNUNET_GTK_main_loop_start ("gnunet-gns-gtk",
                                  "GTK GUI for editing our zone", argc,
                                  argv, options,
                                  "gnunet_gns_gtk_main_window.glade",
                                  &run))
    return 1;
  return 0;
}


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