* Annotated the comments regarding ib_ settings, note that these values may not change
[citadel.git] / webcit / iconbar.c
1 /*
2  * $Id$
3  *
4  * Displays and customizes the iconbar.
5  */
6
7 #include "webcit.h"
8
9 /* Values for ib_displayas ... don't change these or you will break the templates */
10 #define IB_PICTEXT      0       /* picture and text */
11 #define IB_PICONLY      1       /* just a picture */
12 #define IB_TEXTONLY     2       /* just text */
13
14 void DontDeleteThis(void *Data){}
15
16 #define IconbarIsEnabled(a, b) IconbarIsENABLED(a, sizeof(a) - 1, b)
17
18 long IconbarIsENABLED(const char *key, size_t keylen, long defval)
19 {
20         void *Data;
21         wcsession *WCC = WC;
22
23         if (WCC == NULL) 
24                 return defval;
25
26         if (GetHash(WCC->IconBarSettings, 
27                     key, 
28                     keylen,
29                     &Data) && 
30             (Data != NULL))
31                 return (long) Data;
32         else 
33                 return defval;
34 }
35
36 #ifdef DBG_ICONBAR_HASH
37 static char nbuf[32];
38 inline const char *PrintInt(void *Prefstr)
39 {
40         snprintf(nbuf, sizeof(nbuf), "%ld", (long)Prefstr);
41         return nbuf;
42 }
43 #endif
44
45 /* Produces a stylesheet which hides any iconbar icons the user does not want */
46 void doUserIconStylesheet(void) {
47         HashPos *pos;
48         void *Data;
49         long value;
50         const char *key;
51         long HKLen;
52         
53         output_custom_content_header("text/css");
54         hprintf("Cache-Control: private\r\n");
55         
56         begin_burst();
57         wprintf("#global { left: 16%%; }\r\n");
58         pos = GetNewHashPos(WC->IconBarSettings, 0);
59         while(GetNextHashPos(WC->IconBarSettings, pos, &HKLen, &key, &Data)) {
60                 value = (long) Data;
61                 if (value == 0 
62                     && strncasecmp("ib_displayas",key,12) 
63                     && strncasecmp("ib_logoff", key, 9)) {
64                         /* Don't shoot me for this */
65                         wprintf("#%s { display: none !important; }\r\n",key);
66                 } else if (!strncasecmp("ib_users",key, 8) && value == 2) {
67                         wprintf("#online_users { display: block; !important } \r\n");
68                 }
69         }
70         DeleteHashPos(&pos);
71         end_burst();
72 }
73
74 int ConditionalIsActiveStylesheet(StrBuf *Target, WCTemplputParams *TP) {
75         long testFor = TP->Tokens->Params[3]->lvalue;
76         int ib_displayas = IconbarIsEnabled("ib_displayas",IB_PICTEXT);
77         return (testFor == ib_displayas);
78 }
79
80 void LoadIconSettings(StrBuf *iconbar, long lvalue)
81 {
82         wcsession *WCC = WC;
83         StrBuf *buf;
84         StrBuf *key;
85         long val;
86         int i, nTokens;
87
88         buf = NewStrBuf();;
89         key = NewStrBuf();
90         if (WCC->IconBarSettings == NULL)
91                 WCC->IconBarSettings = NewHash(1, NULL);
92         /**
93          * The initialized values of these variables also happen to
94          * specify the default values for users who haven't customized
95          * their iconbars.  These should probably be set in a master
96          * configuration somewhere.
97          */
98
99         nTokens = StrBufNum_tokens(iconbar, ',');
100         for (i=0; i<nTokens; ++i) {
101                 StrBufExtract_token(buf, iconbar, i, ',');
102                 StrBufExtract_token(key, buf, 0, '=');
103                 val = StrBufExtract_long(buf, 1, '=');
104                 Put(WCC->IconBarSettings, 
105                     ChrPtr(key), StrLength(key),
106                     (void*)val, DontDeleteThis);
107         }
108
109 #ifdef DBG_ICONBAR_HASH
110         dbg_PrintHash(WCC->IconBarSetttings, PrintInt, NULL);
111 #endif
112
113         FreeStrBuf(&key);
114         FreeStrBuf(&buf);
115 }
116
117 /*
118  * display a customized version of the iconbar
119  */
120 void display_customize_iconbar(void) {
121         int i;
122         int bar = 0;
123         long val;
124
125         int ib_displayas;
126
127         output_headers(1, 1, 2, 0, 0, 0);
128         wprintf("<div id=\"banner\">");
129         wprintf("<h1>");
130         wprintf(_("Customize the icon bar"));
131         wprintf("</h1></div>\n");
132
133         wprintf("<div id=\"content\" class=\"service\">\n");
134
135         wprintf("<div class=\"fix_scrollbar_bug\">");
136
137         wprintf("<form method=\"post\" action=\"commit_iconbar\">\n");
138         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
139
140         wprintf("<table class=\"altern\" >\n");
141         wprintf("<tr><td></td><td colspan=\"2\"><b>");
142         wprintf(_("Display icons as:"));
143         wprintf("</b>");
144         ib_displayas = IconbarIsEnabled("ib_displayas",IB_PICTEXT);
145         for (i=0; i<=2; ++i) {
146                 wprintf("<input type=\"radio\" name=\"ib_displayas\" value=\"%d\"", i);
147                 if (ib_displayas == i) wprintf(" CHECKED");
148                 wprintf(">");
149                 if (i == IB_PICTEXT)    wprintf(_("pictures and text"));
150                 if (i == IB_PICONLY)    wprintf(_("pictures only"));
151                 if (i == IB_TEXTONLY)   wprintf(_("text only"));
152                 wprintf("\n");
153         }
154         wprintf("<br />\n");
155
156         wprintf(_("Select the icons you would like to see displayed "
157                 "in the 'icon bar' menu on the left side of the "
158                 "screen."));
159         wprintf("</td></tr>\n");
160
161         bar = 1 - bar;
162         val = IconbarIsEnabled("ib_logo", 0);
163         wprintf("<tr class=\"%s\"><td>"
164                 "<input type=\"radio\" name=\"ib_logo\" value=\"yes\" %s> %s &nbsp;&nbsp;&nbsp;"
165                 "<input type=\"radio\" name=\"ib_logo\" value=\"no\" %s> %s <br />"
166                 "</td><td>"
167                 "<img src=\"image&name=hello\" width=\"48\" alt=\"&nbsp;\">"
168                 "</td><td>"
169                 "<b>%s</b><br />"
170                 "%s"
171                 "</td></tr>\n",
172                 (bar ? "even" : "odd"),
173                 (val ? "CHECKED" : ""),_("Yes"),
174                 (!val ? "CHECKED" : ""),_("No"),
175                 _("Site logo"),
176                 _("An icon describing this site")
177         );
178
179         bar = 1 - bar;
180         val = IconbarIsEnabled("ib_summary", 1);
181         wprintf("<tr class=\"%s\"><td>"
182                 "<input type=\"radio\" name=\"ib_summary\" value=\"yes\" %s> %s &nbsp;&nbsp;&nbsp;"
183                 "<input type=\"radio\" name=\"ib_summary\" value=\"no\" %s> %s <br />"
184                 "</td><td>"
185                 "<img src=\"static/summscreen_48x.gif\" alt=\"&nbsp;\">"
186                 "</td><td>"
187                 "<b>%s</b><br />"
188                 "%s"
189                 "</td></tr>\n",
190                 (bar ? "even" : "odd"),
191                 (val ? "CHECKED" : ""),_("Yes"),
192                 (!val ? "CHECKED" : ""),_("No"),
193                 _("Summary"),
194                 _("Your summary page")
195         );
196
197         bar = 1 - bar;
198         val = IconbarIsEnabled("ib_inbox", 1);
199         wprintf("<tr class=\"%s\"><td>"
200                 "<input type=\"radio\" name=\"ib_inbox\" value=\"yes\" %s> %s &nbsp;&nbsp;&nbsp;"
201                 "<input type=\"radio\" name=\"ib_inbox\" value=\"no\" %s> %s <br />"
202                 "</td><td>"
203                 "<img src=\"static/privatemess_48x.gif\" alt=\"&nbsp;\">"
204                 "</td><td>"
205                 "<b>%s</b><br />"
206                 "%s"
207                 "</td></tr>\n",
208                 (bar ? "even" : "odd"),
209                 (val ? "CHECKED" : ""),_("Yes"),
210                 (!val ? "CHECKED" : ""),_("No"),
211                 _("Mail (inbox)"),
212                 _("A shortcut to your email Inbox")
213         );
214
215         bar = 1 - bar;
216         val = IconbarIsEnabled("ib_contacts", 1);
217         wprintf("<tr class=\"%s\"><td>"
218                 "<input type=\"radio\" name=\"ib_contacts\" value=\"yes\" %s> %s &nbsp;&nbsp;&nbsp;"
219                 "<input type=\"radio\" name=\"ib_contacts\" value=\"no\" %s> %s <br />"
220                 "</td><td>"
221                 "<img src=\"static/viewcontacts_48x.gif\" alt=\"&nbsp;\">"
222                 "</td><td>"
223                 "<b>%s</b><br />"
224                 "%s"
225                 "</td></tr>\n",
226                 (bar ? "even" : "odd"),
227                 (val ? "CHECKED" : ""),_("Yes"),
228                 (!val ? "CHECKED" : ""),_("No"),
229                 _("Contacts"),
230                 _("Your personal address book")
231         );
232
233         bar = 1 - bar;
234         val = IconbarIsEnabled("ib_notes", 1);
235         wprintf("<tr class=\"%s\"><td>"
236                 "<input type=\"radio\" name=\"ib_notes\" value=\"yes\" %s> %s &nbsp;&nbsp;&nbsp;"
237                 "<input type=\"radio\" name=\"ib_notes\" value=\"no\" %s> %s <br />"
238                 "</td><td>"
239                 "<img src=\"static/storenotes_48x.gif\" alt=\"&nbsp;\">"
240                 "</td><td>"
241                 "<b>%s</b><br />"
242                 "%s"
243                 "</td></tr>\n",
244                 (bar ? "even" : "odd"),
245                 (val ? "CHECKED" : ""),_("Yes"),
246                 (!val ? "CHECKED" : ""),_("No"),
247                 _("Notes"),
248                 _("Your personal notes")
249         );
250
251         bar = 1 - bar;
252         val = IconbarIsEnabled("ib_calendar", 1);
253         wprintf("<tr class=\"%s\"><td>"
254                 "<input type=\"radio\" name=\"ib_calendar\" value=\"yes\" %s> %s &nbsp;&nbsp;&nbsp;"
255                 "<input type=\"radio\" name=\"ib_calendar\" value=\"no\" %s> %s <br />"
256                 "</td><td>"
257                 "<img src=\"static/calarea_48x.gif\" alt=\"&nbsp;\">"
258                 "</td><td>"
259                 "<b>%s</b><br />"
260                 "%s"
261                 "</td></tr>\n",
262                 (bar ? "even" : "odd"),
263                 (val ? "CHECKED" : ""),_("Yes"),
264                 (!val ? "CHECKED" : ""),_("No"),
265                 _("Calendar"),
266                 _("A shortcut to your personal calendar")
267         );
268
269         bar = 1 - bar;
270         val = IconbarIsEnabled("ib_tasks", 1);
271         wprintf("<tr class=\"%s\"><td>"
272                 "<input type=\"radio\" name=\"ib_tasks\" value=\"yes\" %s> %s &nbsp;&nbsp;&nbsp;"
273                 "<input type=\"radio\" name=\"ib_tasks\" value=\"no\" %s> %s <br />"
274                 "</td><td>"
275                 "<img src=\"static/taskmanag_48x.gif\" alt=\"&nbsp;\">"
276                 "</td><td>"
277                 "<b>%s</b><br />"
278                 "%s"
279                 "</td></tr>\n",
280                 (bar ? "even" : "odd"),
281                 (val ? "CHECKED" : ""),_("Yes"),
282                 (!val ? "CHECKED" : ""),_("No"),
283                 _("Tasks"),
284                 _("A shortcut to your personal task list")
285         );
286
287         bar = 1 - bar;
288         val = IconbarIsEnabled("ib_rooms", 1);
289         wprintf("<tr class=\"%s\"><td>"
290                 "<input type=\"radio\" name=\"ib_rooms\" value=\"yes\" %s> %s &nbsp;&nbsp;&nbsp;"
291                 "<input type=\"radio\" name=\"ib_rooms\" value=\"no\" %s> %s <br />"
292                 "</td><td>"
293                 "<img src=\"static/chatrooms_48x.gif\" alt=\"&nbsp;\">"
294                 "</td><td>"
295                 "<b>%s</b><br />"
296                 "%s"
297                 "</td></tr>\n",
298                 (bar ? "even" : "odd"),
299                 (val ? "CHECKED" : ""),_("Yes"),
300                 (!val ? "CHECKED" : ""),_("No"),
301                 _("Rooms"),
302                 _("Clicking this icon displays a list of all accessible "
303                 "rooms (or folders) available.")
304         );
305
306         bar = 1 - bar;
307         val = IconbarIsEnabled("ib_users", 1);
308         wprintf("<tr class=\"%s\"><td>"
309                 "<input type=\"radio\" name=\"ib_users\" value=\"yes\" %s> %s &nbsp;&nbsp;&nbsp;"
310                 "<input type=\"radio\" name=\"ib_users\" value=\"no\" %s> %s <br />"
311                 "<input type=\"radio\" name=\"ib_users\" value=\"yeslist\" %s> %s"
312                 "</td><td>"
313                 "<img src=\"static/usermanag_48x.gif\" alt=\"&nbsp;\">"
314                 "</td><td>"
315                 "<b>%s</b>"
316                 "<br />%s"
317                 "</td></tr>\n",
318                 (bar ? "even" : "odd"),
319                 (val ? "CHECKED" : ""),_("Yes"),
320                 (!val ? "CHECKED" : ""),_("No"),
321                 ((val > 1) ? "CHECKED" : ""),_("Yes with users list"),
322                 _("Who is online?"),
323                 _("Clicking this icon displays a list of all users "
324                 "currently logged in.")
325         );
326
327         bar = 1 - bar;
328         val = IconbarIsEnabled("ib_chat", 1);
329         wprintf("<tr class=\"%s\"><td>"
330                 "<input type=\"radio\" name=\"ib_chat\" value=\"yes\" %s> %s &nbsp;&nbsp;&nbsp;"
331                 "<input type=\"radio\" name=\"ib_chat\" value=\"no\" %s> %s <br />"
332                 "</td><td>"
333                 "<img src=\"static/citadelchat_48x.gif\" alt=\"&nbsp;\">"
334                 "</td><td>"
335                 "<b>%s</b><br />"
336                 "%s"
337                 "</td></tr>\n",
338                 (bar ? "even" : "odd"),
339                 (val ? "CHECKED" : ""),_("Yes"),
340                 (!val ? "CHECKED" : ""),_("No"),
341                 _("Chat"),
342                 _("Clicking this icon enters real-time chat mode "
343                 "with other users in the same room.")
344                 
345         );
346
347         bar = 1 - bar;
348         val = IconbarIsEnabled("ib_advanced", 1);
349         wprintf("<tr class=\"%s\"><td>"
350                 "<input type=\"radio\" name=\"ib_advanced\" value=\"yes\" %s> %s &nbsp;&nbsp;&nbsp;"
351                 "<input type=\"radio\" name=\"ib_advanced\" value=\"no\" %s> %s <br />"
352                 "</td><td>"
353                 "<img src=\"static/advanpage2_48x.gif\" alt=\"&nbsp;\">"
354                 "</td><td>"
355                 "<b>%s</b><br />"
356                 "%s"
357                 "</td></tr>\n",
358                 (bar ? "even" : "odd"),
359                 (val ? "CHECKED" : ""),_("Yes"),
360                 (!val ? "CHECKED" : ""),_("No"),
361                 _("Advanced options"),
362                 _("Access to the complete menu of Citadel functions.")
363
364         );
365
366         bar = 1 - bar;
367         val = IconbarIsEnabled("ib_citadel", 1);
368         wprintf("<tr class=\"%s\"><td>"
369                 "<input type=\"radio\" name=\"ib_citadel\" value=\"yes\" %s> %s &nbsp;&nbsp;&nbsp;"
370                 "<input type=\"radio\" name=\"ib_citadel\" value=\"no\" %s> %s <br />"
371                 "</td><td>"
372                 "<img border=\"0\" width=\"48\" height=\"48\" "
373                 "src=\"static/citadel-logo.gif\" alt=\"&nbsp;\">"
374                 "</td><td>"
375                 "<b>%s</b><br />"
376                 "%s"
377                 "</td></tr>\n",
378                 (bar ? "even" : "odd"),
379                 (val ? "CHECKED" : ""),_("Yes"),
380                 (!val ? "CHECKED" : ""),_("No"),
381                 _("Citadel logo"),
382                 _("Displays the 'Powered by Citadel' icon")
383         );
384
385         wprintf("</table><br />\n"
386                 "<center>"
387                 "<input type=\"submit\" name=\"ok_button\" value=\"%s\">"
388                 "&nbsp;"
389                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">"
390                 "</center>\n",
391                 _("Save changes"),
392                 _("Cancel")
393         );
394
395         wprintf("</form></div>\n");
396         wDumpContent(2);
397 }
398
399 /*
400  * save changes to iconbar settings
401  */
402 void commit_iconbar(void) {
403         StrBuf *iconbar;
404         StrBuf *buf;
405         int i;
406
407         char *boxen[] = {
408                 "ib_logo",
409                 "ib_summary",
410                 "ib_inbox",
411                 "ib_calendar",
412                 "ib_contacts",
413                 "ib_notes",
414                 "ib_tasks",
415                 "ib_rooms",
416                 "ib_users",
417                 "ib_chat",
418                 "ib_advanced",
419                 "ib_logoff",
420                 "ib_citadel"
421         };
422
423         if (!havebstr("ok_button")) {
424                 display_main_menu();
425                 return;
426         }
427
428         iconbar = NewStrBuf();
429         buf = NewStrBuf();
430         StrBufPrintf(iconbar, "ib_displayas=%d", ibstr("ib_displayas"));
431         for (i=0; i<(sizeof(boxen)/sizeof(char *)); ++i) {
432                 char *Val;
433                 if (!strcasecmp(BSTR(boxen[i]), "yes")) {
434                         Val = "1";
435                 }
436                 else if (!strcasecmp(BSTR(boxen[i]), "yeslist")) {
437                         Val = "2";
438                 }
439                 else {
440                         Val = "0";
441                 }
442                 StrBufPrintf(buf, ",%s=%s", boxen[i], Val);
443                 StrBufAppendBuf(iconbar, buf, 0);
444
445         }
446         FreeStrBuf(&buf);
447         set_preference("iconbar", iconbar, 1);
448
449         output_headers(1, 1, 2, 0, 0, 0);
450         /* TODO: TEMPLATE */
451         wprintf("<div id=\"banner\">\n");
452         wprintf("<h1>");
453         wprintf(_("Customize the icon bar"));
454         wprintf("</h1></div>\n");
455
456         wprintf("<div id=\"content\" class=\"service\">\n");
457         wprintf(
458                 "<center><table border=1 bgcolor=\"#ffffff\"><tr><td>"
459                 "<img src=\"static/advanpage2_48x.gif\">"
460                 "&nbsp;");
461         wprintf(_("Your icon bar has been updated.  Please select any of its "
462                   "choices to continue.<br/><span style=\"font-weight: bold;\">You may need to force refresh (SHIFT-F5) in order for changes to take effect</span>"));
463         wprintf("</td></tr></table>\n");
464         wDumpContent(2);
465 #ifdef DBG_ICONBAR_HASH
466         dbg_PrintHash(WC->IconBarSetttings, PrintInt, NULL);
467 #endif
468 }
469
470
471 void tmplput_iconbar(StrBuf *Target, WCTemplputParams *TP)
472 {
473         wcsession *WCC = WC;
474         
475         if ((WCC != NULL) && (WCC->logged_in)) {
476           DoTemplate(HKEY("iconbar"), NULL, &NoCtx);
477         }
478 }
479
480 void 
481 InitModule_ICONBAR
482 (void)
483 {
484         WebcitAddUrlHandler(HKEY("user_iconbar"), doUserIconStylesheet, 0);
485         WebcitAddUrlHandler(HKEY("commit_iconbar"), commit_iconbar, 0);
486         RegisterConditional(HKEY("COND:ICONBAR:ACTIVE"), 3, ConditionalIsActiveStylesheet, CTX_NONE);
487         WebcitAddUrlHandler(HKEY("display_customize_iconbar"), display_customize_iconbar, 0);
488         RegisterNamespace("ICONBAR", 0, 0, tmplput_iconbar, 0);
489
490         RegisterPreference("iconbar", _("Iconbar Setting"), PRF_STRING, LoadIconSettings);
491 }
492
493
494
495 void 
496 SessionDestroyModule_ICONBAR
497 (wcsession *sess)
498 {
499         DeleteHash(&sess->IconBarSettings);
500 }