5 * \defgroup ManagePrefs Manage user preferences with a little help from the Citadel server.
6 * \ingroup CitadelConfig
11 #include "webserver.h"
16 * \brief display preferences dialog
18 void load_preferences(void) {
22 serv_printf("GOTO %s", USERCONFIGROOM);
23 serv_getln(buf, sizeof buf);
24 if (buf[0] != '2') return;
26 serv_puts("MSGS ALL|0|1");
27 serv_getln(buf, sizeof buf);
29 serv_puts("subj|__ WebCit Preferences __");
32 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
37 serv_printf("MSG0 %ld", msgnum);
38 serv_getln(buf, sizeof buf);
40 while (serv_getln(buf, sizeof buf),
41 (strcmp(buf, "text") && strcmp(buf, "000"))) {
43 if (!strcmp(buf, "text")) {
44 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
45 if (WC->preferences == NULL) {
46 WC->preferences = malloc(SIZ);
47 strcpy(WC->preferences, "");
50 WC->preferences = realloc(
52 strlen(WC->preferences)
56 strcat(WC->preferences, buf);
57 strcat(WC->preferences, "\n");
63 /** Go back to the room we're supposed to be in */
64 serv_printf("GOTO %s", WC->wc_roomname);
65 serv_getln(buf, sizeof buf);
69 * \brief Goto the user's configuration room, creating it if necessary.
70 * \return 0 on success or nonzero upon failure.
72 int goto_config_room(void) {
75 serv_printf("GOTO %s", USERCONFIGROOM);
76 serv_getln(buf, sizeof buf);
77 if (buf[0] != '2') { /* try to create the config room if not there */
78 serv_printf("CRE8 1|%s|4|0", USERCONFIGROOM);
79 serv_getln(buf, sizeof buf);
80 serv_printf("GOTO %s", USERCONFIGROOM);
81 serv_getln(buf, sizeof buf);
82 if (buf[0] != '2') return(1);
88 * \brief save the modifications
90 void save_preferences(void) {
94 if (goto_config_room() != 0) return; /* oh well. */
95 serv_puts("MSGS ALL|0|1");
96 serv_getln(buf, sizeof buf);
98 serv_puts("subj|__ WebCit Preferences __");
101 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
106 serv_printf("DELE %ld", msgnum);
107 serv_getln(buf, sizeof buf);
110 serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
111 serv_getln(buf, sizeof buf);
113 serv_puts(WC->preferences);
118 /** Go back to the room we're supposed to be in */
119 serv_printf("GOTO %s", WC->wc_roomname);
120 serv_getln(buf, sizeof buf);
124 * \brief query the actual setting of key in the citadel database
125 * \param key config key to query
126 * \param value value to the key to get
127 * \param value_len length of the value string
129 void get_preference(char *key, char *value, size_t value_len) {
137 num_prefs = num_tokens(WC->preferences, '\n');
138 for (i=0; i<num_prefs; ++i) {
139 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
140 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
141 if (!strcasecmp(thiskey, key)) {
142 extract_token(value, buf, 1, '|', value_len);
148 * \brief Write a key into the webcit preferences database for this user
150 * \params key key whichs value is to be modified
151 * \param value value to set
152 * \param save_to_server 1 = flush all data to the server, 0 = cache it for now
154 void set_preference(char *key, char *value, int save_to_server) {
159 char *newprefs = NULL;
160 size_t newprefs_len = 0;
162 newprefs_len = strlen(key) + strlen(value) + 10;
163 if (WC->preferences != NULL) newprefs_len += strlen(WC->preferences);
164 newprefs = malloc(newprefs_len);
165 if (newprefs == NULL) return;
166 strcpy(newprefs, "");
168 num_prefs = num_tokens(WC->preferences, '\n');
169 for (i=0; i<num_prefs; ++i) {
170 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
171 if (num_tokens(buf, '|') == 2) {
172 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
173 if (strcasecmp(thiskey, key)) {
174 strcat(newprefs, buf);
175 strcat(newprefs, "\n");
180 sprintf(&newprefs[strlen(newprefs)], "%s|%s\n", key, value);
181 free(WC->preferences);
182 WC->preferences = newprefs;
184 if (save_to_server) save_preferences();
191 * \brief display form for changing your preferences and settings
193 void display_preferences(void)
195 output_headers(1, 1, 1, 0, 0, 0);
204 time_format = get_time_format_cached ();
206 wprintf("<div class=\"box\">\n");
207 wprintf("<div class=\"boxlabel\">");
208 wprintf(_("Preferences and settings"));
211 wprintf("<div class=\"boxcontent\">");
214 wprintf("<form name=\"prefform\" action=\"set_preferences\" "
215 "method=\"post\">\n");
216 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
219 wprintf("<table class=\"altern\">\n");
224 get_preference("roomlistview", buf, sizeof buf);
225 wprintf("<tr class=\"even\"><td>");
226 wprintf(_("Room list view"));
227 wprintf("</td><td>");
229 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
230 if (!strcasecmp(buf, "folders")) wprintf(" checked");
232 wprintf(_("Tree (folders) view"));
233 wprintf("</input> ");
235 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
236 if (!strcasecmp(buf, "rooms")) wprintf(" checked");
238 wprintf(_("Table (rooms) view"));
239 wprintf("</input>\n");
241 wprintf("</td></tr>\n");
247 wprintf("<tr class=\"odd\"><td>");
248 wprintf(_("Time format"));
249 wprintf("</td><td>");
251 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
252 if (time_format == WC_TIMEFORMAT_AMPM)
255 wprintf(_("12 hour (am/pm)"));
256 wprintf("</input> ");
258 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
259 if (time_format == WC_TIMEFORMAT_24)
262 wprintf(_("24 hour"));
263 wprintf("</input>\n");
265 wprintf("</td></tr>\n");
268 * Calendar day view -- day start time
270 get_preference("daystart", buf, sizeof buf);
271 if (buf[0] == 0) strcpy(buf, "8");
272 wprintf("<tr class=\"even\"><td>");
273 wprintf(_("Calendar day view begins at:"));
274 wprintf("</td><td>");
276 wprintf("<select name=\"daystart\" size=\"1\">\n");
277 for (i=0; i<=23; ++i) {
279 if (time_format == WC_TIMEFORMAT_24) {
280 wprintf("<option %s value=\"%d\">%d:00</option>\n",
281 ((atoi(buf) == i) ? "selected" : ""),
286 wprintf("<option %s value=\"%d\">%s</option>\n",
287 ((atoi(buf) == i) ? "selected" : ""),
293 wprintf("</select>\n");
294 wprintf("</td></tr>\n");
297 * Calendar day view -- day end time
299 get_preference("dayend", buf, sizeof buf);
300 if (buf[0] == 0) strcpy(buf, "17");
301 wprintf("<tr class=\"odd\"><td>");
302 wprintf(_("Calendar day view ends at:"));
303 wprintf("</td><td>");
305 wprintf("<select name=\"dayend\" size=\"1\">\n");
306 for (i=0; i<=23; ++i) {
308 if (time_format == WC_TIMEFORMAT_24) {
309 wprintf("<option %s value=\"%d\">%d:00</option>\n",
310 ((atoi(buf) == i) ? "selected" : ""),
315 wprintf("<option %s value=\"%d\">%s</option>\n",
316 ((atoi(buf) == i) ? "selected" : ""),
322 wprintf("</select>\n");
323 wprintf("</td></tr>\n");
326 * Day of week to begin calendar month view
328 get_preference("weekstart", buf, sizeof buf);
329 if (buf[0] == 0) strcpy(buf, "17");
330 wprintf("<tr class=\"even\"><td>");
331 wprintf(_("Week starts on:"));
332 wprintf("</td><td>");
334 wprintf("<select name=\"weekstart\" size=\"1\">\n");
336 for (i=0; i<=1; ++i) {
338 localtime_r(&tt, &tm);
340 wc_strftime(daylabel, sizeof daylabel, "%A", &tm);
342 wprintf("<option %s value=\"%d\">%s</option>\n",
343 ((atoi(buf) == i) ? "selected" : ""),
348 wprintf("</select>\n");
349 wprintf("</td></tr>\n");
354 get_preference("use_sig", buf, sizeof buf);
355 if (buf[0] == 0) strcpy(buf, "no");
356 wprintf("<tr class=\"odd\"><td>");
357 wprintf(_("Attach signature to email messages?"));
358 wprintf("</td><td>");
360 wprintf(" <script type=\"text/javascript\"> "
361 " function show_or_hide_sigbox() { "
362 " if ( $F('yes_sig') ) { "
363 " $('signature_box').style.display = 'inline'; "
366 " $('signature_box').style.display = 'none'; "
372 wprintf("<input type=\"radio\" id=\"no_sig\" name=\"use_sig\" VALUE=\"no\"");
373 if (!strcasecmp(buf, "no")) wprintf(" checked");
374 wprintf(" onChange=\"show_or_hide_sigbox();\" >");
375 wprintf(_("No signature"));
376 wprintf("</input> , \n");
378 wprintf("<input type=\"radio\" id=\"yes_sig\" name=\"use_sig\" VALUE=\"yes\"");
379 if (!strcasecmp(buf, "yes")) wprintf(" checked");
380 wprintf(" onChange=\"show_or_hide_sigbox();\" >");
381 wprintf(_("Use this signature:"));
382 wprintf("<div id=\"signature_box\">"
383 "<br><textarea name=\"signature\" cols=\"40\" rows=\"5\">"
385 get_preference("signature", ebuf, sizeof ebuf);
386 euid_unescapize(buf, ebuf);
388 wprintf("</textarea>"
392 wprintf("</input>\n");
394 wprintf("</td></tr>\n");
396 wprintf(" <script type=\"text/javascript\"> "
397 " show_or_hide_sigbox(); "
401 /** Character set to assume is in use for improperly encoded headers */
402 get_preference("default_header_charset", buf, sizeof buf);
403 if (buf[0] == 0) strcpy(buf, "UTF-8");
404 wprintf("<tr class=\"even\"><td>");
405 wprintf(_("Default character set for email headers:"));
406 wprintf("</td><td>");
407 wprintf("<input type=\"text\" NAME=\"default_header_charset\" MAXLENGTH=\"32\" VALUE=\"");
410 wprintf("</td></tr>");
416 get_preference("emptyfloors", buf, sizeof buf);
417 if (buf[0] == 0) strcpy(buf, "no");
418 wprintf("<tr class=\"odd\"><td>");
419 wprintf(_("Show empty floors"));
420 wprintf("</td><td>");
422 wprintf("<input type=\"radio\" name=\"emptyfloors\" VALUE=\"yes\"");
423 if (!strcasecmp(buf, "yes")) wprintf(" checked");
426 wprintf("</input> ");
428 wprintf("<input type=\"radio\" name=\"emptyfloors\" VALUE=\"no\"");
429 if (!strcasecmp(buf, "no")) wprintf(" checked");
432 wprintf("</input>\n");
434 wprintf("</td></tr>\n");
437 wprintf("</table>\n");
439 /** submit buttons */
440 wprintf("<div class=\"buttons\"> ");
441 wprintf("<input type=\"submit\" name=\"change_button\" value=\"%s\">"
443 "<INPUT type=\"submit\" name=\"cancel_button\" value=\"%s\">\n",
450 wprintf("</form>\n");
456 * \brief Commit new preferences and settings
458 void set_preferences(void)
462 int *time_format_cache;
464 time_format_cache = &(WC->time_format_cache);
466 if (IsEmptyStr(bstr("change_button"))) {
467 safestrncpy(WC->ImportantMessage,
468 _("Cancelled. No settings were changed."),
469 sizeof WC->ImportantMessage);
475 * Set the last argument to 1 only for the final setting, so
476 * we don't send the prefs file to the server repeatedly
478 set_preference("roomlistview", bstr("roomlistview"), 0);
479 fmt = bstr("calhourformat");
480 set_preference("calhourformat", fmt, 0);
481 if (!strcasecmp(fmt, "24"))
482 *time_format_cache = WC_TIMEFORMAT_24;
484 *time_format_cache = WC_TIMEFORMAT_AMPM;
486 set_preference("weekstart", bstr("weekstart"), 0);
487 set_preference("use_sig", bstr("use_sig"), 0);
488 set_preference("daystart", bstr("daystart"), 0);
489 set_preference("dayend", bstr("dayend"), 0);
490 set_preference("default_header_charset", bstr("default_header_charset"), 0);
491 set_preference("emptyfloors", bstr("emptyfloors"), 0);
493 euid_escapize(ebuf, bstr("signature"));
494 set_preference("signature", ebuf, 1);