Blame


1 3b5f459e 2021-11-05 op /*
2 3b5f459e 2021-11-05 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 3b5f459e 2021-11-05 op *
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.
7 3b5f459e 2021-11-05 op *
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.
15 3b5f459e 2021-11-05 op */
16 3b5f459e 2021-11-05 op
17 3b5f459e 2021-11-05 op #include "compat.h"
18 3b5f459e 2021-11-05 op
19 3b5f459e 2021-11-05 op #include <stdlib.h>
20 3b5f459e 2021-11-05 op #include <string.h>
21 3b5f459e 2021-11-05 op
22 3b5f459e 2021-11-05 op #include "telescope.h"
23 3b5f459e 2021-11-05 op #include "ui.h"
24 3b5f459e 2021-11-05 op
25 3b5f459e 2021-11-05 op struct downloads downloads = STAILQ_HEAD_INITIALIZER(downloads);
26 3b5f459e 2021-11-05 op
27 3b5f459e 2021-11-05 op static void
28 6219c17f 2021-11-29 op no_downloads(void)
29 3b5f459e 2021-11-05 op {
30 3b5f459e 2021-11-05 op struct line *l;
31 3b5f459e 2021-11-05 op
32 3b5f459e 2021-11-05 op if ((l = calloc(1, sizeof(*l))) == NULL)
33 3b5f459e 2021-11-05 op abort();
34 3b5f459e 2021-11-05 op
35 1577540c 2021-11-05 op l->type = LINE_DOWNLOAD_INFO;
36 3b5f459e 2021-11-05 op l->line = strdup("No downloads");
37 3b5f459e 2021-11-05 op
38 3b5f459e 2021-11-05 op TAILQ_INSERT_TAIL(&downloadwin.page.head, l, lines);
39 3b5f459e 2021-11-05 op }
40 3b5f459e 2021-11-05 op
41 3b5f459e 2021-11-05 op void
42 3b5f459e 2021-11-05 op recompute_downloads(void)
43 3b5f459e 2021-11-05 op {
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];
47 3b5f459e 2021-11-05 op
48 3b5f459e 2021-11-05 op downloadwin.page.name = "*Downloads*";
49 3b5f459e 2021-11-05 op erase_buffer(&downloadwin);
50 3b5f459e 2021-11-05 op
51 3b5f459e 2021-11-05 op if (STAILQ_EMPTY(&downloads)) {
52 3b5f459e 2021-11-05 op no_downloads();
53 a1a63cf5 2021-11-05 op goto end;
54 3b5f459e 2021-11-05 op }
55 3b5f459e 2021-11-05 op
56 3b5f459e 2021-11-05 op STAILQ_FOREACH(d, &downloads, entries) {
57 3b5f459e 2021-11-05 op if ((l = calloc(1, sizeof(*l))) == NULL)
58 3b5f459e 2021-11-05 op abort();
59 3b5f459e 2021-11-05 op
60 3b5f459e 2021-11-05 op fmt_scaled(d->bytes, buf);
61 3b5f459e 2021-11-05 op
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;
65 1577540c 2021-11-05 op
66 15f22b81 2021-11-05 op l->line = strdup(buf);
67 15f22b81 2021-11-05 op l->alt = strdup(d->path);
68 3b5f459e 2021-11-05 op
69 3b5f459e 2021-11-05 op TAILQ_INSERT_TAIL(&downloadwin.page.head, l, lines);
70 3b5f459e 2021-11-05 op }
71 a1a63cf5 2021-11-05 op
72 a1a63cf5 2021-11-05 op end:
73 84892515 2021-11-27 op /*
74 84892515 2021-11-27 op * The exact value doesn't matter, as wrap_page only considers
75 84892515 2021-11-27 op * l->line, which is the human representation of the byte
76 84892515 2021-11-27 op * counter, and we know for sure is < FMT_SCALED_STRSIZE so it
77 84892515 2021-11-27 op * fits.
78 84892515 2021-11-27 op */
79 84892515 2021-11-27 op wrap_page(&downloadwin, download_cols);
80 3b5f459e 2021-11-05 op }
81 fcd99a0d 2021-11-05 op
82 f63b8f73 2022-04-24 op struct download *
83 fd984e76 2022-05-05 op enqueue_download(uint32_t id, const char *path)
84 fcd99a0d 2021-11-05 op {
85 fcd99a0d 2021-11-05 op struct download *d;
86 fcd99a0d 2021-11-05 op
87 fcd99a0d 2021-11-05 op if ((d = calloc(1, sizeof(*d))) == NULL)
88 fcd99a0d 2021-11-05 op abort();
89 fcd99a0d 2021-11-05 op
90 fcd99a0d 2021-11-05 op d->id = id;
91 fcd99a0d 2021-11-05 op d->fd = -1;
92 fcd99a0d 2021-11-05 op d->path = strdup(path);
93 fcd99a0d 2021-11-05 op
94 868b3a8f 2022-04-13 op STAILQ_INSERT_HEAD(&downloads, d, entries);
95 f63b8f73 2022-04-24 op
96 f63b8f73 2022-04-24 op return d;
97 fcd99a0d 2021-11-05 op }
98 fcd99a0d 2021-11-05 op
99 fcd99a0d 2021-11-05 op struct download *
100 fcd99a0d 2021-11-05 op download_by_id(uint32_t id)
101 fcd99a0d 2021-11-05 op {
102 fcd99a0d 2021-11-05 op struct download *d;
103 fcd99a0d 2021-11-05 op
104 fcd99a0d 2021-11-05 op STAILQ_FOREACH(d, &downloads, entries) {
105 fcd99a0d 2021-11-05 op if (d->id == id)
106 fcd99a0d 2021-11-05 op return d;
107 fcd99a0d 2021-11-05 op }
108 fcd99a0d 2021-11-05 op
109 fcd99a0d 2021-11-05 op return NULL;
110 fcd99a0d 2021-11-05 op }