4 * Manage user preferences with a little help from the Citadel server.
12 inline const char *PrintPref(void *Prefstr)
18 * display preferences dialog
20 void load_preferences(void) {
23 char key[SIZ], value[SIZ];
25 serv_printf("GOTO %s", USERCONFIGROOM);
26 serv_getln(buf, sizeof buf);
27 if (buf[0] != '2') return;
29 serv_puts("MSGS ALL|0|1");
30 serv_getln(buf, sizeof buf);
32 serv_puts("subj|__ WebCit Preferences __");
35 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
40 serv_printf("MSG0 %ld", msgnum);
41 serv_getln(buf, sizeof buf);
43 while (serv_getln(buf, sizeof buf),
44 (strcmp(buf, "text") && strcmp(buf, "000"))) {
46 if (!strcmp(buf, "text")) {
47 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
48 extract_token(key, buf, 0, '|', sizeof key);
49 extract_token(value, buf, 1, '|', sizeof value);
51 Put(WC->hash_prefs, key, strlen(key), strdup(value), free);
57 /* Go back to the room we're supposed to be in */
58 serv_printf("GOTO %s", WC->wc_roomname);
59 serv_getln(buf, sizeof buf);
63 * \brief Goto the user's configuration room, creating it if necessary.
64 * \return 0 on success or nonzero upon failure.
66 int goto_config_room(void) {
69 serv_printf("GOTO %s", USERCONFIGROOM);
70 serv_getln(buf, sizeof buf);
71 if (buf[0] != '2') { /* try to create the config room if not there */
72 serv_printf("CRE8 1|%s|4|0", USERCONFIGROOM);
73 serv_getln(buf, sizeof buf);
74 serv_printf("GOTO %s", USERCONFIGROOM);
75 serv_getln(buf, sizeof buf);
76 if (buf[0] != '2') return(1);
82 * \brief save the modifications
84 void save_preferences(void) {
88 if (goto_config_room() != 0) return; /* oh well. */
89 serv_puts("MSGS ALL|0|1");
90 serv_getln(buf, sizeof buf);
92 serv_puts("subj|__ WebCit Preferences __");
95 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
100 serv_printf("DELE %ld", msgnum);
101 serv_getln(buf, sizeof buf);
104 serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
105 serv_getln(buf, sizeof buf);
113 Hash = WC->hash_prefs;
114 PrintHash(Hash, PrintPref, NULL);
115 HashPos = GetNewHashPos();
116 while (GetNextHashPos(Hash, HashPos, &len, &Key, (void**)&Value)!=0)
118 serv_printf("%s|%s", Key, Value);
122 DeleteHashPos(&HashPos);
125 /** Go back to the room we're supposed to be in */
126 serv_printf("GOTO %s", WC->wc_roomname);
127 serv_getln(buf, sizeof buf);
131 * \brief query the actual setting of key in the citadel database
132 * \param key config key to query
133 * \param value value to the key to get
134 * \param value_len length of the value string
136 void get_preference(char *key, char *value, size_t value_len) {
137 void *hash_value = NULL;
140 PrintHash(WC->hash_prefs, PrintPref, NULL);
141 if (GetHash(WC->hash_prefs, key, strlen(key), &hash_value) == 0)
145 safestrncpy(value, hash_value, value_len);
149 * \brief Write a key into the webcit preferences database for this user
151 * \params key key whichs value is to be modified
152 * \param value value to set
153 * \param save_to_server 1 = flush all data to the server, 0 = cache it for now
155 void set_preference(char *key, char *value, int save_to_server) {
157 Put(WC->hash_prefs, key, strlen(key), strdup(value), free);
159 if (save_to_server) save_preferences();
166 * \brief display form for changing your preferences and settings
168 void display_preferences(void)
170 output_headers(1, 1, 1, 0, 0, 0);
179 time_format = get_time_format_cached ();
181 wprintf("<div class=\"box\">\n");
182 wprintf("<div class=\"boxlabel\">");
183 wprintf(_("Preferences and settings"));
186 wprintf("<div class=\"boxcontent\">");
189 wprintf("<form name=\"prefform\" action=\"set_preferences\" "
190 "method=\"post\">\n");
191 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
194 wprintf("<table class=\"altern\">\n");
199 get_preference("roomlistview", buf, sizeof buf);
200 wprintf("<tr class=\"even\"><td>");
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("</input> ");
210 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
211 if (!strcasecmp(buf, "rooms")) wprintf(" checked");
213 wprintf(_("Table (rooms) view"));
214 wprintf("</input>\n");
216 wprintf("</td></tr>\n");
222 wprintf("<tr class=\"odd\"><td>");
223 wprintf(_("Time format"));
224 wprintf("</td><td>");
226 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
227 if (time_format == WC_TIMEFORMAT_AMPM)
230 wprintf(_("12 hour (am/pm)"));
231 wprintf("</input> ");
233 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
234 if (time_format == WC_TIMEFORMAT_24)
237 wprintf(_("24 hour"));
238 wprintf("</input>\n");
240 wprintf("</td></tr>\n");
243 * Calendar day view -- day start time
245 get_preference("daystart", buf, sizeof buf);
246 if (buf[0] == 0) strcpy(buf, "8");
247 wprintf("<tr class=\"even\"><td>");
248 wprintf(_("Calendar day view begins at:"));
249 wprintf("</td><td>");
251 wprintf("<select name=\"daystart\" size=\"1\">\n");
252 for (i=0; i<=23; ++i) {
254 if (time_format == WC_TIMEFORMAT_24) {
255 wprintf("<option %s value=\"%d\">%d:00</option>\n",
256 ((atoi(buf) == i) ? "selected" : ""),
261 wprintf("<option %s value=\"%d\">%s</option>\n",
262 ((atoi(buf) == i) ? "selected" : ""),
268 wprintf("</select>\n");
269 wprintf("</td></tr>\n");
272 * Calendar day view -- day end time
274 get_preference("dayend", buf, sizeof buf);
275 if (buf[0] == 0) strcpy(buf, "17");
276 wprintf("<tr class=\"odd\"><td>");
277 wprintf(_("Calendar day view ends at:"));
278 wprintf("</td><td>");
280 wprintf("<select name=\"dayend\" size=\"1\">\n");
281 for (i=0; i<=23; ++i) {
283 if (time_format == WC_TIMEFORMAT_24) {
284 wprintf("<option %s value=\"%d\">%d:00</option>\n",
285 ((atoi(buf) == i) ? "selected" : ""),
290 wprintf("<option %s value=\"%d\">%s</option>\n",
291 ((atoi(buf) == i) ? "selected" : ""),
297 wprintf("</select>\n");
298 wprintf("</td></tr>\n");
301 * Day of week to begin calendar month view
303 get_preference("weekstart", buf, sizeof buf);
304 if (buf[0] == 0) strcpy(buf, "17");
305 wprintf("<tr class=\"even\"><td>");
306 wprintf(_("Week starts on:"));
307 wprintf("</td><td>");
309 wprintf("<select name=\"weekstart\" size=\"1\">\n");
311 for (i=0; i<=1; ++i) {
313 localtime_r(&tt, &tm);
315 wc_strftime(daylabel, sizeof daylabel, "%A", &tm);
317 wprintf("<option %s value=\"%d\">%s</option>\n",
318 ((atoi(buf) == i) ? "selected" : ""),
323 wprintf("</select>\n");
324 wprintf("</td></tr>\n");
329 get_preference("use_sig", buf, sizeof buf);
330 if (buf[0] == 0) strcpy(buf, "no");
331 wprintf("<tr class=\"odd\"><td>");
332 wprintf(_("Attach signature to email messages?"));
333 wprintf("</td><td>");
335 wprintf(" <script type=\"text/javascript\"> "
336 " function show_or_hide_sigbox() { "
337 " if ( $F('yes_sig') ) { "
338 " $('signature_box').style.display = 'inline'; "
341 " $('signature_box').style.display = 'none'; "
347 wprintf("<input type=\"radio\" id=\"no_sig\" name=\"use_sig\" VALUE=\"no\"");
348 if (!strcasecmp(buf, "no")) wprintf(" checked");
349 wprintf(" onChange=\"show_or_hide_sigbox();\" >");
350 wprintf(_("No signature"));
351 wprintf("</input> , \n");
353 wprintf("<input type=\"radio\" id=\"yes_sig\" name=\"use_sig\" VALUE=\"yes\"");
354 if (!strcasecmp(buf, "yes")) wprintf(" checked");
355 wprintf(" onChange=\"show_or_hide_sigbox();\" >");
356 wprintf(_("Use this signature:"));
357 wprintf("<div id=\"signature_box\">"
358 "<br><textarea name=\"signature\" cols=\"40\" rows=\"5\">"
360 get_preference("signature", ebuf, sizeof ebuf);
361 euid_unescapize(buf, ebuf);
363 wprintf("</textarea>"
367 wprintf("</input>\n");
369 wprintf("</td></tr>\n");
371 wprintf(" <script type=\"text/javascript\"> "
372 " show_or_hide_sigbox(); "
376 /** Character set to assume is in use for improperly encoded headers */
377 get_preference("default_header_charset", buf, sizeof buf);
378 if (buf[0] == 0) strcpy(buf, "UTF-8");
379 wprintf("<tr class=\"even\"><td>");
380 wprintf(_("Default character set for email headers:"));
381 wprintf("</td><td>");
382 wprintf("<input type=\"text\" NAME=\"default_header_charset\" MAXLENGTH=\"32\" VALUE=\"");
385 wprintf("</td></tr>");
391 get_preference("emptyfloors", buf, sizeof buf);
392 if (buf[0] == 0) strcpy(buf, "no");
393 wprintf("<tr class=\"odd\"><td>");
394 wprintf(_("Show empty floors"));
395 wprintf("</td><td>");
397 wprintf("<input type=\"radio\" name=\"emptyfloors\" VALUE=\"yes\"");
398 if (!strcasecmp(buf, "yes")) wprintf(" checked");
401 wprintf("</input> ");
403 wprintf("<input type=\"radio\" name=\"emptyfloors\" VALUE=\"no\"");
404 if (!strcasecmp(buf, "no")) wprintf(" checked");
407 wprintf("</input>\n");
409 wprintf("</td></tr>\n");
412 wprintf("</table>\n");
414 /** submit buttons */
415 wprintf("<div class=\"buttons\"> ");
416 wprintf("<input type=\"submit\" name=\"change_button\" value=\"%s\">"
418 "<INPUT type=\"submit\" name=\"cancel_button\" value=\"%s\">\n",
425 wprintf("</form>\n");
431 * \brief Commit new preferences and settings
433 void set_preferences(void)
437 int *time_format_cache;
439 time_format_cache = &(WC->time_format_cache);
441 if (IsEmptyStr(bstr("change_button"))) {
442 safestrncpy(WC->ImportantMessage,
443 _("Cancelled. No settings were changed."),
444 sizeof WC->ImportantMessage);
450 * Set the last argument to 1 only for the final setting, so
451 * we don't send the prefs file to the server repeatedly
453 set_preference("roomlistview", bstr("roomlistview"), 0);
454 fmt = bstr("calhourformat");
455 set_preference("calhourformat", fmt, 0);
456 if (!strcasecmp(fmt, "24"))
457 *time_format_cache = WC_TIMEFORMAT_24;
459 *time_format_cache = WC_TIMEFORMAT_AMPM;
461 set_preference("weekstart", bstr("weekstart"), 0);
462 set_preference("use_sig", bstr("use_sig"), 0);
463 set_preference("daystart", bstr("daystart"), 0);
464 set_preference("dayend", bstr("dayend"), 0);
465 set_preference("default_header_charset", bstr("default_header_charset"), 0);
466 set_preference("emptyfloors", bstr("emptyfloors"), 0);
468 euid_escapize(ebuf, bstr("signature"));
469 set_preference("signature", ebuf, 1);