Download
jcs
/amend
/settings.c
(View History)
jcs browser: Speed up repo loads by only loading newest 100 amendments | Latest amendment: 249 on 2023-05-04 |
1 | /* |
2 | * Copyright (c) 2021 joshua stein <jcs@jcs.org> |
3 | * |
4 | * Permission to use, copy, modify, and distribute this software for any |
5 | * purpose with or without fee is hereby granted, provided that the above |
6 | * copyright notice and this permission notice appear in all copies. |
7 | * |
8 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
9 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
10 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
11 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
12 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
13 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
14 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
15 | */ |
16 | |
17 | #include <stdio.h> |
18 | #include <string.h> |
19 | |
20 | #include "amend.h" |
21 | #include "settings.h" |
22 | #include "tetab.h" |
23 | #include "util.h" |
24 | |
25 | struct settings settings = { 0 }; |
26 | |
27 | void |
28 | settings_load(void) |
29 | { |
30 | char *author; |
31 | |
32 | author = xGetStringAsChar(STR_AUTHOR_ID); |
33 | strlcpy(settings.author, author, sizeof(settings.author)); |
34 | xfree(&author); |
35 | if (settings.author[0] == '\0') |
36 | snprintf(settings.author, sizeof(settings.author), "unknown"); |
37 | |
38 | settings.tabwidth = (short)xGetStringAsLong(STR_TABWIDTH_ID); |
39 | if (settings.tabwidth < 1 || settings.tabwidth > 20) |
40 | settings.tabwidth = 4; |
41 | |
42 | settings.max_amendments = (short)xGetStringAsLong(STR_MAX_AMENDMENTS_ID); |
43 | if (settings.max_amendments < 0) |
44 | settings.max_amendments = 0; |
45 | |
46 | TETabWidth = settings.tabwidth; |
47 | } |
48 | |
49 | void |
50 | settings_save(void) |
51 | { |
52 | Handle res; |
53 | size_t l; |
54 | char tmp[8]; |
55 | |
56 | res = xGetResource('STR ', STR_AUTHOR_ID); |
57 | HLock(res); |
58 | l = strlen(settings.author); |
59 | xSetHandleSize(res, l + 1); |
60 | memcpy(*res, settings.author, l + 1); |
61 | CtoPstr(*res); |
62 | ChangedResource(res); |
63 | ReleaseResource(res); |
64 | |
65 | res = xGetResource('STR ', STR_TABWIDTH_ID); |
66 | HLock(res); |
67 | snprintf(tmp, sizeof(tmp), "%d", settings.tabwidth); |
68 | l = strlen(tmp); |
69 | xSetHandleSize(res, l + 1); |
70 | memcpy(*res, tmp, l + 1); |
71 | CtoPstr(*res); |
72 | ChangedResource(res); |
73 | ReleaseResource(res); |
74 | |
75 | res = xGetResource('STR ', STR_MAX_AMENDMENTS_ID); |
76 | HLock(res); |
77 | snprintf(tmp, sizeof(tmp), "%d", settings.max_amendments); |
78 | l = strlen(tmp); |
79 | xSetHandleSize(res, l + 1); |
80 | memcpy(*res, tmp, l + 1); |
81 | CtoPstr(*res); |
82 | ChangedResource(res); |
83 | ReleaseResource(res); |
84 | |
85 | UpdateResFile(CurResFile()); |
86 | if (ResError()) |
87 | warn("UpdateResFile failed! %d", ResError()); |
88 | |
89 | TETabWidth = settings.tabwidth; |
90 | } |
91 | |
92 | void |
93 | settings_edit(void) |
94 | { |
95 | Str255 txt; |
96 | Handle ihandle; |
97 | short itype, hit; |
98 | bool done; |
99 | DialogPtr dlg; |
100 | Rect irect; |
101 | |
102 | dlg = GetNewDialog(SETTINGS_DLOG_ID, nil, (WindowPtr)-1); |
103 | if (dlg == NULL) |
104 | err(1, "Can't find settings DLOG %d", SETTINGS_DLOG_ID); |
105 | |
106 | GetDItem(dlg, SETTINGS_AUTHOR_ID, &itype, &ihandle, &irect); |
107 | strlcpy((char *)txt, settings.author, sizeof(txt)); |
108 | CtoPstr(txt); |
109 | SetIText(ihandle, txt); |
110 | |
111 | GetDItem(dlg, SETTINGS_TABWIDTH_ID, &itype, &ihandle, &irect); |
112 | snprintf((char *)txt, sizeof(txt), "%d", settings.tabwidth); |
113 | CtoPstr(txt); |
114 | SetIText(ihandle, txt); |
115 | |
116 | GetDItem(dlg, SETTINGS_MAX_AMENDMENTS_ID, &itype, &ihandle, &irect); |
117 | snprintf((char *)txt, sizeof(txt), "%d", settings.max_amendments); |
118 | CtoPstr(txt); |
119 | SetIText(ihandle, txt); |
120 | |
121 | ShowWindow(dlg); |
122 | |
123 | dialog_wait: |
124 | done = false; |
125 | while (!done) { |
126 | ModalDialog(nil, &hit); |
127 | switch (hit) { |
128 | case SETTINGS_SAVE_ID: |
129 | done = true; |
130 | break; |
131 | case SETTINGS_CANCEL_ID: |
132 | goto dialog_close; |
133 | } |
134 | } |
135 | |
136 | GetDItem(dlg, SETTINGS_AUTHOR_ID, &itype, &ihandle, &irect); |
137 | GetIText(ihandle, txt); |
138 | PtoCstr(txt); |
139 | if (strlen((char *)&txt) >= sizeof(settings.author)) { |
140 | warn("Author setting is too long (%ld max)", |
141 | sizeof(settings.author) - 1); |
142 | goto dialog_wait; |
143 | } |
144 | |
145 | memcpy(settings.author, txt, strlen((char *)&txt) + 1); |
146 | |
147 | GetDItem(dlg, SETTINGS_TABWIDTH_ID, &itype, &ihandle, &irect); |
148 | GetIText(ihandle, txt); |
149 | PtoCstr(txt); |
150 | settings.tabwidth = atoi((char *)&txt); |
151 | if (settings.tabwidth < 1) |
152 | settings.tabwidth = 1; |
153 | HUnlock(ihandle); |
154 | |
155 | GetDItem(dlg, SETTINGS_MAX_AMENDMENTS_ID, &itype, &ihandle, &irect); |
156 | GetIText(ihandle, txt); |
157 | PtoCstr(txt); |
158 | settings.max_amendments = atoi((char *)&txt); |
159 | if (settings.max_amendments < 0) |
160 | settings.max_amendments = 0; |
161 | HUnlock(ihandle); |
162 | |
163 | settings_save(); |
164 | |
165 | dialog_close: |
166 | DisposeDialog(dlg); |
167 | } |