aboutsummaryrefslogtreecommitdiff
path: root/src/fs/gnunet-fs-gtk-download.c
blob: dc5b2310ad775c33b155cdf6e597371f7b3c8cca (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
/*
     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.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 DownloadAsDialogContext
{
  GtkBuilder *builder;
  GtkWidget *dialog;
  gint response;
  struct DownloadContext *dc;
};

void
GNUNET_GTK_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);  
}

gboolean
GNUNET_GTK_save_as_dialog_delete_event_cb (GtkWidget * widget, GdkEvent * event,
                                           gpointer user_data)
{
  struct DownloadAsDialogContext *dlc = user_data;
  GtkBuilder *builder;
  struct DownloadContext *dc;
  GtkWidget *cb;

  if (dlc == NULL)
  {
    GNUNET_break (0);
    return FALSE;
  }
  builder = dlc->builder;
  dc = dlc->dc;
  cb = GTK_WIDGET (gtk_builder_get_object
                   (builder, "GNUNET_GTK_save_as_recursive_check_button"));
  if (GTK_RESPONSE_OK != dlc->response)
  {
    GNUNET_GTK_save_as_dialog_free_download_context (dc);
    g_object_unref (G_OBJECT (dlc->builder));
    GNUNET_free (dlc);
    return FALSE;
  }
  GNUNET_free_non_null (dc->filename);
  dc->filename =
      GNUNET_GTK_filechooser_get_filename_utf8 (GTK_FILE_CHOOSER (dlc->dialog));
  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
                                  (builder,
                                   "GNUNET_GTK_save_as_dialog_anonymity_spin_button")));
  g_object_unref (G_OBJECT (builder));

  GNUNET_free (dlc);

  dc->cb (dc);
  return FALSE;
}


void
GNUNET_GTK_save_as_dialog_response_cb (GtkDialog * dialog, gint response_id,
                                       gpointer user_data)
{
  struct DownloadAsDialogContext *dlc = user_data;

  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 DownloadAsDialogContext *dlc;

  dlc = GNUNET_malloc (sizeof (struct DownloadAsDialogContext));
  builder =
    GNUNET_GTK_get_new_builder ("gnunet_fs_gtk_download_as_dialog.glade",
				dlc);
  if (builder == NULL)
  {
    GNUNET_break (0);
    GNUNET_GTK_save_as_dialog_free_download_context (dc);
    GNUNET_free (dlc);
    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);
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (cb), dc->is_recursive);
    
  ad = GTK_WIDGET (gtk_builder_get_object
                   (builder, "GNUNET_GTK_save_as_dialog"));
  if (dc->filename != NULL)
  {
    char *dirname;
    char *basename;
    dirname = GNUNET_strdup (dc->filename);
    basename = (char *) GNUNET_STRINGS_get_short_name (dirname);
    if (basename > dirname)
      basename[-1] = '\0';
    gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (ad), dirname);
    gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (ad), basename);
    GNUNET_free (dirname);
  }
  dlc->builder = builder;
  dlc->dialog = ad;
  dlc->response = 0;
  dlc->dc = dc;
  gtk_window_present (GTK_WINDOW (ad));
}

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