diff options
Diffstat (limited to 'src/fs/gnunet-fs-gtk_main-window-namespace-dropdown.c')
-rw-r--r-- | src/fs/gnunet-fs-gtk_main-window-namespace-dropdown.c | 467 |
1 files changed, 0 insertions, 467 deletions
diff --git a/src/fs/gnunet-fs-gtk_main-window-namespace-dropdown.c b/src/fs/gnunet-fs-gtk_main-window-namespace-dropdown.c deleted file mode 100644 index 666f58bf..00000000 --- a/src/fs/gnunet-fs-gtk_main-window-namespace-dropdown.c +++ /dev/null | |||
@@ -1,467 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2011-2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 3, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file src/fs/gnunet-fs-gtk_main-window-namespace-dropdown.c | ||
23 | * @author LRN | ||
24 | * @brief event handlers for the namespace selection dropdown box in the main window | ||
25 | */ | ||
26 | #include "gnunet-fs-gtk_common.h" | ||
27 | #include "gnunet-fs-gtk.h" | ||
28 | #include "gnunet-fs-gtk_event-handler.h" | ||
29 | |||
30 | /** | ||
31 | * How long until we automatically hide the drop-down if the cursor is outside the bounds? | ||
32 | */ | ||
33 | #define AUTO_HIDE_TIMEOUT_MS 100 | ||
34 | |||
35 | |||
36 | /** | ||
37 | * ID of a timeout task which we schedule to close the drop-down automatically | ||
38 | * if the mouse leaves the area for a while. 0 for no such task. | ||
39 | * | ||
40 | * FIXME-BUG-MINOR: this task is not cancelled if the main window is closed while | ||
41 | * the drop-down is down. | ||
42 | */ | ||
43 | static guint namespace_selector_window_leave_timeout_source; | ||
44 | |||
45 | |||
46 | /** | ||
47 | * The mouse has re-entered the dropdown window. Stop the | ||
48 | * timeout task that would hide the dropdown. | ||
49 | * | ||
50 | * @param widget the dropdown widget | ||
51 | * @param event the mouse-enter event | ||
52 | * @param user_data the context for the main window | ||
53 | */ | ||
54 | gboolean | ||
55 | GNUNET_FS_GTK_search_namespace_dropdown_button_enter_notify_event_cb (GtkWidget *widget, | ||
56 | GdkEvent *event, | ||
57 | gpointer user_data) | ||
58 | { | ||
59 | if (namespace_selector_window_leave_timeout_source > 0) | ||
60 | g_source_remove (namespace_selector_window_leave_timeout_source); | ||
61 | namespace_selector_window_leave_timeout_source = 0; | ||
62 | return FALSE; | ||
63 | } | ||
64 | |||
65 | |||
66 | /** | ||
67 | * Run when the timeout has expired. Hides the drop down window. | ||
68 | * | ||
69 | * @param user_data the toggle button which we will use to hide the dropdown | ||
70 | * @return FALSE | ||
71 | */ | ||
72 | static gboolean | ||
73 | namespace_selector_window_leave_timeout_cb (gpointer user_data) | ||
74 | { | ||
75 | GtkToggleButton *toggle_button = GTK_TOGGLE_BUTTON (user_data); | ||
76 | |||
77 | /* This will eventually hide the namespace selector */ | ||
78 | namespace_selector_window_leave_timeout_source = 0; | ||
79 | gtk_toggle_button_set_active (toggle_button, FALSE); | ||
80 | return FALSE; | ||
81 | } | ||
82 | |||
83 | |||
84 | /** | ||
85 | * The cursor has left the window. Place a timeout to hide the | ||
86 | * window. It will be cancelled if the cursor re-enters the namespace | ||
87 | * selector window or the toggle button within 100ms | ||
88 | * | ||
89 | * @param user_data the context for the main window | ||
90 | */ | ||
91 | gboolean | ||
92 | GNUNET_FS_GTK_search_namespace_selector_window_leave_notify_event_cb (GtkWidget * widget, | ||
93 | GdkEvent * event, | ||
94 | gpointer user_data) | ||
95 | { | ||
96 | struct GNUNET_GTK_MainWindowContext *main_ctx = user_data; | ||
97 | |||
98 | if (namespace_selector_window_leave_timeout_source > 0) | ||
99 | g_source_remove (namespace_selector_window_leave_timeout_source); | ||
100 | namespace_selector_window_leave_timeout_source | ||
101 | = g_timeout_add (AUTO_HIDE_TIMEOUT_MS, | ||
102 | &namespace_selector_window_leave_timeout_cb, | ||
103 | main_ctx->ns_dropdown_button); | ||
104 | return FALSE; | ||
105 | } | ||
106 | |||
107 | |||
108 | /** | ||
109 | * Given a tree view, find out which row is currently selected. | ||
110 | * | ||
111 | * @param tree a tree view instance | ||
112 | * @return a reference to the currently selected row, or NULL for none | ||
113 | */ | ||
114 | static GtkTreeRowReference * | ||
115 | get_selected_row_from_treeview (GtkTreeView * tree) | ||
116 | { | ||
117 | GtkTreeSelection *sel; | ||
118 | GtkTreeModel *model; | ||
119 | GtkTreePath *path; | ||
120 | GtkTreeIter iter; | ||
121 | GtkTreeRowReference *ref; | ||
122 | |||
123 | sel = gtk_tree_view_get_selection (tree); | ||
124 | if (! gtk_tree_selection_get_selected (sel, &model, &iter)) | ||
125 | return NULL; | ||
126 | path = gtk_tree_model_get_path (model, &iter); | ||
127 | ref = gtk_tree_row_reference_new (model, path); | ||
128 | gtk_tree_path_free (path); | ||
129 | return ref; | ||
130 | } | ||
131 | |||
132 | |||
133 | /** | ||
134 | * Changes were made to the selected entry in the tree view and the | ||
135 | * user clicked to confirm. Hide the drop down and display the | ||
136 | * selected entry as the new namespace label. | ||
137 | * | ||
138 | * @param main_ctx the context for the main window | ||
139 | * @param tv the tree view that was updated | ||
140 | */ | ||
141 | static void | ||
142 | commit_changes (struct GNUNET_GTK_MainWindowContext *main_ctx, | ||
143 | GtkTreeView *tv) | ||
144 | { | ||
145 | GtkTreePath *treepath; | ||
146 | gchar *value; | ||
147 | |||
148 | if (NULL != main_ctx->selected_ns_row) | ||
149 | gtk_tree_row_reference_free (main_ctx->selected_ns_row); | ||
150 | main_ctx->selected_ns_row = get_selected_row_from_treeview (tv); | ||
151 | |||
152 | treepath = gtk_tree_row_reference_get_path (main_ctx->selected_ns_row); | ||
153 | if (GNUNET_GTK_get_tree_string (tv, treepath, | ||
154 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_NAME, | ||
155 | &value)) | ||
156 | { | ||
157 | gtk_label_set_text (main_ctx->search_ns_label, (NULL != value) ? value : ""); | ||
158 | g_free (value); | ||
159 | } | ||
160 | if (GNUNET_GTK_get_tree_string (tv, treepath, | ||
161 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_ROOT, | ||
162 | &value)) | ||
163 | { | ||
164 | gtk_entry_set_text (main_ctx->search_entry, (NULL != value) ? value : ""); | ||
165 | g_free (value); | ||
166 | } | ||
167 | gtk_tree_path_free (treepath); | ||
168 | |||
169 | /* hide the namespace selector */ | ||
170 | gtk_toggle_button_set_active (main_ctx->ns_dropdown_button, FALSE); | ||
171 | } | ||
172 | |||
173 | |||
174 | /** | ||
175 | * User pushed the button in the treeview. Get the selected entry | ||
176 | * and remember it in the "pushed-rowreference" of the widget. | ||
177 | * This way, we can use it when the button is released. | ||
178 | * | ||
179 | * @param widget the tree view widget | ||
180 | * @param event the push event | ||
181 | * @param user_data the context for the main window | ||
182 | * @return FALSE | ||
183 | */ | ||
184 | gboolean | ||
185 | GNUNET_FS_GTK_namespace_selector_treeview_button_press_event_cb (GtkWidget * widget, | ||
186 | GdkEvent * event, | ||
187 | gpointer user_data) | ||
188 | { | ||
189 | struct GNUNET_GTK_MainWindowContext *main_ctx = user_data; | ||
190 | |||
191 | if (NULL != main_ctx->ns_selector_pushed_row) | ||
192 | gtk_tree_row_reference_free (main_ctx->ns_selector_pushed_row); | ||
193 | main_ctx->ns_selector_pushed_row = get_selected_row_from_treeview (GTK_TREE_VIEW (widget)); | ||
194 | return FALSE; | ||
195 | } | ||
196 | |||
197 | |||
198 | /** | ||
199 | * User released the button in the treeview. Get the selected entry | ||
200 | * and update the cursor accordingly, but only if the user pushed the | ||
201 | * button down and released it in the same row. | ||
202 | * | ||
203 | * @param widget the tree view widget | ||
204 | * @param event the release event | ||
205 | * @param user_data the context for the main window | ||
206 | * @return FALSE | ||
207 | */ | ||
208 | gboolean | ||
209 | GNUNET_FS_GTK_namespace_selector_treeview_button_release_event_cb (GtkWidget * widget, | ||
210 | GdkEvent * event, | ||
211 | gpointer user_data) | ||
212 | { | ||
213 | struct GNUNET_GTK_MainWindowContext *main_ctx = user_data; | ||
214 | GtkTreeRowReference *ref; | ||
215 | |||
216 | ref = get_selected_row_from_treeview (GTK_TREE_VIEW (widget)); | ||
217 | if ( (NULL != ref) && (NULL != main_ctx->ns_selector_pushed_row)) | ||
218 | { | ||
219 | GtkTreePath *path_ref; | ||
220 | GtkTreePath *path_old; | ||
221 | |||
222 | path_ref = gtk_tree_row_reference_get_path (ref); | ||
223 | path_old = gtk_tree_row_reference_get_path (main_ctx->ns_selector_pushed_row); | ||
224 | if (0 == gtk_tree_path_compare (path_ref, path_old)) | ||
225 | commit_changes (main_ctx, GTK_TREE_VIEW (widget)); | ||
226 | if (path_ref) | ||
227 | gtk_tree_path_free (path_ref); | ||
228 | if (path_old) | ||
229 | gtk_tree_path_free (path_old); | ||
230 | } | ||
231 | if (NULL != ref) | ||
232 | gtk_tree_row_reference_free (ref); | ||
233 | if (NULL != main_ctx->ns_selector_pushed_row) | ||
234 | gtk_tree_row_reference_free (main_ctx->ns_selector_pushed_row); | ||
235 | main_ctx->ns_selector_pushed_row = NULL; | ||
236 | return FALSE; | ||
237 | } | ||
238 | |||
239 | |||
240 | /** | ||
241 | * The toggle button that changes the visibility of the namespace dropdown | ||
242 | * list was toggled. | ||
243 | * | ||
244 | * @param togglebutton the button that toggles the namespace dropdown list | ||
245 | * @param user_data the contexxt for the main window | ||
246 | */ | ||
247 | void | ||
248 | GNUNET_FS_GTK_search_namespace_dropdown_button_toggled_cb (GtkToggleButton * | ||
249 | togglebutton, | ||
250 | gpointer user_data) | ||
251 | { | ||
252 | struct GNUNET_GTK_MainWindowContext *main_ctx = user_data; | ||
253 | gboolean active; | ||
254 | GtkAllocation togglebutton_allocation; | ||
255 | GdkWindow *main_window_gdk; | ||
256 | gint mwg_x; | ||
257 | gint mwg_y; | ||
258 | gint tgb_x; | ||
259 | gint tgb_y; | ||
260 | gint popup_x; | ||
261 | gint popup_y; | ||
262 | |||
263 | g_object_get (G_OBJECT (togglebutton), "active", &active, NULL); | ||
264 | if (! active) | ||
265 | { | ||
266 | gtk_widget_hide (main_ctx->ns_selector_window); | ||
267 | gtk_widget_grab_focus (GTK_WIDGET (togglebutton)); | ||
268 | return; | ||
269 | } | ||
270 | gtk_widget_get_allocation (GTK_WIDGET (togglebutton), | ||
271 | &togglebutton_allocation); | ||
272 | main_window_gdk = gtk_widget_get_window (GTK_WIDGET (togglebutton)); | ||
273 | gdk_window_get_origin (main_window_gdk, &mwg_x, &mwg_y); | ||
274 | /* show the window below the button */ | ||
275 | tgb_x = mwg_x + togglebutton_allocation.x; | ||
276 | tgb_y = mwg_y + togglebutton_allocation.y; | ||
277 | popup_x = tgb_x; | ||
278 | popup_y = tgb_y + togglebutton_allocation.height; | ||
279 | gtk_window_move (GTK_WINDOW (main_ctx->ns_selector_window), popup_x, popup_y); | ||
280 | gtk_widget_show_all (main_ctx->ns_selector_window); | ||
281 | gtk_widget_grab_focus (GTK_WIDGET (main_ctx->ns_selector_treeview)); | ||
282 | } | ||
283 | |||
284 | |||
285 | /** | ||
286 | * Add pseudonym data to tree store | ||
287 | * | ||
288 | * @param cls closure (the 'GtkListStore') | ||
289 | * @param pseudonym hash code of public key of pseudonym | ||
290 | * @param md meta data known about the pseudonym | ||
291 | * @param rating the local rating of the pseudonym | ||
292 | * @return GNUNET_OK to continue iteration, GNUNET_SYSERR to abort | ||
293 | */ | ||
294 | static int | ||
295 | add_namespace_to_ts (void *cls, | ||
296 | const struct GNUNET_CRYPTO_EccPublicKey *pseudonym, | ||
297 | const char *name, | ||
298 | const char *unique_name, | ||
299 | const struct GNUNET_CONTAINER_MetaData *md, | ||
300 | int rating) | ||
301 | { | ||
302 | GtkTreeStore *ts = cls; | ||
303 | char *root; | ||
304 | char *ns_name; | ||
305 | char *unique_ns_name; | ||
306 | struct GNUNET_CRYPTO_EccPublicKey *nsid; | ||
307 | char *description; | ||
308 | int desc_is_a_dup; | ||
309 | char *uris; | ||
310 | char *emsg; | ||
311 | struct GNUNET_FS_Uri *uri; | ||
312 | GtkTreeIter iter; | ||
313 | |||
314 | if (rating < 0) | ||
315 | return GNUNET_OK; | ||
316 | |||
317 | GNUNET_FS_pseudonym_get_info (GNUNET_FS_GTK_get_configuration (), | ||
318 | pseudonym, NULL, NULL, | ||
319 | &ns_name, NULL); | ||
320 | unique_ns_name = GNUNET_FS_pseudonym_name_uniquify (GNUNET_FS_GTK_get_configuration (), | ||
321 | pseudonym, ns_name, NULL); | ||
322 | GNUNET_free (ns_name); | ||
323 | nsid = GNUNET_new (struct GNUNET_CRYPTO_EccPublicKey); | ||
324 | *nsid = *pseudonym; | ||
325 | root = NULL; | ||
326 | uris = GNUNET_CONTAINER_meta_data_get_by_type (md, EXTRACTOR_METATYPE_URI); | ||
327 | if (uris != NULL) | ||
328 | { | ||
329 | emsg = NULL; | ||
330 | uri = GNUNET_FS_uri_parse (uris, &emsg); | ||
331 | if (uri == NULL) | ||
332 | GNUNET_free (emsg); | ||
333 | root = GNUNET_FS_uri_sks_get_content_id (uri); | ||
334 | GNUNET_FS_uri_destroy (uri); | ||
335 | } | ||
336 | description = GNUNET_FS_GTK_get_description_from_metadata (md, &desc_is_a_dup); | ||
337 | gtk_tree_store_insert_with_values (ts, &iter, NULL, G_MAXINT, | ||
338 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_NAME, | ||
339 | unique_ns_name, | ||
340 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_KEY, | ||
341 | nsid, | ||
342 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_ROOT, | ||
343 | root, | ||
344 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_TOOLTIP, | ||
345 | description, | ||
346 | -1); | ||
347 | GNUNET_free (unique_ns_name); | ||
348 | GNUNET_free_non_null (root); | ||
349 | GNUNET_free (description); | ||
350 | return GNUNET_OK; | ||
351 | } | ||
352 | |||
353 | |||
354 | void | ||
355 | GNUNET_GTK_main_window_refresh_ns_list (struct GNUNET_GTK_MainWindowContext *main_ctx) | ||
356 | { | ||
357 | GtkTreeIter iter; | ||
358 | GtkTreePath *treepath; | ||
359 | GtkTreeModel *model; | ||
360 | struct GNUNET_CRYPTO_EccPublicKey *key; | ||
361 | struct GNUNET_CRYPTO_EccPublicKey *selected_ns_id; | ||
362 | gboolean found; | ||
363 | gchar *value; | ||
364 | |||
365 | key = NULL; | ||
366 | if (NULL != main_ctx->selected_ns_row) | ||
367 | { | ||
368 | treepath = gtk_tree_row_reference_get_path (main_ctx->selected_ns_row); | ||
369 | model = gtk_tree_view_get_model (main_ctx->ns_selector_treeview); | ||
370 | if (gtk_tree_model_get_iter (model, &iter, treepath)) | ||
371 | gtk_tree_model_get (model, &iter, | ||
372 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_KEY, | ||
373 | &key, | ||
374 | -1); | ||
375 | gtk_tree_path_free (treepath); | ||
376 | gtk_tree_row_reference_free (main_ctx->selected_ns_row); | ||
377 | main_ctx->selected_ns_row = NULL; | ||
378 | } | ||
379 | selected_ns_id = NULL; | ||
380 | if (NULL != key) | ||
381 | { | ||
382 | selected_ns_id = GNUNET_new (struct GNUNET_CRYPTO_EccPublicKey); | ||
383 | *selected_ns_id = *key; | ||
384 | } | ||
385 | |||
386 | if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (main_ctx->search_ns_treestore), | ||
387 | &iter)) | ||
388 | { | ||
389 | do | ||
390 | { | ||
391 | gtk_tree_model_get (GTK_TREE_MODEL (main_ctx->search_ns_treestore), &iter, | ||
392 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_KEY, | ||
393 | &key, | ||
394 | -1); | ||
395 | GNUNET_free_non_null (key); | ||
396 | } while (gtk_tree_model_iter_next (GTK_TREE_MODEL (main_ctx->search_ns_treestore), | ||
397 | &iter)); | ||
398 | } | ||
399 | gtk_tree_store_clear (main_ctx->search_ns_treestore); | ||
400 | gtk_tree_store_insert_with_values (main_ctx->search_ns_treestore, &iter, NULL, G_MAXINT, | ||
401 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_NAME, | ||
402 | "Any", | ||
403 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_KEY, | ||
404 | NULL, | ||
405 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_ROOT, | ||
406 | "", | ||
407 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_TOOLTIP, | ||
408 | "Do not search in any particular namespace", | ||
409 | -1); | ||
410 | |||
411 | if (NULL != main_ctx->ns_discovery_handle) | ||
412 | GNUNET_FS_pseudonym_discovery_callback_unregister (main_ctx->ns_discovery_handle); | ||
413 | main_ctx->ns_discovery_handle | ||
414 | = GNUNET_FS_pseudonym_discovery_callback_register (main_ctx->cfg, | ||
415 | &add_namespace_to_ts, | ||
416 | main_ctx->search_ns_treestore); | ||
417 | |||
418 | found = FALSE; | ||
419 | value = NULL; | ||
420 | if (gtk_tree_model_get_iter_first (GTK_TREE_MODEL (main_ctx->search_ns_treestore), | ||
421 | &iter)) | ||
422 | { | ||
423 | while (TRUE) | ||
424 | { | ||
425 | gtk_tree_model_get (GTK_TREE_MODEL (main_ctx->search_ns_treestore), &iter, | ||
426 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_NAME, | ||
427 | &value, | ||
428 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_KEY, | ||
429 | &key, | ||
430 | -1); | ||
431 | if (NULL == selected_ns_id) | ||
432 | found = TRUE; | ||
433 | if ( (key != NULL) && | ||
434 | (0 == memcmp (key, selected_ns_id, sizeof (struct GNUNET_CRYPTO_EccPublicKey))) ) | ||
435 | found = TRUE; | ||
436 | if (found || | ||
437 | (! gtk_tree_model_iter_next (GTK_TREE_MODEL (main_ctx->search_ns_treestore), | ||
438 | &iter))) | ||
439 | break; | ||
440 | g_free (value); | ||
441 | } | ||
442 | } | ||
443 | if ( (!found) && | ||
444 | gtk_tree_model_get_iter_first (GTK_TREE_MODEL (main_ctx->search_ns_treestore), | ||
445 | &iter)) | ||
446 | { | ||
447 | gtk_tree_model_get (GTK_TREE_MODEL (main_ctx->search_ns_treestore), &iter, | ||
448 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_NAME, | ||
449 | &value, | ||
450 | GNUNET_GTK_FS_MAIN_WINDOW_SEARCH_NAMESPACE_MC_KEY, | ||
451 | &key, | ||
452 | -1); | ||
453 | found = TRUE; | ||
454 | } | ||
455 | if (found) | ||
456 | gtk_tree_selection_select_iter (gtk_tree_view_get_selection | ||
457 | (main_ctx->ns_selector_treeview), &iter); | ||
458 | if (value != NULL) | ||
459 | { | ||
460 | gtk_label_set_text (main_ctx->search_ns_label, value); | ||
461 | g_free (value); | ||
462 | } | ||
463 | GNUNET_free_non_null (selected_ns_id); | ||
464 | } | ||
465 | |||
466 | |||
467 | /* end of gnunet-fs-gtk_main-window-namespace-dropdown.c */ | ||