* Doxygen groups. Sorted the files into groups. so now we have a nice structure
[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 citadel settings database
149  * \param key key whichs value is to be modified
150  * \param value value to set
151  * \param save_to_server really write it????
152  */
153 void set_preference(char *key, char *value, int save_to_server) {
154         int num_prefs;
155         int i;
156         char buf[SIZ];
157         char thiskey[SIZ];
158         char *newprefs = NULL;
159
160         num_prefs = num_tokens(WC->preferences, '\n');
161         for (i=0; i<num_prefs; ++i) {
162                 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
163                 if (num_tokens(buf, '|') == 2) {
164                         extract_token(thiskey, buf, 0, '|', sizeof thiskey);
165                         if (strcasecmp(thiskey, key)) {
166                                 if (newprefs == NULL) newprefs = strdup("");
167                                 newprefs = realloc(newprefs,
168                                         strlen(newprefs) + SIZ );
169                                 strcat(newprefs, buf);
170                                 strcat(newprefs, "\n");
171                         }
172                 }
173         }
174
175
176         if (newprefs == NULL) newprefs = strdup("");
177         newprefs = realloc(newprefs, strlen(newprefs) + SIZ);
178         sprintf(&newprefs[strlen(newprefs)], "%s|%s\n", key, value);
179
180         free(WC->preferences);
181         WC->preferences = newprefs;
182
183         if (save_to_server) save_preferences();
184 }
185
186
187
188
189 /** 
190  * \brief display form for changing your preferences and settings
191  */
192 void display_preferences(void)
193 {
194         output_headers(1, 1, 2, 0, 0, 0);
195         char ebuf[300];
196         char buf[256];
197         char calhourformat[16];
198         int i;
199
200         wprintf("<div id=\"banner\">\n");
201         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
202         wprintf("<img src=\"static/advanpage2_48x.gif\" ALT=\" \" ALIGN=MIDDLE>");
203         wprintf("<SPAN CLASS=\"titlebar\">&nbsp;");
204         wprintf(_("Preferences and settings"));
205         wprintf("</SPAN></TD><TD ALIGN=RIGHT>");
206         offer_start_page();
207         wprintf("</TD></TR></TABLE>\n");
208         wprintf("</div>\n"
209                 "<div id=\"content\">\n");
210
211         wprintf("<div class=\"fix_scrollbar_bug\">"
212                 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
213
214         /** begin form */
215         wprintf("<center>\n"
216                 "<form name=\"prefform\" action=\"set_preferences\" "
217                 "method=\"post\">\n"
218                 "<table border=0 cellspacing=5 cellpadding=5>\n");
219
220         /**
221          * Room list view
222          */
223         get_preference("roomlistview", buf, sizeof buf);
224         wprintf("<tr><td>");
225         wprintf(_("Room list view"));
226         wprintf("</td><td>");
227
228         wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
229         if (!strcasecmp(buf, "folders")) wprintf(" checked");
230         wprintf(">");
231         wprintf(_("Tree (folders) view"));
232         wprintf("<br></input>\n");
233
234         wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
235         if (!strcasecmp(buf, "rooms")) wprintf(" checked");
236         wprintf(">");
237         wprintf(_("Table (rooms) view"));
238         wprintf("<br></input>\n");
239
240         wprintf("</td></tr>\n");
241
242         /**
243          * Calendar hour format
244          */
245         get_preference("calhourformat", calhourformat, sizeof calhourformat);
246         if (calhourformat[0] == 0) strcpy(calhourformat, "12");
247         wprintf("<tr><td>");
248         wprintf(_("Calendar hour format"));
249         wprintf("</td><td>");
250
251         wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
252         if (!strcasecmp(calhourformat, "12")) wprintf(" checked");
253         wprintf(">");
254         wprintf(_("12 hour (am/pm)"));
255         wprintf("<br></input>\n");
256
257         wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
258         if (!strcasecmp(calhourformat, "24")) wprintf(" checked");
259         wprintf(">");
260         wprintf(_("24 hour"));
261         wprintf("<br></input>\n");
262
263         wprintf("</td></tr>\n");
264
265         /**
266          * Calendar day view -- day start time
267          */
268         get_preference("daystart", buf, sizeof buf);
269         if (buf[0] == 0) strcpy(buf, "8");
270         wprintf("<tr><td>");
271         wprintf(_("Calendar day view begins at:"));
272         wprintf("</td><td>");
273
274         wprintf("<SELECT NAME=\"daystart\" SIZE=\"1\">\n");
275         for (i=0; i<=23; ++i) {
276
277                 if (!strcasecmp(calhourformat, "24")) {
278                         wprintf("<OPTION %s VALUE=\"%d\">%d:00</OPTION>\n",
279                                 ((atoi(buf) == i) ? "SELECTED" : ""),
280                                 i, i
281                         );
282                 }
283                 else {
284                         wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
285                                 ((atoi(buf) == i) ? "SELECTED" : ""),
286                                 i, hourname[i]
287                         );
288                 }
289
290         }
291         wprintf("</SELECT>\n");
292         wprintf("</td></tr>\n");
293
294         /**
295          * Calendar day view -- day end time
296          */
297         get_preference("dayend", buf, sizeof buf);
298         if (buf[0] == 0) strcpy(buf, "17");
299         wprintf("<tr><td>");
300         wprintf(_("Calendar day view ends at:"));
301         wprintf("</td><td>");
302
303         wprintf("<SELECT NAME=\"dayend\" SIZE=\"1\">\n");
304         for (i=0; i<=23; ++i) {
305
306                 if (!strcasecmp(calhourformat, "24")) {
307                         wprintf("<OPTION %s VALUE=\"%d\">%d:00</OPTION>\n",
308                                 ((atoi(buf) == i) ? "SELECTED" : ""),
309                                 i, i
310                         );
311                 }
312                 else {
313                         wprintf("<OPTION %s VALUE=\"%d\">%s</OPTION>\n",
314                                 ((atoi(buf) == i) ? "SELECTED" : ""),
315                                 i, hourname[i]
316                         );
317                 }
318
319         }
320         wprintf("</SELECT>\n");
321         wprintf("</td></tr>\n");
322
323         /**
324          * Signature
325          */
326         get_preference("use_sig", buf, sizeof buf);
327         if (buf[0] == 0) strcpy(buf, "no");
328         wprintf("<tr><td>");
329         wprintf(_("Attach signature to email messages?"));
330         wprintf("</td><td>");
331
332         wprintf("       <script type=\"text/javascript\">                                       "
333                 "       function show_or_hide_sigbox() {                                        "
334                 "               if ( $F('yes_sig') ) {                                          "
335                 "                       $('signature_box').style.display = 'inline';            "
336                 "               }                                                               "
337                 "               else {                                                          "
338                 "                       $('signature_box').style.display = 'none';              "
339                 "               }                                                               "
340                 "       }                                                                       "
341                 "       </script>                                                               "
342         );
343
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("<br></input>\n");
349
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\">"
356         );
357         get_preference("signature", ebuf, sizeof ebuf);
358         euid_unescapize(buf, ebuf);
359         escputs(buf);
360         wprintf("</textarea>"
361                 "</div>"
362         );
363
364         wprintf("<br></input>\n");
365
366         wprintf("</td></tr>\n");
367
368         wprintf("       <script type=\"text/javascript\">       "
369                 "       show_or_hide_sigbox();                  "
370                 "       </script>                               "
371         );
372
373
374         wprintf("</table>\n"
375                 "<input type=\"submit\" name=\"change_button\" value=\"%s\">"
376                 "&nbsp;"
377                 "<INPUT type=\"submit\" name=\"cancel_button\" value=\"%s\">\n",
378                 _("Change"),
379                 _("Cancel")
380         );
381
382         wprintf("</form></center>\n");
383
384         /** end form */
385
386
387         wprintf("</td></tr></table></div>\n");
388         wDumpContent(1);
389 }
390
391 /**
392  * \brief Commit new preferences and settings
393  */
394 void set_preferences(void)
395 {
396         char ebuf[300];
397
398         if (strlen(bstr("change_button")) == 0) {
399                 safestrncpy(WC->ImportantMessage, 
400                         _("Cancelled.  No settings were changed."),
401                         sizeof WC->ImportantMessage);
402                 display_main_menu();
403                 return;
404         }
405
406         /**
407          * Set the last argument to 1 only for the final setting, so
408          * we don't send the prefs file to the server repeatedly
409          */
410         set_preference("roomlistview", bstr("roomlistview"), 0);
411         set_preference("calhourformat", bstr("calhourformat"), 0);
412         set_preference("use_sig", bstr("use_sig"), 0);
413         set_preference("daystart", bstr("daystart"), 0);
414         set_preference("dayend", bstr("dayend"), 0);
415
416         euid_escapize(ebuf, bstr("signature"));
417         set_preference("signature", ebuf, 1);
418
419         display_main_menu();
420 }
421
422
423 /*@}*/