diff options
author | Christian Grothoff <christian@grothoff.org> | 2012-02-04 20:13:38 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2012-02-04 20:13:38 +0000 |
commit | 63a781934cfe8a180159bdfc6201dfacfde90704 (patch) | |
tree | 10c39e66f3c81d3718bc88913fa25bdff0882254 | |
parent | 71d91170ac47edf02a2a97776b63286b77a630af (diff) | |
download | gnunet-gtk-63a781934cfe8a180159bdfc6201dfacfde90704.tar.gz gnunet-gtk-63a781934cfe8a180159bdfc6201dfacfde90704.zip |
-create directory if it does not exist to allow nice save-as suggestion; clean it up on abort
-rw-r--r-- | src/fs/gnunet-fs-gtk_download-save-as.c | 61 |
1 files changed, 46 insertions, 15 deletions
diff --git a/src/fs/gnunet-fs-gtk_download-save-as.c b/src/fs/gnunet-fs-gtk_download-save-as.c index 33998313..8e3cbc5f 100644 --- a/src/fs/gnunet-fs-gtk_download-save-as.c +++ b/src/fs/gnunet-fs-gtk_download-save-as.c | |||
@@ -49,10 +49,36 @@ struct DownloadAsDialogContext | |||
49 | */ | 49 | */ |
50 | GtkWidget *dialog; | 50 | GtkWidget *dialog; |
51 | 51 | ||
52 | /** | ||
53 | * Name of the directory that should be unlinked if the operation is aborted, | ||
54 | * can be NULL. | ||
55 | */ | ||
56 | char *dirname; | ||
57 | |||
52 | }; | 58 | }; |
53 | 59 | ||
54 | 60 | ||
55 | /** | 61 | /** |
62 | * Clean up the given DLC context, including possibly removing | ||
63 | * the left-over (empty) directory. | ||
64 | * | ||
65 | * @param dlc context to clean up | ||
66 | */ | ||
67 | static void | ||
68 | clean_up_download_as_context (struct DownloadAsDialogContext *dlc) | ||
69 | { | ||
70 | g_object_unref (G_OBJECT (dlc->builder)); | ||
71 | if (NULL != dlc->dirname) | ||
72 | { | ||
73 | if (0 != RMDIR (dlc->dirname)) | ||
74 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "rmdir", dlc->dirname); | ||
75 | GNUNET_free (dlc->dirname); | ||
76 | } | ||
77 | GNUNET_free (dlc); | ||
78 | } | ||
79 | |||
80 | |||
81 | /** | ||
56 | * Free resources associated with the given download entry. | 82 | * Free resources associated with the given download entry. |
57 | * | 83 | * |
58 | * @param de context to free | 84 | * @param de context to free |
@@ -89,8 +115,7 @@ GNUNET_GTK_save_as_dialog_delete_event_cb (GtkWidget * widget, GdkEvent * event, | |||
89 | struct DownloadAsDialogContext *dlc = user_data; | 115 | struct DownloadAsDialogContext *dlc = user_data; |
90 | 116 | ||
91 | GNUNET_FS_GTK_free_download_entry (dlc->de); | 117 | GNUNET_FS_GTK_free_download_entry (dlc->de); |
92 | g_object_unref (G_OBJECT (dlc->builder)); | 118 | clean_up_download_as_context (dlc); |
93 | GNUNET_free (dlc); | ||
94 | return FALSE; | 119 | return FALSE; |
95 | } | 120 | } |
96 | 121 | ||
@@ -116,9 +141,8 @@ GNUNET_GTK_save_as_dialog_response_cb (GtkDialog * dialog, | |||
116 | if (GTK_RESPONSE_OK != response_id) | 141 | if (GTK_RESPONSE_OK != response_id) |
117 | { | 142 | { |
118 | GNUNET_FS_GTK_free_download_entry (de); | 143 | GNUNET_FS_GTK_free_download_entry (de); |
119 | gtk_widget_destroy (GTK_WIDGET (dialog)); | 144 | gtk_widget_destroy (GTK_WIDGET (dialog)); |
120 | g_object_unref (G_OBJECT (dlc->builder)); | 145 | clean_up_download_as_context (dlc); |
121 | GNUNET_free (dlc); | ||
122 | return; | 146 | return; |
123 | } | 147 | } |
124 | GNUNET_free_non_null (de->filename); | 148 | GNUNET_free_non_null (de->filename); |
@@ -136,8 +160,9 @@ GNUNET_GTK_save_as_dialog_response_cb (GtkDialog * dialog, | |||
136 | (dlc->builder, | 160 | (dlc->builder, |
137 | "GNUNET_GTK_save_as_dialog_anonymity_spin_button"))); | 161 | "GNUNET_GTK_save_as_dialog_anonymity_spin_button"))); |
138 | gtk_widget_destroy (GTK_WIDGET (dialog)); | 162 | gtk_widget_destroy (GTK_WIDGET (dialog)); |
139 | g_object_unref (G_OBJECT (dlc->builder)); | 163 | GNUNET_free_non_null (dlc->dirname); |
140 | GNUNET_free (dlc); | 164 | dlc->dirname = NULL; |
165 | clean_up_download_as_context (dlc); | ||
141 | GNUNET_FS_GTK_download_context_start_download (de); | 166 | GNUNET_FS_GTK_download_context_start_download (de); |
142 | } | 167 | } |
143 | 168 | ||
@@ -184,6 +209,7 @@ GNUNET_FS_GTK_open_download_as_dialog (struct DownloadEntry *de) | |||
184 | { | 209 | { |
185 | char *dirname; | 210 | char *dirname; |
186 | char *basename; | 211 | char *basename; |
212 | struct stat sbuf; | ||
187 | 213 | ||
188 | dirname = GNUNET_strdup (de->filename); | 214 | dirname = GNUNET_strdup (de->filename); |
189 | basename = (char *) GNUNET_STRINGS_get_short_name (dirname); | 215 | basename = (char *) GNUNET_STRINGS_get_short_name (dirname); |
@@ -191,16 +217,21 @@ GNUNET_FS_GTK_open_download_as_dialog (struct DownloadEntry *de) | |||
191 | if (basename > dirname) | 217 | if (basename > dirname) |
192 | basename[-1] = '\0'; | 218 | basename[-1] = '\0'; |
193 | 219 | ||
194 | /* FIXME: remove following lines after testing... */ | 220 | if (0 != STAT (dirname, &sbuf)) |
195 | fprintf (stderr, | 221 | { |
196 | "Splitting `%s' into `%s' + `%s' for file chooser dialog.\n", | 222 | if (GNUNET_OK != GNUNET_DISK_directory_create (dirname)) |
197 | de->filename, | 223 | { |
198 | dirname, | 224 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "mkdir", dirname); |
199 | basename); | 225 | } |
200 | 226 | else | |
227 | { | ||
228 | dlc->dirname = dirname; | ||
229 | } | ||
230 | } | ||
201 | gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dlc->dialog), dirname); | 231 | gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dlc->dialog), dirname); |
202 | gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dlc->dialog), basename); | 232 | gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dlc->dialog), basename); |
203 | GNUNET_free (dirname); | 233 | if (NULL == dlc->dirname) |
234 | GNUNET_free (dirname); | ||
204 | } | 235 | } |
205 | gtk_dialog_set_default_response (GTK_DIALOG (dlc->dialog), | 236 | gtk_dialog_set_default_response (GTK_DIALOG (dlc->dialog), |
206 | GTK_RESPONSE_OK); | 237 | GTK_RESPONSE_OK); |