5 * \defgroup ManagePrefs Manage user preferences with a little help from the Citadel server.
6 * \ingroup CitadelConfig
11 #include "webserver.h"
19 * \brief display preferences dialog
21 void load_preferences(void) {
24 char key[SIZ], value[SIZ];
27 serv_printf("GOTO %s", USERCONFIGROOM);
28 serv_getln(buf, sizeof buf);
29 if (buf[0] != '2') return;
31 serv_puts("MSGS ALL|0|1");
32 serv_getln(buf, sizeof buf);
34 serv_puts("subj|__ WebCit Preferences __");
37 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
42 serv_printf("MSG0 %ld", msgnum);
43 serv_getln(buf, sizeof buf);
45 while (serv_getln(buf, sizeof buf),
46 (strcmp(buf, "text") && strcmp(buf, "000"))) {
48 if (!strcmp(buf, "text")) {
49 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
50 extract_token(key, buf, 0, '|', sizeof key);
51 extract_token(value, buf, 1, '|', sizeof value);
53 Put(WC->hash_prefs, key, strlen(key), strdup(value), free);
59 /** Go back to the room we're supposed to be in */
60 serv_printf("GOTO %s", WC->wc_roomname);
61 serv_getln(buf, sizeof buf);
65 * \brief Goto the user's configuration room, creating it if necessary.
66 * \return 0 on success or nonzero upon failure.
68 int goto_config_room(void) {
71 serv_printf("GOTO %s", USERCONFIGROOM);
72 serv_getln(buf, sizeof buf);
73 if (buf[0] != '2') { /* try to create the config room if not there */
74 serv_printf("CRE8 1|%s|4|0", USERCONFIGROOM);
75 serv_getln(buf, sizeof buf);
76 serv_printf("GOTO %s", USERCONFIGROOM);
77 serv_getln(buf, sizeof buf);
78 if (buf[0] != '2') return(1);
84 * \brief save the modifications
86 void save_preferences(void) {
90 if (goto_config_room() != 0) return; /* oh well. */
91 serv_puts("MSGS ALL|0|1");
92 serv_getln(buf, sizeof buf);
94 serv_puts("subj|__ WebCit Preferences __");
97 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
102 serv_printf("DELE %ld", msgnum);
103 serv_getln(buf, sizeof buf);
106 serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
107 serv_getln(buf, sizeof buf);
115 Hash = WC->hash_prefs;
117 HashPos = GetNewHashPos();
118 printf("_______> writing entries\n");
119 while (GetNextHashPos(Hash, HashPos, &len, &Key, (void**)&Value)!=0)
121 printf("__________>%s -> %s \n", Key, Value);
122 serv_printf("%s|%s", Key, Value);
126 DeleteHashPos(&HashPos);
129 /** Go back to the room we're supposed to be in */
130 serv_printf("GOTO %s", WC->wc_roomname);
131 serv_getln(buf, sizeof buf);
135 * \brief query the actual setting of key in the citadel database
136 * \param key config key to query
137 * \param value value to the key to get
138 * \param value_len length of the value string
140 void get_preference(char *key, char *value, size_t value_len) {
141 void *hash_value = NULL;
144 PrintHash(WC->hash_prefs);
145 if (GetHash(WC->hash_prefs, key, strlen(key), &hash_value) == 0)
149 safestrncpy(value, hash_value, value_len);
153 * \brief Write a key into the webcit preferences database for this user
155 * \params key key whichs value is to be modified
156 * \param value value to set
157 * \param save_to_server 1 = flush all data to the server, 0 = cache it for now
159 void set_preference(char *key, char *value, int save_to_server) {
161 Put(WC->hash_prefs, key, strlen(key), strdup(value), free);
163 if (save_to_server) save_preferences();
170 * \brief display form for changing your preferences and settings
172 void display_preferences(void)
174 output_headers(1, 1, 1, 0, 0, 0);
183 time_format = get_time_format_cached ();
185 wprintf("<div class=\"box\">\n");
186 wprintf("<div class=\"boxlabel\">");
187 wprintf(_("Preferences and settings"));
190 wprintf("<div class=\"boxcontent\">");
193 wprintf("<form name=\"prefform\" action=\"set_preferences\" "
194 "method=\"post\">\n");
195 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
198 wprintf("<table class=\"altern\">\n");
203 get_preference("roomlistview", buf, sizeof buf);
204 wprintf("<tr class=\"even\"><td>");
205 wprintf(_("Room list view"));
206 wprintf("</td><td>");
208 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
209 if (!strcasecmp(buf, "folders")) wprintf(" checked");
211 wprintf(_("Tree (folders) view"));
212 wprintf("</input> ");
214 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
215 if (!strcasecmp(buf, "rooms")) wprintf(" checked");
217 wprintf(_("Table (rooms) view"));
218 wprintf("</input>\n");
220 wprintf("</td></tr>\n");
226 wprintf("<tr class=\"odd\"><td>");
227 wprintf(_("Time format"));
228 wprintf("</td><td>");
230 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
231 if (time_format == WC_TIMEFORMAT_AMPM)
234 wprintf(_("12 hour (am/pm)"));
235 wprintf("</input> ");
237 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
238 if (time_format == WC_TIMEFORMAT_24)
241 wprintf(_("24 hour"));
242 wprintf("</input>\n");
244 wprintf("</td></tr>\n");
247 * Calendar day view -- day start time
249 get_preference("daystart", buf, sizeof buf);
250 if (buf[0] == 0) strcpy(buf, "8");
251 wprintf("<tr class=\"even\"><td>");
252 wprintf(_("Calendar day view begins at:"));
253 wprintf("</td><td>");
255 wprintf("<select name=\"daystart\" size=\"1\">\n");
256 for (i=0; i<=23; ++i) {
258 if (time_format == WC_TIMEFORMAT_24) {
259 wprintf("<option %s value=\"%d\">%d:00</option>\n",
260 ((atoi(buf) == i) ? "selected" : ""),
265 wprintf("<option %s value=\"%d\">%s</option>\n",
266 ((atoi(buf) == i) ? "selected" : ""),
272 wprintf("</select>\n");
273 wprintf("</td></tr>\n");
276 * Calendar day view -- day end time
278 get_preference("dayend", buf, sizeof buf);
279 if (buf[0] == 0) strcpy(buf, "17");
280 wprintf("<tr class=\"odd\"><td>");
281 wprintf(_("Calendar day view ends at:"));
282 wprintf("</td><td>");
284 wprintf("<select name=\"dayend\" size=\"1\">\n");
285 for (i=0; i<=23; ++i) {
287 if (time_format == WC_TIMEFORMAT_24) {
288 wprintf("<option %s value=\"%d\">%d:00</option>\n",
289 ((atoi(buf) == i) ? "selected" : ""),
294 wprintf("<option %s value=\"%d\">%s</option>\n",
295 ((atoi(buf) == i) ? "selected" : ""),
301 wprintf("</select>\n");
302 wprintf("</td></tr>\n");
305 * Day of week to begin calendar month view
307 get_preference("weekstart", buf, sizeof buf);
308 if (buf[0] == 0) strcpy(buf, "17");
309 wprintf("<tr class=\"even\"><td>");
310 wprintf(_("Week starts on:"));
311 wprintf("</td><td>");
313 wprintf("<select name=\"weekstart\" size=\"1\">\n");
315 for (i=0; i<=1; ++i) {
317 localtime_r(&tt, &tm);
319 wc_strftime(daylabel, sizeof daylabel, "%A", &tm);
321 wprintf("<option %s value=\"%d\">%s</option>\n",
322 ((atoi(buf) == i) ? "selected" : ""),
327 wprintf("</select>\n");
328 wprintf("</td></tr>\n");
333 get_preference("use_sig", buf, sizeof buf);
334 if (buf[0] == 0) strcpy(buf, "no");
335 wprintf("<tr class=\"odd\"><td>");
336 wprintf(_("Attach signature to email messages?"));
337 wprintf("</td><td>");
339 wprintf(" <script type=\"text/javascript\"> "
340 " function show_or_hide_sigbox() { "
341 " if ( $F('yes_sig') ) { "
342 " $('signature_box').style.display = 'inline'; "
345 " $('signature_box').style.display = 'none'; "
351 wprintf("<input type=\"radio\" id=\"no_sig\" name=\"use_sig\" VALUE=\"no\"");
352 if (!strcasecmp(buf, "no")) wprintf(" checked");
353 wprintf(" onChange=\"show_or_hide_sigbox();\" >");
354 wprintf(_("No signature"));
355 wprintf("</input> , \n");
357 wprintf("<input type=\"radio\" id=\"yes_sig\" name=\"use_sig\" VALUE=\"yes\"");
358 if (!strcasecmp(buf, "yes")) wprintf(" checked");
359 wprintf(" onChange=\"show_or_hide_sigbox();\" >");
360 wprintf(_("Use this signature:"));
361 wprintf("<div id=\"signature_box\">"
362 "<br><textarea name=\"signature\" cols=\"40\" rows=\"5\">"
364 get_preference("signature", ebuf, sizeof ebuf);
365 euid_unescapize(buf, ebuf);
367 wprintf("</textarea>"
371 wprintf("</input>\n");
373 wprintf("</td></tr>\n");
375 wprintf(" <script type=\"text/javascript\"> "
376 " show_or_hide_sigbox(); "
380 /** Character set to assume is in use for improperly encoded headers */
381 get_preference("default_header_charset", buf, sizeof buf);
382 if (buf[0] == 0) strcpy(buf, "UTF-8");
383 wprintf("<tr class=\"even\"><td>");
384 wprintf(_("Default character set for email headers:"));
385 wprintf("</td><td>");
386 wprintf("<input type=\"text\" NAME=\"default_header_charset\" MAXLENGTH=\"32\" VALUE=\"");
389 wprintf("</td></tr>");
395 get_preference("emptyfloors", buf, sizeof buf);
396 if (buf[0] == 0) strcpy(buf, "no");
397 wprintf("<tr class=\"odd\"><td>");
398 wprintf(_("Show empty floors"));
399 wprintf("</td><td>");
401 wprintf("<input type=\"radio\" name=\"emptyfloors\" VALUE=\"yes\"");
402 if (!strcasecmp(buf, "yes")) wprintf(" checked");
405 wprintf("</input> ");
407 wprintf("<input type=\"radio\" name=\"emptyfloors\" VALUE=\"no\"");
408 if (!strcasecmp(buf, "no")) wprintf(" checked");
411 wprintf("</input>\n");
413 wprintf("</td></tr>\n");
416 wprintf("</table>\n");
418 /** submit buttons */
419 wprintf("<div class=\"buttons\"> ");
420 wprintf("<input type=\"submit\" name=\"change_button\" value=\"%s\">"
422 "<INPUT type=\"submit\" name=\"cancel_button\" value=\"%s\">\n",
429 wprintf("</form>\n");
435 * \brief Commit new preferences and settings
437 void set_preferences(void)
441 int *time_format_cache;
443 time_format_cache = &(WC->time_format_cache);
445 if (IsEmptyStr(bstr("change_button"))) {
446 safestrncpy(WC->ImportantMessage,
447 _("Cancelled. No settings were changed."),
448 sizeof WC->ImportantMessage);
454 * Set the last argument to 1 only for the final setting, so
455 * we don't send the prefs file to the server repeatedly
457 set_preference("roomlistview", bstr("roomlistview"), 0);
458 fmt = bstr("calhourformat");
459 set_preference("calhourformat", fmt, 0);
460 if (!strcasecmp(fmt, "24"))
461 *time_format_cache = WC_TIMEFORMAT_24;
463 *time_format_cache = WC_TIMEFORMAT_AMPM;
465 set_preference("weekstart", bstr("weekstart"), 0);
466 set_preference("use_sig", bstr("use_sig"), 0);
467 set_preference("daystart", bstr("daystart"), 0);
468 set_preference("dayend", bstr("dayend"), 0);
469 set_preference("default_header_charset", bstr("default_header_charset"), 0);
470 set_preference("emptyfloors", bstr("emptyfloors"), 0);
472 euid_escapize(ebuf, bstr("signature"));
473 set_preference("signature", ebuf, 1);