aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/fs/download.c
blob: dec1160211284b246e2b74e86b8eb38e4f1e031e (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
/*
     This file is part of GNUnet.
     (C) 2005, 2006 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/plugins/fs/download.c
 * @brief code for downloading with gnunet-gtk
 * @author Christian Grothoff
 */

#include "platform.h"
#include "gnunetgtk_common.h"
#include "download.h"
#include "search.h"
#include "fs.h"
#include <extractor.h>

typedef struct DL {
  struct DL * next;
  struct ECRS_URI * uri;
  char * filename;
  GtkTreeRowReference * rr;
  GtkTreeModel * model;
} DownloadList;

static DownloadList * head;

static GtkTreeStore * summary;

static int addFilesToDirectory
  (const ECRS_FileInfo * fi,
   const HashCode512 * key,
   int isRoot,
   void * closure) {
  struct ECRS_URI * uri = closure;
  DownloadList * pos;

  if (isRoot == YES)
    return OK;
  FSUI_trackURI(fi);
  pos = head;
  while (pos != NULL) {
    if (ECRS_equalsUri(uri,
                       pos->uri))
      break;
    pos = pos->next;
  }
  if (pos == NULL) {
    BREAK(); /* odd */
  } else {
    GtkTreeIter iter;
    GtkTreeIter child;
    int i;
    GtkTreePath * path;

    if (! gtk_tree_row_reference_valid(pos->rr))
      return SYSERR;
    path = gtk_tree_row_reference_get_path(pos->rr);
    gtk_tree_model_get_iter(GTK_TREE_MODEL(pos->model),
                            &iter,
                            path);
    gtk_tree_path_free(path);
    for (i=gtk_tree_model_iter_n_children(pos->model,
                                          &iter)-1;i>=0;i--) {
      if (TRUE == gtk_tree_model_iter_nth_child(pos->model,
                                                &child,
                                                &iter,
                                                i)) {
        struct ECRS_URI * uri;
        uri = NULL;
        gtk_tree_model_get(pos->model,
                           &child,
                           SEARCH_URI, &uri,
                           -1);
        if ( (uri != NULL) &&
             (ECRS_equalsUri(uri,
                             fi->uri)) )
          return OK;
      }
    }
    gtk_tree_store_append(GTK_TREE_STORE(pos->model),
                          &child,
                          &iter);
    addEntryToSearchTree(GTK_TREE_STORE(pos->model),
                         &child,
                         fi->uri,
                         fi->meta);
  }
  return OK;
}

static void initiateDownload(GtkTreeModel * model,
                             GtkTreePath * path,
                             GtkTreeIter * iter,
                             gpointer unused) {
  struct ECRS_URI * uri;
  struct ECRS_MetaData * meta;
  char * filename;
  char * pfx;
  char * lnk;
  char * fn;
  char * inc;
  DownloadList * list;
  GtkTreeIter iiter;
  GtkWidget * spin;
  const char * name;
  const char * mime;
  struct stat sbuf;
  unsigned int anon;
  GtkTreePath *dirTreePath;
  char *dirPath;
  unsigned int dirPathLen;
#ifdef WINDOWS
  unsigned int downDirLen;
#endif

  uri = NULL;
  meta = NULL;
  name = NULL;
  mime = NULL;
  gtk_tree_model_get(model,
                     iter,
                     SEARCH_NAME, &name,
                     SEARCH_URI, &uri,
                     SEARCH_META, &meta,
                     SEARCH_MIME, &mime,
                     -1);
  if (uri == NULL) {
    BREAK();
    return;
  }
  spin = getAnonymityButtonFromTM(model);
  if (spin == NULL) {
    BREAK();
    anon = 1;
  } else {
    anon = gtk_spin_button_get_value_as_int
      (GTK_SPIN_BUTTON(spin));
  }
  if (! ECRS_isFileUri(uri)) {
    if (ECRS_isNamespaceUri(uri)) {
      /* start namespace search; would probably be better
	 to add this as a subtree, but for simplicity
	 we'll just add it as a new tab for now */
      if (OK == FSUI_startSearch(ctx,
				 anon,
				 uri))
	openTabForSearch(NULL,
			 uri,
			 anon,
			 0,
			 NULL);
      return;
    } else {
      BREAK(); /* unsupported URI type (i.e. ksk or loc) */
      return;
    }
  }
  filename = ECRS_uriToString(uri);
  if ( (filename == NULL) ||
       (strlen(filename) <
        strlen(ECRS_URI_PREFIX) +
        strlen(ECRS_FILE_INFIX)) ) {
    BREAK();
    FREENONNULL(filename);
    return;
  }
  if (name == NULL)
    name = filename;

  fn = getFileName("FS",
                   "DOWNLOADDIR",
                   _("You must specify a directory in the configuration"
                     " in section `%s' under `%s'."));
  inc = getFileName("FS",
                   "INCOMINGDIR",
                   _("You must specify a directory in the configuration"
                     " in section `%s' under `%s'."));
                     
  if (strcmp(fn, inc) == 0) {
    LOG(LOG_ERROR, _("You must specify different directories in the "
                      "configuration in section `%s' under `%s' and `%s'."),
                      "FS", "DOWNLOADDIR", "INCOMINGDIR");
                      
    FREE(filename);
    FREENONNULL(fn);
    FREENONNULL(inc);
                      
    return;
  }

  addLogEntry(_("Downloading `%s'"), name);

  list = MALLOC(sizeof(DownloadList));
  list->next = head;
  list->rr = NULL;
  list->model = NULL;
  if (YES == ECRS_isDirectory(meta)) {
    list->rr = gtk_tree_row_reference_new(model, path);
    list->model = model;
  }
  list->uri = ECRS_dupUri(uri);
                     
#ifdef WINDOWS
  downDirLen = strlen(fn);
#endif
  mkdirp(fn);
  pfx = MALLOC(strlen(fn) + 2 +
               strlen(name) + 4); /* 4 = ".gnd" */
  lnk = MALLOC(strlen(fn) + 2 +
               strlen(filename));
  strcpy(pfx, fn);
  if (pfx[strlen(pfx)-1] != DIR_SEPARATOR)
    strcat(pfx,
           DIR_SEPARATOR_STR);
  strcpy(lnk, fn);
  FREE(fn);
  if (lnk[strlen(lnk)-1] != DIR_SEPARATOR)
    strcat(lnk,
           DIR_SEPARATOR_STR);
  strcat(pfx,
         name);
  strcat(lnk,
         &filename[strlen(ECRS_URI_PREFIX) +
                   strlen(ECRS_FILE_INFIX)]);

  /* Append ".gnd" if needed */
  if (mime && strcmp(mime, GNUNET_DIRECTORY_MIME) == 0) {
    int len = strlen(pfx);
    if (len > 4 && strcmp(pfx + len - 4, GNUNET_DIRECTORY_EXT) != 0) {
      char *end = pfx + len - 1;
      if (*end == '/' || *end == '\\')
        *end = 0;
    }
    strcat(pfx, GNUNET_DIRECTORY_EXT);
  }

#ifdef WINDOWS
  {
    /* Do not exceed MAX_PATH under Windows
       The longest path we get is
         $DOWNLOADDIR\hash.lnk
        or
         $INCOMINGDIR\hash.lnk

       We truncate the hash here accordingly. */

    int diff; /* How much is INCOMINGDIR larger than DOWNLOADDIR */

    diff = downDirLen - strlen(inc);
    if (diff < 0)
      diff = 0;

    char *end = lnk + MAX_PATH - 5 - diff; /* 4 = ".lnk" */

    *end = 0;

    if (strchr(end + 1, DIR_SEPARATOR)) {
      BREAK();
      return;
    }
  }
#endif

  FREENONNULL(inc);

  if (0 != STAT(pfx,
                &sbuf)) {
    if (0 != SYMLINK(lnk, pfx))
      LOG_FILE_STRERROR(LOG_ERROR, "symlink", pfx);
  } else {
    FREE(pfx);
    pfx = STRDUP(lnk);
  }

  /* If file is inside a directory, get the full path */
  dirTreePath = gtk_tree_path_copy(path);
  dirPath = MALLOC(1);
  dirPathLen = 0;
  while (gtk_tree_path_get_depth(dirTreePath) > 1) {
    const char *dirname;
    char *new;

    if (! gtk_tree_path_up(dirTreePath))
      break;

    if (!gtk_tree_model_get_iter(model,
				 &iiter,
				 dirTreePath))
      break;
    gtk_tree_model_get(model,
                       &iiter,
                       SEARCH_NAME, &dirname,
                       -1);
    dirPathLen += strlen(dirname) + 1;
    new = MALLOC(dirPathLen + 1);
    strcpy(new, dirname);
    strcat(new, DIR_SEPARATOR_STR);
    strcat(new, dirPath);
    FREE(dirPath);
    dirPath = new;
  }
  gtk_tree_path_free(dirTreePath);

  list->filename = pfx;
  head = list;
  gtk_tree_store_insert(summary,
                        &iiter,
                        NULL,
                        0);
  gtk_tree_store_set(summary,
                     &iiter,
                     DOWNLOAD_FILENAME, lnk,
                     DOWNLOAD_LINKNAME, pfx,
                     DOWNLOAD_SIZE, ECRS_fileSize(uri),
                     DOWNLOAD_PROGRESS, 0, /* progress */
                     DOWNLOAD_URISTRING, filename,
                     DOWNLOAD_URI, ECRS_dupUri(uri),
                     DOWNLOAD_TREEPATH, list->rr,
                     DOWNLOAD_DIRPATH, dirPath,
                     /* internal: row reference! */
                     -1);
  FREE(filename);

  FSUI_startDownload(ctx,
                     anon,
                     uri,
                     pfx);
  FREE(lnk);
}

void on_downloadButton_clicked(GtkWidget * treeview,
                               GtkWidget * downloadButton) {
  GtkTreeSelection * selection;

  selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(treeview));
  gtk_tree_selection_selected_foreach
    (selection,
     &initiateDownload,
     NULL);
}


