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