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 4e32d3a6 2024-06-04 thomas.ad #include <unistd.h>
22 3b5f459e 2021-11-05 op
23 3b5f459e 2021-11-05 op #include "telescope.h"
24 3b5f459e 2021-11-05 op #include "ui.h"
25 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
26 3b5f459e 2021-11-05 op
27 3b5f459e 2021-11-05 op struct downloads downloads = STAILQ_HEAD_INITIALIZER(downloads);
28 3b5f459e 2021-11-05 op
29 3b5f459e 2021-11-05 op static void
30 6219c17f 2021-11-29 op no_downloads(void)
31 3b5f459e 2021-11-05 op {
32 3b5f459e 2021-11-05 op struct line *l;
33 3b5f459e 2021-11-05 op
34 3d89457c 2024-06-18 thomas.ad l = xcalloc(1, sizeof(*l));
35 3b5f459e 2021-11-05 op
36 1577540c 2021-11-05 op l->type = LINE_DOWNLOAD_INFO;
37 3d89457c 2024-06-18 thomas.ad l->line = xstrdup("No downloads");
38 3b5f459e 2021-11-05 op
39 c1d27b0e 2024-06-14 op TAILQ_INSERT_TAIL(&downloadwin.head, l, lines);
40 3b5f459e 2021-11-05 op }
41 3b5f459e 2021-11-05 op
42 3b5f459e 2021-11-05 op void
43 3b5f459e 2021-11-05 op recompute_downloads(void)
44 3b5f459e 2021-11-05 op {
45 3b5f459e 2021-11-05 op struct download *d;
46 3b5f459e 2021-11-05 op struct line *l;
47 3b5f459e 2021-11-05 op char buf[FMT_SCALED_STRSIZE];
48 3b5f459e 2021-11-05 op
49 c1d27b0e 2024-06-14 op downloadwin.mode = "*Downloads*";
50 3b5f459e 2021-11-05 op erase_buffer(&downloadwin);
51 3b5f459e 2021-11-05 op
52 3b5f459e 2021-11-05 op if (STAILQ_EMPTY(&downloads)) {
53 3b5f459e 2021-11-05 op no_downloads();
54 a1a63cf5 2021-11-05 op goto end;
55 3b5f459e 2021-11-05 op }
56 3b5f459e 2021-11-05 op
57 3b5f459e 2021-11-05 op STAILQ_FOREACH(d, &downloads, entries) {
58 3d89457c 2024-06-18 thomas.ad l = xcalloc(1, sizeof(*l));
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 3d89457c 2024-06-18 thomas.ad l->line = xstrdup(buf);
67 3d89457c 2024-06-18 thomas.ad l->alt = xstrdup(d->path);
68 3b5f459e 2021-11-05 op
69 c1d27b0e 2024-06-14 op TAILQ_INSERT_TAIL(&downloadwin.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 f853ec6f 2024-10-22 op wrap_page(&downloadwin, download_cols, 0);
80 3b5f459e 2021-11-05 op }
81 fcd99a0d 2021-11-05 op
82 f63b8f73 2022-04-24 op struct download *
83 7b7a69f2 2024-06-13 thomas.ad enqueue_download(uint32_t id, const char *path, const char *mime_type)
84 fcd99a0d 2021-11-05 op {
85 fcd99a0d 2021-11-05 op struct download *d;
86 fcd99a0d 2021-11-05 op
87 3d89457c 2024-06-18 thomas.ad d = xcalloc(1, sizeof(*d));
88 fcd99a0d 2021-11-05 op
89 fcd99a0d 2021-11-05 op d->id = id;
90 fcd99a0d 2021-11-05 op d->fd = -1;
91 3d89457c 2024-06-18 thomas.ad d->path = xstrdup(path);
92 3d89457c 2024-06-18 thomas.ad d->mime_type = xstrdup(mime_type);
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 }
111 4e32d3a6 2024-06-04 thomas.ad
112 4e32d3a6 2024-06-04 thomas.ad void
113 4e32d3a6 2024-06-04 thomas.ad download_finished(struct download *d)
114 4e32d3a6 2024-06-04 thomas.ad {
115 4e32d3a6 2024-06-04 thomas.ad if (d == NULL)
116 4e32d3a6 2024-06-04 thomas.ad return;
117 4e32d3a6 2024-06-04 thomas.ad
118 4e32d3a6 2024-06-04 thomas.ad close(d->fd);
119 4e32d3a6 2024-06-04 thomas.ad d->fd = -1;
120 4e32d3a6 2024-06-04 thomas.ad
121 4e32d3a6 2024-06-04 thomas.ad ui_on_download_refresh();
122 2f0ffab4 2024-06-22 op ui_prompt_download_cmd(d);
123 4e32d3a6 2024-06-04 thomas.ad }