]> code.citadel.org Git - citadel.git/blob - webcit/preferences.c
* sig is done, but need to add proper handling of linebreaks
[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
180         wprintf("<div id=\"banner\">\n");
181         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
182         wprintf("<IMG SRC=\"/static/advanpage2_48x.gif\" ALT=\" \" ALIGN=MIDDLE>");
183         wprintf("<SPAN CLASS=\"titlebar\">&nbsp;");
184         wprintf(_("Preferences and settings"));
185         wprintf("</SPAN></TD><TD ALIGN=RIGHT>");
186         offer_start_page();
187         wprintf("</TD></TR></TABLE>\n");
188         wprintf("</div>\n"
189                 "<div id=\"content\">\n");
190
191         wprintf("<div id=\"fix_scrollbar_bug\">"
192                 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
193
194         /* begin form */
195         wprintf("<center>\n"
196                 "<form name=\"prefform\" action=\"set_preferences\" "
197                 "method=\"post\">\n"
198                 "<table border=0 cellspacing=5 cellpadding=5>\n");
199
200         /*
201          * Room list view
202          */
203         get_preference("roomlistview", buf, sizeof buf);
204         wprintf("<tr><td>");
205         wprintf(_("Room list view"));
206         wprintf("</td><td>");
207
208         wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
209         if (!strcasecmp(buf, "folders")) wprintf(" checked");
210         wprintf(">");
211         wprintf(_("Tree (folders) view"));
212         wprintf("<br></input>\n");
213
214         wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
215         if (!strcasecmp(buf, "rooms")) wprintf(" checked");
216         wprintf(">");
217         wprintf(_("Table (rooms) view"));
218         wprintf("<br></input>\n");
219
220         wprintf("</td></tr>\n");
221
222         /*
223          * Calendar hour format
224          */
225         get_preference("calhourformat", buf, sizeof buf);
226         if (buf[0] == 0) strcpy(buf, "12");
227         wprintf("<tr><td>");
228         wprintf(_("Calendar hour format"));
229         wprintf("</td><td>");
230
231         wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
232         if (!strcasecmp(buf, "12")) wprintf(" checked");
233         wprintf(">");
234         wprintf(_("12 hour (am/pm)"));
235         wprintf("<br></input>\n");
236
237         wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
238         if (!strcasecmp(buf, "24")) wprintf(" checked");
239         wprintf(">");
240         wprintf(_("24 hour"));
241         wprintf("<br></input>\n");
242
243         wprintf("</td></tr>\n");
244
245         /*
246          * Signature
247          */
248         get_preference("use_sig", buf, sizeof buf);
249         if (buf[0] == 0) strcpy(buf, "no");
250         wprintf("<tr><td>");
251         wprintf(_("Attach signature to email messages?"));
252         wprintf("</td><td>");
253
254         wprintf("       <script type=\"text/javascript\">                                       "
255                 "       function show_or_hide_sigbox() {                                        "
256                 "               if ( $F('yes_sig') ) {                                          "
257                 "                       $('signature_box').style.display = 'inline';            "
258                 "               }                                                               "
259                 "               else {                                                          "
260                 "                       $('signature_box').style.display = 'none';              "
261                 "               }                                                               "
262                 "       }                                                                       "
263                 "       </script>                                                               "
264         );
265
266         wprintf("<input type=\"radio\" id=\"no_sig\" name=\"use_sig\" VALUE=\"no\"");
267         if (!strcasecmp(buf, "no")) wprintf(" checked");
268         wprintf(" onChange=\"show_or_hide_sigbox();\" >");
269         wprintf(_("No signature"));
270         wprintf("<br></input>\n");
271
272         wprintf("<input type=\"radio\" id=\"yes_sig\" name=\"use_sig\" VALUE=\"yes\"");
273         if (!strcasecmp(buf, "yes")) wprintf(" checked");
274         wprintf(" onChange=\"show_or_hide_sigbox();\" >");
275         wprintf(_("Use this signature:"));
276         wprintf("<div id=\"signature_box\">"
277                 "<br><textarea name=\"signature\" cols=\"40\" rows=\"5\">"
278         );
279         get_preference("signature", ebuf, sizeof ebuf);
280         euid_unescapize(buf, ebuf);
281         escputs(buf);
282         wprintf("</textarea>"
283                 "</div>"
284         );
285
286         wprintf("<br></input>\n");
287
288         wprintf("</td></tr>\n");
289
290         wprintf("       <script type=\"text/javascript\">       "
291                 "       show_or_hide_sigbox();                  "
292                 "       </script>                               "
293         );
294
295
296         wprintf("</table>\n"
297                 "<input type=\"submit\" name=\"change_button\" value=\"%s\">"
298                 "&nbsp;"
299                 "<INPUT type=\"submit\" name=\"cancel_button\" value=\"%s\">\n",
300                 _("Change"),
301                 _("Cancel")
302         );
303
304         wprintf("</form></center>\n");
305
306         /* end form */
307
308
309         wprintf("</td></tr></table></div>\n");
310         wDumpContent(1);
311 }
312
313 /*
314  * Commit new preferences and settings
315  */
316 void set_preferences(void)
317 {
318         char ebuf[300];
319
320         if (strlen(bstr("change_button")) == 0) {
321                 safestrncpy(WC->ImportantMessage, 
322                         _("Cancelled.  No settings were changed."),
323                         sizeof WC->ImportantMessage);
324                 display_main_menu();
325                 return;
326         }
327
328         /* Set the last argument to 1 only for the final setting, so
329          * we don't send the prefs file to the server repeatedly
330          */
331         set_preference("roomlistview", bstr("roomlistview"), 0);
332         set_preference("calhourformat", bstr("calhourformat"), 0);
333         set_preference("use_sig", bstr("use_sig"), 0);
334
335         euid_escapize(ebuf, bstr("signature"));
336         set_preference("signature", ebuf, 1);
337
338         display_main_menu();
339 }