Removed some spurious trace messages
[citadel.git] / webcit / preferences.c
1 /*
2  * $Id$
3  *
4  * Manage user preferences with a little help from the Citadel server.
5  *
6  */
7
8 #include "webcit.h"
9 #include "webserver.h"
10 #include "groupdav.h"
11
12 /*
13  * display preferences dialog
14  */
15 void load_preferences(void) {
16         char buf[SIZ];
17         long msgnum = 0L;
18         char key[SIZ], value[SIZ];
19         
20         serv_printf("GOTO %s", USERCONFIGROOM);
21         serv_getln(buf, sizeof buf);
22         if (buf[0] != '2') return;
23         
24         serv_puts("MSGS ALL|0|1");
25         serv_getln(buf, sizeof buf);
26         if (buf[0] == '8') {
27                 serv_puts("subj|__ WebCit Preferences __");
28                 serv_puts("000");
29         }
30         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
31                 msgnum = atol(buf);
32         }
33
34         if (msgnum > 0L) {
35                 serv_printf("MSG0 %ld", msgnum);
36                 serv_getln(buf, sizeof buf);
37                 if (buf[0] == '1') {
38                         while (serv_getln(buf, sizeof buf),
39                                 (strcmp(buf, "text") && strcmp(buf, "000"))) {
40                         }
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);
45                                         if (!IsEmptyStr(key))
46                                                 Put(WC->hash_prefs, key, strlen(key), strdup(value), free);
47                                 }
48                         }
49                 }
50         }
51
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);
55 }
56
57 /**
58  * \brief Goto the user's configuration room, creating it if necessary.
59  * \return 0 on success or nonzero upon failure.
60  */
61 int goto_config_room(void) {
62         char buf[SIZ];
63
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);
72         }
73         return(0);
74 }
75
76 /**
77  * \brief save the modifications
78  */
79 void save_preferences(void) {
80         char buf[SIZ];
81         long msgnum = 0L;
82         
83         if (goto_config_room() != 0) return;    /* oh well. */
84         serv_puts("MSGS ALL|0|1");
85         serv_getln(buf, sizeof buf);
86         if (buf[0] == '8') {
87                 serv_puts("subj|__ WebCit Preferences __");
88                 serv_puts("000");
89         }
90         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
91                 msgnum = atol(buf);
92         }
93
94         if (msgnum > 0L) {
95                 serv_printf("DELE %ld", msgnum);
96                 serv_getln(buf, sizeof buf);
97         }
98
99         serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
100         serv_getln(buf, sizeof buf);
101         if (buf[0] == '4') {
102                 long len;
103                 HashPos *HashPos;
104                 HashList *Hash;
105                 char *Value;
106                 char *Key;
107                 
108                 Hash = WC->hash_prefs;
109                 PrintHash(Hash);
110                 HashPos = GetNewHashPos();
111                 while (GetNextHashPos(Hash, HashPos, &len, &Key, (void**)&Value)!=0)
112                 {
113                         serv_printf("%s|%s", Key, Value);
114                 }
115                 serv_puts("");
116                 serv_puts("000");
117                 DeleteHashPos(&HashPos);
118         }
119
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);
123 }
124
125 /**
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
130  */
131 void get_preference(char *key, char *value, size_t value_len) {
132         void *hash_value = NULL;
133         
134         strcpy(value, "");
135         PrintHash(WC->hash_prefs);
136         if (GetHash(WC->hash_prefs, key, strlen(key), &hash_value) == 0)
137                 return;
138         
139         if(hash_value)
140                 safestrncpy(value, hash_value, value_len);
141 }
142
143 /**
144  * \brief       Write a key into the webcit preferences database for this user
145  *
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
149  */
150 void set_preference(char *key, char *value, int save_to_server) {
151         
152         Put(WC->hash_prefs, key, strlen(key), strdup(value), free);
153         
154         if (save_to_server) save_preferences();
155 }
156
157
158
159
160 /** 
161  * \brief display form for changing your preferences and settings
162  */
163 void display_preferences(void)
164 {
165         output_headers(1, 1, 1, 0, 0, 0);
166         char ebuf[300];
167         char buf[256];
168         int i;
169         int time_format;
170         time_t tt;
171         struct tm tm;
172         char daylabel[32];
173         
174         time_format = get_time_format_cached ();
175
176         wprintf("<div class=\"box\">\n");
177         wprintf("<div class=\"boxlabel\">");
178         wprintf(_("Preferences and settings"));
179         wprintf("</div>");
180
181         wprintf("<div class=\"boxcontent\">");
182
183         /** begin form */
184         wprintf("<form name=\"prefform\" action=\"set_preferences\" "
185                 "method=\"post\">\n");
186         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
187
188         /** begin table */
189         wprintf("<table class=\"altern\">\n");
190
191         /**
192          * Room list view
193          */
194         get_preference("roomlistview", buf, sizeof buf);
195         wprintf("<tr class=\"even\"><td>");
196         wprintf(_("Room list view"));
197         wprintf("</td><td>");
198
199         wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
200         if (!strcasecmp(buf, "folders")) wprintf(" checked");
201         wprintf(">");
202         wprintf(_("Tree (folders) view"));
203         wprintf("</input>&nbsp;&nbsp;&nbsp;");
204
205         wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
206         if (!strcasecmp(buf, "rooms")) wprintf(" checked");
207         wprintf(">");
208         wprintf(_("Table (rooms) view"));
209         wprintf("</input>\n");
210
211         wprintf("</td></tr>\n");
212
213         /**
214          * Time hour format
215          */
216
217         wprintf("<tr class=\"odd\"><td>");
218         wprintf(_("Time format"));
219         wprintf("</td><td>");
220
221         wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
222         if (time_format == WC_TIMEFORMAT_AMPM) 
223                 wprintf(" checked");
224         wprintf(">");
225         wprintf(_("12 hour (am/pm)"));
226         wprintf("</input>&nbsp;&nbsp;&nbsp;");
227
228         wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
229         if (time_format == WC_TIMEFORMAT_24)
230                 wprintf(" checked");
231         wprintf(">");
232         wprintf(_("24 hour"));
233         wprintf("</input>\n");
234
235         wprintf("</td></tr>\n");
236
237         /**
238          * Calendar day view -- day start time
239          */
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>");
245
246         wprintf("<select name=\"daystart\" size=\"1\">\n");
247         for (i=0; i<=23; ++i) {
248
249                 if (time_format == WC_TIMEFORMAT_24) {
250                         wprintf("<option %s value=\"%d\">%d:00</option>\n",
251                                 ((atoi(buf) == i) ? "selected" : ""),
252                                 i, i
253                         );
254                 }
255                 else {
256                         wprintf("<option %s value=\"%d\">%s</option>\n",
257                                 ((atoi(buf) == i) ? "selected" : ""),
258                                 i, hourname[i]
259                         );
260                 }
261
262         }
263         wprintf("</select>\n");
264         wprintf("</td></tr>\n");
265
266         /**
267          * Calendar day view -- day end time
268          */
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>");
274
275         wprintf("<select name=\"dayend\" size=\"1\">\n");
276         for (i=0; i<=23; ++i) {
277
278                 if (time_format == WC_TIMEFORMAT_24) {
279                         wprintf("<option %s value=\"%d\">%d:00</option>\n",
280                                 ((atoi(buf) == i) ? "selected" : ""),
281                                 i, i
282                         );
283                 }
284                 else {
285                         wprintf("<option %s value=\"%d\">%s</option>\n",
286                                 ((atoi(buf) == i) ? "selected" : ""),
287                                 i, hourname[i]
288                         );
289                 }
290
291         }
292         wprintf("</select>\n");
293         wprintf("</td></tr>\n");
294
295         /**
296          * Day of week to begin calendar month view
297          */
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>");
303
304         wprintf("<select name=\"weekstart\" size=\"1\">\n");
305
306         for (i=0; i<=1; ++i) {
307                 tt = time(NULL);
308                 localtime_r(&tt, &tm);
309                 tm.tm_wday = i;
310                 wc_strftime(daylabel, sizeof daylabel, "%A", &tm);
311
312                 wprintf("<option %s value=\"%d\">%s</option>\n",
313                         ((atoi(buf) == i) ? "selected" : ""),
314                         i, daylabel
315                 );
316         }
317
318         wprintf("</select>\n");
319         wprintf("</td></tr>\n");
320
321         /**
322          * Signature
323          */
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>");
329
330         wprintf("       <script type=\"text/javascript\">                                       "
331                 "       function show_or_hide_sigbox() {                                        "
332                 "               if ( $F('yes_sig') ) {                                          "
333                 "                       $('signature_box').style.display = 'inline';            "
334                 "               }                                                               "
335                 "               else {                                                          "
336                 "                       $('signature_box').style.display = 'none';              "
337                 "               }                                                               "
338                 "       }                                                                       "
339                 "       </script>                                                               "
340         );
341
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>&nbsp,&nbsp;&nbsp;\n");
347
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\">"
354         );
355         get_preference("signature", ebuf, sizeof ebuf);
356         euid_unescapize(buf, ebuf);
357         escputs(buf);
358         wprintf("</textarea>"
359                 "</div>"
360         );
361
362         wprintf("</input>\n");
363
364         wprintf("</td></tr>\n");
365
366         wprintf("       <script type=\"text/javascript\">       "
367                 "       show_or_hide_sigbox();                  "
368                 "       </script>                               "
369         );
370
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=\"");
378         escputs(buf);
379         wprintf("\">");
380         wprintf("</td></tr>");
381
382         /**
383          * Show empty floors?
384          */
385
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>");
391
392         wprintf("<input type=\"radio\" name=\"emptyfloors\" VALUE=\"yes\"");
393         if (!strcasecmp(buf, "yes")) wprintf(" checked");
394         wprintf(">");
395         wprintf(_("Yes"));
396         wprintf("</input>&nbsp;&nbsp;&nbsp;");
397
398         wprintf("<input type=\"radio\" name=\"emptyfloors\" VALUE=\"no\"");
399         if (!strcasecmp(buf, "no")) wprintf(" checked");
400         wprintf(">");
401         wprintf(_("No"));
402         wprintf("</input>\n");
403
404         wprintf("</td></tr>\n");
405
406         /** end table */
407         wprintf("</table>\n");
408
409         /** submit buttons */
410         wprintf("<div class=\"buttons\"> ");
411         wprintf("<input type=\"submit\" name=\"change_button\" value=\"%s\">"
412                 "&nbsp;"
413                 "<INPUT type=\"submit\" name=\"cancel_button\" value=\"%s\">\n",
414                 _("Change"),
415                 _("Cancel")
416         );
417         wprintf("</div>\n");
418
419         /** end form */
420         wprintf("</form>\n");
421         wprintf("</div>\n");
422         wDumpContent(1);
423 }
424
425 /**
426  * \brief Commit new preferences and settings
427  */
428 void set_preferences(void)
429 {
430         char *fmt;
431         char ebuf[300];
432         int *time_format_cache;
433         
434         time_format_cache = &(WC->time_format_cache);
435
436         if (IsEmptyStr(bstr("change_button"))) {
437                 safestrncpy(WC->ImportantMessage, 
438                         _("Cancelled.  No settings were changed."),
439                         sizeof WC->ImportantMessage);
440                 display_main_menu();
441                 return;
442         }
443
444         /**
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
447          */
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;
453         else
454                 *time_format_cache = WC_TIMEFORMAT_AMPM;
455
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);
462
463         euid_escapize(ebuf, bstr("signature"));
464         set_preference("signature", ebuf, 1);
465
466         display_main_menu();
467 }
468
469
470 /*@}*/