4 * Manage user preferences with a little help from the Citadel server.
13 * display preferences dialog
15 void load_preferences(void) {
18 char key[SIZ], value[SIZ];
20 serv_printf("GOTO %s", USERCONFIGROOM);
21 serv_getln(buf, sizeof buf);
22 if (buf[0] != '2') return;
24 serv_puts("MSGS ALL|0|1");
25 serv_getln(buf, sizeof buf);
27 serv_puts("subj|__ WebCit Preferences __");
30 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
35 serv_printf("MSG0 %ld", msgnum);
36 serv_getln(buf, sizeof buf);
38 while (serv_getln(buf, sizeof buf),
39 (strcmp(buf, "text") && strcmp(buf, "000"))) {
41 if (!strcmp(buf, "text")) {
42 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
43 extract_token(key, buf, 0, '|', sizeof key);
44 extract_token(value, buf, 1, '|', sizeof value);
46 Put(WC->hash_prefs, key, strlen(key), strdup(value), free);
52 /* Go back to the room we're supposed to be in */
53 serv_printf("GOTO %s", WC->wc_roomname);
54 serv_getln(buf, sizeof buf);
58 * \brief Goto the user's configuration room, creating it if necessary.
59 * \return 0 on success or nonzero upon failure.
61 int goto_config_room(void) {
64 serv_printf("GOTO %s", USERCONFIGROOM);
65 serv_getln(buf, sizeof buf);
66 if (buf[0] != '2') { /* try to create the config room if not there */
67 serv_printf("CRE8 1|%s|4|0", USERCONFIGROOM);
68 serv_getln(buf, sizeof buf);
69 serv_printf("GOTO %s", USERCONFIGROOM);
70 serv_getln(buf, sizeof buf);
71 if (buf[0] != '2') return(1);
77 * \brief save the modifications
79 void save_preferences(void) {
83 if (goto_config_room() != 0) return; /* oh well. */
84 serv_puts("MSGS ALL|0|1");
85 serv_getln(buf, sizeof buf);
87 serv_puts("subj|__ WebCit Preferences __");
90 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
95 serv_printf("DELE %ld", msgnum);
96 serv_getln(buf, sizeof buf);
99 serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
100 serv_getln(buf, sizeof buf);
108 Hash = WC->hash_prefs;
110 HashPos = GetNewHashPos();
111 while (GetNextHashPos(Hash, HashPos, &len, &Key, (void**)&Value)!=0)
113 serv_printf("%s|%s", Key, Value);
117 DeleteHashPos(&HashPos);
120 /** Go back to the room we're supposed to be in */
121 serv_printf("GOTO %s", WC->wc_roomname);
122 serv_getln(buf, sizeof buf);
126 * \brief query the actual setting of key in the citadel database
127 * \param key config key to query
128 * \param value value to the key to get
129 * \param value_len length of the value string
131 void get_preference(char *key, char *value, size_t value_len) {
132 void *hash_value = NULL;
135 PrintHash(WC->hash_prefs);
136 if (GetHash(WC->hash_prefs, key, strlen(key), &hash_value) == 0)
140 safestrncpy(value, hash_value, value_len);
144 * \brief Write a key into the webcit preferences database for this user
146 * \params key key whichs value is to be modified
147 * \param value value to set
148 * \param save_to_server 1 = flush all data to the server, 0 = cache it for now
150 void set_preference(char *key, char *value, int save_to_server) {
152 Put(WC->hash_prefs, key, strlen(key), strdup(value), free);
154 if (save_to_server) save_preferences();
161 * \brief display form for changing your preferences and settings
163 void display_preferences(void)
165 output_headers(1, 1, 1, 0, 0, 0);
174 time_format = get_time_format_cached ();
176 wprintf("<div class=\"box\">\n");
177 wprintf("<div class=\"boxlabel\">");
178 wprintf(_("Preferences and settings"));
181 wprintf("<div class=\"boxcontent\">");
184 wprintf("<form name=\"prefform\" action=\"set_preferences\" "
185 "method=\"post\">\n");
186 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
189 wprintf("<table class=\"altern\">\n");
194 get_preference("roomlistview", buf, sizeof buf);
195 wprintf("<tr class=\"even\"><td>");
196 wprintf(_("Room list view"));
197 wprintf("</td><td>");
199 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
200 if (!strcasecmp(buf, "folders")) wprintf(" checked");
202 wprintf(_("Tree (folders) view"));
203 wprintf("</input> ");
205 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
206 if (!strcasecmp(buf, "rooms")) wprintf(" checked");
208 wprintf(_("Table (rooms) view"));
209 wprintf("</input>\n");
211 wprintf("</td></tr>\n");
217 wprintf("<tr class=\"odd\"><td>");
218 wprintf(_("Time format"));
219 wprintf("</td><td>");
221 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
222 if (time_format == WC_TIMEFORMAT_AMPM)
225 wprintf(_("12 hour (am/pm)"));
226 wprintf("</input> ");
228 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
229 if (time_format == WC_TIMEFORMAT_24)
232 wprintf(_("24 hour"));
233 wprintf("</input>\n");
235 wprintf("</td></tr>\n");
238 * Calendar day view -- day start time
240 get_preference("daystart", buf, sizeof buf);
241 if (buf[0] == 0) strcpy(buf, "8");
242 wprintf("<tr class=\"even\"><td>");
243 wprintf(_("Calendar day view begins at:"));
244 wprintf("</td><td>");
246 wprintf("<select name=\"daystart\" size=\"1\">\n");
247 for (i=0; i<=23; ++i) {
249 if (time_format == WC_TIMEFORMAT_24) {
250 wprintf("<option %s value=\"%d\">%d:00</option>\n",
251 ((atoi(buf) == i) ? "selected" : ""),
256 wprintf("<option %s value=\"%d\">%s</option>\n",
257 ((atoi(buf) == i) ? "selected" : ""),
263 wprintf("</select>\n");
264 wprintf("</td></tr>\n");
267 * Calendar day view -- day end time
269 get_preference("dayend", buf, sizeof buf);
270 if (buf[0] == 0) strcpy(buf, "17");
271 wprintf("<tr class=\"odd\"><td>");
272 wprintf(_("Calendar day view ends at:"));
273 wprintf("</td><td>");
275 wprintf("<select name=\"dayend\" size=\"1\">\n");
276 for (i=0; i<=23; ++i) {
278 if (time_format == WC_TIMEFORMAT_24) {
279 wprintf("<option %s value=\"%d\">%d:00</option>\n",
280 ((atoi(buf) == i) ? "selected" : ""),
285 wprintf("<option %s value=\"%d\">%s</option>\n",
286 ((atoi(buf) == i) ? "selected" : ""),
292 wprintf("</select>\n");
293 wprintf("</td></tr>\n");
296 * Day of week to begin calendar month view
298 get_preference("weekstart", buf, sizeof buf);
299 if (buf[0] == 0) strcpy(buf, "17");
300 wprintf("<tr class=\"even\"><td>");
301 wprintf(_("Week starts on:"));
302 wprintf("</td><td>");
304 wprintf("<select name=\"weekstart\" size=\"1\">\n");
306 for (i=0; i<=1; ++i) {
308 localtime_r(&tt, &tm);
310 wc_strftime(daylabel, sizeof daylabel, "%A", &tm);
312 wprintf("<option %s value=\"%d\">%s</option>\n",
313 ((atoi(buf) == i) ? "selected" : ""),
318 wprintf("</select>\n");
319 wprintf("</td></tr>\n");
324 get_preference("use_sig", buf, sizeof buf);
325 if (buf[0] == 0) strcpy(buf, "no");
326 wprintf("<tr class=\"odd\"><td>");
327 wprintf(_("Attach signature to email messages?"));
328 wprintf("</td><td>");
330 wprintf(" <script type=\"text/javascript\"> "
331 " function show_or_hide_sigbox() { "
332 " if ( $F('yes_sig') ) { "
333 " $('signature_box').style.display = 'inline'; "
336 " $('signature_box').style.display = 'none'; "
342 wprintf("<input type=\"radio\" id=\"no_sig\" name=\"use_sig\" VALUE=\"no\"");
343 if (!strcasecmp(buf, "no")) wprintf(" checked");
344 wprintf(" onChange=\"show_or_hide_sigbox();\" >");
345 wprintf(_("No signature"));
346 wprintf("</input> , \n");
348 wprintf("<input type=\"radio\" id=\"yes_sig\" name=\"use_sig\" VALUE=\"yes\"");
349 if (!strcasecmp(buf, "yes")) wprintf(" checked");
350 wprintf(" onChange=\"show_or_hide_sigbox();\" >");
351 wprintf(_("Use this signature:"));
352 wprintf("<div id=\"signature_box\">"
353 "<br><textarea name=\"signature\" cols=\"40\" rows=\"5\">"
355 get_preference("signature", ebuf, sizeof ebuf);
356 euid_unescapize(buf, ebuf);
358 wprintf("</textarea>"
362 wprintf("</input>\n");
364 wprintf("</td></tr>\n");
366 wprintf(" <script type=\"text/javascript\"> "
367 " show_or_hide_sigbox(); "
371 /** Character set to assume is in use for improperly encoded headers */
372 get_preference("default_header_charset", buf, sizeof buf);
373 if (buf[0] == 0) strcpy(buf, "UTF-8");
374 wprintf("<tr class=\"even\"><td>");
375 wprintf(_("Default character set for email headers:"));
376 wprintf("</td><td>");
377 wprintf("<input type=\"text\" NAME=\"default_header_charset\" MAXLENGTH=\"32\" VALUE=\"");
380 wprintf("</td></tr>");
386 get_preference("emptyfloors", buf, sizeof buf);
387 if (buf[0] == 0) strcpy(buf, "no");
388 wprintf("<tr class=\"odd\"><td>");
389 wprintf(_("Show empty floors"));
390 wprintf("</td><td>");
392 wprintf("<input type=\"radio\" name=\"emptyfloors\" VALUE=\"yes\"");
393 if (!strcasecmp(buf, "yes")) wprintf(" checked");
396 wprintf("</input> ");
398 wprintf("<input type=\"radio\" name=\"emptyfloors\" VALUE=\"no\"");
399 if (!strcasecmp(buf, "no")) wprintf(" checked");
402 wprintf("</input>\n");
404 wprintf("</td></tr>\n");
407 wprintf("</table>\n");
409 /** submit buttons */
410 wprintf("<div class=\"buttons\"> ");
411 wprintf("<input type=\"submit\" name=\"change_button\" value=\"%s\">"
413 "<INPUT type=\"submit\" name=\"cancel_button\" value=\"%s\">\n",
420 wprintf("</form>\n");
426 * \brief Commit new preferences and settings
428 void set_preferences(void)
432 int *time_format_cache;
434 time_format_cache = &(WC->time_format_cache);
436 if (IsEmptyStr(bstr("change_button"))) {
437 safestrncpy(WC->ImportantMessage,
438 _("Cancelled. No settings were changed."),
439 sizeof WC->ImportantMessage);
445 * Set the last argument to 1 only for the final setting, so
446 * we don't send the prefs file to the server repeatedly
448 set_preference("roomlistview", bstr("roomlistview"), 0);
449 fmt = bstr("calhourformat");
450 set_preference("calhourformat", fmt, 0);
451 if (!strcasecmp(fmt, "24"))
452 *time_format_cache = WC_TIMEFORMAT_24;
454 *time_format_cache = WC_TIMEFORMAT_AMPM;
456 set_preference("weekstart", bstr("weekstart"), 0);
457 set_preference("use_sig", bstr("use_sig"), 0);
458 set_preference("daystart", bstr("daystart"), 0);
459 set_preference("dayend", bstr("dayend"), 0);
460 set_preference("default_header_charset", bstr("default_header_charset"), 0);
461 set_preference("emptyfloors", bstr("emptyfloors"), 0);
463 euid_escapize(ebuf, bstr("signature"));
464 set_preference("signature", ebuf, 1);