* Unfinished code is now disabled.
[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  * display preferences dialog
14  */
15 void load_preferences(void) {
16         char buf[SIZ];
17         long msgnum = 0L;
18         char key[SIZ], value[SIZ];
19         
20         serv_printf("GOTO %s", USERCONFIGROOM);
21         serv_getln(buf, sizeof buf);
22         if (buf[0] != '2') return;
23         
24         serv_puts("MSGS ALL|0|1");
25         serv_getln(buf, sizeof buf);
26         if (buf[0] == '8') {
27                 serv_puts("subj|__ WebCit Preferences __");
28                 serv_puts("000");
29         }
30         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
31                 msgnum = atol(buf);
32         }
33
34         if (msgnum > 0L) {
35                 serv_printf("MSG0 %ld", msgnum);
36                 serv_getln(buf, sizeof buf);
37                 if (buf[0] == '1') {
38                         while (serv_getln(buf, sizeof buf),
39                                 (strcmp(buf, "text") && strcmp(buf, "000"))) {
40                         }
41                         if (!strcmp(buf, "text")) {
42                                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
43                                         extract_token(key, buf, 0, '|', sizeof key);
44                                         extract_token(value, buf, 1, '|', sizeof value);
45                                         if (!IsEmptyStr(key))
46                                                 Put(WC->hash_prefs, key, strlen(key), strdup(value), free);
47                                 }
48                         }
49                 }
50         }
51
52         /* Go back to the room we're supposed to be in */
53         serv_printf("GOTO %s", WC->wc_roomname);
54         serv_getln(buf, sizeof buf);
55 }
56
57 /**
58  * \brief Goto the user's configuration room, creating it if necessary.
59  * \return 0 on success or nonzero upon failure.
60  */
61 int goto_config_room(void) {
62         char buf[SIZ];
63
64         serv_printf("GOTO %s", USERCONFIGROOM);
65         serv_getln(buf, sizeof buf);
66         if (buf[0] != '2') { /* try to create the config room if not there */
67                 serv_printf("CRE8 1|%s|4|0", USERCONFIGROOM);
68                 serv_getln(buf, sizeof buf);
69                 serv_printf("GOTO %s", USERCONFIGROOM);
70                 serv_getln(buf, sizeof buf);
71                 if (buf[0] != '2') return(1);
72         }
73         return(0);
74 }
75
76 /**
77  * \brief save the modifications
78  */
79 void save_preferences(void) {
80         char buf[SIZ];
81         long msgnum = 0L;
82         
83         if (goto_config_room() != 0) return;    /* oh well. */
84         serv_puts("MSGS ALL|0|1");
85         serv_getln(buf, sizeof buf);
86         if (buf[0] == '8') {
87                 serv_puts("subj|__ WebCit Preferences __");
88                 serv_puts("000");
89         }
90         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
91                 msgnum = atol(buf);
92         }
93
94         if (msgnum > 0L) {
95                 serv_printf("DELE %ld", msgnum);
96                 serv_getln(buf, sizeof buf);
97         }
98
99         serv_printf("ENT0 1||0|1|__ WebCit Preferences __|");
100         serv_getln(buf, sizeof buf);
101         if (buf[0] == '4') {
102                 long len;
103                 HashPos *HashPos;
104                 HashList *Hash;
105                 char *Value;
106                 char *Key;
107                 
108                 Hash = WC->hash_prefs;
109                 PrintHash(Hash);
110                 HashPos = GetNewHashPos();
111                 printf("_______> writing entries\n");
112                 while (GetNextHashPos(Hash, HashPos, &len, &Key, (void**)&Value)!=0)
113                 {
114                         printf("__________>%s -> %s \n", Key, Value);
115                         serv_printf("%s|%s", Key, Value);
116                 }
117                 serv_puts("");
118                 serv_puts("000");
119                 DeleteHashPos(&HashPos);
120         }
121
122         /** Go back to the room we're supposed to be in */
123         serv_printf("GOTO %s", WC->wc_roomname);
124         serv_getln(buf, sizeof buf);
125 }
126
127 /**
128  * \brief query the actual setting of key in the citadel database
129  * \param key config key to query
130  * \param value value to the key to get
131  * \param value_len length of the value string
132  */
133 void get_preference(char *key, char *value, size_t value_len) {
134         void *hash_value = NULL;
135         
136         strcpy(value, "");
137         PrintHash(WC->hash_prefs);
138         if (GetHash(WC->hash_prefs, key, strlen(key), &hash_value) == 0)
139                 return;
140         
141         if(hash_value)
142                 safestrncpy(value, hash_value, value_len);
143 }
144
145 /**
146  * \brief       Write a key into the webcit preferences database for this user
147  *
148  * \params      key             key whichs value is to be modified
149  * \param       value           value to set
150  * \param       save_to_server  1 = flush all data to the server, 0 = cache it for now
151  */
152 void set_preference(char *key, char *value, int save_to_server) {
153         
154         Put(WC->hash_prefs, key, strlen(key), strdup(value), free);
155         
156         if (save_to_server) save_preferences();
157 }
158
159
160
161
162 /** 
163  * \brief display form for changing your preferences and settings
164  */
165 void display_preferences(void)
166 {
167         output_headers(1, 1, 1, 0, 0, 0);
168         char ebuf[300];
169         char buf[256];
170         int i;
171         int time_format;
172         time_t tt;
173         struct tm tm;
174         char daylabel[32];
175         
176         time_format = get_time_format_cached ();
177
178         wprintf("<div class=\"box\">\n");
179         wprintf("<div class=\"boxlabel\">");
180         wprintf(_("Preferences and settings"));
181         wprintf("</div>");
182
183         wprintf("<div class=\"boxcontent\">");
184
185         /** begin form */
186         wprintf("<form name=\"prefform\" action=\"set_preferences\" "
187                 "method=\"post\">\n");
188         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
189
190         /** begin table */
191         wprintf("<table class=\"altern\">\n");
192
193         /**
194          * Room list view
195          */
196         get_preference("roomlistview", buf, sizeof buf);
197         wprintf("<tr class=\"even\"><td>");
198         wprintf(_("Room list view"));
199         wprintf("</td><td>");
200
201         wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"folders\"");
202         if (!strcasecmp(buf, "folders")) wprintf(" checked");
203         wprintf(">");
204         wprintf(_("Tree (folders) view"));
205         wprintf("</input>&nbsp;&nbsp;&nbsp;");
206
207         wprintf("<input type=\"radio\" name=\"roomlistview\" VALUE=\"rooms\"");
208         if (!strcasecmp(buf, "rooms")) wprintf(" checked");
209         wprintf(">");
210         wprintf(_("Table (rooms) view"));
211         wprintf("</input>\n");
212
213         wprintf("</td></tr>\n");
214
215         /**
216          * Time hour format
217          */
218
219         wprintf("<tr class=\"odd\"><td>");
220         wprintf(_("Time format"));
221         wprintf("</td><td>");
222
223         wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"12\"");
224         if (time_format == WC_TIMEFORMAT_AMPM) 
225                 wprintf(" checked");
226         wprintf(">");
227         wprintf(_("12 hour (am/pm)"));
228         wprintf("</input>&nbsp;&nbsp;&nbsp;");
229
230         wprintf("<input type=\"radio\" name=\"calhourformat\" VALUE=\"24\"");
231         if (time_format == WC_TIMEFORMAT_24)
232                 wprintf(" checked");
233         wprintf(">");
234         wprintf(_("24 hour"));
235         wprintf("</input>\n");
236
237         wprintf("</td></tr>\n");
238
239         /**
240          * Calendar day view -- day start time
241          */
242         get_preference("daystart", buf, sizeof buf);
243         if (buf[0] == 0) strcpy(buf, "8");
244         wprintf("<tr class=\"even\"><td>");
245         wprintf(_("Calendar day view begins at:"));
246         wprintf("</td><td>");
247
248         wprintf("<select name=\"daystart\" size=\"1\">\n");
249         for (i=0; i<=23; ++i) {
250
251                 if (time_format == WC_TIMEFORMAT_24) {
252                         wprintf("<option %s value=\"%d\">%d:00</option>\n",
253                                 ((atoi(buf) == i) ? "selected" : ""),
254                                 i, i
255                         );
256                 }
257                 else {
258                         wprintf("<option %s value=\"%d\">%s</option>\n",
259                                 ((atoi(buf) == i) ? "selected" : ""),
260                                 i, hourname[i]
261                         );
262                 }
263
264         }
265         wprintf("</select>\n");
266         wprintf("</td></tr>\n");
267
268         /**
269          * Calendar day view -- day end time
270          */
271         get_preference("dayend", buf, sizeof buf);
272         if (buf[0] == 0) strcpy(buf, "17");
273         wprintf("<tr class=\"odd\"><td>");
274         wprintf(_("Calendar day view ends at:"));
275         wprintf("</td><td>");
276
277         wprintf("<select name=\"dayend\" size=\"1\">\n");
278         for (i=0; i<=23; ++i) {
279
280                 if (time_format == WC_TIMEFORMAT_24) {
281                         wprintf("<option %s value=\"%d\">%d:00</option>\n",
282                                 ((atoi(buf) == i) ? "selected" : ""),
283                                 i, i
284                         );
285                 }
286                 else {
287                         wprintf("<option %s value=\"%d\">%s</option>\n",
288                                 ((atoi(buf) == i) ? "selected" : ""),
289                                 i, hourname[i]
290                         );
291                 }
292
293         }
294         wprintf("</select>\n");
295         wprintf("</td></tr>\n");
296
297         /**
298          * Day of week to begin calendar month view
299          */
300         get_preference("weekstart", buf, sizeof buf);
301         if (buf[0] == 0) strcpy(buf, "17");
302         wprintf("<tr class=\"even\"><td>");
303         wprintf(_("Week starts on:"));
304         wprintf("</td><td>");
305
306         wprintf("<select name=\"weekstart\" size=\"1\">\n");
307
308         for (i=0; i<=1; ++i) {
309                 tt = time(NULL);
310                 localtime_r(&tt, &tm);
311                 tm.tm_wday = i;
312                 wc_strftime(daylabel, sizeof daylabel, "%A", &tm);
313
314                 wprintf("<option %s value=\"%d\">%s</option>\n",
315                         ((atoi(buf) == i) ? "selected" : ""),
316                         i, daylabel
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 class=\"odd\"><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("</input>&nbsp,&nbsp;&nbsp;\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("</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         /** Character set to assume is in use for improperly encoded headers */
374         get_preference("default_header_charset", buf, sizeof buf);
375         if (buf[0] == 0) strcpy(buf, "UTF-8");
376         wprintf("<tr class=\"even\"><td>");
377         wprintf(_("Default character set for email headers:"));
378         wprintf("</td><td>");
379         wprintf("<input type=\"text\" NAME=\"default_header_charset\" MAXLENGTH=\"32\" VALUE=\"");
380         escputs(buf);
381         wprintf("\">");
382         wprintf("</td></tr>");
383
384         /**
385          * Show empty floors?
386          */
387
388         get_preference("emptyfloors", buf, sizeof buf);
389         if (buf[0] == 0) strcpy(buf, "no");
390         wprintf("<tr class=\"odd\"><td>");
391         wprintf(_("Show empty floors"));
392         wprintf("</td><td>");
393
394         wprintf("<input type=\"radio\" name=\"emptyfloors\" VALUE=\"yes\"");
395         if (!strcasecmp(buf, "yes")) wprintf(" checked");
396         wprintf(">");
397         wprintf(_("Yes"));
398         wprintf("</input>&nbsp;&nbsp;&nbsp;");
399
400         wprintf("<input type=\"radio\" name=\"emptyfloors\" VALUE=\"no\"");
401         if (!strcasecmp(buf, "no")) wprintf(" checked");
402         wprintf(">");
403         wprintf(_("No"));
404         wprintf("</input>\n");
405
406         wprintf("</td></tr>\n");
407
408         /** end table */
409         wprintf("</table>\n");
410
411         /** submit buttons */
412         wprintf("<div class=\"buttons\"> ");
413         wprintf("<input type=\"submit\" name=\"change_button\" value=\"%s\">"
414                 "&nbsp;"
415                 "<INPUT type=\"submit\" name=\"cancel_button\" value=\"%s\">\n",
416                 _("Change"),
417                 _("Cancel")
418         );
419         wprintf("</div>\n");
420
421         /** end form */
422         wprintf("</form>\n");
423         wprintf("</div>\n");
424         wDumpContent(1);
425 }
426
427 /**
428  * \brief Commit new preferences and settings
429  */
430 void set_preferences(void)
431 {
432         char *fmt;
433         char ebuf[300];
434         int *time_format_cache;
435         
436         time_format_cache = &(WC->time_format_cache);
437
438         if (IsEmptyStr(bstr("change_button"))) {
439                 safestrncpy(WC->ImportantMessage, 
440                         _("Cancelled.  No settings were changed."),
441                         sizeof WC->ImportantMessage);
442                 display_main_menu();
443                 return;
444         }
445
446         /**
447          * Set the last argument to 1 only for the final setting, so
448          * we don't send the prefs file to the server repeatedly
449          */
450         set_preference("roomlistview", bstr("roomlistview"), 0);
451         fmt = bstr("calhourformat");
452         set_preference("calhourformat", fmt, 0);
453         if (!strcasecmp(fmt, "24")) 
454                 *time_format_cache = WC_TIMEFORMAT_24;
455         else
456                 *time_format_cache = WC_TIMEFORMAT_AMPM;
457
458         set_preference("weekstart", bstr("weekstart"), 0);
459         set_preference("use_sig", bstr("use_sig"), 0);
460         set_preference("daystart", bstr("daystart"), 0);
461         set_preference("dayend", bstr("dayend"), 0);
462         set_preference("default_header_charset", bstr("default_header_charset"), 0);
463         set_preference("emptyfloors", bstr("emptyfloors"), 0);
464
465         euid_escapize(ebuf, bstr("signature"));
466         set_preference("signature", ebuf, 1);
467
468         display_main_menu();
469 }
470
471
472 /*@}*/