* summary.c: summary screen is now updated using ajax instead of refreshing
[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
11
12
13 void load_preferences(void) {
14         char buf[SIZ];
15         long msgnum = 0L;
16
17         serv_printf("GOTO %s", USERCONFIGROOM);
18         serv_getln(buf, sizeof buf);
19         if (buf[0] != '2') return;
20         
21         serv_puts("MSGS ALL|0|1");
22         serv_getln(buf, sizeof buf);
23         if (buf[0] == '8') {
24                 serv_puts("subj|__ WebCit Preferences __");
25                 serv_puts("000");
26         }
27         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
28                 msgnum = atol(buf);
29         }
30
31         if (msgnum > 0L) {
32                 serv_printf("MSG0 %ld", msgnum);
33                 serv_getln(buf, sizeof buf);
34                 if (buf[0] == '1') {
35                         while (serv_getln(buf, sizeof buf),
36                                 (strcmp(buf, "text") && strcmp(buf, "000"))) {
37                         }
38                         if (!strcmp(buf, "text")) {
39                                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
40                                         if (WC->preferences == NULL) {
41                                                 WC->preferences = malloc(SIZ);
42                                                 strcpy(WC->preferences, "");
43                                         }
44                                         else {
45                                                 WC->preferences = realloc(
46                                                         WC->preferences,
47                                                         strlen(WC->preferences)
48                                                         +SIZ
49                                                 );
50                                         }
51                                         strcat(WC->preferences, buf);
52                                         strcat(WC->preferences, "\n");
53                                 }
54                         }
55                 }
56         }
57
58         /* Go back to the room we're supposed to be in */
59         serv_printf("GOTO %s", WC->wc_roomname);
60         serv_getln(buf, sizeof buf);
61 }
62
63 /*
64  * Goto the user's configuration room, creating it if necessary.
65  * Returns 0 on success or nonzero upon failure.
66  */
67 int goto_config_room(void) {
68         char buf[SIZ];
69
70         serv_printf("GOTO %s", USERCONFIGROOM);
71         serv_getln(buf, sizeof buf);
72         if (buf[0] != '2') { /* try to create the config room if not there */
73                 serv_printf("CRE8 1|%s|4|0", USERCONFIGROOM);
74                 serv_getln(buf, sizeof buf);
75                 serv_printf("GOTO %s", USERCONFIGROOM);
76                 serv_getln(buf, sizeof buf);
77                 if (buf[0] != '2') return(1);
78         }
79         return(0);
80 }
81
82
83 void save_preferences(void) {
84         char buf[SIZ];
85         long msgnum = 0L;
86
87         if (goto_config_room() != 0) return;    /* oh well. */
88         serv_puts("MSGS ALL|0|1");
89         serv_getln(buf, sizeof buf);
90         if (buf[0] == '8') {
91                 serv_puts("subj|__ WebCit Preferences __");
92                 serv_puts("000");
93         }
94         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
95                 msgnum = atol(buf);
96         }
97
98         if (msgnum > 0L) {
99                 serv_printf("DELE %ld", msgnum);
100                 serv_getln(buf, sizeof buf);
101         }
102
103         serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
104         serv_getln(buf, sizeof buf);
105         if (buf[0] == '4') {
106                 serv_puts(WC->preferences);
107                 serv_puts("");
108                 serv_puts("000");
109         }
110
111         /* Go back to the room we're supposed to be in */
112         serv_printf("GOTO %s", WC->wc_roomname);
113         serv_getln(buf, sizeof buf);
114 }
115
116 void get_preference(char *key, char *value, size_t value_len) {
117         int num_prefs;
118         int i;
119         char buf[SIZ];
120         char thiskey[SIZ];
121
122         strcpy(value, "");
123
124         num_prefs = num_tokens(WC->preferences, '\n');
125         for (i=0; i<num_prefs; ++i) {
126                 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
127                 extract_token(thiskey, buf, 0, '|', sizeof thiskey);
128                 if (!strcasecmp(thiskey, key)) {
129                         extract_token(value, buf, 1, '|', value_len);
130                 }
131         }
132 }
133
134 void set_preference(char *key, char *value, int save_to_server) {
135         int num_prefs;
136         int i;
137         char buf[SIZ];
138         char thiskey[SIZ];
139         char *newprefs = NULL;
140
141         num_prefs = num_tokens(WC->preferences, '\n');
142         for (i=0; i<num_prefs; ++i) {
143                 extract_token(buf, WC->preferences, i, '\n', sizeof buf);
144                 if (num_tokens(buf, '|') == 2) {
145                         extract_token(thiskey, buf, 0, '|', sizeof thiskey);
146                         if (strcasecmp(thiskey, key)) {
147                                 if (newprefs == NULL) newprefs = strdup("");
148                                 newprefs = realloc(newprefs,
149                                         strlen(newprefs) + SIZ );
150                                 strcat(newprefs, buf);
151                                 strcat(newprefs, "\n");
152                         }
153                 }
154         }
155
156
157         if (newprefs == NULL) newprefs = strdup("");
158         newprefs = realloc(newprefs, strlen(newprefs) + SIZ);
159         sprintf(&newprefs[strlen(newprefs)], "%s|%s\n", key, value);
160
161         free(WC->preferences);
162         WC->preferences = newprefs;
163
164         if (save_to_server) save_preferences();
165 }
166
167
168
169
170 /* 
171  * display form for changing your preferences and settings
172  */
173 void display_preferences(void)
174 {
175         output_headers(1, 1, 2, 0, 0, 0);
176         char buf[256];
177
178         wprintf("<div id=\"banner\">\n");
179         wprintf("<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>");
180         wprintf("<IMG SRC=\"/static/advanpage2_48x.gif\" ALT=\" \" ALIGN=MIDDLE>");
181         wprintf("<SPAN CLASS=\"titlebar\">&nbsp;");
182         wprintf(_("Preferences and settings"));
183         wprintf("</SPAN></TD><TD ALIGN=RIGHT>");
184         offer_start_page();
185         wprintf("</TD></TR></TABLE>\n");
186         wprintf("</div>\n"
187                 "<div id=\"content\">\n");
188
189         wprintf("<div id=\"fix_scrollbar_bug\">"
190                 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
191
192         /* begin form */
193         wprintf("<center>\n"
194                 "<form name=\"prefform\" action=\"set_preferences\" "
195                 "method=\"post\">\n"
196                 "<table border=0 cellspacing=5 cellpadding=5>\n");
197
198
199         get_preference("roomlistview", buf, sizeof buf);
200         wprintf("<tr><td>");
201         wprintf(_("Room list view"));
202         wprintf("</td><td>");
203
204         wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
205         if (!strcasecmp(buf, "folders")) wprintf(" checked");
206         wprintf(">");
207         wprintf(_("Tree (folders) view"));
208         wprintf("<br></input>\n");
209
210         wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
211         if (!strcasecmp(buf, "rooms")) wprintf(" checked");
212         wprintf(">");
213         wprintf(_("Table (rooms) view"));
214         wprintf("<br></input>\n");
215
216         wprintf("</td></tr>\n");
217
218
219         get_preference("calhourformat", buf, sizeof buf);
220         if (buf[0] == 0) strcpy(buf, "12");
221         wprintf("<tr><td>");
222         wprintf(_("Calendar hour format"));
223         wprintf("</td><td>");
224
225         wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
226         if (!strcasecmp(buf, "12")) wprintf(" checked");
227         wprintf(">");
228         wprintf(_("12 hour (am/pm)"));
229         wprintf("<br></input>\n");
230
231         wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
232         if (!strcasecmp(buf, "24")) wprintf(" checked");
233         wprintf(">");
234         wprintf(_("24 hour"));
235         wprintf("<br></input>\n");
236
237         wprintf("</td></tr>\n");
238
239         wprintf("</table>\n"
240                 "<input type=\"submit\" name=\"change_button\" value=\"%s\">"
241                 "&nbsp;"
242                 "<INPUT type=\"submit\" name=\"cancel_button\" value=\"%s\">\n",
243                 _("Change"),
244                 _("Cancel")
245         );
246
247         wprintf("</form></center>\n");
248
249         /* end form */
250
251
252         wprintf("</td></tr></table></div>\n");
253         wDumpContent(1);
254 }
255
256 /*
257  * Commit new preferences and settings
258  */
259 void set_preferences(void)
260 {
261         if (strlen(bstr("change_button")) == 0) {
262                 safestrncpy(WC->ImportantMessage, 
263                         _("Cancelled.  No settings were changed."),
264                         sizeof WC->ImportantMessage);
265                 display_main_menu();
266                 return;
267         }
268
269         /* Set the last argument to 1 only for the final setting, so
270          * we don't send the prefs file to the server repeatedly
271          */
272         set_preference("roomlistview", bstr("roomlistview"), 0);
273         set_preference("calhourformat", bstr("calhourformat"), 1);
274
275         display_main_menu();
276 }