aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk_download-save-as.c
blob: 96dd2f45858f8eb61fdc78aa00c92b7a8a9694ea (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
     This file is part of GNUnet.
     (C) 2010, 2011, 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_download-save-as.c
 * @brief functions for managing the 'save as' dialog 
 * @author Christian Grothoff
 */
#include "gnunet-fs-gtk_download-save-as.h"
#include "gnunet-fs-gtk.h"
#include "gnunet-fs-gtk_event-handler.h"

/**
 * State for a 'save as' dialog of a download.
 */
struct DownloadAsDialogContext
{

  /**
   * Download context for which this dialog determines the
   * filename (and other options).
   */
  struct DownloadContext *dc;

  /**
   * Builder for the dialog.
   */
  GtkBuilder *builder;
  
  /**
   * Main dialog object.
   */
  GtkWidget *dialog;

  /**
   * Final response code from the dialog.
   */
  gint response;
};


/**
 * Free resources associated with the given download context.
 *
 * @param dc context to free
 */
static void
save_as_dialog_free_download_context (struct DownloadContext *dc)
{
  if (NULL != dc->rr)
    gtk_tree_row_reference_free (dc->rr);
  GNUNET_free_non_null (dc->mime);
  GNUNET_free_non_null (dc->filename);
  if (NULL != dc->meta)
    GNUNET_CONTAINER_meta_data_destroy (dc->meta);
  if (NULL != dc->uri)
    GNUNET_FS_uri_destroy (dc->uri);
  GNUNET_free (dc);  
}


/**
 * The 'save_as' dialog is being deleted.  Check which state we're in
 * and either simply clean up or start the download with the
 * respective options (by calling the respective continuation).
 *
 * @param widget the dialog object
 * @param event the deletion event
 * @param user_data the 'structDownloadAsDialogContext' of the dialog
 * @return always FALSE (destroy the window)
 */
gboolean
GNUNET_GTK_save_as_dialog_delete_event_cb (GtkWidget * widget, GdkEvent * event,
                                           gpointer user_data)
{
  struct DownloadAsDialogContext *dlc = user_data;

  save_as_dialog_free_download_context (dlc->dc);
  g_object_unref (G_OBJECT (dlc->builder));
  GNUNET_free (dlc);
  return FALSE;
}


/**
 * The user clicked on the 'save as' button of the dialog.
 * Delete the window and start the download.
 *
 * @param dialog the dialog object
 * @param response_id response_id associated with the button
 * @param user_data the 'structDownloadAsDialogContext' of the dialog
 */
void
GNUNET_GTK_save_as_dialog_response_cb (GtkDialog * dialog, 
				       gint response_id,
                                       gpointer user_data)
{
  struct DownloadAsDialogContext *dlc = user_data;
  struct DownloadContext *dc;
  GtkToggleButton *cb;

  dc = dlc->dc;
  fprintf (stderr,
	   "Response is %u, ok is %u\n",
	   dlc->response,
	   GTK_RESPONSE_OK);
  if (GTK_RESPONSE_OK != dlc->response)
  {
    save_as_dialog_free_download_context (dc);
    gtk_widget_destroy (GTK_WIDGET (dialog));
    g_object_unref (G_OBJECT (dlc->builder));
    GNUNET_free (dlc);
    return;
  }
  GNUNET_free_non_null (dc->filename);
  dc->filename =
      GNUNET_GTK_filechooser_get_filename_utf8 (GTK_FILE_CHOOSER (dlc->dialog));
  cb = GTK_TOGGLE_BUTTON (gtk_builder_get_object
			  (dlc->builder, "GNUNET_GTK_save_as_recursive_check_button"));
  dc->is_recursive =
      (TRUE ==
       gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (cb))) ? GNUNET_YES :
      GNUNET_NO;
  dc->anonymity =
      gtk_spin_button_get_value (GTK_SPIN_BUTTON
                                 (gtk_builder_get_object
                                  (dlc->builder,
                                   "GNUNET_GTK_save_as_dialog_anonymity_spin_button")));
  gtk_widget_destroy (GTK_WIDGET (dialog));
  g_object_unref (G_OBJECT (dlc->builder));
  GNUNET_free (dlc);
  GNUNET_FS_GTK_download_context_start_download (dc);
}


/**
 * Open the 'save as' dialog for a download.  Calls the 'dc->cb'
 * continutation when the dialog is complete.  Will release the 'dc'
 * resources if the dialog is cancelled.
 *
 * @param dc download context for the file/directory 
 */