/**
 */
void displayDownloadUpdate(const struct ECRS_URI * uri,
                           unsigned long long completed,
                           const char * data,
                           unsigned int size) {
  GtkTreeIter iter;
  unsigned int val;
  unsigned long long total;
  struct ECRS_URI * u;
  struct ECRS_MetaData * meta;


  if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(summary),
                                    &iter)) {
    do {
      gtk_tree_model_get(GTK_TREE_MODEL(summary),
                         &iter,
                         DOWNLOAD_SIZE, &total,
                         DOWNLOAD_URI, &u,
                         -1);
      if (u == NULL)
        return;
      if (ECRS_equalsUri(u, uri)) {
        if (total != 0)
          val = completed * 100 / total;
        else
          val = 100;
        gtk_tree_store_set(summary,
                           &iter,
                           DOWNLOAD_PROGRESS, val,
                           -1);
        break;
      }
    } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(summary),
                                      &iter));
  }
  meta = NULL;
  ECRS_listDirectory(data,
                     size,
                     &meta,
                     &addFilesToDirectory,
                     (void*)uri);
  if (meta != NULL)
    ECRS_freeMetaData(meta);
}

/**
 */
void displayDownloadComplete(const struct ECRS_URI * uri,
                             const char * filename) {
  char * ren;
  const char * fn;
  const char * ln;
  unsigned long long size;
  char * data;
  int fd;
  struct ECRS_MetaData * meta;
  DownloadList * pos;
  GtkTreeIter iter;
  GtkTreePath * path;
  struct ECRS_URI * u;
  char *dirPath;

  pos = head;
  while (pos != NULL) {
    if (ECRS_equalsUri(uri,
                       pos->uri))
      break;
    pos = pos->next;
  }
  
  /* Not available for resumed downloads */
  if (pos != NULL) {
    if ( (pos->rr != NULL) &&
         (gtk_tree_row_reference_valid(pos->rr)) ) {
  
      /* update directory view (if applicable!) */
      if (OK == getFileSize(filename, &size)) {
        fd = fileopen(filename, O_RDONLY);
        data = MMAP(NULL,
                    size,
                    PROT_READ,
                    MAP_SHARED,
                    fd,
                    0);
        meta = NULL;
        if (data != NULL) {
          ECRS_listDirectory(data,
                             size,
                             &meta,
                             &addFilesToDirectory,
                             (void*)uri);
          MUNMAP(data, size);
        }
        CLOSE(fd);
        if (meta != NULL)
          ECRS_freeMetaData(meta);
      }
  
      path = gtk_tree_row_reference_get_path(pos->rr);
      if (gtk_tree_path_get_depth(path) > 1) {
        gtk_tree_path_free(path);
        return;
      }
      gtk_tree_path_free(path);
    }
  }

  /* only rename top-level files, not files inside of directories! */
  if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(summary),
                                    &iter)) {
    do {
      ln = NULL;
      fn = NULL;
      gtk_tree_model_get(GTK_TREE_MODEL(summary),
                         &iter,
                         DOWNLOAD_URI, &u,
                         DOWNLOAD_FILENAME, &fn,
                         DOWNLOAD_LINKNAME, &ln,
                         DOWNLOAD_DIRPATH, &dirPath,
                         -1);
      if (ECRS_equalsUri(u, uri)) {
        char *dstPath, *newFn, *dstFile;
        char fnURL[PATH_MAX + 1], dummy[2];
        size_t len;

        len = strlen(ln);
        /* Don't change the name of GNUnet directories (.gnd) */
        if (len >= 4 && strcmp(ln + len - 4, ".gnd") != 0)
          ren = ECRS_suggestFilename(fn);
        else
          ren = NULL;
          
        newFn = strrchr(ren ? ren : ln, DIR_SEPARATOR) + 1;

        dstPath = getFileName("FS",
                         "INCOMINGDIR",
                         _("You must specify a directory in the configuration"
                           " in section `%s' under `%s'."));
        if (!dstPath) {
          FREENONNULL(ren);
          return;
        }

        /* If file is contained in a directory, create directory structure in
           the file system. */
        if (dirPath) {
          unsigned int pathLen = strlen(dirPath);
          if (pathLen) {
            pathLen += strlen(dstPath) + 2;
            dstPath = REALLOC(dstPath, pathLen);
            strcat(dstPath, DIR_SEPARATOR_STR);
            strcat(dstPath, dirPath);
            FREE(dirPath);
          }
        }

        mkdirp(dstPath);

        dstFile = MALLOC(strlen(dstPath) + strlen(newFn) + 2);
        strcpy(dstFile, dstPath);
        strcat(dstFile, DIR_SEPARATOR_STR);
        strcat(dstFile, newFn);
        
        if ((len = READLINK(ln ? ln : fn, fnURL, PATH_MAX)) == -1) {
          LOG(LOG_ERROR, _("Could not open symlink `%s': %s\n"),
            ln ? ln : fn, STRERROR(errno));
            
          FREE(dstFile);
          FREENONNULL(ren);
            
          return;
        }
        fnURL[len] = 0;
        
        /* If the file was downloaded before, fnURL is a symlink to
           dstFile */
        if ((READLINK(fnURL, dummy, 1) == -1) && (errno == EINVAL)) {
          if (RENAME(fnURL, dstFile) == -1) {
            /* renaming failed, try to copy */
            if (!copyFile(fnURL, dstFile)) {
              LOG(LOG_ERROR, _("Could not move or copy downloaded file %s to %s: %s."),
                fnURL, dstFile, STRERROR(errno));
              FREE(dstFile);
              FREENONNULL(ren);
  
              return;
            }
  
            if (REMOVE(fnURL) == -1)
              LOG(LOG_ERROR, _("Could not remove temporary file %s: %s\n"), fnURL, STRERROR(errno));
          }

          SYMLINK(dstFile, fnURL);

        }  

        if (ren)
          REMOVE(ren);
        else
          REMOVE(ln);

        gtk_tree_store_set(summary,
                           &iter,
                           DOWNLOAD_LINKNAME, dstFile,
                           -1);
        FREENONNULL(ren);
        FREE(dstFile);
        FREE(dstPath);

        break;
      }
    } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(summary),
                                      &iter));
  }

}

