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 printf("_______> writing entries\n");
112 while (GetNextHashPos(Hash, HashPos, &len, &Key, (void**)&Value)!=0)
114 printf("__________>%s -> %s \n", Key, Value);
115 serv_printf("%s|%s", Key, Value);
119 DeleteHashPos(&HashPos);
122 /** Go back to the room we're supposed to be in */
123 serv_printf("GOTO %s", WC->wc_roomname);
124 serv_getln(buf, sizeof buf);
128 * \brief query the actual setting of key in the citadel database
129 * \param key config key to query
130 * \param value value to the key to get
131 * \param value_len length of the value string
133 void get_preference(char *key, char *value, size_t value_len) {
134 void *hash_value = NULL;
137 PrintHash(WC->hash_prefs);
138 if (GetHash(WC->hash_prefs, key, strlen(key), &hash_value) == 0)
142 safestrncpy(value, hash_value, value_len);
146 * \brief Write a key into the webcit preferences database for this user
148 * \params key key whichs value is to be modified
149 * \param value value to set
150 * \param save_to_server 1 = flush all data to the server, 0 = cache it for now
152 void set_preference(char *key, char *value, int save_to_server) {
154 Put(WC->hash_prefs, key, strlen(key), strdup(value), free);
156 if (save_to_server) save_preferences();
163 * \brief display form for changing your preferences and settings
165 void display_preferences(void)
167 output_headers(1, 1, 1, 0, 0, 0);
176 time_format = get_time_format_cached ();
178 wprintf("<div class=\"box\">\n");
179 wprintf("<div class=\"boxlabel\">");
180 wprintf(_("Preferences and settings"));
183 wprintf("<div class=\"boxcontent\">");
186 wprintf("<form name=\"prefform\" action=\"set_preferences\" "
187 "method=\"post\">\n");
188 wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
191 wprintf("<table class=\"altern\">\n");
196 get_preference("roomlistview", buf, sizeof buf);
197 wprintf("<tr class=\"even\"><td>");
198 wprintf(_("Room list view"));
199 wprintf("</td><td>");
201 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
202 if (!strcasecmp(buf, "folders")) wprintf(" checked");
204 wprintf(_("Tree (folders) view"));
205 wprintf("</input> ");
207 wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
208 if (!strcasecmp(buf, "rooms")) wprintf(" checked");
210 wprintf(_("Table (rooms) view"));
211 wprintf("</input>\n");
213 wprintf("</td></tr>\n");
219 wprintf("<tr class=\"odd\"><td>");
220 wprintf(_("Time format"));
221 wprintf("</td><td>");
223 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
224 if (time_format == WC_TIMEFORMAT_AMPM)
227 wprintf(_("12 hour (am/pm)"));
228 wprintf("</input> ");
230 wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
231 if (time_format == WC_TIMEFORMAT_24)
234 wprintf(_("24 hour"));
235 wprintf("</input>\n");
237 wprintf("</td></tr>\n");
240 * Calendar day view -- day start time
242 get_preference("daystart", buf, sizeof buf);
243 if (buf[0] == 0) strcpy(buf, "8");
244 wprintf("<tr class=\"even\"><td>");
245 wprintf(_("Calendar day view begins at:"));
246 wprintf("</td><td>");
248 wprintf("<select name=\"daystart\" size=\"1\">\n");
249 for (i=0; i<=23; ++i) {
251 if (time_format == WC_TIMEFORMAT_24) {
252 wprintf("<option %s value=\"%d\">%d:00</option>\n",
253 ((atoi(buf) == i) ? "selected" : ""),
258 wprintf("<option %s value=\"%d\">%s</option>\n",
259 ((atoi(buf) == i) ? "selected" : ""),
265 wprintf("</select>\n");
266 wprintf("</td></tr>\n");
269 * Calendar day view -- day end time
271 get_preference("dayend", buf, sizeof buf);
272 if (buf[0] == 0) strcpy(buf, "17");
273 wprintf("<tr class=\"odd\"><td>");
274 wprintf(_("Calendar day view ends at:"));
275 wprintf("</td><td>");
277 wprintf("<select name=\"dayend\" size=\"1\">\n");
278 for (i=0; i<=23; ++i) {
280 if (time_format == WC_TIMEFORMAT_24) {
281 wprintf("<option %s value=\"%d\">%d:00</option>\n",
282 ((atoi(buf) == i) ? "selected" : ""),
287 wprintf("<option %s value=\"%d\">%s</option>\n",
288 ((atoi(buf) == i) ? "selected" : ""),
294 wprintf("</select>\n");
295 wprintf("</td></tr>\n");
298 * Day of week to begin calendar month view
300 get_preference("weekstart", buf, sizeof buf);
301 if (buf[0] == 0) strcpy(buf, "17");
302 wprintf("<tr class=\"even\"><td>");
303 wprintf(_("Week starts on:"));
304 wprintf("</td><td>");
306 wprintf("<select name=\"weekstart\" size=\"1\">\n");
308 for (i=0; i<=1; ++i) {
310 localtime_r(&tt, &tm);
312 wc_strftime(daylabel, sizeof daylabel, "%A", &tm);
314 wprintf("<option %s value=\"%d\">%s</option>\n",
315 ((atoi(buf) == i) ? "selected" : ""),
320 wprintf("</select>\n");
321 wprintf("</td></tr>\n");
326 get_preference("use_sig", buf, sizeof buf);
327 if (buf[0] == 0) strcpy(buf, "no");
328 wprintf("<tr class=\"odd\"><td>");
329 wprintf(_("Attach signature to email messages?"));
330 wprintf("</td><td>");
332 wprintf(" <script type=\"text/javascript\"> "
333 " function show_or_hide_sigbox() { "
334 " if ( $F('yes_sig') ) { "
335 " $('signature_box').style.display = 'inline'; "
338 " $('signature_box').style.display = 'none'; "
344 wprintf("<input type=\"radio\" id=\"no_sig\" name=\"use_sig\" VALUE=\"no\"");
345 if (!strcasecmp(buf, "no")) wprintf(" checked");
346 wprintf(" onChange=\"show_or_hide_sigbox();\" >");
347 wprintf(_("No signature"));
348 wprintf("</input> , \n");
350 wprintf("<input type=\"radio\" id=\"yes_sig\" name=\"use_sig\" VALUE=\"yes\"");
351 if (!strcasecmp(buf, "yes")) wprintf(" checked");
352 wprintf(" onChange=\"show_or_hide_sigbox();\" >");
353 wprintf(_("Use this signature:"));
354 wprintf("<div id=\"signature_box\">"
355 "<br><textarea name=\"signature\" cols=\"40\" rows=\"5\">"
357 get_preference("signature", ebuf, sizeof ebuf);
358 euid_unescapize(buf, ebuf);
360 wprintf("</textarea>"
364 wprintf("</input>\n");
366 wprintf("</td></tr>\n");
368 wprintf(" <script type=\"text/javascript\"> "
369 " show_or_hide_sigbox(); "
373 /** Character set to assume is in use for improperly encoded headers */
374 get_preference("default_header_charset", buf, sizeof buf);
375 if (buf[0] == 0) strcpy(buf, "UTF-8");
376 wprintf("<tr class=\"even\"><td>");
377 wprintf(_("Default character set for email headers:"));
378 wprintf("</td><td>");
379 wprintf("<input type=\"text\" NAME=\"default_header_charset\" MAXLENGTH=\"32\" VALUE=\"");
382 wprintf("</td></tr>");
388 get_preference("emptyfloors", buf, sizeof buf);
389 if (buf[0] == 0) strcpy(buf, "no");
390 wprintf("<tr class=\"odd\"><td>");
391 wprintf(_("Show empty floors"));
392 wprintf("</td><td>");
394 wprintf("<input type=\"radio\" name=\"emptyfloors\" VALUE=\"yes\"");
395 if (!strcasecmp(buf, "yes")) wprintf(" checked");
398 wprintf("</input> ");
400 wprintf("<input type=\"radio\" name=\"emptyfloors\" VALUE=\"no\"");
401 if (!strcasecmp(buf, "no")) wprintf(" checked");
404 wprintf("</input>\n");
406 wprintf("</td></tr>\n");
409 wprintf("</table>\n");
411 /** submit buttons */
412 wprintf("<div class=\"buttons\"> ");
413 wprintf("<input type=\"submit\" name=\"change_button\" value=\"%s\">"
415 "<INPUT type=\"submit\" name=\"cancel_button\" value=\"%s\">\n",
422 wprintf("</form>\n");
428 * \brief Commit new preferences and settings
430 void set_preferences(void)
434 int *time_format_cache;
436 time_format_cache = &(WC->time_format_cache);
438 if (IsEmptyStr(bstr("change_button"))) {
439 safestrncpy(WC->ImportantMessage,
440 _("Cancelled. No settings were changed."),
441 sizeof WC->ImportantMessage);
447 * Set the last argument to 1 only for the final setting, so
448 * we don't send the prefs file to the server repeatedly
450 set_preference("roomlistview", bstr("roomlistview"), 0);
451 fmt = bstr("calhourformat");
452 set_preference("calhourformat", fmt, 0);
453 if (!strcasecmp(fmt, "24"))
454 *time_format_cache = WC_TIMEFORMAT_24;
456 *time_format_cache = WC_TIMEFORMAT_AMPM;
458 set_preference("weekstart", bstr("weekstart"), 0);
459 set_preference("use_sig", bstr("use_sig"), 0);
460 set_preference("daystart", bstr("daystart"), 0);
461 set_preference("dayend", bstr("dayend"), 0);
462 set_preference("default_header_charset", bstr("default_header_charset"), 0);
463 set_preference("emptyfloors", bstr("emptyfloors"), 0);
465 euid_escapize(ebuf, bstr("signature"));
466 set_preference("signature", ebuf, 1);