aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk-download.c
blob: 3f5f213b75209b4f0c96958f09b6138889d2ab12 (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
/*
     This file is part of GNUnet.
     (C) 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-download.c
 * @brief functions for downloading
 * @author Christian Grothoff
 */
#include "gnunet-fs-gtk-download.h"
#include "gnunet-fs-gtk.h"
#include "gnunet-fs-gtk-event_handler.h"

struct dialog_context
{
  GtkBuilder *builder;
  GtkWidget *dialog;
  gint response;
  struct DownloadContext *dc;
};


gboolean
GNUNET_GTK_save_as_dialog_delete_event_cb (GtkWidget *widget, GdkEvent  *event,
    gpointer user_data)
{
  GtkBuilder *builder;
  struct dialog_context *dlc;
  struct DownloadContext *dc;
  enum GNUNET_FS_DownloadOptions opt;
  struct GNUNET_FS_Handle *fs;
  struct DownloadEntry *de;
  GtkWidget *cb;
  uint32_t anonymity;
  uint64_t len;

  builder = GTK_BUILDER (user_data);
  dlc = g_object_get_data (G_OBJECT (widget), "dialog-context");
  if (dlc == NULL)
  {
    g_object_unref (G_OBJECT (builder));
    return FALSE;
  }
  dc = dlc->dc;
  cb = GTK_WIDGET (gtk_builder_get_object
                   (builder, "GNUNET_GTK_save_as_recursive_check_button"));
  if (GTK_RESPONSE_OK != dlc->response)
  {
    g_object_unref (G_OBJECT (dlc->builder));
    if (dc->rr != NULL)
      gtk_tree_row_reference_free (dc->rr);
    GNUNET_free_non_null (dc->mime);
    GNUNET_free_non_null (dc->filename);
    GNUNET_FS_uri_destroy (dc->uri);
    GNUNET_free (dc);
    g_free (dlc);
    return FALSE;
  }
  GNUNET_free_non_null (dc->filename);
  dc->filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dlc->dialog));
  dc->is_recursive =
      (TRUE ==
       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cb))) ? GNUNET_YES :
      GNUNET_NO;
  fs = GNUNET_FS_GTK_get_fs_handle ();
  opt = GNUNET_FS_DOWNLOAD_OPTION_NONE;
  if (dc->is_recursive)
    opt |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE;
  anonymity =
      gtk_spin_button_get_value (GTK_SPIN_BUTTON
                                 (gtk_builder_get_object
                                  (builder,
                                   "GNUNET_GTK_save_as_dialog_anonymity_spin_button")));
  len = GNUNET_FS_uri_chk_get_file_size (dc->uri);
  g_object_unref (G_OBJECT (builder));
  de = GNUNET_malloc (sizeof (struct DownloadEntry));
  de->uri = dc->uri;
  de->meta = dc->meta;
  if (dc->rr != NULL)
  {
    de->rr = dc->rr;
    de->ts = GTK_TREE_STORE (gtk_tree_row_reference_get_model (dc->rr));
  }
  if (dc->sr != NULL)
  {
    GNUNET_break (NULL !=
                  GNUNET_FS_download_start_from_search (fs, dc->sr,
                                                        dc->filename,
                                                        NULL /* tempname */ ,
                                                        0 /* offset */ ,
                                                        len, anonymity, opt,
                                                        de));
  }
  else
  {
    GNUNET_break (NULL !=
                  GNUNET_FS_download_start (fs, dc->uri, NULL /* meta */ ,
                                            dc->filename, NULL /* tempname */ ,
                                            0 /* offset */ ,
                                            len, anonymity, opt, de,
                                            NULL /* parent download ctx */ ));
  }
  GNUNET_free (dc);
  GNUNET_free (dlc);
  return FALSE;
}

void
GNUNET_GTK_save_as_dialog_response_cb (GtkDialog *dialog,
    gint response_id, gpointer user_data)
{
  struct dialog_context *dlc;
  dlc = g_object_get_data (G_OBJECT (dialog), "dialog-context");
  if (dlc != NULL)
    dlc->response = response_id;
  /* dialogs don't get delete-event the way normal windows do,
   * call the handler manually
   */
  GNUNET_GTK_save_as_dialog_delete_event_cb (GTK_WIDGET (dialog), NULL, user_data);
  gtk_widget_destroy (GTK_WIDGET (dialog));
}

void
GNUNET_FS_GTK_open_download_as_dialog (struct DownloadContext *dc)
{
  GtkWidget *ad;
  GtkBuilder *builder;
  GtkWidget *cb;
  struct dialog_context *dlc;

  builder =
      GNUNET_GTK_get_new_builder ("gnunet_fs_gtk_download_as_dialog.glade");
  if (builder == NULL)
  {
    if (dc->rr != NULL)
      gtk_tree_row_reference_free (dc->rr);
    GNUNET_free_non_null (dc->mime);
    GNUNET_free_non_null (dc->filename);
    GNUNET_FS_uri_destroy (dc->uri);
    GNUNET_free (dc);
    return;
  }
  cb = GTK_WIDGET (gtk_builder_get_object
                   (builder, "GNUNET_GTK_save_as_recursive_check_button"));
  if (GNUNET_FS_meta_data_test_for_directory (dc->meta))
    gtk_widget_set_sensitive (cb, TRUE);
  ad = GTK_WIDGET (gtk_builder_get_object
                   (builder, "GNUNET_GTK_save_as_dialog"));
  if (dc->filename != NULL)
  {
    char buf[1024];

    if (NULL != getcwd (buf, sizeof (buf)))
    {
      if (strlen (buf) + strlen (dc->filename) + 2 < sizeof (buf))
      {
        strcat (buf, DIR_SEPARATOR_STR);
        strcat (buf, dc->filename);
      }
      gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (ad), buf);
    }
  }
  dlc = g_new0 (struct dialog_context, 1);
  dlc->builder = builder;
  dlc->dialog = ad;
  dlc->response = 0;
  dlc->dc = dc;
  g_object_set_data (G_OBJECT (ad), "dialog-context", dlc);
  gtk_window_present (GTK_WINDOW (ad));
}

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