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