aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/fs/download.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/fs/download.c')
-rw-r--r--src/plugins/fs/download.c190
1 files changed, 112 insertions, 78 deletions
diff --git a/src/plugins/fs/download.c b/src/plugins/fs/download.c
index f627dfb1..ab03525a 100644
--- a/src/plugins/fs/download.c
+++ b/src/plugins/fs/download.c
@@ -71,6 +71,7 @@ static int addFilesToDirectory
71 gtk_tree_model_get_iter(GTK_TREE_MODEL(pos->model), 71 gtk_tree_model_get_iter(GTK_TREE_MODEL(pos->model),
72 &iter, 72 &iter,
73 path); 73 path);
74 gtk_tree_path_free(path);
74 for (i=gtk_tree_model_iter_n_children(pos->model, 75 for (i=gtk_tree_model_iter_n_children(pos->model,
75 &iter)-1;i>=0;i--) { 76 &iter)-1;i>=0;i--) {
76 if (TRUE == gtk_tree_model_iter_nth_child(pos->model, 77 if (TRUE == gtk_tree_model_iter_nth_child(pos->model,
@@ -108,11 +109,12 @@ static void initiateDownload(GtkTreeModel * model,
108 struct ECRS_MetaData * meta; 109 struct ECRS_MetaData * meta;
109 char * filename; 110 char * filename;
110 char * pfx; 111 char * pfx;
112 char * lnk;
111 char * fn; 113 char * fn;
112 DownloadList * list; 114 DownloadList * list;
113 GtkTreeIter iiter; 115 GtkTreeIter iiter;
114 GtkWidget * spin; 116 GtkWidget * spin;
115 char * name; 117 const char * name;
116 struct stat sbuf; 118 struct stat sbuf;
117 unsigned int anon; 119 unsigned int anon;
118 120
@@ -127,7 +129,6 @@ static void initiateDownload(GtkTreeModel * model,
127 -1); 129 -1);
128 if (uri == NULL) { 130 if (uri == NULL) {
129 BREAK(); 131 BREAK();
130 FREENONNULL(name);
131 return; 132 return;
132 } 133 }
133 filename = ECRS_uriToString(uri); 134 filename = ECRS_uriToString(uri);
@@ -136,17 +137,16 @@ static void initiateDownload(GtkTreeModel * model,
136 strlen(ECRS_URI_PREFIX) + 137 strlen(ECRS_URI_PREFIX) +
137 strlen(ECRS_FILE_INFIX)) ) { 138 strlen(ECRS_FILE_INFIX)) ) {
138 BREAK(); 139 BREAK();
139 FREENONNULL(name);
140 FREENONNULL(filename); 140 FREENONNULL(filename);
141 return; 141 return;
142 } 142 }
143 if (name == NULL) 143 if (name == NULL)
144 name = STRDUP(_("unnamed")); 144 name = filename;
145 list = MALLOC(sizeof(DownloadList)); 145 list = MALLOC(sizeof(DownloadList));
146 list->next = head; 146 list->next = head;
147 list->rr = NULL; 147 list->rr = NULL;
148 list->model = NULL; 148 list->model = NULL;
149 if (ECRS_isDirectory(meta)) { 149 if (YES == ECRS_isDirectory(meta)) {
150 list->rr = gtk_tree_row_reference_new(model, path); 150 list->rr = gtk_tree_row_reference_new(model, path);
151 list->model = model; 151 list->model = model;
152 } 152 }
@@ -157,53 +157,41 @@ static void initiateDownload(GtkTreeModel * model,
157 " in section '%s' under '%s'.")); 157 " in section '%s' under '%s'."));
158 mkdirp(fn); 158 mkdirp(fn);
159 pfx = MALLOC(strlen(fn) + 2 + 159 pfx = MALLOC(strlen(fn) + 2 +
160 strlen(filename) + strlen(name)); 160 strlen(name));
161 lnk = MALLOC(strlen(fn) + 2 +
162 strlen(filename));
161 strcpy(pfx, fn); 163 strcpy(pfx, fn);
162 strcat(pfx, 164 if (pfx[strlen(pfx)-1] != DIR_SEPARATOR)
163 DIR_SEPARATOR_STR); 165 strcat(pfx,
166 DIR_SEPARATOR_STR);
167 strcpy(lnk, fn);
168 FREE(fn);
169 if (lnk[strlen(lnk)-1] != DIR_SEPARATOR)
170 strcat(lnk,
171 DIR_SEPARATOR_STR);
164 strcat(pfx, 172 strcat(pfx,
165 name); 173 name);
166 while ( (strlen(pfx) > 0) && 174 strcat(lnk,
167 (pfx[strlen(pfx)-1] == DIR_SEPARATOR) ) 175 &filename[strlen(ECRS_URI_PREFIX) +
168 pfx[strlen(pfx)-1] = '\0'; 176 strlen(ECRS_FILE_INFIX)]);
169 if (0 == STAT(pfx, 177 if (0 != STAT(pfx,
170 &sbuf)) { 178 &sbuf)) {
171 GtkWidget * dialog; 179 if (0 != SYMLINK(lnk, pfx))
172 180 LOG_FILE_STRERROR(LOG_ERROR, "symlink", pfx);
173 dialog = gtk_message_dialog_new 181 } else {
174 (NULL, 182 FREE(pfx);
175 GTK_DIALOG_MODAL, 183 pfx = STRDUP(lnk);
176 GTK_MESSAGE_WARNING,
177 GTK_BUTTONS_CLOSE,
178 _("File '%s' exists in '%s',\n"
179 "will store download under its"
180 " GNUnet URI '%s' instead.\n"),
181 name,
182 fn,
183 &filename[strlen(ECRS_URI_PREFIX) +
184 strlen(ECRS_FILE_INFIX)]);
185 gtk_dialog_run(GTK_DIALOG(dialog));
186 gtk_widget_destroy(dialog);
187 strcpy(pfx, fn);
188 strcat(pfx,
189 DIR_SEPARATOR_STR);
190 strcat(pfx,
191 &filename[strlen(ECRS_URI_PREFIX) +
192 strlen(ECRS_FILE_INFIX)]);
193 } 184 }
194 FREE(fn);
195
196
197 list->filename = pfx; 185 list->filename = pfx;
198 head = list; 186 head = list;
199
200 gtk_tree_store_insert(summary, 187 gtk_tree_store_insert(summary,
201 &iiter, 188 &iiter,
202 NULL, 189 NULL,
203 0); 190 0);
204 gtk_tree_store_set(summary, 191 gtk_tree_store_set(summary,
205 &iiter, 192 &iiter,
206 DOWNLOAD_FILENAME, name, 193 DOWNLOAD_FILENAME, lnk,
194 DOWNLOAD_LINKNAME, pfx,
207 DOWNLOAD_SIZE, ECRS_fileSize(uri), 195 DOWNLOAD_SIZE, ECRS_fileSize(uri),
208 DOWNLOAD_PROGRESS, 0, /* progress */ 196 DOWNLOAD_PROGRESS, 0, /* progress */
209 DOWNLOAD_URISTRING, filename, 197 DOWNLOAD_URISTRING, filename,
@@ -212,7 +200,6 @@ static void initiateDownload(GtkTreeModel * model,
212 /* internal: row reference! */ 200 /* internal: row reference! */
213 -1); 201 -1);
214 FREE(filename); 202 FREE(filename);
215 FREE(name);
216 spin = getAnonymityButtonFromTM(model); 203 spin = getAnonymityButtonFromTM(model);
217 if (spin == NULL) { 204 if (spin == NULL) {
218 BREAK(); 205 BREAK();
@@ -220,12 +207,12 @@ static void initiateDownload(GtkTreeModel * model,
220 } else { 207 } else {
221 anon = gtk_spin_button_get_value_as_int 208 anon = gtk_spin_button_get_value_as_int
222 (GTK_SPIN_BUTTON(spin)); 209 (GTK_SPIN_BUTTON(spin));
223 } 210 }
224
225 FSUI_startDownload(ctx, 211 FSUI_startDownload(ctx,
226 anon, 212 anon,
227 uri, 213 uri,
228 pfx); 214 lnk);
215 FREE(lnk);
229} 216}
230 217
231void on_downloadButton_clicked(GtkWidget * treeview, 218void on_downloadButton_clicked(GtkWidget * treeview,
@@ -290,12 +277,17 @@ void displayDownloadUpdate(const struct ECRS_URI * uri,
290void displayDownloadComplete(const struct ECRS_URI * uri, 277void displayDownloadComplete(const struct ECRS_URI * uri,
291 const char * filename) { 278 const char * filename) {
292 char * ren; 279 char * ren;
280 const char * fn;
281 const char * ln;
293 unsigned long long size; 282 unsigned long long size;
294 char * data; 283 char * data;
295 int fd; 284 int fd;
296 struct ECRS_MetaData * meta; 285 struct ECRS_MetaData * meta;
297 DownloadList * pos; 286 DownloadList * pos;
298 287 GtkTreeIter iter;
288 GtkTreePath * path;
289 struct ECRS_URI * u;
290
299 pos = head; 291 pos = head;
300 while (pos != NULL) { 292 while (pos != NULL) {
301 if (ECRS_equalsUri(uri, 293 if (ECRS_equalsUri(uri,
@@ -303,40 +295,80 @@ void displayDownloadComplete(const struct ECRS_URI * uri,
303 break; 295 break;
304 pos = pos->next; 296 pos = pos->next;
305 } 297 }
306 298 if (pos == NULL)
307 ren = ECRS_suggestFilename(filename);
308 if (ren == NULL)
309 return; 299 return;
310 if (0 != strcmp(ren, 300 if ( (pos->rr != NULL) &&
311 filename)) { 301 (gtk_tree_row_reference_valid(pos->rr)) ) {
312 /* fixme: ask user for confirmation? */ 302
313 /* rename(filename, ren); */ 303 /* update directory view (if applicable!) */
314 } 304 if (OK == getFileSize(filename, &size)) {
315 FREE(ren); 305 fd = fileopen(filename, O_RDONLY);
316 /* fixme: also update download model? */ 306 data = MMAP(NULL,
317 /* update directory view (if applicable!) */ 307 size,
318 if ( (pos != NULL) && 308 PROT_READ,
319 (pos->rr != NULL) && 309 MAP_SHARED,
320 (OK == getFileSize(filename, &size)) ) { 310 fd,
321 fd = fileopen(filename, O_RDONLY); 311 0);
322 data = MMAP(NULL, 312 meta = NULL;
323 size, 313 if (data != NULL) {
324 PROT_READ, 314 ECRS_listDirectory(data,
325 MAP_SHARED, 315 size,
326 fd, 316 &meta,
327 0); 317 &addFilesToDirectory,
328 meta = NULL; 318 (void*)uri);
329 if (data != NULL) { 319 MUNMAP(data, size);
330 ECRS_listDirectory(data, 320 }
331 size, 321 CLOSE(fd);
332 &meta, 322 if (meta != NULL)
333 &addFilesToDirectory, 323 ECRS_freeMetaData(meta);
334 (void*)uri);
335 MUNMAP(data, size);
336 } 324 }
337 CLOSE(fd); 325
338 if (meta != NULL) 326 path = gtk_tree_row_reference_get_path(pos->rr);
339 ECRS_freeMetaData(meta); 327 if (gtk_tree_path_get_depth(path) > 1) {
328 gtk_tree_path_free(path);
329 return;
330 }
331 gtk_tree_path_free(path);
332 }
333
334 /* only rename top-level files, not files inside of directories! */
335 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(summary),
336 &iter)) {
337 do {
338 ln = NULL;
339 fn = NULL;
340 gtk_tree_model_get(GTK_TREE_MODEL(summary),
341 &iter,
342 DOWNLOAD_URI, &u,
343 DOWNLOAD_FILENAME, &fn,
344 DOWNLOAD_LINKNAME, &ln,
345 -1);
346 if (ECRS_equalsUri(u, uri)) {
347 ren = ECRS_suggestFilename(ln);
348 if (ren != NULL) {
349 gtk_tree_store_set(summary,
350 &iter,
351 DOWNLOAD_LINKNAME, ren,
352 -1);
353 if (0 == strcmp(fn, ln)) {
354 /* keep the actual data under the URI name
355 and create a symlink */
356 if (0 != RENAME(ren, fn))
357 LOG(LOG_ERROR,
358 _("Renaming of file '%s' to '%s' failed: %s\n"),
359 ren,
360 fn,
361 STRERROR(errno));
362 else
363 if (0 != SYMLINK(fn, ren))
364 LOG_FILE_STRERROR(LOG_ERROR, "symlink", ren);
365 }
366 FREE(ren);
367 }
368 break;
369 }
370 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(summary),
371 &iter));
340 } 372 }
341 373
342} 374}
@@ -440,6 +472,7 @@ static int addDownloadView(void * cls,
440 gtk_tree_store_set(summary, 472 gtk_tree_store_set(summary,
441 &iiter, 473 &iiter,
442 DOWNLOAD_FILENAME, filename, 474 DOWNLOAD_FILENAME, filename,
475 DOWNLOAD_LINKNAME, filename,
443 DOWNLOAD_SIZE, filesize, 476 DOWNLOAD_SIZE, filesize,
444 DOWNLOAD_PROGRESS, progress, 477 DOWNLOAD_PROGRESS, progress,
445 DOWNLOAD_URISTRING, uriname, 478 DOWNLOAD_URISTRING, uriname,
@@ -460,7 +493,8 @@ void fs_download_start() {
460 "activeDownloadsList"); 493 "activeDownloadsList");
461 summary = 494 summary =
462 gtk_tree_store_new(DOWNLOAD_NUM, 495 gtk_tree_store_new(DOWNLOAD_NUM,
463 G_TYPE_STRING, /* name */ 496 G_TYPE_STRING, /* name (URI as string) */
497 G_TYPE_STRING, /* name (user-friendly name) */
464 G_TYPE_UINT64, /* size */ 498 G_TYPE_UINT64, /* size */
465 G_TYPE_INT, /* progress */ 499 G_TYPE_INT, /* progress */
466 G_TYPE_STRING, /* uri */ 500 G_TYPE_STRING, /* uri */
@@ -474,7 +508,7 @@ void fs_download_start() {
474 _("Name"), 508 _("Name"),
475 renderer, 509 renderer,
476 "value", DOWNLOAD_PROGRESS, 510 "value", DOWNLOAD_PROGRESS,
477 "text", DOWNLOAD_FILENAME, 511 "text", DOWNLOAD_LINKNAME,
478 NULL); 512 NULL);
479 renderer = gtk_cell_renderer_text_new(); 513 renderer = gtk_cell_renderer_text_new();
480 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(downloadList), 514 gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(downloadList),