aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk_main-window-search.c
blob: 3032b501e7b00b5b3b832a671ccfeaf2c3d1b64f (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
/*
     This file is part of GNUnet
     (C) 2011, 2012, 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/fs/gnunet-fs-gtk_main-window-search.c
 * @author Christian Grothoff
 * @brief event handlers for the search function in the main window
 */
#include "gnunet-fs-gtk_common.h"
#include "gnunet-fs-gtk.h"
#include "gnunet-fs-gtk_anonymity-widgets.h"


/**
 * How long until we decide a SKS namespace GNS lookup has failed?
 */
#define LOOKUP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)

/**
 * Start a search.
 *
 * @param uri uri to search for, freed in this function
 * @param anonymity_level degree of anonymity to apply for the search
 */
static void
search_for_uri (struct GNUNET_FS_Uri *uri,
		guint anonymity_level)
{
  GNUNET_FS_search_start (GNUNET_FS_GTK_get_fs_handle (), 
			  uri, anonymity_level,
                          GNUNET_FS_SEARCH_OPTION_NONE, NULL);
  GNUNET_FS_uri_destroy (uri);
}


/**
 * Abort the given search lookup.
 *
 * @param sl lookup to abort.
 */
void
abort_search_lookup (struct SearchLookup *sl)
{
  struct GNUNET_GTK_MainWindowContext *main_ctx = GNUNET_FS_GTK_get_main_context ();

  GNUNET_CONTAINER_DLL_remove (main_ctx->sl_head,
			       main_ctx->sl_tail,
			       sl);
  if (GNUNET_SCHEDULER_NO_TASK != sl->timeout_task)
  {
    GNUNET_SCHEDULER_cancel (sl->timeout_task);
    sl->timeout_task = GNUNET_SCHEDULER_NO_TASK;
  }
  if (NULL != sl->gns)
  {
    GNUNET_GNS_lookup_cancel (sl->gns);
    sl->gns = NULL;
  }
  g_free (sl->keywords);
  GNUNET_free (sl);			       
}


/**
 * Task run when the GNS timeout during the resolution of
 * the GNS namespace times out.
 *
 * @param cls the 'struct SearchLookup'
 * @param tc scheduler context
 */
static void
timeout_search_lookup (void *cls,
		       const struct GNUNET_SCHEDULER_TaskContext *tc)
{
  struct SearchLookup *sl = cls;

  sl->timeout_task = GNUNET_SCHEDULER_NO_TASK;
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
	      _("Failed to resolve namespace in time\n"));
  abort_search_lookup (sl);
}


/**
 * Iterator called on obtained result for a GNS lookup for
 * the public key of a namespace identifier.
 *
 * @param cls closure
 * @param rd_count number of records in @a rd
 * @param rd the records in reply
 */
static void
handle_gns_result (void *cls,
		   uint32_t rd_count,
		   const struct GNUNET_NAMESTORE_RecordData *rd)
{
  struct SearchLookup *sl = cls;
  unsigned int i;
  struct GNUNET_FS_Uri *uri;

  sl->gns = NULL;
  for (i=0;i<rd_count;i++)
  {
    if (GNUNET_NAMESTORE_TYPE_PKEY != rd[i].record_type)
      continue;
    if (sizeof (struct GNUNET_CRYPTO_EccPublicKey) != rd[i].data_size)
    {
      GNUNET_break_op (0);
      continue;
    }
    uri = GNUNET_FS_uri_sks_create (rd[i].data, sl->keywords);
    search_for_uri (uri, sl->anonymity_level);
    abort_search_lookup (sl);
    return;      
  }
  GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
	      _("Failed to resolve namespace in time\n"));
  abort_search_lookup (sl);
}


/**
 * Start a search.
 *
 * @param main_ctx the main window context
 */
