diff options
Diffstat (limited to 'src/plugins/fs/download.c')
-rw-r--r-- | src/plugins/fs/download.c | 858 |
1 files changed, 0 insertions, 858 deletions
diff --git a/src/plugins/fs/download.c b/src/plugins/fs/download.c deleted file mode 100644 index f1ba0589..00000000 --- a/src/plugins/fs/download.c +++ /dev/null | |||
@@ -1,858 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | (C) 2005, 2006 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | GNUnet is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published | ||
7 | by the Free Software Foundation; either version 2, or (at your | ||
8 | option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with GNUnet; see the file COPYING. If not, write to the | ||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
18 | Boston, MA 02111-1307, USA. | ||
19 | */ | ||
20 | |||
21 | /** | ||
22 | * @file src/plugins/fs/download.c | ||
23 | * @brief code for downloading with gnunet-gtk | ||
24 | * @author Christian Grothoff | ||
25 | */ | ||
26 | |||
27 | #include "platform.h" | ||
28 | #include "gnunetgtk_common.h" | ||
29 | #include "download.h" | ||
30 | #include "search.h" | ||
31 | #include "fs.h" | ||
32 | #include <extractor.h> | ||
33 | |||
34 | typedef struct DL { | ||
35 | struct DL * next; | ||
36 | struct ECRS_URI * uri; | ||
37 | char * filename; | ||
38 | char * finalName; | ||
39 | GtkTreeRowReference * rr; | ||
40 | GtkTreeModel * model; | ||
41 | } DownloadList; | ||
42 | |||
43 | static DownloadList * head; | ||
44 | |||
45 | static GtkTreeStore * summary; | ||
46 | |||
47 | static struct GE_Context * ectx; | ||
48 | |||
49 | static struct GC_Configuration * cfg; | ||
50 | |||
51 | static int addFilesToDirectory | ||
52 | (const ECRS_FileInfo * fi, | ||
53 | const HashCode512 * key, | ||
54 | int isRoot, | ||
55 | void * closure) { | ||
56 | struct ECRS_URI * uri = closure; | ||
57 | DownloadList * pos; | ||
58 | GtkTreeIter iter; | ||
59 | GtkTreeIter child; | ||
60 | int i; | ||
61 | GtkTreePath * path; | ||
62 | |||
63 | if (isRoot == YES) | ||
64 | return OK; | ||
65 | DEBUG_BEGIN(); | ||
66 | pos = head; | ||
67 | while (pos != NULL) { | ||
68 | if (ECRS_equalsUri(uri, | ||
69 | pos->uri)) | ||
70 | break; | ||
71 | pos = pos->next; | ||
72 | } | ||
73 | if (pos != NULL) { | ||
74 | if (! gtk_tree_row_reference_valid(pos->rr)) | ||
75 | return SYSERR; | ||
76 | path = gtk_tree_row_reference_get_path(pos->rr); | ||
77 | gtk_tree_model_get_iter(GTK_TREE_MODEL(pos->model), | ||
78 | &iter, | ||
79 | path); | ||
80 | gtk_tree_path_free(path); | ||
81 | for (i=gtk_tree_model_iter_n_children(pos->model, | ||
82 | &iter)-1;i>=0;i--) { | ||
83 | if (TRUE == gtk_tree_model_iter_nth_child(pos->model, | ||
84 | &child, | ||
85 | &iter, | ||
86 | i)) { | ||
87 | struct ECRS_URI * uri; | ||
88 | uri = NULL; | ||
89 | gtk_tree_model_get(pos->model, | ||
90 | &child, | ||
91 | SEARCH_URI, &uri, | ||
92 | -1); | ||
93 | if ( (uri != NULL) && | ||
94 | (ECRS_equalsUri(uri, | ||
95 | fi->uri)) ) | ||
96 | return OK; | ||
97 | } | ||
98 | } | ||
99 | gtk_tree_store_append(GTK_TREE_STORE(pos->model), | ||
100 | &child, | ||
101 | &iter); | ||
102 | addEntryToSearchTree(GTK_TREE_STORE(pos->model), | ||
103 | &child, | ||
104 | fi->uri, | ||
105 | fi->meta); | ||
106 | } | ||
107 | DEBUG_END(); | ||
108 | return OK; | ||
109 | } | ||
110 | |||
111 | typedef struct { | ||
112 | struct ECRS_URI * uri; | ||
113 | struct ECRS_MetaData * meta; | ||
114 | const char * name; | ||
115 | const char * mime; | ||
116 | char * final_download_destination; | ||
117 | unsigned int anon; | ||
118 | struct FSUI_SearchList * ret; | ||
119 | } InitiateDownloadCls; | ||
120 | |||
121 | static void * startSearch(void * cls) { | ||
122 | InitiateDownloadCls * idc = cls; | ||
123 | |||
124 | idc->ret = FSUI_startSearch(ctx, | ||
125 | idc->anon, | ||
126 | 1000, /* FIXME: max results */ | ||
127 | 99 * cronYEARS, /* fixme: timeout */ | ||
128 | idc->uri); | ||
129 | return NULL; | ||
130 | } | ||
131 | |||
132 | static void * startDownload(void * cls) { | ||
133 | InitiateDownloadCls * idc = cls; | ||
134 | |||
135 | FSUI_startDownload(ctx, | ||
136 | idc->anon, | ||
137 | NO, /* FIXME: isRecursive */ | ||
138 | idc->uri, | ||
139 | idc->final_download_destination); | ||
140 | return NULL; | ||
141 | } | ||
142 | |||
143 | static void initiateDownload(GtkTreeModel * model, | ||
144 | GtkTreePath * path, | ||
145 | GtkTreeIter * iter, | ||
146 | gpointer unused) { | ||
147 | char * uri_name; | ||
148 | char * final_download_dir; | ||
149 | DownloadList * list; | ||
150 | GtkTreeIter iiter; | ||
151 | GtkWidget * spin; | ||
152 | const char * oname; | ||
153 | const char * cname; | ||
154 | char * dname; | ||
155 | GtkTreePath *dirTreePath; | ||
156 | char *dirPath; | ||
157 | unsigned int dirPathLen; | ||
158 | char * size_h; | ||
159 | unsigned long long size; | ||
160 | InitiateDownloadCls idc; | ||
161 | #ifdef WINDOWS | ||
162 | char *filehash = NULL; | ||
163 | #endif | ||
164 | |||
165 | DEBUG_BEGIN(); | ||
166 | idc.uri = NULL; | ||
167 | idc.meta = NULL; | ||
168 | idc.name = NULL; | ||
169 | idc.mime = NULL; | ||
170 | gtk_tree_model_get(model, | ||
171 | iter, | ||
172 | SEARCH_NAME, &idc.name, | ||
173 | SEARCH_URI, &idc.uri, | ||
174 | SEARCH_META, &idc.meta, | ||
175 | SEARCH_MIME, &idc.mime, | ||
176 | -1); | ||
177 | if (idc.uri == NULL) { | ||
178 | GE_BREAK(ectx, 0); | ||
179 | return; | ||
180 | } | ||
181 | |||
182 | spin = getAnonymityButtonFromTM(model); | ||
183 | if (spin == NULL) { | ||
184 | GE_BREAK(ectx, 0); | ||
185 | idc.anon = 1; | ||
186 | } else { | ||
187 | idc.anon = gtk_spin_button_get_value_as_int | ||
188 | (GTK_SPIN_BUTTON(spin)); | ||
189 | } | ||
190 | |||
191 | if (! ECRS_isFileUri(idc.uri)) { | ||
192 | if (ECRS_isNamespaceUri(idc.uri)) { | ||
193 | /* start namespace search; would probably be better | ||
194 | to add this as a subtree, but for simplicity | ||
195 | we'll just add it as a new tab for now */ | ||
196 | run_with_save_calls(&startSearch, | ||
197 | &idc); | ||
198 | return; | ||
199 | } else { | ||
200 | GE_BREAK(ectx, 0); /* unsupported URI type (i.e. ksk or loc) */ | ||
201 | return; | ||
202 | } | ||
203 | } | ||
204 | |||
205 | uri_name = ECRS_uriToString(idc.uri); | ||
206 | if ( (uri_name == NULL) || | ||
207 | (strlen(uri_name) < | ||
208 | strlen(ECRS_URI_PREFIX) + | ||
209 | strlen(ECRS_FILE_INFIX)) ) { | ||
210 | GE_BREAK(ectx, 0); | ||
211 | FREENONNULL(uri_name); | ||
212 | return; | ||
213 | } | ||
214 | |||
215 | if (idc.name == NULL) { | ||
216 | #ifdef WINDOWS | ||
217 | filehash = STRDUP(uri_name); | ||
218 | filehash[16] = 0; | ||
219 | idc.name = filehash; | ||
220 | #else | ||
221 | idc.name = uri_name; | ||
222 | #endif | ||
223 | } | ||
224 | |||
225 | cname = idc.name; | ||
226 | oname = idc.name; | ||
227 | dname = MALLOC(strlen(idc.name)+1); | ||
228 | dname[0] = '\0'; | ||
229 | while (*idc.name != '\0') { | ||
230 | if ( (*idc.name == DIR_SEPARATOR) && | ||
231 | (idc.name[1] != '\0') ) { | ||
232 | memcpy(dname, oname, idc.name - oname); | ||
233 | dname[idc.name - oname] = '\0'; | ||
234 | cname = &idc.name[1]; | ||
235 | } | ||
236 | idc.name++; | ||
237 | } | ||
238 | if (*cname == '\0') /* name ended in '/' - likely directory */ | ||
239 | cname = oname; | ||
240 | idc.name = cname; | ||
241 | GC_get_configuration_value_filename(cfg, | ||
242 | "FS", | ||
243 | "INCOMINGDIR", | ||
244 | "$HOME/gnunet-downloads/", | ||
245 | &final_download_dir); | ||
246 | if (strlen(dname) > 0) { | ||
247 | char * tmp; | ||
248 | tmp = MALLOC(strlen(final_download_dir) + strlen(dname) + 2); | ||
249 | strcpy(tmp, final_download_dir); | ||
250 | if (tmp[strlen(tmp)] != DIR_SEPARATOR) | ||
251 | strcat(tmp, DIR_SEPARATOR_STR); | ||
252 | if (dname[0] == DIR_SEPARATOR) | ||
253 | strcat(tmp, &dname[1]); | ||
254 | else | ||
255 | strcat(tmp, dname); | ||
256 | FREE(final_download_dir); | ||
257 | final_download_dir = tmp; | ||
258 | } | ||
259 | FREE(dname); | ||
260 | disk_directory_create(ectx, final_download_dir); | ||
261 | |||
262 | |||
263 | /* If file is inside a directory, get the full path */ | ||
264 | dirTreePath = gtk_tree_path_copy(path); | ||
265 | dirPath = MALLOC(1); | ||
266 | dirPath[0] = '\0'; | ||
267 | dirPathLen = 0; | ||
268 | while (gtk_tree_path_get_depth(dirTreePath) > 1) { | ||
269 | const char * dirname; | ||
270 | char * new; | ||
271 | |||
272 | if (! gtk_tree_path_up(dirTreePath)) | ||
273 | break; | ||
274 | |||
275 | if (!gtk_tree_model_get_iter(model, | ||
276 | &iiter, | ||
277 | dirTreePath)) | ||
278 | break; | ||
279 | gtk_tree_model_get(model, | ||
280 | &iiter, | ||
281 | SEARCH_NAME, &dirname, | ||
282 | -1); | ||
283 | dirPathLen = strlen(dirPath) + strlen(dirname) + strlen(DIR_SEPARATOR_STR) + 1; | ||
284 | new = MALLOC(dirPathLen + 1); | ||
285 | strcpy(new, dirname); | ||
286 | if (new[strlen(new)-1] != DIR_SEPARATOR) | ||
287 | strcat(new, DIR_SEPARATOR_STR); | ||
288 | strcat(new, dirPath); | ||
289 | FREE(dirPath); | ||
290 | dirPath = new; | ||
291 | } | ||
292 | gtk_tree_path_free(dirTreePath); | ||
293 | |||
294 | |||
295 | /* construct completed/directory/real-filename */ | ||
296 | idc.final_download_destination = MALLOC(strlen(final_download_dir) + 2 + | ||
297 | strlen(idc.name) + strlen(GNUNET_DIRECTORY_EXT) + | ||
298 | strlen(dirPath)); | ||
299 | strcpy(idc.final_download_destination, final_download_dir); | ||
300 | if (idc.final_download_destination[strlen(idc.final_download_destination)-1] != DIR_SEPARATOR) | ||
301 | strcat(idc.final_download_destination, | ||
302 | DIR_SEPARATOR_STR); | ||
303 | strcat(idc.final_download_destination, dirPath); | ||
304 | disk_directory_create(ectx, | ||
305 | idc.final_download_destination); | ||
306 | strcat(idc.final_download_destination, idc.name); | ||
307 | if ( (idc.final_download_destination[strlen(idc.final_download_destination) - 1] == '/') || | ||
308 | (idc.final_download_destination[strlen(idc.final_download_destination) - 1] == '\\') ) | ||
309 | idc.final_download_destination[strlen(idc.final_download_destination) - 1] = '\0'; | ||
310 | /* append ".gnd" if needed (== directory and .gnd not present) */ | ||
311 | if ( (idc.mime != NULL) && | ||
312 | (0 == strcmp(idc.mime, GNUNET_DIRECTORY_MIME)) && | ||
313 | ( (strlen(idc.final_download_destination) < strlen(GNUNET_DIRECTORY_EXT)) || | ||
314 | (0 != strcmp(&idc.final_download_destination[strlen(idc.final_download_destination) | ||
315 | - strlen(GNUNET_DIRECTORY_EXT)], | ||
316 | GNUNET_DIRECTORY_EXT)) ) ) | ||
317 | strcat(idc.final_download_destination, GNUNET_DIRECTORY_EXT); | ||
318 | |||
319 | /* setup visualization */ | ||
320 | list = MALLOC(sizeof(DownloadList)); | ||
321 | list->next = head; | ||
322 | list->rr = NULL; | ||
323 | list->model = NULL; | ||
324 | if (YES == ECRS_isDirectory(idc.meta)) { | ||
325 | list->rr = gtk_tree_row_reference_new(model, path); | ||
326 | list->model = model; | ||
327 | } | ||
328 | list->uri = ECRS_dupUri(idc.uri); | ||
329 | list->filename = idc.final_download_destination; | ||
330 | list->finalName = MALLOC(strlen(final_download_dir) + strlen(dirPath) + strlen(idc.name) + 2); | ||
331 | strcpy(list->finalName, final_download_dir); | ||
332 | if (final_download_dir[strlen(final_download_dir)-1] != DIR_SEPARATOR) | ||
333 | strcat(list->finalName, DIR_SEPARATOR_STR); | ||
334 | strcat(list->finalName, dirPath); | ||
335 | disk_directory_create(ectx, list->finalName); | ||
336 | strcat(list->finalName, idc.name); | ||
337 | head = list; | ||
338 | size = ECRS_fileSize(idc.uri); | ||
339 | size_h = string_get_fancy_byte_size(size); | ||
340 | gtk_tree_store_insert(summary, | ||
341 | &iiter, | ||
342 | NULL, | ||
343 | 0); | ||
344 | gtk_tree_store_set(summary, | ||
345 | &iiter, | ||
346 | DOWNLOAD_FILENAME, idc.final_download_destination, | ||
347 | DOWNLOAD_SHORTNAME, idc.name, | ||
348 | DOWNLOAD_SIZE, size, | ||
349 | DOWNLOAD_HSIZE, size_h, | ||
350 | DOWNLOAD_PROGRESS, 0, /* progress */ | ||
351 | DOWNLOAD_URISTRING, uri_name, | ||
352 | DOWNLOAD_URI, ECRS_dupUri(idc.uri), | ||
353 | DOWNLOAD_TREEPATH, list->rr, /* internal: row reference! */ | ||
354 | DOWNLOAD_DIRPATH, dirPath, | ||
355 | -1); | ||
356 | FREE(size_h); | ||
357 | FREE(uri_name); | ||
358 | FREE(dirPath); | ||
359 | FREENONNULL(final_download_dir); | ||
360 | |||
361 | addLogEntry(_("Downloading `%s'"), idc.name); | ||
362 | run_with_save_calls(&startDownload, | ||
363 | &idc); | ||
364 | #ifdef WINDOWS | ||
365 | FREENONNULL(filehash); | ||
366 | #endif | ||
367 | |||
368 | DEBUG_END(); | ||
369 | } | ||
370 | |||
371 | void on_downloadButton_clicked_fs(GtkWidget * treeview, | ||
372 | GtkWidget * downloadButton) { | ||
373 | GtkTreeSelection * selection; | ||
374 | |||
375 | selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview)); | ||
376 | gtk_tree_selection_selected_foreach | ||
377 | (selection, | ||
378 | &initiateDownload, | ||
379 | NULL); | ||
380 | } | ||
381 | |||
382 | |||
383 | void on_statusDownloadURIEntry_editing_done_fs(GtkWidget * entry, | ||
384 | GtkWidget * downloadButton) { | ||
385 | InitiateDownloadCls idc; | ||
386 | const char * uris; | ||
387 | char * urid; | ||
388 | GtkWidget * spin; | ||
389 | char * final_download_dir; | ||
390 | const char * dname; | ||
391 | DownloadList * list; | ||
392 | GtkTreeIter iiter; | ||
393 | char * size_h; | ||
394 | |||
395 | uris = gtk_entry_get_text(GTK_ENTRY(entry)); | ||
396 | urid = STRDUP(uris); | ||
397 | gtk_entry_set_text(GTK_ENTRY(entry), | ||
398 | ECRS_URI_PREFIX); | ||
399 | idc.uri = ECRS_stringToUri(ectx, urid); | ||
400 | if (idc.uri == NULL) { | ||
401 | addLogEntry(_("Invalid URI `%s'"), urid); | ||
402 | FREE(urid); | ||
403 | return; | ||
404 | } | ||
405 | if (ECRS_isKeywordUri(idc.uri)) { | ||
406 | addLogEntry(_("Please use the search function for keyword (KSK) URIs!")); | ||
407 | FREE(urid); | ||
408 | ECRS_freeUri(idc.uri); | ||
409 | return; | ||
410 | } else if (ECRS_isLocationUri(idc.uri)) { | ||
411 | addLogEntry(_("Location URIs are not yet supported")); | ||
412 | FREE(urid); | ||
413 | ECRS_freeUri(idc.uri); | ||
414 | return; | ||
415 | } | ||
416 | GC_get_configuration_value_filename(cfg, | ||
417 | "FS", | ||
418 | "INCOMINGDIR", | ||
419 | "$HOME/gnunet-downloads/", | ||
420 | &final_download_dir); | ||
421 | disk_directory_create(ectx, final_download_dir); | ||
422 | dname = &uris[strlen(ECRS_URI_PREFIX) + strlen(ECRS_FILE_INFIX)]; | ||
423 | idc.final_download_destination = MALLOC(strlen(final_download_dir) + strlen(dname) + 2); | ||
424 | strcpy(idc.final_download_destination, final_download_dir); | ||
425 | FREE(final_download_dir); | ||
426 | if (idc.final_download_destination[strlen(idc.final_download_destination)] != DIR_SEPARATOR) | ||
427 | strcat(idc.final_download_destination, DIR_SEPARATOR_STR); | ||
428 | strcat(idc.final_download_destination, dname); | ||
429 | |||
430 | /* setup visualization */ | ||
431 | list = MALLOC(sizeof(DownloadList)); | ||
432 | list->next = head; | ||
433 | list->rr = NULL; | ||
434 | list->model = NULL; | ||
435 | list->uri = idc.uri; | ||
436 | list->filename = idc.final_download_destination; | ||
437 | list->finalName = STRDUP(idc.final_download_destination); | ||
438 | head = list; | ||
439 | size_h = string_get_fancy_byte_size(ECRS_fileSize(idc.uri)); | ||
440 | gtk_tree_store_insert(summary, | ||
441 | &iiter, | ||
442 | NULL, | ||
443 | 0); | ||
444 | gtk_tree_store_set(summary, | ||
445 | &iiter, | ||
446 | DOWNLOAD_FILENAME, idc.final_download_destination, | ||
447 | DOWNLOAD_SHORTNAME, uris, | ||
448 | DOWNLOAD_SIZE, ECRS_fileSize(idc.uri), | ||
449 | DOWNLOAD_HSIZE, size_h, | ||
450 | DOWNLOAD_PROGRESS, 0, /* progress */ | ||
451 | DOWNLOAD_URISTRING, uris, | ||
452 | DOWNLOAD_URI, ECRS_dupUri(idc.uri), | ||
453 | DOWNLOAD_TREEPATH, NULL, /* internal: row reference! */ | ||
454 | DOWNLOAD_DIRPATH, "", | ||
455 | -1); | ||
456 | FREE(size_h); | ||
457 | /* get anonymity level */ | ||
458 | spin = glade_xml_get_widget(getMainXML(), | ||
459 | "fsstatusAnonymitySpin"); | ||
460 | if (spin == NULL) { | ||
461 | GE_BREAK(ectx, 0); | ||
462 | idc.anon = 1; | ||
463 | } else { | ||
464 | idc.anon = gtk_spin_button_get_value_as_int | ||
465 | (GTK_SPIN_BUTTON(spin)); | ||
466 | } | ||
467 | addLogEntry(_("Downloading `%s'"), uris); | ||
468 | run_with_save_calls(&startDownload, | ||
469 | &idc); | ||
470 | FREE(urid); | ||
471 | } | ||
472 | |||
473 | |||
474 | /** | ||
475 | */ | ||
476 | void displayDownloadUpdate(const struct ECRS_URI * uri, | ||
477 | unsigned long long completed, | ||
478 | const char * data, | ||
479 | unsigned int size) { | ||
480 | GtkTreeIter iter; | ||
481 | unsigned int val; | ||
482 | unsigned long long total; | ||
483 | struct ECRS_URI * u; | ||
484 | struct ECRS_MetaData * meta; | ||
485 | |||
486 | DEBUG_BEGIN(); | ||
487 | if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(summary), | ||
488 | &iter)) { | ||
489 | do { | ||
490 | gtk_tree_model_get(GTK_TREE_MODEL(summary), | ||
491 | &iter, | ||
492 | DOWNLOAD_SIZE, &total, | ||
493 | DOWNLOAD_URI, &u, | ||
494 | -1); | ||
495 | if (u == NULL) | ||
496 | return; | ||
497 | if (ECRS_equalsUri(u, uri)) { | ||
498 | if (total != 0) | ||
499 | val = completed * 100 / total; | ||
500 | else | ||
501 | val = 100; | ||
502 | gtk_tree_store_set(summary, | ||
503 | &iter, | ||
504 | DOWNLOAD_PROGRESS, val, | ||
505 | -1); | ||
506 | break; | ||
507 | } | ||
508 | } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(summary), | ||
509 | &iter)); | ||
510 | } | ||
511 | meta = NULL; | ||
512 | ECRS_listDirectory(ectx, | ||
513 | data, | ||
514 | size, | ||
515 | &meta, | ||
516 | &addFilesToDirectory, | ||
517 | (void*)uri); | ||
518 | if (meta != NULL) | ||
519 | ECRS_freeMetaData(meta); | ||
520 | DEBUG_END(); | ||
521 | } | ||
522 | |||
523 | /** | ||
524 | */ | ||
525 | void displayDownloadComplete(const struct ECRS_URI * uri, | ||
526 | const char * filename) { | ||
527 | unsigned long long size; | ||
528 | char * data; | ||
529 | int fd; | ||
530 | struct ECRS_MetaData * meta; | ||
531 | DownloadList * pos; | ||
532 | |||
533 | DEBUG_BEGIN(); | ||
534 | GE_LOG(ectx, | ||
535 | GE_STATUS | GE_USER | GE_BULK, | ||
536 | _("Download '%s' complete.\n"), | ||
537 | filename); | ||
538 | pos = head; | ||
539 | while (pos != NULL) { | ||
540 | if (ECRS_equalsUri(uri, | ||
541 | pos->uri)) | ||
542 | break; | ||
543 | pos = pos->next; | ||
544 | } | ||
545 | |||
546 | /* Not available for resumed downloads */ | ||
547 | if (pos != NULL) { | ||
548 | if ( (pos->rr != NULL) && | ||
549 | (gtk_tree_row_reference_valid(pos->rr)) ) { | ||
550 | /* update directory view (if applicable!) */ | ||
551 | if (OK == disk_file_size(ectx, | ||
552 | filename, | ||
553 | &size, | ||
554 | YES)) { | ||
555 | GE_LOG(ectx, | ||
556 | GE_DEBUG, | ||
557 | "Updating directory view of '%s'\n", | ||
558 | filename); | ||
559 | |||
560 | meta = NULL; | ||
561 | fd = disk_file_open(ectx, | ||
562 | filename, | ||
563 | O_RDONLY); | ||
564 | if (fd != -1) { | ||
565 | data = MMAP(NULL, | ||
566 | size, | ||
567 | PROT_READ, | ||
568 | MAP_SHARED, | ||
569 | fd, | ||
570 | 0); | ||
571 | if (data == MAP_FAILED) { | ||
572 | GE_LOG_STRERROR_FILE(ectx, | ||
573 | GE_ERROR | GE_ADMIN | GE_BULK, | ||
574 | "mmap", | ||
575 | filename); | ||
576 | } else { | ||
577 | if (data != NULL) { | ||
578 | ECRS_listDirectory(ectx, | ||
579 | data, | ||
580 | size, | ||
581 | &meta, | ||
582 | &addFilesToDirectory, | ||
583 | (void*)uri); | ||
584 | MUNMAP(data, size); | ||
585 | } | ||
586 | } | ||
587 | CLOSE(fd); | ||
588 | } | ||
589 | if (meta != NULL) | ||
590 | ECRS_freeMetaData(meta); | ||
591 | } | ||
592 | } | ||
593 | } | ||
594 | DEBUG_END(); | ||
595 | } | ||
596 | |||
597 | static int delDownloadView(void * cls, | ||
598 | const struct FSUI_DownloadList * pos, | ||
599 | const char * filename, | ||
600 | const struct ECRS_URI * uri, | ||
601 | unsigned long long filesize, | ||
602 | unsigned long long bytesCompleted, | ||
603 | int isRecursive, | ||
604 | unsigned int anonymityLevel) { | ||
605 | GtkTreeIter iter; | ||
606 | char * f; | ||
607 | char * fn; | ||
608 | struct ECRS_URI * u; | ||
609 | |||
610 | DEBUG_BEGIN(); | ||
611 | if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(summary), | ||
612 | &iter)) { | ||
613 | do { | ||
614 | gtk_tree_model_get(GTK_TREE_MODEL(summary), | ||
615 | &iter, | ||
616 | DOWNLOAD_FILENAME, &f, | ||
617 | DOWNLOAD_URI, &u, | ||
618 | -1); | ||
619 | |||
620 | f = strrchr(f, DIR_SEPARATOR); | ||
621 | fn = strrchr(filename, DIR_SEPARATOR); | ||
622 | |||
623 | if ( (ECRS_equalsUri(u, uri)) && | ||
624 | (0 == strcmp(f, fn)) ) { | ||
625 | gtk_tree_store_remove(summary, | ||
626 | &iter); | ||
627 | break; | ||
628 | } | ||
629 | } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(summary), | ||
630 | &iter)); | ||
631 | } | ||
632 | DEBUG_END(); | ||
633 | return OK; | ||
634 | } | ||
635 | |||
636 | static void * clearCompletedDownloads(void * unused) { | ||
637 | /* FIXME */ | ||
638 | return NULL; | ||
639 | } | ||
640 | |||
641 | void on_clearCompletedDownloadsButton_clicked_fs(void * unused, | ||
642 | GtkWidget * clearButton) { | ||
643 | run_with_save_calls(&clearCompletedDownloads, | ||
644 | NULL); | ||
645 | } | ||
646 | |||
647 | static void * shutdownCode(void * c) { | ||
648 | struct FSUI_DownloadList * pos = c; | ||
649 | FSUI_stopDownload(ctx, | ||
650 | pos); | ||
651 | return NULL; | ||
652 | } | ||
653 | |||
654 | static void abortDownloadCallback(GtkTreeModel * model, | ||
655 | GtkTreePath * path, | ||
656 | GtkTreeIter * iter, | ||
657 | GtkTreeStore * tree) { | ||
658 | void * c; | ||
659 | struct ECRS_URI * u; | ||
660 | |||
661 | GE_ASSERT(ectx, model == GTK_TREE_MODEL(summary)); | ||
662 | gtk_tree_model_get(model, | ||
663 | iter, | ||
664 | DOWNLOAD_POS, &c, | ||
665 | DOWNLOAD_URI, &u, | ||
666 | -1); | ||
667 | run_with_save_calls(&shutdownCode, | ||
668 | c); | ||
669 | gtk_tree_store_remove(summary, | ||
670 | iter); | ||
671 | if (u != NULL) | ||
672 | ECRS_freeUri(u); | ||
673 | } | ||
674 | |||
675 | void on_abortDownloadButton_clicked_fs(void * unused, | ||
676 | GtkWidget * clearButton) { | ||
677 | GtkTreeSelection * selection; | ||
678 | GtkWidget * downloadList; | ||
679 | |||
680 | DEBUG_BEGIN(); | ||
681 | downloadList = glade_xml_get_widget(getMainXML(), | ||
682 | "activeDownloadsList"); | ||
683 | selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(downloadList)); | ||
684 | gtk_tree_selection_selected_foreach | ||
685 | (selection, | ||
686 | (GtkTreeSelectionForeachFunc) &abortDownloadCallback, | ||
687 | NULL); | ||
688 | DEBUG_END(); | ||
689 | } | ||
690 | |||
691 | int addDownloadView(struct FSUI_DownloadList * pos, | ||
692 | struct FSUI_DownloadList * ppos, | ||
693 | const char * filename, | ||
694 | const struct ECRS_URI * uri, | ||
695 | unsigned long long filesize, | ||
696 | unsigned long long bytesCompleted, | ||
697 | int isRecursive, | ||
698 | unsigned int anonymityLevel) { | ||
699 | GtkTreeIter iiter; | ||
700 | int progress; | ||
701 | char * uriname; | ||
702 | const char * sname; | ||
703 | char * size_h; | ||
704 | |||
705 | DEBUG_BEGIN(); | ||
706 | if (filesize != 0) | ||
707 | progress = bytesCompleted * 100 / filesize; | ||
708 | else | ||
709 | progress = 100; | ||
710 | uriname = ECRS_uriToString(uri); | ||
711 | gtk_tree_store_insert(summary, | ||
712 | &iiter, | ||
713 | NULL, | ||
714 | 0); | ||
715 | sname = &filename[strlen(filename)-1]; | ||
716 | while ( (sname > filename) && | ||
717 | (sname[-1] != '/') && | ||
718 | (sname[-1] != '\\') ) | ||
719 | sname--; | ||
720 | size_h = string_get_fancy_byte_size(filesize); | ||
721 | gtk_tree_store_set(summary, | ||
722 | &iiter, | ||
723 | DOWNLOAD_FILENAME, filename, | ||
724 | DOWNLOAD_SHORTNAME, sname, | ||
725 | DOWNLOAD_SIZE, filesize, | ||
726 | DOWNLOAD_HSIZE, size_h, | ||
727 | DOWNLOAD_PROGRESS, progress, | ||
728 | DOWNLOAD_URISTRING, uriname, | ||
729 | DOWNLOAD_URI, ECRS_dupUri(uri), | ||
730 | DOWNLOAD_TREEPATH, NULL, | ||
731 | -1); | ||
732 | FREE(size_h); | ||
733 | FREE(uriname); | ||
734 | DEBUG_END(); | ||
735 | return OK; | ||
736 | } | ||
737 | |||
738 | |||
739 | void fs_download_start(struct GE_Context * e, | ||
740 | struct GC_Configuration * c) { | ||
741 | GtkWidget * downloadList; | ||
742 | GtkCellRenderer * renderer; | ||
743 | GtkTreeViewColumn * column; | ||
744 | int col; | ||
745 | |||
746 | ectx = e; | ||
747 | cfg = c; | ||
748 | DEBUG_BEGIN(); | ||
749 | downloadList = glade_xml_get_widget(getMainXML(), | ||
750 | "activeDownloadsList"); | ||
751 | summary = | ||
752 | gtk_tree_store_new(DOWNLOAD_NUM, | ||
753 | G_TYPE_STRING, /* name (full-path file name) */ | ||
754 | G_TYPE_STRING, /* name (user-friendly name) */ | ||
755 | G_TYPE_UINT64, /* size */ | ||
756 | G_TYPE_STRING, /* human readable size */ | ||
757 | G_TYPE_INT, /* progress */ | ||
758 | G_TYPE_STRING, /* uri */ | ||
759 | G_TYPE_POINTER, /* url */ | ||
760 | G_TYPE_POINTER, /* internal: gtk tree path / NULL */ | ||
761 | G_TYPE_STRING); /* directory path if file is inside a dir */ | ||
762 | gtk_tree_view_set_model(GTK_TREE_VIEW(downloadList), | ||
763 | GTK_TREE_MODEL(summary)); | ||
764 | renderer = gtk_cell_renderer_progress_new(); | ||
765 | col = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(downloadList), | ||
766 | -1, | ||
767 | _("Name"), | ||
768 | renderer, | ||
769 | "value", DOWNLOAD_PROGRESS, | ||
770 | "text", DOWNLOAD_SHORTNAME, | ||
771 | NULL); | ||
772 | column = gtk_tree_view_get_column(GTK_TREE_VIEW(downloadList), | ||
773 | col - 1); | ||
774 | gtk_tree_view_column_set_resizable(column, TRUE); | ||
775 | gtk_tree_view_column_set_clickable(column, TRUE); | ||
776 | gtk_tree_view_column_set_reorderable(column, TRUE); | ||
777 | gtk_tree_view_column_set_sort_column_id(column, DOWNLOAD_PROGRESS); | ||
778 | /*gtk_tree_view_column_set_sort_indicator(column, TRUE);*/ | ||
779 | gtk_tree_view_column_set_resizable(gtk_tree_view_get_column(GTK_TREE_VIEW(downloadList), | ||
780 | col - 1), | ||
781 | TRUE); | ||
782 | renderer = gtk_cell_renderer_text_new(); | ||
783 | g_object_set (renderer, "xalign", 1.00, NULL); | ||
784 | col = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(downloadList), | ||
785 | -1, | ||
786 | _("Size"), | ||
787 | renderer, | ||
788 | "text", DOWNLOAD_HSIZE, | ||
789 | NULL); | ||
790 | |||
791 | column = gtk_tree_view_get_column(GTK_TREE_VIEW(downloadList), | ||
792 | col - 1); | ||
793 | gtk_tree_view_column_set_resizable(column, TRUE); | ||
794 | gtk_tree_view_column_set_clickable(column, TRUE); | ||
795 | gtk_tree_view_column_set_reorderable(column, TRUE); | ||
796 | gtk_tree_view_column_set_sort_column_id(column, DOWNLOAD_SIZE); | ||
797 | /*gtk_tree_view_column_set_sort_indicator(column, TRUE);*/ | ||
798 | gtk_tree_view_column_set_resizable(gtk_tree_view_get_column(GTK_TREE_VIEW(downloadList), | ||
799 | col - 1), | ||
800 | TRUE); | ||
801 | renderer = gtk_cell_renderer_text_new(); | ||
802 | col = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(downloadList), | ||
803 | -1, | ||
804 | _("URI"), | ||
805 | renderer, | ||
806 | "text", DOWNLOAD_URISTRING, | ||
807 | NULL); | ||
808 | column = gtk_tree_view_get_column(GTK_TREE_VIEW(downloadList), | ||
809 | col - 1); | ||
810 | gtk_tree_view_column_set_resizable(column, TRUE); | ||
811 | gtk_tree_view_column_set_reorderable(column, TRUE); | ||
812 | /*gtk_tree_view_column_set_sort_indicator(column, TRUE);*/ | ||
813 | gtk_tree_view_column_set_resizable(gtk_tree_view_get_column(GTK_TREE_VIEW(downloadList), | ||
814 | col - 1), | ||
815 | TRUE); | ||
816 | DEBUG_END(); | ||
817 | } | ||
818 | |||
819 | |||
820 | void fs_download_stop() { | ||
821 | GtkTreeIter iter; | ||
822 | struct ECRS_URI * u; | ||
823 | DownloadList * pos; | ||
824 | char *dirPath; | ||
825 | |||
826 | DEBUG_BEGIN(); | ||
827 | /* free URIs in summary model */ | ||
828 | if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(summary), | ||
829 | &iter)) | ||
830 | return; | ||
831 | do { | ||
832 | gtk_tree_model_get(GTK_TREE_MODEL(summary), | ||
833 | &iter, | ||
834 | DOWNLOAD_URI, &u, | ||
835 | DOWNLOAD_DIRPATH, &dirPath, | ||
836 | -1); | ||
837 | gtk_tree_store_set(summary, | ||
838 | &iter, | ||
839 | DOWNLOAD_URI, NULL, | ||
840 | -1); | ||
841 | if (u != NULL) | ||
842 | ECRS_freeUri(u); | ||
843 | FREENONNULL(dirPath); | ||
844 | } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(summary), | ||
845 | &iter)); | ||
846 | while (head != NULL) { | ||
847 | pos = head->next; | ||
848 | ECRS_freeUri(head->uri); | ||
849 | FREE(head->filename); | ||
850 | gtk_tree_row_reference_free(head->rr); | ||
851 | FREE(head); | ||
852 | head = pos; | ||
853 | } | ||
854 | DEBUG_END(); | ||
855 | } | ||
856 | |||
857 | |||
858 | /* end of download.c */ | ||