5 * \defgroup ManagePrefs Manage user preferences with a little help from the Citadel server.
10 #include "webserver.h"
15 * \brief display preferences dialog
17 void load_preferences(void) {
21 serv_printf("GOTO %s", USERCONFIGROOM);
22 serv_getln(buf, sizeof buf);
23 if (buf[0] != '2') return;
25 serv_puts("MSGS ALL|0|1");
26 serv_getln(buf, sizeof buf);
28 serv_puts("subj|__ WebCit Preferences __");
31 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
36 serv_printf("MSG0 %ld", msgnum);
37 serv_getln(buf, sizeof buf);
39 while (serv_getln(buf, sizeof buf),
40 (strcmp(buf, "text") && strcmp(buf, "000"))) {
42 if (!strcmp(buf, "text")) {
43 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
44 if (WC->preferences == NULL) {
45 WC->preferences = malloc(SIZ);
46 strcpy(WC->preferences, "");
49 WC->preferences = realloc(
51 strlen(WC->preferences)
55 strcat(WC->preferences, buf);
56 strcat(WC->preferences, "\n");
62 /** Go back to the room we're supposed to be in */
63 serv_printf("GOTO %s", WC->wc_roomname);
64 serv_getln(buf, sizeof buf);
68 * \brief Goto the user's configuration room, creating it if necessary.
69 * \return 0 on success or nonzero upon failure.
71 int goto_config_room(void) {
74 serv_printf("GOTO %s", USERCONFIGROOM);
75 serv_getln(buf, sizeof buf);
76 if (buf[0] != '2') { /* try to create the config room if not there */
77 serv_printf("CRE8 1|%s|4|0", USERCONFIGROOM);
78 serv_getln(buf, sizeof buf);
79 serv_printf("GOTO %s", USERCONFIGROOM);
80 serv_getln(buf, sizeof buf);
81 if (buf[0] != '2') return(1);
87 * \brief save the modifications
89 void save_preferences(void) {
93 if (goto_config_room() != 0) return; /* oh well. */
94 serv_puts("MSGS ALL|0|1");
95 serv_getln(buf, sizeof buf);
97 serv_puts("subj|__ WebCit Preferences __");
100 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
105 serv_printf("DELE %ld", msgnum);
106 serv_getln(buf, sizeof buf);
109 serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
110 serv_getln(buf, sizeof buf);
112 serv_puts(WC->preferences);
117 /** Go back to the room we're supposed to be in */
118 serv_printf("GOTO %s", WC->wc_roomname);
119 serv_getln(buf, sizeof buf);
123 * \brief query the actual setting of key in the citadel database
124 * \param key config key to query
125 * \param value value to the key to get
126 * \param value_len length of the value string
128 void get_preference(char *key, char *value, size_t value_len) {
136 num_prefs = num_tokens(WC->preferences, '\n');
137 for (i=0; i<num_prefs; ++i) {
138 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
139 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
140 if (!strcasecmp(thiskey, key)) {
141 extract_token(value, buf, 1, '|', value_len);
147 * \brief Write a key into the citadel settings database
148 * \param key key whichs value is to be modified
149 * \param value value to set
150 * \param save_to_server really write it????
152 void set_preference(char *key, char *value, int save_to_server) {
157 char *newprefs = NULL;
159 num_prefs = num_tokens(WC->preferences, '\n');
160 for (i=0; i<num_prefs; ++i) {
161 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
162 if (num_tokens(buf, '|') == 2) {
163 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
164 if (strcasecmp(thiskey, key)) {
165 if (newprefs == NULL) newprefs = strdup("");
166 newprefs = realloc(newprefs,
167 strlen(newprefs) + SIZ );
168 strcat(newprefs, buf);
169 strcat(newprefs, "\n");
175 if (newprefs == NULL) newprefs = strdup("");
176 newprefs = realloc(newprefs, strlen(newprefs) + SIZ);
177 sprintf(&newprefs[strlen(newprefs)], "%s|%s\n", key, value);
179 free(WC->preferences);
180 WC->preferences = newprefs;
182 if (save_to_server) save_preferences();
189 * \brief display form for changing your preferences and settings
191 void display_preferences(void)
193 output_headers(1, 1, 2, 0, 0, 0);
196 char calhourformat[16];
199 wprintf("<div id=\"banner\">\n");
200 wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
201 wprintf("<img src=\"static/advanpage2_48x.gif\" ALT=\" \" ALIGN=MIDDLE>");
202 wprintf("<SPAN CLASS=\"titlebar\"> ");
203 wprintf(_("Preferences and settings"));
204 wprintf("</SPAN></TD><TD ALIGN=RIGHT>");
206 wprintf("</TD></TR></TABLE>\n");
208 "<div id=\"content\">\n");
210 wprintf("<div class=\"fix_scrollbar_bug\">"
211 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
215 "<form name=\"prefform\" action=\"set_preferences\" "
217 "<table border=0 cellspacing=5 cellpadding=5>\n");
222 get_preference("roomlistview", buf, sizeof buf);
224 wprintf(_("Room list view"));
225 wprintf("</td><td>");
227 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
228 if (!strcasecmp(buf, "folders")) wprintf(" checked");
230 wprintf(_("Tree (folders) view"));
231 wprintf("<br></input>\n");
233 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
234 if (!strcasecmp(buf, "rooms")) wprintf(" checked");
236 wprintf(_("Table (rooms) view"));
237 wprintf("<br></input>\n");
239 wprintf("</td></tr>\n");
242 * Calendar hour format
244 get_preference("calhourformat", calhourformat, sizeof calhourformat);
245 if (calhourformat[0] == 0) strcpy(calhourformat, "12");
247 wprintf(_("Calendar hour format"));
248 wprintf("</td><td>");
250 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
251 if (!strcasecmp(calhourformat, "12")) wprintf(" checked");
253 wprintf(_("12 hour (am/pm)"));
254 wprintf("<br></input>\n");
256 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
257 if (!strcasecmp(calhourformat, "24")) wprintf(" checked");
259 wprintf(_("24 hour"));
260 wprintf("<br></input>\n");
262 wprintf("</td></tr>\n");
265 * Calendar day view -- day start time
267 get_preference("daystart", buf, sizeof buf);
268 if (buf[0] == 0) strcpy(buf, "8");
270 wprintf(_("Calendar day view begins at:"));
271 wprintf("</td><td>");
273 wprintf("<SELECT NAME=\"daystart\" SIZE=\"1\">\n");
274 for (i=0; i<=23; ++i) {
276 if (!strcasecmp(calhourformat, "24")) {
277 wprintf("<OPTION %s VALUE=\"%d\">%d:00</OPTION>\n",
278 ((atoi(buf) == i) ? "SELECTED" : ""),
283 wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
284 ((atoi(buf) == i) ? "SELECTED" : ""),
290 wprintf("</SELECT>\n");
291 wprintf("</td></tr>\n");
294 * Calendar day view -- day end time
296 get_preference("dayend", buf, sizeof buf);
297 if (buf[0] == 0) strcpy(buf, "17");
299 wprintf(_("Calendar day view ends at:"));
300 wprintf("</td><td>");
302 wprintf("<SELECT NAME=\"dayend\" SIZE=\"1\">\n");
303 for (i=0; i<=23; ++i) {
305 if (!strcasecmp(calhourformat, "24")) {
306 wprintf("<OPTION %s VALUE=\"%d\">%d:00</OPTION>\n",
307 ((atoi(buf) == i) ? "SELECTED" : ""),
312 wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
313 ((atoi(buf) == i) ? "SELECTED" : ""),
319 wprintf("</SELECT>\n");
320 wprintf("</td></tr>\n");
325 get_preference("use_sig", buf, sizeof buf);
326 if (buf[0] == 0) strcpy(buf, "no");
328 wprintf(_("Attach signature to email messages?"));
329 wprintf("</td><td>");
331 wprintf(" <script type=\"text/javascript\"> "
332 " function show_or_hide_sigbox() { "
333 " if ( $F('yes_sig') ) { "
334 " $('signature_box').style.display = 'inline'; "
337 " $('signature_box').style.display = 'none'; "
343 wprintf("<input type=\"radio\" id=\"no_sig\" name=\"use_sig\" VALUE=\"no\"");
344 if (!strcasecmp(buf, "no")) wprintf(" checked");
345 wprintf(" onChange=\"show_or_hide_sigbox();\" >");
346 wprintf(_("No signature"));
347 wprintf("<br></input>\n");
349 wprintf("<input type=\"radio\" id=\"yes_sig\" name=\"use_sig\" VALUE=\"yes\"");
350 if (!strcasecmp(buf, "yes")) wprintf(" checked");
351 wprintf(" onChange=\"show_or_hide_sigbox();\" >");
352 wprintf(_("Use this signature:"));
353 wprintf("<div id=\"signature_box\">"
354 "<br><textarea name=\"signature\" cols=\"40\" rows=\"5\">"
356 get_preference("signature", ebuf, sizeof ebuf);
357 euid_unescapize(buf, ebuf);
359 wprintf("</textarea>"
363 wprintf("<br></input>\n");
365 wprintf("</td></tr>\n");
367 wprintf(" <script type=\"text/javascript\"> "
368 " show_or_hide_sigbox(); "
374 "<input type=\"submit\" name=\"change_button\" value=\"%s\">"
376 "<INPUT type=\"submit\" name=\"cancel_button\" value=\"%s\">\n",
381 wprintf("</form></center>\n");
386 wprintf("</td></tr></table></div>\n");
391 * \brief Commit new preferences and settings
393 void set_preferences(void)
397 if (strlen(bstr("change_button")) == 0) {
398 safestrncpy(WC->ImportantMessage,
399 _("Cancelled. No settings were changed."),
400 sizeof WC->ImportantMessage);
406 * Set the last argument to 1 only for the final setting, so
407 * we don't send the prefs file to the server repeatedly
409 set_preference("roomlistview", bstr("roomlistview"), 0);
410 set_preference("calhourformat", bstr("calhourformat"), 0);
411 set_preference("use_sig", bstr("use_sig"), 0);
412 set_preference("daystart", bstr("daystart"), 0);
413 set_preference("dayend", bstr("dayend"), 0);
415 euid_escapize(ebuf, bstr("signature"));
416 set_preference("signature", ebuf, 1);