void
GNUNET_FS_GTK_open_download_as_dialog (struct DownloadContext *dc)
{
  struct DownloadAsDialogContext *dlc;
  GtkWidget *cb;

  dlc = GNUNET_malloc (sizeof (struct DownloadAsDialogContext));
  dlc->dc = dc;
  dlc->builder =
    GNUNET_GTK_get_new_builder ("gnunet_fs_gtk_download_as_dialog.glade",
				dlc);
  if (NULL == dlc->builder)
  {
    GNUNET_break (0);
    save_as_dialog_free_download_context (dc);
    GNUNET_free (dlc);
    return;
  }
  dlc->dialog = GTK_WIDGET (gtk_builder_get_object
			    (dlc->builder, "GNUNET_GTK_save_as_dialog"));

  /* Enable recursive button for directories and
     set recursive 'default' value based on what the 'dc' tells us */
  cb = GTK_WIDGET (gtk_builder_get_object
                   (dlc->builder, "GNUNET_GTK_save_as_recursive_check_button"));
  if (GNUNET_FS_meta_data_test_for_directory (dc->meta))
    gtk_widget_set_sensitive (cb, TRUE);  
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cb),
				dc->is_recursive);   

  /* initialize filename based on filename from 'dc' */
  if (NULL != dc->filename)
  {
    char *dirname;
    char *basename;

    dirname = GNUNET_strdup (dc->filename);
    basename = (char *) GNUNET_STRINGS_get_short_name (dirname);
    /* basename now points into 'dirname'; cut 'dirname' off at the '/' before basename */
    if (basename > dirname)
      basename[-1] = '\0';

    /* FIXME: remove following lines after testing... */
    fprintf (stderr,
	     "Splitting `%s' into `%s' + `%s' for file chooser dialog.\n",
	     dc->filename,
	     dirname,
	     basename);

    gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dlc->dialog), dirname);
    gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dlc->dialog), basename);
    GNUNET_free (dirname);
  }
  gtk_window_present (GTK_WINDOW (dlc->dialog));
}


/**
 * Actually start the download that is specified by the given download
 * context and its options.  Will add a download entry to the
 * respective tree model and trigger a start of the download using the
 * FS-API.
 *
 * @param dc download context of the download to execute
 */
void
GNUNET_FS_GTK_download_context_start_download (struct DownloadContext *dc)
{
  enum GNUNET_FS_DownloadOptions opt;
  struct GNUNET_FS_Handle *fs;
  struct DownloadEntry *de;
  uint64_t len;
  GtkTreeIter iter;
  GtkTreePath *path;

  de = GNUNET_malloc (sizeof (struct DownloadEntry));
  de->uri = dc->uri;
  dc->uri = NULL;
  de->meta = dc->meta;
  dc->meta = NULL;
  if (NULL != dc->rr)
  {
    de->rr = gtk_tree_row_reference_copy (dc->rr);
    de->ts = GTK_TREE_STORE (gtk_tree_row_reference_get_model (dc->rr));
    path = gtk_tree_row_reference_get_path (de->rr);
    if  ( (NULL != path) &&
	  (gtk_tree_model_get_iter (GTK_TREE_MODEL (de->ts), &iter, path)) )
    {
      /* Store filename and anonymity as specified by the user.
       * These will be re-used when this is a directory, and the user
       * downloads its children.
       */
      gtk_tree_store_set (de->ts, &iter, 15, dc->filename, 16, dc->anonymity, -1);
    } 
    else
    {
      /* We could not find the referenced row in the search tab; this is
	 conceivable as the search tab might have been closed by the 
	 user while the 'save as' dialog was open.  In this case, this
	 is equivalent to having no search, so we drop the (now invalid)
	 'sr' reference */
      dc->sr = NULL;
    }
    if (NULL != path)
      gtk_tree_path_free (path);
  }
  fs = GNUNET_FS_GTK_get_fs_handle ();
  opt = GNUNET_FS_DOWNLOAD_OPTION_NONE;
  if (dc->is_recursive)
    opt |= GNUNET_FS_DOWNLOAD_OPTION_RECURSIVE;
  len = GNUNET_FS_uri_chk_get_file_size (de->uri);
  if (NULL != dc->sr)
  {
    GNUNET_break (NULL !=
                  GNUNET_FS_download_start_from_search (fs, dc->sr,
                                                        dc->filename,
                                                        NULL /* tempname */ ,
                                                        0 /* offset */ ,
                                                        len, dc->anonymity, opt,
                                                        de));
  }
  else
  {
    GNUNET_break (NULL !=
                  GNUNET_FS_download_start (fs, de->uri, NULL /* meta */ ,
                                            dc->filename, NULL /* tempname */ ,
                                            0 /* offset */ ,
                                            len, dc->anonymity, opt, de,
                                            NULL /* parent download ctx */ ));
  }
  save_as_dialog_free_download_context (dc);
}


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