aboutsummaryrefslogtreecommitdiff
path: root/src/download.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/download.c')
-rw-r--r--src/download.c227
1 files changed, 203 insertions, 24 deletions
diff --git a/src/download.c b/src/download.c
index dca61157..0154e9ac 100644
--- a/src/download.c
+++ b/src/download.c
@@ -36,12 +36,137 @@ typedef struct DL {
36 struct ECRS_URI * uri; 36 struct ECRS_URI * uri;
37 char * filename; 37 char * filename;
38 GtkTreeRowReference * rr; 38 GtkTreeRowReference * rr;
39 GtkTreeModel * model;
39} DownloadList; 40} DownloadList;
40 41
41static DownloadList * head; 42static DownloadList * head;
42 43
43static GtkTreeStore * summary; 44static GtkTreeStore * summary;
44 45
46static int addFilesToDirectory
47 (const ECRS_FileInfo * fi,
48 const HashCode512 * key,
49 void * closure) {
50 struct ECRS_URI * uri = closure;
51 DownloadList * pos;
52
53 pos = head;
54 while (pos != NULL) {
55 if (ECRS_equalsUri(uri,
56 pos->uri))
57 break;
58 pos = pos->next;
59 }
60 if (pos == NULL) {
61 BREAK(); /* odd */
62 } else {
63 GtkTreeIter iter;
64 GtkTreeIter child;
65 int i;
66 unsigned long long size;
67 char * name;
68 char * mime;
69 char * desc;
70 unsigned char * thumb;
71 size_t ts;
72 GdkPixbuf * pixbuf;
73 GdkPixbufLoader * loader;
74 GtkTreePath * path;
75
76 if (! gtk_tree_row_reference_valid(pos->rr))
77 return SYSERR;
78 path = gtk_tree_row_reference_get_path(pos->rr);
79 gtk_tree_model_get_iter(GTK_TREE_MODEL(pos->model),
80 &iter,
81 path);
82 for (i=gtk_tree_model_iter_n_children(pos->model,
83 &iter)-1;i>=0;i--) {
84 if (TRUE == gtk_tree_model_iter_nth_child(pos->model,
85 &child,
86 &iter,
87 i)) {
88 struct ECRS_URI * uri;
89 uri = NULL;
90 gtk_tree_model_get(pos->model,
91 &iter,
92 5, &uri,
93 -1);
94 if ( (uri != NULL) &&
95 (ECRS_equalsUri(uri,
96 fi->uri)) )
97 return OK;
98 }
99 }
100 gtk_tree_store_append(GTK_TREE_STORE(pos->model),
101 &child,
102 &iter);
103
104 /* FIXME: this code is identical to some
105 code in search.c -- refactor into one
106 function (on model, iter and fi) */
107 mime = ECRS_getFromMetaData(fi->meta,
108 EXTRACTOR_MIMETYPE);
109 if (mime == NULL)
110 mime = STRDUP(_("unknown"));
111 desc = ECRS_getFirstFromMetaData(fi->meta,
112 EXTRACTOR_DESCRIPTION,
113 EXTRACTOR_GENRE,
114 EXTRACTOR_ALBUM,
115 EXTRACTOR_COMMENT,
116 EXTRACTOR_SUBJECT,
117 EXTRACTOR_FORMAT,
118 EXTRACTOR_SIZE,
119 EXTRACTOR_KEYWORDS,
120 -1);
121 if (desc == NULL)
122 desc = STRDUP(_(""));
123 name = ECRS_getFirstFromMetaData(fi->meta,
124 EXTRACTOR_FILENAME,
125 EXTRACTOR_TITLE,
126 EXTRACTOR_ARTIST,
127 EXTRACTOR_AUTHOR,
128 EXTRACTOR_PUBLISHER,
129 EXTRACTOR_CREATOR,
130 EXTRACTOR_PRODUCER,
131 EXTRACTOR_UNKNOWN,
132 -1);
133 if (name == NULL)
134 name = STRDUP(_("no name given"));
135 size = ECRS_fileSize(fi->uri);
136 thumb = NULL;
137 ts = ECRS_getThumbnailFromMetaData(fi->meta,
138 &thumb);
139 if (ts != 0) {
140 loader = gdk_pixbuf_loader_new();
141 gdk_pixbuf_loader_write(loader,
142 (const guchar*) thumb,
143 ts,
144 NULL);
145 pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
146 gdk_pixbuf_loader_close(loader,
147 NULL);
148 } else {
149 pixbuf = NULL;
150 }
151 gtk_tree_store_set(GTK_TREE_STORE(pos->model),
152 &child,
153 0, name,
154 1, size,
155 2, mime,
156 3, desc,
157 4, pixbuf,
158 5, ECRS_dupUri(fi->uri),
159 6, ECRS_dupMetaData(fi->meta),
160 7, NULL, /* internal */
161 -1);
162 FREE(mime);
163 FREE(desc);
164 FREE(name);
165 FREENONNULL(thumb);
166 }
167 return OK;
168}
169
45static void initiateDownload(GtkTreeModel *model, 170static void initiateDownload(GtkTreeModel *model,
46 GtkTreePath * path, 171 GtkTreePath * path,
47 GtkTreeIter * iter, 172 GtkTreeIter * iter,
@@ -85,8 +210,11 @@ static void initiateDownload(GtkTreeModel *model,
85 list = MALLOC(sizeof(DownloadList)); 210 list = MALLOC(sizeof(DownloadList));
86 list->next = head; 211 list->next = head;
87 list->rr = NULL; 212 list->rr = NULL;
88 if (ECRS_isDirectory(meta)) 213 list->model = NULL;
214 if (ECRS_isDirectory(meta)) {
89 list->rr = gtk_tree_row_reference_new(model, path); 215 list->rr = gtk_tree_row_reference_new(model, path);
216 list->model = model;
217 }
90 list->uri = ECRS_dupUri(uri); 218 list->uri = ECRS_dupUri(uri);
91 fn = getFileName("FS", 219 fn = getFileName("FS",
92 "DOWNLOADDIR", 220 "DOWNLOADDIR",
@@ -160,35 +288,46 @@ void on_downloadButton_clicked(GtkWidget * treeview,
160/** 288/**
161 */ 289 */
162void displayDownloadUpdate(const struct ECRS_URI * uri, 290void displayDownloadUpdate(const struct ECRS_URI * uri,
163 unsigned long long completed) { 291 unsigned long long completed,
292 const char * data,
293 unsigned int size) {
164 GtkTreeIter iter; 294 GtkTreeIter iter;
165 unsigned int val; 295 unsigned int val;
166 unsigned long long total; 296 unsigned long long total;
167 struct ECRS_URI * u; 297 struct ECRS_URI * u;
298 struct ECRS_MetaData * meta;
299
168 300
169 if (! gtk_tree_model_get_iter_first(GTK_TREE_MODEL(summary), 301 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(summary),
170 &iter)) 302 &iter)) {
171 return; 303 do {
172 do { 304 gtk_tree_model_get(GTK_TREE_MODEL(summary),
173
174 gtk_tree_model_get(GTK_TREE_MODEL(summary),
175 &iter,
176 1, &total,
177 4, &u,
178 -1);
179 if (ECRS_equalsUri(u, uri)) {
180 if (total != 0)
181 val = completed * 100 / total;
182 else
183 val = 100;
184 gtk_tree_store_set(GTK_TREE_STORE(summary),
185 &iter, 305 &iter,
186 2, val, 306 1, &total,
187 -1); 307 4, &u,
188 break; 308 -1);
189 } 309 if (ECRS_equalsUri(u, uri)) {
190 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(summary), 310 if (total != 0)
191 &iter)); 311 val = completed * 100 / total;
312 else
313 val = 100;
314 gtk_tree_store_set(GTK_TREE_STORE(summary),
315 &iter,
316 2, val,
317 -1);
318 break;
319 }
320 } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(summary),
321 &iter));
322 }
323 meta = NULL;
324 ECRS_listDirectory(data,
325 size,
326 &meta,
327 &addFilesToDirectory,
328 (void*)uri);
329 if (meta != NULL)
330 ECRS_freeMetaData(meta);
192} 331}
193 332
194/** 333/**
@@ -196,6 +335,19 @@ void displayDownloadUpdate(const struct ECRS_URI * uri,
196void displayDownloadComplete(const struct ECRS_URI * uri, 335void displayDownloadComplete(const struct ECRS_URI * uri,
197 const char * filename) { 336 const char * filename) {
198 char * ren; 337 char * ren;
338 unsigned long long size;
339 char * data;
340 int fd;
341 struct ECRS_MetaData * meta;
342 DownloadList * pos;
343
344 pos = head;
345 while (pos != NULL) {
346 if (ECRS_equalsUri(uri,
347 pos->uri))
348 break;
349 pos = pos->next;
350 }
199 351
200 ren = ECRS_suggestFilename(filename); 352 ren = ECRS_suggestFilename(filename);
201 if (ren == NULL) 353 if (ren == NULL)
@@ -207,9 +359,35 @@ void displayDownloadComplete(const struct ECRS_URI * uri,
207 } 359 }
208 FREE(ren); 360 FREE(ren);
209 /* fixme: also update download model? */ 361 /* fixme: also update download model? */
362 /* update directory view (if applicable!) */
363 if ( (pos != NULL) &&
364 (pos->rr != NULL) ) {
365 size = getFileSize(filename);
366 fd = fileopen(filename, O_RDONLY);
367 data = MMAP(NULL,
368 size,
369 PROT_READ,
370 MAP_SHARED,
371 fd,
372 0);
373 meta = NULL;
374 if (data != NULL) {
375 ECRS_listDirectory(data,
376 size,
377 &meta,
378 &addFilesToDirectory,
379 (void*)uri);
380 MUNMAP(data, size);
381 }
382 CLOSE(fd);
383 if (meta != NULL)
384 ECRS_freeMetaData(meta);
385 }
386
210} 387}
211 388
212static int addDownloadView(void * cls, 389static int addDownloadView(void * cls,
390 const struct FSUI_DownloadList * pos,
213 const char * filename, 391 const char * filename,
214 const struct ECRS_URI * uri, 392 const struct ECRS_URI * uri,
215 unsigned long long filesize, 393 unsigned long long filesize,
@@ -284,6 +462,7 @@ void fs_download_start() {
284 "text", 3, 462 "text", 3,
285 NULL); 463 NULL);
286 FSUI_listDownloads(ctx, 464 FSUI_listDownloads(ctx,
465 NULL,
287 &addDownloadView, 466 &addDownloadView,
288 NULL); 467 NULL);
289} 468}