static void
start_search (struct GNUNET_GTK_MainWindowContext *main_ctx)
{
  guint anonymity_level;
  gchar *keywords;
  gchar *mime_keyword;
  const char *nsid;
  struct GNUNET_FS_Uri *uri;
  struct GNUNET_CRYPTO_EccPublicKey pub_sks_zone;
  struct SearchLookup *sl;
  char *emsg;

  /* get anonymity level */
  if (!GNUNET_GTK_get_selected_anonymity_level
      (main_ctx->builder, "main_window_search_anonymity_combobox", &anonymity_level))
  {
    GNUNET_break (0);
    return;
  }

  /* get selected mime type */
  {
    GtkComboBox *mime_combo;
    GtkTreeModel *mime_model;
    GtkTreeIter iter;

    mime_combo = GTK_COMBO_BOX (GNUNET_FS_GTK_get_main_window_object
				("main_window_search_mime_combobox"));
    mime_model = gtk_combo_box_get_model (mime_combo);
    mime_keyword = NULL;
    if ( (NULL != mime_model) && 
	 gtk_combo_box_get_active_iter (mime_combo, &iter))
      gtk_tree_model_get (mime_model, &iter,
                          GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_MIME_MC_MIME,
                          &mime_keyword,
                          -1);
    if ( (NULL != mime_keyword) &&
	 (0 == strcmp (mime_keyword, " ")) )
    {
      g_free (mime_keyword);
      mime_keyword = NULL;
    }
  }
  {
    GtkComboBox *namespace_box;
    
    namespace_box = GTK_COMBO_BOX (GNUNET_FS_GTK_get_main_window_object ("main_window_search_namespace_combobox"));
    nsid = gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (namespace_box))));
    if ( (NULL != nsid) &&
	 ( (0 == strcasecmp (nsid,
			     "<none>")) ||
	   (0 == strcasecmp (nsid,
			     _("<none>"))) ) )
      nsid = NULL;
  }
   
  /* get keywords and compose keyword string */
  {
    const char *entry_keywords;

    entry_keywords = gtk_entry_get_text (main_ctx->search_entry);  
    if (NULL != mime_keyword)
    {
      keywords = g_strdup_printf ("%s +%s", 
				  entry_keywords, 
				  mime_keyword);
      g_free (mime_keyword);
    }
    else
    {
      keywords = g_strdup (entry_keywords);
    }
  }

  /* build KSK/SKS URI */
  if ( (NULL != nsid) &&
       (0 < strlen (nsid)) )
  {    
    sl = GNUNET_new (struct SearchLookup);
    sl->keywords = keywords;
    sl->anonymity_level = anonymity_level;
    sl->timeout_task = GNUNET_SCHEDULER_add_delayed (LOOKUP_TIMEOUT,
						     &timeout_search_lookup, sl);
    GNUNET_CRYPTO_ecc_key_get_public (main_ctx->sks_zone,
				      &pub_sks_zone);
    sl->gns = GNUNET_GNS_lookup (main_ctx->gns,
				 nsid,
				 &pub_sks_zone,
				 GNUNET_NAMESTORE_TYPE_PKEY,
				 GNUNET_NO,
				 NULL /* no shortening */,
				 &handle_gns_result,
				 sl);
    GNUNET_CONTAINER_DLL_insert (main_ctx->sl_head,
				 main_ctx->sl_tail,
				 sl);
    return;
  }

  emsg = NULL;
  uri = GNUNET_FS_uri_ksk_create (keywords, &emsg);
  if (NULL == uri)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 
		_("Invalid keyword string `%s': %s"),
		keywords, emsg);
    g_free (keywords);
    GNUNET_free_non_null (emsg);
    return;
  }  
  g_free (keywords);
  search_for_uri (uri, anonymity_level);
}


/**
 * User clicked on the 'search' button in the main window.
 *
 * @param button the search button
 * @param user_data the main window context builder
 */
void
main_window_search_button_clicked_cb (GtkButton * button, 
				      gpointer user_data)
{
  struct GNUNET_GTK_MainWindowContext *main_ctx = user_data;

  start_search (main_ctx);
}


/**
 * User pushed a key (possibly ENTER) in the search entry widget.
 * Start the search if it was ENTER.
 *
 * @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
main_window_search_entry_key_press_event_cb (GtkWidget * widget,
                                             GdkEventKey * event,
                                             gpointer user_data)
{
  struct GNUNET_GTK_MainWindowContext *main_ctx = user_data;

  if (event->keyval == GDK_KEY_Return)
  {
    start_search (main_ctx);
    return TRUE;
  }
  return FALSE;
}


/**
 * User clicked on the 'save' button in the search line of the main window.
 * Store the selected namespace in the "sks-fs" zone.
 *
 * @param button the "save" button
 * @param user_data the main window context builder
 */

void
GNUNET_FS_GTK_save_button_clicked_cb (GtkButton * button, 
				      gpointer user_data)
{
  struct GNUNET_GTK_MainWindowContext *main_ctx = user_data;

  GNUNET_break (0);
  (void) main_ctx;
}


/**
 * The user has somehow changed the selectionin the namespace combo box entry.
 * Update the visibility of the "save" button.
 *
 * @param widget the combo box
 * @param user_data the main window context builder (unused)
 */
void
main_window_search_namespace_combobox_changed_cb (GtkComboBox *widget,
						  gpointer user_data)
{
  GtkButton *button;
  const gchar *text;
  struct GNUNET_CRYPTO_EccPublicKey pkey;
  int ret;
  
  text = gtk_entry_get_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (widget))));
  ret = GNUNET_NAMESTORE_zkey_to_pkey (text, &pkey);
  button = GTK_BUTTON (GNUNET_FS_GTK_get_main_window_object
		       ("GNUNET_FS_GTK_save_button"));
  gtk_widget_set_visible (GTK_WIDGET (button),
			  (GNUNET_OK == ret) ? TRUE : FALSE);
  gtk_widget_set_sensitive (GTK_WIDGET (button),
			    (GNUNET_OK == ret) ? TRUE : FALSE);
}


/* end of gnunet-fs-gtk_main-window-search.c */