aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk_open-uri.c
blob: 93a164ccc0082cdcffe8d3cc69773c4493ecf8db (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
/*
     This file is part of GNUnet
     (C) 2005, 2006, 2010, 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/fs/gnunet-fs-gtk_open-uri.c
 * @author Christian Grothoff
 * @brief code for the 'Open URI' dialog.
 *
 * TODO:
 * - automatically populate dialog from clipboard?
 */
#include "gnunet-fs-gtk_common.h"
#include "gnunet-fs-gtk_download-save-as.h"
#include "gnunet-fs-gtk_open-uri.h"
#include "gnunet-fs-gtk.h"
#include <gdk/gdkkeysyms.h>


/**
 * Handle a URI string by running the appropriate action.
 *
 * @param uris string we got
 * @param anonymity_level anonymity level to use
 * @return GNUNET_OK on success, GNUNET_NO if the URI type is not supported, GNUNET_SYSERR if we failed to
 *         parse the URI
 */
int
GNUNET_FS_GTK_handle_uri_string (const char *uris,
				 guint anonymity_level)
{
  struct GNUNET_FS_Uri *uri;
  char *perr;

  uri = GNUNET_FS_uri_parse (uris, &perr);
  if (uri == NULL)
  {
    GNUNET_free (perr);
    return GNUNET_SYSERR;
  }
  GNUNET_FS_GTK_handle_uri (uri, anonymity_level);
  return GNUNET_OK;
}


/**
 * User selected "execute" in the open-URI dialog.
 * 
 * @param button the execute button
 * @param user_data the 'GtkBuilder' of the URI dialog
 */
void
GNUNET_GTK_open_url_dialog_execute_button_clicked_cb (GtkButton * button,
                                                      gpointer user_data)
{
  GtkBuilder *builder = GTK_BUILDER (user_data);
  GtkWidget *dialog;
  GtkTextBuffer *tb;
  GtkTextIter ti_start;
  GtkTextIter ti_end;
  guint anonymity_level;
  char *uris;

  dialog =
      GTK_WIDGET (gtk_builder_get_object
                  (builder, "GNUNET_GTK_open_url_window"));
  tb = GTK_TEXT_BUFFER (gtk_builder_get_object
                        (builder,
                         "GNUNET_GTK_open_url_dialog_url_textview_buffer"));
  gtk_text_buffer_get_iter_at_offset (tb, &ti_start, 0);
  gtk_text_buffer_get_iter_at_offset (tb, &ti_end, -1);

  uris = gtk_text_buffer_get_text (tb, &ti_start, &ti_end, FALSE);
  if (!GNUNET_GTK_get_selected_anonymity_level
      (builder, "main_window_search_anonymity_combobox", &anonymity_level))
  {
    GNUNET_break (0);
    gtk_widget_destroy (dialog);
    g_object_unref (G_OBJECT (builder));  
    return;
  }
  GNUNET_break (GNUNET_OK ==
		GNUNET_FS_GTK_handle_uri_string (uris, anonymity_level));
  g_free (uris); 
  gtk_widget_destroy (dialog);
  g_object_unref (G_OBJECT (builder));  
}


/**
 * User selected "cancel" in the open-URI dialog.
 * 
 * @param button the cancel button
 * @param user_data the 'GtkBuilder' of the URI dialog
 */
void
GNUNET_GTK_open_url_dialog_cancel_button_clicked_cb (GtkButton * button,
                                                     gpointer user_data)
{
  GtkBuilder *builder = GTK_BUILDER (user_data);
  GtkWidget *dialog;

  dialog = GTK_WIDGET (gtk_builder_get_object
		       (builder, 
			"GNUNET_GTK_open_url_window"));
  gtk_widget_destroy (GTK_WIDGET (dialog));
  g_object_unref (G_OBJECT (builder));
}


/**
 * User closed the window of the open-URI dialog.
 * 
 * @param widget the window
 * @param event the deletion event
 * @param user_data the 'GtkBuilder' of the URI dialog
 * @return FALSE (allow destruction)
 */
gboolean
GNUNET_GTK_open_url_window_delete_event_cb (GtkWidget * widget,
                                            GdkEvent * event,
                                            gpointer user_data)
{
  GtkBuilder *builder = GTK_BUILDER (user_data);

  g_object_unref (G_OBJECT (builder));
  return FALSE;
}


/**
 * User pushed a key in the open-URI dialog, check if we currently
 * have valid URI and if the key was 'RETURN', run the action.
 * 
 * @param widget the window
 * @param event the deletion event
 * @param user_data the 'GtkBuilder' of the URI dialog
 */
gboolean
GNUNET_GTK_open_url_dialog_url_textview_key_press_event_cb (GtkWidget * widget,
                                                            GdkEventKey * event,
                                                            gpointer user_data)
{
  GtkBuilder *builder = GTK_BUILDER (user_data);
  GtkWidget *execute;

  if (event->keyval != GDK_KEY_Return)
    return FALSE;
  execute = GTK_WIDGET (gtk_builder_get_object (builder,
						"GNUNET_GTK_open_url_dialog_execute_button"));  
  if (gtk_widget_get_sensitive (execute))
    GNUNET_GTK_open_url_dialog_execute_button_clicked_cb (GTK_BUTTON (execute),
							  user_data);
  return TRUE;
}


/**
 * User edited the URI of the open-URI dialog, check if it is currently
 * a valid URI and update the sensitivity of the 'execute' button accordingly.
 * 
 * @param textbuffer the updated buffer
 * @param user_data the 'GtkBuilder' of the URI dialog
 */
void
GNUNET_GTK_open_url_dialog_url_textview_buffer_changed_cb (GtkTextBuffer *
                                                           textbuffer,
                                                           gpointer user_data)
{
  GtkBuilder *builder = GTK_BUILDER (user_data);
  struct GNUNET_FS_Uri *uri;
  GtkTextBuffer *tb;
  GtkTextIter ti_start;
  GtkTextIter ti_end;
  char *perr;
  char *uris;

  perr = NULL;
  tb = GTK_TEXT_BUFFER (gtk_builder_get_object
                        (builder,
                         "GNUNET_GTK_open_url_dialog_url_textview_buffer"));
  gtk_text_buffer_get_iter_at_offset (tb, &ti_start, 0);
  gtk_text_buffer_get_iter_at_offset (tb, &ti_end, -1);
  uris = gtk_text_buffer_get_text (tb, &ti_start, &ti_end, FALSE);
  if (uris != NULL)
    uri = GNUNET_FS_uri_parse (uris, &perr);
  else
    uri = NULL;
  g_free (uris);
  gtk_widget_set_sensitive (GTK_WIDGET
                            (gtk_builder_get_object
                             (builder,
                              "GNUNET_GTK_open_url_dialog_execute_button")),
                            (uri == NULL) ? FALSE : TRUE);
  if (uri != NULL)
    GNUNET_FS_uri_destroy (uri);
  GNUNET_free_non_null (perr);
}


/**
 * User selected "Open URI" in main window.
 *
 * @param dummy the menu entry
 * @param user_data unused
 */
void
GNUNET_GTK_main_menu_file_download_uri_activate_cb (GtkWidget * dummy,
                                                    gpointer user_data)
{
  GtkBuilder *builder;
  GtkWidget *dialog;
  GtkTextBuffer *tb;
  GtkTextIter ti_start;
  GtkTextIter ti_end;
  GtkWidget *toplevel;
  GtkComboBox *combo;

  builder =
    GNUNET_GTK_get_new_builder ("gnunet_fs_gtk_open_url_dialog.glade", NULL);
  if (NULL == builder)
  {
    GNUNET_break (0);
    return;
  }
  dialog = GTK_WIDGET (gtk_builder_get_object
		       (builder, 
			"GNUNET_GTK_open_url_window"));
  tb = GTK_TEXT_BUFFER (gtk_builder_get_object
                        (builder,
                         "GNUNET_GTK_open_url_dialog_url_textview_buffer"));
  gtk_text_buffer_get_iter_at_offset (tb, &ti_start, 0);
  gtk_text_buffer_get_iter_at_offset (tb, &ti_end, -1);
  gtk_text_buffer_delete (tb, &ti_start, &ti_end);

  /* FIXME-FEATURE: query the clipboard, maybe there's valid URI in there?
   * If so, get it. */
  combo = GTK_COMBO_BOX (gtk_builder_get_object 
			 (builder,
			  "GNUNET_GTK_open_url_dialog_anonymity_combobox"));
  gtk_combo_box_set_model (combo, GNUNET_FS_GTK_get_anonymity_level_list_store ());
  toplevel = gtk_widget_get_toplevel (dummy);
  if (GTK_IS_WINDOW (toplevel))
    gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (toplevel));
  gtk_widget_show (dialog);
}


/* end of gnunet-fs-gtk_open-uri.c */