summaryrefslogtreecommitdiff
path: root/clients/drafts.h
blob: 2cc67dbedd297b648e4f4d57ae21979a28486182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef BOKF_DRAFTS_H
#define BOKF_DRAFTS_H

#include <stddef.h>
#include <stdint.h>

/* Client-local edit drafts: every non-committed edit is mirrored to
   $XDG_CACHE_HOME/bokf/drafts.json (mode 0600, atomic replace) so a reload,
   a crash or Ctrl+C never loses work. A record is keyed by
   (org, entity, id); a new entity uses a temporary id from
   drafts_new_id(). See docs/TUI-GUIDELINES.md "Interaction model". */

struct draft {
    int64_t org;
    char *entity;
    char *id;
    char *fields; /* JSON object text */
};

struct drafts {
    struct draft *v;
    size_t n, cap;
    char path[600];
};

void drafts_init(struct drafts *d);
/* Explicit path, for tests. */
void drafts_init_path(struct drafts *d, const char *path);
void drafts_free(struct drafts *d);

/* The draft's fields JSON, or NULL; the store owns the string. */
const char *drafts_get(const struct drafts *d, int64_t org,
                       const char *entity, const char *id);
int drafts_have(const struct drafts *d, int64_t org, const char *entity,
                const char *id);
/* Inserts or replaces the draft and persists the file. */
void drafts_put(struct drafts *d, int64_t org, const char *entity,
                const char *id, const char *fields);
void drafts_del(struct drafts *d, int64_t org, const char *entity,
                const char *id);
size_t drafts_count(const struct drafts *d, int64_t org, const char *entity);
void drafts_foreach(const struct drafts *d, int64_t org, const char *entity,
                    void (*cb)(void *ud, const char *id, const char *fields),
                    void *ud);

/* "ny-<time>-<seq>", unique within the process. Caller frees. */
char *drafts_new_id(void);

#endif