53dead7a2bbe9d5b2d796a082f83a64fb60a6171
[citadel.git] / webcit / iconbar.c
1 /*
2  * Displays and customizes the iconbar.
3  *
4  * Copyright (c) 1996-2012 by the citadel.org team
5  *
6  * This program is open source software.  You can redistribute it and/or
7  * modify it under the terms of the GNU General Public License, version 3.
8  * 
9  * 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * 
17  * 
18  * 
19  */
20
21 #include "webcit.h"
22
23 /* Values for ib_displayas ... don't change these or you will break the templates */
24 #define IB_PICTEXT      0       /* picture and text */
25 #define IB_PICONLY      1       /* just a picture */
26 #define IB_TEXTONLY     2       /* just text */
27
28 void DontDeleteThis(void *Data){}
29
30 #define IconbarIsEnabled(a, b) IconbarIsENABLED(a, sizeof(a) - 1, b)
31
32
33 HashList *IB_Seeting_Order = NULL;
34 typedef struct _dflt_IB_Setting {
35         int         DefVal;  /* default value for non-set users */
36         long        n;       /* counter for internal purposes   */
37         const char *Key;     /* Stringvalue */
38         long        len;     /* Length... */
39 }dflt_IB_Setting;
40
41 long nIBV = 0;
42 dflt_IB_Setting IconbarDefaults[] = {
43         {0,  0, HKEY("unused")},
44         {0,  1, HKEY("ib_displayas")},
45         {0,  2, HKEY("ib_logo")},
46         {1,  3, HKEY("ib_summary")},
47         {1,  4, HKEY("ib_inbox")},
48         {1,  5, HKEY("ib_calendar")},
49         {1,  6, HKEY("ib_contacts")},
50         {1,  7, HKEY("ib_notes")},
51         {1,  8, HKEY("ib_tasks")},
52         {1,  9, HKEY("ib_rooms")},
53         {1, 10, HKEY("ib_users")},
54         {1, 11, HKEY("ib_chat")},
55         {1, 12, HKEY("ib_advanced")},
56         {1, 13, HKEY("ib_logoff")},
57         {1, 14, HKEY("ib_citadel")},
58         {0, 15, HKEY("")}
59 };
60
61 HashList *IBDfl = NULL;
62
63
64 long IconbarIsENABLED(long val, const char *key, size_t keylen)
65 {
66         void *vIBDfl = NULL;
67         wcsession *WCC = WC;
68
69         if ((WCC != NULL) && 
70             (WCC->IBSettingsVec != NULL) && 
71             (val < nIBV))
72         {
73                 return WCC->IBSettingsVec[val];
74         }
75         if (GetHash(IBDfl, key, keylen, &vIBDfl)) {
76                 dflt_IB_Setting *Set = (dflt_IB_Setting*)vIBDfl;
77                 return Set->DefVal;
78         }
79         else 
80                 return 1;
81 }
82
83 #ifdef DBG_ICONBAR_HASH
84 static char nbuf[32];
85 inline const char *PrintInt(void *Prefstr)
86 {
87         snprintf(nbuf, sizeof(nbuf), "%ld", (long)Prefstr);
88         return nbuf;
89 }
90 #endif
91
92 /* 
93         hprintf("Cache-Control: private\r\n");
94 */
95
96
97 int ConditionalIsActiveStylesheet(StrBuf *Target, WCTemplputParams *TP) {
98         long testFor;
99         long lookAt;
100         long ib_displayas;
101
102         lookAt = GetTemplateTokenNumber(Target, TP, 3, IB_PICTEXT);
103         testFor = GetTemplateTokenNumber(Target, TP, 2, IB_PICTEXT);
104
105
106
107         ib_displayas = IconbarIsENABLED(lookAt, TKEY(3));
108 /*
109         printf ("%ld == %ld ? %s : %s\n", 
110                 testFor, 
111                 ib_displayas, 
112                 IconbarDefaults[lookAt ].Key, 
113                 ChrPtr(TP->Tokens->FlatToken));
114 */
115
116         return (testFor == ib_displayas);
117 }
118
119 void LoadIconSettings(StrBuf *iconbar, long lvalue)
120 {
121         void *vIBDfl;
122         dflt_IB_Setting *Set;
123         const char *pCh = NULL;
124
125         wcsession *WCC = WC;
126         StrBuf *buf;
127         StrBuf *key;
128         long val;
129
130         buf = NewStrBuf();
131         key = NewStrBuf();
132         if (WCC->IBSettingsVec == NULL)
133         {
134                 WCC->IBSettingsVec = (long*) malloc (nIBV * sizeof(long));
135         }
136         /*
137          * The initialized values of these variables also happen to
138          * specify the default values for users who haven't customized
139          * their iconbars.  These should probably be set in a master
140          * configuration somewhere.
141          */
142
143         while (StrBufExtract_NextToken(buf, iconbar, &pCh,  ',') >= 0)
144         {
145                 StrBufExtract_token(key, buf, 0, '=');
146                 val = StrBufExtract_long(buf, 1, '=');
147
148                 if (!GetHash(IBDfl, SKEY(key), &vIBDfl)) 
149                         continue;
150                 Set = (dflt_IB_Setting*)vIBDfl;
151
152                 WCC->IBSettingsVec[Set->n] = val;
153 /*              printf("%ld %s %s -> %ld \n", Set->n, Set->Key, IconbarDefaults[Set->n].Key, val);*/
154         }
155 #ifdef DBG_ICONBAR_HASH
156         dbg_PrintHash(WCC->IconBarSetttings, PrintInt, NULL);
157 #endif
158
159         FreeStrBuf(&key);
160         FreeStrBuf(&buf);
161 }
162
163
164 /*
165  * save changes to iconbar settings
166  */
167 void commit_iconbar(void) {
168         const StrBuf *MimeType;
169         StrBuf *iconbar;
170         StrBuf *buf;
171         int i;
172
173
174         if (!havebstr("ok_button")) {
175                 display_main_menu();
176                 return;
177         }
178
179         iconbar = NewStrBuf();
180         buf = NewStrBuf();
181         StrBufPrintf(iconbar, "ib_displayas=%d", ibstr("ib_displayas"));
182         for (i=0; i<(sizeof(IconbarDefaults)/sizeof(dflt_IB_Setting )); ++i) {
183                 char *Val;
184                 if (!strcasecmp(Bstr(IconbarDefaults[i].Key,
185                                      IconbarDefaults[i].len),
186                                 "yes")) 
187                 {
188                         Val = "1";
189                 }
190                 else if (!strcasecmp(Bstr(IconbarDefaults[i].Key,
191                                           IconbarDefaults[i].len),
192                                      "yeslist")) 
193                 {
194                         Val = "2";
195                 }
196                 else {
197                         Val = "0";
198                 }
199                 StrBufPrintf(buf, ",%s=%s", IconbarDefaults[i].Key, Val);
200                 StrBufAppendBuf(iconbar, buf, 0);
201
202         }
203         FreeStrBuf(&buf);
204         set_preference("iconbar", iconbar, 1);
205
206
207         begin_burst();
208         MimeType = DoTemplate(HKEY("iconbar_save"), NULL, &NoCtx);
209         http_transmit_thing(ChrPtr(MimeType), 0);
210 #ifdef DBG_ICONBAR_HASH
211         dbg_PrintHash(WC->IconBarSetttings, PrintInt, NULL);
212 #endif
213 }
214
215
216 /*
217  * Display the icon bar as long as we have an active session,
218  * and either the user is logged in or the server allows guest mode.
219  */
220 void tmplput_iconbar(StrBuf *Target, WCTemplputParams *TP)
221 {
222         wcsession *WCC = WC;
223         
224          if (   (WCC != NULL)
225                 && (    (WCC->logged_in)
226                         || (WCC->serv_info->serv_supports_guest)
227                 )
228         ) {
229                 DoTemplate(HKEY("iconbar"), NULL, &NoCtx);
230         }
231 }
232
233
234 void 
235 ServerShutdownModule_ICONBAR
236 (void)
237 {
238         DeleteHash(&IBDfl);
239 }
240
241
242
243 void
244 ServerStartModule_ICONBAR
245 (void)
246 {
247         int i = 1;
248         IBDfl = NewHash(1, NULL);
249
250         while (IconbarDefaults[i].len != 0)
251         {
252                 Put(IBDfl, 
253                     IconbarDefaults[i].Key, 
254                     IconbarDefaults[i].len, 
255                     &IconbarDefaults[i], 
256                     reference_free_handler);
257                 i++;
258         }
259 }
260
261
262 int ConditionalWholistExpanded(StrBuf *Target, WCTemplputParams *TP)
263 {
264         int r = 0;
265         if (WC) r = WC->ib_wholist_expanded;
266         syslog(LOG_DEBUG, "ConditionalWholistExpanded() returns %d", r);
267         return(r);
268 }
269
270
271 int ConditionalRoomlistExpanded(StrBuf *Target, WCTemplputParams *TP)
272 {
273         if (WC) return(WC->ib_roomlist_expanded);
274         return(0);
275 }
276
277
278
279 /*
280  * Toggle the roomlist expanded state in session memory
281  */
282 void toggle_roomlist_expanded_state(void) {
283         wcsession *WCC = WC;
284
285         if (!WCC) {
286                 wc_printf("no session");
287                 return;
288         }
289
290         WCC->ib_roomlist_expanded = IBSTR("wstate");
291         wc_printf("%d", WCC->ib_roomlist_expanded);
292         syslog(LOG_DEBUG, "ib_roomlist_expanded set to %d", WCC->ib_roomlist_expanded);
293 }
294
295
296 /*
297  * Toggle the wholist expanded state in session memory
298  */
299 void toggle_wholist_expanded_state(void) {
300         wcsession *WCC = WC;
301
302         if (!WCC) {
303                 wc_printf("no session");
304                 return;
305         }
306
307         WCC->ib_wholist_expanded = IBSTR("wstate");
308         wc_printf("%d", WCC->ib_wholist_expanded);
309         syslog(LOG_DEBUG, "ib_wholist_expanded set to %d", WCC->ib_wholist_expanded);
310 }
311
312
313 void 
314 InitModule_ICONBAR
315 (void)
316 {
317         long l;
318
319         /*WebcitAddUrlHandler(HKEY("user_iconbar"), "", 0, doUserIconStylesheet, 0); */
320         WebcitAddUrlHandler(HKEY("commit_iconbar"), "", 0, commit_iconbar, 0);
321         WebcitAddUrlHandler(HKEY("toggle_wholist_expanded_state"), "", 0, toggle_wholist_expanded_state, AJAX);
322         WebcitAddUrlHandler(HKEY("toggle_roomlist_expanded_state"), "", 0, toggle_roomlist_expanded_state, AJAX);
323         RegisterConditional(HKEY("COND:ICONBAR:ACTIVE"), 3, ConditionalIsActiveStylesheet, CTX_NONE);
324         RegisterNamespace("ICONBAR", 0, 0, tmplput_iconbar, NULL, CTX_NONE);
325         RegisterConditional(HKEY("COND:ICONBAR:WHOLISTEXPANDED"), 0, ConditionalWholistExpanded, CTX_NONE);
326         RegisterConditional(HKEY("COND:ICONBAR:ROOMLISTEXPANDED"), 0, ConditionalRoomlistExpanded, CTX_NONE);
327
328         RegisterPreference("iconbar", _("Iconbar Setting"), PRF_STRING, LoadIconSettings);
329         l = 1;
330         while (IconbarDefaults[l].len != 0)
331         {
332                 RegisterTokenParamDefine(IconbarDefaults[l].Key, 
333                                          IconbarDefaults[l].len, l);
334                 l ++;
335         }
336         nIBV = l;
337 }
338
339
340
341 void 
342 SessionDestroyModule_ICONBAR
343 (wcsession *sess)
344 {
345         if (sess->IBSettingsVec != NULL)
346                 free(sess->IBSettingsVec);
347 }
348