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>
21 4e32d3a6 2024-06-04 thomas.ad #include <unistd.h>
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"
27 3b5f459e 2021-11-05 op struct downloads downloads = STAILQ_HEAD_INITIALIZER(downloads);
30 6219c17f 2021-11-29 op no_downloads(void)
32 3b5f459e 2021-11-05 op struct line *l;
34 3d89457c 2024-06-18 thomas.ad l = xcalloc(1, sizeof(*l));
36 1577540c 2021-11-05 op l->type = LINE_DOWNLOAD_INFO;
37 3d89457c 2024-06-18 thomas.ad l->line = xstrdup("No downloads");
39 c1d27b0e 2024-06-14 op TAILQ_INSERT_TAIL(&downloadwin.head, l, lines);
43 3b5f459e 2021-11-05 op recompute_downloads(void)
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];
49 c1d27b0e 2024-06-14 op downloadwin.mode = "*Downloads*";
50 3b5f459e 2021-11-05 op erase_buffer(&downloadwin);
52 3b5f459e 2021-11-05 op if (STAILQ_EMPTY(&downloads)) {
53 3b5f459e 2021-11-05 op no_downloads();
57 3b5f459e 2021-11-05 op STAILQ_FOREACH(d, &downloads, entries) {
58 3d89457c 2024-06-18 thomas.ad l = xcalloc(1, sizeof(*l));
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 3d89457c 2024-06-18 thomas.ad l->line = xstrdup(buf);
67 3d89457c 2024-06-18 thomas.ad l->alt = xstrdup(d->path);
69 c1d27b0e 2024-06-14 op TAILQ_INSERT_TAIL(&downloadwin.head, l, lines);
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
79 f853ec6f 2024-10-22 op wrap_page(&downloadwin, download_cols, 0);
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)
85 fcd99a0d 2021-11-05 op struct download *d;
87 3d89457c 2024-06-18 thomas.ad d = xcalloc(1, sizeof(*d));
91 3d89457c 2024-06-18 thomas.ad d->path = xstrdup(path);
92 3d89457c 2024-06-18 thomas.ad d->mime_type = xstrdup(mime_type);
94 868b3a8f 2022-04-13 op STAILQ_INSERT_HEAD(&downloads, d, entries);
99 fcd99a0d 2021-11-05 op struct download *
100 fcd99a0d 2021-11-05 op download_by_id(uint32_t id)
102 fcd99a0d 2021-11-05 op struct download *d;
104 fcd99a0d 2021-11-05 op STAILQ_FOREACH(d, &downloads, entries) {
105 fcd99a0d 2021-11-05 op if (d->id == id)
113 4e32d3a6 2024-06-04 thomas.ad download_finished(struct download *d)
115 4e32d3a6 2024-06-04 thomas.ad if (d == NULL)
116 4e32d3a6 2024-06-04 thomas.ad return;
118 4e32d3a6 2024-06-04 thomas.ad close(d->fd);
119 4e32d3a6 2024-06-04 thomas.ad d->fd = -1;
121 4e32d3a6 2024-06-04 thomas.ad ui_on_download_refresh();
122 2f0ffab4 2024-06-22 op ui_prompt_download_cmd(d);