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