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