diff options
Diffstat (limited to 'clients/screens_invoices.c')
| -rw-r--r-- | clients/screens_invoices.c | 88 |
1 files changed, 3 insertions, 85 deletions
diff --git a/clients/screens_invoices.c b/clients/screens_invoices.c index 3782a5f..8fb32eb 100644 --- a/clients/screens_invoices.c +++ b/clients/screens_invoices.c @@ -41,89 +41,7 @@ static void today_iso(char *buf, size_t n) buf[len] = '\0'; } -/* Directory for generated invoice PDFs. */ -static void invoice_cache_dir(char *buf, size_t n) -{ - const char *cache = getenv("XDG_CACHE_HOME"); - if (cache && *cache) { - snprintf(buf, n, "%s/bokf", cache); - return; - } - snprintf(buf, n, "/tmp"); -} - -/* Keep a customer-supplied name from escaping the directory. */ -static void safe_fname(const char *in, char *out, size_t n) -{ - size_t o = 0; - for (const char *p = in ? in : ""; *p && o + 1 < n; p++) { - unsigned char c = (unsigned char)*p; - if (c < 32 || c == '/') - c = '-'; - out[o++] = (char)c; - } - out[o] = '\0'; -} - -static int write_pdf_b64(const char *b64, const char *path) -{ - unsigned char *data = NULL; - size_t len = 0; - if (!b64 || util_b64_decode(b64, strlen(b64), &data, &len) != 0) - return -1; - FILE *fp = fopen(path, "wb"); - if (!fp) { - free(data); - return -1; - } - size_t wrote = fwrite(data, 1, len, fp); - fclose(fp); - free(data); - return wrote == len ? 0 : -1; -} -/* Open a saved PDF with xdg-open when a desktop session is present; - otherwise (or on fork failure) show where it was saved. */ -static void open_pdf(const char *path) -{ - const char *display = getenv("DISPLAY"); - const char *wayland = getenv("WAYLAND_DISPLAY"); - if ((!display || !*display) && (!wayland || !*wayland)) { - tui_message("Faktura PDF", "Sparad: %s", path); - return; - } - pid_t pid = fork(); - if (pid == 0) { - pid_t gc = fork(); - if (gc == 0) { - setsid(); - execlp("xdg-open", "xdg-open", path, (char *)NULL); - _exit(127); - } - _exit(gc < 0 ? 1 : 0); - } - if (pid > 0) - waitpid(pid, NULL, 0); - else - tui_message("Faktura PDF", "Sparad: %s", path); -} - -static void save_and_open_pdf(const char *b64, const char *filename) -{ - char dir[512], path[700], safe[600]; - invoice_cache_dir(dir, sizeof dir); - safe_fname(filename, safe, sizeof safe); - if (snprintf(path, sizeof path, "%s/%s", dir, safe) >= (int)sizeof path) { - tui_message("Faktura PDF", "Sökvägen blir för lång."); - return; - } - config_mkdirs(path); - if (write_pdf_b64(b64, path) != 0) { - tui_message("Faktura PDF", "Kunde inte spara %s", path); - return; - } - open_pdf(path); -} static const char *invoice_status_sv(const char *s) { @@ -558,7 +476,7 @@ static void inv_detail_pdf(struct invctx *ctx) char fname[800]; snprintf(fname, sizeof fname, "Faktura %lld %s.pdf", (long long)ctx->number, ctx->customer); - save_and_open_pdf(b64, fname); + pdf_save_and_open(b64, fname, "Faktura PDF"); free(b64); } free(resp); @@ -1049,7 +967,7 @@ static void iform_preview(struct iform *f) } char *b64 = jstr_dup(resp, "result.content_base64"); if (b64) { - save_and_open_pdf(b64, "Faktura förhandsvisning.pdf"); + pdf_save_and_open(b64, "Faktura förhandsvisning.pdf", "Faktura PDF"); free(b64); } free(resp); @@ -1174,7 +1092,7 @@ static int64_t invoices_new(struct app *a) tui_rt_set_fields(&f.rt, ff, 7); const char *hint = "Enter = välj/ändra Tab = byta fält F5 = förhandsvisa" - " ^Enter = utfärda Esc = avbryt"; + " F9 = utfärda Esc = avbryt"; int64_t out = 0; for (;;) { int rr = tui_rt_run("Ny faktura", &f.rt, hint); |
