4 * Manage user preferences with a little help from the Citadel server.
13 void load_preferences(void) {
17 serv_printf("GOTO %s", USERCONFIGROOM);
18 serv_getln(buf, sizeof buf);
19 if (buf[0] != '2') return;
21 serv_puts("MSGS ALL|0|1");
22 serv_getln(buf, sizeof buf);
24 serv_puts("subj|__ WebCit Preferences __");
27 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
32 serv_printf("MSG0 %ld", msgnum);
33 serv_getln(buf, sizeof buf);
35 while (serv_getln(buf, sizeof buf),
36 (strcmp(buf, "text") && strcmp(buf, "000"))) {
38 if (!strcmp(buf, "text")) {
39 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
40 if (WC->preferences == NULL) {
41 WC->preferences = malloc(SIZ);
42 strcpy(WC->preferences, "");
45 WC->preferences = realloc(
47 strlen(WC->preferences)
51 strcat(WC->preferences, buf);
52 strcat(WC->preferences, "\n");
58 /* Go back to the room we're supposed to be in */
59 serv_printf("GOTO %s", WC->wc_roomname);
60 serv_getln(buf, sizeof buf);
64 * Goto the user's configuration room, creating it if necessary.
65 * Returns 0 on success or nonzero upon failure.
67 int goto_config_room(void) {
70 serv_printf("GOTO %s", USERCONFIGROOM);
71 serv_getln(buf, sizeof buf);
72 if (buf[0] != '2') { /* try to create the config room if not there */
73 serv_printf("CRE8 1|%s|4|0", USERCONFIGROOM);
74 serv_getln(buf, sizeof buf);
75 serv_printf("GOTO %s", USERCONFIGROOM);
76 serv_getln(buf, sizeof buf);
77 if (buf[0] != '2') return(1);
83 void save_preferences(void) {
87 if (goto_config_room() != 0) return; /* oh well. */
88 serv_puts("MSGS ALL|0|1");
89 serv_getln(buf, sizeof buf);
91 serv_puts("subj|__ WebCit Preferences __");
94 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
99 serv_printf("DELE %ld", msgnum);
100 serv_getln(buf, sizeof buf);
103 serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
104 serv_getln(buf, sizeof buf);
106 serv_puts(WC->preferences);
111 /* Go back to the room we're supposed to be in */
112 serv_printf("GOTO %s", WC->wc_roomname);
113 serv_getln(buf, sizeof buf);
116 void get_preference(char *key, char *value, size_t value_len) {
124 num_prefs = num_tokens(WC->preferences, '\n');
125 for (i=0; i<num_prefs; ++i) {
126 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
127 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
128 if (!strcasecmp(thiskey, key)) {
129 extract_token(value, buf, 1, '|', value_len);
134 void set_preference(char *key, char *value, int save_to_server) {
139 char *newprefs = NULL;
141 num_prefs = num_tokens(WC->preferences, '\n');
142 for (i=0; i<num_prefs; ++i) {
143 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
144 if (num_tokens(buf, '|') == 2) {
145 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
146 if (strcasecmp(thiskey, key)) {
147 if (newprefs == NULL) newprefs = strdup("");
148 newprefs = realloc(newprefs,
149 strlen(newprefs) + SIZ );
150 strcat(newprefs, buf);
151 strcat(newprefs, "\n");
157 if (newprefs == NULL) newprefs = strdup("");
158 newprefs = realloc(newprefs, strlen(newprefs) + SIZ);
159 sprintf(&newprefs[strlen(newprefs)], "%s|%s\n", key, value);
161 free(WC->preferences);
162 WC->preferences = newprefs;
164 if (save_to_server) save_preferences();
171 * display form for changing your preferences and settings
173 void display_preferences(void)
175 output_headers(1, 1, 2, 0, 0, 0, 0);
178 wprintf("<div id=\"banner\">\n");
179 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
180 wprintf("<IMG SRC=\"/static/advanpage2_48x.gif\" ALT=\" \" ALIGN=MIDDLE>");
181 wprintf("<SPAN CLASS=\"titlebar\"> ");
182 wprintf(_("Preferences and settings"));
183 wprintf("</SPAN></TD><TD ALIGN=RIGHT>");
185 wprintf("</TD></TR></TABLE>\n");
187 "<div id=\"content\">\n");
189 wprintf("<div id=\"fix_scrollbar_bug\">"
190 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
194 "<form name=\"prefform\" action=\"set_preferences\" "
196 "<table border=0 cellspacing=5 cellpadding=5>\n");
199 get_preference("roomlistview", buf, sizeof buf);
201 wprintf(_("Room list view"));
202 wprintf("</td><td>");
204 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
205 if (!strcasecmp(buf, "folders")) wprintf(" checked");
207 wprintf(_("Tree (folders) view"));
208 wprintf("<br></input>\n");
210 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
211 if (!strcasecmp(buf, "rooms")) wprintf(" checked");
213 wprintf(_("Table (rooms) view"));
214 wprintf("<br></input>\n");
216 wprintf("</td></tr>\n");
219 get_preference("calhourformat", buf, sizeof buf);
220 if (buf[0] == 0) strcpy(buf, "12");
222 wprintf(_("Calendar hour format"));
223 wprintf("</td><td>");
225 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
226 if (!strcasecmp(buf, "12")) wprintf(" checked");
228 wprintf(_("12 hour (am/pm)"));
229 wprintf("<br></input>\n");
231 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
232 if (!strcasecmp(buf, "24")) wprintf(" checked");
234 wprintf(_("24 hour"));
235 wprintf("<br></input>\n");
237 wprintf("</td></tr>\n");
240 "<input type=\"submit\" name=\"change_button\" value=\"%s\">"
242 "<INPUT type=\"submit\" name=\"cancel_button\" value=\"%s\">\n",
247 wprintf("</form></center>\n");
252 wprintf("</td></tr></table></div>\n");
257 * Commit new preferences and settings
259 void set_preferences(void)
261 if (strlen(bstr("change_button")) == 0) {
262 safestrncpy(WC->ImportantMessage,
263 _("Cancelled. No settings were changed."),
264 sizeof WC->ImportantMessage);
269 /* Set the last argument to 1 only for the final setting, so
270 * we don't send the prefs file to the server repeatedly
272 set_preference("roomlistview", bstr("roomlistview"), 0);
273 set_preference("calhourformat", bstr("calhourformat"), 1);