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