static int delDownloadView(void * cls,
                           const struct FSUI_DownloadList * pos,
                           const char * filename,
                           const struct ECRS_URI * uri,
                           unsigned long long filesize,
                           unsigned long long bytesCompleted,
                           int isRecursive,
                           unsigned int anonymityLevel) {
  GtkTreeIter iter;
  char * f, * f2, * fn;
  struct ECRS_URI * u;

  if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(summary),
                                    &iter)) {
    do {
      gtk_tree_model_get(GTK_TREE_MODEL(summary),
                         &iter,
                         DOWNLOAD_FILENAME, &f,
                         DOWNLOAD_LINKNAME, &f2,
                         DOWNLOAD_URI, &u,
                         -1);
                         
      f = strrchr(f, DIR_SEPARATOR);
      f2 = strrchr(f2, DIR_SEPARATOR);
      fn = strrchr(filename, DIR_SEPARATOR);
                         
      if ( (ECRS_equalsUri(u, uri)) &&
           (0 == strcmp(f, fn) || 0 == strcmp(f2, fn)) ) {
        gtk_tree_store_remove(summary,
                              &iter);
        break;
      }
    } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(summary),
                                      &iter));
  }
  return OK;
}

void on_clearCompletedDownloadsButton_clicked(void * unused,
                                              GtkWidget * clearButton) {
  FSUI_clearCompletedDownloads(ctx,
                               &delDownloadView,
                               NULL);
}

