aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk-main_window_file_download.c
blob: 4f79329f7ed7211f86d2b2badca5129c626c968a (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
/*
     This file is part of GNUnet
     (C) 2005, 2006, 2010 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-main_window_file_download.c
 * @author Christian Grothoff
 */
#include "gnunet-fs-gtk-common.h"
#include "gnunet-fs-gtk-download.h"
#include "gnunet-fs-gtk.h"
#include <gdk/gdkkeysyms.h>

/**
 * User selected "Open URI" in main window.
 */
void
GNUNET_GTK_main_menu_file_download_uri_activate_cb (GtkWidget * dummy,
                                                    gpointer user_data)
{
  GtkBuilder *builder;
  GtkWidget *dialog;
  GtkTextBuffer *tb;
  GtkTextIter ti_start, ti_end;

  builder = GTK_BUILDER (user_data);

  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);

  /* TODO: queue the clipboard, maybe there's valid URI in there?
   * If so, get it. */

  gtk_widget_show (dialog);
}

void
GNUNET_GTK_open_url_dialog_execute_button_clicked_cb (GtkButton *button,
    gpointer user_data)
{
  GtkBuilder *builder;
  GtkWidget *dialog;
  GtkTextBuffer *tb;
  GtkTextIter ti_start, ti_end;
  guint anonymity_level;
  char *perr;
  char *uris;
  struct GNUNET_FS_Uri *uri;

  builder = GTK_BUILDER (user_data);

  dialog = GTK_WIDGET (gtk_builder_get_object (builder, "GNUNET_GTK_open_url_window"));

  gtk_widget_hide (dialog);

  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))
    return;

  uri = GNUNET_FS_uri_parse (uris, &perr);
  g_free (uris);
  if (uri == NULL)
  {
    GNUNET_free (perr);
    /* Why was "execute" button sensitive!? */
    GNUNET_break (0);
    return;
  }
  if (GNUNET_FS_uri_test_sks (uri) || GNUNET_FS_uri_test_ksk (uri))
  {
    fprintf (stderr, "Starting search!\n");
    GNUNET_break (NULL !=
                  GNUNET_FS_search_start (GNUNET_FS_GTK_get_fs_handle (), uri,
                                          anonymity_level,
                                          GNUNET_FS_SEARCH_OPTION_NONE, NULL));
    GNUNET_FS_uri_destroy (uri);
    return;
  }
  if (GNUNET_FS_uri_test_chk (uri) || GNUNET_FS_uri_test_loc (uri))
  {
    struct DownloadContext *dc;
    dc = GNUNET_malloc (sizeof (struct DownloadContext));
    dc->uri = uri;
    dc->anonymity = anonymity_level;
    GNUNET_FS_GTK_open_download_as_dialog (dc);
    return;
  }
  GNUNET_break (0);
  GNUNET_FS_uri_destroy (uri);
}

void
GNUNET_GTK_open_url_dialog_cancel_button_clicked_cb (GtkButton *button,
    gpointer user_data)
{
  GtkBuilder *builder;
  builder = GTK_BUILDER (user_data);

  gtk_widget_hide (GTK_WIDGET (gtk_builder_get_object (builder, "GNUNET_GTK_open_url_window")));
  return;
}

#ifndef GDK_KEY_Return
#define GDK_KEY_Return GDK_Return
#endif

gboolean
GNUNET_GTK_open_url_dialog_url_textview_key_press_event_cb (GtkWidget *widget,
    GdkEventKey *event, gpointer user_data)
{
  GtkBuilder *builder;
  builder = GTK_BUILDER (user_data);

  if (event->keyval == GDK_KEY_Return)
  {
    GtkWidget *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;
  }
  return FALSE;
}

void
GNUNET_GTK_open_url_dialog_url_textview_buffer_changed_cb (
    GtkTextBuffer *textbuffer, gpointer user_data)
{
  struct GNUNET_FS_Uri *uri;
  GtkTextBuffer *tb;
  GtkTextIter ti_start, ti_end;
  GtkBuilder *builder;
  char *perr;
  char *uris;

  builder = GTK_BUILDER (user_data);

  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);
  else
    GNUNET_free_non_null (perr);
}

/* end of gnunet-fs-gtk-main_window_file_download.c */