2 3b5f459e 2021-11-05 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 3b5f459e 2021-11-05 op * Permission to use, copy, modify, and distribute this software for any
5 3b5f459e 2021-11-05 op * purpose with or without fee is hereby granted, provided that the above
6 3b5f459e 2021-11-05 op * copyright notice and this permission notice appear in all copies.
8 3b5f459e 2021-11-05 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 3b5f459e 2021-11-05 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 3b5f459e 2021-11-05 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 3b5f459e 2021-11-05 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 3b5f459e 2021-11-05 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 3b5f459e 2021-11-05 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 3b5f459e 2021-11-05 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 3b5f459e 2021-11-05 op #include "compat.h"
19 3b5f459e 2021-11-05 op #include <stdlib.h>
20 3b5f459e 2021-11-05 op #include <string.h>
22 3b5f459e 2021-11-05 op #include "telescope.h"
23 3b5f459e 2021-11-05 op #include "ui.h"
25 3b5f459e 2021-11-05 op struct downloads downloads = STAILQ_HEAD_INITIALIZER(downloads);
28 3b5f459e 2021-11-05 op no_downloads()
30 3b5f459e 2021-11-05 op struct line *l;
32 3b5f459e 2021-11-05 op if ((l = calloc(1, sizeof(*l))) == NULL)
35 1577540c 2021-11-05 op l->type = LINE_DOWNLOAD_INFO;
36 3b5f459e 2021-11-05 op l->line = strdup("No downloads");
38 3b5f459e 2021-11-05 op TAILQ_INSERT_TAIL(&downloadwin.page.head, l, lines);
42 3b5f459e 2021-11-05 op recompute_downloads(void)
44 3b5f459e 2021-11-05 op struct download *d;
45 3b5f459e 2021-11-05 op struct line *l;
46 3b5f459e 2021-11-05 op char buf[FMT_SCALED_STRSIZE];
48 3b5f459e 2021-11-05 op downloadwin.page.name = "*Downloads*";
49 3b5f459e 2021-11-05 op erase_buffer(&downloadwin);
51 3b5f459e 2021-11-05 op if (STAILQ_EMPTY(&downloads)) {
52 3b5f459e 2021-11-05 op no_downloads();
56 3b5f459e 2021-11-05 op STAILQ_FOREACH(d, &downloads, entries) {
57 3b5f459e 2021-11-05 op if ((l = calloc(1, sizeof(*l))) == NULL)
60 3b5f459e 2021-11-05 op fmt_scaled(d->bytes, buf);
62 1577540c 2021-11-05 op l->type = LINE_DOWNLOAD;
63 1577540c 2021-11-05 op if (d->fd == -1)
64 1577540c 2021-11-05 op l->type = LINE_DOWNLOAD_DONE;
66 15f22b81 2021-11-05 op l->line = strdup(buf);
67 15f22b81 2021-11-05 op l->alt = strdup(d->path);
69 3b5f459e 2021-11-05 op TAILQ_INSERT_TAIL(&downloadwin.page.head, l, lines);
73 a1a63cf5 2021-11-05 op wrap_page(&downloadwin.page, download_lines);
77 fcd99a0d 2021-11-05 op enqueue_download(uint32_t id, const char *path)
79 fcd99a0d 2021-11-05 op struct download *d;
81 fcd99a0d 2021-11-05 op if ((d = calloc(1, sizeof(*d))) == NULL)
86 fcd99a0d 2021-11-05 op d->path = strdup(path);
88 fcd99a0d 2021-11-05 op STAILQ_INSERT_TAIL(&downloads, d, entries);
91 fcd99a0d 2021-11-05 op struct download *
92 fcd99a0d 2021-11-05 op download_by_id(uint32_t id)
94 fcd99a0d 2021-11-05 op struct download *d;
96 fcd99a0d 2021-11-05 op STAILQ_FOREACH(d, &downloads, entries) {
97 fcd99a0d 2021-11-05 op if (d->id == id)