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