aboutsummaryrefslogtreecommitdiff
path: root/clients/ui.c
diff options
context:
space:
mode:
Diffstat (limited to 'clients/ui.c')
-rw-r--r--clients/ui.c104
1 files changed, 95 insertions, 9 deletions
diff --git a/clients/ui.c b/clients/ui.c
index ff573c9..ca6906f 100644
--- a/clients/ui.c
+++ b/clients/ui.c
@@ -217,17 +217,12 @@ void app_refresh_context(struct app *a)
free(resp);
snprintf(a->default_series, sizeof a->default_series, "%s", "A");
- a->attachment_dir[0] = '\0';
resp = client_rpc(&a->conn, "settings.get", a->session, a->org, "{}");
if (resp && client_ok(resp)) {
char *ser = jstr_dup(resp, "result.default_series");
if (ser && *ser)
snprintf(a->default_series, sizeof a->default_series, "%s", ser);
free(ser);
- char *dir = jstr_dup(resp, "result.attachment_dir");
- if (dir && *dir)
- snprintf(a->attachment_dir, sizeof a->attachment_dir, "%s", dir);
- free(dir);
}
free(resp);
@@ -332,14 +327,18 @@ static void expand_path(const char *in, char *out, size_t n)
Returns a malloc'd path, or NULL on cancel. */
char *file_browser(struct app *a, const char *start_dir)
{
- (void)a;
if (g_quit)
return NULL;
char dir[1024];
- const char *start = (start_dir && *start_dir) ? start_dir : getenv("HOME");
+ const char *home = getenv("HOME");
+ const char *start = (start_dir && *start_dir) ? start_dir : home;
expand_path(start, dir, sizeof dir);
- if (!dir[0])
- snprintf(dir, sizeof dir, ".");
+ struct stat sb;
+ if (!dir[0] || stat(dir, &sb) != 0 || !S_ISDIR(sb.st_mode)) {
+ expand_path(home && *home ? home : ".", dir, sizeof dir);
+ if (!dir[0])
+ snprintf(dir, sizeof dir, ".");
+ }
for (;;) {
DIR *d = opendir(dir);
if (!d) {
@@ -408,6 +407,8 @@ char *file_browser(struct app *a, const char *start_dir)
continue;
}
free(ents);
+ snprintf(a->attachment_dir, sizeof a->attachment_dir, "%.255s", dir);
+ config_save(a);
return xstrdup(full);
}
}
@@ -442,6 +443,91 @@ static int bytes_look_text(const unsigned char *p, size_t n)
return 1;
}
+/* Directory for generated PDFs. */
+static void pdf_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 user-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 *title)
+{
+ const char *display = getenv("DISPLAY");
+ const char *wayland = getenv("WAYLAND_DISPLAY");
+ if ((!display || !*display) && (!wayland || !*wayland)) {
+ tui_message(title, "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(title, "Sparad: %s", path);
+}
+
+void pdf_save_and_open(const char *b64, const char *filename,
+ const char *title)
+{
+ char dir[512], path[700], safe[600];
+ pdf_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(title, "Sökvägen blir för lång.");
+ return;
+ }
+ config_mkdirs(path);
+ if (write_pdf_b64(b64, path) != 0) {
+ tui_message(title, "Kunde inte spara %s", path);
+ return;
+ }
+ open_pdf(path, title);
+}
+
/* Fetch one attachment, save it to a prompted path and verify its hash.
Text attachments are shown inline after saving. */
void attachment_download(struct app *a, int64_t att_id)