struct aDC_closure {
  struct ECRS_URI * u;
  char * fn;
};

static void * shutdownCode(void * c) {
  struct aDC_closure * cls = c;
  FSUI_stopDownload(ctx,
                    cls->u,
                    cls->fn);
  return NULL;
}

static void abortDownloadCallback(GtkTreeModel * model,
                                  GtkTreePath * path,
                                  GtkTreeIter * iter,
                                  GtkTreeStore * tree) {
  struct aDC_closure cls;

  GNUNET_ASSERT(model == GTK_TREE_MODEL(summary));
  gtk_tree_model_get(model,
                     iter,
                     DOWNLOAD_URI, &cls.u,
                     DOWNLOAD_FILENAME, &cls.fn,
                     -1);
  run_with_save_calls(&shutdownCode,
                      &cls);
  gtk_tree_store_remove(summary,
                        iter);
  if (cls.u != NULL)
    ECRS_freeUri(cls.u);
}

void on_abortDownloadButton_clicked(void * unused,
                                    GtkWidget * clearButton) {
  GtkTreeSelection * selection;
  GtkWidget * downloadList;

  downloadList = glade_xml_get_widget(getMainXML(),
                                      "activeDownloadsList");
  selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(downloadList));
  gtk_tree_selection_selected_foreach
    (selection,
     (GtkTreeSelectionForeachFunc) &abortDownloadCallback,
     NULL);
}

