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