* make the iconbar css a template
[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
19 HashList *IB_Seeting_Order = NULL;
20 typedef struct _dflt_IB_Setting {
21         int         DefVal;  /* default value for non-set users */
22         long        n;       /* counter for internal purposes   */
23         const char *Key;     /* Stringvalue */
24         long        len;     /* Length... */
25 }dflt_IB_Setting;
26
27 long nIBV = 0;
28 dflt_IB_Setting IconbarDefaults[] = {
29         {0,  0, HKEY("unused")},
30         {0,  1, HKEY("ib_displayas")},
31         {0,  2, HKEY("ib_logo")},
32         {1,  3, HKEY("ib_summary")},
33         {1,  4, HKEY("ib_inbox")},
34         {1,  5, HKEY("ib_calendar")},
35         {1,  6, HKEY("ib_contacts")},
36         {1,  7, HKEY("ib_notes")},
37         {1,  8, HKEY("ib_tasks")},
38         {1,  9, HKEY("ib_rooms")},
39         {1, 10, HKEY("ib_users")},
40         {1, 11, HKEY("ib_chat")},
41         {1, 12, HKEY("ib_advanced")},
42         {1, 13, HKEY("ib_logoff")},
43         {1, 14, HKEY("ib_citadel")},
44         {0, 15, HKEY("")}
45 };
46
47 HashList *IBDfl = NULL;
48
49
50 long IconbarIsENABLED(long val, const char *key, size_t keylen)
51 {
52         void *vIBDfl = NULL;
53         wcsession *WCC = WC;
54
55         if ((WCC != NULL) && 
56             (WCC->IBSettingsVec != NULL) && 
57             (val < nIBV))
58         {
59                 return WCC->IBSettingsVec[val];
60         }
61         if (GetHash(IBDfl, key, keylen, &vIBDfl)) {
62                 dflt_IB_Setting *Set = (dflt_IB_Setting*)vIBDfl;
63                 return Set->DefVal;
64         }
65         else 
66                 return 1;
67 }
68
69 #ifdef DBG_ICONBAR_HASH
70 static char nbuf[32];
71 inline const char *PrintInt(void *Prefstr)
72 {
73         snprintf(nbuf, sizeof(nbuf), "%ld", (long)Prefstr);
74         return nbuf;
75 }
76 #endif
77
78 /* Produces a stylesheet which hides any iconbar icons the user does not want * /
79 void doUserIconStylesheet(void) {
80         HashPos *pos;
81         void *Data;
82         long value;
83         const char *key;
84         long HKLen;
85         
86         output_custom_content_header("text/css");
87         hprintf("Cache-Control: private\r\n");
88         
89         begin_burst();
90         wc_printf("#global { left: 16%%; }\r\n");
91         pos = GetNewHashPos(WC->IconBarSettings, 0);
92         while(GetNextHashPos(WC->IconBarSettings, pos, &HKLen, &key, &Data)) {
93                 value = (long) Data;
94                 if (value == 0 
95                     && strncasecmp("ib_displayas",key,12) 
96                     && strncasecmp("ib_logoff", key, 9)) {
97                         / * Don't shoot me for this * /
98                         wc_printf("#%s { display: none !important; }\r\n",key);
99                 } else if (!strncasecmp("ib_users",key, 8) && value == 2) {
100                         wc_printf("#online_users { display: block; !important } \r\n");
101                 }
102         }
103         DeleteHashPos(&pos);
104         end_burst();
105 }
106
107 void doUserIconStylesheet(void) {
108         HashPos *pos;
109         void *Data;
110         long value;
111         const char *key;
112         long HKLen;
113         
114 //      output_custom_content_header("text/css");
115         const StrBuf *MimeType;
116         begin_burst();
117         MimeType = DoTemplate(HKEY("user_iconbar"), NULL, &NoCtx);
118         http_transmit_thing(ChrPtr(MimeType), 0);
119         hprintf("Cache-Control: private\r\n");
120         
121         end_burst();
122 }
123
124 */
125
126
127 int ConditionalIsActiveStylesheet(StrBuf *Target, WCTemplputParams *TP) {
128         long testFor;
129         long lookAt;
130         long ib_displayas;
131
132         lookAt = GetTemplateTokenNumber(Target, TP, 3, IB_PICTEXT);
133         testFor = GetTemplateTokenNumber(Target, TP, 2, IB_PICTEXT);
134
135
136
137         ib_displayas = IconbarIsENABLED(lookAt, TKEY(3));
138
139         printf ("%ld == %ld ? %s : %s\n", 
140                 testFor, 
141                 ib_displayas, 
142                 IconbarDefaults[lookAt ].Key, 
143                 ChrPtr(TP->Tokens->FlatToken));
144
145
146         return (testFor == ib_displayas);
147 }
148
149 void LoadIconSettings(StrBuf *iconbar, long lvalue)
150 {
151         void *vIBDfl;
152         dflt_IB_Setting *Set;
153         const char *pCh = NULL;
154
155         wcsession *WCC = WC;
156         StrBuf *buf;
157         StrBuf *key;
158         long val;
159
160         buf = NewStrBuf();
161         key = NewStrBuf();
162         if (WCC->IBSettingsVec == NULL)
163         {
164                 WCC->IBSettingsVec = (long*) malloc (nIBV * sizeof(long));
165         }
166         /**
167          * The initialized values of these variables also happen to
168          * specify the default values for users who haven't customized
169          * their iconbars.  These should probably be set in a master
170          * configuration somewhere.
171          */
172
173         while (StrBufExtract_NextToken(buf, iconbar, &pCh,  ',') >= 0)
174         {
175                 StrBufExtract_token(key, buf, 0, '=');
176                 val = StrBufExtract_long(buf, 1, '=');
177
178                 if (!GetHash(IBDfl, SKEY(key), &vIBDfl)) 
179                         continue;
180                 Set = (dflt_IB_Setting*)vIBDfl;
181
182                 WCC->IBSettingsVec[Set->n] = val;
183                 printf("%ld %s %s -> %ld \n", Set->n, Set->Key, IconbarDefaults[Set->n].Key, val);
184         }
185 #ifdef DBG_ICONBAR_HASH
186         dbg_PrintHash(WCC->IconBarSetttings, PrintInt, NULL);
187 #endif
188
189         FreeStrBuf(&key);
190         FreeStrBuf(&buf);
191 }
192
193
194 /*
195  * save changes to iconbar settings
196  */
197 void commit_iconbar(void) {
198         const StrBuf *MimeType;
199         StrBuf *iconbar;
200         StrBuf *buf;
201         int i;
202
203
204         if (!havebstr("ok_button")) {
205                 display_main_menu();
206                 return;
207         }
208
209         iconbar = NewStrBuf();
210         buf = NewStrBuf();
211         StrBufPrintf(iconbar, "ib_displayas=%d", ibstr("ib_displayas"));
212         for (i=0; i<(sizeof(IconbarDefaults)/sizeof(dflt_IB_Setting )); ++i) {
213                 char *Val;
214                 if (!strcasecmp(Bstr(IconbarDefaults[i].Key,
215                                      IconbarDefaults[i].len),
216                                 "yes")) 
217                 {
218                         Val = "1";
219                 }
220                 else if (!strcasecmp(Bstr(IconbarDefaults[i].Key,
221                                           IconbarDefaults[i].len),
222                                      "yeslist")) 
223                 {
224                         Val = "2";
225                 }
226                 else {
227                         Val = "0";
228                 }
229                 StrBufPrintf(buf, ",%s=%s", IconbarDefaults[i].Key, Val);
230                 StrBufAppendBuf(iconbar, buf, 0);
231
232         }
233         FreeStrBuf(&buf);
234         set_preference("iconbar", iconbar, 1);
235
236
237         begin_burst();
238         MimeType = DoTemplate(HKEY("iconbar_save"), NULL, &NoCtx);
239         http_transmit_thing(ChrPtr(MimeType), 0);
240 #ifdef DBG_ICONBAR_HASH
241         dbg_PrintHash(WC->IconBarSetttings, PrintInt, NULL);
242 #endif
243 }
244
245
246 void tmplput_iconbar(StrBuf *Target, WCTemplputParams *TP)
247 {
248         wcsession *WCC = WC;
249         
250         if ((WCC != NULL) && (WCC->logged_in)) {
251           DoTemplate(HKEY("iconbar"), NULL, &NoCtx);
252         }
253 }
254
255
256 void 
257 ServerShutdownModule_ICONBAR
258 (void)
259 {
260         DeleteHash(&IBDfl);
261 }
262
263
264
265 void
266 ServerStartModule_ICONBAR
267 (void)
268 {
269         int i = 1;
270         IBDfl = NewHash(1, NULL);
271
272         while (IconbarDefaults[i].len != 0)
273         {
274                 Put(IBDfl, 
275                     IconbarDefaults[i].Key, 
276                     IconbarDefaults[i].len, 
277                     &IconbarDefaults[i], 
278                     reference_free_handler);
279                 i++;
280         }
281 }
282
283 void 
284 InitModule_ICONBAR
285 (void)
286 {
287         long l;
288
289         /*WebcitAddUrlHandler(HKEY("user_iconbar"), "", 0, doUserIconStylesheet, 0); */
290         WebcitAddUrlHandler(HKEY("commit_iconbar"), "", 0, commit_iconbar, 0);
291         RegisterConditional(HKEY("COND:ICONBAR:ACTIVE"), 3, ConditionalIsActiveStylesheet, CTX_NONE);
292         RegisterNamespace("ICONBAR", 0, 0, tmplput_iconbar, NULL, CTX_NONE);
293
294         RegisterPreference("iconbar", _("Iconbar Setting"), PRF_STRING, LoadIconSettings);
295         l = 1;
296         while (IconbarDefaults[l].len != 0)
297         {
298                 RegisterTokenParamDefine(IconbarDefaults[l].Key, 
299                                          IconbarDefaults[l].len, l);
300                 l ++;
301         }
302         nIBV = l;
303 }
304
305
306
307 void 
308 SessionDestroyModule_ICONBAR
309 (wcsession *sess)
310 {
311         if (sess->IBSettingsVec != NULL)
312                 free(sess->IBSettingsVec);
313 }