static int addDownloadView(void * cls,
                           const struct FSUI_DownloadList * pos,
                           const char * filename,
                           const struct ECRS_URI * uri,
                           unsigned long long filesize,
                           unsigned long long bytesCompleted,
                           int isRecursive,
                           unsigned int anonymityLevel) {
  GtkTreeIter iiter;
  int progress;
  char * uriname;

  if (filesize != 0)
    progress = bytesCompleted * 100 / filesize;
  else
    progress = 100;
  uriname = ECRS_uriToString(uri);
  gtk_tree_store_insert(summary,
                        &iiter,
                        NULL,
                        0);
  gtk_tree_store_set(summary,
                     &iiter,
                     DOWNLOAD_FILENAME, filename,
                     DOWNLOAD_LINKNAME, filename,
                     DOWNLOAD_SIZE, filesize,
                     DOWNLOAD_PROGRESS, progress,
                     DOWNLOAD_URISTRING, uriname,
                     DOWNLOAD_URI, ECRS_dupUri(uri),
                     DOWNLOAD_TREEPATH, NULL,
                     -1);
  FREE(uriname);
  return OK;
}


void fs_download_start() {
  GtkWidget * downloadList;
  GtkCellRenderer * renderer;
  int col;


  downloadList = glade_xml_get_widget(getMainXML(),
                                      "activeDownloadsList");
  summary =
    gtk_tree_store_new(DOWNLOAD_NUM,
                       G_TYPE_STRING, /* name (URI as string) */
                       G_TYPE_STRING, /* name (user-friendly name) */
                       G_TYPE_UINT64,  /* size */
                       G_TYPE_INT,  /* progress */
                       G_TYPE_STRING, /* uri */
                       G_TYPE_POINTER,  /* url */
                       G_TYPE_POINTER, /* internal: gtk tree path / NULL */
                       G_TYPE_POINTER); /* directory path if file is inside a dir */
  gtk_tree_view_set_model(GTK_TREE_VIEW(downloadList),
                          GTK_TREE_MODEL(summary));
  renderer = gtk_cell_renderer_progress_new();
  col = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(downloadList),
                                              -1,
                                              _("Name"),
                                              renderer,
                                              "value", DOWNLOAD_PROGRESS,
                                              "text", DOWNLOAD_LINKNAME,
                                              NULL);
  gtk_tree_view_column_set_resizable(gtk_tree_view_get_column(GTK_TREE_VIEW(downloadList),
                                              col - 1),
                                              TRUE);
  renderer = gtk_cell_renderer_text_new();
  col = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(downloadList),
                                              -1,
                                              _("Size"),
                                              renderer,
                                              "text", DOWNLOAD_SIZE,
                                              NULL);
  gtk_tree_view_column_set_resizable(gtk_tree_view_get_column(GTK_TREE_VIEW(downloadList),
                                              col - 1),
                                              TRUE);
  renderer = gtk_cell_renderer_text_new();
  col = gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(downloadList),
                                              -1,
                                              _("URI"),
                                              renderer,
                                              "text", DOWNLOAD_URISTRING,
                                              NULL);
  gtk_tree_view_column_set_resizable(gtk_tree_view_get_column(GTK_TREE_VIEW(downloadList),
                                              col - 1),
                                              TRUE);
  FSUI_listDownloads(ctx,
                     NULL,
                     &addDownloadView,
                     NULL);
}


void fs_download_stop() {
  GtkTreeIter iter;
  struct ECRS_URI * u;
  DownloadList * pos;
  char *dirPath;

  /* free URIs in summary model */
  if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(summary),
                                      &iter))
    return;
  do {
    gtk_tree_model_get(GTK_TREE_MODEL(summary),
                       &iter,
                       DOWNLOAD_URI, &u,
                       DOWNLOAD_DIRPATH, &dirPath,
                       -1);
    gtk_tree_store_set(summary,
                       &iter,
                       DOWNLOAD_URI, NULL,
                       -1);
    if (u != NULL)
      ECRS_freeUri(u);
    FREENONNULL(dirPath);
  } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(summary),
                                    &iter));
  while (head != NULL) {
    pos = head->next;
    ECRS_freeUri(head->uri);
    FREE(head->filename);
    gtk_tree_row_reference_free(head->rr);
    FREE(head);
    head = pos;
  }
}


/* end of download.c */