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