diff options
Diffstat (limited to 'src/fs/gnunet-fs-gtk_open-uri.c')
-rw-r--r-- | src/fs/gnunet-fs-gtk_open-uri.c | 270 |
1 files changed, 270 insertions, 0 deletions
diff --git a/src/fs/gnunet-fs-gtk_open-uri.c b/src/fs/gnunet-fs-gtk_open-uri.c new file mode 100644 index 00000000..6962c2a5 --- /dev/null +++ b/src/fs/gnunet-fs-gtk_open-uri.c | |||
@@ -0,0 +1,270 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | (C) 2005, 2006, 2010, 2012 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 2, 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_open-uri.c | ||
23 | * @author Christian Grothoff | ||
24 | * @brief code for the 'Open URI' dialog. | ||
25 | * | ||
26 | * TODO: | ||
27 | * - automatically populate dialog from clipboard? | ||
28 | */ | ||
29 | #include "gnunet-fs-gtk_common.h" | ||
30 | #include "gnunet-fs-gtk_download-save-as.h" | ||
31 | #include "gnunet-fs-gtk.h" | ||
32 | #include <gdk/gdkkeysyms.h> | ||
33 | |||
34 | |||
35 | /** | ||
36 | * User selected "execute" in the open-URI dialog. | ||
37 | * | ||
38 | * @param button the execute button | ||
39 | * @param user_data the 'GtkBuilder' of the URI dialog | ||
40 | */ | ||
41 | void | ||
42 | GNUNET_GTK_open_url_dialog_execute_button_clicked_cb (GtkButton * button, | ||
43 | gpointer user_data) | ||
44 | { | ||
45 | GtkBuilder *builder; | ||
46 | GtkWidget *dialog; | ||
47 | GtkTextBuffer *tb; | ||
48 | GtkTextIter ti_start, ti_end; | ||
49 | guint anonymity_level; | ||
50 | char *perr; | ||
51 | char *uris; | ||
52 | struct GNUNET_FS_Uri *uri; | ||
53 | |||
54 | builder = GTK_BUILDER (user_data); | ||
55 | dialog = | ||
56 | GTK_WIDGET (gtk_builder_get_object | ||
57 | (builder, "GNUNET_GTK_open_url_window")); | ||
58 | tb = GTK_TEXT_BUFFER (gtk_builder_get_object | ||
59 | (builder, | ||
60 | "GNUNET_GTK_open_url_dialog_url_textview_buffer")); | ||
61 | gtk_text_buffer_get_iter_at_offset (tb, &ti_start, 0); | ||
62 | gtk_text_buffer_get_iter_at_offset (tb, &ti_end, -1); | ||
63 | |||
64 | uris = gtk_text_buffer_get_text (tb, &ti_start, &ti_end, FALSE); | ||
65 | |||
66 | if (!GNUNET_GTK_get_selected_anonymity_level | ||
67 | (builder, "main_window_search_anonymity_combobox", &anonymity_level)) | ||
68 | { | ||
69 | GNUNET_break (0); | ||
70 | gtk_widget_destroy (dialog); | ||
71 | g_object_unref (G_OBJECT (builder)); | ||
72 | return; | ||
73 | } | ||
74 | uri = GNUNET_FS_uri_parse (uris, &perr); | ||
75 | g_free (uris); | ||
76 | if (uri == NULL) | ||
77 | { | ||
78 | /* Why was "execute" button sensitive!? */ | ||
79 | GNUNET_break (0); | ||
80 | GNUNET_free (perr); | ||
81 | gtk_widget_destroy (dialog); | ||
82 | g_object_unref (G_OBJECT (builder)); | ||
83 | return; | ||
84 | } | ||
85 | if (GNUNET_FS_uri_test_sks (uri) || GNUNET_FS_uri_test_ksk (uri)) | ||
86 | { | ||
87 | GNUNET_break (NULL != | ||
88 | GNUNET_FS_search_start (GNUNET_FS_GTK_get_fs_handle (), uri, | ||
89 | anonymity_level, | ||
90 | GNUNET_FS_SEARCH_OPTION_NONE, NULL)); | ||
91 | GNUNET_FS_uri_destroy (uri); | ||
92 | gtk_widget_destroy (dialog); | ||
93 | g_object_unref (G_OBJECT (builder)); | ||
94 | return; | ||
95 | } | ||
96 | if (GNUNET_FS_uri_test_chk (uri) || GNUNET_FS_uri_test_loc (uri)) | ||
97 | { | ||
98 | struct DownloadContext *dc; | ||
99 | |||
100 | dc = GNUNET_malloc (sizeof (struct DownloadContext)); | ||
101 | dc->uri = uri; | ||
102 | dc->anonymity = anonymity_level; | ||
103 | GNUNET_FS_GTK_open_download_as_dialog (dc); | ||
104 | gtk_widget_destroy (dialog); | ||
105 | g_object_unref (G_OBJECT (builder)); | ||
106 | return; | ||
107 | } | ||
108 | GNUNET_break (0); | ||
109 | GNUNET_FS_uri_destroy (uri); | ||
110 | gtk_widget_destroy (dialog); | ||
111 | g_object_unref (G_OBJECT (builder)); | ||
112 | } | ||
113 | |||
114 | |||
115 | /** | ||
116 | * User selected "cancel" in the open-URI dialog. | ||
117 | * | ||
118 | * @param button the cancel button | ||
119 | * @param user_data the 'GtkBuilder' of the URI dialog | ||
120 | */ | ||
121 | void | ||
122 | GNUNET_GTK_open_url_dialog_cancel_button_clicked_cb (GtkButton * button, | ||
123 | gpointer user_data) | ||
124 | { | ||
125 | GtkBuilder *builder = GTK_BUILDER (user_data); | ||
126 | GtkWidget *dialog; | ||
127 | |||
128 | dialog = GTK_WIDGET (gtk_builder_get_object | ||
129 | (builder, | ||
130 | "GNUNET_GTK_open_url_window")); | ||
131 | gtk_widget_destroy (GTK_WIDGET (dialog)); | ||
132 | g_object_unref (G_OBJECT (builder)); | ||
133 | } | ||
134 | |||
135 | |||
136 | /** | ||
137 | * User closed the window of the open-URI dialog. | ||
138 | * | ||
139 | * @param widget the window | ||
140 | * @param event the deletion event | ||
141 | * @param user_data the 'GtkBuilder' of the URI dialog | ||
142 | * @return TRUE (allow destruction) | ||
143 | */ | ||
144 | gboolean | ||
145 | GNUNET_GTK_open_url_window_delete_event_cb (GtkWidget * widget, | ||
146 | GdkEvent * event, | ||
147 | gpointer user_data) | ||
148 | { | ||
149 | GtkBuilder *builder = GTK_BUILDER (user_data); | ||
150 | |||
151 | g_object_unref (G_OBJECT (builder)); | ||
152 | return TRUE; | ||
153 | } | ||
154 | |||
155 | |||
156 | /** | ||
157 | * User pushed a key in the open-URI dialog, check if we currently | ||
158 | * have valid URI and if the key was 'RETURN', run the action. | ||
159 | * | ||
160 | * @param widget the window | ||
161 | * @param event the deletion event | ||
162 | * @param user_data the 'GtkBuilder' of the URI dialog | ||
163 | */ | ||
164 | gboolean | ||
165 | GNUNET_GTK_open_url_dialog_url_textview_key_press_event_cb (GtkWidget * widget, | ||
166 | GdkEventKey * event, | ||
167 | gpointer user_data) | ||
168 | { | ||
169 | GtkBuilder *builder = GTK_BUILDER (user_data); | ||
170 | GtkWidget *execute; | ||
171 | |||
172 | if (event->keyval != GDK_KEY_Return) | ||
173 | return FALSE; | ||
174 | execute = GTK_WIDGET (gtk_builder_get_object (builder, | ||
175 | "GNUNET_GTK_open_url_dialog_execute_button")); | ||
176 | if (gtk_widget_get_sensitive (execute)) | ||
177 | GNUNET_GTK_open_url_dialog_execute_button_clicked_cb (GTK_BUTTON (execute), | ||
178 | user_data); | ||
179 | return TRUE; | ||
180 | } | ||
181 | |||
182 | |||
183 | /** | ||
184 | * User edited the URI of the open-URI dialog, check if it is currently | ||
185 | * a valid URI and update the sensitivity of the 'execute' button accordingly. | ||
186 | * | ||
187 | * @param textbuffer the updated buffer | ||
188 | * @param user_data the 'GtkBuilder' of the URI dialog | ||
189 | */ | ||
190 | void | ||
191 | GNUNET_GTK_open_url_dialog_url_textview_buffer_changed_cb (GtkTextBuffer * | ||
192 | textbuffer, | ||
193 | gpointer user_data) | ||
194 | { | ||
195 | GtkBuilder *builder = GTK_BUILDER (user_data); | ||
196 | struct GNUNET_FS_Uri *uri; | ||
197 | GtkTextBuffer *tb; | ||
198 | GtkTextIter ti_start; | ||
199 | GtkTextIter ti_end; | ||
200 | char *perr; | ||
201 | char *uris; | ||
202 | |||
203 | perr = NULL; | ||
204 | tb = GTK_TEXT_BUFFER (gtk_builder_get_object | ||
205 | (builder, | ||
206 | "GNUNET_GTK_open_url_dialog_url_textview_buffer")); | ||
207 | gtk_text_buffer_get_iter_at_offset (tb, &ti_start, 0); | ||
208 | gtk_text_buffer_get_iter_at_offset (tb, &ti_end, -1); | ||
209 | uris = gtk_text_buffer_get_text (tb, &ti_start, &ti_end, FALSE); | ||
210 | if (uris != NULL) | ||
211 | uri = GNUNET_FS_uri_parse (uris, &perr); | ||
212 | else | ||
213 | uri = NULL; | ||
214 | g_free (uris); | ||
215 | gtk_widget_set_sensitive (GTK_WIDGET | ||
216 | (gtk_builder_get_object | ||
217 | (builder, | ||
218 | "GNUNET_GTK_open_url_dialog_execute_button")), | ||
219 | (uri == NULL) ? FALSE : TRUE); | ||
220 | if (uri != NULL) | ||
221 | GNUNET_FS_uri_destroy (uri); | ||
222 | GNUNET_free_non_null (perr); | ||
223 | } | ||
224 | |||
225 | |||
226 | /** | ||
227 | * User selected "Open URI" in main window. | ||
228 | * | ||
229 | * @param dummy the menu entry | ||
230 | * @param user_data unused | ||
231 | */ | ||
232 | void | ||
233 | GNUNET_GTK_main_menu_file_download_uri_activate_cb (GtkWidget * dummy, | ||
234 | gpointer user_data) | ||
235 | { | ||
236 | GtkBuilder *builder; | ||
237 | GtkWidget *dialog; | ||
238 | GtkTextBuffer *tb; | ||
239 | GtkTextIter ti_start; | ||
240 | GtkTextIter ti_end; | ||
241 | GtkWidget *toplevel; | ||
242 | |||
243 | builder = | ||
244 | GNUNET_GTK_get_new_builder ("gnunet_fs_gtk_open_url_dialog.glade", NULL); | ||
245 | if (NULL == builder) | ||
246 | { | ||
247 | GNUNET_break (0); | ||
248 | return; | ||
249 | } | ||
250 | dialog = GTK_WIDGET (gtk_builder_get_object | ||
251 | (builder, | ||
252 | "GNUNET_GTK_open_url_window")); | ||
253 | tb = GTK_TEXT_BUFFER (gtk_builder_get_object | ||
254 | (builder, | ||
255 | "GNUNET_GTK_open_url_dialog_url_textview_buffer")); | ||
256 | gtk_text_buffer_get_iter_at_offset (tb, &ti_start, 0); | ||
257 | gtk_text_buffer_get_iter_at_offset (tb, &ti_end, -1); | ||
258 | gtk_text_buffer_delete (tb, &ti_start, &ti_end); | ||
259 | |||
260 | /* FIXME-FEATURE: query the clipboard, maybe there's valid URI in there? | ||
261 | * If so, get it. */ | ||
262 | |||
263 | toplevel = gtk_widget_get_toplevel (dummy); | ||
264 | if (GTK_IS_WINDOW (toplevel)) | ||
265 | gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (toplevel)); | ||
266 | gtk_widget_show (dialog); | ||
267 | } | ||
268 | |||
269 | |||
270 | /* end of gnunet-fs-gtk_open-uri.c */ | ||