* Three box with Advanced (on click Advanced icon) are display in the
[citadel.git] / webcit / preferences.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup ManagePrefs Manage user preferences with a little help from the Citadel server.
6  * \ingroup CitadelConfig
7  *
8  */
9 /*@{*/
10 #include "webcit.h"
11 #include "webserver.h"
12 #include "groupdav.h"
13
14
15 /**
16  * \brief display preferences dialog
17  */
18 void load_preferences(void) {
19         char buf[SIZ];
20         long msgnum = 0L;
21
22         serv_printf("GOTO %s", USERCONFIGROOM);
23         serv_getln(buf, sizeof buf);
24         if (buf[0] != '2') return;
25         
26         serv_puts("MSGS ALL|0|1");
27         serv_getln(buf, sizeof buf);
28         if (buf[0] == '8') {
29                 serv_puts("subj|__ WebCit Preferences __");
30                 serv_puts("000");
31         }
32         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
33                 msgnum = atol(buf);
34         }
35
36         if (msgnum > 0L) {
37                 serv_printf("MSG0 %ld", msgnum);
38                 serv_getln(buf, sizeof buf);
39                 if (buf[0] == '1') {
40                         while (serv_getln(buf, sizeof buf),
41                                 (strcmp(buf, "text") && strcmp(buf, "000"))) {
42                         }
43                         if (!strcmp(buf, "text")) {
44                                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
45                                         if (WC->preferences == NULL) {
46                                                 WC->preferences = malloc(SIZ);
47                                                 strcpy(WC->preferences, "");
48                                         }
49                                         else {
50                                                 WC->preferences = realloc(
51                                                         WC->preferences,
52                                                         strlen(WC->preferences)
53                                                         +SIZ
54                                                 );
55                                         }
56                                         strcat(WC->preferences, buf);
57                                         strcat(WC->preferences, "\n");
58                                 }
59                         }
60                 }
61         }
62
63         /** Go back to the room we're supposed to be in */
64         serv_printf("GOTO %s", WC->wc_roomname);
65         serv_getln(buf, sizeof buf);
66 }
67
68 /**
69  * \brief Goto the user's configuration room, creating it if necessary.
70  * \return 0 on success or nonzero upon failure.
71  */
72 int goto_config_room(void) {
73         char buf[SIZ];
74
75         serv_printf("GOTO %s", USERCONFIGROOM);
76         serv_getln(buf, sizeof buf);
77         if (buf[0] != '2') { /* try to create the config room if not there */
78                 serv_printf("CRE8 1|%s|4|0", USERCONFIGROOM);
79                 serv_getln(buf, sizeof buf);
80                 serv_printf("GOTO %s", USERCONFIGROOM);
81                 serv_getln(buf, sizeof buf);
82                 if (buf[0] != '2') return(1);
83         }
84         return(0);
85 }
86
87 /**
88  * \brief save the modifications
89  */
90 void save_preferences(void) {
91         char buf[SIZ];
92         long msgnum = 0L;
93
94         if (goto_config_room() != 0) return;    /* oh well. */
95         serv_puts("MSGS ALL|0|1");
96         serv_getln(buf, sizeof buf);
97         if (buf[0] == '8') {
98                 serv_puts("subj|__ WebCit Preferences __");
99                 serv_puts("000");
100         }
101         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
102                 msgnum = atol(buf);
103         }
104
105         if (msgnum > 0L) {
106                 serv_printf("DELE %ld", msgnum);
107                 serv_getln(buf, sizeof buf);
108         }
109
110         serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
111         serv_getln(buf, sizeof buf);
112         if (buf[0] == '4') {
113                 serv_puts(WC->preferences);
114                 serv_puts("");
115                 serv_puts("000");
116         }
117
118         /** Go back to the room we're supposed to be in */
119         serv_printf("GOTO %s", WC->wc_roomname);
120         serv_getln(buf, sizeof buf);
121 }
122
123 /**
124  * \brief query the actual setting of key in the citadel database
125  * \param key config key to query
126  * \param value value to the key to get
127  * \param value_len length of the value string
128  */
129 void get_preference(char *key, char *value, size_t value_len) {
130         int num_prefs;
131         int i;
132         char buf[SIZ];
133         char thiskey[SIZ];
134
135         strcpy(value, "");
136
137         num_prefs = num_tokens(WC->preferences, '\n');
138         for (i=0; i<num_prefs; ++i) {
139                 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
140                 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
141                 if (!strcasecmp(thiskey, key)) {
142                         extract_token(value, buf, 1, '|', value_len);
143                 }
144         }
145 }
146
147 /**
148  * \brief       Write a key into the webcit preferences database for this user
149  *
150  * \params      key             key whichs value is to be modified
151  * \param       value           value to set
152  * \param       save_to_server  1 = flush all data to the server, 0 = cache it for now
153  */
154 void set_preference(char *key, char *value, int save_to_server) {
155         int num_prefs;
156         int i;
157         char buf[SIZ];
158         char thiskey[SIZ];
159         char *newprefs = NULL;
160         size_t newprefs_len = 0;
161
162         newprefs_len = strlen(key) + strlen(value) + 10;
163         if (WC->preferences != NULL) newprefs_len += strlen(WC->preferences);
164         newprefs = malloc(newprefs_len);
165         if (newprefs == NULL) return;
166         strcpy(newprefs, "");
167
168         num_prefs = num_tokens(WC->preferences, '\n');
169         for (i=0; i<num_prefs; ++i) {
170                 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
171                 if (num_tokens(buf, '|') == 2) {
172                         extract_token(thiskey, buf, 0, '|', sizeof thiskey);
173                         if (strcasecmp(thiskey, key)) {
174                                 strcat(newprefs, buf);
175                                 strcat(newprefs, "\n");
176                         }
177                 }
178         }
179
180         sprintf(&newprefs[strlen(newprefs)], "%s|%s\n", key, value);
181         free(WC->preferences);
182         WC->preferences = newprefs;
183
184         if (save_to_server) save_preferences();
185 }
186
187
188
189
190 /** 
191  * \brief display form for changing your preferences and settings
192  */
193 void display_preferences(void)
194 {
195         output_headers(1, 1, 1, 0, 0, 0);
196         char ebuf[300];
197         char buf[256];
198         char calhourformat[16];
199         int i;
200
201         wprintf("<div class=\"box\">\n");
202         wprintf("<div class=\"boxlabel\">");
203         wprintf(_("Preferences and settings"));
204         wprintf("</div>");
205
206         wprintf("<div class=\"boxcontent\">");
207
208         /** begin form */
209         wprintf("<form name=\"prefform\" action=\"set_preferences\" "
210                 "method=\"post\">\n");
211         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
212
213         /** begin table */
214         wprintf("<table class=\"altern\">\n");
215
216         /**
217          * Room list view
218          */
219         get_preference("roomlistview", buf, sizeof buf);
220         wprintf("<tr class=\"even\"><td>");
221         wprintf(_("Room list view"));
222         wprintf("</td><td>");
223
224         wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
225         if (!strcasecmp(buf, "folders")) wprintf(" checked");
226         wprintf(">");
227         wprintf(_("Tree (folders) view"));
228         wprintf("</input>&nbsp;&nbsp;&nbsp;");
229
230         wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
231         if (!strcasecmp(buf, "rooms")) wprintf(" checked");
232         wprintf(">");
233         wprintf(_("Table (rooms) view"));
234         wprintf("</input>\n");
235
236         wprintf("</td></tr>\n");
237
238         /**
239          * Calendar hour format
240          */
241         get_preference("calhourformat", calhourformat, sizeof calhourformat);
242         if (calhourformat[0] == 0) strcpy(calhourformat, "12");
243         wprintf("<tr class=\"odd\"><td>");
244         wprintf(_("Calendar hour format"));
245         wprintf("</td><td>");
246
247         wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
248         if (!strcasecmp(calhourformat, "12")) wprintf(" checked");
249         wprintf(">");
250         wprintf(_("12 hour (am/pm)"));
251         wprintf("</input>&nbsp;&nbsp;&nbsp;");
252
253         wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
254         if (!strcasecmp(calhourformat, "24")) wprintf(" checked");
255         wprintf(">");
256         wprintf(_("24 hour"));
257         wprintf("</input>\n");
258
259         wprintf("</td></tr>\n");
260
261         /**
262          * Calendar day view -- day start time
263          */
264         get_preference("daystart", buf, sizeof buf);
265         if (buf[0] == 0) strcpy(buf, "8");
266         wprintf("<tr class=\"even\"><td>");
267         wprintf(_("Calendar day view begins at:"));
268         wprintf("</td><td>");
269
270         wprintf("<select name=\"daystart\" size=\"1\">\n");
271         for (i=0; i<=23; ++i) {
272
273                 if (!strcasecmp(calhourformat, "24")) {
274                         wprintf("<option %s value=\"%d\">%d:00</option>\n",
275                                 ((atoi(buf) == i) ? "selected" : ""),
276                                 i, i
277                         );
278                 }
279                 else {
280                         wprintf("<option %s value=\"%d\">%s</option>\n",
281                                 ((atoi(buf) == i) ? "selected" : ""),
282                                 i, hourname[i]
283                         );
284                 }
285
286         }
287         wprintf("</select>\n");
288         wprintf("</td></tr>\n");
289
290         /**
291          * Calendar day view -- day end time
292          */
293         get_preference("dayend", buf, sizeof buf);
294         if (buf[0] == 0) strcpy(buf, "17");
295         wprintf("<tr class=\"odd\"><td>");
296         wprintf(_("Calendar day view ends at:"));
297         wprintf("</td><td>");
298
299         wprintf("<select name=\"dayend\" size=\"1\">\n");
300         for (i=0; i<=23; ++i) {
301
302                 if (!strcasecmp(calhourformat, "24")) {
303                         wprintf("<option %s value=\"%d\">%d:00</option>\n",
304                                 ((atoi(buf) == i) ? "selected" : ""),
305                                 i, i
306                         );
307                 }
308                 else {
309                         wprintf("<option %s value=\"%d\">%s</option>\n",
310                                 ((atoi(buf) == i) ? "selected" : ""),
311                                 i, hourname[i]
312                         );
313                 }
314
315         }
316         wprintf("</select>\n");
317         wprintf("</td></tr>\n");
318
319         /**
320          * Signature
321          */
322         get_preference("use_sig", buf, sizeof buf);
323         if (buf[0] == 0) strcpy(buf, "no");
324         wprintf("<tr class=\"even\"><td>");
325         wprintf(_("Attach signature to email messages?"));
326         wprintf("</td><td>");
327
328         wprintf("       <script type=\"text/javascript\">                                       "
329                 "       function show_or_hide_sigbox() {                                        "
330                 "               if ( $F('yes_sig') ) {                                          "
331                 "                       $('signature_box').style.display = 'inline';            "
332                 "               }                                                               "
333                 "               else {                                                          "
334                 "                       $('signature_box').style.display = 'none';              "
335                 "               }                                                               "
336                 "       }                                                                       "
337                 "       </script>                                                               "
338         );
339
340         wprintf("<input type=\"radio\" id=\"no_sig\" name=\"use_sig\" VALUE=\"no\"");
341         if (!strcasecmp(buf, "no")) wprintf(" checked");
342         wprintf(" onChange=\"show_or_hide_sigbox();\" >");
343         wprintf(_("No signature"));
344         wprintf("</input>&nbsp,&nbsp;&nbsp;\n");
345
346         wprintf("<input type=\"radio\" id=\"yes_sig\" name=\"use_sig\" VALUE=\"yes\"");
347         if (!strcasecmp(buf, "yes")) wprintf(" checked");
348         wprintf(" onChange=\"show_or_hide_sigbox();\" >");
349         wprintf(_("Use this signature:"));
350         wprintf("<div id=\"signature_box\">"
351                 "<br><textarea name=\"signature\" cols=\"40\" rows=\"5\">"
352         );
353         get_preference("signature", ebuf, sizeof ebuf);
354         euid_unescapize(buf, ebuf);
355         escputs(buf);
356         wprintf("</textarea>"
357                 "</div>"
358         );
359
360         wprintf("</input>\n");
361
362         wprintf("</td></tr>\n");
363
364         wprintf("       <script type=\"text/javascript\">       "
365                 "       show_or_hide_sigbox();                  "
366                 "       </script>                               "
367         );
368
369         /** Character set to assume is in use for improperly encoded headers */
370         get_preference("default_header_charset", buf, sizeof buf);
371         if (buf[0] == 0) strcpy(buf, "UTF-8");
372         wprintf("<tr class=\"odd\"><td>");
373         wprintf(_("Default character set for email headers:"));
374         wprintf("</td><td>");
375         wprintf("<input type=\"text\" NAME=\"default_header_charset\" MAXLENGTH=\"32\" VALUE=\"");
376         escputs(buf);
377         wprintf("\">");
378         wprintf("</td></tr>");
379
380         /** submit buttons */
381         wprintf("<tr class=\"even buttons\"><td colspan=\"2\">");
382         wprintf("<input type=\"submit\" name=\"change_button\" value=\"%s\">"
383                 "&nbsp;"
384                 "<INPUT type=\"submit\" name=\"cancel_button\" value=\"%s\">\n",
385                 _("Change"),
386                 _("Cancel")
387         );
388         wprintf("</td></tr>");
389
390
391         /** end table */
392         wprintf("</table>\n");
393
394         /** end form */
395         wprintf("</form>\n");
396         wprintf("</div>\n");
397         wDumpContent(1);
398 }
399
400 /**
401  * \brief Commit new preferences and settings
402  */
403 void set_preferences(void)
404 {
405         char ebuf[300];
406
407         if (IsEmptyStr(bstr("change_button"))) {
408                 safestrncpy(WC->ImportantMessage, 
409                         _("Cancelled.  No settings were changed."),
410                         sizeof WC->ImportantMessage);
411                 display_main_menu();
412                 return;
413         }
414
415         /**
416          * Set the last argument to 1 only for the final setting, so
417          * we don't send the prefs file to the server repeatedly
418          */
419         set_preference("roomlistview", bstr("roomlistview"), 0);
420         set_preference("calhourformat", bstr("calhourformat"), 0);
421         set_preference("use_sig", bstr("use_sig"), 0);
422         set_preference("daystart", bstr("daystart"), 0);
423         set_preference("dayend", bstr("dayend"), 0);
424         set_preference("default_header_charset", bstr("default_header_charset"), 0);
425
426         euid_escapize(ebuf, bstr("signature"));
427         set_preference("signature", ebuf, 1);
428
429         display_main_menu();
430 }
431
432
433 /*@}*/